Make ">>" act as double template ender in C++ Mode.
[emacs.git] / src / eval.c
blob4b2e256a7224ea905f11c1a5002bbbdb5505d90c
1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2014 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "commands.h"
28 #include "keyboard.h"
29 #include "dispextern.h"
31 /* Chain of condition and catch handlers currently in effect. */
33 struct handler *handlerlist;
35 #ifdef DEBUG_GCPRO
36 /* Count levels of GCPRO to detect failure to UNGCPRO. */
37 int gcpro_level;
38 #endif
40 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
41 Lisp_Object Qinhibit_quit;
42 Lisp_Object Qand_rest;
43 static Lisp_Object Qand_optional;
44 static Lisp_Object Qinhibit_debugger;
45 static Lisp_Object Qdeclare;
46 Lisp_Object Qinternal_interpreter_environment, Qclosure;
48 static Lisp_Object Qdebug;
50 /* This holds either the symbol `run-hooks' or nil.
51 It is nil at an early stage of startup, and when Emacs
52 is shutting down. */
54 Lisp_Object Vrun_hooks;
56 /* Non-nil means record all fset's and provide's, to be undone
57 if the file being autoloaded is not fully loaded.
58 They are recorded by being consed onto the front of Vautoload_queue:
59 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
61 Lisp_Object Vautoload_queue;
63 /* Current number of specbindings allocated in specpdl, not counting
64 the dummy entry specpdl[-1]. */
66 ptrdiff_t specpdl_size;
68 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
69 only so that its address can be taken. */
71 union specbinding *specpdl;
73 /* Pointer to first unused element in specpdl. */
75 union specbinding *specpdl_ptr;
77 /* Depth in Lisp evaluations and function calls. */
79 EMACS_INT lisp_eval_depth;
81 /* The value of num_nonmacro_input_events as of the last time we
82 started to enter the debugger. If we decide to enter the debugger
83 again when this is still equal to num_nonmacro_input_events, then we
84 know that the debugger itself has an error, and we should just
85 signal the error instead of entering an infinite loop of debugger
86 invocations. */
88 static EMACS_INT when_entered_debugger;
90 /* The function from which the last `signal' was called. Set in
91 Fsignal. */
92 /* FIXME: We should probably get rid of this! */
93 Lisp_Object Vsignaling_function;
95 /* If non-nil, Lisp code must not be run since some part of Emacs is in
96 an inconsistent state. Currently unused. */
97 Lisp_Object inhibit_lisp_code;
99 /* These would ordinarily be static, but they need to be visible to GDB. */
100 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
101 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
102 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
103 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
104 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
106 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
107 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
109 static Lisp_Object
110 specpdl_symbol (union specbinding *pdl)
112 eassert (pdl->kind >= SPECPDL_LET);
113 return pdl->let.symbol;
116 static Lisp_Object
117 specpdl_old_value (union specbinding *pdl)
119 eassert (pdl->kind >= SPECPDL_LET);
120 return pdl->let.old_value;
123 static void
124 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
126 eassert (pdl->kind >= SPECPDL_LET);
127 pdl->let.old_value = val;
130 static Lisp_Object
131 specpdl_where (union specbinding *pdl)
133 eassert (pdl->kind > SPECPDL_LET);
134 return pdl->let.where;
137 static Lisp_Object
138 specpdl_arg (union specbinding *pdl)
140 eassert (pdl->kind == SPECPDL_UNWIND);
141 return pdl->unwind.arg;
144 Lisp_Object
145 backtrace_function (union specbinding *pdl)
147 eassert (pdl->kind == SPECPDL_BACKTRACE);
148 return pdl->bt.function;
151 static ptrdiff_t
152 backtrace_nargs (union specbinding *pdl)
154 eassert (pdl->kind == SPECPDL_BACKTRACE);
155 return pdl->bt.nargs;
158 Lisp_Object *
159 backtrace_args (union specbinding *pdl)
161 eassert (pdl->kind == SPECPDL_BACKTRACE);
162 return pdl->bt.args;
165 static bool
166 backtrace_debug_on_exit (union specbinding *pdl)
168 eassert (pdl->kind == SPECPDL_BACKTRACE);
169 return pdl->bt.debug_on_exit;
172 /* Functions to modify slots of backtrace records. */
174 static void
175 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
177 eassert (pdl->kind == SPECPDL_BACKTRACE);
178 pdl->bt.args = args;
181 static void
182 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
184 eassert (pdl->kind == SPECPDL_BACKTRACE);
185 pdl->bt.nargs = n;
188 static void
189 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
191 eassert (pdl->kind == SPECPDL_BACKTRACE);
192 pdl->bt.debug_on_exit = doe;
195 /* Helper functions to scan the backtrace. */
197 bool
198 backtrace_p (union specbinding *pdl)
199 { return pdl >= specpdl; }
201 union specbinding *
202 backtrace_top (void)
204 union specbinding *pdl = specpdl_ptr - 1;
205 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
206 pdl--;
207 return pdl;
210 union specbinding *
211 backtrace_next (union specbinding *pdl)
213 pdl--;
214 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
215 pdl--;
216 return pdl;
220 void
221 init_eval_once (void)
223 enum { size = 50 };
224 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
225 specpdl_size = size;
226 specpdl = specpdl_ptr = pdlvec + 1;
227 /* Don't forget to update docs (lispref node "Local Variables"). */
228 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
229 max_lisp_eval_depth = 600;
231 Vrun_hooks = Qnil;
234 static struct handler handlerlist_sentinel;
236 void
237 init_eval (void)
239 specpdl_ptr = specpdl;
240 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
241 This is important since handlerlist->nextfree holds the freelist
242 which would otherwise leak every time we unwind back to top-level. */
243 struct handler *c;
244 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
245 PUSH_HANDLER (c, Qunbound, CATCHER);
246 eassert (c == &handlerlist_sentinel);
247 handlerlist_sentinel.nextfree = NULL;
248 handlerlist_sentinel.next = NULL;
250 Vquit_flag = Qnil;
251 debug_on_next_call = 0;
252 lisp_eval_depth = 0;
253 #ifdef DEBUG_GCPRO
254 gcpro_level = 0;
255 #endif
256 /* This is less than the initial value of num_nonmacro_input_events. */
257 when_entered_debugger = -1;
260 /* Unwind-protect function used by call_debugger. */
262 static void
263 restore_stack_limits (Lisp_Object data)
265 max_specpdl_size = XINT (XCAR (data));
266 max_lisp_eval_depth = XINT (XCDR (data));
269 static void grow_specpdl (void);
271 /* Call the Lisp debugger, giving it argument ARG. */
273 Lisp_Object
274 call_debugger (Lisp_Object arg)
276 bool debug_while_redisplaying;
277 ptrdiff_t count = SPECPDL_INDEX ();
278 Lisp_Object val;
279 EMACS_INT old_depth = max_lisp_eval_depth;
280 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
281 EMACS_INT old_max = max (max_specpdl_size, count);
283 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
284 max_lisp_eval_depth = lisp_eval_depth + 40;
286 /* While debugging Bug#16603, previous value of 100 was found
287 too small to avoid specpdl overflow in the debugger itself. */
288 if (max_specpdl_size - 200 < count)
289 max_specpdl_size = count + 200;
291 if (old_max == count)
293 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
294 specpdl_ptr--;
295 grow_specpdl ();
298 /* Restore limits after leaving the debugger. */
299 record_unwind_protect (restore_stack_limits,
300 Fcons (make_number (old_max),
301 make_number (old_depth)));
303 #ifdef HAVE_WINDOW_SYSTEM
304 if (display_hourglass_p)
305 cancel_hourglass ();
306 #endif
308 debug_on_next_call = 0;
309 when_entered_debugger = num_nonmacro_input_events;
311 /* Resetting redisplaying_p to 0 makes sure that debug output is
312 displayed if the debugger is invoked during redisplay. */
313 debug_while_redisplaying = redisplaying_p;
314 redisplaying_p = 0;
315 specbind (intern ("debugger-may-continue"),
316 debug_while_redisplaying ? Qnil : Qt);
317 specbind (Qinhibit_redisplay, Qnil);
318 specbind (Qinhibit_debugger, Qt);
320 #if 0 /* Binding this prevents execution of Lisp code during
321 redisplay, which necessarily leads to display problems. */
322 specbind (Qinhibit_eval_during_redisplay, Qt);
323 #endif
325 val = apply1 (Vdebugger, arg);
327 /* Interrupting redisplay and resuming it later is not safe under
328 all circumstances. So, when the debugger returns, abort the
329 interrupted redisplay by going back to the top-level. */
330 if (debug_while_redisplaying)
331 Ftop_level ();
333 return unbind_to (count, val);
336 static void
337 do_debug_on_call (Lisp_Object code)
339 debug_on_next_call = 0;
340 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
341 call_debugger (list1 (code));
344 /* NOTE!!! Every function that can call EVAL must protect its args
345 and temporaries from garbage collection while it needs them.
346 The definition of `For' shows what you have to do. */
348 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
349 doc: /* Eval args until one of them yields non-nil, then return that value.
350 The remaining args are not evalled at all.
351 If all args return nil, return nil.
352 usage: (or CONDITIONS...) */)
353 (Lisp_Object args)
355 register Lisp_Object val = Qnil;
356 struct gcpro gcpro1;
358 GCPRO1 (args);
360 while (CONSP (args))
362 val = eval_sub (XCAR (args));
363 if (!NILP (val))
364 break;
365 args = XCDR (args);
368 UNGCPRO;
369 return val;
372 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
373 doc: /* Eval args until one of them yields nil, then return nil.
374 The remaining args are not evalled at all.
375 If no arg yields nil, return the last arg's value.
376 usage: (and CONDITIONS...) */)
377 (Lisp_Object args)
379 register Lisp_Object val = Qt;
380 struct gcpro gcpro1;
382 GCPRO1 (args);
384 while (CONSP (args))
386 val = eval_sub (XCAR (args));
387 if (NILP (val))
388 break;
389 args = XCDR (args);
392 UNGCPRO;
393 return val;
396 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
397 doc: /* If COND yields non-nil, do THEN, else do ELSE...
398 Returns the value of THEN or the value of the last of the ELSE's.
399 THEN must be one expression, but ELSE... can be zero or more expressions.
400 If COND yields nil, and there are no ELSE's, the value is nil.
401 usage: (if COND THEN ELSE...) */)
402 (Lisp_Object args)
404 Lisp_Object cond;
405 struct gcpro gcpro1;
407 GCPRO1 (args);
408 cond = eval_sub (XCAR (args));
409 UNGCPRO;
411 if (!NILP (cond))
412 return eval_sub (Fcar (XCDR (args)));
413 return Fprogn (XCDR (XCDR (args)));
416 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
417 doc: /* Try each clause until one succeeds.
418 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
419 and, if the value is non-nil, this clause succeeds:
420 then the expressions in BODY are evaluated and the last one's
421 value is the value of the cond-form.
422 If a clause has one element, as in (CONDITION), then the cond-form
423 returns CONDITION's value, if that is non-nil.
424 If no clause succeeds, cond returns nil.
425 usage: (cond CLAUSES...) */)
426 (Lisp_Object args)
428 Lisp_Object val = args;
429 struct gcpro gcpro1;
431 GCPRO1 (args);
432 while (CONSP (args))
434 Lisp_Object clause = XCAR (args);
435 val = eval_sub (Fcar (clause));
436 if (!NILP (val))
438 if (!NILP (XCDR (clause)))
439 val = Fprogn (XCDR (clause));
440 break;
442 args = XCDR (args);
444 UNGCPRO;
446 return val;
449 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
450 doc: /* Eval BODY forms sequentially and return value of last one.
451 usage: (progn BODY...) */)
452 (Lisp_Object body)
454 Lisp_Object val = Qnil;
455 struct gcpro gcpro1;
457 GCPRO1 (body);
459 while (CONSP (body))
461 val = eval_sub (XCAR (body));
462 body = XCDR (body);
465 UNGCPRO;
466 return val;
469 /* Evaluate BODY sequentially, discarding its value. Suitable for
470 record_unwind_protect. */
472 void
473 unwind_body (Lisp_Object body)
475 Fprogn (body);
478 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
479 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
480 The value of FIRST is saved during the evaluation of the remaining args,
481 whose values are discarded.
482 usage: (prog1 FIRST BODY...) */)
483 (Lisp_Object args)
485 Lisp_Object val;
486 Lisp_Object args_left;
487 struct gcpro gcpro1, gcpro2;
489 args_left = args;
490 val = args;
491 GCPRO2 (args, val);
493 val = eval_sub (XCAR (args_left));
494 while (CONSP (args_left = XCDR (args_left)))
495 eval_sub (XCAR (args_left));
497 UNGCPRO;
498 return val;
501 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
502 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
503 The value of FORM2 is saved during the evaluation of the
504 remaining args, whose values are discarded.
505 usage: (prog2 FORM1 FORM2 BODY...) */)
506 (Lisp_Object args)
508 struct gcpro gcpro1;
510 GCPRO1 (args);
511 eval_sub (XCAR (args));
512 UNGCPRO;
513 return Fprog1 (XCDR (args));
516 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
517 doc: /* Set each SYM to the value of its VAL.
518 The symbols SYM are variables; they are literal (not evaluated).
519 The values VAL are expressions; they are evaluated.
520 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
521 The second VAL is not computed until after the first SYM is set, and so on;
522 each VAL can use the new value of variables set earlier in the `setq'.
523 The return value of the `setq' form is the value of the last VAL.
524 usage: (setq [SYM VAL]...) */)
525 (Lisp_Object args)
527 Lisp_Object val, sym, lex_binding;
529 val = args;
530 if (CONSP (args))
532 Lisp_Object args_left = args;
533 struct gcpro gcpro1;
534 GCPRO1 (args);
538 val = eval_sub (Fcar (XCDR (args_left)));
539 sym = XCAR (args_left);
541 /* Like for eval_sub, we do not check declared_special here since
542 it's been done when let-binding. */
543 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
544 && SYMBOLP (sym)
545 && !NILP (lex_binding
546 = Fassq (sym, Vinternal_interpreter_environment)))
547 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
548 else
549 Fset (sym, val); /* SYM is dynamically bound. */
551 args_left = Fcdr (XCDR (args_left));
553 while (CONSP (args_left));
555 UNGCPRO;
558 return val;
561 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
562 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
563 Warning: `quote' does not construct its return value, but just returns
564 the value that was pre-constructed by the Lisp reader (see info node
565 `(elisp)Printed Representation').
566 This means that '(a . b) is not identical to (cons 'a 'b): the former
567 does not cons. Quoting should be reserved for constants that will
568 never be modified by side-effects, unless you like self-modifying code.
569 See the common pitfall in info node `(elisp)Rearrangement' for an example
570 of unexpected results when a quoted object is modified.
571 usage: (quote ARG) */)
572 (Lisp_Object args)
574 if (CONSP (XCDR (args)))
575 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
576 return XCAR (args);
579 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
580 doc: /* Like `quote', but preferred for objects which are functions.
581 In byte compilation, `function' causes its argument to be compiled.
582 `quote' cannot do that.
583 usage: (function ARG) */)
584 (Lisp_Object args)
586 Lisp_Object quoted = XCAR (args);
588 if (CONSP (XCDR (args)))
589 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
591 if (!NILP (Vinternal_interpreter_environment)
592 && CONSP (quoted)
593 && EQ (XCAR (quoted), Qlambda))
594 /* This is a lambda expression within a lexical environment;
595 return an interpreted closure instead of a simple lambda. */
596 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
597 XCDR (quoted)));
598 else
599 /* Simply quote the argument. */
600 return quoted;
604 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
605 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
606 Aliased variables always have the same value; setting one sets the other.
607 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
608 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
609 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
610 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
611 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
612 The return value is BASE-VARIABLE. */)
613 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
615 struct Lisp_Symbol *sym;
617 CHECK_SYMBOL (new_alias);
618 CHECK_SYMBOL (base_variable);
620 sym = XSYMBOL (new_alias);
622 if (sym->constant)
623 /* Not sure why, but why not? */
624 error ("Cannot make a constant an alias");
626 switch (sym->redirect)
628 case SYMBOL_FORWARDED:
629 error ("Cannot make an internal variable an alias");
630 case SYMBOL_LOCALIZED:
631 error ("Don't know how to make a localized variable an alias");
634 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
635 If n_a is bound, but b_v is not, set the value of b_v to n_a,
636 so that old-code that affects n_a before the aliasing is setup
637 still works. */
638 if (NILP (Fboundp (base_variable)))
639 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
642 union specbinding *p;
644 for (p = specpdl_ptr; p > specpdl; )
645 if ((--p)->kind >= SPECPDL_LET
646 && (EQ (new_alias, specpdl_symbol (p))))
647 error ("Don't know how to make a let-bound variable an alias");
650 sym->declared_special = 1;
651 XSYMBOL (base_variable)->declared_special = 1;
652 sym->redirect = SYMBOL_VARALIAS;
653 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
654 sym->constant = SYMBOL_CONSTANT_P (base_variable);
655 LOADHIST_ATTACH (new_alias);
656 /* Even if docstring is nil: remove old docstring. */
657 Fput (new_alias, Qvariable_documentation, docstring);
659 return base_variable;
662 static union specbinding *
663 default_toplevel_binding (Lisp_Object symbol)
665 union specbinding *binding = NULL;
666 union specbinding *pdl = specpdl_ptr;
667 while (pdl > specpdl)
669 switch ((--pdl)->kind)
671 case SPECPDL_LET_DEFAULT:
672 case SPECPDL_LET:
673 if (EQ (specpdl_symbol (pdl), symbol))
674 binding = pdl;
675 break;
678 return binding;
681 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
682 doc: /* Return SYMBOL's toplevel default value.
683 "Toplevel" means outside of any let binding. */)
684 (Lisp_Object symbol)
686 union specbinding *binding = default_toplevel_binding (symbol);
687 Lisp_Object value
688 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
689 if (!EQ (value, Qunbound))
690 return value;
691 xsignal1 (Qvoid_variable, symbol);
694 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
695 Sset_default_toplevel_value, 2, 2, 0,
696 doc: /* Set SYMBOL's toplevel default value to VALUE.
697 "Toplevel" means outside of any let binding. */)
698 (Lisp_Object symbol, Lisp_Object value)
700 union specbinding *binding = default_toplevel_binding (symbol);
701 if (binding)
702 set_specpdl_old_value (binding, value);
703 else
704 Fset_default (symbol, value);
705 return Qnil;
708 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
709 doc: /* Define SYMBOL as a variable, and return SYMBOL.
710 You are not required to define a variable in order to use it, but
711 defining it lets you supply an initial value and documentation, which
712 can be referred to by the Emacs help facilities and other programming
713 tools. The `defvar' form also declares the variable as \"special\",
714 so that it is always dynamically bound even if `lexical-binding' is t.
716 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
717 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
718 default value is what is set; buffer-local values are not affected.
719 If INITVALUE is missing, SYMBOL's value is not set.
721 If SYMBOL has a local binding, then this form affects the local
722 binding. This is usually not what you want. Thus, if you need to
723 load a file defining variables, with this form or with `defconst' or
724 `defcustom', you should always load that file _outside_ any bindings
725 for these variables. \(`defconst' and `defcustom' behave similarly in
726 this respect.)
728 The optional argument DOCSTRING is a documentation string for the
729 variable.
731 To define a user option, use `defcustom' instead of `defvar'.
732 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
733 (Lisp_Object args)
735 Lisp_Object sym, tem, tail;
737 sym = XCAR (args);
738 tail = XCDR (args);
740 if (CONSP (tail))
742 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
743 error ("Too many arguments");
745 tem = Fdefault_boundp (sym);
747 /* Do it before evaluating the initial value, for self-references. */
748 XSYMBOL (sym)->declared_special = 1;
750 if (NILP (tem))
751 Fset_default (sym, eval_sub (XCAR (tail)));
752 else
753 { /* Check if there is really a global binding rather than just a let
754 binding that shadows the global unboundness of the var. */
755 union specbinding *binding = default_toplevel_binding (sym);
756 if (binding && EQ (specpdl_old_value (binding), Qunbound))
758 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
761 tail = XCDR (tail);
762 tem = Fcar (tail);
763 if (!NILP (tem))
765 if (!NILP (Vpurify_flag))
766 tem = Fpurecopy (tem);
767 Fput (sym, Qvariable_documentation, tem);
769 LOADHIST_ATTACH (sym);
771 else if (!NILP (Vinternal_interpreter_environment)
772 && !XSYMBOL (sym)->declared_special)
773 /* A simple (defvar foo) with lexical scoping does "nothing" except
774 declare that var to be dynamically scoped *locally* (i.e. within
775 the current file or let-block). */
776 Vinternal_interpreter_environment
777 = Fcons (sym, Vinternal_interpreter_environment);
778 else
780 /* Simple (defvar <var>) should not count as a definition at all.
781 It could get in the way of other definitions, and unloading this
782 package could try to make the variable unbound. */
785 return sym;
788 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
789 doc: /* Define SYMBOL as a constant variable.
790 This declares that neither programs nor users should ever change the
791 value. This constancy is not actually enforced by Emacs Lisp, but
792 SYMBOL is marked as a special variable so that it is never lexically
793 bound.
795 The `defconst' form always sets the value of SYMBOL to the result of
796 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
797 what is set; buffer-local values are not affected. If SYMBOL has a
798 local binding, then this form sets the local binding's value.
799 However, you should normally not make local bindings for variables
800 defined with this form.
802 The optional DOCSTRING specifies the variable's documentation string.
803 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
804 (Lisp_Object args)
806 Lisp_Object sym, tem;
808 sym = XCAR (args);
809 if (CONSP (Fcdr (XCDR (XCDR (args)))))
810 error ("Too many arguments");
812 tem = eval_sub (Fcar (XCDR (args)));
813 if (!NILP (Vpurify_flag))
814 tem = Fpurecopy (tem);
815 Fset_default (sym, tem);
816 XSYMBOL (sym)->declared_special = 1;
817 tem = Fcar (XCDR (XCDR (args)));
818 if (!NILP (tem))
820 if (!NILP (Vpurify_flag))
821 tem = Fpurecopy (tem);
822 Fput (sym, Qvariable_documentation, tem);
824 Fput (sym, Qrisky_local_variable, Qt);
825 LOADHIST_ATTACH (sym);
826 return sym;
829 /* Make SYMBOL lexically scoped. */
830 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
831 Smake_var_non_special, 1, 1, 0,
832 doc: /* Internal function. */)
833 (Lisp_Object symbol)
835 CHECK_SYMBOL (symbol);
836 XSYMBOL (symbol)->declared_special = 0;
837 return Qnil;
841 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
842 doc: /* Bind variables according to VARLIST then eval BODY.
843 The value of the last form in BODY is returned.
844 Each element of VARLIST is a symbol (which is bound to nil)
845 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
846 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
847 usage: (let* VARLIST BODY...) */)
848 (Lisp_Object args)
850 Lisp_Object varlist, var, val, elt, lexenv;
851 ptrdiff_t count = SPECPDL_INDEX ();
852 struct gcpro gcpro1, gcpro2, gcpro3;
854 GCPRO3 (args, elt, varlist);
856 lexenv = Vinternal_interpreter_environment;
858 varlist = XCAR (args);
859 while (CONSP (varlist))
861 QUIT;
863 elt = XCAR (varlist);
864 if (SYMBOLP (elt))
866 var = elt;
867 val = Qnil;
869 else if (! NILP (Fcdr (Fcdr (elt))))
870 signal_error ("`let' bindings can have only one value-form", elt);
871 else
873 var = Fcar (elt);
874 val = eval_sub (Fcar (Fcdr (elt)));
877 if (!NILP (lexenv) && SYMBOLP (var)
878 && !XSYMBOL (var)->declared_special
879 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
880 /* Lexically bind VAR by adding it to the interpreter's binding
881 alist. */
883 Lisp_Object newenv
884 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
885 if (EQ (Vinternal_interpreter_environment, lexenv))
886 /* Save the old lexical environment on the specpdl stack,
887 but only for the first lexical binding, since we'll never
888 need to revert to one of the intermediate ones. */
889 specbind (Qinternal_interpreter_environment, newenv);
890 else
891 Vinternal_interpreter_environment = newenv;
893 else
894 specbind (var, val);
896 varlist = XCDR (varlist);
898 UNGCPRO;
899 val = Fprogn (XCDR (args));
900 return unbind_to (count, val);
903 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
904 doc: /* Bind variables according to VARLIST then eval BODY.
905 The value of the last form in BODY is returned.
906 Each element of VARLIST is a symbol (which is bound to nil)
907 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
908 All the VALUEFORMs are evalled before any symbols are bound.
909 usage: (let VARLIST BODY...) */)
910 (Lisp_Object args)
912 Lisp_Object *temps, tem, lexenv;
913 register Lisp_Object elt, varlist;
914 ptrdiff_t count = SPECPDL_INDEX ();
915 ptrdiff_t argnum;
916 struct gcpro gcpro1, gcpro2;
917 USE_SAFE_ALLOCA;
919 varlist = XCAR (args);
921 /* Make space to hold the values to give the bound variables. */
922 elt = Flength (varlist);
923 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
925 /* Compute the values and store them in `temps'. */
927 GCPRO2 (args, *temps);
928 gcpro2.nvars = 0;
930 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
932 QUIT;
933 elt = XCAR (varlist);
934 if (SYMBOLP (elt))
935 temps [argnum++] = Qnil;
936 else if (! NILP (Fcdr (Fcdr (elt))))
937 signal_error ("`let' bindings can have only one value-form", elt);
938 else
939 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
940 gcpro2.nvars = argnum;
942 UNGCPRO;
944 lexenv = Vinternal_interpreter_environment;
946 varlist = XCAR (args);
947 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
949 Lisp_Object var;
951 elt = XCAR (varlist);
952 var = SYMBOLP (elt) ? elt : Fcar (elt);
953 tem = temps[argnum++];
955 if (!NILP (lexenv) && SYMBOLP (var)
956 && !XSYMBOL (var)->declared_special
957 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
958 /* Lexically bind VAR by adding it to the lexenv alist. */
959 lexenv = Fcons (Fcons (var, tem), lexenv);
960 else
961 /* Dynamically bind VAR. */
962 specbind (var, tem);
965 if (!EQ (lexenv, Vinternal_interpreter_environment))
966 /* Instantiate a new lexical environment. */
967 specbind (Qinternal_interpreter_environment, lexenv);
969 elt = Fprogn (XCDR (args));
970 SAFE_FREE ();
971 return unbind_to (count, elt);
974 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
975 doc: /* If TEST yields non-nil, eval BODY... and repeat.
976 The order of execution is thus TEST, BODY, TEST, BODY and so on
977 until TEST returns nil.
978 usage: (while TEST BODY...) */)
979 (Lisp_Object args)
981 Lisp_Object test, body;
982 struct gcpro gcpro1, gcpro2;
984 GCPRO2 (test, body);
986 test = XCAR (args);
987 body = XCDR (args);
988 while (!NILP (eval_sub (test)))
990 QUIT;
991 Fprogn (body);
994 UNGCPRO;
995 return Qnil;
998 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
999 doc: /* Return result of expanding macros at top level of FORM.
1000 If FORM is not a macro call, it is returned unchanged.
1001 Otherwise, the macro is expanded and the expansion is considered
1002 in place of FORM. When a non-macro-call results, it is returned.
1004 The second optional arg ENVIRONMENT specifies an environment of macro
1005 definitions to shadow the loaded ones for use in file byte-compilation. */)
1006 (Lisp_Object form, Lisp_Object environment)
1008 /* With cleanups from Hallvard Furuseth. */
1009 register Lisp_Object expander, sym, def, tem;
1011 while (1)
1013 /* Come back here each time we expand a macro call,
1014 in case it expands into another macro call. */
1015 if (!CONSP (form))
1016 break;
1017 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1018 def = sym = XCAR (form);
1019 tem = Qnil;
1020 /* Trace symbols aliases to other symbols
1021 until we get a symbol that is not an alias. */
1022 while (SYMBOLP (def))
1024 QUIT;
1025 sym = def;
1026 tem = Fassq (sym, environment);
1027 if (NILP (tem))
1029 def = XSYMBOL (sym)->function;
1030 if (!NILP (def))
1031 continue;
1033 break;
1035 /* Right now TEM is the result from SYM in ENVIRONMENT,
1036 and if TEM is nil then DEF is SYM's function definition. */
1037 if (NILP (tem))
1039 /* SYM is not mentioned in ENVIRONMENT.
1040 Look at its function definition. */
1041 struct gcpro gcpro1;
1042 GCPRO1 (form);
1043 def = Fautoload_do_load (def, sym, Qmacro);
1044 UNGCPRO;
1045 if (!CONSP (def))
1046 /* Not defined or definition not suitable. */
1047 break;
1048 if (!EQ (XCAR (def), Qmacro))
1049 break;
1050 else expander = XCDR (def);
1052 else
1054 expander = XCDR (tem);
1055 if (NILP (expander))
1056 break;
1059 Lisp_Object newform = apply1 (expander, XCDR (form));
1060 if (EQ (form, newform))
1061 break;
1062 else
1063 form = newform;
1066 return form;
1069 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1070 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1071 TAG is evalled to get the tag to use; it must not be nil.
1073 Then the BODY is executed.
1074 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1075 If no throw happens, `catch' returns the value of the last BODY form.
1076 If a throw happens, it specifies the value to return from `catch'.
1077 usage: (catch TAG BODY...) */)
1078 (Lisp_Object args)
1080 register Lisp_Object tag;
1081 struct gcpro gcpro1;
1083 GCPRO1 (args);
1084 tag = eval_sub (XCAR (args));
1085 UNGCPRO;
1086 return internal_catch (tag, Fprogn, XCDR (args));
1089 /* Assert that E is true, as a comment only. Use this instead of
1090 eassert (E) when E contains variables that might be clobbered by a
1091 longjmp. */
1093 #define clobbered_eassert(E) ((void) 0)
1095 /* Set up a catch, then call C function FUNC on argument ARG.
1096 FUNC should return a Lisp_Object.
1097 This is how catches are done from within C code. */
1099 Lisp_Object
1100 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1102 /* This structure is made part of the chain `catchlist'. */
1103 struct handler *c;
1105 /* Fill in the components of c, and put it on the list. */
1106 PUSH_HANDLER (c, tag, CATCHER);
1108 /* Call FUNC. */
1109 if (! sys_setjmp (c->jmp))
1111 Lisp_Object val = (*func) (arg);
1112 clobbered_eassert (handlerlist == c);
1113 handlerlist = handlerlist->next;
1114 return val;
1116 else
1117 { /* Throw works by a longjmp that comes right here. */
1118 Lisp_Object val = handlerlist->val;
1119 clobbered_eassert (handlerlist == c);
1120 handlerlist = handlerlist->next;
1121 return val;
1125 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1126 jump to that CATCH, returning VALUE as the value of that catch.
1128 This is the guts of Fthrow and Fsignal; they differ only in the way
1129 they choose the catch tag to throw to. A catch tag for a
1130 condition-case form has a TAG of Qnil.
1132 Before each catch is discarded, unbind all special bindings and
1133 execute all unwind-protect clauses made above that catch. Unwind
1134 the handler stack as we go, so that the proper handlers are in
1135 effect for each unwind-protect clause we run. At the end, restore
1136 some static info saved in CATCH, and longjmp to the location
1137 specified there.
1139 This is used for correct unwinding in Fthrow and Fsignal. */
1141 static _Noreturn void
1142 unwind_to_catch (struct handler *catch, Lisp_Object value)
1144 bool last_time;
1146 eassert (catch->next);
1148 /* Save the value in the tag. */
1149 catch->val = value;
1151 /* Restore certain special C variables. */
1152 set_poll_suppress_count (catch->poll_suppress_count);
1153 unblock_input_to (catch->interrupt_input_blocked);
1154 immediate_quit = 0;
1158 /* Unwind the specpdl stack, and then restore the proper set of
1159 handlers. */
1160 unbind_to (handlerlist->pdlcount, Qnil);
1161 last_time = handlerlist == catch;
1162 if (! last_time)
1163 handlerlist = handlerlist->next;
1165 while (! last_time);
1167 eassert (handlerlist == catch);
1169 byte_stack_list = catch->byte_stack;
1170 gcprolist = catch->gcpro;
1171 #ifdef DEBUG_GCPRO
1172 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1173 #endif
1174 lisp_eval_depth = catch->lisp_eval_depth;
1176 sys_longjmp (catch->jmp, 1);
1179 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1180 doc: /* Throw to the catch for TAG and return VALUE from it.
1181 Both TAG and VALUE are evalled. */)
1182 (register Lisp_Object tag, Lisp_Object value)
1184 struct handler *c;
1186 if (!NILP (tag))
1187 for (c = handlerlist; c; c = c->next)
1189 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1190 unwind_to_catch (c, value);
1192 xsignal2 (Qno_catch, tag, value);
1196 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1197 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1198 If BODYFORM completes normally, its value is returned
1199 after executing the UNWINDFORMS.
1200 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1201 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1202 (Lisp_Object args)
1204 Lisp_Object val;
1205 ptrdiff_t count = SPECPDL_INDEX ();
1207 record_unwind_protect (unwind_body, XCDR (args));
1208 val = eval_sub (XCAR (args));
1209 return unbind_to (count, val);
1212 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1213 doc: /* Regain control when an error is signaled.
1214 Executes BODYFORM and returns its value if no error happens.
1215 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1216 where the BODY is made of Lisp expressions.
1218 A handler is applicable to an error
1219 if CONDITION-NAME is one of the error's condition names.
1220 If an error happens, the first applicable handler is run.
1222 The car of a handler may be a list of condition names instead of a
1223 single condition name; then it handles all of them. If the special
1224 condition name `debug' is present in this list, it allows another
1225 condition in the list to run the debugger if `debug-on-error' and the
1226 other usual mechanisms says it should (otherwise, `condition-case'
1227 suppresses the debugger).
1229 When a handler handles an error, control returns to the `condition-case'
1230 and it executes the handler's BODY...
1231 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1232 \(If VAR is nil, the handler can't access that information.)
1233 Then the value of the last BODY form is returned from the `condition-case'
1234 expression.
1236 See also the function `signal' for more info.
1237 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1238 (Lisp_Object args)
1240 Lisp_Object var = XCAR (args);
1241 Lisp_Object bodyform = XCAR (XCDR (args));
1242 Lisp_Object handlers = XCDR (XCDR (args));
1244 return internal_lisp_condition_case (var, bodyform, handlers);
1247 /* Like Fcondition_case, but the args are separate
1248 rather than passed in a list. Used by Fbyte_code. */
1250 Lisp_Object
1251 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1252 Lisp_Object handlers)
1254 Lisp_Object val;
1255 struct handler *c;
1256 struct handler *oldhandlerlist = handlerlist;
1257 int clausenb = 0;
1259 CHECK_SYMBOL (var);
1261 for (val = handlers; CONSP (val); val = XCDR (val))
1263 Lisp_Object tem = XCAR (val);
1264 clausenb++;
1265 if (! (NILP (tem)
1266 || (CONSP (tem)
1267 && (SYMBOLP (XCAR (tem))
1268 || CONSP (XCAR (tem))))))
1269 error ("Invalid condition handler: %s",
1270 SDATA (Fprin1_to_string (tem, Qt)));
1273 { /* The first clause is the one that should be checked first, so it should
1274 be added to handlerlist last. So we build in `clauses' a table that
1275 contains `handlers' but in reverse order. */
1276 Lisp_Object *clauses = alloca (clausenb * sizeof (Lisp_Object *));
1277 Lisp_Object *volatile clauses_volatile = clauses;
1278 int i = clausenb;
1279 for (val = handlers; CONSP (val); val = XCDR (val))
1280 clauses[--i] = XCAR (val);
1281 for (i = 0; i < clausenb; i++)
1283 Lisp_Object clause = clauses[i];
1284 Lisp_Object condition = XCAR (clause);
1285 if (!CONSP (condition))
1286 condition = Fcons (condition, Qnil);
1287 PUSH_HANDLER (c, condition, CONDITION_CASE);
1288 if (sys_setjmp (c->jmp))
1290 ptrdiff_t count = SPECPDL_INDEX ();
1291 Lisp_Object val = handlerlist->val;
1292 Lisp_Object *chosen_clause = clauses_volatile;
1293 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1294 chosen_clause++;
1295 handlerlist = oldhandlerlist;
1296 if (!NILP (var))
1298 if (!NILP (Vinternal_interpreter_environment))
1299 specbind (Qinternal_interpreter_environment,
1300 Fcons (Fcons (var, val),
1301 Vinternal_interpreter_environment));
1302 else
1303 specbind (var, val);
1305 val = Fprogn (XCDR (*chosen_clause));
1306 /* Note that this just undoes the binding of var; whoever
1307 longjumped to us unwound the stack to c.pdlcount before
1308 throwing. */
1309 if (!NILP (var))
1310 unbind_to (count, Qnil);
1311 return val;
1316 val = eval_sub (bodyform);
1317 handlerlist = oldhandlerlist;
1318 return val;
1321 /* Call the function BFUN with no arguments, catching errors within it
1322 according to HANDLERS. If there is an error, call HFUN with
1323 one argument which is the data that describes the error:
1324 (SIGNALNAME . DATA)
1326 HANDLERS can be a list of conditions to catch.
1327 If HANDLERS is Qt, catch all errors.
1328 If HANDLERS is Qerror, catch all errors
1329 but allow the debugger to run if that is enabled. */
1331 Lisp_Object
1332 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1333 Lisp_Object (*hfun) (Lisp_Object))
1335 Lisp_Object val;
1336 struct handler *c;
1338 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1339 if (sys_setjmp (c->jmp))
1341 Lisp_Object val = handlerlist->val;
1342 clobbered_eassert (handlerlist == c);
1343 handlerlist = handlerlist->next;
1344 return (*hfun) (val);
1347 val = (*bfun) ();
1348 clobbered_eassert (handlerlist == c);
1349 handlerlist = handlerlist->next;
1350 return val;
1353 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1355 Lisp_Object
1356 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1357 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1359 Lisp_Object val;
1360 struct handler *c;
1362 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1363 if (sys_setjmp (c->jmp))
1365 Lisp_Object val = handlerlist->val;
1366 clobbered_eassert (handlerlist == c);
1367 handlerlist = handlerlist->next;
1368 return (*hfun) (val);
1371 val = (*bfun) (arg);
1372 clobbered_eassert (handlerlist == c);
1373 handlerlist = handlerlist->next;
1374 return val;
1377 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1378 its arguments. */
1380 Lisp_Object
1381 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1382 Lisp_Object arg1,
1383 Lisp_Object arg2,
1384 Lisp_Object handlers,
1385 Lisp_Object (*hfun) (Lisp_Object))
1387 Lisp_Object val;
1388 struct handler *c;
1390 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1391 if (sys_setjmp (c->jmp))
1393 Lisp_Object val = handlerlist->val;
1394 clobbered_eassert (handlerlist == c);
1395 handlerlist = handlerlist->next;
1396 return (*hfun) (val);
1399 val = (*bfun) (arg1, arg2);
1400 clobbered_eassert (handlerlist == c);
1401 handlerlist = handlerlist->next;
1402 return val;
1405 /* Like internal_condition_case but call BFUN with NARGS as first,
1406 and ARGS as second argument. */
1408 Lisp_Object
1409 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1410 ptrdiff_t nargs,
1411 Lisp_Object *args,
1412 Lisp_Object handlers,
1413 Lisp_Object (*hfun) (Lisp_Object err,
1414 ptrdiff_t nargs,
1415 Lisp_Object *args))
1417 Lisp_Object val;
1418 struct handler *c;
1420 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1421 if (sys_setjmp (c->jmp))
1423 Lisp_Object val = handlerlist->val;
1424 clobbered_eassert (handlerlist == c);
1425 handlerlist = handlerlist->next;
1426 return (*hfun) (val, nargs, args);
1429 val = (*bfun) (nargs, args);
1430 clobbered_eassert (handlerlist == c);
1431 handlerlist = handlerlist->next;
1432 return val;
1436 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1437 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1438 Lisp_Object data);
1440 void
1441 process_quit_flag (void)
1443 Lisp_Object flag = Vquit_flag;
1444 Vquit_flag = Qnil;
1445 if (EQ (flag, Qkill_emacs))
1446 Fkill_emacs (Qnil);
1447 if (EQ (Vthrow_on_input, flag))
1448 Fthrow (Vthrow_on_input, Qt);
1449 Fsignal (Qquit, Qnil);
1452 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1453 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1454 This function does not return.
1456 An error symbol is a symbol with an `error-conditions' property
1457 that is a list of condition names.
1458 A handler for any of those names will get to handle this signal.
1459 The symbol `error' should normally be one of them.
1461 DATA should be a list. Its elements are printed as part of the error message.
1462 See Info anchor `(elisp)Definition of signal' for some details on how this
1463 error message is constructed.
1464 If the signal is handled, DATA is made available to the handler.
1465 See also the function `condition-case'. */)
1466 (Lisp_Object error_symbol, Lisp_Object data)
1468 /* When memory is full, ERROR-SYMBOL is nil,
1469 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1470 That is a special case--don't do this in other situations. */
1471 Lisp_Object conditions;
1472 Lisp_Object string;
1473 Lisp_Object real_error_symbol
1474 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1475 register Lisp_Object clause = Qnil;
1476 struct handler *h;
1478 immediate_quit = 0;
1479 abort_on_gc = 0;
1480 if (gc_in_progress || waiting_for_input)
1481 emacs_abort ();
1483 #if 0 /* rms: I don't know why this was here,
1484 but it is surely wrong for an error that is handled. */
1485 #ifdef HAVE_WINDOW_SYSTEM
1486 if (display_hourglass_p)
1487 cancel_hourglass ();
1488 #endif
1489 #endif
1491 /* This hook is used by edebug. */
1492 if (! NILP (Vsignal_hook_function)
1493 && ! NILP (error_symbol))
1495 /* Edebug takes care of restoring these variables when it exits. */
1496 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1497 max_lisp_eval_depth = lisp_eval_depth + 20;
1499 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1500 max_specpdl_size = SPECPDL_INDEX () + 40;
1502 call2 (Vsignal_hook_function, error_symbol, data);
1505 conditions = Fget (real_error_symbol, Qerror_conditions);
1507 /* Remember from where signal was called. Skip over the frame for
1508 `signal' itself. If a frame for `error' follows, skip that,
1509 too. Don't do this when ERROR_SYMBOL is nil, because that
1510 is a memory-full error. */
1511 Vsignaling_function = Qnil;
1512 if (!NILP (error_symbol))
1514 union specbinding *pdl = backtrace_next (backtrace_top ());
1515 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1516 pdl = backtrace_next (pdl);
1517 if (backtrace_p (pdl))
1518 Vsignaling_function = backtrace_function (pdl);
1521 for (h = handlerlist; h; h = h->next)
1523 if (h->type != CONDITION_CASE)
1524 continue;
1525 clause = find_handler_clause (h->tag_or_ch, conditions);
1526 if (!NILP (clause))
1527 break;
1530 if (/* Don't run the debugger for a memory-full error.
1531 (There is no room in memory to do that!) */
1532 !NILP (error_symbol)
1533 && (!NILP (Vdebug_on_signal)
1534 /* If no handler is present now, try to run the debugger. */
1535 || NILP (clause)
1536 /* A `debug' symbol in the handler list disables the normal
1537 suppression of the debugger. */
1538 || (CONSP (clause) && CONSP (clause)
1539 && !NILP (Fmemq (Qdebug, clause)))
1540 /* Special handler that means "print a message and run debugger
1541 if requested". */
1542 || EQ (h->tag_or_ch, Qerror)))
1544 bool debugger_called
1545 = maybe_call_debugger (conditions, error_symbol, data);
1546 /* We can't return values to code which signaled an error, but we
1547 can continue code which has signaled a quit. */
1548 if (debugger_called && EQ (real_error_symbol, Qquit))
1549 return Qnil;
1552 if (!NILP (clause))
1554 Lisp_Object unwind_data
1555 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1557 unwind_to_catch (h, unwind_data);
1559 else
1561 if (handlerlist != &handlerlist_sentinel)
1562 /* FIXME: This will come right back here if there's no `top-level'
1563 catcher. A better solution would be to abort here, and instead
1564 add a catch-all condition handler so we never come here. */
1565 Fthrow (Qtop_level, Qt);
1568 if (! NILP (error_symbol))
1569 data = Fcons (error_symbol, data);
1571 string = Ferror_message_string (data);
1572 fatal ("%s", SDATA (string));
1575 /* Internal version of Fsignal that never returns.
1576 Used for anything but Qquit (which can return from Fsignal). */
1578 void
1579 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1581 Fsignal (error_symbol, data);
1582 emacs_abort ();
1585 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1587 void
1588 xsignal0 (Lisp_Object error_symbol)
1590 xsignal (error_symbol, Qnil);
1593 void
1594 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1596 xsignal (error_symbol, list1 (arg));
1599 void
1600 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1602 xsignal (error_symbol, list2 (arg1, arg2));
1605 void
1606 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1608 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1611 /* Signal `error' with message S, and additional arg ARG.
1612 If ARG is not a genuine list, make it a one-element list. */
1614 void
1615 signal_error (const char *s, Lisp_Object arg)
1617 Lisp_Object tortoise, hare;
1619 hare = tortoise = arg;
1620 while (CONSP (hare))
1622 hare = XCDR (hare);
1623 if (!CONSP (hare))
1624 break;
1626 hare = XCDR (hare);
1627 tortoise = XCDR (tortoise);
1629 if (EQ (hare, tortoise))
1630 break;
1633 if (!NILP (hare))
1634 arg = list1 (arg);
1636 xsignal (Qerror, Fcons (build_string (s), arg));
1640 /* Return true if LIST is a non-nil atom or
1641 a list containing one of CONDITIONS. */
1643 static bool
1644 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1646 if (NILP (list))
1647 return 0;
1648 if (! CONSP (list))
1649 return 1;
1651 while (CONSP (conditions))
1653 Lisp_Object this, tail;
1654 this = XCAR (conditions);
1655 for (tail = list; CONSP (tail); tail = XCDR (tail))
1656 if (EQ (XCAR (tail), this))
1657 return 1;
1658 conditions = XCDR (conditions);
1660 return 0;
1663 /* Return true if an error with condition-symbols CONDITIONS,
1664 and described by SIGNAL-DATA, should skip the debugger
1665 according to debugger-ignored-errors. */
1667 static bool
1668 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1670 Lisp_Object tail;
1671 bool first_string = 1;
1672 Lisp_Object error_message;
1674 error_message = Qnil;
1675 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1677 if (STRINGP (XCAR (tail)))
1679 if (first_string)
1681 error_message = Ferror_message_string (data);
1682 first_string = 0;
1685 if (fast_string_match (XCAR (tail), error_message) >= 0)
1686 return 1;
1688 else
1690 Lisp_Object contail;
1692 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1693 if (EQ (XCAR (tail), XCAR (contail)))
1694 return 1;
1698 return 0;
1701 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1702 SIG and DATA describe the signal. There are two ways to pass them:
1703 = SIG is the error symbol, and DATA is the rest of the data.
1704 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1705 This is for memory-full errors only. */
1706 static bool
1707 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1709 Lisp_Object combined_data;
1711 combined_data = Fcons (sig, data);
1713 if (
1714 /* Don't try to run the debugger with interrupts blocked.
1715 The editing loop would return anyway. */
1716 ! input_blocked_p ()
1717 && NILP (Vinhibit_debugger)
1718 /* Does user want to enter debugger for this kind of error? */
1719 && (EQ (sig, Qquit)
1720 ? debug_on_quit
1721 : wants_debugger (Vdebug_on_error, conditions))
1722 && ! skip_debugger (conditions, combined_data)
1723 /* RMS: What's this for? */
1724 && when_entered_debugger < num_nonmacro_input_events)
1726 call_debugger (list2 (Qerror, combined_data));
1727 return 1;
1730 return 0;
1733 static Lisp_Object
1734 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1736 register Lisp_Object h;
1738 /* t is used by handlers for all conditions, set up by C code. */
1739 if (EQ (handlers, Qt))
1740 return Qt;
1742 /* error is used similarly, but means print an error message
1743 and run the debugger if that is enabled. */
1744 if (EQ (handlers, Qerror))
1745 return Qt;
1747 for (h = handlers; CONSP (h); h = XCDR (h))
1749 Lisp_Object handler = XCAR (h);
1750 if (!NILP (Fmemq (handler, conditions)))
1751 return handlers;
1754 return Qnil;
1758 /* Dump an error message; called like vprintf. */
1759 void
1760 verror (const char *m, va_list ap)
1762 char buf[4000];
1763 ptrdiff_t size = sizeof buf;
1764 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1765 char *buffer = buf;
1766 ptrdiff_t used;
1767 Lisp_Object string;
1769 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1770 string = make_string (buffer, used);
1771 if (buffer != buf)
1772 xfree (buffer);
1774 xsignal1 (Qerror, string);
1778 /* Dump an error message; called like printf. */
1780 /* VARARGS 1 */
1781 void
1782 error (const char *m, ...)
1784 va_list ap;
1785 va_start (ap, m);
1786 verror (m, ap);
1789 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1790 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1791 This means it contains a description for how to read arguments to give it.
1792 The value is nil for an invalid function or a symbol with no function
1793 definition.
1795 Interactively callable functions include strings and vectors (treated
1796 as keyboard macros), lambda-expressions that contain a top-level call
1797 to `interactive', autoload definitions made by `autoload' with non-nil
1798 fourth argument, and some of the built-in functions of Lisp.
1800 Also, a symbol satisfies `commandp' if its function definition does so.
1802 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1803 then strings and vectors are not accepted. */)
1804 (Lisp_Object function, Lisp_Object for_call_interactively)
1806 register Lisp_Object fun;
1807 register Lisp_Object funcar;
1808 Lisp_Object if_prop = Qnil;
1810 fun = function;
1812 fun = indirect_function (fun); /* Check cycles. */
1813 if (NILP (fun))
1814 return Qnil;
1816 /* Check an `interactive-form' property if present, analogous to the
1817 function-documentation property. */
1818 fun = function;
1819 while (SYMBOLP (fun))
1821 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1822 if (!NILP (tmp))
1823 if_prop = Qt;
1824 fun = Fsymbol_function (fun);
1827 /* Emacs primitives are interactive if their DEFUN specifies an
1828 interactive spec. */
1829 if (SUBRP (fun))
1830 return XSUBR (fun)->intspec ? Qt : if_prop;
1832 /* Bytecode objects are interactive if they are long enough to
1833 have an element whose index is COMPILED_INTERACTIVE, which is
1834 where the interactive spec is stored. */
1835 else if (COMPILEDP (fun))
1836 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1837 ? Qt : if_prop);
1839 /* Strings and vectors are keyboard macros. */
1840 if (STRINGP (fun) || VECTORP (fun))
1841 return (NILP (for_call_interactively) ? Qt : Qnil);
1843 /* Lists may represent commands. */
1844 if (!CONSP (fun))
1845 return Qnil;
1846 funcar = XCAR (fun);
1847 if (EQ (funcar, Qclosure))
1848 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1849 ? Qt : if_prop);
1850 else if (EQ (funcar, Qlambda))
1851 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1852 else if (EQ (funcar, Qautoload))
1853 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1854 else
1855 return Qnil;
1858 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1859 doc: /* Define FUNCTION to autoload from FILE.
1860 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1861 Third arg DOCSTRING is documentation for the function.
1862 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1863 Fifth arg TYPE indicates the type of the object:
1864 nil or omitted says FUNCTION is a function,
1865 `keymap' says FUNCTION is really a keymap, and
1866 `macro' or t says FUNCTION is really a macro.
1867 Third through fifth args give info about the real definition.
1868 They default to nil.
1869 If FUNCTION is already defined other than as an autoload,
1870 this does nothing and returns nil. */)
1871 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1873 CHECK_SYMBOL (function);
1874 CHECK_STRING (file);
1876 /* If function is defined and not as an autoload, don't override. */
1877 if (!NILP (XSYMBOL (function)->function)
1878 && !AUTOLOADP (XSYMBOL (function)->function))
1879 return Qnil;
1881 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1882 /* `read1' in lread.c has found the docstring starting with "\
1883 and assumed the docstring will be provided by Snarf-documentation, so it
1884 passed us 0 instead. But that leads to accidental sharing in purecopy's
1885 hash-consing, so we use a (hopefully) unique integer instead. */
1886 docstring = make_number (XHASH (function));
1887 return Fdefalias (function,
1888 list5 (Qautoload, file, docstring, interactive, type),
1889 Qnil);
1892 void
1893 un_autoload (Lisp_Object oldqueue)
1895 Lisp_Object queue, first, second;
1897 /* Queue to unwind is current value of Vautoload_queue.
1898 oldqueue is the shadowed value to leave in Vautoload_queue. */
1899 queue = Vautoload_queue;
1900 Vautoload_queue = oldqueue;
1901 while (CONSP (queue))
1903 first = XCAR (queue);
1904 second = Fcdr (first);
1905 first = Fcar (first);
1906 if (EQ (first, make_number (0)))
1907 Vfeatures = second;
1908 else
1909 Ffset (first, second);
1910 queue = XCDR (queue);
1914 /* Load an autoloaded function.
1915 FUNNAME is the symbol which is the function's name.
1916 FUNDEF is the autoload definition (a list). */
1918 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1919 doc: /* Load FUNDEF which should be an autoload.
1920 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1921 in which case the function returns the new autoloaded function value.
1922 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1923 it is defines a macro. */)
1924 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1926 ptrdiff_t count = SPECPDL_INDEX ();
1927 struct gcpro gcpro1, gcpro2, gcpro3;
1929 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1930 return fundef;
1932 if (EQ (macro_only, Qmacro))
1934 Lisp_Object kind = Fnth (make_number (4), fundef);
1935 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1936 return fundef;
1939 /* This is to make sure that loadup.el gives a clear picture
1940 of what files are preloaded and when. */
1941 if (! NILP (Vpurify_flag))
1942 error ("Attempt to autoload %s while preparing to dump",
1943 SDATA (SYMBOL_NAME (funname)));
1945 CHECK_SYMBOL (funname);
1946 GCPRO3 (funname, fundef, macro_only);
1948 /* Preserve the match data. */
1949 record_unwind_save_match_data ();
1951 /* If autoloading gets an error (which includes the error of failing
1952 to define the function being called), we use Vautoload_queue
1953 to undo function definitions and `provide' calls made by
1954 the function. We do this in the specific case of autoloading
1955 because autoloading is not an explicit request "load this file",
1956 but rather a request to "call this function".
1958 The value saved here is to be restored into Vautoload_queue. */
1959 record_unwind_protect (un_autoload, Vautoload_queue);
1960 Vautoload_queue = Qt;
1961 /* If `macro_only', assume this autoload to be a "best-effort",
1962 so don't signal an error if autoloading fails. */
1963 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1965 /* Once loading finishes, don't undo it. */
1966 Vautoload_queue = Qt;
1967 unbind_to (count, Qnil);
1969 UNGCPRO;
1971 if (NILP (funname))
1972 return Qnil;
1973 else
1975 Lisp_Object fun = Findirect_function (funname, Qnil);
1977 if (!NILP (Fequal (fun, fundef)))
1978 error ("Autoloading failed to define function %s",
1979 SDATA (SYMBOL_NAME (funname)));
1980 else
1981 return fun;
1986 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1987 doc: /* Evaluate FORM and return its value.
1988 If LEXICAL is t, evaluate using lexical scoping.
1989 LEXICAL can also be an actual lexical environment, in the form of an
1990 alist mapping symbols to their value. */)
1991 (Lisp_Object form, Lisp_Object lexical)
1993 ptrdiff_t count = SPECPDL_INDEX ();
1994 specbind (Qinternal_interpreter_environment,
1995 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1996 return unbind_to (count, eval_sub (form));
1999 /* Grow the specpdl stack by one entry.
2000 The caller should have already initialized the entry.
2001 Signal an error on stack overflow.
2003 Make sure that there is always one unused entry past the top of the
2004 stack, so that the just-initialized entry is safely unwound if
2005 memory exhausted and an error is signaled here. Also, allocate a
2006 never-used entry just before the bottom of the stack; sometimes its
2007 address is taken. */
2009 static void
2010 grow_specpdl (void)
2012 specpdl_ptr++;
2014 if (specpdl_ptr == specpdl + specpdl_size)
2016 ptrdiff_t count = SPECPDL_INDEX ();
2017 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2018 union specbinding *pdlvec = specpdl - 1;
2019 ptrdiff_t pdlvecsize = specpdl_size + 1;
2020 if (max_size <= specpdl_size)
2022 if (max_specpdl_size < 400)
2023 max_size = max_specpdl_size = 400;
2024 if (max_size <= specpdl_size)
2025 signal_error ("Variable binding depth exceeds max-specpdl-size",
2026 Qnil);
2028 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2029 specpdl = pdlvec + 1;
2030 specpdl_size = pdlvecsize - 1;
2031 specpdl_ptr = specpdl + count;
2035 void
2036 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2038 eassert (nargs >= UNEVALLED);
2039 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2040 specpdl_ptr->bt.debug_on_exit = false;
2041 specpdl_ptr->bt.function = function;
2042 specpdl_ptr->bt.args = args;
2043 specpdl_ptr->bt.nargs = nargs;
2044 grow_specpdl ();
2047 /* Eval a sub-expression of the current expression (i.e. in the same
2048 lexical scope). */
2049 Lisp_Object
2050 eval_sub (Lisp_Object form)
2052 Lisp_Object fun, val, original_fun, original_args;
2053 Lisp_Object funcar;
2054 struct gcpro gcpro1, gcpro2, gcpro3;
2056 if (SYMBOLP (form))
2058 /* Look up its binding in the lexical environment.
2059 We do not pay attention to the declared_special flag here, since we
2060 already did that when let-binding the variable. */
2061 Lisp_Object lex_binding
2062 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2063 ? Fassq (form, Vinternal_interpreter_environment)
2064 : Qnil;
2065 if (CONSP (lex_binding))
2066 return XCDR (lex_binding);
2067 else
2068 return Fsymbol_value (form);
2071 if (!CONSP (form))
2072 return form;
2074 QUIT;
2076 GCPRO1 (form);
2077 maybe_gc ();
2078 UNGCPRO;
2080 if (++lisp_eval_depth > max_lisp_eval_depth)
2082 if (max_lisp_eval_depth < 100)
2083 max_lisp_eval_depth = 100;
2084 if (lisp_eval_depth > max_lisp_eval_depth)
2085 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2088 original_fun = XCAR (form);
2089 original_args = XCDR (form);
2091 /* This also protects them from gc. */
2092 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2094 if (debug_on_next_call)
2095 do_debug_on_call (Qt);
2097 /* At this point, only original_fun and original_args
2098 have values that will be used below. */
2099 retry:
2101 /* Optimize for no indirection. */
2102 fun = original_fun;
2103 if (!SYMBOLP (fun))
2104 fun = Ffunction (Fcons (fun, Qnil));
2105 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2106 fun = indirect_function (fun);
2108 if (SUBRP (fun))
2110 Lisp_Object numargs;
2111 Lisp_Object argvals[8];
2112 Lisp_Object args_left;
2113 register int i, maxargs;
2115 args_left = original_args;
2116 numargs = Flength (args_left);
2118 check_cons_list ();
2120 if (XINT (numargs) < XSUBR (fun)->min_args
2121 || (XSUBR (fun)->max_args >= 0
2122 && XSUBR (fun)->max_args < XINT (numargs)))
2123 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2125 else if (XSUBR (fun)->max_args == UNEVALLED)
2126 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2127 else if (XSUBR (fun)->max_args == MANY)
2129 /* Pass a vector of evaluated arguments. */
2130 Lisp_Object *vals;
2131 ptrdiff_t argnum = 0;
2132 USE_SAFE_ALLOCA;
2134 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2136 GCPRO3 (args_left, fun, fun);
2137 gcpro3.var = vals;
2138 gcpro3.nvars = 0;
2140 while (!NILP (args_left))
2142 vals[argnum++] = eval_sub (Fcar (args_left));
2143 args_left = Fcdr (args_left);
2144 gcpro3.nvars = argnum;
2147 set_backtrace_args (specpdl_ptr - 1, vals);
2148 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2150 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2151 UNGCPRO;
2152 SAFE_FREE ();
2154 else
2156 GCPRO3 (args_left, fun, fun);
2157 gcpro3.var = argvals;
2158 gcpro3.nvars = 0;
2160 maxargs = XSUBR (fun)->max_args;
2161 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2163 argvals[i] = eval_sub (Fcar (args_left));
2164 gcpro3.nvars = ++i;
2167 UNGCPRO;
2169 set_backtrace_args (specpdl_ptr - 1, argvals);
2170 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2172 switch (i)
2174 case 0:
2175 val = (XSUBR (fun)->function.a0 ());
2176 break;
2177 case 1:
2178 val = (XSUBR (fun)->function.a1 (argvals[0]));
2179 break;
2180 case 2:
2181 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2182 break;
2183 case 3:
2184 val = (XSUBR (fun)->function.a3
2185 (argvals[0], argvals[1], argvals[2]));
2186 break;
2187 case 4:
2188 val = (XSUBR (fun)->function.a4
2189 (argvals[0], argvals[1], argvals[2], argvals[3]));
2190 break;
2191 case 5:
2192 val = (XSUBR (fun)->function.a5
2193 (argvals[0], argvals[1], argvals[2], argvals[3],
2194 argvals[4]));
2195 break;
2196 case 6:
2197 val = (XSUBR (fun)->function.a6
2198 (argvals[0], argvals[1], argvals[2], argvals[3],
2199 argvals[4], argvals[5]));
2200 break;
2201 case 7:
2202 val = (XSUBR (fun)->function.a7
2203 (argvals[0], argvals[1], argvals[2], argvals[3],
2204 argvals[4], argvals[5], argvals[6]));
2205 break;
2207 case 8:
2208 val = (XSUBR (fun)->function.a8
2209 (argvals[0], argvals[1], argvals[2], argvals[3],
2210 argvals[4], argvals[5], argvals[6], argvals[7]));
2211 break;
2213 default:
2214 /* Someone has created a subr that takes more arguments than
2215 is supported by this code. We need to either rewrite the
2216 subr to use a different argument protocol, or add more
2217 cases to this switch. */
2218 emacs_abort ();
2222 else if (COMPILEDP (fun))
2223 val = apply_lambda (fun, original_args);
2224 else
2226 if (NILP (fun))
2227 xsignal1 (Qvoid_function, original_fun);
2228 if (!CONSP (fun))
2229 xsignal1 (Qinvalid_function, original_fun);
2230 funcar = XCAR (fun);
2231 if (!SYMBOLP (funcar))
2232 xsignal1 (Qinvalid_function, original_fun);
2233 if (EQ (funcar, Qautoload))
2235 Fautoload_do_load (fun, original_fun, Qnil);
2236 goto retry;
2238 if (EQ (funcar, Qmacro))
2240 ptrdiff_t count = SPECPDL_INDEX ();
2241 Lisp_Object exp;
2242 /* Bind lexical-binding during expansion of the macro, so the
2243 macro can know reliably if the code it outputs will be
2244 interpreted using lexical-binding or not. */
2245 specbind (Qlexical_binding,
2246 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2247 exp = apply1 (Fcdr (fun), original_args);
2248 unbind_to (count, Qnil);
2249 val = eval_sub (exp);
2251 else if (EQ (funcar, Qlambda)
2252 || EQ (funcar, Qclosure))
2253 val = apply_lambda (fun, original_args);
2254 else
2255 xsignal1 (Qinvalid_function, original_fun);
2257 check_cons_list ();
2259 lisp_eval_depth--;
2260 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2261 val = call_debugger (list2 (Qexit, val));
2262 specpdl_ptr--;
2264 return val;
2267 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2268 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2269 Then return the value FUNCTION returns.
2270 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2271 usage: (apply FUNCTION &rest ARGUMENTS) */)
2272 (ptrdiff_t nargs, Lisp_Object *args)
2274 ptrdiff_t i;
2275 EMACS_INT numargs;
2276 register Lisp_Object spread_arg;
2277 register Lisp_Object *funcall_args;
2278 Lisp_Object fun, retval;
2279 struct gcpro gcpro1;
2280 USE_SAFE_ALLOCA;
2282 fun = args [0];
2283 funcall_args = 0;
2284 spread_arg = args [nargs - 1];
2285 CHECK_LIST (spread_arg);
2287 numargs = XINT (Flength (spread_arg));
2289 if (numargs == 0)
2290 return Ffuncall (nargs - 1, args);
2291 else if (numargs == 1)
2293 args [nargs - 1] = XCAR (spread_arg);
2294 return Ffuncall (nargs, args);
2297 numargs += nargs - 2;
2299 /* Optimize for no indirection. */
2300 if (SYMBOLP (fun) && !NILP (fun)
2301 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2302 fun = indirect_function (fun);
2303 if (NILP (fun))
2305 /* Let funcall get the error. */
2306 fun = args[0];
2307 goto funcall;
2310 if (SUBRP (fun))
2312 if (numargs < XSUBR (fun)->min_args
2313 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2314 goto funcall; /* Let funcall get the error. */
2315 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2317 /* Avoid making funcall cons up a yet another new vector of arguments
2318 by explicitly supplying nil's for optional values. */
2319 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2320 for (i = numargs; i < XSUBR (fun)->max_args;)
2321 funcall_args[++i] = Qnil;
2322 GCPRO1 (*funcall_args);
2323 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2326 funcall:
2327 /* We add 1 to numargs because funcall_args includes the
2328 function itself as well as its arguments. */
2329 if (!funcall_args)
2331 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2332 GCPRO1 (*funcall_args);
2333 gcpro1.nvars = 1 + numargs;
2336 memcpy (funcall_args, args, nargs * word_size);
2337 /* Spread the last arg we got. Its first element goes in
2338 the slot that it used to occupy, hence this value of I. */
2339 i = nargs - 1;
2340 while (!NILP (spread_arg))
2342 funcall_args [i++] = XCAR (spread_arg);
2343 spread_arg = XCDR (spread_arg);
2346 /* By convention, the caller needs to gcpro Ffuncall's args. */
2347 retval = Ffuncall (gcpro1.nvars, funcall_args);
2348 UNGCPRO;
2349 SAFE_FREE ();
2351 return retval;
2354 /* Run hook variables in various ways. */
2356 static Lisp_Object
2357 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2359 Ffuncall (nargs, args);
2360 return Qnil;
2363 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2364 doc: /* Run each hook in HOOKS.
2365 Each argument should be a symbol, a hook variable.
2366 These symbols are processed in the order specified.
2367 If a hook symbol has a non-nil value, that value may be a function
2368 or a list of functions to be called to run the hook.
2369 If the value is a function, it is called with no arguments.
2370 If it is a list, the elements are called, in order, with no arguments.
2372 Major modes should not use this function directly to run their mode
2373 hook; they should use `run-mode-hooks' instead.
2375 Do not use `make-local-variable' to make a hook variable buffer-local.
2376 Instead, use `add-hook' and specify t for the LOCAL argument.
2377 usage: (run-hooks &rest HOOKS) */)
2378 (ptrdiff_t nargs, Lisp_Object *args)
2380 Lisp_Object hook[1];
2381 ptrdiff_t i;
2383 for (i = 0; i < nargs; i++)
2385 hook[0] = args[i];
2386 run_hook_with_args (1, hook, funcall_nil);
2389 return Qnil;
2392 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2393 Srun_hook_with_args, 1, MANY, 0,
2394 doc: /* Run HOOK with the specified arguments ARGS.
2395 HOOK should be a symbol, a hook variable. The value of HOOK
2396 may be nil, a function, or a list of functions. Call each
2397 function in order with arguments ARGS. The final return value
2398 is unspecified.
2400 Do not use `make-local-variable' to make a hook variable buffer-local.
2401 Instead, use `add-hook' and specify t for the LOCAL argument.
2402 usage: (run-hook-with-args HOOK &rest ARGS) */)
2403 (ptrdiff_t nargs, Lisp_Object *args)
2405 return run_hook_with_args (nargs, args, funcall_nil);
2408 /* NB this one still documents a specific non-nil return value.
2409 (As did run-hook-with-args and run-hook-with-args-until-failure
2410 until they were changed in 24.1.) */
2411 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2412 Srun_hook_with_args_until_success, 1, MANY, 0,
2413 doc: /* Run HOOK with the specified arguments ARGS.
2414 HOOK should be a symbol, a hook variable. The value of HOOK
2415 may be nil, a function, or a list of functions. Call each
2416 function in order with arguments ARGS, stopping at the first
2417 one that returns non-nil, and return that value. Otherwise (if
2418 all functions return nil, or if there are no functions to call),
2419 return nil.
2421 Do not use `make-local-variable' to make a hook variable buffer-local.
2422 Instead, use `add-hook' and specify t for the LOCAL argument.
2423 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2424 (ptrdiff_t nargs, Lisp_Object *args)
2426 return run_hook_with_args (nargs, args, Ffuncall);
2429 static Lisp_Object
2430 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2432 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2435 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2436 Srun_hook_with_args_until_failure, 1, MANY, 0,
2437 doc: /* Run HOOK with the specified arguments ARGS.
2438 HOOK should be a symbol, a hook variable. The value of HOOK
2439 may be nil, a function, or a list of functions. Call each
2440 function in order with arguments ARGS, stopping at the first
2441 one that returns nil, and return nil. Otherwise (if all functions
2442 return non-nil, or if there are no functions to call), return non-nil
2443 \(do not rely on the precise return value in this case).
2445 Do not use `make-local-variable' to make a hook variable buffer-local.
2446 Instead, use `add-hook' and specify t for the LOCAL argument.
2447 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2448 (ptrdiff_t nargs, Lisp_Object *args)
2450 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2453 static Lisp_Object
2454 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2456 Lisp_Object tmp = args[0], ret;
2457 args[0] = args[1];
2458 args[1] = tmp;
2459 ret = Ffuncall (nargs, args);
2460 args[1] = args[0];
2461 args[0] = tmp;
2462 return ret;
2465 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2466 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2467 I.e. instead of calling each function FUN directly with arguments ARGS,
2468 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2469 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2470 aborts and returns that value.
2471 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2472 (ptrdiff_t nargs, Lisp_Object *args)
2474 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2477 /* ARGS[0] should be a hook symbol.
2478 Call each of the functions in the hook value, passing each of them
2479 as arguments all the rest of ARGS (all NARGS - 1 elements).
2480 FUNCALL specifies how to call each function on the hook.
2481 The caller (or its caller, etc) must gcpro all of ARGS,
2482 except that it isn't necessary to gcpro ARGS[0]. */
2484 Lisp_Object
2485 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2486 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2488 Lisp_Object sym, val, ret = Qnil;
2489 struct gcpro gcpro1, gcpro2, gcpro3;
2491 /* If we are dying or still initializing,
2492 don't do anything--it would probably crash if we tried. */
2493 if (NILP (Vrun_hooks))
2494 return Qnil;
2496 sym = args[0];
2497 val = find_symbol_value (sym);
2499 if (EQ (val, Qunbound) || NILP (val))
2500 return ret;
2501 else if (!CONSP (val) || FUNCTIONP (val))
2503 args[0] = val;
2504 return funcall (nargs, args);
2506 else
2508 Lisp_Object global_vals = Qnil;
2509 GCPRO3 (sym, val, global_vals);
2511 for (;
2512 CONSP (val) && NILP (ret);
2513 val = XCDR (val))
2515 if (EQ (XCAR (val), Qt))
2517 /* t indicates this hook has a local binding;
2518 it means to run the global binding too. */
2519 global_vals = Fdefault_value (sym);
2520 if (NILP (global_vals)) continue;
2522 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2524 args[0] = global_vals;
2525 ret = funcall (nargs, args);
2527 else
2529 for (;
2530 CONSP (global_vals) && NILP (ret);
2531 global_vals = XCDR (global_vals))
2533 args[0] = XCAR (global_vals);
2534 /* In a global value, t should not occur. If it does, we
2535 must ignore it to avoid an endless loop. */
2536 if (!EQ (args[0], Qt))
2537 ret = funcall (nargs, args);
2541 else
2543 args[0] = XCAR (val);
2544 ret = funcall (nargs, args);
2548 UNGCPRO;
2549 return ret;
2553 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2555 void
2556 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2558 Lisp_Object temp[3];
2559 temp[0] = hook;
2560 temp[1] = arg1;
2561 temp[2] = arg2;
2563 Frun_hook_with_args (3, temp);
2566 /* Apply fn to arg. */
2567 Lisp_Object
2568 apply1 (Lisp_Object fn, Lisp_Object arg)
2570 struct gcpro gcpro1;
2572 GCPRO1 (fn);
2573 if (NILP (arg))
2574 RETURN_UNGCPRO (Ffuncall (1, &fn));
2575 gcpro1.nvars = 2;
2577 Lisp_Object args[2];
2578 args[0] = fn;
2579 args[1] = arg;
2580 gcpro1.var = args;
2581 RETURN_UNGCPRO (Fapply (2, args));
2585 /* Call function fn on no arguments. */
2586 Lisp_Object
2587 call0 (Lisp_Object fn)
2589 struct gcpro gcpro1;
2591 GCPRO1 (fn);
2592 RETURN_UNGCPRO (Ffuncall (1, &fn));
2595 /* Call function fn with 1 argument arg1. */
2596 /* ARGSUSED */
2597 Lisp_Object
2598 call1 (Lisp_Object fn, Lisp_Object arg1)
2600 struct gcpro gcpro1;
2601 Lisp_Object args[2];
2603 args[0] = fn;
2604 args[1] = arg1;
2605 GCPRO1 (args[0]);
2606 gcpro1.nvars = 2;
2607 RETURN_UNGCPRO (Ffuncall (2, args));
2610 /* Call function fn with 2 arguments arg1, arg2. */
2611 /* ARGSUSED */
2612 Lisp_Object
2613 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2615 struct gcpro gcpro1;
2616 Lisp_Object args[3];
2617 args[0] = fn;
2618 args[1] = arg1;
2619 args[2] = arg2;
2620 GCPRO1 (args[0]);
2621 gcpro1.nvars = 3;
2622 RETURN_UNGCPRO (Ffuncall (3, args));
2625 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2626 /* ARGSUSED */
2627 Lisp_Object
2628 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2630 struct gcpro gcpro1;
2631 Lisp_Object args[4];
2632 args[0] = fn;
2633 args[1] = arg1;
2634 args[2] = arg2;
2635 args[3] = arg3;
2636 GCPRO1 (args[0]);
2637 gcpro1.nvars = 4;
2638 RETURN_UNGCPRO (Ffuncall (4, args));
2641 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2642 /* ARGSUSED */
2643 Lisp_Object
2644 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2645 Lisp_Object arg4)
2647 struct gcpro gcpro1;
2648 Lisp_Object args[5];
2649 args[0] = fn;
2650 args[1] = arg1;
2651 args[2] = arg2;
2652 args[3] = arg3;
2653 args[4] = arg4;
2654 GCPRO1 (args[0]);
2655 gcpro1.nvars = 5;
2656 RETURN_UNGCPRO (Ffuncall (5, args));
2659 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2660 /* ARGSUSED */
2661 Lisp_Object
2662 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2663 Lisp_Object arg4, Lisp_Object arg5)
2665 struct gcpro gcpro1;
2666 Lisp_Object args[6];
2667 args[0] = fn;
2668 args[1] = arg1;
2669 args[2] = arg2;
2670 args[3] = arg3;
2671 args[4] = arg4;
2672 args[5] = arg5;
2673 GCPRO1 (args[0]);
2674 gcpro1.nvars = 6;
2675 RETURN_UNGCPRO (Ffuncall (6, args));
2678 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2679 /* ARGSUSED */
2680 Lisp_Object
2681 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2682 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2684 struct gcpro gcpro1;
2685 Lisp_Object args[7];
2686 args[0] = fn;
2687 args[1] = arg1;
2688 args[2] = arg2;
2689 args[3] = arg3;
2690 args[4] = arg4;
2691 args[5] = arg5;
2692 args[6] = arg6;
2693 GCPRO1 (args[0]);
2694 gcpro1.nvars = 7;
2695 RETURN_UNGCPRO (Ffuncall (7, args));
2698 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2699 /* ARGSUSED */
2700 Lisp_Object
2701 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2702 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2704 struct gcpro gcpro1;
2705 Lisp_Object args[8];
2706 args[0] = fn;
2707 args[1] = arg1;
2708 args[2] = arg2;
2709 args[3] = arg3;
2710 args[4] = arg4;
2711 args[5] = arg5;
2712 args[6] = arg6;
2713 args[7] = arg7;
2714 GCPRO1 (args[0]);
2715 gcpro1.nvars = 8;
2716 RETURN_UNGCPRO (Ffuncall (8, args));
2719 /* The caller should GCPRO all the elements of ARGS. */
2721 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2722 doc: /* Non-nil if OBJECT is a function. */)
2723 (Lisp_Object object)
2725 if (FUNCTIONP (object))
2726 return Qt;
2727 return Qnil;
2730 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2731 doc: /* Call first argument as a function, passing remaining arguments to it.
2732 Return the value that function returns.
2733 Thus, (funcall 'cons 'x 'y) returns (x . y).
2734 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2735 (ptrdiff_t nargs, Lisp_Object *args)
2737 Lisp_Object fun, original_fun;
2738 Lisp_Object funcar;
2739 ptrdiff_t numargs = nargs - 1;
2740 Lisp_Object lisp_numargs;
2741 Lisp_Object val;
2742 register Lisp_Object *internal_args;
2743 ptrdiff_t i;
2745 QUIT;
2747 if (++lisp_eval_depth > max_lisp_eval_depth)
2749 if (max_lisp_eval_depth < 100)
2750 max_lisp_eval_depth = 100;
2751 if (lisp_eval_depth > max_lisp_eval_depth)
2752 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2755 /* This also GCPROs them. */
2756 record_in_backtrace (args[0], &args[1], nargs - 1);
2758 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2759 maybe_gc ();
2761 if (debug_on_next_call)
2762 do_debug_on_call (Qlambda);
2764 check_cons_list ();
2766 original_fun = args[0];
2768 retry:
2770 /* Optimize for no indirection. */
2771 fun = original_fun;
2772 if (SYMBOLP (fun) && !NILP (fun)
2773 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2774 fun = indirect_function (fun);
2776 if (SUBRP (fun))
2778 if (numargs < XSUBR (fun)->min_args
2779 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2781 XSETFASTINT (lisp_numargs, numargs);
2782 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2785 else if (XSUBR (fun)->max_args == UNEVALLED)
2786 xsignal1 (Qinvalid_function, original_fun);
2788 else if (XSUBR (fun)->max_args == MANY)
2789 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2790 else
2792 if (XSUBR (fun)->max_args > numargs)
2794 internal_args = alloca (XSUBR (fun)->max_args
2795 * sizeof *internal_args);
2796 memcpy (internal_args, args + 1, numargs * word_size);
2797 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2798 internal_args[i] = Qnil;
2800 else
2801 internal_args = args + 1;
2802 switch (XSUBR (fun)->max_args)
2804 case 0:
2805 val = (XSUBR (fun)->function.a0 ());
2806 break;
2807 case 1:
2808 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2809 break;
2810 case 2:
2811 val = (XSUBR (fun)->function.a2
2812 (internal_args[0], internal_args[1]));
2813 break;
2814 case 3:
2815 val = (XSUBR (fun)->function.a3
2816 (internal_args[0], internal_args[1], internal_args[2]));
2817 break;
2818 case 4:
2819 val = (XSUBR (fun)->function.a4
2820 (internal_args[0], internal_args[1], internal_args[2],
2821 internal_args[3]));
2822 break;
2823 case 5:
2824 val = (XSUBR (fun)->function.a5
2825 (internal_args[0], internal_args[1], internal_args[2],
2826 internal_args[3], internal_args[4]));
2827 break;
2828 case 6:
2829 val = (XSUBR (fun)->function.a6
2830 (internal_args[0], internal_args[1], internal_args[2],
2831 internal_args[3], internal_args[4], internal_args[5]));
2832 break;
2833 case 7:
2834 val = (XSUBR (fun)->function.a7
2835 (internal_args[0], internal_args[1], internal_args[2],
2836 internal_args[3], internal_args[4], internal_args[5],
2837 internal_args[6]));
2838 break;
2840 case 8:
2841 val = (XSUBR (fun)->function.a8
2842 (internal_args[0], internal_args[1], internal_args[2],
2843 internal_args[3], internal_args[4], internal_args[5],
2844 internal_args[6], internal_args[7]));
2845 break;
2847 default:
2849 /* If a subr takes more than 8 arguments without using MANY
2850 or UNEVALLED, we need to extend this function to support it.
2851 Until this is done, there is no way to call the function. */
2852 emacs_abort ();
2856 else if (COMPILEDP (fun))
2857 val = funcall_lambda (fun, numargs, args + 1);
2858 else
2860 if (NILP (fun))
2861 xsignal1 (Qvoid_function, original_fun);
2862 if (!CONSP (fun))
2863 xsignal1 (Qinvalid_function, original_fun);
2864 funcar = XCAR (fun);
2865 if (!SYMBOLP (funcar))
2866 xsignal1 (Qinvalid_function, original_fun);
2867 if (EQ (funcar, Qlambda)
2868 || EQ (funcar, Qclosure))
2869 val = funcall_lambda (fun, numargs, args + 1);
2870 else if (EQ (funcar, Qautoload))
2872 Fautoload_do_load (fun, original_fun, Qnil);
2873 check_cons_list ();
2874 goto retry;
2876 else
2877 xsignal1 (Qinvalid_function, original_fun);
2879 check_cons_list ();
2880 lisp_eval_depth--;
2881 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2882 val = call_debugger (list2 (Qexit, val));
2883 specpdl_ptr--;
2884 return val;
2887 static Lisp_Object
2888 apply_lambda (Lisp_Object fun, Lisp_Object args)
2890 Lisp_Object args_left;
2891 ptrdiff_t i;
2892 EMACS_INT numargs;
2893 register Lisp_Object *arg_vector;
2894 struct gcpro gcpro1, gcpro2, gcpro3;
2895 register Lisp_Object tem;
2896 USE_SAFE_ALLOCA;
2898 numargs = XFASTINT (Flength (args));
2899 SAFE_ALLOCA_LISP (arg_vector, numargs);
2900 args_left = args;
2902 GCPRO3 (*arg_vector, args_left, fun);
2903 gcpro1.nvars = 0;
2905 for (i = 0; i < numargs; )
2907 tem = Fcar (args_left), args_left = Fcdr (args_left);
2908 tem = eval_sub (tem);
2909 arg_vector[i++] = tem;
2910 gcpro1.nvars = i;
2913 UNGCPRO;
2915 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2916 set_backtrace_nargs (specpdl_ptr - 1, i);
2917 tem = funcall_lambda (fun, numargs, arg_vector);
2919 /* Do the debug-on-exit now, while arg_vector still exists. */
2920 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2922 /* Don't do it again when we return to eval. */
2923 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2924 tem = call_debugger (list2 (Qexit, tem));
2926 SAFE_FREE ();
2927 return tem;
2930 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2931 and return the result of evaluation.
2932 FUN must be either a lambda-expression or a compiled-code object. */
2934 static Lisp_Object
2935 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2936 register Lisp_Object *arg_vector)
2938 Lisp_Object val, syms_left, next, lexenv;
2939 ptrdiff_t count = SPECPDL_INDEX ();
2940 ptrdiff_t i;
2941 bool optional, rest;
2943 if (CONSP (fun))
2945 if (EQ (XCAR (fun), Qclosure))
2947 fun = XCDR (fun); /* Drop `closure'. */
2948 lexenv = XCAR (fun);
2949 CHECK_LIST_CONS (fun, fun);
2951 else
2952 lexenv = Qnil;
2953 syms_left = XCDR (fun);
2954 if (CONSP (syms_left))
2955 syms_left = XCAR (syms_left);
2956 else
2957 xsignal1 (Qinvalid_function, fun);
2959 else if (COMPILEDP (fun))
2961 syms_left = AREF (fun, COMPILED_ARGLIST);
2962 if (INTEGERP (syms_left))
2963 /* A byte-code object with a non-nil `push args' slot means we
2964 shouldn't bind any arguments, instead just call the byte-code
2965 interpreter directly; it will push arguments as necessary.
2967 Byte-code objects with either a non-existent, or a nil value for
2968 the `push args' slot (the default), have dynamically-bound
2969 arguments, and use the argument-binding code below instead (as do
2970 all interpreted functions, even lexically bound ones). */
2972 /* If we have not actually read the bytecode string
2973 and constants vector yet, fetch them from the file. */
2974 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2975 Ffetch_bytecode (fun);
2976 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2977 AREF (fun, COMPILED_CONSTANTS),
2978 AREF (fun, COMPILED_STACK_DEPTH),
2979 syms_left,
2980 nargs, arg_vector);
2982 lexenv = Qnil;
2984 else
2985 emacs_abort ();
2987 i = optional = rest = 0;
2988 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2990 QUIT;
2992 next = XCAR (syms_left);
2993 if (!SYMBOLP (next))
2994 xsignal1 (Qinvalid_function, fun);
2996 if (EQ (next, Qand_rest))
2997 rest = 1;
2998 else if (EQ (next, Qand_optional))
2999 optional = 1;
3000 else
3002 Lisp_Object arg;
3003 if (rest)
3005 arg = Flist (nargs - i, &arg_vector[i]);
3006 i = nargs;
3008 else if (i < nargs)
3009 arg = arg_vector[i++];
3010 else if (!optional)
3011 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3012 else
3013 arg = Qnil;
3015 /* Bind the argument. */
3016 if (!NILP (lexenv) && SYMBOLP (next))
3017 /* Lexically bind NEXT by adding it to the lexenv alist. */
3018 lexenv = Fcons (Fcons (next, arg), lexenv);
3019 else
3020 /* Dynamically bind NEXT. */
3021 specbind (next, arg);
3025 if (!NILP (syms_left))
3026 xsignal1 (Qinvalid_function, fun);
3027 else if (i < nargs)
3028 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3030 if (!EQ (lexenv, Vinternal_interpreter_environment))
3031 /* Instantiate a new lexical environment. */
3032 specbind (Qinternal_interpreter_environment, lexenv);
3034 if (CONSP (fun))
3035 val = Fprogn (XCDR (XCDR (fun)));
3036 else
3038 /* If we have not actually read the bytecode string
3039 and constants vector yet, fetch them from the file. */
3040 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3041 Ffetch_bytecode (fun);
3042 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3043 AREF (fun, COMPILED_CONSTANTS),
3044 AREF (fun, COMPILED_STACK_DEPTH),
3045 Qnil, 0, 0);
3048 return unbind_to (count, val);
3051 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3052 1, 1, 0,
3053 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3054 (Lisp_Object object)
3056 Lisp_Object tem;
3058 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3060 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3061 if (!CONSP (tem))
3063 tem = AREF (object, COMPILED_BYTECODE);
3064 if (CONSP (tem) && STRINGP (XCAR (tem)))
3065 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3066 else
3067 error ("Invalid byte code");
3069 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3070 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3072 return object;
3075 /* Return true if SYMBOL currently has a let-binding
3076 which was made in the buffer that is now current. */
3078 bool
3079 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3081 union specbinding *p;
3082 Lisp_Object buf = Fcurrent_buffer ();
3084 for (p = specpdl_ptr; p > specpdl; )
3085 if ((--p)->kind > SPECPDL_LET)
3087 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3088 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3089 if (symbol == let_bound_symbol
3090 && EQ (specpdl_where (p), buf))
3091 return 1;
3094 return 0;
3097 bool
3098 let_shadows_global_binding_p (Lisp_Object symbol)
3100 union specbinding *p;
3102 for (p = specpdl_ptr; p > specpdl; )
3103 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3104 return 1;
3106 return 0;
3109 /* `specpdl_ptr' describes which variable is
3110 let-bound, so it can be properly undone when we unbind_to.
3111 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3112 - SYMBOL is the variable being bound. Note that it should not be
3113 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3114 to record V2 here).
3115 - WHERE tells us in which buffer the binding took place.
3116 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3117 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3118 i.e. bindings to the default value of a variable which can be
3119 buffer-local. */
3121 void
3122 specbind (Lisp_Object symbol, Lisp_Object value)
3124 struct Lisp_Symbol *sym;
3126 CHECK_SYMBOL (symbol);
3127 sym = XSYMBOL (symbol);
3129 start:
3130 switch (sym->redirect)
3132 case SYMBOL_VARALIAS:
3133 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3134 case SYMBOL_PLAINVAL:
3135 /* The most common case is that of a non-constant symbol with a
3136 trivial value. Make that as fast as we can. */
3137 specpdl_ptr->let.kind = SPECPDL_LET;
3138 specpdl_ptr->let.symbol = symbol;
3139 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3140 grow_specpdl ();
3141 if (!sym->constant)
3142 SET_SYMBOL_VAL (sym, value);
3143 else
3144 set_internal (symbol, value, Qnil, 1);
3145 break;
3146 case SYMBOL_LOCALIZED:
3147 if (SYMBOL_BLV (sym)->frame_local)
3148 error ("Frame-local vars cannot be let-bound");
3149 case SYMBOL_FORWARDED:
3151 Lisp_Object ovalue = find_symbol_value (symbol);
3152 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3153 specpdl_ptr->let.symbol = symbol;
3154 specpdl_ptr->let.old_value = ovalue;
3155 specpdl_ptr->let.where = Fcurrent_buffer ();
3157 eassert (sym->redirect != SYMBOL_LOCALIZED
3158 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3160 if (sym->redirect == SYMBOL_LOCALIZED)
3162 if (!blv_found (SYMBOL_BLV (sym)))
3163 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3165 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3167 /* If SYMBOL is a per-buffer variable which doesn't have a
3168 buffer-local value here, make the `let' change the global
3169 value by changing the value of SYMBOL in all buffers not
3170 having their own value. This is consistent with what
3171 happens with other buffer-local variables. */
3172 if (NILP (Flocal_variable_p (symbol, Qnil)))
3174 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3175 grow_specpdl ();
3176 Fset_default (symbol, value);
3177 return;
3180 else
3181 specpdl_ptr->let.kind = SPECPDL_LET;
3183 grow_specpdl ();
3184 set_internal (symbol, value, Qnil, 1);
3185 break;
3187 default: emacs_abort ();
3191 /* Push unwind-protect entries of various types. */
3193 void
3194 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3196 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3197 specpdl_ptr->unwind.func = function;
3198 specpdl_ptr->unwind.arg = arg;
3199 grow_specpdl ();
3202 void
3203 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3205 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3206 specpdl_ptr->unwind_ptr.func = function;
3207 specpdl_ptr->unwind_ptr.arg = arg;
3208 grow_specpdl ();
3211 void
3212 record_unwind_protect_int (void (*function) (int), int arg)
3214 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3215 specpdl_ptr->unwind_int.func = function;
3216 specpdl_ptr->unwind_int.arg = arg;
3217 grow_specpdl ();
3220 void
3221 record_unwind_protect_void (void (*function) (void))
3223 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3224 specpdl_ptr->unwind_void.func = function;
3225 grow_specpdl ();
3228 static void
3229 do_nothing (void)
3232 /* Push an unwind-protect entry that does nothing, so that
3233 set_unwind_protect_ptr can overwrite it later. */
3235 void
3236 record_unwind_protect_nothing (void)
3238 record_unwind_protect_void (do_nothing);
3241 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3242 It need not be at the top of the stack. */
3244 void
3245 clear_unwind_protect (ptrdiff_t count)
3247 union specbinding *p = specpdl + count;
3248 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3249 p->unwind_void.func = do_nothing;
3252 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3253 It need not be at the top of the stack. Discard the entry's
3254 previous value without invoking it. */
3256 void
3257 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3258 Lisp_Object arg)
3260 union specbinding *p = specpdl + count;
3261 p->unwind.kind = SPECPDL_UNWIND;
3262 p->unwind.func = func;
3263 p->unwind.arg = arg;
3266 void
3267 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3269 union specbinding *p = specpdl + count;
3270 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3271 p->unwind_ptr.func = func;
3272 p->unwind_ptr.arg = arg;
3275 /* Pop and execute entries from the unwind-protect stack until the
3276 depth COUNT is reached. Return VALUE. */
3278 Lisp_Object
3279 unbind_to (ptrdiff_t count, Lisp_Object value)
3281 Lisp_Object quitf = Vquit_flag;
3282 struct gcpro gcpro1, gcpro2;
3284 GCPRO2 (value, quitf);
3285 Vquit_flag = Qnil;
3287 while (specpdl_ptr != specpdl + count)
3289 /* Decrement specpdl_ptr before we do the work to unbind it, so
3290 that an error in unbinding won't try to unbind the same entry
3291 again. Take care to copy any parts of the binding needed
3292 before invoking any code that can make more bindings. */
3294 specpdl_ptr--;
3296 switch (specpdl_ptr->kind)
3298 case SPECPDL_UNWIND:
3299 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3300 break;
3301 case SPECPDL_UNWIND_PTR:
3302 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3303 break;
3304 case SPECPDL_UNWIND_INT:
3305 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3306 break;
3307 case SPECPDL_UNWIND_VOID:
3308 specpdl_ptr->unwind_void.func ();
3309 break;
3310 case SPECPDL_BACKTRACE:
3311 break;
3312 case SPECPDL_LET:
3313 { /* If variable has a trivial value (no forwarding), we can
3314 just set it. No need to check for constant symbols here,
3315 since that was already done by specbind. */
3316 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3317 if (sym->redirect == SYMBOL_PLAINVAL)
3319 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3320 break;
3322 else
3323 { /* FALLTHROUGH!!
3324 NOTE: we only ever come here if make_local_foo was used for
3325 the first time on this var within this let. */
3328 case SPECPDL_LET_DEFAULT:
3329 Fset_default (specpdl_symbol (specpdl_ptr),
3330 specpdl_old_value (specpdl_ptr));
3331 break;
3332 case SPECPDL_LET_LOCAL:
3334 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3335 Lisp_Object where = specpdl_where (specpdl_ptr);
3336 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3337 eassert (BUFFERP (where));
3339 /* If this was a local binding, reset the value in the appropriate
3340 buffer, but only if that buffer's binding still exists. */
3341 if (!NILP (Flocal_variable_p (symbol, where)))
3342 set_internal (symbol, old_value, where, 1);
3344 break;
3348 if (NILP (Vquit_flag) && !NILP (quitf))
3349 Vquit_flag = quitf;
3351 UNGCPRO;
3352 return value;
3355 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3356 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3357 A special variable is one that will be bound dynamically, even in a
3358 context where binding is lexical by default. */)
3359 (Lisp_Object symbol)
3361 CHECK_SYMBOL (symbol);
3362 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3366 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3367 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3368 The debugger is entered when that frame exits, if the flag is non-nil. */)
3369 (Lisp_Object level, Lisp_Object flag)
3371 union specbinding *pdl = backtrace_top ();
3372 register EMACS_INT i;
3374 CHECK_NUMBER (level);
3376 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3377 pdl = backtrace_next (pdl);
3379 if (backtrace_p (pdl))
3380 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3382 return flag;
3385 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3386 doc: /* Print a trace of Lisp function calls currently active.
3387 Output stream used is value of `standard-output'. */)
3388 (void)
3390 union specbinding *pdl = backtrace_top ();
3391 Lisp_Object tem;
3392 Lisp_Object old_print_level = Vprint_level;
3394 if (NILP (Vprint_level))
3395 XSETFASTINT (Vprint_level, 8);
3397 while (backtrace_p (pdl))
3399 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3400 if (backtrace_nargs (pdl) == UNEVALLED)
3402 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3403 Qnil);
3404 write_string ("\n", -1);
3406 else
3408 tem = backtrace_function (pdl);
3409 Fprin1 (tem, Qnil); /* This can QUIT. */
3410 write_string ("(", -1);
3412 ptrdiff_t i;
3413 for (i = 0; i < backtrace_nargs (pdl); i++)
3415 if (i) write_string (" ", -1);
3416 Fprin1 (backtrace_args (pdl)[i], Qnil);
3419 write_string (")\n", -1);
3421 pdl = backtrace_next (pdl);
3424 Vprint_level = old_print_level;
3425 return Qnil;
3428 static union specbinding *
3429 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3431 union specbinding *pdl = backtrace_top ();
3432 register EMACS_INT i;
3434 CHECK_NATNUM (nframes);
3436 if (!NILP (base))
3437 { /* Skip up to `base'. */
3438 base = Findirect_function (base, Qt);
3439 while (backtrace_p (pdl)
3440 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3441 pdl = backtrace_next (pdl);
3444 /* Find the frame requested. */
3445 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3446 pdl = backtrace_next (pdl);
3448 return pdl;
3451 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3452 doc: /* Return the function and arguments NFRAMES up from current execution point.
3453 If that frame has not evaluated the arguments yet (or is a special form),
3454 the value is (nil FUNCTION ARG-FORMS...).
3455 If that frame has evaluated its arguments and called its function already,
3456 the value is (t FUNCTION ARG-VALUES...).
3457 A &rest arg is represented as the tail of the list ARG-VALUES.
3458 FUNCTION is whatever was supplied as car of evaluated list,
3459 or a lambda expression for macro calls.
3460 If NFRAMES is more than the number of frames, the value is nil.
3461 If BASE is non-nil, it should be a function and NFRAMES counts from its
3462 nearest activation frame. */)
3463 (Lisp_Object nframes, Lisp_Object base)
3465 union specbinding *pdl = get_backtrace_frame (nframes, base);
3467 if (!backtrace_p (pdl))
3468 return Qnil;
3469 if (backtrace_nargs (pdl) == UNEVALLED)
3470 return Fcons (Qnil,
3471 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3472 else
3474 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3476 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3480 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3481 the specpdl stack, and then rewind them. We store the pre-unwind values
3482 directly in the pre-existing specpdl elements (i.e. we swap the current
3483 value and the old value stored in the specpdl), kind of like the inplace
3484 pointer-reversal trick. As it turns out, the rewind does the same as the
3485 unwind, except it starts from the other end of the specpdl stack, so we use
3486 the same function for both unwind and rewind. */
3487 static void
3488 backtrace_eval_unrewind (int distance)
3490 union specbinding *tmp = specpdl_ptr;
3491 int step = -1;
3492 if (distance < 0)
3493 { /* It's a rewind rather than unwind. */
3494 tmp += distance - 1;
3495 step = 1;
3496 distance = -distance;
3499 for (; distance > 0; distance--)
3501 tmp += step;
3502 /* */
3503 switch (tmp->kind)
3505 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3506 unwind_protect, but the problem is that we don't know how to
3507 rewind them afterwards. */
3508 case SPECPDL_UNWIND:
3509 case SPECPDL_UNWIND_PTR:
3510 case SPECPDL_UNWIND_INT:
3511 case SPECPDL_UNWIND_VOID:
3512 case SPECPDL_BACKTRACE:
3513 break;
3514 case SPECPDL_LET:
3515 { /* If variable has a trivial value (no forwarding), we can
3516 just set it. No need to check for constant symbols here,
3517 since that was already done by specbind. */
3518 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3519 if (sym->redirect == SYMBOL_PLAINVAL)
3521 Lisp_Object old_value = specpdl_old_value (tmp);
3522 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3523 SET_SYMBOL_VAL (sym, old_value);
3524 break;
3526 else
3527 { /* FALLTHROUGH!!
3528 NOTE: we only ever come here if make_local_foo was used for
3529 the first time on this var within this let. */
3532 case SPECPDL_LET_DEFAULT:
3534 Lisp_Object sym = specpdl_symbol (tmp);
3535 Lisp_Object old_value = specpdl_old_value (tmp);
3536 set_specpdl_old_value (tmp, Fdefault_value (sym));
3537 Fset_default (sym, old_value);
3539 break;
3540 case SPECPDL_LET_LOCAL:
3542 Lisp_Object symbol = specpdl_symbol (tmp);
3543 Lisp_Object where = specpdl_where (tmp);
3544 Lisp_Object old_value = specpdl_old_value (tmp);
3545 eassert (BUFFERP (where));
3547 /* If this was a local binding, reset the value in the appropriate
3548 buffer, but only if that buffer's binding still exists. */
3549 if (!NILP (Flocal_variable_p (symbol, where)))
3551 set_specpdl_old_value
3552 (tmp, Fbuffer_local_value (symbol, where));
3553 set_internal (symbol, old_value, where, 1);
3556 break;
3561 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3562 doc: /* Evaluate EXP in the context of some activation frame.
3563 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3564 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3566 union specbinding *pdl = get_backtrace_frame (nframes, base);
3567 ptrdiff_t count = SPECPDL_INDEX ();
3568 ptrdiff_t distance = specpdl_ptr - pdl;
3569 eassert (distance >= 0);
3571 if (!backtrace_p (pdl))
3572 error ("Activation frame not found!");
3574 backtrace_eval_unrewind (distance);
3575 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3577 /* Use eval_sub rather than Feval since the main motivation behind
3578 backtrace-eval is to be able to get/set the value of lexical variables
3579 from the debugger. */
3580 return unbind_to (count, eval_sub (exp));
3583 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3584 doc: /* Return names and values of local variables of a stack frame.
3585 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3586 (Lisp_Object nframes, Lisp_Object base)
3588 union specbinding *frame = get_backtrace_frame (nframes, base);
3589 union specbinding *prevframe
3590 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3591 ptrdiff_t distance = specpdl_ptr - frame;
3592 Lisp_Object result = Qnil;
3593 eassert (distance >= 0);
3595 if (!backtrace_p (prevframe))
3596 error ("Activation frame not found!");
3597 if (!backtrace_p (frame))
3598 error ("Activation frame not found!");
3600 /* The specpdl entries normally contain the symbol being bound along with its
3601 `old_value', so it can be restored. The new value to which it is bound is
3602 available in one of two places: either in the current value of the
3603 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3604 next specpdl entry for it.
3605 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3606 and "new value", so we abuse it here, to fetch the new value.
3607 It's ugly (we'd rather not modify global data) and a bit inefficient,
3608 but it does the job for now. */
3609 backtrace_eval_unrewind (distance);
3611 /* Grab values. */
3613 union specbinding *tmp = prevframe;
3614 for (; tmp > frame; tmp--)
3616 switch (tmp->kind)
3618 case SPECPDL_LET:
3619 case SPECPDL_LET_DEFAULT:
3620 case SPECPDL_LET_LOCAL:
3622 Lisp_Object sym = specpdl_symbol (tmp);
3623 Lisp_Object val = specpdl_old_value (tmp);
3624 if (EQ (sym, Qinternal_interpreter_environment))
3626 Lisp_Object env = val;
3627 for (; CONSP (env); env = XCDR (env))
3629 Lisp_Object binding = XCAR (env);
3630 if (CONSP (binding))
3631 result = Fcons (Fcons (XCAR (binding),
3632 XCDR (binding)),
3633 result);
3636 else
3637 result = Fcons (Fcons (sym, val), result);
3643 /* Restore values from specpdl to original place. */
3644 backtrace_eval_unrewind (-distance);
3646 return result;
3650 void
3651 mark_specpdl (void)
3653 union specbinding *pdl;
3654 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3656 switch (pdl->kind)
3658 case SPECPDL_UNWIND:
3659 mark_object (specpdl_arg (pdl));
3660 break;
3662 case SPECPDL_BACKTRACE:
3664 ptrdiff_t nargs = backtrace_nargs (pdl);
3665 mark_object (backtrace_function (pdl));
3666 if (nargs == UNEVALLED)
3667 nargs = 1;
3668 while (nargs--)
3669 mark_object (backtrace_args (pdl)[nargs]);
3671 break;
3673 case SPECPDL_LET_DEFAULT:
3674 case SPECPDL_LET_LOCAL:
3675 mark_object (specpdl_where (pdl));
3676 /* Fall through. */
3677 case SPECPDL_LET:
3678 mark_object (specpdl_symbol (pdl));
3679 mark_object (specpdl_old_value (pdl));
3680 break;
3685 void
3686 get_backtrace (Lisp_Object array)
3688 union specbinding *pdl = backtrace_next (backtrace_top ());
3689 ptrdiff_t i = 0, asize = ASIZE (array);
3691 /* Copy the backtrace contents into working memory. */
3692 for (; i < asize; i++)
3694 if (backtrace_p (pdl))
3696 ASET (array, i, backtrace_function (pdl));
3697 pdl = backtrace_next (pdl);
3699 else
3700 ASET (array, i, Qnil);
3704 Lisp_Object backtrace_top_function (void)
3706 union specbinding *pdl = backtrace_top ();
3707 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3710 void
3711 syms_of_eval (void)
3713 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3714 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3715 If Lisp code tries to increase the total number past this amount,
3716 an error is signaled.
3717 You can safely use a value considerably larger than the default value,
3718 if that proves inconveniently small. However, if you increase it too far,
3719 Emacs could run out of memory trying to make the stack bigger.
3720 Note that this limit may be silently increased by the debugger
3721 if `debug-on-error' or `debug-on-quit' is set. */);
3723 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3724 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3726 This limit serves to catch infinite recursions for you before they cause
3727 actual stack overflow in C, which would be fatal for Emacs.
3728 You can safely make it considerably larger than its default value,
3729 if that proves inconveniently small. However, if you increase it too far,
3730 Emacs could overflow the real C stack, and crash. */);
3732 DEFVAR_LISP ("quit-flag", Vquit_flag,
3733 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3734 If the value is t, that means do an ordinary quit.
3735 If the value equals `throw-on-input', that means quit by throwing
3736 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3737 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3738 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3739 Vquit_flag = Qnil;
3741 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3742 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3743 Note that `quit-flag' will still be set by typing C-g,
3744 so a quit will be signaled as soon as `inhibit-quit' is nil.
3745 To prevent this happening, set `quit-flag' to nil
3746 before making `inhibit-quit' nil. */);
3747 Vinhibit_quit = Qnil;
3749 DEFSYM (Qinhibit_quit, "inhibit-quit");
3750 DEFSYM (Qautoload, "autoload");
3751 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3752 DEFSYM (Qmacro, "macro");
3753 DEFSYM (Qdeclare, "declare");
3755 /* Note that the process handling also uses Qexit, but we don't want
3756 to staticpro it twice, so we just do it here. */
3757 DEFSYM (Qexit, "exit");
3759 DEFSYM (Qinteractive, "interactive");
3760 DEFSYM (Qcommandp, "commandp");
3761 DEFSYM (Qand_rest, "&rest");
3762 DEFSYM (Qand_optional, "&optional");
3763 DEFSYM (Qclosure, "closure");
3764 DEFSYM (Qdebug, "debug");
3766 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3767 doc: /* Non-nil means never enter the debugger.
3768 Normally set while the debugger is already active, to avoid recursive
3769 invocations. */);
3770 Vinhibit_debugger = Qnil;
3772 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3773 doc: /* Non-nil means enter debugger if an error is signaled.
3774 Does not apply to errors handled by `condition-case' or those
3775 matched by `debug-ignored-errors'.
3776 If the value is a list, an error only means to enter the debugger
3777 if one of its condition symbols appears in the list.
3778 When you evaluate an expression interactively, this variable
3779 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3780 The command `toggle-debug-on-error' toggles this.
3781 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3782 Vdebug_on_error = Qnil;
3784 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3785 doc: /* List of errors for which the debugger should not be called.
3786 Each element may be a condition-name or a regexp that matches error messages.
3787 If any element applies to a given error, that error skips the debugger
3788 and just returns to top level.
3789 This overrides the variable `debug-on-error'.
3790 It does not apply to errors handled by `condition-case'. */);
3791 Vdebug_ignored_errors = Qnil;
3793 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3794 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3795 Does not apply if quit is handled by a `condition-case'. */);
3796 debug_on_quit = 0;
3798 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3799 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3801 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3802 doc: /* Non-nil means debugger may continue execution.
3803 This is nil when the debugger is called under circumstances where it
3804 might not be safe to continue. */);
3805 debugger_may_continue = 1;
3807 DEFVAR_LISP ("debugger", Vdebugger,
3808 doc: /* Function to call to invoke debugger.
3809 If due to frame exit, args are `exit' and the value being returned;
3810 this function's value will be returned instead of that.
3811 If due to error, args are `error' and a list of the args to `signal'.
3812 If due to `apply' or `funcall' entry, one arg, `lambda'.
3813 If due to `eval' entry, one arg, t. */);
3814 Vdebugger = Qnil;
3816 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3817 doc: /* If non-nil, this is a function for `signal' to call.
3818 It receives the same arguments that `signal' was given.
3819 The Edebug package uses this to regain control. */);
3820 Vsignal_hook_function = Qnil;
3822 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3823 doc: /* Non-nil means call the debugger regardless of condition handlers.
3824 Note that `debug-on-error', `debug-on-quit' and friends
3825 still determine whether to handle the particular condition. */);
3826 Vdebug_on_signal = Qnil;
3828 /* When lexical binding is being used,
3829 Vinternal_interpreter_environment is non-nil, and contains an alist
3830 of lexically-bound variable, or (t), indicating an empty
3831 environment. The lisp name of this variable would be
3832 `internal-interpreter-environment' if it weren't hidden.
3833 Every element of this list can be either a cons (VAR . VAL)
3834 specifying a lexical binding, or a single symbol VAR indicating
3835 that this variable should use dynamic scoping. */
3836 DEFSYM (Qinternal_interpreter_environment,
3837 "internal-interpreter-environment");
3838 DEFVAR_LISP ("internal-interpreter-environment",
3839 Vinternal_interpreter_environment,
3840 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3841 When lexical binding is not being used, this variable is nil.
3842 A value of `(t)' indicates an empty environment, otherwise it is an
3843 alist of active lexical bindings. */);
3844 Vinternal_interpreter_environment = Qnil;
3845 /* Don't export this variable to Elisp, so no one can mess with it
3846 (Just imagine if someone makes it buffer-local). */
3847 Funintern (Qinternal_interpreter_environment, Qnil);
3849 DEFSYM (Vrun_hooks, "run-hooks");
3851 staticpro (&Vautoload_queue);
3852 Vautoload_queue = Qnil;
3853 staticpro (&Vsignaling_function);
3854 Vsignaling_function = Qnil;
3856 inhibit_lisp_code = Qnil;
3858 defsubr (&Sor);
3859 defsubr (&Sand);
3860 defsubr (&Sif);
3861 defsubr (&Scond);
3862 defsubr (&Sprogn);
3863 defsubr (&Sprog1);
3864 defsubr (&Sprog2);
3865 defsubr (&Ssetq);
3866 defsubr (&Squote);
3867 defsubr (&Sfunction);
3868 defsubr (&Sdefault_toplevel_value);
3869 defsubr (&Sset_default_toplevel_value);
3870 defsubr (&Sdefvar);
3871 defsubr (&Sdefvaralias);
3872 defsubr (&Sdefconst);
3873 defsubr (&Smake_var_non_special);
3874 defsubr (&Slet);
3875 defsubr (&SletX);
3876 defsubr (&Swhile);
3877 defsubr (&Smacroexpand);
3878 defsubr (&Scatch);
3879 defsubr (&Sthrow);
3880 defsubr (&Sunwind_protect);
3881 defsubr (&Scondition_case);
3882 defsubr (&Ssignal);
3883 defsubr (&Scommandp);
3884 defsubr (&Sautoload);
3885 defsubr (&Sautoload_do_load);
3886 defsubr (&Seval);
3887 defsubr (&Sapply);
3888 defsubr (&Sfuncall);
3889 defsubr (&Srun_hooks);
3890 defsubr (&Srun_hook_with_args);
3891 defsubr (&Srun_hook_with_args_until_success);
3892 defsubr (&Srun_hook_with_args_until_failure);
3893 defsubr (&Srun_hook_wrapped);
3894 defsubr (&Sfetch_bytecode);
3895 defsubr (&Sbacktrace_debug);
3896 defsubr (&Sbacktrace);
3897 defsubr (&Sbacktrace_frame);
3898 defsubr (&Sbacktrace_eval);
3899 defsubr (&Sbacktrace__locals);
3900 defsubr (&Sspecial_variable_p);
3901 defsubr (&Sfunctionp);