Check autoload's "type" argument correctly in bytecomp.el
[emacs.git] / src / eval.c
blobd3545add21dc706966f7874d316a3374fd44248d
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 #if !BYTE_MARK_STACK
36 static
37 #endif
38 struct catchtag *catchlist;
40 /* Chain of condition handlers currently in effect.
41 The elements of this chain are contained in the stack frames
42 of Fcondition_case and internal_condition_case.
43 When an error is signaled (by calling Fsignal, below),
44 this chain is searched for an element that applies. */
46 #if !BYTE_MARK_STACK
47 static
48 #endif
49 struct handler *handlerlist;
51 #ifdef DEBUG_GCPRO
52 /* Count levels of GCPRO to detect failure to UNGCPRO. */
53 int gcpro_level;
54 #endif
56 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57 Lisp_Object Qinhibit_quit;
58 Lisp_Object Qand_rest;
59 static Lisp_Object Qand_optional;
60 static Lisp_Object Qinhibit_debugger;
61 static Lisp_Object Qdeclare;
62 Lisp_Object Qinternal_interpreter_environment, Qclosure;
64 static Lisp_Object Qdebug;
66 /* This holds either the symbol `run-hooks' or nil.
67 It is nil at an early stage of startup, and when Emacs
68 is shutting down. */
70 Lisp_Object Vrun_hooks;
72 /* Non-nil means record all fset's and provide's, to be undone
73 if the file being autoloaded is not fully loaded.
74 They are recorded by being consed onto the front of Vautoload_queue:
75 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
77 Lisp_Object Vautoload_queue;
79 /* Current number of specbindings allocated in specpdl, not counting
80 the dummy entry specpdl[-1]. */
82 ptrdiff_t specpdl_size;
84 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
87 union specbinding *specpdl;
89 /* Pointer to first unused element in specpdl. */
91 union specbinding *specpdl_ptr;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
102 invocations. */
104 static EMACS_INT when_entered_debugger;
106 /* The function from which the last `signal' was called. Set in
107 Fsignal. */
108 /* FIXME: We should probably get rid of this! */
109 Lisp_Object Vsignaling_function;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code;
117 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
118 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
120 static Lisp_Object
121 specpdl_symbol (union specbinding *pdl)
123 eassert (pdl->kind >= SPECPDL_LET);
124 return pdl->let.symbol;
127 static Lisp_Object
128 specpdl_old_value (union specbinding *pdl)
130 eassert (pdl->kind >= SPECPDL_LET);
131 return pdl->let.old_value;
134 static Lisp_Object
135 specpdl_where (union specbinding *pdl)
137 eassert (pdl->kind > SPECPDL_LET);
138 return pdl->let.where;
141 static Lisp_Object
142 specpdl_arg (union specbinding *pdl)
144 eassert (pdl->kind == SPECPDL_UNWIND);
145 return pdl->unwind.arg;
148 static specbinding_func
149 specpdl_func (union specbinding *pdl)
151 eassert (pdl->kind == SPECPDL_UNWIND);
152 return pdl->unwind.func;
155 static Lisp_Object
156 backtrace_function (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_BACKTRACE);
159 return pdl->bt.function;
162 static ptrdiff_t
163 backtrace_nargs (union specbinding *pdl)
165 eassert (pdl->kind == SPECPDL_BACKTRACE);
166 return pdl->bt.nargs;
169 static Lisp_Object *
170 backtrace_args (union specbinding *pdl)
172 eassert (pdl->kind == SPECPDL_BACKTRACE);
173 return pdl->bt.args;
176 static bool
177 backtrace_debug_on_exit (union specbinding *pdl)
179 eassert (pdl->kind == SPECPDL_BACKTRACE);
180 return pdl->bt.debug_on_exit;
183 /* Functions to modify slots of backtrace records. */
185 static void
186 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
188 eassert (pdl->kind == SPECPDL_BACKTRACE);
189 pdl->bt.args = args;
192 static void
193 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
195 eassert (pdl->kind == SPECPDL_BACKTRACE);
196 pdl->bt.nargs = n;
199 static void
200 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
202 eassert (pdl->kind == SPECPDL_BACKTRACE);
203 pdl->bt.debug_on_exit = doe;
206 /* Helper functions to scan the backtrace. */
208 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
209 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
210 union specbinding *backtrace_next (union specbinding *pdl) EXTERNALLY_VISIBLE;
212 bool
213 backtrace_p (union specbinding *pdl)
214 { return pdl >= specpdl; }
216 union specbinding *
217 backtrace_top (void)
219 union specbinding *pdl = specpdl_ptr - 1;
220 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
221 pdl--;
222 return pdl;
225 union specbinding *
226 backtrace_next (union specbinding *pdl)
228 pdl--;
229 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
230 pdl--;
231 return pdl;
235 void
236 init_eval_once (void)
238 enum { size = 50 };
239 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
240 specpdl_size = size;
241 specpdl = specpdl_ptr = pdlvec + 1;
242 /* Don't forget to update docs (lispref node "Local Variables"). */
243 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
244 max_lisp_eval_depth = 600;
246 Vrun_hooks = Qnil;
249 void
250 init_eval (void)
252 specpdl_ptr = specpdl;
253 catchlist = 0;
254 handlerlist = 0;
255 Vquit_flag = Qnil;
256 debug_on_next_call = 0;
257 lisp_eval_depth = 0;
258 #ifdef DEBUG_GCPRO
259 gcpro_level = 0;
260 #endif
261 /* This is less than the initial value of num_nonmacro_input_events. */
262 when_entered_debugger = -1;
265 /* Unwind-protect function used by call_debugger. */
267 static Lisp_Object
268 restore_stack_limits (Lisp_Object data)
270 max_specpdl_size = XINT (XCAR (data));
271 max_lisp_eval_depth = XINT (XCDR (data));
272 return Qnil;
275 /* Call the Lisp debugger, giving it argument ARG. */
277 Lisp_Object
278 call_debugger (Lisp_Object arg)
280 bool debug_while_redisplaying;
281 ptrdiff_t count = SPECPDL_INDEX ();
282 Lisp_Object val;
283 EMACS_INT old_max = max_specpdl_size;
285 /* Temporarily bump up the stack limits,
286 so the debugger won't run out of stack. */
288 max_specpdl_size += 1;
289 record_unwind_protect (restore_stack_limits,
290 Fcons (make_number (old_max),
291 make_number (max_lisp_eval_depth)));
292 max_specpdl_size = old_max;
294 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
295 max_lisp_eval_depth = lisp_eval_depth + 40;
297 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
298 max_specpdl_size = SPECPDL_INDEX () + 100;
300 #ifdef HAVE_WINDOW_SYSTEM
301 if (display_hourglass_p)
302 cancel_hourglass ();
303 #endif
305 debug_on_next_call = 0;
306 when_entered_debugger = num_nonmacro_input_events;
308 /* Resetting redisplaying_p to 0 makes sure that debug output is
309 displayed if the debugger is invoked during redisplay. */
310 debug_while_redisplaying = redisplaying_p;
311 redisplaying_p = 0;
312 specbind (intern ("debugger-may-continue"),
313 debug_while_redisplaying ? Qnil : Qt);
314 specbind (Qinhibit_redisplay, Qnil);
315 specbind (Qinhibit_debugger, Qt);
317 #if 0 /* Binding this prevents execution of Lisp code during
318 redisplay, which necessarily leads to display problems. */
319 specbind (Qinhibit_eval_during_redisplay, Qt);
320 #endif
322 val = apply1 (Vdebugger, arg);
324 /* Interrupting redisplay and resuming it later is not safe under
325 all circumstances. So, when the debugger returns, abort the
326 interrupted redisplay by going back to the top-level. */
327 if (debug_while_redisplaying)
328 Ftop_level ();
330 return unbind_to (count, val);
333 static void
334 do_debug_on_call (Lisp_Object code)
336 debug_on_next_call = 0;
337 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
338 call_debugger (Fcons (code, Qnil));
341 /* NOTE!!! Every function that can call EVAL must protect its args
342 and temporaries from garbage collection while it needs them.
343 The definition of `For' shows what you have to do. */
345 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
346 doc: /* Eval args until one of them yields non-nil, then return that value.
347 The remaining args are not evalled at all.
348 If all args return nil, return nil.
349 usage: (or CONDITIONS...) */)
350 (Lisp_Object args)
352 register Lisp_Object val = Qnil;
353 struct gcpro gcpro1;
355 GCPRO1 (args);
357 while (CONSP (args))
359 val = eval_sub (XCAR (args));
360 if (!NILP (val))
361 break;
362 args = XCDR (args);
365 UNGCPRO;
366 return val;
369 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
370 doc: /* Eval args until one of them yields nil, then return nil.
371 The remaining args are not evalled at all.
372 If no arg yields nil, return the last arg's value.
373 usage: (and CONDITIONS...) */)
374 (Lisp_Object args)
376 register Lisp_Object val = Qt;
377 struct gcpro gcpro1;
379 GCPRO1 (args);
381 while (CONSP (args))
383 val = eval_sub (XCAR (args));
384 if (NILP (val))
385 break;
386 args = XCDR (args);
389 UNGCPRO;
390 return val;
393 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
394 doc: /* If COND yields non-nil, do THEN, else do ELSE...
395 Returns the value of THEN or the value of the last of the ELSE's.
396 THEN must be one expression, but ELSE... can be zero or more expressions.
397 If COND yields nil, and there are no ELSE's, the value is nil.
398 usage: (if COND THEN ELSE...) */)
399 (Lisp_Object args)
401 register Lisp_Object cond;
402 struct gcpro gcpro1;
404 GCPRO1 (args);
405 cond = eval_sub (Fcar (args));
406 UNGCPRO;
408 if (!NILP (cond))
409 return eval_sub (Fcar (Fcdr (args)));
410 return Fprogn (Fcdr (Fcdr (args)));
413 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
414 doc: /* Try each clause until one succeeds.
415 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
416 and, if the value is non-nil, this clause succeeds:
417 then the expressions in BODY are evaluated and the last one's
418 value is the value of the cond-form.
419 If no clause succeeds, cond returns nil.
420 If a clause has one element, as in (CONDITION),
421 CONDITION's value if non-nil is returned from the cond-form.
422 usage: (cond CLAUSES...) */)
423 (Lisp_Object args)
425 register Lisp_Object clause, val;
426 struct gcpro gcpro1;
428 val = Qnil;
429 GCPRO1 (args);
430 while (!NILP (args))
432 clause = Fcar (args);
433 val = eval_sub (Fcar (clause));
434 if (!NILP (val))
436 if (!EQ (XCDR (clause), Qnil))
437 val = Fprogn (XCDR (clause));
438 break;
440 args = XCDR (args);
442 UNGCPRO;
444 return val;
447 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
448 doc: /* Eval BODY forms sequentially and return value of last one.
449 usage: (progn BODY...) */)
450 (Lisp_Object args)
452 register Lisp_Object val = Qnil;
453 struct gcpro gcpro1;
455 GCPRO1 (args);
457 while (CONSP (args))
459 val = eval_sub (XCAR (args));
460 args = XCDR (args);
463 UNGCPRO;
464 return val;
467 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
468 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
469 The value of FIRST is saved during the evaluation of the remaining args,
470 whose values are discarded.
471 usage: (prog1 FIRST BODY...) */)
472 (Lisp_Object args)
474 Lisp_Object val;
475 register Lisp_Object args_left;
476 struct gcpro gcpro1, gcpro2;
478 args_left = args;
479 val = Qnil;
480 GCPRO2 (args, val);
482 val = eval_sub (XCAR (args_left));
483 while (CONSP (args_left = XCDR (args_left)))
484 eval_sub (XCAR (args_left));
486 UNGCPRO;
487 return val;
490 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
491 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
492 The value of FORM2 is saved during the evaluation of the
493 remaining args, whose values are discarded.
494 usage: (prog2 FORM1 FORM2 BODY...) */)
495 (Lisp_Object args)
497 struct gcpro gcpro1;
499 GCPRO1 (args);
500 eval_sub (XCAR (args));
501 UNGCPRO;
502 return Fprog1 (XCDR (args));
505 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
506 doc: /* Set each SYM to the value of its VAL.
507 The symbols SYM are variables; they are literal (not evaluated).
508 The values VAL are expressions; they are evaluated.
509 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
510 The second VAL is not computed until after the first SYM is set, and so on;
511 each VAL can use the new value of variables set earlier in the `setq'.
512 The return value of the `setq' form is the value of the last VAL.
513 usage: (setq [SYM VAL]...) */)
514 (Lisp_Object args)
516 register Lisp_Object args_left;
517 register Lisp_Object val, sym, lex_binding;
518 struct gcpro gcpro1;
520 if (NILP (args))
521 return Qnil;
523 args_left = args;
524 GCPRO1 (args);
528 val = eval_sub (Fcar (Fcdr (args_left)));
529 sym = Fcar (args_left);
531 /* Like for eval_sub, we do not check declared_special here since
532 it's been done when let-binding. */
533 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
534 && SYMBOLP (sym)
535 && !NILP (lex_binding
536 = Fassq (sym, Vinternal_interpreter_environment)))
537 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
538 else
539 Fset (sym, val); /* SYM is dynamically bound. */
541 args_left = Fcdr (Fcdr (args_left));
543 while (!NILP (args_left));
545 UNGCPRO;
546 return val;
549 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
550 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
551 Warning: `quote' does not construct its return value, but just returns
552 the value that was pre-constructed by the Lisp reader (see info node
553 `(elisp)Printed Representation').
554 This means that '(a . b) is not identical to (cons 'a 'b): the former
555 does not cons. Quoting should be reserved for constants that will
556 never be modified by side-effects, unless you like self-modifying code.
557 See the common pitfall in info node `(elisp)Rearrangement' for an example
558 of unexpected results when a quoted object is modified.
559 usage: (quote ARG) */)
560 (Lisp_Object args)
562 if (!NILP (Fcdr (args)))
563 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
564 return Fcar (args);
567 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
568 doc: /* Like `quote', but preferred for objects which are functions.
569 In byte compilation, `function' causes its argument to be compiled.
570 `quote' cannot do that.
571 usage: (function ARG) */)
572 (Lisp_Object args)
574 Lisp_Object quoted = XCAR (args);
576 if (!NILP (Fcdr (args)))
577 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
579 if (!NILP (Vinternal_interpreter_environment)
580 && CONSP (quoted)
581 && EQ (XCAR (quoted), Qlambda))
582 /* This is a lambda expression within a lexical environment;
583 return an interpreted closure instead of a simple lambda. */
584 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
585 XCDR (quoted)));
586 else
587 /* Simply quote the argument. */
588 return quoted;
592 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
593 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
594 Aliased variables always have the same value; setting one sets the other.
595 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
596 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
597 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
598 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
599 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
600 The return value is BASE-VARIABLE. */)
601 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
603 struct Lisp_Symbol *sym;
605 CHECK_SYMBOL (new_alias);
606 CHECK_SYMBOL (base_variable);
608 sym = XSYMBOL (new_alias);
610 if (sym->constant)
611 /* Not sure why, but why not? */
612 error ("Cannot make a constant an alias");
614 switch (sym->redirect)
616 case SYMBOL_FORWARDED:
617 error ("Cannot make an internal variable an alias");
618 case SYMBOL_LOCALIZED:
619 error ("Don't know how to make a localized variable an alias");
622 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
623 If n_a is bound, but b_v is not, set the value of b_v to n_a,
624 so that old-code that affects n_a before the aliasing is setup
625 still works. */
626 if (NILP (Fboundp (base_variable)))
627 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
630 union specbinding *p;
632 for (p = specpdl_ptr; p > specpdl; )
633 if ((--p)->kind >= SPECPDL_LET
634 && (EQ (new_alias, specpdl_symbol (p))))
635 error ("Don't know how to make a let-bound variable an alias");
638 sym->declared_special = 1;
639 XSYMBOL (base_variable)->declared_special = 1;
640 sym->redirect = SYMBOL_VARALIAS;
641 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
642 sym->constant = SYMBOL_CONSTANT_P (base_variable);
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;
651 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
652 doc: /* Define SYMBOL as a variable, and return SYMBOL.
653 You are not required to define a variable in order to use it, but
654 defining it lets you supply an initial value and documentation, which
655 can be referred to by the Emacs help facilities and other programming
656 tools. The `defvar' form also declares the variable as \"special\",
657 so that it is always dynamically bound even if `lexical-binding' is t.
659 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
660 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
661 default value is what is set; buffer-local values are not affected.
662 If INITVALUE is missing, SYMBOL's value is not set.
664 If SYMBOL has a local binding, then this form affects the local
665 binding. This is usually not what you want. Thus, if you need to
666 load a file defining variables, with this form or with `defconst' or
667 `defcustom', you should always load that file _outside_ any bindings
668 for these variables. \(`defconst' and `defcustom' behave similarly in
669 this respect.)
671 The optional argument DOCSTRING is a documentation string for the
672 variable.
674 To define a user option, use `defcustom' instead of `defvar'.
675 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
676 (Lisp_Object args)
678 register Lisp_Object sym, tem, tail;
680 sym = Fcar (args);
681 tail = Fcdr (args);
682 if (!NILP (Fcdr (Fcdr (tail))))
683 error ("Too many arguments");
685 tem = Fdefault_boundp (sym);
686 if (!NILP (tail))
688 /* Do it before evaluating the initial value, for self-references. */
689 XSYMBOL (sym)->declared_special = 1;
691 if (NILP (tem))
692 Fset_default (sym, eval_sub (Fcar (tail)));
693 else
694 { /* Check if there is really a global binding rather than just a let
695 binding that shadows the global unboundness of the var. */
696 union specbinding *pdl = specpdl_ptr;
697 while (pdl > specpdl)
699 if ((--pdl)->kind >= SPECPDL_LET
700 && EQ (specpdl_symbol (pdl), sym)
701 && EQ (specpdl_old_value (pdl), Qunbound))
703 message_with_string
704 ("Warning: defvar ignored because %s is let-bound",
705 SYMBOL_NAME (sym), 1);
706 break;
710 tail = Fcdr (tail);
711 tem = Fcar (tail);
712 if (!NILP (tem))
714 if (!NILP (Vpurify_flag))
715 tem = Fpurecopy (tem);
716 Fput (sym, Qvariable_documentation, tem);
718 LOADHIST_ATTACH (sym);
720 else if (!NILP (Vinternal_interpreter_environment)
721 && !XSYMBOL (sym)->declared_special)
722 /* A simple (defvar foo) with lexical scoping does "nothing" except
723 declare that var to be dynamically scoped *locally* (i.e. within
724 the current file or let-block). */
725 Vinternal_interpreter_environment
726 = Fcons (sym, Vinternal_interpreter_environment);
727 else
729 /* Simple (defvar <var>) should not count as a definition at all.
730 It could get in the way of other definitions, and unloading this
731 package could try to make the variable unbound. */
734 return sym;
737 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
738 doc: /* Define SYMBOL as a constant variable.
739 This declares that neither programs nor users should ever change the
740 value. This constancy is not actually enforced by Emacs Lisp, but
741 SYMBOL is marked as a special variable so that it is never lexically
742 bound.
744 The `defconst' form always sets the value of SYMBOL to the result of
745 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
746 what is set; buffer-local values are not affected. If SYMBOL has a
747 local binding, then this form sets the local binding's value.
748 However, you should normally not make local bindings for variables
749 defined with this form.
751 The optional DOCSTRING specifies the variable's documentation string.
752 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
753 (Lisp_Object args)
755 register Lisp_Object sym, tem;
757 sym = Fcar (args);
758 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
759 error ("Too many arguments");
761 tem = eval_sub (Fcar (Fcdr (args)));
762 if (!NILP (Vpurify_flag))
763 tem = Fpurecopy (tem);
764 Fset_default (sym, tem);
765 XSYMBOL (sym)->declared_special = 1;
766 tem = Fcar (Fcdr (Fcdr (args)));
767 if (!NILP (tem))
769 if (!NILP (Vpurify_flag))
770 tem = Fpurecopy (tem);
771 Fput (sym, Qvariable_documentation, tem);
773 Fput (sym, Qrisky_local_variable, Qt);
774 LOADHIST_ATTACH (sym);
775 return sym;
778 /* Make SYMBOL lexically scoped. */
779 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
780 Smake_var_non_special, 1, 1, 0,
781 doc: /* Internal function. */)
782 (Lisp_Object symbol)
784 CHECK_SYMBOL (symbol);
785 XSYMBOL (symbol)->declared_special = 0;
786 return Qnil;
790 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
791 doc: /* Bind variables according to VARLIST then eval BODY.
792 The value of the last form in BODY is returned.
793 Each element of VARLIST is a symbol (which is bound to nil)
794 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
795 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
796 usage: (let* VARLIST BODY...) */)
797 (Lisp_Object args)
799 Lisp_Object varlist, var, val, elt, lexenv;
800 ptrdiff_t count = SPECPDL_INDEX ();
801 struct gcpro gcpro1, gcpro2, gcpro3;
803 GCPRO3 (args, elt, varlist);
805 lexenv = Vinternal_interpreter_environment;
807 varlist = Fcar (args);
808 while (CONSP (varlist))
810 QUIT;
812 elt = XCAR (varlist);
813 if (SYMBOLP (elt))
815 var = elt;
816 val = Qnil;
818 else if (! NILP (Fcdr (Fcdr (elt))))
819 signal_error ("`let' bindings can have only one value-form", elt);
820 else
822 var = Fcar (elt);
823 val = eval_sub (Fcar (Fcdr (elt)));
826 if (!NILP (lexenv) && SYMBOLP (var)
827 && !XSYMBOL (var)->declared_special
828 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
829 /* Lexically bind VAR by adding it to the interpreter's binding
830 alist. */
832 Lisp_Object newenv
833 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
834 if (EQ (Vinternal_interpreter_environment, lexenv))
835 /* Save the old lexical environment on the specpdl stack,
836 but only for the first lexical binding, since we'll never
837 need to revert to one of the intermediate ones. */
838 specbind (Qinternal_interpreter_environment, newenv);
839 else
840 Vinternal_interpreter_environment = newenv;
842 else
843 specbind (var, val);
845 varlist = XCDR (varlist);
847 UNGCPRO;
848 val = Fprogn (Fcdr (args));
849 return unbind_to (count, val);
852 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
853 doc: /* Bind variables according to VARLIST then eval BODY.
854 The value of the last form in BODY is returned.
855 Each element of VARLIST is a symbol (which is bound to nil)
856 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
857 All the VALUEFORMs are evalled before any symbols are bound.
858 usage: (let VARLIST BODY...) */)
859 (Lisp_Object args)
861 Lisp_Object *temps, tem, lexenv;
862 register Lisp_Object elt, varlist;
863 ptrdiff_t count = SPECPDL_INDEX ();
864 ptrdiff_t argnum;
865 struct gcpro gcpro1, gcpro2;
866 USE_SAFE_ALLOCA;
868 varlist = Fcar (args);
870 /* Make space to hold the values to give the bound variables. */
871 elt = Flength (varlist);
872 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
874 /* Compute the values and store them in `temps'. */
876 GCPRO2 (args, *temps);
877 gcpro2.nvars = 0;
879 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
881 QUIT;
882 elt = XCAR (varlist);
883 if (SYMBOLP (elt))
884 temps [argnum++] = Qnil;
885 else if (! NILP (Fcdr (Fcdr (elt))))
886 signal_error ("`let' bindings can have only one value-form", elt);
887 else
888 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
889 gcpro2.nvars = argnum;
891 UNGCPRO;
893 lexenv = Vinternal_interpreter_environment;
895 varlist = Fcar (args);
896 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
898 Lisp_Object var;
900 elt = XCAR (varlist);
901 var = SYMBOLP (elt) ? elt : Fcar (elt);
902 tem = temps[argnum++];
904 if (!NILP (lexenv) && SYMBOLP (var)
905 && !XSYMBOL (var)->declared_special
906 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
907 /* Lexically bind VAR by adding it to the lexenv alist. */
908 lexenv = Fcons (Fcons (var, tem), lexenv);
909 else
910 /* Dynamically bind VAR. */
911 specbind (var, tem);
914 if (!EQ (lexenv, Vinternal_interpreter_environment))
915 /* Instantiate a new lexical environment. */
916 specbind (Qinternal_interpreter_environment, lexenv);
918 elt = Fprogn (Fcdr (args));
919 SAFE_FREE ();
920 return unbind_to (count, elt);
923 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
924 doc: /* If TEST yields non-nil, eval BODY... and repeat.
925 The order of execution is thus TEST, BODY, TEST, BODY and so on
926 until TEST returns nil.
927 usage: (while TEST BODY...) */)
928 (Lisp_Object args)
930 Lisp_Object test, body;
931 struct gcpro gcpro1, gcpro2;
933 GCPRO2 (test, body);
935 test = Fcar (args);
936 body = Fcdr (args);
937 while (!NILP (eval_sub (test)))
939 QUIT;
940 Fprogn (body);
943 UNGCPRO;
944 return Qnil;
947 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
948 doc: /* Return result of expanding macros at top level of FORM.
949 If FORM is not a macro call, it is returned unchanged.
950 Otherwise, the macro is expanded and the expansion is considered
951 in place of FORM. When a non-macro-call results, it is returned.
953 The second optional arg ENVIRONMENT specifies an environment of macro
954 definitions to shadow the loaded ones for use in file byte-compilation. */)
955 (Lisp_Object form, Lisp_Object environment)
957 /* With cleanups from Hallvard Furuseth. */
958 register Lisp_Object expander, sym, def, tem;
960 while (1)
962 /* Come back here each time we expand a macro call,
963 in case it expands into another macro call. */
964 if (!CONSP (form))
965 break;
966 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
967 def = sym = XCAR (form);
968 tem = Qnil;
969 /* Trace symbols aliases to other symbols
970 until we get a symbol that is not an alias. */
971 while (SYMBOLP (def))
973 QUIT;
974 sym = def;
975 tem = Fassq (sym, environment);
976 if (NILP (tem))
978 def = XSYMBOL (sym)->function;
979 if (!NILP (def))
980 continue;
982 break;
984 /* Right now TEM is the result from SYM in ENVIRONMENT,
985 and if TEM is nil then DEF is SYM's function definition. */
986 if (NILP (tem))
988 /* SYM is not mentioned in ENVIRONMENT.
989 Look at its function definition. */
990 struct gcpro gcpro1;
991 GCPRO1 (form);
992 def = Fautoload_do_load (def, sym, Qmacro);
993 UNGCPRO;
994 if (!CONSP (def))
995 /* Not defined or definition not suitable. */
996 break;
997 if (!EQ (XCAR (def), Qmacro))
998 break;
999 else expander = XCDR (def);
1001 else
1003 expander = XCDR (tem);
1004 if (NILP (expander))
1005 break;
1008 Lisp_Object newform = apply1 (expander, XCDR (form));
1009 if (EQ (form, newform))
1010 break;
1011 else
1012 form = newform;
1015 return form;
1018 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1019 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1020 TAG is evalled to get the tag to use; it must not be nil.
1022 Then the BODY is executed.
1023 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1024 If no throw happens, `catch' returns the value of the last BODY form.
1025 If a throw happens, it specifies the value to return from `catch'.
1026 usage: (catch TAG BODY...) */)
1027 (Lisp_Object args)
1029 register Lisp_Object tag;
1030 struct gcpro gcpro1;
1032 GCPRO1 (args);
1033 tag = eval_sub (Fcar (args));
1034 UNGCPRO;
1035 return internal_catch (tag, Fprogn, Fcdr (args));
1038 /* Set up a catch, then call C function FUNC on argument ARG.
1039 FUNC should return a Lisp_Object.
1040 This is how catches are done from within C code. */
1042 Lisp_Object
1043 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1045 /* This structure is made part of the chain `catchlist'. */
1046 struct catchtag c;
1048 /* Fill in the components of c, and put it on the list. */
1049 c.next = catchlist;
1050 c.tag = tag;
1051 c.val = Qnil;
1052 c.handlerlist = handlerlist;
1053 c.lisp_eval_depth = lisp_eval_depth;
1054 c.pdlcount = SPECPDL_INDEX ();
1055 c.poll_suppress_count = poll_suppress_count;
1056 c.interrupt_input_blocked = interrupt_input_blocked;
1057 c.gcpro = gcprolist;
1058 c.byte_stack = byte_stack_list;
1059 catchlist = &c;
1061 /* Call FUNC. */
1062 if (! sys_setjmp (c.jmp))
1063 c.val = (*func) (arg);
1065 /* Throw works by a longjmp that comes right here. */
1066 catchlist = c.next;
1067 return c.val;
1070 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1071 jump to that CATCH, returning VALUE as the value of that catch.
1073 This is the guts of Fthrow and Fsignal; they differ only in the way
1074 they choose the catch tag to throw to. A catch tag for a
1075 condition-case form has a TAG of Qnil.
1077 Before each catch is discarded, unbind all special bindings and
1078 execute all unwind-protect clauses made above that catch. Unwind
1079 the handler stack as we go, so that the proper handlers are in
1080 effect for each unwind-protect clause we run. At the end, restore
1081 some static info saved in CATCH, and longjmp to the location
1082 specified there.
1084 This is used for correct unwinding in Fthrow and Fsignal. */
1086 static _Noreturn void
1087 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1089 bool last_time;
1091 /* Save the value in the tag. */
1092 catch->val = value;
1094 /* Restore certain special C variables. */
1095 set_poll_suppress_count (catch->poll_suppress_count);
1096 unblock_input_to (catch->interrupt_input_blocked);
1097 immediate_quit = 0;
1101 last_time = catchlist == catch;
1103 /* Unwind the specpdl stack, and then restore the proper set of
1104 handlers. */
1105 unbind_to (catchlist->pdlcount, Qnil);
1106 handlerlist = catchlist->handlerlist;
1107 catchlist = catchlist->next;
1109 while (! last_time);
1111 byte_stack_list = catch->byte_stack;
1112 gcprolist = catch->gcpro;
1113 #ifdef DEBUG_GCPRO
1114 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1115 #endif
1116 lisp_eval_depth = catch->lisp_eval_depth;
1118 sys_longjmp (catch->jmp, 1);
1121 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1122 doc: /* Throw to the catch for TAG and return VALUE from it.
1123 Both TAG and VALUE are evalled. */)
1124 (register Lisp_Object tag, Lisp_Object value)
1126 register struct catchtag *c;
1128 if (!NILP (tag))
1129 for (c = catchlist; c; c = c->next)
1131 if (EQ (c->tag, tag))
1132 unwind_to_catch (c, value);
1134 xsignal2 (Qno_catch, tag, value);
1138 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1139 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1140 If BODYFORM completes normally, its value is returned
1141 after executing the UNWINDFORMS.
1142 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1143 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1144 (Lisp_Object args)
1146 Lisp_Object val;
1147 ptrdiff_t count = SPECPDL_INDEX ();
1149 record_unwind_protect (Fprogn, Fcdr (args));
1150 val = eval_sub (Fcar (args));
1151 return unbind_to (count, val);
1154 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1155 doc: /* Regain control when an error is signaled.
1156 Executes BODYFORM and returns its value if no error happens.
1157 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1158 where the BODY is made of Lisp expressions.
1160 A handler is applicable to an error
1161 if CONDITION-NAME is one of the error's condition names.
1162 If an error happens, the first applicable handler is run.
1164 The car of a handler may be a list of condition names instead of a
1165 single condition name; then it handles all of them. If the special
1166 condition name `debug' is present in this list, it allows another
1167 condition in the list to run the debugger if `debug-on-error' and the
1168 other usual mechanisms says it should (otherwise, `condition-case'
1169 suppresses the debugger).
1171 When a handler handles an error, control returns to the `condition-case'
1172 and it executes the handler's BODY...
1173 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1174 \(If VAR is nil, the handler can't access that information.)
1175 Then the value of the last BODY form is returned from the `condition-case'
1176 expression.
1178 See also the function `signal' for more info.
1179 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1180 (Lisp_Object args)
1182 Lisp_Object var = Fcar (args);
1183 Lisp_Object bodyform = Fcar (Fcdr (args));
1184 Lisp_Object handlers = Fcdr (Fcdr (args));
1186 return internal_lisp_condition_case (var, bodyform, handlers);
1189 /* Like Fcondition_case, but the args are separate
1190 rather than passed in a list. Used by Fbyte_code. */
1192 Lisp_Object
1193 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1194 Lisp_Object handlers)
1196 Lisp_Object val;
1197 struct catchtag c;
1198 struct handler h;
1200 CHECK_SYMBOL (var);
1202 for (val = handlers; CONSP (val); val = XCDR (val))
1204 Lisp_Object tem;
1205 tem = XCAR (val);
1206 if (! (NILP (tem)
1207 || (CONSP (tem)
1208 && (SYMBOLP (XCAR (tem))
1209 || CONSP (XCAR (tem))))))
1210 error ("Invalid condition handler: %s",
1211 SDATA (Fprin1_to_string (tem, Qt)));
1214 c.tag = Qnil;
1215 c.val = Qnil;
1216 c.handlerlist = handlerlist;
1217 c.lisp_eval_depth = lisp_eval_depth;
1218 c.pdlcount = SPECPDL_INDEX ();
1219 c.poll_suppress_count = poll_suppress_count;
1220 c.interrupt_input_blocked = interrupt_input_blocked;
1221 c.gcpro = gcprolist;
1222 c.byte_stack = byte_stack_list;
1223 if (sys_setjmp (c.jmp))
1225 if (!NILP (h.var))
1226 specbind (h.var, c.val);
1227 val = Fprogn (Fcdr (h.chosen_clause));
1229 /* Note that this just undoes the binding of h.var; whoever
1230 longjumped to us unwound the stack to c.pdlcount before
1231 throwing. */
1232 unbind_to (c.pdlcount, Qnil);
1233 return val;
1235 c.next = catchlist;
1236 catchlist = &c;
1238 h.var = var;
1239 h.handler = handlers;
1240 h.next = handlerlist;
1241 h.tag = &c;
1242 handlerlist = &h;
1244 val = eval_sub (bodyform);
1245 catchlist = c.next;
1246 handlerlist = h.next;
1247 return val;
1250 /* Call the function BFUN with no arguments, catching errors within it
1251 according to HANDLERS. If there is an error, call HFUN with
1252 one argument which is the data that describes the error:
1253 (SIGNALNAME . DATA)
1255 HANDLERS can be a list of conditions to catch.
1256 If HANDLERS is Qt, catch all errors.
1257 If HANDLERS is Qerror, catch all errors
1258 but allow the debugger to run if that is enabled. */
1260 Lisp_Object
1261 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1262 Lisp_Object (*hfun) (Lisp_Object))
1264 Lisp_Object val;
1265 struct catchtag c;
1266 struct handler h;
1268 c.tag = Qnil;
1269 c.val = Qnil;
1270 c.handlerlist = handlerlist;
1271 c.lisp_eval_depth = lisp_eval_depth;
1272 c.pdlcount = SPECPDL_INDEX ();
1273 c.poll_suppress_count = poll_suppress_count;
1274 c.interrupt_input_blocked = interrupt_input_blocked;
1275 c.gcpro = gcprolist;
1276 c.byte_stack = byte_stack_list;
1277 if (sys_setjmp (c.jmp))
1279 return (*hfun) (c.val);
1281 c.next = catchlist;
1282 catchlist = &c;
1283 h.handler = handlers;
1284 h.var = Qnil;
1285 h.next = handlerlist;
1286 h.tag = &c;
1287 handlerlist = &h;
1289 val = (*bfun) ();
1290 catchlist = c.next;
1291 handlerlist = h.next;
1292 return val;
1295 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1297 Lisp_Object
1298 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1299 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1301 Lisp_Object val;
1302 struct catchtag c;
1303 struct handler h;
1305 c.tag = Qnil;
1306 c.val = Qnil;
1307 c.handlerlist = handlerlist;
1308 c.lisp_eval_depth = lisp_eval_depth;
1309 c.pdlcount = SPECPDL_INDEX ();
1310 c.poll_suppress_count = poll_suppress_count;
1311 c.interrupt_input_blocked = interrupt_input_blocked;
1312 c.gcpro = gcprolist;
1313 c.byte_stack = byte_stack_list;
1314 if (sys_setjmp (c.jmp))
1316 return (*hfun) (c.val);
1318 c.next = catchlist;
1319 catchlist = &c;
1320 h.handler = handlers;
1321 h.var = Qnil;
1322 h.next = handlerlist;
1323 h.tag = &c;
1324 handlerlist = &h;
1326 val = (*bfun) (arg);
1327 catchlist = c.next;
1328 handlerlist = h.next;
1329 return val;
1332 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1333 its arguments. */
1335 Lisp_Object
1336 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1337 Lisp_Object arg1,
1338 Lisp_Object arg2,
1339 Lisp_Object handlers,
1340 Lisp_Object (*hfun) (Lisp_Object))
1342 Lisp_Object val;
1343 struct catchtag c;
1344 struct handler h;
1346 c.tag = Qnil;
1347 c.val = Qnil;
1348 c.handlerlist = handlerlist;
1349 c.lisp_eval_depth = lisp_eval_depth;
1350 c.pdlcount = SPECPDL_INDEX ();
1351 c.poll_suppress_count = poll_suppress_count;
1352 c.interrupt_input_blocked = interrupt_input_blocked;
1353 c.gcpro = gcprolist;
1354 c.byte_stack = byte_stack_list;
1355 if (sys_setjmp (c.jmp))
1357 return (*hfun) (c.val);
1359 c.next = catchlist;
1360 catchlist = &c;
1361 h.handler = handlers;
1362 h.var = Qnil;
1363 h.next = handlerlist;
1364 h.tag = &c;
1365 handlerlist = &h;
1367 val = (*bfun) (arg1, arg2);
1368 catchlist = c.next;
1369 handlerlist = h.next;
1370 return val;
1373 /* Like internal_condition_case but call BFUN with NARGS as first,
1374 and ARGS as second argument. */
1376 Lisp_Object
1377 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1378 ptrdiff_t nargs,
1379 Lisp_Object *args,
1380 Lisp_Object handlers,
1381 Lisp_Object (*hfun) (Lisp_Object err,
1382 ptrdiff_t nargs,
1383 Lisp_Object *args))
1385 Lisp_Object val;
1386 struct catchtag c;
1387 struct handler h;
1389 c.tag = Qnil;
1390 c.val = Qnil;
1391 c.handlerlist = handlerlist;
1392 c.lisp_eval_depth = lisp_eval_depth;
1393 c.pdlcount = SPECPDL_INDEX ();
1394 c.poll_suppress_count = poll_suppress_count;
1395 c.interrupt_input_blocked = interrupt_input_blocked;
1396 c.gcpro = gcprolist;
1397 c.byte_stack = byte_stack_list;
1398 if (sys_setjmp (c.jmp))
1400 return (*hfun) (c.val, nargs, args);
1402 c.next = catchlist;
1403 catchlist = &c;
1404 h.handler = handlers;
1405 h.var = Qnil;
1406 h.next = handlerlist;
1407 h.tag = &c;
1408 handlerlist = &h;
1410 val = (*bfun) (nargs, args);
1411 catchlist = c.next;
1412 handlerlist = h.next;
1413 return val;
1417 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1418 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1419 Lisp_Object data);
1421 void
1422 process_quit_flag (void)
1424 Lisp_Object flag = Vquit_flag;
1425 Vquit_flag = Qnil;
1426 if (EQ (flag, Qkill_emacs))
1427 Fkill_emacs (Qnil);
1428 if (EQ (Vthrow_on_input, flag))
1429 Fthrow (Vthrow_on_input, Qt);
1430 Fsignal (Qquit, Qnil);
1433 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1434 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1435 This function does not return.
1437 An error symbol is a symbol with an `error-conditions' property
1438 that is a list of condition names.
1439 A handler for any of those names will get to handle this signal.
1440 The symbol `error' should normally be one of them.
1442 DATA should be a list. Its elements are printed as part of the error message.
1443 See Info anchor `(elisp)Definition of signal' for some details on how this
1444 error message is constructed.
1445 If the signal is handled, DATA is made available to the handler.
1446 See also the function `condition-case'. */)
1447 (Lisp_Object error_symbol, Lisp_Object data)
1449 /* When memory is full, ERROR-SYMBOL is nil,
1450 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1451 That is a special case--don't do this in other situations. */
1452 Lisp_Object conditions;
1453 Lisp_Object string;
1454 Lisp_Object real_error_symbol
1455 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1456 register Lisp_Object clause = Qnil;
1457 struct handler *h;
1459 immediate_quit = 0;
1460 abort_on_gc = 0;
1461 if (gc_in_progress || waiting_for_input)
1462 emacs_abort ();
1464 #if 0 /* rms: I don't know why this was here,
1465 but it is surely wrong for an error that is handled. */
1466 #ifdef HAVE_WINDOW_SYSTEM
1467 if (display_hourglass_p)
1468 cancel_hourglass ();
1469 #endif
1470 #endif
1472 /* This hook is used by edebug. */
1473 if (! NILP (Vsignal_hook_function)
1474 && ! NILP (error_symbol))
1476 /* Edebug takes care of restoring these variables when it exits. */
1477 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1478 max_lisp_eval_depth = lisp_eval_depth + 20;
1480 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1481 max_specpdl_size = SPECPDL_INDEX () + 40;
1483 call2 (Vsignal_hook_function, error_symbol, data);
1486 conditions = Fget (real_error_symbol, Qerror_conditions);
1488 /* Remember from where signal was called. Skip over the frame for
1489 `signal' itself. If a frame for `error' follows, skip that,
1490 too. Don't do this when ERROR_SYMBOL is nil, because that
1491 is a memory-full error. */
1492 Vsignaling_function = Qnil;
1493 if (!NILP (error_symbol))
1495 union specbinding *pdl = backtrace_next (backtrace_top ());
1496 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1497 pdl = backtrace_next (pdl);
1498 if (backtrace_p (pdl))
1499 Vsignaling_function = backtrace_function (pdl);
1502 for (h = handlerlist; h; h = h->next)
1504 clause = find_handler_clause (h->handler, conditions);
1505 if (!NILP (clause))
1506 break;
1509 if (/* Don't run the debugger for a memory-full error.
1510 (There is no room in memory to do that!) */
1511 !NILP (error_symbol)
1512 && (!NILP (Vdebug_on_signal)
1513 /* If no handler is present now, try to run the debugger. */
1514 || NILP (clause)
1515 /* A `debug' symbol in the handler list disables the normal
1516 suppression of the debugger. */
1517 || (CONSP (clause) && CONSP (XCAR (clause))
1518 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1519 /* Special handler that means "print a message and run debugger
1520 if requested". */
1521 || EQ (h->handler, Qerror)))
1523 bool debugger_called
1524 = maybe_call_debugger (conditions, error_symbol, data);
1525 /* We can't return values to code which signaled an error, but we
1526 can continue code which has signaled a quit. */
1527 if (debugger_called && EQ (real_error_symbol, Qquit))
1528 return Qnil;
1531 if (!NILP (clause))
1533 Lisp_Object unwind_data
1534 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1536 h->chosen_clause = clause;
1537 unwind_to_catch (h->tag, unwind_data);
1539 else
1541 if (catchlist != 0)
1542 Fthrow (Qtop_level, Qt);
1545 if (! NILP (error_symbol))
1546 data = Fcons (error_symbol, data);
1548 string = Ferror_message_string (data);
1549 fatal ("%s", SDATA (string));
1552 /* Internal version of Fsignal that never returns.
1553 Used for anything but Qquit (which can return from Fsignal). */
1555 void
1556 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1558 Fsignal (error_symbol, data);
1559 emacs_abort ();
1562 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1564 void
1565 xsignal0 (Lisp_Object error_symbol)
1567 xsignal (error_symbol, Qnil);
1570 void
1571 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1573 xsignal (error_symbol, list1 (arg));
1576 void
1577 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1579 xsignal (error_symbol, list2 (arg1, arg2));
1582 void
1583 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1585 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1588 /* Signal `error' with message S, and additional arg ARG.
1589 If ARG is not a genuine list, make it a one-element list. */
1591 void
1592 signal_error (const char *s, Lisp_Object arg)
1594 Lisp_Object tortoise, hare;
1596 hare = tortoise = arg;
1597 while (CONSP (hare))
1599 hare = XCDR (hare);
1600 if (!CONSP (hare))
1601 break;
1603 hare = XCDR (hare);
1604 tortoise = XCDR (tortoise);
1606 if (EQ (hare, tortoise))
1607 break;
1610 if (!NILP (hare))
1611 arg = Fcons (arg, Qnil); /* Make it a list. */
1613 xsignal (Qerror, Fcons (build_string (s), arg));
1617 /* Return true if LIST is a non-nil atom or
1618 a list containing one of CONDITIONS. */
1620 static bool
1621 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1623 if (NILP (list))
1624 return 0;
1625 if (! CONSP (list))
1626 return 1;
1628 while (CONSP (conditions))
1630 Lisp_Object this, tail;
1631 this = XCAR (conditions);
1632 for (tail = list; CONSP (tail); tail = XCDR (tail))
1633 if (EQ (XCAR (tail), this))
1634 return 1;
1635 conditions = XCDR (conditions);
1637 return 0;
1640 /* Return true if an error with condition-symbols CONDITIONS,
1641 and described by SIGNAL-DATA, should skip the debugger
1642 according to debugger-ignored-errors. */
1644 static bool
1645 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1647 Lisp_Object tail;
1648 bool first_string = 1;
1649 Lisp_Object error_message;
1651 error_message = Qnil;
1652 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1654 if (STRINGP (XCAR (tail)))
1656 if (first_string)
1658 error_message = Ferror_message_string (data);
1659 first_string = 0;
1662 if (fast_string_match (XCAR (tail), error_message) >= 0)
1663 return 1;
1665 else
1667 Lisp_Object contail;
1669 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1670 if (EQ (XCAR (tail), XCAR (contail)))
1671 return 1;
1675 return 0;
1678 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1679 SIG and DATA describe the signal. There are two ways to pass them:
1680 = SIG is the error symbol, and DATA is the rest of the data.
1681 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1682 This is for memory-full errors only. */
1683 static bool
1684 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1686 Lisp_Object combined_data;
1688 combined_data = Fcons (sig, data);
1690 if (
1691 /* Don't try to run the debugger with interrupts blocked.
1692 The editing loop would return anyway. */
1693 ! input_blocked_p ()
1694 && NILP (Vinhibit_debugger)
1695 /* Does user want to enter debugger for this kind of error? */
1696 && (EQ (sig, Qquit)
1697 ? debug_on_quit
1698 : wants_debugger (Vdebug_on_error, conditions))
1699 && ! skip_debugger (conditions, combined_data)
1700 /* RMS: What's this for? */
1701 && when_entered_debugger < num_nonmacro_input_events)
1703 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1704 return 1;
1707 return 0;
1710 static Lisp_Object
1711 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1713 register Lisp_Object h;
1715 /* t is used by handlers for all conditions, set up by C code. */
1716 if (EQ (handlers, Qt))
1717 return Qt;
1719 /* error is used similarly, but means print an error message
1720 and run the debugger if that is enabled. */
1721 if (EQ (handlers, Qerror))
1722 return Qt;
1724 for (h = handlers; CONSP (h); h = XCDR (h))
1726 Lisp_Object handler = XCAR (h);
1727 Lisp_Object condit, tem;
1729 if (!CONSP (handler))
1730 continue;
1731 condit = XCAR (handler);
1732 /* Handle a single condition name in handler HANDLER. */
1733 if (SYMBOLP (condit))
1735 tem = Fmemq (Fcar (handler), conditions);
1736 if (!NILP (tem))
1737 return handler;
1739 /* Handle a list of condition names in handler HANDLER. */
1740 else if (CONSP (condit))
1742 Lisp_Object tail;
1743 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1745 tem = Fmemq (XCAR (tail), conditions);
1746 if (!NILP (tem))
1747 return handler;
1752 return Qnil;
1756 /* Dump an error message; called like vprintf. */
1757 void
1758 verror (const char *m, va_list ap)
1760 char buf[4000];
1761 ptrdiff_t size = sizeof buf;
1762 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1763 char *buffer = buf;
1764 ptrdiff_t used;
1765 Lisp_Object string;
1767 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1768 string = make_string (buffer, used);
1769 if (buffer != buf)
1770 xfree (buffer);
1772 xsignal1 (Qerror, string);
1776 /* Dump an error message; called like printf. */
1778 /* VARARGS 1 */
1779 void
1780 error (const char *m, ...)
1782 va_list ap;
1783 va_start (ap, m);
1784 verror (m, ap);
1787 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1788 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1789 This means it contains a description for how to read arguments to give it.
1790 The value is nil for an invalid function or a symbol with no function
1791 definition.
1793 Interactively callable functions include strings and vectors (treated
1794 as keyboard macros), lambda-expressions that contain a top-level call
1795 to `interactive', autoload definitions made by `autoload' with non-nil
1796 fourth argument, and some of the built-in functions of Lisp.
1798 Also, a symbol satisfies `commandp' if its function definition does so.
1800 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1801 then strings and vectors are not accepted. */)
1802 (Lisp_Object function, Lisp_Object for_call_interactively)
1804 register Lisp_Object fun;
1805 register Lisp_Object funcar;
1806 Lisp_Object if_prop = Qnil;
1808 fun = function;
1810 fun = indirect_function (fun); /* Check cycles. */
1811 if (NILP (fun))
1812 return Qnil;
1814 /* Check an `interactive-form' property if present, analogous to the
1815 function-documentation property. */
1816 fun = function;
1817 while (SYMBOLP (fun))
1819 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1820 if (!NILP (tmp))
1821 if_prop = Qt;
1822 fun = Fsymbol_function (fun);
1825 /* Emacs primitives are interactive if their DEFUN specifies an
1826 interactive spec. */
1827 if (SUBRP (fun))
1828 return XSUBR (fun)->intspec ? Qt : if_prop;
1830 /* Bytecode objects are interactive if they are long enough to
1831 have an element whose index is COMPILED_INTERACTIVE, which is
1832 where the interactive spec is stored. */
1833 else if (COMPILEDP (fun))
1834 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1835 ? Qt : if_prop);
1837 /* Strings and vectors are keyboard macros. */
1838 if (STRINGP (fun) || VECTORP (fun))
1839 return (NILP (for_call_interactively) ? Qt : Qnil);
1841 /* Lists may represent commands. */
1842 if (!CONSP (fun))
1843 return Qnil;
1844 funcar = XCAR (fun);
1845 if (EQ (funcar, Qclosure))
1846 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1847 ? Qt : if_prop);
1848 else if (EQ (funcar, Qlambda))
1849 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1850 else if (EQ (funcar, Qautoload))
1851 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1852 else
1853 return Qnil;
1856 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1857 doc: /* Define FUNCTION to autoload from FILE.
1858 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1859 Third arg DOCSTRING is documentation for the function.
1860 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1861 Fifth arg TYPE indicates the type of the object:
1862 nil or omitted says FUNCTION is a function,
1863 `keymap' says FUNCTION is really a keymap, and
1864 `macro' or t says FUNCTION is really a macro.
1865 Third through fifth args give info about the real definition.
1866 They default to nil.
1867 If FUNCTION is already defined other than as an autoload,
1868 this does nothing and returns nil. */)
1869 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1871 CHECK_SYMBOL (function);
1872 CHECK_STRING (file);
1874 /* If function is defined and not as an autoload, don't override. */
1875 if (!NILP (XSYMBOL (function)->function)
1876 && !AUTOLOADP (XSYMBOL (function)->function))
1877 return Qnil;
1879 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1880 /* `read1' in lread.c has found the docstring starting with "\
1881 and assumed the docstring will be provided by Snarf-documentation, so it
1882 passed us 0 instead. But that leads to accidental sharing in purecopy's
1883 hash-consing, so we use a (hopefully) unique integer instead. */
1884 docstring = make_number (XHASH (function));
1885 return Fdefalias (function,
1886 list5 (Qautoload, file, docstring, interactive, type),
1887 Qnil);
1890 Lisp_Object
1891 un_autoload (Lisp_Object oldqueue)
1893 register Lisp_Object queue, first, second;
1895 /* Queue to unwind is current value of Vautoload_queue.
1896 oldqueue is the shadowed value to leave in Vautoload_queue. */
1897 queue = Vautoload_queue;
1898 Vautoload_queue = oldqueue;
1899 while (CONSP (queue))
1901 first = XCAR (queue);
1902 second = Fcdr (first);
1903 first = Fcar (first);
1904 if (EQ (first, make_number (0)))
1905 Vfeatures = second;
1906 else
1907 Ffset (first, second);
1908 queue = XCDR (queue);
1910 return Qnil;
1913 /* Load an autoloaded function.
1914 FUNNAME is the symbol which is the function's name.
1915 FUNDEF is the autoload definition (a list). */
1917 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1918 doc: /* Load FUNDEF which should be an autoload.
1919 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1920 in which case the function returns the new autoloaded function value.
1921 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1922 it is defines a macro. */)
1923 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1925 ptrdiff_t count = SPECPDL_INDEX ();
1926 struct gcpro gcpro1, gcpro2, gcpro3;
1928 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1929 return fundef;
1931 if (EQ (macro_only, Qmacro))
1933 Lisp_Object kind = Fnth (make_number (4), fundef);
1934 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1935 return fundef;
1938 /* This is to make sure that loadup.el gives a clear picture
1939 of what files are preloaded and when. */
1940 if (! NILP (Vpurify_flag))
1941 error ("Attempt to autoload %s while preparing to dump",
1942 SDATA (SYMBOL_NAME (funname)));
1944 CHECK_SYMBOL (funname);
1945 GCPRO3 (funname, fundef, macro_only);
1947 /* Preserve the match data. */
1948 record_unwind_save_match_data ();
1950 /* If autoloading gets an error (which includes the error of failing
1951 to define the function being called), we use Vautoload_queue
1952 to undo function definitions and `provide' calls made by
1953 the function. We do this in the specific case of autoloading
1954 because autoloading is not an explicit request "load this file",
1955 but rather a request to "call this function".
1957 The value saved here is to be restored into Vautoload_queue. */
1958 record_unwind_protect (un_autoload, Vautoload_queue);
1959 Vautoload_queue = Qt;
1960 /* If `macro_only', assume this autoload to be a "best-effort",
1961 so don't signal an error if autoloading fails. */
1962 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1964 /* Once loading finishes, don't undo it. */
1965 Vautoload_queue = Qt;
1966 unbind_to (count, Qnil);
1968 UNGCPRO;
1970 if (NILP (funname))
1971 return Qnil;
1972 else
1974 Lisp_Object fun = Findirect_function (funname, Qnil);
1976 if (!NILP (Fequal (fun, fundef)))
1977 error ("Autoloading failed to define function %s",
1978 SDATA (SYMBOL_NAME (funname)));
1979 else
1980 return fun;
1985 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1986 doc: /* Evaluate FORM and return its value.
1987 If LEXICAL is t, evaluate using lexical scoping. */)
1988 (Lisp_Object form, Lisp_Object lexical)
1990 ptrdiff_t count = SPECPDL_INDEX ();
1991 specbind (Qinternal_interpreter_environment,
1992 CONSP (lexical) || NILP (lexical) ? lexical : Fcons (Qt, Qnil));
1993 return unbind_to (count, eval_sub (form));
1996 static void
1997 grow_specpdl (void)
1999 ptrdiff_t count = SPECPDL_INDEX ();
2000 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2001 union specbinding *pdlvec = specpdl - 1;
2002 ptrdiff_t pdlvecsize = specpdl_size + 1;
2003 if (max_size <= specpdl_size)
2005 if (max_specpdl_size < 400)
2006 max_size = max_specpdl_size = 400;
2007 if (max_size <= specpdl_size)
2008 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
2010 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2011 specpdl = pdlvec + 1;
2012 specpdl_size = pdlvecsize - 1;
2013 specpdl_ptr = specpdl + count;
2016 void
2017 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2019 eassert (nargs >= UNEVALLED);
2020 if (specpdl_ptr == specpdl + specpdl_size)
2021 grow_specpdl ();
2022 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2023 specpdl_ptr->bt.debug_on_exit = false;
2024 specpdl_ptr->bt.function = function;
2025 specpdl_ptr->bt.args = args;
2026 specpdl_ptr->bt.nargs = nargs;
2027 specpdl_ptr++;
2030 /* Eval a sub-expression of the current expression (i.e. in the same
2031 lexical scope). */
2032 Lisp_Object
2033 eval_sub (Lisp_Object form)
2035 Lisp_Object fun, val, original_fun, original_args;
2036 Lisp_Object funcar;
2037 struct gcpro gcpro1, gcpro2, gcpro3;
2039 if (SYMBOLP (form))
2041 /* Look up its binding in the lexical environment.
2042 We do not pay attention to the declared_special flag here, since we
2043 already did that when let-binding the variable. */
2044 Lisp_Object lex_binding
2045 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2046 ? Fassq (form, Vinternal_interpreter_environment)
2047 : Qnil;
2048 if (CONSP (lex_binding))
2049 return XCDR (lex_binding);
2050 else
2051 return Fsymbol_value (form);
2054 if (!CONSP (form))
2055 return form;
2057 QUIT;
2059 GCPRO1 (form);
2060 maybe_gc ();
2061 UNGCPRO;
2063 if (++lisp_eval_depth > max_lisp_eval_depth)
2065 if (max_lisp_eval_depth < 100)
2066 max_lisp_eval_depth = 100;
2067 if (lisp_eval_depth > max_lisp_eval_depth)
2068 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2071 original_fun = XCAR (form);
2072 original_args = XCDR (form);
2074 /* This also protects them from gc. */
2075 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2077 if (debug_on_next_call)
2078 do_debug_on_call (Qt);
2080 /* At this point, only original_fun and original_args
2081 have values that will be used below. */
2082 retry:
2084 /* Optimize for no indirection. */
2085 fun = original_fun;
2086 if (SYMBOLP (fun) && !NILP (fun)
2087 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2088 fun = indirect_function (fun);
2090 if (SUBRP (fun))
2092 Lisp_Object numargs;
2093 Lisp_Object argvals[8];
2094 Lisp_Object args_left;
2095 register int i, maxargs;
2097 args_left = original_args;
2098 numargs = Flength (args_left);
2100 check_cons_list ();
2102 if (XINT (numargs) < XSUBR (fun)->min_args
2103 || (XSUBR (fun)->max_args >= 0
2104 && XSUBR (fun)->max_args < XINT (numargs)))
2105 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2107 else if (XSUBR (fun)->max_args == UNEVALLED)
2108 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2109 else if (XSUBR (fun)->max_args == MANY)
2111 /* Pass a vector of evaluated arguments. */
2112 Lisp_Object *vals;
2113 ptrdiff_t argnum = 0;
2114 USE_SAFE_ALLOCA;
2116 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2118 GCPRO3 (args_left, fun, fun);
2119 gcpro3.var = vals;
2120 gcpro3.nvars = 0;
2122 while (!NILP (args_left))
2124 vals[argnum++] = eval_sub (Fcar (args_left));
2125 args_left = Fcdr (args_left);
2126 gcpro3.nvars = argnum;
2129 set_backtrace_args (specpdl_ptr - 1, vals);
2130 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2132 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2133 UNGCPRO;
2134 SAFE_FREE ();
2136 else
2138 GCPRO3 (args_left, fun, fun);
2139 gcpro3.var = argvals;
2140 gcpro3.nvars = 0;
2142 maxargs = XSUBR (fun)->max_args;
2143 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2145 argvals[i] = eval_sub (Fcar (args_left));
2146 gcpro3.nvars = ++i;
2149 UNGCPRO;
2151 set_backtrace_args (specpdl_ptr - 1, argvals);
2152 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2154 switch (i)
2156 case 0:
2157 val = (XSUBR (fun)->function.a0 ());
2158 break;
2159 case 1:
2160 val = (XSUBR (fun)->function.a1 (argvals[0]));
2161 break;
2162 case 2:
2163 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2164 break;
2165 case 3:
2166 val = (XSUBR (fun)->function.a3
2167 (argvals[0], argvals[1], argvals[2]));
2168 break;
2169 case 4:
2170 val = (XSUBR (fun)->function.a4
2171 (argvals[0], argvals[1], argvals[2], argvals[3]));
2172 break;
2173 case 5:
2174 val = (XSUBR (fun)->function.a5
2175 (argvals[0], argvals[1], argvals[2], argvals[3],
2176 argvals[4]));
2177 break;
2178 case 6:
2179 val = (XSUBR (fun)->function.a6
2180 (argvals[0], argvals[1], argvals[2], argvals[3],
2181 argvals[4], argvals[5]));
2182 break;
2183 case 7:
2184 val = (XSUBR (fun)->function.a7
2185 (argvals[0], argvals[1], argvals[2], argvals[3],
2186 argvals[4], argvals[5], argvals[6]));
2187 break;
2189 case 8:
2190 val = (XSUBR (fun)->function.a8
2191 (argvals[0], argvals[1], argvals[2], argvals[3],
2192 argvals[4], argvals[5], argvals[6], argvals[7]));
2193 break;
2195 default:
2196 /* Someone has created a subr that takes more arguments than
2197 is supported by this code. We need to either rewrite the
2198 subr to use a different argument protocol, or add more
2199 cases to this switch. */
2200 emacs_abort ();
2204 else if (COMPILEDP (fun))
2205 val = apply_lambda (fun, original_args);
2206 else
2208 if (NILP (fun))
2209 xsignal1 (Qvoid_function, original_fun);
2210 if (!CONSP (fun))
2211 xsignal1 (Qinvalid_function, original_fun);
2212 funcar = XCAR (fun);
2213 if (!SYMBOLP (funcar))
2214 xsignal1 (Qinvalid_function, original_fun);
2215 if (EQ (funcar, Qautoload))
2217 Fautoload_do_load (fun, original_fun, Qnil);
2218 goto retry;
2220 if (EQ (funcar, Qmacro))
2222 ptrdiff_t count = SPECPDL_INDEX ();
2223 Lisp_Object exp;
2224 /* Bind lexical-binding during expansion of the macro, so the
2225 macro can know reliably if the code it outputs will be
2226 interpreted using lexical-binding or not. */
2227 specbind (Qlexical_binding,
2228 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2229 exp = apply1 (Fcdr (fun), original_args);
2230 unbind_to (count, Qnil);
2231 val = eval_sub (exp);
2233 else if (EQ (funcar, Qlambda)
2234 || EQ (funcar, Qclosure))
2235 val = apply_lambda (fun, original_args);
2236 else
2237 xsignal1 (Qinvalid_function, original_fun);
2239 check_cons_list ();
2241 lisp_eval_depth--;
2242 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2243 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2244 specpdl_ptr--;
2246 return val;
2249 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2250 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2251 Then return the value FUNCTION returns.
2252 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2253 usage: (apply FUNCTION &rest ARGUMENTS) */)
2254 (ptrdiff_t nargs, Lisp_Object *args)
2256 ptrdiff_t i;
2257 EMACS_INT numargs;
2258 register Lisp_Object spread_arg;
2259 register Lisp_Object *funcall_args;
2260 Lisp_Object fun, retval;
2261 struct gcpro gcpro1;
2262 USE_SAFE_ALLOCA;
2264 fun = args [0];
2265 funcall_args = 0;
2266 spread_arg = args [nargs - 1];
2267 CHECK_LIST (spread_arg);
2269 numargs = XINT (Flength (spread_arg));
2271 if (numargs == 0)
2272 return Ffuncall (nargs - 1, args);
2273 else if (numargs == 1)
2275 args [nargs - 1] = XCAR (spread_arg);
2276 return Ffuncall (nargs, args);
2279 numargs += nargs - 2;
2281 /* Optimize for no indirection. */
2282 if (SYMBOLP (fun) && !NILP (fun)
2283 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2284 fun = indirect_function (fun);
2285 if (NILP (fun))
2287 /* Let funcall get the error. */
2288 fun = args[0];
2289 goto funcall;
2292 if (SUBRP (fun))
2294 if (numargs < XSUBR (fun)->min_args
2295 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2296 goto funcall; /* Let funcall get the error. */
2297 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2299 /* Avoid making funcall cons up a yet another new vector of arguments
2300 by explicitly supplying nil's for optional values. */
2301 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2302 for (i = numargs; i < XSUBR (fun)->max_args;)
2303 funcall_args[++i] = Qnil;
2304 GCPRO1 (*funcall_args);
2305 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2308 funcall:
2309 /* We add 1 to numargs because funcall_args includes the
2310 function itself as well as its arguments. */
2311 if (!funcall_args)
2313 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2314 GCPRO1 (*funcall_args);
2315 gcpro1.nvars = 1 + numargs;
2318 memcpy (funcall_args, args, nargs * word_size);
2319 /* Spread the last arg we got. Its first element goes in
2320 the slot that it used to occupy, hence this value of I. */
2321 i = nargs - 1;
2322 while (!NILP (spread_arg))
2324 funcall_args [i++] = XCAR (spread_arg);
2325 spread_arg = XCDR (spread_arg);
2328 /* By convention, the caller needs to gcpro Ffuncall's args. */
2329 retval = Ffuncall (gcpro1.nvars, funcall_args);
2330 UNGCPRO;
2331 SAFE_FREE ();
2333 return retval;
2336 /* Run hook variables in various ways. */
2338 static Lisp_Object
2339 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2341 Ffuncall (nargs, args);
2342 return Qnil;
2345 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2346 doc: /* Run each hook in HOOKS.
2347 Each argument should be a symbol, a hook variable.
2348 These symbols are processed in the order specified.
2349 If a hook symbol has a non-nil value, that value may be a function
2350 or a list of functions to be called to run the hook.
2351 If the value is a function, it is called with no arguments.
2352 If it is a list, the elements are called, in order, with no arguments.
2354 Major modes should not use this function directly to run their mode
2355 hook; they should use `run-mode-hooks' instead.
2357 Do not use `make-local-variable' to make a hook variable buffer-local.
2358 Instead, use `add-hook' and specify t for the LOCAL argument.
2359 usage: (run-hooks &rest HOOKS) */)
2360 (ptrdiff_t nargs, Lisp_Object *args)
2362 Lisp_Object hook[1];
2363 ptrdiff_t i;
2365 for (i = 0; i < nargs; i++)
2367 hook[0] = args[i];
2368 run_hook_with_args (1, hook, funcall_nil);
2371 return Qnil;
2374 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2375 Srun_hook_with_args, 1, MANY, 0,
2376 doc: /* Run HOOK with the specified arguments ARGS.
2377 HOOK should be a symbol, a hook variable. The value of HOOK
2378 may be nil, a function, or a list of functions. Call each
2379 function in order with arguments ARGS. The final return value
2380 is unspecified.
2382 Do not use `make-local-variable' to make a hook variable buffer-local.
2383 Instead, use `add-hook' and specify t for the LOCAL argument.
2384 usage: (run-hook-with-args HOOK &rest ARGS) */)
2385 (ptrdiff_t nargs, Lisp_Object *args)
2387 return run_hook_with_args (nargs, args, funcall_nil);
2390 /* NB this one still documents a specific non-nil return value.
2391 (As did run-hook-with-args and run-hook-with-args-until-failure
2392 until they were changed in 24.1.) */
2393 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2394 Srun_hook_with_args_until_success, 1, MANY, 0,
2395 doc: /* Run HOOK with the specified arguments ARGS.
2396 HOOK should be a symbol, a hook variable. The value of HOOK
2397 may be nil, a function, or a list of functions. Call each
2398 function in order with arguments ARGS, stopping at the first
2399 one that returns non-nil, and return that value. Otherwise (if
2400 all functions return nil, or if there are no functions to call),
2401 return nil.
2403 Do not use `make-local-variable' to make a hook variable buffer-local.
2404 Instead, use `add-hook' and specify t for the LOCAL argument.
2405 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2406 (ptrdiff_t nargs, Lisp_Object *args)
2408 return run_hook_with_args (nargs, args, Ffuncall);
2411 static Lisp_Object
2412 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2414 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2417 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2418 Srun_hook_with_args_until_failure, 1, MANY, 0,
2419 doc: /* Run HOOK with the specified arguments ARGS.
2420 HOOK should be a symbol, a hook variable. The value of HOOK
2421 may be nil, a function, or a list of functions. Call each
2422 function in order with arguments ARGS, stopping at the first
2423 one that returns nil, and return nil. Otherwise (if all functions
2424 return non-nil, or if there are no functions to call), return non-nil
2425 \(do not rely on the precise return value in this case).
2427 Do not use `make-local-variable' to make a hook variable buffer-local.
2428 Instead, use `add-hook' and specify t for the LOCAL argument.
2429 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2430 (ptrdiff_t nargs, Lisp_Object *args)
2432 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2435 static Lisp_Object
2436 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2438 Lisp_Object tmp = args[0], ret;
2439 args[0] = args[1];
2440 args[1] = tmp;
2441 ret = Ffuncall (nargs, args);
2442 args[1] = args[0];
2443 args[0] = tmp;
2444 return ret;
2447 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2448 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2449 I.e. instead of calling each function FUN directly with arguments ARGS,
2450 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2451 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2452 aborts and returns that value.
2453 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2454 (ptrdiff_t nargs, Lisp_Object *args)
2456 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2459 /* ARGS[0] should be a hook symbol.
2460 Call each of the functions in the hook value, passing each of them
2461 as arguments all the rest of ARGS (all NARGS - 1 elements).
2462 FUNCALL specifies how to call each function on the hook.
2463 The caller (or its caller, etc) must gcpro all of ARGS,
2464 except that it isn't necessary to gcpro ARGS[0]. */
2466 Lisp_Object
2467 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2468 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2470 Lisp_Object sym, val, ret = Qnil;
2471 struct gcpro gcpro1, gcpro2, gcpro3;
2473 /* If we are dying or still initializing,
2474 don't do anything--it would probably crash if we tried. */
2475 if (NILP (Vrun_hooks))
2476 return Qnil;
2478 sym = args[0];
2479 val = find_symbol_value (sym);
2481 if (EQ (val, Qunbound) || NILP (val))
2482 return ret;
2483 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2485 args[0] = val;
2486 return funcall (nargs, args);
2488 else
2490 Lisp_Object global_vals = Qnil;
2491 GCPRO3 (sym, val, global_vals);
2493 for (;
2494 CONSP (val) && NILP (ret);
2495 val = XCDR (val))
2497 if (EQ (XCAR (val), Qt))
2499 /* t indicates this hook has a local binding;
2500 it means to run the global binding too. */
2501 global_vals = Fdefault_value (sym);
2502 if (NILP (global_vals)) continue;
2504 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2506 args[0] = global_vals;
2507 ret = funcall (nargs, args);
2509 else
2511 for (;
2512 CONSP (global_vals) && NILP (ret);
2513 global_vals = XCDR (global_vals))
2515 args[0] = XCAR (global_vals);
2516 /* In a global value, t should not occur. If it does, we
2517 must ignore it to avoid an endless loop. */
2518 if (!EQ (args[0], Qt))
2519 ret = funcall (nargs, args);
2523 else
2525 args[0] = XCAR (val);
2526 ret = funcall (nargs, args);
2530 UNGCPRO;
2531 return ret;
2535 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2537 void
2538 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2540 Lisp_Object temp[3];
2541 temp[0] = hook;
2542 temp[1] = arg1;
2543 temp[2] = arg2;
2545 Frun_hook_with_args (3, temp);
2548 /* Apply fn to arg. */
2549 Lisp_Object
2550 apply1 (Lisp_Object fn, Lisp_Object arg)
2552 struct gcpro gcpro1;
2554 GCPRO1 (fn);
2555 if (NILP (arg))
2556 RETURN_UNGCPRO (Ffuncall (1, &fn));
2557 gcpro1.nvars = 2;
2559 Lisp_Object args[2];
2560 args[0] = fn;
2561 args[1] = arg;
2562 gcpro1.var = args;
2563 RETURN_UNGCPRO (Fapply (2, args));
2567 /* Call function fn on no arguments. */
2568 Lisp_Object
2569 call0 (Lisp_Object fn)
2571 struct gcpro gcpro1;
2573 GCPRO1 (fn);
2574 RETURN_UNGCPRO (Ffuncall (1, &fn));
2577 /* Call function fn with 1 argument arg1. */
2578 /* ARGSUSED */
2579 Lisp_Object
2580 call1 (Lisp_Object fn, Lisp_Object arg1)
2582 struct gcpro gcpro1;
2583 Lisp_Object args[2];
2585 args[0] = fn;
2586 args[1] = arg1;
2587 GCPRO1 (args[0]);
2588 gcpro1.nvars = 2;
2589 RETURN_UNGCPRO (Ffuncall (2, args));
2592 /* Call function fn with 2 arguments arg1, arg2. */
2593 /* ARGSUSED */
2594 Lisp_Object
2595 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2597 struct gcpro gcpro1;
2598 Lisp_Object args[3];
2599 args[0] = fn;
2600 args[1] = arg1;
2601 args[2] = arg2;
2602 GCPRO1 (args[0]);
2603 gcpro1.nvars = 3;
2604 RETURN_UNGCPRO (Ffuncall (3, args));
2607 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2608 /* ARGSUSED */
2609 Lisp_Object
2610 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2612 struct gcpro gcpro1;
2613 Lisp_Object args[4];
2614 args[0] = fn;
2615 args[1] = arg1;
2616 args[2] = arg2;
2617 args[3] = arg3;
2618 GCPRO1 (args[0]);
2619 gcpro1.nvars = 4;
2620 RETURN_UNGCPRO (Ffuncall (4, args));
2623 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2624 /* ARGSUSED */
2625 Lisp_Object
2626 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2627 Lisp_Object arg4)
2629 struct gcpro gcpro1;
2630 Lisp_Object args[5];
2631 args[0] = fn;
2632 args[1] = arg1;
2633 args[2] = arg2;
2634 args[3] = arg3;
2635 args[4] = arg4;
2636 GCPRO1 (args[0]);
2637 gcpro1.nvars = 5;
2638 RETURN_UNGCPRO (Ffuncall (5, args));
2641 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2642 /* ARGSUSED */
2643 Lisp_Object
2644 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2645 Lisp_Object arg4, Lisp_Object arg5)
2647 struct gcpro gcpro1;
2648 Lisp_Object args[6];
2649 args[0] = fn;
2650 args[1] = arg1;
2651 args[2] = arg2;
2652 args[3] = arg3;
2653 args[4] = arg4;
2654 args[5] = arg5;
2655 GCPRO1 (args[0]);
2656 gcpro1.nvars = 6;
2657 RETURN_UNGCPRO (Ffuncall (6, args));
2660 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2661 /* ARGSUSED */
2662 Lisp_Object
2663 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2664 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2666 struct gcpro gcpro1;
2667 Lisp_Object args[7];
2668 args[0] = fn;
2669 args[1] = arg1;
2670 args[2] = arg2;
2671 args[3] = arg3;
2672 args[4] = arg4;
2673 args[5] = arg5;
2674 args[6] = arg6;
2675 GCPRO1 (args[0]);
2676 gcpro1.nvars = 7;
2677 RETURN_UNGCPRO (Ffuncall (7, args));
2680 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2681 /* ARGSUSED */
2682 Lisp_Object
2683 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2684 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2686 struct gcpro gcpro1;
2687 Lisp_Object args[8];
2688 args[0] = fn;
2689 args[1] = arg1;
2690 args[2] = arg2;
2691 args[3] = arg3;
2692 args[4] = arg4;
2693 args[5] = arg5;
2694 args[6] = arg6;
2695 args[7] = arg7;
2696 GCPRO1 (args[0]);
2697 gcpro1.nvars = 8;
2698 RETURN_UNGCPRO (Ffuncall (8, args));
2701 /* The caller should GCPRO all the elements of ARGS. */
2703 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2704 doc: /* Non-nil if OBJECT is a function. */)
2705 (Lisp_Object object)
2707 if (FUNCTIONP (object))
2708 return Qt;
2709 return Qnil;
2712 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2713 doc: /* Call first argument as a function, passing remaining arguments to it.
2714 Return the value that function returns.
2715 Thus, (funcall 'cons 'x 'y) returns (x . y).
2716 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2717 (ptrdiff_t nargs, Lisp_Object *args)
2719 Lisp_Object fun, original_fun;
2720 Lisp_Object funcar;
2721 ptrdiff_t numargs = nargs - 1;
2722 Lisp_Object lisp_numargs;
2723 Lisp_Object val;
2724 register Lisp_Object *internal_args;
2725 ptrdiff_t i;
2727 QUIT;
2729 if (++lisp_eval_depth > max_lisp_eval_depth)
2731 if (max_lisp_eval_depth < 100)
2732 max_lisp_eval_depth = 100;
2733 if (lisp_eval_depth > max_lisp_eval_depth)
2734 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2737 /* This also GCPROs them. */
2738 record_in_backtrace (args[0], &args[1], nargs - 1);
2740 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2741 maybe_gc ();
2743 if (debug_on_next_call)
2744 do_debug_on_call (Qlambda);
2746 check_cons_list ();
2748 original_fun = args[0];
2750 retry:
2752 /* Optimize for no indirection. */
2753 fun = original_fun;
2754 if (SYMBOLP (fun) && !NILP (fun)
2755 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2756 fun = indirect_function (fun);
2758 if (SUBRP (fun))
2760 if (numargs < XSUBR (fun)->min_args
2761 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2763 XSETFASTINT (lisp_numargs, numargs);
2764 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2767 else if (XSUBR (fun)->max_args == UNEVALLED)
2768 xsignal1 (Qinvalid_function, original_fun);
2770 else if (XSUBR (fun)->max_args == MANY)
2771 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2772 else
2774 if (XSUBR (fun)->max_args > numargs)
2776 internal_args = alloca (XSUBR (fun)->max_args
2777 * sizeof *internal_args);
2778 memcpy (internal_args, args + 1, numargs * word_size);
2779 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2780 internal_args[i] = Qnil;
2782 else
2783 internal_args = args + 1;
2784 switch (XSUBR (fun)->max_args)
2786 case 0:
2787 val = (XSUBR (fun)->function.a0 ());
2788 break;
2789 case 1:
2790 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2791 break;
2792 case 2:
2793 val = (XSUBR (fun)->function.a2
2794 (internal_args[0], internal_args[1]));
2795 break;
2796 case 3:
2797 val = (XSUBR (fun)->function.a3
2798 (internal_args[0], internal_args[1], internal_args[2]));
2799 break;
2800 case 4:
2801 val = (XSUBR (fun)->function.a4
2802 (internal_args[0], internal_args[1], internal_args[2],
2803 internal_args[3]));
2804 break;
2805 case 5:
2806 val = (XSUBR (fun)->function.a5
2807 (internal_args[0], internal_args[1], internal_args[2],
2808 internal_args[3], internal_args[4]));
2809 break;
2810 case 6:
2811 val = (XSUBR (fun)->function.a6
2812 (internal_args[0], internal_args[1], internal_args[2],
2813 internal_args[3], internal_args[4], internal_args[5]));
2814 break;
2815 case 7:
2816 val = (XSUBR (fun)->function.a7
2817 (internal_args[0], internal_args[1], internal_args[2],
2818 internal_args[3], internal_args[4], internal_args[5],
2819 internal_args[6]));
2820 break;
2822 case 8:
2823 val = (XSUBR (fun)->function.a8
2824 (internal_args[0], internal_args[1], internal_args[2],
2825 internal_args[3], internal_args[4], internal_args[5],
2826 internal_args[6], internal_args[7]));
2827 break;
2829 default:
2831 /* If a subr takes more than 8 arguments without using MANY
2832 or UNEVALLED, we need to extend this function to support it.
2833 Until this is done, there is no way to call the function. */
2834 emacs_abort ();
2838 else if (COMPILEDP (fun))
2839 val = funcall_lambda (fun, numargs, args + 1);
2840 else
2842 if (NILP (fun))
2843 xsignal1 (Qvoid_function, original_fun);
2844 if (!CONSP (fun))
2845 xsignal1 (Qinvalid_function, original_fun);
2846 funcar = XCAR (fun);
2847 if (!SYMBOLP (funcar))
2848 xsignal1 (Qinvalid_function, original_fun);
2849 if (EQ (funcar, Qlambda)
2850 || EQ (funcar, Qclosure))
2851 val = funcall_lambda (fun, numargs, args + 1);
2852 else if (EQ (funcar, Qautoload))
2854 Fautoload_do_load (fun, original_fun, Qnil);
2855 check_cons_list ();
2856 goto retry;
2858 else
2859 xsignal1 (Qinvalid_function, original_fun);
2861 check_cons_list ();
2862 lisp_eval_depth--;
2863 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2864 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2865 specpdl_ptr--;
2866 return val;
2869 static Lisp_Object
2870 apply_lambda (Lisp_Object fun, Lisp_Object args)
2872 Lisp_Object args_left;
2873 ptrdiff_t i;
2874 EMACS_INT numargs;
2875 register Lisp_Object *arg_vector;
2876 struct gcpro gcpro1, gcpro2, gcpro3;
2877 register Lisp_Object tem;
2878 USE_SAFE_ALLOCA;
2880 numargs = XFASTINT (Flength (args));
2881 SAFE_ALLOCA_LISP (arg_vector, numargs);
2882 args_left = args;
2884 GCPRO3 (*arg_vector, args_left, fun);
2885 gcpro1.nvars = 0;
2887 for (i = 0; i < numargs; )
2889 tem = Fcar (args_left), args_left = Fcdr (args_left);
2890 tem = eval_sub (tem);
2891 arg_vector[i++] = tem;
2892 gcpro1.nvars = i;
2895 UNGCPRO;
2897 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2898 set_backtrace_nargs (specpdl_ptr - 1, i);
2899 tem = funcall_lambda (fun, numargs, arg_vector);
2901 /* Do the debug-on-exit now, while arg_vector still exists. */
2902 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2904 /* Don't do it again when we return to eval. */
2905 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2906 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2908 SAFE_FREE ();
2909 return tem;
2912 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2913 and return the result of evaluation.
2914 FUN must be either a lambda-expression or a compiled-code object. */
2916 static Lisp_Object
2917 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2918 register Lisp_Object *arg_vector)
2920 Lisp_Object val, syms_left, next, lexenv;
2921 ptrdiff_t count = SPECPDL_INDEX ();
2922 ptrdiff_t i;
2923 bool optional, rest;
2925 if (CONSP (fun))
2927 if (EQ (XCAR (fun), Qclosure))
2929 fun = XCDR (fun); /* Drop `closure'. */
2930 lexenv = XCAR (fun);
2931 CHECK_LIST_CONS (fun, fun);
2933 else
2934 lexenv = Qnil;
2935 syms_left = XCDR (fun);
2936 if (CONSP (syms_left))
2937 syms_left = XCAR (syms_left);
2938 else
2939 xsignal1 (Qinvalid_function, fun);
2941 else if (COMPILEDP (fun))
2943 syms_left = AREF (fun, COMPILED_ARGLIST);
2944 if (INTEGERP (syms_left))
2945 /* A byte-code object with a non-nil `push args' slot means we
2946 shouldn't bind any arguments, instead just call the byte-code
2947 interpreter directly; it will push arguments as necessary.
2949 Byte-code objects with either a non-existent, or a nil value for
2950 the `push args' slot (the default), have dynamically-bound
2951 arguments, and use the argument-binding code below instead (as do
2952 all interpreted functions, even lexically bound ones). */
2954 /* If we have not actually read the bytecode string
2955 and constants vector yet, fetch them from the file. */
2956 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2957 Ffetch_bytecode (fun);
2958 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2959 AREF (fun, COMPILED_CONSTANTS),
2960 AREF (fun, COMPILED_STACK_DEPTH),
2961 syms_left,
2962 nargs, arg_vector);
2964 lexenv = Qnil;
2966 else
2967 emacs_abort ();
2969 i = optional = rest = 0;
2970 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2972 QUIT;
2974 next = XCAR (syms_left);
2975 if (!SYMBOLP (next))
2976 xsignal1 (Qinvalid_function, fun);
2978 if (EQ (next, Qand_rest))
2979 rest = 1;
2980 else if (EQ (next, Qand_optional))
2981 optional = 1;
2982 else
2984 Lisp_Object arg;
2985 if (rest)
2987 arg = Flist (nargs - i, &arg_vector[i]);
2988 i = nargs;
2990 else if (i < nargs)
2991 arg = arg_vector[i++];
2992 else if (!optional)
2993 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2994 else
2995 arg = Qnil;
2997 /* Bind the argument. */
2998 if (!NILP (lexenv) && SYMBOLP (next))
2999 /* Lexically bind NEXT by adding it to the lexenv alist. */
3000 lexenv = Fcons (Fcons (next, arg), lexenv);
3001 else
3002 /* Dynamically bind NEXT. */
3003 specbind (next, arg);
3007 if (!NILP (syms_left))
3008 xsignal1 (Qinvalid_function, fun);
3009 else if (i < nargs)
3010 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3012 if (!EQ (lexenv, Vinternal_interpreter_environment))
3013 /* Instantiate a new lexical environment. */
3014 specbind (Qinternal_interpreter_environment, lexenv);
3016 if (CONSP (fun))
3017 val = Fprogn (XCDR (XCDR (fun)));
3018 else
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 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3025 AREF (fun, COMPILED_CONSTANTS),
3026 AREF (fun, COMPILED_STACK_DEPTH),
3027 Qnil, 0, 0);
3030 return unbind_to (count, val);
3033 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3034 1, 1, 0,
3035 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3036 (Lisp_Object object)
3038 Lisp_Object tem;
3040 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3042 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3043 if (!CONSP (tem))
3045 tem = AREF (object, COMPILED_BYTECODE);
3046 if (CONSP (tem) && STRINGP (XCAR (tem)))
3047 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3048 else
3049 error ("Invalid byte code");
3051 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3052 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3054 return object;
3057 /* Return true if SYMBOL currently has a let-binding
3058 which was made in the buffer that is now current. */
3060 bool
3061 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3063 union specbinding *p;
3064 Lisp_Object buf = Fcurrent_buffer ();
3066 for (p = specpdl_ptr; p > specpdl; )
3067 if ((--p)->kind > SPECPDL_LET)
3069 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3070 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3071 if (symbol == let_bound_symbol
3072 && EQ (specpdl_where (p), buf))
3073 return 1;
3076 return 0;
3079 bool
3080 let_shadows_global_binding_p (Lisp_Object symbol)
3082 union specbinding *p;
3084 for (p = specpdl_ptr; p > specpdl; )
3085 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3086 return 1;
3088 return 0;
3091 /* `specpdl_ptr->symbol' is a field which describes which variable is
3092 let-bound, so it can be properly undone when we unbind_to.
3093 It can have the following two shapes:
3094 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3095 a symbol that is not buffer-local (at least at the time
3096 the let binding started). Note also that it should not be
3097 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3098 to record V2 here).
3099 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3100 variable SYMBOL which can be buffer-local. WHERE tells us
3101 which buffer is affected (or nil if the let-binding affects the
3102 global value of the variable) and BUFFER tells us which buffer was
3103 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3104 BUFFER did not yet have a buffer-local value). */
3106 void
3107 specbind (Lisp_Object symbol, Lisp_Object value)
3109 struct Lisp_Symbol *sym;
3111 CHECK_SYMBOL (symbol);
3112 sym = XSYMBOL (symbol);
3113 if (specpdl_ptr == specpdl + specpdl_size)
3114 grow_specpdl ();
3116 start:
3117 switch (sym->redirect)
3119 case SYMBOL_VARALIAS:
3120 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3121 case SYMBOL_PLAINVAL:
3122 /* The most common case is that of a non-constant symbol with a
3123 trivial value. Make that as fast as we can. */
3124 specpdl_ptr->let.kind = SPECPDL_LET;
3125 specpdl_ptr->let.symbol = symbol;
3126 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3127 ++specpdl_ptr;
3128 if (!sym->constant)
3129 SET_SYMBOL_VAL (sym, value);
3130 else
3131 set_internal (symbol, value, Qnil, 1);
3132 break;
3133 case SYMBOL_LOCALIZED:
3134 if (SYMBOL_BLV (sym)->frame_local)
3135 error ("Frame-local vars cannot be let-bound");
3136 case SYMBOL_FORWARDED:
3138 Lisp_Object ovalue = find_symbol_value (symbol);
3139 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3140 specpdl_ptr->let.symbol = symbol;
3141 specpdl_ptr->let.old_value = ovalue;
3142 specpdl_ptr->let.where = Fcurrent_buffer ();
3144 eassert (sym->redirect != SYMBOL_LOCALIZED
3145 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3147 if (sym->redirect == SYMBOL_LOCALIZED)
3149 if (!blv_found (SYMBOL_BLV (sym)))
3150 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3152 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3154 /* If SYMBOL is a per-buffer variable which doesn't have a
3155 buffer-local value here, make the `let' change the global
3156 value by changing the value of SYMBOL in all buffers not
3157 having their own value. This is consistent with what
3158 happens with other buffer-local variables. */
3159 if (NILP (Flocal_variable_p (symbol, Qnil)))
3161 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3162 ++specpdl_ptr;
3163 Fset_default (symbol, value);
3164 return;
3167 else
3168 specpdl_ptr->let.kind = SPECPDL_LET;
3170 specpdl_ptr++;
3171 set_internal (symbol, value, Qnil, 1);
3172 break;
3174 default: emacs_abort ();
3178 void
3179 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3181 if (specpdl_ptr == specpdl + specpdl_size)
3182 grow_specpdl ();
3183 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3184 specpdl_ptr->unwind.func = function;
3185 specpdl_ptr->unwind.arg = arg;
3186 specpdl_ptr++;
3189 Lisp_Object
3190 unbind_to (ptrdiff_t count, Lisp_Object value)
3192 Lisp_Object quitf = Vquit_flag;
3193 struct gcpro gcpro1, gcpro2;
3195 GCPRO2 (value, quitf);
3196 Vquit_flag = Qnil;
3198 while (specpdl_ptr != specpdl + count)
3200 /* Decrement specpdl_ptr before we do the work to unbind it, so
3201 that an error in unbinding won't try to unbind the same entry
3202 again. Take care to copy any parts of the binding needed
3203 before invoking any code that can make more bindings. */
3205 specpdl_ptr--;
3207 switch (specpdl_ptr->kind)
3209 case SPECPDL_UNWIND:
3210 specpdl_func (specpdl_ptr) (specpdl_arg (specpdl_ptr));
3211 break;
3212 case SPECPDL_LET:
3213 /* If variable has a trivial value (no forwarding), we can
3214 just set it. No need to check for constant symbols here,
3215 since that was already done by specbind. */
3216 if (XSYMBOL (specpdl_symbol (specpdl_ptr))->redirect
3217 == SYMBOL_PLAINVAL)
3218 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (specpdl_ptr)),
3219 specpdl_old_value (specpdl_ptr));
3220 else
3221 /* NOTE: we only ever come here if make_local_foo was used for
3222 the first time on this var within this let. */
3223 Fset_default (specpdl_symbol (specpdl_ptr),
3224 specpdl_old_value (specpdl_ptr));
3225 break;
3226 case SPECPDL_BACKTRACE:
3227 break;
3228 case SPECPDL_LET_LOCAL:
3229 case SPECPDL_LET_DEFAULT:
3230 { /* If the symbol is a list, it is really (SYMBOL WHERE
3231 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3232 frame. If WHERE is a buffer or frame, this indicates we
3233 bound a variable that had a buffer-local or frame-local
3234 binding. WHERE nil means that the variable had the default
3235 value when it was bound. CURRENT-BUFFER is the buffer that
3236 was current when the variable was bound. */
3237 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3238 Lisp_Object where = specpdl_where (specpdl_ptr);
3239 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3240 eassert (BUFFERP (where));
3242 if (specpdl_ptr->kind == SPECPDL_LET_DEFAULT)
3243 Fset_default (symbol, old_value);
3244 /* If this was a local binding, reset the value in the appropriate
3245 buffer, but only if that buffer's binding still exists. */
3246 else if (!NILP (Flocal_variable_p (symbol, where)))
3247 set_internal (symbol, old_value, where, 1);
3249 break;
3253 if (NILP (Vquit_flag) && !NILP (quitf))
3254 Vquit_flag = quitf;
3256 UNGCPRO;
3257 return value;
3260 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3261 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3262 A special variable is one that will be bound dynamically, even in a
3263 context where binding is lexical by default. */)
3264 (Lisp_Object symbol)
3266 CHECK_SYMBOL (symbol);
3267 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3271 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3272 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3273 The debugger is entered when that frame exits, if the flag is non-nil. */)
3274 (Lisp_Object level, Lisp_Object flag)
3276 union specbinding *pdl = backtrace_top ();
3277 register EMACS_INT i;
3279 CHECK_NUMBER (level);
3281 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3282 pdl = backtrace_next (pdl);
3284 if (backtrace_p (pdl))
3285 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3287 return flag;
3290 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3291 doc: /* Print a trace of Lisp function calls currently active.
3292 Output stream used is value of `standard-output'. */)
3293 (void)
3295 union specbinding *pdl = backtrace_top ();
3296 Lisp_Object tem;
3297 Lisp_Object old_print_level = Vprint_level;
3299 if (NILP (Vprint_level))
3300 XSETFASTINT (Vprint_level, 8);
3302 while (backtrace_p (pdl))
3304 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3305 if (backtrace_nargs (pdl) == UNEVALLED)
3307 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3308 Qnil);
3309 write_string ("\n", -1);
3311 else
3313 tem = backtrace_function (pdl);
3314 Fprin1 (tem, Qnil); /* This can QUIT. */
3315 write_string ("(", -1);
3317 ptrdiff_t i;
3318 for (i = 0; i < backtrace_nargs (pdl); i++)
3320 if (i) write_string (" ", -1);
3321 Fprin1 (backtrace_args (pdl)[i], Qnil);
3324 write_string (")\n", -1);
3326 pdl = backtrace_next (pdl);
3329 Vprint_level = old_print_level;
3330 return Qnil;
3333 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3334 doc: /* Return the function and arguments NFRAMES up from current execution point.
3335 If that frame has not evaluated the arguments yet (or is a special form),
3336 the value is (nil FUNCTION ARG-FORMS...).
3337 If that frame has evaluated its arguments and called its function already,
3338 the value is (t FUNCTION ARG-VALUES...).
3339 A &rest arg is represented as the tail of the list ARG-VALUES.
3340 FUNCTION is whatever was supplied as car of evaluated list,
3341 or a lambda expression for macro calls.
3342 If NFRAMES is more than the number of frames, the value is nil. */)
3343 (Lisp_Object nframes)
3345 union specbinding *pdl = backtrace_top ();
3346 register EMACS_INT i;
3348 CHECK_NATNUM (nframes);
3350 /* Find the frame requested. */
3351 for (i = 0; backtrace_p (pdl) && i < XFASTINT (nframes); i++)
3352 pdl = backtrace_next (pdl);
3354 if (!backtrace_p (pdl))
3355 return Qnil;
3356 if (backtrace_nargs (pdl) == UNEVALLED)
3357 return Fcons (Qnil,
3358 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3359 else
3361 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3363 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3368 void
3369 mark_specpdl (void)
3371 union specbinding *pdl;
3372 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3374 switch (pdl->kind)
3376 case SPECPDL_UNWIND:
3377 mark_object (specpdl_arg (pdl));
3378 break;
3380 case SPECPDL_BACKTRACE:
3382 ptrdiff_t nargs = backtrace_nargs (pdl);
3383 mark_object (backtrace_function (pdl));
3384 if (nargs == UNEVALLED)
3385 nargs = 1;
3386 while (nargs--)
3387 mark_object (backtrace_args (pdl)[nargs]);
3389 break;
3391 case SPECPDL_LET_DEFAULT:
3392 case SPECPDL_LET_LOCAL:
3393 mark_object (specpdl_where (pdl));
3394 /* Fall through. */
3395 case SPECPDL_LET:
3396 mark_object (specpdl_symbol (pdl));
3397 mark_object (specpdl_old_value (pdl));
3398 break;
3403 void
3404 get_backtrace (Lisp_Object array)
3406 union specbinding *pdl = backtrace_next (backtrace_top ());
3407 ptrdiff_t i = 0, asize = ASIZE (array);
3409 /* Copy the backtrace contents into working memory. */
3410 for (; i < asize; i++)
3412 if (backtrace_p (pdl))
3414 ASET (array, i, backtrace_function (pdl));
3415 pdl = backtrace_next (pdl);
3417 else
3418 ASET (array, i, Qnil);
3422 Lisp_Object backtrace_top_function (void)
3424 union specbinding *pdl = backtrace_top ();
3425 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3428 void
3429 syms_of_eval (void)
3431 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3432 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3433 If Lisp code tries to increase the total number past this amount,
3434 an error is signaled.
3435 You can safely use a value considerably larger than the default value,
3436 if that proves inconveniently small. However, if you increase it too far,
3437 Emacs could run out of memory trying to make the stack bigger. */);
3439 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3440 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3442 This limit serves to catch infinite recursions for you before they cause
3443 actual stack overflow in C, which would be fatal for Emacs.
3444 You can safely make it considerably larger than its default value,
3445 if that proves inconveniently small. However, if you increase it too far,
3446 Emacs could overflow the real C stack, and crash. */);
3448 DEFVAR_LISP ("quit-flag", Vquit_flag,
3449 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3450 If the value is t, that means do an ordinary quit.
3451 If the value equals `throw-on-input', that means quit by throwing
3452 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3453 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3454 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3455 Vquit_flag = Qnil;
3457 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3458 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3459 Note that `quit-flag' will still be set by typing C-g,
3460 so a quit will be signaled as soon as `inhibit-quit' is nil.
3461 To prevent this happening, set `quit-flag' to nil
3462 before making `inhibit-quit' nil. */);
3463 Vinhibit_quit = Qnil;
3465 DEFSYM (Qinhibit_quit, "inhibit-quit");
3466 DEFSYM (Qautoload, "autoload");
3467 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3468 DEFSYM (Qmacro, "macro");
3469 DEFSYM (Qdeclare, "declare");
3471 /* Note that the process handling also uses Qexit, but we don't want
3472 to staticpro it twice, so we just do it here. */
3473 DEFSYM (Qexit, "exit");
3475 DEFSYM (Qinteractive, "interactive");
3476 DEFSYM (Qcommandp, "commandp");
3477 DEFSYM (Qand_rest, "&rest");
3478 DEFSYM (Qand_optional, "&optional");
3479 DEFSYM (Qclosure, "closure");
3480 DEFSYM (Qdebug, "debug");
3482 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3483 doc: /* Non-nil means never enter the debugger.
3484 Normally set while the debugger is already active, to avoid recursive
3485 invocations. */);
3486 Vinhibit_debugger = Qnil;
3488 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3489 doc: /* Non-nil means enter debugger if an error is signaled.
3490 Does not apply to errors handled by `condition-case' or those
3491 matched by `debug-ignored-errors'.
3492 If the value is a list, an error only means to enter the debugger
3493 if one of its condition symbols appears in the list.
3494 When you evaluate an expression interactively, this variable
3495 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3496 The command `toggle-debug-on-error' toggles this.
3497 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3498 Vdebug_on_error = Qnil;
3500 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3501 doc: /* List of errors for which the debugger should not be called.
3502 Each element may be a condition-name or a regexp that matches error messages.
3503 If any element applies to a given error, that error skips the debugger
3504 and just returns to top level.
3505 This overrides the variable `debug-on-error'.
3506 It does not apply to errors handled by `condition-case'. */);
3507 Vdebug_ignored_errors = Qnil;
3509 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3510 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3511 Does not apply if quit is handled by a `condition-case'. */);
3512 debug_on_quit = 0;
3514 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3515 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3517 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3518 doc: /* Non-nil means debugger may continue execution.
3519 This is nil when the debugger is called under circumstances where it
3520 might not be safe to continue. */);
3521 debugger_may_continue = 1;
3523 DEFVAR_LISP ("debugger", Vdebugger,
3524 doc: /* Function to call to invoke debugger.
3525 If due to frame exit, args are `exit' and the value being returned;
3526 this function's value will be returned instead of that.
3527 If due to error, args are `error' and a list of the args to `signal'.
3528 If due to `apply' or `funcall' entry, one arg, `lambda'.
3529 If due to `eval' entry, one arg, t. */);
3530 Vdebugger = Qnil;
3532 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3533 doc: /* If non-nil, this is a function for `signal' to call.
3534 It receives the same arguments that `signal' was given.
3535 The Edebug package uses this to regain control. */);
3536 Vsignal_hook_function = Qnil;
3538 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3539 doc: /* Non-nil means call the debugger regardless of condition handlers.
3540 Note that `debug-on-error', `debug-on-quit' and friends
3541 still determine whether to handle the particular condition. */);
3542 Vdebug_on_signal = Qnil;
3544 /* When lexical binding is being used,
3545 Vinternal_interpreter_environment is non-nil, and contains an alist
3546 of lexically-bound variable, or (t), indicating an empty
3547 environment. The lisp name of this variable would be
3548 `internal-interpreter-environment' if it weren't hidden.
3549 Every element of this list can be either a cons (VAR . VAL)
3550 specifying a lexical binding, or a single symbol VAR indicating
3551 that this variable should use dynamic scoping. */
3552 DEFSYM (Qinternal_interpreter_environment,
3553 "internal-interpreter-environment");
3554 DEFVAR_LISP ("internal-interpreter-environment",
3555 Vinternal_interpreter_environment,
3556 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3557 When lexical binding is not being used, this variable is nil.
3558 A value of `(t)' indicates an empty environment, otherwise it is an
3559 alist of active lexical bindings. */);
3560 Vinternal_interpreter_environment = Qnil;
3561 /* Don't export this variable to Elisp, so no one can mess with it
3562 (Just imagine if someone makes it buffer-local). */
3563 Funintern (Qinternal_interpreter_environment, Qnil);
3565 DEFSYM (Vrun_hooks, "run-hooks");
3567 staticpro (&Vautoload_queue);
3568 Vautoload_queue = Qnil;
3569 staticpro (&Vsignaling_function);
3570 Vsignaling_function = Qnil;
3572 inhibit_lisp_code = Qnil;
3574 defsubr (&Sor);
3575 defsubr (&Sand);
3576 defsubr (&Sif);
3577 defsubr (&Scond);
3578 defsubr (&Sprogn);
3579 defsubr (&Sprog1);
3580 defsubr (&Sprog2);
3581 defsubr (&Ssetq);
3582 defsubr (&Squote);
3583 defsubr (&Sfunction);
3584 defsubr (&Sdefvar);
3585 defsubr (&Sdefvaralias);
3586 defsubr (&Sdefconst);
3587 defsubr (&Smake_var_non_special);
3588 defsubr (&Slet);
3589 defsubr (&SletX);
3590 defsubr (&Swhile);
3591 defsubr (&Smacroexpand);
3592 defsubr (&Scatch);
3593 defsubr (&Sthrow);
3594 defsubr (&Sunwind_protect);
3595 defsubr (&Scondition_case);
3596 defsubr (&Ssignal);
3597 defsubr (&Scommandp);
3598 defsubr (&Sautoload);
3599 defsubr (&Sautoload_do_load);
3600 defsubr (&Seval);
3601 defsubr (&Sapply);
3602 defsubr (&Sfuncall);
3603 defsubr (&Srun_hooks);
3604 defsubr (&Srun_hook_with_args);
3605 defsubr (&Srun_hook_with_args_until_success);
3606 defsubr (&Srun_hook_with_args_until_failure);
3607 defsubr (&Srun_hook_wrapped);
3608 defsubr (&Sfetch_bytecode);
3609 defsubr (&Sbacktrace_debug);
3610 defsubr (&Sbacktrace);
3611 defsubr (&Sbacktrace_frame);
3612 defsubr (&Sspecial_variable_p);
3613 defsubr (&Sfunctionp);