merge from trunk; clean up some issues
[emacs.git] / src / eval.c
blobbe9de93bf1f65cb2a929f116616a0aaf01141070
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. */
81 /* ptrdiff_t specpdl_size; */
83 /* Pointer to beginning of specpdl. */
85 /* struct specbinding *specpdl; */
87 /* Pointer to first unused element in specpdl. */
89 /* struct specbinding *specpdl_ptr; */
91 /* Depth in Lisp evaluations and function calls. */
93 /* static EMACS_INT lisp_eval_depth; */
95 /* The value of num_nonmacro_input_events as of the last time we
96 started to enter the debugger. If we decide to enter the debugger
97 again when this is still equal to num_nonmacro_input_events, then we
98 know that the debugger itself has an error, and we should just
99 signal the error instead of entering an infinite loop of debugger
100 invocations. */
102 static EMACS_INT when_entered_debugger;
104 /* The function from which the last `signal' was called. Set in
105 Fsignal. */
106 /* FIXME: We should probably get rid of this! */
107 Lisp_Object Vsignaling_function;
109 /* If non-nil, Lisp code must not be run since some part of Emacs is
110 in an inconsistent state. Currently, x-create-frame uses this to
111 avoid triggering window-configuration-change-hook while the new
112 frame is half-initialized. */
113 Lisp_Object inhibit_lisp_code;
115 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
116 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
118 /* Functions to modify slots of backtrace records. */
120 static void set_backtrace_args (struct specbinding *pdl, Lisp_Object *args)
121 { eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.args = args; }
123 static void set_backtrace_nargs (struct specbinding *pdl, ptrdiff_t n)
124 { eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.nargs = n; }
126 void set_backtrace_debug_on_exit (struct specbinding *pdl, bool doe)
127 { eassert (pdl->kind == SPECPDL_BACKTRACE); pdl->v.bt.debug_on_exit = doe; }
129 /* Helper functions to scan the backtrace. */
131 EXTERN_INLINE bool backtrace_p (struct specbinding *pdl)
132 { return pdl >= specpdl; }
134 EXTERN_INLINE struct specbinding *backtrace_top (void)
136 struct specbinding *pdl = specpdl_ptr - 1;
137 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
138 pdl--;
139 return pdl;
142 EXTERN_INLINE struct specbinding *backtrace_next (struct specbinding *pdl)
144 pdl--;
145 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
146 pdl--;
147 return pdl;
151 void
152 init_eval_once (void)
154 enum { size = 50 };
155 specpdl = xmalloc (size * sizeof *specpdl);
156 specpdl_size = size;
157 specpdl_ptr = specpdl;
158 /* Don't forget to update docs (lispref node "Local Variables"). */
159 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
160 max_lisp_eval_depth = 600;
162 Vrun_hooks = Qnil;
165 void
166 init_eval (void)
168 specpdl_ptr = specpdl;
169 catchlist = 0;
170 handlerlist = 0;
171 Vquit_flag = Qnil;
172 debug_on_next_call = 0;
173 lisp_eval_depth = 0;
174 #ifdef DEBUG_GCPRO
175 gcpro_level = 0;
176 #endif
177 /* This is less than the initial value of num_nonmacro_input_events. */
178 when_entered_debugger = -1;
181 #if (GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS \
182 || GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS)
183 void
184 mark_catchlist (struct catchtag *catch)
186 for (; catch; catch = catch->next)
188 mark_object (catch->tag);
189 mark_object (catch->val);
192 #endif
194 /* Unwind-protect function used by call_debugger. */
196 static Lisp_Object
197 restore_stack_limits (Lisp_Object data)
199 max_specpdl_size = XINT (XCAR (data));
200 max_lisp_eval_depth = XINT (XCDR (data));
201 return Qnil;
204 /* Call the Lisp debugger, giving it argument ARG. */
206 Lisp_Object
207 call_debugger (Lisp_Object arg)
209 bool debug_while_redisplaying;
210 ptrdiff_t count = SPECPDL_INDEX ();
211 Lisp_Object val;
212 EMACS_INT old_max = max_specpdl_size;
214 /* Temporarily bump up the stack limits,
215 so the debugger won't run out of stack. */
217 max_specpdl_size += 1;
218 record_unwind_protect (restore_stack_limits,
219 Fcons (make_number (old_max),
220 make_number (max_lisp_eval_depth)));
221 max_specpdl_size = old_max;
223 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
224 max_lisp_eval_depth = lisp_eval_depth + 40;
226 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
227 max_specpdl_size = SPECPDL_INDEX () + 100;
229 #ifdef HAVE_WINDOW_SYSTEM
230 if (display_hourglass_p)
231 cancel_hourglass ();
232 #endif
234 debug_on_next_call = 0;
235 when_entered_debugger = num_nonmacro_input_events;
237 /* Resetting redisplaying_p to 0 makes sure that debug output is
238 displayed if the debugger is invoked during redisplay. */
239 debug_while_redisplaying = redisplaying_p;
240 redisplaying_p = 0;
241 specbind (intern ("debugger-may-continue"),
242 debug_while_redisplaying ? Qnil : Qt);
243 specbind (Qinhibit_redisplay, Qnil);
244 specbind (Qinhibit_debugger, Qt);
246 #if 0 /* Binding this prevents execution of Lisp code during
247 redisplay, which necessarily leads to display problems. */
248 specbind (Qinhibit_eval_during_redisplay, Qt);
249 #endif
251 val = apply1 (Vdebugger, arg);
253 /* Interrupting redisplay and resuming it later is not safe under
254 all circumstances. So, when the debugger returns, abort the
255 interrupted redisplay by going back to the top-level. */
256 if (debug_while_redisplaying)
257 Ftop_level ();
259 return unbind_to (count, val);
262 static void
263 do_debug_on_call (Lisp_Object code)
265 debug_on_next_call = 0;
266 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
267 call_debugger (Fcons (code, Qnil));
270 /* NOTE!!! Every function that can call EVAL must protect its args
271 and temporaries from garbage collection while it needs them.
272 The definition of `For' shows what you have to do. */
274 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
275 doc: /* Eval args until one of them yields non-nil, then return that value.
276 The remaining args are not evalled at all.
277 If all args return nil, return nil.
278 usage: (or CONDITIONS...) */)
279 (Lisp_Object args)
281 register Lisp_Object val = Qnil;
282 struct gcpro gcpro1;
284 GCPRO1 (args);
286 while (CONSP (args))
288 val = eval_sub (XCAR (args));
289 if (!NILP (val))
290 break;
291 args = XCDR (args);
294 UNGCPRO;
295 return val;
298 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
299 doc: /* Eval args until one of them yields nil, then return nil.
300 The remaining args are not evalled at all.
301 If no arg yields nil, return the last arg's value.
302 usage: (and CONDITIONS...) */)
303 (Lisp_Object args)
305 register Lisp_Object val = Qt;
306 struct gcpro gcpro1;
308 GCPRO1 (args);
310 while (CONSP (args))
312 val = eval_sub (XCAR (args));
313 if (NILP (val))
314 break;
315 args = XCDR (args);
318 UNGCPRO;
319 return val;
322 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
323 doc: /* If COND yields non-nil, do THEN, else do ELSE...
324 Returns the value of THEN or the value of the last of the ELSE's.
325 THEN must be one expression, but ELSE... can be zero or more expressions.
326 If COND yields nil, and there are no ELSE's, the value is nil.
327 usage: (if COND THEN ELSE...) */)
328 (Lisp_Object args)
330 register Lisp_Object cond;
331 struct gcpro gcpro1;
333 GCPRO1 (args);
334 cond = eval_sub (Fcar (args));
335 UNGCPRO;
337 if (!NILP (cond))
338 return eval_sub (Fcar (Fcdr (args)));
339 return Fprogn (Fcdr (Fcdr (args)));
342 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
343 doc: /* Try each clause until one succeeds.
344 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
345 and, if the value is non-nil, this clause succeeds:
346 then the expressions in BODY are evaluated and the last one's
347 value is the value of the cond-form.
348 If no clause succeeds, cond returns nil.
349 If a clause has one element, as in (CONDITION),
350 CONDITION's value if non-nil is returned from the cond-form.
351 usage: (cond CLAUSES...) */)
352 (Lisp_Object args)
354 register Lisp_Object clause, val;
355 struct gcpro gcpro1;
357 val = Qnil;
358 GCPRO1 (args);
359 while (!NILP (args))
361 clause = Fcar (args);
362 val = eval_sub (Fcar (clause));
363 if (!NILP (val))
365 if (!EQ (XCDR (clause), Qnil))
366 val = Fprogn (XCDR (clause));
367 break;
369 args = XCDR (args);
371 UNGCPRO;
373 return val;
376 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
377 doc: /* Eval BODY forms sequentially and return value of last one.
378 usage: (progn BODY...) */)
379 (Lisp_Object args)
381 register Lisp_Object val = Qnil;
382 struct gcpro gcpro1;
384 GCPRO1 (args);
386 while (CONSP (args))
388 val = eval_sub (XCAR (args));
389 args = XCDR (args);
392 UNGCPRO;
393 return val;
396 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
397 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
398 The value of FIRST is saved during the evaluation of the remaining args,
399 whose values are discarded.
400 usage: (prog1 FIRST BODY...) */)
401 (Lisp_Object args)
403 Lisp_Object val;
404 register Lisp_Object args_left;
405 struct gcpro gcpro1, gcpro2;
407 args_left = args;
408 val = Qnil;
409 GCPRO2 (args, val);
411 val = eval_sub (XCAR (args_left));
412 while (CONSP (args_left = XCDR (args_left)))
413 eval_sub (XCAR (args_left));
415 UNGCPRO;
416 return val;
419 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
420 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
421 The value of FORM2 is saved during the evaluation of the
422 remaining args, whose values are discarded.
423 usage: (prog2 FORM1 FORM2 BODY...) */)
424 (Lisp_Object args)
426 struct gcpro gcpro1;
428 GCPRO1 (args);
429 eval_sub (XCAR (args));
430 UNGCPRO;
431 return Fprog1 (XCDR (args));
434 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
435 doc: /* Set each SYM to the value of its VAL.
436 The symbols SYM are variables; they are literal (not evaluated).
437 The values VAL are expressions; they are evaluated.
438 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
439 The second VAL is not computed until after the first SYM is set, and so on;
440 each VAL can use the new value of variables set earlier in the `setq'.
441 The return value of the `setq' form is the value of the last VAL.
442 usage: (setq [SYM VAL]...) */)
443 (Lisp_Object args)
445 register Lisp_Object args_left;
446 register Lisp_Object val, sym, lex_binding;
447 struct gcpro gcpro1;
449 if (NILP (args))
450 return Qnil;
452 args_left = args;
453 GCPRO1 (args);
457 val = eval_sub (Fcar (Fcdr (args_left)));
458 sym = Fcar (args_left);
460 /* Like for eval_sub, we do not check declared_special here since
461 it's been done when let-binding. */
462 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
463 && SYMBOLP (sym)
464 && !NILP (lex_binding
465 = Fassq (sym, Vinternal_interpreter_environment)))
466 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
467 else
468 Fset (sym, val); /* SYM is dynamically bound. */
470 args_left = Fcdr (Fcdr (args_left));
472 while (!NILP (args_left));
474 UNGCPRO;
475 return val;
478 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
479 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
480 Warning: `quote' does not construct its return value, but just returns
481 the value that was pre-constructed by the Lisp reader (see info node
482 `(elisp)Printed Representation').
483 This means that '(a . b) is not identical to (cons 'a 'b): the former
484 does not cons. Quoting should be reserved for constants that will
485 never be modified by side-effects, unless you like self-modifying code.
486 See the common pitfall in info node `(elisp)Rearrangement' for an example
487 of unexpected results when a quoted object is modified.
488 usage: (quote ARG) */)
489 (Lisp_Object args)
491 if (!NILP (Fcdr (args)))
492 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
493 return Fcar (args);
496 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
497 doc: /* Like `quote', but preferred for objects which are functions.
498 In byte compilation, `function' causes its argument to be compiled.
499 `quote' cannot do that.
500 usage: (function ARG) */)
501 (Lisp_Object args)
503 Lisp_Object quoted = XCAR (args);
505 if (!NILP (Fcdr (args)))
506 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
508 if (!NILP (Vinternal_interpreter_environment)
509 && CONSP (quoted)
510 && EQ (XCAR (quoted), Qlambda))
511 /* This is a lambda expression within a lexical environment;
512 return an interpreted closure instead of a simple lambda. */
513 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
514 XCDR (quoted)));
515 else
516 /* Simply quote the argument. */
517 return quoted;
521 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
522 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
523 Aliased variables always have the same value; setting one sets the other.
524 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
525 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
526 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
527 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
528 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
529 The return value is BASE-VARIABLE. */)
530 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
532 struct Lisp_Symbol *sym;
534 CHECK_SYMBOL (new_alias);
535 CHECK_SYMBOL (base_variable);
537 sym = XSYMBOL (new_alias);
539 if (sym->constant)
540 /* Not sure why, but why not? */
541 error ("Cannot make a constant an alias");
543 switch (sym->redirect)
545 case SYMBOL_FORWARDED:
546 error ("Cannot make an internal variable an alias");
547 case SYMBOL_LOCALIZED:
548 error ("Don't know how to make a localized variable an alias");
551 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
552 If n_a is bound, but b_v is not, set the value of b_v to n_a,
553 so that old-code that affects n_a before the aliasing is setup
554 still works. */
555 if (NILP (Fboundp (base_variable)))
556 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
559 struct specbinding *p;
561 for (p = specpdl_ptr; p > specpdl; )
562 if ((--p)->kind >= SPECPDL_LET
563 && (EQ (new_alias, specpdl_symbol (p))))
564 error ("Don't know how to make a let-bound variable an alias");
567 sym->declared_special = 1;
568 XSYMBOL (base_variable)->declared_special = 1;
569 sym->redirect = SYMBOL_VARALIAS;
570 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
571 sym->constant = SYMBOL_CONSTANT_P (base_variable);
572 LOADHIST_ATTACH (new_alias);
573 /* Even if docstring is nil: remove old docstring. */
574 Fput (new_alias, Qvariable_documentation, docstring);
576 return base_variable;
580 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
581 doc: /* Define SYMBOL as a variable, and return SYMBOL.
582 You are not required to define a variable in order to use it, but
583 defining it lets you supply an initial value and documentation, which
584 can be referred to by the Emacs help facilities and other programming
585 tools. The `defvar' form also declares the variable as \"special\",
586 so that it is always dynamically bound even if `lexical-binding' is t.
588 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
589 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
590 default value is what is set; buffer-local values are not affected.
591 If INITVALUE is missing, SYMBOL's value is not set.
593 If SYMBOL has a local binding, then this form affects the local
594 binding. This is usually not what you want. Thus, if you need to
595 load a file defining variables, with this form or with `defconst' or
596 `defcustom', you should always load that file _outside_ any bindings
597 for these variables. \(`defconst' and `defcustom' behave similarly in
598 this respect.)
600 The optional argument DOCSTRING is a documentation string for the
601 variable.
603 To define a user option, use `defcustom' instead of `defvar'.
604 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
605 (Lisp_Object args)
607 register Lisp_Object sym, tem, tail;
609 sym = Fcar (args);
610 tail = Fcdr (args);
611 if (!NILP (Fcdr (Fcdr (tail))))
612 error ("Too many arguments");
614 tem = Fdefault_boundp (sym);
615 if (!NILP (tail))
617 /* Do it before evaluating the initial value, for self-references. */
618 XSYMBOL (sym)->declared_special = 1;
620 if (NILP (tem))
621 Fset_default (sym, eval_sub (Fcar (tail)));
622 else
623 { /* Check if there is really a global binding rather than just a let
624 binding that shadows the global unboundness of the var. */
625 struct specbinding *pdl = specpdl_ptr;
626 while (pdl > specpdl)
628 if ((--pdl)->kind >= SPECPDL_LET
629 && EQ (specpdl_symbol (pdl), sym)
630 && EQ (specpdl_old_value (pdl), Qunbound))
632 message_with_string
633 ("Warning: defvar ignored because %s is let-bound",
634 SYMBOL_NAME (sym), 1);
635 break;
639 tail = Fcdr (tail);
640 tem = Fcar (tail);
641 if (!NILP (tem))
643 if (!NILP (Vpurify_flag))
644 tem = Fpurecopy (tem);
645 Fput (sym, Qvariable_documentation, tem);
647 LOADHIST_ATTACH (sym);
649 else if (!NILP (Vinternal_interpreter_environment)
650 && !XSYMBOL (sym)->declared_special)
651 /* A simple (defvar foo) with lexical scoping does "nothing" except
652 declare that var to be dynamically scoped *locally* (i.e. within
653 the current file or let-block). */
654 Vinternal_interpreter_environment
655 = Fcons (sym, Vinternal_interpreter_environment);
656 else
658 /* Simple (defvar <var>) should not count as a definition at all.
659 It could get in the way of other definitions, and unloading this
660 package could try to make the variable unbound. */
663 return sym;
666 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
667 doc: /* Define SYMBOL as a constant variable.
668 This declares that neither programs nor users should ever change the
669 value. This constancy is not actually enforced by Emacs Lisp, but
670 SYMBOL is marked as a special variable so that it is never lexically
671 bound.
673 The `defconst' form always sets the value of SYMBOL to the result of
674 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
675 what is set; buffer-local values are not affected. If SYMBOL has a
676 local binding, then this form sets the local binding's value.
677 However, you should normally not make local bindings for variables
678 defined with this form.
680 The optional DOCSTRING specifies the variable's documentation string.
681 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
682 (Lisp_Object args)
684 register Lisp_Object sym, tem;
686 sym = Fcar (args);
687 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
688 error ("Too many arguments");
690 tem = eval_sub (Fcar (Fcdr (args)));
691 if (!NILP (Vpurify_flag))
692 tem = Fpurecopy (tem);
693 Fset_default (sym, tem);
694 XSYMBOL (sym)->declared_special = 1;
695 tem = Fcar (Fcdr (Fcdr (args)));
696 if (!NILP (tem))
698 if (!NILP (Vpurify_flag))
699 tem = Fpurecopy (tem);
700 Fput (sym, Qvariable_documentation, tem);
702 Fput (sym, Qrisky_local_variable, Qt);
703 LOADHIST_ATTACH (sym);
704 return sym;
707 /* Make SYMBOL lexically scoped. */
708 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
709 Smake_var_non_special, 1, 1, 0,
710 doc: /* Internal function. */)
711 (Lisp_Object symbol)
713 CHECK_SYMBOL (symbol);
714 XSYMBOL (symbol)->declared_special = 0;
715 return Qnil;
719 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
720 doc: /* Bind variables according to VARLIST then eval BODY.
721 The value of the last form in BODY is returned.
722 Each element of VARLIST is a symbol (which is bound to nil)
723 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
724 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
725 usage: (let* VARLIST BODY...) */)
726 (Lisp_Object args)
728 Lisp_Object varlist, var, val, elt, lexenv;
729 ptrdiff_t count = SPECPDL_INDEX ();
730 struct gcpro gcpro1, gcpro2, gcpro3;
732 GCPRO3 (args, elt, varlist);
734 lexenv = Vinternal_interpreter_environment;
736 varlist = Fcar (args);
737 while (CONSP (varlist))
739 QUIT;
741 elt = XCAR (varlist);
742 if (SYMBOLP (elt))
744 var = elt;
745 val = Qnil;
747 else if (! NILP (Fcdr (Fcdr (elt))))
748 signal_error ("`let' bindings can have only one value-form", elt);
749 else
751 var = Fcar (elt);
752 val = eval_sub (Fcar (Fcdr (elt)));
755 if (!NILP (lexenv) && SYMBOLP (var)
756 && !XSYMBOL (var)->declared_special
757 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
758 /* Lexically bind VAR by adding it to the interpreter's binding
759 alist. */
761 Lisp_Object newenv
762 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
763 if (EQ (Vinternal_interpreter_environment, lexenv))
764 /* Save the old lexical environment on the specpdl stack,
765 but only for the first lexical binding, since we'll never
766 need to revert to one of the intermediate ones. */
767 specbind (Qinternal_interpreter_environment, newenv);
768 else
769 Vinternal_interpreter_environment = newenv;
771 else
772 specbind (var, val);
774 varlist = XCDR (varlist);
776 UNGCPRO;
777 val = Fprogn (Fcdr (args));
778 return unbind_to (count, val);
781 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
782 doc: /* Bind variables according to VARLIST then eval BODY.
783 The value of the last form in BODY is returned.
784 Each element of VARLIST is a symbol (which is bound to nil)
785 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
786 All the VALUEFORMs are evalled before any symbols are bound.
787 usage: (let VARLIST BODY...) */)
788 (Lisp_Object args)
790 Lisp_Object *temps, tem, lexenv;
791 register Lisp_Object elt, varlist;
792 ptrdiff_t count = SPECPDL_INDEX ();
793 ptrdiff_t argnum;
794 struct gcpro gcpro1, gcpro2;
795 USE_SAFE_ALLOCA;
797 varlist = Fcar (args);
799 /* Make space to hold the values to give the bound variables. */
800 elt = Flength (varlist);
801 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
803 /* Compute the values and store them in `temps'. */
805 GCPRO2 (args, *temps);
806 gcpro2.nvars = 0;
808 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
810 QUIT;
811 elt = XCAR (varlist);
812 if (SYMBOLP (elt))
813 temps [argnum++] = Qnil;
814 else if (! NILP (Fcdr (Fcdr (elt))))
815 signal_error ("`let' bindings can have only one value-form", elt);
816 else
817 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
818 gcpro2.nvars = argnum;
820 UNGCPRO;
822 lexenv = Vinternal_interpreter_environment;
824 varlist = Fcar (args);
825 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
827 Lisp_Object var;
829 elt = XCAR (varlist);
830 var = SYMBOLP (elt) ? elt : Fcar (elt);
831 tem = temps[argnum++];
833 if (!NILP (lexenv) && SYMBOLP (var)
834 && !XSYMBOL (var)->declared_special
835 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
836 /* Lexically bind VAR by adding it to the lexenv alist. */
837 lexenv = Fcons (Fcons (var, tem), lexenv);
838 else
839 /* Dynamically bind VAR. */
840 specbind (var, tem);
843 if (!EQ (lexenv, Vinternal_interpreter_environment))
844 /* Instantiate a new lexical environment. */
845 specbind (Qinternal_interpreter_environment, lexenv);
847 elt = Fprogn (Fcdr (args));
848 SAFE_FREE ();
849 return unbind_to (count, elt);
852 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
853 doc: /* If TEST yields non-nil, eval BODY... and repeat.
854 The order of execution is thus TEST, BODY, TEST, BODY and so on
855 until TEST returns nil.
856 usage: (while TEST BODY...) */)
857 (Lisp_Object args)
859 Lisp_Object test, body;
860 struct gcpro gcpro1, gcpro2;
862 GCPRO2 (test, body);
864 test = Fcar (args);
865 body = Fcdr (args);
866 while (!NILP (eval_sub (test)))
868 QUIT;
869 Fprogn (body);
872 UNGCPRO;
873 return Qnil;
876 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
877 doc: /* Return result of expanding macros at top level of FORM.
878 If FORM is not a macro call, it is returned unchanged.
879 Otherwise, the macro is expanded and the expansion is considered
880 in place of FORM. When a non-macro-call results, it is returned.
882 The second optional arg ENVIRONMENT specifies an environment of macro
883 definitions to shadow the loaded ones for use in file byte-compilation. */)
884 (Lisp_Object form, Lisp_Object environment)
886 /* With cleanups from Hallvard Furuseth. */
887 register Lisp_Object expander, sym, def, tem;
889 while (1)
891 /* Come back here each time we expand a macro call,
892 in case it expands into another macro call. */
893 if (!CONSP (form))
894 break;
895 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
896 def = sym = XCAR (form);
897 tem = Qnil;
898 /* Trace symbols aliases to other symbols
899 until we get a symbol that is not an alias. */
900 while (SYMBOLP (def))
902 QUIT;
903 sym = def;
904 tem = Fassq (sym, environment);
905 if (NILP (tem))
907 def = XSYMBOL (sym)->function;
908 if (!NILP (def))
909 continue;
911 break;
913 /* Right now TEM is the result from SYM in ENVIRONMENT,
914 and if TEM is nil then DEF is SYM's function definition. */
915 if (NILP (tem))
917 /* SYM is not mentioned in ENVIRONMENT.
918 Look at its function definition. */
919 struct gcpro gcpro1;
920 GCPRO1 (form);
921 def = Fautoload_do_load (def, sym, Qmacro);
922 UNGCPRO;
923 if (!CONSP (def))
924 /* Not defined or definition not suitable. */
925 break;
926 if (!EQ (XCAR (def), Qmacro))
927 break;
928 else expander = XCDR (def);
930 else
932 expander = XCDR (tem);
933 if (NILP (expander))
934 break;
937 Lisp_Object newform = apply1 (expander, XCDR (form));
938 if (EQ (form, newform))
939 break;
940 else
941 form = newform;
944 return form;
947 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
948 doc: /* Eval BODY allowing nonlocal exits using `throw'.
949 TAG is evalled to get the tag to use; it must not be nil.
951 Then the BODY is executed.
952 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
953 If no throw happens, `catch' returns the value of the last BODY form.
954 If a throw happens, it specifies the value to return from `catch'.
955 usage: (catch TAG BODY...) */)
956 (Lisp_Object args)
958 register Lisp_Object tag;
959 struct gcpro gcpro1;
961 GCPRO1 (args);
962 tag = eval_sub (Fcar (args));
963 UNGCPRO;
964 return internal_catch (tag, Fprogn, Fcdr (args));
967 /* Set up a catch, then call C function FUNC on argument ARG.
968 FUNC should return a Lisp_Object.
969 This is how catches are done from within C code. */
971 Lisp_Object
972 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
974 /* This structure is made part of the chain `catchlist'. */
975 struct catchtag c;
977 /* Fill in the components of c, and put it on the list. */
978 c.next = catchlist;
979 c.tag = tag;
980 c.val = Qnil;
981 c.f_handlerlist = handlerlist;
982 c.f_lisp_eval_depth = lisp_eval_depth;
983 c.pdlcount = SPECPDL_INDEX ();
984 c.poll_suppress_count = poll_suppress_count;
985 c.interrupt_input_blocked = interrupt_input_blocked;
986 c.gcpro = gcprolist;
987 c.byte_stack = byte_stack_list;
988 catchlist = &c;
990 /* Call FUNC. */
991 if (! sys_setjmp (c.jmp))
992 c.val = (*func) (arg);
994 /* Throw works by a longjmp that comes right here. */
995 catchlist = c.next;
996 return c.val;
999 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1000 jump to that CATCH, returning VALUE as the value of that catch.
1002 This is the guts of Fthrow and Fsignal; they differ only in the way
1003 they choose the catch tag to throw to. A catch tag for a
1004 condition-case form has a TAG of Qnil.
1006 Before each catch is discarded, unbind all special bindings and
1007 execute all unwind-protect clauses made above that catch. Unwind
1008 the handler stack as we go, so that the proper handlers are in
1009 effect for each unwind-protect clause we run. At the end, restore
1010 some static info saved in CATCH, and longjmp to the location
1011 specified there.
1013 This is used for correct unwinding in Fthrow and Fsignal. */
1015 static _Noreturn void
1016 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1018 bool last_time;
1020 /* Save the value in the tag. */
1021 catch->val = value;
1023 /* Restore certain special C variables. */
1024 set_poll_suppress_count (catch->poll_suppress_count);
1025 unblock_input_to (catch->interrupt_input_blocked);
1026 immediate_quit = 0;
1030 last_time = catchlist == catch;
1032 /* Unwind the specpdl stack, and then restore the proper set of
1033 handlers. */
1034 unbind_to (catchlist->pdlcount, Qnil);
1035 handlerlist = catchlist->f_handlerlist;
1036 catchlist = catchlist->next;
1038 while (! last_time);
1040 byte_stack_list = catch->byte_stack;
1041 gcprolist = catch->gcpro;
1042 #ifdef DEBUG_GCPRO
1043 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1044 #endif
1045 lisp_eval_depth = catch->f_lisp_eval_depth;
1047 sys_longjmp (catch->jmp, 1);
1050 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1051 doc: /* Throw to the catch for TAG and return VALUE from it.
1052 Both TAG and VALUE are evalled. */)
1053 (register Lisp_Object tag, Lisp_Object value)
1055 register struct catchtag *c;
1057 if (!NILP (tag))
1058 for (c = catchlist; c; c = c->next)
1060 if (EQ (c->tag, tag))
1061 unwind_to_catch (c, value);
1063 xsignal2 (Qno_catch, tag, value);
1067 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1068 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1069 If BODYFORM completes normally, its value is returned
1070 after executing the UNWINDFORMS.
1071 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1072 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1073 (Lisp_Object args)
1075 Lisp_Object val;
1076 ptrdiff_t count = SPECPDL_INDEX ();
1078 record_unwind_protect (Fprogn, Fcdr (args));
1079 val = eval_sub (Fcar (args));
1080 return unbind_to (count, val);
1083 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1084 doc: /* Regain control when an error is signaled.
1085 Executes BODYFORM and returns its value if no error happens.
1086 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1087 where the BODY is made of Lisp expressions.
1089 A handler is applicable to an error
1090 if CONDITION-NAME is one of the error's condition names.
1091 If an error happens, the first applicable handler is run.
1093 The car of a handler may be a list of condition names instead of a
1094 single condition name; then it handles all of them. If the special
1095 condition name `debug' is present in this list, it allows another
1096 condition in the list to run the debugger if `debug-on-error' and the
1097 other usual mechanisms says it should (otherwise, `condition-case'
1098 suppresses the debugger).
1100 When a handler handles an error, control returns to the `condition-case'
1101 and it executes the handler's BODY...
1102 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1103 \(If VAR is nil, the handler can't access that information.)
1104 Then the value of the last BODY form is returned from the `condition-case'
1105 expression.
1107 See also the function `signal' for more info.
1108 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1109 (Lisp_Object args)
1111 Lisp_Object var = Fcar (args);
1112 Lisp_Object bodyform = Fcar (Fcdr (args));
1113 Lisp_Object handlers = Fcdr (Fcdr (args));
1115 return internal_lisp_condition_case (var, bodyform, handlers);
1118 /* Like Fcondition_case, but the args are separate
1119 rather than passed in a list. Used by Fbyte_code. */
1121 Lisp_Object
1122 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1123 Lisp_Object handlers)
1125 Lisp_Object val;
1126 struct catchtag c;
1127 struct handler h;
1129 CHECK_SYMBOL (var);
1131 for (val = handlers; CONSP (val); val = XCDR (val))
1133 Lisp_Object tem;
1134 tem = XCAR (val);
1135 if (! (NILP (tem)
1136 || (CONSP (tem)
1137 && (SYMBOLP (XCAR (tem))
1138 || CONSP (XCAR (tem))))))
1139 error ("Invalid condition handler: %s",
1140 SDATA (Fprin1_to_string (tem, Qt)));
1143 c.tag = Qnil;
1144 c.val = Qnil;
1145 c.f_handlerlist = handlerlist;
1146 c.f_lisp_eval_depth = lisp_eval_depth;
1147 c.pdlcount = SPECPDL_INDEX ();
1148 c.poll_suppress_count = poll_suppress_count;
1149 c.interrupt_input_blocked = interrupt_input_blocked;
1150 c.gcpro = gcprolist;
1151 c.byte_stack = byte_stack_list;
1152 if (sys_setjmp (c.jmp))
1154 if (!NILP (h.var))
1155 specbind (h.var, c.val);
1156 val = Fprogn (Fcdr (h.chosen_clause));
1158 /* Note that this just undoes the binding of h.var; whoever
1159 longjumped to us unwound the stack to c.pdlcount before
1160 throwing. */
1161 unbind_to (c.pdlcount, Qnil);
1162 return val;
1164 c.next = catchlist;
1165 catchlist = &c;
1167 h.var = var;
1168 h.handler = handlers;
1169 h.next = handlerlist;
1170 h.tag = &c;
1171 handlerlist = &h;
1173 val = eval_sub (bodyform);
1174 catchlist = c.next;
1175 handlerlist = h.next;
1176 return val;
1179 /* Call the function BFUN with no arguments, catching errors within it
1180 according to HANDLERS. If there is an error, call HFUN with
1181 one argument which is the data that describes the error:
1182 (SIGNALNAME . DATA)
1184 HANDLERS can be a list of conditions to catch.
1185 If HANDLERS is Qt, catch all errors.
1186 If HANDLERS is Qerror, catch all errors
1187 but allow the debugger to run if that is enabled. */
1189 Lisp_Object
1190 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1191 Lisp_Object (*hfun) (Lisp_Object))
1193 Lisp_Object val;
1194 struct catchtag c;
1195 struct handler h;
1197 c.tag = Qnil;
1198 c.val = Qnil;
1199 c.f_handlerlist = handlerlist;
1200 c.f_lisp_eval_depth = lisp_eval_depth;
1201 c.pdlcount = SPECPDL_INDEX ();
1202 c.poll_suppress_count = poll_suppress_count;
1203 c.interrupt_input_blocked = interrupt_input_blocked;
1204 c.gcpro = gcprolist;
1205 c.byte_stack = byte_stack_list;
1206 if (sys_setjmp (c.jmp))
1208 return (*hfun) (c.val);
1210 c.next = catchlist;
1211 catchlist = &c;
1212 h.handler = handlers;
1213 h.var = Qnil;
1214 h.next = handlerlist;
1215 h.tag = &c;
1216 handlerlist = &h;
1218 val = (*bfun) ();
1219 catchlist = c.next;
1220 handlerlist = h.next;
1221 return val;
1224 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1226 Lisp_Object
1227 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1228 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1230 Lisp_Object val;
1231 struct catchtag c;
1232 struct handler h;
1234 c.tag = Qnil;
1235 c.val = Qnil;
1236 c.f_handlerlist = handlerlist;
1237 c.f_lisp_eval_depth = lisp_eval_depth;
1238 c.pdlcount = SPECPDL_INDEX ();
1239 c.poll_suppress_count = poll_suppress_count;
1240 c.interrupt_input_blocked = interrupt_input_blocked;
1241 c.gcpro = gcprolist;
1242 c.byte_stack = byte_stack_list;
1243 if (sys_setjmp (c.jmp))
1245 return (*hfun) (c.val);
1247 c.next = catchlist;
1248 catchlist = &c;
1249 h.handler = handlers;
1250 h.var = Qnil;
1251 h.next = handlerlist;
1252 h.tag = &c;
1253 handlerlist = &h;
1255 val = (*bfun) (arg);
1256 catchlist = c.next;
1257 handlerlist = h.next;
1258 return val;
1261 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1262 its arguments. */
1264 Lisp_Object
1265 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1266 Lisp_Object arg1,
1267 Lisp_Object arg2,
1268 Lisp_Object handlers,
1269 Lisp_Object (*hfun) (Lisp_Object))
1271 Lisp_Object val;
1272 struct catchtag c;
1273 struct handler h;
1275 c.tag = Qnil;
1276 c.val = Qnil;
1277 c.f_handlerlist = handlerlist;
1278 c.f_lisp_eval_depth = lisp_eval_depth;
1279 c.pdlcount = SPECPDL_INDEX ();
1280 c.poll_suppress_count = poll_suppress_count;
1281 c.interrupt_input_blocked = interrupt_input_blocked;
1282 c.gcpro = gcprolist;
1283 c.byte_stack = byte_stack_list;
1284 if (sys_setjmp (c.jmp))
1286 return (*hfun) (c.val);
1288 c.next = catchlist;
1289 catchlist = &c;
1290 h.handler = handlers;
1291 h.var = Qnil;
1292 h.next = handlerlist;
1293 h.tag = &c;
1294 handlerlist = &h;
1296 val = (*bfun) (arg1, arg2);
1297 catchlist = c.next;
1298 handlerlist = h.next;
1299 return val;
1302 /* Like internal_condition_case but call BFUN with NARGS as first,
1303 and ARGS as second argument. */
1305 Lisp_Object
1306 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1307 ptrdiff_t nargs,
1308 Lisp_Object *args,
1309 Lisp_Object handlers,
1310 Lisp_Object (*hfun) (Lisp_Object err,
1311 ptrdiff_t nargs,
1312 Lisp_Object *args))
1314 Lisp_Object val;
1315 struct catchtag c;
1316 struct handler h;
1318 c.tag = Qnil;
1319 c.val = Qnil;
1320 c.f_handlerlist = handlerlist;
1321 c.f_lisp_eval_depth = lisp_eval_depth;
1322 c.pdlcount = SPECPDL_INDEX ();
1323 c.poll_suppress_count = poll_suppress_count;
1324 c.interrupt_input_blocked = interrupt_input_blocked;
1325 c.gcpro = gcprolist;
1326 c.byte_stack = byte_stack_list;
1327 if (sys_setjmp (c.jmp))
1329 return (*hfun) (c.val, nargs, args);
1331 c.next = catchlist;
1332 catchlist = &c;
1333 h.handler = handlers;
1334 h.var = Qnil;
1335 h.next = handlerlist;
1336 h.tag = &c;
1337 handlerlist = &h;
1339 val = (*bfun) (nargs, args);
1340 catchlist = c.next;
1341 handlerlist = h.next;
1342 return val;
1346 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1347 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1348 Lisp_Object data);
1350 void
1351 process_quit_flag (void)
1353 Lisp_Object flag = Vquit_flag;
1354 Vquit_flag = Qnil;
1355 if (EQ (flag, Qkill_emacs))
1356 Fkill_emacs (Qnil);
1357 if (EQ (Vthrow_on_input, flag))
1358 Fthrow (Vthrow_on_input, Qt);
1359 Fsignal (Qquit, Qnil);
1362 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1363 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1364 This function does not return.
1366 An error symbol is a symbol with an `error-conditions' property
1367 that is a list of condition names.
1368 A handler for any of those names will get to handle this signal.
1369 The symbol `error' should normally be one of them.
1371 DATA should be a list. Its elements are printed as part of the error message.
1372 See Info anchor `(elisp)Definition of signal' for some details on how this
1373 error message is constructed.
1374 If the signal is handled, DATA is made available to the handler.
1375 See also the function `condition-case'. */)
1376 (Lisp_Object error_symbol, Lisp_Object data)
1378 /* When memory is full, ERROR-SYMBOL is nil,
1379 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1380 That is a special case--don't do this in other situations. */
1381 Lisp_Object conditions;
1382 Lisp_Object string;
1383 Lisp_Object real_error_symbol
1384 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1385 register Lisp_Object clause = Qnil;
1386 struct handler *h;
1388 immediate_quit = 0;
1389 abort_on_gc = 0;
1390 if (gc_in_progress || waiting_for_input)
1391 emacs_abort ();
1393 #if 0 /* rms: I don't know why this was here,
1394 but it is surely wrong for an error that is handled. */
1395 #ifdef HAVE_WINDOW_SYSTEM
1396 if (display_hourglass_p)
1397 cancel_hourglass ();
1398 #endif
1399 #endif
1401 /* This hook is used by edebug. */
1402 if (! NILP (Vsignal_hook_function)
1403 && ! NILP (error_symbol))
1405 /* Edebug takes care of restoring these variables when it exits. */
1406 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1407 max_lisp_eval_depth = lisp_eval_depth + 20;
1409 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1410 max_specpdl_size = SPECPDL_INDEX () + 40;
1412 call2 (Vsignal_hook_function, error_symbol, data);
1415 conditions = Fget (real_error_symbol, Qerror_conditions);
1417 /* Remember from where signal was called. Skip over the frame for
1418 `signal' itself. If a frame for `error' follows, skip that,
1419 too. Don't do this when ERROR_SYMBOL is nil, because that
1420 is a memory-full error. */
1421 Vsignaling_function = Qnil;
1422 if (!NILP (error_symbol))
1424 struct specbinding *pdl = backtrace_next (backtrace_top ());
1425 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1426 pdl = backtrace_next (pdl);
1427 if (backtrace_p (pdl))
1428 Vsignaling_function = backtrace_function (pdl);
1431 for (h = handlerlist; h; h = h->next)
1433 clause = find_handler_clause (h->handler, conditions);
1434 if (!NILP (clause))
1435 break;
1438 if (/* Don't run the debugger for a memory-full error.
1439 (There is no room in memory to do that!) */
1440 !NILP (error_symbol)
1441 && (!NILP (Vdebug_on_signal)
1442 /* If no handler is present now, try to run the debugger. */
1443 || NILP (clause)
1444 /* A `debug' symbol in the handler list disables the normal
1445 suppression of the debugger. */
1446 || (CONSP (clause) && CONSP (XCAR (clause))
1447 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1448 /* Special handler that means "print a message and run debugger
1449 if requested". */
1450 || EQ (h->handler, Qerror)))
1452 bool debugger_called
1453 = maybe_call_debugger (conditions, error_symbol, data);
1454 /* We can't return values to code which signaled an error, but we
1455 can continue code which has signaled a quit. */
1456 if (debugger_called && EQ (real_error_symbol, Qquit))
1457 return Qnil;
1460 if (!NILP (clause))
1462 Lisp_Object unwind_data
1463 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1465 h->chosen_clause = clause;
1466 unwind_to_catch (h->tag, unwind_data);
1468 else
1470 if (catchlist != 0)
1471 Fthrow (Qtop_level, Qt);
1474 if (! NILP (error_symbol))
1475 data = Fcons (error_symbol, data);
1477 string = Ferror_message_string (data);
1478 fatal ("%s", SDATA (string));
1481 /* Internal version of Fsignal that never returns.
1482 Used for anything but Qquit (which can return from Fsignal). */
1484 void
1485 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1487 Fsignal (error_symbol, data);
1488 emacs_abort ();
1491 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1493 void
1494 xsignal0 (Lisp_Object error_symbol)
1496 xsignal (error_symbol, Qnil);
1499 void
1500 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1502 xsignal (error_symbol, list1 (arg));
1505 void
1506 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1508 xsignal (error_symbol, list2 (arg1, arg2));
1511 void
1512 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1514 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1517 /* Signal `error' with message S, and additional arg ARG.
1518 If ARG is not a genuine list, make it a one-element list. */
1520 void
1521 signal_error (const char *s, Lisp_Object arg)
1523 Lisp_Object tortoise, hare;
1525 hare = tortoise = arg;
1526 while (CONSP (hare))
1528 hare = XCDR (hare);
1529 if (!CONSP (hare))
1530 break;
1532 hare = XCDR (hare);
1533 tortoise = XCDR (tortoise);
1535 if (EQ (hare, tortoise))
1536 break;
1539 if (!NILP (hare))
1540 arg = Fcons (arg, Qnil); /* Make it a list. */
1542 xsignal (Qerror, Fcons (build_string (s), arg));
1546 /* Return true if LIST is a non-nil atom or
1547 a list containing one of CONDITIONS. */
1549 static bool
1550 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1552 if (NILP (list))
1553 return 0;
1554 if (! CONSP (list))
1555 return 1;
1557 while (CONSP (conditions))
1559 Lisp_Object this, tail;
1560 this = XCAR (conditions);
1561 for (tail = list; CONSP (tail); tail = XCDR (tail))
1562 if (EQ (XCAR (tail), this))
1563 return 1;
1564 conditions = XCDR (conditions);
1566 return 0;
1569 /* Return true if an error with condition-symbols CONDITIONS,
1570 and described by SIGNAL-DATA, should skip the debugger
1571 according to debugger-ignored-errors. */
1573 static bool
1574 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1576 Lisp_Object tail;
1577 bool first_string = 1;
1578 Lisp_Object error_message;
1580 error_message = Qnil;
1581 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1583 if (STRINGP (XCAR (tail)))
1585 if (first_string)
1587 error_message = Ferror_message_string (data);
1588 first_string = 0;
1591 if (fast_string_match (XCAR (tail), error_message) >= 0)
1592 return 1;
1594 else
1596 Lisp_Object contail;
1598 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1599 if (EQ (XCAR (tail), XCAR (contail)))
1600 return 1;
1604 return 0;
1607 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1608 SIG and DATA describe the signal. There are two ways to pass them:
1609 = SIG is the error symbol, and DATA is the rest of the data.
1610 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1611 This is for memory-full errors only. */
1612 static bool
1613 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1615 Lisp_Object combined_data;
1617 combined_data = Fcons (sig, data);
1619 if (
1620 /* Don't try to run the debugger with interrupts blocked.
1621 The editing loop would return anyway. */
1622 ! input_blocked_p ()
1623 && NILP (Vinhibit_debugger)
1624 /* Does user want to enter debugger for this kind of error? */
1625 && (EQ (sig, Qquit)
1626 ? debug_on_quit
1627 : wants_debugger (Vdebug_on_error, conditions))
1628 && ! skip_debugger (conditions, combined_data)
1629 /* RMS: What's this for? */
1630 && when_entered_debugger < num_nonmacro_input_events)
1632 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1633 return 1;
1636 return 0;
1639 static Lisp_Object
1640 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1642 register Lisp_Object h;
1644 /* t is used by handlers for all conditions, set up by C code. */
1645 if (EQ (handlers, Qt))
1646 return Qt;
1648 /* error is used similarly, but means print an error message
1649 and run the debugger if that is enabled. */
1650 if (EQ (handlers, Qerror))
1651 return Qt;
1653 for (h = handlers; CONSP (h); h = XCDR (h))
1655 Lisp_Object handler = XCAR (h);
1656 Lisp_Object condit, tem;
1658 if (!CONSP (handler))
1659 continue;
1660 condit = XCAR (handler);
1661 /* Handle a single condition name in handler HANDLER. */
1662 if (SYMBOLP (condit))
1664 tem = Fmemq (Fcar (handler), conditions);
1665 if (!NILP (tem))
1666 return handler;
1668 /* Handle a list of condition names in handler HANDLER. */
1669 else if (CONSP (condit))
1671 Lisp_Object tail;
1672 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1674 tem = Fmemq (XCAR (tail), conditions);
1675 if (!NILP (tem))
1676 return handler;
1681 return Qnil;
1685 /* Dump an error message; called like vprintf. */
1686 void
1687 verror (const char *m, va_list ap)
1689 char buf[4000];
1690 ptrdiff_t size = sizeof buf;
1691 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1692 char *buffer = buf;
1693 ptrdiff_t used;
1694 Lisp_Object string;
1696 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1697 string = make_string (buffer, used);
1698 if (buffer != buf)
1699 xfree (buffer);
1701 xsignal1 (Qerror, string);
1705 /* Dump an error message; called like printf. */
1707 /* VARARGS 1 */
1708 void
1709 error (const char *m, ...)
1711 va_list ap;
1712 va_start (ap, m);
1713 verror (m, ap);
1716 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1717 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1718 This means it contains a description for how to read arguments to give it.
1719 The value is nil for an invalid function or a symbol with no function
1720 definition.
1722 Interactively callable functions include strings and vectors (treated
1723 as keyboard macros), lambda-expressions that contain a top-level call
1724 to `interactive', autoload definitions made by `autoload' with non-nil
1725 fourth argument, and some of the built-in functions of Lisp.
1727 Also, a symbol satisfies `commandp' if its function definition does so.
1729 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1730 then strings and vectors are not accepted. */)
1731 (Lisp_Object function, Lisp_Object for_call_interactively)
1733 register Lisp_Object fun;
1734 register Lisp_Object funcar;
1735 Lisp_Object if_prop = Qnil;
1737 fun = function;
1739 fun = indirect_function (fun); /* Check cycles. */
1740 if (NILP (fun))
1741 return Qnil;
1743 /* Check an `interactive-form' property if present, analogous to the
1744 function-documentation property. */
1745 fun = function;
1746 while (SYMBOLP (fun))
1748 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1749 if (!NILP (tmp))
1750 if_prop = Qt;
1751 fun = Fsymbol_function (fun);
1754 /* Emacs primitives are interactive if their DEFUN specifies an
1755 interactive spec. */
1756 if (SUBRP (fun))
1757 return XSUBR (fun)->intspec ? Qt : if_prop;
1759 /* Bytecode objects are interactive if they are long enough to
1760 have an element whose index is COMPILED_INTERACTIVE, which is
1761 where the interactive spec is stored. */
1762 else if (COMPILEDP (fun))
1763 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1764 ? Qt : if_prop);
1766 /* Strings and vectors are keyboard macros. */
1767 if (STRINGP (fun) || VECTORP (fun))
1768 return (NILP (for_call_interactively) ? Qt : Qnil);
1770 /* Lists may represent commands. */
1771 if (!CONSP (fun))
1772 return Qnil;
1773 funcar = XCAR (fun);
1774 if (EQ (funcar, Qclosure))
1775 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1776 ? Qt : if_prop);
1777 else if (EQ (funcar, Qlambda))
1778 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1779 else if (EQ (funcar, Qautoload))
1780 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1781 else
1782 return Qnil;
1785 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1786 doc: /* Define FUNCTION to autoload from FILE.
1787 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1788 Third arg DOCSTRING is documentation for the function.
1789 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1790 Fifth arg TYPE indicates the type of the object:
1791 nil or omitted says FUNCTION is a function,
1792 `keymap' says FUNCTION is really a keymap, and
1793 `macro' or t says FUNCTION is really a macro.
1794 Third through fifth args give info about the real definition.
1795 They default to nil.
1796 If FUNCTION is already defined other than as an autoload,
1797 this does nothing and returns nil. */)
1798 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1800 CHECK_SYMBOL (function);
1801 CHECK_STRING (file);
1803 /* If function is defined and not as an autoload, don't override. */
1804 if (!NILP (XSYMBOL (function)->function)
1805 && !AUTOLOADP (XSYMBOL (function)->function))
1806 return Qnil;
1808 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1809 /* `read1' in lread.c has found the docstring starting with "\
1810 and assumed the docstring will be provided by Snarf-documentation, so it
1811 passed us 0 instead. But that leads to accidental sharing in purecopy's
1812 hash-consing, so we use a (hopefully) unique integer instead. */
1813 docstring = make_number (XHASH (function));
1814 return Fdefalias (function,
1815 list5 (Qautoload, file, docstring, interactive, type),
1816 Qnil);
1819 Lisp_Object
1820 un_autoload (Lisp_Object oldqueue)
1822 register Lisp_Object queue, first, second;
1824 /* Queue to unwind is current value of Vautoload_queue.
1825 oldqueue is the shadowed value to leave in Vautoload_queue. */
1826 queue = Vautoload_queue;
1827 Vautoload_queue = oldqueue;
1828 while (CONSP (queue))
1830 first = XCAR (queue);
1831 second = Fcdr (first);
1832 first = Fcar (first);
1833 if (EQ (first, make_number (0)))
1834 Vfeatures = second;
1835 else
1836 Ffset (first, second);
1837 queue = XCDR (queue);
1839 return Qnil;
1842 /* Load an autoloaded function.
1843 FUNNAME is the symbol which is the function's name.
1844 FUNDEF is the autoload definition (a list). */
1846 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1847 doc: /* Load FUNDEF which should be an autoload.
1848 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1849 in which case the function returns the new autoloaded function value.
1850 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1851 it is defines a macro. */)
1852 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1854 ptrdiff_t count = SPECPDL_INDEX ();
1855 struct gcpro gcpro1, gcpro2, gcpro3;
1857 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1858 return fundef;
1860 if (EQ (macro_only, Qmacro))
1862 Lisp_Object kind = Fnth (make_number (4), fundef);
1863 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1864 return fundef;
1867 /* This is to make sure that loadup.el gives a clear picture
1868 of what files are preloaded and when. */
1869 if (! NILP (Vpurify_flag))
1870 error ("Attempt to autoload %s while preparing to dump",
1871 SDATA (SYMBOL_NAME (funname)));
1873 CHECK_SYMBOL (funname);
1874 GCPRO3 (funname, fundef, macro_only);
1876 /* Preserve the match data. */
1877 record_unwind_save_match_data ();
1879 /* If autoloading gets an error (which includes the error of failing
1880 to define the function being called), we use Vautoload_queue
1881 to undo function definitions and `provide' calls made by
1882 the function. We do this in the specific case of autoloading
1883 because autoloading is not an explicit request "load this file",
1884 but rather a request to "call this function".
1886 The value saved here is to be restored into Vautoload_queue. */
1887 record_unwind_protect (un_autoload, Vautoload_queue);
1888 Vautoload_queue = Qt;
1889 /* If `macro_only', assume this autoload to be a "best-effort",
1890 so don't signal an error if autoloading fails. */
1891 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1893 /* Once loading finishes, don't undo it. */
1894 Vautoload_queue = Qt;
1895 unbind_to (count, Qnil);
1897 UNGCPRO;
1899 if (NILP (funname))
1900 return Qnil;
1901 else
1903 Lisp_Object fun = Findirect_function (funname, Qnil);
1905 if (!NILP (Fequal (fun, fundef)))
1906 error ("Autoloading failed to define function %s",
1907 SDATA (SYMBOL_NAME (funname)));
1908 else
1909 return fun;
1914 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1915 doc: /* Evaluate FORM and return its value.
1916 If LEXICAL is t, evaluate using lexical scoping. */)
1917 (Lisp_Object form, Lisp_Object lexical)
1919 ptrdiff_t count = SPECPDL_INDEX ();
1920 specbind (Qinternal_interpreter_environment,
1921 CONSP (lexical) || NILP (lexical) ? lexical : Fcons (Qt, Qnil));
1922 return unbind_to (count, eval_sub (form));
1925 static void
1926 grow_specpdl (void)
1928 register ptrdiff_t count = SPECPDL_INDEX ();
1929 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX);
1930 if (max_size <= specpdl_size)
1932 if (max_specpdl_size < 400)
1933 max_size = max_specpdl_size = 400;
1934 if (max_size <= specpdl_size)
1935 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
1937 specpdl = xpalloc (specpdl, &specpdl_size, 1, max_size, sizeof *specpdl);
1938 specpdl_ptr = specpdl + count;
1941 LISP_INLINE void
1942 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
1944 eassert (nargs >= UNEVALLED);
1945 if (specpdl_ptr == specpdl + specpdl_size)
1946 grow_specpdl ();
1947 specpdl_ptr->kind = SPECPDL_BACKTRACE;
1948 specpdl_ptr->v.bt.function = function;
1949 specpdl_ptr->v.bt.args = args;
1950 specpdl_ptr->v.bt.nargs = nargs;
1951 specpdl_ptr->v.bt.debug_on_exit = false;
1952 specpdl_ptr++;
1955 /* Eval a sub-expression of the current expression (i.e. in the same
1956 lexical scope). */
1957 Lisp_Object
1958 eval_sub (Lisp_Object form)
1960 Lisp_Object fun, val, original_fun, original_args;
1961 Lisp_Object funcar;
1962 struct gcpro gcpro1, gcpro2, gcpro3;
1964 if (SYMBOLP (form))
1966 /* Look up its binding in the lexical environment.
1967 We do not pay attention to the declared_special flag here, since we
1968 already did that when let-binding the variable. */
1969 Lisp_Object lex_binding
1970 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
1971 ? Fassq (form, Vinternal_interpreter_environment)
1972 : Qnil;
1973 if (CONSP (lex_binding))
1974 return XCDR (lex_binding);
1975 else
1976 return Fsymbol_value (form);
1979 if (!CONSP (form))
1980 return form;
1982 QUIT;
1984 GCPRO1 (form);
1985 maybe_gc ();
1986 UNGCPRO;
1988 if (++lisp_eval_depth > max_lisp_eval_depth)
1990 if (max_lisp_eval_depth < 100)
1991 max_lisp_eval_depth = 100;
1992 if (lisp_eval_depth > max_lisp_eval_depth)
1993 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
1996 original_fun = XCAR (form);
1997 original_args = XCDR (form);
1999 /* This also protects them from gc. */
2000 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2002 if (debug_on_next_call)
2003 do_debug_on_call (Qt);
2005 /* At this point, only original_fun and original_args
2006 have values that will be used below. */
2007 retry:
2009 /* Optimize for no indirection. */
2010 fun = original_fun;
2011 if (SYMBOLP (fun) && !NILP (fun)
2012 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2013 fun = indirect_function (fun);
2015 if (SUBRP (fun))
2017 Lisp_Object numargs;
2018 Lisp_Object argvals[8];
2019 Lisp_Object args_left;
2020 register int i, maxargs;
2022 args_left = original_args;
2023 numargs = Flength (args_left);
2025 check_cons_list ();
2027 if (XINT (numargs) < XSUBR (fun)->min_args
2028 || (XSUBR (fun)->max_args >= 0
2029 && XSUBR (fun)->max_args < XINT (numargs)))
2030 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2032 else if (XSUBR (fun)->max_args == UNEVALLED)
2033 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2034 else if (XSUBR (fun)->max_args == MANY)
2036 /* Pass a vector of evaluated arguments. */
2037 Lisp_Object *vals;
2038 ptrdiff_t argnum = 0;
2039 USE_SAFE_ALLOCA;
2041 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2043 GCPRO3 (args_left, fun, fun);
2044 gcpro3.var = vals;
2045 gcpro3.nvars = 0;
2047 while (!NILP (args_left))
2049 vals[argnum++] = eval_sub (Fcar (args_left));
2050 args_left = Fcdr (args_left);
2051 gcpro3.nvars = argnum;
2054 set_backtrace_args (specpdl_ptr - 1, vals);
2055 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2057 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2058 UNGCPRO;
2059 SAFE_FREE ();
2061 else
2063 GCPRO3 (args_left, fun, fun);
2064 gcpro3.var = argvals;
2065 gcpro3.nvars = 0;
2067 maxargs = XSUBR (fun)->max_args;
2068 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2070 argvals[i] = eval_sub (Fcar (args_left));
2071 gcpro3.nvars = ++i;
2074 UNGCPRO;
2076 set_backtrace_args (specpdl_ptr - 1, argvals);
2077 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2079 switch (i)
2081 case 0:
2082 val = (XSUBR (fun)->function.a0 ());
2083 break;
2084 case 1:
2085 val = (XSUBR (fun)->function.a1 (argvals[0]));
2086 break;
2087 case 2:
2088 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2089 break;
2090 case 3:
2091 val = (XSUBR (fun)->function.a3
2092 (argvals[0], argvals[1], argvals[2]));
2093 break;
2094 case 4:
2095 val = (XSUBR (fun)->function.a4
2096 (argvals[0], argvals[1], argvals[2], argvals[3]));
2097 break;
2098 case 5:
2099 val = (XSUBR (fun)->function.a5
2100 (argvals[0], argvals[1], argvals[2], argvals[3],
2101 argvals[4]));
2102 break;
2103 case 6:
2104 val = (XSUBR (fun)->function.a6
2105 (argvals[0], argvals[1], argvals[2], argvals[3],
2106 argvals[4], argvals[5]));
2107 break;
2108 case 7:
2109 val = (XSUBR (fun)->function.a7
2110 (argvals[0], argvals[1], argvals[2], argvals[3],
2111 argvals[4], argvals[5], argvals[6]));
2112 break;
2114 case 8:
2115 val = (XSUBR (fun)->function.a8
2116 (argvals[0], argvals[1], argvals[2], argvals[3],
2117 argvals[4], argvals[5], argvals[6], argvals[7]));
2118 break;
2120 default:
2121 /* Someone has created a subr that takes more arguments than
2122 is supported by this code. We need to either rewrite the
2123 subr to use a different argument protocol, or add more
2124 cases to this switch. */
2125 emacs_abort ();
2129 else if (COMPILEDP (fun))
2130 val = apply_lambda (fun, original_args);
2131 else
2133 if (NILP (fun))
2134 xsignal1 (Qvoid_function, original_fun);
2135 if (!CONSP (fun))
2136 xsignal1 (Qinvalid_function, original_fun);
2137 funcar = XCAR (fun);
2138 if (!SYMBOLP (funcar))
2139 xsignal1 (Qinvalid_function, original_fun);
2140 if (EQ (funcar, Qautoload))
2142 Fautoload_do_load (fun, original_fun, Qnil);
2143 goto retry;
2145 if (EQ (funcar, Qmacro))
2147 ptrdiff_t count = SPECPDL_INDEX ();
2148 Lisp_Object exp;
2149 /* Bind lexical-binding during expansion of the macro, so the
2150 macro can know reliably if the code it outputs will be
2151 interpreted using lexical-binding or not. */
2152 specbind (Qlexical_binding,
2153 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2154 exp = apply1 (Fcdr (fun), original_args);
2155 unbind_to (count, Qnil);
2156 val = eval_sub (exp);
2158 else if (EQ (funcar, Qlambda)
2159 || EQ (funcar, Qclosure))
2160 val = apply_lambda (fun, original_args);
2161 else
2162 xsignal1 (Qinvalid_function, original_fun);
2164 check_cons_list ();
2166 lisp_eval_depth--;
2167 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2168 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2169 specpdl_ptr--;
2171 return val;
2174 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2175 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2176 Then return the value FUNCTION returns.
2177 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2178 usage: (apply FUNCTION &rest ARGUMENTS) */)
2179 (ptrdiff_t nargs, Lisp_Object *args)
2181 ptrdiff_t i;
2182 EMACS_INT numargs;
2183 register Lisp_Object spread_arg;
2184 register Lisp_Object *funcall_args;
2185 Lisp_Object fun, retval;
2186 struct gcpro gcpro1;
2187 USE_SAFE_ALLOCA;
2189 fun = args [0];
2190 funcall_args = 0;
2191 spread_arg = args [nargs - 1];
2192 CHECK_LIST (spread_arg);
2194 numargs = XINT (Flength (spread_arg));
2196 if (numargs == 0)
2197 return Ffuncall (nargs - 1, args);
2198 else if (numargs == 1)
2200 args [nargs - 1] = XCAR (spread_arg);
2201 return Ffuncall (nargs, args);
2204 numargs += nargs - 2;
2206 /* Optimize for no indirection. */
2207 if (SYMBOLP (fun) && !NILP (fun)
2208 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2209 fun = indirect_function (fun);
2210 if (NILP (fun))
2212 /* Let funcall get the error. */
2213 fun = args[0];
2214 goto funcall;
2217 if (SUBRP (fun))
2219 if (numargs < XSUBR (fun)->min_args
2220 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2221 goto funcall; /* Let funcall get the error. */
2222 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2224 /* Avoid making funcall cons up a yet another new vector of arguments
2225 by explicitly supplying nil's for optional values. */
2226 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2227 for (i = numargs; i < XSUBR (fun)->max_args;)
2228 funcall_args[++i] = Qnil;
2229 GCPRO1 (*funcall_args);
2230 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2233 funcall:
2234 /* We add 1 to numargs because funcall_args includes the
2235 function itself as well as its arguments. */
2236 if (!funcall_args)
2238 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2239 GCPRO1 (*funcall_args);
2240 gcpro1.nvars = 1 + numargs;
2243 memcpy (funcall_args, args, nargs * word_size);
2244 /* Spread the last arg we got. Its first element goes in
2245 the slot that it used to occupy, hence this value of I. */
2246 i = nargs - 1;
2247 while (!NILP (spread_arg))
2249 funcall_args [i++] = XCAR (spread_arg);
2250 spread_arg = XCDR (spread_arg);
2253 /* By convention, the caller needs to gcpro Ffuncall's args. */
2254 retval = Ffuncall (gcpro1.nvars, funcall_args);
2255 UNGCPRO;
2256 SAFE_FREE ();
2258 return retval;
2261 /* Run hook variables in various ways. */
2263 static Lisp_Object
2264 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2266 Ffuncall (nargs, args);
2267 return Qnil;
2270 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2271 doc: /* Run each hook in HOOKS.
2272 Each argument should be a symbol, a hook variable.
2273 These symbols are processed in the order specified.
2274 If a hook symbol has a non-nil value, that value may be a function
2275 or a list of functions to be called to run the hook.
2276 If the value is a function, it is called with no arguments.
2277 If it is a list, the elements are called, in order, with no arguments.
2279 Major modes should not use this function directly to run their mode
2280 hook; they should use `run-mode-hooks' instead.
2282 Do not use `make-local-variable' to make a hook variable buffer-local.
2283 Instead, use `add-hook' and specify t for the LOCAL argument.
2284 usage: (run-hooks &rest HOOKS) */)
2285 (ptrdiff_t nargs, Lisp_Object *args)
2287 Lisp_Object hook[1];
2288 ptrdiff_t i;
2290 for (i = 0; i < nargs; i++)
2292 hook[0] = args[i];
2293 run_hook_with_args (1, hook, funcall_nil);
2296 return Qnil;
2299 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2300 Srun_hook_with_args, 1, MANY, 0,
2301 doc: /* Run HOOK with the specified arguments ARGS.
2302 HOOK should be a symbol, a hook variable. The value of HOOK
2303 may be nil, a function, or a list of functions. Call each
2304 function in order with arguments ARGS. The final return value
2305 is unspecified.
2307 Do not use `make-local-variable' to make a hook variable buffer-local.
2308 Instead, use `add-hook' and specify t for the LOCAL argument.
2309 usage: (run-hook-with-args HOOK &rest ARGS) */)
2310 (ptrdiff_t nargs, Lisp_Object *args)
2312 return run_hook_with_args (nargs, args, funcall_nil);
2315 /* NB this one still documents a specific non-nil return value.
2316 (As did run-hook-with-args and run-hook-with-args-until-failure
2317 until they were changed in 24.1.) */
2318 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2319 Srun_hook_with_args_until_success, 1, MANY, 0,
2320 doc: /* Run HOOK with the specified arguments ARGS.
2321 HOOK should be a symbol, a hook variable. The value of HOOK
2322 may be nil, a function, or a list of functions. Call each
2323 function in order with arguments ARGS, stopping at the first
2324 one that returns non-nil, and return that value. Otherwise (if
2325 all functions return nil, or if there are no functions to call),
2326 return nil.
2328 Do not use `make-local-variable' to make a hook variable buffer-local.
2329 Instead, use `add-hook' and specify t for the LOCAL argument.
2330 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2331 (ptrdiff_t nargs, Lisp_Object *args)
2333 return run_hook_with_args (nargs, args, Ffuncall);
2336 static Lisp_Object
2337 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2339 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2342 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2343 Srun_hook_with_args_until_failure, 1, MANY, 0,
2344 doc: /* Run HOOK with the specified arguments ARGS.
2345 HOOK should be a symbol, a hook variable. The value of HOOK
2346 may be nil, a function, or a list of functions. Call each
2347 function in order with arguments ARGS, stopping at the first
2348 one that returns nil, and return nil. Otherwise (if all functions
2349 return non-nil, or if there are no functions to call), return non-nil
2350 \(do not rely on the precise return value in this case).
2352 Do not use `make-local-variable' to make a hook variable buffer-local.
2353 Instead, use `add-hook' and specify t for the LOCAL argument.
2354 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2355 (ptrdiff_t nargs, Lisp_Object *args)
2357 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2360 static Lisp_Object
2361 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2363 Lisp_Object tmp = args[0], ret;
2364 args[0] = args[1];
2365 args[1] = tmp;
2366 ret = Ffuncall (nargs, args);
2367 args[1] = args[0];
2368 args[0] = tmp;
2369 return ret;
2372 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2373 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2374 I.e. instead of calling each function FUN directly with arguments ARGS,
2375 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2376 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2377 aborts and returns that value.
2378 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2379 (ptrdiff_t nargs, Lisp_Object *args)
2381 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2384 /* ARGS[0] should be a hook symbol.
2385 Call each of the functions in the hook value, passing each of them
2386 as arguments all the rest of ARGS (all NARGS - 1 elements).
2387 FUNCALL specifies how to call each function on the hook.
2388 The caller (or its caller, etc) must gcpro all of ARGS,
2389 except that it isn't necessary to gcpro ARGS[0]. */
2391 Lisp_Object
2392 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2393 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2395 Lisp_Object sym, val, ret = Qnil;
2396 struct gcpro gcpro1, gcpro2, gcpro3;
2398 /* If we are dying or still initializing,
2399 don't do anything--it would probably crash if we tried. */
2400 if (NILP (Vrun_hooks))
2401 return Qnil;
2403 sym = args[0];
2404 val = find_symbol_value (sym);
2406 if (EQ (val, Qunbound) || NILP (val))
2407 return ret;
2408 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2410 args[0] = val;
2411 return funcall (nargs, args);
2413 else
2415 Lisp_Object global_vals = Qnil;
2416 GCPRO3 (sym, val, global_vals);
2418 for (;
2419 CONSP (val) && NILP (ret);
2420 val = XCDR (val))
2422 if (EQ (XCAR (val), Qt))
2424 /* t indicates this hook has a local binding;
2425 it means to run the global binding too. */
2426 global_vals = Fdefault_value (sym);
2427 if (NILP (global_vals)) continue;
2429 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2431 args[0] = global_vals;
2432 ret = funcall (nargs, args);
2434 else
2436 for (;
2437 CONSP (global_vals) && NILP (ret);
2438 global_vals = XCDR (global_vals))
2440 args[0] = XCAR (global_vals);
2441 /* In a global value, t should not occur. If it does, we
2442 must ignore it to avoid an endless loop. */
2443 if (!EQ (args[0], Qt))
2444 ret = funcall (nargs, args);
2448 else
2450 args[0] = XCAR (val);
2451 ret = funcall (nargs, args);
2455 UNGCPRO;
2456 return ret;
2460 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2462 void
2463 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2465 Lisp_Object temp[3];
2466 temp[0] = hook;
2467 temp[1] = arg1;
2468 temp[2] = arg2;
2470 Frun_hook_with_args (3, temp);
2473 /* Apply fn to arg. */
2474 Lisp_Object
2475 apply1 (Lisp_Object fn, Lisp_Object arg)
2477 struct gcpro gcpro1;
2479 GCPRO1 (fn);
2480 if (NILP (arg))
2481 RETURN_UNGCPRO (Ffuncall (1, &fn));
2482 gcpro1.nvars = 2;
2484 Lisp_Object args[2];
2485 args[0] = fn;
2486 args[1] = arg;
2487 gcpro1.var = args;
2488 RETURN_UNGCPRO (Fapply (2, args));
2492 /* Call function fn on no arguments. */
2493 Lisp_Object
2494 call0 (Lisp_Object fn)
2496 struct gcpro gcpro1;
2498 GCPRO1 (fn);
2499 RETURN_UNGCPRO (Ffuncall (1, &fn));
2502 /* Call function fn with 1 argument arg1. */
2503 /* ARGSUSED */
2504 Lisp_Object
2505 call1 (Lisp_Object fn, Lisp_Object arg1)
2507 struct gcpro gcpro1;
2508 Lisp_Object args[2];
2510 args[0] = fn;
2511 args[1] = arg1;
2512 GCPRO1 (args[0]);
2513 gcpro1.nvars = 2;
2514 RETURN_UNGCPRO (Ffuncall (2, args));
2517 /* Call function fn with 2 arguments arg1, arg2. */
2518 /* ARGSUSED */
2519 Lisp_Object
2520 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2522 struct gcpro gcpro1;
2523 Lisp_Object args[3];
2524 args[0] = fn;
2525 args[1] = arg1;
2526 args[2] = arg2;
2527 GCPRO1 (args[0]);
2528 gcpro1.nvars = 3;
2529 RETURN_UNGCPRO (Ffuncall (3, args));
2532 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2533 /* ARGSUSED */
2534 Lisp_Object
2535 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2537 struct gcpro gcpro1;
2538 Lisp_Object args[4];
2539 args[0] = fn;
2540 args[1] = arg1;
2541 args[2] = arg2;
2542 args[3] = arg3;
2543 GCPRO1 (args[0]);
2544 gcpro1.nvars = 4;
2545 RETURN_UNGCPRO (Ffuncall (4, args));
2548 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2549 /* ARGSUSED */
2550 Lisp_Object
2551 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2552 Lisp_Object arg4)
2554 struct gcpro gcpro1;
2555 Lisp_Object args[5];
2556 args[0] = fn;
2557 args[1] = arg1;
2558 args[2] = arg2;
2559 args[3] = arg3;
2560 args[4] = arg4;
2561 GCPRO1 (args[0]);
2562 gcpro1.nvars = 5;
2563 RETURN_UNGCPRO (Ffuncall (5, args));
2566 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2567 /* ARGSUSED */
2568 Lisp_Object
2569 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2570 Lisp_Object arg4, Lisp_Object arg5)
2572 struct gcpro gcpro1;
2573 Lisp_Object args[6];
2574 args[0] = fn;
2575 args[1] = arg1;
2576 args[2] = arg2;
2577 args[3] = arg3;
2578 args[4] = arg4;
2579 args[5] = arg5;
2580 GCPRO1 (args[0]);
2581 gcpro1.nvars = 6;
2582 RETURN_UNGCPRO (Ffuncall (6, args));
2585 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2586 /* ARGSUSED */
2587 Lisp_Object
2588 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2589 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2591 struct gcpro gcpro1;
2592 Lisp_Object args[7];
2593 args[0] = fn;
2594 args[1] = arg1;
2595 args[2] = arg2;
2596 args[3] = arg3;
2597 args[4] = arg4;
2598 args[5] = arg5;
2599 args[6] = arg6;
2600 GCPRO1 (args[0]);
2601 gcpro1.nvars = 7;
2602 RETURN_UNGCPRO (Ffuncall (7, args));
2605 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2606 /* ARGSUSED */
2607 Lisp_Object
2608 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2609 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2611 struct gcpro gcpro1;
2612 Lisp_Object args[8];
2613 args[0] = fn;
2614 args[1] = arg1;
2615 args[2] = arg2;
2616 args[3] = arg3;
2617 args[4] = arg4;
2618 args[5] = arg5;
2619 args[6] = arg6;
2620 args[7] = arg7;
2621 GCPRO1 (args[0]);
2622 gcpro1.nvars = 8;
2623 RETURN_UNGCPRO (Ffuncall (8, args));
2626 /* The caller should GCPRO all the elements of ARGS. */
2628 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2629 doc: /* Non-nil if OBJECT is a function. */)
2630 (Lisp_Object object)
2632 if (FUNCTIONP (object))
2633 return Qt;
2634 return Qnil;
2637 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2638 doc: /* Call first argument as a function, passing remaining arguments to it.
2639 Return the value that function returns.
2640 Thus, (funcall 'cons 'x 'y) returns (x . y).
2641 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2642 (ptrdiff_t nargs, Lisp_Object *args)
2644 Lisp_Object fun, original_fun;
2645 Lisp_Object funcar;
2646 ptrdiff_t numargs = nargs - 1;
2647 Lisp_Object lisp_numargs;
2648 Lisp_Object val;
2649 register Lisp_Object *internal_args;
2650 ptrdiff_t i;
2652 QUIT;
2654 if (++lisp_eval_depth > max_lisp_eval_depth)
2656 if (max_lisp_eval_depth < 100)
2657 max_lisp_eval_depth = 100;
2658 if (lisp_eval_depth > max_lisp_eval_depth)
2659 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2662 /* This also GCPROs them. */
2663 record_in_backtrace (args[0], &args[1], nargs - 1);
2665 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2666 maybe_gc ();
2668 if (debug_on_next_call)
2669 do_debug_on_call (Qlambda);
2671 check_cons_list ();
2673 original_fun = args[0];
2675 retry:
2677 /* Optimize for no indirection. */
2678 fun = original_fun;
2679 if (SYMBOLP (fun) && !NILP (fun)
2680 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2681 fun = indirect_function (fun);
2683 if (SUBRP (fun))
2685 if (numargs < XSUBR (fun)->min_args
2686 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2688 XSETFASTINT (lisp_numargs, numargs);
2689 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2692 else if (XSUBR (fun)->max_args == UNEVALLED)
2693 xsignal1 (Qinvalid_function, original_fun);
2695 else if (XSUBR (fun)->max_args == MANY)
2696 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2697 else
2699 if (XSUBR (fun)->max_args > numargs)
2701 internal_args = alloca (XSUBR (fun)->max_args
2702 * sizeof *internal_args);
2703 memcpy (internal_args, args + 1, numargs * word_size);
2704 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2705 internal_args[i] = Qnil;
2707 else
2708 internal_args = args + 1;
2709 switch (XSUBR (fun)->max_args)
2711 case 0:
2712 val = (XSUBR (fun)->function.a0 ());
2713 break;
2714 case 1:
2715 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2716 break;
2717 case 2:
2718 val = (XSUBR (fun)->function.a2
2719 (internal_args[0], internal_args[1]));
2720 break;
2721 case 3:
2722 val = (XSUBR (fun)->function.a3
2723 (internal_args[0], internal_args[1], internal_args[2]));
2724 break;
2725 case 4:
2726 val = (XSUBR (fun)->function.a4
2727 (internal_args[0], internal_args[1], internal_args[2],
2728 internal_args[3]));
2729 break;
2730 case 5:
2731 val = (XSUBR (fun)->function.a5
2732 (internal_args[0], internal_args[1], internal_args[2],
2733 internal_args[3], internal_args[4]));
2734 break;
2735 case 6:
2736 val = (XSUBR (fun)->function.a6
2737 (internal_args[0], internal_args[1], internal_args[2],
2738 internal_args[3], internal_args[4], internal_args[5]));
2739 break;
2740 case 7:
2741 val = (XSUBR (fun)->function.a7
2742 (internal_args[0], internal_args[1], internal_args[2],
2743 internal_args[3], internal_args[4], internal_args[5],
2744 internal_args[6]));
2745 break;
2747 case 8:
2748 val = (XSUBR (fun)->function.a8
2749 (internal_args[0], internal_args[1], internal_args[2],
2750 internal_args[3], internal_args[4], internal_args[5],
2751 internal_args[6], internal_args[7]));
2752 break;
2754 default:
2756 /* If a subr takes more than 8 arguments without using MANY
2757 or UNEVALLED, we need to extend this function to support it.
2758 Until this is done, there is no way to call the function. */
2759 emacs_abort ();
2763 else if (COMPILEDP (fun))
2764 val = funcall_lambda (fun, numargs, args + 1);
2765 else
2767 if (NILP (fun))
2768 xsignal1 (Qvoid_function, original_fun);
2769 if (!CONSP (fun))
2770 xsignal1 (Qinvalid_function, original_fun);
2771 funcar = XCAR (fun);
2772 if (!SYMBOLP (funcar))
2773 xsignal1 (Qinvalid_function, original_fun);
2774 if (EQ (funcar, Qlambda)
2775 || EQ (funcar, Qclosure))
2776 val = funcall_lambda (fun, numargs, args + 1);
2777 else if (EQ (funcar, Qautoload))
2779 Fautoload_do_load (fun, original_fun, Qnil);
2780 check_cons_list ();
2781 goto retry;
2783 else
2784 xsignal1 (Qinvalid_function, original_fun);
2786 check_cons_list ();
2787 lisp_eval_depth--;
2788 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2789 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2790 specpdl_ptr--;
2791 return val;
2794 static Lisp_Object
2795 apply_lambda (Lisp_Object fun, Lisp_Object args)
2797 Lisp_Object args_left;
2798 ptrdiff_t i;
2799 EMACS_INT numargs;
2800 register Lisp_Object *arg_vector;
2801 struct gcpro gcpro1, gcpro2, gcpro3;
2802 register Lisp_Object tem;
2803 USE_SAFE_ALLOCA;
2805 numargs = XFASTINT (Flength (args));
2806 SAFE_ALLOCA_LISP (arg_vector, numargs);
2807 args_left = args;
2809 GCPRO3 (*arg_vector, args_left, fun);
2810 gcpro1.nvars = 0;
2812 for (i = 0; i < numargs; )
2814 tem = Fcar (args_left), args_left = Fcdr (args_left);
2815 tem = eval_sub (tem);
2816 arg_vector[i++] = tem;
2817 gcpro1.nvars = i;
2820 UNGCPRO;
2822 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2823 set_backtrace_nargs (specpdl_ptr - 1, i);
2824 tem = funcall_lambda (fun, numargs, arg_vector);
2826 /* Do the debug-on-exit now, while arg_vector still exists. */
2827 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2829 /* Don't do it again when we return to eval. */
2830 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2831 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2833 SAFE_FREE ();
2834 return tem;
2837 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2838 and return the result of evaluation.
2839 FUN must be either a lambda-expression or a compiled-code object. */
2841 static Lisp_Object
2842 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2843 register Lisp_Object *arg_vector)
2845 Lisp_Object val, syms_left, next, lexenv;
2846 ptrdiff_t count = SPECPDL_INDEX ();
2847 ptrdiff_t i;
2848 bool optional, rest;
2850 if (CONSP (fun))
2852 if (EQ (XCAR (fun), Qclosure))
2854 fun = XCDR (fun); /* Drop `closure'. */
2855 lexenv = XCAR (fun);
2856 CHECK_LIST_CONS (fun, fun);
2858 else
2859 lexenv = Qnil;
2860 syms_left = XCDR (fun);
2861 if (CONSP (syms_left))
2862 syms_left = XCAR (syms_left);
2863 else
2864 xsignal1 (Qinvalid_function, fun);
2866 else if (COMPILEDP (fun))
2868 syms_left = AREF (fun, COMPILED_ARGLIST);
2869 if (INTEGERP (syms_left))
2870 /* A byte-code object with a non-nil `push args' slot means we
2871 shouldn't bind any arguments, instead just call the byte-code
2872 interpreter directly; it will push arguments as necessary.
2874 Byte-code objects with either a non-existent, or a nil value for
2875 the `push args' slot (the default), have dynamically-bound
2876 arguments, and use the argument-binding code below instead (as do
2877 all interpreted functions, even lexically bound ones). */
2879 /* If we have not actually read the bytecode string
2880 and constants vector yet, fetch them from the file. */
2881 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2882 Ffetch_bytecode (fun);
2883 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2884 AREF (fun, COMPILED_CONSTANTS),
2885 AREF (fun, COMPILED_STACK_DEPTH),
2886 syms_left,
2887 nargs, arg_vector);
2889 lexenv = Qnil;
2891 else
2892 emacs_abort ();
2894 i = optional = rest = 0;
2895 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2897 QUIT;
2899 next = XCAR (syms_left);
2900 if (!SYMBOLP (next))
2901 xsignal1 (Qinvalid_function, fun);
2903 if (EQ (next, Qand_rest))
2904 rest = 1;
2905 else if (EQ (next, Qand_optional))
2906 optional = 1;
2907 else
2909 Lisp_Object arg;
2910 if (rest)
2912 arg = Flist (nargs - i, &arg_vector[i]);
2913 i = nargs;
2915 else if (i < nargs)
2916 arg = arg_vector[i++];
2917 else if (!optional)
2918 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2919 else
2920 arg = Qnil;
2922 /* Bind the argument. */
2923 if (!NILP (lexenv) && SYMBOLP (next))
2924 /* Lexically bind NEXT by adding it to the lexenv alist. */
2925 lexenv = Fcons (Fcons (next, arg), lexenv);
2926 else
2927 /* Dynamically bind NEXT. */
2928 specbind (next, arg);
2932 if (!NILP (syms_left))
2933 xsignal1 (Qinvalid_function, fun);
2934 else if (i < nargs)
2935 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2937 if (!EQ (lexenv, Vinternal_interpreter_environment))
2938 /* Instantiate a new lexical environment. */
2939 specbind (Qinternal_interpreter_environment, lexenv);
2941 if (CONSP (fun))
2942 val = Fprogn (XCDR (XCDR (fun)));
2943 else
2945 /* If we have not actually read the bytecode string
2946 and constants vector yet, fetch them from the file. */
2947 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2948 Ffetch_bytecode (fun);
2949 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2950 AREF (fun, COMPILED_CONSTANTS),
2951 AREF (fun, COMPILED_STACK_DEPTH),
2952 Qnil, 0, 0);
2955 return unbind_to (count, val);
2958 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2959 1, 1, 0,
2960 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2961 (Lisp_Object object)
2963 Lisp_Object tem;
2965 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2967 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2968 if (!CONSP (tem))
2970 tem = AREF (object, COMPILED_BYTECODE);
2971 if (CONSP (tem) && STRINGP (XCAR (tem)))
2972 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2973 else
2974 error ("Invalid byte code");
2976 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2977 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2979 return object;
2982 /* Return true if SYMBOL currently has a let-binding
2983 which was made in the buffer that is now current. */
2985 bool
2986 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2988 struct specbinding *p;
2989 Lisp_Object buf = Fcurrent_buffer ();
2991 for (p = specpdl_ptr; p > specpdl; )
2992 if ((--p)->kind > SPECPDL_LET)
2994 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2995 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2996 if (symbol == let_bound_symbol
2997 && EQ (specpdl_where (p), buf))
2998 return 1;
3001 return 0;
3004 bool
3005 let_shadows_global_binding_p (Lisp_Object symbol)
3007 struct specbinding *p;
3009 for (p = specpdl_ptr; p > specpdl; )
3010 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3011 return 1;
3013 return 0;
3016 static Lisp_Object
3017 binding_symbol (const struct specbinding *bind)
3019 if (!CONSP (bind->symbol))
3020 return bind->symbol;
3021 return XCAR (bind->symbol);
3024 void
3025 do_specbind (struct Lisp_Symbol *sym, struct specbinding *bind,
3026 Lisp_Object value)
3028 switch (sym->redirect)
3030 case SYMBOL_PLAINVAL:
3031 if (!sym->constant)
3032 SET_SYMBOL_VAL (sym, value);
3033 else
3034 set_internal (bind->symbol, value, Qnil, 1);
3035 break;
3037 case SYMBOL_LOCALIZED:
3038 case SYMBOL_FORWARDED:
3039 if ((sym->redirect == SYMBOL_LOCALIZED
3040 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3041 && CONSP (bind->symbol))
3043 Lisp_Object where;
3045 where = XCAR (XCDR (bind->symbol));
3046 if (NILP (where)
3047 && sym->redirect == SYMBOL_FORWARDED)
3049 Fset_default (XCAR (bind->symbol), value);
3050 return;
3054 set_internal (binding_symbol (bind), value, Qnil, 1);
3055 break;
3057 default:
3058 abort ();
3062 /* `specpdl_ptr->symbol' is a field which describes which variable is
3063 let-bound, so it can be properly undone when we unbind_to.
3064 It can have the following two shapes:
3065 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3066 a symbol that is not buffer-local (at least at the time
3067 the let binding started). Note also that it should not be
3068 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3069 to record V2 here).
3070 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3071 variable SYMBOL which can be buffer-local. WHERE tells us
3072 which buffer is affected (or nil if the let-binding affects the
3073 global value of the variable) and BUFFER tells us which buffer was
3074 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3075 BUFFER did not yet have a buffer-local value). */
3077 void
3078 specbind (Lisp_Object symbol, Lisp_Object value)
3080 struct Lisp_Symbol *sym;
3082 CHECK_SYMBOL (symbol);
3083 sym = XSYMBOL (symbol);
3084 if (specpdl_ptr == specpdl + specpdl_size)
3085 grow_specpdl ();
3087 start:
3088 switch (sym->redirect)
3090 case SYMBOL_VARALIAS:
3091 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3092 case SYMBOL_PLAINVAL:
3093 /* The most common case is that of a non-constant symbol with a
3094 trivial value. Make that as fast as we can. */
3095 specpdl_ptr->kind = SPECPDL_LET;
3096 specpdl_ptr->v.let.symbol = symbol;
3097 specpdl_ptr->v.let.old_value = SYMBOL_VAL (sym);
3098 specpdl_ptr->v.let.saved_value = Qnil;
3099 ++specpdl_ptr;
3100 do_specbind (sym, specpdl_ptr - 1, value);
3101 break;
3102 case SYMBOL_LOCALIZED:
3103 if (SYMBOL_BLV (sym)->frame_local)
3104 error ("Frame-local vars cannot be let-bound");
3105 case SYMBOL_FORWARDED:
3107 Lisp_Object ovalue = find_symbol_value (symbol);
3108 specpdl_ptr->kind = SPECPDL_LET_LOCAL;
3109 specpdl_ptr->v.let.symbol = symbol;
3110 specpdl_ptr->v.let.old_value = ovalue;
3111 specpdl_ptr->v.let.where = Fcurrent_buffer ();
3113 eassert (sym->redirect != SYMBOL_LOCALIZED
3114 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3116 if (sym->redirect == SYMBOL_LOCALIZED)
3118 if (!blv_found (SYMBOL_BLV (sym)))
3119 specpdl_ptr->kind = SPECPDL_LET_DEFAULT;
3121 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3123 /* If SYMBOL is a per-buffer variable which doesn't have a
3124 buffer-local value here, make the `let' change the global
3125 value by changing the value of SYMBOL in all buffers not
3126 having their own value. This is consistent with what
3127 happens with other buffer-local variables. */
3128 if (NILP (Flocal_variable_p (symbol, Qnil)))
3130 specpdl_ptr->kind = SPECPDL_LET_DEFAULT;
3131 ++specpdl_ptr;
3132 do_specbind (sym, specpdl_ptr - 1, value);
3133 return;
3136 else
3137 specpdl_ptr->kind = SPECPDL_LET;
3139 specpdl_ptr++;
3140 do_specbind (sym, specpdl_ptr - 1, value);
3141 break;
3143 default: emacs_abort ();
3147 void
3148 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3150 if (specpdl_ptr == specpdl + specpdl_size)
3151 grow_specpdl ();
3152 specpdl_ptr->kind = SPECPDL_UNWIND;
3153 specpdl_ptr->v.unwind.func = function;
3154 specpdl_ptr->v.unwind.arg = arg;
3155 specpdl_ptr++;
3158 void
3159 rebind_for_thread_switch (void)
3161 struct specbinding *bind;
3163 for (bind = specpdl; bind != specpdl_ptr; ++bind)
3165 if (bind->kind >= SPECPDL_LET)
3167 Lisp_Object value = bind->saved_value;
3169 bind->saved_value = Qnil;
3170 do_specbind (XSYMBOL (binding_symbol (bind)), bind, value);
3175 static void
3176 do_one_unbind (const struct specbinding *this_binding, int unwinding)
3178 switch (this_binding->kind)
3180 case SPECPDL_UNWIND:
3181 (*specpdl_func (this_binding)) (specpdl_arg (this_binding));
3182 break;
3183 case SPECPDL_LET:
3184 /* If variable has a trivial value (no forwarding), we can
3185 just set it. No need to check for constant symbols here,
3186 since that was already done by specbind. */
3187 if (XSYMBOL (specpdl_symbol (this_binding))->redirect
3188 == SYMBOL_PLAINVAL)
3189 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (this_binding)),
3190 specpdl_old_value (this_binding));
3191 else
3192 /* NOTE: we only ever come here if make_local_foo was used for
3193 the first time on this var within this let. */
3194 Fset_default (specpdl_symbol (this_binding),
3195 specpdl_old_value (this_binding));
3196 break;
3197 case SPECPDL_BACKTRACE:
3198 break;
3199 case SPECPDL_LET_LOCAL:
3200 case SPECPDL_LET_DEFAULT:
3201 { /* If the symbol is a list, it is really (SYMBOL WHERE
3202 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3203 frame. If WHERE is a buffer or frame, this indicates we
3204 bound a variable that had a buffer-local or frame-local
3205 binding. WHERE nil means that the variable had the default
3206 value when it was bound. CURRENT-BUFFER is the buffer that
3207 was current when the variable was bound. */
3208 Lisp_Object symbol = specpdl_symbol (this_binding);
3209 Lisp_Object where = specpdl_where (this_binding);
3210 eassert (BUFFERP (where));
3212 if (this_binding->kind == SPECPDL_LET_DEFAULT)
3213 Fset_default (symbol, specpdl_old_value (this_binding));
3214 /* If this was a local binding, reset the value in the appropriate
3215 buffer, but only if that buffer's binding still exists. */
3216 else if (!NILP (Flocal_variable_p (symbol, where)))
3217 set_internal (symbol, specpdl_old_value (this_binding),
3218 where, 1);
3220 break;
3224 Lisp_Object
3225 unbind_to (ptrdiff_t count, Lisp_Object value)
3227 Lisp_Object quitf = Vquit_flag;
3228 struct gcpro gcpro1, gcpro2;
3230 GCPRO2 (value, quitf);
3231 Vquit_flag = Qnil;
3233 while (specpdl_ptr != specpdl + count)
3235 /* Copy the binding, and decrement specpdl_ptr, before we do
3236 the work to unbind it. We decrement first
3237 so that an error in unbinding won't try to unbind
3238 the same entry again, and we copy the binding first
3239 in case more bindings are made during some of the code we run. */
3241 struct specbinding this_binding;
3242 this_binding = *--specpdl_ptr;
3244 do_one_unbind (&this_binding, 1);
3247 if (NILP (Vquit_flag) && !NILP (quitf))
3248 Vquit_flag = quitf;
3250 UNGCPRO;
3251 return value;
3254 void
3255 unbind_for_thread_switch (void)
3257 struct specbinding *bind;
3259 for (bind = specpdl_ptr; bind != specpdl; --bind)
3261 if (bind->kind >= SPECPDL_LET)
3263 bind->saved_value = find_symbol_value (binding_symbol (bind));
3264 do_one_unbind (bind, 0);
3269 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3270 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3271 A special variable is one that will be bound dynamically, even in a
3272 context where binding is lexical by default. */)
3273 (Lisp_Object symbol)
3275 CHECK_SYMBOL (symbol);
3276 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3280 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3281 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3282 The debugger is entered when that frame exits, if the flag is non-nil. */)
3283 (Lisp_Object level, Lisp_Object flag)
3285 struct specbinding *pdl = backtrace_top ();
3286 register EMACS_INT i;
3288 CHECK_NUMBER (level);
3290 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3291 pdl = backtrace_next (pdl);
3293 if (backtrace_p (pdl))
3294 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3296 return flag;
3299 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3300 doc: /* Print a trace of Lisp function calls currently active.
3301 Output stream used is value of `standard-output'. */)
3302 (void)
3304 struct specbinding *pdl = backtrace_top ();
3305 Lisp_Object tem;
3306 Lisp_Object old_print_level = Vprint_level;
3308 if (NILP (Vprint_level))
3309 XSETFASTINT (Vprint_level, 8);
3311 while (backtrace_p (pdl))
3313 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3314 if (backtrace_nargs (pdl) == UNEVALLED)
3316 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3317 Qnil);
3318 write_string ("\n", -1);
3320 else
3322 tem = backtrace_function (pdl);
3323 Fprin1 (tem, Qnil); /* This can QUIT. */
3324 write_string ("(", -1);
3326 ptrdiff_t i;
3327 for (i = 0; i < backtrace_nargs (pdl); i++)
3329 if (i) write_string (" ", -1);
3330 Fprin1 (backtrace_args (pdl)[i], Qnil);
3333 write_string (")\n", -1);
3335 pdl = backtrace_next (pdl);
3338 Vprint_level = old_print_level;
3339 return Qnil;
3342 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3343 doc: /* Return the function and arguments NFRAMES up from current execution point.
3344 If that frame has not evaluated the arguments yet (or is a special form),
3345 the value is (nil FUNCTION ARG-FORMS...).
3346 If that frame has evaluated its arguments and called its function already,
3347 the value is (t FUNCTION ARG-VALUES...).
3348 A &rest arg is represented as the tail of the list ARG-VALUES.
3349 FUNCTION is whatever was supplied as car of evaluated list,
3350 or a lambda expression for macro calls.
3351 If NFRAMES is more than the number of frames, the value is nil. */)
3352 (Lisp_Object nframes)
3354 struct specbinding *pdl = backtrace_top ();
3355 register EMACS_INT i;
3357 CHECK_NATNUM (nframes);
3359 /* Find the frame requested. */
3360 for (i = 0; backtrace_p (pdl) && i < XFASTINT (nframes); i++)
3361 pdl = backtrace_next (pdl);
3363 if (!backtrace_p (pdl))
3364 return Qnil;
3365 if (backtrace_nargs (pdl) == UNEVALLED)
3366 return Fcons (Qnil,
3367 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3368 else
3370 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3372 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3377 void
3378 mark_specpdl (struct specbinding *first, struct specbinding *ptr)
3380 struct specbinding *pdl;
3381 for (pdl = first; pdl != ptr; pdl++)
3383 switch (pdl->kind)
3385 case SPECPDL_UNWIND:
3386 mark_object (specpdl_arg (pdl));
3387 break;
3388 case SPECPDL_BACKTRACE:
3390 ptrdiff_t nargs = backtrace_nargs (pdl);
3391 mark_object (backtrace_function (pdl));
3392 if (nargs == UNEVALLED)
3393 nargs = 1;
3394 while (nargs--)
3395 mark_object (backtrace_args (pdl)[nargs]);
3397 break;
3398 case SPECPDL_LET_DEFAULT:
3399 case SPECPDL_LET_LOCAL:
3400 mark_object (specpdl_where (pdl));
3401 case SPECPDL_LET:
3402 mark_object (specpdl_symbol (pdl));
3403 mark_object (specpdl_old_value (pdl));
3404 mark_object (specpdl_saved_value (pdl));
3409 void
3410 get_backtrace (Lisp_Object array)
3412 struct specbinding *pdl = backtrace_next (backtrace_top ());
3413 ptrdiff_t i = 0, asize = ASIZE (array);
3415 /* Copy the backtrace contents into working memory. */
3416 for (; i < asize; i++)
3418 if (backtrace_p (pdl))
3420 ASET (array, i, backtrace_function (pdl));
3421 pdl = backtrace_next (pdl);
3423 else
3424 ASET (array, i, Qnil);
3428 Lisp_Object backtrace_top_function (void)
3430 struct specbinding *pdl = backtrace_top ();
3431 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3434 void
3435 syms_of_eval (void)
3437 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3438 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3439 If Lisp code tries to increase the total number past this amount,
3440 an error is signaled.
3441 You can safely use a value considerably larger than the default value,
3442 if that proves inconveniently small. However, if you increase it too far,
3443 Emacs could run out of memory trying to make the stack bigger. */);
3445 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3446 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3448 This limit serves to catch infinite recursions for you before they cause
3449 actual stack overflow in C, which would be fatal for Emacs.
3450 You can safely make it considerably larger than its default value,
3451 if that proves inconveniently small. However, if you increase it too far,
3452 Emacs could overflow the real C stack, and crash. */);
3454 DEFVAR_LISP ("quit-flag", Vquit_flag,
3455 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3456 If the value is t, that means do an ordinary quit.
3457 If the value equals `throw-on-input', that means quit by throwing
3458 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3459 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3460 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3461 Vquit_flag = Qnil;
3463 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3464 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3465 Note that `quit-flag' will still be set by typing C-g,
3466 so a quit will be signaled as soon as `inhibit-quit' is nil.
3467 To prevent this happening, set `quit-flag' to nil
3468 before making `inhibit-quit' nil. */);
3469 Vinhibit_quit = Qnil;
3471 DEFSYM (Qinhibit_quit, "inhibit-quit");
3472 DEFSYM (Qautoload, "autoload");
3473 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3474 DEFSYM (Qmacro, "macro");
3475 DEFSYM (Qdeclare, "declare");
3477 /* Note that the process handling also uses Qexit, but we don't want
3478 to staticpro it twice, so we just do it here. */
3479 DEFSYM (Qexit, "exit");
3481 DEFSYM (Qinteractive, "interactive");
3482 DEFSYM (Qcommandp, "commandp");
3483 DEFSYM (Qand_rest, "&rest");
3484 DEFSYM (Qand_optional, "&optional");
3485 DEFSYM (Qclosure, "closure");
3486 DEFSYM (Qdebug, "debug");
3488 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3489 doc: /* Non-nil means never enter the debugger.
3490 Normally set while the debugger is already active, to avoid recursive
3491 invocations. */);
3492 Vinhibit_debugger = Qnil;
3494 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3495 doc: /* Non-nil means enter debugger if an error is signaled.
3496 Does not apply to errors handled by `condition-case' or those
3497 matched by `debug-ignored-errors'.
3498 If the value is a list, an error only means to enter the debugger
3499 if one of its condition symbols appears in the list.
3500 When you evaluate an expression interactively, this variable
3501 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3502 The command `toggle-debug-on-error' toggles this.
3503 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3504 Vdebug_on_error = Qnil;
3506 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3507 doc: /* List of errors for which the debugger should not be called.
3508 Each element may be a condition-name or a regexp that matches error messages.
3509 If any element applies to a given error, that error skips the debugger
3510 and just returns to top level.
3511 This overrides the variable `debug-on-error'.
3512 It does not apply to errors handled by `condition-case'. */);
3513 Vdebug_ignored_errors = Qnil;
3515 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3516 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3517 Does not apply if quit is handled by a `condition-case'. */);
3518 debug_on_quit = 0;
3520 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3521 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3523 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3524 doc: /* Non-nil means debugger may continue execution.
3525 This is nil when the debugger is called under circumstances where it
3526 might not be safe to continue. */);
3527 debugger_may_continue = 1;
3529 DEFVAR_LISP ("debugger", Vdebugger,
3530 doc: /* Function to call to invoke debugger.
3531 If due to frame exit, args are `exit' and the value being returned;
3532 this function's value will be returned instead of that.
3533 If due to error, args are `error' and a list of the args to `signal'.
3534 If due to `apply' or `funcall' entry, one arg, `lambda'.
3535 If due to `eval' entry, one arg, t. */);
3536 Vdebugger = Qnil;
3538 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3539 doc: /* If non-nil, this is a function for `signal' to call.
3540 It receives the same arguments that `signal' was given.
3541 The Edebug package uses this to regain control. */);
3542 Vsignal_hook_function = Qnil;
3544 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3545 doc: /* Non-nil means call the debugger regardless of condition handlers.
3546 Note that `debug-on-error', `debug-on-quit' and friends
3547 still determine whether to handle the particular condition. */);
3548 Vdebug_on_signal = Qnil;
3550 /* When lexical binding is being used,
3551 Vinternal_interpreter_environment is non-nil, and contains an alist
3552 of lexically-bound variable, or (t), indicating an empty
3553 environment. The lisp name of this variable would be
3554 `internal-interpreter-environment' if it weren't hidden.
3555 Every element of this list can be either a cons (VAR . VAL)
3556 specifying a lexical binding, or a single symbol VAR indicating
3557 that this variable should use dynamic scoping. */
3558 DEFSYM (Qinternal_interpreter_environment,
3559 "internal-interpreter-environment");
3560 DEFVAR_LISP ("internal-interpreter-environment",
3561 Vinternal_interpreter_environment,
3562 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3563 When lexical binding is not being used, this variable is nil.
3564 A value of `(t)' indicates an empty environment, otherwise it is an
3565 alist of active lexical bindings. */);
3566 Vinternal_interpreter_environment = Qnil;
3567 /* Don't export this variable to Elisp, so no one can mess with it
3568 (Just imagine if someone makes it buffer-local). */
3569 Funintern (Qinternal_interpreter_environment, Qnil);
3571 DEFSYM (Vrun_hooks, "run-hooks");
3573 staticpro (&Vautoload_queue);
3574 Vautoload_queue = Qnil;
3575 staticpro (&Vsignaling_function);
3576 Vsignaling_function = Qnil;
3578 inhibit_lisp_code = Qnil;
3580 defsubr (&Sor);
3581 defsubr (&Sand);
3582 defsubr (&Sif);
3583 defsubr (&Scond);
3584 defsubr (&Sprogn);
3585 defsubr (&Sprog1);
3586 defsubr (&Sprog2);
3587 defsubr (&Ssetq);
3588 defsubr (&Squote);
3589 defsubr (&Sfunction);
3590 defsubr (&Sdefvar);
3591 defsubr (&Sdefvaralias);
3592 defsubr (&Sdefconst);
3593 defsubr (&Smake_var_non_special);
3594 defsubr (&Slet);
3595 defsubr (&SletX);
3596 defsubr (&Swhile);
3597 defsubr (&Smacroexpand);
3598 defsubr (&Scatch);
3599 defsubr (&Sthrow);
3600 defsubr (&Sunwind_protect);
3601 defsubr (&Scondition_case);
3602 defsubr (&Ssignal);
3603 defsubr (&Scommandp);
3604 defsubr (&Sautoload);
3605 defsubr (&Sautoload_do_load);
3606 defsubr (&Seval);
3607 defsubr (&Sapply);
3608 defsubr (&Sfuncall);
3609 defsubr (&Srun_hooks);
3610 defsubr (&Srun_hook_with_args);
3611 defsubr (&Srun_hook_with_args_until_success);
3612 defsubr (&Srun_hook_with_args_until_failure);
3613 defsubr (&Srun_hook_wrapped);
3614 defsubr (&Sfetch_bytecode);
3615 defsubr (&Sbacktrace_debug);
3616 defsubr (&Sbacktrace);
3617 defsubr (&Sbacktrace_frame);
3618 defsubr (&Sspecial_variable_p);
3619 defsubr (&Sfunctionp);