lisp/frameset.el (frameset--reuse-frame): Remove workaround for bug#16793.
[emacs.git] / src / eval.c
bloba96d413d09fc6494c9982d3046b05a29a8076a94
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2014 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 "frame.h" /* For XFRAME. */
32 #if HAVE_X_WINDOWS
33 #include "xterm.h"
34 #endif
36 /* Chain of condition and catch handlers currently in effect. */
38 struct handler *handlerlist;
40 #ifdef DEBUG_GCPRO
41 /* Count levels of GCPRO to detect failure to UNGCPRO. */
42 int gcpro_level;
43 #endif
45 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
46 Lisp_Object Qinhibit_quit;
47 Lisp_Object Qand_rest;
48 static Lisp_Object Qand_optional;
49 static Lisp_Object Qinhibit_debugger;
50 static Lisp_Object Qdeclare;
51 Lisp_Object Qinternal_interpreter_environment, Qclosure;
53 static Lisp_Object Qdebug;
55 /* This holds either the symbol `run-hooks' or nil.
56 It is nil at an early stage of startup, and when Emacs
57 is shutting down. */
59 Lisp_Object Vrun_hooks;
61 /* Non-nil means record all fset's and provide's, to be undone
62 if the file being autoloaded is not fully loaded.
63 They are recorded by being consed onto the front of Vautoload_queue:
64 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
66 Lisp_Object Vautoload_queue;
68 /* Current number of specbindings allocated in specpdl, not counting
69 the dummy entry specpdl[-1]. */
71 ptrdiff_t specpdl_size;
73 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
74 only so that its address can be taken. */
76 union specbinding *specpdl;
78 /* Pointer to first unused element in specpdl. */
80 union specbinding *specpdl_ptr;
82 /* Depth in Lisp evaluations and function calls. */
84 EMACS_INT lisp_eval_depth;
86 /* The value of num_nonmacro_input_events as of the last time we
87 started to enter the debugger. If we decide to enter the debugger
88 again when this is still equal to num_nonmacro_input_events, then we
89 know that the debugger itself has an error, and we should just
90 signal the error instead of entering an infinite loop of debugger
91 invocations. */
93 static EMACS_INT when_entered_debugger;
95 /* The function from which the last `signal' was called. Set in
96 Fsignal. */
97 /* FIXME: We should probably get rid of this! */
98 Lisp_Object Vsignaling_function;
100 /* If non-nil, Lisp code must not be run since some part of Emacs is
101 in an inconsistent state. Currently, x-create-frame uses this to
102 avoid triggering window-configuration-change-hook while the new
103 frame is half-initialized. */
104 Lisp_Object inhibit_lisp_code;
106 /* These would ordinarily be static, but they need to be visible to GDB. */
107 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
108 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
109 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
110 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
111 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
113 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
114 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
116 static Lisp_Object
117 specpdl_symbol (union specbinding *pdl)
119 eassert (pdl->kind >= SPECPDL_LET);
120 return pdl->let.symbol;
123 static Lisp_Object
124 specpdl_old_value (union specbinding *pdl)
126 eassert (pdl->kind >= SPECPDL_LET);
127 return pdl->let.old_value;
130 static void
131 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
133 eassert (pdl->kind >= SPECPDL_LET);
134 pdl->let.old_value = val;
137 static Lisp_Object
138 specpdl_where (union specbinding *pdl)
140 eassert (pdl->kind > SPECPDL_LET);
141 return pdl->let.where;
144 static Lisp_Object
145 specpdl_arg (union specbinding *pdl)
147 eassert (pdl->kind == SPECPDL_UNWIND);
148 return pdl->unwind.arg;
151 Lisp_Object
152 backtrace_function (union specbinding *pdl)
154 eassert (pdl->kind == SPECPDL_BACKTRACE);
155 return pdl->bt.function;
158 static ptrdiff_t
159 backtrace_nargs (union specbinding *pdl)
161 eassert (pdl->kind == SPECPDL_BACKTRACE);
162 return pdl->bt.nargs;
165 Lisp_Object *
166 backtrace_args (union specbinding *pdl)
168 eassert (pdl->kind == SPECPDL_BACKTRACE);
169 return pdl->bt.args;
172 static bool
173 backtrace_debug_on_exit (union specbinding *pdl)
175 eassert (pdl->kind == SPECPDL_BACKTRACE);
176 return pdl->bt.debug_on_exit;
179 /* Functions to modify slots of backtrace records. */
181 static void
182 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
184 eassert (pdl->kind == SPECPDL_BACKTRACE);
185 pdl->bt.args = args;
188 static void
189 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
191 eassert (pdl->kind == SPECPDL_BACKTRACE);
192 pdl->bt.nargs = n;
195 static void
196 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
198 eassert (pdl->kind == SPECPDL_BACKTRACE);
199 pdl->bt.debug_on_exit = doe;
202 /* Helper functions to scan the backtrace. */
204 bool
205 backtrace_p (union specbinding *pdl)
206 { return pdl >= specpdl; }
208 union specbinding *
209 backtrace_top (void)
211 union specbinding *pdl = specpdl_ptr - 1;
212 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
213 pdl--;
214 return pdl;
217 union specbinding *
218 backtrace_next (union specbinding *pdl)
220 pdl--;
221 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
222 pdl--;
223 return pdl;
227 void
228 init_eval_once (void)
230 enum { size = 50 };
231 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
232 specpdl_size = size;
233 specpdl = specpdl_ptr = pdlvec + 1;
234 /* Don't forget to update docs (lispref node "Local Variables"). */
235 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
236 max_lisp_eval_depth = 600;
238 Vrun_hooks = Qnil;
241 static struct handler handlerlist_sentinel;
243 void
244 init_eval (void)
246 specpdl_ptr = specpdl;
247 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
248 This is important since handlerlist->nextfree holds the freelist
249 which would otherwise leak every time we unwind back to top-level. */
250 struct handler *c;
251 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
252 PUSH_HANDLER (c, Qunbound, CATCHER);
253 eassert (c == &handlerlist_sentinel);
254 handlerlist_sentinel.nextfree = NULL;
255 handlerlist_sentinel.next = NULL;
257 Vquit_flag = Qnil;
258 debug_on_next_call = 0;
259 lisp_eval_depth = 0;
260 #ifdef DEBUG_GCPRO
261 gcpro_level = 0;
262 #endif
263 /* This is less than the initial value of num_nonmacro_input_events. */
264 when_entered_debugger = -1;
267 /* Unwind-protect function used by call_debugger. */
269 static void
270 restore_stack_limits (Lisp_Object data)
272 max_specpdl_size = XINT (XCAR (data));
273 max_lisp_eval_depth = XINT (XCDR (data));
276 static void grow_specpdl (void);
278 /* Call the Lisp debugger, giving it argument ARG. */
280 Lisp_Object
281 call_debugger (Lisp_Object arg)
283 bool debug_while_redisplaying;
284 ptrdiff_t count = SPECPDL_INDEX ();
285 Lisp_Object val;
286 EMACS_INT old_depth = max_lisp_eval_depth;
287 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
288 EMACS_INT old_max = max (max_specpdl_size, count);
290 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
291 max_lisp_eval_depth = lisp_eval_depth + 40;
293 /* While debugging Bug#16603, previous value of 100 was found
294 too small to avoid specpdl overflow in the debugger itself. */
295 if (max_specpdl_size - 200 < count)
296 max_specpdl_size = count + 200;
298 if (old_max == count)
300 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
301 specpdl_ptr--;
302 grow_specpdl ();
305 /* Restore limits after leaving the debugger. */
306 record_unwind_protect (restore_stack_limits,
307 Fcons (make_number (old_max),
308 make_number (old_depth)));
310 #ifdef HAVE_WINDOW_SYSTEM
311 if (display_hourglass_p)
312 cancel_hourglass ();
313 #endif
315 debug_on_next_call = 0;
316 when_entered_debugger = num_nonmacro_input_events;
318 /* Resetting redisplaying_p to 0 makes sure that debug output is
319 displayed if the debugger is invoked during redisplay. */
320 debug_while_redisplaying = redisplaying_p;
321 redisplaying_p = 0;
322 specbind (intern ("debugger-may-continue"),
323 debug_while_redisplaying ? Qnil : Qt);
324 specbind (Qinhibit_redisplay, Qnil);
325 specbind (Qinhibit_debugger, Qt);
327 #if 0 /* Binding this prevents execution of Lisp code during
328 redisplay, which necessarily leads to display problems. */
329 specbind (Qinhibit_eval_during_redisplay, Qt);
330 #endif
332 val = apply1 (Vdebugger, arg);
334 /* Interrupting redisplay and resuming it later is not safe under
335 all circumstances. So, when the debugger returns, abort the
336 interrupted redisplay by going back to the top-level. */
337 if (debug_while_redisplaying)
338 Ftop_level ();
340 return unbind_to (count, val);
343 static void
344 do_debug_on_call (Lisp_Object code)
346 debug_on_next_call = 0;
347 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
348 call_debugger (list1 (code));
351 /* NOTE!!! Every function that can call EVAL must protect its args
352 and temporaries from garbage collection while it needs them.
353 The definition of `For' shows what you have to do. */
355 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
356 doc: /* Eval args until one of them yields non-nil, then return that value.
357 The remaining args are not evalled at all.
358 If all args return nil, return nil.
359 usage: (or CONDITIONS...) */)
360 (Lisp_Object args)
362 register Lisp_Object val = Qnil;
363 struct gcpro gcpro1;
365 GCPRO1 (args);
367 while (CONSP (args))
369 val = eval_sub (XCAR (args));
370 if (!NILP (val))
371 break;
372 args = XCDR (args);
375 UNGCPRO;
376 return val;
379 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
380 doc: /* Eval args until one of them yields nil, then return nil.
381 The remaining args are not evalled at all.
382 If no arg yields nil, return the last arg's value.
383 usage: (and CONDITIONS...) */)
384 (Lisp_Object args)
386 register Lisp_Object val = Qt;
387 struct gcpro gcpro1;
389 GCPRO1 (args);
391 while (CONSP (args))
393 val = eval_sub (XCAR (args));
394 if (NILP (val))
395 break;
396 args = XCDR (args);
399 UNGCPRO;
400 return val;
403 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
404 doc: /* If COND yields non-nil, do THEN, else do ELSE...
405 Returns the value of THEN or the value of the last of the ELSE's.
406 THEN must be one expression, but ELSE... can be zero or more expressions.
407 If COND yields nil, and there are no ELSE's, the value is nil.
408 usage: (if COND THEN ELSE...) */)
409 (Lisp_Object args)
411 Lisp_Object cond;
412 struct gcpro gcpro1;
414 GCPRO1 (args);
415 cond = eval_sub (XCAR (args));
416 UNGCPRO;
418 if (!NILP (cond))
419 return eval_sub (Fcar (XCDR (args)));
420 return Fprogn (XCDR (XCDR (args)));
423 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
424 doc: /* Try each clause until one succeeds.
425 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
426 and, if the value is non-nil, this clause succeeds:
427 then the expressions in BODY are evaluated and the last one's
428 value is the value of the cond-form.
429 If a clause has one element, as in (CONDITION), then the cond-form
430 returns CONDITION's value, if that is non-nil.
431 If no clause succeeds, cond returns nil.
432 usage: (cond CLAUSES...) */)
433 (Lisp_Object args)
435 Lisp_Object val = args;
436 struct gcpro gcpro1;
438 GCPRO1 (args);
439 while (CONSP (args))
441 Lisp_Object clause = XCAR (args);
442 val = eval_sub (Fcar (clause));
443 if (!NILP (val))
445 if (!NILP (XCDR (clause)))
446 val = Fprogn (XCDR (clause));
447 break;
449 args = XCDR (args);
451 UNGCPRO;
453 return val;
456 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
457 doc: /* Eval BODY forms sequentially and return value of last one.
458 usage: (progn BODY...) */)
459 (Lisp_Object body)
461 Lisp_Object val = Qnil;
462 struct gcpro gcpro1;
464 GCPRO1 (body);
466 while (CONSP (body))
468 val = eval_sub (XCAR (body));
469 body = XCDR (body);
472 UNGCPRO;
473 return val;
476 /* Evaluate BODY sequentially, discarding its value. Suitable for
477 record_unwind_protect. */
479 void
480 unwind_body (Lisp_Object body)
482 Fprogn (body);
485 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
486 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
487 The value of FIRST is saved during the evaluation of the remaining args,
488 whose values are discarded.
489 usage: (prog1 FIRST BODY...) */)
490 (Lisp_Object args)
492 Lisp_Object val;
493 Lisp_Object args_left;
494 struct gcpro gcpro1, gcpro2;
496 args_left = args;
497 val = args;
498 GCPRO2 (args, val);
500 val = eval_sub (XCAR (args_left));
501 while (CONSP (args_left = XCDR (args_left)))
502 eval_sub (XCAR (args_left));
504 UNGCPRO;
505 return val;
508 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
509 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
510 The value of FORM2 is saved during the evaluation of the
511 remaining args, whose values are discarded.
512 usage: (prog2 FORM1 FORM2 BODY...) */)
513 (Lisp_Object args)
515 struct gcpro gcpro1;
517 GCPRO1 (args);
518 eval_sub (XCAR (args));
519 UNGCPRO;
520 return Fprog1 (XCDR (args));
523 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
524 doc: /* Set each SYM to the value of its VAL.
525 The symbols SYM are variables; they are literal (not evaluated).
526 The values VAL are expressions; they are evaluated.
527 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
528 The second VAL is not computed until after the first SYM is set, and so on;
529 each VAL can use the new value of variables set earlier in the `setq'.
530 The return value of the `setq' form is the value of the last VAL.
531 usage: (setq [SYM VAL]...) */)
532 (Lisp_Object args)
534 Lisp_Object val, sym, lex_binding;
536 val = args;
537 if (CONSP (args))
539 Lisp_Object args_left = args;
540 struct gcpro gcpro1;
541 GCPRO1 (args);
545 val = eval_sub (Fcar (XCDR (args_left)));
546 sym = XCAR (args_left);
548 /* Like for eval_sub, we do not check declared_special here since
549 it's been done when let-binding. */
550 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
551 && SYMBOLP (sym)
552 && !NILP (lex_binding
553 = Fassq (sym, Vinternal_interpreter_environment)))
554 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
555 else
556 Fset (sym, val); /* SYM is dynamically bound. */
558 args_left = Fcdr (XCDR (args_left));
560 while (CONSP (args_left));
562 UNGCPRO;
565 return val;
568 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
569 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
570 Warning: `quote' does not construct its return value, but just returns
571 the value that was pre-constructed by the Lisp reader (see info node
572 `(elisp)Printed Representation').
573 This means that '(a . b) is not identical to (cons 'a 'b): the former
574 does not cons. Quoting should be reserved for constants that will
575 never be modified by side-effects, unless you like self-modifying code.
576 See the common pitfall in info node `(elisp)Rearrangement' for an example
577 of unexpected results when a quoted object is modified.
578 usage: (quote ARG) */)
579 (Lisp_Object args)
581 if (CONSP (XCDR (args)))
582 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
583 return XCAR (args);
586 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
587 doc: /* Like `quote', but preferred for objects which are functions.
588 In byte compilation, `function' causes its argument to be compiled.
589 `quote' cannot do that.
590 usage: (function ARG) */)
591 (Lisp_Object args)
593 Lisp_Object quoted = XCAR (args);
595 if (CONSP (XCDR (args)))
596 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
598 if (!NILP (Vinternal_interpreter_environment)
599 && CONSP (quoted)
600 && EQ (XCAR (quoted), Qlambda))
601 /* This is a lambda expression within a lexical environment;
602 return an interpreted closure instead of a simple lambda. */
603 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
604 XCDR (quoted)));
605 else
606 /* Simply quote the argument. */
607 return quoted;
611 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
612 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
613 Aliased variables always have the same value; setting one sets the other.
614 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
615 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
616 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
617 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
618 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
619 The return value is BASE-VARIABLE. */)
620 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
622 struct Lisp_Symbol *sym;
624 CHECK_SYMBOL (new_alias);
625 CHECK_SYMBOL (base_variable);
627 sym = XSYMBOL (new_alias);
629 if (sym->constant)
630 /* Not sure why, but why not? */
631 error ("Cannot make a constant an alias");
633 switch (sym->redirect)
635 case SYMBOL_FORWARDED:
636 error ("Cannot make an internal variable an alias");
637 case SYMBOL_LOCALIZED:
638 error ("Don't know how to make a localized variable an alias");
641 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
642 If n_a is bound, but b_v is not, set the value of b_v to n_a,
643 so that old-code that affects n_a before the aliasing is setup
644 still works. */
645 if (NILP (Fboundp (base_variable)))
646 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
649 union specbinding *p;
651 for (p = specpdl_ptr; p > specpdl; )
652 if ((--p)->kind >= SPECPDL_LET
653 && (EQ (new_alias, specpdl_symbol (p))))
654 error ("Don't know how to make a let-bound variable an alias");
657 sym->declared_special = 1;
658 XSYMBOL (base_variable)->declared_special = 1;
659 sym->redirect = SYMBOL_VARALIAS;
660 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
661 sym->constant = SYMBOL_CONSTANT_P (base_variable);
662 LOADHIST_ATTACH (new_alias);
663 /* Even if docstring is nil: remove old docstring. */
664 Fput (new_alias, Qvariable_documentation, docstring);
666 return base_variable;
669 static union specbinding *
670 default_toplevel_binding (Lisp_Object symbol)
672 union specbinding *binding = NULL;
673 union specbinding *pdl = specpdl_ptr;
674 while (pdl > specpdl)
676 switch ((--pdl)->kind)
678 case SPECPDL_LET_DEFAULT:
679 case SPECPDL_LET:
680 if (EQ (specpdl_symbol (pdl), symbol))
681 binding = pdl;
682 break;
685 return binding;
688 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
689 doc: /* Return SYMBOL's toplevel default value.
690 "Toplevel" means outside of any let binding. */)
691 (Lisp_Object symbol)
693 union specbinding *binding = default_toplevel_binding (symbol);
694 Lisp_Object value
695 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
696 if (!EQ (value, Qunbound))
697 return value;
698 xsignal1 (Qvoid_variable, symbol);
701 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
702 Sset_default_toplevel_value, 2, 2, 0,
703 doc: /* Set SYMBOL's toplevel default value to VALUE.
704 "Toplevel" means outside of any let binding. */)
705 (Lisp_Object symbol, Lisp_Object value)
707 union specbinding *binding = default_toplevel_binding (symbol);
708 if (binding)
709 set_specpdl_old_value (binding, value);
710 else
711 Fset_default (symbol, value);
712 return Qnil;
715 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
716 doc: /* Define SYMBOL as a variable, and return SYMBOL.
717 You are not required to define a variable in order to use it, but
718 defining it lets you supply an initial value and documentation, which
719 can be referred to by the Emacs help facilities and other programming
720 tools. The `defvar' form also declares the variable as \"special\",
721 so that it is always dynamically bound even if `lexical-binding' is t.
723 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
724 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
725 default value is what is set; buffer-local values are not affected.
726 If INITVALUE is missing, SYMBOL's value is not set.
728 If SYMBOL has a local binding, then this form affects the local
729 binding. This is usually not what you want. Thus, if you need to
730 load a file defining variables, with this form or with `defconst' or
731 `defcustom', you should always load that file _outside_ any bindings
732 for these variables. \(`defconst' and `defcustom' behave similarly in
733 this respect.)
735 The optional argument DOCSTRING is a documentation string for the
736 variable.
738 To define a user option, use `defcustom' instead of `defvar'.
739 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
740 (Lisp_Object args)
742 Lisp_Object sym, tem, tail;
744 sym = XCAR (args);
745 tail = XCDR (args);
747 if (CONSP (tail))
749 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
750 error ("Too many arguments");
752 tem = Fdefault_boundp (sym);
754 /* Do it before evaluating the initial value, for self-references. */
755 XSYMBOL (sym)->declared_special = 1;
757 if (NILP (tem))
758 Fset_default (sym, eval_sub (XCAR (tail)));
759 else
760 { /* Check if there is really a global binding rather than just a let
761 binding that shadows the global unboundness of the var. */
762 union specbinding *binding = default_toplevel_binding (sym);
763 if (binding && EQ (specpdl_old_value (binding), Qunbound))
765 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
768 tail = XCDR (tail);
769 tem = Fcar (tail);
770 if (!NILP (tem))
772 if (!NILP (Vpurify_flag))
773 tem = Fpurecopy (tem);
774 Fput (sym, Qvariable_documentation, tem);
776 LOADHIST_ATTACH (sym);
778 else if (!NILP (Vinternal_interpreter_environment)
779 && !XSYMBOL (sym)->declared_special)
780 /* A simple (defvar foo) with lexical scoping does "nothing" except
781 declare that var to be dynamically scoped *locally* (i.e. within
782 the current file or let-block). */
783 Vinternal_interpreter_environment
784 = Fcons (sym, Vinternal_interpreter_environment);
785 else
787 /* Simple (defvar <var>) should not count as a definition at all.
788 It could get in the way of other definitions, and unloading this
789 package could try to make the variable unbound. */
792 return sym;
795 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
796 doc: /* Define SYMBOL as a constant variable.
797 This declares that neither programs nor users should ever change the
798 value. This constancy is not actually enforced by Emacs Lisp, but
799 SYMBOL is marked as a special variable so that it is never lexically
800 bound.
802 The `defconst' form always sets the value of SYMBOL to the result of
803 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
804 what is set; buffer-local values are not affected. If SYMBOL has a
805 local binding, then this form sets the local binding's value.
806 However, you should normally not make local bindings for variables
807 defined with this form.
809 The optional DOCSTRING specifies the variable's documentation string.
810 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
811 (Lisp_Object args)
813 Lisp_Object sym, tem;
815 sym = XCAR (args);
816 if (CONSP (Fcdr (XCDR (XCDR (args)))))
817 error ("Too many arguments");
819 tem = eval_sub (Fcar (XCDR (args)));
820 if (!NILP (Vpurify_flag))
821 tem = Fpurecopy (tem);
822 Fset_default (sym, tem);
823 XSYMBOL (sym)->declared_special = 1;
824 tem = Fcar (XCDR (XCDR (args)));
825 if (!NILP (tem))
827 if (!NILP (Vpurify_flag))
828 tem = Fpurecopy (tem);
829 Fput (sym, Qvariable_documentation, tem);
831 Fput (sym, Qrisky_local_variable, Qt);
832 LOADHIST_ATTACH (sym);
833 return sym;
836 /* Make SYMBOL lexically scoped. */
837 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
838 Smake_var_non_special, 1, 1, 0,
839 doc: /* Internal function. */)
840 (Lisp_Object symbol)
842 CHECK_SYMBOL (symbol);
843 XSYMBOL (symbol)->declared_special = 0;
844 return Qnil;
848 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
849 doc: /* Bind variables according to VARLIST then eval BODY.
850 The value of the last form in BODY is returned.
851 Each element of VARLIST is a symbol (which is bound to nil)
852 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
853 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
854 usage: (let* VARLIST BODY...) */)
855 (Lisp_Object args)
857 Lisp_Object varlist, var, val, elt, lexenv;
858 ptrdiff_t count = SPECPDL_INDEX ();
859 struct gcpro gcpro1, gcpro2, gcpro3;
861 GCPRO3 (args, elt, varlist);
863 lexenv = Vinternal_interpreter_environment;
865 varlist = XCAR (args);
866 while (CONSP (varlist))
868 QUIT;
870 elt = XCAR (varlist);
871 if (SYMBOLP (elt))
873 var = elt;
874 val = Qnil;
876 else if (! NILP (Fcdr (Fcdr (elt))))
877 signal_error ("`let' bindings can have only one value-form", elt);
878 else
880 var = Fcar (elt);
881 val = eval_sub (Fcar (Fcdr (elt)));
884 if (!NILP (lexenv) && SYMBOLP (var)
885 && !XSYMBOL (var)->declared_special
886 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
887 /* Lexically bind VAR by adding it to the interpreter's binding
888 alist. */
890 Lisp_Object newenv
891 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
892 if (EQ (Vinternal_interpreter_environment, lexenv))
893 /* Save the old lexical environment on the specpdl stack,
894 but only for the first lexical binding, since we'll never
895 need to revert to one of the intermediate ones. */
896 specbind (Qinternal_interpreter_environment, newenv);
897 else
898 Vinternal_interpreter_environment = newenv;
900 else
901 specbind (var, val);
903 varlist = XCDR (varlist);
905 UNGCPRO;
906 val = Fprogn (XCDR (args));
907 return unbind_to (count, val);
910 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
911 doc: /* Bind variables according to VARLIST then eval BODY.
912 The value of the last form in BODY is returned.
913 Each element of VARLIST is a symbol (which is bound to nil)
914 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
915 All the VALUEFORMs are evalled before any symbols are bound.
916 usage: (let VARLIST BODY...) */)
917 (Lisp_Object args)
919 Lisp_Object *temps, tem, lexenv;
920 register Lisp_Object elt, varlist;
921 ptrdiff_t count = SPECPDL_INDEX ();
922 ptrdiff_t argnum;
923 struct gcpro gcpro1, gcpro2;
924 USE_SAFE_ALLOCA;
926 varlist = XCAR (args);
928 /* Make space to hold the values to give the bound variables. */
929 elt = Flength (varlist);
930 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
932 /* Compute the values and store them in `temps'. */
934 GCPRO2 (args, *temps);
935 gcpro2.nvars = 0;
937 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
939 QUIT;
940 elt = XCAR (varlist);
941 if (SYMBOLP (elt))
942 temps [argnum++] = Qnil;
943 else if (! NILP (Fcdr (Fcdr (elt))))
944 signal_error ("`let' bindings can have only one value-form", elt);
945 else
946 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
947 gcpro2.nvars = argnum;
949 UNGCPRO;
951 lexenv = Vinternal_interpreter_environment;
953 varlist = XCAR (args);
954 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
956 Lisp_Object var;
958 elt = XCAR (varlist);
959 var = SYMBOLP (elt) ? elt : Fcar (elt);
960 tem = temps[argnum++];
962 if (!NILP (lexenv) && SYMBOLP (var)
963 && !XSYMBOL (var)->declared_special
964 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
965 /* Lexically bind VAR by adding it to the lexenv alist. */
966 lexenv = Fcons (Fcons (var, tem), lexenv);
967 else
968 /* Dynamically bind VAR. */
969 specbind (var, tem);
972 if (!EQ (lexenv, Vinternal_interpreter_environment))
973 /* Instantiate a new lexical environment. */
974 specbind (Qinternal_interpreter_environment, lexenv);
976 elt = Fprogn (XCDR (args));
977 SAFE_FREE ();
978 return unbind_to (count, elt);
981 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
982 doc: /* If TEST yields non-nil, eval BODY... and repeat.
983 The order of execution is thus TEST, BODY, TEST, BODY and so on
984 until TEST returns nil.
985 usage: (while TEST BODY...) */)
986 (Lisp_Object args)
988 Lisp_Object test, body;
989 struct gcpro gcpro1, gcpro2;
991 GCPRO2 (test, body);
993 test = XCAR (args);
994 body = XCDR (args);
995 while (!NILP (eval_sub (test)))
997 QUIT;
998 Fprogn (body);
1001 UNGCPRO;
1002 return Qnil;
1005 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1006 doc: /* Return result of expanding macros at top level of FORM.
1007 If FORM is not a macro call, it is returned unchanged.
1008 Otherwise, the macro is expanded and the expansion is considered
1009 in place of FORM. When a non-macro-call results, it is returned.
1011 The second optional arg ENVIRONMENT specifies an environment of macro
1012 definitions to shadow the loaded ones for use in file byte-compilation. */)
1013 (Lisp_Object form, Lisp_Object environment)
1015 /* With cleanups from Hallvard Furuseth. */
1016 register Lisp_Object expander, sym, def, tem;
1018 while (1)
1020 /* Come back here each time we expand a macro call,
1021 in case it expands into another macro call. */
1022 if (!CONSP (form))
1023 break;
1024 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1025 def = sym = XCAR (form);
1026 tem = Qnil;
1027 /* Trace symbols aliases to other symbols
1028 until we get a symbol that is not an alias. */
1029 while (SYMBOLP (def))
1031 QUIT;
1032 sym = def;
1033 tem = Fassq (sym, environment);
1034 if (NILP (tem))
1036 def = XSYMBOL (sym)->function;
1037 if (!NILP (def))
1038 continue;
1040 break;
1042 /* Right now TEM is the result from SYM in ENVIRONMENT,
1043 and if TEM is nil then DEF is SYM's function definition. */
1044 if (NILP (tem))
1046 /* SYM is not mentioned in ENVIRONMENT.
1047 Look at its function definition. */
1048 struct gcpro gcpro1;
1049 GCPRO1 (form);
1050 def = Fautoload_do_load (def, sym, Qmacro);
1051 UNGCPRO;
1052 if (!CONSP (def))
1053 /* Not defined or definition not suitable. */
1054 break;
1055 if (!EQ (XCAR (def), Qmacro))
1056 break;
1057 else expander = XCDR (def);
1059 else
1061 expander = XCDR (tem);
1062 if (NILP (expander))
1063 break;
1066 Lisp_Object newform = apply1 (expander, XCDR (form));
1067 if (EQ (form, newform))
1068 break;
1069 else
1070 form = newform;
1073 return form;
1076 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1077 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1078 TAG is evalled to get the tag to use; it must not be nil.
1080 Then the BODY is executed.
1081 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1082 If no throw happens, `catch' returns the value of the last BODY form.
1083 If a throw happens, it specifies the value to return from `catch'.
1084 usage: (catch TAG BODY...) */)
1085 (Lisp_Object args)
1087 register Lisp_Object tag;
1088 struct gcpro gcpro1;
1090 GCPRO1 (args);
1091 tag = eval_sub (XCAR (args));
1092 UNGCPRO;
1093 return internal_catch (tag, Fprogn, XCDR (args));
1096 /* Assert that E is true, as a comment only. Use this instead of
1097 eassert (E) when E contains variables that might be clobbered by a
1098 longjmp. */
1100 #define clobbered_eassert(E) ((void) 0)
1102 /* Set up a catch, then call C function FUNC on argument ARG.
1103 FUNC should return a Lisp_Object.
1104 This is how catches are done from within C code. */
1106 Lisp_Object
1107 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1109 /* This structure is made part of the chain `catchlist'. */
1110 struct handler *c;
1112 /* Fill in the components of c, and put it on the list. */
1113 PUSH_HANDLER (c, tag, CATCHER);
1115 /* Call FUNC. */
1116 if (! sys_setjmp (c->jmp))
1118 Lisp_Object val = (*func) (arg);
1119 clobbered_eassert (handlerlist == c);
1120 handlerlist = handlerlist->next;
1121 return val;
1123 else
1124 { /* Throw works by a longjmp that comes right here. */
1125 Lisp_Object val = handlerlist->val;
1126 clobbered_eassert (handlerlist == c);
1127 handlerlist = handlerlist->next;
1128 return val;
1132 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1133 jump to that CATCH, returning VALUE as the value of that catch.
1135 This is the guts of Fthrow and Fsignal; they differ only in the way
1136 they choose the catch tag to throw to. A catch tag for a
1137 condition-case form has a TAG of Qnil.
1139 Before each catch is discarded, unbind all special bindings and
1140 execute all unwind-protect clauses made above that catch. Unwind
1141 the handler stack as we go, so that the proper handlers are in
1142 effect for each unwind-protect clause we run. At the end, restore
1143 some static info saved in CATCH, and longjmp to the location
1144 specified there.
1146 This is used for correct unwinding in Fthrow and Fsignal. */
1148 static _Noreturn void
1149 unwind_to_catch (struct handler *catch, Lisp_Object value)
1151 bool last_time;
1153 eassert (catch->next);
1155 /* Save the value in the tag. */
1156 catch->val = value;
1158 /* Restore certain special C variables. */
1159 set_poll_suppress_count (catch->poll_suppress_count);
1160 unblock_input_to (catch->interrupt_input_blocked);
1161 immediate_quit = 0;
1165 /* Unwind the specpdl stack, and then restore the proper set of
1166 handlers. */
1167 unbind_to (handlerlist->pdlcount, Qnil);
1168 last_time = handlerlist == catch;
1169 if (! last_time)
1170 handlerlist = handlerlist->next;
1172 while (! last_time);
1174 eassert (handlerlist == catch);
1176 byte_stack_list = catch->byte_stack;
1177 gcprolist = catch->gcpro;
1178 #ifdef DEBUG_GCPRO
1179 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1180 #endif
1181 lisp_eval_depth = catch->lisp_eval_depth;
1183 sys_longjmp (catch->jmp, 1);
1186 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1187 doc: /* Throw to the catch for TAG and return VALUE from it.
1188 Both TAG and VALUE are evalled. */)
1189 (register Lisp_Object tag, Lisp_Object value)
1191 struct handler *c;
1193 if (!NILP (tag))
1194 for (c = handlerlist; c; c = c->next)
1196 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1197 unwind_to_catch (c, value);
1199 xsignal2 (Qno_catch, tag, value);
1203 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1204 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1205 If BODYFORM completes normally, its value is returned
1206 after executing the UNWINDFORMS.
1207 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1208 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1209 (Lisp_Object args)
1211 Lisp_Object val;
1212 ptrdiff_t count = SPECPDL_INDEX ();
1214 record_unwind_protect (unwind_body, XCDR (args));
1215 val = eval_sub (XCAR (args));
1216 return unbind_to (count, val);
1219 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1220 doc: /* Regain control when an error is signaled.
1221 Executes BODYFORM and returns its value if no error happens.
1222 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1223 where the BODY is made of Lisp expressions.
1225 A handler is applicable to an error
1226 if CONDITION-NAME is one of the error's condition names.
1227 If an error happens, the first applicable handler is run.
1229 The car of a handler may be a list of condition names instead of a
1230 single condition name; then it handles all of them. If the special
1231 condition name `debug' is present in this list, it allows another
1232 condition in the list to run the debugger if `debug-on-error' and the
1233 other usual mechanisms says it should (otherwise, `condition-case'
1234 suppresses the debugger).
1236 When a handler handles an error, control returns to the `condition-case'
1237 and it executes the handler's BODY...
1238 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1239 \(If VAR is nil, the handler can't access that information.)
1240 Then the value of the last BODY form is returned from the `condition-case'
1241 expression.
1243 See also the function `signal' for more info.
1244 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1245 (Lisp_Object args)
1247 Lisp_Object var = XCAR (args);
1248 Lisp_Object bodyform = XCAR (XCDR (args));
1249 Lisp_Object handlers = XCDR (XCDR (args));
1251 return internal_lisp_condition_case (var, bodyform, handlers);
1254 /* Like Fcondition_case, but the args are separate
1255 rather than passed in a list. Used by Fbyte_code. */
1257 Lisp_Object
1258 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1259 Lisp_Object handlers)
1261 Lisp_Object val;
1262 struct handler *c;
1263 struct handler *oldhandlerlist = handlerlist;
1264 int clausenb = 0;
1266 CHECK_SYMBOL (var);
1268 for (val = handlers; CONSP (val); val = XCDR (val))
1270 Lisp_Object tem = XCAR (val);
1271 clausenb++;
1272 if (! (NILP (tem)
1273 || (CONSP (tem)
1274 && (SYMBOLP (XCAR (tem))
1275 || CONSP (XCAR (tem))))))
1276 error ("Invalid condition handler: %s",
1277 SDATA (Fprin1_to_string (tem, Qt)));
1280 { /* The first clause is the one that should be checked first, so it should
1281 be added to handlerlist last. So we build in `clauses' a table that
1282 contains `handlers' but in reverse order. */
1283 Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *));
1284 Lisp_Object *volatile clauses_volatile = clauses;
1285 int i = clausenb;
1286 for (val = handlers; CONSP (val); val = XCDR (val))
1287 clauses[--i] = XCAR (val);
1288 for (i = 0; i < clausenb; i++)
1290 Lisp_Object clause = clauses[i];
1291 Lisp_Object condition = XCAR (clause);
1292 if (!CONSP (condition))
1293 condition = Fcons (condition, Qnil);
1294 PUSH_HANDLER (c, condition, CONDITION_CASE);
1295 if (sys_setjmp (c->jmp))
1297 ptrdiff_t count = SPECPDL_INDEX ();
1298 Lisp_Object val = handlerlist->val;
1299 Lisp_Object *chosen_clause = clauses_volatile;
1300 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1301 chosen_clause++;
1302 handlerlist = oldhandlerlist;
1303 if (!NILP (var))
1305 if (!NILP (Vinternal_interpreter_environment))
1306 specbind (Qinternal_interpreter_environment,
1307 Fcons (Fcons (var, val),
1308 Vinternal_interpreter_environment));
1309 else
1310 specbind (var, val);
1312 val = Fprogn (XCDR (*chosen_clause));
1313 /* Note that this just undoes the binding of var; whoever
1314 longjumped to us unwound the stack to c.pdlcount before
1315 throwing. */
1316 if (!NILP (var))
1317 unbind_to (count, Qnil);
1318 return val;
1323 val = eval_sub (bodyform);
1324 handlerlist = oldhandlerlist;
1325 return val;
1328 /* Call the function BFUN with no arguments, catching errors within it
1329 according to HANDLERS. If there is an error, call HFUN with
1330 one argument which is the data that describes the error:
1331 (SIGNALNAME . DATA)
1333 HANDLERS can be a list of conditions to catch.
1334 If HANDLERS is Qt, catch all errors.
1335 If HANDLERS is Qerror, catch all errors
1336 but allow the debugger to run if that is enabled. */
1338 Lisp_Object
1339 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1340 Lisp_Object (*hfun) (Lisp_Object))
1342 Lisp_Object val;
1343 struct handler *c;
1345 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1346 if (sys_setjmp (c->jmp))
1348 Lisp_Object val = handlerlist->val;
1349 clobbered_eassert (handlerlist == c);
1350 handlerlist = handlerlist->next;
1351 return (*hfun) (val);
1354 val = (*bfun) ();
1355 clobbered_eassert (handlerlist == c);
1356 handlerlist = handlerlist->next;
1357 return val;
1360 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1362 Lisp_Object
1363 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1364 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1366 Lisp_Object val;
1367 struct handler *c;
1369 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1370 if (sys_setjmp (c->jmp))
1372 Lisp_Object val = handlerlist->val;
1373 clobbered_eassert (handlerlist == c);
1374 handlerlist = handlerlist->next;
1375 return (*hfun) (val);
1378 val = (*bfun) (arg);
1379 clobbered_eassert (handlerlist == c);
1380 handlerlist = handlerlist->next;
1381 return val;
1384 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1385 its arguments. */
1387 Lisp_Object
1388 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1389 Lisp_Object arg1,
1390 Lisp_Object arg2,
1391 Lisp_Object handlers,
1392 Lisp_Object (*hfun) (Lisp_Object))
1394 Lisp_Object val;
1395 struct handler *c;
1397 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1398 if (sys_setjmp (c->jmp))
1400 Lisp_Object val = handlerlist->val;
1401 clobbered_eassert (handlerlist == c);
1402 handlerlist = handlerlist->next;
1403 return (*hfun) (val);
1406 val = (*bfun) (arg1, arg2);
1407 clobbered_eassert (handlerlist == c);
1408 handlerlist = handlerlist->next;
1409 return val;
1412 /* Like internal_condition_case but call BFUN with NARGS as first,
1413 and ARGS as second argument. */
1415 Lisp_Object
1416 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1417 ptrdiff_t nargs,
1418 Lisp_Object *args,
1419 Lisp_Object handlers,
1420 Lisp_Object (*hfun) (Lisp_Object err,
1421 ptrdiff_t nargs,
1422 Lisp_Object *args))
1424 Lisp_Object val;
1425 struct handler *c;
1427 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1428 if (sys_setjmp (c->jmp))
1430 Lisp_Object val = handlerlist->val;
1431 clobbered_eassert (handlerlist == c);
1432 handlerlist = handlerlist->next;
1433 return (*hfun) (val, nargs, args);
1436 val = (*bfun) (nargs, args);
1437 clobbered_eassert (handlerlist == c);
1438 handlerlist = handlerlist->next;
1439 return val;
1443 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1444 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1445 Lisp_Object data);
1447 void
1448 process_quit_flag (void)
1450 Lisp_Object flag = Vquit_flag;
1451 Vquit_flag = Qnil;
1452 if (EQ (flag, Qkill_emacs))
1453 Fkill_emacs (Qnil);
1454 if (EQ (Vthrow_on_input, flag))
1455 Fthrow (Vthrow_on_input, Qt);
1456 Fsignal (Qquit, Qnil);
1459 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1460 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1461 This function does not return.
1463 An error symbol is a symbol with an `error-conditions' property
1464 that is a list of condition names.
1465 A handler for any of those names will get to handle this signal.
1466 The symbol `error' should normally be one of them.
1468 DATA should be a list. Its elements are printed as part of the error message.
1469 See Info anchor `(elisp)Definition of signal' for some details on how this
1470 error message is constructed.
1471 If the signal is handled, DATA is made available to the handler.
1472 See also the function `condition-case'. */)
1473 (Lisp_Object error_symbol, Lisp_Object data)
1475 /* When memory is full, ERROR-SYMBOL is nil,
1476 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1477 That is a special case--don't do this in other situations. */
1478 Lisp_Object conditions;
1479 Lisp_Object string;
1480 Lisp_Object real_error_symbol
1481 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1482 register Lisp_Object clause = Qnil;
1483 struct handler *h;
1485 immediate_quit = 0;
1486 abort_on_gc = 0;
1487 if (gc_in_progress || waiting_for_input)
1488 emacs_abort ();
1490 #if 0 /* rms: I don't know why this was here,
1491 but it is surely wrong for an error that is handled. */
1492 #ifdef HAVE_WINDOW_SYSTEM
1493 if (display_hourglass_p)
1494 cancel_hourglass ();
1495 #endif
1496 #endif
1498 /* This hook is used by edebug. */
1499 if (! NILP (Vsignal_hook_function)
1500 && ! NILP (error_symbol))
1502 /* Edebug takes care of restoring these variables when it exits. */
1503 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1504 max_lisp_eval_depth = lisp_eval_depth + 20;
1506 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1507 max_specpdl_size = SPECPDL_INDEX () + 40;
1509 call2 (Vsignal_hook_function, error_symbol, data);
1512 conditions = Fget (real_error_symbol, Qerror_conditions);
1514 /* Remember from where signal was called. Skip over the frame for
1515 `signal' itself. If a frame for `error' follows, skip that,
1516 too. Don't do this when ERROR_SYMBOL is nil, because that
1517 is a memory-full error. */
1518 Vsignaling_function = Qnil;
1519 if (!NILP (error_symbol))
1521 union specbinding *pdl = backtrace_next (backtrace_top ());
1522 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1523 pdl = backtrace_next (pdl);
1524 if (backtrace_p (pdl))
1525 Vsignaling_function = backtrace_function (pdl);
1528 for (h = handlerlist; h; h = h->next)
1530 if (h->type != CONDITION_CASE)
1531 continue;
1532 clause = find_handler_clause (h->tag_or_ch, conditions);
1533 if (!NILP (clause))
1534 break;
1537 if (/* Don't run the debugger for a memory-full error.
1538 (There is no room in memory to do that!) */
1539 !NILP (error_symbol)
1540 && (!NILP (Vdebug_on_signal)
1541 /* If no handler is present now, try to run the debugger. */
1542 || NILP (clause)
1543 /* A `debug' symbol in the handler list disables the normal
1544 suppression of the debugger. */
1545 || (CONSP (clause) && CONSP (clause)
1546 && !NILP (Fmemq (Qdebug, clause)))
1547 /* Special handler that means "print a message and run debugger
1548 if requested". */
1549 || EQ (h->tag_or_ch, Qerror)))
1551 bool debugger_called
1552 = maybe_call_debugger (conditions, error_symbol, data);
1553 /* We can't return values to code which signaled an error, but we
1554 can continue code which has signaled a quit. */
1555 if (debugger_called && EQ (real_error_symbol, Qquit))
1556 return Qnil;
1559 if (!NILP (clause))
1561 Lisp_Object unwind_data
1562 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1564 unwind_to_catch (h, unwind_data);
1566 else
1568 if (handlerlist != &handlerlist_sentinel)
1569 /* FIXME: This will come right back here if there's no `top-level'
1570 catcher. A better solution would be to abort here, and instead
1571 add a catch-all condition handler so we never come here. */
1572 Fthrow (Qtop_level, Qt);
1575 if (! NILP (error_symbol))
1576 data = Fcons (error_symbol, data);
1578 string = Ferror_message_string (data);
1579 fatal ("%s", SDATA (string));
1582 /* Internal version of Fsignal that never returns.
1583 Used for anything but Qquit (which can return from Fsignal). */
1585 void
1586 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1588 Fsignal (error_symbol, data);
1589 emacs_abort ();
1592 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1594 void
1595 xsignal0 (Lisp_Object error_symbol)
1597 xsignal (error_symbol, Qnil);
1600 void
1601 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1603 xsignal (error_symbol, list1 (arg));
1606 void
1607 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1609 xsignal (error_symbol, list2 (arg1, arg2));
1612 void
1613 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1615 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1618 /* Signal `error' with message S, and additional arg ARG.
1619 If ARG is not a genuine list, make it a one-element list. */
1621 void
1622 signal_error (const char *s, Lisp_Object arg)
1624 Lisp_Object tortoise, hare;
1626 hare = tortoise = arg;
1627 while (CONSP (hare))
1629 hare = XCDR (hare);
1630 if (!CONSP (hare))
1631 break;
1633 hare = XCDR (hare);
1634 tortoise = XCDR (tortoise);
1636 if (EQ (hare, tortoise))
1637 break;
1640 if (!NILP (hare))
1641 arg = list1 (arg);
1643 xsignal (Qerror, Fcons (build_string (s), arg));
1647 /* Return true if LIST is a non-nil atom or
1648 a list containing one of CONDITIONS. */
1650 static bool
1651 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1653 if (NILP (list))
1654 return 0;
1655 if (! CONSP (list))
1656 return 1;
1658 while (CONSP (conditions))
1660 Lisp_Object this, tail;
1661 this = XCAR (conditions);
1662 for (tail = list; CONSP (tail); tail = XCDR (tail))
1663 if (EQ (XCAR (tail), this))
1664 return 1;
1665 conditions = XCDR (conditions);
1667 return 0;
1670 /* Return true if an error with condition-symbols CONDITIONS,
1671 and described by SIGNAL-DATA, should skip the debugger
1672 according to debugger-ignored-errors. */
1674 static bool
1675 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1677 Lisp_Object tail;
1678 bool first_string = 1;
1679 Lisp_Object error_message;
1681 error_message = Qnil;
1682 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1684 if (STRINGP (XCAR (tail)))
1686 if (first_string)
1688 error_message = Ferror_message_string (data);
1689 first_string = 0;
1692 if (fast_string_match (XCAR (tail), error_message) >= 0)
1693 return 1;
1695 else
1697 Lisp_Object contail;
1699 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1700 if (EQ (XCAR (tail), XCAR (contail)))
1701 return 1;
1705 return 0;
1708 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1709 SIG and DATA describe the signal. There are two ways to pass them:
1710 = SIG is the error symbol, and DATA is the rest of the data.
1711 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1712 This is for memory-full errors only. */
1713 static bool
1714 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1716 Lisp_Object combined_data;
1718 combined_data = Fcons (sig, data);
1720 if (
1721 /* Don't try to run the debugger with interrupts blocked.
1722 The editing loop would return anyway. */
1723 ! input_blocked_p ()
1724 && NILP (Vinhibit_debugger)
1725 /* Does user want to enter debugger for this kind of error? */
1726 && (EQ (sig, Qquit)
1727 ? debug_on_quit
1728 : wants_debugger (Vdebug_on_error, conditions))
1729 && ! skip_debugger (conditions, combined_data)
1730 /* RMS: What's this for? */
1731 && when_entered_debugger < num_nonmacro_input_events)
1733 call_debugger (list2 (Qerror, combined_data));
1734 return 1;
1737 return 0;
1740 static Lisp_Object
1741 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1743 register Lisp_Object h;
1745 /* t is used by handlers for all conditions, set up by C code. */
1746 if (EQ (handlers, Qt))
1747 return Qt;
1749 /* error is used similarly, but means print an error message
1750 and run the debugger if that is enabled. */
1751 if (EQ (handlers, Qerror))
1752 return Qt;
1754 for (h = handlers; CONSP (h); h = XCDR (h))
1756 Lisp_Object handler = XCAR (h);
1757 if (!NILP (Fmemq (handler, conditions)))
1758 return handlers;
1761 return Qnil;
1765 /* Dump an error message; called like vprintf. */
1766 void
1767 verror (const char *m, va_list ap)
1769 char buf[4000];
1770 ptrdiff_t size = sizeof buf;
1771 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1772 char *buffer = buf;
1773 ptrdiff_t used;
1774 Lisp_Object string;
1776 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1777 string = make_string (buffer, used);
1778 if (buffer != buf)
1779 xfree (buffer);
1781 xsignal1 (Qerror, string);
1785 /* Dump an error message; called like printf. */
1787 /* VARARGS 1 */
1788 void
1789 error (const char *m, ...)
1791 va_list ap;
1792 va_start (ap, m);
1793 verror (m, ap);
1796 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1797 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1798 This means it contains a description for how to read arguments to give it.
1799 The value is nil for an invalid function or a symbol with no function
1800 definition.
1802 Interactively callable functions include strings and vectors (treated
1803 as keyboard macros), lambda-expressions that contain a top-level call
1804 to `interactive', autoload definitions made by `autoload' with non-nil
1805 fourth argument, and some of the built-in functions of Lisp.
1807 Also, a symbol satisfies `commandp' if its function definition does so.
1809 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1810 then strings and vectors are not accepted. */)
1811 (Lisp_Object function, Lisp_Object for_call_interactively)
1813 register Lisp_Object fun;
1814 register Lisp_Object funcar;
1815 Lisp_Object if_prop = Qnil;
1817 fun = function;
1819 fun = indirect_function (fun); /* Check cycles. */
1820 if (NILP (fun))
1821 return Qnil;
1823 /* Check an `interactive-form' property if present, analogous to the
1824 function-documentation property. */
1825 fun = function;
1826 while (SYMBOLP (fun))
1828 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1829 if (!NILP (tmp))
1830 if_prop = Qt;
1831 fun = Fsymbol_function (fun);
1834 /* Emacs primitives are interactive if their DEFUN specifies an
1835 interactive spec. */
1836 if (SUBRP (fun))
1837 return XSUBR (fun)->intspec ? Qt : if_prop;
1839 /* Bytecode objects are interactive if they are long enough to
1840 have an element whose index is COMPILED_INTERACTIVE, which is
1841 where the interactive spec is stored. */
1842 else if (COMPILEDP (fun))
1843 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1844 ? Qt : if_prop);
1846 /* Strings and vectors are keyboard macros. */
1847 if (STRINGP (fun) || VECTORP (fun))
1848 return (NILP (for_call_interactively) ? Qt : Qnil);
1850 /* Lists may represent commands. */
1851 if (!CONSP (fun))
1852 return Qnil;
1853 funcar = XCAR (fun);
1854 if (EQ (funcar, Qclosure))
1855 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1856 ? Qt : if_prop);
1857 else if (EQ (funcar, Qlambda))
1858 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1859 else if (EQ (funcar, Qautoload))
1860 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1861 else
1862 return Qnil;
1865 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1866 doc: /* Define FUNCTION to autoload from FILE.
1867 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1868 Third arg DOCSTRING is documentation for the function.
1869 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1870 Fifth arg TYPE indicates the type of the object:
1871 nil or omitted says FUNCTION is a function,
1872 `keymap' says FUNCTION is really a keymap, and
1873 `macro' or t says FUNCTION is really a macro.
1874 Third through fifth args give info about the real definition.
1875 They default to nil.
1876 If FUNCTION is already defined other than as an autoload,
1877 this does nothing and returns nil. */)
1878 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1880 CHECK_SYMBOL (function);
1881 CHECK_STRING (file);
1883 /* If function is defined and not as an autoload, don't override. */
1884 if (!NILP (XSYMBOL (function)->function)
1885 && !AUTOLOADP (XSYMBOL (function)->function))
1886 return Qnil;
1888 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1889 /* `read1' in lread.c has found the docstring starting with "\
1890 and assumed the docstring will be provided by Snarf-documentation, so it
1891 passed us 0 instead. But that leads to accidental sharing in purecopy's
1892 hash-consing, so we use a (hopefully) unique integer instead. */
1893 docstring = make_number (XHASH (function));
1894 return Fdefalias (function,
1895 list5 (Qautoload, file, docstring, interactive, type),
1896 Qnil);
1899 void
1900 un_autoload (Lisp_Object oldqueue)
1902 Lisp_Object queue, first, second;
1904 /* Queue to unwind is current value of Vautoload_queue.
1905 oldqueue is the shadowed value to leave in Vautoload_queue. */
1906 queue = Vautoload_queue;
1907 Vautoload_queue = oldqueue;
1908 while (CONSP (queue))
1910 first = XCAR (queue);
1911 second = Fcdr (first);
1912 first = Fcar (first);
1913 if (EQ (first, make_number (0)))
1914 Vfeatures = second;
1915 else
1916 Ffset (first, second);
1917 queue = XCDR (queue);
1921 /* Load an autoloaded function.
1922 FUNNAME is the symbol which is the function's name.
1923 FUNDEF is the autoload definition (a list). */
1925 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1926 doc: /* Load FUNDEF which should be an autoload.
1927 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1928 in which case the function returns the new autoloaded function value.
1929 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1930 it is defines a macro. */)
1931 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1933 ptrdiff_t count = SPECPDL_INDEX ();
1934 struct gcpro gcpro1, gcpro2, gcpro3;
1936 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1937 return fundef;
1939 if (EQ (macro_only, Qmacro))
1941 Lisp_Object kind = Fnth (make_number (4), fundef);
1942 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1943 return fundef;
1946 /* This is to make sure that loadup.el gives a clear picture
1947 of what files are preloaded and when. */
1948 if (! NILP (Vpurify_flag))
1949 error ("Attempt to autoload %s while preparing to dump",
1950 SDATA (SYMBOL_NAME (funname)));
1952 CHECK_SYMBOL (funname);
1953 GCPRO3 (funname, fundef, macro_only);
1955 /* Preserve the match data. */
1956 record_unwind_save_match_data ();
1958 /* If autoloading gets an error (which includes the error of failing
1959 to define the function being called), we use Vautoload_queue
1960 to undo function definitions and `provide' calls made by
1961 the function. We do this in the specific case of autoloading
1962 because autoloading is not an explicit request "load this file",
1963 but rather a request to "call this function".
1965 The value saved here is to be restored into Vautoload_queue. */
1966 record_unwind_protect (un_autoload, Vautoload_queue);
1967 Vautoload_queue = Qt;
1968 /* If `macro_only', assume this autoload to be a "best-effort",
1969 so don't signal an error if autoloading fails. */
1970 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1972 /* Once loading finishes, don't undo it. */
1973 Vautoload_queue = Qt;
1974 unbind_to (count, Qnil);
1976 UNGCPRO;
1978 if (NILP (funname))
1979 return Qnil;
1980 else
1982 Lisp_Object fun = Findirect_function (funname, Qnil);
1984 if (!NILP (Fequal (fun, fundef)))
1985 error ("Autoloading failed to define function %s",
1986 SDATA (SYMBOL_NAME (funname)));
1987 else
1988 return fun;
1993 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1994 doc: /* Evaluate FORM and return its value.
1995 If LEXICAL is t, evaluate using lexical scoping.
1996 LEXICAL can also be an actual lexical environment, in the form of an
1997 alist mapping symbols to their value. */)
1998 (Lisp_Object form, Lisp_Object lexical)
2000 ptrdiff_t count = SPECPDL_INDEX ();
2001 specbind (Qinternal_interpreter_environment,
2002 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2003 return unbind_to (count, eval_sub (form));
2006 /* Grow the specpdl stack by one entry.
2007 The caller should have already initialized the entry.
2008 Signal an error on stack overflow.
2010 Make sure that there is always one unused entry past the top of the
2011 stack, so that the just-initialized entry is safely unwound if
2012 memory exhausted and an error is signaled here. Also, allocate a
2013 never-used entry just before the bottom of the stack; sometimes its
2014 address is taken. */
2016 static void
2017 grow_specpdl (void)
2019 specpdl_ptr++;
2021 if (specpdl_ptr == specpdl + specpdl_size)
2023 ptrdiff_t count = SPECPDL_INDEX ();
2024 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2025 union specbinding *pdlvec = specpdl - 1;
2026 ptrdiff_t pdlvecsize = specpdl_size + 1;
2027 if (max_size <= specpdl_size)
2029 if (max_specpdl_size < 400)
2030 max_size = max_specpdl_size = 400;
2031 if (max_size <= specpdl_size)
2032 signal_error ("Variable binding depth exceeds max-specpdl-size",
2033 Qnil);
2035 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2036 specpdl = pdlvec + 1;
2037 specpdl_size = pdlvecsize - 1;
2038 specpdl_ptr = specpdl + count;
2042 void
2043 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2045 eassert (nargs >= UNEVALLED);
2046 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2047 specpdl_ptr->bt.debug_on_exit = false;
2048 specpdl_ptr->bt.function = function;
2049 specpdl_ptr->bt.args = args;
2050 specpdl_ptr->bt.nargs = nargs;
2051 grow_specpdl ();
2054 /* Eval a sub-expression of the current expression (i.e. in the same
2055 lexical scope). */
2056 Lisp_Object
2057 eval_sub (Lisp_Object form)
2059 Lisp_Object fun, val, original_fun, original_args;
2060 Lisp_Object funcar;
2061 struct gcpro gcpro1, gcpro2, gcpro3;
2063 if (SYMBOLP (form))
2065 /* Look up its binding in the lexical environment.
2066 We do not pay attention to the declared_special flag here, since we
2067 already did that when let-binding the variable. */
2068 Lisp_Object lex_binding
2069 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2070 ? Fassq (form, Vinternal_interpreter_environment)
2071 : Qnil;
2072 if (CONSP (lex_binding))
2073 return XCDR (lex_binding);
2074 else
2075 return Fsymbol_value (form);
2078 if (!CONSP (form))
2079 return form;
2081 QUIT;
2083 GCPRO1 (form);
2084 maybe_gc ();
2085 UNGCPRO;
2087 if (++lisp_eval_depth > max_lisp_eval_depth)
2089 if (max_lisp_eval_depth < 100)
2090 max_lisp_eval_depth = 100;
2091 if (lisp_eval_depth > max_lisp_eval_depth)
2092 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2095 original_fun = XCAR (form);
2096 original_args = XCDR (form);
2098 /* This also protects them from gc. */
2099 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2101 if (debug_on_next_call)
2102 do_debug_on_call (Qt);
2104 /* At this point, only original_fun and original_args
2105 have values that will be used below. */
2106 retry:
2108 /* Optimize for no indirection. */
2109 fun = original_fun;
2110 if (!SYMBOLP (fun))
2111 fun = Ffunction (Fcons (fun, Qnil));
2112 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2113 fun = indirect_function (fun);
2115 if (SUBRP (fun))
2117 Lisp_Object numargs;
2118 Lisp_Object argvals[8];
2119 Lisp_Object args_left;
2120 register int i, maxargs;
2122 args_left = original_args;
2123 numargs = Flength (args_left);
2125 check_cons_list ();
2127 if (XINT (numargs) < XSUBR (fun)->min_args
2128 || (XSUBR (fun)->max_args >= 0
2129 && XSUBR (fun)->max_args < XINT (numargs)))
2130 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2132 else if (XSUBR (fun)->max_args == UNEVALLED)
2133 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2134 else if (XSUBR (fun)->max_args == MANY)
2136 /* Pass a vector of evaluated arguments. */
2137 Lisp_Object *vals;
2138 ptrdiff_t argnum = 0;
2139 USE_SAFE_ALLOCA;
2141 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2143 GCPRO3 (args_left, fun, fun);
2144 gcpro3.var = vals;
2145 gcpro3.nvars = 0;
2147 while (!NILP (args_left))
2149 vals[argnum++] = eval_sub (Fcar (args_left));
2150 args_left = Fcdr (args_left);
2151 gcpro3.nvars = argnum;
2154 set_backtrace_args (specpdl_ptr - 1, vals);
2155 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2157 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2158 UNGCPRO;
2159 SAFE_FREE ();
2161 else
2163 GCPRO3 (args_left, fun, fun);
2164 gcpro3.var = argvals;
2165 gcpro3.nvars = 0;
2167 maxargs = XSUBR (fun)->max_args;
2168 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2170 argvals[i] = eval_sub (Fcar (args_left));
2171 gcpro3.nvars = ++i;
2174 UNGCPRO;
2176 set_backtrace_args (specpdl_ptr - 1, argvals);
2177 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2179 switch (i)
2181 case 0:
2182 val = (XSUBR (fun)->function.a0 ());
2183 break;
2184 case 1:
2185 val = (XSUBR (fun)->function.a1 (argvals[0]));
2186 break;
2187 case 2:
2188 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2189 break;
2190 case 3:
2191 val = (XSUBR (fun)->function.a3
2192 (argvals[0], argvals[1], argvals[2]));
2193 break;
2194 case 4:
2195 val = (XSUBR (fun)->function.a4
2196 (argvals[0], argvals[1], argvals[2], argvals[3]));
2197 break;
2198 case 5:
2199 val = (XSUBR (fun)->function.a5
2200 (argvals[0], argvals[1], argvals[2], argvals[3],
2201 argvals[4]));
2202 break;
2203 case 6:
2204 val = (XSUBR (fun)->function.a6
2205 (argvals[0], argvals[1], argvals[2], argvals[3],
2206 argvals[4], argvals[5]));
2207 break;
2208 case 7:
2209 val = (XSUBR (fun)->function.a7
2210 (argvals[0], argvals[1], argvals[2], argvals[3],
2211 argvals[4], argvals[5], argvals[6]));
2212 break;
2214 case 8:
2215 val = (XSUBR (fun)->function.a8
2216 (argvals[0], argvals[1], argvals[2], argvals[3],
2217 argvals[4], argvals[5], argvals[6], argvals[7]));
2218 break;
2220 default:
2221 /* Someone has created a subr that takes more arguments than
2222 is supported by this code. We need to either rewrite the
2223 subr to use a different argument protocol, or add more
2224 cases to this switch. */
2225 emacs_abort ();
2229 else if (COMPILEDP (fun))
2230 val = apply_lambda (fun, original_args);
2231 else
2233 if (NILP (fun))
2234 xsignal1 (Qvoid_function, original_fun);
2235 if (!CONSP (fun))
2236 xsignal1 (Qinvalid_function, original_fun);
2237 funcar = XCAR (fun);
2238 if (!SYMBOLP (funcar))
2239 xsignal1 (Qinvalid_function, original_fun);
2240 if (EQ (funcar, Qautoload))
2242 Fautoload_do_load (fun, original_fun, Qnil);
2243 goto retry;
2245 if (EQ (funcar, Qmacro))
2247 ptrdiff_t count = SPECPDL_INDEX ();
2248 Lisp_Object exp;
2249 /* Bind lexical-binding during expansion of the macro, so the
2250 macro can know reliably if the code it outputs will be
2251 interpreted using lexical-binding or not. */
2252 specbind (Qlexical_binding,
2253 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2254 exp = apply1 (Fcdr (fun), original_args);
2255 unbind_to (count, Qnil);
2256 val = eval_sub (exp);
2258 else if (EQ (funcar, Qlambda)
2259 || EQ (funcar, Qclosure))
2260 val = apply_lambda (fun, original_args);
2261 else
2262 xsignal1 (Qinvalid_function, original_fun);
2264 check_cons_list ();
2266 lisp_eval_depth--;
2267 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2268 val = call_debugger (list2 (Qexit, val));
2269 specpdl_ptr--;
2271 return val;
2274 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2275 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2276 Then return the value FUNCTION returns.
2277 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2278 usage: (apply FUNCTION &rest ARGUMENTS) */)
2279 (ptrdiff_t nargs, Lisp_Object *args)
2281 ptrdiff_t i;
2282 EMACS_INT numargs;
2283 register Lisp_Object spread_arg;
2284 register Lisp_Object *funcall_args;
2285 Lisp_Object fun, retval;
2286 struct gcpro gcpro1;
2287 USE_SAFE_ALLOCA;
2289 fun = args [0];
2290 funcall_args = 0;
2291 spread_arg = args [nargs - 1];
2292 CHECK_LIST (spread_arg);
2294 numargs = XINT (Flength (spread_arg));
2296 if (numargs == 0)
2297 return Ffuncall (nargs - 1, args);
2298 else if (numargs == 1)
2300 args [nargs - 1] = XCAR (spread_arg);
2301 return Ffuncall (nargs, args);
2304 numargs += nargs - 2;
2306 /* Optimize for no indirection. */
2307 if (SYMBOLP (fun) && !NILP (fun)
2308 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2309 fun = indirect_function (fun);
2310 if (NILP (fun))
2312 /* Let funcall get the error. */
2313 fun = args[0];
2314 goto funcall;
2317 if (SUBRP (fun))
2319 if (numargs < XSUBR (fun)->min_args
2320 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2321 goto funcall; /* Let funcall get the error. */
2322 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2324 /* Avoid making funcall cons up a yet another new vector of arguments
2325 by explicitly supplying nil's for optional values. */
2326 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2327 for (i = numargs; i < XSUBR (fun)->max_args;)
2328 funcall_args[++i] = Qnil;
2329 GCPRO1 (*funcall_args);
2330 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2333 funcall:
2334 /* We add 1 to numargs because funcall_args includes the
2335 function itself as well as its arguments. */
2336 if (!funcall_args)
2338 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2339 GCPRO1 (*funcall_args);
2340 gcpro1.nvars = 1 + numargs;
2343 memcpy (funcall_args, args, nargs * word_size);
2344 /* Spread the last arg we got. Its first element goes in
2345 the slot that it used to occupy, hence this value of I. */
2346 i = nargs - 1;
2347 while (!NILP (spread_arg))
2349 funcall_args [i++] = XCAR (spread_arg);
2350 spread_arg = XCDR (spread_arg);
2353 /* By convention, the caller needs to gcpro Ffuncall's args. */
2354 retval = Ffuncall (gcpro1.nvars, funcall_args);
2355 UNGCPRO;
2356 SAFE_FREE ();
2358 return retval;
2361 /* Run hook variables in various ways. */
2363 static Lisp_Object
2364 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2366 Ffuncall (nargs, args);
2367 return Qnil;
2370 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2371 doc: /* Run each hook in HOOKS.
2372 Each argument should be a symbol, a hook variable.
2373 These symbols are processed in the order specified.
2374 If a hook symbol has a non-nil value, that value may be a function
2375 or a list of functions to be called to run the hook.
2376 If the value is a function, it is called with no arguments.
2377 If it is a list, the elements are called, in order, with no arguments.
2379 Major modes should not use this function directly to run their mode
2380 hook; they should use `run-mode-hooks' instead.
2382 Do not use `make-local-variable' to make a hook variable buffer-local.
2383 Instead, use `add-hook' and specify t for the LOCAL argument.
2384 usage: (run-hooks &rest HOOKS) */)
2385 (ptrdiff_t nargs, Lisp_Object *args)
2387 Lisp_Object hook[1];
2388 ptrdiff_t i;
2390 for (i = 0; i < nargs; i++)
2392 hook[0] = args[i];
2393 run_hook_with_args (1, hook, funcall_nil);
2396 return Qnil;
2399 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2400 Srun_hook_with_args, 1, MANY, 0,
2401 doc: /* Run HOOK with the specified arguments ARGS.
2402 HOOK should be a symbol, a hook variable. The value of HOOK
2403 may be nil, a function, or a list of functions. Call each
2404 function in order with arguments ARGS. The final return value
2405 is unspecified.
2407 Do not use `make-local-variable' to make a hook variable buffer-local.
2408 Instead, use `add-hook' and specify t for the LOCAL argument.
2409 usage: (run-hook-with-args HOOK &rest ARGS) */)
2410 (ptrdiff_t nargs, Lisp_Object *args)
2412 return run_hook_with_args (nargs, args, funcall_nil);
2415 /* NB this one still documents a specific non-nil return value.
2416 (As did run-hook-with-args and run-hook-with-args-until-failure
2417 until they were changed in 24.1.) */
2418 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2419 Srun_hook_with_args_until_success, 1, MANY, 0,
2420 doc: /* Run HOOK with the specified arguments ARGS.
2421 HOOK should be a symbol, a hook variable. The value of HOOK
2422 may be nil, a function, or a list of functions. Call each
2423 function in order with arguments ARGS, stopping at the first
2424 one that returns non-nil, and return that value. Otherwise (if
2425 all functions return nil, or if there are no functions to call),
2426 return nil.
2428 Do not use `make-local-variable' to make a hook variable buffer-local.
2429 Instead, use `add-hook' and specify t for the LOCAL argument.
2430 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2431 (ptrdiff_t nargs, Lisp_Object *args)
2433 return run_hook_with_args (nargs, args, Ffuncall);
2436 static Lisp_Object
2437 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2439 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2442 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2443 Srun_hook_with_args_until_failure, 1, MANY, 0,
2444 doc: /* Run HOOK with the specified arguments ARGS.
2445 HOOK should be a symbol, a hook variable. The value of HOOK
2446 may be nil, a function, or a list of functions. Call each
2447 function in order with arguments ARGS, stopping at the first
2448 one that returns nil, and return nil. Otherwise (if all functions
2449 return non-nil, or if there are no functions to call), return non-nil
2450 \(do not rely on the precise return value in this case).
2452 Do not use `make-local-variable' to make a hook variable buffer-local.
2453 Instead, use `add-hook' and specify t for the LOCAL argument.
2454 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2455 (ptrdiff_t nargs, Lisp_Object *args)
2457 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2460 static Lisp_Object
2461 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2463 Lisp_Object tmp = args[0], ret;
2464 args[0] = args[1];
2465 args[1] = tmp;
2466 ret = Ffuncall (nargs, args);
2467 args[1] = args[0];
2468 args[0] = tmp;
2469 return ret;
2472 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2473 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2474 I.e. instead of calling each function FUN directly with arguments ARGS,
2475 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2476 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2477 aborts and returns that value.
2478 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2479 (ptrdiff_t nargs, Lisp_Object *args)
2481 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2484 /* ARGS[0] should be a hook symbol.
2485 Call each of the functions in the hook value, passing each of them
2486 as arguments all the rest of ARGS (all NARGS - 1 elements).
2487 FUNCALL specifies how to call each function on the hook.
2488 The caller (or its caller, etc) must gcpro all of ARGS,
2489 except that it isn't necessary to gcpro ARGS[0]. */
2491 Lisp_Object
2492 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2493 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2495 Lisp_Object sym, val, ret = Qnil;
2496 struct gcpro gcpro1, gcpro2, gcpro3;
2498 /* If we are dying or still initializing,
2499 don't do anything--it would probably crash if we tried. */
2500 if (NILP (Vrun_hooks))
2501 return Qnil;
2503 sym = args[0];
2504 val = find_symbol_value (sym);
2506 if (EQ (val, Qunbound) || NILP (val))
2507 return ret;
2508 else if (!CONSP (val) || FUNCTIONP (val))
2510 args[0] = val;
2511 return funcall (nargs, args);
2513 else
2515 Lisp_Object global_vals = Qnil;
2516 GCPRO3 (sym, val, global_vals);
2518 for (;
2519 CONSP (val) && NILP (ret);
2520 val = XCDR (val))
2522 if (EQ (XCAR (val), Qt))
2524 /* t indicates this hook has a local binding;
2525 it means to run the global binding too. */
2526 global_vals = Fdefault_value (sym);
2527 if (NILP (global_vals)) continue;
2529 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2531 args[0] = global_vals;
2532 ret = funcall (nargs, args);
2534 else
2536 for (;
2537 CONSP (global_vals) && NILP (ret);
2538 global_vals = XCDR (global_vals))
2540 args[0] = XCAR (global_vals);
2541 /* In a global value, t should not occur. If it does, we
2542 must ignore it to avoid an endless loop. */
2543 if (!EQ (args[0], Qt))
2544 ret = funcall (nargs, args);
2548 else
2550 args[0] = XCAR (val);
2551 ret = funcall (nargs, args);
2555 UNGCPRO;
2556 return ret;
2560 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2562 void
2563 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2565 Lisp_Object temp[3];
2566 temp[0] = hook;
2567 temp[1] = arg1;
2568 temp[2] = arg2;
2570 Frun_hook_with_args (3, temp);
2573 /* Apply fn to arg. */
2574 Lisp_Object
2575 apply1 (Lisp_Object fn, Lisp_Object arg)
2577 struct gcpro gcpro1;
2579 GCPRO1 (fn);
2580 if (NILP (arg))
2581 RETURN_UNGCPRO (Ffuncall (1, &fn));
2582 gcpro1.nvars = 2;
2584 Lisp_Object args[2];
2585 args[0] = fn;
2586 args[1] = arg;
2587 gcpro1.var = args;
2588 RETURN_UNGCPRO (Fapply (2, args));
2592 /* Call function fn on no arguments. */
2593 Lisp_Object
2594 call0 (Lisp_Object fn)
2596 struct gcpro gcpro1;
2598 GCPRO1 (fn);
2599 RETURN_UNGCPRO (Ffuncall (1, &fn));
2602 /* Call function fn with 1 argument arg1. */
2603 /* ARGSUSED */
2604 Lisp_Object
2605 call1 (Lisp_Object fn, Lisp_Object arg1)
2607 struct gcpro gcpro1;
2608 Lisp_Object args[2];
2610 args[0] = fn;
2611 args[1] = arg1;
2612 GCPRO1 (args[0]);
2613 gcpro1.nvars = 2;
2614 RETURN_UNGCPRO (Ffuncall (2, args));
2617 /* Call function fn with 2 arguments arg1, arg2. */
2618 /* ARGSUSED */
2619 Lisp_Object
2620 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2622 struct gcpro gcpro1;
2623 Lisp_Object args[3];
2624 args[0] = fn;
2625 args[1] = arg1;
2626 args[2] = arg2;
2627 GCPRO1 (args[0]);
2628 gcpro1.nvars = 3;
2629 RETURN_UNGCPRO (Ffuncall (3, args));
2632 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2633 /* ARGSUSED */
2634 Lisp_Object
2635 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2637 struct gcpro gcpro1;
2638 Lisp_Object args[4];
2639 args[0] = fn;
2640 args[1] = arg1;
2641 args[2] = arg2;
2642 args[3] = arg3;
2643 GCPRO1 (args[0]);
2644 gcpro1.nvars = 4;
2645 RETURN_UNGCPRO (Ffuncall (4, args));
2648 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2649 /* ARGSUSED */
2650 Lisp_Object
2651 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2652 Lisp_Object arg4)
2654 struct gcpro gcpro1;
2655 Lisp_Object args[5];
2656 args[0] = fn;
2657 args[1] = arg1;
2658 args[2] = arg2;
2659 args[3] = arg3;
2660 args[4] = arg4;
2661 GCPRO1 (args[0]);
2662 gcpro1.nvars = 5;
2663 RETURN_UNGCPRO (Ffuncall (5, args));
2666 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2667 /* ARGSUSED */
2668 Lisp_Object
2669 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2670 Lisp_Object arg4, Lisp_Object arg5)
2672 struct gcpro gcpro1;
2673 Lisp_Object args[6];
2674 args[0] = fn;
2675 args[1] = arg1;
2676 args[2] = arg2;
2677 args[3] = arg3;
2678 args[4] = arg4;
2679 args[5] = arg5;
2680 GCPRO1 (args[0]);
2681 gcpro1.nvars = 6;
2682 RETURN_UNGCPRO (Ffuncall (6, args));
2685 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2686 /* ARGSUSED */
2687 Lisp_Object
2688 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2689 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2691 struct gcpro gcpro1;
2692 Lisp_Object args[7];
2693 args[0] = fn;
2694 args[1] = arg1;
2695 args[2] = arg2;
2696 args[3] = arg3;
2697 args[4] = arg4;
2698 args[5] = arg5;
2699 args[6] = arg6;
2700 GCPRO1 (args[0]);
2701 gcpro1.nvars = 7;
2702 RETURN_UNGCPRO (Ffuncall (7, args));
2705 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2706 /* ARGSUSED */
2707 Lisp_Object
2708 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2709 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2711 struct gcpro gcpro1;
2712 Lisp_Object args[8];
2713 args[0] = fn;
2714 args[1] = arg1;
2715 args[2] = arg2;
2716 args[3] = arg3;
2717 args[4] = arg4;
2718 args[5] = arg5;
2719 args[6] = arg6;
2720 args[7] = arg7;
2721 GCPRO1 (args[0]);
2722 gcpro1.nvars = 8;
2723 RETURN_UNGCPRO (Ffuncall (8, args));
2726 /* The caller should GCPRO all the elements of ARGS. */
2728 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2729 doc: /* Non-nil if OBJECT is a function. */)
2730 (Lisp_Object object)
2732 if (FUNCTIONP (object))
2733 return Qt;
2734 return Qnil;
2737 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2738 doc: /* Call first argument as a function, passing remaining arguments to it.
2739 Return the value that function returns.
2740 Thus, (funcall 'cons 'x 'y) returns (x . y).
2741 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2742 (ptrdiff_t nargs, Lisp_Object *args)
2744 Lisp_Object fun, original_fun;
2745 Lisp_Object funcar;
2746 ptrdiff_t numargs = nargs - 1;
2747 Lisp_Object lisp_numargs;
2748 Lisp_Object val;
2749 register Lisp_Object *internal_args;
2750 ptrdiff_t i;
2752 QUIT;
2754 if (++lisp_eval_depth > max_lisp_eval_depth)
2756 if (max_lisp_eval_depth < 100)
2757 max_lisp_eval_depth = 100;
2758 if (lisp_eval_depth > max_lisp_eval_depth)
2759 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2762 /* This also GCPROs them. */
2763 record_in_backtrace (args[0], &args[1], nargs - 1);
2765 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2766 maybe_gc ();
2768 if (debug_on_next_call)
2769 do_debug_on_call (Qlambda);
2771 check_cons_list ();
2773 original_fun = args[0];
2775 retry:
2777 /* Optimize for no indirection. */
2778 fun = original_fun;
2779 if (SYMBOLP (fun) && !NILP (fun)
2780 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2781 fun = indirect_function (fun);
2783 if (SUBRP (fun))
2785 if (numargs < XSUBR (fun)->min_args
2786 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2788 XSETFASTINT (lisp_numargs, numargs);
2789 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2792 else if (XSUBR (fun)->max_args == UNEVALLED)
2793 xsignal1 (Qinvalid_function, original_fun);
2795 else if (XSUBR (fun)->max_args == MANY)
2796 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2797 else
2799 if (XSUBR (fun)->max_args > numargs)
2801 internal_args = alloca (XSUBR (fun)->max_args
2802 * sizeof *internal_args);
2803 memcpy (internal_args, args + 1, numargs * word_size);
2804 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2805 internal_args[i] = Qnil;
2807 else
2808 internal_args = args + 1;
2809 switch (XSUBR (fun)->max_args)
2811 case 0:
2812 val = (XSUBR (fun)->function.a0 ());
2813 break;
2814 case 1:
2815 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2816 break;
2817 case 2:
2818 val = (XSUBR (fun)->function.a2
2819 (internal_args[0], internal_args[1]));
2820 break;
2821 case 3:
2822 val = (XSUBR (fun)->function.a3
2823 (internal_args[0], internal_args[1], internal_args[2]));
2824 break;
2825 case 4:
2826 val = (XSUBR (fun)->function.a4
2827 (internal_args[0], internal_args[1], internal_args[2],
2828 internal_args[3]));
2829 break;
2830 case 5:
2831 val = (XSUBR (fun)->function.a5
2832 (internal_args[0], internal_args[1], internal_args[2],
2833 internal_args[3], internal_args[4]));
2834 break;
2835 case 6:
2836 val = (XSUBR (fun)->function.a6
2837 (internal_args[0], internal_args[1], internal_args[2],
2838 internal_args[3], internal_args[4], internal_args[5]));
2839 break;
2840 case 7:
2841 val = (XSUBR (fun)->function.a7
2842 (internal_args[0], internal_args[1], internal_args[2],
2843 internal_args[3], internal_args[4], internal_args[5],
2844 internal_args[6]));
2845 break;
2847 case 8:
2848 val = (XSUBR (fun)->function.a8
2849 (internal_args[0], internal_args[1], internal_args[2],
2850 internal_args[3], internal_args[4], internal_args[5],
2851 internal_args[6], internal_args[7]));
2852 break;
2854 default:
2856 /* If a subr takes more than 8 arguments without using MANY
2857 or UNEVALLED, we need to extend this function to support it.
2858 Until this is done, there is no way to call the function. */
2859 emacs_abort ();
2863 else if (COMPILEDP (fun))
2864 val = funcall_lambda (fun, numargs, args + 1);
2865 else
2867 if (NILP (fun))
2868 xsignal1 (Qvoid_function, original_fun);
2869 if (!CONSP (fun))
2870 xsignal1 (Qinvalid_function, original_fun);
2871 funcar = XCAR (fun);
2872 if (!SYMBOLP (funcar))
2873 xsignal1 (Qinvalid_function, original_fun);
2874 if (EQ (funcar, Qlambda)
2875 || EQ (funcar, Qclosure))
2876 val = funcall_lambda (fun, numargs, args + 1);
2877 else if (EQ (funcar, Qautoload))
2879 Fautoload_do_load (fun, original_fun, Qnil);
2880 check_cons_list ();
2881 goto retry;
2883 else
2884 xsignal1 (Qinvalid_function, original_fun);
2886 check_cons_list ();
2887 lisp_eval_depth--;
2888 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2889 val = call_debugger (list2 (Qexit, val));
2890 specpdl_ptr--;
2891 return val;
2894 static Lisp_Object
2895 apply_lambda (Lisp_Object fun, Lisp_Object args)
2897 Lisp_Object args_left;
2898 ptrdiff_t i;
2899 EMACS_INT numargs;
2900 register Lisp_Object *arg_vector;
2901 struct gcpro gcpro1, gcpro2, gcpro3;
2902 register Lisp_Object tem;
2903 USE_SAFE_ALLOCA;
2905 numargs = XFASTINT (Flength (args));
2906 SAFE_ALLOCA_LISP (arg_vector, numargs);
2907 args_left = args;
2909 GCPRO3 (*arg_vector, args_left, fun);
2910 gcpro1.nvars = 0;
2912 for (i = 0; i < numargs; )
2914 tem = Fcar (args_left), args_left = Fcdr (args_left);
2915 tem = eval_sub (tem);
2916 arg_vector[i++] = tem;
2917 gcpro1.nvars = i;
2920 UNGCPRO;
2922 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2923 set_backtrace_nargs (specpdl_ptr - 1, i);
2924 tem = funcall_lambda (fun, numargs, arg_vector);
2926 /* Do the debug-on-exit now, while arg_vector still exists. */
2927 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2929 /* Don't do it again when we return to eval. */
2930 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2931 tem = call_debugger (list2 (Qexit, tem));
2933 SAFE_FREE ();
2934 return tem;
2937 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2938 and return the result of evaluation.
2939 FUN must be either a lambda-expression or a compiled-code object. */
2941 static Lisp_Object
2942 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2943 register Lisp_Object *arg_vector)
2945 Lisp_Object val, syms_left, next, lexenv;
2946 ptrdiff_t count = SPECPDL_INDEX ();
2947 ptrdiff_t i;
2948 bool optional, rest;
2950 if (CONSP (fun))
2952 if (EQ (XCAR (fun), Qclosure))
2954 fun = XCDR (fun); /* Drop `closure'. */
2955 lexenv = XCAR (fun);
2956 CHECK_LIST_CONS (fun, fun);
2958 else
2959 lexenv = Qnil;
2960 syms_left = XCDR (fun);
2961 if (CONSP (syms_left))
2962 syms_left = XCAR (syms_left);
2963 else
2964 xsignal1 (Qinvalid_function, fun);
2966 else if (COMPILEDP (fun))
2968 syms_left = AREF (fun, COMPILED_ARGLIST);
2969 if (INTEGERP (syms_left))
2970 /* A byte-code object with a non-nil `push args' slot means we
2971 shouldn't bind any arguments, instead just call the byte-code
2972 interpreter directly; it will push arguments as necessary.
2974 Byte-code objects with either a non-existent, or a nil value for
2975 the `push args' slot (the default), have dynamically-bound
2976 arguments, and use the argument-binding code below instead (as do
2977 all interpreted functions, even lexically bound ones). */
2979 /* If we have not actually read the bytecode string
2980 and constants vector yet, fetch them from the file. */
2981 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2982 Ffetch_bytecode (fun);
2983 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2984 AREF (fun, COMPILED_CONSTANTS),
2985 AREF (fun, COMPILED_STACK_DEPTH),
2986 syms_left,
2987 nargs, arg_vector);
2989 lexenv = Qnil;
2991 else
2992 emacs_abort ();
2994 i = optional = rest = 0;
2995 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2997 QUIT;
2999 next = XCAR (syms_left);
3000 if (!SYMBOLP (next))
3001 xsignal1 (Qinvalid_function, fun);
3003 if (EQ (next, Qand_rest))
3004 rest = 1;
3005 else if (EQ (next, Qand_optional))
3006 optional = 1;
3007 else
3009 Lisp_Object arg;
3010 if (rest)
3012 arg = Flist (nargs - i, &arg_vector[i]);
3013 i = nargs;
3015 else if (i < nargs)
3016 arg = arg_vector[i++];
3017 else if (!optional)
3018 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3019 else
3020 arg = Qnil;
3022 /* Bind the argument. */
3023 if (!NILP (lexenv) && SYMBOLP (next))
3024 /* Lexically bind NEXT by adding it to the lexenv alist. */
3025 lexenv = Fcons (Fcons (next, arg), lexenv);
3026 else
3027 /* Dynamically bind NEXT. */
3028 specbind (next, arg);
3032 if (!NILP (syms_left))
3033 xsignal1 (Qinvalid_function, fun);
3034 else if (i < nargs)
3035 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3037 if (!EQ (lexenv, Vinternal_interpreter_environment))
3038 /* Instantiate a new lexical environment. */
3039 specbind (Qinternal_interpreter_environment, lexenv);
3041 if (CONSP (fun))
3042 val = Fprogn (XCDR (XCDR (fun)));
3043 else
3045 /* If we have not actually read the bytecode string
3046 and constants vector yet, fetch them from the file. */
3047 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3048 Ffetch_bytecode (fun);
3049 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3050 AREF (fun, COMPILED_CONSTANTS),
3051 AREF (fun, COMPILED_STACK_DEPTH),
3052 Qnil, 0, 0);
3055 return unbind_to (count, val);
3058 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3059 1, 1, 0,
3060 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3061 (Lisp_Object object)
3063 Lisp_Object tem;
3065 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3067 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3068 if (!CONSP (tem))
3070 tem = AREF (object, COMPILED_BYTECODE);
3071 if (CONSP (tem) && STRINGP (XCAR (tem)))
3072 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3073 else
3074 error ("Invalid byte code");
3076 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3077 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3079 return object;
3082 /* Return true if SYMBOL currently has a let-binding
3083 which was made in the buffer that is now current. */
3085 bool
3086 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3088 union specbinding *p;
3089 Lisp_Object buf = Fcurrent_buffer ();
3091 for (p = specpdl_ptr; p > specpdl; )
3092 if ((--p)->kind > SPECPDL_LET)
3094 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3095 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3096 if (symbol == let_bound_symbol
3097 && EQ (specpdl_where (p), buf))
3098 return 1;
3101 return 0;
3104 bool
3105 let_shadows_global_binding_p (Lisp_Object symbol)
3107 union specbinding *p;
3109 for (p = specpdl_ptr; p > specpdl; )
3110 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3111 return 1;
3113 return 0;
3116 /* `specpdl_ptr' describes which variable is
3117 let-bound, so it can be properly undone when we unbind_to.
3118 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3119 - SYMBOL is the variable being bound. Note that it should not be
3120 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3121 to record V2 here).
3122 - WHERE tells us in which buffer the binding took place.
3123 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3124 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3125 i.e. bindings to the default value of a variable which can be
3126 buffer-local. */
3128 void
3129 specbind (Lisp_Object symbol, Lisp_Object value)
3131 struct Lisp_Symbol *sym;
3133 CHECK_SYMBOL (symbol);
3134 sym = XSYMBOL (symbol);
3136 start:
3137 switch (sym->redirect)
3139 case SYMBOL_VARALIAS:
3140 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3141 case SYMBOL_PLAINVAL:
3142 /* The most common case is that of a non-constant symbol with a
3143 trivial value. Make that as fast as we can. */
3144 specpdl_ptr->let.kind = SPECPDL_LET;
3145 specpdl_ptr->let.symbol = symbol;
3146 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3147 grow_specpdl ();
3148 if (!sym->constant)
3149 SET_SYMBOL_VAL (sym, value);
3150 else
3151 set_internal (symbol, value, Qnil, 1);
3152 break;
3153 case SYMBOL_LOCALIZED:
3154 if (SYMBOL_BLV (sym)->frame_local)
3155 error ("Frame-local vars cannot be let-bound");
3156 case SYMBOL_FORWARDED:
3158 Lisp_Object ovalue = find_symbol_value (symbol);
3159 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3160 specpdl_ptr->let.symbol = symbol;
3161 specpdl_ptr->let.old_value = ovalue;
3162 specpdl_ptr->let.where = Fcurrent_buffer ();
3164 eassert (sym->redirect != SYMBOL_LOCALIZED
3165 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3167 if (sym->redirect == SYMBOL_LOCALIZED)
3169 if (!blv_found (SYMBOL_BLV (sym)))
3170 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3172 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3174 /* If SYMBOL is a per-buffer variable which doesn't have a
3175 buffer-local value here, make the `let' change the global
3176 value by changing the value of SYMBOL in all buffers not
3177 having their own value. This is consistent with what
3178 happens with other buffer-local variables. */
3179 if (NILP (Flocal_variable_p (symbol, Qnil)))
3181 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3182 grow_specpdl ();
3183 Fset_default (symbol, value);
3184 return;
3187 else
3188 specpdl_ptr->let.kind = SPECPDL_LET;
3190 grow_specpdl ();
3191 set_internal (symbol, value, Qnil, 1);
3192 break;
3194 default: emacs_abort ();
3198 /* Push unwind-protect entries of various types. */
3200 void
3201 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3203 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3204 specpdl_ptr->unwind.func = function;
3205 specpdl_ptr->unwind.arg = arg;
3206 grow_specpdl ();
3209 void
3210 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3212 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3213 specpdl_ptr->unwind_ptr.func = function;
3214 specpdl_ptr->unwind_ptr.arg = arg;
3215 grow_specpdl ();
3218 void
3219 record_unwind_protect_int (void (*function) (int), int arg)
3221 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3222 specpdl_ptr->unwind_int.func = function;
3223 specpdl_ptr->unwind_int.arg = arg;
3224 grow_specpdl ();
3227 void
3228 record_unwind_protect_void (void (*function) (void))
3230 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3231 specpdl_ptr->unwind_void.func = function;
3232 grow_specpdl ();
3235 static void
3236 do_nothing (void)
3239 /* Push an unwind-protect entry that does nothing, so that
3240 set_unwind_protect_ptr can overwrite it later. */
3242 void
3243 record_unwind_protect_nothing (void)
3245 record_unwind_protect_void (do_nothing);
3248 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3249 It need not be at the top of the stack. */
3251 void
3252 clear_unwind_protect (ptrdiff_t count)
3254 union specbinding *p = specpdl + count;
3255 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3256 p->unwind_void.func = do_nothing;
3259 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3260 It need not be at the top of the stack. Discard the entry's
3261 previous value without invoking it. */
3263 void
3264 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3265 Lisp_Object arg)
3267 union specbinding *p = specpdl + count;
3268 p->unwind.kind = SPECPDL_UNWIND;
3269 p->unwind.func = func;
3270 p->unwind.arg = arg;
3273 void
3274 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3276 union specbinding *p = specpdl + count;
3277 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3278 p->unwind_ptr.func = func;
3279 p->unwind_ptr.arg = arg;
3282 /* Pop and execute entries from the unwind-protect stack until the
3283 depth COUNT is reached. Return VALUE. */
3285 Lisp_Object
3286 unbind_to (ptrdiff_t count, Lisp_Object value)
3288 Lisp_Object quitf = Vquit_flag;
3289 struct gcpro gcpro1, gcpro2;
3291 GCPRO2 (value, quitf);
3292 Vquit_flag = Qnil;
3294 while (specpdl_ptr != specpdl + count)
3296 /* Decrement specpdl_ptr before we do the work to unbind it, so
3297 that an error in unbinding won't try to unbind the same entry
3298 again. Take care to copy any parts of the binding needed
3299 before invoking any code that can make more bindings. */
3301 specpdl_ptr--;
3303 switch (specpdl_ptr->kind)
3305 case SPECPDL_UNWIND:
3306 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3307 break;
3308 case SPECPDL_UNWIND_PTR:
3309 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3310 break;
3311 case SPECPDL_UNWIND_INT:
3312 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3313 break;
3314 case SPECPDL_UNWIND_VOID:
3315 specpdl_ptr->unwind_void.func ();
3316 break;
3317 case SPECPDL_BACKTRACE:
3318 break;
3319 case SPECPDL_LET:
3320 { /* If variable has a trivial value (no forwarding), we can
3321 just set it. No need to check for constant symbols here,
3322 since that was already done by specbind. */
3323 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3324 if (sym->redirect == SYMBOL_PLAINVAL)
3326 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3327 break;
3329 else
3330 { /* FALLTHROUGH!!
3331 NOTE: we only ever come here if make_local_foo was used for
3332 the first time on this var within this let. */
3335 case SPECPDL_LET_DEFAULT:
3336 Fset_default (specpdl_symbol (specpdl_ptr),
3337 specpdl_old_value (specpdl_ptr));
3338 break;
3339 case SPECPDL_LET_LOCAL:
3341 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3342 Lisp_Object where = specpdl_where (specpdl_ptr);
3343 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3344 eassert (BUFFERP (where));
3346 /* If this was a local binding, reset the value in the appropriate
3347 buffer, but only if that buffer's binding still exists. */
3348 if (!NILP (Flocal_variable_p (symbol, where)))
3349 set_internal (symbol, old_value, where, 1);
3351 break;
3355 if (NILP (Vquit_flag) && !NILP (quitf))
3356 Vquit_flag = quitf;
3358 UNGCPRO;
3359 return value;
3362 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3363 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3364 A special variable is one that will be bound dynamically, even in a
3365 context where binding is lexical by default. */)
3366 (Lisp_Object symbol)
3368 CHECK_SYMBOL (symbol);
3369 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3373 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3374 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3375 The debugger is entered when that frame exits, if the flag is non-nil. */)
3376 (Lisp_Object level, Lisp_Object flag)
3378 union specbinding *pdl = backtrace_top ();
3379 register EMACS_INT i;
3381 CHECK_NUMBER (level);
3383 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3384 pdl = backtrace_next (pdl);
3386 if (backtrace_p (pdl))
3387 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3389 return flag;
3392 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3393 doc: /* Print a trace of Lisp function calls currently active.
3394 Output stream used is value of `standard-output'. */)
3395 (void)
3397 union specbinding *pdl = backtrace_top ();
3398 Lisp_Object tem;
3399 Lisp_Object old_print_level = Vprint_level;
3401 if (NILP (Vprint_level))
3402 XSETFASTINT (Vprint_level, 8);
3404 while (backtrace_p (pdl))
3406 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3407 if (backtrace_nargs (pdl) == UNEVALLED)
3409 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3410 Qnil);
3411 write_string ("\n", -1);
3413 else
3415 tem = backtrace_function (pdl);
3416 Fprin1 (tem, Qnil); /* This can QUIT. */
3417 write_string ("(", -1);
3419 ptrdiff_t i;
3420 for (i = 0; i < backtrace_nargs (pdl); i++)
3422 if (i) write_string (" ", -1);
3423 Fprin1 (backtrace_args (pdl)[i], Qnil);
3426 write_string (")\n", -1);
3428 pdl = backtrace_next (pdl);
3431 Vprint_level = old_print_level;
3432 return Qnil;
3435 static union specbinding *
3436 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3438 union specbinding *pdl = backtrace_top ();
3439 register EMACS_INT i;
3441 CHECK_NATNUM (nframes);
3443 if (!NILP (base))
3444 { /* Skip up to `base'. */
3445 base = Findirect_function (base, Qt);
3446 while (backtrace_p (pdl)
3447 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3448 pdl = backtrace_next (pdl);
3451 /* Find the frame requested. */
3452 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3453 pdl = backtrace_next (pdl);
3455 return pdl;
3458 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3459 doc: /* Return the function and arguments NFRAMES up from current execution point.
3460 If that frame has not evaluated the arguments yet (or is a special form),
3461 the value is (nil FUNCTION ARG-FORMS...).
3462 If that frame has evaluated its arguments and called its function already,
3463 the value is (t FUNCTION ARG-VALUES...).
3464 A &rest arg is represented as the tail of the list ARG-VALUES.
3465 FUNCTION is whatever was supplied as car of evaluated list,
3466 or a lambda expression for macro calls.
3467 If NFRAMES is more than the number of frames, the value is nil.
3468 If BASE is non-nil, it should be a function and NFRAMES counts from its
3469 nearest activation frame. */)
3470 (Lisp_Object nframes, Lisp_Object base)
3472 union specbinding *pdl = get_backtrace_frame (nframes, base);
3474 if (!backtrace_p (pdl))
3475 return Qnil;
3476 if (backtrace_nargs (pdl) == UNEVALLED)
3477 return Fcons (Qnil,
3478 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3479 else
3481 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3483 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3487 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3488 the specpdl stack, and then rewind them. We store the pre-unwind values
3489 directly in the pre-existing specpdl elements (i.e. we swap the current
3490 value and the old value stored in the specpdl), kind of like the inplace
3491 pointer-reversal trick. As it turns out, the rewind does the same as the
3492 unwind, except it starts from the other end of the specpdl stack, so we use
3493 the same function for both unwind and rewind. */
3494 static void
3495 backtrace_eval_unrewind (int distance)
3497 union specbinding *tmp = specpdl_ptr;
3498 int step = -1;
3499 if (distance < 0)
3500 { /* It's a rewind rather than unwind. */
3501 tmp += distance - 1;
3502 step = 1;
3503 distance = -distance;
3506 for (; distance > 0; distance--)
3508 tmp += step;
3509 /* */
3510 switch (tmp->kind)
3512 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3513 unwind_protect, but the problem is that we don't know how to
3514 rewind them afterwards. */
3515 case SPECPDL_UNWIND:
3516 case SPECPDL_UNWIND_PTR:
3517 case SPECPDL_UNWIND_INT:
3518 case SPECPDL_UNWIND_VOID:
3519 case SPECPDL_BACKTRACE:
3520 break;
3521 case SPECPDL_LET:
3522 { /* If variable has a trivial value (no forwarding), we can
3523 just set it. No need to check for constant symbols here,
3524 since that was already done by specbind. */
3525 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3526 if (sym->redirect == SYMBOL_PLAINVAL)
3528 Lisp_Object old_value = specpdl_old_value (tmp);
3529 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3530 SET_SYMBOL_VAL (sym, old_value);
3531 break;
3533 else
3534 { /* FALLTHROUGH!!
3535 NOTE: we only ever come here if make_local_foo was used for
3536 the first time on this var within this let. */
3539 case SPECPDL_LET_DEFAULT:
3541 Lisp_Object sym = specpdl_symbol (tmp);
3542 Lisp_Object old_value = specpdl_old_value (tmp);
3543 set_specpdl_old_value (tmp, Fdefault_value (sym));
3544 Fset_default (sym, old_value);
3546 break;
3547 case SPECPDL_LET_LOCAL:
3549 Lisp_Object symbol = specpdl_symbol (tmp);
3550 Lisp_Object where = specpdl_where (tmp);
3551 Lisp_Object old_value = specpdl_old_value (tmp);
3552 eassert (BUFFERP (where));
3554 /* If this was a local binding, reset the value in the appropriate
3555 buffer, but only if that buffer's binding still exists. */
3556 if (!NILP (Flocal_variable_p (symbol, where)))
3558 set_specpdl_old_value
3559 (tmp, Fbuffer_local_value (symbol, where));
3560 set_internal (symbol, old_value, where, 1);
3563 break;
3568 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3569 doc: /* Evaluate EXP in the context of some activation frame.
3570 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3571 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3573 union specbinding *pdl = get_backtrace_frame (nframes, base);
3574 ptrdiff_t count = SPECPDL_INDEX ();
3575 ptrdiff_t distance = specpdl_ptr - pdl;
3576 eassert (distance >= 0);
3578 if (!backtrace_p (pdl))
3579 error ("Activation frame not found!");
3581 backtrace_eval_unrewind (distance);
3582 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3584 /* Use eval_sub rather than Feval since the main motivation behind
3585 backtrace-eval is to be able to get/set the value of lexical variables
3586 from the debugger. */
3587 return unbind_to (count, eval_sub (exp));
3590 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3591 doc: /* Return names and values of local variables of a stack frame.
3592 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3593 (Lisp_Object nframes, Lisp_Object base)
3595 union specbinding *frame = get_backtrace_frame (nframes, base);
3596 union specbinding *prevframe
3597 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3598 ptrdiff_t distance = specpdl_ptr - frame;
3599 Lisp_Object result = Qnil;
3600 eassert (distance >= 0);
3602 if (!backtrace_p (prevframe))
3603 error ("Activation frame not found!");
3604 if (!backtrace_p (frame))
3605 error ("Activation frame not found!");
3607 /* The specpdl entries normally contain the symbol being bound along with its
3608 `old_value', so it can be restored. The new value to which it is bound is
3609 available in one of two places: either in the current value of the
3610 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3611 next specpdl entry for it.
3612 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3613 and "new value", so we abuse it here, to fetch the new value.
3614 It's ugly (we'd rather not modify global data) and a bit inefficient,
3615 but it does the job for now. */
3616 backtrace_eval_unrewind (distance);
3618 /* Grab values. */
3620 union specbinding *tmp = prevframe;
3621 for (; tmp > frame; tmp--)
3623 switch (tmp->kind)
3625 case SPECPDL_LET:
3626 case SPECPDL_LET_DEFAULT:
3627 case SPECPDL_LET_LOCAL:
3629 Lisp_Object sym = specpdl_symbol (tmp);
3630 Lisp_Object val = specpdl_old_value (tmp);
3631 if (EQ (sym, Qinternal_interpreter_environment))
3633 Lisp_Object env = val;
3634 for (; CONSP (env); env = XCDR (env))
3636 Lisp_Object binding = XCAR (env);
3637 if (CONSP (binding))
3638 result = Fcons (Fcons (XCAR (binding),
3639 XCDR (binding)),
3640 result);
3643 else
3644 result = Fcons (Fcons (sym, val), result);
3650 /* Restore values from specpdl to original place. */
3651 backtrace_eval_unrewind (-distance);
3653 return result;
3657 void
3658 mark_specpdl (void)
3660 union specbinding *pdl;
3661 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3663 switch (pdl->kind)
3665 case SPECPDL_UNWIND:
3666 mark_object (specpdl_arg (pdl));
3667 break;
3669 case SPECPDL_BACKTRACE:
3671 ptrdiff_t nargs = backtrace_nargs (pdl);
3672 mark_object (backtrace_function (pdl));
3673 if (nargs == UNEVALLED)
3674 nargs = 1;
3675 while (nargs--)
3676 mark_object (backtrace_args (pdl)[nargs]);
3678 break;
3680 case SPECPDL_LET_DEFAULT:
3681 case SPECPDL_LET_LOCAL:
3682 mark_object (specpdl_where (pdl));
3683 /* Fall through. */
3684 case SPECPDL_LET:
3685 mark_object (specpdl_symbol (pdl));
3686 mark_object (specpdl_old_value (pdl));
3687 break;
3692 void
3693 get_backtrace (Lisp_Object array)
3695 union specbinding *pdl = backtrace_next (backtrace_top ());
3696 ptrdiff_t i = 0, asize = ASIZE (array);
3698 /* Copy the backtrace contents into working memory. */
3699 for (; i < asize; i++)
3701 if (backtrace_p (pdl))
3703 ASET (array, i, backtrace_function (pdl));
3704 pdl = backtrace_next (pdl);
3706 else
3707 ASET (array, i, Qnil);
3711 Lisp_Object backtrace_top_function (void)
3713 union specbinding *pdl = backtrace_top ();
3714 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3717 void
3718 syms_of_eval (void)
3720 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3721 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3722 If Lisp code tries to increase the total number past this amount,
3723 an error is signaled.
3724 You can safely use a value considerably larger than the default value,
3725 if that proves inconveniently small. However, if you increase it too far,
3726 Emacs could run out of memory trying to make the stack bigger.
3727 Note that this limit may be silently increased by the debugger
3728 if `debug-on-error' or `debug-on-quit' is set. */);
3730 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3731 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3733 This limit serves to catch infinite recursions for you before they cause
3734 actual stack overflow in C, which would be fatal for Emacs.
3735 You can safely make it considerably larger than its default value,
3736 if that proves inconveniently small. However, if you increase it too far,
3737 Emacs could overflow the real C stack, and crash. */);
3739 DEFVAR_LISP ("quit-flag", Vquit_flag,
3740 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3741 If the value is t, that means do an ordinary quit.
3742 If the value equals `throw-on-input', that means quit by throwing
3743 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3744 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3745 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3746 Vquit_flag = Qnil;
3748 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3749 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3750 Note that `quit-flag' will still be set by typing C-g,
3751 so a quit will be signaled as soon as `inhibit-quit' is nil.
3752 To prevent this happening, set `quit-flag' to nil
3753 before making `inhibit-quit' nil. */);
3754 Vinhibit_quit = Qnil;
3756 DEFSYM (Qinhibit_quit, "inhibit-quit");
3757 DEFSYM (Qautoload, "autoload");
3758 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3759 DEFSYM (Qmacro, "macro");
3760 DEFSYM (Qdeclare, "declare");
3762 /* Note that the process handling also uses Qexit, but we don't want
3763 to staticpro it twice, so we just do it here. */
3764 DEFSYM (Qexit, "exit");
3766 DEFSYM (Qinteractive, "interactive");
3767 DEFSYM (Qcommandp, "commandp");
3768 DEFSYM (Qand_rest, "&rest");
3769 DEFSYM (Qand_optional, "&optional");
3770 DEFSYM (Qclosure, "closure");
3771 DEFSYM (Qdebug, "debug");
3773 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3774 doc: /* Non-nil means never enter the debugger.
3775 Normally set while the debugger is already active, to avoid recursive
3776 invocations. */);
3777 Vinhibit_debugger = Qnil;
3779 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3780 doc: /* Non-nil means enter debugger if an error is signaled.
3781 Does not apply to errors handled by `condition-case' or those
3782 matched by `debug-ignored-errors'.
3783 If the value is a list, an error only means to enter the debugger
3784 if one of its condition symbols appears in the list.
3785 When you evaluate an expression interactively, this variable
3786 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3787 The command `toggle-debug-on-error' toggles this.
3788 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3789 Vdebug_on_error = Qnil;
3791 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3792 doc: /* List of errors for which the debugger should not be called.
3793 Each element may be a condition-name or a regexp that matches error messages.
3794 If any element applies to a given error, that error skips the debugger
3795 and just returns to top level.
3796 This overrides the variable `debug-on-error'.
3797 It does not apply to errors handled by `condition-case'. */);
3798 Vdebug_ignored_errors = Qnil;
3800 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3801 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3802 Does not apply if quit is handled by a `condition-case'. */);
3803 debug_on_quit = 0;
3805 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3806 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3808 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3809 doc: /* Non-nil means debugger may continue execution.
3810 This is nil when the debugger is called under circumstances where it
3811 might not be safe to continue. */);
3812 debugger_may_continue = 1;
3814 DEFVAR_LISP ("debugger", Vdebugger,
3815 doc: /* Function to call to invoke debugger.
3816 If due to frame exit, args are `exit' and the value being returned;
3817 this function's value will be returned instead of that.
3818 If due to error, args are `error' and a list of the args to `signal'.
3819 If due to `apply' or `funcall' entry, one arg, `lambda'.
3820 If due to `eval' entry, one arg, t. */);
3821 Vdebugger = Qnil;
3823 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3824 doc: /* If non-nil, this is a function for `signal' to call.
3825 It receives the same arguments that `signal' was given.
3826 The Edebug package uses this to regain control. */);
3827 Vsignal_hook_function = Qnil;
3829 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3830 doc: /* Non-nil means call the debugger regardless of condition handlers.
3831 Note that `debug-on-error', `debug-on-quit' and friends
3832 still determine whether to handle the particular condition. */);
3833 Vdebug_on_signal = Qnil;
3835 /* When lexical binding is being used,
3836 Vinternal_interpreter_environment is non-nil, and contains an alist
3837 of lexically-bound variable, or (t), indicating an empty
3838 environment. The lisp name of this variable would be
3839 `internal-interpreter-environment' if it weren't hidden.
3840 Every element of this list can be either a cons (VAR . VAL)
3841 specifying a lexical binding, or a single symbol VAR indicating
3842 that this variable should use dynamic scoping. */
3843 DEFSYM (Qinternal_interpreter_environment,
3844 "internal-interpreter-environment");
3845 DEFVAR_LISP ("internal-interpreter-environment",
3846 Vinternal_interpreter_environment,
3847 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3848 When lexical binding is not being used, this variable is nil.
3849 A value of `(t)' indicates an empty environment, otherwise it is an
3850 alist of active lexical bindings. */);
3851 Vinternal_interpreter_environment = Qnil;
3852 /* Don't export this variable to Elisp, so no one can mess with it
3853 (Just imagine if someone makes it buffer-local). */
3854 Funintern (Qinternal_interpreter_environment, Qnil);
3856 DEFSYM (Vrun_hooks, "run-hooks");
3858 staticpro (&Vautoload_queue);
3859 Vautoload_queue = Qnil;
3860 staticpro (&Vsignaling_function);
3861 Vsignaling_function = Qnil;
3863 inhibit_lisp_code = Qnil;
3865 defsubr (&Sor);
3866 defsubr (&Sand);
3867 defsubr (&Sif);
3868 defsubr (&Scond);
3869 defsubr (&Sprogn);
3870 defsubr (&Sprog1);
3871 defsubr (&Sprog2);
3872 defsubr (&Ssetq);
3873 defsubr (&Squote);
3874 defsubr (&Sfunction);
3875 defsubr (&Sdefault_toplevel_value);
3876 defsubr (&Sset_default_toplevel_value);
3877 defsubr (&Sdefvar);
3878 defsubr (&Sdefvaralias);
3879 defsubr (&Sdefconst);
3880 defsubr (&Smake_var_non_special);
3881 defsubr (&Slet);
3882 defsubr (&SletX);
3883 defsubr (&Swhile);
3884 defsubr (&Smacroexpand);
3885 defsubr (&Scatch);
3886 defsubr (&Sthrow);
3887 defsubr (&Sunwind_protect);
3888 defsubr (&Scondition_case);
3889 defsubr (&Ssignal);
3890 defsubr (&Scommandp);
3891 defsubr (&Sautoload);
3892 defsubr (&Sautoload_do_load);
3893 defsubr (&Seval);
3894 defsubr (&Sapply);
3895 defsubr (&Sfuncall);
3896 defsubr (&Srun_hooks);
3897 defsubr (&Srun_hook_with_args);
3898 defsubr (&Srun_hook_with_args_until_success);
3899 defsubr (&Srun_hook_with_args_until_failure);
3900 defsubr (&Srun_hook_wrapped);
3901 defsubr (&Sfetch_bytecode);
3902 defsubr (&Sbacktrace_debug);
3903 defsubr (&Sbacktrace);
3904 defsubr (&Sbacktrace_frame);
3905 defsubr (&Sbacktrace_eval);
3906 defsubr (&Sbacktrace__locals);
3907 defsubr (&Sspecial_variable_p);
3908 defsubr (&Sfunctionp);