commands.texi small fix for bug#13393
[emacs.git] / src / eval.c
blob7709b8ba2dcd751e3421866fcbae839f70b24725
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;
2037 maybe_gc ();
2039 if (++lisp_eval_depth > max_lisp_eval_depth)
2041 if (max_lisp_eval_depth < 100)
2042 max_lisp_eval_depth = 100;
2043 if (lisp_eval_depth > max_lisp_eval_depth)
2044 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2047 original_fun = XCAR (form);
2048 original_args = XCDR (form);
2050 backtrace.next = backtrace_list;
2051 backtrace.function = original_fun; /* This also protects them from gc. */
2052 backtrace.args = &original_args;
2053 backtrace.nargs = UNEVALLED;
2054 backtrace.debug_on_exit = 0;
2055 backtrace_list = &backtrace;
2057 if (debug_on_next_call)
2058 do_debug_on_call (Qt);
2060 /* At this point, only original_fun and original_args
2061 have values that will be used below. */
2062 retry:
2064 /* Optimize for no indirection. */
2065 fun = original_fun;
2066 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2067 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2068 fun = indirect_function (fun);
2070 if (SUBRP (fun))
2072 Lisp_Object numargs;
2073 Lisp_Object argvals[8];
2074 Lisp_Object args_left;
2075 register int i, maxargs;
2077 args_left = original_args;
2078 numargs = Flength (args_left);
2080 check_cons_list ();
2082 if (XINT (numargs) < XSUBR (fun)->min_args
2083 || (XSUBR (fun)->max_args >= 0
2084 && XSUBR (fun)->max_args < XINT (numargs)))
2085 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2087 else if (XSUBR (fun)->max_args == UNEVALLED)
2088 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2089 else if (XSUBR (fun)->max_args == MANY)
2091 /* Pass a vector of evaluated arguments. */
2092 Lisp_Object *vals;
2093 ptrdiff_t argnum = 0;
2094 USE_SAFE_ALLOCA;
2096 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2098 GCPRO3 (args_left, fun, fun);
2099 gcpro3.var = vals;
2100 gcpro3.nvars = 0;
2102 while (!NILP (args_left))
2104 vals[argnum++] = eval_sub (Fcar (args_left));
2105 args_left = Fcdr (args_left);
2106 gcpro3.nvars = argnum;
2109 backtrace.args = vals;
2110 backtrace.nargs = XINT (numargs);
2112 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2113 UNGCPRO;
2114 SAFE_FREE ();
2116 else
2118 GCPRO3 (args_left, fun, fun);
2119 gcpro3.var = argvals;
2120 gcpro3.nvars = 0;
2122 maxargs = XSUBR (fun)->max_args;
2123 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2125 argvals[i] = eval_sub (Fcar (args_left));
2126 gcpro3.nvars = ++i;
2129 UNGCPRO;
2131 backtrace.args = argvals;
2132 backtrace.nargs = XINT (numargs);
2134 switch (i)
2136 case 0:
2137 val = (XSUBR (fun)->function.a0 ());
2138 break;
2139 case 1:
2140 val = (XSUBR (fun)->function.a1 (argvals[0]));
2141 break;
2142 case 2:
2143 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2144 break;
2145 case 3:
2146 val = (XSUBR (fun)->function.a3
2147 (argvals[0], argvals[1], argvals[2]));
2148 break;
2149 case 4:
2150 val = (XSUBR (fun)->function.a4
2151 (argvals[0], argvals[1], argvals[2], argvals[3]));
2152 break;
2153 case 5:
2154 val = (XSUBR (fun)->function.a5
2155 (argvals[0], argvals[1], argvals[2], argvals[3],
2156 argvals[4]));
2157 break;
2158 case 6:
2159 val = (XSUBR (fun)->function.a6
2160 (argvals[0], argvals[1], argvals[2], argvals[3],
2161 argvals[4], argvals[5]));
2162 break;
2163 case 7:
2164 val = (XSUBR (fun)->function.a7
2165 (argvals[0], argvals[1], argvals[2], argvals[3],
2166 argvals[4], argvals[5], argvals[6]));
2167 break;
2169 case 8:
2170 val = (XSUBR (fun)->function.a8
2171 (argvals[0], argvals[1], argvals[2], argvals[3],
2172 argvals[4], argvals[5], argvals[6], argvals[7]));
2173 break;
2175 default:
2176 /* Someone has created a subr that takes more arguments than
2177 is supported by this code. We need to either rewrite the
2178 subr to use a different argument protocol, or add more
2179 cases to this switch. */
2180 emacs_abort ();
2184 else if (COMPILEDP (fun))
2185 val = apply_lambda (fun, original_args);
2186 else
2188 if (EQ (fun, Qunbound))
2189 xsignal1 (Qvoid_function, original_fun);
2190 if (!CONSP (fun))
2191 xsignal1 (Qinvalid_function, original_fun);
2192 funcar = XCAR (fun);
2193 if (!SYMBOLP (funcar))
2194 xsignal1 (Qinvalid_function, original_fun);
2195 if (EQ (funcar, Qautoload))
2197 Fautoload_do_load (fun, original_fun, Qnil);
2198 goto retry;
2200 if (EQ (funcar, Qmacro))
2202 ptrdiff_t count = SPECPDL_INDEX ();
2203 Lisp_Object exp;
2204 /* Bind lexical-binding during expansion of the macro, so the
2205 macro can know reliably if the code it outputs will be
2206 interpreted using lexical-binding or not. */
2207 specbind (Qlexical_binding,
2208 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2209 exp = apply1 (Fcdr (fun), original_args);
2210 unbind_to (count, Qnil);
2211 val = eval_sub (exp);
2213 else if (EQ (funcar, Qlambda)
2214 || EQ (funcar, Qclosure))
2215 val = apply_lambda (fun, original_args);
2216 else
2217 xsignal1 (Qinvalid_function, original_fun);
2219 check_cons_list ();
2221 lisp_eval_depth--;
2222 if (backtrace.debug_on_exit)
2223 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2224 backtrace_list = backtrace.next;
2226 return val;
2229 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2230 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2231 Then return the value FUNCTION returns.
2232 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2233 usage: (apply FUNCTION &rest ARGUMENTS) */)
2234 (ptrdiff_t nargs, Lisp_Object *args)
2236 ptrdiff_t i;
2237 EMACS_INT numargs;
2238 register Lisp_Object spread_arg;
2239 register Lisp_Object *funcall_args;
2240 Lisp_Object fun, retval;
2241 struct gcpro gcpro1;
2242 USE_SAFE_ALLOCA;
2244 fun = args [0];
2245 funcall_args = 0;
2246 spread_arg = args [nargs - 1];
2247 CHECK_LIST (spread_arg);
2249 numargs = XINT (Flength (spread_arg));
2251 if (numargs == 0)
2252 return Ffuncall (nargs - 1, args);
2253 else if (numargs == 1)
2255 args [nargs - 1] = XCAR (spread_arg);
2256 return Ffuncall (nargs, args);
2259 numargs += nargs - 2;
2261 /* Optimize for no indirection. */
2262 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2263 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2264 fun = indirect_function (fun);
2265 if (EQ (fun, Qunbound))
2267 /* Let funcall get the error. */
2268 fun = args[0];
2269 goto funcall;
2272 if (SUBRP (fun))
2274 if (numargs < XSUBR (fun)->min_args
2275 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2276 goto funcall; /* Let funcall get the error. */
2277 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2279 /* Avoid making funcall cons up a yet another new vector of arguments
2280 by explicitly supplying nil's for optional values. */
2281 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2282 for (i = numargs; i < XSUBR (fun)->max_args;)
2283 funcall_args[++i] = Qnil;
2284 GCPRO1 (*funcall_args);
2285 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2288 funcall:
2289 /* We add 1 to numargs because funcall_args includes the
2290 function itself as well as its arguments. */
2291 if (!funcall_args)
2293 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2294 GCPRO1 (*funcall_args);
2295 gcpro1.nvars = 1 + numargs;
2298 memcpy (funcall_args, args, nargs * word_size);
2299 /* Spread the last arg we got. Its first element goes in
2300 the slot that it used to occupy, hence this value of I. */
2301 i = nargs - 1;
2302 while (!NILP (spread_arg))
2304 funcall_args [i++] = XCAR (spread_arg);
2305 spread_arg = XCDR (spread_arg);
2308 /* By convention, the caller needs to gcpro Ffuncall's args. */
2309 retval = Ffuncall (gcpro1.nvars, funcall_args);
2310 UNGCPRO;
2311 SAFE_FREE ();
2313 return retval;
2316 /* Run hook variables in various ways. */
2318 static Lisp_Object
2319 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2321 Ffuncall (nargs, args);
2322 return Qnil;
2325 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2326 doc: /* Run each hook in HOOKS.
2327 Each argument should be a symbol, a hook variable.
2328 These symbols are processed in the order specified.
2329 If a hook symbol has a non-nil value, that value may be a function
2330 or a list of functions to be called to run the hook.
2331 If the value is a function, it is called with no arguments.
2332 If it is a list, the elements are called, in order, with no arguments.
2334 Major modes should not use this function directly to run their mode
2335 hook; they should use `run-mode-hooks' instead.
2337 Do not use `make-local-variable' to make a hook variable buffer-local.
2338 Instead, use `add-hook' and specify t for the LOCAL argument.
2339 usage: (run-hooks &rest HOOKS) */)
2340 (ptrdiff_t nargs, Lisp_Object *args)
2342 Lisp_Object hook[1];
2343 ptrdiff_t i;
2345 for (i = 0; i < nargs; i++)
2347 hook[0] = args[i];
2348 run_hook_with_args (1, hook, funcall_nil);
2351 return Qnil;
2354 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2355 Srun_hook_with_args, 1, MANY, 0,
2356 doc: /* Run HOOK with the specified arguments ARGS.
2357 HOOK should be a symbol, a hook variable. The value of HOOK
2358 may be nil, a function, or a list of functions. Call each
2359 function in order with arguments ARGS. The final return value
2360 is unspecified.
2362 Do not use `make-local-variable' to make a hook variable buffer-local.
2363 Instead, use `add-hook' and specify t for the LOCAL argument.
2364 usage: (run-hook-with-args HOOK &rest ARGS) */)
2365 (ptrdiff_t nargs, Lisp_Object *args)
2367 return run_hook_with_args (nargs, args, funcall_nil);
2370 /* NB this one still documents a specific non-nil return value.
2371 (As did run-hook-with-args and run-hook-with-args-until-failure
2372 until they were changed in 24.1.) */
2373 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2374 Srun_hook_with_args_until_success, 1, MANY, 0,
2375 doc: /* Run HOOK with the specified arguments ARGS.
2376 HOOK should be a symbol, a hook variable. The value of HOOK
2377 may be nil, a function, or a list of functions. Call each
2378 function in order with arguments ARGS, stopping at the first
2379 one that returns non-nil, and return that value. Otherwise (if
2380 all functions return nil, or if there are no functions to call),
2381 return nil.
2383 Do not use `make-local-variable' to make a hook variable buffer-local.
2384 Instead, use `add-hook' and specify t for the LOCAL argument.
2385 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2386 (ptrdiff_t nargs, Lisp_Object *args)
2388 return run_hook_with_args (nargs, args, Ffuncall);
2391 static Lisp_Object
2392 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2394 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2397 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2398 Srun_hook_with_args_until_failure, 1, MANY, 0,
2399 doc: /* Run HOOK with the specified arguments ARGS.
2400 HOOK should be a symbol, a hook variable. The value of HOOK
2401 may be nil, a function, or a list of functions. Call each
2402 function in order with arguments ARGS, stopping at the first
2403 one that returns nil, and return nil. Otherwise (if all functions
2404 return non-nil, or if there are no functions to call), return non-nil
2405 \(do not rely on the precise return value in this case).
2407 Do not use `make-local-variable' to make a hook variable buffer-local.
2408 Instead, use `add-hook' and specify t for the LOCAL argument.
2409 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2410 (ptrdiff_t nargs, Lisp_Object *args)
2412 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2415 static Lisp_Object
2416 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2418 Lisp_Object tmp = args[0], ret;
2419 args[0] = args[1];
2420 args[1] = tmp;
2421 ret = Ffuncall (nargs, args);
2422 args[1] = args[0];
2423 args[0] = tmp;
2424 return ret;
2427 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2428 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2429 I.e. instead of calling each function FUN directly with arguments ARGS,
2430 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2431 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2432 aborts and returns that value.
2433 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2434 (ptrdiff_t nargs, Lisp_Object *args)
2436 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2439 /* ARGS[0] should be a hook symbol.
2440 Call each of the functions in the hook value, passing each of them
2441 as arguments all the rest of ARGS (all NARGS - 1 elements).
2442 FUNCALL specifies how to call each function on the hook.
2443 The caller (or its caller, etc) must gcpro all of ARGS,
2444 except that it isn't necessary to gcpro ARGS[0]. */
2446 Lisp_Object
2447 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2448 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2450 Lisp_Object sym, val, ret = Qnil;
2451 struct gcpro gcpro1, gcpro2, gcpro3;
2453 /* If we are dying or still initializing,
2454 don't do anything--it would probably crash if we tried. */
2455 if (NILP (Vrun_hooks))
2456 return Qnil;
2458 sym = args[0];
2459 val = find_symbol_value (sym);
2461 if (EQ (val, Qunbound) || NILP (val))
2462 return ret;
2463 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2465 args[0] = val;
2466 return funcall (nargs, args);
2468 else
2470 Lisp_Object global_vals = Qnil;
2471 GCPRO3 (sym, val, global_vals);
2473 for (;
2474 CONSP (val) && NILP (ret);
2475 val = XCDR (val))
2477 if (EQ (XCAR (val), Qt))
2479 /* t indicates this hook has a local binding;
2480 it means to run the global binding too. */
2481 global_vals = Fdefault_value (sym);
2482 if (NILP (global_vals)) continue;
2484 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2486 args[0] = global_vals;
2487 ret = funcall (nargs, args);
2489 else
2491 for (;
2492 CONSP (global_vals) && NILP (ret);
2493 global_vals = XCDR (global_vals))
2495 args[0] = XCAR (global_vals);
2496 /* In a global value, t should not occur. If it does, we
2497 must ignore it to avoid an endless loop. */
2498 if (!EQ (args[0], Qt))
2499 ret = funcall (nargs, args);
2503 else
2505 args[0] = XCAR (val);
2506 ret = funcall (nargs, args);
2510 UNGCPRO;
2511 return ret;
2515 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2517 void
2518 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2520 Lisp_Object temp[3];
2521 temp[0] = hook;
2522 temp[1] = arg1;
2523 temp[2] = arg2;
2525 Frun_hook_with_args (3, temp);
2528 /* Apply fn to arg. */
2529 Lisp_Object
2530 apply1 (Lisp_Object fn, Lisp_Object arg)
2532 struct gcpro gcpro1;
2534 GCPRO1 (fn);
2535 if (NILP (arg))
2536 RETURN_UNGCPRO (Ffuncall (1, &fn));
2537 gcpro1.nvars = 2;
2539 Lisp_Object args[2];
2540 args[0] = fn;
2541 args[1] = arg;
2542 gcpro1.var = args;
2543 RETURN_UNGCPRO (Fapply (2, args));
2547 /* Call function fn on no arguments. */
2548 Lisp_Object
2549 call0 (Lisp_Object fn)
2551 struct gcpro gcpro1;
2553 GCPRO1 (fn);
2554 RETURN_UNGCPRO (Ffuncall (1, &fn));
2557 /* Call function fn with 1 argument arg1. */
2558 /* ARGSUSED */
2559 Lisp_Object
2560 call1 (Lisp_Object fn, Lisp_Object arg1)
2562 struct gcpro gcpro1;
2563 Lisp_Object args[2];
2565 args[0] = fn;
2566 args[1] = arg1;
2567 GCPRO1 (args[0]);
2568 gcpro1.nvars = 2;
2569 RETURN_UNGCPRO (Ffuncall (2, args));
2572 /* Call function fn with 2 arguments arg1, arg2. */
2573 /* ARGSUSED */
2574 Lisp_Object
2575 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2577 struct gcpro gcpro1;
2578 Lisp_Object args[3];
2579 args[0] = fn;
2580 args[1] = arg1;
2581 args[2] = arg2;
2582 GCPRO1 (args[0]);
2583 gcpro1.nvars = 3;
2584 RETURN_UNGCPRO (Ffuncall (3, args));
2587 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2588 /* ARGSUSED */
2589 Lisp_Object
2590 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2592 struct gcpro gcpro1;
2593 Lisp_Object args[4];
2594 args[0] = fn;
2595 args[1] = arg1;
2596 args[2] = arg2;
2597 args[3] = arg3;
2598 GCPRO1 (args[0]);
2599 gcpro1.nvars = 4;
2600 RETURN_UNGCPRO (Ffuncall (4, args));
2603 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2604 /* ARGSUSED */
2605 Lisp_Object
2606 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2607 Lisp_Object arg4)
2609 struct gcpro gcpro1;
2610 Lisp_Object args[5];
2611 args[0] = fn;
2612 args[1] = arg1;
2613 args[2] = arg2;
2614 args[3] = arg3;
2615 args[4] = arg4;
2616 GCPRO1 (args[0]);
2617 gcpro1.nvars = 5;
2618 RETURN_UNGCPRO (Ffuncall (5, args));
2621 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2622 /* ARGSUSED */
2623 Lisp_Object
2624 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2625 Lisp_Object arg4, Lisp_Object arg5)
2627 struct gcpro gcpro1;
2628 Lisp_Object args[6];
2629 args[0] = fn;
2630 args[1] = arg1;
2631 args[2] = arg2;
2632 args[3] = arg3;
2633 args[4] = arg4;
2634 args[5] = arg5;
2635 GCPRO1 (args[0]);
2636 gcpro1.nvars = 6;
2637 RETURN_UNGCPRO (Ffuncall (6, args));
2640 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2641 /* ARGSUSED */
2642 Lisp_Object
2643 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2644 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2646 struct gcpro gcpro1;
2647 Lisp_Object args[7];
2648 args[0] = fn;
2649 args[1] = arg1;
2650 args[2] = arg2;
2651 args[3] = arg3;
2652 args[4] = arg4;
2653 args[5] = arg5;
2654 args[6] = arg6;
2655 GCPRO1 (args[0]);
2656 gcpro1.nvars = 7;
2657 RETURN_UNGCPRO (Ffuncall (7, args));
2660 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2661 /* ARGSUSED */
2662 Lisp_Object
2663 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2664 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2666 struct gcpro gcpro1;
2667 Lisp_Object args[8];
2668 args[0] = fn;
2669 args[1] = arg1;
2670 args[2] = arg2;
2671 args[3] = arg3;
2672 args[4] = arg4;
2673 args[5] = arg5;
2674 args[6] = arg6;
2675 args[7] = arg7;
2676 GCPRO1 (args[0]);
2677 gcpro1.nvars = 8;
2678 RETURN_UNGCPRO (Ffuncall (8, args));
2681 /* The caller should GCPRO all the elements of ARGS. */
2683 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2684 doc: /* Non-nil if OBJECT is a function. */)
2685 (Lisp_Object object)
2687 if (FUNCTIONP (object))
2688 return Qt;
2689 return Qnil;
2692 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2693 doc: /* Call first argument as a function, passing remaining arguments to it.
2694 Return the value that function returns.
2695 Thus, (funcall 'cons 'x 'y) returns (x . y).
2696 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2697 (ptrdiff_t nargs, Lisp_Object *args)
2699 Lisp_Object fun, original_fun;
2700 Lisp_Object funcar;
2701 ptrdiff_t numargs = nargs - 1;
2702 Lisp_Object lisp_numargs;
2703 Lisp_Object val;
2704 struct backtrace backtrace;
2705 register Lisp_Object *internal_args;
2706 ptrdiff_t i;
2708 QUIT;
2710 if (++lisp_eval_depth > max_lisp_eval_depth)
2712 if (max_lisp_eval_depth < 100)
2713 max_lisp_eval_depth = 100;
2714 if (lisp_eval_depth > max_lisp_eval_depth)
2715 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2718 backtrace.next = backtrace_list;
2719 backtrace.function = args[0];
2720 backtrace.args = &args[1]; /* This also GCPROs them. */
2721 backtrace.nargs = nargs - 1;
2722 backtrace.debug_on_exit = 0;
2723 backtrace_list = &backtrace;
2725 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2726 maybe_gc ();
2728 if (debug_on_next_call)
2729 do_debug_on_call (Qlambda);
2731 check_cons_list ();
2733 original_fun = args[0];
2735 retry:
2737 /* Optimize for no indirection. */
2738 fun = original_fun;
2739 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2740 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2741 fun = indirect_function (fun);
2743 if (SUBRP (fun))
2745 if (numargs < XSUBR (fun)->min_args
2746 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2748 XSETFASTINT (lisp_numargs, numargs);
2749 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2752 else if (XSUBR (fun)->max_args == UNEVALLED)
2753 xsignal1 (Qinvalid_function, original_fun);
2755 else if (XSUBR (fun)->max_args == MANY)
2756 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2757 else
2759 if (XSUBR (fun)->max_args > numargs)
2761 internal_args = alloca (XSUBR (fun)->max_args
2762 * sizeof *internal_args);
2763 memcpy (internal_args, args + 1, numargs * word_size);
2764 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2765 internal_args[i] = Qnil;
2767 else
2768 internal_args = args + 1;
2769 switch (XSUBR (fun)->max_args)
2771 case 0:
2772 val = (XSUBR (fun)->function.a0 ());
2773 break;
2774 case 1:
2775 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2776 break;
2777 case 2:
2778 val = (XSUBR (fun)->function.a2
2779 (internal_args[0], internal_args[1]));
2780 break;
2781 case 3:
2782 val = (XSUBR (fun)->function.a3
2783 (internal_args[0], internal_args[1], internal_args[2]));
2784 break;
2785 case 4:
2786 val = (XSUBR (fun)->function.a4
2787 (internal_args[0], internal_args[1], internal_args[2],
2788 internal_args[3]));
2789 break;
2790 case 5:
2791 val = (XSUBR (fun)->function.a5
2792 (internal_args[0], internal_args[1], internal_args[2],
2793 internal_args[3], internal_args[4]));
2794 break;
2795 case 6:
2796 val = (XSUBR (fun)->function.a6
2797 (internal_args[0], internal_args[1], internal_args[2],
2798 internal_args[3], internal_args[4], internal_args[5]));
2799 break;
2800 case 7:
2801 val = (XSUBR (fun)->function.a7
2802 (internal_args[0], internal_args[1], internal_args[2],
2803 internal_args[3], internal_args[4], internal_args[5],
2804 internal_args[6]));
2805 break;
2807 case 8:
2808 val = (XSUBR (fun)->function.a8
2809 (internal_args[0], internal_args[1], internal_args[2],
2810 internal_args[3], internal_args[4], internal_args[5],
2811 internal_args[6], internal_args[7]));
2812 break;
2814 default:
2816 /* If a subr takes more than 8 arguments without using MANY
2817 or UNEVALLED, we need to extend this function to support it.
2818 Until this is done, there is no way to call the function. */
2819 emacs_abort ();
2823 else if (COMPILEDP (fun))
2824 val = funcall_lambda (fun, numargs, args + 1);
2825 else
2827 if (EQ (fun, Qunbound))
2828 xsignal1 (Qvoid_function, original_fun);
2829 if (!CONSP (fun))
2830 xsignal1 (Qinvalid_function, original_fun);
2831 funcar = XCAR (fun);
2832 if (!SYMBOLP (funcar))
2833 xsignal1 (Qinvalid_function, original_fun);
2834 if (EQ (funcar, Qlambda)
2835 || EQ (funcar, Qclosure))
2836 val = funcall_lambda (fun, numargs, args + 1);
2837 else if (EQ (funcar, Qautoload))
2839 Fautoload_do_load (fun, original_fun, Qnil);
2840 check_cons_list ();
2841 goto retry;
2843 else
2844 xsignal1 (Qinvalid_function, original_fun);
2846 check_cons_list ();
2847 lisp_eval_depth--;
2848 if (backtrace.debug_on_exit)
2849 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2850 backtrace_list = backtrace.next;
2851 return val;
2854 static Lisp_Object
2855 apply_lambda (Lisp_Object fun, Lisp_Object args)
2857 Lisp_Object args_left;
2858 ptrdiff_t i;
2859 EMACS_INT numargs;
2860 register Lisp_Object *arg_vector;
2861 struct gcpro gcpro1, gcpro2, gcpro3;
2862 register Lisp_Object tem;
2863 USE_SAFE_ALLOCA;
2865 numargs = XFASTINT (Flength (args));
2866 SAFE_ALLOCA_LISP (arg_vector, numargs);
2867 args_left = args;
2869 GCPRO3 (*arg_vector, args_left, fun);
2870 gcpro1.nvars = 0;
2872 for (i = 0; i < numargs; )
2874 tem = Fcar (args_left), args_left = Fcdr (args_left);
2875 tem = eval_sub (tem);
2876 arg_vector[i++] = tem;
2877 gcpro1.nvars = i;
2880 UNGCPRO;
2882 backtrace_list->args = arg_vector;
2883 backtrace_list->nargs = i;
2884 tem = funcall_lambda (fun, numargs, arg_vector);
2886 /* Do the debug-on-exit now, while arg_vector still exists. */
2887 if (backtrace_list->debug_on_exit)
2888 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2889 /* Don't do it again when we return to eval. */
2890 backtrace_list->debug_on_exit = 0;
2891 SAFE_FREE ();
2892 return tem;
2895 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2896 and return the result of evaluation.
2897 FUN must be either a lambda-expression or a compiled-code object. */
2899 static Lisp_Object
2900 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2901 register Lisp_Object *arg_vector)
2903 Lisp_Object val, syms_left, next, lexenv;
2904 ptrdiff_t count = SPECPDL_INDEX ();
2905 ptrdiff_t i;
2906 bool optional, rest;
2908 if (CONSP (fun))
2910 if (EQ (XCAR (fun), Qclosure))
2912 fun = XCDR (fun); /* Drop `closure'. */
2913 lexenv = XCAR (fun);
2914 CHECK_LIST_CONS (fun, fun);
2916 else
2917 lexenv = Qnil;
2918 syms_left = XCDR (fun);
2919 if (CONSP (syms_left))
2920 syms_left = XCAR (syms_left);
2921 else
2922 xsignal1 (Qinvalid_function, fun);
2924 else if (COMPILEDP (fun))
2926 syms_left = AREF (fun, COMPILED_ARGLIST);
2927 if (INTEGERP (syms_left))
2928 /* A byte-code object with a non-nil `push args' slot means we
2929 shouldn't bind any arguments, instead just call the byte-code
2930 interpreter directly; it will push arguments as necessary.
2932 Byte-code objects with either a non-existent, or a nil value for
2933 the `push args' slot (the default), have dynamically-bound
2934 arguments, and use the argument-binding code below instead (as do
2935 all interpreted functions, even lexically bound ones). */
2937 /* If we have not actually read the bytecode string
2938 and constants vector yet, fetch them from the file. */
2939 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2940 Ffetch_bytecode (fun);
2941 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2942 AREF (fun, COMPILED_CONSTANTS),
2943 AREF (fun, COMPILED_STACK_DEPTH),
2944 syms_left,
2945 nargs, arg_vector);
2947 lexenv = Qnil;
2949 else
2950 emacs_abort ();
2952 i = optional = rest = 0;
2953 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2955 QUIT;
2957 next = XCAR (syms_left);
2958 if (!SYMBOLP (next))
2959 xsignal1 (Qinvalid_function, fun);
2961 if (EQ (next, Qand_rest))
2962 rest = 1;
2963 else if (EQ (next, Qand_optional))
2964 optional = 1;
2965 else
2967 Lisp_Object arg;
2968 if (rest)
2970 arg = Flist (nargs - i, &arg_vector[i]);
2971 i = nargs;
2973 else if (i < nargs)
2974 arg = arg_vector[i++];
2975 else if (!optional)
2976 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2977 else
2978 arg = Qnil;
2980 /* Bind the argument. */
2981 if (!NILP (lexenv) && SYMBOLP (next))
2982 /* Lexically bind NEXT by adding it to the lexenv alist. */
2983 lexenv = Fcons (Fcons (next, arg), lexenv);
2984 else
2985 /* Dynamically bind NEXT. */
2986 specbind (next, arg);
2990 if (!NILP (syms_left))
2991 xsignal1 (Qinvalid_function, fun);
2992 else if (i < nargs)
2993 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2995 if (!EQ (lexenv, Vinternal_interpreter_environment))
2996 /* Instantiate a new lexical environment. */
2997 specbind (Qinternal_interpreter_environment, lexenv);
2999 if (CONSP (fun))
3000 val = Fprogn (XCDR (XCDR (fun)));
3001 else
3003 /* If we have not actually read the bytecode string
3004 and constants vector yet, fetch them from the file. */
3005 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3006 Ffetch_bytecode (fun);
3007 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3008 AREF (fun, COMPILED_CONSTANTS),
3009 AREF (fun, COMPILED_STACK_DEPTH),
3010 Qnil, 0, 0);
3013 return unbind_to (count, val);
3016 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3017 1, 1, 0,
3018 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3019 (Lisp_Object object)
3021 Lisp_Object tem;
3023 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3025 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3026 if (!CONSP (tem))
3028 tem = AREF (object, COMPILED_BYTECODE);
3029 if (CONSP (tem) && STRINGP (XCAR (tem)))
3030 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3031 else
3032 error ("Invalid byte code");
3034 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3035 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3037 return object;
3040 static void
3041 grow_specpdl (void)
3043 register ptrdiff_t count = SPECPDL_INDEX ();
3044 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX);
3045 if (max_size <= specpdl_size)
3047 if (max_specpdl_size < 400)
3048 max_size = max_specpdl_size = 400;
3049 if (max_size <= specpdl_size)
3050 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3052 specpdl = xpalloc (specpdl, &specpdl_size, 1, max_size, sizeof *specpdl);
3053 specpdl_ptr = specpdl + count;
3056 /* `specpdl_ptr->symbol' is a field which describes which variable is
3057 let-bound, so it can be properly undone when we unbind_to.
3058 It can have the following two shapes:
3059 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3060 a symbol that is not buffer-local (at least at the time
3061 the let binding started). Note also that it should not be
3062 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3063 to record V2 here).
3064 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3065 variable SYMBOL which can be buffer-local. WHERE tells us
3066 which buffer is affected (or nil if the let-binding affects the
3067 global value of the variable) and BUFFER tells us which buffer was
3068 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3069 BUFFER did not yet have a buffer-local value). */
3071 void
3072 specbind (Lisp_Object symbol, Lisp_Object value)
3074 struct Lisp_Symbol *sym;
3076 CHECK_SYMBOL (symbol);
3077 sym = XSYMBOL (symbol);
3078 if (specpdl_ptr == specpdl + specpdl_size)
3079 grow_specpdl ();
3081 start:
3082 switch (sym->redirect)
3084 case SYMBOL_VARALIAS:
3085 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3086 case SYMBOL_PLAINVAL:
3087 /* The most common case is that of a non-constant symbol with a
3088 trivial value. Make that as fast as we can. */
3089 set_specpdl_symbol (symbol);
3090 set_specpdl_old_value (SYMBOL_VAL (sym));
3091 specpdl_ptr->func = NULL;
3092 ++specpdl_ptr;
3093 if (!sym->constant)
3094 SET_SYMBOL_VAL (sym, value);
3095 else
3096 set_internal (symbol, value, Qnil, 1);
3097 break;
3098 case SYMBOL_LOCALIZED:
3099 if (SYMBOL_BLV (sym)->frame_local)
3100 error ("Frame-local vars cannot be let-bound");
3101 case SYMBOL_FORWARDED:
3103 Lisp_Object ovalue = find_symbol_value (symbol);
3104 specpdl_ptr->func = 0;
3105 set_specpdl_old_value (ovalue);
3107 eassert (sym->redirect != SYMBOL_LOCALIZED
3108 || (EQ (SYMBOL_BLV (sym)->where,
3109 SYMBOL_BLV (sym)->frame_local ?
3110 Fselected_frame () : Fcurrent_buffer ())));
3112 if (sym->redirect == SYMBOL_LOCALIZED
3113 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3115 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3117 /* For a local variable, record both the symbol and which
3118 buffer's or frame's value we are saving. */
3119 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3121 eassert (sym->redirect != SYMBOL_LOCALIZED
3122 || (blv_found (SYMBOL_BLV (sym))
3123 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3124 where = cur_buf;
3126 else if (sym->redirect == SYMBOL_LOCALIZED
3127 && blv_found (SYMBOL_BLV (sym)))
3128 where = SYMBOL_BLV (sym)->where;
3129 else
3130 where = Qnil;
3132 /* We're not using the `unused' slot in the specbinding
3133 structure because this would mean we have to do more
3134 work for simple variables. */
3135 /* FIXME: The third value `current_buffer' is only used in
3136 let_shadows_buffer_binding_p which is itself only used
3137 in set_internal for local_if_set. */
3138 eassert (NILP (where) || EQ (where, cur_buf));
3139 set_specpdl_symbol (Fcons (symbol, Fcons (where, cur_buf)));
3141 /* If SYMBOL is a per-buffer variable which doesn't have a
3142 buffer-local value here, make the `let' change the global
3143 value by changing the value of SYMBOL in all buffers not
3144 having their own value. This is consistent with what
3145 happens with other buffer-local variables. */
3146 if (NILP (where)
3147 && sym->redirect == SYMBOL_FORWARDED)
3149 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3150 ++specpdl_ptr;
3151 Fset_default (symbol, value);
3152 return;
3155 else
3156 set_specpdl_symbol (symbol);
3158 specpdl_ptr++;
3159 set_internal (symbol, value, Qnil, 1);
3160 break;
3162 default: emacs_abort ();
3166 void
3167 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3169 if (specpdl_ptr == specpdl + specpdl_size)
3170 grow_specpdl ();
3171 specpdl_ptr->func = function;
3172 set_specpdl_symbol (Qnil);
3173 set_specpdl_old_value (arg);
3174 specpdl_ptr++;
3177 Lisp_Object
3178 unbind_to (ptrdiff_t count, Lisp_Object value)
3180 Lisp_Object quitf = Vquit_flag;
3181 struct gcpro gcpro1, gcpro2;
3183 GCPRO2 (value, quitf);
3184 Vquit_flag = Qnil;
3186 while (specpdl_ptr != specpdl + count)
3188 /* Copy the binding, and decrement specpdl_ptr, before we do
3189 the work to unbind it. We decrement first
3190 so that an error in unbinding won't try to unbind
3191 the same entry again, and we copy the binding first
3192 in case more bindings are made during some of the code we run. */
3194 struct specbinding this_binding;
3195 this_binding = *--specpdl_ptr;
3197 if (this_binding.func != 0)
3198 (*this_binding.func) (this_binding.old_value);
3199 /* If the symbol is a list, it is really (SYMBOL WHERE
3200 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3201 frame. If WHERE is a buffer or frame, this indicates we
3202 bound a variable that had a buffer-local or frame-local
3203 binding. WHERE nil means that the variable had the default
3204 value when it was bound. CURRENT-BUFFER is the buffer that
3205 was current when the variable was bound. */
3206 else if (CONSP (this_binding.symbol))
3208 Lisp_Object symbol, where;
3210 symbol = XCAR (this_binding.symbol);
3211 where = XCAR (XCDR (this_binding.symbol));
3213 if (NILP (where))
3214 Fset_default (symbol, this_binding.old_value);
3215 /* If `where' is non-nil, reset the value in the appropriate
3216 local binding, but only if that binding still exists. */
3217 else if (BUFFERP (where)
3218 ? !NILP (Flocal_variable_p (symbol, where))
3219 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3220 set_internal (symbol, this_binding.old_value, where, 1);
3222 /* If variable has a trivial value (no forwarding), we can
3223 just set it. No need to check for constant symbols here,
3224 since that was already done by specbind. */
3225 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3226 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3227 this_binding.old_value);
3228 else
3229 /* NOTE: we only ever come here if make_local_foo was used for
3230 the first time on this var within this let. */
3231 Fset_default (this_binding.symbol, this_binding.old_value);
3234 if (NILP (Vquit_flag) && !NILP (quitf))
3235 Vquit_flag = quitf;
3237 UNGCPRO;
3238 return value;
3241 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3242 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3243 A special variable is one that will be bound dynamically, even in a
3244 context where binding is lexical by default. */)
3245 (Lisp_Object symbol)
3247 CHECK_SYMBOL (symbol);
3248 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3252 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3253 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3254 The debugger is entered when that frame exits, if the flag is non-nil. */)
3255 (Lisp_Object level, Lisp_Object flag)
3257 register struct backtrace *backlist = backtrace_list;
3258 register EMACS_INT i;
3260 CHECK_NUMBER (level);
3262 for (i = 0; backlist && i < XINT (level); i++)
3264 backlist = backlist->next;
3267 if (backlist)
3268 backlist->debug_on_exit = !NILP (flag);
3270 return flag;
3273 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3274 doc: /* Print a trace of Lisp function calls currently active.
3275 Output stream used is value of `standard-output'. */)
3276 (void)
3278 register struct backtrace *backlist = backtrace_list;
3279 Lisp_Object tail;
3280 Lisp_Object tem;
3281 struct gcpro gcpro1;
3282 Lisp_Object old_print_level = Vprint_level;
3284 if (NILP (Vprint_level))
3285 XSETFASTINT (Vprint_level, 8);
3287 tail = Qnil;
3288 GCPRO1 (tail);
3290 while (backlist)
3292 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3293 if (backlist->nargs == UNEVALLED)
3295 Fprin1 (Fcons (backlist->function, *backlist->args), Qnil);
3296 write_string ("\n", -1);
3298 else
3300 tem = backlist->function;
3301 Fprin1 (tem, Qnil); /* This can QUIT. */
3302 write_string ("(", -1);
3303 if (backlist->nargs == MANY)
3304 { /* FIXME: Can this happen? */
3305 bool later_arg = 0;
3306 for (tail = *backlist->args; !NILP (tail); tail = Fcdr (tail))
3308 if (later_arg)
3309 write_string (" ", -1);
3310 Fprin1 (Fcar (tail), Qnil);
3311 later_arg = 1;
3314 else
3316 ptrdiff_t i;
3317 for (i = 0; i < backlist->nargs; i++)
3319 if (i) write_string (" ", -1);
3320 Fprin1 (backlist->args[i], Qnil);
3323 write_string (")\n", -1);
3325 backlist = backlist->next;
3328 Vprint_level = old_print_level;
3329 UNGCPRO;
3330 return Qnil;
3333 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3334 doc: /* Return the function and arguments NFRAMES up from current execution point.
3335 If that frame has not evaluated the arguments yet (or is a special form),
3336 the value is (nil FUNCTION ARG-FORMS...).
3337 If that frame has evaluated its arguments and called its function already,
3338 the value is (t FUNCTION ARG-VALUES...).
3339 A &rest arg is represented as the tail of the list ARG-VALUES.
3340 FUNCTION is whatever was supplied as car of evaluated list,
3341 or a lambda expression for macro calls.
3342 If NFRAMES is more than the number of frames, the value is nil. */)
3343 (Lisp_Object nframes)
3345 register struct backtrace *backlist = backtrace_list;
3346 register EMACS_INT i;
3347 Lisp_Object tem;
3349 CHECK_NATNUM (nframes);
3351 /* Find the frame requested. */
3352 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3353 backlist = backlist->next;
3355 if (!backlist)
3356 return Qnil;
3357 if (backlist->nargs == UNEVALLED)
3358 return Fcons (Qnil, Fcons (backlist->function, *backlist->args));
3359 else
3361 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3362 tem = *backlist->args;
3363 else
3364 tem = Flist (backlist->nargs, backlist->args);
3366 return Fcons (Qt, Fcons (backlist->function, tem));
3371 #if BYTE_MARK_STACK
3372 void
3373 mark_backtrace (void)
3375 register struct backtrace *backlist;
3376 ptrdiff_t i;
3378 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3380 mark_object (*backlist->function);
3382 if (backlist->nargs == UNEVALLED
3383 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3384 i = 1;
3385 else
3386 i = backlist->nargs;
3387 while (i--)
3388 mark_object (backlist->args[i]);
3391 #endif
3393 void
3394 syms_of_eval (void)
3396 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3397 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3398 If Lisp code tries to increase the total number past this amount,
3399 an error is signaled.
3400 You can safely use a value considerably larger than the default value,
3401 if that proves inconveniently small. However, if you increase it too far,
3402 Emacs could run out of memory trying to make the stack bigger. */);
3404 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3405 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3407 This limit serves to catch infinite recursions for you before they cause
3408 actual stack overflow in C, which would be fatal for Emacs.
3409 You can safely make it considerably larger than its default value,
3410 if that proves inconveniently small. However, if you increase it too far,
3411 Emacs could overflow the real C stack, and crash. */);
3413 DEFVAR_LISP ("quit-flag", Vquit_flag,
3414 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3415 If the value is t, that means do an ordinary quit.
3416 If the value equals `throw-on-input', that means quit by throwing
3417 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3418 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3419 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3420 Vquit_flag = Qnil;
3422 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3423 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3424 Note that `quit-flag' will still be set by typing C-g,
3425 so a quit will be signaled as soon as `inhibit-quit' is nil.
3426 To prevent this happening, set `quit-flag' to nil
3427 before making `inhibit-quit' nil. */);
3428 Vinhibit_quit = Qnil;
3430 DEFSYM (Qinhibit_quit, "inhibit-quit");
3431 DEFSYM (Qautoload, "autoload");
3432 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3433 DEFSYM (Qmacro, "macro");
3434 DEFSYM (Qdeclare, "declare");
3436 /* Note that the process handling also uses Qexit, but we don't want
3437 to staticpro it twice, so we just do it here. */
3438 DEFSYM (Qexit, "exit");
3440 DEFSYM (Qinteractive, "interactive");
3441 DEFSYM (Qcommandp, "commandp");
3442 DEFSYM (Qand_rest, "&rest");
3443 DEFSYM (Qand_optional, "&optional");
3444 DEFSYM (Qclosure, "closure");
3445 DEFSYM (Qdebug, "debug");
3447 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3448 doc: /* Non-nil means never enter the debugger.
3449 Normally set while the debugger is already active, to avoid recursive
3450 invocations. */);
3451 Vinhibit_debugger = Qnil;
3453 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3454 doc: /* Non-nil means enter debugger if an error is signaled.
3455 Does not apply to errors handled by `condition-case' or those
3456 matched by `debug-ignored-errors'.
3457 If the value is a list, an error only means to enter the debugger
3458 if one of its condition symbols appears in the list.
3459 When you evaluate an expression interactively, this variable
3460 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3461 The command `toggle-debug-on-error' toggles this.
3462 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3463 Vdebug_on_error = Qnil;
3465 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3466 doc: /* List of errors for which the debugger should not be called.
3467 Each element may be a condition-name or a regexp that matches error messages.
3468 If any element applies to a given error, that error skips the debugger
3469 and just returns to top level.
3470 This overrides the variable `debug-on-error'.
3471 It does not apply to errors handled by `condition-case'. */);
3472 Vdebug_ignored_errors = Qnil;
3474 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3475 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3476 Does not apply if quit is handled by a `condition-case'. */);
3477 debug_on_quit = 0;
3479 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3480 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3482 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3483 doc: /* Non-nil means debugger may continue execution.
3484 This is nil when the debugger is called under circumstances where it
3485 might not be safe to continue. */);
3486 debugger_may_continue = 1;
3488 DEFVAR_LISP ("debugger", Vdebugger,
3489 doc: /* Function to call to invoke debugger.
3490 If due to frame exit, args are `exit' and the value being returned;
3491 this function's value will be returned instead of that.
3492 If due to error, args are `error' and a list of the args to `signal'.
3493 If due to `apply' or `funcall' entry, one arg, `lambda'.
3494 If due to `eval' entry, one arg, t. */);
3495 Vdebugger = Qnil;
3497 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3498 doc: /* If non-nil, this is a function for `signal' to call.
3499 It receives the same arguments that `signal' was given.
3500 The Edebug package uses this to regain control. */);
3501 Vsignal_hook_function = Qnil;
3503 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3504 doc: /* Non-nil means call the debugger regardless of condition handlers.
3505 Note that `debug-on-error', `debug-on-quit' and friends
3506 still determine whether to handle the particular condition. */);
3507 Vdebug_on_signal = Qnil;
3509 /* When lexical binding is being used,
3510 Vinternal_interpreter_environment is non-nil, and contains an alist
3511 of lexically-bound variable, or (t), indicating an empty
3512 environment. The lisp name of this variable would be
3513 `internal-interpreter-environment' if it weren't hidden.
3514 Every element of this list can be either a cons (VAR . VAL)
3515 specifying a lexical binding, or a single symbol VAR indicating
3516 that this variable should use dynamic scoping. */
3517 DEFSYM (Qinternal_interpreter_environment,
3518 "internal-interpreter-environment");
3519 DEFVAR_LISP ("internal-interpreter-environment",
3520 Vinternal_interpreter_environment,
3521 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3522 When lexical binding is not being used, this variable is nil.
3523 A value of `(t)' indicates an empty environment, otherwise it is an
3524 alist of active lexical bindings. */);
3525 Vinternal_interpreter_environment = Qnil;
3526 /* Don't export this variable to Elisp, so no one can mess with it
3527 (Just imagine if someone makes it buffer-local). */
3528 Funintern (Qinternal_interpreter_environment, Qnil);
3530 DEFSYM (Vrun_hooks, "run-hooks");
3532 staticpro (&Vautoload_queue);
3533 Vautoload_queue = Qnil;
3534 staticpro (&Vsignaling_function);
3535 Vsignaling_function = Qnil;
3537 inhibit_lisp_code = Qnil;
3539 defsubr (&Sor);
3540 defsubr (&Sand);
3541 defsubr (&Sif);
3542 defsubr (&Scond);
3543 defsubr (&Sprogn);
3544 defsubr (&Sprog1);
3545 defsubr (&Sprog2);
3546 defsubr (&Ssetq);
3547 defsubr (&Squote);
3548 defsubr (&Sfunction);
3549 defsubr (&Sdefvar);
3550 defsubr (&Sdefvaralias);
3551 defsubr (&Sdefconst);
3552 defsubr (&Smake_var_non_special);
3553 defsubr (&Slet);
3554 defsubr (&SletX);
3555 defsubr (&Swhile);
3556 defsubr (&Smacroexpand);
3557 defsubr (&Scatch);
3558 defsubr (&Sthrow);
3559 defsubr (&Sunwind_protect);
3560 defsubr (&Scondition_case);
3561 defsubr (&Ssignal);
3562 defsubr (&Sinteractive_p);
3563 defsubr (&Scalled_interactively_p);
3564 defsubr (&Scommandp);
3565 defsubr (&Sautoload);
3566 defsubr (&Sautoload_do_load);
3567 defsubr (&Seval);
3568 defsubr (&Sapply);
3569 defsubr (&Sfuncall);
3570 defsubr (&Srun_hooks);
3571 defsubr (&Srun_hook_with_args);
3572 defsubr (&Srun_hook_with_args_until_success);
3573 defsubr (&Srun_hook_with_args_until_failure);
3574 defsubr (&Srun_hook_wrapped);
3575 defsubr (&Sfetch_bytecode);
3576 defsubr (&Sbacktrace_debug);
3577 defsubr (&Sbacktrace);
3578 defsubr (&Sbacktrace_frame);
3579 defsubr (&Sspecial_variable_p);
3580 defsubr (&Sfunctionp);