Fix following doc-links in `widget-documentation-link-action'
[emacs.git] / src / eval.c
blob490226149ffba30e5829a9f44af4a23acd3e148e
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "commands.h"
28 #include "keyboard.h"
29 #include "dispextern.h"
30 #include "buffer.h"
32 /* Chain of condition and catch handlers currently in effect. */
34 struct handler *handlerlist;
36 #ifdef DEBUG_GCPRO
37 /* Count levels of GCPRO to detect failure to UNGCPRO. */
38 int gcpro_level;
39 #endif
41 /* Non-nil means record all fset's and provide's, to be undone
42 if the file being autoloaded is not fully loaded.
43 They are recorded by being consed onto the front of Vautoload_queue:
44 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
46 Lisp_Object Vautoload_queue;
48 /* This holds either the symbol `run-hooks' or nil.
49 It is nil at an early stage of startup, and when Emacs
50 is shutting down. */
51 Lisp_Object Vrun_hooks;
53 /* Current number of specbindings allocated in specpdl, not counting
54 the dummy entry specpdl[-1]. */
56 ptrdiff_t specpdl_size;
58 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
59 only so that its address can be taken. */
61 union specbinding *specpdl;
63 /* Pointer to first unused element in specpdl. */
65 union specbinding *specpdl_ptr;
67 /* Depth in Lisp evaluations and function calls. */
69 EMACS_INT lisp_eval_depth;
71 /* The value of num_nonmacro_input_events as of the last time we
72 started to enter the debugger. If we decide to enter the debugger
73 again when this is still equal to num_nonmacro_input_events, then we
74 know that the debugger itself has an error, and we should just
75 signal the error instead of entering an infinite loop of debugger
76 invocations. */
78 static EMACS_INT when_entered_debugger;
80 /* The function from which the last `signal' was called. Set in
81 Fsignal. */
82 /* FIXME: We should probably get rid of this! */
83 Lisp_Object Vsignaling_function;
85 /* If non-nil, Lisp code must not be run since some part of Emacs is in
86 an inconsistent state. Currently unused. */
87 Lisp_Object inhibit_lisp_code;
89 /* These would ordinarily be static, but they need to be visible to GDB. */
90 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
91 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
92 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
93 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
94 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
96 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
97 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
99 static Lisp_Object
100 specpdl_symbol (union specbinding *pdl)
102 eassert (pdl->kind >= SPECPDL_LET);
103 return pdl->let.symbol;
106 static Lisp_Object
107 specpdl_old_value (union specbinding *pdl)
109 eassert (pdl->kind >= SPECPDL_LET);
110 return pdl->let.old_value;
113 static void
114 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
116 eassert (pdl->kind >= SPECPDL_LET);
117 pdl->let.old_value = val;
120 static Lisp_Object
121 specpdl_where (union specbinding *pdl)
123 eassert (pdl->kind > SPECPDL_LET);
124 return pdl->let.where;
127 static Lisp_Object
128 specpdl_arg (union specbinding *pdl)
130 eassert (pdl->kind == SPECPDL_UNWIND);
131 return pdl->unwind.arg;
134 Lisp_Object
135 backtrace_function (union specbinding *pdl)
137 eassert (pdl->kind == SPECPDL_BACKTRACE);
138 return pdl->bt.function;
141 static ptrdiff_t
142 backtrace_nargs (union specbinding *pdl)
144 eassert (pdl->kind == SPECPDL_BACKTRACE);
145 return pdl->bt.nargs;
148 Lisp_Object *
149 backtrace_args (union specbinding *pdl)
151 eassert (pdl->kind == SPECPDL_BACKTRACE);
152 return pdl->bt.args;
155 static bool
156 backtrace_debug_on_exit (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_BACKTRACE);
159 return pdl->bt.debug_on_exit;
162 /* Functions to modify slots of backtrace records. */
164 static void
165 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
167 eassert (pdl->kind == SPECPDL_BACKTRACE);
168 pdl->bt.args = args;
169 pdl->bt.nargs = nargs;
172 static void
173 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
175 eassert (pdl->kind == SPECPDL_BACKTRACE);
176 pdl->bt.debug_on_exit = doe;
179 /* Helper functions to scan the backtrace. */
181 bool
182 backtrace_p (union specbinding *pdl)
183 { return pdl >= specpdl; }
185 union specbinding *
186 backtrace_top (void)
188 union specbinding *pdl = specpdl_ptr - 1;
189 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
190 pdl--;
191 return pdl;
194 union specbinding *
195 backtrace_next (union specbinding *pdl)
197 pdl--;
198 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
199 pdl--;
200 return pdl;
204 void
205 init_eval_once (void)
207 enum { size = 50 };
208 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
209 specpdl_size = size;
210 specpdl = specpdl_ptr = pdlvec + 1;
211 /* Don't forget to update docs (lispref node "Local Variables"). */
212 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
213 max_lisp_eval_depth = 800;
215 Vrun_hooks = Qnil;
218 static struct handler handlerlist_sentinel;
220 void
221 init_eval (void)
223 specpdl_ptr = specpdl;
224 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
225 This is important since handlerlist->nextfree holds the freelist
226 which would otherwise leak every time we unwind back to top-level. */
227 struct handler *c;
228 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
229 PUSH_HANDLER (c, Qunbound, CATCHER);
230 eassert (c == &handlerlist_sentinel);
231 handlerlist_sentinel.nextfree = NULL;
232 handlerlist_sentinel.next = NULL;
234 Vquit_flag = Qnil;
235 debug_on_next_call = 0;
236 lisp_eval_depth = 0;
237 #ifdef DEBUG_GCPRO
238 gcpro_level = 0;
239 #endif
240 /* This is less than the initial value of num_nonmacro_input_events. */
241 when_entered_debugger = -1;
244 /* Unwind-protect function used by call_debugger. */
246 static void
247 restore_stack_limits (Lisp_Object data)
249 max_specpdl_size = XINT (XCAR (data));
250 max_lisp_eval_depth = XINT (XCDR (data));
253 static void grow_specpdl (void);
255 /* Call the Lisp debugger, giving it argument ARG. */
257 Lisp_Object
258 call_debugger (Lisp_Object arg)
260 bool debug_while_redisplaying;
261 ptrdiff_t count = SPECPDL_INDEX ();
262 Lisp_Object val;
263 EMACS_INT old_depth = max_lisp_eval_depth;
264 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
265 EMACS_INT old_max = max (max_specpdl_size, count);
267 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
268 max_lisp_eval_depth = lisp_eval_depth + 40;
270 /* While debugging Bug#16603, previous value of 100 was found
271 too small to avoid specpdl overflow in the debugger itself. */
272 if (max_specpdl_size - 200 < count)
273 max_specpdl_size = count + 200;
275 if (old_max == count)
277 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
278 specpdl_ptr--;
279 grow_specpdl ();
282 /* Restore limits after leaving the debugger. */
283 record_unwind_protect (restore_stack_limits,
284 Fcons (make_number (old_max),
285 make_number (old_depth)));
287 #ifdef HAVE_WINDOW_SYSTEM
288 if (display_hourglass_p)
289 cancel_hourglass ();
290 #endif
292 debug_on_next_call = 0;
293 when_entered_debugger = num_nonmacro_input_events;
295 /* Resetting redisplaying_p to 0 makes sure that debug output is
296 displayed if the debugger is invoked during redisplay. */
297 debug_while_redisplaying = redisplaying_p;
298 redisplaying_p = 0;
299 specbind (intern ("debugger-may-continue"),
300 debug_while_redisplaying ? Qnil : Qt);
301 specbind (Qinhibit_redisplay, Qnil);
302 specbind (Qinhibit_debugger, Qt);
304 #if 0 /* Binding this prevents execution of Lisp code during
305 redisplay, which necessarily leads to display problems. */
306 specbind (Qinhibit_eval_during_redisplay, Qt);
307 #endif
309 val = apply1 (Vdebugger, arg);
311 /* Interrupting redisplay and resuming it later is not safe under
312 all circumstances. So, when the debugger returns, abort the
313 interrupted redisplay by going back to the top-level. */
314 if (debug_while_redisplaying)
315 Ftop_level ();
317 return unbind_to (count, val);
320 static void
321 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
323 debug_on_next_call = 0;
324 set_backtrace_debug_on_exit (specpdl + count, true);
325 call_debugger (list1 (code));
328 /* NOTE!!! Every function that can call EVAL must protect its args
329 and temporaries from garbage collection while it needs them.
330 The definition of `For' shows what you have to do. */
332 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
333 doc: /* Eval args until one of them yields non-nil, then return that value.
334 The remaining args are not evalled at all.
335 If all args return nil, return nil.
336 usage: (or CONDITIONS...) */)
337 (Lisp_Object args)
339 register Lisp_Object val = Qnil;
340 struct gcpro gcpro1;
342 GCPRO1 (args);
344 while (CONSP (args))
346 val = eval_sub (XCAR (args));
347 if (!NILP (val))
348 break;
349 args = XCDR (args);
352 UNGCPRO;
353 return val;
356 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
357 doc: /* Eval args until one of them yields nil, then return nil.
358 The remaining args are not evalled at all.
359 If no arg yields nil, return the last arg's value.
360 usage: (and CONDITIONS...) */)
361 (Lisp_Object args)
363 register Lisp_Object val = Qt;
364 struct gcpro gcpro1;
366 GCPRO1 (args);
368 while (CONSP (args))
370 val = eval_sub (XCAR (args));
371 if (NILP (val))
372 break;
373 args = XCDR (args);
376 UNGCPRO;
377 return val;
380 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
381 doc: /* If COND yields non-nil, do THEN, else do ELSE...
382 Returns the value of THEN or the value of the last of the ELSE's.
383 THEN must be one expression, but ELSE... can be zero or more expressions.
384 If COND yields nil, and there are no ELSE's, the value is nil.
385 usage: (if COND THEN ELSE...) */)
386 (Lisp_Object args)
388 Lisp_Object cond;
389 struct gcpro gcpro1;
391 GCPRO1 (args);
392 cond = eval_sub (XCAR (args));
393 UNGCPRO;
395 if (!NILP (cond))
396 return eval_sub (Fcar (XCDR (args)));
397 return Fprogn (XCDR (XCDR (args)));
400 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
401 doc: /* Try each clause until one succeeds.
402 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
403 and, if the value is non-nil, this clause succeeds:
404 then the expressions in BODY are evaluated and the last one's
405 value is the value of the cond-form.
406 If a clause has one element, as in (CONDITION), then the cond-form
407 returns CONDITION's value, if that is non-nil.
408 If no clause succeeds, cond returns nil.
409 usage: (cond CLAUSES...) */)
410 (Lisp_Object args)
412 Lisp_Object val = args;
413 struct gcpro gcpro1;
415 GCPRO1 (args);
416 while (CONSP (args))
418 Lisp_Object clause = XCAR (args);
419 val = eval_sub (Fcar (clause));
420 if (!NILP (val))
422 if (!NILP (XCDR (clause)))
423 val = Fprogn (XCDR (clause));
424 break;
426 args = XCDR (args);
428 UNGCPRO;
430 return val;
433 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
434 doc: /* Eval BODY forms sequentially and return value of last one.
435 usage: (progn BODY...) */)
436 (Lisp_Object body)
438 Lisp_Object val = Qnil;
439 struct gcpro gcpro1;
441 GCPRO1 (body);
443 while (CONSP (body))
445 val = eval_sub (XCAR (body));
446 body = XCDR (body);
449 UNGCPRO;
450 return val;
453 /* Evaluate BODY sequentially, discarding its value. Suitable for
454 record_unwind_protect. */
456 void
457 unwind_body (Lisp_Object body)
459 Fprogn (body);
462 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
463 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
464 The value of FIRST is saved during the evaluation of the remaining args,
465 whose values are discarded.
466 usage: (prog1 FIRST BODY...) */)
467 (Lisp_Object args)
469 Lisp_Object val;
470 Lisp_Object args_left;
471 struct gcpro gcpro1, gcpro2;
473 args_left = args;
474 val = args;
475 GCPRO2 (args, val);
477 val = eval_sub (XCAR (args_left));
478 while (CONSP (args_left = XCDR (args_left)))
479 eval_sub (XCAR (args_left));
481 UNGCPRO;
482 return val;
485 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
486 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
487 The value of FORM2 is saved during the evaluation of the
488 remaining args, whose values are discarded.
489 usage: (prog2 FORM1 FORM2 BODY...) */)
490 (Lisp_Object args)
492 struct gcpro gcpro1;
494 GCPRO1 (args);
495 eval_sub (XCAR (args));
496 UNGCPRO;
497 return Fprog1 (XCDR (args));
500 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
501 doc: /* Set each SYM to the value of its VAL.
502 The symbols SYM are variables; they are literal (not evaluated).
503 The values VAL are expressions; they are evaluated.
504 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
505 The second VAL is not computed until after the first SYM is set, and so on;
506 each VAL can use the new value of variables set earlier in the `setq'.
507 The return value of the `setq' form is the value of the last VAL.
508 usage: (setq [SYM VAL]...) */)
509 (Lisp_Object args)
511 Lisp_Object val, sym, lex_binding;
513 val = args;
514 if (CONSP (args))
516 Lisp_Object args_left = args;
517 struct gcpro gcpro1;
518 GCPRO1 (args);
522 val = eval_sub (Fcar (XCDR (args_left)));
523 sym = XCAR (args_left);
525 /* Like for eval_sub, we do not check declared_special here since
526 it's been done when let-binding. */
527 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
528 && SYMBOLP (sym)
529 && !NILP (lex_binding
530 = Fassq (sym, Vinternal_interpreter_environment)))
531 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
532 else
533 Fset (sym, val); /* SYM is dynamically bound. */
535 args_left = Fcdr (XCDR (args_left));
537 while (CONSP (args_left));
539 UNGCPRO;
542 return val;
545 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
546 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
547 Warning: `quote' does not construct its return value, but just returns
548 the value that was pre-constructed by the Lisp reader (see info node
549 `(elisp)Printed Representation').
550 This means that '(a . b) is not identical to (cons 'a 'b): the former
551 does not cons. Quoting should be reserved for constants that will
552 never be modified by side-effects, unless you like self-modifying code.
553 See the common pitfall in info node `(elisp)Rearrangement' for an example
554 of unexpected results when a quoted object is modified.
555 usage: (quote ARG) */)
556 (Lisp_Object args)
558 if (CONSP (XCDR (args)))
559 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
560 return XCAR (args);
563 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
564 doc: /* Like `quote', but preferred for objects which are functions.
565 In byte compilation, `function' causes its argument to be compiled.
566 `quote' cannot do that.
567 usage: (function ARG) */)
568 (Lisp_Object args)
570 Lisp_Object quoted = XCAR (args);
572 if (CONSP (XCDR (args)))
573 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
575 if (!NILP (Vinternal_interpreter_environment)
576 && CONSP (quoted)
577 && EQ (XCAR (quoted), Qlambda))
578 { /* This is a lambda expression within a lexical environment;
579 return an interpreted closure instead of a simple lambda. */
580 Lisp_Object cdr = XCDR (quoted);
581 Lisp_Object tmp = cdr;
582 if (CONSP (tmp)
583 && (tmp = XCDR (tmp), CONSP (tmp))
584 && (tmp = XCAR (tmp), CONSP (tmp))
585 && (EQ (QCdocumentation, XCAR (tmp))))
586 { /* Handle the special (:documentation <form>) to build the docstring
587 dynamically. */
588 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
589 CHECK_STRING (docstring);
590 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
592 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
593 cdr));
595 else
596 /* Simply quote the argument. */
597 return quoted;
601 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
602 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
603 Aliased variables always have the same value; setting one sets the other.
604 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
605 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
606 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
607 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
608 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
609 The return value is BASE-VARIABLE. */)
610 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
612 struct Lisp_Symbol *sym;
614 CHECK_SYMBOL (new_alias);
615 CHECK_SYMBOL (base_variable);
617 sym = XSYMBOL (new_alias);
619 if (sym->constant)
620 /* Not sure why, but why not? */
621 error ("Cannot make a constant an alias");
623 switch (sym->redirect)
625 case SYMBOL_FORWARDED:
626 error ("Cannot make an internal variable an alias");
627 case SYMBOL_LOCALIZED:
628 error ("Don't know how to make a localized variable an alias");
631 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
632 If n_a is bound, but b_v is not, set the value of b_v to n_a,
633 so that old-code that affects n_a before the aliasing is setup
634 still works. */
635 if (NILP (Fboundp (base_variable)))
636 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
639 union specbinding *p;
641 for (p = specpdl_ptr; p > specpdl; )
642 if ((--p)->kind >= SPECPDL_LET
643 && (EQ (new_alias, specpdl_symbol (p))))
644 error ("Don't know how to make a let-bound variable an alias");
647 sym->declared_special = 1;
648 XSYMBOL (base_variable)->declared_special = 1;
649 sym->redirect = SYMBOL_VARALIAS;
650 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
651 sym->constant = SYMBOL_CONSTANT_P (base_variable);
652 LOADHIST_ATTACH (new_alias);
653 /* Even if docstring is nil: remove old docstring. */
654 Fput (new_alias, Qvariable_documentation, docstring);
656 return base_variable;
659 static union specbinding *
660 default_toplevel_binding (Lisp_Object symbol)
662 union specbinding *binding = NULL;
663 union specbinding *pdl = specpdl_ptr;
664 while (pdl > specpdl)
666 switch ((--pdl)->kind)
668 case SPECPDL_LET_DEFAULT:
669 case SPECPDL_LET:
670 if (EQ (specpdl_symbol (pdl), symbol))
671 binding = pdl;
672 break;
675 return binding;
678 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
679 doc: /* Return SYMBOL's toplevel default value.
680 "Toplevel" means outside of any let binding. */)
681 (Lisp_Object symbol)
683 union specbinding *binding = default_toplevel_binding (symbol);
684 Lisp_Object value
685 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
686 if (!EQ (value, Qunbound))
687 return value;
688 xsignal1 (Qvoid_variable, symbol);
691 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
692 Sset_default_toplevel_value, 2, 2, 0,
693 doc: /* Set SYMBOL's toplevel default value to VALUE.
694 "Toplevel" means outside of any let binding. */)
695 (Lisp_Object symbol, Lisp_Object value)
697 union specbinding *binding = default_toplevel_binding (symbol);
698 if (binding)
699 set_specpdl_old_value (binding, value);
700 else
701 Fset_default (symbol, value);
702 return Qnil;
705 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
706 doc: /* Define SYMBOL as a variable, and return SYMBOL.
707 You are not required to define a variable in order to use it, but
708 defining it lets you supply an initial value and documentation, which
709 can be referred to by the Emacs help facilities and other programming
710 tools. The `defvar' form also declares the variable as \"special\",
711 so that it is always dynamically bound even if `lexical-binding' is t.
713 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
714 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
715 default value is what is set; buffer-local values are not affected.
716 If INITVALUE is missing, SYMBOL's value is not set.
718 If SYMBOL has a local binding, then this form affects the local
719 binding. This is usually not what you want. Thus, if you need to
720 load a file defining variables, with this form or with `defconst' or
721 `defcustom', you should always load that file _outside_ any bindings
722 for these variables. \(`defconst' and `defcustom' behave similarly in
723 this respect.)
725 The optional argument DOCSTRING is a documentation string for the
726 variable.
728 To define a user option, use `defcustom' instead of `defvar'.
729 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
730 (Lisp_Object args)
732 Lisp_Object sym, tem, tail;
734 sym = XCAR (args);
735 tail = XCDR (args);
737 if (CONSP (tail))
739 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
740 error ("Too many arguments");
742 tem = Fdefault_boundp (sym);
744 /* Do it before evaluating the initial value, for self-references. */
745 XSYMBOL (sym)->declared_special = 1;
747 if (NILP (tem))
748 Fset_default (sym, eval_sub (XCAR (tail)));
749 else
750 { /* Check if there is really a global binding rather than just a let
751 binding that shadows the global unboundness of the var. */
752 union specbinding *binding = default_toplevel_binding (sym);
753 if (binding && EQ (specpdl_old_value (binding), Qunbound))
755 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
758 tail = XCDR (tail);
759 tem = Fcar (tail);
760 if (!NILP (tem))
762 if (!NILP (Vpurify_flag))
763 tem = Fpurecopy (tem);
764 Fput (sym, Qvariable_documentation, tem);
766 LOADHIST_ATTACH (sym);
768 else if (!NILP (Vinternal_interpreter_environment)
769 && !XSYMBOL (sym)->declared_special)
770 /* A simple (defvar foo) with lexical scoping does "nothing" except
771 declare that var to be dynamically scoped *locally* (i.e. within
772 the current file or let-block). */
773 Vinternal_interpreter_environment
774 = Fcons (sym, Vinternal_interpreter_environment);
775 else
777 /* Simple (defvar <var>) should not count as a definition at all.
778 It could get in the way of other definitions, and unloading this
779 package could try to make the variable unbound. */
782 return sym;
785 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
786 doc: /* Define SYMBOL as a constant variable.
787 This declares that neither programs nor users should ever change the
788 value. This constancy is not actually enforced by Emacs Lisp, but
789 SYMBOL is marked as a special variable so that it is never lexically
790 bound.
792 The `defconst' form always sets the value of SYMBOL to the result of
793 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
794 what is set; buffer-local values are not affected. If SYMBOL has a
795 local binding, then this form sets the local binding's value.
796 However, you should normally not make local bindings for variables
797 defined with this form.
799 The optional DOCSTRING specifies the variable's documentation string.
800 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
801 (Lisp_Object args)
803 Lisp_Object sym, tem;
805 sym = XCAR (args);
806 if (CONSP (Fcdr (XCDR (XCDR (args)))))
807 error ("Too many arguments");
809 tem = eval_sub (Fcar (XCDR (args)));
810 if (!NILP (Vpurify_flag))
811 tem = Fpurecopy (tem);
812 Fset_default (sym, tem);
813 XSYMBOL (sym)->declared_special = 1;
814 tem = Fcar (XCDR (XCDR (args)));
815 if (!NILP (tem))
817 if (!NILP (Vpurify_flag))
818 tem = Fpurecopy (tem);
819 Fput (sym, Qvariable_documentation, tem);
821 Fput (sym, Qrisky_local_variable, Qt);
822 LOADHIST_ATTACH (sym);
823 return sym;
826 /* Make SYMBOL lexically scoped. */
827 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
828 Smake_var_non_special, 1, 1, 0,
829 doc: /* Internal function. */)
830 (Lisp_Object symbol)
832 CHECK_SYMBOL (symbol);
833 XSYMBOL (symbol)->declared_special = 0;
834 return Qnil;
838 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
839 doc: /* Bind variables according to VARLIST then eval BODY.
840 The value of the last form in BODY is returned.
841 Each element of VARLIST is a symbol (which is bound to nil)
842 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
843 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
844 usage: (let* VARLIST BODY...) */)
845 (Lisp_Object args)
847 Lisp_Object varlist, var, val, elt, lexenv;
848 ptrdiff_t count = SPECPDL_INDEX ();
849 struct gcpro gcpro1, gcpro2, gcpro3;
851 GCPRO3 (args, elt, varlist);
853 lexenv = Vinternal_interpreter_environment;
855 varlist = XCAR (args);
856 while (CONSP (varlist))
858 QUIT;
860 elt = XCAR (varlist);
861 if (SYMBOLP (elt))
863 var = elt;
864 val = Qnil;
866 else if (! NILP (Fcdr (Fcdr (elt))))
867 signal_error ("`let' bindings can have only one value-form", elt);
868 else
870 var = Fcar (elt);
871 val = eval_sub (Fcar (Fcdr (elt)));
874 if (!NILP (lexenv) && SYMBOLP (var)
875 && !XSYMBOL (var)->declared_special
876 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
877 /* Lexically bind VAR by adding it to the interpreter's binding
878 alist. */
880 Lisp_Object newenv
881 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
882 if (EQ (Vinternal_interpreter_environment, lexenv))
883 /* Save the old lexical environment on the specpdl stack,
884 but only for the first lexical binding, since we'll never
885 need to revert to one of the intermediate ones. */
886 specbind (Qinternal_interpreter_environment, newenv);
887 else
888 Vinternal_interpreter_environment = newenv;
890 else
891 specbind (var, val);
893 varlist = XCDR (varlist);
895 UNGCPRO;
896 val = Fprogn (XCDR (args));
897 return unbind_to (count, val);
900 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
901 doc: /* Bind variables according to VARLIST then eval BODY.
902 The value of the last form in BODY is returned.
903 Each element of VARLIST is a symbol (which is bound to nil)
904 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
905 All the VALUEFORMs are evalled before any symbols are bound.
906 usage: (let VARLIST BODY...) */)
907 (Lisp_Object args)
909 Lisp_Object *temps, tem, lexenv;
910 register Lisp_Object elt, varlist;
911 ptrdiff_t count = SPECPDL_INDEX ();
912 ptrdiff_t argnum;
913 struct gcpro gcpro1, gcpro2;
914 USE_SAFE_ALLOCA;
916 varlist = XCAR (args);
918 /* Make space to hold the values to give the bound variables. */
919 elt = Flength (varlist);
920 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
922 /* Compute the values and store them in `temps'. */
924 GCPRO2 (args, *temps);
925 gcpro2.nvars = 0;
927 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
929 QUIT;
930 elt = XCAR (varlist);
931 if (SYMBOLP (elt))
932 temps [argnum++] = Qnil;
933 else if (! NILP (Fcdr (Fcdr (elt))))
934 signal_error ("`let' bindings can have only one value-form", elt);
935 else
936 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
937 gcpro2.nvars = argnum;
939 UNGCPRO;
941 lexenv = Vinternal_interpreter_environment;
943 varlist = XCAR (args);
944 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
946 Lisp_Object var;
948 elt = XCAR (varlist);
949 var = SYMBOLP (elt) ? elt : Fcar (elt);
950 tem = temps[argnum++];
952 if (!NILP (lexenv) && SYMBOLP (var)
953 && !XSYMBOL (var)->declared_special
954 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
955 /* Lexically bind VAR by adding it to the lexenv alist. */
956 lexenv = Fcons (Fcons (var, tem), lexenv);
957 else
958 /* Dynamically bind VAR. */
959 specbind (var, tem);
962 if (!EQ (lexenv, Vinternal_interpreter_environment))
963 /* Instantiate a new lexical environment. */
964 specbind (Qinternal_interpreter_environment, lexenv);
966 elt = Fprogn (XCDR (args));
967 SAFE_FREE ();
968 return unbind_to (count, elt);
971 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
972 doc: /* If TEST yields non-nil, eval BODY... and repeat.
973 The order of execution is thus TEST, BODY, TEST, BODY and so on
974 until TEST returns nil.
975 usage: (while TEST BODY...) */)
976 (Lisp_Object args)
978 Lisp_Object test, body;
979 struct gcpro gcpro1, gcpro2;
981 GCPRO2 (test, body);
983 test = XCAR (args);
984 body = XCDR (args);
985 while (!NILP (eval_sub (test)))
987 QUIT;
988 Fprogn (body);
991 UNGCPRO;
992 return Qnil;
995 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
996 doc: /* Return result of expanding macros at top level of FORM.
997 If FORM is not a macro call, it is returned unchanged.
998 Otherwise, the macro is expanded and the expansion is considered
999 in place of FORM. When a non-macro-call results, it is returned.
1001 The second optional arg ENVIRONMENT specifies an environment of macro
1002 definitions to shadow the loaded ones for use in file byte-compilation. */)
1003 (Lisp_Object form, Lisp_Object environment)
1005 /* With cleanups from Hallvard Furuseth. */
1006 register Lisp_Object expander, sym, def, tem;
1008 while (1)
1010 /* Come back here each time we expand a macro call,
1011 in case it expands into another macro call. */
1012 if (!CONSP (form))
1013 break;
1014 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1015 def = sym = XCAR (form);
1016 tem = Qnil;
1017 /* Trace symbols aliases to other symbols
1018 until we get a symbol that is not an alias. */
1019 while (SYMBOLP (def))
1021 QUIT;
1022 sym = def;
1023 tem = Fassq (sym, environment);
1024 if (NILP (tem))
1026 def = XSYMBOL (sym)->function;
1027 if (!NILP (def))
1028 continue;
1030 break;
1032 /* Right now TEM is the result from SYM in ENVIRONMENT,
1033 and if TEM is nil then DEF is SYM's function definition. */
1034 if (NILP (tem))
1036 /* SYM is not mentioned in ENVIRONMENT.
1037 Look at its function definition. */
1038 struct gcpro gcpro1;
1039 GCPRO1 (form);
1040 def = Fautoload_do_load (def, sym, Qmacro);
1041 UNGCPRO;
1042 if (!CONSP (def))
1043 /* Not defined or definition not suitable. */
1044 break;
1045 if (!EQ (XCAR (def), Qmacro))
1046 break;
1047 else expander = XCDR (def);
1049 else
1051 expander = XCDR (tem);
1052 if (NILP (expander))
1053 break;
1056 Lisp_Object newform = apply1 (expander, XCDR (form));
1057 if (EQ (form, newform))
1058 break;
1059 else
1060 form = newform;
1063 return form;
1066 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1067 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1068 TAG is evalled to get the tag to use; it must not be nil.
1070 Then the BODY is executed.
1071 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1072 If no throw happens, `catch' returns the value of the last BODY form.
1073 If a throw happens, it specifies the value to return from `catch'.
1074 usage: (catch TAG BODY...) */)
1075 (Lisp_Object args)
1077 register Lisp_Object tag;
1078 struct gcpro gcpro1;
1080 GCPRO1 (args);
1081 tag = eval_sub (XCAR (args));
1082 UNGCPRO;
1083 return internal_catch (tag, Fprogn, XCDR (args));
1086 /* Assert that E is true, as a comment only. Use this instead of
1087 eassert (E) when E contains variables that might be clobbered by a
1088 longjmp. */
1090 #define clobbered_eassert(E) ((void) 0)
1092 /* Set up a catch, then call C function FUNC on argument ARG.
1093 FUNC should return a Lisp_Object.
1094 This is how catches are done from within C code. */
1096 Lisp_Object
1097 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1099 /* This structure is made part of the chain `catchlist'. */
1100 struct handler *c;
1102 /* Fill in the components of c, and put it on the list. */
1103 PUSH_HANDLER (c, tag, CATCHER);
1105 /* Call FUNC. */
1106 if (! sys_setjmp (c->jmp))
1108 Lisp_Object val = (*func) (arg);
1109 clobbered_eassert (handlerlist == c);
1110 handlerlist = handlerlist->next;
1111 return val;
1113 else
1114 { /* Throw works by a longjmp that comes right here. */
1115 Lisp_Object val = handlerlist->val;
1116 clobbered_eassert (handlerlist == c);
1117 handlerlist = handlerlist->next;
1118 return val;
1122 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1123 jump to that CATCH, returning VALUE as the value of that catch.
1125 This is the guts of Fthrow and Fsignal; they differ only in the way
1126 they choose the catch tag to throw to. A catch tag for a
1127 condition-case form has a TAG of Qnil.
1129 Before each catch is discarded, unbind all special bindings and
1130 execute all unwind-protect clauses made above that catch. Unwind
1131 the handler stack as we go, so that the proper handlers are in
1132 effect for each unwind-protect clause we run. At the end, restore
1133 some static info saved in CATCH, and longjmp to the location
1134 specified there.
1136 This is used for correct unwinding in Fthrow and Fsignal. */
1138 static _Noreturn void
1139 unwind_to_catch (struct handler *catch, Lisp_Object value)
1141 bool last_time;
1143 eassert (catch->next);
1145 /* Save the value in the tag. */
1146 catch->val = value;
1148 /* Restore certain special C variables. */
1149 set_poll_suppress_count (catch->poll_suppress_count);
1150 unblock_input_to (catch->interrupt_input_blocked);
1151 immediate_quit = 0;
1155 /* Unwind the specpdl stack, and then restore the proper set of
1156 handlers. */
1157 unbind_to (handlerlist->pdlcount, Qnil);
1158 last_time = handlerlist == catch;
1159 if (! last_time)
1160 handlerlist = handlerlist->next;
1162 while (! last_time);
1164 eassert (handlerlist == catch);
1166 byte_stack_list = catch->byte_stack;
1167 gcprolist = catch->gcpro;
1168 #ifdef DEBUG_GCPRO
1169 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1170 #endif
1171 lisp_eval_depth = catch->lisp_eval_depth;
1173 sys_longjmp (catch->jmp, 1);
1176 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1177 doc: /* Throw to the catch for TAG and return VALUE from it.
1178 Both TAG and VALUE are evalled. */
1179 attributes: noreturn)
1180 (register Lisp_Object tag, Lisp_Object value)
1182 struct handler *c;
1184 if (!NILP (tag))
1185 for (c = handlerlist; c; c = c->next)
1187 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1188 unwind_to_catch (c, value);
1190 xsignal2 (Qno_catch, tag, value);
1194 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1195 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1196 If BODYFORM completes normally, its value is returned
1197 after executing the UNWINDFORMS.
1198 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1199 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1200 (Lisp_Object args)
1202 Lisp_Object val;
1203 ptrdiff_t count = SPECPDL_INDEX ();
1205 record_unwind_protect (unwind_body, XCDR (args));
1206 val = eval_sub (XCAR (args));
1207 return unbind_to (count, val);
1210 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1211 doc: /* Regain control when an error is signaled.
1212 Executes BODYFORM and returns its value if no error happens.
1213 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1214 where the BODY is made of Lisp expressions.
1216 A handler is applicable to an error
1217 if CONDITION-NAME is one of the error's condition names.
1218 If an error happens, the first applicable handler is run.
1220 The car of a handler may be a list of condition names instead of a
1221 single condition name; then it handles all of them. If the special
1222 condition name `debug' is present in this list, it allows another
1223 condition in the list to run the debugger if `debug-on-error' and the
1224 other usual mechanisms says it should (otherwise, `condition-case'
1225 suppresses the debugger).
1227 When a handler handles an error, control returns to the `condition-case'
1228 and it executes the handler's BODY...
1229 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1230 \(If VAR is nil, the handler can't access that information.)
1231 Then the value of the last BODY form is returned from the `condition-case'
1232 expression.
1234 See also the function `signal' for more info.
1235 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1236 (Lisp_Object args)
1238 Lisp_Object var = XCAR (args);
1239 Lisp_Object bodyform = XCAR (XCDR (args));
1240 Lisp_Object handlers = XCDR (XCDR (args));
1242 return internal_lisp_condition_case (var, bodyform, handlers);
1245 /* Like Fcondition_case, but the args are separate
1246 rather than passed in a list. Used by Fbyte_code. */
1248 Lisp_Object
1249 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1250 Lisp_Object handlers)
1252 Lisp_Object val;
1253 struct handler *c;
1254 struct handler *oldhandlerlist = handlerlist;
1255 int clausenb = 0;
1257 CHECK_SYMBOL (var);
1259 for (val = handlers; CONSP (val); val = XCDR (val))
1261 Lisp_Object tem = XCAR (val);
1262 clausenb++;
1263 if (! (NILP (tem)
1264 || (CONSP (tem)
1265 && (SYMBOLP (XCAR (tem))
1266 || CONSP (XCAR (tem))))))
1267 error ("Invalid condition handler: %s",
1268 SDATA (Fprin1_to_string (tem, Qt)));
1271 { /* The first clause is the one that should be checked first, so it should
1272 be added to handlerlist last. So we build in `clauses' a table that
1273 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1274 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1275 if (MAX_ALLOCA / word_size < clausenb)
1276 memory_full (SIZE_MAX);
1277 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1278 Lisp_Object *volatile clauses_volatile = clauses;
1279 int i = clausenb;
1280 for (val = handlers; CONSP (val); val = XCDR (val))
1281 clauses[--i] = XCAR (val);
1282 for (i = 0; i < clausenb; i++)
1284 Lisp_Object clause = clauses[i];
1285 Lisp_Object condition = XCAR (clause);
1286 if (!CONSP (condition))
1287 condition = Fcons (condition, Qnil);
1288 PUSH_HANDLER (c, condition, CONDITION_CASE);
1289 if (sys_setjmp (c->jmp))
1291 ptrdiff_t count = SPECPDL_INDEX ();
1292 Lisp_Object val = handlerlist->val;
1293 Lisp_Object *chosen_clause = clauses_volatile;
1294 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1295 chosen_clause++;
1296 handlerlist = oldhandlerlist;
1297 if (!NILP (var))
1299 if (!NILP (Vinternal_interpreter_environment))
1300 specbind (Qinternal_interpreter_environment,
1301 Fcons (Fcons (var, val),
1302 Vinternal_interpreter_environment));
1303 else
1304 specbind (var, val);
1306 val = Fprogn (XCDR (*chosen_clause));
1307 /* Note that this just undoes the binding of var; whoever
1308 longjumped to us unwound the stack to c.pdlcount before
1309 throwing. */
1310 if (!NILP (var))
1311 unbind_to (count, Qnil);
1312 return val;
1317 val = eval_sub (bodyform);
1318 handlerlist = oldhandlerlist;
1319 return val;
1322 /* Call the function BFUN with no arguments, catching errors within it
1323 according to HANDLERS. If there is an error, call HFUN with
1324 one argument which is the data that describes the error:
1325 (SIGNALNAME . DATA)
1327 HANDLERS can be a list of conditions to catch.
1328 If HANDLERS is Qt, catch all errors.
1329 If HANDLERS is Qerror, catch all errors
1330 but allow the debugger to run if that is enabled. */
1332 Lisp_Object
1333 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1334 Lisp_Object (*hfun) (Lisp_Object))
1336 Lisp_Object val;
1337 struct handler *c;
1339 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1340 if (sys_setjmp (c->jmp))
1342 Lisp_Object val = handlerlist->val;
1343 clobbered_eassert (handlerlist == c);
1344 handlerlist = handlerlist->next;
1345 return (*hfun) (val);
1348 val = (*bfun) ();
1349 clobbered_eassert (handlerlist == c);
1350 handlerlist = handlerlist->next;
1351 return val;
1354 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1356 Lisp_Object
1357 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1358 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1360 Lisp_Object val;
1361 struct handler *c;
1363 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1364 if (sys_setjmp (c->jmp))
1366 Lisp_Object val = handlerlist->val;
1367 clobbered_eassert (handlerlist == c);
1368 handlerlist = handlerlist->next;
1369 return (*hfun) (val);
1372 val = (*bfun) (arg);
1373 clobbered_eassert (handlerlist == c);
1374 handlerlist = handlerlist->next;
1375 return val;
1378 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1379 its arguments. */
1381 Lisp_Object
1382 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1383 Lisp_Object arg1,
1384 Lisp_Object arg2,
1385 Lisp_Object handlers,
1386 Lisp_Object (*hfun) (Lisp_Object))
1388 Lisp_Object val;
1389 struct handler *c;
1391 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1392 if (sys_setjmp (c->jmp))
1394 Lisp_Object val = handlerlist->val;
1395 clobbered_eassert (handlerlist == c);
1396 handlerlist = handlerlist->next;
1397 return (*hfun) (val);
1400 val = (*bfun) (arg1, arg2);
1401 clobbered_eassert (handlerlist == c);
1402 handlerlist = handlerlist->next;
1403 return val;
1406 /* Like internal_condition_case but call BFUN with NARGS as first,
1407 and ARGS as second argument. */
1409 Lisp_Object
1410 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1411 ptrdiff_t nargs,
1412 Lisp_Object *args,
1413 Lisp_Object handlers,
1414 Lisp_Object (*hfun) (Lisp_Object err,
1415 ptrdiff_t nargs,
1416 Lisp_Object *args))
1418 Lisp_Object val;
1419 struct handler *c;
1421 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1422 if (sys_setjmp (c->jmp))
1424 Lisp_Object val = handlerlist->val;
1425 clobbered_eassert (handlerlist == c);
1426 handlerlist = handlerlist->next;
1427 return (*hfun) (val, nargs, args);
1430 val = (*bfun) (nargs, args);
1431 clobbered_eassert (handlerlist == c);
1432 handlerlist = handlerlist->next;
1433 return val;
1437 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1438 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1439 Lisp_Object data);
1441 void
1442 process_quit_flag (void)
1444 Lisp_Object flag = Vquit_flag;
1445 Vquit_flag = Qnil;
1446 if (EQ (flag, Qkill_emacs))
1447 Fkill_emacs (Qnil);
1448 if (EQ (Vthrow_on_input, flag))
1449 Fthrow (Vthrow_on_input, Qt);
1450 Fsignal (Qquit, Qnil);
1453 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1454 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1455 This function does not return.
1457 An error symbol is a symbol with an `error-conditions' property
1458 that is a list of condition names.
1459 A handler for any of those names will get to handle this signal.
1460 The symbol `error' should normally be one of them.
1462 DATA should be a list. Its elements are printed as part of the error message.
1463 See Info anchor `(elisp)Definition of signal' for some details on how this
1464 error message is constructed.
1465 If the signal is handled, DATA is made available to the handler.
1466 See also the function `condition-case'. */)
1467 (Lisp_Object error_symbol, Lisp_Object data)
1469 /* When memory is full, ERROR-SYMBOL is nil,
1470 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1471 That is a special case--don't do this in other situations. */
1472 Lisp_Object conditions;
1473 Lisp_Object string;
1474 Lisp_Object real_error_symbol
1475 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1476 register Lisp_Object clause = Qnil;
1477 struct handler *h;
1479 immediate_quit = 0;
1480 abort_on_gc = 0;
1481 if (gc_in_progress || waiting_for_input)
1482 emacs_abort ();
1484 #if 0 /* rms: I don't know why this was here,
1485 but it is surely wrong for an error that is handled. */
1486 #ifdef HAVE_WINDOW_SYSTEM
1487 if (display_hourglass_p)
1488 cancel_hourglass ();
1489 #endif
1490 #endif
1492 /* This hook is used by edebug. */
1493 if (! NILP (Vsignal_hook_function)
1494 && ! NILP (error_symbol))
1496 /* Edebug takes care of restoring these variables when it exits. */
1497 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1498 max_lisp_eval_depth = lisp_eval_depth + 20;
1500 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1501 max_specpdl_size = SPECPDL_INDEX () + 40;
1503 call2 (Vsignal_hook_function, error_symbol, data);
1506 conditions = Fget (real_error_symbol, Qerror_conditions);
1508 /* Remember from where signal was called. Skip over the frame for
1509 `signal' itself. If a frame for `error' follows, skip that,
1510 too. Don't do this when ERROR_SYMBOL is nil, because that
1511 is a memory-full error. */
1512 Vsignaling_function = Qnil;
1513 if (!NILP (error_symbol))
1515 union specbinding *pdl = backtrace_next (backtrace_top ());
1516 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1517 pdl = backtrace_next (pdl);
1518 if (backtrace_p (pdl))
1519 Vsignaling_function = backtrace_function (pdl);
1522 for (h = handlerlist; h; h = h->next)
1524 if (h->type != CONDITION_CASE)
1525 continue;
1526 clause = find_handler_clause (h->tag_or_ch, conditions);
1527 if (!NILP (clause))
1528 break;
1531 if (/* Don't run the debugger for a memory-full error.
1532 (There is no room in memory to do that!) */
1533 !NILP (error_symbol)
1534 && (!NILP (Vdebug_on_signal)
1535 /* If no handler is present now, try to run the debugger. */
1536 || NILP (clause)
1537 /* A `debug' symbol in the handler list disables the normal
1538 suppression of the debugger. */
1539 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1540 /* Special handler that means "print a message and run debugger
1541 if requested". */
1542 || EQ (h->tag_or_ch, Qerror)))
1544 bool debugger_called
1545 = maybe_call_debugger (conditions, error_symbol, data);
1546 /* We can't return values to code which signaled an error, but we
1547 can continue code which has signaled a quit. */
1548 if (debugger_called && EQ (real_error_symbol, Qquit))
1549 return Qnil;
1552 if (!NILP (clause))
1554 Lisp_Object unwind_data
1555 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1557 unwind_to_catch (h, unwind_data);
1559 else
1561 if (handlerlist != &handlerlist_sentinel)
1562 /* FIXME: This will come right back here if there's no `top-level'
1563 catcher. A better solution would be to abort here, and instead
1564 add a catch-all condition handler so we never come here. */
1565 Fthrow (Qtop_level, Qt);
1568 if (! NILP (error_symbol))
1569 data = Fcons (error_symbol, data);
1571 string = Ferror_message_string (data);
1572 fatal ("%s", SDATA (string));
1575 /* Internal version of Fsignal that never returns.
1576 Used for anything but Qquit (which can return from Fsignal). */
1578 void
1579 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1581 Fsignal (error_symbol, data);
1582 emacs_abort ();
1585 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1587 void
1588 xsignal0 (Lisp_Object error_symbol)
1590 xsignal (error_symbol, Qnil);
1593 void
1594 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1596 xsignal (error_symbol, list1 (arg));
1599 void
1600 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1602 xsignal (error_symbol, list2 (arg1, arg2));
1605 void
1606 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1608 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1611 /* Signal `error' with message S, and additional arg ARG.
1612 If ARG is not a genuine list, make it a one-element list. */
1614 void
1615 signal_error (const char *s, Lisp_Object arg)
1617 Lisp_Object tortoise, hare;
1619 hare = tortoise = arg;
1620 while (CONSP (hare))
1622 hare = XCDR (hare);
1623 if (!CONSP (hare))
1624 break;
1626 hare = XCDR (hare);
1627 tortoise = XCDR (tortoise);
1629 if (EQ (hare, tortoise))
1630 break;
1633 if (!NILP (hare))
1634 arg = list1 (arg);
1636 xsignal (Qerror, Fcons (build_string (s), arg));
1640 /* Return true if LIST is a non-nil atom or
1641 a list containing one of CONDITIONS. */
1643 static bool
1644 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1646 if (NILP (list))
1647 return 0;
1648 if (! CONSP (list))
1649 return 1;
1651 while (CONSP (conditions))
1653 Lisp_Object this, tail;
1654 this = XCAR (conditions);
1655 for (tail = list; CONSP (tail); tail = XCDR (tail))
1656 if (EQ (XCAR (tail), this))
1657 return 1;
1658 conditions = XCDR (conditions);
1660 return 0;
1663 /* Return true if an error with condition-symbols CONDITIONS,
1664 and described by SIGNAL-DATA, should skip the debugger
1665 according to debugger-ignored-errors. */
1667 static bool
1668 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1670 Lisp_Object tail;
1671 bool first_string = 1;
1672 Lisp_Object error_message;
1674 error_message = Qnil;
1675 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1677 if (STRINGP (XCAR (tail)))
1679 if (first_string)
1681 error_message = Ferror_message_string (data);
1682 first_string = 0;
1685 if (fast_string_match (XCAR (tail), error_message) >= 0)
1686 return 1;
1688 else
1690 Lisp_Object contail;
1692 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1693 if (EQ (XCAR (tail), XCAR (contail)))
1694 return 1;
1698 return 0;
1701 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1702 SIG and DATA describe the signal. There are two ways to pass them:
1703 = SIG is the error symbol, and DATA is the rest of the data.
1704 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1705 This is for memory-full errors only. */
1706 static bool
1707 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1709 Lisp_Object combined_data;
1711 combined_data = Fcons (sig, data);
1713 if (
1714 /* Don't try to run the debugger with interrupts blocked.
1715 The editing loop would return anyway. */
1716 ! input_blocked_p ()
1717 && NILP (Vinhibit_debugger)
1718 /* Does user want to enter debugger for this kind of error? */
1719 && (EQ (sig, Qquit)
1720 ? debug_on_quit
1721 : wants_debugger (Vdebug_on_error, conditions))
1722 && ! skip_debugger (conditions, combined_data)
1723 /* RMS: What's this for? */
1724 && when_entered_debugger < num_nonmacro_input_events)
1726 call_debugger (list2 (Qerror, combined_data));
1727 return 1;
1730 return 0;
1733 static Lisp_Object
1734 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1736 register Lisp_Object h;
1738 /* t is used by handlers for all conditions, set up by C code. */
1739 if (EQ (handlers, Qt))
1740 return Qt;
1742 /* error is used similarly, but means print an error message
1743 and run the debugger if that is enabled. */
1744 if (EQ (handlers, Qerror))
1745 return Qt;
1747 for (h = handlers; CONSP (h); h = XCDR (h))
1749 Lisp_Object handler = XCAR (h);
1750 if (!NILP (Fmemq (handler, conditions)))
1751 return handlers;
1754 return Qnil;
1758 /* Dump an error message; called like vprintf. */
1759 void
1760 verror (const char *m, va_list ap)
1762 char buf[4000];
1763 ptrdiff_t size = sizeof buf;
1764 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1765 char *buffer = buf;
1766 ptrdiff_t used;
1767 Lisp_Object string;
1769 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1770 string = make_string (buffer, used);
1771 if (buffer != buf)
1772 xfree (buffer);
1774 xsignal1 (Qerror, string);
1778 /* Dump an error message; called like printf. */
1780 /* VARARGS 1 */
1781 void
1782 error (const char *m, ...)
1784 va_list ap;
1785 va_start (ap, m);
1786 verror (m, ap);
1789 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1790 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1791 This means it contains a description for how to read arguments to give it.
1792 The value is nil for an invalid function or a symbol with no function
1793 definition.
1795 Interactively callable functions include strings and vectors (treated
1796 as keyboard macros), lambda-expressions that contain a top-level call
1797 to `interactive', autoload definitions made by `autoload' with non-nil
1798 fourth argument, and some of the built-in functions of Lisp.
1800 Also, a symbol satisfies `commandp' if its function definition does so.
1802 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1803 then strings and vectors are not accepted. */)
1804 (Lisp_Object function, Lisp_Object for_call_interactively)
1806 register Lisp_Object fun;
1807 register Lisp_Object funcar;
1808 Lisp_Object if_prop = Qnil;
1810 fun = function;
1812 fun = indirect_function (fun); /* Check cycles. */
1813 if (NILP (fun))
1814 return Qnil;
1816 /* Check an `interactive-form' property if present, analogous to the
1817 function-documentation property. */
1818 fun = function;
1819 while (SYMBOLP (fun))
1821 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1822 if (!NILP (tmp))
1823 if_prop = Qt;
1824 fun = Fsymbol_function (fun);
1827 /* Emacs primitives are interactive if their DEFUN specifies an
1828 interactive spec. */
1829 if (SUBRP (fun))
1830 return XSUBR (fun)->intspec ? Qt : if_prop;
1832 /* Bytecode objects are interactive if they are long enough to
1833 have an element whose index is COMPILED_INTERACTIVE, which is
1834 where the interactive spec is stored. */
1835 else if (COMPILEDP (fun))
1836 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1837 ? Qt : if_prop);
1839 /* Strings and vectors are keyboard macros. */
1840 if (STRINGP (fun) || VECTORP (fun))
1841 return (NILP (for_call_interactively) ? Qt : Qnil);
1843 /* Lists may represent commands. */
1844 if (!CONSP (fun))
1845 return Qnil;
1846 funcar = XCAR (fun);
1847 if (EQ (funcar, Qclosure))
1848 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1849 ? Qt : if_prop);
1850 else if (EQ (funcar, Qlambda))
1851 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1852 else if (EQ (funcar, Qautoload))
1853 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1854 else
1855 return Qnil;
1858 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1859 doc: /* Define FUNCTION to autoload from FILE.
1860 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1861 Third arg DOCSTRING is documentation for the function.
1862 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1863 Fifth arg TYPE indicates the type of the object:
1864 nil or omitted says FUNCTION is a function,
1865 `keymap' says FUNCTION is really a keymap, and
1866 `macro' or t says FUNCTION is really a macro.
1867 Third through fifth args give info about the real definition.
1868 They default to nil.
1869 If FUNCTION is already defined other than as an autoload,
1870 this does nothing and returns nil. */)
1871 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1873 CHECK_SYMBOL (function);
1874 CHECK_STRING (file);
1876 /* If function is defined and not as an autoload, don't override. */
1877 if (!NILP (XSYMBOL (function)->function)
1878 && !AUTOLOADP (XSYMBOL (function)->function))
1879 return Qnil;
1881 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1882 /* `read1' in lread.c has found the docstring starting with "\
1883 and assumed the docstring will be provided by Snarf-documentation, so it
1884 passed us 0 instead. But that leads to accidental sharing in purecopy's
1885 hash-consing, so we use a (hopefully) unique integer instead. */
1886 docstring = make_number (XHASH (function));
1887 return Fdefalias (function,
1888 list5 (Qautoload, file, docstring, interactive, type),
1889 Qnil);
1892 void
1893 un_autoload (Lisp_Object oldqueue)
1895 Lisp_Object queue, first, second;
1897 /* Queue to unwind is current value of Vautoload_queue.
1898 oldqueue is the shadowed value to leave in Vautoload_queue. */
1899 queue = Vautoload_queue;
1900 Vautoload_queue = oldqueue;
1901 while (CONSP (queue))
1903 first = XCAR (queue);
1904 second = Fcdr (first);
1905 first = Fcar (first);
1906 if (EQ (first, make_number (0)))
1907 Vfeatures = second;
1908 else
1909 Ffset (first, second);
1910 queue = XCDR (queue);
1914 /* Load an autoloaded function.
1915 FUNNAME is the symbol which is the function's name.
1916 FUNDEF is the autoload definition (a list). */
1918 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1919 doc: /* Load FUNDEF which should be an autoload.
1920 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1921 in which case the function returns the new autoloaded function value.
1922 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1923 it defines a macro. */)
1924 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1926 ptrdiff_t count = SPECPDL_INDEX ();
1927 struct gcpro gcpro1, gcpro2, gcpro3;
1929 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1930 return fundef;
1932 if (EQ (macro_only, Qmacro))
1934 Lisp_Object kind = Fnth (make_number (4), fundef);
1935 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1936 return fundef;
1939 /* This is to make sure that loadup.el gives a clear picture
1940 of what files are preloaded and when. */
1941 if (! NILP (Vpurify_flag))
1942 error ("Attempt to autoload %s while preparing to dump",
1943 SDATA (SYMBOL_NAME (funname)));
1945 CHECK_SYMBOL (funname);
1946 GCPRO3 (funname, fundef, macro_only);
1948 /* Preserve the match data. */
1949 record_unwind_save_match_data ();
1951 /* If autoloading gets an error (which includes the error of failing
1952 to define the function being called), we use Vautoload_queue
1953 to undo function definitions and `provide' calls made by
1954 the function. We do this in the specific case of autoloading
1955 because autoloading is not an explicit request "load this file",
1956 but rather a request to "call this function".
1958 The value saved here is to be restored into Vautoload_queue. */
1959 record_unwind_protect (un_autoload, Vautoload_queue);
1960 Vautoload_queue = Qt;
1961 /* If `macro_only', assume this autoload to be a "best-effort",
1962 so don't signal an error if autoloading fails. */
1963 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1965 /* Once loading finishes, don't undo it. */
1966 Vautoload_queue = Qt;
1967 unbind_to (count, Qnil);
1969 UNGCPRO;
1971 if (NILP (funname))
1972 return Qnil;
1973 else
1975 Lisp_Object fun = Findirect_function (funname, Qnil);
1977 if (!NILP (Fequal (fun, fundef)))
1978 error ("Autoloading failed to define function %s",
1979 SDATA (SYMBOL_NAME (funname)));
1980 else
1981 return fun;
1986 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1987 doc: /* Evaluate FORM and return its value.
1988 If LEXICAL is t, evaluate using lexical scoping.
1989 LEXICAL can also be an actual lexical environment, in the form of an
1990 alist mapping symbols to their value. */)
1991 (Lisp_Object form, Lisp_Object lexical)
1993 ptrdiff_t count = SPECPDL_INDEX ();
1994 specbind (Qinternal_interpreter_environment,
1995 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1996 return unbind_to (count, eval_sub (form));
1999 /* Grow the specpdl stack by one entry.
2000 The caller should have already initialized the entry.
2001 Signal an error on stack overflow.
2003 Make sure that there is always one unused entry past the top of the
2004 stack, so that the just-initialized entry is safely unwound if
2005 memory exhausted and an error is signaled here. Also, allocate a
2006 never-used entry just before the bottom of the stack; sometimes its
2007 address is taken. */
2009 static void
2010 grow_specpdl (void)
2012 specpdl_ptr++;
2014 if (specpdl_ptr == specpdl + specpdl_size)
2016 ptrdiff_t count = SPECPDL_INDEX ();
2017 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2018 union specbinding *pdlvec = specpdl - 1;
2019 ptrdiff_t pdlvecsize = specpdl_size + 1;
2020 if (max_size <= specpdl_size)
2022 if (max_specpdl_size < 400)
2023 max_size = max_specpdl_size = 400;
2024 if (max_size <= specpdl_size)
2025 signal_error ("Variable binding depth exceeds max-specpdl-size",
2026 Qnil);
2028 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2029 specpdl = pdlvec + 1;
2030 specpdl_size = pdlvecsize - 1;
2031 specpdl_ptr = specpdl + count;
2035 ptrdiff_t
2036 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2038 ptrdiff_t count = SPECPDL_INDEX ();
2040 eassert (nargs >= UNEVALLED);
2041 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2042 specpdl_ptr->bt.debug_on_exit = false;
2043 specpdl_ptr->bt.function = function;
2044 specpdl_ptr->bt.args = args;
2045 specpdl_ptr->bt.nargs = nargs;
2046 grow_specpdl ();
2048 return count;
2051 /* Eval a sub-expression of the current expression (i.e. in the same
2052 lexical scope). */
2053 Lisp_Object
2054 eval_sub (Lisp_Object form)
2056 Lisp_Object fun, val, original_fun, original_args;
2057 Lisp_Object funcar;
2058 struct gcpro gcpro1, gcpro2, gcpro3;
2059 ptrdiff_t count;
2061 if (SYMBOLP (form))
2063 /* Look up its binding in the lexical environment.
2064 We do not pay attention to the declared_special flag here, since we
2065 already did that when let-binding the variable. */
2066 Lisp_Object lex_binding
2067 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2068 ? Fassq (form, Vinternal_interpreter_environment)
2069 : Qnil;
2070 if (CONSP (lex_binding))
2071 return XCDR (lex_binding);
2072 else
2073 return Fsymbol_value (form);
2076 if (!CONSP (form))
2077 return form;
2079 QUIT;
2081 GCPRO1 (form);
2082 maybe_gc ();
2083 UNGCPRO;
2085 if (++lisp_eval_depth > max_lisp_eval_depth)
2087 if (max_lisp_eval_depth < 100)
2088 max_lisp_eval_depth = 100;
2089 if (lisp_eval_depth > max_lisp_eval_depth)
2090 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2093 original_fun = XCAR (form);
2094 original_args = XCDR (form);
2096 /* This also protects them from gc. */
2097 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2099 if (debug_on_next_call)
2100 do_debug_on_call (Qt, count);
2102 /* At this point, only original_fun and original_args
2103 have values that will be used below. */
2104 retry:
2106 /* Optimize for no indirection. */
2107 fun = original_fun;
2108 if (!SYMBOLP (fun))
2109 fun = Ffunction (Fcons (fun, Qnil));
2110 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2111 fun = indirect_function (fun);
2113 if (SUBRP (fun))
2115 Lisp_Object numargs;
2116 Lisp_Object argvals[8];
2117 Lisp_Object args_left;
2118 register int i, maxargs;
2120 args_left = original_args;
2121 numargs = Flength (args_left);
2123 check_cons_list ();
2125 if (XINT (numargs) < XSUBR (fun)->min_args
2126 || (XSUBR (fun)->max_args >= 0
2127 && XSUBR (fun)->max_args < XINT (numargs)))
2128 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2130 else if (XSUBR (fun)->max_args == UNEVALLED)
2131 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2132 else if (XSUBR (fun)->max_args == MANY)
2134 /* Pass a vector of evaluated arguments. */
2135 Lisp_Object *vals;
2136 ptrdiff_t argnum = 0;
2137 USE_SAFE_ALLOCA;
2139 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2141 GCPRO3 (args_left, fun, fun);
2142 gcpro3.var = vals;
2143 gcpro3.nvars = 0;
2145 while (!NILP (args_left))
2147 vals[argnum++] = eval_sub (Fcar (args_left));
2148 args_left = Fcdr (args_left);
2149 gcpro3.nvars = argnum;
2152 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2154 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2155 UNGCPRO;
2156 SAFE_FREE ();
2158 else
2160 GCPRO3 (args_left, fun, fun);
2161 gcpro3.var = argvals;
2162 gcpro3.nvars = 0;
2164 maxargs = XSUBR (fun)->max_args;
2165 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2167 argvals[i] = eval_sub (Fcar (args_left));
2168 gcpro3.nvars = ++i;
2171 UNGCPRO;
2173 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2175 switch (i)
2177 case 0:
2178 val = (XSUBR (fun)->function.a0 ());
2179 break;
2180 case 1:
2181 val = (XSUBR (fun)->function.a1 (argvals[0]));
2182 break;
2183 case 2:
2184 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2185 break;
2186 case 3:
2187 val = (XSUBR (fun)->function.a3
2188 (argvals[0], argvals[1], argvals[2]));
2189 break;
2190 case 4:
2191 val = (XSUBR (fun)->function.a4
2192 (argvals[0], argvals[1], argvals[2], argvals[3]));
2193 break;
2194 case 5:
2195 val = (XSUBR (fun)->function.a5
2196 (argvals[0], argvals[1], argvals[2], argvals[3],
2197 argvals[4]));
2198 break;
2199 case 6:
2200 val = (XSUBR (fun)->function.a6
2201 (argvals[0], argvals[1], argvals[2], argvals[3],
2202 argvals[4], argvals[5]));
2203 break;
2204 case 7:
2205 val = (XSUBR (fun)->function.a7
2206 (argvals[0], argvals[1], argvals[2], argvals[3],
2207 argvals[4], argvals[5], argvals[6]));
2208 break;
2210 case 8:
2211 val = (XSUBR (fun)->function.a8
2212 (argvals[0], argvals[1], argvals[2], argvals[3],
2213 argvals[4], argvals[5], argvals[6], argvals[7]));
2214 break;
2216 default:
2217 /* Someone has created a subr that takes more arguments than
2218 is supported by this code. We need to either rewrite the
2219 subr to use a different argument protocol, or add more
2220 cases to this switch. */
2221 emacs_abort ();
2225 else if (COMPILEDP (fun))
2226 val = apply_lambda (fun, original_args, count);
2227 else
2229 if (NILP (fun))
2230 xsignal1 (Qvoid_function, original_fun);
2231 if (!CONSP (fun))
2232 xsignal1 (Qinvalid_function, original_fun);
2233 funcar = XCAR (fun);
2234 if (!SYMBOLP (funcar))
2235 xsignal1 (Qinvalid_function, original_fun);
2236 if (EQ (funcar, Qautoload))
2238 Fautoload_do_load (fun, original_fun, Qnil);
2239 goto retry;
2241 if (EQ (funcar, Qmacro))
2243 ptrdiff_t count1 = SPECPDL_INDEX ();
2244 Lisp_Object exp;
2245 /* Bind lexical-binding during expansion of the macro, so the
2246 macro can know reliably if the code it outputs will be
2247 interpreted using lexical-binding or not. */
2248 specbind (Qlexical_binding,
2249 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2250 exp = apply1 (Fcdr (fun), original_args);
2251 unbind_to (count1, Qnil);
2252 val = eval_sub (exp);
2254 else if (EQ (funcar, Qlambda)
2255 || EQ (funcar, Qclosure))
2256 val = apply_lambda (fun, original_args, count);
2257 else
2258 xsignal1 (Qinvalid_function, original_fun);
2260 check_cons_list ();
2262 lisp_eval_depth--;
2263 if (backtrace_debug_on_exit (specpdl + count))
2264 val = call_debugger (list2 (Qexit, val));
2265 specpdl_ptr--;
2267 return val;
2270 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2271 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2272 Then return the value FUNCTION returns.
2273 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2274 usage: (apply FUNCTION &rest ARGUMENTS) */)
2275 (ptrdiff_t nargs, Lisp_Object *args)
2277 ptrdiff_t i, numargs, funcall_nargs;
2278 register Lisp_Object *funcall_args = NULL;
2279 register Lisp_Object spread_arg = args[nargs - 1];
2280 Lisp_Object fun = args[0];
2281 Lisp_Object retval;
2282 USE_SAFE_ALLOCA;
2284 CHECK_LIST (spread_arg);
2286 numargs = XINT (Flength (spread_arg));
2288 if (numargs == 0)
2289 return Ffuncall (nargs - 1, args);
2290 else if (numargs == 1)
2292 args [nargs - 1] = XCAR (spread_arg);
2293 return Ffuncall (nargs, args);
2296 numargs += nargs - 2;
2298 /* Optimize for no indirection. */
2299 if (SYMBOLP (fun) && !NILP (fun)
2300 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2302 fun = indirect_function (fun);
2303 if (NILP (fun))
2304 /* Let funcall get the error. */
2305 fun = args[0];
2308 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2309 /* Don't hide an error by adding missing arguments. */
2310 && numargs >= XSUBR (fun)->min_args)
2312 /* Avoid making funcall cons up a yet another new vector of arguments
2313 by explicitly supplying nil's for optional values. */
2314 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2315 memclear (funcall_args + numargs + 1,
2316 (XSUBR (fun)->max_args - numargs) * word_size);
2317 funcall_nargs = 1 + XSUBR (fun)->max_args;
2319 else
2320 { /* We add 1 to numargs because funcall_args includes the
2321 function itself as well as its arguments. */
2322 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2323 funcall_nargs = 1 + numargs;
2326 memcpy (funcall_args, args, nargs * word_size);
2327 /* Spread the last arg we got. Its first element goes in
2328 the slot that it used to occupy, hence this value of I. */
2329 i = nargs - 1;
2330 while (!NILP (spread_arg))
2332 funcall_args [i++] = XCAR (spread_arg);
2333 spread_arg = XCDR (spread_arg);
2336 /* Ffuncall gcpro's all of its args. */
2337 retval = Ffuncall (funcall_nargs, funcall_args);
2339 SAFE_FREE ();
2340 return retval;
2343 /* Run hook variables in various ways. */
2345 static Lisp_Object
2346 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2348 Ffuncall (nargs, args);
2349 return Qnil;
2352 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2353 doc: /* Run each hook in HOOKS.
2354 Each argument should be a symbol, a hook variable.
2355 These symbols are processed in the order specified.
2356 If a hook symbol has a non-nil value, that value may be a function
2357 or a list of functions to be called to run the hook.
2358 If the value is a function, it is called with no arguments.
2359 If it is a list, the elements are called, in order, with no arguments.
2361 Major modes should not use this function directly to run their mode
2362 hook; they should use `run-mode-hooks' instead.
2364 Do not use `make-local-variable' to make a hook variable buffer-local.
2365 Instead, use `add-hook' and specify t for the LOCAL argument.
2366 usage: (run-hooks &rest HOOKS) */)
2367 (ptrdiff_t nargs, Lisp_Object *args)
2369 ptrdiff_t i;
2371 for (i = 0; i < nargs; i++)
2372 run_hook (args[i]);
2374 return Qnil;
2377 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2378 Srun_hook_with_args, 1, MANY, 0,
2379 doc: /* Run HOOK with the specified arguments ARGS.
2380 HOOK should be a symbol, a hook variable. The value of HOOK
2381 may be nil, a function, or a list of functions. Call each
2382 function in order with arguments ARGS. The final return value
2383 is unspecified.
2385 Do not use `make-local-variable' to make a hook variable buffer-local.
2386 Instead, use `add-hook' and specify t for the LOCAL argument.
2387 usage: (run-hook-with-args HOOK &rest ARGS) */)
2388 (ptrdiff_t nargs, Lisp_Object *args)
2390 return run_hook_with_args (nargs, args, funcall_nil);
2393 /* NB this one still documents a specific non-nil return value.
2394 (As did run-hook-with-args and run-hook-with-args-until-failure
2395 until they were changed in 24.1.) */
2396 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2397 Srun_hook_with_args_until_success, 1, MANY, 0,
2398 doc: /* Run HOOK with the specified arguments ARGS.
2399 HOOK should be a symbol, a hook variable. The value of HOOK
2400 may be nil, a function, or a list of functions. Call each
2401 function in order with arguments ARGS, stopping at the first
2402 one that returns non-nil, and return that value. Otherwise (if
2403 all functions return nil, or if there are no functions to call),
2404 return nil.
2406 Do not use `make-local-variable' to make a hook variable buffer-local.
2407 Instead, use `add-hook' and specify t for the LOCAL argument.
2408 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2409 (ptrdiff_t nargs, Lisp_Object *args)
2411 return run_hook_with_args (nargs, args, Ffuncall);
2414 static Lisp_Object
2415 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2417 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2420 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2421 Srun_hook_with_args_until_failure, 1, MANY, 0,
2422 doc: /* Run HOOK with the specified arguments ARGS.
2423 HOOK should be a symbol, a hook variable. The value of HOOK
2424 may be nil, a function, or a list of functions. Call each
2425 function in order with arguments ARGS, stopping at the first
2426 one that returns nil, and return nil. Otherwise (if all functions
2427 return non-nil, or if there are no functions to call), return non-nil
2428 \(do not rely on the precise return value in this case).
2430 Do not use `make-local-variable' to make a hook variable buffer-local.
2431 Instead, use `add-hook' and specify t for the LOCAL argument.
2432 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2433 (ptrdiff_t nargs, Lisp_Object *args)
2435 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2438 static Lisp_Object
2439 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2441 Lisp_Object tmp = args[0], ret;
2442 args[0] = args[1];
2443 args[1] = tmp;
2444 ret = Ffuncall (nargs, args);
2445 args[1] = args[0];
2446 args[0] = tmp;
2447 return ret;
2450 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2451 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2452 I.e. instead of calling each function FUN directly with arguments ARGS,
2453 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2454 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2455 aborts and returns that value.
2456 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2457 (ptrdiff_t nargs, Lisp_Object *args)
2459 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2462 /* ARGS[0] should be a hook symbol.
2463 Call each of the functions in the hook value, passing each of them
2464 as arguments all the rest of ARGS (all NARGS - 1 elements).
2465 FUNCALL specifies how to call each function on the hook.
2466 The caller (or its caller, etc) must gcpro all of ARGS,
2467 except that it isn't necessary to gcpro ARGS[0]. */
2469 Lisp_Object
2470 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2471 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2473 Lisp_Object sym, val, ret = Qnil;
2474 struct gcpro gcpro1, gcpro2, gcpro3;
2476 /* If we are dying or still initializing,
2477 don't do anything--it would probably crash if we tried. */
2478 if (NILP (Vrun_hooks))
2479 return Qnil;
2481 sym = args[0];
2482 val = find_symbol_value (sym);
2484 if (EQ (val, Qunbound) || NILP (val))
2485 return ret;
2486 else if (!CONSP (val) || FUNCTIONP (val))
2488 args[0] = val;
2489 return funcall (nargs, args);
2491 else
2493 Lisp_Object global_vals = Qnil;
2494 GCPRO3 (sym, val, global_vals);
2496 for (;
2497 CONSP (val) && NILP (ret);
2498 val = XCDR (val))
2500 if (EQ (XCAR (val), Qt))
2502 /* t indicates this hook has a local binding;
2503 it means to run the global binding too. */
2504 global_vals = Fdefault_value (sym);
2505 if (NILP (global_vals)) continue;
2507 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2509 args[0] = global_vals;
2510 ret = funcall (nargs, args);
2512 else
2514 for (;
2515 CONSP (global_vals) && NILP (ret);
2516 global_vals = XCDR (global_vals))
2518 args[0] = XCAR (global_vals);
2519 /* In a global value, t should not occur. If it does, we
2520 must ignore it to avoid an endless loop. */
2521 if (!EQ (args[0], Qt))
2522 ret = funcall (nargs, args);
2526 else
2528 args[0] = XCAR (val);
2529 ret = funcall (nargs, args);
2533 UNGCPRO;
2534 return ret;
2538 /* Run the hook HOOK, giving each function no args. */
2540 void
2541 run_hook (Lisp_Object hook)
2543 Frun_hook_with_args (1, &hook);
2546 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2548 void
2549 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2551 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2554 /* Apply fn to arg. */
2555 Lisp_Object
2556 apply1 (Lisp_Object fn, Lisp_Object arg)
2558 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2561 /* Call function fn on no arguments. */
2562 Lisp_Object
2563 call0 (Lisp_Object fn)
2565 return Ffuncall (1, &fn);
2568 /* Call function fn with 1 argument arg1. */
2569 /* ARGSUSED */
2570 Lisp_Object
2571 call1 (Lisp_Object fn, Lisp_Object arg1)
2573 return CALLN (Ffuncall, fn, arg1);
2576 /* Call function fn with 2 arguments arg1, arg2. */
2577 /* ARGSUSED */
2578 Lisp_Object
2579 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2581 return CALLN (Ffuncall, fn, arg1, arg2);
2584 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2585 /* ARGSUSED */
2586 Lisp_Object
2587 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2589 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2592 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2593 /* ARGSUSED */
2594 Lisp_Object
2595 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2596 Lisp_Object arg4)
2598 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2601 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2602 /* ARGSUSED */
2603 Lisp_Object
2604 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2605 Lisp_Object arg4, Lisp_Object arg5)
2607 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2610 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2611 /* ARGSUSED */
2612 Lisp_Object
2613 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2614 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2616 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2619 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2620 /* ARGSUSED */
2621 Lisp_Object
2622 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2623 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2625 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2628 /* The caller should GCPRO all the elements of ARGS. */
2630 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2631 doc: /* Non-nil if OBJECT is a function. */)
2632 (Lisp_Object object)
2634 if (FUNCTIONP (object))
2635 return Qt;
2636 return Qnil;
2639 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2640 doc: /* Call first argument as a function, passing remaining arguments to it.
2641 Return the value that function returns.
2642 Thus, (funcall 'cons 'x 'y) returns (x . y).
2643 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2644 (ptrdiff_t nargs, Lisp_Object *args)
2646 Lisp_Object fun, original_fun;
2647 Lisp_Object funcar;
2648 ptrdiff_t numargs = nargs - 1;
2649 Lisp_Object lisp_numargs;
2650 Lisp_Object val;
2651 Lisp_Object *internal_args;
2652 ptrdiff_t count;
2654 QUIT;
2656 if (++lisp_eval_depth > max_lisp_eval_depth)
2658 if (max_lisp_eval_depth < 100)
2659 max_lisp_eval_depth = 100;
2660 if (lisp_eval_depth > max_lisp_eval_depth)
2661 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2664 /* This also GCPROs them. */
2665 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2667 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2668 maybe_gc ();
2670 if (debug_on_next_call)
2671 do_debug_on_call (Qlambda, count);
2673 check_cons_list ();
2675 original_fun = args[0];
2677 retry:
2679 /* Optimize for no indirection. */
2680 fun = original_fun;
2681 if (SYMBOLP (fun) && !NILP (fun)
2682 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2683 fun = indirect_function (fun);
2685 if (SUBRP (fun))
2687 if (numargs < XSUBR (fun)->min_args
2688 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2690 XSETFASTINT (lisp_numargs, numargs);
2691 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2694 else if (XSUBR (fun)->max_args == UNEVALLED)
2695 xsignal1 (Qinvalid_function, original_fun);
2697 else if (XSUBR (fun)->max_args == MANY)
2698 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2699 else
2701 Lisp_Object internal_argbuf[8];
2702 if (XSUBR (fun)->max_args > numargs)
2704 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2705 internal_args = internal_argbuf;
2706 memcpy (internal_args, args + 1, numargs * word_size);
2707 memclear (internal_args + numargs,
2708 (XSUBR (fun)->max_args - numargs) * word_size);
2710 else
2711 internal_args = args + 1;
2712 switch (XSUBR (fun)->max_args)
2714 case 0:
2715 val = (XSUBR (fun)->function.a0 ());
2716 break;
2717 case 1:
2718 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2719 break;
2720 case 2:
2721 val = (XSUBR (fun)->function.a2
2722 (internal_args[0], internal_args[1]));
2723 break;
2724 case 3:
2725 val = (XSUBR (fun)->function.a3
2726 (internal_args[0], internal_args[1], internal_args[2]));
2727 break;
2728 case 4:
2729 val = (XSUBR (fun)->function.a4
2730 (internal_args[0], internal_args[1], internal_args[2],
2731 internal_args[3]));
2732 break;
2733 case 5:
2734 val = (XSUBR (fun)->function.a5
2735 (internal_args[0], internal_args[1], internal_args[2],
2736 internal_args[3], internal_args[4]));
2737 break;
2738 case 6:
2739 val = (XSUBR (fun)->function.a6
2740 (internal_args[0], internal_args[1], internal_args[2],
2741 internal_args[3], internal_args[4], internal_args[5]));
2742 break;
2743 case 7:
2744 val = (XSUBR (fun)->function.a7
2745 (internal_args[0], internal_args[1], internal_args[2],
2746 internal_args[3], internal_args[4], internal_args[5],
2747 internal_args[6]));
2748 break;
2750 case 8:
2751 val = (XSUBR (fun)->function.a8
2752 (internal_args[0], internal_args[1], internal_args[2],
2753 internal_args[3], internal_args[4], internal_args[5],
2754 internal_args[6], internal_args[7]));
2755 break;
2757 default:
2759 /* If a subr takes more than 8 arguments without using MANY
2760 or UNEVALLED, we need to extend this function to support it.
2761 Until this is done, there is no way to call the function. */
2762 emacs_abort ();
2766 else if (COMPILEDP (fun))
2767 val = funcall_lambda (fun, numargs, args + 1);
2768 else
2770 if (NILP (fun))
2771 xsignal1 (Qvoid_function, original_fun);
2772 if (!CONSP (fun))
2773 xsignal1 (Qinvalid_function, original_fun);
2774 funcar = XCAR (fun);
2775 if (!SYMBOLP (funcar))
2776 xsignal1 (Qinvalid_function, original_fun);
2777 if (EQ (funcar, Qlambda)
2778 || EQ (funcar, Qclosure))
2779 val = funcall_lambda (fun, numargs, args + 1);
2780 else if (EQ (funcar, Qautoload))
2782 Fautoload_do_load (fun, original_fun, Qnil);
2783 check_cons_list ();
2784 goto retry;
2786 else
2787 xsignal1 (Qinvalid_function, original_fun);
2789 check_cons_list ();
2790 lisp_eval_depth--;
2791 if (backtrace_debug_on_exit (specpdl + count))
2792 val = call_debugger (list2 (Qexit, val));
2793 specpdl_ptr--;
2794 return val;
2797 static Lisp_Object
2798 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2800 Lisp_Object args_left;
2801 ptrdiff_t i;
2802 EMACS_INT numargs;
2803 register Lisp_Object *arg_vector;
2804 struct gcpro gcpro1, gcpro2, gcpro3;
2805 register Lisp_Object tem;
2806 USE_SAFE_ALLOCA;
2808 numargs = XFASTINT (Flength (args));
2809 SAFE_ALLOCA_LISP (arg_vector, numargs);
2810 args_left = args;
2812 GCPRO3 (*arg_vector, args_left, fun);
2813 gcpro1.nvars = 0;
2815 for (i = 0; i < numargs; )
2817 tem = Fcar (args_left), args_left = Fcdr (args_left);
2818 tem = eval_sub (tem);
2819 arg_vector[i++] = tem;
2820 gcpro1.nvars = i;
2823 UNGCPRO;
2825 set_backtrace_args (specpdl + count, arg_vector, i);
2826 tem = funcall_lambda (fun, numargs, arg_vector);
2828 /* Do the debug-on-exit now, while arg_vector still exists. */
2829 if (backtrace_debug_on_exit (specpdl + count))
2831 /* Don't do it again when we return to eval. */
2832 set_backtrace_debug_on_exit (specpdl + count, false);
2833 tem = call_debugger (list2 (Qexit, tem));
2835 SAFE_FREE ();
2836 return tem;
2839 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2840 and return the result of evaluation.
2841 FUN must be either a lambda-expression or a compiled-code object. */
2843 static Lisp_Object
2844 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2845 register Lisp_Object *arg_vector)
2847 Lisp_Object val, syms_left, next, lexenv;
2848 ptrdiff_t count = SPECPDL_INDEX ();
2849 ptrdiff_t i;
2850 bool optional, rest;
2852 if (CONSP (fun))
2854 if (EQ (XCAR (fun), Qclosure))
2856 fun = XCDR (fun); /* Drop `closure'. */
2857 lexenv = XCAR (fun);
2858 CHECK_LIST_CONS (fun, fun);
2860 else
2861 lexenv = Qnil;
2862 syms_left = XCDR (fun);
2863 if (CONSP (syms_left))
2864 syms_left = XCAR (syms_left);
2865 else
2866 xsignal1 (Qinvalid_function, fun);
2868 else if (COMPILEDP (fun))
2870 syms_left = AREF (fun, COMPILED_ARGLIST);
2871 if (INTEGERP (syms_left))
2872 /* A byte-code object with a non-nil `push args' slot means we
2873 shouldn't bind any arguments, instead just call the byte-code
2874 interpreter directly; it will push arguments as necessary.
2876 Byte-code objects with either a non-existent, or a nil value for
2877 the `push args' slot (the default), have dynamically-bound
2878 arguments, and use the argument-binding code below instead (as do
2879 all interpreted functions, even lexically bound ones). */
2881 /* If we have not actually read the bytecode string
2882 and constants vector yet, fetch them from the file. */
2883 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2884 Ffetch_bytecode (fun);
2885 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2886 AREF (fun, COMPILED_CONSTANTS),
2887 AREF (fun, COMPILED_STACK_DEPTH),
2888 syms_left,
2889 nargs, arg_vector);
2891 lexenv = Qnil;
2893 else
2894 emacs_abort ();
2896 i = optional = rest = 0;
2897 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2899 QUIT;
2901 next = XCAR (syms_left);
2902 if (!SYMBOLP (next))
2903 xsignal1 (Qinvalid_function, fun);
2905 if (EQ (next, Qand_rest))
2906 rest = 1;
2907 else if (EQ (next, Qand_optional))
2908 optional = 1;
2909 else
2911 Lisp_Object arg;
2912 if (rest)
2914 arg = Flist (nargs - i, &arg_vector[i]);
2915 i = nargs;
2917 else if (i < nargs)
2918 arg = arg_vector[i++];
2919 else if (!optional)
2920 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2921 else
2922 arg = Qnil;
2924 /* Bind the argument. */
2925 if (!NILP (lexenv) && SYMBOLP (next))
2926 /* Lexically bind NEXT by adding it to the lexenv alist. */
2927 lexenv = Fcons (Fcons (next, arg), lexenv);
2928 else
2929 /* Dynamically bind NEXT. */
2930 specbind (next, arg);
2934 if (!NILP (syms_left))
2935 xsignal1 (Qinvalid_function, fun);
2936 else if (i < nargs)
2937 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2939 if (!EQ (lexenv, Vinternal_interpreter_environment))
2940 /* Instantiate a new lexical environment. */
2941 specbind (Qinternal_interpreter_environment, lexenv);
2943 if (CONSP (fun))
2944 val = Fprogn (XCDR (XCDR (fun)));
2945 else
2947 /* If we have not actually read the bytecode string
2948 and constants vector yet, fetch them from the file. */
2949 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2950 Ffetch_bytecode (fun);
2951 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2952 AREF (fun, COMPILED_CONSTANTS),
2953 AREF (fun, COMPILED_STACK_DEPTH),
2954 Qnil, 0, 0);
2957 return unbind_to (count, val);
2960 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2961 1, 1, 0,
2962 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2963 (Lisp_Object object)
2965 Lisp_Object tem;
2967 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2969 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2970 if (!CONSP (tem))
2972 tem = AREF (object, COMPILED_BYTECODE);
2973 if (CONSP (tem) && STRINGP (XCAR (tem)))
2974 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2975 else
2976 error ("Invalid byte code");
2978 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2979 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2981 return object;
2984 /* Return true if SYMBOL currently has a let-binding
2985 which was made in the buffer that is now current. */
2987 bool
2988 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2990 union specbinding *p;
2991 Lisp_Object buf = Fcurrent_buffer ();
2993 for (p = specpdl_ptr; p > specpdl; )
2994 if ((--p)->kind > SPECPDL_LET)
2996 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2997 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2998 if (symbol == let_bound_symbol
2999 && EQ (specpdl_where (p), buf))
3000 return 1;
3003 return 0;
3006 bool
3007 let_shadows_global_binding_p (Lisp_Object symbol)
3009 union specbinding *p;
3011 for (p = specpdl_ptr; p > specpdl; )
3012 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3013 return 1;
3015 return 0;
3018 /* `specpdl_ptr' describes which variable is
3019 let-bound, so it can be properly undone when we unbind_to.
3020 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3021 - SYMBOL is the variable being bound. Note that it should not be
3022 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3023 to record V2 here).
3024 - WHERE tells us in which buffer the binding took place.
3025 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3026 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3027 i.e. bindings to the default value of a variable which can be
3028 buffer-local. */
3030 void
3031 specbind (Lisp_Object symbol, Lisp_Object value)
3033 struct Lisp_Symbol *sym;
3035 CHECK_SYMBOL (symbol);
3036 sym = XSYMBOL (symbol);
3038 start:
3039 switch (sym->redirect)
3041 case SYMBOL_VARALIAS:
3042 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3043 case SYMBOL_PLAINVAL:
3044 /* The most common case is that of a non-constant symbol with a
3045 trivial value. Make that as fast as we can. */
3046 specpdl_ptr->let.kind = SPECPDL_LET;
3047 specpdl_ptr->let.symbol = symbol;
3048 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3049 grow_specpdl ();
3050 if (!sym->constant)
3051 SET_SYMBOL_VAL (sym, value);
3052 else
3053 set_internal (symbol, value, Qnil, 1);
3054 break;
3055 case SYMBOL_LOCALIZED:
3056 if (SYMBOL_BLV (sym)->frame_local)
3057 error ("Frame-local vars cannot be let-bound");
3058 case SYMBOL_FORWARDED:
3060 Lisp_Object ovalue = find_symbol_value (symbol);
3061 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3062 specpdl_ptr->let.symbol = symbol;
3063 specpdl_ptr->let.old_value = ovalue;
3064 specpdl_ptr->let.where = Fcurrent_buffer ();
3066 eassert (sym->redirect != SYMBOL_LOCALIZED
3067 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3069 if (sym->redirect == SYMBOL_LOCALIZED)
3071 if (!blv_found (SYMBOL_BLV (sym)))
3072 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3074 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3076 /* If SYMBOL is a per-buffer variable which doesn't have a
3077 buffer-local value here, make the `let' change the global
3078 value by changing the value of SYMBOL in all buffers not
3079 having their own value. This is consistent with what
3080 happens with other buffer-local variables. */
3081 if (NILP (Flocal_variable_p (symbol, Qnil)))
3083 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3084 grow_specpdl ();
3085 Fset_default (symbol, value);
3086 return;
3089 else
3090 specpdl_ptr->let.kind = SPECPDL_LET;
3092 grow_specpdl ();
3093 set_internal (symbol, value, Qnil, 1);
3094 break;
3096 default: emacs_abort ();
3100 /* Push unwind-protect entries of various types. */
3102 void
3103 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3105 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3106 specpdl_ptr->unwind.func = function;
3107 specpdl_ptr->unwind.arg = arg;
3108 grow_specpdl ();
3111 void
3112 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3114 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3115 specpdl_ptr->unwind_ptr.func = function;
3116 specpdl_ptr->unwind_ptr.arg = arg;
3117 grow_specpdl ();
3120 void
3121 record_unwind_protect_int (void (*function) (int), int arg)
3123 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3124 specpdl_ptr->unwind_int.func = function;
3125 specpdl_ptr->unwind_int.arg = arg;
3126 grow_specpdl ();
3129 void
3130 record_unwind_protect_void (void (*function) (void))
3132 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3133 specpdl_ptr->unwind_void.func = function;
3134 grow_specpdl ();
3137 static void
3138 do_nothing (void)
3141 /* Push an unwind-protect entry that does nothing, so that
3142 set_unwind_protect_ptr can overwrite it later. */
3144 void
3145 record_unwind_protect_nothing (void)
3147 record_unwind_protect_void (do_nothing);
3150 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3151 It need not be at the top of the stack. */
3153 void
3154 clear_unwind_protect (ptrdiff_t count)
3156 union specbinding *p = specpdl + count;
3157 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3158 p->unwind_void.func = do_nothing;
3161 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3162 It need not be at the top of the stack. Discard the entry's
3163 previous value without invoking it. */
3165 void
3166 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3167 Lisp_Object arg)
3169 union specbinding *p = specpdl + count;
3170 p->unwind.kind = SPECPDL_UNWIND;
3171 p->unwind.func = func;
3172 p->unwind.arg = arg;
3175 void
3176 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3178 union specbinding *p = specpdl + count;
3179 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3180 p->unwind_ptr.func = func;
3181 p->unwind_ptr.arg = arg;
3184 /* Pop and execute entries from the unwind-protect stack until the
3185 depth COUNT is reached. Return VALUE. */
3187 Lisp_Object
3188 unbind_to (ptrdiff_t count, Lisp_Object value)
3190 Lisp_Object quitf = Vquit_flag;
3191 struct gcpro gcpro1, gcpro2;
3193 GCPRO2 (value, quitf);
3194 Vquit_flag = Qnil;
3196 while (specpdl_ptr != specpdl + count)
3198 /* Decrement specpdl_ptr before we do the work to unbind it, so
3199 that an error in unbinding won't try to unbind the same entry
3200 again. Take care to copy any parts of the binding needed
3201 before invoking any code that can make more bindings. */
3203 specpdl_ptr--;
3205 switch (specpdl_ptr->kind)
3207 case SPECPDL_UNWIND:
3208 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3209 break;
3210 case SPECPDL_UNWIND_PTR:
3211 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3212 break;
3213 case SPECPDL_UNWIND_INT:
3214 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3215 break;
3216 case SPECPDL_UNWIND_VOID:
3217 specpdl_ptr->unwind_void.func ();
3218 break;
3219 case SPECPDL_BACKTRACE:
3220 break;
3221 case SPECPDL_LET:
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 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3226 if (sym->redirect == SYMBOL_PLAINVAL)
3228 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3229 break;
3231 else
3232 { /* FALLTHROUGH!!
3233 NOTE: we only ever come here if make_local_foo was used for
3234 the first time on this var within this let. */
3237 case SPECPDL_LET_DEFAULT:
3238 Fset_default (specpdl_symbol (specpdl_ptr),
3239 specpdl_old_value (specpdl_ptr));
3240 break;
3241 case SPECPDL_LET_LOCAL:
3243 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3244 Lisp_Object where = specpdl_where (specpdl_ptr);
3245 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3246 eassert (BUFFERP (where));
3248 /* If this was a local binding, reset the value in the appropriate
3249 buffer, but only if that buffer's binding still exists. */
3250 if (!NILP (Flocal_variable_p (symbol, where)))
3251 set_internal (symbol, old_value, where, 1);
3253 break;
3257 if (NILP (Vquit_flag) && !NILP (quitf))
3258 Vquit_flag = quitf;
3260 UNGCPRO;
3261 return value;
3264 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3265 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3266 A special variable is one that will be bound dynamically, even in a
3267 context where binding is lexical by default. */)
3268 (Lisp_Object symbol)
3270 CHECK_SYMBOL (symbol);
3271 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3275 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3276 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3277 The debugger is entered when that frame exits, if the flag is non-nil. */)
3278 (Lisp_Object level, Lisp_Object flag)
3280 union specbinding *pdl = backtrace_top ();
3281 register EMACS_INT i;
3283 CHECK_NUMBER (level);
3285 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3286 pdl = backtrace_next (pdl);
3288 if (backtrace_p (pdl))
3289 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3291 return flag;
3294 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3295 doc: /* Print a trace of Lisp function calls currently active.
3296 Output stream used is value of `standard-output'. */)
3297 (void)
3299 union specbinding *pdl = backtrace_top ();
3300 Lisp_Object tem;
3301 Lisp_Object old_print_level = Vprint_level;
3303 if (NILP (Vprint_level))
3304 XSETFASTINT (Vprint_level, 8);
3306 while (backtrace_p (pdl))
3308 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3309 if (backtrace_nargs (pdl) == UNEVALLED)
3311 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3312 Qnil);
3313 write_string ("\n");
3315 else
3317 tem = backtrace_function (pdl);
3318 Fprin1 (tem, Qnil); /* This can QUIT. */
3319 write_string ("(");
3321 ptrdiff_t i;
3322 for (i = 0; i < backtrace_nargs (pdl); i++)
3324 if (i) write_string (" ");
3325 Fprin1 (backtrace_args (pdl)[i], Qnil);
3328 write_string (")\n");
3330 pdl = backtrace_next (pdl);
3333 Vprint_level = old_print_level;
3334 return Qnil;
3337 static union specbinding *
3338 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3340 union specbinding *pdl = backtrace_top ();
3341 register EMACS_INT i;
3343 CHECK_NATNUM (nframes);
3345 if (!NILP (base))
3346 { /* Skip up to `base'. */
3347 base = Findirect_function (base, Qt);
3348 while (backtrace_p (pdl)
3349 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3350 pdl = backtrace_next (pdl);
3353 /* Find the frame requested. */
3354 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3355 pdl = backtrace_next (pdl);
3357 return pdl;
3360 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3361 doc: /* Return the function and arguments NFRAMES up from current execution point.
3362 If that frame has not evaluated the arguments yet (or is a special form),
3363 the value is (nil FUNCTION ARG-FORMS...).
3364 If that frame has evaluated its arguments and called its function already,
3365 the value is (t FUNCTION ARG-VALUES...).
3366 A &rest arg is represented as the tail of the list ARG-VALUES.
3367 FUNCTION is whatever was supplied as car of evaluated list,
3368 or a lambda expression for macro calls.
3369 If NFRAMES is more than the number of frames, the value is nil.
3370 If BASE is non-nil, it should be a function and NFRAMES counts from its
3371 nearest activation frame. */)
3372 (Lisp_Object nframes, Lisp_Object base)
3374 union specbinding *pdl = get_backtrace_frame (nframes, base);
3376 if (!backtrace_p (pdl))
3377 return Qnil;
3378 if (backtrace_nargs (pdl) == UNEVALLED)
3379 return Fcons (Qnil,
3380 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3381 else
3383 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3385 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3389 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3390 the specpdl stack, and then rewind them. We store the pre-unwind values
3391 directly in the pre-existing specpdl elements (i.e. we swap the current
3392 value and the old value stored in the specpdl), kind of like the inplace
3393 pointer-reversal trick. As it turns out, the rewind does the same as the
3394 unwind, except it starts from the other end of the specpdl stack, so we use
3395 the same function for both unwind and rewind. */
3396 static void
3397 backtrace_eval_unrewind (int distance)
3399 union specbinding *tmp = specpdl_ptr;
3400 int step = -1;
3401 if (distance < 0)
3402 { /* It's a rewind rather than unwind. */
3403 tmp += distance - 1;
3404 step = 1;
3405 distance = -distance;
3408 for (; distance > 0; distance--)
3410 tmp += step;
3411 switch (tmp->kind)
3413 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3414 unwind_protect, but the problem is that we don't know how to
3415 rewind them afterwards. */
3416 case SPECPDL_UNWIND:
3418 Lisp_Object oldarg = tmp->unwind.arg;
3419 if (tmp->unwind.func == set_buffer_if_live)
3420 tmp->unwind.arg = Fcurrent_buffer ();
3421 else if (tmp->unwind.func == save_excursion_restore)
3422 tmp->unwind.arg = save_excursion_save ();
3423 else
3424 break;
3425 tmp->unwind.func (oldarg);
3426 break;
3429 case SPECPDL_UNWIND_PTR:
3430 case SPECPDL_UNWIND_INT:
3431 case SPECPDL_UNWIND_VOID:
3432 case SPECPDL_BACKTRACE:
3433 break;
3434 case SPECPDL_LET:
3435 { /* If variable has a trivial value (no forwarding), we can
3436 just set it. No need to check for constant symbols here,
3437 since that was already done by specbind. */
3438 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3439 if (sym->redirect == SYMBOL_PLAINVAL)
3441 Lisp_Object old_value = specpdl_old_value (tmp);
3442 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3443 SET_SYMBOL_VAL (sym, old_value);
3444 break;
3446 else
3447 { /* FALLTHROUGH!!
3448 NOTE: we only ever come here if make_local_foo was used for
3449 the first time on this var within this let. */
3452 case SPECPDL_LET_DEFAULT:
3454 Lisp_Object sym = specpdl_symbol (tmp);
3455 Lisp_Object old_value = specpdl_old_value (tmp);
3456 set_specpdl_old_value (tmp, Fdefault_value (sym));
3457 Fset_default (sym, old_value);
3459 break;
3460 case SPECPDL_LET_LOCAL:
3462 Lisp_Object symbol = specpdl_symbol (tmp);
3463 Lisp_Object where = specpdl_where (tmp);
3464 Lisp_Object old_value = specpdl_old_value (tmp);
3465 eassert (BUFFERP (where));
3467 /* If this was a local binding, reset the value in the appropriate
3468 buffer, but only if that buffer's binding still exists. */
3469 if (!NILP (Flocal_variable_p (symbol, where)))
3471 set_specpdl_old_value
3472 (tmp, Fbuffer_local_value (symbol, where));
3473 set_internal (symbol, old_value, where, 1);
3476 break;
3481 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3482 doc: /* Evaluate EXP in the context of some activation frame.
3483 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3484 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3486 union specbinding *pdl = get_backtrace_frame (nframes, base);
3487 ptrdiff_t count = SPECPDL_INDEX ();
3488 ptrdiff_t distance = specpdl_ptr - pdl;
3489 eassert (distance >= 0);
3491 if (!backtrace_p (pdl))
3492 error ("Activation frame not found!");
3494 backtrace_eval_unrewind (distance);
3495 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3497 /* Use eval_sub rather than Feval since the main motivation behind
3498 backtrace-eval is to be able to get/set the value of lexical variables
3499 from the debugger. */
3500 return unbind_to (count, eval_sub (exp));
3503 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3504 doc: /* Return names and values of local variables of a stack frame.
3505 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3506 (Lisp_Object nframes, Lisp_Object base)
3508 union specbinding *frame = get_backtrace_frame (nframes, base);
3509 union specbinding *prevframe
3510 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3511 ptrdiff_t distance = specpdl_ptr - frame;
3512 Lisp_Object result = Qnil;
3513 eassert (distance >= 0);
3515 if (!backtrace_p (prevframe))
3516 error ("Activation frame not found!");
3517 if (!backtrace_p (frame))
3518 error ("Activation frame not found!");
3520 /* The specpdl entries normally contain the symbol being bound along with its
3521 `old_value', so it can be restored. The new value to which it is bound is
3522 available in one of two places: either in the current value of the
3523 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3524 next specpdl entry for it.
3525 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3526 and "new value", so we abuse it here, to fetch the new value.
3527 It's ugly (we'd rather not modify global data) and a bit inefficient,
3528 but it does the job for now. */
3529 backtrace_eval_unrewind (distance);
3531 /* Grab values. */
3533 union specbinding *tmp = prevframe;
3534 for (; tmp > frame; tmp--)
3536 switch (tmp->kind)
3538 case SPECPDL_LET:
3539 case SPECPDL_LET_DEFAULT:
3540 case SPECPDL_LET_LOCAL:
3542 Lisp_Object sym = specpdl_symbol (tmp);
3543 Lisp_Object val = specpdl_old_value (tmp);
3544 if (EQ (sym, Qinternal_interpreter_environment))
3546 Lisp_Object env = val;
3547 for (; CONSP (env); env = XCDR (env))
3549 Lisp_Object binding = XCAR (env);
3550 if (CONSP (binding))
3551 result = Fcons (Fcons (XCAR (binding),
3552 XCDR (binding)),
3553 result);
3556 else
3557 result = Fcons (Fcons (sym, val), result);
3563 /* Restore values from specpdl to original place. */
3564 backtrace_eval_unrewind (-distance);
3566 return result;
3570 void
3571 mark_specpdl (void)
3573 union specbinding *pdl;
3574 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3576 switch (pdl->kind)
3578 case SPECPDL_UNWIND:
3579 mark_object (specpdl_arg (pdl));
3580 break;
3582 case SPECPDL_BACKTRACE:
3584 ptrdiff_t nargs = backtrace_nargs (pdl);
3585 mark_object (backtrace_function (pdl));
3586 if (nargs == UNEVALLED)
3587 nargs = 1;
3588 while (nargs--)
3589 mark_object (backtrace_args (pdl)[nargs]);
3591 break;
3593 case SPECPDL_LET_DEFAULT:
3594 case SPECPDL_LET_LOCAL:
3595 mark_object (specpdl_where (pdl));
3596 /* Fall through. */
3597 case SPECPDL_LET:
3598 mark_object (specpdl_symbol (pdl));
3599 mark_object (specpdl_old_value (pdl));
3600 break;
3605 void
3606 get_backtrace (Lisp_Object array)
3608 union specbinding *pdl = backtrace_next (backtrace_top ());
3609 ptrdiff_t i = 0, asize = ASIZE (array);
3611 /* Copy the backtrace contents into working memory. */
3612 for (; i < asize; i++)
3614 if (backtrace_p (pdl))
3616 ASET (array, i, backtrace_function (pdl));
3617 pdl = backtrace_next (pdl);
3619 else
3620 ASET (array, i, Qnil);
3624 Lisp_Object backtrace_top_function (void)
3626 union specbinding *pdl = backtrace_top ();
3627 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3630 void
3631 syms_of_eval (void)
3633 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3634 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3635 If Lisp code tries to increase the total number past this amount,
3636 an error is signaled.
3637 You can safely use a value considerably larger than the default value,
3638 if that proves inconveniently small. However, if you increase it too far,
3639 Emacs could run out of memory trying to make the stack bigger.
3640 Note that this limit may be silently increased by the debugger
3641 if `debug-on-error' or `debug-on-quit' is set. */);
3643 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3644 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3646 This limit serves to catch infinite recursions for you before they cause
3647 actual stack overflow in C, which would be fatal for Emacs.
3648 You can safely make it considerably larger than its default value,
3649 if that proves inconveniently small. However, if you increase it too far,
3650 Emacs could overflow the real C stack, and crash. */);
3652 DEFVAR_LISP ("quit-flag", Vquit_flag,
3653 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3654 If the value is t, that means do an ordinary quit.
3655 If the value equals `throw-on-input', that means quit by throwing
3656 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3657 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3658 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3659 Vquit_flag = Qnil;
3661 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3662 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3663 Note that `quit-flag' will still be set by typing C-g,
3664 so a quit will be signaled as soon as `inhibit-quit' is nil.
3665 To prevent this happening, set `quit-flag' to nil
3666 before making `inhibit-quit' nil. */);
3667 Vinhibit_quit = Qnil;
3669 DEFSYM (Qinhibit_quit, "inhibit-quit");
3670 DEFSYM (Qautoload, "autoload");
3671 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3672 DEFSYM (Qmacro, "macro");
3673 DEFSYM (Qdeclare, "declare");
3675 /* Note that the process handling also uses Qexit, but we don't want
3676 to staticpro it twice, so we just do it here. */
3677 DEFSYM (Qexit, "exit");
3679 DEFSYM (Qinteractive, "interactive");
3680 DEFSYM (Qcommandp, "commandp");
3681 DEFSYM (Qand_rest, "&rest");
3682 DEFSYM (Qand_optional, "&optional");
3683 DEFSYM (Qclosure, "closure");
3684 DEFSYM (QCdocumentation, ":documentation");
3685 DEFSYM (Qdebug, "debug");
3687 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3688 doc: /* Non-nil means never enter the debugger.
3689 Normally set while the debugger is already active, to avoid recursive
3690 invocations. */);
3691 Vinhibit_debugger = Qnil;
3693 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3694 doc: /* Non-nil means enter debugger if an error is signaled.
3695 Does not apply to errors handled by `condition-case' or those
3696 matched by `debug-ignored-errors'.
3697 If the value is a list, an error only means to enter the debugger
3698 if one of its condition symbols appears in the list.
3699 When you evaluate an expression interactively, this variable
3700 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3701 The command `toggle-debug-on-error' toggles this.
3702 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3703 Vdebug_on_error = Qnil;
3705 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3706 doc: /* List of errors for which the debugger should not be called.
3707 Each element may be a condition-name or a regexp that matches error messages.
3708 If any element applies to a given error, that error skips the debugger
3709 and just returns to top level.
3710 This overrides the variable `debug-on-error'.
3711 It does not apply to errors handled by `condition-case'. */);
3712 Vdebug_ignored_errors = Qnil;
3714 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3715 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3716 Does not apply if quit is handled by a `condition-case'. */);
3717 debug_on_quit = 0;
3719 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3720 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3722 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3723 doc: /* Non-nil means debugger may continue execution.
3724 This is nil when the debugger is called under circumstances where it
3725 might not be safe to continue. */);
3726 debugger_may_continue = 1;
3728 DEFVAR_LISP ("debugger", Vdebugger,
3729 doc: /* Function to call to invoke debugger.
3730 If due to frame exit, args are `exit' and the value being returned;
3731 this function's value will be returned instead of that.
3732 If due to error, args are `error' and a list of the args to `signal'.
3733 If due to `apply' or `funcall' entry, one arg, `lambda'.
3734 If due to `eval' entry, one arg, t. */);
3735 Vdebugger = Qnil;
3737 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3738 doc: /* If non-nil, this is a function for `signal' to call.
3739 It receives the same arguments that `signal' was given.
3740 The Edebug package uses this to regain control. */);
3741 Vsignal_hook_function = Qnil;
3743 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3744 doc: /* Non-nil means call the debugger regardless of condition handlers.
3745 Note that `debug-on-error', `debug-on-quit' and friends
3746 still determine whether to handle the particular condition. */);
3747 Vdebug_on_signal = Qnil;
3749 /* When lexical binding is being used,
3750 Vinternal_interpreter_environment is non-nil, and contains an alist
3751 of lexically-bound variable, or (t), indicating an empty
3752 environment. The lisp name of this variable would be
3753 `internal-interpreter-environment' if it weren't hidden.
3754 Every element of this list can be either a cons (VAR . VAL)
3755 specifying a lexical binding, or a single symbol VAR indicating
3756 that this variable should use dynamic scoping. */
3757 DEFSYM (Qinternal_interpreter_environment,
3758 "internal-interpreter-environment");
3759 DEFVAR_LISP ("internal-interpreter-environment",
3760 Vinternal_interpreter_environment,
3761 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3762 When lexical binding is not being used, this variable is nil.
3763 A value of `(t)' indicates an empty environment, otherwise it is an
3764 alist of active lexical bindings. */);
3765 Vinternal_interpreter_environment = Qnil;
3766 /* Don't export this variable to Elisp, so no one can mess with it
3767 (Just imagine if someone makes it buffer-local). */
3768 Funintern (Qinternal_interpreter_environment, Qnil);
3770 Vrun_hooks = intern_c_string ("run-hooks");
3771 staticpro (&Vrun_hooks);
3773 staticpro (&Vautoload_queue);
3774 Vautoload_queue = Qnil;
3775 staticpro (&Vsignaling_function);
3776 Vsignaling_function = Qnil;
3778 inhibit_lisp_code = Qnil;
3780 defsubr (&Sor);
3781 defsubr (&Sand);
3782 defsubr (&Sif);
3783 defsubr (&Scond);
3784 defsubr (&Sprogn);
3785 defsubr (&Sprog1);
3786 defsubr (&Sprog2);
3787 defsubr (&Ssetq);
3788 defsubr (&Squote);
3789 defsubr (&Sfunction);
3790 defsubr (&Sdefault_toplevel_value);
3791 defsubr (&Sset_default_toplevel_value);
3792 defsubr (&Sdefvar);
3793 defsubr (&Sdefvaralias);
3794 defsubr (&Sdefconst);
3795 defsubr (&Smake_var_non_special);
3796 defsubr (&Slet);
3797 defsubr (&SletX);
3798 defsubr (&Swhile);
3799 defsubr (&Smacroexpand);
3800 defsubr (&Scatch);
3801 defsubr (&Sthrow);
3802 defsubr (&Sunwind_protect);
3803 defsubr (&Scondition_case);
3804 defsubr (&Ssignal);
3805 defsubr (&Scommandp);
3806 defsubr (&Sautoload);
3807 defsubr (&Sautoload_do_load);
3808 defsubr (&Seval);
3809 defsubr (&Sapply);
3810 defsubr (&Sfuncall);
3811 defsubr (&Srun_hooks);
3812 defsubr (&Srun_hook_with_args);
3813 defsubr (&Srun_hook_with_args_until_success);
3814 defsubr (&Srun_hook_with_args_until_failure);
3815 defsubr (&Srun_hook_wrapped);
3816 defsubr (&Sfetch_bytecode);
3817 defsubr (&Sbacktrace_debug);
3818 defsubr (&Sbacktrace);
3819 defsubr (&Sbacktrace_frame);
3820 defsubr (&Sbacktrace_eval);
3821 defsubr (&Sbacktrace__locals);
3822 defsubr (&Sspecial_variable_p);
3823 defsubr (&Sfunctionp);