Comment formatting
[emacs.git] / src / eval.c
blob6e964f6604bed98ab3c258dba018eb618fa11ffc
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 #if !BYTE_MARK_STACK
36 static
37 #endif
38 struct catchtag *catchlist;
40 /* Chain of condition handlers currently in effect.
41 The elements of this chain are contained in the stack frames
42 of Fcondition_case and internal_condition_case.
43 When an error is signaled (by calling Fsignal, below),
44 this chain is searched for an element that applies. */
46 #if !BYTE_MARK_STACK
47 static
48 #endif
49 struct handler *handlerlist;
51 #ifdef DEBUG_GCPRO
52 /* Count levels of GCPRO to detect failure to UNGCPRO. */
53 int gcpro_level;
54 #endif
56 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57 Lisp_Object Qinhibit_quit;
58 Lisp_Object Qand_rest;
59 static Lisp_Object Qand_optional;
60 static Lisp_Object Qinhibit_debugger;
61 static Lisp_Object Qdeclare;
62 Lisp_Object Qinternal_interpreter_environment, Qclosure;
64 static Lisp_Object Qdebug;
66 /* This holds either the symbol `run-hooks' or nil.
67 It is nil at an early stage of startup, and when Emacs
68 is shutting down. */
70 Lisp_Object Vrun_hooks;
72 /* Non-nil means record all fset's and provide's, to be undone
73 if the file being autoloaded is not fully loaded.
74 They are recorded by being consed onto the front of Vautoload_queue:
75 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
77 Lisp_Object Vautoload_queue;
79 /* Current number of specbindings allocated in specpdl, not counting
80 the dummy entry specpdl[-1]. */
82 ptrdiff_t specpdl_size;
84 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
87 union specbinding *specpdl;
89 /* Pointer to first unused element in specpdl. */
91 union specbinding *specpdl_ptr;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
102 invocations. */
104 static EMACS_INT when_entered_debugger;
106 /* The function from which the last `signal' was called. Set in
107 Fsignal. */
108 /* FIXME: We should probably get rid of this! */
109 Lisp_Object Vsignaling_function;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code;
117 /* These would ordinarily be static, but they need to be visible to GDB. */
118 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
119 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
120 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
121 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
122 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
124 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
125 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
127 static Lisp_Object
128 specpdl_symbol (union specbinding *pdl)
130 eassert (pdl->kind >= SPECPDL_LET);
131 return pdl->let.symbol;
134 static Lisp_Object
135 specpdl_old_value (union specbinding *pdl)
137 eassert (pdl->kind >= SPECPDL_LET);
138 return pdl->let.old_value;
141 static void
142 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
144 eassert (pdl->kind >= SPECPDL_LET);
145 pdl->let.old_value = val;
148 static Lisp_Object
149 specpdl_where (union specbinding *pdl)
151 eassert (pdl->kind > SPECPDL_LET);
152 return pdl->let.where;
155 static Lisp_Object
156 specpdl_arg (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_UNWIND);
159 return pdl->unwind.arg;
162 Lisp_Object
163 backtrace_function (union specbinding *pdl)
165 eassert (pdl->kind == SPECPDL_BACKTRACE);
166 return pdl->bt.function;
169 static ptrdiff_t
170 backtrace_nargs (union specbinding *pdl)
172 eassert (pdl->kind == SPECPDL_BACKTRACE);
173 return pdl->bt.nargs;
176 Lisp_Object *
177 backtrace_args (union specbinding *pdl)
179 eassert (pdl->kind == SPECPDL_BACKTRACE);
180 return pdl->bt.args;
183 static bool
184 backtrace_debug_on_exit (union specbinding *pdl)
186 eassert (pdl->kind == SPECPDL_BACKTRACE);
187 return pdl->bt.debug_on_exit;
190 /* Functions to modify slots of backtrace records. */
192 static void
193 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
195 eassert (pdl->kind == SPECPDL_BACKTRACE);
196 pdl->bt.args = args;
199 static void
200 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
202 eassert (pdl->kind == SPECPDL_BACKTRACE);
203 pdl->bt.nargs = n;
206 static void
207 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
209 eassert (pdl->kind == SPECPDL_BACKTRACE);
210 pdl->bt.debug_on_exit = doe;
213 /* Helper functions to scan the backtrace. */
215 bool
216 backtrace_p (union specbinding *pdl)
217 { return pdl >= specpdl; }
219 union specbinding *
220 backtrace_top (void)
222 union specbinding *pdl = specpdl_ptr - 1;
223 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
224 pdl--;
225 return pdl;
228 union specbinding *
229 backtrace_next (union specbinding *pdl)
231 pdl--;
232 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
233 pdl--;
234 return pdl;
238 void
239 init_eval_once (void)
241 enum { size = 50 };
242 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
243 specpdl_size = size;
244 specpdl = specpdl_ptr = pdlvec + 1;
245 /* Don't forget to update docs (lispref node "Local Variables"). */
246 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
247 max_lisp_eval_depth = 600;
249 Vrun_hooks = Qnil;
252 void
253 init_eval (void)
255 specpdl_ptr = specpdl;
256 catchlist = 0;
257 handlerlist = 0;
258 Vquit_flag = Qnil;
259 debug_on_next_call = 0;
260 lisp_eval_depth = 0;
261 #ifdef DEBUG_GCPRO
262 gcpro_level = 0;
263 #endif
264 /* This is less than the initial value of num_nonmacro_input_events. */
265 when_entered_debugger = -1;
268 /* Unwind-protect function used by call_debugger. */
270 static void
271 restore_stack_limits (Lisp_Object data)
273 max_specpdl_size = XINT (XCAR (data));
274 max_lisp_eval_depth = XINT (XCDR (data));
277 /* Call the Lisp debugger, giving it argument ARG. */
279 Lisp_Object
280 call_debugger (Lisp_Object arg)
282 bool debug_while_redisplaying;
283 ptrdiff_t count = SPECPDL_INDEX ();
284 Lisp_Object val;
285 EMACS_INT old_max = max_specpdl_size;
287 /* Temporarily bump up the stack limits,
288 so the debugger won't run out of stack. */
290 max_specpdl_size += 1;
291 record_unwind_protect (restore_stack_limits,
292 Fcons (make_number (old_max),
293 make_number (max_lisp_eval_depth)));
294 max_specpdl_size = old_max;
296 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
297 max_lisp_eval_depth = lisp_eval_depth + 40;
299 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
300 max_specpdl_size = SPECPDL_INDEX () + 100;
302 #ifdef HAVE_WINDOW_SYSTEM
303 if (display_hourglass_p)
304 cancel_hourglass ();
305 #endif
307 debug_on_next_call = 0;
308 when_entered_debugger = num_nonmacro_input_events;
310 /* Resetting redisplaying_p to 0 makes sure that debug output is
311 displayed if the debugger is invoked during redisplay. */
312 debug_while_redisplaying = redisplaying_p;
313 redisplaying_p = 0;
314 specbind (intern ("debugger-may-continue"),
315 debug_while_redisplaying ? Qnil : Qt);
316 specbind (Qinhibit_redisplay, Qnil);
317 specbind (Qinhibit_debugger, Qt);
319 #if 0 /* Binding this prevents execution of Lisp code during
320 redisplay, which necessarily leads to display problems. */
321 specbind (Qinhibit_eval_during_redisplay, Qt);
322 #endif
324 val = apply1 (Vdebugger, arg);
326 /* Interrupting redisplay and resuming it later is not safe under
327 all circumstances. So, when the debugger returns, abort the
328 interrupted redisplay by going back to the top-level. */
329 if (debug_while_redisplaying)
330 Ftop_level ();
332 return unbind_to (count, val);
335 static void
336 do_debug_on_call (Lisp_Object code)
338 debug_on_next_call = 0;
339 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
340 call_debugger (list1 (code));
343 /* NOTE!!! Every function that can call EVAL must protect its args
344 and temporaries from garbage collection while it needs them.
345 The definition of `For' shows what you have to do. */
347 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
348 doc: /* Eval args until one of them yields non-nil, then return that value.
349 The remaining args are not evalled at all.
350 If all args return nil, return nil.
351 usage: (or CONDITIONS...) */)
352 (Lisp_Object args)
354 register Lisp_Object val = Qnil;
355 struct gcpro gcpro1;
357 GCPRO1 (args);
359 while (CONSP (args))
361 val = eval_sub (XCAR (args));
362 if (!NILP (val))
363 break;
364 args = XCDR (args);
367 UNGCPRO;
368 return val;
371 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
372 doc: /* Eval args until one of them yields nil, then return nil.
373 The remaining args are not evalled at all.
374 If no arg yields nil, return the last arg's value.
375 usage: (and CONDITIONS...) */)
376 (Lisp_Object args)
378 register Lisp_Object val = Qt;
379 struct gcpro gcpro1;
381 GCPRO1 (args);
383 while (CONSP (args))
385 val = eval_sub (XCAR (args));
386 if (NILP (val))
387 break;
388 args = XCDR (args);
391 UNGCPRO;
392 return val;
395 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
396 doc: /* If COND yields non-nil, do THEN, else do ELSE...
397 Returns the value of THEN or the value of the last of the ELSE's.
398 THEN must be one expression, but ELSE... can be zero or more expressions.
399 If COND yields nil, and there are no ELSE's, the value is nil.
400 usage: (if COND THEN ELSE...) */)
401 (Lisp_Object args)
403 Lisp_Object cond;
404 struct gcpro gcpro1;
406 GCPRO1 (args);
407 cond = eval_sub (XCAR (args));
408 UNGCPRO;
410 if (!NILP (cond))
411 return eval_sub (Fcar (XCDR (args)));
412 return Fprogn (XCDR (XCDR (args)));
415 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
416 doc: /* Try each clause until one succeeds.
417 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
418 and, if the value is non-nil, this clause succeeds:
419 then the expressions in BODY are evaluated and the last one's
420 value is the value of the cond-form.
421 If no clause succeeds, cond returns nil.
422 If a clause has one element, as in (CONDITION),
423 CONDITION's value if non-nil is returned from the cond-form.
424 usage: (cond CLAUSES...) */)
425 (Lisp_Object args)
427 Lisp_Object val = args;
428 struct gcpro gcpro1;
430 GCPRO1 (args);
431 while (CONSP (args))
433 Lisp_Object clause = XCAR (args);
434 val = eval_sub (Fcar (clause));
435 if (!NILP (val))
437 if (!NILP (XCDR (clause)))
438 val = Fprogn (XCDR (clause));
439 break;
441 args = XCDR (args);
443 UNGCPRO;
445 return val;
448 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
449 doc: /* Eval BODY forms sequentially and return value of last one.
450 usage: (progn BODY...) */)
451 (Lisp_Object body)
453 Lisp_Object val = Qnil;
454 struct gcpro gcpro1;
456 GCPRO1 (body);
458 while (CONSP (body))
460 val = eval_sub (XCAR (body));
461 body = XCDR (body);
464 UNGCPRO;
465 return val;
468 /* Evaluate BODY sequentially, discarding its value. Suitable for
469 record_unwind_protect. */
471 void
472 unwind_body (Lisp_Object body)
474 Fprogn (body);
477 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
478 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
479 The value of FIRST is saved during the evaluation of the remaining args,
480 whose values are discarded.
481 usage: (prog1 FIRST BODY...) */)
482 (Lisp_Object args)
484 Lisp_Object val;
485 Lisp_Object args_left;
486 struct gcpro gcpro1, gcpro2;
488 args_left = args;
489 val = args;
490 GCPRO2 (args, val);
492 val = eval_sub (XCAR (args_left));
493 while (CONSP (args_left = XCDR (args_left)))
494 eval_sub (XCAR (args_left));
496 UNGCPRO;
497 return val;
500 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
501 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
502 The value of FORM2 is saved during the evaluation of the
503 remaining args, whose values are discarded.
504 usage: (prog2 FORM1 FORM2 BODY...) */)
505 (Lisp_Object args)
507 struct gcpro gcpro1;
509 GCPRO1 (args);
510 eval_sub (XCAR (args));
511 UNGCPRO;
512 return Fprog1 (XCDR (args));
515 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
516 doc: /* Set each SYM to the value of its VAL.
517 The symbols SYM are variables; they are literal (not evaluated).
518 The values VAL are expressions; they are evaluated.
519 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
520 The second VAL is not computed until after the first SYM is set, and so on;
521 each VAL can use the new value of variables set earlier in the `setq'.
522 The return value of the `setq' form is the value of the last VAL.
523 usage: (setq [SYM VAL]...) */)
524 (Lisp_Object args)
526 Lisp_Object val, sym, lex_binding;
528 val = args;
529 if (CONSP (args))
531 Lisp_Object args_left = args;
532 struct gcpro gcpro1;
533 GCPRO1 (args);
537 val = eval_sub (Fcar (XCDR (args_left)));
538 sym = XCAR (args_left);
540 /* Like for eval_sub, we do not check declared_special here since
541 it's been done when let-binding. */
542 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
543 && SYMBOLP (sym)
544 && !NILP (lex_binding
545 = Fassq (sym, Vinternal_interpreter_environment)))
546 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
547 else
548 Fset (sym, val); /* SYM is dynamically bound. */
550 args_left = Fcdr (XCDR (args_left));
552 while (CONSP (args_left));
554 UNGCPRO;
557 return val;
560 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
561 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
562 Warning: `quote' does not construct its return value, but just returns
563 the value that was pre-constructed by the Lisp reader (see info node
564 `(elisp)Printed Representation').
565 This means that '(a . b) is not identical to (cons 'a 'b): the former
566 does not cons. Quoting should be reserved for constants that will
567 never be modified by side-effects, unless you like self-modifying code.
568 See the common pitfall in info node `(elisp)Rearrangement' for an example
569 of unexpected results when a quoted object is modified.
570 usage: (quote ARG) */)
571 (Lisp_Object args)
573 if (CONSP (XCDR (args)))
574 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
575 return XCAR (args);
578 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
579 doc: /* Like `quote', but preferred for objects which are functions.
580 In byte compilation, `function' causes its argument to be compiled.
581 `quote' cannot do that.
582 usage: (function ARG) */)
583 (Lisp_Object args)
585 Lisp_Object quoted = XCAR (args);
587 if (CONSP (XCDR (args)))
588 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
590 if (!NILP (Vinternal_interpreter_environment)
591 && CONSP (quoted)
592 && EQ (XCAR (quoted), Qlambda))
593 /* This is a lambda expression within a lexical environment;
594 return an interpreted closure instead of a simple lambda. */
595 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
596 XCDR (quoted)));
597 else
598 /* Simply quote the argument. */
599 return quoted;
603 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
604 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
605 Aliased variables always have the same value; setting one sets the other.
606 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
607 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
608 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
609 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
610 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
611 The return value is BASE-VARIABLE. */)
612 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
614 struct Lisp_Symbol *sym;
616 CHECK_SYMBOL (new_alias);
617 CHECK_SYMBOL (base_variable);
619 sym = XSYMBOL (new_alias);
621 if (sym->constant)
622 /* Not sure why, but why not? */
623 error ("Cannot make a constant an alias");
625 switch (sym->redirect)
627 case SYMBOL_FORWARDED:
628 error ("Cannot make an internal variable an alias");
629 case SYMBOL_LOCALIZED:
630 error ("Don't know how to make a localized variable an alias");
633 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
634 If n_a is bound, but b_v is not, set the value of b_v to n_a,
635 so that old-code that affects n_a before the aliasing is setup
636 still works. */
637 if (NILP (Fboundp (base_variable)))
638 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
641 union specbinding *p;
643 for (p = specpdl_ptr; p > specpdl; )
644 if ((--p)->kind >= SPECPDL_LET
645 && (EQ (new_alias, specpdl_symbol (p))))
646 error ("Don't know how to make a let-bound variable an alias");
649 sym->declared_special = 1;
650 XSYMBOL (base_variable)->declared_special = 1;
651 sym->redirect = SYMBOL_VARALIAS;
652 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
653 sym->constant = SYMBOL_CONSTANT_P (base_variable);
654 LOADHIST_ATTACH (new_alias);
655 /* Even if docstring is nil: remove old docstring. */
656 Fput (new_alias, Qvariable_documentation, docstring);
658 return base_variable;
661 static union specbinding *
662 default_toplevel_binding (Lisp_Object symbol)
664 union specbinding *binding = NULL;
665 union specbinding *pdl = specpdl_ptr;
666 while (pdl > specpdl)
668 switch ((--pdl)->kind)
670 case SPECPDL_LET_DEFAULT:
671 case SPECPDL_LET:
672 if (EQ (specpdl_symbol (pdl), symbol))
673 binding = pdl;
674 break;
677 return binding;
680 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
681 doc: /* Return SYMBOL's toplevel default value.
682 "Toplevel" means outside of any let binding. */)
683 (Lisp_Object symbol)
685 union specbinding *binding = default_toplevel_binding (symbol);
686 Lisp_Object value
687 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
688 if (!EQ (value, Qunbound))
689 return value;
690 xsignal1 (Qvoid_variable, symbol);
693 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
694 Sset_default_toplevel_value, 2, 2, 0,
695 doc: /* Set SYMBOL's toplevel default value to VALUE.
696 "Toplevel" means outside of any let binding. */)
697 (Lisp_Object symbol, Lisp_Object value)
699 union specbinding *binding = default_toplevel_binding (symbol);
700 if (binding)
701 set_specpdl_old_value (binding, value);
702 else
703 Fset_default (symbol, value);
704 return Qnil;
707 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
708 doc: /* Define SYMBOL as a variable, and return SYMBOL.
709 You are not required to define a variable in order to use it, but
710 defining it lets you supply an initial value and documentation, which
711 can be referred to by the Emacs help facilities and other programming
712 tools. The `defvar' form also declares the variable as \"special\",
713 so that it is always dynamically bound even if `lexical-binding' is t.
715 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
716 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
717 default value is what is set; buffer-local values are not affected.
718 If INITVALUE is missing, SYMBOL's value is not set.
720 If SYMBOL has a local binding, then this form affects the local
721 binding. This is usually not what you want. Thus, if you need to
722 load a file defining variables, with this form or with `defconst' or
723 `defcustom', you should always load that file _outside_ any bindings
724 for these variables. \(`defconst' and `defcustom' behave similarly in
725 this respect.)
727 The optional argument DOCSTRING is a documentation string for the
728 variable.
730 To define a user option, use `defcustom' instead of `defvar'.
731 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
732 (Lisp_Object args)
734 Lisp_Object sym, tem, tail;
736 sym = XCAR (args);
737 tail = XCDR (args);
739 if (CONSP (tail))
741 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
742 error ("Too many arguments");
744 tem = Fdefault_boundp (sym);
746 /* Do it before evaluating the initial value, for self-references. */
747 XSYMBOL (sym)->declared_special = 1;
749 if (NILP (tem))
750 Fset_default (sym, eval_sub (XCAR (tail)));
751 else
752 { /* Check if there is really a global binding rather than just a let
753 binding that shadows the global unboundness of the var. */
754 union specbinding *binding = default_toplevel_binding (sym);
755 if (binding && EQ (specpdl_old_value (binding), Qunbound))
757 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
760 tail = XCDR (tail);
761 tem = Fcar (tail);
762 if (!NILP (tem))
764 if (!NILP (Vpurify_flag))
765 tem = Fpurecopy (tem);
766 Fput (sym, Qvariable_documentation, tem);
768 LOADHIST_ATTACH (sym);
770 else if (!NILP (Vinternal_interpreter_environment)
771 && !XSYMBOL (sym)->declared_special)
772 /* A simple (defvar foo) with lexical scoping does "nothing" except
773 declare that var to be dynamically scoped *locally* (i.e. within
774 the current file or let-block). */
775 Vinternal_interpreter_environment
776 = Fcons (sym, Vinternal_interpreter_environment);
777 else
779 /* Simple (defvar <var>) should not count as a definition at all.
780 It could get in the way of other definitions, and unloading this
781 package could try to make the variable unbound. */
784 return sym;
787 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
788 doc: /* Define SYMBOL as a constant variable.
789 This declares that neither programs nor users should ever change the
790 value. This constancy is not actually enforced by Emacs Lisp, but
791 SYMBOL is marked as a special variable so that it is never lexically
792 bound.
794 The `defconst' form always sets the value of SYMBOL to the result of
795 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
796 what is set; buffer-local values are not affected. If SYMBOL has a
797 local binding, then this form sets the local binding's value.
798 However, you should normally not make local bindings for variables
799 defined with this form.
801 The optional DOCSTRING specifies the variable's documentation string.
802 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
803 (Lisp_Object args)
805 Lisp_Object sym, tem;
807 sym = XCAR (args);
808 if (CONSP (Fcdr (XCDR (XCDR (args)))))
809 error ("Too many arguments");
811 tem = eval_sub (Fcar (XCDR (args)));
812 if (!NILP (Vpurify_flag))
813 tem = Fpurecopy (tem);
814 Fset_default (sym, tem);
815 XSYMBOL (sym)->declared_special = 1;
816 tem = Fcar (XCDR (XCDR (args)));
817 if (!NILP (tem))
819 if (!NILP (Vpurify_flag))
820 tem = Fpurecopy (tem);
821 Fput (sym, Qvariable_documentation, tem);
823 Fput (sym, Qrisky_local_variable, Qt);
824 LOADHIST_ATTACH (sym);
825 return sym;
828 /* Make SYMBOL lexically scoped. */
829 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
830 Smake_var_non_special, 1, 1, 0,
831 doc: /* Internal function. */)
832 (Lisp_Object symbol)
834 CHECK_SYMBOL (symbol);
835 XSYMBOL (symbol)->declared_special = 0;
836 return Qnil;
840 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
841 doc: /* Bind variables according to VARLIST then eval BODY.
842 The value of the last form in BODY is returned.
843 Each element of VARLIST is a symbol (which is bound to nil)
844 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
845 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
846 usage: (let* VARLIST BODY...) */)
847 (Lisp_Object args)
849 Lisp_Object varlist, var, val, elt, lexenv;
850 ptrdiff_t count = SPECPDL_INDEX ();
851 struct gcpro gcpro1, gcpro2, gcpro3;
853 GCPRO3 (args, elt, varlist);
855 lexenv = Vinternal_interpreter_environment;
857 varlist = XCAR (args);
858 while (CONSP (varlist))
860 QUIT;
862 elt = XCAR (varlist);
863 if (SYMBOLP (elt))
865 var = elt;
866 val = Qnil;
868 else if (! NILP (Fcdr (Fcdr (elt))))
869 signal_error ("`let' bindings can have only one value-form", elt);
870 else
872 var = Fcar (elt);
873 val = eval_sub (Fcar (Fcdr (elt)));
876 if (!NILP (lexenv) && SYMBOLP (var)
877 && !XSYMBOL (var)->declared_special
878 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
879 /* Lexically bind VAR by adding it to the interpreter's binding
880 alist. */
882 Lisp_Object newenv
883 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
884 if (EQ (Vinternal_interpreter_environment, lexenv))
885 /* Save the old lexical environment on the specpdl stack,
886 but only for the first lexical binding, since we'll never
887 need to revert to one of the intermediate ones. */
888 specbind (Qinternal_interpreter_environment, newenv);
889 else
890 Vinternal_interpreter_environment = newenv;
892 else
893 specbind (var, val);
895 varlist = XCDR (varlist);
897 UNGCPRO;
898 val = Fprogn (XCDR (args));
899 return unbind_to (count, val);
902 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
903 doc: /* Bind variables according to VARLIST then eval BODY.
904 The value of the last form in BODY is returned.
905 Each element of VARLIST is a symbol (which is bound to nil)
906 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
907 All the VALUEFORMs are evalled before any symbols are bound.
908 usage: (let VARLIST BODY...) */)
909 (Lisp_Object args)
911 Lisp_Object *temps, tem, lexenv;
912 register Lisp_Object elt, varlist;
913 ptrdiff_t count = SPECPDL_INDEX ();
914 ptrdiff_t argnum;
915 struct gcpro gcpro1, gcpro2;
916 USE_SAFE_ALLOCA;
918 varlist = XCAR (args);
920 /* Make space to hold the values to give the bound variables. */
921 elt = Flength (varlist);
922 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
924 /* Compute the values and store them in `temps'. */
926 GCPRO2 (args, *temps);
927 gcpro2.nvars = 0;
929 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
931 QUIT;
932 elt = XCAR (varlist);
933 if (SYMBOLP (elt))
934 temps [argnum++] = Qnil;
935 else if (! NILP (Fcdr (Fcdr (elt))))
936 signal_error ("`let' bindings can have only one value-form", elt);
937 else
938 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
939 gcpro2.nvars = argnum;
941 UNGCPRO;
943 lexenv = Vinternal_interpreter_environment;
945 varlist = XCAR (args);
946 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
948 Lisp_Object var;
950 elt = XCAR (varlist);
951 var = SYMBOLP (elt) ? elt : Fcar (elt);
952 tem = temps[argnum++];
954 if (!NILP (lexenv) && SYMBOLP (var)
955 && !XSYMBOL (var)->declared_special
956 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
957 /* Lexically bind VAR by adding it to the lexenv alist. */
958 lexenv = Fcons (Fcons (var, tem), lexenv);
959 else
960 /* Dynamically bind VAR. */
961 specbind (var, tem);
964 if (!EQ (lexenv, Vinternal_interpreter_environment))
965 /* Instantiate a new lexical environment. */
966 specbind (Qinternal_interpreter_environment, lexenv);
968 elt = Fprogn (XCDR (args));
969 SAFE_FREE ();
970 return unbind_to (count, elt);
973 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
974 doc: /* If TEST yields non-nil, eval BODY... and repeat.
975 The order of execution is thus TEST, BODY, TEST, BODY and so on
976 until TEST returns nil.
977 usage: (while TEST BODY...) */)
978 (Lisp_Object args)
980 Lisp_Object test, body;
981 struct gcpro gcpro1, gcpro2;
983 GCPRO2 (test, body);
985 test = XCAR (args);
986 body = XCDR (args);
987 while (!NILP (eval_sub (test)))
989 QUIT;
990 Fprogn (body);
993 UNGCPRO;
994 return Qnil;
997 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
998 doc: /* Return result of expanding macros at top level of FORM.
999 If FORM is not a macro call, it is returned unchanged.
1000 Otherwise, the macro is expanded and the expansion is considered
1001 in place of FORM. When a non-macro-call results, it is returned.
1003 The second optional arg ENVIRONMENT specifies an environment of macro
1004 definitions to shadow the loaded ones for use in file byte-compilation. */)
1005 (Lisp_Object form, Lisp_Object environment)
1007 /* With cleanups from Hallvard Furuseth. */
1008 register Lisp_Object expander, sym, def, tem;
1010 while (1)
1012 /* Come back here each time we expand a macro call,
1013 in case it expands into another macro call. */
1014 if (!CONSP (form))
1015 break;
1016 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1017 def = sym = XCAR (form);
1018 tem = Qnil;
1019 /* Trace symbols aliases to other symbols
1020 until we get a symbol that is not an alias. */
1021 while (SYMBOLP (def))
1023 QUIT;
1024 sym = def;
1025 tem = Fassq (sym, environment);
1026 if (NILP (tem))
1028 def = XSYMBOL (sym)->function;
1029 if (!NILP (def))
1030 continue;
1032 break;
1034 /* Right now TEM is the result from SYM in ENVIRONMENT,
1035 and if TEM is nil then DEF is SYM's function definition. */
1036 if (NILP (tem))
1038 /* SYM is not mentioned in ENVIRONMENT.
1039 Look at its function definition. */
1040 struct gcpro gcpro1;
1041 GCPRO1 (form);
1042 def = Fautoload_do_load (def, sym, Qmacro);
1043 UNGCPRO;
1044 if (!CONSP (def))
1045 /* Not defined or definition not suitable. */
1046 break;
1047 if (!EQ (XCAR (def), Qmacro))
1048 break;
1049 else expander = XCDR (def);
1051 else
1053 expander = XCDR (tem);
1054 if (NILP (expander))
1055 break;
1058 Lisp_Object newform = apply1 (expander, XCDR (form));
1059 if (EQ (form, newform))
1060 break;
1061 else
1062 form = newform;
1065 return form;
1068 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1069 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1070 TAG is evalled to get the tag to use; it must not be nil.
1072 Then the BODY is executed.
1073 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1074 If no throw happens, `catch' returns the value of the last BODY form.
1075 If a throw happens, it specifies the value to return from `catch'.
1076 usage: (catch TAG BODY...) */)
1077 (Lisp_Object args)
1079 register Lisp_Object tag;
1080 struct gcpro gcpro1;
1082 GCPRO1 (args);
1083 tag = eval_sub (XCAR (args));
1084 UNGCPRO;
1085 return internal_catch (tag, Fprogn, XCDR (args));
1088 /* Set up a catch, then call C function FUNC on argument ARG.
1089 FUNC should return a Lisp_Object.
1090 This is how catches are done from within C code. */
1092 Lisp_Object
1093 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1095 /* This structure is made part of the chain `catchlist'. */
1096 struct catchtag c;
1098 /* Fill in the components of c, and put it on the list. */
1099 c.next = catchlist;
1100 c.tag = tag;
1101 c.val = Qnil;
1102 c.handlerlist = handlerlist;
1103 c.lisp_eval_depth = lisp_eval_depth;
1104 c.pdlcount = SPECPDL_INDEX ();
1105 c.poll_suppress_count = poll_suppress_count;
1106 c.interrupt_input_blocked = interrupt_input_blocked;
1107 c.gcpro = gcprolist;
1108 c.byte_stack = byte_stack_list;
1109 catchlist = &c;
1111 /* Call FUNC. */
1112 if (! sys_setjmp (c.jmp))
1113 c.val = (*func) (arg);
1115 /* Throw works by a longjmp that comes right here. */
1116 catchlist = c.next;
1117 return c.val;
1120 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1121 jump to that CATCH, returning VALUE as the value of that catch.
1123 This is the guts of Fthrow and Fsignal; they differ only in the way
1124 they choose the catch tag to throw to. A catch tag for a
1125 condition-case form has a TAG of Qnil.
1127 Before each catch is discarded, unbind all special bindings and
1128 execute all unwind-protect clauses made above that catch. Unwind
1129 the handler stack as we go, so that the proper handlers are in
1130 effect for each unwind-protect clause we run. At the end, restore
1131 some static info saved in CATCH, and longjmp to the location
1132 specified there.
1134 This is used for correct unwinding in Fthrow and Fsignal. */
1136 static _Noreturn void
1137 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1139 bool last_time;
1141 /* Save the value in the tag. */
1142 catch->val = value;
1144 /* Restore certain special C variables. */
1145 set_poll_suppress_count (catch->poll_suppress_count);
1146 unblock_input_to (catch->interrupt_input_blocked);
1147 immediate_quit = 0;
1151 last_time = catchlist == catch;
1153 /* Unwind the specpdl stack, and then restore the proper set of
1154 handlers. */
1155 unbind_to (catchlist->pdlcount, Qnil);
1156 handlerlist = catchlist->handlerlist;
1157 catchlist = catchlist->next;
1159 while (! last_time);
1161 byte_stack_list = catch->byte_stack;
1162 gcprolist = catch->gcpro;
1163 #ifdef DEBUG_GCPRO
1164 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1165 #endif
1166 lisp_eval_depth = catch->lisp_eval_depth;
1168 sys_longjmp (catch->jmp, 1);
1171 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1172 doc: /* Throw to the catch for TAG and return VALUE from it.
1173 Both TAG and VALUE are evalled. */)
1174 (register Lisp_Object tag, Lisp_Object value)
1176 register struct catchtag *c;
1178 if (!NILP (tag))
1179 for (c = catchlist; c; c = c->next)
1181 if (EQ (c->tag, tag))
1182 unwind_to_catch (c, value);
1184 xsignal2 (Qno_catch, tag, value);
1188 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1189 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1190 If BODYFORM completes normally, its value is returned
1191 after executing the UNWINDFORMS.
1192 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1193 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1194 (Lisp_Object args)
1196 Lisp_Object val;
1197 ptrdiff_t count = SPECPDL_INDEX ();
1199 record_unwind_protect (unwind_body, XCDR (args));
1200 val = eval_sub (XCAR (args));
1201 return unbind_to (count, val);
1204 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1205 doc: /* Regain control when an error is signaled.
1206 Executes BODYFORM and returns its value if no error happens.
1207 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1208 where the BODY is made of Lisp expressions.
1210 A handler is applicable to an error
1211 if CONDITION-NAME is one of the error's condition names.
1212 If an error happens, the first applicable handler is run.
1214 The car of a handler may be a list of condition names instead of a
1215 single condition name; then it handles all of them. If the special
1216 condition name `debug' is present in this list, it allows another
1217 condition in the list to run the debugger if `debug-on-error' and the
1218 other usual mechanisms says it should (otherwise, `condition-case'
1219 suppresses the debugger).
1221 When a handler handles an error, control returns to the `condition-case'
1222 and it executes the handler's BODY...
1223 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1224 \(If VAR is nil, the handler can't access that information.)
1225 Then the value of the last BODY form is returned from the `condition-case'
1226 expression.
1228 See also the function `signal' for more info.
1229 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1230 (Lisp_Object args)
1232 Lisp_Object var = XCAR (args);
1233 Lisp_Object bodyform = XCAR (XCDR (args));
1234 Lisp_Object handlers = XCDR (XCDR (args));
1236 return internal_lisp_condition_case (var, bodyform, handlers);
1239 /* Like Fcondition_case, but the args are separate
1240 rather than passed in a list. Used by Fbyte_code. */
1242 Lisp_Object
1243 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1244 Lisp_Object handlers)
1246 Lisp_Object val;
1247 struct catchtag c;
1248 struct handler h;
1250 CHECK_SYMBOL (var);
1252 for (val = handlers; CONSP (val); val = XCDR (val))
1254 Lisp_Object tem;
1255 tem = XCAR (val);
1256 if (! (NILP (tem)
1257 || (CONSP (tem)
1258 && (SYMBOLP (XCAR (tem))
1259 || CONSP (XCAR (tem))))))
1260 error ("Invalid condition handler: %s",
1261 SDATA (Fprin1_to_string (tem, Qt)));
1264 c.tag = Qnil;
1265 c.val = Qnil;
1266 c.handlerlist = handlerlist;
1267 c.lisp_eval_depth = lisp_eval_depth;
1268 c.pdlcount = SPECPDL_INDEX ();
1269 c.poll_suppress_count = poll_suppress_count;
1270 c.interrupt_input_blocked = interrupt_input_blocked;
1271 c.gcpro = gcprolist;
1272 c.byte_stack = byte_stack_list;
1273 if (sys_setjmp (c.jmp))
1275 if (!NILP (h.var))
1276 specbind (h.var, c.val);
1277 val = Fprogn (Fcdr (h.chosen_clause));
1279 /* Note that this just undoes the binding of h.var; whoever
1280 longjumped to us unwound the stack to c.pdlcount before
1281 throwing. */
1282 unbind_to (c.pdlcount, Qnil);
1283 return val;
1285 c.next = catchlist;
1286 catchlist = &c;
1288 h.var = var;
1289 h.handler = handlers;
1290 h.next = handlerlist;
1291 h.tag = &c;
1292 handlerlist = &h;
1294 val = eval_sub (bodyform);
1295 catchlist = c.next;
1296 handlerlist = h.next;
1297 return val;
1300 /* Call the function BFUN with no arguments, catching errors within it
1301 according to HANDLERS. If there is an error, call HFUN with
1302 one argument which is the data that describes the error:
1303 (SIGNALNAME . DATA)
1305 HANDLERS can be a list of conditions to catch.
1306 If HANDLERS is Qt, catch all errors.
1307 If HANDLERS is Qerror, catch all errors
1308 but allow the debugger to run if that is enabled. */
1310 Lisp_Object
1311 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1312 Lisp_Object (*hfun) (Lisp_Object))
1314 Lisp_Object val;
1315 struct catchtag c;
1316 struct handler h;
1318 c.tag = Qnil;
1319 c.val = Qnil;
1320 c.handlerlist = handlerlist;
1321 c.lisp_eval_depth = lisp_eval_depth;
1322 c.pdlcount = SPECPDL_INDEX ();
1323 c.poll_suppress_count = poll_suppress_count;
1324 c.interrupt_input_blocked = interrupt_input_blocked;
1325 c.gcpro = gcprolist;
1326 c.byte_stack = byte_stack_list;
1327 if (sys_setjmp (c.jmp))
1329 return (*hfun) (c.val);
1331 c.next = catchlist;
1332 catchlist = &c;
1333 h.handler = handlers;
1334 h.var = Qnil;
1335 h.next = handlerlist;
1336 h.tag = &c;
1337 handlerlist = &h;
1339 val = (*bfun) ();
1340 catchlist = c.next;
1341 handlerlist = h.next;
1342 return val;
1345 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1347 Lisp_Object
1348 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1349 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1351 Lisp_Object val;
1352 struct catchtag c;
1353 struct handler h;
1355 c.tag = Qnil;
1356 c.val = Qnil;
1357 c.handlerlist = handlerlist;
1358 c.lisp_eval_depth = lisp_eval_depth;
1359 c.pdlcount = SPECPDL_INDEX ();
1360 c.poll_suppress_count = poll_suppress_count;
1361 c.interrupt_input_blocked = interrupt_input_blocked;
1362 c.gcpro = gcprolist;
1363 c.byte_stack = byte_stack_list;
1364 if (sys_setjmp (c.jmp))
1366 return (*hfun) (c.val);
1368 c.next = catchlist;
1369 catchlist = &c;
1370 h.handler = handlers;
1371 h.var = Qnil;
1372 h.next = handlerlist;
1373 h.tag = &c;
1374 handlerlist = &h;
1376 val = (*bfun) (arg);
1377 catchlist = c.next;
1378 handlerlist = h.next;
1379 return val;
1382 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1383 its arguments. */
1385 Lisp_Object
1386 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1387 Lisp_Object arg1,
1388 Lisp_Object arg2,
1389 Lisp_Object handlers,
1390 Lisp_Object (*hfun) (Lisp_Object))
1392 Lisp_Object val;
1393 struct catchtag c;
1394 struct handler h;
1396 c.tag = Qnil;
1397 c.val = Qnil;
1398 c.handlerlist = handlerlist;
1399 c.lisp_eval_depth = lisp_eval_depth;
1400 c.pdlcount = SPECPDL_INDEX ();
1401 c.poll_suppress_count = poll_suppress_count;
1402 c.interrupt_input_blocked = interrupt_input_blocked;
1403 c.gcpro = gcprolist;
1404 c.byte_stack = byte_stack_list;
1405 if (sys_setjmp (c.jmp))
1407 return (*hfun) (c.val);
1409 c.next = catchlist;
1410 catchlist = &c;
1411 h.handler = handlers;
1412 h.var = Qnil;
1413 h.next = handlerlist;
1414 h.tag = &c;
1415 handlerlist = &h;
1417 val = (*bfun) (arg1, arg2);
1418 catchlist = c.next;
1419 handlerlist = h.next;
1420 return val;
1423 /* Like internal_condition_case but call BFUN with NARGS as first,
1424 and ARGS as second argument. */
1426 Lisp_Object
1427 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1428 ptrdiff_t nargs,
1429 Lisp_Object *args,
1430 Lisp_Object handlers,
1431 Lisp_Object (*hfun) (Lisp_Object err,
1432 ptrdiff_t nargs,
1433 Lisp_Object *args))
1435 Lisp_Object val;
1436 struct catchtag c;
1437 struct handler h;
1439 c.tag = Qnil;
1440 c.val = Qnil;
1441 c.handlerlist = handlerlist;
1442 c.lisp_eval_depth = lisp_eval_depth;
1443 c.pdlcount = SPECPDL_INDEX ();
1444 c.poll_suppress_count = poll_suppress_count;
1445 c.interrupt_input_blocked = interrupt_input_blocked;
1446 c.gcpro = gcprolist;
1447 c.byte_stack = byte_stack_list;
1448 if (sys_setjmp (c.jmp))
1450 return (*hfun) (c.val, nargs, args);
1452 c.next = catchlist;
1453 catchlist = &c;
1454 h.handler = handlers;
1455 h.var = Qnil;
1456 h.next = handlerlist;
1457 h.tag = &c;
1458 handlerlist = &h;
1460 val = (*bfun) (nargs, args);
1461 catchlist = c.next;
1462 handlerlist = h.next;
1463 return val;
1467 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1468 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1469 Lisp_Object data);
1471 void
1472 process_quit_flag (void)
1474 Lisp_Object flag = Vquit_flag;
1475 Vquit_flag = Qnil;
1476 if (EQ (flag, Qkill_emacs))
1477 Fkill_emacs (Qnil);
1478 if (EQ (Vthrow_on_input, flag))
1479 Fthrow (Vthrow_on_input, Qt);
1480 Fsignal (Qquit, Qnil);
1483 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1484 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1485 This function does not return.
1487 An error symbol is a symbol with an `error-conditions' property
1488 that is a list of condition names.
1489 A handler for any of those names will get to handle this signal.
1490 The symbol `error' should normally be one of them.
1492 DATA should be a list. Its elements are printed as part of the error message.
1493 See Info anchor `(elisp)Definition of signal' for some details on how this
1494 error message is constructed.
1495 If the signal is handled, DATA is made available to the handler.
1496 See also the function `condition-case'. */)
1497 (Lisp_Object error_symbol, Lisp_Object data)
1499 /* When memory is full, ERROR-SYMBOL is nil,
1500 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1501 That is a special case--don't do this in other situations. */
1502 Lisp_Object conditions;
1503 Lisp_Object string;
1504 Lisp_Object real_error_symbol
1505 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1506 register Lisp_Object clause = Qnil;
1507 struct handler *h;
1509 immediate_quit = 0;
1510 abort_on_gc = 0;
1511 if (gc_in_progress || waiting_for_input)
1512 emacs_abort ();
1514 #if 0 /* rms: I don't know why this was here,
1515 but it is surely wrong for an error that is handled. */
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (display_hourglass_p)
1518 cancel_hourglass ();
1519 #endif
1520 #endif
1522 /* This hook is used by edebug. */
1523 if (! NILP (Vsignal_hook_function)
1524 && ! NILP (error_symbol))
1526 /* Edebug takes care of restoring these variables when it exits. */
1527 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1528 max_lisp_eval_depth = lisp_eval_depth + 20;
1530 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1531 max_specpdl_size = SPECPDL_INDEX () + 40;
1533 call2 (Vsignal_hook_function, error_symbol, data);
1536 conditions = Fget (real_error_symbol, Qerror_conditions);
1538 /* Remember from where signal was called. Skip over the frame for
1539 `signal' itself. If a frame for `error' follows, skip that,
1540 too. Don't do this when ERROR_SYMBOL is nil, because that
1541 is a memory-full error. */
1542 Vsignaling_function = Qnil;
1543 if (!NILP (error_symbol))
1545 union specbinding *pdl = backtrace_next (backtrace_top ());
1546 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1547 pdl = backtrace_next (pdl);
1548 if (backtrace_p (pdl))
1549 Vsignaling_function = backtrace_function (pdl);
1552 for (h = handlerlist; h; h = h->next)
1554 clause = find_handler_clause (h->handler, conditions);
1555 if (!NILP (clause))
1556 break;
1559 if (/* Don't run the debugger for a memory-full error.
1560 (There is no room in memory to do that!) */
1561 !NILP (error_symbol)
1562 && (!NILP (Vdebug_on_signal)
1563 /* If no handler is present now, try to run the debugger. */
1564 || NILP (clause)
1565 /* A `debug' symbol in the handler list disables the normal
1566 suppression of the debugger. */
1567 || (CONSP (clause) && CONSP (XCAR (clause))
1568 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1569 /* Special handler that means "print a message and run debugger
1570 if requested". */
1571 || EQ (h->handler, Qerror)))
1573 bool debugger_called
1574 = maybe_call_debugger (conditions, error_symbol, data);
1575 /* We can't return values to code which signaled an error, but we
1576 can continue code which has signaled a quit. */
1577 if (debugger_called && EQ (real_error_symbol, Qquit))
1578 return Qnil;
1581 if (!NILP (clause))
1583 Lisp_Object unwind_data
1584 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1586 h->chosen_clause = clause;
1587 unwind_to_catch (h->tag, unwind_data);
1589 else
1591 if (catchlist != 0)
1592 Fthrow (Qtop_level, Qt);
1595 if (! NILP (error_symbol))
1596 data = Fcons (error_symbol, data);
1598 string = Ferror_message_string (data);
1599 fatal ("%s", SDATA (string));
1602 /* Internal version of Fsignal that never returns.
1603 Used for anything but Qquit (which can return from Fsignal). */
1605 void
1606 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1608 Fsignal (error_symbol, data);
1609 emacs_abort ();
1612 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1614 void
1615 xsignal0 (Lisp_Object error_symbol)
1617 xsignal (error_symbol, Qnil);
1620 void
1621 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1623 xsignal (error_symbol, list1 (arg));
1626 void
1627 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1629 xsignal (error_symbol, list2 (arg1, arg2));
1632 void
1633 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1635 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1638 /* Signal `error' with message S, and additional arg ARG.
1639 If ARG is not a genuine list, make it a one-element list. */
1641 void
1642 signal_error (const char *s, Lisp_Object arg)
1644 Lisp_Object tortoise, hare;
1646 hare = tortoise = arg;
1647 while (CONSP (hare))
1649 hare = XCDR (hare);
1650 if (!CONSP (hare))
1651 break;
1653 hare = XCDR (hare);
1654 tortoise = XCDR (tortoise);
1656 if (EQ (hare, tortoise))
1657 break;
1660 if (!NILP (hare))
1661 arg = list1 (arg);
1663 xsignal (Qerror, Fcons (build_string (s), arg));
1667 /* Return true if LIST is a non-nil atom or
1668 a list containing one of CONDITIONS. */
1670 static bool
1671 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1673 if (NILP (list))
1674 return 0;
1675 if (! CONSP (list))
1676 return 1;
1678 while (CONSP (conditions))
1680 Lisp_Object this, tail;
1681 this = XCAR (conditions);
1682 for (tail = list; CONSP (tail); tail = XCDR (tail))
1683 if (EQ (XCAR (tail), this))
1684 return 1;
1685 conditions = XCDR (conditions);
1687 return 0;
1690 /* Return true if an error with condition-symbols CONDITIONS,
1691 and described by SIGNAL-DATA, should skip the debugger
1692 according to debugger-ignored-errors. */
1694 static bool
1695 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1697 Lisp_Object tail;
1698 bool first_string = 1;
1699 Lisp_Object error_message;
1701 error_message = Qnil;
1702 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1704 if (STRINGP (XCAR (tail)))
1706 if (first_string)
1708 error_message = Ferror_message_string (data);
1709 first_string = 0;
1712 if (fast_string_match (XCAR (tail), error_message) >= 0)
1713 return 1;
1715 else
1717 Lisp_Object contail;
1719 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1720 if (EQ (XCAR (tail), XCAR (contail)))
1721 return 1;
1725 return 0;
1728 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1729 SIG and DATA describe the signal. There are two ways to pass them:
1730 = SIG is the error symbol, and DATA is the rest of the data.
1731 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1732 This is for memory-full errors only. */
1733 static bool
1734 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1736 Lisp_Object combined_data;
1738 combined_data = Fcons (sig, data);
1740 if (
1741 /* Don't try to run the debugger with interrupts blocked.
1742 The editing loop would return anyway. */
1743 ! input_blocked_p ()
1744 && NILP (Vinhibit_debugger)
1745 /* Does user want to enter debugger for this kind of error? */
1746 && (EQ (sig, Qquit)
1747 ? debug_on_quit
1748 : wants_debugger (Vdebug_on_error, conditions))
1749 && ! skip_debugger (conditions, combined_data)
1750 /* RMS: What's this for? */
1751 && when_entered_debugger < num_nonmacro_input_events)
1753 call_debugger (list2 (Qerror, combined_data));
1754 return 1;
1757 return 0;
1760 static Lisp_Object
1761 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1763 register Lisp_Object h;
1765 /* t is used by handlers for all conditions, set up by C code. */
1766 if (EQ (handlers, Qt))
1767 return Qt;
1769 /* error is used similarly, but means print an error message
1770 and run the debugger if that is enabled. */
1771 if (EQ (handlers, Qerror))
1772 return Qt;
1774 for (h = handlers; CONSP (h); h = XCDR (h))
1776 Lisp_Object handler = XCAR (h);
1777 Lisp_Object condit, tem;
1779 if (!CONSP (handler))
1780 continue;
1781 condit = XCAR (handler);
1782 /* Handle a single condition name in handler HANDLER. */
1783 if (SYMBOLP (condit))
1785 tem = Fmemq (Fcar (handler), conditions);
1786 if (!NILP (tem))
1787 return handler;
1789 /* Handle a list of condition names in handler HANDLER. */
1790 else if (CONSP (condit))
1792 Lisp_Object tail;
1793 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1795 tem = Fmemq (XCAR (tail), conditions);
1796 if (!NILP (tem))
1797 return handler;
1802 return Qnil;
1806 /* Dump an error message; called like vprintf. */
1807 void
1808 verror (const char *m, va_list ap)
1810 char buf[4000];
1811 ptrdiff_t size = sizeof buf;
1812 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1813 char *buffer = buf;
1814 ptrdiff_t used;
1815 Lisp_Object string;
1817 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1818 string = make_string (buffer, used);
1819 if (buffer != buf)
1820 xfree (buffer);
1822 xsignal1 (Qerror, string);
1826 /* Dump an error message; called like printf. */
1828 /* VARARGS 1 */
1829 void
1830 error (const char *m, ...)
1832 va_list ap;
1833 va_start (ap, m);
1834 verror (m, ap);
1837 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1838 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1839 This means it contains a description for how to read arguments to give it.
1840 The value is nil for an invalid function or a symbol with no function
1841 definition.
1843 Interactively callable functions include strings and vectors (treated
1844 as keyboard macros), lambda-expressions that contain a top-level call
1845 to `interactive', autoload definitions made by `autoload' with non-nil
1846 fourth argument, and some of the built-in functions of Lisp.
1848 Also, a symbol satisfies `commandp' if its function definition does so.
1850 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1851 then strings and vectors are not accepted. */)
1852 (Lisp_Object function, Lisp_Object for_call_interactively)
1854 register Lisp_Object fun;
1855 register Lisp_Object funcar;
1856 Lisp_Object if_prop = Qnil;
1858 fun = function;
1860 fun = indirect_function (fun); /* Check cycles. */
1861 if (NILP (fun))
1862 return Qnil;
1864 /* Check an `interactive-form' property if present, analogous to the
1865 function-documentation property. */
1866 fun = function;
1867 while (SYMBOLP (fun))
1869 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1870 if (!NILP (tmp))
1871 if_prop = Qt;
1872 fun = Fsymbol_function (fun);
1875 /* Emacs primitives are interactive if their DEFUN specifies an
1876 interactive spec. */
1877 if (SUBRP (fun))
1878 return XSUBR (fun)->intspec ? Qt : if_prop;
1880 /* Bytecode objects are interactive if they are long enough to
1881 have an element whose index is COMPILED_INTERACTIVE, which is
1882 where the interactive spec is stored. */
1883 else if (COMPILEDP (fun))
1884 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1885 ? Qt : if_prop);
1887 /* Strings and vectors are keyboard macros. */
1888 if (STRINGP (fun) || VECTORP (fun))
1889 return (NILP (for_call_interactively) ? Qt : Qnil);
1891 /* Lists may represent commands. */
1892 if (!CONSP (fun))
1893 return Qnil;
1894 funcar = XCAR (fun);
1895 if (EQ (funcar, Qclosure))
1896 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1897 ? Qt : if_prop);
1898 else if (EQ (funcar, Qlambda))
1899 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1900 else if (EQ (funcar, Qautoload))
1901 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1902 else
1903 return Qnil;
1906 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1907 doc: /* Define FUNCTION to autoload from FILE.
1908 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1909 Third arg DOCSTRING is documentation for the function.
1910 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1911 Fifth arg TYPE indicates the type of the object:
1912 nil or omitted says FUNCTION is a function,
1913 `keymap' says FUNCTION is really a keymap, and
1914 `macro' or t says FUNCTION is really a macro.
1915 Third through fifth args give info about the real definition.
1916 They default to nil.
1917 If FUNCTION is already defined other than as an autoload,
1918 this does nothing and returns nil. */)
1919 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1921 CHECK_SYMBOL (function);
1922 CHECK_STRING (file);
1924 /* If function is defined and not as an autoload, don't override. */
1925 if (!NILP (XSYMBOL (function)->function)
1926 && !AUTOLOADP (XSYMBOL (function)->function))
1927 return Qnil;
1929 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1930 /* `read1' in lread.c has found the docstring starting with "\
1931 and assumed the docstring will be provided by Snarf-documentation, so it
1932 passed us 0 instead. But that leads to accidental sharing in purecopy's
1933 hash-consing, so we use a (hopefully) unique integer instead. */
1934 docstring = make_number (XHASH (function));
1935 return Fdefalias (function,
1936 list5 (Qautoload, file, docstring, interactive, type),
1937 Qnil);
1940 void
1941 un_autoload (Lisp_Object oldqueue)
1943 Lisp_Object queue, first, second;
1945 /* Queue to unwind is current value of Vautoload_queue.
1946 oldqueue is the shadowed value to leave in Vautoload_queue. */
1947 queue = Vautoload_queue;
1948 Vautoload_queue = oldqueue;
1949 while (CONSP (queue))
1951 first = XCAR (queue);
1952 second = Fcdr (first);
1953 first = Fcar (first);
1954 if (EQ (first, make_number (0)))
1955 Vfeatures = second;
1956 else
1957 Ffset (first, second);
1958 queue = XCDR (queue);
1962 /* Load an autoloaded function.
1963 FUNNAME is the symbol which is the function's name.
1964 FUNDEF is the autoload definition (a list). */
1966 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1967 doc: /* Load FUNDEF which should be an autoload.
1968 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1969 in which case the function returns the new autoloaded function value.
1970 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1971 it is defines a macro. */)
1972 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1974 ptrdiff_t count = SPECPDL_INDEX ();
1975 struct gcpro gcpro1, gcpro2, gcpro3;
1977 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1978 return fundef;
1980 if (EQ (macro_only, Qmacro))
1982 Lisp_Object kind = Fnth (make_number (4), fundef);
1983 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1984 return fundef;
1987 /* This is to make sure that loadup.el gives a clear picture
1988 of what files are preloaded and when. */
1989 if (! NILP (Vpurify_flag))
1990 error ("Attempt to autoload %s while preparing to dump",
1991 SDATA (SYMBOL_NAME (funname)));
1993 CHECK_SYMBOL (funname);
1994 GCPRO3 (funname, fundef, macro_only);
1996 /* Preserve the match data. */
1997 record_unwind_save_match_data ();
1999 /* If autoloading gets an error (which includes the error of failing
2000 to define the function being called), we use Vautoload_queue
2001 to undo function definitions and `provide' calls made by
2002 the function. We do this in the specific case of autoloading
2003 because autoloading is not an explicit request "load this file",
2004 but rather a request to "call this function".
2006 The value saved here is to be restored into Vautoload_queue. */
2007 record_unwind_protect (un_autoload, Vautoload_queue);
2008 Vautoload_queue = Qt;
2009 /* If `macro_only', assume this autoload to be a "best-effort",
2010 so don't signal an error if autoloading fails. */
2011 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
2013 /* Once loading finishes, don't undo it. */
2014 Vautoload_queue = Qt;
2015 unbind_to (count, Qnil);
2017 UNGCPRO;
2019 if (NILP (funname))
2020 return Qnil;
2021 else
2023 Lisp_Object fun = Findirect_function (funname, Qnil);
2025 if (!NILP (Fequal (fun, fundef)))
2026 error ("Autoloading failed to define function %s",
2027 SDATA (SYMBOL_NAME (funname)));
2028 else
2029 return fun;
2034 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2035 doc: /* Evaluate FORM and return its value.
2036 If LEXICAL is t, evaluate using lexical scoping.
2037 LEXICAL can also be an actual lexical environment, in the form of an
2038 alist mapping symbols to their value. */)
2039 (Lisp_Object form, Lisp_Object lexical)
2041 ptrdiff_t count = SPECPDL_INDEX ();
2042 specbind (Qinternal_interpreter_environment,
2043 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2044 return unbind_to (count, eval_sub (form));
2047 /* Grow the specpdl stack by one entry.
2048 The caller should have already initialized the entry.
2049 Signal an error on stack overflow.
2051 Make sure that there is always one unused entry past the top of the
2052 stack, so that the just-initialized entry is safely unwound if
2053 memory exhausted and an error is signaled here. Also, allocate a
2054 never-used entry just before the bottom of the stack; sometimes its
2055 address is taken. */
2057 static void
2058 grow_specpdl (void)
2060 specpdl_ptr++;
2062 if (specpdl_ptr == specpdl + specpdl_size)
2064 ptrdiff_t count = SPECPDL_INDEX ();
2065 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2066 union specbinding *pdlvec = specpdl - 1;
2067 ptrdiff_t pdlvecsize = specpdl_size + 1;
2068 if (max_size <= specpdl_size)
2070 if (max_specpdl_size < 400)
2071 max_size = max_specpdl_size = 400;
2072 if (max_size <= specpdl_size)
2073 signal_error ("Variable binding depth exceeds max-specpdl-size",
2074 Qnil);
2076 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2077 specpdl = pdlvec + 1;
2078 specpdl_size = pdlvecsize - 1;
2079 specpdl_ptr = specpdl + count;
2083 void
2084 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2086 eassert (nargs >= UNEVALLED);
2087 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2088 specpdl_ptr->bt.debug_on_exit = false;
2089 specpdl_ptr->bt.function = function;
2090 specpdl_ptr->bt.args = args;
2091 specpdl_ptr->bt.nargs = nargs;
2092 grow_specpdl ();
2095 /* Eval a sub-expression of the current expression (i.e. in the same
2096 lexical scope). */
2097 Lisp_Object
2098 eval_sub (Lisp_Object form)
2100 Lisp_Object fun, val, original_fun, original_args;
2101 Lisp_Object funcar;
2102 struct gcpro gcpro1, gcpro2, gcpro3;
2104 if (SYMBOLP (form))
2106 /* Look up its binding in the lexical environment.
2107 We do not pay attention to the declared_special flag here, since we
2108 already did that when let-binding the variable. */
2109 Lisp_Object lex_binding
2110 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2111 ? Fassq (form, Vinternal_interpreter_environment)
2112 : Qnil;
2113 if (CONSP (lex_binding))
2114 return XCDR (lex_binding);
2115 else
2116 return Fsymbol_value (form);
2119 if (!CONSP (form))
2120 return form;
2122 QUIT;
2124 GCPRO1 (form);
2125 maybe_gc ();
2126 UNGCPRO;
2128 if (++lisp_eval_depth > max_lisp_eval_depth)
2130 if (max_lisp_eval_depth < 100)
2131 max_lisp_eval_depth = 100;
2132 if (lisp_eval_depth > max_lisp_eval_depth)
2133 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2136 original_fun = XCAR (form);
2137 original_args = XCDR (form);
2139 /* This also protects them from gc. */
2140 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2142 if (debug_on_next_call)
2143 do_debug_on_call (Qt);
2145 /* At this point, only original_fun and original_args
2146 have values that will be used below. */
2147 retry:
2149 /* Optimize for no indirection. */
2150 fun = original_fun;
2151 if (!SYMBOLP (fun))
2152 fun = Ffunction (Fcons (fun, Qnil));
2153 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2154 fun = indirect_function (fun);
2156 if (SUBRP (fun))
2158 Lisp_Object numargs;
2159 Lisp_Object argvals[8];
2160 Lisp_Object args_left;
2161 register int i, maxargs;
2163 args_left = original_args;
2164 numargs = Flength (args_left);
2166 check_cons_list ();
2168 if (XINT (numargs) < XSUBR (fun)->min_args
2169 || (XSUBR (fun)->max_args >= 0
2170 && XSUBR (fun)->max_args < XINT (numargs)))
2171 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2173 else if (XSUBR (fun)->max_args == UNEVALLED)
2174 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2175 else if (XSUBR (fun)->max_args == MANY)
2177 /* Pass a vector of evaluated arguments. */
2178 Lisp_Object *vals;
2179 ptrdiff_t argnum = 0;
2180 USE_SAFE_ALLOCA;
2182 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2184 GCPRO3 (args_left, fun, fun);
2185 gcpro3.var = vals;
2186 gcpro3.nvars = 0;
2188 while (!NILP (args_left))
2190 vals[argnum++] = eval_sub (Fcar (args_left));
2191 args_left = Fcdr (args_left);
2192 gcpro3.nvars = argnum;
2195 set_backtrace_args (specpdl_ptr - 1, vals);
2196 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2198 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2199 UNGCPRO;
2200 SAFE_FREE ();
2202 else
2204 GCPRO3 (args_left, fun, fun);
2205 gcpro3.var = argvals;
2206 gcpro3.nvars = 0;
2208 maxargs = XSUBR (fun)->max_args;
2209 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2211 argvals[i] = eval_sub (Fcar (args_left));
2212 gcpro3.nvars = ++i;
2215 UNGCPRO;
2217 set_backtrace_args (specpdl_ptr - 1, argvals);
2218 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2220 switch (i)
2222 case 0:
2223 val = (XSUBR (fun)->function.a0 ());
2224 break;
2225 case 1:
2226 val = (XSUBR (fun)->function.a1 (argvals[0]));
2227 break;
2228 case 2:
2229 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2230 break;
2231 case 3:
2232 val = (XSUBR (fun)->function.a3
2233 (argvals[0], argvals[1], argvals[2]));
2234 break;
2235 case 4:
2236 val = (XSUBR (fun)->function.a4
2237 (argvals[0], argvals[1], argvals[2], argvals[3]));
2238 break;
2239 case 5:
2240 val = (XSUBR (fun)->function.a5
2241 (argvals[0], argvals[1], argvals[2], argvals[3],
2242 argvals[4]));
2243 break;
2244 case 6:
2245 val = (XSUBR (fun)->function.a6
2246 (argvals[0], argvals[1], argvals[2], argvals[3],
2247 argvals[4], argvals[5]));
2248 break;
2249 case 7:
2250 val = (XSUBR (fun)->function.a7
2251 (argvals[0], argvals[1], argvals[2], argvals[3],
2252 argvals[4], argvals[5], argvals[6]));
2253 break;
2255 case 8:
2256 val = (XSUBR (fun)->function.a8
2257 (argvals[0], argvals[1], argvals[2], argvals[3],
2258 argvals[4], argvals[5], argvals[6], argvals[7]));
2259 break;
2261 default:
2262 /* Someone has created a subr that takes more arguments than
2263 is supported by this code. We need to either rewrite the
2264 subr to use a different argument protocol, or add more
2265 cases to this switch. */
2266 emacs_abort ();
2270 else if (COMPILEDP (fun))
2271 val = apply_lambda (fun, original_args);
2272 else
2274 if (NILP (fun))
2275 xsignal1 (Qvoid_function, original_fun);
2276 if (!CONSP (fun))
2277 xsignal1 (Qinvalid_function, original_fun);
2278 funcar = XCAR (fun);
2279 if (!SYMBOLP (funcar))
2280 xsignal1 (Qinvalid_function, original_fun);
2281 if (EQ (funcar, Qautoload))
2283 Fautoload_do_load (fun, original_fun, Qnil);
2284 goto retry;
2286 if (EQ (funcar, Qmacro))
2288 ptrdiff_t count = SPECPDL_INDEX ();
2289 Lisp_Object exp;
2290 /* Bind lexical-binding during expansion of the macro, so the
2291 macro can know reliably if the code it outputs will be
2292 interpreted using lexical-binding or not. */
2293 specbind (Qlexical_binding,
2294 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2295 exp = apply1 (Fcdr (fun), original_args);
2296 unbind_to (count, Qnil);
2297 val = eval_sub (exp);
2299 else if (EQ (funcar, Qlambda)
2300 || EQ (funcar, Qclosure))
2301 val = apply_lambda (fun, original_args);
2302 else
2303 xsignal1 (Qinvalid_function, original_fun);
2305 check_cons_list ();
2307 lisp_eval_depth--;
2308 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2309 val = call_debugger (list2 (Qexit, val));
2310 specpdl_ptr--;
2312 return val;
2315 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2316 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2317 Then return the value FUNCTION returns.
2318 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2319 usage: (apply FUNCTION &rest ARGUMENTS) */)
2320 (ptrdiff_t nargs, Lisp_Object *args)
2322 ptrdiff_t i;
2323 EMACS_INT numargs;
2324 register Lisp_Object spread_arg;
2325 register Lisp_Object *funcall_args;
2326 Lisp_Object fun, retval;
2327 struct gcpro gcpro1;
2328 USE_SAFE_ALLOCA;
2330 fun = args [0];
2331 funcall_args = 0;
2332 spread_arg = args [nargs - 1];
2333 CHECK_LIST (spread_arg);
2335 numargs = XINT (Flength (spread_arg));
2337 if (numargs == 0)
2338 return Ffuncall (nargs - 1, args);
2339 else if (numargs == 1)
2341 args [nargs - 1] = XCAR (spread_arg);
2342 return Ffuncall (nargs, args);
2345 numargs += nargs - 2;
2347 /* Optimize for no indirection. */
2348 if (SYMBOLP (fun) && !NILP (fun)
2349 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2350 fun = indirect_function (fun);
2351 if (NILP (fun))
2353 /* Let funcall get the error. */
2354 fun = args[0];
2355 goto funcall;
2358 if (SUBRP (fun))
2360 if (numargs < XSUBR (fun)->min_args
2361 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2362 goto funcall; /* Let funcall get the error. */
2363 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2365 /* Avoid making funcall cons up a yet another new vector of arguments
2366 by explicitly supplying nil's for optional values. */
2367 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2368 for (i = numargs; i < XSUBR (fun)->max_args;)
2369 funcall_args[++i] = Qnil;
2370 GCPRO1 (*funcall_args);
2371 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2374 funcall:
2375 /* We add 1 to numargs because funcall_args includes the
2376 function itself as well as its arguments. */
2377 if (!funcall_args)
2379 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2380 GCPRO1 (*funcall_args);
2381 gcpro1.nvars = 1 + numargs;
2384 memcpy (funcall_args, args, nargs * word_size);
2385 /* Spread the last arg we got. Its first element goes in
2386 the slot that it used to occupy, hence this value of I. */
2387 i = nargs - 1;
2388 while (!NILP (spread_arg))
2390 funcall_args [i++] = XCAR (spread_arg);
2391 spread_arg = XCDR (spread_arg);
2394 /* By convention, the caller needs to gcpro Ffuncall's args. */
2395 retval = Ffuncall (gcpro1.nvars, funcall_args);
2396 UNGCPRO;
2397 SAFE_FREE ();
2399 return retval;
2402 /* Run hook variables in various ways. */
2404 static Lisp_Object
2405 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2407 Ffuncall (nargs, args);
2408 return Qnil;
2411 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2412 doc: /* Run each hook in HOOKS.
2413 Each argument should be a symbol, a hook variable.
2414 These symbols are processed in the order specified.
2415 If a hook symbol has a non-nil value, that value may be a function
2416 or a list of functions to be called to run the hook.
2417 If the value is a function, it is called with no arguments.
2418 If it is a list, the elements are called, in order, with no arguments.
2420 Major modes should not use this function directly to run their mode
2421 hook; they should use `run-mode-hooks' instead.
2423 Do not use `make-local-variable' to make a hook variable buffer-local.
2424 Instead, use `add-hook' and specify t for the LOCAL argument.
2425 usage: (run-hooks &rest HOOKS) */)
2426 (ptrdiff_t nargs, Lisp_Object *args)
2428 Lisp_Object hook[1];
2429 ptrdiff_t i;
2431 for (i = 0; i < nargs; i++)
2433 hook[0] = args[i];
2434 run_hook_with_args (1, hook, funcall_nil);
2437 return Qnil;
2440 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2441 Srun_hook_with_args, 1, MANY, 0,
2442 doc: /* Run HOOK with the specified arguments ARGS.
2443 HOOK should be a symbol, a hook variable. The value of HOOK
2444 may be nil, a function, or a list of functions. Call each
2445 function in order with arguments ARGS. The final return value
2446 is unspecified.
2448 Do not use `make-local-variable' to make a hook variable buffer-local.
2449 Instead, use `add-hook' and specify t for the LOCAL argument.
2450 usage: (run-hook-with-args HOOK &rest ARGS) */)
2451 (ptrdiff_t nargs, Lisp_Object *args)
2453 return run_hook_with_args (nargs, args, funcall_nil);
2456 /* NB this one still documents a specific non-nil return value.
2457 (As did run-hook-with-args and run-hook-with-args-until-failure
2458 until they were changed in 24.1.) */
2459 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2460 Srun_hook_with_args_until_success, 1, MANY, 0,
2461 doc: /* Run HOOK with the specified arguments ARGS.
2462 HOOK should be a symbol, a hook variable. The value of HOOK
2463 may be nil, a function, or a list of functions. Call each
2464 function in order with arguments ARGS, stopping at the first
2465 one that returns non-nil, and return that value. Otherwise (if
2466 all functions return nil, or if there are no functions to call),
2467 return nil.
2469 Do not use `make-local-variable' to make a hook variable buffer-local.
2470 Instead, use `add-hook' and specify t for the LOCAL argument.
2471 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2472 (ptrdiff_t nargs, Lisp_Object *args)
2474 return run_hook_with_args (nargs, args, Ffuncall);
2477 static Lisp_Object
2478 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2480 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2483 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2484 Srun_hook_with_args_until_failure, 1, MANY, 0,
2485 doc: /* Run HOOK with the specified arguments ARGS.
2486 HOOK should be a symbol, a hook variable. The value of HOOK
2487 may be nil, a function, or a list of functions. Call each
2488 function in order with arguments ARGS, stopping at the first
2489 one that returns nil, and return nil. Otherwise (if all functions
2490 return non-nil, or if there are no functions to call), return non-nil
2491 \(do not rely on the precise return value in this case).
2493 Do not use `make-local-variable' to make a hook variable buffer-local.
2494 Instead, use `add-hook' and specify t for the LOCAL argument.
2495 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2496 (ptrdiff_t nargs, Lisp_Object *args)
2498 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2501 static Lisp_Object
2502 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2504 Lisp_Object tmp = args[0], ret;
2505 args[0] = args[1];
2506 args[1] = tmp;
2507 ret = Ffuncall (nargs, args);
2508 args[1] = args[0];
2509 args[0] = tmp;
2510 return ret;
2513 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2514 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2515 I.e. instead of calling each function FUN directly with arguments ARGS,
2516 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2517 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2518 aborts and returns that value.
2519 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2520 (ptrdiff_t nargs, Lisp_Object *args)
2522 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2525 /* ARGS[0] should be a hook symbol.
2526 Call each of the functions in the hook value, passing each of them
2527 as arguments all the rest of ARGS (all NARGS - 1 elements).
2528 FUNCALL specifies how to call each function on the hook.
2529 The caller (or its caller, etc) must gcpro all of ARGS,
2530 except that it isn't necessary to gcpro ARGS[0]. */
2532 Lisp_Object
2533 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2534 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2536 Lisp_Object sym, val, ret = Qnil;
2537 struct gcpro gcpro1, gcpro2, gcpro3;
2539 /* If we are dying or still initializing,
2540 don't do anything--it would probably crash if we tried. */
2541 if (NILP (Vrun_hooks))
2542 return Qnil;
2544 sym = args[0];
2545 val = find_symbol_value (sym);
2547 if (EQ (val, Qunbound) || NILP (val))
2548 return ret;
2549 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2551 args[0] = val;
2552 return funcall (nargs, args);
2554 else
2556 Lisp_Object global_vals = Qnil;
2557 GCPRO3 (sym, val, global_vals);
2559 for (;
2560 CONSP (val) && NILP (ret);
2561 val = XCDR (val))
2563 if (EQ (XCAR (val), Qt))
2565 /* t indicates this hook has a local binding;
2566 it means to run the global binding too. */
2567 global_vals = Fdefault_value (sym);
2568 if (NILP (global_vals)) continue;
2570 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2572 args[0] = global_vals;
2573 ret = funcall (nargs, args);
2575 else
2577 for (;
2578 CONSP (global_vals) && NILP (ret);
2579 global_vals = XCDR (global_vals))
2581 args[0] = XCAR (global_vals);
2582 /* In a global value, t should not occur. If it does, we
2583 must ignore it to avoid an endless loop. */
2584 if (!EQ (args[0], Qt))
2585 ret = funcall (nargs, args);
2589 else
2591 args[0] = XCAR (val);
2592 ret = funcall (nargs, args);
2596 UNGCPRO;
2597 return ret;
2601 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2603 void
2604 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2606 Lisp_Object temp[3];
2607 temp[0] = hook;
2608 temp[1] = arg1;
2609 temp[2] = arg2;
2611 Frun_hook_with_args (3, temp);
2614 /* Apply fn to arg. */
2615 Lisp_Object
2616 apply1 (Lisp_Object fn, Lisp_Object arg)
2618 struct gcpro gcpro1;
2620 GCPRO1 (fn);
2621 if (NILP (arg))
2622 RETURN_UNGCPRO (Ffuncall (1, &fn));
2623 gcpro1.nvars = 2;
2625 Lisp_Object args[2];
2626 args[0] = fn;
2627 args[1] = arg;
2628 gcpro1.var = args;
2629 RETURN_UNGCPRO (Fapply (2, args));
2633 /* Call function fn on no arguments. */
2634 Lisp_Object
2635 call0 (Lisp_Object fn)
2637 struct gcpro gcpro1;
2639 GCPRO1 (fn);
2640 RETURN_UNGCPRO (Ffuncall (1, &fn));
2643 /* Call function fn with 1 argument arg1. */
2644 /* ARGSUSED */
2645 Lisp_Object
2646 call1 (Lisp_Object fn, Lisp_Object arg1)
2648 struct gcpro gcpro1;
2649 Lisp_Object args[2];
2651 args[0] = fn;
2652 args[1] = arg1;
2653 GCPRO1 (args[0]);
2654 gcpro1.nvars = 2;
2655 RETURN_UNGCPRO (Ffuncall (2, args));
2658 /* Call function fn with 2 arguments arg1, arg2. */
2659 /* ARGSUSED */
2660 Lisp_Object
2661 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2663 struct gcpro gcpro1;
2664 Lisp_Object args[3];
2665 args[0] = fn;
2666 args[1] = arg1;
2667 args[2] = arg2;
2668 GCPRO1 (args[0]);
2669 gcpro1.nvars = 3;
2670 RETURN_UNGCPRO (Ffuncall (3, args));
2673 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2674 /* ARGSUSED */
2675 Lisp_Object
2676 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2678 struct gcpro gcpro1;
2679 Lisp_Object args[4];
2680 args[0] = fn;
2681 args[1] = arg1;
2682 args[2] = arg2;
2683 args[3] = arg3;
2684 GCPRO1 (args[0]);
2685 gcpro1.nvars = 4;
2686 RETURN_UNGCPRO (Ffuncall (4, args));
2689 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2690 /* ARGSUSED */
2691 Lisp_Object
2692 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2693 Lisp_Object arg4)
2695 struct gcpro gcpro1;
2696 Lisp_Object args[5];
2697 args[0] = fn;
2698 args[1] = arg1;
2699 args[2] = arg2;
2700 args[3] = arg3;
2701 args[4] = arg4;
2702 GCPRO1 (args[0]);
2703 gcpro1.nvars = 5;
2704 RETURN_UNGCPRO (Ffuncall (5, args));
2707 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2708 /* ARGSUSED */
2709 Lisp_Object
2710 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2711 Lisp_Object arg4, Lisp_Object arg5)
2713 struct gcpro gcpro1;
2714 Lisp_Object args[6];
2715 args[0] = fn;
2716 args[1] = arg1;
2717 args[2] = arg2;
2718 args[3] = arg3;
2719 args[4] = arg4;
2720 args[5] = arg5;
2721 GCPRO1 (args[0]);
2722 gcpro1.nvars = 6;
2723 RETURN_UNGCPRO (Ffuncall (6, args));
2726 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2727 /* ARGSUSED */
2728 Lisp_Object
2729 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2730 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2732 struct gcpro gcpro1;
2733 Lisp_Object args[7];
2734 args[0] = fn;
2735 args[1] = arg1;
2736 args[2] = arg2;
2737 args[3] = arg3;
2738 args[4] = arg4;
2739 args[5] = arg5;
2740 args[6] = arg6;
2741 GCPRO1 (args[0]);
2742 gcpro1.nvars = 7;
2743 RETURN_UNGCPRO (Ffuncall (7, args));
2746 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2747 /* ARGSUSED */
2748 Lisp_Object
2749 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2750 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2752 struct gcpro gcpro1;
2753 Lisp_Object args[8];
2754 args[0] = fn;
2755 args[1] = arg1;
2756 args[2] = arg2;
2757 args[3] = arg3;
2758 args[4] = arg4;
2759 args[5] = arg5;
2760 args[6] = arg6;
2761 args[7] = arg7;
2762 GCPRO1 (args[0]);
2763 gcpro1.nvars = 8;
2764 RETURN_UNGCPRO (Ffuncall (8, args));
2767 /* The caller should GCPRO all the elements of ARGS. */
2769 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2770 doc: /* Non-nil if OBJECT is a function. */)
2771 (Lisp_Object object)
2773 if (FUNCTIONP (object))
2774 return Qt;
2775 return Qnil;
2778 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2779 doc: /* Call first argument as a function, passing remaining arguments to it.
2780 Return the value that function returns.
2781 Thus, (funcall 'cons 'x 'y) returns (x . y).
2782 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2783 (ptrdiff_t nargs, Lisp_Object *args)
2785 Lisp_Object fun, original_fun;
2786 Lisp_Object funcar;
2787 ptrdiff_t numargs = nargs - 1;
2788 Lisp_Object lisp_numargs;
2789 Lisp_Object val;
2790 register Lisp_Object *internal_args;
2791 ptrdiff_t i;
2793 QUIT;
2795 if (++lisp_eval_depth > max_lisp_eval_depth)
2797 if (max_lisp_eval_depth < 100)
2798 max_lisp_eval_depth = 100;
2799 if (lisp_eval_depth > max_lisp_eval_depth)
2800 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2803 /* This also GCPROs them. */
2804 record_in_backtrace (args[0], &args[1], nargs - 1);
2806 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2807 maybe_gc ();
2809 if (debug_on_next_call)
2810 do_debug_on_call (Qlambda);
2812 check_cons_list ();
2814 original_fun = args[0];
2816 retry:
2818 /* Optimize for no indirection. */
2819 fun = original_fun;
2820 if (SYMBOLP (fun) && !NILP (fun)
2821 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2822 fun = indirect_function (fun);
2824 if (SUBRP (fun))
2826 if (numargs < XSUBR (fun)->min_args
2827 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2829 XSETFASTINT (lisp_numargs, numargs);
2830 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2833 else if (XSUBR (fun)->max_args == UNEVALLED)
2834 xsignal1 (Qinvalid_function, original_fun);
2836 else if (XSUBR (fun)->max_args == MANY)
2837 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2838 else
2840 if (XSUBR (fun)->max_args > numargs)
2842 internal_args = alloca (XSUBR (fun)->max_args
2843 * sizeof *internal_args);
2844 memcpy (internal_args, args + 1, numargs * word_size);
2845 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2846 internal_args[i] = Qnil;
2848 else
2849 internal_args = args + 1;
2850 switch (XSUBR (fun)->max_args)
2852 case 0:
2853 val = (XSUBR (fun)->function.a0 ());
2854 break;
2855 case 1:
2856 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2857 break;
2858 case 2:
2859 val = (XSUBR (fun)->function.a2
2860 (internal_args[0], internal_args[1]));
2861 break;
2862 case 3:
2863 val = (XSUBR (fun)->function.a3
2864 (internal_args[0], internal_args[1], internal_args[2]));
2865 break;
2866 case 4:
2867 val = (XSUBR (fun)->function.a4
2868 (internal_args[0], internal_args[1], internal_args[2],
2869 internal_args[3]));
2870 break;
2871 case 5:
2872 val = (XSUBR (fun)->function.a5
2873 (internal_args[0], internal_args[1], internal_args[2],
2874 internal_args[3], internal_args[4]));
2875 break;
2876 case 6:
2877 val = (XSUBR (fun)->function.a6
2878 (internal_args[0], internal_args[1], internal_args[2],
2879 internal_args[3], internal_args[4], internal_args[5]));
2880 break;
2881 case 7:
2882 val = (XSUBR (fun)->function.a7
2883 (internal_args[0], internal_args[1], internal_args[2],
2884 internal_args[3], internal_args[4], internal_args[5],
2885 internal_args[6]));
2886 break;
2888 case 8:
2889 val = (XSUBR (fun)->function.a8
2890 (internal_args[0], internal_args[1], internal_args[2],
2891 internal_args[3], internal_args[4], internal_args[5],
2892 internal_args[6], internal_args[7]));
2893 break;
2895 default:
2897 /* If a subr takes more than 8 arguments without using MANY
2898 or UNEVALLED, we need to extend this function to support it.
2899 Until this is done, there is no way to call the function. */
2900 emacs_abort ();
2904 else if (COMPILEDP (fun))
2905 val = funcall_lambda (fun, numargs, args + 1);
2906 else
2908 if (NILP (fun))
2909 xsignal1 (Qvoid_function, original_fun);
2910 if (!CONSP (fun))
2911 xsignal1 (Qinvalid_function, original_fun);
2912 funcar = XCAR (fun);
2913 if (!SYMBOLP (funcar))
2914 xsignal1 (Qinvalid_function, original_fun);
2915 if (EQ (funcar, Qlambda)
2916 || EQ (funcar, Qclosure))
2917 val = funcall_lambda (fun, numargs, args + 1);
2918 else if (EQ (funcar, Qautoload))
2920 Fautoload_do_load (fun, original_fun, Qnil);
2921 check_cons_list ();
2922 goto retry;
2924 else
2925 xsignal1 (Qinvalid_function, original_fun);
2927 check_cons_list ();
2928 lisp_eval_depth--;
2929 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2930 val = call_debugger (list2 (Qexit, val));
2931 specpdl_ptr--;
2932 return val;
2935 static Lisp_Object
2936 apply_lambda (Lisp_Object fun, Lisp_Object args)
2938 Lisp_Object args_left;
2939 ptrdiff_t i;
2940 EMACS_INT numargs;
2941 register Lisp_Object *arg_vector;
2942 struct gcpro gcpro1, gcpro2, gcpro3;
2943 register Lisp_Object tem;
2944 USE_SAFE_ALLOCA;
2946 numargs = XFASTINT (Flength (args));
2947 SAFE_ALLOCA_LISP (arg_vector, numargs);
2948 args_left = args;
2950 GCPRO3 (*arg_vector, args_left, fun);
2951 gcpro1.nvars = 0;
2953 for (i = 0; i < numargs; )
2955 tem = Fcar (args_left), args_left = Fcdr (args_left);
2956 tem = eval_sub (tem);
2957 arg_vector[i++] = tem;
2958 gcpro1.nvars = i;
2961 UNGCPRO;
2963 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2964 set_backtrace_nargs (specpdl_ptr - 1, i);
2965 tem = funcall_lambda (fun, numargs, arg_vector);
2967 /* Do the debug-on-exit now, while arg_vector still exists. */
2968 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2970 /* Don't do it again when we return to eval. */
2971 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2972 tem = call_debugger (list2 (Qexit, tem));
2974 SAFE_FREE ();
2975 return tem;
2978 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2979 and return the result of evaluation.
2980 FUN must be either a lambda-expression or a compiled-code object. */
2982 static Lisp_Object
2983 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2984 register Lisp_Object *arg_vector)
2986 Lisp_Object val, syms_left, next, lexenv;
2987 ptrdiff_t count = SPECPDL_INDEX ();
2988 ptrdiff_t i;
2989 bool optional, rest;
2991 if (CONSP (fun))
2993 if (EQ (XCAR (fun), Qclosure))
2995 fun = XCDR (fun); /* Drop `closure'. */
2996 lexenv = XCAR (fun);
2997 CHECK_LIST_CONS (fun, fun);
2999 else
3000 lexenv = Qnil;
3001 syms_left = XCDR (fun);
3002 if (CONSP (syms_left))
3003 syms_left = XCAR (syms_left);
3004 else
3005 xsignal1 (Qinvalid_function, fun);
3007 else if (COMPILEDP (fun))
3009 syms_left = AREF (fun, COMPILED_ARGLIST);
3010 if (INTEGERP (syms_left))
3011 /* A byte-code object with a non-nil `push args' slot means we
3012 shouldn't bind any arguments, instead just call the byte-code
3013 interpreter directly; it will push arguments as necessary.
3015 Byte-code objects with either a non-existent, or a nil value for
3016 the `push args' slot (the default), have dynamically-bound
3017 arguments, and use the argument-binding code below instead (as do
3018 all interpreted functions, even lexically bound ones). */
3020 /* If we have not actually read the bytecode string
3021 and constants vector yet, fetch them from the file. */
3022 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3023 Ffetch_bytecode (fun);
3024 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3025 AREF (fun, COMPILED_CONSTANTS),
3026 AREF (fun, COMPILED_STACK_DEPTH),
3027 syms_left,
3028 nargs, arg_vector);
3030 lexenv = Qnil;
3032 else
3033 emacs_abort ();
3035 i = optional = rest = 0;
3036 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3038 QUIT;
3040 next = XCAR (syms_left);
3041 if (!SYMBOLP (next))
3042 xsignal1 (Qinvalid_function, fun);
3044 if (EQ (next, Qand_rest))
3045 rest = 1;
3046 else if (EQ (next, Qand_optional))
3047 optional = 1;
3048 else
3050 Lisp_Object arg;
3051 if (rest)
3053 arg = Flist (nargs - i, &arg_vector[i]);
3054 i = nargs;
3056 else if (i < nargs)
3057 arg = arg_vector[i++];
3058 else if (!optional)
3059 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3060 else
3061 arg = Qnil;
3063 /* Bind the argument. */
3064 if (!NILP (lexenv) && SYMBOLP (next))
3065 /* Lexically bind NEXT by adding it to the lexenv alist. */
3066 lexenv = Fcons (Fcons (next, arg), lexenv);
3067 else
3068 /* Dynamically bind NEXT. */
3069 specbind (next, arg);
3073 if (!NILP (syms_left))
3074 xsignal1 (Qinvalid_function, fun);
3075 else if (i < nargs)
3076 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3078 if (!EQ (lexenv, Vinternal_interpreter_environment))
3079 /* Instantiate a new lexical environment. */
3080 specbind (Qinternal_interpreter_environment, lexenv);
3082 if (CONSP (fun))
3083 val = Fprogn (XCDR (XCDR (fun)));
3084 else
3086 /* If we have not actually read the bytecode string
3087 and constants vector yet, fetch them from the file. */
3088 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3089 Ffetch_bytecode (fun);
3090 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3091 AREF (fun, COMPILED_CONSTANTS),
3092 AREF (fun, COMPILED_STACK_DEPTH),
3093 Qnil, 0, 0);
3096 return unbind_to (count, val);
3099 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3100 1, 1, 0,
3101 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3102 (Lisp_Object object)
3104 Lisp_Object tem;
3106 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3108 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3109 if (!CONSP (tem))
3111 tem = AREF (object, COMPILED_BYTECODE);
3112 if (CONSP (tem) && STRINGP (XCAR (tem)))
3113 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3114 else
3115 error ("Invalid byte code");
3117 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3118 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3120 return object;
3123 /* Return true if SYMBOL currently has a let-binding
3124 which was made in the buffer that is now current. */
3126 bool
3127 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3129 union specbinding *p;
3130 Lisp_Object buf = Fcurrent_buffer ();
3132 for (p = specpdl_ptr; p > specpdl; )
3133 if ((--p)->kind > SPECPDL_LET)
3135 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3136 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3137 if (symbol == let_bound_symbol
3138 && EQ (specpdl_where (p), buf))
3139 return 1;
3142 return 0;
3145 bool
3146 let_shadows_global_binding_p (Lisp_Object symbol)
3148 union specbinding *p;
3150 for (p = specpdl_ptr; p > specpdl; )
3151 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3152 return 1;
3154 return 0;
3157 /* `specpdl_ptr' describes which variable is
3158 let-bound, so it can be properly undone when we unbind_to.
3159 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3160 - SYMBOL is the variable being bound. Note that it should not be
3161 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3162 to record V2 here).
3163 - WHERE tells us in which buffer the binding took place.
3164 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3165 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3166 i.e. bindings to the default value of a variable which can be
3167 buffer-local. */
3169 void
3170 specbind (Lisp_Object symbol, Lisp_Object value)
3172 struct Lisp_Symbol *sym;
3174 CHECK_SYMBOL (symbol);
3175 sym = XSYMBOL (symbol);
3177 start:
3178 switch (sym->redirect)
3180 case SYMBOL_VARALIAS:
3181 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3182 case SYMBOL_PLAINVAL:
3183 /* The most common case is that of a non-constant symbol with a
3184 trivial value. Make that as fast as we can. */
3185 specpdl_ptr->let.kind = SPECPDL_LET;
3186 specpdl_ptr->let.symbol = symbol;
3187 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3188 grow_specpdl ();
3189 if (!sym->constant)
3190 SET_SYMBOL_VAL (sym, value);
3191 else
3192 set_internal (symbol, value, Qnil, 1);
3193 break;
3194 case SYMBOL_LOCALIZED:
3195 if (SYMBOL_BLV (sym)->frame_local)
3196 error ("Frame-local vars cannot be let-bound");
3197 case SYMBOL_FORWARDED:
3199 Lisp_Object ovalue = find_symbol_value (symbol);
3200 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3201 specpdl_ptr->let.symbol = symbol;
3202 specpdl_ptr->let.old_value = ovalue;
3203 specpdl_ptr->let.where = Fcurrent_buffer ();
3205 eassert (sym->redirect != SYMBOL_LOCALIZED
3206 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3208 if (sym->redirect == SYMBOL_LOCALIZED)
3210 if (!blv_found (SYMBOL_BLV (sym)))
3211 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3213 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3215 /* If SYMBOL is a per-buffer variable which doesn't have a
3216 buffer-local value here, make the `let' change the global
3217 value by changing the value of SYMBOL in all buffers not
3218 having their own value. This is consistent with what
3219 happens with other buffer-local variables. */
3220 if (NILP (Flocal_variable_p (symbol, Qnil)))
3222 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3223 grow_specpdl ();
3224 Fset_default (symbol, value);
3225 return;
3228 else
3229 specpdl_ptr->let.kind = SPECPDL_LET;
3231 grow_specpdl ();
3232 set_internal (symbol, value, Qnil, 1);
3233 break;
3235 default: emacs_abort ();
3239 /* Push unwind-protect entries of various types. */
3241 void
3242 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3244 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3245 specpdl_ptr->unwind.func = function;
3246 specpdl_ptr->unwind.arg = arg;
3247 grow_specpdl ();
3250 void
3251 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3253 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3254 specpdl_ptr->unwind_ptr.func = function;
3255 specpdl_ptr->unwind_ptr.arg = arg;
3256 grow_specpdl ();
3259 void
3260 record_unwind_protect_int (void (*function) (int), int arg)
3262 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3263 specpdl_ptr->unwind_int.func = function;
3264 specpdl_ptr->unwind_int.arg = arg;
3265 grow_specpdl ();
3268 void
3269 record_unwind_protect_void (void (*function) (void))
3271 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3272 specpdl_ptr->unwind_void.func = function;
3273 grow_specpdl ();
3276 static void
3277 do_nothing (void)
3280 /* Push an unwind-protect entry that does nothing, so that
3281 set_unwind_protect_ptr can overwrite it later. */
3283 void
3284 record_unwind_protect_nothing (void)
3286 record_unwind_protect_void (do_nothing);
3289 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3290 It need not be at the top of the stack. */
3292 void
3293 clear_unwind_protect (ptrdiff_t count)
3295 union specbinding *p = specpdl + count;
3296 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3297 p->unwind_void.func = do_nothing;
3300 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3301 It need not be at the top of the stack. Discard the entry's
3302 previous value without invoking it. */
3304 void
3305 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3306 Lisp_Object arg)
3308 union specbinding *p = specpdl + count;
3309 p->unwind.kind = SPECPDL_UNWIND;
3310 p->unwind.func = func;
3311 p->unwind.arg = arg;
3314 void
3315 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3317 union specbinding *p = specpdl + count;
3318 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3319 p->unwind_ptr.func = func;
3320 p->unwind_ptr.arg = arg;
3323 /* Pop and execute entries from the unwind-protect stack until the
3324 depth COUNT is reached. Return VALUE. */
3326 Lisp_Object
3327 unbind_to (ptrdiff_t count, Lisp_Object value)
3329 Lisp_Object quitf = Vquit_flag;
3330 struct gcpro gcpro1, gcpro2;
3332 GCPRO2 (value, quitf);
3333 Vquit_flag = Qnil;
3335 while (specpdl_ptr != specpdl + count)
3337 /* Decrement specpdl_ptr before we do the work to unbind it, so
3338 that an error in unbinding won't try to unbind the same entry
3339 again. Take care to copy any parts of the binding needed
3340 before invoking any code that can make more bindings. */
3342 specpdl_ptr--;
3344 switch (specpdl_ptr->kind)
3346 case SPECPDL_UNWIND:
3347 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3348 break;
3349 case SPECPDL_UNWIND_PTR:
3350 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3351 break;
3352 case SPECPDL_UNWIND_INT:
3353 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3354 break;
3355 case SPECPDL_UNWIND_VOID:
3356 specpdl_ptr->unwind_void.func ();
3357 break;
3358 case SPECPDL_BACKTRACE:
3359 break;
3360 case SPECPDL_LET:
3361 { /* If variable has a trivial value (no forwarding), we can
3362 just set it. No need to check for constant symbols here,
3363 since that was already done by specbind. */
3364 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3365 if (sym->redirect == SYMBOL_PLAINVAL)
3367 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3368 break;
3370 else
3371 { /* FALLTHROUGH!!
3372 NOTE: we only ever come here if make_local_foo was used for
3373 the first time on this var within this let. */
3376 case SPECPDL_LET_DEFAULT:
3377 Fset_default (specpdl_symbol (specpdl_ptr),
3378 specpdl_old_value (specpdl_ptr));
3379 break;
3380 case SPECPDL_LET_LOCAL:
3382 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3383 Lisp_Object where = specpdl_where (specpdl_ptr);
3384 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3385 eassert (BUFFERP (where));
3387 /* If this was a local binding, reset the value in the appropriate
3388 buffer, but only if that buffer's binding still exists. */
3389 if (!NILP (Flocal_variable_p (symbol, where)))
3390 set_internal (symbol, old_value, where, 1);
3392 break;
3396 if (NILP (Vquit_flag) && !NILP (quitf))
3397 Vquit_flag = quitf;
3399 UNGCPRO;
3400 return value;
3403 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3404 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3405 A special variable is one that will be bound dynamically, even in a
3406 context where binding is lexical by default. */)
3407 (Lisp_Object symbol)
3409 CHECK_SYMBOL (symbol);
3410 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3414 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3415 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3416 The debugger is entered when that frame exits, if the flag is non-nil. */)
3417 (Lisp_Object level, Lisp_Object flag)
3419 union specbinding *pdl = backtrace_top ();
3420 register EMACS_INT i;
3422 CHECK_NUMBER (level);
3424 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3425 pdl = backtrace_next (pdl);
3427 if (backtrace_p (pdl))
3428 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3430 return flag;
3433 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3434 doc: /* Print a trace of Lisp function calls currently active.
3435 Output stream used is value of `standard-output'. */)
3436 (void)
3438 union specbinding *pdl = backtrace_top ();
3439 Lisp_Object tem;
3440 Lisp_Object old_print_level = Vprint_level;
3442 if (NILP (Vprint_level))
3443 XSETFASTINT (Vprint_level, 8);
3445 while (backtrace_p (pdl))
3447 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3448 if (backtrace_nargs (pdl) == UNEVALLED)
3450 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3451 Qnil);
3452 write_string ("\n", -1);
3454 else
3456 tem = backtrace_function (pdl);
3457 Fprin1 (tem, Qnil); /* This can QUIT. */
3458 write_string ("(", -1);
3460 ptrdiff_t i;
3461 for (i = 0; i < backtrace_nargs (pdl); i++)
3463 if (i) write_string (" ", -1);
3464 Fprin1 (backtrace_args (pdl)[i], Qnil);
3467 write_string (")\n", -1);
3469 pdl = backtrace_next (pdl);
3472 Vprint_level = old_print_level;
3473 return Qnil;
3476 static union specbinding *
3477 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3479 union specbinding *pdl = backtrace_top ();
3480 register EMACS_INT i;
3482 CHECK_NATNUM (nframes);
3484 if (!NILP (base))
3485 { /* Skip up to `base'. */
3486 base = Findirect_function (base, Qt);
3487 while (backtrace_p (pdl)
3488 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3489 pdl = backtrace_next (pdl);
3492 /* Find the frame requested. */
3493 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3494 pdl = backtrace_next (pdl);
3496 return pdl;
3499 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3500 doc: /* Return the function and arguments NFRAMES up from current execution point.
3501 If that frame has not evaluated the arguments yet (or is a special form),
3502 the value is (nil FUNCTION ARG-FORMS...).
3503 If that frame has evaluated its arguments and called its function already,
3504 the value is (t FUNCTION ARG-VALUES...).
3505 A &rest arg is represented as the tail of the list ARG-VALUES.
3506 FUNCTION is whatever was supplied as car of evaluated list,
3507 or a lambda expression for macro calls.
3508 If NFRAMES is more than the number of frames, the value is nil.
3509 If BASE is non-nil, it should be a function and NFRAMES counts from its
3510 nearest activation frame. */)
3511 (Lisp_Object nframes, Lisp_Object base)
3513 union specbinding *pdl = get_backtrace_frame (nframes, base);
3515 if (!backtrace_p (pdl))
3516 return Qnil;
3517 if (backtrace_nargs (pdl) == UNEVALLED)
3518 return Fcons (Qnil,
3519 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3520 else
3522 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3524 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3528 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3529 the specpdl stack, and then rewind them. We store the pre-unwind values
3530 directly in the pre-existing specpdl elements (i.e. we swap the current
3531 value and the old value stored in the specpdl), kind of like the inplace
3532 pointer-reversal trick. As it turns out, the rewind does the same as the
3533 unwind, except it starts from the other end of the specpdl stack, so we use
3534 the same function for both unwind and rewind. */
3535 static void
3536 backtrace_eval_unrewind (int distance)
3538 union specbinding *tmp = specpdl_ptr;
3539 int step = -1;
3540 if (distance < 0)
3541 { /* It's a rewind rather than unwind. */
3542 tmp += distance - 1;
3543 step = 1;
3544 distance = -distance;
3547 for (; distance > 0; distance--)
3549 tmp += step;
3550 /* */
3551 switch (tmp->kind)
3553 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3554 unwind_protect, but the problem is that we don't know how to
3555 rewind them afterwards. */
3556 case SPECPDL_UNWIND:
3557 case SPECPDL_UNWIND_PTR:
3558 case SPECPDL_UNWIND_INT:
3559 case SPECPDL_UNWIND_VOID:
3560 case SPECPDL_BACKTRACE:
3561 break;
3562 case SPECPDL_LET:
3563 { /* If variable has a trivial value (no forwarding), we can
3564 just set it. No need to check for constant symbols here,
3565 since that was already done by specbind. */
3566 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3567 if (sym->redirect == SYMBOL_PLAINVAL)
3569 Lisp_Object old_value = specpdl_old_value (tmp);
3570 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3571 SET_SYMBOL_VAL (sym, old_value);
3572 break;
3574 else
3575 { /* FALLTHROUGH!!
3576 NOTE: we only ever come here if make_local_foo was used for
3577 the first time on this var within this let. */
3580 case SPECPDL_LET_DEFAULT:
3582 Lisp_Object sym = specpdl_symbol (tmp);
3583 Lisp_Object old_value = specpdl_old_value (tmp);
3584 set_specpdl_old_value (tmp, Fdefault_value (sym));
3585 Fset_default (sym, old_value);
3587 break;
3588 case SPECPDL_LET_LOCAL:
3590 Lisp_Object symbol = specpdl_symbol (tmp);
3591 Lisp_Object where = specpdl_where (tmp);
3592 Lisp_Object old_value = specpdl_old_value (tmp);
3593 eassert (BUFFERP (where));
3595 /* If this was a local binding, reset the value in the appropriate
3596 buffer, but only if that buffer's binding still exists. */
3597 if (!NILP (Flocal_variable_p (symbol, where)))
3599 set_specpdl_old_value
3600 (tmp, Fbuffer_local_value (symbol, where));
3601 set_internal (symbol, old_value, where, 1);
3604 break;
3609 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3610 doc: /* Evaluate EXP in the context of some activation frame.
3611 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3612 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3614 union specbinding *pdl = get_backtrace_frame (nframes, base);
3615 ptrdiff_t count = SPECPDL_INDEX ();
3616 ptrdiff_t distance = specpdl_ptr - pdl;
3617 eassert (distance >= 0);
3619 if (!backtrace_p (pdl))
3620 error ("Activation frame not found!");
3622 backtrace_eval_unrewind (distance);
3623 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3625 /* Use eval_sub rather than Feval since the main motivation behind
3626 backtrace-eval is to be able to get/set the value of lexical variables
3627 from the debugger. */
3628 return unbind_to (count, eval_sub (exp));
3631 void
3632 mark_specpdl (void)
3634 union specbinding *pdl;
3635 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3637 switch (pdl->kind)
3639 case SPECPDL_UNWIND:
3640 mark_object (specpdl_arg (pdl));
3641 break;
3643 case SPECPDL_BACKTRACE:
3645 ptrdiff_t nargs = backtrace_nargs (pdl);
3646 mark_object (backtrace_function (pdl));
3647 if (nargs == UNEVALLED)
3648 nargs = 1;
3649 while (nargs--)
3650 mark_object (backtrace_args (pdl)[nargs]);
3652 break;
3654 case SPECPDL_LET_DEFAULT:
3655 case SPECPDL_LET_LOCAL:
3656 mark_object (specpdl_where (pdl));
3657 /* Fall through. */
3658 case SPECPDL_LET:
3659 mark_object (specpdl_symbol (pdl));
3660 mark_object (specpdl_old_value (pdl));
3661 break;
3666 void
3667 get_backtrace (Lisp_Object array)
3669 union specbinding *pdl = backtrace_next (backtrace_top ());
3670 ptrdiff_t i = 0, asize = ASIZE (array);
3672 /* Copy the backtrace contents into working memory. */
3673 for (; i < asize; i++)
3675 if (backtrace_p (pdl))
3677 ASET (array, i, backtrace_function (pdl));
3678 pdl = backtrace_next (pdl);
3680 else
3681 ASET (array, i, Qnil);
3685 Lisp_Object backtrace_top_function (void)
3687 union specbinding *pdl = backtrace_top ();
3688 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3691 void
3692 syms_of_eval (void)
3694 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3695 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3696 If Lisp code tries to increase the total number past this amount,
3697 an error is signaled.
3698 You can safely use a value considerably larger than the default value,
3699 if that proves inconveniently small. However, if you increase it too far,
3700 Emacs could run out of memory trying to make the stack bigger. */);
3702 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3703 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3705 This limit serves to catch infinite recursions for you before they cause
3706 actual stack overflow in C, which would be fatal for Emacs.
3707 You can safely make it considerably larger than its default value,
3708 if that proves inconveniently small. However, if you increase it too far,
3709 Emacs could overflow the real C stack, and crash. */);
3711 DEFVAR_LISP ("quit-flag", Vquit_flag,
3712 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3713 If the value is t, that means do an ordinary quit.
3714 If the value equals `throw-on-input', that means quit by throwing
3715 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3716 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3717 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3718 Vquit_flag = Qnil;
3720 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3721 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3722 Note that `quit-flag' will still be set by typing C-g,
3723 so a quit will be signaled as soon as `inhibit-quit' is nil.
3724 To prevent this happening, set `quit-flag' to nil
3725 before making `inhibit-quit' nil. */);
3726 Vinhibit_quit = Qnil;
3728 DEFSYM (Qinhibit_quit, "inhibit-quit");
3729 DEFSYM (Qautoload, "autoload");
3730 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3731 DEFSYM (Qmacro, "macro");
3732 DEFSYM (Qdeclare, "declare");
3734 /* Note that the process handling also uses Qexit, but we don't want
3735 to staticpro it twice, so we just do it here. */
3736 DEFSYM (Qexit, "exit");
3738 DEFSYM (Qinteractive, "interactive");
3739 DEFSYM (Qcommandp, "commandp");
3740 DEFSYM (Qand_rest, "&rest");
3741 DEFSYM (Qand_optional, "&optional");
3742 DEFSYM (Qclosure, "closure");
3743 DEFSYM (Qdebug, "debug");
3745 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3746 doc: /* Non-nil means never enter the debugger.
3747 Normally set while the debugger is already active, to avoid recursive
3748 invocations. */);
3749 Vinhibit_debugger = Qnil;
3751 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3752 doc: /* Non-nil means enter debugger if an error is signaled.
3753 Does not apply to errors handled by `condition-case' or those
3754 matched by `debug-ignored-errors'.
3755 If the value is a list, an error only means to enter the debugger
3756 if one of its condition symbols appears in the list.
3757 When you evaluate an expression interactively, this variable
3758 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3759 The command `toggle-debug-on-error' toggles this.
3760 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3761 Vdebug_on_error = Qnil;
3763 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3764 doc: /* List of errors for which the debugger should not be called.
3765 Each element may be a condition-name or a regexp that matches error messages.
3766 If any element applies to a given error, that error skips the debugger
3767 and just returns to top level.
3768 This overrides the variable `debug-on-error'.
3769 It does not apply to errors handled by `condition-case'. */);
3770 Vdebug_ignored_errors = Qnil;
3772 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3773 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3774 Does not apply if quit is handled by a `condition-case'. */);
3775 debug_on_quit = 0;
3777 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3778 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3780 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3781 doc: /* Non-nil means debugger may continue execution.
3782 This is nil when the debugger is called under circumstances where it
3783 might not be safe to continue. */);
3784 debugger_may_continue = 1;
3786 DEFVAR_LISP ("debugger", Vdebugger,
3787 doc: /* Function to call to invoke debugger.
3788 If due to frame exit, args are `exit' and the value being returned;
3789 this function's value will be returned instead of that.
3790 If due to error, args are `error' and a list of the args to `signal'.
3791 If due to `apply' or `funcall' entry, one arg, `lambda'.
3792 If due to `eval' entry, one arg, t. */);
3793 Vdebugger = Qnil;
3795 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3796 doc: /* If non-nil, this is a function for `signal' to call.
3797 It receives the same arguments that `signal' was given.
3798 The Edebug package uses this to regain control. */);
3799 Vsignal_hook_function = Qnil;
3801 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3802 doc: /* Non-nil means call the debugger regardless of condition handlers.
3803 Note that `debug-on-error', `debug-on-quit' and friends
3804 still determine whether to handle the particular condition. */);
3805 Vdebug_on_signal = Qnil;
3807 /* When lexical binding is being used,
3808 Vinternal_interpreter_environment is non-nil, and contains an alist
3809 of lexically-bound variable, or (t), indicating an empty
3810 environment. The lisp name of this variable would be
3811 `internal-interpreter-environment' if it weren't hidden.
3812 Every element of this list can be either a cons (VAR . VAL)
3813 specifying a lexical binding, or a single symbol VAR indicating
3814 that this variable should use dynamic scoping. */
3815 DEFSYM (Qinternal_interpreter_environment,
3816 "internal-interpreter-environment");
3817 DEFVAR_LISP ("internal-interpreter-environment",
3818 Vinternal_interpreter_environment,
3819 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3820 When lexical binding is not being used, this variable is nil.
3821 A value of `(t)' indicates an empty environment, otherwise it is an
3822 alist of active lexical bindings. */);
3823 Vinternal_interpreter_environment = Qnil;
3824 /* Don't export this variable to Elisp, so no one can mess with it
3825 (Just imagine if someone makes it buffer-local). */
3826 Funintern (Qinternal_interpreter_environment, Qnil);
3828 DEFSYM (Vrun_hooks, "run-hooks");
3830 staticpro (&Vautoload_queue);
3831 Vautoload_queue = Qnil;
3832 staticpro (&Vsignaling_function);
3833 Vsignaling_function = Qnil;
3835 inhibit_lisp_code = Qnil;
3837 defsubr (&Sor);
3838 defsubr (&Sand);
3839 defsubr (&Sif);
3840 defsubr (&Scond);
3841 defsubr (&Sprogn);
3842 defsubr (&Sprog1);
3843 defsubr (&Sprog2);
3844 defsubr (&Ssetq);
3845 defsubr (&Squote);
3846 defsubr (&Sfunction);
3847 defsubr (&Sdefault_toplevel_value);
3848 defsubr (&Sset_default_toplevel_value);
3849 defsubr (&Sdefvar);
3850 defsubr (&Sdefvaralias);
3851 defsubr (&Sdefconst);
3852 defsubr (&Smake_var_non_special);
3853 defsubr (&Slet);
3854 defsubr (&SletX);
3855 defsubr (&Swhile);
3856 defsubr (&Smacroexpand);
3857 defsubr (&Scatch);
3858 defsubr (&Sthrow);
3859 defsubr (&Sunwind_protect);
3860 defsubr (&Scondition_case);
3861 defsubr (&Ssignal);
3862 defsubr (&Scommandp);
3863 defsubr (&Sautoload);
3864 defsubr (&Sautoload_do_load);
3865 defsubr (&Seval);
3866 defsubr (&Sapply);
3867 defsubr (&Sfuncall);
3868 defsubr (&Srun_hooks);
3869 defsubr (&Srun_hook_with_args);
3870 defsubr (&Srun_hook_with_args_until_success);
3871 defsubr (&Srun_hook_with_args_until_failure);
3872 defsubr (&Srun_hook_wrapped);
3873 defsubr (&Sfetch_bytecode);
3874 defsubr (&Sbacktrace_debug);
3875 defsubr (&Sbacktrace);
3876 defsubr (&Sbacktrace_frame);
3877 defsubr (&Sbacktrace_eval);
3878 defsubr (&Sspecial_variable_p);
3879 defsubr (&Sfunctionp);