Backport GCPRO fix from trunk.
[emacs.git] / src / eval.c
bloba21b2b0576222337323e90439a18a29dbbf95cee
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 struct backtrace *backtrace_list;
37 #if !BYTE_MARK_STACK
38 static
39 #endif
40 struct catchtag *catchlist;
42 /* Chain of condition handlers currently in effect.
43 The elements of this chain are contained in the stack frames
44 of Fcondition_case and internal_condition_case.
45 When an error is signaled (by calling Fsignal, below),
46 this chain is searched for an element that applies. */
48 #if !BYTE_MARK_STACK
49 static
50 #endif
51 struct handler *handlerlist;
53 #ifdef DEBUG_GCPRO
54 /* Count levels of GCPRO to detect failure to UNGCPRO. */
55 int gcpro_level;
56 #endif
58 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
59 Lisp_Object Qinhibit_quit;
60 Lisp_Object Qand_rest;
61 static Lisp_Object Qand_optional;
62 static Lisp_Object Qinhibit_debugger;
63 static Lisp_Object Qdeclare;
64 Lisp_Object Qinternal_interpreter_environment, Qclosure;
66 static Lisp_Object Qdebug;
68 /* This holds either the symbol `run-hooks' or nil.
69 It is nil at an early stage of startup, and when Emacs
70 is shutting down. */
72 Lisp_Object Vrun_hooks;
74 /* Non-nil means record all fset's and provide's, to be undone
75 if the file being autoloaded is not fully loaded.
76 They are recorded by being consed onto the front of Vautoload_queue:
77 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
79 Lisp_Object Vautoload_queue;
81 /* Current number of specbindings allocated in specpdl. */
83 ptrdiff_t specpdl_size;
85 /* Pointer to beginning of specpdl. */
87 struct specbinding *specpdl;
89 /* Pointer to first unused element in specpdl. */
91 struct specbinding *specpdl_ptr;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
102 invocations. */
104 static EMACS_INT when_entered_debugger;
106 /* The function from which the last `signal' was called. Set in
107 Fsignal. */
109 Lisp_Object Vsignaling_function;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code;
117 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
118 static bool interactive_p (void);
119 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
121 /* Functions to set Lisp_Object slots of struct specbinding. */
123 static void
124 set_specpdl_symbol (Lisp_Object symbol)
126 specpdl_ptr->symbol = symbol;
129 static void
130 set_specpdl_old_value (Lisp_Object oldval)
132 specpdl_ptr->old_value = oldval;
135 void
136 init_eval_once (void)
138 enum { size = 50 };
139 specpdl = xmalloc (size * sizeof *specpdl);
140 specpdl_size = size;
141 specpdl_ptr = specpdl;
142 /* Don't forget to update docs (lispref node "Local Variables"). */
143 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
144 max_lisp_eval_depth = 600;
146 Vrun_hooks = Qnil;
149 void
150 init_eval (void)
152 specpdl_ptr = specpdl;
153 catchlist = 0;
154 handlerlist = 0;
155 backtrace_list = 0;
156 Vquit_flag = Qnil;
157 debug_on_next_call = 0;
158 lisp_eval_depth = 0;
159 #ifdef DEBUG_GCPRO
160 gcpro_level = 0;
161 #endif
162 /* This is less than the initial value of num_nonmacro_input_events. */
163 when_entered_debugger = -1;
166 /* Unwind-protect function used by call_debugger. */
168 static Lisp_Object
169 restore_stack_limits (Lisp_Object data)
171 max_specpdl_size = XINT (XCAR (data));
172 max_lisp_eval_depth = XINT (XCDR (data));
173 return Qnil;
176 /* Call the Lisp debugger, giving it argument ARG. */
178 Lisp_Object
179 call_debugger (Lisp_Object arg)
181 bool debug_while_redisplaying;
182 ptrdiff_t count = SPECPDL_INDEX ();
183 Lisp_Object val;
184 EMACS_INT old_max = max_specpdl_size;
186 /* Temporarily bump up the stack limits,
187 so the debugger won't run out of stack. */
189 max_specpdl_size += 1;
190 record_unwind_protect (restore_stack_limits,
191 Fcons (make_number (old_max),
192 make_number (max_lisp_eval_depth)));
193 max_specpdl_size = old_max;
195 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
196 max_lisp_eval_depth = lisp_eval_depth + 40;
198 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
199 max_specpdl_size = SPECPDL_INDEX () + 100;
201 #ifdef HAVE_WINDOW_SYSTEM
202 if (display_hourglass_p)
203 cancel_hourglass ();
204 #endif
206 debug_on_next_call = 0;
207 when_entered_debugger = num_nonmacro_input_events;
209 /* Resetting redisplaying_p to 0 makes sure that debug output is
210 displayed if the debugger is invoked during redisplay. */
211 debug_while_redisplaying = redisplaying_p;
212 redisplaying_p = 0;
213 specbind (intern ("debugger-may-continue"),
214 debug_while_redisplaying ? Qnil : Qt);
215 specbind (Qinhibit_redisplay, Qnil);
216 specbind (Qinhibit_debugger, Qt);
218 #if 0 /* Binding this prevents execution of Lisp code during
219 redisplay, which necessarily leads to display problems. */
220 specbind (Qinhibit_eval_during_redisplay, Qt);
221 #endif
223 val = apply1 (Vdebugger, arg);
225 /* Interrupting redisplay and resuming it later is not safe under
226 all circumstances. So, when the debugger returns, abort the
227 interrupted redisplay by going back to the top-level. */
228 if (debug_while_redisplaying)
229 Ftop_level ();
231 return unbind_to (count, val);
234 static void
235 do_debug_on_call (Lisp_Object code)
237 debug_on_next_call = 0;
238 backtrace_list->debug_on_exit = 1;
239 call_debugger (Fcons (code, Qnil));
242 /* NOTE!!! Every function that can call EVAL must protect its args
243 and temporaries from garbage collection while it needs them.
244 The definition of `For' shows what you have to do. */
246 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
247 doc: /* Eval args until one of them yields non-nil, then return that value.
248 The remaining args are not evalled at all.
249 If all args return nil, return nil.
250 usage: (or CONDITIONS...) */)
251 (Lisp_Object args)
253 register Lisp_Object val = Qnil;
254 struct gcpro gcpro1;
256 GCPRO1 (args);
258 while (CONSP (args))
260 val = eval_sub (XCAR (args));
261 if (!NILP (val))
262 break;
263 args = XCDR (args);
266 UNGCPRO;
267 return val;
270 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
271 doc: /* Eval args until one of them yields nil, then return nil.
272 The remaining args are not evalled at all.
273 If no arg yields nil, return the last arg's value.
274 usage: (and CONDITIONS...) */)
275 (Lisp_Object args)
277 register Lisp_Object val = Qt;
278 struct gcpro gcpro1;
280 GCPRO1 (args);
282 while (CONSP (args))
284 val = eval_sub (XCAR (args));
285 if (NILP (val))
286 break;
287 args = XCDR (args);
290 UNGCPRO;
291 return val;
294 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
295 doc: /* If COND yields non-nil, do THEN, else do ELSE...
296 Returns the value of THEN or the value of the last of the ELSE's.
297 THEN must be one expression, but ELSE... can be zero or more expressions.
298 If COND yields nil, and there are no ELSE's, the value is nil.
299 usage: (if COND THEN ELSE...) */)
300 (Lisp_Object args)
302 register Lisp_Object cond;
303 struct gcpro gcpro1;
305 GCPRO1 (args);
306 cond = eval_sub (Fcar (args));
307 UNGCPRO;
309 if (!NILP (cond))
310 return eval_sub (Fcar (Fcdr (args)));
311 return Fprogn (Fcdr (Fcdr (args)));
314 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
315 doc: /* Try each clause until one succeeds.
316 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
317 and, if the value is non-nil, this clause succeeds:
318 then the expressions in BODY are evaluated and the last one's
319 value is the value of the cond-form.
320 If no clause succeeds, cond returns nil.
321 If a clause has one element, as in (CONDITION),
322 CONDITION's value if non-nil is returned from the cond-form.
323 usage: (cond CLAUSES...) */)
324 (Lisp_Object args)
326 register Lisp_Object clause, val;
327 struct gcpro gcpro1;
329 val = Qnil;
330 GCPRO1 (args);
331 while (!NILP (args))
333 clause = Fcar (args);
334 val = eval_sub (Fcar (clause));
335 if (!NILP (val))
337 if (!EQ (XCDR (clause), Qnil))
338 val = Fprogn (XCDR (clause));
339 break;
341 args = XCDR (args);
343 UNGCPRO;
345 return val;
348 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
349 doc: /* Eval BODY forms sequentially and return value of last one.
350 usage: (progn BODY...) */)
351 (Lisp_Object args)
353 register Lisp_Object val = Qnil;
354 struct gcpro gcpro1;
356 GCPRO1 (args);
358 while (CONSP (args))
360 val = eval_sub (XCAR (args));
361 args = XCDR (args);
364 UNGCPRO;
365 return val;
368 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
369 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
370 The value of FIRST is saved during the evaluation of the remaining args,
371 whose values are discarded.
372 usage: (prog1 FIRST BODY...) */)
373 (Lisp_Object args)
375 Lisp_Object val;
376 register Lisp_Object args_left;
377 struct gcpro gcpro1, gcpro2;
379 args_left = args;
380 val = Qnil;
381 GCPRO2 (args, val);
383 val = eval_sub (XCAR (args_left));
384 while (CONSP (args_left = XCDR (args_left)))
385 eval_sub (XCAR (args_left));
387 UNGCPRO;
388 return val;
391 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
392 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
393 The value of FORM2 is saved during the evaluation of the
394 remaining args, whose values are discarded.
395 usage: (prog2 FORM1 FORM2 BODY...) */)
396 (Lisp_Object args)
398 struct gcpro gcpro1;
400 GCPRO1 (args);
401 eval_sub (XCAR (args));
402 UNGCPRO;
403 return Fprog1 (XCDR (args));
406 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
407 doc: /* Set each SYM to the value of its VAL.
408 The symbols SYM are variables; they are literal (not evaluated).
409 The values VAL are expressions; they are evaluated.
410 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
411 The second VAL is not computed until after the first SYM is set, and so on;
412 each VAL can use the new value of variables set earlier in the `setq'.
413 The return value of the `setq' form is the value of the last VAL.
414 usage: (setq [SYM VAL]...) */)
415 (Lisp_Object args)
417 register Lisp_Object args_left;
418 register Lisp_Object val, sym, lex_binding;
419 struct gcpro gcpro1;
421 if (NILP (args))
422 return Qnil;
424 args_left = args;
425 GCPRO1 (args);
429 val = eval_sub (Fcar (Fcdr (args_left)));
430 sym = Fcar (args_left);
432 /* Like for eval_sub, we do not check declared_special here since
433 it's been done when let-binding. */
434 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
435 && SYMBOLP (sym)
436 && !NILP (lex_binding
437 = Fassq (sym, Vinternal_interpreter_environment)))
438 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
439 else
440 Fset (sym, val); /* SYM is dynamically bound. */
442 args_left = Fcdr (Fcdr (args_left));
444 while (!NILP (args_left));
446 UNGCPRO;
447 return val;
450 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
451 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
452 Warning: `quote' does not construct its return value, but just returns
453 the value that was pre-constructed by the Lisp reader (see info node
454 `(elisp)Printed Representation').
455 This means that '(a . b) is not identical to (cons 'a 'b): the former
456 does not cons. Quoting should be reserved for constants that will
457 never be modified by side-effects, unless you like self-modifying code.
458 See the common pitfall in info node `(elisp)Rearrangement' for an example
459 of unexpected results when a quoted object is modified.
460 usage: (quote ARG) */)
461 (Lisp_Object args)
463 if (!NILP (Fcdr (args)))
464 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
465 return Fcar (args);
468 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
469 doc: /* Like `quote', but preferred for objects which are functions.
470 In byte compilation, `function' causes its argument to be compiled.
471 `quote' cannot do that.
472 usage: (function ARG) */)
473 (Lisp_Object args)
475 Lisp_Object quoted = XCAR (args);
477 if (!NILP (Fcdr (args)))
478 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
480 if (!NILP (Vinternal_interpreter_environment)
481 && CONSP (quoted)
482 && EQ (XCAR (quoted), Qlambda))
483 /* This is a lambda expression within a lexical environment;
484 return an interpreted closure instead of a simple lambda. */
485 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
486 XCDR (quoted)));
487 else
488 /* Simply quote the argument. */
489 return quoted;
493 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
494 doc: /* Return t if the containing function was run directly by user input.
495 This means that the function was called with `call-interactively'
496 \(which includes being called as the binding of a key)
497 and input is currently coming from the keyboard (not a keyboard macro),
498 and Emacs is not running in batch mode (`noninteractive' is nil).
500 The only known proper use of `interactive-p' is in deciding whether to
501 display a helpful message, or how to display it. If you're thinking
502 of using it for any other purpose, it is quite likely that you're
503 making a mistake. Think: what do you want to do when the command is
504 called from a keyboard macro?
506 To test whether your function was called with `call-interactively',
507 either (i) add an extra optional argument and give it an `interactive'
508 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
509 use `called-interactively-p'. */)
510 (void)
512 return (INTERACTIVE && interactive_p ()) ? Qt : Qnil;
516 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
517 doc: /* Return t if the containing function was called by `call-interactively'.
518 If KIND is `interactive', then only return t if the call was made
519 interactively by the user, i.e. not in `noninteractive' mode nor
520 when `executing-kbd-macro'.
521 If KIND is `any', on the other hand, it will return t for any kind of
522 interactive call, including being called as the binding of a key, or
523 from a keyboard macro, or in `noninteractive' mode.
525 The only known proper use of `interactive' for KIND is in deciding
526 whether to display a helpful message, or how to display it. If you're
527 thinking of using it for any other purpose, it is quite likely that
528 you're making a mistake. Think: what do you want to do when the
529 command is called from a keyboard macro?
531 Instead of using this function, it is sometimes cleaner to give your
532 function an extra optional argument whose `interactive' spec specifies
533 non-nil unconditionally (\"p\" is a good way to do this), or via
534 \(not (or executing-kbd-macro noninteractive)). */)
535 (Lisp_Object kind)
537 return (((INTERACTIVE || !EQ (kind, intern ("interactive")))
538 && interactive_p ())
539 ? Qt : Qnil);
543 /* Return true if function in which this appears was called using
544 call-interactively and is not a built-in. */
546 static bool
547 interactive_p (void)
549 struct backtrace *btp;
550 Lisp_Object fun;
552 btp = backtrace_list;
554 /* If this isn't a byte-compiled function, there may be a frame at
555 the top for Finteractive_p. If so, skip it. */
556 fun = Findirect_function (btp->function, Qnil);
557 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
558 || XSUBR (fun) == &Scalled_interactively_p))
559 btp = btp->next;
561 /* If we're running an Emacs 18-style byte-compiled function, there
562 may be a frame for Fbytecode at the top level. In any version of
563 Emacs there can be Fbytecode frames for subexpressions evaluated
564 inside catch and condition-case. Skip past them.
566 If this isn't a byte-compiled function, then we may now be
567 looking at several frames for special forms. Skip past them. */
568 while (btp
569 && (EQ (btp->function, Qbytecode)
570 || btp->nargs == UNEVALLED))
571 btp = btp->next;
573 /* `btp' now points at the frame of the innermost function that isn't
574 a special form, ignoring frames for Finteractive_p and/or
575 Fbytecode at the top. If this frame is for a built-in function
576 (such as load or eval-region) return false. */
577 fun = Findirect_function (btp->function, Qnil);
578 if (SUBRP (fun))
579 return 0;
581 /* `btp' points to the frame of a Lisp function that called interactive-p.
582 Return t if that function was called interactively. */
583 if (btp && btp->next && EQ (btp->next->function, Qcall_interactively))
584 return 1;
585 return 0;
589 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
590 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
591 Aliased variables always have the same value; setting one sets the other.
592 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
593 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
594 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
595 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
596 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
597 The return value is BASE-VARIABLE. */)
598 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
600 struct Lisp_Symbol *sym;
602 CHECK_SYMBOL (new_alias);
603 CHECK_SYMBOL (base_variable);
605 sym = XSYMBOL (new_alias);
607 if (sym->constant)
608 /* Not sure why, but why not? */
609 error ("Cannot make a constant an alias");
611 switch (sym->redirect)
613 case SYMBOL_FORWARDED:
614 error ("Cannot make an internal variable an alias");
615 case SYMBOL_LOCALIZED:
616 error ("Don't know how to make a localized variable an alias");
619 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
620 If n_a is bound, but b_v is not, set the value of b_v to n_a,
621 so that old-code that affects n_a before the aliasing is setup
622 still works. */
623 if (NILP (Fboundp (base_variable)))
624 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
627 struct specbinding *p;
629 for (p = specpdl_ptr; p > specpdl; )
630 if ((--p)->func == NULL
631 && (EQ (new_alias,
632 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
633 error ("Don't know how to make a let-bound variable an alias");
636 sym->declared_special = 1;
637 XSYMBOL (base_variable)->declared_special = 1;
638 sym->redirect = SYMBOL_VARALIAS;
639 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
640 sym->constant = SYMBOL_CONSTANT_P (base_variable);
641 LOADHIST_ATTACH (new_alias);
642 /* Even if docstring is nil: remove old docstring. */
643 Fput (new_alias, Qvariable_documentation, docstring);
645 return base_variable;
649 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
650 doc: /* Define SYMBOL as a variable, and return SYMBOL.
651 You are not required to define a variable in order to use it, but
652 defining it lets you supply an initial value and documentation, which
653 can be referred to by the Emacs help facilities and other programming
654 tools. The `defvar' form also declares the variable as \"special\",
655 so that it is always dynamically bound even if `lexical-binding' is t.
657 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
658 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
659 default value is what is set; buffer-local values are not affected.
660 If INITVALUE is missing, SYMBOL's value is not set.
662 If SYMBOL has a local binding, then this form affects the local
663 binding. This is usually not what you want. Thus, if you need to
664 load a file defining variables, with this form or with `defconst' or
665 `defcustom', you should always load that file _outside_ any bindings
666 for these variables. \(`defconst' and `defcustom' behave similarly in
667 this respect.)
669 The optional argument DOCSTRING is a documentation string for the
670 variable.
672 To define a user option, use `defcustom' instead of `defvar'.
673 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
674 (Lisp_Object args)
676 register Lisp_Object sym, tem, tail;
678 sym = Fcar (args);
679 tail = Fcdr (args);
680 if (!NILP (Fcdr (Fcdr (tail))))
681 error ("Too many arguments");
683 tem = Fdefault_boundp (sym);
684 if (!NILP (tail))
686 /* Do it before evaluating the initial value, for self-references. */
687 XSYMBOL (sym)->declared_special = 1;
689 if (NILP (tem))
690 Fset_default (sym, eval_sub (Fcar (tail)));
691 else
692 { /* Check if there is really a global binding rather than just a let
693 binding that shadows the global unboundness of the var. */
694 struct specbinding *pdl = specpdl_ptr;
695 while (pdl > specpdl)
697 if (EQ ((--pdl)->symbol, sym) && !pdl->func
698 && EQ (pdl->old_value, Qunbound))
700 message_with_string ("Warning: defvar ignored because %s is let-bound",
701 SYMBOL_NAME (sym), 1);
702 break;
706 tail = Fcdr (tail);
707 tem = Fcar (tail);
708 if (!NILP (tem))
710 if (!NILP (Vpurify_flag))
711 tem = Fpurecopy (tem);
712 Fput (sym, Qvariable_documentation, tem);
714 LOADHIST_ATTACH (sym);
716 else if (!NILP (Vinternal_interpreter_environment)
717 && !XSYMBOL (sym)->declared_special)
718 /* A simple (defvar foo) with lexical scoping does "nothing" except
719 declare that var to be dynamically scoped *locally* (i.e. within
720 the current file or let-block). */
721 Vinternal_interpreter_environment =
722 Fcons (sym, Vinternal_interpreter_environment);
723 else
725 /* Simple (defvar <var>) should not count as a definition at all.
726 It could get in the way of other definitions, and unloading this
727 package could try to make the variable unbound. */
730 return sym;
733 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
734 doc: /* Define SYMBOL as a constant variable.
735 This declares that neither programs nor users should ever change the
736 value. This constancy is not actually enforced by Emacs Lisp, but
737 SYMBOL is marked as a special variable so that it is never lexically
738 bound.
740 The `defconst' form always sets the value of SYMBOL to the result of
741 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
742 what is set; buffer-local values are not affected. If SYMBOL has a
743 local binding, then this form sets the local binding's value.
744 However, you should normally not make local bindings for variables
745 defined with this form.
747 The optional DOCSTRING specifies the variable's documentation string.
748 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
749 (Lisp_Object args)
751 register Lisp_Object sym, tem;
753 sym = Fcar (args);
754 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
755 error ("Too many arguments");
757 tem = eval_sub (Fcar (Fcdr (args)));
758 if (!NILP (Vpurify_flag))
759 tem = Fpurecopy (tem);
760 Fset_default (sym, tem);
761 XSYMBOL (sym)->declared_special = 1;
762 tem = Fcar (Fcdr (Fcdr (args)));
763 if (!NILP (tem))
765 if (!NILP (Vpurify_flag))
766 tem = Fpurecopy (tem);
767 Fput (sym, Qvariable_documentation, tem);
769 Fput (sym, Qrisky_local_variable, Qt);
770 LOADHIST_ATTACH (sym);
771 return sym;
774 /* Make SYMBOL lexically scoped. */
775 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
776 Smake_var_non_special, 1, 1, 0,
777 doc: /* Internal function. */)
778 (Lisp_Object symbol)
780 CHECK_SYMBOL (symbol);
781 XSYMBOL (symbol)->declared_special = 0;
782 return Qnil;
786 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
787 doc: /* Bind variables according to VARLIST then eval BODY.
788 The value of the last form in BODY is returned.
789 Each element of VARLIST is a symbol (which is bound to nil)
790 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
791 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
792 usage: (let* VARLIST BODY...) */)
793 (Lisp_Object args)
795 Lisp_Object varlist, var, val, elt, lexenv;
796 ptrdiff_t count = SPECPDL_INDEX ();
797 struct gcpro gcpro1, gcpro2, gcpro3;
799 GCPRO3 (args, elt, varlist);
801 lexenv = Vinternal_interpreter_environment;
803 varlist = Fcar (args);
804 while (CONSP (varlist))
806 QUIT;
808 elt = XCAR (varlist);
809 if (SYMBOLP (elt))
811 var = elt;
812 val = Qnil;
814 else if (! NILP (Fcdr (Fcdr (elt))))
815 signal_error ("`let' bindings can have only one value-form", elt);
816 else
818 var = Fcar (elt);
819 val = eval_sub (Fcar (Fcdr (elt)));
822 if (!NILP (lexenv) && SYMBOLP (var)
823 && !XSYMBOL (var)->declared_special
824 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
825 /* Lexically bind VAR by adding it to the interpreter's binding
826 alist. */
828 Lisp_Object newenv
829 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
830 if (EQ (Vinternal_interpreter_environment, lexenv))
831 /* Save the old lexical environment on the specpdl stack,
832 but only for the first lexical binding, since we'll never
833 need to revert to one of the intermediate ones. */
834 specbind (Qinternal_interpreter_environment, newenv);
835 else
836 Vinternal_interpreter_environment = newenv;
838 else
839 specbind (var, val);
841 varlist = XCDR (varlist);
843 UNGCPRO;
844 val = Fprogn (Fcdr (args));
845 return unbind_to (count, val);
848 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
849 doc: /* Bind variables according to VARLIST then eval BODY.
850 The value of the last form in BODY is returned.
851 Each element of VARLIST is a symbol (which is bound to nil)
852 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
853 All the VALUEFORMs are evalled before any symbols are bound.
854 usage: (let VARLIST BODY...) */)
855 (Lisp_Object args)
857 Lisp_Object *temps, tem, lexenv;
858 register Lisp_Object elt, varlist;
859 ptrdiff_t count = SPECPDL_INDEX ();
860 ptrdiff_t argnum;
861 struct gcpro gcpro1, gcpro2;
862 USE_SAFE_ALLOCA;
864 varlist = Fcar (args);
866 /* Make space to hold the values to give the bound variables. */
867 elt = Flength (varlist);
868 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
870 /* Compute the values and store them in `temps'. */
872 GCPRO2 (args, *temps);
873 gcpro2.nvars = 0;
875 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
877 QUIT;
878 elt = XCAR (varlist);
879 if (SYMBOLP (elt))
880 temps [argnum++] = Qnil;
881 else if (! NILP (Fcdr (Fcdr (elt))))
882 signal_error ("`let' bindings can have only one value-form", elt);
883 else
884 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
885 gcpro2.nvars = argnum;
887 UNGCPRO;
889 lexenv = Vinternal_interpreter_environment;
891 varlist = Fcar (args);
892 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
894 Lisp_Object var;
896 elt = XCAR (varlist);
897 var = SYMBOLP (elt) ? elt : Fcar (elt);
898 tem = temps[argnum++];
900 if (!NILP (lexenv) && SYMBOLP (var)
901 && !XSYMBOL (var)->declared_special
902 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
903 /* Lexically bind VAR by adding it to the lexenv alist. */
904 lexenv = Fcons (Fcons (var, tem), lexenv);
905 else
906 /* Dynamically bind VAR. */
907 specbind (var, tem);
910 if (!EQ (lexenv, Vinternal_interpreter_environment))
911 /* Instantiate a new lexical environment. */
912 specbind (Qinternal_interpreter_environment, lexenv);
914 elt = Fprogn (Fcdr (args));
915 SAFE_FREE ();
916 return unbind_to (count, elt);
919 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
920 doc: /* If TEST yields non-nil, eval BODY... and repeat.
921 The order of execution is thus TEST, BODY, TEST, BODY and so on
922 until TEST returns nil.
923 usage: (while TEST BODY...) */)
924 (Lisp_Object args)
926 Lisp_Object test, body;
927 struct gcpro gcpro1, gcpro2;
929 GCPRO2 (test, body);
931 test = Fcar (args);
932 body = Fcdr (args);
933 while (!NILP (eval_sub (test)))
935 QUIT;
936 Fprogn (body);
939 UNGCPRO;
940 return Qnil;
943 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
944 doc: /* Return result of expanding macros at top level of FORM.
945 If FORM is not a macro call, it is returned unchanged.
946 Otherwise, the macro is expanded and the expansion is considered
947 in place of FORM. When a non-macro-call results, it is returned.
949 The second optional arg ENVIRONMENT specifies an environment of macro
950 definitions to shadow the loaded ones for use in file byte-compilation. */)
951 (Lisp_Object form, Lisp_Object environment)
953 /* With cleanups from Hallvard Furuseth. */
954 register Lisp_Object expander, sym, def, tem;
956 while (1)
958 /* Come back here each time we expand a macro call,
959 in case it expands into another macro call. */
960 if (!CONSP (form))
961 break;
962 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
963 def = sym = XCAR (form);
964 tem = Qnil;
965 /* Trace symbols aliases to other symbols
966 until we get a symbol that is not an alias. */
967 while (SYMBOLP (def))
969 QUIT;
970 sym = def;
971 tem = Fassq (sym, environment);
972 if (NILP (tem))
974 def = XSYMBOL (sym)->function;
975 if (!EQ (def, Qunbound))
976 continue;
978 break;
980 /* Right now TEM is the result from SYM in ENVIRONMENT,
981 and if TEM is nil then DEF is SYM's function definition. */
982 if (NILP (tem))
984 /* SYM is not mentioned in ENVIRONMENT.
985 Look at its function definition. */
986 struct gcpro gcpro1;
987 GCPRO1 (form);
988 def = Fautoload_do_load (def, sym, Qmacro);
989 UNGCPRO;
990 if (EQ (def, Qunbound) || !CONSP (def))
991 /* Not defined or definition not suitable. */
992 break;
993 if (!EQ (XCAR (def), Qmacro))
994 break;
995 else expander = XCDR (def);
997 else
999 expander = XCDR (tem);
1000 if (NILP (expander))
1001 break;
1004 Lisp_Object newform = apply1 (expander, XCDR (form));
1005 if (EQ (form, newform))
1006 break;
1007 else
1008 form = newform;
1011 return form;
1014 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1015 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1016 TAG is evalled to get the tag to use; it must not be nil.
1018 Then the BODY is executed.
1019 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1020 If no throw happens, `catch' returns the value of the last BODY form.
1021 If a throw happens, it specifies the value to return from `catch'.
1022 usage: (catch TAG BODY...) */)
1023 (Lisp_Object args)
1025 register Lisp_Object tag;
1026 struct gcpro gcpro1;
1028 GCPRO1 (args);
1029 tag = eval_sub (Fcar (args));
1030 UNGCPRO;
1031 return internal_catch (tag, Fprogn, Fcdr (args));
1034 /* Set up a catch, then call C function FUNC on argument ARG.
1035 FUNC should return a Lisp_Object.
1036 This is how catches are done from within C code. */
1038 Lisp_Object
1039 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1041 /* This structure is made part of the chain `catchlist'. */
1042 struct catchtag c;
1044 /* Fill in the components of c, and put it on the list. */
1045 c.next = catchlist;
1046 c.tag = tag;
1047 c.val = Qnil;
1048 c.backlist = backtrace_list;
1049 c.handlerlist = handlerlist;
1050 c.lisp_eval_depth = lisp_eval_depth;
1051 c.pdlcount = SPECPDL_INDEX ();
1052 c.poll_suppress_count = poll_suppress_count;
1053 c.interrupt_input_blocked = interrupt_input_blocked;
1054 c.gcpro = gcprolist;
1055 c.byte_stack = byte_stack_list;
1056 catchlist = &c;
1058 /* Call FUNC. */
1059 if (! sys_setjmp (c.jmp))
1060 c.val = (*func) (arg);
1062 /* Throw works by a longjmp that comes right here. */
1063 catchlist = c.next;
1064 return c.val;
1067 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1068 jump to that CATCH, returning VALUE as the value of that catch.
1070 This is the guts of Fthrow and Fsignal; they differ only in the way
1071 they choose the catch tag to throw to. A catch tag for a
1072 condition-case form has a TAG of Qnil.
1074 Before each catch is discarded, unbind all special bindings and
1075 execute all unwind-protect clauses made above that catch. Unwind
1076 the handler stack as we go, so that the proper handlers are in
1077 effect for each unwind-protect clause we run. At the end, restore
1078 some static info saved in CATCH, and longjmp to the location
1079 specified there.
1081 This is used for correct unwinding in Fthrow and Fsignal. */
1083 static _Noreturn void
1084 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1086 bool last_time;
1088 /* Save the value in the tag. */
1089 catch->val = value;
1091 /* Restore certain special C variables. */
1092 set_poll_suppress_count (catch->poll_suppress_count);
1093 unblock_input_to (catch->interrupt_input_blocked);
1094 immediate_quit = 0;
1098 last_time = catchlist == catch;
1100 /* Unwind the specpdl stack, and then restore the proper set of
1101 handlers. */
1102 unbind_to (catchlist->pdlcount, Qnil);
1103 handlerlist = catchlist->handlerlist;
1104 catchlist = catchlist->next;
1106 while (! last_time);
1108 byte_stack_list = catch->byte_stack;
1109 gcprolist = catch->gcpro;
1110 #ifdef DEBUG_GCPRO
1111 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1112 #endif
1113 backtrace_list = catch->backlist;
1114 lisp_eval_depth = catch->lisp_eval_depth;
1116 sys_longjmp (catch->jmp, 1);
1119 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1120 doc: /* Throw to the catch for TAG and return VALUE from it.
1121 Both TAG and VALUE are evalled. */)
1122 (register Lisp_Object tag, Lisp_Object value)
1124 register struct catchtag *c;
1126 if (!NILP (tag))
1127 for (c = catchlist; c; c = c->next)
1129 if (EQ (c->tag, tag))
1130 unwind_to_catch (c, value);
1132 xsignal2 (Qno_catch, tag, value);
1136 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1137 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1138 If BODYFORM completes normally, its value is returned
1139 after executing the UNWINDFORMS.
1140 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1141 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1142 (Lisp_Object args)
1144 Lisp_Object val;
1145 ptrdiff_t count = SPECPDL_INDEX ();
1147 record_unwind_protect (Fprogn, Fcdr (args));
1148 val = eval_sub (Fcar (args));
1149 return unbind_to (count, val);
1152 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1153 doc: /* Regain control when an error is signaled.
1154 Executes BODYFORM and returns its value if no error happens.
1155 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1156 where the BODY is made of Lisp expressions.
1158 A handler is applicable to an error
1159 if CONDITION-NAME is one of the error's condition names.
1160 If an error happens, the first applicable handler is run.
1162 The car of a handler may be a list of condition names instead of a
1163 single condition name; then it handles all of them. If the special
1164 condition name `debug' is present in this list, it allows another
1165 condition in the list to run the debugger if `debug-on-error' and the
1166 other usual mechanisms says it should (otherwise, `condition-case'
1167 suppresses the debugger).
1169 When a handler handles an error, control returns to the `condition-case'
1170 and it executes the handler's BODY...
1171 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1172 \(If VAR is nil, the handler can't access that information.)
1173 Then the value of the last BODY form is returned from the `condition-case'
1174 expression.
1176 See also the function `signal' for more info.
1177 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1178 (Lisp_Object args)
1180 Lisp_Object var = Fcar (args);
1181 Lisp_Object bodyform = Fcar (Fcdr (args));
1182 Lisp_Object handlers = Fcdr (Fcdr (args));
1184 return internal_lisp_condition_case (var, bodyform, handlers);
1187 /* Like Fcondition_case, but the args are separate
1188 rather than passed in a list. Used by Fbyte_code. */
1190 Lisp_Object
1191 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1192 Lisp_Object handlers)
1194 Lisp_Object val;
1195 struct catchtag c;
1196 struct handler h;
1198 CHECK_SYMBOL (var);
1200 for (val = handlers; CONSP (val); val = XCDR (val))
1202 Lisp_Object tem;
1203 tem = XCAR (val);
1204 if (! (NILP (tem)
1205 || (CONSP (tem)
1206 && (SYMBOLP (XCAR (tem))
1207 || CONSP (XCAR (tem))))))
1208 error ("Invalid condition handler: %s",
1209 SDATA (Fprin1_to_string (tem, Qt)));
1212 c.tag = Qnil;
1213 c.val = Qnil;
1214 c.backlist = backtrace_list;
1215 c.handlerlist = handlerlist;
1216 c.lisp_eval_depth = lisp_eval_depth;
1217 c.pdlcount = SPECPDL_INDEX ();
1218 c.poll_suppress_count = poll_suppress_count;
1219 c.interrupt_input_blocked = interrupt_input_blocked;
1220 c.gcpro = gcprolist;
1221 c.byte_stack = byte_stack_list;
1222 if (sys_setjmp (c.jmp))
1224 if (!NILP (h.var))
1225 specbind (h.var, c.val);
1226 val = Fprogn (Fcdr (h.chosen_clause));
1228 /* Note that this just undoes the binding of h.var; whoever
1229 longjumped to us unwound the stack to c.pdlcount before
1230 throwing. */
1231 unbind_to (c.pdlcount, Qnil);
1232 return val;
1234 c.next = catchlist;
1235 catchlist = &c;
1237 h.var = var;
1238 h.handler = handlers;
1239 h.next = handlerlist;
1240 h.tag = &c;
1241 handlerlist = &h;
1243 val = eval_sub (bodyform);
1244 catchlist = c.next;
1245 handlerlist = h.next;
1246 return val;
1249 /* Call the function BFUN with no arguments, catching errors within it
1250 according to HANDLERS. If there is an error, call HFUN with
1251 one argument which is the data that describes the error:
1252 (SIGNALNAME . DATA)
1254 HANDLERS can be a list of conditions to catch.
1255 If HANDLERS is Qt, catch all errors.
1256 If HANDLERS is Qerror, catch all errors
1257 but allow the debugger to run if that is enabled. */
1259 Lisp_Object
1260 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1261 Lisp_Object (*hfun) (Lisp_Object))
1263 Lisp_Object val;
1264 struct catchtag c;
1265 struct handler h;
1267 c.tag = Qnil;
1268 c.val = Qnil;
1269 c.backlist = backtrace_list;
1270 c.handlerlist = handlerlist;
1271 c.lisp_eval_depth = lisp_eval_depth;
1272 c.pdlcount = SPECPDL_INDEX ();
1273 c.poll_suppress_count = poll_suppress_count;
1274 c.interrupt_input_blocked = interrupt_input_blocked;
1275 c.gcpro = gcprolist;
1276 c.byte_stack = byte_stack_list;
1277 if (sys_setjmp (c.jmp))
1279 return (*hfun) (c.val);
1281 c.next = catchlist;
1282 catchlist = &c;
1283 h.handler = handlers;
1284 h.var = Qnil;
1285 h.next = handlerlist;
1286 h.tag = &c;
1287 handlerlist = &h;
1289 val = (*bfun) ();
1290 catchlist = c.next;
1291 handlerlist = h.next;
1292 return val;
1295 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1297 Lisp_Object
1298 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1299 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1301 Lisp_Object val;
1302 struct catchtag c;
1303 struct handler h;
1305 c.tag = Qnil;
1306 c.val = Qnil;
1307 c.backlist = backtrace_list;
1308 c.handlerlist = handlerlist;
1309 c.lisp_eval_depth = lisp_eval_depth;
1310 c.pdlcount = SPECPDL_INDEX ();
1311 c.poll_suppress_count = poll_suppress_count;
1312 c.interrupt_input_blocked = interrupt_input_blocked;
1313 c.gcpro = gcprolist;
1314 c.byte_stack = byte_stack_list;
1315 if (sys_setjmp (c.jmp))
1317 return (*hfun) (c.val);
1319 c.next = catchlist;
1320 catchlist = &c;
1321 h.handler = handlers;
1322 h.var = Qnil;
1323 h.next = handlerlist;
1324 h.tag = &c;
1325 handlerlist = &h;
1327 val = (*bfun) (arg);
1328 catchlist = c.next;
1329 handlerlist = h.next;
1330 return val;
1333 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1334 its arguments. */
1336 Lisp_Object
1337 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1338 Lisp_Object arg1,
1339 Lisp_Object arg2,
1340 Lisp_Object handlers,
1341 Lisp_Object (*hfun) (Lisp_Object))
1343 Lisp_Object val;
1344 struct catchtag c;
1345 struct handler h;
1347 c.tag = Qnil;
1348 c.val = Qnil;
1349 c.backlist = backtrace_list;
1350 c.handlerlist = handlerlist;
1351 c.lisp_eval_depth = lisp_eval_depth;
1352 c.pdlcount = SPECPDL_INDEX ();
1353 c.poll_suppress_count = poll_suppress_count;
1354 c.interrupt_input_blocked = interrupt_input_blocked;
1355 c.gcpro = gcprolist;
1356 c.byte_stack = byte_stack_list;
1357 if (sys_setjmp (c.jmp))
1359 return (*hfun) (c.val);
1361 c.next = catchlist;
1362 catchlist = &c;
1363 h.handler = handlers;
1364 h.var = Qnil;
1365 h.next = handlerlist;
1366 h.tag = &c;
1367 handlerlist = &h;
1369 val = (*bfun) (arg1, arg2);
1370 catchlist = c.next;
1371 handlerlist = h.next;
1372 return val;
1375 /* Like internal_condition_case but call BFUN with NARGS as first,
1376 and ARGS as second argument. */
1378 Lisp_Object
1379 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1380 ptrdiff_t nargs,
1381 Lisp_Object *args,
1382 Lisp_Object handlers,
1383 Lisp_Object (*hfun) (Lisp_Object err,
1384 ptrdiff_t nargs,
1385 Lisp_Object *args))
1387 Lisp_Object val;
1388 struct catchtag c;
1389 struct handler h;
1391 c.tag = Qnil;
1392 c.val = Qnil;
1393 c.backlist = backtrace_list;
1394 c.handlerlist = handlerlist;
1395 c.lisp_eval_depth = lisp_eval_depth;
1396 c.pdlcount = SPECPDL_INDEX ();
1397 c.poll_suppress_count = poll_suppress_count;
1398 c.interrupt_input_blocked = interrupt_input_blocked;
1399 c.gcpro = gcprolist;
1400 c.byte_stack = byte_stack_list;
1401 if (sys_setjmp (c.jmp))
1403 return (*hfun) (c.val, nargs, args);
1405 c.next = catchlist;
1406 catchlist = &c;
1407 h.handler = handlers;
1408 h.var = Qnil;
1409 h.next = handlerlist;
1410 h.tag = &c;
1411 handlerlist = &h;
1413 val = (*bfun) (nargs, args);
1414 catchlist = c.next;
1415 handlerlist = h.next;
1416 return val;
1420 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1421 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1422 Lisp_Object data);
1424 void
1425 process_quit_flag (void)
1427 Lisp_Object flag = Vquit_flag;
1428 Vquit_flag = Qnil;
1429 if (EQ (flag, Qkill_emacs))
1430 Fkill_emacs (Qnil);
1431 if (EQ (Vthrow_on_input, flag))
1432 Fthrow (Vthrow_on_input, Qt);
1433 Fsignal (Qquit, Qnil);
1436 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1437 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1438 This function does not return.
1440 An error symbol is a symbol with an `error-conditions' property
1441 that is a list of condition names.
1442 A handler for any of those names will get to handle this signal.
1443 The symbol `error' should normally be one of them.
1445 DATA should be a list. Its elements are printed as part of the error message.
1446 See Info anchor `(elisp)Definition of signal' for some details on how this
1447 error message is constructed.
1448 If the signal is handled, DATA is made available to the handler.
1449 See also the function `condition-case'. */)
1450 (Lisp_Object error_symbol, Lisp_Object data)
1452 /* When memory is full, ERROR-SYMBOL is nil,
1453 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1454 That is a special case--don't do this in other situations. */
1455 Lisp_Object conditions;
1456 Lisp_Object string;
1457 Lisp_Object real_error_symbol
1458 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1459 register Lisp_Object clause = Qnil;
1460 struct handler *h;
1461 struct backtrace *bp;
1463 immediate_quit = 0;
1464 abort_on_gc = 0;
1465 if (gc_in_progress || waiting_for_input)
1466 emacs_abort ();
1468 #if 0 /* rms: I don't know why this was here,
1469 but it is surely wrong for an error that is handled. */
1470 #ifdef HAVE_WINDOW_SYSTEM
1471 if (display_hourglass_p)
1472 cancel_hourglass ();
1473 #endif
1474 #endif
1476 /* This hook is used by edebug. */
1477 if (! NILP (Vsignal_hook_function)
1478 && ! NILP (error_symbol))
1480 /* Edebug takes care of restoring these variables when it exits. */
1481 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1482 max_lisp_eval_depth = lisp_eval_depth + 20;
1484 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1485 max_specpdl_size = SPECPDL_INDEX () + 40;
1487 call2 (Vsignal_hook_function, error_symbol, data);
1490 conditions = Fget (real_error_symbol, Qerror_conditions);
1492 /* Remember from where signal was called. Skip over the frame for
1493 `signal' itself. If a frame for `error' follows, skip that,
1494 too. Don't do this when ERROR_SYMBOL is nil, because that
1495 is a memory-full error. */
1496 Vsignaling_function = Qnil;
1497 if (backtrace_list && !NILP (error_symbol))
1499 bp = backtrace_list->next;
1500 if (bp && EQ (bp->function, Qerror))
1501 bp = bp->next;
1502 if (bp)
1503 Vsignaling_function = bp->function;
1506 for (h = handlerlist; h; h = h->next)
1508 clause = find_handler_clause (h->handler, conditions);
1509 if (!NILP (clause))
1510 break;
1513 if (/* Don't run the debugger for a memory-full error.
1514 (There is no room in memory to do that!) */
1515 !NILP (error_symbol)
1516 && (!NILP (Vdebug_on_signal)
1517 /* If no handler is present now, try to run the debugger. */
1518 || NILP (clause)
1519 /* A `debug' symbol in the handler list disables the normal
1520 suppression of the debugger. */
1521 || (CONSP (clause) && CONSP (XCAR (clause))
1522 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1523 /* Special handler that means "print a message and run debugger
1524 if requested". */
1525 || EQ (h->handler, Qerror)))
1527 bool debugger_called
1528 = maybe_call_debugger (conditions, error_symbol, data);
1529 /* We can't return values to code which signaled an error, but we
1530 can continue code which has signaled a quit. */
1531 if (debugger_called && EQ (real_error_symbol, Qquit))
1532 return Qnil;
1535 if (!NILP (clause))
1537 Lisp_Object unwind_data
1538 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1540 h->chosen_clause = clause;
1541 unwind_to_catch (h->tag, unwind_data);
1543 else
1545 if (catchlist != 0)
1546 Fthrow (Qtop_level, Qt);
1549 if (! NILP (error_symbol))
1550 data = Fcons (error_symbol, data);
1552 string = Ferror_message_string (data);
1553 fatal ("%s", SDATA (string));
1556 /* Internal version of Fsignal that never returns.
1557 Used for anything but Qquit (which can return from Fsignal). */
1559 void
1560 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1562 Fsignal (error_symbol, data);
1563 emacs_abort ();
1566 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1568 void
1569 xsignal0 (Lisp_Object error_symbol)
1571 xsignal (error_symbol, Qnil);
1574 void
1575 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1577 xsignal (error_symbol, list1 (arg));
1580 void
1581 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1583 xsignal (error_symbol, list2 (arg1, arg2));
1586 void
1587 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1589 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1592 /* Signal `error' with message S, and additional arg ARG.
1593 If ARG is not a genuine list, make it a one-element list. */
1595 void
1596 signal_error (const char *s, Lisp_Object arg)
1598 Lisp_Object tortoise, hare;
1600 hare = tortoise = arg;
1601 while (CONSP (hare))
1603 hare = XCDR (hare);
1604 if (!CONSP (hare))
1605 break;
1607 hare = XCDR (hare);
1608 tortoise = XCDR (tortoise);
1610 if (EQ (hare, tortoise))
1611 break;
1614 if (!NILP (hare))
1615 arg = Fcons (arg, Qnil); /* Make it a list. */
1617 xsignal (Qerror, Fcons (build_string (s), arg));
1621 /* Return true if LIST is a non-nil atom or
1622 a list containing one of CONDITIONS. */
1624 static bool
1625 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1627 if (NILP (list))
1628 return 0;
1629 if (! CONSP (list))
1630 return 1;
1632 while (CONSP (conditions))
1634 Lisp_Object this, tail;
1635 this = XCAR (conditions);
1636 for (tail = list; CONSP (tail); tail = XCDR (tail))
1637 if (EQ (XCAR (tail), this))
1638 return 1;
1639 conditions = XCDR (conditions);
1641 return 0;
1644 /* Return true if an error with condition-symbols CONDITIONS,
1645 and described by SIGNAL-DATA, should skip the debugger
1646 according to debugger-ignored-errors. */
1648 static bool
1649 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1651 Lisp_Object tail;
1652 bool first_string = 1;
1653 Lisp_Object error_message;
1655 error_message = Qnil;
1656 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1658 if (STRINGP (XCAR (tail)))
1660 if (first_string)
1662 error_message = Ferror_message_string (data);
1663 first_string = 0;
1666 if (fast_string_match (XCAR (tail), error_message) >= 0)
1667 return 1;
1669 else
1671 Lisp_Object contail;
1673 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1674 if (EQ (XCAR (tail), XCAR (contail)))
1675 return 1;
1679 return 0;
1682 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1683 SIG and DATA describe the signal. There are two ways to pass them:
1684 = SIG is the error symbol, and DATA is the rest of the data.
1685 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1686 This is for memory-full errors only. */
1687 static bool
1688 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1690 Lisp_Object combined_data;
1692 combined_data = Fcons (sig, data);
1694 if (
1695 /* Don't try to run the debugger with interrupts blocked.
1696 The editing loop would return anyway. */
1697 ! input_blocked_p ()
1698 && NILP (Vinhibit_debugger)
1699 /* Does user want to enter debugger for this kind of error? */
1700 && (EQ (sig, Qquit)
1701 ? debug_on_quit
1702 : wants_debugger (Vdebug_on_error, conditions))
1703 && ! skip_debugger (conditions, combined_data)
1704 /* RMS: What's this for? */
1705 && when_entered_debugger < num_nonmacro_input_events)
1707 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1708 return 1;
1711 return 0;
1714 static Lisp_Object
1715 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1717 register Lisp_Object h;
1719 /* t is used by handlers for all conditions, set up by C code. */
1720 if (EQ (handlers, Qt))
1721 return Qt;
1723 /* error is used similarly, but means print an error message
1724 and run the debugger if that is enabled. */
1725 if (EQ (handlers, Qerror))
1726 return Qt;
1728 for (h = handlers; CONSP (h); h = XCDR (h))
1730 Lisp_Object handler = XCAR (h);
1731 Lisp_Object condit, tem;
1733 if (!CONSP (handler))
1734 continue;
1735 condit = XCAR (handler);
1736 /* Handle a single condition name in handler HANDLER. */
1737 if (SYMBOLP (condit))
1739 tem = Fmemq (Fcar (handler), conditions);
1740 if (!NILP (tem))
1741 return handler;
1743 /* Handle a list of condition names in handler HANDLER. */
1744 else if (CONSP (condit))
1746 Lisp_Object tail;
1747 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1749 tem = Fmemq (XCAR (tail), conditions);
1750 if (!NILP (tem))
1751 return handler;
1756 return Qnil;
1760 /* Dump an error message; called like vprintf. */
1761 void
1762 verror (const char *m, va_list ap)
1764 char buf[4000];
1765 ptrdiff_t size = sizeof buf;
1766 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1767 char *buffer = buf;
1768 ptrdiff_t used;
1769 Lisp_Object string;
1771 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1772 string = make_string (buffer, used);
1773 if (buffer != buf)
1774 xfree (buffer);
1776 xsignal1 (Qerror, string);
1780 /* Dump an error message; called like printf. */
1782 /* VARARGS 1 */
1783 void
1784 error (const char *m, ...)
1786 va_list ap;
1787 va_start (ap, m);
1788 verror (m, ap);
1789 va_end (ap);
1792 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1793 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1794 This means it contains a description for how to read arguments to give it.
1795 The value is nil for an invalid function or a symbol with no function
1796 definition.
1798 Interactively callable functions include strings and vectors (treated
1799 as keyboard macros), lambda-expressions that contain a top-level call
1800 to `interactive', autoload definitions made by `autoload' with non-nil
1801 fourth argument, and some of the built-in functions of Lisp.
1803 Also, a symbol satisfies `commandp' if its function definition does so.
1805 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1806 then strings and vectors are not accepted. */)
1807 (Lisp_Object function, Lisp_Object for_call_interactively)
1809 register Lisp_Object fun;
1810 register Lisp_Object funcar;
1811 Lisp_Object if_prop = Qnil;
1813 fun = function;
1815 fun = indirect_function (fun); /* Check cycles. */
1816 if (NILP (fun) || EQ (fun, Qunbound))
1817 return Qnil;
1819 /* Check an `interactive-form' property if present, analogous to the
1820 function-documentation property. */
1821 fun = function;
1822 while (SYMBOLP (fun))
1824 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1825 if (!NILP (tmp))
1826 if_prop = Qt;
1827 fun = Fsymbol_function (fun);
1830 /* Emacs primitives are interactive if their DEFUN specifies an
1831 interactive spec. */
1832 if (SUBRP (fun))
1833 return XSUBR (fun)->intspec ? Qt : if_prop;
1835 /* Bytecode objects are interactive if they are long enough to
1836 have an element whose index is COMPILED_INTERACTIVE, which is
1837 where the interactive spec is stored. */
1838 else if (COMPILEDP (fun))
1839 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1840 ? Qt : if_prop);
1842 /* Strings and vectors are keyboard macros. */
1843 if (STRINGP (fun) || VECTORP (fun))
1844 return (NILP (for_call_interactively) ? Qt : Qnil);
1846 /* Lists may represent commands. */
1847 if (!CONSP (fun))
1848 return Qnil;
1849 funcar = XCAR (fun);
1850 if (EQ (funcar, Qclosure))
1851 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1852 ? Qt : if_prop);
1853 else if (EQ (funcar, Qlambda))
1854 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1855 else if (EQ (funcar, Qautoload))
1856 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1857 else
1858 return Qnil;
1861 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1862 doc: /* Define FUNCTION to autoload from FILE.
1863 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1864 Third arg DOCSTRING is documentation for the function.
1865 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1866 Fifth arg TYPE indicates the type of the object:
1867 nil or omitted says FUNCTION is a function,
1868 `keymap' says FUNCTION is really a keymap, and
1869 `macro' or t says FUNCTION is really a macro.
1870 Third through fifth args give info about the real definition.
1871 They default to nil.
1872 If FUNCTION is already defined other than as an autoload,
1873 this does nothing and returns nil. */)
1874 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1876 CHECK_SYMBOL (function);
1877 CHECK_STRING (file);
1879 /* If function is defined and not as an autoload, don't override. */
1880 if ((CONSP (XSYMBOL (function)->function)
1881 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1882 /* Remember that the function was already an autoload. */
1883 LOADHIST_ATTACH (Fcons (Qt, function));
1884 else if (!EQ (XSYMBOL (function)->function, Qunbound))
1885 return Qnil;
1887 if (NILP (Vpurify_flag))
1888 /* Only add entries after dumping, because the ones before are
1889 not useful and else we get loads of them from the loaddefs.el. */
1890 LOADHIST_ATTACH (Fcons (Qautoload, function));
1891 else if (EQ (docstring, make_number (0)))
1892 /* `read1' in lread.c has found the docstring starting with "\
1893 and assumed the docstring will be provided by Snarf-documentation, so it
1894 passed us 0 instead. But that leads to accidental sharing in purecopy's
1895 hash-consing, so we use a (hopefully) unique integer instead. */
1896 docstring = make_number (XUNTAG (function, Lisp_Symbol));
1897 return Ffset (function,
1898 Fpurecopy (list5 (Qautoload, file, docstring,
1899 interactive, type)));
1902 Lisp_Object
1903 un_autoload (Lisp_Object oldqueue)
1905 register Lisp_Object queue, first, second;
1907 /* Queue to unwind is current value of Vautoload_queue.
1908 oldqueue is the shadowed value to leave in Vautoload_queue. */
1909 queue = Vautoload_queue;
1910 Vautoload_queue = oldqueue;
1911 while (CONSP (queue))
1913 first = XCAR (queue);
1914 second = Fcdr (first);
1915 first = Fcar (first);
1916 if (EQ (first, make_number (0)))
1917 Vfeatures = second;
1918 else
1919 Ffset (first, second);
1920 queue = XCDR (queue);
1922 return Qnil;
1925 /* Load an autoloaded function.
1926 FUNNAME is the symbol which is the function's name.
1927 FUNDEF is the autoload definition (a list). */
1929 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1930 doc: /* Load FUNDEF which should be an autoload.
1931 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1932 in which case the function returns the new autoloaded function value.
1933 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1934 it is defines a macro. */)
1935 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1937 ptrdiff_t count = SPECPDL_INDEX ();
1938 struct gcpro gcpro1, gcpro2, gcpro3;
1940 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1941 return fundef;
1943 if (EQ (macro_only, Qmacro))
1945 Lisp_Object kind = Fnth (make_number (4), fundef);
1946 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1947 return fundef;
1950 /* This is to make sure that loadup.el gives a clear picture
1951 of what files are preloaded and when. */
1952 if (! NILP (Vpurify_flag))
1953 error ("Attempt to autoload %s while preparing to dump",
1954 SDATA (SYMBOL_NAME (funname)));
1956 CHECK_SYMBOL (funname);
1957 GCPRO3 (funname, fundef, macro_only);
1959 /* Preserve the match data. */
1960 record_unwind_save_match_data ();
1962 /* If autoloading gets an error (which includes the error of failing
1963 to define the function being called), we use Vautoload_queue
1964 to undo function definitions and `provide' calls made by
1965 the function. We do this in the specific case of autoloading
1966 because autoloading is not an explicit request "load this file",
1967 but rather a request to "call this function".
1969 The value saved here is to be restored into Vautoload_queue. */
1970 record_unwind_protect (un_autoload, Vautoload_queue);
1971 Vautoload_queue = Qt;
1972 /* If `macro_only', assume this autoload to be a "best-effort",
1973 so don't signal an error if autoloading fails. */
1974 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1976 /* Once loading finishes, don't undo it. */
1977 Vautoload_queue = Qt;
1978 unbind_to (count, Qnil);
1980 UNGCPRO;
1982 if (NILP (funname))
1983 return Qnil;
1984 else
1986 Lisp_Object fun = Findirect_function (funname, Qnil);
1988 if (!NILP (Fequal (fun, fundef)))
1989 error ("Autoloading failed to define function %s",
1990 SDATA (SYMBOL_NAME (funname)));
1991 else
1992 return fun;
1997 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1998 doc: /* Evaluate FORM and return its value.
1999 If LEXICAL is t, evaluate using lexical scoping. */)
2000 (Lisp_Object form, Lisp_Object lexical)
2002 ptrdiff_t count = SPECPDL_INDEX ();
2003 specbind (Qinternal_interpreter_environment,
2004 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2005 return unbind_to (count, eval_sub (form));
2008 /* Eval a sub-expression of the current expression (i.e. in the same
2009 lexical scope). */
2010 Lisp_Object
2011 eval_sub (Lisp_Object form)
2013 Lisp_Object fun, val, original_fun, original_args;
2014 Lisp_Object funcar;
2015 struct backtrace backtrace;
2016 struct gcpro gcpro1, gcpro2, gcpro3;
2018 if (SYMBOLP (form))
2020 /* Look up its binding in the lexical environment.
2021 We do not pay attention to the declared_special flag here, since we
2022 already did that when let-binding the variable. */
2023 Lisp_Object lex_binding
2024 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2025 ? Fassq (form, Vinternal_interpreter_environment)
2026 : Qnil;
2027 if (CONSP (lex_binding))
2028 return XCDR (lex_binding);
2029 else
2030 return Fsymbol_value (form);
2033 if (!CONSP (form))
2034 return form;
2036 QUIT;
2038 GCPRO1 (form);
2039 maybe_gc ();
2040 UNGCPRO;
2042 if (++lisp_eval_depth > max_lisp_eval_depth)
2044 if (max_lisp_eval_depth < 100)
2045 max_lisp_eval_depth = 100;
2046 if (lisp_eval_depth > max_lisp_eval_depth)
2047 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2050 original_fun = XCAR (form);
2051 original_args = XCDR (form);
2053 backtrace.next = backtrace_list;
2054 backtrace.function = original_fun; /* This also protects them from gc. */
2055 backtrace.args = &original_args;
2056 backtrace.nargs = UNEVALLED;
2057 backtrace.debug_on_exit = 0;
2058 backtrace_list = &backtrace;
2060 if (debug_on_next_call)
2061 do_debug_on_call (Qt);
2063 /* At this point, only original_fun and original_args
2064 have values that will be used below. */
2065 retry:
2067 /* Optimize for no indirection. */
2068 fun = original_fun;
2069 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2070 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2071 fun = indirect_function (fun);
2073 if (SUBRP (fun))
2075 Lisp_Object numargs;
2076 Lisp_Object argvals[8];
2077 Lisp_Object args_left;
2078 register int i, maxargs;
2080 args_left = original_args;
2081 numargs = Flength (args_left);
2083 check_cons_list ();
2085 if (XINT (numargs) < XSUBR (fun)->min_args
2086 || (XSUBR (fun)->max_args >= 0
2087 && XSUBR (fun)->max_args < XINT (numargs)))
2088 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2090 else if (XSUBR (fun)->max_args == UNEVALLED)
2091 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2092 else if (XSUBR (fun)->max_args == MANY)
2094 /* Pass a vector of evaluated arguments. */
2095 Lisp_Object *vals;
2096 ptrdiff_t argnum = 0;
2097 USE_SAFE_ALLOCA;
2099 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2101 GCPRO3 (args_left, fun, fun);
2102 gcpro3.var = vals;
2103 gcpro3.nvars = 0;
2105 while (!NILP (args_left))
2107 vals[argnum++] = eval_sub (Fcar (args_left));
2108 args_left = Fcdr (args_left);
2109 gcpro3.nvars = argnum;
2112 backtrace.args = vals;
2113 backtrace.nargs = XINT (numargs);
2115 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2116 UNGCPRO;
2117 SAFE_FREE ();
2119 else
2121 GCPRO3 (args_left, fun, fun);
2122 gcpro3.var = argvals;
2123 gcpro3.nvars = 0;
2125 maxargs = XSUBR (fun)->max_args;
2126 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2128 argvals[i] = eval_sub (Fcar (args_left));
2129 gcpro3.nvars = ++i;
2132 UNGCPRO;
2134 backtrace.args = argvals;
2135 backtrace.nargs = XINT (numargs);
2137 switch (i)
2139 case 0:
2140 val = (XSUBR (fun)->function.a0 ());
2141 break;
2142 case 1:
2143 val = (XSUBR (fun)->function.a1 (argvals[0]));
2144 break;
2145 case 2:
2146 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2147 break;
2148 case 3:
2149 val = (XSUBR (fun)->function.a3
2150 (argvals[0], argvals[1], argvals[2]));
2151 break;
2152 case 4:
2153 val = (XSUBR (fun)->function.a4
2154 (argvals[0], argvals[1], argvals[2], argvals[3]));
2155 break;
2156 case 5:
2157 val = (XSUBR (fun)->function.a5
2158 (argvals[0], argvals[1], argvals[2], argvals[3],
2159 argvals[4]));
2160 break;
2161 case 6:
2162 val = (XSUBR (fun)->function.a6
2163 (argvals[0], argvals[1], argvals[2], argvals[3],
2164 argvals[4], argvals[5]));
2165 break;
2166 case 7:
2167 val = (XSUBR (fun)->function.a7
2168 (argvals[0], argvals[1], argvals[2], argvals[3],
2169 argvals[4], argvals[5], argvals[6]));
2170 break;
2172 case 8:
2173 val = (XSUBR (fun)->function.a8
2174 (argvals[0], argvals[1], argvals[2], argvals[3],
2175 argvals[4], argvals[5], argvals[6], argvals[7]));
2176 break;
2178 default:
2179 /* Someone has created a subr that takes more arguments than
2180 is supported by this code. We need to either rewrite the
2181 subr to use a different argument protocol, or add more
2182 cases to this switch. */
2183 emacs_abort ();
2187 else if (COMPILEDP (fun))
2188 val = apply_lambda (fun, original_args);
2189 else
2191 if (EQ (fun, Qunbound))
2192 xsignal1 (Qvoid_function, original_fun);
2193 if (!CONSP (fun))
2194 xsignal1 (Qinvalid_function, original_fun);
2195 funcar = XCAR (fun);
2196 if (!SYMBOLP (funcar))
2197 xsignal1 (Qinvalid_function, original_fun);
2198 if (EQ (funcar, Qautoload))
2200 Fautoload_do_load (fun, original_fun, Qnil);
2201 goto retry;
2203 if (EQ (funcar, Qmacro))
2205 ptrdiff_t count = SPECPDL_INDEX ();
2206 Lisp_Object exp;
2207 /* Bind lexical-binding during expansion of the macro, so the
2208 macro can know reliably if the code it outputs will be
2209 interpreted using lexical-binding or not. */
2210 specbind (Qlexical_binding,
2211 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2212 exp = apply1 (Fcdr (fun), original_args);
2213 unbind_to (count, Qnil);
2214 val = eval_sub (exp);
2216 else if (EQ (funcar, Qlambda)
2217 || EQ (funcar, Qclosure))
2218 val = apply_lambda (fun, original_args);
2219 else
2220 xsignal1 (Qinvalid_function, original_fun);
2222 check_cons_list ();
2224 lisp_eval_depth--;
2225 if (backtrace.debug_on_exit)
2226 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2227 backtrace_list = backtrace.next;
2229 return val;
2232 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2233 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2234 Then return the value FUNCTION returns.
2235 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2236 usage: (apply FUNCTION &rest ARGUMENTS) */)
2237 (ptrdiff_t nargs, Lisp_Object *args)
2239 ptrdiff_t i;
2240 EMACS_INT numargs;
2241 register Lisp_Object spread_arg;
2242 register Lisp_Object *funcall_args;
2243 Lisp_Object fun, retval;
2244 struct gcpro gcpro1;
2245 USE_SAFE_ALLOCA;
2247 fun = args [0];
2248 funcall_args = 0;
2249 spread_arg = args [nargs - 1];
2250 CHECK_LIST (spread_arg);
2252 numargs = XINT (Flength (spread_arg));
2254 if (numargs == 0)
2255 return Ffuncall (nargs - 1, args);
2256 else if (numargs == 1)
2258 args [nargs - 1] = XCAR (spread_arg);
2259 return Ffuncall (nargs, args);
2262 numargs += nargs - 2;
2264 /* Optimize for no indirection. */
2265 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2266 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2267 fun = indirect_function (fun);
2268 if (EQ (fun, Qunbound))
2270 /* Let funcall get the error. */
2271 fun = args[0];
2272 goto funcall;
2275 if (SUBRP (fun))
2277 if (numargs < XSUBR (fun)->min_args
2278 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2279 goto funcall; /* Let funcall get the error. */
2280 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2282 /* Avoid making funcall cons up a yet another new vector of arguments
2283 by explicitly supplying nil's for optional values. */
2284 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2285 for (i = numargs; i < XSUBR (fun)->max_args;)
2286 funcall_args[++i] = Qnil;
2287 GCPRO1 (*funcall_args);
2288 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2291 funcall:
2292 /* We add 1 to numargs because funcall_args includes the
2293 function itself as well as its arguments. */
2294 if (!funcall_args)
2296 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2297 GCPRO1 (*funcall_args);
2298 gcpro1.nvars = 1 + numargs;
2301 memcpy (funcall_args, args, nargs * word_size);
2302 /* Spread the last arg we got. Its first element goes in
2303 the slot that it used to occupy, hence this value of I. */
2304 i = nargs - 1;
2305 while (!NILP (spread_arg))
2307 funcall_args [i++] = XCAR (spread_arg);
2308 spread_arg = XCDR (spread_arg);
2311 /* By convention, the caller needs to gcpro Ffuncall's args. */
2312 retval = Ffuncall (gcpro1.nvars, funcall_args);
2313 UNGCPRO;
2314 SAFE_FREE ();
2316 return retval;
2319 /* Run hook variables in various ways. */
2321 static Lisp_Object
2322 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2324 Ffuncall (nargs, args);
2325 return Qnil;
2328 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2329 doc: /* Run each hook in HOOKS.
2330 Each argument should be a symbol, a hook variable.
2331 These symbols are processed in the order specified.
2332 If a hook symbol has a non-nil value, that value may be a function
2333 or a list of functions to be called to run the hook.
2334 If the value is a function, it is called with no arguments.
2335 If it is a list, the elements are called, in order, with no arguments.
2337 Major modes should not use this function directly to run their mode
2338 hook; they should use `run-mode-hooks' instead.
2340 Do not use `make-local-variable' to make a hook variable buffer-local.
2341 Instead, use `add-hook' and specify t for the LOCAL argument.
2342 usage: (run-hooks &rest HOOKS) */)
2343 (ptrdiff_t nargs, Lisp_Object *args)
2345 Lisp_Object hook[1];
2346 ptrdiff_t i;
2348 for (i = 0; i < nargs; i++)
2350 hook[0] = args[i];
2351 run_hook_with_args (1, hook, funcall_nil);
2354 return Qnil;
2357 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2358 Srun_hook_with_args, 1, MANY, 0,
2359 doc: /* Run HOOK with the specified arguments ARGS.
2360 HOOK should be a symbol, a hook variable. The value of HOOK
2361 may be nil, a function, or a list of functions. Call each
2362 function in order with arguments ARGS. The final return value
2363 is unspecified.
2365 Do not use `make-local-variable' to make a hook variable buffer-local.
2366 Instead, use `add-hook' and specify t for the LOCAL argument.
2367 usage: (run-hook-with-args HOOK &rest ARGS) */)
2368 (ptrdiff_t nargs, Lisp_Object *args)
2370 return run_hook_with_args (nargs, args, funcall_nil);
2373 /* NB this one still documents a specific non-nil return value.
2374 (As did run-hook-with-args and run-hook-with-args-until-failure
2375 until they were changed in 24.1.) */
2376 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2377 Srun_hook_with_args_until_success, 1, MANY, 0,
2378 doc: /* Run HOOK with the specified arguments ARGS.
2379 HOOK should be a symbol, a hook variable. The value of HOOK
2380 may be nil, a function, or a list of functions. Call each
2381 function in order with arguments ARGS, stopping at the first
2382 one that returns non-nil, and return that value. Otherwise (if
2383 all functions return nil, or if there are no functions to call),
2384 return nil.
2386 Do not use `make-local-variable' to make a hook variable buffer-local.
2387 Instead, use `add-hook' and specify t for the LOCAL argument.
2388 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2389 (ptrdiff_t nargs, Lisp_Object *args)
2391 return run_hook_with_args (nargs, args, Ffuncall);
2394 static Lisp_Object
2395 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2397 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2400 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2401 Srun_hook_with_args_until_failure, 1, MANY, 0,
2402 doc: /* Run HOOK with the specified arguments ARGS.
2403 HOOK should be a symbol, a hook variable. The value of HOOK
2404 may be nil, a function, or a list of functions. Call each
2405 function in order with arguments ARGS, stopping at the first
2406 one that returns nil, and return nil. Otherwise (if all functions
2407 return non-nil, or if there are no functions to call), return non-nil
2408 \(do not rely on the precise return value in this case).
2410 Do not use `make-local-variable' to make a hook variable buffer-local.
2411 Instead, use `add-hook' and specify t for the LOCAL argument.
2412 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2413 (ptrdiff_t nargs, Lisp_Object *args)
2415 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2418 static Lisp_Object
2419 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2421 Lisp_Object tmp = args[0], ret;
2422 args[0] = args[1];
2423 args[1] = tmp;
2424 ret = Ffuncall (nargs, args);
2425 args[1] = args[0];
2426 args[0] = tmp;
2427 return ret;
2430 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2431 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2432 I.e. instead of calling each function FUN directly with arguments ARGS,
2433 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2434 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2435 aborts and returns that value.
2436 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2437 (ptrdiff_t nargs, Lisp_Object *args)
2439 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2442 /* ARGS[0] should be a hook symbol.
2443 Call each of the functions in the hook value, passing each of them
2444 as arguments all the rest of ARGS (all NARGS - 1 elements).
2445 FUNCALL specifies how to call each function on the hook.
2446 The caller (or its caller, etc) must gcpro all of ARGS,
2447 except that it isn't necessary to gcpro ARGS[0]. */
2449 Lisp_Object
2450 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2451 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2453 Lisp_Object sym, val, ret = Qnil;
2454 struct gcpro gcpro1, gcpro2, gcpro3;
2456 /* If we are dying or still initializing,
2457 don't do anything--it would probably crash if we tried. */
2458 if (NILP (Vrun_hooks))
2459 return Qnil;
2461 sym = args[0];
2462 val = find_symbol_value (sym);
2464 if (EQ (val, Qunbound) || NILP (val))
2465 return ret;
2466 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2468 args[0] = val;
2469 return funcall (nargs, args);
2471 else
2473 Lisp_Object global_vals = Qnil;
2474 GCPRO3 (sym, val, global_vals);
2476 for (;
2477 CONSP (val) && NILP (ret);
2478 val = XCDR (val))
2480 if (EQ (XCAR (val), Qt))
2482 /* t indicates this hook has a local binding;
2483 it means to run the global binding too. */
2484 global_vals = Fdefault_value (sym);
2485 if (NILP (global_vals)) continue;
2487 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2489 args[0] = global_vals;
2490 ret = funcall (nargs, args);
2492 else
2494 for (;
2495 CONSP (global_vals) && NILP (ret);
2496 global_vals = XCDR (global_vals))
2498 args[0] = XCAR (global_vals);
2499 /* In a global value, t should not occur. If it does, we
2500 must ignore it to avoid an endless loop. */
2501 if (!EQ (args[0], Qt))
2502 ret = funcall (nargs, args);
2506 else
2508 args[0] = XCAR (val);
2509 ret = funcall (nargs, args);
2513 UNGCPRO;
2514 return ret;
2518 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2520 void
2521 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2523 Lisp_Object temp[3];
2524 temp[0] = hook;
2525 temp[1] = arg1;
2526 temp[2] = arg2;
2528 Frun_hook_with_args (3, temp);
2531 /* Apply fn to arg. */
2532 Lisp_Object
2533 apply1 (Lisp_Object fn, Lisp_Object arg)
2535 struct gcpro gcpro1;
2537 GCPRO1 (fn);
2538 if (NILP (arg))
2539 RETURN_UNGCPRO (Ffuncall (1, &fn));
2540 gcpro1.nvars = 2;
2542 Lisp_Object args[2];
2543 args[0] = fn;
2544 args[1] = arg;
2545 gcpro1.var = args;
2546 RETURN_UNGCPRO (Fapply (2, args));
2550 /* Call function fn on no arguments. */
2551 Lisp_Object
2552 call0 (Lisp_Object fn)
2554 struct gcpro gcpro1;
2556 GCPRO1 (fn);
2557 RETURN_UNGCPRO (Ffuncall (1, &fn));
2560 /* Call function fn with 1 argument arg1. */
2561 /* ARGSUSED */
2562 Lisp_Object
2563 call1 (Lisp_Object fn, Lisp_Object arg1)
2565 struct gcpro gcpro1;
2566 Lisp_Object args[2];
2568 args[0] = fn;
2569 args[1] = arg1;
2570 GCPRO1 (args[0]);
2571 gcpro1.nvars = 2;
2572 RETURN_UNGCPRO (Ffuncall (2, args));
2575 /* Call function fn with 2 arguments arg1, arg2. */
2576 /* ARGSUSED */
2577 Lisp_Object
2578 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2580 struct gcpro gcpro1;
2581 Lisp_Object args[3];
2582 args[0] = fn;
2583 args[1] = arg1;
2584 args[2] = arg2;
2585 GCPRO1 (args[0]);
2586 gcpro1.nvars = 3;
2587 RETURN_UNGCPRO (Ffuncall (3, args));
2590 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2591 /* ARGSUSED */
2592 Lisp_Object
2593 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2595 struct gcpro gcpro1;
2596 Lisp_Object args[4];
2597 args[0] = fn;
2598 args[1] = arg1;
2599 args[2] = arg2;
2600 args[3] = arg3;
2601 GCPRO1 (args[0]);
2602 gcpro1.nvars = 4;
2603 RETURN_UNGCPRO (Ffuncall (4, args));
2606 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2607 /* ARGSUSED */
2608 Lisp_Object
2609 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2610 Lisp_Object arg4)
2612 struct gcpro gcpro1;
2613 Lisp_Object args[5];
2614 args[0] = fn;
2615 args[1] = arg1;
2616 args[2] = arg2;
2617 args[3] = arg3;
2618 args[4] = arg4;
2619 GCPRO1 (args[0]);
2620 gcpro1.nvars = 5;
2621 RETURN_UNGCPRO (Ffuncall (5, args));
2624 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2625 /* ARGSUSED */
2626 Lisp_Object
2627 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2628 Lisp_Object arg4, Lisp_Object arg5)
2630 struct gcpro gcpro1;
2631 Lisp_Object args[6];
2632 args[0] = fn;
2633 args[1] = arg1;
2634 args[2] = arg2;
2635 args[3] = arg3;
2636 args[4] = arg4;
2637 args[5] = arg5;
2638 GCPRO1 (args[0]);
2639 gcpro1.nvars = 6;
2640 RETURN_UNGCPRO (Ffuncall (6, args));
2643 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2644 /* ARGSUSED */
2645 Lisp_Object
2646 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2647 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2649 struct gcpro gcpro1;
2650 Lisp_Object args[7];
2651 args[0] = fn;
2652 args[1] = arg1;
2653 args[2] = arg2;
2654 args[3] = arg3;
2655 args[4] = arg4;
2656 args[5] = arg5;
2657 args[6] = arg6;
2658 GCPRO1 (args[0]);
2659 gcpro1.nvars = 7;
2660 RETURN_UNGCPRO (Ffuncall (7, args));
2663 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2664 /* ARGSUSED */
2665 Lisp_Object
2666 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2667 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2669 struct gcpro gcpro1;
2670 Lisp_Object args[8];
2671 args[0] = fn;
2672 args[1] = arg1;
2673 args[2] = arg2;
2674 args[3] = arg3;
2675 args[4] = arg4;
2676 args[5] = arg5;
2677 args[6] = arg6;
2678 args[7] = arg7;
2679 GCPRO1 (args[0]);
2680 gcpro1.nvars = 8;
2681 RETURN_UNGCPRO (Ffuncall (8, args));
2684 /* The caller should GCPRO all the elements of ARGS. */
2686 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2687 doc: /* Non-nil if OBJECT is a function. */)
2688 (Lisp_Object object)
2690 if (FUNCTIONP (object))
2691 return Qt;
2692 return Qnil;
2695 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2696 doc: /* Call first argument as a function, passing remaining arguments to it.
2697 Return the value that function returns.
2698 Thus, (funcall 'cons 'x 'y) returns (x . y).
2699 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2700 (ptrdiff_t nargs, Lisp_Object *args)
2702 Lisp_Object fun, original_fun;
2703 Lisp_Object funcar;
2704 ptrdiff_t numargs = nargs - 1;
2705 Lisp_Object lisp_numargs;
2706 Lisp_Object val;
2707 struct backtrace backtrace;
2708 register Lisp_Object *internal_args;
2709 ptrdiff_t i;
2711 QUIT;
2713 if (++lisp_eval_depth > max_lisp_eval_depth)
2715 if (max_lisp_eval_depth < 100)
2716 max_lisp_eval_depth = 100;
2717 if (lisp_eval_depth > max_lisp_eval_depth)
2718 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2721 backtrace.next = backtrace_list;
2722 backtrace.function = args[0];
2723 backtrace.args = &args[1]; /* This also GCPROs them. */
2724 backtrace.nargs = nargs - 1;
2725 backtrace.debug_on_exit = 0;
2726 backtrace_list = &backtrace;
2728 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2729 maybe_gc ();
2731 if (debug_on_next_call)
2732 do_debug_on_call (Qlambda);
2734 check_cons_list ();
2736 original_fun = args[0];
2738 retry:
2740 /* Optimize for no indirection. */
2741 fun = original_fun;
2742 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2743 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2744 fun = indirect_function (fun);
2746 if (SUBRP (fun))
2748 if (numargs < XSUBR (fun)->min_args
2749 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2751 XSETFASTINT (lisp_numargs, numargs);
2752 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2755 else if (XSUBR (fun)->max_args == UNEVALLED)
2756 xsignal1 (Qinvalid_function, original_fun);
2758 else if (XSUBR (fun)->max_args == MANY)
2759 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2760 else
2762 if (XSUBR (fun)->max_args > numargs)
2764 internal_args = alloca (XSUBR (fun)->max_args
2765 * sizeof *internal_args);
2766 memcpy (internal_args, args + 1, numargs * word_size);
2767 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2768 internal_args[i] = Qnil;
2770 else
2771 internal_args = args + 1;
2772 switch (XSUBR (fun)->max_args)
2774 case 0:
2775 val = (XSUBR (fun)->function.a0 ());
2776 break;
2777 case 1:
2778 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2779 break;
2780 case 2:
2781 val = (XSUBR (fun)->function.a2
2782 (internal_args[0], internal_args[1]));
2783 break;
2784 case 3:
2785 val = (XSUBR (fun)->function.a3
2786 (internal_args[0], internal_args[1], internal_args[2]));
2787 break;
2788 case 4:
2789 val = (XSUBR (fun)->function.a4
2790 (internal_args[0], internal_args[1], internal_args[2],
2791 internal_args[3]));
2792 break;
2793 case 5:
2794 val = (XSUBR (fun)->function.a5
2795 (internal_args[0], internal_args[1], internal_args[2],
2796 internal_args[3], internal_args[4]));
2797 break;
2798 case 6:
2799 val = (XSUBR (fun)->function.a6
2800 (internal_args[0], internal_args[1], internal_args[2],
2801 internal_args[3], internal_args[4], internal_args[5]));
2802 break;
2803 case 7:
2804 val = (XSUBR (fun)->function.a7
2805 (internal_args[0], internal_args[1], internal_args[2],
2806 internal_args[3], internal_args[4], internal_args[5],
2807 internal_args[6]));
2808 break;
2810 case 8:
2811 val = (XSUBR (fun)->function.a8
2812 (internal_args[0], internal_args[1], internal_args[2],
2813 internal_args[3], internal_args[4], internal_args[5],
2814 internal_args[6], internal_args[7]));
2815 break;
2817 default:
2819 /* If a subr takes more than 8 arguments without using MANY
2820 or UNEVALLED, we need to extend this function to support it.
2821 Until this is done, there is no way to call the function. */
2822 emacs_abort ();
2826 else if (COMPILEDP (fun))
2827 val = funcall_lambda (fun, numargs, args + 1);
2828 else
2830 if (EQ (fun, Qunbound))
2831 xsignal1 (Qvoid_function, original_fun);
2832 if (!CONSP (fun))
2833 xsignal1 (Qinvalid_function, original_fun);
2834 funcar = XCAR (fun);
2835 if (!SYMBOLP (funcar))
2836 xsignal1 (Qinvalid_function, original_fun);
2837 if (EQ (funcar, Qlambda)
2838 || EQ (funcar, Qclosure))
2839 val = funcall_lambda (fun, numargs, args + 1);
2840 else if (EQ (funcar, Qautoload))
2842 Fautoload_do_load (fun, original_fun, Qnil);
2843 check_cons_list ();
2844 goto retry;
2846 else
2847 xsignal1 (Qinvalid_function, original_fun);
2849 check_cons_list ();
2850 lisp_eval_depth--;
2851 if (backtrace.debug_on_exit)
2852 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2853 backtrace_list = backtrace.next;
2854 return val;
2857 static Lisp_Object
2858 apply_lambda (Lisp_Object fun, Lisp_Object args)
2860 Lisp_Object args_left;
2861 ptrdiff_t i;
2862 EMACS_INT numargs;
2863 register Lisp_Object *arg_vector;
2864 struct gcpro gcpro1, gcpro2, gcpro3;
2865 register Lisp_Object tem;
2866 USE_SAFE_ALLOCA;
2868 numargs = XFASTINT (Flength (args));
2869 SAFE_ALLOCA_LISP (arg_vector, numargs);
2870 args_left = args;
2872 GCPRO3 (*arg_vector, args_left, fun);
2873 gcpro1.nvars = 0;
2875 for (i = 0; i < numargs; )
2877 tem = Fcar (args_left), args_left = Fcdr (args_left);
2878 tem = eval_sub (tem);
2879 arg_vector[i++] = tem;
2880 gcpro1.nvars = i;
2883 UNGCPRO;
2885 backtrace_list->args = arg_vector;
2886 backtrace_list->nargs = i;
2887 tem = funcall_lambda (fun, numargs, arg_vector);
2889 /* Do the debug-on-exit now, while arg_vector still exists. */
2890 if (backtrace_list->debug_on_exit)
2891 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2892 /* Don't do it again when we return to eval. */
2893 backtrace_list->debug_on_exit = 0;
2894 SAFE_FREE ();
2895 return tem;
2898 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2899 and return the result of evaluation.
2900 FUN must be either a lambda-expression or a compiled-code object. */
2902 static Lisp_Object
2903 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2904 register Lisp_Object *arg_vector)
2906 Lisp_Object val, syms_left, next, lexenv;
2907 ptrdiff_t count = SPECPDL_INDEX ();
2908 ptrdiff_t i;
2909 bool optional, rest;
2911 if (CONSP (fun))
2913 if (EQ (XCAR (fun), Qclosure))
2915 fun = XCDR (fun); /* Drop `closure'. */
2916 lexenv = XCAR (fun);
2917 CHECK_LIST_CONS (fun, fun);
2919 else
2920 lexenv = Qnil;
2921 syms_left = XCDR (fun);
2922 if (CONSP (syms_left))
2923 syms_left = XCAR (syms_left);
2924 else
2925 xsignal1 (Qinvalid_function, fun);
2927 else if (COMPILEDP (fun))
2929 syms_left = AREF (fun, COMPILED_ARGLIST);
2930 if (INTEGERP (syms_left))
2931 /* A byte-code object with a non-nil `push args' slot means we
2932 shouldn't bind any arguments, instead just call the byte-code
2933 interpreter directly; it will push arguments as necessary.
2935 Byte-code objects with either a non-existent, or a nil value for
2936 the `push args' slot (the default), have dynamically-bound
2937 arguments, and use the argument-binding code below instead (as do
2938 all interpreted functions, even lexically bound ones). */
2940 /* If we have not actually read the bytecode string
2941 and constants vector yet, fetch them from the file. */
2942 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2943 Ffetch_bytecode (fun);
2944 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2945 AREF (fun, COMPILED_CONSTANTS),
2946 AREF (fun, COMPILED_STACK_DEPTH),
2947 syms_left,
2948 nargs, arg_vector);
2950 lexenv = Qnil;
2952 else
2953 emacs_abort ();
2955 i = optional = rest = 0;
2956 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2958 QUIT;
2960 next = XCAR (syms_left);
2961 if (!SYMBOLP (next))
2962 xsignal1 (Qinvalid_function, fun);
2964 if (EQ (next, Qand_rest))
2965 rest = 1;
2966 else if (EQ (next, Qand_optional))
2967 optional = 1;
2968 else
2970 Lisp_Object arg;
2971 if (rest)
2973 arg = Flist (nargs - i, &arg_vector[i]);
2974 i = nargs;
2976 else if (i < nargs)
2977 arg = arg_vector[i++];
2978 else if (!optional)
2979 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2980 else
2981 arg = Qnil;
2983 /* Bind the argument. */
2984 if (!NILP (lexenv) && SYMBOLP (next))
2985 /* Lexically bind NEXT by adding it to the lexenv alist. */
2986 lexenv = Fcons (Fcons (next, arg), lexenv);
2987 else
2988 /* Dynamically bind NEXT. */
2989 specbind (next, arg);
2993 if (!NILP (syms_left))
2994 xsignal1 (Qinvalid_function, fun);
2995 else if (i < nargs)
2996 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2998 if (!EQ (lexenv, Vinternal_interpreter_environment))
2999 /* Instantiate a new lexical environment. */
3000 specbind (Qinternal_interpreter_environment, lexenv);
3002 if (CONSP (fun))
3003 val = Fprogn (XCDR (XCDR (fun)));
3004 else
3006 /* If we have not actually read the bytecode string
3007 and constants vector yet, fetch them from the file. */
3008 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3009 Ffetch_bytecode (fun);
3010 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3011 AREF (fun, COMPILED_CONSTANTS),
3012 AREF (fun, COMPILED_STACK_DEPTH),
3013 Qnil, 0, 0);
3016 return unbind_to (count, val);
3019 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3020 1, 1, 0,
3021 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3022 (Lisp_Object object)
3024 Lisp_Object tem;
3026 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3028 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3029 if (!CONSP (tem))
3031 tem = AREF (object, COMPILED_BYTECODE);
3032 if (CONSP (tem) && STRINGP (XCAR (tem)))
3033 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3034 else
3035 error ("Invalid byte code");
3037 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3038 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3040 return object;
3043 static void
3044 grow_specpdl (void)
3046 register ptrdiff_t count = SPECPDL_INDEX ();
3047 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX);
3048 if (max_size <= specpdl_size)
3050 if (max_specpdl_size < 400)
3051 max_size = max_specpdl_size = 400;
3052 if (max_size <= specpdl_size)
3053 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3055 specpdl = xpalloc (specpdl, &specpdl_size, 1, max_size, sizeof *specpdl);
3056 specpdl_ptr = specpdl + count;
3059 /* `specpdl_ptr->symbol' is a field which describes which variable is
3060 let-bound, so it can be properly undone when we unbind_to.
3061 It can have the following two shapes:
3062 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3063 a symbol that is not buffer-local (at least at the time
3064 the let binding started). Note also that it should not be
3065 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3066 to record V2 here).
3067 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3068 variable SYMBOL which can be buffer-local. WHERE tells us
3069 which buffer is affected (or nil if the let-binding affects the
3070 global value of the variable) and BUFFER tells us which buffer was
3071 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3072 BUFFER did not yet have a buffer-local value). */
3074 void
3075 specbind (Lisp_Object symbol, Lisp_Object value)
3077 struct Lisp_Symbol *sym;
3079 CHECK_SYMBOL (symbol);
3080 sym = XSYMBOL (symbol);
3081 if (specpdl_ptr == specpdl + specpdl_size)
3082 grow_specpdl ();
3084 start:
3085 switch (sym->redirect)
3087 case SYMBOL_VARALIAS:
3088 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3089 case SYMBOL_PLAINVAL:
3090 /* The most common case is that of a non-constant symbol with a
3091 trivial value. Make that as fast as we can. */
3092 set_specpdl_symbol (symbol);
3093 set_specpdl_old_value (SYMBOL_VAL (sym));
3094 specpdl_ptr->func = NULL;
3095 ++specpdl_ptr;
3096 if (!sym->constant)
3097 SET_SYMBOL_VAL (sym, value);
3098 else
3099 set_internal (symbol, value, Qnil, 1);
3100 break;
3101 case SYMBOL_LOCALIZED:
3102 if (SYMBOL_BLV (sym)->frame_local)
3103 error ("Frame-local vars cannot be let-bound");
3104 case SYMBOL_FORWARDED:
3106 Lisp_Object ovalue = find_symbol_value (symbol);
3107 specpdl_ptr->func = 0;
3108 set_specpdl_old_value (ovalue);
3110 eassert (sym->redirect != SYMBOL_LOCALIZED
3111 || (EQ (SYMBOL_BLV (sym)->where,
3112 SYMBOL_BLV (sym)->frame_local ?
3113 Fselected_frame () : Fcurrent_buffer ())));
3115 if (sym->redirect == SYMBOL_LOCALIZED
3116 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3118 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3120 /* For a local variable, record both the symbol and which
3121 buffer's or frame's value we are saving. */
3122 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3124 eassert (sym->redirect != SYMBOL_LOCALIZED
3125 || (blv_found (SYMBOL_BLV (sym))
3126 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3127 where = cur_buf;
3129 else if (sym->redirect == SYMBOL_LOCALIZED
3130 && blv_found (SYMBOL_BLV (sym)))
3131 where = SYMBOL_BLV (sym)->where;
3132 else
3133 where = Qnil;
3135 /* We're not using the `unused' slot in the specbinding
3136 structure because this would mean we have to do more
3137 work for simple variables. */
3138 /* FIXME: The third value `current_buffer' is only used in
3139 let_shadows_buffer_binding_p which is itself only used
3140 in set_internal for local_if_set. */
3141 eassert (NILP (where) || EQ (where, cur_buf));
3142 set_specpdl_symbol (Fcons (symbol, Fcons (where, cur_buf)));
3144 /* If SYMBOL is a per-buffer variable which doesn't have a
3145 buffer-local value here, make the `let' change the global
3146 value by changing the value of SYMBOL in all buffers not
3147 having their own value. This is consistent with what
3148 happens with other buffer-local variables. */
3149 if (NILP (where)
3150 && sym->redirect == SYMBOL_FORWARDED)
3152 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3153 ++specpdl_ptr;
3154 Fset_default (symbol, value);
3155 return;
3158 else
3159 set_specpdl_symbol (symbol);
3161 specpdl_ptr++;
3162 set_internal (symbol, value, Qnil, 1);
3163 break;
3165 default: emacs_abort ();
3169 void
3170 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3172 if (specpdl_ptr == specpdl + specpdl_size)
3173 grow_specpdl ();
3174 specpdl_ptr->func = function;
3175 set_specpdl_symbol (Qnil);
3176 set_specpdl_old_value (arg);
3177 specpdl_ptr++;
3180 Lisp_Object
3181 unbind_to (ptrdiff_t count, Lisp_Object value)
3183 Lisp_Object quitf = Vquit_flag;
3184 struct gcpro gcpro1, gcpro2;
3186 GCPRO2 (value, quitf);
3187 Vquit_flag = Qnil;
3189 while (specpdl_ptr != specpdl + count)
3191 /* Copy the binding, and decrement specpdl_ptr, before we do
3192 the work to unbind it. We decrement first
3193 so that an error in unbinding won't try to unbind
3194 the same entry again, and we copy the binding first
3195 in case more bindings are made during some of the code we run. */
3197 struct specbinding this_binding;
3198 this_binding = *--specpdl_ptr;
3200 if (this_binding.func != 0)
3201 (*this_binding.func) (this_binding.old_value);
3202 /* If the symbol is a list, it is really (SYMBOL WHERE
3203 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3204 frame. If WHERE is a buffer or frame, this indicates we
3205 bound a variable that had a buffer-local or frame-local
3206 binding. WHERE nil means that the variable had the default
3207 value when it was bound. CURRENT-BUFFER is the buffer that
3208 was current when the variable was bound. */
3209 else if (CONSP (this_binding.symbol))
3211 Lisp_Object symbol, where;
3213 symbol = XCAR (this_binding.symbol);
3214 where = XCAR (XCDR (this_binding.symbol));
3216 if (NILP (where))
3217 Fset_default (symbol, this_binding.old_value);
3218 /* If `where' is non-nil, reset the value in the appropriate
3219 local binding, but only if that binding still exists. */
3220 else if (BUFFERP (where)
3221 ? !NILP (Flocal_variable_p (symbol, where))
3222 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3223 set_internal (symbol, this_binding.old_value, where, 1);
3225 /* If variable has a trivial value (no forwarding), we can
3226 just set it. No need to check for constant symbols here,
3227 since that was already done by specbind. */
3228 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3229 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3230 this_binding.old_value);
3231 else
3232 /* NOTE: we only ever come here if make_local_foo was used for
3233 the first time on this var within this let. */
3234 Fset_default (this_binding.symbol, this_binding.old_value);
3237 if (NILP (Vquit_flag) && !NILP (quitf))
3238 Vquit_flag = quitf;
3240 UNGCPRO;
3241 return value;
3244 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3245 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3246 A special variable is one that will be bound dynamically, even in a
3247 context where binding is lexical by default. */)
3248 (Lisp_Object symbol)
3250 CHECK_SYMBOL (symbol);
3251 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3255 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3256 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3257 The debugger is entered when that frame exits, if the flag is non-nil. */)
3258 (Lisp_Object level, Lisp_Object flag)
3260 register struct backtrace *backlist = backtrace_list;
3261 register EMACS_INT i;
3263 CHECK_NUMBER (level);
3265 for (i = 0; backlist && i < XINT (level); i++)
3267 backlist = backlist->next;
3270 if (backlist)
3271 backlist->debug_on_exit = !NILP (flag);
3273 return flag;
3276 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3277 doc: /* Print a trace of Lisp function calls currently active.
3278 Output stream used is value of `standard-output'. */)
3279 (void)
3281 register struct backtrace *backlist = backtrace_list;
3282 Lisp_Object tail;
3283 Lisp_Object tem;
3284 struct gcpro gcpro1;
3285 Lisp_Object old_print_level = Vprint_level;
3287 if (NILP (Vprint_level))
3288 XSETFASTINT (Vprint_level, 8);
3290 tail = Qnil;
3291 GCPRO1 (tail);
3293 while (backlist)
3295 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3296 if (backlist->nargs == UNEVALLED)
3298 Fprin1 (Fcons (backlist->function, *backlist->args), Qnil);
3299 write_string ("\n", -1);
3301 else
3303 tem = backlist->function;
3304 Fprin1 (tem, Qnil); /* This can QUIT. */
3305 write_string ("(", -1);
3306 if (backlist->nargs == MANY)
3307 { /* FIXME: Can this happen? */
3308 bool later_arg = 0;
3309 for (tail = *backlist->args; !NILP (tail); tail = Fcdr (tail))
3311 if (later_arg)
3312 write_string (" ", -1);
3313 Fprin1 (Fcar (tail), Qnil);
3314 later_arg = 1;
3317 else
3319 ptrdiff_t i;
3320 for (i = 0; i < backlist->nargs; i++)
3322 if (i) write_string (" ", -1);
3323 Fprin1 (backlist->args[i], Qnil);
3326 write_string (")\n", -1);
3328 backlist = backlist->next;
3331 Vprint_level = old_print_level;
3332 UNGCPRO;
3333 return Qnil;
3336 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3337 doc: /* Return the function and arguments NFRAMES up from current execution point.
3338 If that frame has not evaluated the arguments yet (or is a special form),
3339 the value is (nil FUNCTION ARG-FORMS...).
3340 If that frame has evaluated its arguments and called its function already,
3341 the value is (t FUNCTION ARG-VALUES...).
3342 A &rest arg is represented as the tail of the list ARG-VALUES.
3343 FUNCTION is whatever was supplied as car of evaluated list,
3344 or a lambda expression for macro calls.
3345 If NFRAMES is more than the number of frames, the value is nil. */)
3346 (Lisp_Object nframes)
3348 register struct backtrace *backlist = backtrace_list;
3349 register EMACS_INT i;
3350 Lisp_Object tem;
3352 CHECK_NATNUM (nframes);
3354 /* Find the frame requested. */
3355 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3356 backlist = backlist->next;
3358 if (!backlist)
3359 return Qnil;
3360 if (backlist->nargs == UNEVALLED)
3361 return Fcons (Qnil, Fcons (backlist->function, *backlist->args));
3362 else
3364 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3365 tem = *backlist->args;
3366 else
3367 tem = Flist (backlist->nargs, backlist->args);
3369 return Fcons (Qt, Fcons (backlist->function, tem));
3374 #if BYTE_MARK_STACK
3375 void
3376 mark_backtrace (void)
3378 register struct backtrace *backlist;
3379 ptrdiff_t i;
3381 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3383 mark_object (backlist->function);
3385 if (backlist->nargs == UNEVALLED
3386 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3387 i = 1;
3388 else
3389 i = backlist->nargs;
3390 while (i--)
3391 mark_object (backlist->args[i]);
3394 #endif
3396 void
3397 syms_of_eval (void)
3399 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3400 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3401 If Lisp code tries to increase the total number past this amount,
3402 an error is signaled.
3403 You can safely use a value considerably larger than the default value,
3404 if that proves inconveniently small. However, if you increase it too far,
3405 Emacs could run out of memory trying to make the stack bigger. */);
3407 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3408 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3410 This limit serves to catch infinite recursions for you before they cause
3411 actual stack overflow in C, which would be fatal for Emacs.
3412 You can safely make it considerably larger than its default value,
3413 if that proves inconveniently small. However, if you increase it too far,
3414 Emacs could overflow the real C stack, and crash. */);
3416 DEFVAR_LISP ("quit-flag", Vquit_flag,
3417 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3418 If the value is t, that means do an ordinary quit.
3419 If the value equals `throw-on-input', that means quit by throwing
3420 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3421 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3422 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3423 Vquit_flag = Qnil;
3425 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3426 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3427 Note that `quit-flag' will still be set by typing C-g,
3428 so a quit will be signaled as soon as `inhibit-quit' is nil.
3429 To prevent this happening, set `quit-flag' to nil
3430 before making `inhibit-quit' nil. */);
3431 Vinhibit_quit = Qnil;
3433 DEFSYM (Qinhibit_quit, "inhibit-quit");
3434 DEFSYM (Qautoload, "autoload");
3435 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3436 DEFSYM (Qmacro, "macro");
3437 DEFSYM (Qdeclare, "declare");
3439 /* Note that the process handling also uses Qexit, but we don't want
3440 to staticpro it twice, so we just do it here. */
3441 DEFSYM (Qexit, "exit");
3443 DEFSYM (Qinteractive, "interactive");
3444 DEFSYM (Qcommandp, "commandp");
3445 DEFSYM (Qand_rest, "&rest");
3446 DEFSYM (Qand_optional, "&optional");
3447 DEFSYM (Qclosure, "closure");
3448 DEFSYM (Qdebug, "debug");
3450 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3451 doc: /* Non-nil means never enter the debugger.
3452 Normally set while the debugger is already active, to avoid recursive
3453 invocations. */);
3454 Vinhibit_debugger = Qnil;
3456 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3457 doc: /* Non-nil means enter debugger if an error is signaled.
3458 Does not apply to errors handled by `condition-case' or those
3459 matched by `debug-ignored-errors'.
3460 If the value is a list, an error only means to enter the debugger
3461 if one of its condition symbols appears in the list.
3462 When you evaluate an expression interactively, this variable
3463 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3464 The command `toggle-debug-on-error' toggles this.
3465 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3466 Vdebug_on_error = Qnil;
3468 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3469 doc: /* List of errors for which the debugger should not be called.
3470 Each element may be a condition-name or a regexp that matches error messages.
3471 If any element applies to a given error, that error skips the debugger
3472 and just returns to top level.
3473 This overrides the variable `debug-on-error'.
3474 It does not apply to errors handled by `condition-case'. */);
3475 Vdebug_ignored_errors = Qnil;
3477 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3478 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3479 Does not apply if quit is handled by a `condition-case'. */);
3480 debug_on_quit = 0;
3482 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3483 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3485 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3486 doc: /* Non-nil means debugger may continue execution.
3487 This is nil when the debugger is called under circumstances where it
3488 might not be safe to continue. */);
3489 debugger_may_continue = 1;
3491 DEFVAR_LISP ("debugger", Vdebugger,
3492 doc: /* Function to call to invoke debugger.
3493 If due to frame exit, args are `exit' and the value being returned;
3494 this function's value will be returned instead of that.
3495 If due to error, args are `error' and a list of the args to `signal'.
3496 If due to `apply' or `funcall' entry, one arg, `lambda'.
3497 If due to `eval' entry, one arg, t. */);
3498 Vdebugger = Qnil;
3500 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3501 doc: /* If non-nil, this is a function for `signal' to call.
3502 It receives the same arguments that `signal' was given.
3503 The Edebug package uses this to regain control. */);
3504 Vsignal_hook_function = Qnil;
3506 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3507 doc: /* Non-nil means call the debugger regardless of condition handlers.
3508 Note that `debug-on-error', `debug-on-quit' and friends
3509 still determine whether to handle the particular condition. */);
3510 Vdebug_on_signal = Qnil;
3512 /* When lexical binding is being used,
3513 Vinternal_interpreter_environment is non-nil, and contains an alist
3514 of lexically-bound variable, or (t), indicating an empty
3515 environment. The lisp name of this variable would be
3516 `internal-interpreter-environment' if it weren't hidden.
3517 Every element of this list can be either a cons (VAR . VAL)
3518 specifying a lexical binding, or a single symbol VAR indicating
3519 that this variable should use dynamic scoping. */
3520 DEFSYM (Qinternal_interpreter_environment,
3521 "internal-interpreter-environment");
3522 DEFVAR_LISP ("internal-interpreter-environment",
3523 Vinternal_interpreter_environment,
3524 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3525 When lexical binding is not being used, this variable is nil.
3526 A value of `(t)' indicates an empty environment, otherwise it is an
3527 alist of active lexical bindings. */);
3528 Vinternal_interpreter_environment = Qnil;
3529 /* Don't export this variable to Elisp, so no one can mess with it
3530 (Just imagine if someone makes it buffer-local). */
3531 Funintern (Qinternal_interpreter_environment, Qnil);
3533 DEFSYM (Vrun_hooks, "run-hooks");
3535 staticpro (&Vautoload_queue);
3536 Vautoload_queue = Qnil;
3537 staticpro (&Vsignaling_function);
3538 Vsignaling_function = Qnil;
3540 inhibit_lisp_code = Qnil;
3542 defsubr (&Sor);
3543 defsubr (&Sand);
3544 defsubr (&Sif);
3545 defsubr (&Scond);
3546 defsubr (&Sprogn);
3547 defsubr (&Sprog1);
3548 defsubr (&Sprog2);
3549 defsubr (&Ssetq);
3550 defsubr (&Squote);
3551 defsubr (&Sfunction);
3552 defsubr (&Sdefvar);
3553 defsubr (&Sdefvaralias);
3554 defsubr (&Sdefconst);
3555 defsubr (&Smake_var_non_special);
3556 defsubr (&Slet);
3557 defsubr (&SletX);
3558 defsubr (&Swhile);
3559 defsubr (&Smacroexpand);
3560 defsubr (&Scatch);
3561 defsubr (&Sthrow);
3562 defsubr (&Sunwind_protect);
3563 defsubr (&Scondition_case);
3564 defsubr (&Ssignal);
3565 defsubr (&Sinteractive_p);
3566 defsubr (&Scalled_interactively_p);
3567 defsubr (&Scommandp);
3568 defsubr (&Sautoload);
3569 defsubr (&Sautoload_do_load);
3570 defsubr (&Seval);
3571 defsubr (&Sapply);
3572 defsubr (&Sfuncall);
3573 defsubr (&Srun_hooks);
3574 defsubr (&Srun_hook_with_args);
3575 defsubr (&Srun_hook_with_args_until_success);
3576 defsubr (&Srun_hook_with_args_until_failure);
3577 defsubr (&Srun_hook_wrapped);
3578 defsubr (&Sfetch_bytecode);
3579 defsubr (&Sbacktrace_debug);
3580 defsubr (&Sbacktrace);
3581 defsubr (&Sbacktrace_frame);
3582 defsubr (&Sspecial_variable_p);
3583 defsubr (&Sfunctionp);