(gnus-blocked-images): Clarify privacy implications
[emacs.git] / src / eval.c
blob90d8c3351855de7d9f83caddc407850de4126dba
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2018 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 CHECK_SYMBOL (sym);
742 if (!NILP (tail))
744 if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail))))
745 error ("Too many arguments");
747 tem = Fdefault_boundp (sym);
749 /* Do it before evaluating the initial value, for self-references. */
750 XSYMBOL (sym)->u.s.declared_special = true;
752 if (NILP (tem))
753 Fset_default (sym, eval_sub (XCAR (tail)));
754 else
755 { /* Check if there is really a global binding rather than just a let
756 binding that shadows the global unboundness of the var. */
757 union specbinding *binding = default_toplevel_binding (sym);
758 if (binding && EQ (specpdl_old_value (binding), Qunbound))
760 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
763 tail = XCDR (tail);
764 tem = Fcar (tail);
765 if (!NILP (tem))
767 if (!NILP (Vpurify_flag))
768 tem = Fpurecopy (tem);
769 Fput (sym, Qvariable_documentation, tem);
771 LOADHIST_ATTACH (sym);
773 else if (!NILP (Vinternal_interpreter_environment)
774 && !XSYMBOL (sym)->u.s.declared_special)
775 /* A simple (defvar foo) with lexical scoping does "nothing" except
776 declare that var to be dynamically scoped *locally* (i.e. within
777 the current file or let-block). */
778 Vinternal_interpreter_environment
779 = Fcons (sym, Vinternal_interpreter_environment);
780 else
782 /* Simple (defvar <var>) should not count as a definition at all.
783 It could get in the way of other definitions, and unloading this
784 package could try to make the variable unbound. */
787 return sym;
790 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
791 doc: /* Define SYMBOL as a constant variable.
792 This declares that neither programs nor users should ever change the
793 value. This constancy is not actually enforced by Emacs Lisp, but
794 SYMBOL is marked as a special variable so that it is never lexically
795 bound.
797 The `defconst' form always sets the value of SYMBOL to the result of
798 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
799 what is set; buffer-local values are not affected. If SYMBOL has a
800 local binding, then this form sets the local binding's value.
801 However, you should normally not make local bindings for variables
802 defined with this form.
804 The optional DOCSTRING specifies the variable's documentation string.
805 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
806 (Lisp_Object args)
808 Lisp_Object sym, tem;
810 sym = XCAR (args);
811 Lisp_Object docstring = Qnil;
812 if (!NILP (XCDR (XCDR (args))))
814 if (!NILP (XCDR (XCDR (XCDR (args)))))
815 error ("Too many arguments");
816 docstring = XCAR (XCDR (XCDR (args)));
819 tem = eval_sub (XCAR (XCDR (args)));
820 if (!NILP (Vpurify_flag))
821 tem = Fpurecopy (tem);
822 Fset_default (sym, tem);
823 XSYMBOL (sym)->u.s.declared_special = true;
824 if (!NILP (docstring))
826 if (!NILP (Vpurify_flag))
827 docstring = Fpurecopy (docstring);
828 Fput (sym, Qvariable_documentation, docstring);
830 Fput (sym, Qrisky_local_variable, Qt);
831 LOADHIST_ATTACH (sym);
832 return sym;
835 /* Make SYMBOL lexically scoped. */
836 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
837 Smake_var_non_special, 1, 1, 0,
838 doc: /* Internal function. */)
839 (Lisp_Object symbol)
841 CHECK_SYMBOL (symbol);
842 XSYMBOL (symbol)->u.s.declared_special = false;
843 return Qnil;
847 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
848 doc: /* Bind variables according to VARLIST then eval BODY.
849 The value of the last form in BODY is returned.
850 Each element of VARLIST is a symbol (which is bound to nil)
851 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
852 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
853 usage: (let* VARLIST BODY...) */)
854 (Lisp_Object args)
856 Lisp_Object var, val, elt, lexenv;
857 ptrdiff_t count = SPECPDL_INDEX ();
859 lexenv = Vinternal_interpreter_environment;
861 Lisp_Object varlist = XCAR (args);
862 while (CONSP (varlist))
864 maybe_quit ();
866 elt = XCAR (varlist);
867 varlist = XCDR (varlist);
868 if (SYMBOLP (elt))
870 var = elt;
871 val = Qnil;
873 else
875 var = Fcar (elt);
876 if (! NILP (Fcdr (XCDR (elt))))
877 signal_error ("`let' bindings can have only one value-form", elt);
878 val = eval_sub (Fcar (XCDR (elt)));
881 if (!NILP (lexenv) && SYMBOLP (var)
882 && !XSYMBOL (var)->u.s.declared_special
883 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
884 /* Lexically bind VAR by adding it to the interpreter's binding
885 alist. */
887 Lisp_Object newenv
888 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
889 if (EQ (Vinternal_interpreter_environment, lexenv))
890 /* Save the old lexical environment on the specpdl stack,
891 but only for the first lexical binding, since we'll never
892 need to revert to one of the intermediate ones. */
893 specbind (Qinternal_interpreter_environment, newenv);
894 else
895 Vinternal_interpreter_environment = newenv;
897 else
898 specbind (var, val);
900 CHECK_LIST_END (varlist, XCAR (args));
902 val = Fprogn (XCDR (args));
903 return unbind_to (count, val);
906 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
907 doc: /* Bind variables according to VARLIST then eval BODY.
908 The value of the last form in BODY is returned.
909 Each element of VARLIST is a symbol (which is bound to nil)
910 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
911 All the VALUEFORMs are evalled before any symbols are bound.
912 usage: (let VARLIST BODY...) */)
913 (Lisp_Object args)
915 Lisp_Object *temps, tem, lexenv;
916 Lisp_Object elt, varlist;
917 ptrdiff_t count = SPECPDL_INDEX ();
918 ptrdiff_t argnum;
919 USE_SAFE_ALLOCA;
921 varlist = XCAR (args);
922 CHECK_LIST (varlist);
924 /* Make space to hold the values to give the bound variables. */
925 EMACS_INT varlist_len = XFASTINT (Flength (varlist));
926 SAFE_ALLOCA_LISP (temps, varlist_len);
927 ptrdiff_t nvars = varlist_len;
929 /* Compute the values and store them in `temps'. */
931 for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
933 maybe_quit ();
934 elt = XCAR (varlist);
935 varlist = XCDR (varlist);
936 if (SYMBOLP (elt))
937 temps[argnum] = Qnil;
938 else if (! NILP (Fcdr (Fcdr (elt))))
939 signal_error ("`let' bindings can have only one value-form", elt);
940 else
941 temps[argnum] = eval_sub (Fcar (Fcdr (elt)));
943 nvars = argnum;
945 lexenv = Vinternal_interpreter_environment;
947 varlist = XCAR (args);
948 for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
950 Lisp_Object var;
952 elt = XCAR (varlist);
953 varlist = XCDR (varlist);
954 var = SYMBOLP (elt) ? elt : Fcar (elt);
955 tem = temps[argnum];
957 if (!NILP (lexenv) && SYMBOLP (var)
958 && !XSYMBOL (var)->u.s.declared_special
959 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
960 /* Lexically bind VAR by adding it to the lexenv alist. */
961 lexenv = Fcons (Fcons (var, tem), lexenv);
962 else
963 /* Dynamically bind VAR. */
964 specbind (var, tem);
967 if (!EQ (lexenv, Vinternal_interpreter_environment))
968 /* Instantiate a new lexical environment. */
969 specbind (Qinternal_interpreter_environment, lexenv);
971 elt = Fprogn (XCDR (args));
972 SAFE_FREE ();
973 return unbind_to (count, elt);
976 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
977 doc: /* If TEST yields non-nil, eval BODY... and repeat.
978 The order of execution is thus TEST, BODY, TEST, BODY and so on
979 until TEST returns nil.
980 usage: (while TEST BODY...) */)
981 (Lisp_Object args)
983 Lisp_Object test, body;
985 test = XCAR (args);
986 body = XCDR (args);
987 while (!NILP (eval_sub (test)))
989 maybe_quit ();
990 prog_ignore (body);
993 return Qnil;
996 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
997 doc: /* Return result of expanding macros at top level of FORM.
998 If FORM is not a macro call, it is returned unchanged.
999 Otherwise, the macro is expanded and the expansion is considered
1000 in place of FORM. When a non-macro-call results, it is returned.
1002 The second optional arg ENVIRONMENT specifies an environment of macro
1003 definitions to shadow the loaded ones for use in file byte-compilation. */)
1004 (Lisp_Object form, Lisp_Object environment)
1006 /* With cleanups from Hallvard Furuseth. */
1007 register Lisp_Object expander, sym, def, tem;
1009 while (1)
1011 /* Come back here each time we expand a macro call,
1012 in case it expands into another macro call. */
1013 if (!CONSP (form))
1014 break;
1015 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1016 def = sym = XCAR (form);
1017 tem = Qnil;
1018 /* Trace symbols aliases to other symbols
1019 until we get a symbol that is not an alias. */
1020 while (SYMBOLP (def))
1022 maybe_quit ();
1023 sym = def;
1024 tem = Fassq (sym, environment);
1025 if (NILP (tem))
1027 def = XSYMBOL (sym)->u.s.function;
1028 if (!NILP (def))
1029 continue;
1031 break;
1033 /* Right now TEM is the result from SYM in ENVIRONMENT,
1034 and if TEM is nil then DEF is SYM's function definition. */
1035 if (NILP (tem))
1037 /* SYM is not mentioned in ENVIRONMENT.
1038 Look at its function definition. */
1039 def = Fautoload_do_load (def, sym, Qmacro);
1040 if (!CONSP (def))
1041 /* Not defined or definition not suitable. */
1042 break;
1043 if (!EQ (XCAR (def), Qmacro))
1044 break;
1045 else expander = XCDR (def);
1047 else
1049 expander = XCDR (tem);
1050 if (NILP (expander))
1051 break;
1054 Lisp_Object newform = apply1 (expander, XCDR (form));
1055 if (EQ (form, newform))
1056 break;
1057 else
1058 form = newform;
1061 return form;
1064 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1065 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1066 TAG is evalled to get the tag to use; it must not be nil.
1068 Then the BODY is executed.
1069 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1070 If no throw happens, `catch' returns the value of the last BODY form.
1071 If a throw happens, it specifies the value to return from `catch'.
1072 usage: (catch TAG BODY...) */)
1073 (Lisp_Object args)
1075 Lisp_Object tag = eval_sub (XCAR (args));
1076 return internal_catch (tag, Fprogn, XCDR (args));
1079 /* Assert that E is true, but do not evaluate E. Use this instead of
1080 eassert (E) when E contains variables that might be clobbered by a
1081 longjmp. */
1083 #define clobbered_eassert(E) verify (sizeof (E) != 0)
1085 /* Set up a catch, then call C function FUNC on argument ARG.
1086 FUNC should return a Lisp_Object.
1087 This is how catches are done from within C code. */
1089 Lisp_Object
1090 internal_catch (Lisp_Object tag,
1091 Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1093 /* This structure is made part of the chain `catchlist'. */
1094 struct handler *c = push_handler (tag, CATCHER);
1096 /* Call FUNC. */
1097 if (! sys_setjmp (c->jmp))
1099 Lisp_Object val = func (arg);
1100 eassert (handlerlist == c);
1101 handlerlist = c->next;
1102 return val;
1104 else
1105 { /* Throw works by a longjmp that comes right here. */
1106 Lisp_Object val = handlerlist->val;
1107 clobbered_eassert (handlerlist == c);
1108 handlerlist = handlerlist->next;
1109 return val;
1113 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1114 jump to that CATCH, returning VALUE as the value of that catch.
1116 This is the guts of Fthrow and Fsignal; they differ only in the way
1117 they choose the catch tag to throw to. A catch tag for a
1118 condition-case form has a TAG of Qnil.
1120 Before each catch is discarded, unbind all special bindings and
1121 execute all unwind-protect clauses made above that catch. Unwind
1122 the handler stack as we go, so that the proper handlers are in
1123 effect for each unwind-protect clause we run. At the end, restore
1124 some static info saved in CATCH, and longjmp to the location
1125 specified there.
1127 This is used for correct unwinding in Fthrow and Fsignal. */
1129 static _Noreturn void
1130 unwind_to_catch (struct handler *catch, Lisp_Object value)
1132 bool last_time;
1134 eassert (catch->next);
1136 /* Save the value in the tag. */
1137 catch->val = value;
1139 /* Restore certain special C variables. */
1140 set_poll_suppress_count (catch->poll_suppress_count);
1141 unblock_input_to (catch->interrupt_input_blocked);
1145 /* Unwind the specpdl stack, and then restore the proper set of
1146 handlers. */
1147 unbind_to (handlerlist->pdlcount, Qnil);
1148 last_time = handlerlist == catch;
1149 if (! last_time)
1150 handlerlist = handlerlist->next;
1152 while (! last_time);
1154 eassert (handlerlist == catch);
1156 lisp_eval_depth = catch->f_lisp_eval_depth;
1158 sys_longjmp (catch->jmp, 1);
1161 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1162 doc: /* Throw to the catch for TAG and return VALUE from it.
1163 Both TAG and VALUE are evalled. */
1164 attributes: noreturn)
1165 (register Lisp_Object tag, Lisp_Object value)
1167 struct handler *c;
1169 if (!NILP (tag))
1170 for (c = handlerlist; c; c = c->next)
1172 if (c->type == CATCHER_ALL)
1173 unwind_to_catch (c, Fcons (tag, value));
1174 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1175 unwind_to_catch (c, value);
1177 xsignal2 (Qno_catch, tag, value);
1181 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1182 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1183 If BODYFORM completes normally, its value is returned
1184 after executing the UNWINDFORMS.
1185 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1186 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1187 (Lisp_Object args)
1189 Lisp_Object val;
1190 ptrdiff_t count = SPECPDL_INDEX ();
1192 record_unwind_protect (prog_ignore, XCDR (args));
1193 val = eval_sub (XCAR (args));
1194 return unbind_to (count, val);
1197 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1198 doc: /* Regain control when an error is signaled.
1199 Executes BODYFORM and returns its value if no error happens.
1200 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1201 where the BODY is made of Lisp expressions.
1203 A handler is applicable to an error
1204 if CONDITION-NAME is one of the error's condition names.
1205 If an error happens, the first applicable handler is run.
1207 The car of a handler may be a list of condition names instead of a
1208 single condition name; then it handles all of them. If the special
1209 condition name `debug' is present in this list, it allows another
1210 condition in the list to run the debugger if `debug-on-error' and the
1211 other usual mechanisms says it should (otherwise, `condition-case'
1212 suppresses the debugger).
1214 When a handler handles an error, control returns to the `condition-case'
1215 and it executes the handler's BODY...
1216 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1217 \(If VAR is nil, the handler can't access that information.)
1218 Then the value of the last BODY form is returned from the `condition-case'
1219 expression.
1221 See also the function `signal' for more info.
1222 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1223 (Lisp_Object args)
1225 Lisp_Object var = XCAR (args);
1226 Lisp_Object bodyform = XCAR (XCDR (args));
1227 Lisp_Object handlers = XCDR (XCDR (args));
1229 return internal_lisp_condition_case (var, bodyform, handlers);
1232 /* Like Fcondition_case, but the args are separate
1233 rather than passed in a list. Used by Fbyte_code. */
1235 Lisp_Object
1236 internal_lisp_condition_case (Lisp_Object var, Lisp_Object bodyform,
1237 Lisp_Object handlers)
1239 struct handler *oldhandlerlist = handlerlist;
1240 ptrdiff_t CACHEABLE clausenb = 0;
1242 CHECK_SYMBOL (var);
1244 for (Lisp_Object tail = handlers; CONSP (tail); tail = XCDR (tail))
1246 Lisp_Object tem = XCAR (tail);
1247 clausenb++;
1248 if (! (NILP (tem)
1249 || (CONSP (tem)
1250 && (SYMBOLP (XCAR (tem))
1251 || CONSP (XCAR (tem))))))
1252 error ("Invalid condition handler: %s",
1253 SDATA (Fprin1_to_string (tem, Qt)));
1256 /* The first clause is the one that should be checked first, so it
1257 should be added to handlerlist last. So build in CLAUSES a table
1258 that contains HANDLERS but in reverse order. CLAUSES is pointer
1259 to volatile to avoid issues with setjmp and local storage.
1260 SAFE_ALLOCA won't work here due to the setjmp, so impose a
1261 MAX_ALLOCA limit. */
1262 if (MAX_ALLOCA / word_size < clausenb)
1263 memory_full (SIZE_MAX);
1264 Lisp_Object volatile *clauses = alloca (clausenb * sizeof *clauses);
1265 clauses += clausenb;
1266 for (Lisp_Object tail = handlers; CONSP (tail); tail = XCDR (tail))
1267 *--clauses = XCAR (tail);
1268 for (ptrdiff_t i = 0; i < clausenb; i++)
1270 Lisp_Object clause = clauses[i];
1271 Lisp_Object condition = CONSP (clause) ? XCAR (clause) : Qnil;
1272 if (!CONSP (condition))
1273 condition = list1 (condition);
1274 struct handler *c = push_handler (condition, CONDITION_CASE);
1275 if (sys_setjmp (c->jmp))
1277 Lisp_Object val = handlerlist->val;
1278 Lisp_Object volatile *chosen_clause = clauses;
1279 for (struct handler *h = handlerlist->next; h != oldhandlerlist;
1280 h = h->next)
1281 chosen_clause++;
1282 Lisp_Object handler_body = XCDR (*chosen_clause);
1283 handlerlist = oldhandlerlist;
1285 if (NILP (var))
1286 return Fprogn (handler_body);
1288 Lisp_Object handler_var = var;
1289 if (!NILP (Vinternal_interpreter_environment))
1291 val = Fcons (Fcons (var, val),
1292 Vinternal_interpreter_environment);
1293 handler_var = Qinternal_interpreter_environment;
1296 /* Bind HANDLER_VAR to VAL while evaluating HANDLER_BODY.
1297 The unbind_to undoes just this binding; whoever longjumped
1298 to us unwound the stack to C->pdlcount before throwing. */
1299 ptrdiff_t count = SPECPDL_INDEX ();
1300 specbind (handler_var, val);
1301 return unbind_to (count, Fprogn (handler_body));
1305 Lisp_Object result = eval_sub (bodyform);
1306 handlerlist = oldhandlerlist;
1307 return result;
1310 /* Call the function BFUN with no arguments, catching errors within it
1311 according to HANDLERS. If there is an error, call HFUN with
1312 one argument which is the data that describes the error:
1313 (SIGNALNAME . DATA)
1315 HANDLERS can be a list of conditions to catch.
1316 If HANDLERS is Qt, catch all errors.
1317 If HANDLERS is Qerror, catch all errors
1318 but allow the debugger to run if that is enabled. */
1320 Lisp_Object
1321 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1322 Lisp_Object (*hfun) (Lisp_Object))
1324 struct handler *c = push_handler (handlers, CONDITION_CASE);
1325 if (sys_setjmp (c->jmp))
1327 Lisp_Object val = handlerlist->val;
1328 clobbered_eassert (handlerlist == c);
1329 handlerlist = handlerlist->next;
1330 return hfun (val);
1332 else
1334 Lisp_Object val = bfun ();
1335 eassert (handlerlist == c);
1336 handlerlist = c->next;
1337 return val;
1341 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1343 Lisp_Object
1344 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1345 Lisp_Object handlers,
1346 Lisp_Object (*hfun) (Lisp_Object))
1348 struct handler *c = push_handler (handlers, CONDITION_CASE);
1349 if (sys_setjmp (c->jmp))
1351 Lisp_Object val = handlerlist->val;
1352 clobbered_eassert (handlerlist == c);
1353 handlerlist = handlerlist->next;
1354 return hfun (val);
1356 else
1358 Lisp_Object val = bfun (arg);
1359 eassert (handlerlist == c);
1360 handlerlist = c->next;
1361 return val;
1365 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1366 its arguments. */
1368 Lisp_Object
1369 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1370 Lisp_Object arg1,
1371 Lisp_Object arg2,
1372 Lisp_Object handlers,
1373 Lisp_Object (*hfun) (Lisp_Object))
1375 struct handler *c = push_handler (handlers, CONDITION_CASE);
1376 if (sys_setjmp (c->jmp))
1378 Lisp_Object val = handlerlist->val;
1379 clobbered_eassert (handlerlist == c);
1380 handlerlist = handlerlist->next;
1381 return hfun (val);
1383 else
1385 Lisp_Object val = bfun (arg1, arg2);
1386 eassert (handlerlist == c);
1387 handlerlist = c->next;
1388 return val;
1392 /* Like internal_condition_case but call BFUN with NARGS as first,
1393 and ARGS as second argument. */
1395 Lisp_Object
1396 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1397 ptrdiff_t nargs,
1398 Lisp_Object *args,
1399 Lisp_Object handlers,
1400 Lisp_Object (*hfun) (Lisp_Object err,
1401 ptrdiff_t nargs,
1402 Lisp_Object *args))
1404 struct handler *c = push_handler (handlers, CONDITION_CASE);
1405 if (sys_setjmp (c->jmp))
1407 Lisp_Object val = handlerlist->val;
1408 clobbered_eassert (handlerlist == c);
1409 handlerlist = handlerlist->next;
1410 return hfun (val, nargs, args);
1412 else
1414 Lisp_Object val = bfun (nargs, args);
1415 eassert (handlerlist == c);
1416 handlerlist = c->next;
1417 return val;
1421 static Lisp_Object
1422 internal_catch_all_1 (Lisp_Object (*function) (void *), void *argument)
1424 struct handler *c = push_handler_nosignal (Qt, CATCHER_ALL);
1425 if (c == NULL)
1426 return Qcatch_all_memory_full;
1428 if (sys_setjmp (c->jmp) == 0)
1430 Lisp_Object val = function (argument);
1431 eassert (handlerlist == c);
1432 handlerlist = c->next;
1433 return val;
1435 else
1437 eassert (handlerlist == c);
1438 Lisp_Object val = c->val;
1439 handlerlist = c->next;
1440 Fsignal (Qno_catch, val);
1444 /* Like a combination of internal_condition_case_1 and internal_catch.
1445 Catches all signals and throws. Never exits nonlocally; returns
1446 Qcatch_all_memory_full if no handler could be allocated. */
1448 Lisp_Object
1449 internal_catch_all (Lisp_Object (*function) (void *), void *argument,
1450 Lisp_Object (*handler) (Lisp_Object))
1452 struct handler *c = push_handler_nosignal (Qt, CONDITION_CASE);
1453 if (c == NULL)
1454 return Qcatch_all_memory_full;
1456 if (sys_setjmp (c->jmp) == 0)
1458 Lisp_Object val = internal_catch_all_1 (function, argument);
1459 eassert (handlerlist == c);
1460 handlerlist = c->next;
1461 return val;
1463 else
1465 eassert (handlerlist == c);
1466 Lisp_Object val = c->val;
1467 handlerlist = c->next;
1468 return handler (val);
1472 struct handler *
1473 push_handler (Lisp_Object tag_ch_val, enum handlertype handlertype)
1475 struct handler *c = push_handler_nosignal (tag_ch_val, handlertype);
1476 if (!c)
1477 memory_full (sizeof *c);
1478 return c;
1481 struct handler *
1482 push_handler_nosignal (Lisp_Object tag_ch_val, enum handlertype handlertype)
1484 struct handler *CACHEABLE c = handlerlist->nextfree;
1485 if (!c)
1487 c = malloc (sizeof *c);
1488 if (!c)
1489 return c;
1490 if (profiler_memory_running)
1491 malloc_probe (sizeof *c);
1492 c->nextfree = NULL;
1493 handlerlist->nextfree = c;
1495 c->type = handlertype;
1496 c->tag_or_ch = tag_ch_val;
1497 c->val = Qnil;
1498 c->next = handlerlist;
1499 c->f_lisp_eval_depth = lisp_eval_depth;
1500 c->pdlcount = SPECPDL_INDEX ();
1501 c->poll_suppress_count = poll_suppress_count;
1502 c->interrupt_input_blocked = interrupt_input_blocked;
1503 handlerlist = c;
1504 return c;
1508 static Lisp_Object signal_or_quit (Lisp_Object, Lisp_Object, bool);
1509 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1510 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1511 Lisp_Object data);
1513 static void
1514 process_quit_flag (void)
1516 Lisp_Object flag = Vquit_flag;
1517 Vquit_flag = Qnil;
1518 if (EQ (flag, Qkill_emacs))
1519 Fkill_emacs (Qnil);
1520 if (EQ (Vthrow_on_input, flag))
1521 Fthrow (Vthrow_on_input, Qt);
1522 quit ();
1525 /* Check quit-flag and quit if it is non-nil. Typing C-g does not
1526 directly cause a quit; it only sets Vquit_flag. So the program
1527 needs to call maybe_quit at times when it is safe to quit. Every
1528 loop that might run for a long time or might not exit ought to call
1529 maybe_quit at least once, at a safe place. Unless that is
1530 impossible, of course. But it is very desirable to avoid creating
1531 loops where maybe_quit is impossible.
1533 If quit-flag is set to `kill-emacs' the SIGINT handler has received
1534 a request to exit Emacs when it is safe to do.
1536 When not quitting, process any pending signals.
1538 If you change this function, also adapt module_should_quit in
1539 emacs-module.c. */
1541 void
1542 maybe_quit (void)
1544 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
1545 process_quit_flag ();
1546 else if (pending_signals)
1547 process_pending_signals ();
1550 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1551 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1552 This function does not return.
1554 An error symbol is a symbol with an `error-conditions' property
1555 that is a list of condition names.
1556 A handler for any of those names will get to handle this signal.
1557 The symbol `error' should normally be one of them.
1559 DATA should be a list. Its elements are printed as part of the error message.
1560 See Info anchor `(elisp)Definition of signal' for some details on how this
1561 error message is constructed.
1562 If the signal is handled, DATA is made available to the handler.
1563 See also the function `condition-case'. */
1564 attributes: noreturn)
1565 (Lisp_Object error_symbol, Lisp_Object data)
1567 signal_or_quit (error_symbol, data, false);
1568 eassume (false);
1571 /* Quit, in response to a keyboard quit request. */
1572 Lisp_Object
1573 quit (void)
1575 return signal_or_quit (Qquit, Qnil, true);
1578 /* Signal an error, or quit. ERROR_SYMBOL and DATA are as with Fsignal.
1579 If KEYBOARD_QUIT, this is a quit; ERROR_SYMBOL should be
1580 Qquit and DATA should be Qnil, and this function may return.
1581 Otherwise this function is like Fsignal and does not return. */
1583 static Lisp_Object
1584 signal_or_quit (Lisp_Object error_symbol, Lisp_Object data, bool keyboard_quit)
1586 /* When memory is full, ERROR-SYMBOL is nil,
1587 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1588 That is a special case--don't do this in other situations. */
1589 Lisp_Object conditions;
1590 Lisp_Object string;
1591 Lisp_Object real_error_symbol
1592 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1593 Lisp_Object clause = Qnil;
1594 struct handler *h;
1596 if (gc_in_progress || waiting_for_input)
1597 emacs_abort ();
1599 #if 0 /* rms: I don't know why this was here,
1600 but it is surely wrong for an error that is handled. */
1601 #ifdef HAVE_WINDOW_SYSTEM
1602 if (display_hourglass_p)
1603 cancel_hourglass ();
1604 #endif
1605 #endif
1607 /* This hook is used by edebug. */
1608 if (! NILP (Vsignal_hook_function)
1609 && ! NILP (error_symbol)
1610 /* Don't try to call a lisp function if we've already overflowed
1611 the specpdl stack. */
1612 && specpdl_ptr < specpdl + specpdl_size)
1614 /* Edebug takes care of restoring these variables when it exits. */
1615 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1616 max_lisp_eval_depth = lisp_eval_depth + 20;
1618 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1619 max_specpdl_size = SPECPDL_INDEX () + 40;
1621 call2 (Vsignal_hook_function, error_symbol, data);
1624 conditions = Fget (real_error_symbol, Qerror_conditions);
1626 /* Remember from where signal was called. Skip over the frame for
1627 `signal' itself. If a frame for `error' follows, skip that,
1628 too. Don't do this when ERROR_SYMBOL is nil, because that
1629 is a memory-full error. */
1630 Vsignaling_function = Qnil;
1631 if (!NILP (error_symbol))
1633 union specbinding *pdl = backtrace_next (backtrace_top ());
1634 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1635 pdl = backtrace_next (pdl);
1636 if (backtrace_p (pdl))
1637 Vsignaling_function = backtrace_function (pdl);
1640 for (h = handlerlist; h; h = h->next)
1642 if (h->type != CONDITION_CASE)
1643 continue;
1644 clause = find_handler_clause (h->tag_or_ch, conditions);
1645 if (!NILP (clause))
1646 break;
1649 if (/* Don't run the debugger for a memory-full error.
1650 (There is no room in memory to do that!) */
1651 !NILP (error_symbol)
1652 && (!NILP (Vdebug_on_signal)
1653 /* If no handler is present now, try to run the debugger. */
1654 || NILP (clause)
1655 /* A `debug' symbol in the handler list disables the normal
1656 suppression of the debugger. */
1657 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1658 /* Special handler that means "print a message and run debugger
1659 if requested". */
1660 || EQ (h->tag_or_ch, Qerror)))
1662 bool debugger_called
1663 = maybe_call_debugger (conditions, error_symbol, data);
1664 /* We can't return values to code which signaled an error, but we
1665 can continue code which has signaled a quit. */
1666 if (keyboard_quit && debugger_called && EQ (real_error_symbol, Qquit))
1667 return Qnil;
1670 if (!NILP (clause))
1672 Lisp_Object unwind_data
1673 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1675 unwind_to_catch (h, unwind_data);
1677 else
1679 if (handlerlist != handlerlist_sentinel)
1680 /* FIXME: This will come right back here if there's no `top-level'
1681 catcher. A better solution would be to abort here, and instead
1682 add a catch-all condition handler so we never come here. */
1683 Fthrow (Qtop_level, Qt);
1686 if (! NILP (error_symbol))
1687 data = Fcons (error_symbol, data);
1689 string = Ferror_message_string (data);
1690 fatal ("%s", SDATA (string));
1693 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1695 void
1696 xsignal0 (Lisp_Object error_symbol)
1698 xsignal (error_symbol, Qnil);
1701 void
1702 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1704 xsignal (error_symbol, list1 (arg));
1707 void
1708 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1710 xsignal (error_symbol, list2 (arg1, arg2));
1713 void
1714 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1716 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1719 /* Signal `error' with message S, and additional arg ARG.
1720 If ARG is not a genuine list, make it a one-element list. */
1722 void
1723 signal_error (const char *s, Lisp_Object arg)
1725 Lisp_Object tortoise, hare;
1727 hare = tortoise = arg;
1728 while (CONSP (hare))
1730 hare = XCDR (hare);
1731 if (!CONSP (hare))
1732 break;
1734 hare = XCDR (hare);
1735 tortoise = XCDR (tortoise);
1737 if (EQ (hare, tortoise))
1738 break;
1741 if (!NILP (hare))
1742 arg = list1 (arg);
1744 xsignal (Qerror, Fcons (build_string (s), arg));
1748 /* Return true if LIST is a non-nil atom or
1749 a list containing one of CONDITIONS. */
1751 static bool
1752 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1754 if (NILP (list))
1755 return 0;
1756 if (! CONSP (list))
1757 return 1;
1759 while (CONSP (conditions))
1761 Lisp_Object this, tail;
1762 this = XCAR (conditions);
1763 for (tail = list; CONSP (tail); tail = XCDR (tail))
1764 if (EQ (XCAR (tail), this))
1765 return 1;
1766 conditions = XCDR (conditions);
1768 return 0;
1771 /* Return true if an error with condition-symbols CONDITIONS,
1772 and described by SIGNAL-DATA, should skip the debugger
1773 according to debugger-ignored-errors. */
1775 static bool
1776 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1778 Lisp_Object tail;
1779 bool first_string = 1;
1780 Lisp_Object error_message;
1782 error_message = Qnil;
1783 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1785 if (STRINGP (XCAR (tail)))
1787 if (first_string)
1789 error_message = Ferror_message_string (data);
1790 first_string = 0;
1793 if (fast_string_match (XCAR (tail), error_message) >= 0)
1794 return 1;
1796 else
1798 Lisp_Object contail;
1800 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1801 if (EQ (XCAR (tail), XCAR (contail)))
1802 return 1;
1806 return 0;
1809 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1810 SIG and DATA describe the signal. There are two ways to pass them:
1811 = SIG is the error symbol, and DATA is the rest of the data.
1812 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1813 This is for memory-full errors only. */
1814 static bool
1815 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1817 Lisp_Object combined_data;
1819 combined_data = Fcons (sig, data);
1821 if (
1822 /* Don't try to run the debugger with interrupts blocked.
1823 The editing loop would return anyway. */
1824 ! input_blocked_p ()
1825 && NILP (Vinhibit_debugger)
1826 /* Does user want to enter debugger for this kind of error? */
1827 && (EQ (sig, Qquit)
1828 ? debug_on_quit
1829 : wants_debugger (Vdebug_on_error, conditions))
1830 && ! skip_debugger (conditions, combined_data)
1831 /* RMS: What's this for? */
1832 && when_entered_debugger < num_nonmacro_input_events)
1834 call_debugger (list2 (Qerror, combined_data));
1835 return 1;
1838 return 0;
1841 static Lisp_Object
1842 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1844 register Lisp_Object h;
1846 /* t is used by handlers for all conditions, set up by C code. */
1847 if (EQ (handlers, Qt))
1848 return Qt;
1850 /* error is used similarly, but means print an error message
1851 and run the debugger if that is enabled. */
1852 if (EQ (handlers, Qerror))
1853 return Qt;
1855 for (h = handlers; CONSP (h); h = XCDR (h))
1857 Lisp_Object handler = XCAR (h);
1858 if (!NILP (Fmemq (handler, conditions)))
1859 return handlers;
1862 return Qnil;
1866 /* Format and return a string; called like vprintf. */
1867 Lisp_Object
1868 vformat_string (const char *m, va_list ap)
1870 char buf[4000];
1871 ptrdiff_t size = sizeof buf;
1872 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1873 char *buffer = buf;
1874 ptrdiff_t used;
1875 Lisp_Object string;
1877 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1878 string = make_string (buffer, used);
1879 if (buffer != buf)
1880 xfree (buffer);
1882 return string;
1885 /* Dump an error message; called like vprintf. */
1886 void
1887 verror (const char *m, va_list ap)
1889 xsignal1 (Qerror, vformat_string (m, ap));
1893 /* Dump an error message; called like printf. */
1895 /* VARARGS 1 */
1896 void
1897 error (const char *m, ...)
1899 va_list ap;
1900 va_start (ap, m);
1901 verror (m, ap);
1904 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1905 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1906 This means it contains a description for how to read arguments to give it.
1907 The value is nil for an invalid function or a symbol with no function
1908 definition.
1910 Interactively callable functions include strings and vectors (treated
1911 as keyboard macros), lambda-expressions that contain a top-level call
1912 to `interactive', autoload definitions made by `autoload' with non-nil
1913 fourth argument, and some of the built-in functions of Lisp.
1915 Also, a symbol satisfies `commandp' if its function definition does so.
1917 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1918 then strings and vectors are not accepted. */)
1919 (Lisp_Object function, Lisp_Object for_call_interactively)
1921 register Lisp_Object fun;
1922 register Lisp_Object funcar;
1923 Lisp_Object if_prop = Qnil;
1925 fun = function;
1927 fun = indirect_function (fun); /* Check cycles. */
1928 if (NILP (fun))
1929 return Qnil;
1931 /* Check an `interactive-form' property if present, analogous to the
1932 function-documentation property. */
1933 fun = function;
1934 while (SYMBOLP (fun))
1936 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1937 if (!NILP (tmp))
1938 if_prop = Qt;
1939 fun = Fsymbol_function (fun);
1942 /* Emacs primitives are interactive if their DEFUN specifies an
1943 interactive spec. */
1944 if (SUBRP (fun))
1945 return XSUBR (fun)->intspec ? Qt : if_prop;
1947 /* Bytecode objects are interactive if they are long enough to
1948 have an element whose index is COMPILED_INTERACTIVE, which is
1949 where the interactive spec is stored. */
1950 else if (COMPILEDP (fun))
1951 return (PVSIZE (fun) > COMPILED_INTERACTIVE ? Qt : if_prop);
1953 /* Strings and vectors are keyboard macros. */
1954 if (STRINGP (fun) || VECTORP (fun))
1955 return (NILP (for_call_interactively) ? Qt : Qnil);
1957 /* Lists may represent commands. */
1958 if (!CONSP (fun))
1959 return Qnil;
1960 funcar = XCAR (fun);
1961 if (EQ (funcar, Qclosure))
1962 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1963 ? Qt : if_prop);
1964 else if (EQ (funcar, Qlambda))
1965 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1966 else if (EQ (funcar, Qautoload))
1967 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1968 else
1969 return Qnil;
1972 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1973 doc: /* Define FUNCTION to autoload from FILE.
1974 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1975 Third arg DOCSTRING is documentation for the function.
1976 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1977 Fifth arg TYPE indicates the type of the object:
1978 nil or omitted says FUNCTION is a function,
1979 `keymap' says FUNCTION is really a keymap, and
1980 `macro' or t says FUNCTION is really a macro.
1981 Third through fifth args give info about the real definition.
1982 They default to nil.
1983 If FUNCTION is already defined other than as an autoload,
1984 this does nothing and returns nil. */)
1985 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1987 CHECK_SYMBOL (function);
1988 CHECK_STRING (file);
1990 /* If function is defined and not as an autoload, don't override. */
1991 if (!NILP (XSYMBOL (function)->u.s.function)
1992 && !AUTOLOADP (XSYMBOL (function)->u.s.function))
1993 return Qnil;
1995 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1996 /* `read1' in lread.c has found the docstring starting with "\
1997 and assumed the docstring will be provided by Snarf-documentation, so it
1998 passed us 0 instead. But that leads to accidental sharing in purecopy's
1999 hash-consing, so we use a (hopefully) unique integer instead. */
2000 docstring = make_number (XHASH (function));
2001 return Fdefalias (function,
2002 list5 (Qautoload, file, docstring, interactive, type),
2003 Qnil);
2006 void
2007 un_autoload (Lisp_Object oldqueue)
2009 Lisp_Object queue, first, second;
2011 /* Queue to unwind is current value of Vautoload_queue.
2012 oldqueue is the shadowed value to leave in Vautoload_queue. */
2013 queue = Vautoload_queue;
2014 Vautoload_queue = oldqueue;
2015 while (CONSP (queue))
2017 first = XCAR (queue);
2018 second = Fcdr (first);
2019 first = Fcar (first);
2020 if (EQ (first, make_number (0)))
2021 Vfeatures = second;
2022 else
2023 Ffset (first, second);
2024 queue = XCDR (queue);
2028 /* Load an autoloaded function.
2029 FUNNAME is the symbol which is the function's name.
2030 FUNDEF is the autoload definition (a list). */
2032 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
2033 doc: /* Load FUNDEF which should be an autoload.
2034 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
2035 in which case the function returns the new autoloaded function value.
2036 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
2037 it defines a macro. */)
2038 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
2040 ptrdiff_t count = SPECPDL_INDEX ();
2042 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
2043 return fundef;
2045 Lisp_Object kind = Fnth (make_number (4), fundef);
2046 if (EQ (macro_only, Qmacro)
2047 && !(EQ (kind, Qt) || EQ (kind, Qmacro)))
2048 return fundef;
2050 /* This is to make sure that loadup.el gives a clear picture
2051 of what files are preloaded and when. */
2052 if (! NILP (Vpurify_flag))
2053 error ("Attempt to autoload %s while preparing to dump",
2054 SDATA (SYMBOL_NAME (funname)));
2056 CHECK_SYMBOL (funname);
2058 /* Preserve the match data. */
2059 record_unwind_save_match_data ();
2061 /* If autoloading gets an error (which includes the error of failing
2062 to define the function being called), we use Vautoload_queue
2063 to undo function definitions and `provide' calls made by
2064 the function. We do this in the specific case of autoloading
2065 because autoloading is not an explicit request "load this file",
2066 but rather a request to "call this function".
2068 The value saved here is to be restored into Vautoload_queue. */
2069 record_unwind_protect (un_autoload, Vautoload_queue);
2070 Vautoload_queue = Qt;
2071 /* If `macro_only' is set and fundef isn't a macro, assume this autoload to
2072 be a "best-effort" (e.g. to try and find a compiler macro),
2073 so don't signal an error if autoloading fails. */
2074 Lisp_Object ignore_errors
2075 = (EQ (kind, Qt) || EQ (kind, Qmacro)) ? Qnil : macro_only;
2076 Fload (Fcar (Fcdr (fundef)), ignore_errors, Qt, Qnil, Qt);
2078 /* Once loading finishes, don't undo it. */
2079 Vautoload_queue = Qt;
2080 unbind_to (count, Qnil);
2082 if (NILP (funname) || !NILP (ignore_errors))
2083 return Qnil;
2084 else
2086 Lisp_Object fun = Findirect_function (funname, Qnil);
2088 if (!NILP (Fequal (fun, fundef)))
2089 error ("Autoloading file %s failed to define function %s",
2090 SDATA (Fcar (Fcar (Vload_history))),
2091 SDATA (SYMBOL_NAME (funname)));
2092 else
2093 return fun;
2098 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2099 doc: /* Evaluate FORM and return its value.
2100 If LEXICAL is t, evaluate using lexical scoping.
2101 LEXICAL can also be an actual lexical environment, in the form of an
2102 alist mapping symbols to their value. */)
2103 (Lisp_Object form, Lisp_Object lexical)
2105 ptrdiff_t count = SPECPDL_INDEX ();
2106 specbind (Qinternal_interpreter_environment,
2107 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2108 return unbind_to (count, eval_sub (form));
2111 /* Grow the specpdl stack by one entry.
2112 The caller should have already initialized the entry.
2113 Signal an error on stack overflow.
2115 Make sure that there is always one unused entry past the top of the
2116 stack, so that the just-initialized entry is safely unwound if
2117 memory exhausted and an error is signaled here. Also, allocate a
2118 never-used entry just before the bottom of the stack; sometimes its
2119 address is taken. */
2121 static void
2122 grow_specpdl (void)
2124 specpdl_ptr++;
2126 if (specpdl_ptr == specpdl + specpdl_size)
2128 ptrdiff_t count = SPECPDL_INDEX ();
2129 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2130 union specbinding *pdlvec = specpdl - 1;
2131 ptrdiff_t pdlvecsize = specpdl_size + 1;
2132 if (max_size <= specpdl_size)
2134 if (max_specpdl_size < 400)
2135 max_size = max_specpdl_size = 400;
2136 if (max_size <= specpdl_size)
2137 signal_error ("Variable binding depth exceeds max-specpdl-size",
2138 Qnil);
2140 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2141 specpdl = pdlvec + 1;
2142 specpdl_size = pdlvecsize - 1;
2143 specpdl_ptr = specpdl + count;
2147 ptrdiff_t
2148 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2150 ptrdiff_t count = SPECPDL_INDEX ();
2152 eassert (nargs >= UNEVALLED);
2153 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2154 specpdl_ptr->bt.debug_on_exit = false;
2155 specpdl_ptr->bt.function = function;
2156 current_thread->stack_top = specpdl_ptr->bt.args = args;
2157 specpdl_ptr->bt.nargs = nargs;
2158 grow_specpdl ();
2160 return count;
2163 /* Eval a sub-expression of the current expression (i.e. in the same
2164 lexical scope). */
2165 Lisp_Object
2166 eval_sub (Lisp_Object form)
2168 Lisp_Object fun, val, original_fun, original_args;
2169 Lisp_Object funcar;
2170 ptrdiff_t count;
2172 /* Declare here, as this array may be accessed by call_debugger near
2173 the end of this function. See Bug#21245. */
2174 Lisp_Object argvals[8];
2176 if (SYMBOLP (form))
2178 /* Look up its binding in the lexical environment.
2179 We do not pay attention to the declared_special flag here, since we
2180 already did that when let-binding the variable. */
2181 Lisp_Object lex_binding
2182 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2183 ? Fassq (form, Vinternal_interpreter_environment)
2184 : Qnil;
2185 if (CONSP (lex_binding))
2186 return XCDR (lex_binding);
2187 else
2188 return Fsymbol_value (form);
2191 if (!CONSP (form))
2192 return form;
2194 maybe_quit ();
2196 maybe_gc ();
2198 if (++lisp_eval_depth > max_lisp_eval_depth)
2200 if (max_lisp_eval_depth < 100)
2201 max_lisp_eval_depth = 100;
2202 if (lisp_eval_depth > max_lisp_eval_depth)
2203 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2206 original_fun = XCAR (form);
2207 original_args = XCDR (form);
2208 CHECK_LIST (original_args);
2210 /* This also protects them from gc. */
2211 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2213 if (debug_on_next_call)
2214 do_debug_on_call (Qt, count);
2216 /* At this point, only original_fun and original_args
2217 have values that will be used below. */
2218 retry:
2220 /* Optimize for no indirection. */
2221 fun = original_fun;
2222 if (!SYMBOLP (fun))
2223 fun = Ffunction (Fcons (fun, Qnil));
2224 else if (!NILP (fun) && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2225 fun = indirect_function (fun);
2227 if (SUBRP (fun))
2229 Lisp_Object args_left = original_args;
2230 Lisp_Object numargs = Flength (args_left);
2232 check_cons_list ();
2234 if (XINT (numargs) < XSUBR (fun)->min_args
2235 || (XSUBR (fun)->max_args >= 0
2236 && XSUBR (fun)->max_args < XINT (numargs)))
2237 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2239 else if (XSUBR (fun)->max_args == UNEVALLED)
2240 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2241 else if (XSUBR (fun)->max_args == MANY)
2243 /* Pass a vector of evaluated arguments. */
2244 Lisp_Object *vals;
2245 ptrdiff_t argnum = 0;
2246 USE_SAFE_ALLOCA;
2248 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2250 while (CONSP (args_left) && argnum < XINT (numargs))
2252 Lisp_Object arg = XCAR (args_left);
2253 args_left = XCDR (args_left);
2254 vals[argnum++] = eval_sub (arg);
2257 set_backtrace_args (specpdl + count, vals, argnum);
2259 val = XSUBR (fun)->function.aMANY (argnum, vals);
2261 check_cons_list ();
2262 lisp_eval_depth--;
2263 /* Do the debug-on-exit now, while VALS still exists. */
2264 if (backtrace_debug_on_exit (specpdl + count))
2265 val = call_debugger (list2 (Qexit, val));
2266 SAFE_FREE ();
2267 specpdl_ptr--;
2268 return val;
2270 else
2272 int i, maxargs = XSUBR (fun)->max_args;
2274 for (i = 0; i < maxargs; i++)
2276 argvals[i] = eval_sub (Fcar (args_left));
2277 args_left = Fcdr (args_left);
2280 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2282 switch (i)
2284 case 0:
2285 val = (XSUBR (fun)->function.a0 ());
2286 break;
2287 case 1:
2288 val = (XSUBR (fun)->function.a1 (argvals[0]));
2289 break;
2290 case 2:
2291 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2292 break;
2293 case 3:
2294 val = (XSUBR (fun)->function.a3
2295 (argvals[0], argvals[1], argvals[2]));
2296 break;
2297 case 4:
2298 val = (XSUBR (fun)->function.a4
2299 (argvals[0], argvals[1], argvals[2], argvals[3]));
2300 break;
2301 case 5:
2302 val = (XSUBR (fun)->function.a5
2303 (argvals[0], argvals[1], argvals[2], argvals[3],
2304 argvals[4]));
2305 break;
2306 case 6:
2307 val = (XSUBR (fun)->function.a6
2308 (argvals[0], argvals[1], argvals[2], argvals[3],
2309 argvals[4], argvals[5]));
2310 break;
2311 case 7:
2312 val = (XSUBR (fun)->function.a7
2313 (argvals[0], argvals[1], argvals[2], argvals[3],
2314 argvals[4], argvals[5], argvals[6]));
2315 break;
2317 case 8:
2318 val = (XSUBR (fun)->function.a8
2319 (argvals[0], argvals[1], argvals[2], argvals[3],
2320 argvals[4], argvals[5], argvals[6], argvals[7]));
2321 break;
2323 default:
2324 /* Someone has created a subr that takes more arguments than
2325 is supported by this code. We need to either rewrite the
2326 subr to use a different argument protocol, or add more
2327 cases to this switch. */
2328 emacs_abort ();
2332 else if (COMPILEDP (fun) || MODULE_FUNCTIONP (fun))
2333 return apply_lambda (fun, original_args, count);
2334 else
2336 if (NILP (fun))
2337 xsignal1 (Qvoid_function, original_fun);
2338 if (!CONSP (fun))
2339 xsignal1 (Qinvalid_function, original_fun);
2340 funcar = XCAR (fun);
2341 if (!SYMBOLP (funcar))
2342 xsignal1 (Qinvalid_function, original_fun);
2343 if (EQ (funcar, Qautoload))
2345 Fautoload_do_load (fun, original_fun, Qnil);
2346 goto retry;
2348 if (EQ (funcar, Qmacro))
2350 ptrdiff_t count1 = SPECPDL_INDEX ();
2351 Lisp_Object exp;
2352 /* Bind lexical-binding during expansion of the macro, so the
2353 macro can know reliably if the code it outputs will be
2354 interpreted using lexical-binding or not. */
2355 specbind (Qlexical_binding,
2356 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2357 exp = apply1 (Fcdr (fun), original_args);
2358 unbind_to (count1, Qnil);
2359 val = eval_sub (exp);
2361 else if (EQ (funcar, Qlambda)
2362 || EQ (funcar, Qclosure))
2363 return apply_lambda (fun, original_args, count);
2364 else
2365 xsignal1 (Qinvalid_function, original_fun);
2367 check_cons_list ();
2369 lisp_eval_depth--;
2370 if (backtrace_debug_on_exit (specpdl + count))
2371 val = call_debugger (list2 (Qexit, val));
2372 specpdl_ptr--;
2374 return val;
2377 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2378 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2379 Then return the value FUNCTION returns.
2380 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2381 usage: (apply FUNCTION &rest ARGUMENTS) */)
2382 (ptrdiff_t nargs, Lisp_Object *args)
2384 ptrdiff_t i, numargs, funcall_nargs;
2385 register Lisp_Object *funcall_args = NULL;
2386 register Lisp_Object spread_arg = args[nargs - 1];
2387 Lisp_Object fun = args[0];
2388 Lisp_Object retval;
2389 USE_SAFE_ALLOCA;
2391 CHECK_LIST (spread_arg);
2393 numargs = XINT (Flength (spread_arg));
2395 if (numargs == 0)
2396 return Ffuncall (nargs - 1, args);
2397 else if (numargs == 1)
2399 args [nargs - 1] = XCAR (spread_arg);
2400 return Ffuncall (nargs, args);
2403 numargs += nargs - 2;
2405 /* Optimize for no indirection. */
2406 if (SYMBOLP (fun) && !NILP (fun)
2407 && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2409 fun = indirect_function (fun);
2410 if (NILP (fun))
2411 /* Let funcall get the error. */
2412 fun = args[0];
2415 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2416 /* Don't hide an error by adding missing arguments. */
2417 && numargs >= XSUBR (fun)->min_args)
2419 /* Avoid making funcall cons up a yet another new vector of arguments
2420 by explicitly supplying nil's for optional values. */
2421 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2422 memclear (funcall_args + numargs + 1,
2423 (XSUBR (fun)->max_args - numargs) * word_size);
2424 funcall_nargs = 1 + XSUBR (fun)->max_args;
2426 else
2427 { /* We add 1 to numargs because funcall_args includes the
2428 function itself as well as its arguments. */
2429 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2430 funcall_nargs = 1 + numargs;
2433 memcpy (funcall_args, args, nargs * word_size);
2434 /* Spread the last arg we got. Its first element goes in
2435 the slot that it used to occupy, hence this value of I. */
2436 i = nargs - 1;
2437 while (!NILP (spread_arg))
2439 funcall_args [i++] = XCAR (spread_arg);
2440 spread_arg = XCDR (spread_arg);
2443 retval = Ffuncall (funcall_nargs, funcall_args);
2445 SAFE_FREE ();
2446 return retval;
2449 /* Run hook variables in various ways. */
2451 static Lisp_Object
2452 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2454 Ffuncall (nargs, args);
2455 return Qnil;
2458 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2459 doc: /* Run each hook in HOOKS.
2460 Each argument should be a symbol, a hook variable.
2461 These symbols are processed in the order specified.
2462 If a hook symbol has a non-nil value, that value may be a function
2463 or a list of functions to be called to run the hook.
2464 If the value is a function, it is called with no arguments.
2465 If it is a list, the elements are called, in order, with no arguments.
2467 Major modes should not use this function directly to run their mode
2468 hook; they should use `run-mode-hooks' instead.
2470 Do not use `make-local-variable' to make a hook variable buffer-local.
2471 Instead, use `add-hook' and specify t for the LOCAL argument.
2472 usage: (run-hooks &rest HOOKS) */)
2473 (ptrdiff_t nargs, Lisp_Object *args)
2475 ptrdiff_t i;
2477 for (i = 0; i < nargs; i++)
2478 run_hook (args[i]);
2480 return Qnil;
2483 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2484 Srun_hook_with_args, 1, MANY, 0,
2485 doc: /* Run HOOK with the specified arguments ARGS.
2486 HOOK should be a symbol, a hook variable. The value of HOOK
2487 may be nil, a function, or a list of functions. Call each
2488 function in order with arguments ARGS. The final return value
2489 is unspecified.
2491 Do not use `make-local-variable' to make a hook variable buffer-local.
2492 Instead, use `add-hook' and specify t for the LOCAL argument.
2493 usage: (run-hook-with-args HOOK &rest ARGS) */)
2494 (ptrdiff_t nargs, Lisp_Object *args)
2496 return run_hook_with_args (nargs, args, funcall_nil);
2499 /* NB this one still documents a specific non-nil return value.
2500 (As did run-hook-with-args and run-hook-with-args-until-failure
2501 until they were changed in 24.1.) */
2502 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2503 Srun_hook_with_args_until_success, 1, MANY, 0,
2504 doc: /* Run HOOK with the specified arguments ARGS.
2505 HOOK should be a symbol, a hook variable. The value of HOOK
2506 may be nil, a function, or a list of functions. Call each
2507 function in order with arguments ARGS, stopping at the first
2508 one that returns non-nil, and return that value. Otherwise (if
2509 all functions return nil, or if there are no functions to call),
2510 return nil.
2512 Do not use `make-local-variable' to make a hook variable buffer-local.
2513 Instead, use `add-hook' and specify t for the LOCAL argument.
2514 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2515 (ptrdiff_t nargs, Lisp_Object *args)
2517 return run_hook_with_args (nargs, args, Ffuncall);
2520 static Lisp_Object
2521 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2523 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2526 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2527 Srun_hook_with_args_until_failure, 1, MANY, 0,
2528 doc: /* Run HOOK with the specified arguments ARGS.
2529 HOOK should be a symbol, a hook variable. The value of HOOK
2530 may be nil, a function, or a list of functions. Call each
2531 function in order with arguments ARGS, stopping at the first
2532 one that returns nil, and return nil. Otherwise (if all functions
2533 return non-nil, or if there are no functions to call), return non-nil
2534 \(do not rely on the precise return value in this case).
2536 Do not use `make-local-variable' to make a hook variable buffer-local.
2537 Instead, use `add-hook' and specify t for the LOCAL argument.
2538 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2539 (ptrdiff_t nargs, Lisp_Object *args)
2541 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2544 static Lisp_Object
2545 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2547 Lisp_Object tmp = args[0], ret;
2548 args[0] = args[1];
2549 args[1] = tmp;
2550 ret = Ffuncall (nargs, args);
2551 args[1] = args[0];
2552 args[0] = tmp;
2553 return ret;
2556 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2557 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2558 I.e. instead of calling each function FUN directly with arguments ARGS,
2559 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2560 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2561 aborts and returns that value.
2562 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2563 (ptrdiff_t nargs, Lisp_Object *args)
2565 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2568 /* ARGS[0] should be a hook symbol.
2569 Call each of the functions in the hook value, passing each of them
2570 as arguments all the rest of ARGS (all NARGS - 1 elements).
2571 FUNCALL specifies how to call each function on the hook. */
2573 Lisp_Object
2574 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2575 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2577 Lisp_Object sym, val, ret = Qnil;
2579 /* If we are dying or still initializing,
2580 don't do anything--it would probably crash if we tried. */
2581 if (NILP (Vrun_hooks))
2582 return Qnil;
2584 sym = args[0];
2585 val = find_symbol_value (sym);
2587 if (EQ (val, Qunbound) || NILP (val))
2588 return ret;
2589 else if (!CONSP (val) || FUNCTIONP (val))
2591 args[0] = val;
2592 return funcall (nargs, args);
2594 else
2596 Lisp_Object global_vals = Qnil;
2598 for (;
2599 CONSP (val) && NILP (ret);
2600 val = XCDR (val))
2602 if (EQ (XCAR (val), Qt))
2604 /* t indicates this hook has a local binding;
2605 it means to run the global binding too. */
2606 global_vals = Fdefault_value (sym);
2607 if (NILP (global_vals)) continue;
2609 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2611 args[0] = global_vals;
2612 ret = funcall (nargs, args);
2614 else
2616 for (;
2617 CONSP (global_vals) && NILP (ret);
2618 global_vals = XCDR (global_vals))
2620 args[0] = XCAR (global_vals);
2621 /* In a global value, t should not occur. If it does, we
2622 must ignore it to avoid an endless loop. */
2623 if (!EQ (args[0], Qt))
2624 ret = funcall (nargs, args);
2628 else
2630 args[0] = XCAR (val);
2631 ret = funcall (nargs, args);
2635 return ret;
2639 /* Run the hook HOOK, giving each function no args. */
2641 void
2642 run_hook (Lisp_Object hook)
2644 Frun_hook_with_args (1, &hook);
2647 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2649 void
2650 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2652 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2655 /* Apply fn to arg. */
2656 Lisp_Object
2657 apply1 (Lisp_Object fn, Lisp_Object arg)
2659 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2662 /* Call function fn on no arguments. */
2663 Lisp_Object
2664 call0 (Lisp_Object fn)
2666 return Ffuncall (1, &fn);
2669 /* Call function fn with 1 argument arg1. */
2670 /* ARGSUSED */
2671 Lisp_Object
2672 call1 (Lisp_Object fn, Lisp_Object arg1)
2674 return CALLN (Ffuncall, fn, arg1);
2677 /* Call function fn with 2 arguments arg1, arg2. */
2678 /* ARGSUSED */
2679 Lisp_Object
2680 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2682 return CALLN (Ffuncall, fn, arg1, arg2);
2685 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2686 /* ARGSUSED */
2687 Lisp_Object
2688 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2690 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2693 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2694 /* ARGSUSED */
2695 Lisp_Object
2696 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2697 Lisp_Object arg4)
2699 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2702 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2703 /* ARGSUSED */
2704 Lisp_Object
2705 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2706 Lisp_Object arg4, Lisp_Object arg5)
2708 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2711 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2712 /* ARGSUSED */
2713 Lisp_Object
2714 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2715 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2717 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2720 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2721 /* ARGSUSED */
2722 Lisp_Object
2723 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2724 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2726 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2729 /* Call function fn with 8 arguments arg1, arg2, arg3, arg4, arg5,
2730 arg6, arg7, arg8. */
2731 /* ARGSUSED */
2732 Lisp_Object
2733 call8 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2734 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7,
2735 Lisp_Object arg8)
2737 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
2740 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2741 doc: /* Return t if OBJECT is a function. */)
2742 (Lisp_Object object)
2744 if (FUNCTIONP (object))
2745 return Qt;
2746 return Qnil;
2749 bool
2750 FUNCTIONP (Lisp_Object object)
2752 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2754 object = Findirect_function (object, Qt);
2756 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2758 /* Autoloaded symbols are functions, except if they load
2759 macros or keymaps. */
2760 for (int i = 0; i < 4 && CONSP (object); i++)
2761 object = XCDR (object);
2763 return ! (CONSP (object) && !NILP (XCAR (object)));
2767 if (SUBRP (object))
2768 return XSUBR (object)->max_args != UNEVALLED;
2769 else if (COMPILEDP (object) || MODULE_FUNCTIONP (object))
2770 return true;
2771 else if (CONSP (object))
2773 Lisp_Object car = XCAR (object);
2774 return EQ (car, Qlambda) || EQ (car, Qclosure);
2776 else
2777 return false;
2780 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2781 doc: /* Call first argument as a function, passing remaining arguments to it.
2782 Return the value that function returns.
2783 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2784 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2785 (ptrdiff_t nargs, Lisp_Object *args)
2787 Lisp_Object fun, original_fun;
2788 Lisp_Object funcar;
2789 ptrdiff_t numargs = nargs - 1;
2790 Lisp_Object val;
2791 ptrdiff_t count;
2793 maybe_quit ();
2795 if (++lisp_eval_depth > max_lisp_eval_depth)
2797 if (max_lisp_eval_depth < 100)
2798 max_lisp_eval_depth = 100;
2799 if (lisp_eval_depth > max_lisp_eval_depth)
2800 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2803 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2805 maybe_gc ();
2807 if (debug_on_next_call)
2808 do_debug_on_call (Qlambda, count);
2810 check_cons_list ();
2812 original_fun = args[0];
2814 retry:
2816 /* Optimize for no indirection. */
2817 fun = original_fun;
2818 if (SYMBOLP (fun) && !NILP (fun)
2819 && (fun = XSYMBOL (fun)->u.s.function, SYMBOLP (fun)))
2820 fun = indirect_function (fun);
2822 if (SUBRP (fun))
2823 val = funcall_subr (XSUBR (fun), numargs, args + 1);
2824 else if (COMPILEDP (fun) || MODULE_FUNCTIONP (fun))
2825 val = funcall_lambda (fun, numargs, args + 1);
2826 else
2828 if (NILP (fun))
2829 xsignal1 (Qvoid_function, original_fun);
2830 if (!CONSP (fun))
2831 xsignal1 (Qinvalid_function, original_fun);
2832 funcar = XCAR (fun);
2833 if (!SYMBOLP (funcar))
2834 xsignal1 (Qinvalid_function, original_fun);
2835 if (EQ (funcar, Qlambda)
2836 || EQ (funcar, Qclosure))
2837 val = funcall_lambda (fun, numargs, args + 1);
2838 else if (EQ (funcar, Qautoload))
2840 Fautoload_do_load (fun, original_fun, Qnil);
2841 check_cons_list ();
2842 goto retry;
2844 else
2845 xsignal1 (Qinvalid_function, original_fun);
2847 check_cons_list ();
2848 lisp_eval_depth--;
2849 if (backtrace_debug_on_exit (specpdl + count))
2850 val = call_debugger (list2 (Qexit, val));
2851 specpdl_ptr--;
2852 return val;
2856 /* Apply a C subroutine SUBR to the NUMARGS evaluated arguments in ARG_VECTOR
2857 and return the result of evaluation. */
2859 Lisp_Object
2860 funcall_subr (struct Lisp_Subr *subr, ptrdiff_t numargs, Lisp_Object *args)
2862 if (numargs < subr->min_args
2863 || (subr->max_args >= 0 && subr->max_args < numargs))
2865 Lisp_Object fun;
2866 XSETSUBR (fun, subr);
2867 xsignal2 (Qwrong_number_of_arguments, fun, make_number (numargs));
2870 else if (subr->max_args == UNEVALLED)
2872 Lisp_Object fun;
2873 XSETSUBR (fun, subr);
2874 xsignal1 (Qinvalid_function, fun);
2877 else if (subr->max_args == MANY)
2878 return (subr->function.aMANY) (numargs, args);
2879 else
2881 Lisp_Object internal_argbuf[8];
2882 Lisp_Object *internal_args;
2883 if (subr->max_args > numargs)
2885 eassert (subr->max_args <= ARRAYELTS (internal_argbuf));
2886 internal_args = internal_argbuf;
2887 memcpy (internal_args, args, numargs * word_size);
2888 memclear (internal_args + numargs,
2889 (subr->max_args - numargs) * word_size);
2891 else
2892 internal_args = args;
2893 switch (subr->max_args)
2895 case 0:
2896 return (subr->function.a0 ());
2897 case 1:
2898 return (subr->function.a1 (internal_args[0]));
2899 case 2:
2900 return (subr->function.a2
2901 (internal_args[0], internal_args[1]));
2902 case 3:
2903 return (subr->function.a3
2904 (internal_args[0], internal_args[1], internal_args[2]));
2905 case 4:
2906 return (subr->function.a4
2907 (internal_args[0], internal_args[1], internal_args[2],
2908 internal_args[3]));
2909 case 5:
2910 return (subr->function.a5
2911 (internal_args[0], internal_args[1], internal_args[2],
2912 internal_args[3], internal_args[4]));
2913 case 6:
2914 return (subr->function.a6
2915 (internal_args[0], internal_args[1], internal_args[2],
2916 internal_args[3], internal_args[4], internal_args[5]));
2917 case 7:
2918 return (subr->function.a7
2919 (internal_args[0], internal_args[1], internal_args[2],
2920 internal_args[3], internal_args[4], internal_args[5],
2921 internal_args[6]));
2922 case 8:
2923 return (subr->function.a8
2924 (internal_args[0], internal_args[1], internal_args[2],
2925 internal_args[3], internal_args[4], internal_args[5],
2926 internal_args[6], internal_args[7]));
2928 default:
2930 /* If a subr takes more than 8 arguments without using MANY
2931 or UNEVALLED, we need to extend this function to support it.
2932 Until this is done, there is no way to call the function. */
2933 emacs_abort ();
2938 static Lisp_Object
2939 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2941 Lisp_Object args_left;
2942 ptrdiff_t i;
2943 EMACS_INT numargs;
2944 Lisp_Object *arg_vector;
2945 Lisp_Object tem;
2946 USE_SAFE_ALLOCA;
2948 numargs = XFASTINT (Flength (args));
2949 SAFE_ALLOCA_LISP (arg_vector, numargs);
2950 args_left = args;
2952 for (i = 0; i < numargs; )
2954 tem = Fcar (args_left), args_left = Fcdr (args_left);
2955 tem = eval_sub (tem);
2956 arg_vector[i++] = tem;
2959 set_backtrace_args (specpdl + count, arg_vector, i);
2960 tem = funcall_lambda (fun, numargs, arg_vector);
2962 check_cons_list ();
2963 lisp_eval_depth--;
2964 /* Do the debug-on-exit now, while arg_vector still exists. */
2965 if (backtrace_debug_on_exit (specpdl + count))
2966 tem = call_debugger (list2 (Qexit, tem));
2967 SAFE_FREE ();
2968 specpdl_ptr--;
2969 return tem;
2972 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2973 and return the result of evaluation.
2974 FUN must be either a lambda-expression, a compiled-code object,
2975 or a module function. */
2977 static Lisp_Object
2978 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2979 register Lisp_Object *arg_vector)
2981 Lisp_Object val, syms_left, next, lexenv;
2982 ptrdiff_t count = SPECPDL_INDEX ();
2983 ptrdiff_t i;
2984 bool optional, rest;
2986 if (CONSP (fun))
2988 if (EQ (XCAR (fun), Qclosure))
2990 Lisp_Object cdr = XCDR (fun); /* Drop `closure'. */
2991 if (! CONSP (cdr))
2992 xsignal1 (Qinvalid_function, fun);
2993 fun = cdr;
2994 lexenv = XCAR (fun);
2996 else
2997 lexenv = Qnil;
2998 syms_left = XCDR (fun);
2999 if (CONSP (syms_left))
3000 syms_left = XCAR (syms_left);
3001 else
3002 xsignal1 (Qinvalid_function, fun);
3004 else if (COMPILEDP (fun))
3006 ptrdiff_t size = PVSIZE (fun);
3007 if (size <= COMPILED_STACK_DEPTH)
3008 xsignal1 (Qinvalid_function, fun);
3009 syms_left = AREF (fun, COMPILED_ARGLIST);
3010 if (INTEGERP (syms_left))
3011 /* A byte-code object with an integer args template means we
3012 shouldn't bind any arguments, instead just call the byte-code
3013 interpreter directly; it will push arguments as necessary.
3015 Byte-code objects with a nil args template (the default)
3016 have dynamically-bound arguments, and use the
3017 argument-binding code below instead (as do all interpreted
3018 functions, even lexically bound ones). */
3020 /* If we have not actually read the bytecode string
3021 and constants vector yet, fetch them from the file. */
3022 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3023 Ffetch_bytecode (fun);
3024 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3025 AREF (fun, COMPILED_CONSTANTS),
3026 AREF (fun, COMPILED_STACK_DEPTH),
3027 syms_left,
3028 nargs, arg_vector);
3030 lexenv = Qnil;
3032 #ifdef HAVE_MODULES
3033 else if (MODULE_FUNCTIONP (fun))
3034 return funcall_module (fun, nargs, arg_vector);
3035 #endif
3036 else
3037 emacs_abort ();
3039 i = optional = rest = 0;
3040 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3042 maybe_quit ();
3044 next = XCAR (syms_left);
3045 if (!SYMBOLP (next))
3046 xsignal1 (Qinvalid_function, fun);
3048 if (EQ (next, Qand_rest))
3050 if (rest)
3051 xsignal1 (Qinvalid_function, fun);
3052 rest = 1;
3054 else if (EQ (next, Qand_optional))
3056 if (optional || rest)
3057 xsignal1 (Qinvalid_function, fun);
3058 optional = 1;
3060 else
3062 Lisp_Object arg;
3063 if (rest)
3065 arg = Flist (nargs - i, &arg_vector[i]);
3066 i = nargs;
3068 else if (i < nargs)
3069 arg = arg_vector[i++];
3070 else if (!optional)
3071 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3072 else
3073 arg = Qnil;
3075 /* Bind the argument. */
3076 if (!NILP (lexenv) && SYMBOLP (next))
3077 /* Lexically bind NEXT by adding it to the lexenv alist. */
3078 lexenv = Fcons (Fcons (next, arg), lexenv);
3079 else
3080 /* Dynamically bind NEXT. */
3081 specbind (next, arg);
3085 if (!NILP (syms_left))
3086 xsignal1 (Qinvalid_function, fun);
3087 else if (i < nargs)
3088 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3090 if (!EQ (lexenv, Vinternal_interpreter_environment))
3091 /* Instantiate a new lexical environment. */
3092 specbind (Qinternal_interpreter_environment, lexenv);
3094 if (CONSP (fun))
3095 val = Fprogn (XCDR (XCDR (fun)));
3096 else
3098 /* If we have not actually read the bytecode string
3099 and constants vector yet, fetch them from the file. */
3100 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3101 Ffetch_bytecode (fun);
3102 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3103 AREF (fun, COMPILED_CONSTANTS),
3104 AREF (fun, COMPILED_STACK_DEPTH),
3105 Qnil, 0, 0);
3108 return unbind_to (count, val);
3111 DEFUN ("func-arity", Ffunc_arity, Sfunc_arity, 1, 1, 0,
3112 doc: /* Return minimum and maximum number of args allowed for FUNCTION.
3113 FUNCTION must be a function of some kind.
3114 The returned value is a cons cell (MIN . MAX). MIN is the minimum number
3115 of args. MAX is the maximum number, or the symbol `many', for a
3116 function with `&rest' args, or `unevalled' for a special form. */)
3117 (Lisp_Object function)
3119 Lisp_Object original;
3120 Lisp_Object funcar;
3121 Lisp_Object result;
3123 original = function;
3125 retry:
3127 /* Optimize for no indirection. */
3128 function = original;
3129 if (SYMBOLP (function) && !NILP (function))
3131 function = XSYMBOL (function)->u.s.function;
3132 if (SYMBOLP (function))
3133 function = indirect_function (function);
3136 if (CONSP (function) && EQ (XCAR (function), Qmacro))
3137 function = XCDR (function);
3139 if (SUBRP (function))
3140 result = Fsubr_arity (function);
3141 else if (COMPILEDP (function))
3142 result = lambda_arity (function);
3143 #ifdef HAVE_MODULES
3144 else if (MODULE_FUNCTIONP (function))
3145 result = module_function_arity (XMODULE_FUNCTION (function));
3146 #endif
3147 else
3149 if (NILP (function))
3150 xsignal1 (Qvoid_function, original);
3151 if (!CONSP (function))
3152 xsignal1 (Qinvalid_function, original);
3153 funcar = XCAR (function);
3154 if (!SYMBOLP (funcar))
3155 xsignal1 (Qinvalid_function, original);
3156 if (EQ (funcar, Qlambda)
3157 || EQ (funcar, Qclosure))
3158 result = lambda_arity (function);
3159 else if (EQ (funcar, Qautoload))
3161 Fautoload_do_load (function, original, Qnil);
3162 goto retry;
3164 else
3165 xsignal1 (Qinvalid_function, original);
3167 return result;
3170 /* FUN must be either a lambda-expression or a compiled-code object. */
3171 static Lisp_Object
3172 lambda_arity (Lisp_Object fun)
3174 Lisp_Object syms_left;
3176 if (CONSP (fun))
3178 if (EQ (XCAR (fun), Qclosure))
3180 fun = XCDR (fun); /* Drop `closure'. */
3181 CHECK_CONS (fun);
3183 syms_left = XCDR (fun);
3184 if (CONSP (syms_left))
3185 syms_left = XCAR (syms_left);
3186 else
3187 xsignal1 (Qinvalid_function, fun);
3189 else if (COMPILEDP (fun))
3191 ptrdiff_t size = PVSIZE (fun);
3192 if (size <= COMPILED_STACK_DEPTH)
3193 xsignal1 (Qinvalid_function, fun);
3194 syms_left = AREF (fun, COMPILED_ARGLIST);
3195 if (INTEGERP (syms_left))
3196 return get_byte_code_arity (syms_left);
3198 else
3199 emacs_abort ();
3201 EMACS_INT minargs = 0, maxargs = 0;
3202 bool optional = false;
3203 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3205 Lisp_Object next = XCAR (syms_left);
3206 if (!SYMBOLP (next))
3207 xsignal1 (Qinvalid_function, fun);
3209 if (EQ (next, Qand_rest))
3210 return Fcons (make_number (minargs), Qmany);
3211 else if (EQ (next, Qand_optional))
3212 optional = true;
3213 else
3215 if (!optional)
3216 minargs++;
3217 maxargs++;
3221 if (!NILP (syms_left))
3222 xsignal1 (Qinvalid_function, fun);
3224 return Fcons (make_number (minargs), make_number (maxargs));
3227 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3228 1, 1, 0,
3229 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3230 (Lisp_Object object)
3232 Lisp_Object tem;
3234 if (COMPILEDP (object))
3236 ptrdiff_t size = PVSIZE (object);
3237 if (size <= COMPILED_STACK_DEPTH)
3238 xsignal1 (Qinvalid_function, object);
3239 if (CONSP (AREF (object, COMPILED_BYTECODE)))
3241 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3242 if (!CONSP (tem))
3244 tem = AREF (object, COMPILED_BYTECODE);
3245 if (CONSP (tem) && STRINGP (XCAR (tem)))
3246 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3247 else
3248 error ("Invalid byte code");
3250 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3251 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3254 return object;
3257 /* Return true if SYMBOL currently has a let-binding
3258 which was made in the buffer that is now current. */
3260 bool
3261 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3263 union specbinding *p;
3264 Lisp_Object buf = Fcurrent_buffer ();
3266 for (p = specpdl_ptr; p > specpdl; )
3267 if ((--p)->kind > SPECPDL_LET)
3269 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3270 eassert (let_bound_symbol->u.s.redirect != SYMBOL_VARALIAS);
3271 if (symbol == let_bound_symbol
3272 && EQ (specpdl_where (p), buf))
3273 return 1;
3276 return 0;
3279 static void
3280 do_specbind (struct Lisp_Symbol *sym, union specbinding *bind,
3281 Lisp_Object value, enum Set_Internal_Bind bindflag)
3283 switch (sym->u.s.redirect)
3285 case SYMBOL_PLAINVAL:
3286 if (!sym->u.s.trapped_write)
3287 SET_SYMBOL_VAL (sym, value);
3288 else
3289 set_internal (specpdl_symbol (bind), value, Qnil, bindflag);
3290 break;
3292 case SYMBOL_FORWARDED:
3293 if (BUFFER_OBJFWDP (SYMBOL_FWD (sym))
3294 && specpdl_kind (bind) == SPECPDL_LET_DEFAULT)
3296 set_default_internal (specpdl_symbol (bind), value, bindflag);
3297 return;
3299 FALLTHROUGH;
3300 case SYMBOL_LOCALIZED:
3301 set_internal (specpdl_symbol (bind), value, Qnil, bindflag);
3302 break;
3304 default:
3305 emacs_abort ();
3309 /* `specpdl_ptr' describes which variable is
3310 let-bound, so it can be properly undone when we unbind_to.
3311 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3312 - SYMBOL is the variable being bound. Note that it should not be
3313 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3314 to record V2 here).
3315 - WHERE tells us in which buffer the binding took place.
3316 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3317 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3318 i.e. bindings to the default value of a variable which can be
3319 buffer-local. */
3321 void
3322 specbind (Lisp_Object symbol, Lisp_Object value)
3324 struct Lisp_Symbol *sym;
3326 CHECK_SYMBOL (symbol);
3327 sym = XSYMBOL (symbol);
3329 start:
3330 switch (sym->u.s.redirect)
3332 case SYMBOL_VARALIAS:
3333 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3334 case SYMBOL_PLAINVAL:
3335 /* The most common case is that of a non-constant symbol with a
3336 trivial value. Make that as fast as we can. */
3337 specpdl_ptr->let.kind = SPECPDL_LET;
3338 specpdl_ptr->let.symbol = symbol;
3339 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3340 specpdl_ptr->let.saved_value = Qnil;
3341 grow_specpdl ();
3342 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3343 break;
3344 case SYMBOL_LOCALIZED:
3345 case SYMBOL_FORWARDED:
3347 Lisp_Object ovalue = find_symbol_value (symbol);
3348 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3349 specpdl_ptr->let.symbol = symbol;
3350 specpdl_ptr->let.old_value = ovalue;
3351 specpdl_ptr->let.where = Fcurrent_buffer ();
3352 specpdl_ptr->let.saved_value = Qnil;
3354 eassert (sym->u.s.redirect != SYMBOL_LOCALIZED
3355 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3357 if (sym->u.s.redirect == SYMBOL_LOCALIZED)
3359 if (!blv_found (SYMBOL_BLV (sym)))
3360 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3362 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3364 /* If SYMBOL is a per-buffer variable which doesn't have a
3365 buffer-local value here, make the `let' change the global
3366 value by changing the value of SYMBOL in all buffers not
3367 having their own value. This is consistent with what
3368 happens with other buffer-local variables. */
3369 if (NILP (Flocal_variable_p (symbol, Qnil)))
3371 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3372 grow_specpdl ();
3373 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3374 return;
3377 else
3378 specpdl_ptr->let.kind = SPECPDL_LET;
3380 grow_specpdl ();
3381 do_specbind (sym, specpdl_ptr - 1, value, SET_INTERNAL_BIND);
3382 break;
3384 default: emacs_abort ();
3388 /* Push unwind-protect entries of various types. */
3390 void
3391 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3393 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3394 specpdl_ptr->unwind.func = function;
3395 specpdl_ptr->unwind.arg = arg;
3396 grow_specpdl ();
3399 void
3400 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3402 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3403 specpdl_ptr->unwind_ptr.func = function;
3404 specpdl_ptr->unwind_ptr.arg = arg;
3405 grow_specpdl ();
3408 void
3409 record_unwind_protect_int (void (*function) (int), int arg)
3411 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3412 specpdl_ptr->unwind_int.func = function;
3413 specpdl_ptr->unwind_int.arg = arg;
3414 grow_specpdl ();
3417 void
3418 record_unwind_protect_void (void (*function) (void))
3420 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3421 specpdl_ptr->unwind_void.func = function;
3422 grow_specpdl ();
3425 void
3426 rebind_for_thread_switch (void)
3428 union specbinding *bind;
3430 for (bind = specpdl; bind != specpdl_ptr; ++bind)
3432 if (bind->kind >= SPECPDL_LET)
3434 Lisp_Object value = specpdl_saved_value (bind);
3435 Lisp_Object sym = specpdl_symbol (bind);
3436 bind->let.saved_value = Qnil;
3437 do_specbind (XSYMBOL (sym), bind, value,
3438 SET_INTERNAL_THREAD_SWITCH);
3443 static void
3444 do_one_unbind (union specbinding *this_binding, bool unwinding,
3445 enum Set_Internal_Bind bindflag)
3447 eassert (unwinding || this_binding->kind >= SPECPDL_LET);
3448 switch (this_binding->kind)
3450 case SPECPDL_UNWIND:
3451 this_binding->unwind.func (this_binding->unwind.arg);
3452 break;
3453 case SPECPDL_UNWIND_PTR:
3454 this_binding->unwind_ptr.func (this_binding->unwind_ptr.arg);
3455 break;
3456 case SPECPDL_UNWIND_INT:
3457 this_binding->unwind_int.func (this_binding->unwind_int.arg);
3458 break;
3459 case SPECPDL_UNWIND_VOID:
3460 this_binding->unwind_void.func ();
3461 break;
3462 case SPECPDL_BACKTRACE:
3463 break;
3464 case SPECPDL_LET:
3465 { /* If variable has a trivial value (no forwarding), and isn't
3466 trapped, we can just set it. */
3467 Lisp_Object sym = specpdl_symbol (this_binding);
3468 if (SYMBOLP (sym) && XSYMBOL (sym)->u.s.redirect == SYMBOL_PLAINVAL)
3470 if (XSYMBOL (sym)->u.s.trapped_write == SYMBOL_UNTRAPPED_WRITE)
3471 SET_SYMBOL_VAL (XSYMBOL (sym), specpdl_old_value (this_binding));
3472 else
3473 set_internal (sym, specpdl_old_value (this_binding),
3474 Qnil, bindflag);
3475 break;
3478 /* Come here only if make_local_foo was used for the first time
3479 on this var within this let. */
3480 FALLTHROUGH;
3481 case SPECPDL_LET_DEFAULT:
3482 set_default_internal (specpdl_symbol (this_binding),
3483 specpdl_old_value (this_binding),
3484 bindflag);
3485 break;
3486 case SPECPDL_LET_LOCAL:
3488 Lisp_Object symbol = specpdl_symbol (this_binding);
3489 Lisp_Object where = specpdl_where (this_binding);
3490 Lisp_Object old_value = specpdl_old_value (this_binding);
3491 eassert (BUFFERP (where));
3493 /* If this was a local binding, reset the value in the appropriate
3494 buffer, but only if that buffer's binding still exists. */
3495 if (!NILP (Flocal_variable_p (symbol, where)))
3496 set_internal (symbol, old_value, where, bindflag);
3498 break;
3502 static void
3503 do_nothing (void)
3506 /* Push an unwind-protect entry that does nothing, so that
3507 set_unwind_protect_ptr can overwrite it later. */
3509 void
3510 record_unwind_protect_nothing (void)
3512 record_unwind_protect_void (do_nothing);
3515 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3516 It need not be at the top of the stack. */
3518 void
3519 clear_unwind_protect (ptrdiff_t count)
3521 union specbinding *p = specpdl + count;
3522 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3523 p->unwind_void.func = do_nothing;
3526 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3527 It need not be at the top of the stack. Discard the entry's
3528 previous value without invoking it. */
3530 void
3531 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3532 Lisp_Object arg)
3534 union specbinding *p = specpdl + count;
3535 p->unwind.kind = SPECPDL_UNWIND;
3536 p->unwind.func = func;
3537 p->unwind.arg = arg;
3540 void
3541 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3543 union specbinding *p = specpdl + count;
3544 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3545 p->unwind_ptr.func = func;
3546 p->unwind_ptr.arg = arg;
3549 /* Pop and execute entries from the unwind-protect stack until the
3550 depth COUNT is reached. Return VALUE. */
3552 Lisp_Object
3553 unbind_to (ptrdiff_t count, Lisp_Object value)
3555 Lisp_Object quitf = Vquit_flag;
3557 Vquit_flag = Qnil;
3559 while (specpdl_ptr != specpdl + count)
3561 /* Copy the binding, and decrement specpdl_ptr, before we do
3562 the work to unbind it. We decrement first
3563 so that an error in unbinding won't try to unbind
3564 the same entry again, and we copy the binding first
3565 in case more bindings are made during some of the code we run. */
3567 union specbinding this_binding;
3568 this_binding = *--specpdl_ptr;
3570 do_one_unbind (&this_binding, true, SET_INTERNAL_UNBIND);
3573 if (NILP (Vquit_flag) && !NILP (quitf))
3574 Vquit_flag = quitf;
3576 return value;
3579 void
3580 unbind_for_thread_switch (struct thread_state *thr)
3582 union specbinding *bind;
3584 for (bind = thr->m_specpdl_ptr; bind > thr->m_specpdl;)
3586 if ((--bind)->kind >= SPECPDL_LET)
3588 Lisp_Object sym = specpdl_symbol (bind);
3589 bind->let.saved_value = find_symbol_value (sym);
3590 do_one_unbind (bind, false, SET_INTERNAL_THREAD_SWITCH);
3595 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3596 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3597 A special variable is one that will be bound dynamically, even in a
3598 context where binding is lexical by default. */)
3599 (Lisp_Object symbol)
3601 CHECK_SYMBOL (symbol);
3602 return XSYMBOL (symbol)->u.s.declared_special ? Qt : Qnil;
3606 static union specbinding *
3607 get_backtrace_starting_at (Lisp_Object base)
3609 union specbinding *pdl = backtrace_top ();
3611 if (!NILP (base))
3612 { /* Skip up to `base'. */
3613 base = Findirect_function (base, Qt);
3614 while (backtrace_p (pdl)
3615 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3616 pdl = backtrace_next (pdl);
3619 return pdl;
3622 static union specbinding *
3623 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3625 register EMACS_INT i;
3627 CHECK_NATNUM (nframes);
3628 union specbinding *pdl = get_backtrace_starting_at (base);
3630 /* Find the frame requested. */
3631 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3632 pdl = backtrace_next (pdl);
3634 return pdl;
3637 static Lisp_Object
3638 backtrace_frame_apply (Lisp_Object function, union specbinding *pdl)
3640 if (!backtrace_p (pdl))
3641 return Qnil;
3643 Lisp_Object flags = Qnil;
3644 if (backtrace_debug_on_exit (pdl))
3645 flags = Fcons (QCdebug_on_exit, Fcons (Qt, Qnil));
3647 if (backtrace_nargs (pdl) == UNEVALLED)
3648 return call4 (function, Qnil, backtrace_function (pdl), *backtrace_args (pdl), flags);
3649 else
3651 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3652 return call4 (function, Qt, backtrace_function (pdl), tem, flags);
3656 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3657 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3658 The debugger is entered when that frame exits, if the flag is non-nil. */)
3659 (Lisp_Object level, Lisp_Object flag)
3661 CHECK_NUMBER (level);
3662 union specbinding *pdl = get_backtrace_frame(level, Qnil);
3664 if (backtrace_p (pdl))
3665 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3667 return flag;
3670 DEFUN ("mapbacktrace", Fmapbacktrace, Smapbacktrace, 1, 2, 0,
3671 doc: /* Call FUNCTION for each frame in backtrace.
3672 If BASE is non-nil, it should be a function and iteration will start
3673 from its nearest activation frame.
3674 FUNCTION is called with 4 arguments: EVALD, FUNC, ARGS, and FLAGS. If
3675 a frame has not evaluated its arguments yet or is a special form,
3676 EVALD is nil and ARGS is a list of forms. If a frame has evaluated
3677 its arguments and called its function already, EVALD is t and ARGS is
3678 a list of values.
3679 FLAGS is a plist of properties of the current frame: currently, the
3680 only supported property is :debug-on-exit. `mapbacktrace' always
3681 returns nil. */)
3682 (Lisp_Object function, Lisp_Object base)
3684 union specbinding *pdl = get_backtrace_starting_at (base);
3686 while (backtrace_p (pdl))
3688 ptrdiff_t i = pdl - specpdl;
3689 backtrace_frame_apply (function, pdl);
3690 /* Beware! PDL is no longer valid here because FUNCTION might
3691 have caused grow_specpdl to reallocate pdlvec. We must use
3692 the saved index, cf. Bug#27258. */
3693 pdl = backtrace_next (&specpdl[i]);
3696 return Qnil;
3699 DEFUN ("backtrace-frame--internal", Fbacktrace_frame_internal,
3700 Sbacktrace_frame_internal, 3, 3, NULL,
3701 doc: /* Call FUNCTION on stack frame NFRAMES away from BASE.
3702 Return the result of FUNCTION, or nil if no matching frame could be found. */)
3703 (Lisp_Object function, Lisp_Object nframes, Lisp_Object base)
3705 return backtrace_frame_apply (function, get_backtrace_frame (nframes, base));
3708 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3709 the specpdl stack, and then rewind them. We store the pre-unwind values
3710 directly in the pre-existing specpdl elements (i.e. we swap the current
3711 value and the old value stored in the specpdl), kind of like the inplace
3712 pointer-reversal trick. As it turns out, the rewind does the same as the
3713 unwind, except it starts from the other end of the specpdl stack, so we use
3714 the same function for both unwind and rewind. */
3715 static void
3716 backtrace_eval_unrewind (int distance)
3718 union specbinding *tmp = specpdl_ptr;
3719 int step = -1;
3720 if (distance < 0)
3721 { /* It's a rewind rather than unwind. */
3722 tmp += distance - 1;
3723 step = 1;
3724 distance = -distance;
3727 for (; distance > 0; distance--)
3729 tmp += step;
3730 switch (tmp->kind)
3732 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3733 unwind_protect, but the problem is that we don't know how to
3734 rewind them afterwards. */
3735 case SPECPDL_UNWIND:
3737 Lisp_Object oldarg = tmp->unwind.arg;
3738 if (tmp->unwind.func == set_buffer_if_live)
3739 tmp->unwind.arg = Fcurrent_buffer ();
3740 else if (tmp->unwind.func == save_excursion_restore)
3741 tmp->unwind.arg = save_excursion_save ();
3742 else
3743 break;
3744 tmp->unwind.func (oldarg);
3745 break;
3748 case SPECPDL_UNWIND_PTR:
3749 case SPECPDL_UNWIND_INT:
3750 case SPECPDL_UNWIND_VOID:
3751 case SPECPDL_BACKTRACE:
3752 break;
3753 case SPECPDL_LET:
3754 { /* If variable has a trivial value (no forwarding), we can
3755 just set it. No need to check for constant symbols here,
3756 since that was already done by specbind. */
3757 Lisp_Object sym = specpdl_symbol (tmp);
3758 if (SYMBOLP (sym)
3759 && XSYMBOL (sym)->u.s.redirect == SYMBOL_PLAINVAL)
3761 Lisp_Object old_value = specpdl_old_value (tmp);
3762 set_specpdl_old_value (tmp, SYMBOL_VAL (XSYMBOL (sym)));
3763 SET_SYMBOL_VAL (XSYMBOL (sym), old_value);
3764 break;
3767 /* Come here only if make_local_foo was used for the first
3768 time on this var within this let. */
3769 FALLTHROUGH;
3770 case SPECPDL_LET_DEFAULT:
3772 Lisp_Object sym = specpdl_symbol (tmp);
3773 Lisp_Object old_value = specpdl_old_value (tmp);
3774 set_specpdl_old_value (tmp, Fdefault_value (sym));
3775 Fset_default (sym, old_value);
3777 break;
3778 case SPECPDL_LET_LOCAL:
3780 Lisp_Object symbol = specpdl_symbol (tmp);
3781 Lisp_Object where = specpdl_where (tmp);
3782 Lisp_Object old_value = specpdl_old_value (tmp);
3783 eassert (BUFFERP (where));
3785 /* If this was a local binding, reset the value in the appropriate
3786 buffer, but only if that buffer's binding still exists. */
3787 if (!NILP (Flocal_variable_p (symbol, where)))
3789 set_specpdl_old_value
3790 (tmp, Fbuffer_local_value (symbol, where));
3791 set_internal (symbol, old_value, where, SET_INTERNAL_UNBIND);
3794 break;
3799 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3800 doc: /* Evaluate EXP in the context of some activation frame.
3801 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3802 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3804 union specbinding *pdl = get_backtrace_frame (nframes, base);
3805 ptrdiff_t count = SPECPDL_INDEX ();
3806 ptrdiff_t distance = specpdl_ptr - pdl;
3807 eassert (distance >= 0);
3809 if (!backtrace_p (pdl))
3810 error ("Activation frame not found!");
3812 backtrace_eval_unrewind (distance);
3813 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3815 /* Use eval_sub rather than Feval since the main motivation behind
3816 backtrace-eval is to be able to get/set the value of lexical variables
3817 from the debugger. */
3818 return unbind_to (count, eval_sub (exp));
3821 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3822 doc: /* Return names and values of local variables of a stack frame.
3823 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3824 (Lisp_Object nframes, Lisp_Object base)
3826 union specbinding *frame = get_backtrace_frame (nframes, base);
3827 union specbinding *prevframe
3828 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3829 ptrdiff_t distance = specpdl_ptr - frame;
3830 Lisp_Object result = Qnil;
3831 eassert (distance >= 0);
3833 if (!backtrace_p (prevframe))
3834 error ("Activation frame not found!");
3835 if (!backtrace_p (frame))
3836 error ("Activation frame not found!");
3838 /* The specpdl entries normally contain the symbol being bound along with its
3839 `old_value', so it can be restored. The new value to which it is bound is
3840 available in one of two places: either in the current value of the
3841 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3842 next specpdl entry for it.
3843 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3844 and "new value", so we abuse it here, to fetch the new value.
3845 It's ugly (we'd rather not modify global data) and a bit inefficient,
3846 but it does the job for now. */
3847 backtrace_eval_unrewind (distance);
3849 /* Grab values. */
3851 union specbinding *tmp = prevframe;
3852 for (; tmp > frame; tmp--)
3854 switch (tmp->kind)
3856 case SPECPDL_LET:
3857 case SPECPDL_LET_DEFAULT:
3858 case SPECPDL_LET_LOCAL:
3860 Lisp_Object sym = specpdl_symbol (tmp);
3861 Lisp_Object val = specpdl_old_value (tmp);
3862 if (EQ (sym, Qinternal_interpreter_environment))
3864 Lisp_Object env = val;
3865 for (; CONSP (env); env = XCDR (env))
3867 Lisp_Object binding = XCAR (env);
3868 if (CONSP (binding))
3869 result = Fcons (Fcons (XCAR (binding),
3870 XCDR (binding)),
3871 result);
3874 else
3875 result = Fcons (Fcons (sym, val), result);
3877 break;
3879 case SPECPDL_UNWIND:
3880 case SPECPDL_UNWIND_PTR:
3881 case SPECPDL_UNWIND_INT:
3882 case SPECPDL_UNWIND_VOID:
3883 case SPECPDL_BACKTRACE:
3884 break;
3886 default:
3887 emacs_abort ();
3892 /* Restore values from specpdl to original place. */
3893 backtrace_eval_unrewind (-distance);
3895 return result;
3899 void
3900 mark_specpdl (union specbinding *first, union specbinding *ptr)
3902 union specbinding *pdl;
3903 for (pdl = first; pdl != ptr; pdl++)
3905 switch (pdl->kind)
3907 case SPECPDL_UNWIND:
3908 mark_object (specpdl_arg (pdl));
3909 break;
3911 case SPECPDL_BACKTRACE:
3913 ptrdiff_t nargs = backtrace_nargs (pdl);
3914 mark_object (backtrace_function (pdl));
3915 if (nargs == UNEVALLED)
3916 nargs = 1;
3917 while (nargs--)
3918 mark_object (backtrace_args (pdl)[nargs]);
3920 break;
3922 case SPECPDL_LET_DEFAULT:
3923 case SPECPDL_LET_LOCAL:
3924 mark_object (specpdl_where (pdl));
3925 FALLTHROUGH;
3926 case SPECPDL_LET:
3927 mark_object (specpdl_symbol (pdl));
3928 mark_object (specpdl_old_value (pdl));
3929 mark_object (specpdl_saved_value (pdl));
3930 break;
3932 case SPECPDL_UNWIND_PTR:
3933 case SPECPDL_UNWIND_INT:
3934 case SPECPDL_UNWIND_VOID:
3935 break;
3937 default:
3938 emacs_abort ();
3943 void
3944 get_backtrace (Lisp_Object array)
3946 union specbinding *pdl = backtrace_next (backtrace_top ());
3947 ptrdiff_t i = 0, asize = ASIZE (array);
3949 /* Copy the backtrace contents into working memory. */
3950 for (; i < asize; i++)
3952 if (backtrace_p (pdl))
3954 ASET (array, i, backtrace_function (pdl));
3955 pdl = backtrace_next (pdl);
3957 else
3958 ASET (array, i, Qnil);
3962 Lisp_Object backtrace_top_function (void)
3964 union specbinding *pdl = backtrace_top ();
3965 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3968 void
3969 syms_of_eval (void)
3971 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3972 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3973 If Lisp code tries to increase the total number past this amount,
3974 an error is signaled.
3975 You can safely use a value considerably larger than the default value,
3976 if that proves inconveniently small. However, if you increase it too far,
3977 Emacs could run out of memory trying to make the stack bigger.
3978 Note that this limit may be silently increased by the debugger
3979 if `debug-on-error' or `debug-on-quit' is set. */);
3981 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3982 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3984 This limit serves to catch infinite recursions for you before they cause
3985 actual stack overflow in C, which would be fatal for Emacs.
3986 You can safely make it considerably larger than its default value,
3987 if that proves inconveniently small. However, if you increase it too far,
3988 Emacs could overflow the real C stack, and crash. */);
3990 DEFVAR_LISP ("quit-flag", Vquit_flag,
3991 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3992 If the value is t, that means do an ordinary quit.
3993 If the value equals `throw-on-input', that means quit by throwing
3994 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3995 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3996 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3997 Vquit_flag = Qnil;
3999 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
4000 doc: /* Non-nil inhibits C-g quitting from happening immediately.
4001 Note that `quit-flag' will still be set by typing C-g,
4002 so a quit will be signaled as soon as `inhibit-quit' is nil.
4003 To prevent this happening, set `quit-flag' to nil
4004 before making `inhibit-quit' nil. */);
4005 Vinhibit_quit = Qnil;
4007 DEFSYM (Qsetq, "setq");
4008 DEFSYM (Qinhibit_quit, "inhibit-quit");
4009 DEFSYM (Qautoload, "autoload");
4010 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
4011 DEFSYM (Qmacro, "macro");
4013 /* Note that the process handling also uses Qexit, but we don't want
4014 to staticpro it twice, so we just do it here. */
4015 DEFSYM (Qexit, "exit");
4017 DEFSYM (Qinteractive, "interactive");
4018 DEFSYM (Qcommandp, "commandp");
4019 DEFSYM (Qand_rest, "&rest");
4020 DEFSYM (Qand_optional, "&optional");
4021 DEFSYM (Qclosure, "closure");
4022 DEFSYM (QCdocumentation, ":documentation");
4023 DEFSYM (Qdebug, "debug");
4025 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
4026 doc: /* Non-nil means never enter the debugger.
4027 Normally set while the debugger is already active, to avoid recursive
4028 invocations. */);
4029 Vinhibit_debugger = Qnil;
4031 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
4032 doc: /* Non-nil means enter debugger if an error is signaled.
4033 Does not apply to errors handled by `condition-case' or those
4034 matched by `debug-ignored-errors'.
4035 If the value is a list, an error only means to enter the debugger
4036 if one of its condition symbols appears in the list.
4037 When you evaluate an expression interactively, this variable
4038 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
4039 The command `toggle-debug-on-error' toggles this.
4040 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
4041 Vdebug_on_error = Qnil;
4043 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
4044 doc: /* List of errors for which the debugger should not be called.
4045 Each element may be a condition-name or a regexp that matches error messages.
4046 If any element applies to a given error, that error skips the debugger
4047 and just returns to top level.
4048 This overrides the variable `debug-on-error'.
4049 It does not apply to errors handled by `condition-case'. */);
4050 Vdebug_ignored_errors = Qnil;
4052 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
4053 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
4054 Does not apply if quit is handled by a `condition-case'. */);
4055 debug_on_quit = 0;
4057 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
4058 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
4060 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
4061 doc: /* Non-nil means debugger may continue execution.
4062 This is nil when the debugger is called under circumstances where it
4063 might not be safe to continue. */);
4064 debugger_may_continue = 1;
4066 DEFVAR_BOOL ("debugger-stack-frame-as-list", debugger_stack_frame_as_list,
4067 doc: /* Non-nil means display call stack frames as lists. */);
4068 debugger_stack_frame_as_list = 0;
4070 DEFVAR_LISP ("debugger", Vdebugger,
4071 doc: /* Function to call to invoke debugger.
4072 If due to frame exit, args are `exit' and the value being returned;
4073 this function's value will be returned instead of that.
4074 If due to error, args are `error' and a list of the args to `signal'.
4075 If due to `apply' or `funcall' entry, one arg, `lambda'.
4076 If due to `eval' entry, one arg, t. */);
4077 Vdebugger = Qnil;
4079 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
4080 doc: /* If non-nil, this is a function for `signal' to call.
4081 It receives the same arguments that `signal' was given.
4082 The Edebug package uses this to regain control. */);
4083 Vsignal_hook_function = Qnil;
4085 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
4086 doc: /* Non-nil means call the debugger regardless of condition handlers.
4087 Note that `debug-on-error', `debug-on-quit' and friends
4088 still determine whether to handle the particular condition. */);
4089 Vdebug_on_signal = Qnil;
4091 /* When lexical binding is being used,
4092 Vinternal_interpreter_environment is non-nil, and contains an alist
4093 of lexically-bound variable, or (t), indicating an empty
4094 environment. The lisp name of this variable would be
4095 `internal-interpreter-environment' if it weren't hidden.
4096 Every element of this list can be either a cons (VAR . VAL)
4097 specifying a lexical binding, or a single symbol VAR indicating
4098 that this variable should use dynamic scoping. */
4099 DEFSYM (Qinternal_interpreter_environment,
4100 "internal-interpreter-environment");
4101 DEFVAR_LISP ("internal-interpreter-environment",
4102 Vinternal_interpreter_environment,
4103 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
4104 When lexical binding is not being used, this variable is nil.
4105 A value of `(t)' indicates an empty environment, otherwise it is an
4106 alist of active lexical bindings. */);
4107 Vinternal_interpreter_environment = Qnil;
4108 /* Don't export this variable to Elisp, so no one can mess with it
4109 (Just imagine if someone makes it buffer-local). */
4110 Funintern (Qinternal_interpreter_environment, Qnil);
4112 Vrun_hooks = intern_c_string ("run-hooks");
4113 staticpro (&Vrun_hooks);
4115 staticpro (&Vautoload_queue);
4116 Vautoload_queue = Qnil;
4117 staticpro (&Vsignaling_function);
4118 Vsignaling_function = Qnil;
4120 inhibit_lisp_code = Qnil;
4122 DEFSYM (Qcatch_all_memory_full, "catch-all-memory-full");
4123 Funintern (Qcatch_all_memory_full, Qnil);
4125 defsubr (&Sor);
4126 defsubr (&Sand);
4127 defsubr (&Sif);
4128 defsubr (&Scond);
4129 defsubr (&Sprogn);
4130 defsubr (&Sprog1);
4131 defsubr (&Sprog2);
4132 defsubr (&Ssetq);
4133 defsubr (&Squote);
4134 defsubr (&Sfunction);
4135 defsubr (&Sdefault_toplevel_value);
4136 defsubr (&Sset_default_toplevel_value);
4137 defsubr (&Sdefvar);
4138 defsubr (&Sdefvaralias);
4139 DEFSYM (Qdefvaralias, "defvaralias");
4140 defsubr (&Sdefconst);
4141 defsubr (&Smake_var_non_special);
4142 defsubr (&Slet);
4143 defsubr (&SletX);
4144 defsubr (&Swhile);
4145 defsubr (&Smacroexpand);
4146 defsubr (&Scatch);
4147 defsubr (&Sthrow);
4148 defsubr (&Sunwind_protect);
4149 defsubr (&Scondition_case);
4150 defsubr (&Ssignal);
4151 defsubr (&Scommandp);
4152 defsubr (&Sautoload);
4153 defsubr (&Sautoload_do_load);
4154 defsubr (&Seval);
4155 defsubr (&Sapply);
4156 defsubr (&Sfuncall);
4157 defsubr (&Sfunc_arity);
4158 defsubr (&Srun_hooks);
4159 defsubr (&Srun_hook_with_args);
4160 defsubr (&Srun_hook_with_args_until_success);
4161 defsubr (&Srun_hook_with_args_until_failure);
4162 defsubr (&Srun_hook_wrapped);
4163 defsubr (&Sfetch_bytecode);
4164 defsubr (&Sbacktrace_debug);
4165 DEFSYM (QCdebug_on_exit, ":debug-on-exit");
4166 defsubr (&Smapbacktrace);
4167 defsubr (&Sbacktrace_frame_internal);
4168 defsubr (&Sbacktrace_eval);
4169 defsubr (&Sbacktrace__locals);
4170 defsubr (&Sspecial_variable_p);
4171 defsubr (&Sfunctionp);