Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / eval.c
blob8d0c08b2e3af307ea580be39b59ec8f581dedff4
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 /* Call the Lisp debugger, giving it argument ARG. */
278 Lisp_Object
279 call_debugger (Lisp_Object arg)
281 bool debug_while_redisplaying;
282 ptrdiff_t count = SPECPDL_INDEX ();
283 Lisp_Object val;
284 EMACS_INT old_max = max_specpdl_size;
286 /* Temporarily bump up the stack limits,
287 so the debugger won't run out of stack. */
289 max_specpdl_size += 1;
290 record_unwind_protect (restore_stack_limits,
291 Fcons (make_number (old_max),
292 make_number (max_lisp_eval_depth)));
293 max_specpdl_size = old_max;
295 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
296 max_lisp_eval_depth = lisp_eval_depth + 40;
298 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
299 max_specpdl_size = SPECPDL_INDEX () + 100;
301 #ifdef HAVE_WINDOW_SYSTEM
302 if (display_hourglass_p)
303 cancel_hourglass ();
304 #endif
306 debug_on_next_call = 0;
307 when_entered_debugger = num_nonmacro_input_events;
309 /* Resetting redisplaying_p to 0 makes sure that debug output is
310 displayed if the debugger is invoked during redisplay. */
311 debug_while_redisplaying = redisplaying_p;
312 redisplaying_p = 0;
313 specbind (intern ("debugger-may-continue"),
314 debug_while_redisplaying ? Qnil : Qt);
315 specbind (Qinhibit_redisplay, Qnil);
316 specbind (Qinhibit_debugger, Qt);
318 #if 0 /* Binding this prevents execution of Lisp code during
319 redisplay, which necessarily leads to display problems. */
320 specbind (Qinhibit_eval_during_redisplay, Qt);
321 #endif
323 val = apply1 (Vdebugger, arg);
325 /* Interrupting redisplay and resuming it later is not safe under
326 all circumstances. So, when the debugger returns, abort the
327 interrupted redisplay by going back to the top-level. */
328 if (debug_while_redisplaying)
329 Ftop_level ();
331 return unbind_to (count, val);
334 static void
335 do_debug_on_call (Lisp_Object code)
337 debug_on_next_call = 0;
338 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
339 call_debugger (list1 (code));
342 /* NOTE!!! Every function that can call EVAL must protect its args
343 and temporaries from garbage collection while it needs them.
344 The definition of `For' shows what you have to do. */
346 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
347 doc: /* Eval args until one of them yields non-nil, then return that value.
348 The remaining args are not evalled at all.
349 If all args return nil, return nil.
350 usage: (or CONDITIONS...) */)
351 (Lisp_Object args)
353 register Lisp_Object val = Qnil;
354 struct gcpro gcpro1;
356 GCPRO1 (args);
358 while (CONSP (args))
360 val = eval_sub (XCAR (args));
361 if (!NILP (val))
362 break;
363 args = XCDR (args);
366 UNGCPRO;
367 return val;
370 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
371 doc: /* Eval args until one of them yields nil, then return nil.
372 The remaining args are not evalled at all.
373 If no arg yields nil, return the last arg's value.
374 usage: (and CONDITIONS...) */)
375 (Lisp_Object args)
377 register Lisp_Object val = Qt;
378 struct gcpro gcpro1;
380 GCPRO1 (args);
382 while (CONSP (args))
384 val = eval_sub (XCAR (args));
385 if (NILP (val))
386 break;
387 args = XCDR (args);
390 UNGCPRO;
391 return val;
394 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
395 doc: /* If COND yields non-nil, do THEN, else do ELSE...
396 Returns the value of THEN or the value of the last of the ELSE's.
397 THEN must be one expression, but ELSE... can be zero or more expressions.
398 If COND yields nil, and there are no ELSE's, the value is nil.
399 usage: (if COND THEN ELSE...) */)
400 (Lisp_Object args)
402 Lisp_Object cond;
403 struct gcpro gcpro1;
405 GCPRO1 (args);
406 cond = eval_sub (XCAR (args));
407 UNGCPRO;
409 if (!NILP (cond))
410 return eval_sub (Fcar (XCDR (args)));
411 return Fprogn (XCDR (XCDR (args)));
414 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
415 doc: /* Try each clause until one succeeds.
416 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
417 and, if the value is non-nil, this clause succeeds:
418 then the expressions in BODY are evaluated and the last one's
419 value is the value of the cond-form.
420 If a clause has one element, as in (CONDITION), then the cond-form
421 returns CONDITION's value, if that is non-nil.
422 If no clause succeeds, cond returns nil.
423 usage: (cond CLAUSES...) */)
424 (Lisp_Object args)
426 Lisp_Object val = args;
427 struct gcpro gcpro1;
429 GCPRO1 (args);
430 while (CONSP (args))
432 Lisp_Object clause = XCAR (args);
433 val = eval_sub (Fcar (clause));
434 if (!NILP (val))
436 if (!NILP (XCDR (clause)))
437 val = Fprogn (XCDR (clause));
438 break;
440 args = XCDR (args);
442 UNGCPRO;
444 return val;
447 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
448 doc: /* Eval BODY forms sequentially and return value of last one.
449 usage: (progn BODY...) */)
450 (Lisp_Object body)
452 Lisp_Object val = Qnil;
453 struct gcpro gcpro1;
455 GCPRO1 (body);
457 while (CONSP (body))
459 val = eval_sub (XCAR (body));
460 body = XCDR (body);
463 UNGCPRO;
464 return val;
467 /* Evaluate BODY sequentially, discarding its value. Suitable for
468 record_unwind_protect. */
470 void
471 unwind_body (Lisp_Object body)
473 Fprogn (body);
476 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
477 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
478 The value of FIRST is saved during the evaluation of the remaining args,
479 whose values are discarded.
480 usage: (prog1 FIRST BODY...) */)
481 (Lisp_Object args)
483 Lisp_Object val;
484 Lisp_Object args_left;
485 struct gcpro gcpro1, gcpro2;
487 args_left = args;
488 val = args;
489 GCPRO2 (args, val);
491 val = eval_sub (XCAR (args_left));
492 while (CONSP (args_left = XCDR (args_left)))
493 eval_sub (XCAR (args_left));
495 UNGCPRO;
496 return val;
499 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
500 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
501 The value of FORM2 is saved during the evaluation of the
502 remaining args, whose values are discarded.
503 usage: (prog2 FORM1 FORM2 BODY...) */)
504 (Lisp_Object args)
506 struct gcpro gcpro1;
508 GCPRO1 (args);
509 eval_sub (XCAR (args));
510 UNGCPRO;
511 return Fprog1 (XCDR (args));
514 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
515 doc: /* Set each SYM to the value of its VAL.
516 The symbols SYM are variables; they are literal (not evaluated).
517 The values VAL are expressions; they are evaluated.
518 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
519 The second VAL is not computed until after the first SYM is set, and so on;
520 each VAL can use the new value of variables set earlier in the `setq'.
521 The return value of the `setq' form is the value of the last VAL.
522 usage: (setq [SYM VAL]...) */)
523 (Lisp_Object args)
525 Lisp_Object val, sym, lex_binding;
527 val = args;
528 if (CONSP (args))
530 Lisp_Object args_left = args;
531 struct gcpro gcpro1;
532 GCPRO1 (args);
536 val = eval_sub (Fcar (XCDR (args_left)));
537 sym = XCAR (args_left);
539 /* Like for eval_sub, we do not check declared_special here since
540 it's been done when let-binding. */
541 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
542 && SYMBOLP (sym)
543 && !NILP (lex_binding
544 = Fassq (sym, Vinternal_interpreter_environment)))
545 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
546 else
547 Fset (sym, val); /* SYM is dynamically bound. */
549 args_left = Fcdr (XCDR (args_left));
551 while (CONSP (args_left));
553 UNGCPRO;
556 return val;
559 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
560 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
561 Warning: `quote' does not construct its return value, but just returns
562 the value that was pre-constructed by the Lisp reader (see info node
563 `(elisp)Printed Representation').
564 This means that '(a . b) is not identical to (cons 'a 'b): the former
565 does not cons. Quoting should be reserved for constants that will
566 never be modified by side-effects, unless you like self-modifying code.
567 See the common pitfall in info node `(elisp)Rearrangement' for an example
568 of unexpected results when a quoted object is modified.
569 usage: (quote ARG) */)
570 (Lisp_Object args)
572 if (CONSP (XCDR (args)))
573 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
574 return XCAR (args);
577 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
578 doc: /* Like `quote', but preferred for objects which are functions.
579 In byte compilation, `function' causes its argument to be compiled.
580 `quote' cannot do that.
581 usage: (function ARG) */)
582 (Lisp_Object args)
584 Lisp_Object quoted = XCAR (args);
586 if (CONSP (XCDR (args)))
587 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
589 if (!NILP (Vinternal_interpreter_environment)
590 && CONSP (quoted)
591 && EQ (XCAR (quoted), Qlambda))
592 /* This is a lambda expression within a lexical environment;
593 return an interpreted closure instead of a simple lambda. */
594 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
595 XCDR (quoted)));
596 else
597 /* Simply quote the argument. */
598 return quoted;
602 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
603 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
604 Aliased variables always have the same value; setting one sets the other.
605 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
606 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
607 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
608 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
609 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
610 The return value is BASE-VARIABLE. */)
611 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
613 struct Lisp_Symbol *sym;
615 CHECK_SYMBOL (new_alias);
616 CHECK_SYMBOL (base_variable);
618 sym = XSYMBOL (new_alias);
620 if (sym->constant)
621 /* Not sure why, but why not? */
622 error ("Cannot make a constant an alias");
624 switch (sym->redirect)
626 case SYMBOL_FORWARDED:
627 error ("Cannot make an internal variable an alias");
628 case SYMBOL_LOCALIZED:
629 error ("Don't know how to make a localized variable an alias");
632 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
633 If n_a is bound, but b_v is not, set the value of b_v to n_a,
634 so that old-code that affects n_a before the aliasing is setup
635 still works. */
636 if (NILP (Fboundp (base_variable)))
637 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
640 union specbinding *p;
642 for (p = specpdl_ptr; p > specpdl; )
643 if ((--p)->kind >= SPECPDL_LET
644 && (EQ (new_alias, specpdl_symbol (p))))
645 error ("Don't know how to make a let-bound variable an alias");
648 sym->declared_special = 1;
649 XSYMBOL (base_variable)->declared_special = 1;
650 sym->redirect = SYMBOL_VARALIAS;
651 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
652 sym->constant = SYMBOL_CONSTANT_P (base_variable);
653 LOADHIST_ATTACH (new_alias);
654 /* Even if docstring is nil: remove old docstring. */
655 Fput (new_alias, Qvariable_documentation, docstring);
657 return base_variable;
660 static union specbinding *
661 default_toplevel_binding (Lisp_Object symbol)
663 union specbinding *binding = NULL;
664 union specbinding *pdl = specpdl_ptr;
665 while (pdl > specpdl)
667 switch ((--pdl)->kind)
669 case SPECPDL_LET_DEFAULT:
670 case SPECPDL_LET:
671 if (EQ (specpdl_symbol (pdl), symbol))
672 binding = pdl;
673 break;
676 return binding;
679 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
680 doc: /* Return SYMBOL's toplevel default value.
681 "Toplevel" means outside of any let binding. */)
682 (Lisp_Object symbol)
684 union specbinding *binding = default_toplevel_binding (symbol);
685 Lisp_Object value
686 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
687 if (!EQ (value, Qunbound))
688 return value;
689 xsignal1 (Qvoid_variable, symbol);
692 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
693 Sset_default_toplevel_value, 2, 2, 0,
694 doc: /* Set SYMBOL's toplevel default value to VALUE.
695 "Toplevel" means outside of any let binding. */)
696 (Lisp_Object symbol, Lisp_Object value)
698 union specbinding *binding = default_toplevel_binding (symbol);
699 if (binding)
700 set_specpdl_old_value (binding, value);
701 else
702 Fset_default (symbol, value);
703 return Qnil;
706 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
707 doc: /* Define SYMBOL as a variable, and return SYMBOL.
708 You are not required to define a variable in order to use it, but
709 defining it lets you supply an initial value and documentation, which
710 can be referred to by the Emacs help facilities and other programming
711 tools. The `defvar' form also declares the variable as \"special\",
712 so that it is always dynamically bound even if `lexical-binding' is t.
714 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
715 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
716 default value is what is set; buffer-local values are not affected.
717 If INITVALUE is missing, SYMBOL's value is not set.
719 If SYMBOL has a local binding, then this form affects the local
720 binding. This is usually not what you want. Thus, if you need to
721 load a file defining variables, with this form or with `defconst' or
722 `defcustom', you should always load that file _outside_ any bindings
723 for these variables. \(`defconst' and `defcustom' behave similarly in
724 this respect.)
726 The optional argument DOCSTRING is a documentation string for the
727 variable.
729 To define a user option, use `defcustom' instead of `defvar'.
730 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
731 (Lisp_Object args)
733 Lisp_Object sym, tem, tail;
735 sym = XCAR (args);
736 tail = XCDR (args);
738 if (CONSP (tail))
740 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
741 error ("Too many arguments");
743 tem = Fdefault_boundp (sym);
745 /* Do it before evaluating the initial value, for self-references. */
746 XSYMBOL (sym)->declared_special = 1;
748 if (NILP (tem))
749 Fset_default (sym, eval_sub (XCAR (tail)));
750 else
751 { /* Check if there is really a global binding rather than just a let
752 binding that shadows the global unboundness of the var. */
753 union specbinding *binding = default_toplevel_binding (sym);
754 if (binding && EQ (specpdl_old_value (binding), Qunbound))
756 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
759 tail = XCDR (tail);
760 tem = Fcar (tail);
761 if (!NILP (tem))
763 if (!NILP (Vpurify_flag))
764 tem = Fpurecopy (tem);
765 Fput (sym, Qvariable_documentation, tem);
767 LOADHIST_ATTACH (sym);
769 else if (!NILP (Vinternal_interpreter_environment)
770 && !XSYMBOL (sym)->declared_special)
771 /* A simple (defvar foo) with lexical scoping does "nothing" except
772 declare that var to be dynamically scoped *locally* (i.e. within
773 the current file or let-block). */
774 Vinternal_interpreter_environment
775 = Fcons (sym, Vinternal_interpreter_environment);
776 else
778 /* Simple (defvar <var>) should not count as a definition at all.
779 It could get in the way of other definitions, and unloading this
780 package could try to make the variable unbound. */
783 return sym;
786 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
787 doc: /* Define SYMBOL as a constant variable.
788 This declares that neither programs nor users should ever change the
789 value. This constancy is not actually enforced by Emacs Lisp, but
790 SYMBOL is marked as a special variable so that it is never lexically
791 bound.
793 The `defconst' form always sets the value of SYMBOL to the result of
794 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
795 what is set; buffer-local values are not affected. If SYMBOL has a
796 local binding, then this form sets the local binding's value.
797 However, you should normally not make local bindings for variables
798 defined with this form.
800 The optional DOCSTRING specifies the variable's documentation string.
801 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
802 (Lisp_Object args)
804 Lisp_Object sym, tem;
806 sym = XCAR (args);
807 if (CONSP (Fcdr (XCDR (XCDR (args)))))
808 error ("Too many arguments");
810 tem = eval_sub (Fcar (XCDR (args)));
811 if (!NILP (Vpurify_flag))
812 tem = Fpurecopy (tem);
813 Fset_default (sym, tem);
814 XSYMBOL (sym)->declared_special = 1;
815 tem = Fcar (XCDR (XCDR (args)));
816 if (!NILP (tem))
818 if (!NILP (Vpurify_flag))
819 tem = Fpurecopy (tem);
820 Fput (sym, Qvariable_documentation, tem);
822 Fput (sym, Qrisky_local_variable, Qt);
823 LOADHIST_ATTACH (sym);
824 return sym;
827 /* Make SYMBOL lexically scoped. */
828 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
829 Smake_var_non_special, 1, 1, 0,
830 doc: /* Internal function. */)
831 (Lisp_Object symbol)
833 CHECK_SYMBOL (symbol);
834 XSYMBOL (symbol)->declared_special = 0;
835 return Qnil;
839 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
840 doc: /* Bind variables according to VARLIST then eval BODY.
841 The value of the last form in BODY is returned.
842 Each element of VARLIST is a symbol (which is bound to nil)
843 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
844 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
845 usage: (let* VARLIST BODY...) */)
846 (Lisp_Object args)
848 Lisp_Object varlist, var, val, elt, lexenv;
849 ptrdiff_t count = SPECPDL_INDEX ();
850 struct gcpro gcpro1, gcpro2, gcpro3;
852 GCPRO3 (args, elt, varlist);
854 lexenv = Vinternal_interpreter_environment;
856 varlist = XCAR (args);
857 while (CONSP (varlist))
859 QUIT;
861 elt = XCAR (varlist);
862 if (SYMBOLP (elt))
864 var = elt;
865 val = Qnil;
867 else if (! NILP (Fcdr (Fcdr (elt))))
868 signal_error ("`let' bindings can have only one value-form", elt);
869 else
871 var = Fcar (elt);
872 val = eval_sub (Fcar (Fcdr (elt)));
875 if (!NILP (lexenv) && SYMBOLP (var)
876 && !XSYMBOL (var)->declared_special
877 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
878 /* Lexically bind VAR by adding it to the interpreter's binding
879 alist. */
881 Lisp_Object newenv
882 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
883 if (EQ (Vinternal_interpreter_environment, lexenv))
884 /* Save the old lexical environment on the specpdl stack,
885 but only for the first lexical binding, since we'll never
886 need to revert to one of the intermediate ones. */
887 specbind (Qinternal_interpreter_environment, newenv);
888 else
889 Vinternal_interpreter_environment = newenv;
891 else
892 specbind (var, val);
894 varlist = XCDR (varlist);
896 UNGCPRO;
897 val = Fprogn (XCDR (args));
898 return unbind_to (count, val);
901 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
902 doc: /* Bind variables according to VARLIST then eval BODY.
903 The value of the last form in BODY is returned.
904 Each element of VARLIST is a symbol (which is bound to nil)
905 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
906 All the VALUEFORMs are evalled before any symbols are bound.
907 usage: (let VARLIST BODY...) */)
908 (Lisp_Object args)
910 Lisp_Object *temps, tem, lexenv;
911 register Lisp_Object elt, varlist;
912 ptrdiff_t count = SPECPDL_INDEX ();
913 ptrdiff_t argnum;
914 struct gcpro gcpro1, gcpro2;
915 USE_SAFE_ALLOCA;
917 varlist = XCAR (args);
919 /* Make space to hold the values to give the bound variables. */
920 elt = Flength (varlist);
921 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
923 /* Compute the values and store them in `temps'. */
925 GCPRO2 (args, *temps);
926 gcpro2.nvars = 0;
928 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
930 QUIT;
931 elt = XCAR (varlist);
932 if (SYMBOLP (elt))
933 temps [argnum++] = Qnil;
934 else if (! NILP (Fcdr (Fcdr (elt))))
935 signal_error ("`let' bindings can have only one value-form", elt);
936 else
937 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
938 gcpro2.nvars = argnum;
940 UNGCPRO;
942 lexenv = Vinternal_interpreter_environment;
944 varlist = XCAR (args);
945 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
947 Lisp_Object var;
949 elt = XCAR (varlist);
950 var = SYMBOLP (elt) ? elt : Fcar (elt);
951 tem = temps[argnum++];
953 if (!NILP (lexenv) && SYMBOLP (var)
954 && !XSYMBOL (var)->declared_special
955 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
956 /* Lexically bind VAR by adding it to the lexenv alist. */
957 lexenv = Fcons (Fcons (var, tem), lexenv);
958 else
959 /* Dynamically bind VAR. */
960 specbind (var, tem);
963 if (!EQ (lexenv, Vinternal_interpreter_environment))
964 /* Instantiate a new lexical environment. */
965 specbind (Qinternal_interpreter_environment, lexenv);
967 elt = Fprogn (XCDR (args));
968 SAFE_FREE ();
969 return unbind_to (count, elt);
972 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
973 doc: /* If TEST yields non-nil, eval BODY... and repeat.
974 The order of execution is thus TEST, BODY, TEST, BODY and so on
975 until TEST returns nil.
976 usage: (while TEST BODY...) */)
977 (Lisp_Object args)
979 Lisp_Object test, body;
980 struct gcpro gcpro1, gcpro2;
982 GCPRO2 (test, body);
984 test = XCAR (args);
985 body = XCDR (args);
986 while (!NILP (eval_sub (test)))
988 QUIT;
989 Fprogn (body);
992 UNGCPRO;
993 return Qnil;
996 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
997 doc: /* Return result of expanding macros at top level of FORM.
998 If FORM is not a macro call, it is returned unchanged.
999 Otherwise, the macro is expanded and the expansion is considered
1000 in place of FORM. When a non-macro-call results, it is returned.
1002 The second optional arg ENVIRONMENT specifies an environment of macro
1003 definitions to shadow the loaded ones for use in file byte-compilation. */)
1004 (Lisp_Object form, Lisp_Object environment)
1006 /* With cleanups from Hallvard Furuseth. */
1007 register Lisp_Object expander, sym, def, tem;
1009 while (1)
1011 /* Come back here each time we expand a macro call,
1012 in case it expands into another macro call. */
1013 if (!CONSP (form))
1014 break;
1015 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1016 def = sym = XCAR (form);
1017 tem = Qnil;
1018 /* Trace symbols aliases to other symbols
1019 until we get a symbol that is not an alias. */
1020 while (SYMBOLP (def))
1022 QUIT;
1023 sym = def;
1024 tem = Fassq (sym, environment);
1025 if (NILP (tem))
1027 def = XSYMBOL (sym)->function;
1028 if (!NILP (def))
1029 continue;
1031 break;
1033 /* Right now TEM is the result from SYM in ENVIRONMENT,
1034 and if TEM is nil then DEF is SYM's function definition. */
1035 if (NILP (tem))
1037 /* SYM is not mentioned in ENVIRONMENT.
1038 Look at its function definition. */
1039 struct gcpro gcpro1;
1040 GCPRO1 (form);
1041 def = Fautoload_do_load (def, sym, Qmacro);
1042 UNGCPRO;
1043 if (!CONSP (def))
1044 /* Not defined or definition not suitable. */
1045 break;
1046 if (!EQ (XCAR (def), Qmacro))
1047 break;
1048 else expander = XCDR (def);
1050 else
1052 expander = XCDR (tem);
1053 if (NILP (expander))
1054 break;
1057 Lisp_Object newform = apply1 (expander, XCDR (form));
1058 if (EQ (form, newform))
1059 break;
1060 else
1061 form = newform;
1064 return form;
1067 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1068 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1069 TAG is evalled to get the tag to use; it must not be nil.
1071 Then the BODY is executed.
1072 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1073 If no throw happens, `catch' returns the value of the last BODY form.
1074 If a throw happens, it specifies the value to return from `catch'.
1075 usage: (catch TAG BODY...) */)
1076 (Lisp_Object args)
1078 register Lisp_Object tag;
1079 struct gcpro gcpro1;
1081 GCPRO1 (args);
1082 tag = eval_sub (XCAR (args));
1083 UNGCPRO;
1084 return internal_catch (tag, Fprogn, XCDR (args));
1087 /* Assert that E is true, as a comment only. Use this instead of
1088 eassert (E) when E contains variables that might be clobbered by a
1089 longjmp. */
1091 #define clobbered_eassert(E) ((void) 0)
1093 /* Set up a catch, then call C function FUNC on argument ARG.
1094 FUNC should return a Lisp_Object.
1095 This is how catches are done from within C code. */
1097 Lisp_Object
1098 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1100 /* This structure is made part of the chain `catchlist'. */
1101 struct handler *c;
1103 /* Fill in the components of c, and put it on the list. */
1104 PUSH_HANDLER (c, tag, CATCHER);
1106 /* Call FUNC. */
1107 if (! sys_setjmp (c->jmp))
1109 Lisp_Object val = (*func) (arg);
1110 clobbered_eassert (handlerlist == c);
1111 handlerlist = handlerlist->next;
1112 return val;
1114 else
1115 { /* Throw works by a longjmp that comes right here. */
1116 Lisp_Object val = handlerlist->val;
1117 clobbered_eassert (handlerlist == c);
1118 handlerlist = handlerlist->next;
1119 return val;
1123 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1124 jump to that CATCH, returning VALUE as the value of that catch.
1126 This is the guts of Fthrow and Fsignal; they differ only in the way
1127 they choose the catch tag to throw to. A catch tag for a
1128 condition-case form has a TAG of Qnil.
1130 Before each catch is discarded, unbind all special bindings and
1131 execute all unwind-protect clauses made above that catch. Unwind
1132 the handler stack as we go, so that the proper handlers are in
1133 effect for each unwind-protect clause we run. At the end, restore
1134 some static info saved in CATCH, and longjmp to the location
1135 specified there.
1137 This is used for correct unwinding in Fthrow and Fsignal. */
1139 static _Noreturn void
1140 unwind_to_catch (struct handler *catch, Lisp_Object value)
1142 bool last_time;
1144 eassert (catch->next);
1146 /* Save the value in the tag. */
1147 catch->val = value;
1149 /* Restore certain special C variables. */
1150 set_poll_suppress_count (catch->poll_suppress_count);
1151 unblock_input_to (catch->interrupt_input_blocked);
1152 immediate_quit = 0;
1156 /* Unwind the specpdl stack, and then restore the proper set of
1157 handlers. */
1158 unbind_to (handlerlist->pdlcount, Qnil);
1159 last_time = handlerlist == catch;
1160 if (! last_time)
1161 handlerlist = handlerlist->next;
1163 while (! last_time);
1165 eassert (handlerlist == catch);
1167 byte_stack_list = catch->byte_stack;
1168 gcprolist = catch->gcpro;
1169 #ifdef DEBUG_GCPRO
1170 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1171 #endif
1172 lisp_eval_depth = catch->lisp_eval_depth;
1174 sys_longjmp (catch->jmp, 1);
1177 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1178 doc: /* Throw to the catch for TAG and return VALUE from it.
1179 Both TAG and VALUE are evalled. */)
1180 (register Lisp_Object tag, Lisp_Object value)
1182 struct handler *c;
1184 if (!NILP (tag))
1185 for (c = handlerlist; c; c = c->next)
1187 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1188 unwind_to_catch (c, value);
1190 xsignal2 (Qno_catch, tag, value);
1194 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1195 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1196 If BODYFORM completes normally, its value is returned
1197 after executing the UNWINDFORMS.
1198 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1199 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1200 (Lisp_Object args)
1202 Lisp_Object val;
1203 ptrdiff_t count = SPECPDL_INDEX ();
1205 record_unwind_protect (unwind_body, XCDR (args));
1206 val = eval_sub (XCAR (args));
1207 return unbind_to (count, val);
1210 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1211 doc: /* Regain control when an error is signaled.
1212 Executes BODYFORM and returns its value if no error happens.
1213 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1214 where the BODY is made of Lisp expressions.
1216 A handler is applicable to an error
1217 if CONDITION-NAME is one of the error's condition names.
1218 If an error happens, the first applicable handler is run.
1220 The car of a handler may be a list of condition names instead of a
1221 single condition name; then it handles all of them. If the special
1222 condition name `debug' is present in this list, it allows another
1223 condition in the list to run the debugger if `debug-on-error' and the
1224 other usual mechanisms says it should (otherwise, `condition-case'
1225 suppresses the debugger).
1227 When a handler handles an error, control returns to the `condition-case'
1228 and it executes the handler's BODY...
1229 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1230 \(If VAR is nil, the handler can't access that information.)
1231 Then the value of the last BODY form is returned from the `condition-case'
1232 expression.
1234 See also the function `signal' for more info.
1235 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1236 (Lisp_Object args)
1238 Lisp_Object var = XCAR (args);
1239 Lisp_Object bodyform = XCAR (XCDR (args));
1240 Lisp_Object handlers = XCDR (XCDR (args));
1242 return internal_lisp_condition_case (var, bodyform, handlers);
1245 /* Like Fcondition_case, but the args are separate
1246 rather than passed in a list. Used by Fbyte_code. */
1248 Lisp_Object
1249 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1250 Lisp_Object handlers)
1252 Lisp_Object val;
1253 struct handler *c;
1254 struct handler *oldhandlerlist = handlerlist;
1255 int clausenb = 0;
1257 CHECK_SYMBOL (var);
1259 for (val = handlers; CONSP (val); val = XCDR (val))
1261 Lisp_Object tem = XCAR (val);
1262 clausenb++;
1263 if (! (NILP (tem)
1264 || (CONSP (tem)
1265 && (SYMBOLP (XCAR (tem))
1266 || CONSP (XCAR (tem))))))
1267 error ("Invalid condition handler: %s",
1268 SDATA (Fprin1_to_string (tem, Qt)));
1271 { /* The first clause is the one that should be checked first, so it should
1272 be added to handlerlist last. So we build in `clauses' a table that
1273 contains `handlers' but in reverse order. */
1274 Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *));
1275 Lisp_Object *volatile clauses_volatile = clauses;
1276 int i = clausenb;
1277 for (val = handlers; CONSP (val); val = XCDR (val))
1278 clauses[--i] = XCAR (val);
1279 for (i = 0; i < clausenb; i++)
1281 Lisp_Object clause = clauses[i];
1282 Lisp_Object condition = XCAR (clause);
1283 if (!CONSP (condition))
1284 condition = Fcons (condition, Qnil);
1285 PUSH_HANDLER (c, condition, CONDITION_CASE);
1286 if (sys_setjmp (c->jmp))
1288 ptrdiff_t count = SPECPDL_INDEX ();
1289 Lisp_Object val = handlerlist->val;
1290 Lisp_Object *chosen_clause = clauses_volatile;
1291 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1292 chosen_clause++;
1293 handlerlist = oldhandlerlist;
1294 if (!NILP (var))
1296 if (!NILP (Vinternal_interpreter_environment))
1297 specbind (Qinternal_interpreter_environment,
1298 Fcons (Fcons (var, val),
1299 Vinternal_interpreter_environment));
1300 else
1301 specbind (var, val);
1303 val = Fprogn (XCDR (*chosen_clause));
1304 /* Note that this just undoes the binding of var; whoever
1305 longjumped to us unwound the stack to c.pdlcount before
1306 throwing. */
1307 if (!NILP (var))
1308 unbind_to (count, Qnil);
1309 return val;
1314 val = eval_sub (bodyform);
1315 handlerlist = oldhandlerlist;
1316 return val;
1319 /* Call the function BFUN with no arguments, catching errors within it
1320 according to HANDLERS. If there is an error, call HFUN with
1321 one argument which is the data that describes the error:
1322 (SIGNALNAME . DATA)
1324 HANDLERS can be a list of conditions to catch.
1325 If HANDLERS is Qt, catch all errors.
1326 If HANDLERS is Qerror, catch all errors
1327 but allow the debugger to run if that is enabled. */
1329 Lisp_Object
1330 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1331 Lisp_Object (*hfun) (Lisp_Object))
1333 Lisp_Object val;
1334 struct handler *c;
1336 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1337 if (sys_setjmp (c->jmp))
1339 Lisp_Object val = handlerlist->val;
1340 clobbered_eassert (handlerlist == c);
1341 handlerlist = handlerlist->next;
1342 return (*hfun) (val);
1345 val = (*bfun) ();
1346 clobbered_eassert (handlerlist == c);
1347 handlerlist = handlerlist->next;
1348 return val;
1351 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1353 Lisp_Object
1354 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1355 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1357 Lisp_Object val;
1358 struct handler *c;
1360 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1361 if (sys_setjmp (c->jmp))
1363 Lisp_Object val = handlerlist->val;
1364 clobbered_eassert (handlerlist == c);
1365 handlerlist = handlerlist->next;
1366 return (*hfun) (val);
1369 val = (*bfun) (arg);
1370 clobbered_eassert (handlerlist == c);
1371 handlerlist = handlerlist->next;
1372 return val;
1375 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1376 its arguments. */
1378 Lisp_Object
1379 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1380 Lisp_Object arg1,
1381 Lisp_Object arg2,
1382 Lisp_Object handlers,
1383 Lisp_Object (*hfun) (Lisp_Object))
1385 Lisp_Object val;
1386 struct handler *c;
1388 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1389 if (sys_setjmp (c->jmp))
1391 Lisp_Object val = handlerlist->val;
1392 clobbered_eassert (handlerlist == c);
1393 handlerlist = handlerlist->next;
1394 return (*hfun) (val);
1397 val = (*bfun) (arg1, arg2);
1398 clobbered_eassert (handlerlist == c);
1399 handlerlist = handlerlist->next;
1400 return val;
1403 /* Like internal_condition_case but call BFUN with NARGS as first,
1404 and ARGS as second argument. */
1406 Lisp_Object
1407 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1408 ptrdiff_t nargs,
1409 Lisp_Object *args,
1410 Lisp_Object handlers,
1411 Lisp_Object (*hfun) (Lisp_Object err,
1412 ptrdiff_t nargs,
1413 Lisp_Object *args))
1415 Lisp_Object val;
1416 struct handler *c;
1418 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1419 if (sys_setjmp (c->jmp))
1421 Lisp_Object val = handlerlist->val;
1422 clobbered_eassert (handlerlist == c);
1423 handlerlist = handlerlist->next;
1424 return (*hfun) (val, nargs, args);
1427 val = (*bfun) (nargs, args);
1428 clobbered_eassert (handlerlist == c);
1429 handlerlist = handlerlist->next;
1430 return val;
1434 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1435 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1436 Lisp_Object data);
1438 void
1439 process_quit_flag (void)
1441 Lisp_Object flag = Vquit_flag;
1442 Vquit_flag = Qnil;
1443 if (EQ (flag, Qkill_emacs))
1444 Fkill_emacs (Qnil);
1445 if (EQ (Vthrow_on_input, flag))
1446 Fthrow (Vthrow_on_input, Qt);
1447 Fsignal (Qquit, Qnil);
1450 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1451 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1452 This function does not return.
1454 An error symbol is a symbol with an `error-conditions' property
1455 that is a list of condition names.
1456 A handler for any of those names will get to handle this signal.
1457 The symbol `error' should normally be one of them.
1459 DATA should be a list. Its elements are printed as part of the error message.
1460 See Info anchor `(elisp)Definition of signal' for some details on how this
1461 error message is constructed.
1462 If the signal is handled, DATA is made available to the handler.
1463 See also the function `condition-case'. */)
1464 (Lisp_Object error_symbol, Lisp_Object data)
1466 /* When memory is full, ERROR-SYMBOL is nil,
1467 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1468 That is a special case--don't do this in other situations. */
1469 Lisp_Object conditions;
1470 Lisp_Object string;
1471 Lisp_Object real_error_symbol
1472 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1473 register Lisp_Object clause = Qnil;
1474 struct handler *h;
1476 immediate_quit = 0;
1477 abort_on_gc = 0;
1478 if (gc_in_progress || waiting_for_input)
1479 emacs_abort ();
1481 #if 0 /* rms: I don't know why this was here,
1482 but it is surely wrong for an error that is handled. */
1483 #ifdef HAVE_WINDOW_SYSTEM
1484 if (display_hourglass_p)
1485 cancel_hourglass ();
1486 #endif
1487 #endif
1489 /* This hook is used by edebug. */
1490 if (! NILP (Vsignal_hook_function)
1491 && ! NILP (error_symbol))
1493 /* Edebug takes care of restoring these variables when it exits. */
1494 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1495 max_lisp_eval_depth = lisp_eval_depth + 20;
1497 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1498 max_specpdl_size = SPECPDL_INDEX () + 40;
1500 call2 (Vsignal_hook_function, error_symbol, data);
1503 conditions = Fget (real_error_symbol, Qerror_conditions);
1505 /* Remember from where signal was called. Skip over the frame for
1506 `signal' itself. If a frame for `error' follows, skip that,
1507 too. Don't do this when ERROR_SYMBOL is nil, because that
1508 is a memory-full error. */
1509 Vsignaling_function = Qnil;
1510 if (!NILP (error_symbol))
1512 union specbinding *pdl = backtrace_next (backtrace_top ());
1513 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1514 pdl = backtrace_next (pdl);
1515 if (backtrace_p (pdl))
1516 Vsignaling_function = backtrace_function (pdl);
1519 for (h = handlerlist; h; h = h->next)
1521 if (h->type != CONDITION_CASE)
1522 continue;
1523 clause = find_handler_clause (h->tag_or_ch, conditions);
1524 if (!NILP (clause))
1525 break;
1528 if (/* Don't run the debugger for a memory-full error.
1529 (There is no room in memory to do that!) */
1530 !NILP (error_symbol)
1531 && (!NILP (Vdebug_on_signal)
1532 /* If no handler is present now, try to run the debugger. */
1533 || NILP (clause)
1534 /* A `debug' symbol in the handler list disables the normal
1535 suppression of the debugger. */
1536 || (CONSP (clause) && CONSP (XCAR (clause))
1537 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1538 /* Special handler that means "print a message and run debugger
1539 if requested". */
1540 || EQ (h->tag_or_ch, Qerror)))
1542 bool debugger_called
1543 = maybe_call_debugger (conditions, error_symbol, data);
1544 /* We can't return values to code which signaled an error, but we
1545 can continue code which has signaled a quit. */
1546 if (debugger_called && EQ (real_error_symbol, Qquit))
1547 return Qnil;
1550 if (!NILP (clause))
1552 Lisp_Object unwind_data
1553 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1555 unwind_to_catch (h, unwind_data);
1557 else
1559 if (handlerlist != &handlerlist_sentinel)
1560 /* FIXME: This will come right back here if there's no `top-level'
1561 catcher. A better solution would be to abort here, and instead
1562 add a catch-all condition handler so we never come here. */
1563 Fthrow (Qtop_level, Qt);
1566 if (! NILP (error_symbol))
1567 data = Fcons (error_symbol, data);
1569 string = Ferror_message_string (data);
1570 fatal ("%s", SDATA (string));
1573 /* Internal version of Fsignal that never returns.
1574 Used for anything but Qquit (which can return from Fsignal). */
1576 void
1577 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1579 Fsignal (error_symbol, data);
1580 emacs_abort ();
1583 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1585 void
1586 xsignal0 (Lisp_Object error_symbol)
1588 xsignal (error_symbol, Qnil);
1591 void
1592 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1594 xsignal (error_symbol, list1 (arg));
1597 void
1598 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1600 xsignal (error_symbol, list2 (arg1, arg2));
1603 void
1604 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1606 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1609 /* Signal `error' with message S, and additional arg ARG.
1610 If ARG is not a genuine list, make it a one-element list. */
1612 void
1613 signal_error (const char *s, Lisp_Object arg)
1615 Lisp_Object tortoise, hare;
1617 hare = tortoise = arg;
1618 while (CONSP (hare))
1620 hare = XCDR (hare);
1621 if (!CONSP (hare))
1622 break;
1624 hare = XCDR (hare);
1625 tortoise = XCDR (tortoise);
1627 if (EQ (hare, tortoise))
1628 break;
1631 if (!NILP (hare))
1632 arg = list1 (arg);
1634 xsignal (Qerror, Fcons (build_string (s), arg));
1638 /* Return true if LIST is a non-nil atom or
1639 a list containing one of CONDITIONS. */
1641 static bool
1642 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1644 if (NILP (list))
1645 return 0;
1646 if (! CONSP (list))
1647 return 1;
1649 while (CONSP (conditions))
1651 Lisp_Object this, tail;
1652 this = XCAR (conditions);
1653 for (tail = list; CONSP (tail); tail = XCDR (tail))
1654 if (EQ (XCAR (tail), this))
1655 return 1;
1656 conditions = XCDR (conditions);
1658 return 0;
1661 /* Return true if an error with condition-symbols CONDITIONS,
1662 and described by SIGNAL-DATA, should skip the debugger
1663 according to debugger-ignored-errors. */
1665 static bool
1666 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1668 Lisp_Object tail;
1669 bool first_string = 1;
1670 Lisp_Object error_message;
1672 error_message = Qnil;
1673 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1675 if (STRINGP (XCAR (tail)))
1677 if (first_string)
1679 error_message = Ferror_message_string (data);
1680 first_string = 0;
1683 if (fast_string_match (XCAR (tail), error_message) >= 0)
1684 return 1;
1686 else
1688 Lisp_Object contail;
1690 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1691 if (EQ (XCAR (tail), XCAR (contail)))
1692 return 1;
1696 return 0;
1699 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1700 SIG and DATA describe the signal. There are two ways to pass them:
1701 = SIG is the error symbol, and DATA is the rest of the data.
1702 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1703 This is for memory-full errors only. */
1704 static bool
1705 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1707 Lisp_Object combined_data;
1709 combined_data = Fcons (sig, data);
1711 if (
1712 /* Don't try to run the debugger with interrupts blocked.
1713 The editing loop would return anyway. */
1714 ! input_blocked_p ()
1715 && NILP (Vinhibit_debugger)
1716 /* Does user want to enter debugger for this kind of error? */
1717 && (EQ (sig, Qquit)
1718 ? debug_on_quit
1719 : wants_debugger (Vdebug_on_error, conditions))
1720 && ! skip_debugger (conditions, combined_data)
1721 /* RMS: What's this for? */
1722 && when_entered_debugger < num_nonmacro_input_events)
1724 call_debugger (list2 (Qerror, combined_data));
1725 return 1;
1728 return 0;
1731 static Lisp_Object
1732 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1734 register Lisp_Object h;
1736 /* t is used by handlers for all conditions, set up by C code. */
1737 if (EQ (handlers, Qt))
1738 return Qt;
1740 /* error is used similarly, but means print an error message
1741 and run the debugger if that is enabled. */
1742 if (EQ (handlers, Qerror))
1743 return Qt;
1745 for (h = handlers; CONSP (h); h = XCDR (h))
1747 Lisp_Object handler = XCAR (h);
1748 if (!NILP (Fmemq (handler, conditions)))
1749 return handlers;
1752 return Qnil;
1756 /* Dump an error message; called like vprintf. */
1757 void
1758 verror (const char *m, va_list ap)
1760 char buf[4000];
1761 ptrdiff_t size = sizeof buf;
1762 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1763 char *buffer = buf;
1764 ptrdiff_t used;
1765 Lisp_Object string;
1767 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1768 string = make_string (buffer, used);
1769 if (buffer != buf)
1770 xfree (buffer);
1772 xsignal1 (Qerror, string);
1776 /* Dump an error message; called like printf. */
1778 /* VARARGS 1 */
1779 void
1780 error (const char *m, ...)
1782 va_list ap;
1783 va_start (ap, m);
1784 verror (m, ap);
1787 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1788 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1789 This means it contains a description for how to read arguments to give it.
1790 The value is nil for an invalid function or a symbol with no function
1791 definition.
1793 Interactively callable functions include strings and vectors (treated
1794 as keyboard macros), lambda-expressions that contain a top-level call
1795 to `interactive', autoload definitions made by `autoload' with non-nil
1796 fourth argument, and some of the built-in functions of Lisp.
1798 Also, a symbol satisfies `commandp' if its function definition does so.
1800 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1801 then strings and vectors are not accepted. */)
1802 (Lisp_Object function, Lisp_Object for_call_interactively)
1804 register Lisp_Object fun;
1805 register Lisp_Object funcar;
1806 Lisp_Object if_prop = Qnil;
1808 fun = function;
1810 fun = indirect_function (fun); /* Check cycles. */
1811 if (NILP (fun))
1812 return Qnil;
1814 /* Check an `interactive-form' property if present, analogous to the
1815 function-documentation property. */
1816 fun = function;
1817 while (SYMBOLP (fun))
1819 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1820 if (!NILP (tmp))
1821 if_prop = Qt;
1822 fun = Fsymbol_function (fun);
1825 /* Emacs primitives are interactive if their DEFUN specifies an
1826 interactive spec. */
1827 if (SUBRP (fun))
1828 return XSUBR (fun)->intspec ? Qt : if_prop;
1830 /* Bytecode objects are interactive if they are long enough to
1831 have an element whose index is COMPILED_INTERACTIVE, which is
1832 where the interactive spec is stored. */
1833 else if (COMPILEDP (fun))
1834 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1835 ? Qt : if_prop);
1837 /* Strings and vectors are keyboard macros. */
1838 if (STRINGP (fun) || VECTORP (fun))
1839 return (NILP (for_call_interactively) ? Qt : Qnil);
1841 /* Lists may represent commands. */
1842 if (!CONSP (fun))
1843 return Qnil;
1844 funcar = XCAR (fun);
1845 if (EQ (funcar, Qclosure))
1846 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1847 ? Qt : if_prop);
1848 else if (EQ (funcar, Qlambda))
1849 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1850 else if (EQ (funcar, Qautoload))
1851 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1852 else
1853 return Qnil;
1856 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1857 doc: /* Define FUNCTION to autoload from FILE.
1858 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1859 Third arg DOCSTRING is documentation for the function.
1860 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1861 Fifth arg TYPE indicates the type of the object:
1862 nil or omitted says FUNCTION is a function,
1863 `keymap' says FUNCTION is really a keymap, and
1864 `macro' or t says FUNCTION is really a macro.
1865 Third through fifth args give info about the real definition.
1866 They default to nil.
1867 If FUNCTION is already defined other than as an autoload,
1868 this does nothing and returns nil. */)
1869 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1871 CHECK_SYMBOL (function);
1872 CHECK_STRING (file);
1874 /* If function is defined and not as an autoload, don't override. */
1875 if (!NILP (XSYMBOL (function)->function)
1876 && !AUTOLOADP (XSYMBOL (function)->function))
1877 return Qnil;
1879 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1880 /* `read1' in lread.c has found the docstring starting with "\
1881 and assumed the docstring will be provided by Snarf-documentation, so it
1882 passed us 0 instead. But that leads to accidental sharing in purecopy's
1883 hash-consing, so we use a (hopefully) unique integer instead. */
1884 docstring = make_number (XHASH (function));
1885 return Fdefalias (function,
1886 list5 (Qautoload, file, docstring, interactive, type),
1887 Qnil);
1890 void
1891 un_autoload (Lisp_Object oldqueue)
1893 Lisp_Object queue, first, second;
1895 /* Queue to unwind is current value of Vautoload_queue.
1896 oldqueue is the shadowed value to leave in Vautoload_queue. */
1897 queue = Vautoload_queue;
1898 Vautoload_queue = oldqueue;
1899 while (CONSP (queue))
1901 first = XCAR (queue);
1902 second = Fcdr (first);
1903 first = Fcar (first);
1904 if (EQ (first, make_number (0)))
1905 Vfeatures = second;
1906 else
1907 Ffset (first, second);
1908 queue = XCDR (queue);
1912 /* Load an autoloaded function.
1913 FUNNAME is the symbol which is the function's name.
1914 FUNDEF is the autoload definition (a list). */
1916 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1917 doc: /* Load FUNDEF which should be an autoload.
1918 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1919 in which case the function returns the new autoloaded function value.
1920 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1921 it is defines a macro. */)
1922 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1924 ptrdiff_t count = SPECPDL_INDEX ();
1925 struct gcpro gcpro1, gcpro2, gcpro3;
1927 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1928 return fundef;
1930 if (EQ (macro_only, Qmacro))
1932 Lisp_Object kind = Fnth (make_number (4), fundef);
1933 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1934 return fundef;
1937 /* This is to make sure that loadup.el gives a clear picture
1938 of what files are preloaded and when. */
1939 if (! NILP (Vpurify_flag))
1940 error ("Attempt to autoload %s while preparing to dump",
1941 SDATA (SYMBOL_NAME (funname)));
1943 CHECK_SYMBOL (funname);
1944 GCPRO3 (funname, fundef, macro_only);
1946 /* Preserve the match data. */
1947 record_unwind_save_match_data ();
1949 /* If autoloading gets an error (which includes the error of failing
1950 to define the function being called), we use Vautoload_queue
1951 to undo function definitions and `provide' calls made by
1952 the function. We do this in the specific case of autoloading
1953 because autoloading is not an explicit request "load this file",
1954 but rather a request to "call this function".
1956 The value saved here is to be restored into Vautoload_queue. */
1957 record_unwind_protect (un_autoload, Vautoload_queue);
1958 Vautoload_queue = Qt;
1959 /* If `macro_only', assume this autoload to be a "best-effort",
1960 so don't signal an error if autoloading fails. */
1961 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1963 /* Once loading finishes, don't undo it. */
1964 Vautoload_queue = Qt;
1965 unbind_to (count, Qnil);
1967 UNGCPRO;
1969 if (NILP (funname))
1970 return Qnil;
1971 else
1973 Lisp_Object fun = Findirect_function (funname, Qnil);
1975 if (!NILP (Fequal (fun, fundef)))
1976 error ("Autoloading failed to define function %s",
1977 SDATA (SYMBOL_NAME (funname)));
1978 else
1979 return fun;
1984 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1985 doc: /* Evaluate FORM and return its value.
1986 If LEXICAL is t, evaluate using lexical scoping.
1987 LEXICAL can also be an actual lexical environment, in the form of an
1988 alist mapping symbols to their value. */)
1989 (Lisp_Object form, Lisp_Object lexical)
1991 ptrdiff_t count = SPECPDL_INDEX ();
1992 specbind (Qinternal_interpreter_environment,
1993 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1994 return unbind_to (count, eval_sub (form));
1997 /* Grow the specpdl stack by one entry.
1998 The caller should have already initialized the entry.
1999 Signal an error on stack overflow.
2001 Make sure that there is always one unused entry past the top of the
2002 stack, so that the just-initialized entry is safely unwound if
2003 memory exhausted and an error is signaled here. Also, allocate a
2004 never-used entry just before the bottom of the stack; sometimes its
2005 address is taken. */
2007 static void
2008 grow_specpdl (void)
2010 specpdl_ptr++;
2012 if (specpdl_ptr == specpdl + specpdl_size)
2014 ptrdiff_t count = SPECPDL_INDEX ();
2015 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2016 union specbinding *pdlvec = specpdl - 1;
2017 ptrdiff_t pdlvecsize = specpdl_size + 1;
2018 if (max_size <= specpdl_size)
2020 if (max_specpdl_size < 400)
2021 max_size = max_specpdl_size = 400;
2022 if (max_size <= specpdl_size)
2023 signal_error ("Variable binding depth exceeds max-specpdl-size",
2024 Qnil);
2026 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2027 specpdl = pdlvec + 1;
2028 specpdl_size = pdlvecsize - 1;
2029 specpdl_ptr = specpdl + count;
2033 void
2034 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2036 eassert (nargs >= UNEVALLED);
2037 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2038 specpdl_ptr->bt.debug_on_exit = false;
2039 specpdl_ptr->bt.function = function;
2040 specpdl_ptr->bt.args = args;
2041 specpdl_ptr->bt.nargs = nargs;
2042 grow_specpdl ();
2045 /* Eval a sub-expression of the current expression (i.e. in the same
2046 lexical scope). */
2047 Lisp_Object
2048 eval_sub (Lisp_Object form)
2050 Lisp_Object fun, val, original_fun, original_args;
2051 Lisp_Object funcar;
2052 struct gcpro gcpro1, gcpro2, gcpro3;
2054 if (SYMBOLP (form))
2056 /* Look up its binding in the lexical environment.
2057 We do not pay attention to the declared_special flag here, since we
2058 already did that when let-binding the variable. */
2059 Lisp_Object lex_binding
2060 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2061 ? Fassq (form, Vinternal_interpreter_environment)
2062 : Qnil;
2063 if (CONSP (lex_binding))
2064 return XCDR (lex_binding);
2065 else
2066 return Fsymbol_value (form);
2069 if (!CONSP (form))
2070 return form;
2072 QUIT;
2074 GCPRO1 (form);
2075 maybe_gc ();
2076 UNGCPRO;
2078 if (++lisp_eval_depth > max_lisp_eval_depth)
2080 if (max_lisp_eval_depth < 100)
2081 max_lisp_eval_depth = 100;
2082 if (lisp_eval_depth > max_lisp_eval_depth)
2083 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2086 original_fun = XCAR (form);
2087 original_args = XCDR (form);
2089 /* This also protects them from gc. */
2090 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2092 if (debug_on_next_call)
2093 do_debug_on_call (Qt);
2095 /* At this point, only original_fun and original_args
2096 have values that will be used below. */
2097 retry:
2099 /* Optimize for no indirection. */
2100 fun = original_fun;
2101 if (!SYMBOLP (fun))
2102 fun = Ffunction (Fcons (fun, Qnil));
2103 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2104 fun = indirect_function (fun);
2106 if (SUBRP (fun))
2108 Lisp_Object numargs;
2109 Lisp_Object argvals[8];
2110 Lisp_Object args_left;
2111 register int i, maxargs;
2113 args_left = original_args;
2114 numargs = Flength (args_left);
2116 check_cons_list ();
2118 if (XINT (numargs) < XSUBR (fun)->min_args
2119 || (XSUBR (fun)->max_args >= 0
2120 && XSUBR (fun)->max_args < XINT (numargs)))
2121 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2123 else if (XSUBR (fun)->max_args == UNEVALLED)
2124 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2125 else if (XSUBR (fun)->max_args == MANY)
2127 /* Pass a vector of evaluated arguments. */
2128 Lisp_Object *vals;
2129 ptrdiff_t argnum = 0;
2130 USE_SAFE_ALLOCA;
2132 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2134 GCPRO3 (args_left, fun, fun);
2135 gcpro3.var = vals;
2136 gcpro3.nvars = 0;
2138 while (!NILP (args_left))
2140 vals[argnum++] = eval_sub (Fcar (args_left));
2141 args_left = Fcdr (args_left);
2142 gcpro3.nvars = argnum;
2145 set_backtrace_args (specpdl_ptr - 1, vals);
2146 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2148 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2149 UNGCPRO;
2150 SAFE_FREE ();
2152 else
2154 GCPRO3 (args_left, fun, fun);
2155 gcpro3.var = argvals;
2156 gcpro3.nvars = 0;
2158 maxargs = XSUBR (fun)->max_args;
2159 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2161 argvals[i] = eval_sub (Fcar (args_left));
2162 gcpro3.nvars = ++i;
2165 UNGCPRO;
2167 set_backtrace_args (specpdl_ptr - 1, argvals);
2168 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2170 switch (i)
2172 case 0:
2173 val = (XSUBR (fun)->function.a0 ());
2174 break;
2175 case 1:
2176 val = (XSUBR (fun)->function.a1 (argvals[0]));
2177 break;
2178 case 2:
2179 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2180 break;
2181 case 3:
2182 val = (XSUBR (fun)->function.a3
2183 (argvals[0], argvals[1], argvals[2]));
2184 break;
2185 case 4:
2186 val = (XSUBR (fun)->function.a4
2187 (argvals[0], argvals[1], argvals[2], argvals[3]));
2188 break;
2189 case 5:
2190 val = (XSUBR (fun)->function.a5
2191 (argvals[0], argvals[1], argvals[2], argvals[3],
2192 argvals[4]));
2193 break;
2194 case 6:
2195 val = (XSUBR (fun)->function.a6
2196 (argvals[0], argvals[1], argvals[2], argvals[3],
2197 argvals[4], argvals[5]));
2198 break;
2199 case 7:
2200 val = (XSUBR (fun)->function.a7
2201 (argvals[0], argvals[1], argvals[2], argvals[3],
2202 argvals[4], argvals[5], argvals[6]));
2203 break;
2205 case 8:
2206 val = (XSUBR (fun)->function.a8
2207 (argvals[0], argvals[1], argvals[2], argvals[3],
2208 argvals[4], argvals[5], argvals[6], argvals[7]));
2209 break;
2211 default:
2212 /* Someone has created a subr that takes more arguments than
2213 is supported by this code. We need to either rewrite the
2214 subr to use a different argument protocol, or add more
2215 cases to this switch. */
2216 emacs_abort ();
2220 else if (COMPILEDP (fun))
2221 val = apply_lambda (fun, original_args);
2222 else
2224 if (NILP (fun))
2225 xsignal1 (Qvoid_function, original_fun);
2226 if (!CONSP (fun))
2227 xsignal1 (Qinvalid_function, original_fun);
2228 funcar = XCAR (fun);
2229 if (!SYMBOLP (funcar))
2230 xsignal1 (Qinvalid_function, original_fun);
2231 if (EQ (funcar, Qautoload))
2233 Fautoload_do_load (fun, original_fun, Qnil);
2234 goto retry;
2236 if (EQ (funcar, Qmacro))
2238 ptrdiff_t count = SPECPDL_INDEX ();
2239 Lisp_Object exp;
2240 /* Bind lexical-binding during expansion of the macro, so the
2241 macro can know reliably if the code it outputs will be
2242 interpreted using lexical-binding or not. */
2243 specbind (Qlexical_binding,
2244 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2245 exp = apply1 (Fcdr (fun), original_args);
2246 unbind_to (count, Qnil);
2247 val = eval_sub (exp);
2249 else if (EQ (funcar, Qlambda)
2250 || EQ (funcar, Qclosure))
2251 val = apply_lambda (fun, original_args);
2252 else
2253 xsignal1 (Qinvalid_function, original_fun);
2255 check_cons_list ();
2257 lisp_eval_depth--;
2258 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2259 val = call_debugger (list2 (Qexit, val));
2260 specpdl_ptr--;
2262 return val;
2265 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2266 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2267 Then return the value FUNCTION returns.
2268 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2269 usage: (apply FUNCTION &rest ARGUMENTS) */)
2270 (ptrdiff_t nargs, Lisp_Object *args)
2272 ptrdiff_t i;
2273 EMACS_INT numargs;
2274 register Lisp_Object spread_arg;
2275 register Lisp_Object *funcall_args;
2276 Lisp_Object fun, retval;
2277 struct gcpro gcpro1;
2278 USE_SAFE_ALLOCA;
2280 fun = args [0];
2281 funcall_args = 0;
2282 spread_arg = args [nargs - 1];
2283 CHECK_LIST (spread_arg);
2285 numargs = XINT (Flength (spread_arg));
2287 if (numargs == 0)
2288 return Ffuncall (nargs - 1, args);
2289 else if (numargs == 1)
2291 args [nargs - 1] = XCAR (spread_arg);
2292 return Ffuncall (nargs, args);
2295 numargs += nargs - 2;
2297 /* Optimize for no indirection. */
2298 if (SYMBOLP (fun) && !NILP (fun)
2299 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2300 fun = indirect_function (fun);
2301 if (NILP (fun))
2303 /* Let funcall get the error. */
2304 fun = args[0];
2305 goto funcall;
2308 if (SUBRP (fun))
2310 if (numargs < XSUBR (fun)->min_args
2311 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2312 goto funcall; /* Let funcall get the error. */
2313 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2315 /* Avoid making funcall cons up a yet another new vector of arguments
2316 by explicitly supplying nil's for optional values. */
2317 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2318 for (i = numargs; i < XSUBR (fun)->max_args;)
2319 funcall_args[++i] = Qnil;
2320 GCPRO1 (*funcall_args);
2321 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2324 funcall:
2325 /* We add 1 to numargs because funcall_args includes the
2326 function itself as well as its arguments. */
2327 if (!funcall_args)
2329 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2330 GCPRO1 (*funcall_args);
2331 gcpro1.nvars = 1 + numargs;
2334 memcpy (funcall_args, args, nargs * word_size);
2335 /* Spread the last arg we got. Its first element goes in
2336 the slot that it used to occupy, hence this value of I. */
2337 i = nargs - 1;
2338 while (!NILP (spread_arg))
2340 funcall_args [i++] = XCAR (spread_arg);
2341 spread_arg = XCDR (spread_arg);
2344 /* By convention, the caller needs to gcpro Ffuncall's args. */
2345 retval = Ffuncall (gcpro1.nvars, funcall_args);
2346 UNGCPRO;
2347 SAFE_FREE ();
2349 return retval;
2352 /* Run hook variables in various ways. */
2354 static Lisp_Object
2355 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2357 Ffuncall (nargs, args);
2358 return Qnil;
2361 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2362 doc: /* Run each hook in HOOKS.
2363 Each argument should be a symbol, a hook variable.
2364 These symbols are processed in the order specified.
2365 If a hook symbol has a non-nil value, that value may be a function
2366 or a list of functions to be called to run the hook.
2367 If the value is a function, it is called with no arguments.
2368 If it is a list, the elements are called, in order, with no arguments.
2370 Major modes should not use this function directly to run their mode
2371 hook; they should use `run-mode-hooks' instead.
2373 Do not use `make-local-variable' to make a hook variable buffer-local.
2374 Instead, use `add-hook' and specify t for the LOCAL argument.
2375 usage: (run-hooks &rest HOOKS) */)
2376 (ptrdiff_t nargs, Lisp_Object *args)
2378 Lisp_Object hook[1];
2379 ptrdiff_t i;
2381 for (i = 0; i < nargs; i++)
2383 hook[0] = args[i];
2384 run_hook_with_args (1, hook, funcall_nil);
2387 return Qnil;
2390 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2391 Srun_hook_with_args, 1, MANY, 0,
2392 doc: /* Run HOOK with the specified arguments ARGS.
2393 HOOK should be a symbol, a hook variable. The value of HOOK
2394 may be nil, a function, or a list of functions. Call each
2395 function in order with arguments ARGS. The final return value
2396 is unspecified.
2398 Do not use `make-local-variable' to make a hook variable buffer-local.
2399 Instead, use `add-hook' and specify t for the LOCAL argument.
2400 usage: (run-hook-with-args HOOK &rest ARGS) */)
2401 (ptrdiff_t nargs, Lisp_Object *args)
2403 return run_hook_with_args (nargs, args, funcall_nil);
2406 /* NB this one still documents a specific non-nil return value.
2407 (As did run-hook-with-args and run-hook-with-args-until-failure
2408 until they were changed in 24.1.) */
2409 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2410 Srun_hook_with_args_until_success, 1, MANY, 0,
2411 doc: /* Run HOOK with the specified arguments ARGS.
2412 HOOK should be a symbol, a hook variable. The value of HOOK
2413 may be nil, a function, or a list of functions. Call each
2414 function in order with arguments ARGS, stopping at the first
2415 one that returns non-nil, and return that value. Otherwise (if
2416 all functions return nil, or if there are no functions to call),
2417 return nil.
2419 Do not use `make-local-variable' to make a hook variable buffer-local.
2420 Instead, use `add-hook' and specify t for the LOCAL argument.
2421 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2422 (ptrdiff_t nargs, Lisp_Object *args)
2424 return run_hook_with_args (nargs, args, Ffuncall);
2427 static Lisp_Object
2428 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2430 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2433 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2434 Srun_hook_with_args_until_failure, 1, MANY, 0,
2435 doc: /* Run HOOK with the specified arguments ARGS.
2436 HOOK should be a symbol, a hook variable. The value of HOOK
2437 may be nil, a function, or a list of functions. Call each
2438 function in order with arguments ARGS, stopping at the first
2439 one that returns nil, and return nil. Otherwise (if all functions
2440 return non-nil, or if there are no functions to call), return non-nil
2441 \(do not rely on the precise return value in this case).
2443 Do not use `make-local-variable' to make a hook variable buffer-local.
2444 Instead, use `add-hook' and specify t for the LOCAL argument.
2445 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2446 (ptrdiff_t nargs, Lisp_Object *args)
2448 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2451 static Lisp_Object
2452 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2454 Lisp_Object tmp = args[0], ret;
2455 args[0] = args[1];
2456 args[1] = tmp;
2457 ret = Ffuncall (nargs, args);
2458 args[1] = args[0];
2459 args[0] = tmp;
2460 return ret;
2463 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2464 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2465 I.e. instead of calling each function FUN directly with arguments ARGS,
2466 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2467 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2468 aborts and returns that value.
2469 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2470 (ptrdiff_t nargs, Lisp_Object *args)
2472 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2475 /* ARGS[0] should be a hook symbol.
2476 Call each of the functions in the hook value, passing each of them
2477 as arguments all the rest of ARGS (all NARGS - 1 elements).
2478 FUNCALL specifies how to call each function on the hook.
2479 The caller (or its caller, etc) must gcpro all of ARGS,
2480 except that it isn't necessary to gcpro ARGS[0]. */
2482 Lisp_Object
2483 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2484 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2486 Lisp_Object sym, val, ret = Qnil;
2487 struct gcpro gcpro1, gcpro2, gcpro3;
2489 /* If we are dying or still initializing,
2490 don't do anything--it would probably crash if we tried. */
2491 if (NILP (Vrun_hooks))
2492 return Qnil;
2494 sym = args[0];
2495 val = find_symbol_value (sym);
2497 if (EQ (val, Qunbound) || NILP (val))
2498 return ret;
2499 else if (!CONSP (val) || FUNCTIONP (val))
2501 args[0] = val;
2502 return funcall (nargs, args);
2504 else
2506 Lisp_Object global_vals = Qnil;
2507 GCPRO3 (sym, val, global_vals);
2509 for (;
2510 CONSP (val) && NILP (ret);
2511 val = XCDR (val))
2513 if (EQ (XCAR (val), Qt))
2515 /* t indicates this hook has a local binding;
2516 it means to run the global binding too. */
2517 global_vals = Fdefault_value (sym);
2518 if (NILP (global_vals)) continue;
2520 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2522 args[0] = global_vals;
2523 ret = funcall (nargs, args);
2525 else
2527 for (;
2528 CONSP (global_vals) && NILP (ret);
2529 global_vals = XCDR (global_vals))
2531 args[0] = XCAR (global_vals);
2532 /* In a global value, t should not occur. If it does, we
2533 must ignore it to avoid an endless loop. */
2534 if (!EQ (args[0], Qt))
2535 ret = funcall (nargs, args);
2539 else
2541 args[0] = XCAR (val);
2542 ret = funcall (nargs, args);
2546 UNGCPRO;
2547 return ret;
2551 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2553 void
2554 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2556 Lisp_Object temp[3];
2557 temp[0] = hook;
2558 temp[1] = arg1;
2559 temp[2] = arg2;
2561 Frun_hook_with_args (3, temp);
2564 /* Apply fn to arg. */
2565 Lisp_Object
2566 apply1 (Lisp_Object fn, Lisp_Object arg)
2568 struct gcpro gcpro1;
2570 GCPRO1 (fn);
2571 if (NILP (arg))
2572 RETURN_UNGCPRO (Ffuncall (1, &fn));
2573 gcpro1.nvars = 2;
2575 Lisp_Object args[2];
2576 args[0] = fn;
2577 args[1] = arg;
2578 gcpro1.var = args;
2579 RETURN_UNGCPRO (Fapply (2, args));
2583 /* Call function fn on no arguments. */
2584 Lisp_Object
2585 call0 (Lisp_Object fn)
2587 struct gcpro gcpro1;
2589 GCPRO1 (fn);
2590 RETURN_UNGCPRO (Ffuncall (1, &fn));
2593 /* Call function fn with 1 argument arg1. */
2594 /* ARGSUSED */
2595 Lisp_Object
2596 call1 (Lisp_Object fn, Lisp_Object arg1)
2598 struct gcpro gcpro1;
2599 Lisp_Object args[2];
2601 args[0] = fn;
2602 args[1] = arg1;
2603 GCPRO1 (args[0]);
2604 gcpro1.nvars = 2;
2605 RETURN_UNGCPRO (Ffuncall (2, args));
2608 /* Call function fn with 2 arguments arg1, arg2. */
2609 /* ARGSUSED */
2610 Lisp_Object
2611 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2613 struct gcpro gcpro1;
2614 Lisp_Object args[3];
2615 args[0] = fn;
2616 args[1] = arg1;
2617 args[2] = arg2;
2618 GCPRO1 (args[0]);
2619 gcpro1.nvars = 3;
2620 RETURN_UNGCPRO (Ffuncall (3, args));
2623 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2624 /* ARGSUSED */
2625 Lisp_Object
2626 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2628 struct gcpro gcpro1;
2629 Lisp_Object args[4];
2630 args[0] = fn;
2631 args[1] = arg1;
2632 args[2] = arg2;
2633 args[3] = arg3;
2634 GCPRO1 (args[0]);
2635 gcpro1.nvars = 4;
2636 RETURN_UNGCPRO (Ffuncall (4, args));
2639 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2640 /* ARGSUSED */
2641 Lisp_Object
2642 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2643 Lisp_Object arg4)
2645 struct gcpro gcpro1;
2646 Lisp_Object args[5];
2647 args[0] = fn;
2648 args[1] = arg1;
2649 args[2] = arg2;
2650 args[3] = arg3;
2651 args[4] = arg4;
2652 GCPRO1 (args[0]);
2653 gcpro1.nvars = 5;
2654 RETURN_UNGCPRO (Ffuncall (5, args));
2657 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2658 /* ARGSUSED */
2659 Lisp_Object
2660 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2661 Lisp_Object arg4, Lisp_Object arg5)
2663 struct gcpro gcpro1;
2664 Lisp_Object args[6];
2665 args[0] = fn;
2666 args[1] = arg1;
2667 args[2] = arg2;
2668 args[3] = arg3;
2669 args[4] = arg4;
2670 args[5] = arg5;
2671 GCPRO1 (args[0]);
2672 gcpro1.nvars = 6;
2673 RETURN_UNGCPRO (Ffuncall (6, args));
2676 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2677 /* ARGSUSED */
2678 Lisp_Object
2679 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2680 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2682 struct gcpro gcpro1;
2683 Lisp_Object args[7];
2684 args[0] = fn;
2685 args[1] = arg1;
2686 args[2] = arg2;
2687 args[3] = arg3;
2688 args[4] = arg4;
2689 args[5] = arg5;
2690 args[6] = arg6;
2691 GCPRO1 (args[0]);
2692 gcpro1.nvars = 7;
2693 RETURN_UNGCPRO (Ffuncall (7, args));
2696 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2697 /* ARGSUSED */
2698 Lisp_Object
2699 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2700 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2702 struct gcpro gcpro1;
2703 Lisp_Object args[8];
2704 args[0] = fn;
2705 args[1] = arg1;
2706 args[2] = arg2;
2707 args[3] = arg3;
2708 args[4] = arg4;
2709 args[5] = arg5;
2710 args[6] = arg6;
2711 args[7] = arg7;
2712 GCPRO1 (args[0]);
2713 gcpro1.nvars = 8;
2714 RETURN_UNGCPRO (Ffuncall (8, args));
2717 /* The caller should GCPRO all the elements of ARGS. */
2719 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2720 doc: /* Non-nil if OBJECT is a function. */)
2721 (Lisp_Object object)
2723 if (FUNCTIONP (object))
2724 return Qt;
2725 return Qnil;
2728 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2729 doc: /* Call first argument as a function, passing remaining arguments to it.
2730 Return the value that function returns.
2731 Thus, (funcall 'cons 'x 'y) returns (x . y).
2732 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2733 (ptrdiff_t nargs, Lisp_Object *args)
2735 Lisp_Object fun, original_fun;
2736 Lisp_Object funcar;
2737 ptrdiff_t numargs = nargs - 1;
2738 Lisp_Object lisp_numargs;
2739 Lisp_Object val;
2740 register Lisp_Object *internal_args;
2741 ptrdiff_t i;
2743 QUIT;
2745 if (++lisp_eval_depth > max_lisp_eval_depth)
2747 if (max_lisp_eval_depth < 100)
2748 max_lisp_eval_depth = 100;
2749 if (lisp_eval_depth > max_lisp_eval_depth)
2750 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2753 /* This also GCPROs them. */
2754 record_in_backtrace (args[0], &args[1], nargs - 1);
2756 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2757 maybe_gc ();
2759 if (debug_on_next_call)
2760 do_debug_on_call (Qlambda);
2762 check_cons_list ();
2764 original_fun = args[0];
2766 retry:
2768 /* Optimize for no indirection. */
2769 fun = original_fun;
2770 if (SYMBOLP (fun) && !NILP (fun)
2771 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2772 fun = indirect_function (fun);
2774 if (SUBRP (fun))
2776 if (numargs < XSUBR (fun)->min_args
2777 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2779 XSETFASTINT (lisp_numargs, numargs);
2780 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2783 else if (XSUBR (fun)->max_args == UNEVALLED)
2784 xsignal1 (Qinvalid_function, original_fun);
2786 else if (XSUBR (fun)->max_args == MANY)
2787 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2788 else
2790 if (XSUBR (fun)->max_args > numargs)
2792 internal_args = alloca (XSUBR (fun)->max_args
2793 * sizeof *internal_args);
2794 memcpy (internal_args, args + 1, numargs * word_size);
2795 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2796 internal_args[i] = Qnil;
2798 else
2799 internal_args = args + 1;
2800 switch (XSUBR (fun)->max_args)
2802 case 0:
2803 val = (XSUBR (fun)->function.a0 ());
2804 break;
2805 case 1:
2806 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2807 break;
2808 case 2:
2809 val = (XSUBR (fun)->function.a2
2810 (internal_args[0], internal_args[1]));
2811 break;
2812 case 3:
2813 val = (XSUBR (fun)->function.a3
2814 (internal_args[0], internal_args[1], internal_args[2]));
2815 break;
2816 case 4:
2817 val = (XSUBR (fun)->function.a4
2818 (internal_args[0], internal_args[1], internal_args[2],
2819 internal_args[3]));
2820 break;
2821 case 5:
2822 val = (XSUBR (fun)->function.a5
2823 (internal_args[0], internal_args[1], internal_args[2],
2824 internal_args[3], internal_args[4]));
2825 break;
2826 case 6:
2827 val = (XSUBR (fun)->function.a6
2828 (internal_args[0], internal_args[1], internal_args[2],
2829 internal_args[3], internal_args[4], internal_args[5]));
2830 break;
2831 case 7:
2832 val = (XSUBR (fun)->function.a7
2833 (internal_args[0], internal_args[1], internal_args[2],
2834 internal_args[3], internal_args[4], internal_args[5],
2835 internal_args[6]));
2836 break;
2838 case 8:
2839 val = (XSUBR (fun)->function.a8
2840 (internal_args[0], internal_args[1], internal_args[2],
2841 internal_args[3], internal_args[4], internal_args[5],
2842 internal_args[6], internal_args[7]));
2843 break;
2845 default:
2847 /* If a subr takes more than 8 arguments without using MANY
2848 or UNEVALLED, we need to extend this function to support it.
2849 Until this is done, there is no way to call the function. */
2850 emacs_abort ();
2854 else if (COMPILEDP (fun))
2855 val = funcall_lambda (fun, numargs, args + 1);
2856 else
2858 if (NILP (fun))
2859 xsignal1 (Qvoid_function, original_fun);
2860 if (!CONSP (fun))
2861 xsignal1 (Qinvalid_function, original_fun);
2862 funcar = XCAR (fun);
2863 if (!SYMBOLP (funcar))
2864 xsignal1 (Qinvalid_function, original_fun);
2865 if (EQ (funcar, Qlambda)
2866 || EQ (funcar, Qclosure))
2867 val = funcall_lambda (fun, numargs, args + 1);
2868 else if (EQ (funcar, Qautoload))
2870 Fautoload_do_load (fun, original_fun, Qnil);
2871 check_cons_list ();
2872 goto retry;
2874 else
2875 xsignal1 (Qinvalid_function, original_fun);
2877 check_cons_list ();
2878 lisp_eval_depth--;
2879 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2880 val = call_debugger (list2 (Qexit, val));
2881 specpdl_ptr--;
2882 return val;
2885 static Lisp_Object
2886 apply_lambda (Lisp_Object fun, Lisp_Object args)
2888 Lisp_Object args_left;
2889 ptrdiff_t i;
2890 EMACS_INT numargs;
2891 register Lisp_Object *arg_vector;
2892 struct gcpro gcpro1, gcpro2, gcpro3;
2893 register Lisp_Object tem;
2894 USE_SAFE_ALLOCA;
2896 numargs = XFASTINT (Flength (args));
2897 SAFE_ALLOCA_LISP (arg_vector, numargs);
2898 args_left = args;
2900 GCPRO3 (*arg_vector, args_left, fun);
2901 gcpro1.nvars = 0;
2903 for (i = 0; i < numargs; )
2905 tem = Fcar (args_left), args_left = Fcdr (args_left);
2906 tem = eval_sub (tem);
2907 arg_vector[i++] = tem;
2908 gcpro1.nvars = i;
2911 UNGCPRO;
2913 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2914 set_backtrace_nargs (specpdl_ptr - 1, i);
2915 tem = funcall_lambda (fun, numargs, arg_vector);
2917 /* Do the debug-on-exit now, while arg_vector still exists. */
2918 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2920 /* Don't do it again when we return to eval. */
2921 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2922 tem = call_debugger (list2 (Qexit, tem));
2924 SAFE_FREE ();
2925 return tem;
2928 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2929 and return the result of evaluation.
2930 FUN must be either a lambda-expression or a compiled-code object. */
2932 static Lisp_Object
2933 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2934 register Lisp_Object *arg_vector)
2936 Lisp_Object val, syms_left, next, lexenv;
2937 ptrdiff_t count = SPECPDL_INDEX ();
2938 ptrdiff_t i;
2939 bool optional, rest;
2941 if (CONSP (fun))
2943 if (EQ (XCAR (fun), Qclosure))
2945 fun = XCDR (fun); /* Drop `closure'. */
2946 lexenv = XCAR (fun);
2947 CHECK_LIST_CONS (fun, fun);
2949 else
2950 lexenv = Qnil;
2951 syms_left = XCDR (fun);
2952 if (CONSP (syms_left))
2953 syms_left = XCAR (syms_left);
2954 else
2955 xsignal1 (Qinvalid_function, fun);
2957 else if (COMPILEDP (fun))
2959 syms_left = AREF (fun, COMPILED_ARGLIST);
2960 if (INTEGERP (syms_left))
2961 /* A byte-code object with a non-nil `push args' slot means we
2962 shouldn't bind any arguments, instead just call the byte-code
2963 interpreter directly; it will push arguments as necessary.
2965 Byte-code objects with either a non-existent, or a nil value for
2966 the `push args' slot (the default), have dynamically-bound
2967 arguments, and use the argument-binding code below instead (as do
2968 all interpreted functions, even lexically bound ones). */
2970 /* If we have not actually read the bytecode string
2971 and constants vector yet, fetch them from the file. */
2972 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2973 Ffetch_bytecode (fun);
2974 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2975 AREF (fun, COMPILED_CONSTANTS),
2976 AREF (fun, COMPILED_STACK_DEPTH),
2977 syms_left,
2978 nargs, arg_vector);
2980 lexenv = Qnil;
2982 else
2983 emacs_abort ();
2985 i = optional = rest = 0;
2986 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2988 QUIT;
2990 next = XCAR (syms_left);
2991 if (!SYMBOLP (next))
2992 xsignal1 (Qinvalid_function, fun);
2994 if (EQ (next, Qand_rest))
2995 rest = 1;
2996 else if (EQ (next, Qand_optional))
2997 optional = 1;
2998 else
3000 Lisp_Object arg;
3001 if (rest)
3003 arg = Flist (nargs - i, &arg_vector[i]);
3004 i = nargs;
3006 else if (i < nargs)
3007 arg = arg_vector[i++];
3008 else if (!optional)
3009 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3010 else
3011 arg = Qnil;
3013 /* Bind the argument. */
3014 if (!NILP (lexenv) && SYMBOLP (next))
3015 /* Lexically bind NEXT by adding it to the lexenv alist. */
3016 lexenv = Fcons (Fcons (next, arg), lexenv);
3017 else
3018 /* Dynamically bind NEXT. */
3019 specbind (next, arg);
3023 if (!NILP (syms_left))
3024 xsignal1 (Qinvalid_function, fun);
3025 else if (i < nargs)
3026 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3028 if (!EQ (lexenv, Vinternal_interpreter_environment))
3029 /* Instantiate a new lexical environment. */
3030 specbind (Qinternal_interpreter_environment, lexenv);
3032 if (CONSP (fun))
3033 val = Fprogn (XCDR (XCDR (fun)));
3034 else
3036 /* If we have not actually read the bytecode string
3037 and constants vector yet, fetch them from the file. */
3038 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3039 Ffetch_bytecode (fun);
3040 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3041 AREF (fun, COMPILED_CONSTANTS),
3042 AREF (fun, COMPILED_STACK_DEPTH),
3043 Qnil, 0, 0);
3046 return unbind_to (count, val);
3049 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3050 1, 1, 0,
3051 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3052 (Lisp_Object object)
3054 Lisp_Object tem;
3056 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3058 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3059 if (!CONSP (tem))
3061 tem = AREF (object, COMPILED_BYTECODE);
3062 if (CONSP (tem) && STRINGP (XCAR (tem)))
3063 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3064 else
3065 error ("Invalid byte code");
3067 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3068 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3070 return object;
3073 /* Return true if SYMBOL currently has a let-binding
3074 which was made in the buffer that is now current. */
3076 bool
3077 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3079 union specbinding *p;
3080 Lisp_Object buf = Fcurrent_buffer ();
3082 for (p = specpdl_ptr; p > specpdl; )
3083 if ((--p)->kind > SPECPDL_LET)
3085 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3086 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3087 if (symbol == let_bound_symbol
3088 && EQ (specpdl_where (p), buf))
3089 return 1;
3092 return 0;
3095 bool
3096 let_shadows_global_binding_p (Lisp_Object symbol)
3098 union specbinding *p;
3100 for (p = specpdl_ptr; p > specpdl; )
3101 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3102 return 1;
3104 return 0;
3107 /* `specpdl_ptr' describes which variable is
3108 let-bound, so it can be properly undone when we unbind_to.
3109 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3110 - SYMBOL is the variable being bound. Note that it should not be
3111 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3112 to record V2 here).
3113 - WHERE tells us in which buffer the binding took place.
3114 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3115 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3116 i.e. bindings to the default value of a variable which can be
3117 buffer-local. */
3119 void
3120 specbind (Lisp_Object symbol, Lisp_Object value)
3122 struct Lisp_Symbol *sym;
3124 CHECK_SYMBOL (symbol);
3125 sym = XSYMBOL (symbol);
3127 start:
3128 switch (sym->redirect)
3130 case SYMBOL_VARALIAS:
3131 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3132 case SYMBOL_PLAINVAL:
3133 /* The most common case is that of a non-constant symbol with a
3134 trivial value. Make that as fast as we can. */
3135 specpdl_ptr->let.kind = SPECPDL_LET;
3136 specpdl_ptr->let.symbol = symbol;
3137 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3138 grow_specpdl ();
3139 if (!sym->constant)
3140 SET_SYMBOL_VAL (sym, value);
3141 else
3142 set_internal (symbol, value, Qnil, 1);
3143 break;
3144 case SYMBOL_LOCALIZED:
3145 if (SYMBOL_BLV (sym)->frame_local)
3146 error ("Frame-local vars cannot be let-bound");
3147 case SYMBOL_FORWARDED:
3149 Lisp_Object ovalue = find_symbol_value (symbol);
3150 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3151 specpdl_ptr->let.symbol = symbol;
3152 specpdl_ptr->let.old_value = ovalue;
3153 specpdl_ptr->let.where = Fcurrent_buffer ();
3155 eassert (sym->redirect != SYMBOL_LOCALIZED
3156 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3158 if (sym->redirect == SYMBOL_LOCALIZED)
3160 if (!blv_found (SYMBOL_BLV (sym)))
3161 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3163 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3165 /* If SYMBOL is a per-buffer variable which doesn't have a
3166 buffer-local value here, make the `let' change the global
3167 value by changing the value of SYMBOL in all buffers not
3168 having their own value. This is consistent with what
3169 happens with other buffer-local variables. */
3170 if (NILP (Flocal_variable_p (symbol, Qnil)))
3172 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3173 grow_specpdl ();
3174 Fset_default (symbol, value);
3175 return;
3178 else
3179 specpdl_ptr->let.kind = SPECPDL_LET;
3181 grow_specpdl ();
3182 set_internal (symbol, value, Qnil, 1);
3183 break;
3185 default: emacs_abort ();
3189 /* Push unwind-protect entries of various types. */
3191 void
3192 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3194 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3195 specpdl_ptr->unwind.func = function;
3196 specpdl_ptr->unwind.arg = arg;
3197 grow_specpdl ();
3200 void
3201 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3203 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3204 specpdl_ptr->unwind_ptr.func = function;
3205 specpdl_ptr->unwind_ptr.arg = arg;
3206 grow_specpdl ();
3209 void
3210 record_unwind_protect_int (void (*function) (int), int arg)
3212 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3213 specpdl_ptr->unwind_int.func = function;
3214 specpdl_ptr->unwind_int.arg = arg;
3215 grow_specpdl ();
3218 void
3219 record_unwind_protect_void (void (*function) (void))
3221 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3222 specpdl_ptr->unwind_void.func = function;
3223 grow_specpdl ();
3226 static void
3227 do_nothing (void)
3230 /* Push an unwind-protect entry that does nothing, so that
3231 set_unwind_protect_ptr can overwrite it later. */
3233 void
3234 record_unwind_protect_nothing (void)
3236 record_unwind_protect_void (do_nothing);
3239 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3240 It need not be at the top of the stack. */
3242 void
3243 clear_unwind_protect (ptrdiff_t count)
3245 union specbinding *p = specpdl + count;
3246 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3247 p->unwind_void.func = do_nothing;
3250 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3251 It need not be at the top of the stack. Discard the entry's
3252 previous value without invoking it. */
3254 void
3255 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3256 Lisp_Object arg)
3258 union specbinding *p = specpdl + count;
3259 p->unwind.kind = SPECPDL_UNWIND;
3260 p->unwind.func = func;
3261 p->unwind.arg = arg;
3264 void
3265 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3267 union specbinding *p = specpdl + count;
3268 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3269 p->unwind_ptr.func = func;
3270 p->unwind_ptr.arg = arg;
3273 /* Pop and execute entries from the unwind-protect stack until the
3274 depth COUNT is reached. Return VALUE. */
3276 Lisp_Object
3277 unbind_to (ptrdiff_t count, Lisp_Object value)
3279 Lisp_Object quitf = Vquit_flag;
3280 struct gcpro gcpro1, gcpro2;
3282 GCPRO2 (value, quitf);
3283 Vquit_flag = Qnil;
3285 while (specpdl_ptr != specpdl + count)
3287 /* Decrement specpdl_ptr before we do the work to unbind it, so
3288 that an error in unbinding won't try to unbind the same entry
3289 again. Take care to copy any parts of the binding needed
3290 before invoking any code that can make more bindings. */
3292 specpdl_ptr--;
3294 switch (specpdl_ptr->kind)
3296 case SPECPDL_UNWIND:
3297 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3298 break;
3299 case SPECPDL_UNWIND_PTR:
3300 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3301 break;
3302 case SPECPDL_UNWIND_INT:
3303 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3304 break;
3305 case SPECPDL_UNWIND_VOID:
3306 specpdl_ptr->unwind_void.func ();
3307 break;
3308 case SPECPDL_BACKTRACE:
3309 break;
3310 case SPECPDL_LET:
3311 { /* If variable has a trivial value (no forwarding), we can
3312 just set it. No need to check for constant symbols here,
3313 since that was already done by specbind. */
3314 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3315 if (sym->redirect == SYMBOL_PLAINVAL)
3317 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3318 break;
3320 else
3321 { /* FALLTHROUGH!!
3322 NOTE: we only ever come here if make_local_foo was used for
3323 the first time on this var within this let. */
3326 case SPECPDL_LET_DEFAULT:
3327 Fset_default (specpdl_symbol (specpdl_ptr),
3328 specpdl_old_value (specpdl_ptr));
3329 break;
3330 case SPECPDL_LET_LOCAL:
3332 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3333 Lisp_Object where = specpdl_where (specpdl_ptr);
3334 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3335 eassert (BUFFERP (where));
3337 /* If this was a local binding, reset the value in the appropriate
3338 buffer, but only if that buffer's binding still exists. */
3339 if (!NILP (Flocal_variable_p (symbol, where)))
3340 set_internal (symbol, old_value, where, 1);
3342 break;
3346 if (NILP (Vquit_flag) && !NILP (quitf))
3347 Vquit_flag = quitf;
3349 UNGCPRO;
3350 return value;
3353 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3354 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3355 A special variable is one that will be bound dynamically, even in a
3356 context where binding is lexical by default. */)
3357 (Lisp_Object symbol)
3359 CHECK_SYMBOL (symbol);
3360 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3364 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3365 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3366 The debugger is entered when that frame exits, if the flag is non-nil. */)
3367 (Lisp_Object level, Lisp_Object flag)
3369 union specbinding *pdl = backtrace_top ();
3370 register EMACS_INT i;
3372 CHECK_NUMBER (level);
3374 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3375 pdl = backtrace_next (pdl);
3377 if (backtrace_p (pdl))
3378 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3380 return flag;
3383 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3384 doc: /* Print a trace of Lisp function calls currently active.
3385 Output stream used is value of `standard-output'. */)
3386 (void)
3388 union specbinding *pdl = backtrace_top ();
3389 Lisp_Object tem;
3390 Lisp_Object old_print_level = Vprint_level;
3392 if (NILP (Vprint_level))
3393 XSETFASTINT (Vprint_level, 8);
3395 while (backtrace_p (pdl))
3397 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3398 if (backtrace_nargs (pdl) == UNEVALLED)
3400 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3401 Qnil);
3402 write_string ("\n", -1);
3404 else
3406 tem = backtrace_function (pdl);
3407 Fprin1 (tem, Qnil); /* This can QUIT. */
3408 write_string ("(", -1);
3410 ptrdiff_t i;
3411 for (i = 0; i < backtrace_nargs (pdl); i++)
3413 if (i) write_string (" ", -1);
3414 Fprin1 (backtrace_args (pdl)[i], Qnil);
3417 write_string (")\n", -1);
3419 pdl = backtrace_next (pdl);
3422 Vprint_level = old_print_level;
3423 return Qnil;
3426 static union specbinding *
3427 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3429 union specbinding *pdl = backtrace_top ();
3430 register EMACS_INT i;
3432 CHECK_NATNUM (nframes);
3434 if (!NILP (base))
3435 { /* Skip up to `base'. */
3436 base = Findirect_function (base, Qt);
3437 while (backtrace_p (pdl)
3438 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3439 pdl = backtrace_next (pdl);
3442 /* Find the frame requested. */
3443 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3444 pdl = backtrace_next (pdl);
3446 return pdl;
3449 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3450 doc: /* Return the function and arguments NFRAMES up from current execution point.
3451 If that frame has not evaluated the arguments yet (or is a special form),
3452 the value is (nil FUNCTION ARG-FORMS...).
3453 If that frame has evaluated its arguments and called its function already,
3454 the value is (t FUNCTION ARG-VALUES...).
3455 A &rest arg is represented as the tail of the list ARG-VALUES.
3456 FUNCTION is whatever was supplied as car of evaluated list,
3457 or a lambda expression for macro calls.
3458 If NFRAMES is more than the number of frames, the value is nil.
3459 If BASE is non-nil, it should be a function and NFRAMES counts from its
3460 nearest activation frame. */)
3461 (Lisp_Object nframes, Lisp_Object base)
3463 union specbinding *pdl = get_backtrace_frame (nframes, base);
3465 if (!backtrace_p (pdl))
3466 return Qnil;
3467 if (backtrace_nargs (pdl) == UNEVALLED)
3468 return Fcons (Qnil,
3469 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3470 else
3472 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3474 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3478 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3479 the specpdl stack, and then rewind them. We store the pre-unwind values
3480 directly in the pre-existing specpdl elements (i.e. we swap the current
3481 value and the old value stored in the specpdl), kind of like the inplace
3482 pointer-reversal trick. As it turns out, the rewind does the same as the
3483 unwind, except it starts from the other end of the specpdl stack, so we use
3484 the same function for both unwind and rewind. */
3485 static void
3486 backtrace_eval_unrewind (int distance)
3488 union specbinding *tmp = specpdl_ptr;
3489 int step = -1;
3490 if (distance < 0)
3491 { /* It's a rewind rather than unwind. */
3492 tmp += distance - 1;
3493 step = 1;
3494 distance = -distance;
3497 for (; distance > 0; distance--)
3499 tmp += step;
3500 /* */
3501 switch (tmp->kind)
3503 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3504 unwind_protect, but the problem is that we don't know how to
3505 rewind them afterwards. */
3506 case SPECPDL_UNWIND:
3507 case SPECPDL_UNWIND_PTR:
3508 case SPECPDL_UNWIND_INT:
3509 case SPECPDL_UNWIND_VOID:
3510 case SPECPDL_BACKTRACE:
3511 break;
3512 case SPECPDL_LET:
3513 { /* If variable has a trivial value (no forwarding), we can
3514 just set it. No need to check for constant symbols here,
3515 since that was already done by specbind. */
3516 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3517 if (sym->redirect == SYMBOL_PLAINVAL)
3519 Lisp_Object old_value = specpdl_old_value (tmp);
3520 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3521 SET_SYMBOL_VAL (sym, old_value);
3522 break;
3524 else
3525 { /* FALLTHROUGH!!
3526 NOTE: we only ever come here if make_local_foo was used for
3527 the first time on this var within this let. */
3530 case SPECPDL_LET_DEFAULT:
3532 Lisp_Object sym = specpdl_symbol (tmp);
3533 Lisp_Object old_value = specpdl_old_value (tmp);
3534 set_specpdl_old_value (tmp, Fdefault_value (sym));
3535 Fset_default (sym, old_value);
3537 break;
3538 case SPECPDL_LET_LOCAL:
3540 Lisp_Object symbol = specpdl_symbol (tmp);
3541 Lisp_Object where = specpdl_where (tmp);
3542 Lisp_Object old_value = specpdl_old_value (tmp);
3543 eassert (BUFFERP (where));
3545 /* If this was a local binding, reset the value in the appropriate
3546 buffer, but only if that buffer's binding still exists. */
3547 if (!NILP (Flocal_variable_p (symbol, where)))
3549 set_specpdl_old_value
3550 (tmp, Fbuffer_local_value (symbol, where));
3551 set_internal (symbol, old_value, where, 1);
3554 break;
3559 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3560 doc: /* Evaluate EXP in the context of some activation frame.
3561 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3562 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3564 union specbinding *pdl = get_backtrace_frame (nframes, base);
3565 ptrdiff_t count = SPECPDL_INDEX ();
3566 ptrdiff_t distance = specpdl_ptr - pdl;
3567 eassert (distance >= 0);
3569 if (!backtrace_p (pdl))
3570 error ("Activation frame not found!");
3572 backtrace_eval_unrewind (distance);
3573 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3575 /* Use eval_sub rather than Feval since the main motivation behind
3576 backtrace-eval is to be able to get/set the value of lexical variables
3577 from the debugger. */
3578 return unbind_to (count, eval_sub (exp));
3581 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3582 doc: /* Return names and values of local variables of a stack frame.
3583 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3584 (Lisp_Object nframes, Lisp_Object base)
3586 union specbinding *frame = get_backtrace_frame (nframes, base);
3587 union specbinding *prevframe
3588 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3589 ptrdiff_t distance = specpdl_ptr - frame;
3590 Lisp_Object result = Qnil;
3591 eassert (distance >= 0);
3593 if (!backtrace_p (prevframe))
3594 error ("Activation frame not found!");
3595 if (!backtrace_p (frame))
3596 error ("Activation frame not found!");
3598 /* The specpdl entries normally contain the symbol being bound along with its
3599 `old_value', so it can be restored. The new value to which it is bound is
3600 available in one of two places: either in the current value of the
3601 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3602 next specpdl entry for it.
3603 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3604 and "new value", so we abuse it here, to fetch the new value.
3605 It's ugly (we'd rather not modify global data) and a bit inefficient,
3606 but it does the job for now. */
3607 backtrace_eval_unrewind (distance);
3609 /* Grab values. */
3611 union specbinding *tmp = prevframe;
3612 for (; tmp > frame; tmp--)
3614 switch (tmp->kind)
3616 case SPECPDL_LET:
3617 case SPECPDL_LET_DEFAULT:
3618 case SPECPDL_LET_LOCAL:
3620 Lisp_Object sym = specpdl_symbol (tmp);
3621 Lisp_Object val = specpdl_old_value (tmp);
3622 if (EQ (sym, Qinternal_interpreter_environment))
3624 Lisp_Object env = val;
3625 for (; CONSP (env); env = XCDR (env))
3627 Lisp_Object binding = XCAR (env);
3628 if (CONSP (binding))
3629 result = Fcons (Fcons (XCAR (binding),
3630 XCDR (binding)),
3631 result);
3634 else
3635 result = Fcons (Fcons (sym, val), result);
3641 /* Restore values from specpdl to original place. */
3642 backtrace_eval_unrewind (-distance);
3644 return result;
3648 void
3649 mark_specpdl (void)
3651 union specbinding *pdl;
3652 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3654 switch (pdl->kind)
3656 case SPECPDL_UNWIND:
3657 mark_object (specpdl_arg (pdl));
3658 break;
3660 case SPECPDL_BACKTRACE:
3662 ptrdiff_t nargs = backtrace_nargs (pdl);
3663 mark_object (backtrace_function (pdl));
3664 if (nargs == UNEVALLED)
3665 nargs = 1;
3666 while (nargs--)
3667 mark_object (backtrace_args (pdl)[nargs]);
3669 break;
3671 case SPECPDL_LET_DEFAULT:
3672 case SPECPDL_LET_LOCAL:
3673 mark_object (specpdl_where (pdl));
3674 /* Fall through. */
3675 case SPECPDL_LET:
3676 mark_object (specpdl_symbol (pdl));
3677 mark_object (specpdl_old_value (pdl));
3678 break;
3683 void
3684 get_backtrace (Lisp_Object array)
3686 union specbinding *pdl = backtrace_next (backtrace_top ());
3687 ptrdiff_t i = 0, asize = ASIZE (array);
3689 /* Copy the backtrace contents into working memory. */
3690 for (; i < asize; i++)
3692 if (backtrace_p (pdl))
3694 ASET (array, i, backtrace_function (pdl));
3695 pdl = backtrace_next (pdl);
3697 else
3698 ASET (array, i, Qnil);
3702 Lisp_Object backtrace_top_function (void)
3704 union specbinding *pdl = backtrace_top ();
3705 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3708 void
3709 syms_of_eval (void)
3711 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3712 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3713 If Lisp code tries to increase the total number past this amount,
3714 an error is signaled.
3715 You can safely use a value considerably larger than the default value,
3716 if that proves inconveniently small. However, if you increase it too far,
3717 Emacs could run out of memory trying to make the stack bigger. */);
3719 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3720 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3722 This limit serves to catch infinite recursions for you before they cause
3723 actual stack overflow in C, which would be fatal for Emacs.
3724 You can safely make it considerably larger than its default value,
3725 if that proves inconveniently small. However, if you increase it too far,
3726 Emacs could overflow the real C stack, and crash. */);
3728 DEFVAR_LISP ("quit-flag", Vquit_flag,
3729 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3730 If the value is t, that means do an ordinary quit.
3731 If the value equals `throw-on-input', that means quit by throwing
3732 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3733 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3734 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3735 Vquit_flag = Qnil;
3737 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3738 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3739 Note that `quit-flag' will still be set by typing C-g,
3740 so a quit will be signaled as soon as `inhibit-quit' is nil.
3741 To prevent this happening, set `quit-flag' to nil
3742 before making `inhibit-quit' nil. */);
3743 Vinhibit_quit = Qnil;
3745 DEFSYM (Qinhibit_quit, "inhibit-quit");
3746 DEFSYM (Qautoload, "autoload");
3747 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3748 DEFSYM (Qmacro, "macro");
3749 DEFSYM (Qdeclare, "declare");
3751 /* Note that the process handling also uses Qexit, but we don't want
3752 to staticpro it twice, so we just do it here. */
3753 DEFSYM (Qexit, "exit");
3755 DEFSYM (Qinteractive, "interactive");
3756 DEFSYM (Qcommandp, "commandp");
3757 DEFSYM (Qand_rest, "&rest");
3758 DEFSYM (Qand_optional, "&optional");
3759 DEFSYM (Qclosure, "closure");
3760 DEFSYM (Qdebug, "debug");
3762 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3763 doc: /* Non-nil means never enter the debugger.
3764 Normally set while the debugger is already active, to avoid recursive
3765 invocations. */);
3766 Vinhibit_debugger = Qnil;
3768 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3769 doc: /* Non-nil means enter debugger if an error is signaled.
3770 Does not apply to errors handled by `condition-case' or those
3771 matched by `debug-ignored-errors'.
3772 If the value is a list, an error only means to enter the debugger
3773 if one of its condition symbols appears in the list.
3774 When you evaluate an expression interactively, this variable
3775 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3776 The command `toggle-debug-on-error' toggles this.
3777 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3778 Vdebug_on_error = Qnil;
3780 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3781 doc: /* List of errors for which the debugger should not be called.
3782 Each element may be a condition-name or a regexp that matches error messages.
3783 If any element applies to a given error, that error skips the debugger
3784 and just returns to top level.
3785 This overrides the variable `debug-on-error'.
3786 It does not apply to errors handled by `condition-case'. */);
3787 Vdebug_ignored_errors = Qnil;
3789 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3790 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3791 Does not apply if quit is handled by a `condition-case'. */);
3792 debug_on_quit = 0;
3794 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3795 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3797 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3798 doc: /* Non-nil means debugger may continue execution.
3799 This is nil when the debugger is called under circumstances where it
3800 might not be safe to continue. */);
3801 debugger_may_continue = 1;
3803 DEFVAR_LISP ("debugger", Vdebugger,
3804 doc: /* Function to call to invoke debugger.
3805 If due to frame exit, args are `exit' and the value being returned;
3806 this function's value will be returned instead of that.
3807 If due to error, args are `error' and a list of the args to `signal'.
3808 If due to `apply' or `funcall' entry, one arg, `lambda'.
3809 If due to `eval' entry, one arg, t. */);
3810 Vdebugger = Qnil;
3812 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3813 doc: /* If non-nil, this is a function for `signal' to call.
3814 It receives the same arguments that `signal' was given.
3815 The Edebug package uses this to regain control. */);
3816 Vsignal_hook_function = Qnil;
3818 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3819 doc: /* Non-nil means call the debugger regardless of condition handlers.
3820 Note that `debug-on-error', `debug-on-quit' and friends
3821 still determine whether to handle the particular condition. */);
3822 Vdebug_on_signal = Qnil;
3824 /* When lexical binding is being used,
3825 Vinternal_interpreter_environment is non-nil, and contains an alist
3826 of lexically-bound variable, or (t), indicating an empty
3827 environment. The lisp name of this variable would be
3828 `internal-interpreter-environment' if it weren't hidden.
3829 Every element of this list can be either a cons (VAR . VAL)
3830 specifying a lexical binding, or a single symbol VAR indicating
3831 that this variable should use dynamic scoping. */
3832 DEFSYM (Qinternal_interpreter_environment,
3833 "internal-interpreter-environment");
3834 DEFVAR_LISP ("internal-interpreter-environment",
3835 Vinternal_interpreter_environment,
3836 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3837 When lexical binding is not being used, this variable is nil.
3838 A value of `(t)' indicates an empty environment, otherwise it is an
3839 alist of active lexical bindings. */);
3840 Vinternal_interpreter_environment = Qnil;
3841 /* Don't export this variable to Elisp, so no one can mess with it
3842 (Just imagine if someone makes it buffer-local). */
3843 Funintern (Qinternal_interpreter_environment, Qnil);
3845 DEFSYM (Vrun_hooks, "run-hooks");
3847 staticpro (&Vautoload_queue);
3848 Vautoload_queue = Qnil;
3849 staticpro (&Vsignaling_function);
3850 Vsignaling_function = Qnil;
3852 inhibit_lisp_code = Qnil;
3854 defsubr (&Sor);
3855 defsubr (&Sand);
3856 defsubr (&Sif);
3857 defsubr (&Scond);
3858 defsubr (&Sprogn);
3859 defsubr (&Sprog1);
3860 defsubr (&Sprog2);
3861 defsubr (&Ssetq);
3862 defsubr (&Squote);
3863 defsubr (&Sfunction);
3864 defsubr (&Sdefault_toplevel_value);
3865 defsubr (&Sset_default_toplevel_value);
3866 defsubr (&Sdefvar);
3867 defsubr (&Sdefvaralias);
3868 defsubr (&Sdefconst);
3869 defsubr (&Smake_var_non_special);
3870 defsubr (&Slet);
3871 defsubr (&SletX);
3872 defsubr (&Swhile);
3873 defsubr (&Smacroexpand);
3874 defsubr (&Scatch);
3875 defsubr (&Sthrow);
3876 defsubr (&Sunwind_protect);
3877 defsubr (&Scondition_case);
3878 defsubr (&Ssignal);
3879 defsubr (&Scommandp);
3880 defsubr (&Sautoload);
3881 defsubr (&Sautoload_do_load);
3882 defsubr (&Seval);
3883 defsubr (&Sapply);
3884 defsubr (&Sfuncall);
3885 defsubr (&Srun_hooks);
3886 defsubr (&Srun_hook_with_args);
3887 defsubr (&Srun_hook_with_args_until_success);
3888 defsubr (&Srun_hook_with_args_until_failure);
3889 defsubr (&Srun_hook_wrapped);
3890 defsubr (&Sfetch_bytecode);
3891 defsubr (&Sbacktrace_debug);
3892 defsubr (&Sbacktrace);
3893 defsubr (&Sbacktrace_frame);
3894 defsubr (&Sbacktrace_eval);
3895 defsubr (&Sbacktrace__locals);
3896 defsubr (&Sspecial_variable_p);
3897 defsubr (&Sfunctionp);