* ibuf-ext.el (ibuffer-filter-by-filename): Make it work with dired buffers too.
[emacs.git] / src / eval.c
blobcb716690e3c9fe699bcab4ed70a875dd406a2ad9
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 #if !BYTE_MARK_STACK
36 static
37 #endif
38 struct catchtag *catchlist;
40 /* Chain of condition handlers currently in effect.
41 The elements of this chain are contained in the stack frames
42 of Fcondition_case and internal_condition_case.
43 When an error is signaled (by calling Fsignal, below),
44 this chain is searched for an element that applies. */
46 #if !BYTE_MARK_STACK
47 static
48 #endif
49 struct handler *handlerlist;
51 #ifdef DEBUG_GCPRO
52 /* Count levels of GCPRO to detect failure to UNGCPRO. */
53 int gcpro_level;
54 #endif
56 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57 Lisp_Object Qinhibit_quit;
58 Lisp_Object Qand_rest;
59 static Lisp_Object Qand_optional;
60 static Lisp_Object Qinhibit_debugger;
61 static Lisp_Object Qdeclare;
62 Lisp_Object Qinternal_interpreter_environment, Qclosure;
64 static Lisp_Object Qdebug;
66 /* This holds either the symbol `run-hooks' or nil.
67 It is nil at an early stage of startup, and when Emacs
68 is shutting down. */
70 Lisp_Object Vrun_hooks;
72 /* Non-nil means record all fset's and provide's, to be undone
73 if the file being autoloaded is not fully loaded.
74 They are recorded by being consed onto the front of Vautoload_queue:
75 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
77 Lisp_Object Vautoload_queue;
79 /* Current number of specbindings allocated in specpdl, not counting
80 the dummy entry specpdl[-1]. */
82 ptrdiff_t specpdl_size;
84 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
87 union specbinding *specpdl;
89 /* Pointer to first unused element in specpdl. */
91 union specbinding *specpdl_ptr;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
102 invocations. */
104 static EMACS_INT when_entered_debugger;
106 /* The function from which the last `signal' was called. Set in
107 Fsignal. */
108 /* FIXME: We should probably get rid of this! */
109 Lisp_Object Vsignaling_function;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code;
117 /* These would ordinarily be static, but they need to be visible to GDB. */
118 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
119 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
120 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
121 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
122 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
124 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
125 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
127 static Lisp_Object
128 specpdl_symbol (union specbinding *pdl)
130 eassert (pdl->kind >= SPECPDL_LET);
131 return pdl->let.symbol;
134 static Lisp_Object
135 specpdl_old_value (union specbinding *pdl)
137 eassert (pdl->kind >= SPECPDL_LET);
138 return pdl->let.old_value;
141 static void
142 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
144 eassert (pdl->kind >= SPECPDL_LET);
145 pdl->let.old_value = val;
148 static Lisp_Object
149 specpdl_where (union specbinding *pdl)
151 eassert (pdl->kind > SPECPDL_LET);
152 return pdl->let.where;
155 static Lisp_Object
156 specpdl_arg (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_UNWIND);
159 return pdl->unwind.arg;
162 Lisp_Object
163 backtrace_function (union specbinding *pdl)
165 eassert (pdl->kind == SPECPDL_BACKTRACE);
166 return pdl->bt.function;
169 static ptrdiff_t
170 backtrace_nargs (union specbinding *pdl)
172 eassert (pdl->kind == SPECPDL_BACKTRACE);
173 return pdl->bt.nargs;
176 Lisp_Object *
177 backtrace_args (union specbinding *pdl)
179 eassert (pdl->kind == SPECPDL_BACKTRACE);
180 return pdl->bt.args;
183 static bool
184 backtrace_debug_on_exit (union specbinding *pdl)
186 eassert (pdl->kind == SPECPDL_BACKTRACE);
187 return pdl->bt.debug_on_exit;
190 /* Functions to modify slots of backtrace records. */
192 static void
193 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
195 eassert (pdl->kind == SPECPDL_BACKTRACE);
196 pdl->bt.args = args;
199 static void
200 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
202 eassert (pdl->kind == SPECPDL_BACKTRACE);
203 pdl->bt.nargs = n;
206 static void
207 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
209 eassert (pdl->kind == SPECPDL_BACKTRACE);
210 pdl->bt.debug_on_exit = doe;
213 /* Helper functions to scan the backtrace. */
215 bool
216 backtrace_p (union specbinding *pdl)
217 { return pdl >= specpdl; }
219 union specbinding *
220 backtrace_top (void)
222 union specbinding *pdl = specpdl_ptr - 1;
223 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
224 pdl--;
225 return pdl;
228 union specbinding *
229 backtrace_next (union specbinding *pdl)
231 pdl--;
232 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
233 pdl--;
234 return pdl;
238 void
239 init_eval_once (void)
241 enum { size = 50 };
242 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
243 specpdl_size = size;
244 specpdl = specpdl_ptr = pdlvec + 1;
245 /* Don't forget to update docs (lispref node "Local Variables"). */
246 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
247 max_lisp_eval_depth = 600;
249 Vrun_hooks = Qnil;
252 void
253 init_eval (void)
255 specpdl_ptr = specpdl;
256 catchlist = 0;
257 handlerlist = 0;
258 Vquit_flag = Qnil;
259 debug_on_next_call = 0;
260 lisp_eval_depth = 0;
261 #ifdef DEBUG_GCPRO
262 gcpro_level = 0;
263 #endif
264 /* This is less than the initial value of num_nonmacro_input_events. */
265 when_entered_debugger = -1;
268 /* Unwind-protect function used by call_debugger. */
270 static void
271 restore_stack_limits (Lisp_Object data)
273 max_specpdl_size = XINT (XCAR (data));
274 max_lisp_eval_depth = XINT (XCDR (data));
277 /* Call the Lisp debugger, giving it argument ARG. */
279 Lisp_Object
280 call_debugger (Lisp_Object arg)
282 bool debug_while_redisplaying;
283 ptrdiff_t count = SPECPDL_INDEX ();
284 Lisp_Object val;
285 EMACS_INT old_max = max_specpdl_size;
287 /* Temporarily bump up the stack limits,
288 so the debugger won't run out of stack. */
290 max_specpdl_size += 1;
291 record_unwind_protect (restore_stack_limits,
292 Fcons (make_number (old_max),
293 make_number (max_lisp_eval_depth)));
294 max_specpdl_size = old_max;
296 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
297 max_lisp_eval_depth = lisp_eval_depth + 40;
299 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
300 max_specpdl_size = SPECPDL_INDEX () + 100;
302 #ifdef HAVE_WINDOW_SYSTEM
303 if (display_hourglass_p)
304 cancel_hourglass ();
305 #endif
307 debug_on_next_call = 0;
308 when_entered_debugger = num_nonmacro_input_events;
310 /* Resetting redisplaying_p to 0 makes sure that debug output is
311 displayed if the debugger is invoked during redisplay. */
312 debug_while_redisplaying = redisplaying_p;
313 redisplaying_p = 0;
314 specbind (intern ("debugger-may-continue"),
315 debug_while_redisplaying ? Qnil : Qt);
316 specbind (Qinhibit_redisplay, Qnil);
317 specbind (Qinhibit_debugger, Qt);
319 #if 0 /* Binding this prevents execution of Lisp code during
320 redisplay, which necessarily leads to display problems. */
321 specbind (Qinhibit_eval_during_redisplay, Qt);
322 #endif
324 val = apply1 (Vdebugger, arg);
326 /* Interrupting redisplay and resuming it later is not safe under
327 all circumstances. So, when the debugger returns, abort the
328 interrupted redisplay by going back to the top-level. */
329 if (debug_while_redisplaying)
330 Ftop_level ();
332 return unbind_to (count, val);
335 static void
336 do_debug_on_call (Lisp_Object code)
338 debug_on_next_call = 0;
339 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
340 call_debugger (list1 (code));
343 /* NOTE!!! Every function that can call EVAL must protect its args
344 and temporaries from garbage collection while it needs them.
345 The definition of `For' shows what you have to do. */
347 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
348 doc: /* Eval args until one of them yields non-nil, then return that value.
349 The remaining args are not evalled at all.
350 If all args return nil, return nil.
351 usage: (or CONDITIONS...) */)
352 (Lisp_Object args)
354 register Lisp_Object val = Qnil;
355 struct gcpro gcpro1;
357 GCPRO1 (args);
359 while (CONSP (args))
361 val = eval_sub (XCAR (args));
362 if (!NILP (val))
363 break;
364 args = XCDR (args);
367 UNGCPRO;
368 return val;
371 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
372 doc: /* Eval args until one of them yields nil, then return nil.
373 The remaining args are not evalled at all.
374 If no arg yields nil, return the last arg's value.
375 usage: (and CONDITIONS...) */)
376 (Lisp_Object args)
378 register Lisp_Object val = Qt;
379 struct gcpro gcpro1;
381 GCPRO1 (args);
383 while (CONSP (args))
385 val = eval_sub (XCAR (args));
386 if (NILP (val))
387 break;
388 args = XCDR (args);
391 UNGCPRO;
392 return val;
395 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
396 doc: /* If COND yields non-nil, do THEN, else do ELSE...
397 Returns the value of THEN or the value of the last of the ELSE's.
398 THEN must be one expression, but ELSE... can be zero or more expressions.
399 If COND yields nil, and there are no ELSE's, the value is nil.
400 usage: (if COND THEN ELSE...) */)
401 (Lisp_Object args)
403 Lisp_Object cond;
404 struct gcpro gcpro1;
406 GCPRO1 (args);
407 cond = eval_sub (XCAR (args));
408 UNGCPRO;
410 if (!NILP (cond))
411 return eval_sub (Fcar (XCDR (args)));
412 return Fprogn (XCDR (XCDR (args)));
415 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
416 doc: /* Try each clause until one succeeds.
417 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
418 and, if the value is non-nil, this clause succeeds:
419 then the expressions in BODY are evaluated and the last one's
420 value is the value of the cond-form.
421 If no clause succeeds, cond returns nil.
422 If a clause has one element, as in (CONDITION),
423 CONDITION's value if non-nil is returned from the cond-form.
424 usage: (cond CLAUSES...) */)
425 (Lisp_Object args)
427 Lisp_Object val = args;
428 struct gcpro gcpro1;
430 GCPRO1 (args);
431 while (CONSP (args))
433 Lisp_Object clause = XCAR (args);
434 val = eval_sub (Fcar (clause));
435 if (!NILP (val))
437 if (!NILP (XCDR (clause)))
438 val = Fprogn (XCDR (clause));
439 break;
441 args = XCDR (args);
443 UNGCPRO;
445 return val;
448 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
449 doc: /* Eval BODY forms sequentially and return value of last one.
450 usage: (progn BODY...) */)
451 (Lisp_Object body)
453 Lisp_Object val = Qnil;
454 struct gcpro gcpro1;
456 GCPRO1 (body);
458 while (CONSP (body))
460 val = eval_sub (XCAR (body));
461 body = XCDR (body);
464 UNGCPRO;
465 return val;
468 /* Evaluate BODY sequentially, discarding its value. Suitable for
469 record_unwind_protect. */
471 void
472 unwind_body (Lisp_Object body)
474 Fprogn (body);
477 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
478 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
479 The value of FIRST is saved during the evaluation of the remaining args,
480 whose values are discarded.
481 usage: (prog1 FIRST BODY...) */)
482 (Lisp_Object args)
484 Lisp_Object val;
485 Lisp_Object args_left;
486 struct gcpro gcpro1, gcpro2;
488 args_left = args;
489 val = args;
490 GCPRO2 (args, val);
492 val = eval_sub (XCAR (args_left));
493 while (CONSP (args_left = XCDR (args_left)))
494 eval_sub (XCAR (args_left));
496 UNGCPRO;
497 return val;
500 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
501 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
502 The value of FORM2 is saved during the evaluation of the
503 remaining args, whose values are discarded.
504 usage: (prog2 FORM1 FORM2 BODY...) */)
505 (Lisp_Object args)
507 struct gcpro gcpro1;
509 GCPRO1 (args);
510 eval_sub (XCAR (args));
511 UNGCPRO;
512 return Fprog1 (XCDR (args));
515 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
516 doc: /* Set each SYM to the value of its VAL.
517 The symbols SYM are variables; they are literal (not evaluated).
518 The values VAL are expressions; they are evaluated.
519 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
520 The second VAL is not computed until after the first SYM is set, and so on;
521 each VAL can use the new value of variables set earlier in the `setq'.
522 The return value of the `setq' form is the value of the last VAL.
523 usage: (setq [SYM VAL]...) */)
524 (Lisp_Object args)
526 Lisp_Object val, sym, lex_binding;
528 val = args;
529 if (CONSP (args))
531 Lisp_Object args_left = args;
532 struct gcpro gcpro1;
533 GCPRO1 (args);
537 val = eval_sub (Fcar (XCDR (args_left)));
538 sym = XCAR (args_left);
540 /* Like for eval_sub, we do not check declared_special here since
541 it's been done when let-binding. */
542 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
543 && SYMBOLP (sym)
544 && !NILP (lex_binding
545 = Fassq (sym, Vinternal_interpreter_environment)))
546 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
547 else
548 Fset (sym, val); /* SYM is dynamically bound. */
550 args_left = Fcdr (XCDR (args_left));
552 while (CONSP (args_left));
554 UNGCPRO;
557 return val;
560 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
561 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
562 Warning: `quote' does not construct its return value, but just returns
563 the value that was pre-constructed by the Lisp reader (see info node
564 `(elisp)Printed Representation').
565 This means that '(a . b) is not identical to (cons 'a 'b): the former
566 does not cons. Quoting should be reserved for constants that will
567 never be modified by side-effects, unless you like self-modifying code.
568 See the common pitfall in info node `(elisp)Rearrangement' for an example
569 of unexpected results when a quoted object is modified.
570 usage: (quote ARG) */)
571 (Lisp_Object args)
573 if (CONSP (XCDR (args)))
574 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
575 return XCAR (args);
578 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
579 doc: /* Like `quote', but preferred for objects which are functions.
580 In byte compilation, `function' causes its argument to be compiled.
581 `quote' cannot do that.
582 usage: (function ARG) */)
583 (Lisp_Object args)
585 Lisp_Object quoted = XCAR (args);
587 if (CONSP (XCDR (args)))
588 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
590 if (!NILP (Vinternal_interpreter_environment)
591 && CONSP (quoted)
592 && EQ (XCAR (quoted), Qlambda))
593 /* This is a lambda expression within a lexical environment;
594 return an interpreted closure instead of a simple lambda. */
595 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
596 XCDR (quoted)));
597 else
598 /* Simply quote the argument. */
599 return quoted;
603 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
604 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
605 Aliased variables always have the same value; setting one sets the other.
606 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
607 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
608 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
609 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
610 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
611 The return value is BASE-VARIABLE. */)
612 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
614 struct Lisp_Symbol *sym;
616 CHECK_SYMBOL (new_alias);
617 CHECK_SYMBOL (base_variable);
619 sym = XSYMBOL (new_alias);
621 if (sym->constant)
622 /* Not sure why, but why not? */
623 error ("Cannot make a constant an alias");
625 switch (sym->redirect)
627 case SYMBOL_FORWARDED:
628 error ("Cannot make an internal variable an alias");
629 case SYMBOL_LOCALIZED:
630 error ("Don't know how to make a localized variable an alias");
633 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
634 If n_a is bound, but b_v is not, set the value of b_v to n_a,
635 so that old-code that affects n_a before the aliasing is setup
636 still works. */
637 if (NILP (Fboundp (base_variable)))
638 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
641 union specbinding *p;
643 for (p = specpdl_ptr; p > specpdl; )
644 if ((--p)->kind >= SPECPDL_LET
645 && (EQ (new_alias, specpdl_symbol (p))))
646 error ("Don't know how to make a let-bound variable an alias");
649 sym->declared_special = 1;
650 XSYMBOL (base_variable)->declared_special = 1;
651 sym->redirect = SYMBOL_VARALIAS;
652 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
653 sym->constant = SYMBOL_CONSTANT_P (base_variable);
654 LOADHIST_ATTACH (new_alias);
655 /* Even if docstring is nil: remove old docstring. */
656 Fput (new_alias, Qvariable_documentation, docstring);
658 return base_variable;
662 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
663 doc: /* Define SYMBOL as a variable, and return SYMBOL.
664 You are not required to define a variable in order to use it, but
665 defining it lets you supply an initial value and documentation, which
666 can be referred to by the Emacs help facilities and other programming
667 tools. The `defvar' form also declares the variable as \"special\",
668 so that it is always dynamically bound even if `lexical-binding' is t.
670 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
671 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
672 default value is what is set; buffer-local values are not affected.
673 If INITVALUE is missing, SYMBOL's value is not set.
675 If SYMBOL has a local binding, then this form affects the local
676 binding. This is usually not what you want. Thus, if you need to
677 load a file defining variables, with this form or with `defconst' or
678 `defcustom', you should always load that file _outside_ any bindings
679 for these variables. \(`defconst' and `defcustom' behave similarly in
680 this respect.)
682 The optional argument DOCSTRING is a documentation string for the
683 variable.
685 To define a user option, use `defcustom' instead of `defvar'.
686 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
687 (Lisp_Object args)
689 Lisp_Object sym, tem, tail;
691 sym = XCAR (args);
692 tail = XCDR (args);
694 if (CONSP (tail))
696 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
697 error ("Too many arguments");
699 tem = Fdefault_boundp (sym);
701 /* Do it before evaluating the initial value, for self-references. */
702 XSYMBOL (sym)->declared_special = 1;
704 if (NILP (tem))
705 Fset_default (sym, eval_sub (XCAR (tail)));
706 else
707 { /* Check if there is really a global binding rather than just a let
708 binding that shadows the global unboundness of the var. */
709 union specbinding *pdl = specpdl_ptr;
710 while (pdl > specpdl)
712 if ((--pdl)->kind >= SPECPDL_LET
713 && EQ (specpdl_symbol (pdl), sym)
714 && EQ (specpdl_old_value (pdl), Qunbound))
716 message_with_string
717 ("Warning: defvar ignored because %s is let-bound",
718 SYMBOL_NAME (sym), 1);
719 break;
723 tail = XCDR (tail);
724 tem = Fcar (tail);
725 if (!NILP (tem))
727 if (!NILP (Vpurify_flag))
728 tem = Fpurecopy (tem);
729 Fput (sym, Qvariable_documentation, tem);
731 LOADHIST_ATTACH (sym);
733 else if (!NILP (Vinternal_interpreter_environment)
734 && !XSYMBOL (sym)->declared_special)
735 /* A simple (defvar foo) with lexical scoping does "nothing" except
736 declare that var to be dynamically scoped *locally* (i.e. within
737 the current file or let-block). */
738 Vinternal_interpreter_environment
739 = Fcons (sym, Vinternal_interpreter_environment);
740 else
742 /* Simple (defvar <var>) should not count as a definition at all.
743 It could get in the way of other definitions, and unloading this
744 package could try to make the variable unbound. */
747 return sym;
750 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
751 doc: /* Define SYMBOL as a constant variable.
752 This declares that neither programs nor users should ever change the
753 value. This constancy is not actually enforced by Emacs Lisp, but
754 SYMBOL is marked as a special variable so that it is never lexically
755 bound.
757 The `defconst' form always sets the value of SYMBOL to the result of
758 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
759 what is set; buffer-local values are not affected. If SYMBOL has a
760 local binding, then this form sets the local binding's value.
761 However, you should normally not make local bindings for variables
762 defined with this form.
764 The optional DOCSTRING specifies the variable's documentation string.
765 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
766 (Lisp_Object args)
768 Lisp_Object sym, tem;
770 sym = XCAR (args);
771 if (CONSP (Fcdr (XCDR (XCDR (args)))))
772 error ("Too many arguments");
774 tem = eval_sub (Fcar (XCDR (args)));
775 if (!NILP (Vpurify_flag))
776 tem = Fpurecopy (tem);
777 Fset_default (sym, tem);
778 XSYMBOL (sym)->declared_special = 1;
779 tem = Fcar (XCDR (XCDR (args)));
780 if (!NILP (tem))
782 if (!NILP (Vpurify_flag))
783 tem = Fpurecopy (tem);
784 Fput (sym, Qvariable_documentation, tem);
786 Fput (sym, Qrisky_local_variable, Qt);
787 LOADHIST_ATTACH (sym);
788 return sym;
791 /* Make SYMBOL lexically scoped. */
792 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
793 Smake_var_non_special, 1, 1, 0,
794 doc: /* Internal function. */)
795 (Lisp_Object symbol)
797 CHECK_SYMBOL (symbol);
798 XSYMBOL (symbol)->declared_special = 0;
799 return Qnil;
803 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
804 doc: /* Bind variables according to VARLIST then eval BODY.
805 The value of the last form in BODY is returned.
806 Each element of VARLIST is a symbol (which is bound to nil)
807 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
808 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
809 usage: (let* VARLIST BODY...) */)
810 (Lisp_Object args)
812 Lisp_Object varlist, var, val, elt, lexenv;
813 ptrdiff_t count = SPECPDL_INDEX ();
814 struct gcpro gcpro1, gcpro2, gcpro3;
816 GCPRO3 (args, elt, varlist);
818 lexenv = Vinternal_interpreter_environment;
820 varlist = XCAR (args);
821 while (CONSP (varlist))
823 QUIT;
825 elt = XCAR (varlist);
826 if (SYMBOLP (elt))
828 var = elt;
829 val = Qnil;
831 else if (! NILP (Fcdr (Fcdr (elt))))
832 signal_error ("`let' bindings can have only one value-form", elt);
833 else
835 var = Fcar (elt);
836 val = eval_sub (Fcar (Fcdr (elt)));
839 if (!NILP (lexenv) && SYMBOLP (var)
840 && !XSYMBOL (var)->declared_special
841 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
842 /* Lexically bind VAR by adding it to the interpreter's binding
843 alist. */
845 Lisp_Object newenv
846 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
847 if (EQ (Vinternal_interpreter_environment, lexenv))
848 /* Save the old lexical environment on the specpdl stack,
849 but only for the first lexical binding, since we'll never
850 need to revert to one of the intermediate ones. */
851 specbind (Qinternal_interpreter_environment, newenv);
852 else
853 Vinternal_interpreter_environment = newenv;
855 else
856 specbind (var, val);
858 varlist = XCDR (varlist);
860 UNGCPRO;
861 val = Fprogn (XCDR (args));
862 return unbind_to (count, val);
865 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
866 doc: /* Bind variables according to VARLIST then eval BODY.
867 The value of the last form in BODY is returned.
868 Each element of VARLIST is a symbol (which is bound to nil)
869 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
870 All the VALUEFORMs are evalled before any symbols are bound.
871 usage: (let VARLIST BODY...) */)
872 (Lisp_Object args)
874 Lisp_Object *temps, tem, lexenv;
875 register Lisp_Object elt, varlist;
876 ptrdiff_t count = SPECPDL_INDEX ();
877 ptrdiff_t argnum;
878 struct gcpro gcpro1, gcpro2;
879 USE_SAFE_ALLOCA;
881 varlist = XCAR (args);
883 /* Make space to hold the values to give the bound variables. */
884 elt = Flength (varlist);
885 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
887 /* Compute the values and store them in `temps'. */
889 GCPRO2 (args, *temps);
890 gcpro2.nvars = 0;
892 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
894 QUIT;
895 elt = XCAR (varlist);
896 if (SYMBOLP (elt))
897 temps [argnum++] = Qnil;
898 else if (! NILP (Fcdr (Fcdr (elt))))
899 signal_error ("`let' bindings can have only one value-form", elt);
900 else
901 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
902 gcpro2.nvars = argnum;
904 UNGCPRO;
906 lexenv = Vinternal_interpreter_environment;
908 varlist = XCAR (args);
909 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
911 Lisp_Object var;
913 elt = XCAR (varlist);
914 var = SYMBOLP (elt) ? elt : Fcar (elt);
915 tem = temps[argnum++];
917 if (!NILP (lexenv) && SYMBOLP (var)
918 && !XSYMBOL (var)->declared_special
919 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
920 /* Lexically bind VAR by adding it to the lexenv alist. */
921 lexenv = Fcons (Fcons (var, tem), lexenv);
922 else
923 /* Dynamically bind VAR. */
924 specbind (var, tem);
927 if (!EQ (lexenv, Vinternal_interpreter_environment))
928 /* Instantiate a new lexical environment. */
929 specbind (Qinternal_interpreter_environment, lexenv);
931 elt = Fprogn (XCDR (args));
932 SAFE_FREE ();
933 return unbind_to (count, elt);
936 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
937 doc: /* If TEST yields non-nil, eval BODY... and repeat.
938 The order of execution is thus TEST, BODY, TEST, BODY and so on
939 until TEST returns nil.
940 usage: (while TEST BODY...) */)
941 (Lisp_Object args)
943 Lisp_Object test, body;
944 struct gcpro gcpro1, gcpro2;
946 GCPRO2 (test, body);
948 test = XCAR (args);
949 body = XCDR (args);
950 while (!NILP (eval_sub (test)))
952 QUIT;
953 Fprogn (body);
956 UNGCPRO;
957 return Qnil;
960 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
961 doc: /* Return result of expanding macros at top level of FORM.
962 If FORM is not a macro call, it is returned unchanged.
963 Otherwise, the macro is expanded and the expansion is considered
964 in place of FORM. When a non-macro-call results, it is returned.
966 The second optional arg ENVIRONMENT specifies an environment of macro
967 definitions to shadow the loaded ones for use in file byte-compilation. */)
968 (Lisp_Object form, Lisp_Object environment)
970 /* With cleanups from Hallvard Furuseth. */
971 register Lisp_Object expander, sym, def, tem;
973 while (1)
975 /* Come back here each time we expand a macro call,
976 in case it expands into another macro call. */
977 if (!CONSP (form))
978 break;
979 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
980 def = sym = XCAR (form);
981 tem = Qnil;
982 /* Trace symbols aliases to other symbols
983 until we get a symbol that is not an alias. */
984 while (SYMBOLP (def))
986 QUIT;
987 sym = def;
988 tem = Fassq (sym, environment);
989 if (NILP (tem))
991 def = XSYMBOL (sym)->function;
992 if (!NILP (def))
993 continue;
995 break;
997 /* Right now TEM is the result from SYM in ENVIRONMENT,
998 and if TEM is nil then DEF is SYM's function definition. */
999 if (NILP (tem))
1001 /* SYM is not mentioned in ENVIRONMENT.
1002 Look at its function definition. */
1003 struct gcpro gcpro1;
1004 GCPRO1 (form);
1005 def = Fautoload_do_load (def, sym, Qmacro);
1006 UNGCPRO;
1007 if (!CONSP (def))
1008 /* Not defined or definition not suitable. */
1009 break;
1010 if (!EQ (XCAR (def), Qmacro))
1011 break;
1012 else expander = XCDR (def);
1014 else
1016 expander = XCDR (tem);
1017 if (NILP (expander))
1018 break;
1021 Lisp_Object newform = apply1 (expander, XCDR (form));
1022 if (EQ (form, newform))
1023 break;
1024 else
1025 form = newform;
1028 return form;
1031 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1032 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1033 TAG is evalled to get the tag to use; it must not be nil.
1035 Then the BODY is executed.
1036 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1037 If no throw happens, `catch' returns the value of the last BODY form.
1038 If a throw happens, it specifies the value to return from `catch'.
1039 usage: (catch TAG BODY...) */)
1040 (Lisp_Object args)
1042 register Lisp_Object tag;
1043 struct gcpro gcpro1;
1045 GCPRO1 (args);
1046 tag = eval_sub (XCAR (args));
1047 UNGCPRO;
1048 return internal_catch (tag, Fprogn, XCDR (args));
1051 /* Set up a catch, then call C function FUNC on argument ARG.
1052 FUNC should return a Lisp_Object.
1053 This is how catches are done from within C code. */
1055 Lisp_Object
1056 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1058 /* This structure is made part of the chain `catchlist'. */
1059 struct catchtag c;
1061 /* Fill in the components of c, and put it on the list. */
1062 c.next = catchlist;
1063 c.tag = tag;
1064 c.val = Qnil;
1065 c.handlerlist = handlerlist;
1066 c.lisp_eval_depth = lisp_eval_depth;
1067 c.pdlcount = SPECPDL_INDEX ();
1068 c.poll_suppress_count = poll_suppress_count;
1069 c.interrupt_input_blocked = interrupt_input_blocked;
1070 c.gcpro = gcprolist;
1071 c.byte_stack = byte_stack_list;
1072 catchlist = &c;
1074 /* Call FUNC. */
1075 if (! sys_setjmp (c.jmp))
1076 c.val = (*func) (arg);
1078 /* Throw works by a longjmp that comes right here. */
1079 catchlist = c.next;
1080 return c.val;
1083 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1084 jump to that CATCH, returning VALUE as the value of that catch.
1086 This is the guts of Fthrow and Fsignal; they differ only in the way
1087 they choose the catch tag to throw to. A catch tag for a
1088 condition-case form has a TAG of Qnil.
1090 Before each catch is discarded, unbind all special bindings and
1091 execute all unwind-protect clauses made above that catch. Unwind
1092 the handler stack as we go, so that the proper handlers are in
1093 effect for each unwind-protect clause we run. At the end, restore
1094 some static info saved in CATCH, and longjmp to the location
1095 specified there.
1097 This is used for correct unwinding in Fthrow and Fsignal. */
1099 static _Noreturn void
1100 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1102 bool last_time;
1104 /* Save the value in the tag. */
1105 catch->val = value;
1107 /* Restore certain special C variables. */
1108 set_poll_suppress_count (catch->poll_suppress_count);
1109 unblock_input_to (catch->interrupt_input_blocked);
1110 immediate_quit = 0;
1114 last_time = catchlist == catch;
1116 /* Unwind the specpdl stack, and then restore the proper set of
1117 handlers. */
1118 unbind_to (catchlist->pdlcount, Qnil);
1119 handlerlist = catchlist->handlerlist;
1120 catchlist = catchlist->next;
1122 while (! last_time);
1124 byte_stack_list = catch->byte_stack;
1125 gcprolist = catch->gcpro;
1126 #ifdef DEBUG_GCPRO
1127 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1128 #endif
1129 lisp_eval_depth = catch->lisp_eval_depth;
1131 sys_longjmp (catch->jmp, 1);
1134 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1135 doc: /* Throw to the catch for TAG and return VALUE from it.
1136 Both TAG and VALUE are evalled. */)
1137 (register Lisp_Object tag, Lisp_Object value)
1139 register struct catchtag *c;
1141 if (!NILP (tag))
1142 for (c = catchlist; c; c = c->next)
1144 if (EQ (c->tag, tag))
1145 unwind_to_catch (c, value);
1147 xsignal2 (Qno_catch, tag, value);
1151 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1152 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1153 If BODYFORM completes normally, its value is returned
1154 after executing the UNWINDFORMS.
1155 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1156 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1157 (Lisp_Object args)
1159 Lisp_Object val;
1160 ptrdiff_t count = SPECPDL_INDEX ();
1162 record_unwind_protect (unwind_body, XCDR (args));
1163 val = eval_sub (XCAR (args));
1164 return unbind_to (count, val);
1167 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1168 doc: /* Regain control when an error is signaled.
1169 Executes BODYFORM and returns its value if no error happens.
1170 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1171 where the BODY is made of Lisp expressions.
1173 A handler is applicable to an error
1174 if CONDITION-NAME is one of the error's condition names.
1175 If an error happens, the first applicable handler is run.
1177 The car of a handler may be a list of condition names instead of a
1178 single condition name; then it handles all of them. If the special
1179 condition name `debug' is present in this list, it allows another
1180 condition in the list to run the debugger if `debug-on-error' and the
1181 other usual mechanisms says it should (otherwise, `condition-case'
1182 suppresses the debugger).
1184 When a handler handles an error, control returns to the `condition-case'
1185 and it executes the handler's BODY...
1186 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1187 \(If VAR is nil, the handler can't access that information.)
1188 Then the value of the last BODY form is returned from the `condition-case'
1189 expression.
1191 See also the function `signal' for more info.
1192 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1193 (Lisp_Object args)
1195 Lisp_Object var = XCAR (args);
1196 Lisp_Object bodyform = XCAR (XCDR (args));
1197 Lisp_Object handlers = XCDR (XCDR (args));
1199 return internal_lisp_condition_case (var, bodyform, handlers);
1202 /* Like Fcondition_case, but the args are separate
1203 rather than passed in a list. Used by Fbyte_code. */
1205 Lisp_Object
1206 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1207 Lisp_Object handlers)
1209 Lisp_Object val;
1210 struct catchtag c;
1211 struct handler h;
1213 CHECK_SYMBOL (var);
1215 for (val = handlers; CONSP (val); val = XCDR (val))
1217 Lisp_Object tem;
1218 tem = XCAR (val);
1219 if (! (NILP (tem)
1220 || (CONSP (tem)
1221 && (SYMBOLP (XCAR (tem))
1222 || CONSP (XCAR (tem))))))
1223 error ("Invalid condition handler: %s",
1224 SDATA (Fprin1_to_string (tem, Qt)));
1227 c.tag = Qnil;
1228 c.val = Qnil;
1229 c.handlerlist = handlerlist;
1230 c.lisp_eval_depth = lisp_eval_depth;
1231 c.pdlcount = SPECPDL_INDEX ();
1232 c.poll_suppress_count = poll_suppress_count;
1233 c.interrupt_input_blocked = interrupt_input_blocked;
1234 c.gcpro = gcprolist;
1235 c.byte_stack = byte_stack_list;
1236 if (sys_setjmp (c.jmp))
1238 if (!NILP (h.var))
1239 specbind (h.var, c.val);
1240 val = Fprogn (Fcdr (h.chosen_clause));
1242 /* Note that this just undoes the binding of h.var; whoever
1243 longjumped to us unwound the stack to c.pdlcount before
1244 throwing. */
1245 unbind_to (c.pdlcount, Qnil);
1246 return val;
1248 c.next = catchlist;
1249 catchlist = &c;
1251 h.var = var;
1252 h.handler = handlers;
1253 h.next = handlerlist;
1254 h.tag = &c;
1255 handlerlist = &h;
1257 val = eval_sub (bodyform);
1258 catchlist = c.next;
1259 handlerlist = h.next;
1260 return val;
1263 /* Call the function BFUN with no arguments, catching errors within it
1264 according to HANDLERS. If there is an error, call HFUN with
1265 one argument which is the data that describes the error:
1266 (SIGNALNAME . DATA)
1268 HANDLERS can be a list of conditions to catch.
1269 If HANDLERS is Qt, catch all errors.
1270 If HANDLERS is Qerror, catch all errors
1271 but allow the debugger to run if that is enabled. */
1273 Lisp_Object
1274 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1275 Lisp_Object (*hfun) (Lisp_Object))
1277 Lisp_Object val;
1278 struct catchtag c;
1279 struct handler h;
1281 c.tag = Qnil;
1282 c.val = Qnil;
1283 c.handlerlist = handlerlist;
1284 c.lisp_eval_depth = lisp_eval_depth;
1285 c.pdlcount = SPECPDL_INDEX ();
1286 c.poll_suppress_count = poll_suppress_count;
1287 c.interrupt_input_blocked = interrupt_input_blocked;
1288 c.gcpro = gcprolist;
1289 c.byte_stack = byte_stack_list;
1290 if (sys_setjmp (c.jmp))
1292 return (*hfun) (c.val);
1294 c.next = catchlist;
1295 catchlist = &c;
1296 h.handler = handlers;
1297 h.var = Qnil;
1298 h.next = handlerlist;
1299 h.tag = &c;
1300 handlerlist = &h;
1302 val = (*bfun) ();
1303 catchlist = c.next;
1304 handlerlist = h.next;
1305 return val;
1308 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1310 Lisp_Object
1311 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1312 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1314 Lisp_Object val;
1315 struct catchtag c;
1316 struct handler h;
1318 c.tag = Qnil;
1319 c.val = Qnil;
1320 c.handlerlist = handlerlist;
1321 c.lisp_eval_depth = lisp_eval_depth;
1322 c.pdlcount = SPECPDL_INDEX ();
1323 c.poll_suppress_count = poll_suppress_count;
1324 c.interrupt_input_blocked = interrupt_input_blocked;
1325 c.gcpro = gcprolist;
1326 c.byte_stack = byte_stack_list;
1327 if (sys_setjmp (c.jmp))
1329 return (*hfun) (c.val);
1331 c.next = catchlist;
1332 catchlist = &c;
1333 h.handler = handlers;
1334 h.var = Qnil;
1335 h.next = handlerlist;
1336 h.tag = &c;
1337 handlerlist = &h;
1339 val = (*bfun) (arg);
1340 catchlist = c.next;
1341 handlerlist = h.next;
1342 return val;
1345 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1346 its arguments. */
1348 Lisp_Object
1349 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1350 Lisp_Object arg1,
1351 Lisp_Object arg2,
1352 Lisp_Object handlers,
1353 Lisp_Object (*hfun) (Lisp_Object))
1355 Lisp_Object val;
1356 struct catchtag c;
1357 struct handler h;
1359 c.tag = Qnil;
1360 c.val = Qnil;
1361 c.handlerlist = handlerlist;
1362 c.lisp_eval_depth = lisp_eval_depth;
1363 c.pdlcount = SPECPDL_INDEX ();
1364 c.poll_suppress_count = poll_suppress_count;
1365 c.interrupt_input_blocked = interrupt_input_blocked;
1366 c.gcpro = gcprolist;
1367 c.byte_stack = byte_stack_list;
1368 if (sys_setjmp (c.jmp))
1370 return (*hfun) (c.val);
1372 c.next = catchlist;
1373 catchlist = &c;
1374 h.handler = handlers;
1375 h.var = Qnil;
1376 h.next = handlerlist;
1377 h.tag = &c;
1378 handlerlist = &h;
1380 val = (*bfun) (arg1, arg2);
1381 catchlist = c.next;
1382 handlerlist = h.next;
1383 return val;
1386 /* Like internal_condition_case but call BFUN with NARGS as first,
1387 and ARGS as second argument. */
1389 Lisp_Object
1390 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1391 ptrdiff_t nargs,
1392 Lisp_Object *args,
1393 Lisp_Object handlers,
1394 Lisp_Object (*hfun) (Lisp_Object err,
1395 ptrdiff_t nargs,
1396 Lisp_Object *args))
1398 Lisp_Object val;
1399 struct catchtag c;
1400 struct handler h;
1402 c.tag = Qnil;
1403 c.val = Qnil;
1404 c.handlerlist = handlerlist;
1405 c.lisp_eval_depth = lisp_eval_depth;
1406 c.pdlcount = SPECPDL_INDEX ();
1407 c.poll_suppress_count = poll_suppress_count;
1408 c.interrupt_input_blocked = interrupt_input_blocked;
1409 c.gcpro = gcprolist;
1410 c.byte_stack = byte_stack_list;
1411 if (sys_setjmp (c.jmp))
1413 return (*hfun) (c.val, nargs, args);
1415 c.next = catchlist;
1416 catchlist = &c;
1417 h.handler = handlers;
1418 h.var = Qnil;
1419 h.next = handlerlist;
1420 h.tag = &c;
1421 handlerlist = &h;
1423 val = (*bfun) (nargs, args);
1424 catchlist = c.next;
1425 handlerlist = h.next;
1426 return val;
1430 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1431 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1432 Lisp_Object data);
1434 void
1435 process_quit_flag (void)
1437 Lisp_Object flag = Vquit_flag;
1438 Vquit_flag = Qnil;
1439 if (EQ (flag, Qkill_emacs))
1440 Fkill_emacs (Qnil);
1441 if (EQ (Vthrow_on_input, flag))
1442 Fthrow (Vthrow_on_input, Qt);
1443 Fsignal (Qquit, Qnil);
1446 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1447 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1448 This function does not return.
1450 An error symbol is a symbol with an `error-conditions' property
1451 that is a list of condition names.
1452 A handler for any of those names will get to handle this signal.
1453 The symbol `error' should normally be one of them.
1455 DATA should be a list. Its elements are printed as part of the error message.
1456 See Info anchor `(elisp)Definition of signal' for some details on how this
1457 error message is constructed.
1458 If the signal is handled, DATA is made available to the handler.
1459 See also the function `condition-case'. */)
1460 (Lisp_Object error_symbol, Lisp_Object data)
1462 /* When memory is full, ERROR-SYMBOL is nil,
1463 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1464 That is a special case--don't do this in other situations. */
1465 Lisp_Object conditions;
1466 Lisp_Object string;
1467 Lisp_Object real_error_symbol
1468 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1469 register Lisp_Object clause = Qnil;
1470 struct handler *h;
1472 immediate_quit = 0;
1473 abort_on_gc = 0;
1474 if (gc_in_progress || waiting_for_input)
1475 emacs_abort ();
1477 #if 0 /* rms: I don't know why this was here,
1478 but it is surely wrong for an error that is handled. */
1479 #ifdef HAVE_WINDOW_SYSTEM
1480 if (display_hourglass_p)
1481 cancel_hourglass ();
1482 #endif
1483 #endif
1485 /* This hook is used by edebug. */
1486 if (! NILP (Vsignal_hook_function)
1487 && ! NILP (error_symbol))
1489 /* Edebug takes care of restoring these variables when it exits. */
1490 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1491 max_lisp_eval_depth = lisp_eval_depth + 20;
1493 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1494 max_specpdl_size = SPECPDL_INDEX () + 40;
1496 call2 (Vsignal_hook_function, error_symbol, data);
1499 conditions = Fget (real_error_symbol, Qerror_conditions);
1501 /* Remember from where signal was called. Skip over the frame for
1502 `signal' itself. If a frame for `error' follows, skip that,
1503 too. Don't do this when ERROR_SYMBOL is nil, because that
1504 is a memory-full error. */
1505 Vsignaling_function = Qnil;
1506 if (!NILP (error_symbol))
1508 union specbinding *pdl = backtrace_next (backtrace_top ());
1509 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1510 pdl = backtrace_next (pdl);
1511 if (backtrace_p (pdl))
1512 Vsignaling_function = backtrace_function (pdl);
1515 for (h = handlerlist; h; h = h->next)
1517 clause = find_handler_clause (h->handler, conditions);
1518 if (!NILP (clause))
1519 break;
1522 if (/* Don't run the debugger for a memory-full error.
1523 (There is no room in memory to do that!) */
1524 !NILP (error_symbol)
1525 && (!NILP (Vdebug_on_signal)
1526 /* If no handler is present now, try to run the debugger. */
1527 || NILP (clause)
1528 /* A `debug' symbol in the handler list disables the normal
1529 suppression of the debugger. */
1530 || (CONSP (clause) && CONSP (XCAR (clause))
1531 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1532 /* Special handler that means "print a message and run debugger
1533 if requested". */
1534 || EQ (h->handler, Qerror)))
1536 bool debugger_called
1537 = maybe_call_debugger (conditions, error_symbol, data);
1538 /* We can't return values to code which signaled an error, but we
1539 can continue code which has signaled a quit. */
1540 if (debugger_called && EQ (real_error_symbol, Qquit))
1541 return Qnil;
1544 if (!NILP (clause))
1546 Lisp_Object unwind_data
1547 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1549 h->chosen_clause = clause;
1550 unwind_to_catch (h->tag, unwind_data);
1552 else
1554 if (catchlist != 0)
1555 Fthrow (Qtop_level, Qt);
1558 if (! NILP (error_symbol))
1559 data = Fcons (error_symbol, data);
1561 string = Ferror_message_string (data);
1562 fatal ("%s", SDATA (string));
1565 /* Internal version of Fsignal that never returns.
1566 Used for anything but Qquit (which can return from Fsignal). */
1568 void
1569 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1571 Fsignal (error_symbol, data);
1572 emacs_abort ();
1575 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1577 void
1578 xsignal0 (Lisp_Object error_symbol)
1580 xsignal (error_symbol, Qnil);
1583 void
1584 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1586 xsignal (error_symbol, list1 (arg));
1589 void
1590 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1592 xsignal (error_symbol, list2 (arg1, arg2));
1595 void
1596 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1598 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1601 /* Signal `error' with message S, and additional arg ARG.
1602 If ARG is not a genuine list, make it a one-element list. */
1604 void
1605 signal_error (const char *s, Lisp_Object arg)
1607 Lisp_Object tortoise, hare;
1609 hare = tortoise = arg;
1610 while (CONSP (hare))
1612 hare = XCDR (hare);
1613 if (!CONSP (hare))
1614 break;
1616 hare = XCDR (hare);
1617 tortoise = XCDR (tortoise);
1619 if (EQ (hare, tortoise))
1620 break;
1623 if (!NILP (hare))
1624 arg = list1 (arg);
1626 xsignal (Qerror, Fcons (build_string (s), arg));
1630 /* Return true if LIST is a non-nil atom or
1631 a list containing one of CONDITIONS. */
1633 static bool
1634 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1636 if (NILP (list))
1637 return 0;
1638 if (! CONSP (list))
1639 return 1;
1641 while (CONSP (conditions))
1643 Lisp_Object this, tail;
1644 this = XCAR (conditions);
1645 for (tail = list; CONSP (tail); tail = XCDR (tail))
1646 if (EQ (XCAR (tail), this))
1647 return 1;
1648 conditions = XCDR (conditions);
1650 return 0;
1653 /* Return true if an error with condition-symbols CONDITIONS,
1654 and described by SIGNAL-DATA, should skip the debugger
1655 according to debugger-ignored-errors. */
1657 static bool
1658 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1660 Lisp_Object tail;
1661 bool first_string = 1;
1662 Lisp_Object error_message;
1664 error_message = Qnil;
1665 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1667 if (STRINGP (XCAR (tail)))
1669 if (first_string)
1671 error_message = Ferror_message_string (data);
1672 first_string = 0;
1675 if (fast_string_match (XCAR (tail), error_message) >= 0)
1676 return 1;
1678 else
1680 Lisp_Object contail;
1682 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1683 if (EQ (XCAR (tail), XCAR (contail)))
1684 return 1;
1688 return 0;
1691 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1692 SIG and DATA describe the signal. There are two ways to pass them:
1693 = SIG is the error symbol, and DATA is the rest of the data.
1694 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1695 This is for memory-full errors only. */
1696 static bool
1697 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1699 Lisp_Object combined_data;
1701 combined_data = Fcons (sig, data);
1703 if (
1704 /* Don't try to run the debugger with interrupts blocked.
1705 The editing loop would return anyway. */
1706 ! input_blocked_p ()
1707 && NILP (Vinhibit_debugger)
1708 /* Does user want to enter debugger for this kind of error? */
1709 && (EQ (sig, Qquit)
1710 ? debug_on_quit
1711 : wants_debugger (Vdebug_on_error, conditions))
1712 && ! skip_debugger (conditions, combined_data)
1713 /* RMS: What's this for? */
1714 && when_entered_debugger < num_nonmacro_input_events)
1716 call_debugger (list2 (Qerror, combined_data));
1717 return 1;
1720 return 0;
1723 static Lisp_Object
1724 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1726 register Lisp_Object h;
1728 /* t is used by handlers for all conditions, set up by C code. */
1729 if (EQ (handlers, Qt))
1730 return Qt;
1732 /* error is used similarly, but means print an error message
1733 and run the debugger if that is enabled. */
1734 if (EQ (handlers, Qerror))
1735 return Qt;
1737 for (h = handlers; CONSP (h); h = XCDR (h))
1739 Lisp_Object handler = XCAR (h);
1740 Lisp_Object condit, tem;
1742 if (!CONSP (handler))
1743 continue;
1744 condit = XCAR (handler);
1745 /* Handle a single condition name in handler HANDLER. */
1746 if (SYMBOLP (condit))
1748 tem = Fmemq (Fcar (handler), conditions);
1749 if (!NILP (tem))
1750 return handler;
1752 /* Handle a list of condition names in handler HANDLER. */
1753 else if (CONSP (condit))
1755 Lisp_Object tail;
1756 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1758 tem = Fmemq (XCAR (tail), conditions);
1759 if (!NILP (tem))
1760 return handler;
1765 return Qnil;
1769 /* Dump an error message; called like vprintf. */
1770 void
1771 verror (const char *m, va_list ap)
1773 char buf[4000];
1774 ptrdiff_t size = sizeof buf;
1775 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1776 char *buffer = buf;
1777 ptrdiff_t used;
1778 Lisp_Object string;
1780 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1781 string = make_string (buffer, used);
1782 if (buffer != buf)
1783 xfree (buffer);
1785 xsignal1 (Qerror, string);
1789 /* Dump an error message; called like printf. */
1791 /* VARARGS 1 */
1792 void
1793 error (const char *m, ...)
1795 va_list ap;
1796 va_start (ap, m);
1797 verror (m, ap);
1800 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1801 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1802 This means it contains a description for how to read arguments to give it.
1803 The value is nil for an invalid function or a symbol with no function
1804 definition.
1806 Interactively callable functions include strings and vectors (treated
1807 as keyboard macros), lambda-expressions that contain a top-level call
1808 to `interactive', autoload definitions made by `autoload' with non-nil
1809 fourth argument, and some of the built-in functions of Lisp.
1811 Also, a symbol satisfies `commandp' if its function definition does so.
1813 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1814 then strings and vectors are not accepted. */)
1815 (Lisp_Object function, Lisp_Object for_call_interactively)
1817 register Lisp_Object fun;
1818 register Lisp_Object funcar;
1819 Lisp_Object if_prop = Qnil;
1821 fun = function;
1823 fun = indirect_function (fun); /* Check cycles. */
1824 if (NILP (fun))
1825 return Qnil;
1827 /* Check an `interactive-form' property if present, analogous to the
1828 function-documentation property. */
1829 fun = function;
1830 while (SYMBOLP (fun))
1832 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1833 if (!NILP (tmp))
1834 if_prop = Qt;
1835 fun = Fsymbol_function (fun);
1838 /* Emacs primitives are interactive if their DEFUN specifies an
1839 interactive spec. */
1840 if (SUBRP (fun))
1841 return XSUBR (fun)->intspec ? Qt : if_prop;
1843 /* Bytecode objects are interactive if they are long enough to
1844 have an element whose index is COMPILED_INTERACTIVE, which is
1845 where the interactive spec is stored. */
1846 else if (COMPILEDP (fun))
1847 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1848 ? Qt : if_prop);
1850 /* Strings and vectors are keyboard macros. */
1851 if (STRINGP (fun) || VECTORP (fun))
1852 return (NILP (for_call_interactively) ? Qt : Qnil);
1854 /* Lists may represent commands. */
1855 if (!CONSP (fun))
1856 return Qnil;
1857 funcar = XCAR (fun);
1858 if (EQ (funcar, Qclosure))
1859 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1860 ? Qt : if_prop);
1861 else if (EQ (funcar, Qlambda))
1862 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1863 else if (EQ (funcar, Qautoload))
1864 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1865 else
1866 return Qnil;
1869 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1870 doc: /* Define FUNCTION to autoload from FILE.
1871 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1872 Third arg DOCSTRING is documentation for the function.
1873 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1874 Fifth arg TYPE indicates the type of the object:
1875 nil or omitted says FUNCTION is a function,
1876 `keymap' says FUNCTION is really a keymap, and
1877 `macro' or t says FUNCTION is really a macro.
1878 Third through fifth args give info about the real definition.
1879 They default to nil.
1880 If FUNCTION is already defined other than as an autoload,
1881 this does nothing and returns nil. */)
1882 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1884 CHECK_SYMBOL (function);
1885 CHECK_STRING (file);
1887 /* If function is defined and not as an autoload, don't override. */
1888 if (!NILP (XSYMBOL (function)->function)
1889 && !AUTOLOADP (XSYMBOL (function)->function))
1890 return Qnil;
1892 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1893 /* `read1' in lread.c has found the docstring starting with "\
1894 and assumed the docstring will be provided by Snarf-documentation, so it
1895 passed us 0 instead. But that leads to accidental sharing in purecopy's
1896 hash-consing, so we use a (hopefully) unique integer instead. */
1897 docstring = make_number (XHASH (function));
1898 return Fdefalias (function,
1899 list5 (Qautoload, file, docstring, interactive, type),
1900 Qnil);
1903 void
1904 un_autoload (Lisp_Object oldqueue)
1906 Lisp_Object queue, first, second;
1908 /* Queue to unwind is current value of Vautoload_queue.
1909 oldqueue is the shadowed value to leave in Vautoload_queue. */
1910 queue = Vautoload_queue;
1911 Vautoload_queue = oldqueue;
1912 while (CONSP (queue))
1914 first = XCAR (queue);
1915 second = Fcdr (first);
1916 first = Fcar (first);
1917 if (EQ (first, make_number (0)))
1918 Vfeatures = second;
1919 else
1920 Ffset (first, second);
1921 queue = XCDR (queue);
1925 /* Load an autoloaded function.
1926 FUNNAME is the symbol which is the function's name.
1927 FUNDEF is the autoload definition (a list). */
1929 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1930 doc: /* Load FUNDEF which should be an autoload.
1931 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1932 in which case the function returns the new autoloaded function value.
1933 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1934 it is defines a macro. */)
1935 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1937 ptrdiff_t count = SPECPDL_INDEX ();
1938 struct gcpro gcpro1, gcpro2, gcpro3;
1940 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1941 return fundef;
1943 if (EQ (macro_only, Qmacro))
1945 Lisp_Object kind = Fnth (make_number (4), fundef);
1946 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1947 return fundef;
1950 /* This is to make sure that loadup.el gives a clear picture
1951 of what files are preloaded and when. */
1952 if (! NILP (Vpurify_flag))
1953 error ("Attempt to autoload %s while preparing to dump",
1954 SDATA (SYMBOL_NAME (funname)));
1956 CHECK_SYMBOL (funname);
1957 GCPRO3 (funname, fundef, macro_only);
1959 /* Preserve the match data. */
1960 record_unwind_save_match_data ();
1962 /* If autoloading gets an error (which includes the error of failing
1963 to define the function being called), we use Vautoload_queue
1964 to undo function definitions and `provide' calls made by
1965 the function. We do this in the specific case of autoloading
1966 because autoloading is not an explicit request "load this file",
1967 but rather a request to "call this function".
1969 The value saved here is to be restored into Vautoload_queue. */
1970 record_unwind_protect (un_autoload, Vautoload_queue);
1971 Vautoload_queue = Qt;
1972 /* If `macro_only', assume this autoload to be a "best-effort",
1973 so don't signal an error if autoloading fails. */
1974 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1976 /* Once loading finishes, don't undo it. */
1977 Vautoload_queue = Qt;
1978 unbind_to (count, Qnil);
1980 UNGCPRO;
1982 if (NILP (funname))
1983 return Qnil;
1984 else
1986 Lisp_Object fun = Findirect_function (funname, Qnil);
1988 if (!NILP (Fequal (fun, fundef)))
1989 error ("Autoloading failed to define function %s",
1990 SDATA (SYMBOL_NAME (funname)));
1991 else
1992 return fun;
1997 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1998 doc: /* Evaluate FORM and return its value.
1999 If LEXICAL is t, evaluate using lexical scoping. */)
2000 (Lisp_Object form, Lisp_Object lexical)
2002 ptrdiff_t count = SPECPDL_INDEX ();
2003 specbind (Qinternal_interpreter_environment,
2004 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2005 return unbind_to (count, eval_sub (form));
2008 /* Grow the specpdl stack by one entry.
2009 The caller should have already initialized the entry.
2010 Signal an error on stack overflow.
2012 Make sure that there is always one unused entry past the top of the
2013 stack, so that the just-initialized entry is safely unwound if
2014 memory exhausted and an error is signaled here. Also, allocate a
2015 never-used entry just before the bottom of the stack; sometimes its
2016 address is taken. */
2018 static void
2019 grow_specpdl (void)
2021 specpdl_ptr++;
2023 if (specpdl_ptr == specpdl + specpdl_size)
2025 ptrdiff_t count = SPECPDL_INDEX ();
2026 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2027 union specbinding *pdlvec = specpdl - 1;
2028 ptrdiff_t pdlvecsize = specpdl_size + 1;
2029 if (max_size <= specpdl_size)
2031 if (max_specpdl_size < 400)
2032 max_size = max_specpdl_size = 400;
2033 if (max_size <= specpdl_size)
2034 signal_error ("Variable binding depth exceeds max-specpdl-size",
2035 Qnil);
2037 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2038 specpdl = pdlvec + 1;
2039 specpdl_size = pdlvecsize - 1;
2040 specpdl_ptr = specpdl + count;
2044 void
2045 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2047 eassert (nargs >= UNEVALLED);
2048 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2049 specpdl_ptr->bt.debug_on_exit = false;
2050 specpdl_ptr->bt.function = function;
2051 specpdl_ptr->bt.args = args;
2052 specpdl_ptr->bt.nargs = nargs;
2053 grow_specpdl ();
2056 /* Eval a sub-expression of the current expression (i.e. in the same
2057 lexical scope). */
2058 Lisp_Object
2059 eval_sub (Lisp_Object form)
2061 Lisp_Object fun, val, original_fun, original_args;
2062 Lisp_Object funcar;
2063 struct gcpro gcpro1, gcpro2, gcpro3;
2065 if (SYMBOLP (form))
2067 /* Look up its binding in the lexical environment.
2068 We do not pay attention to the declared_special flag here, since we
2069 already did that when let-binding the variable. */
2070 Lisp_Object lex_binding
2071 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2072 ? Fassq (form, Vinternal_interpreter_environment)
2073 : Qnil;
2074 if (CONSP (lex_binding))
2075 return XCDR (lex_binding);
2076 else
2077 return Fsymbol_value (form);
2080 if (!CONSP (form))
2081 return form;
2083 QUIT;
2085 GCPRO1 (form);
2086 maybe_gc ();
2087 UNGCPRO;
2089 if (++lisp_eval_depth > max_lisp_eval_depth)
2091 if (max_lisp_eval_depth < 100)
2092 max_lisp_eval_depth = 100;
2093 if (lisp_eval_depth > max_lisp_eval_depth)
2094 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2097 original_fun = XCAR (form);
2098 original_args = XCDR (form);
2100 /* This also protects them from gc. */
2101 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2103 if (debug_on_next_call)
2104 do_debug_on_call (Qt);
2106 /* At this point, only original_fun and original_args
2107 have values that will be used below. */
2108 retry:
2110 /* Optimize for no indirection. */
2111 fun = original_fun;
2112 if (SYMBOLP (fun) && !NILP (fun)
2113 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2114 fun = indirect_function (fun);
2116 if (SUBRP (fun))
2118 Lisp_Object numargs;
2119 Lisp_Object argvals[8];
2120 Lisp_Object args_left;
2121 register int i, maxargs;
2123 args_left = original_args;
2124 numargs = Flength (args_left);
2126 check_cons_list ();
2128 if (XINT (numargs) < XSUBR (fun)->min_args
2129 || (XSUBR (fun)->max_args >= 0
2130 && XSUBR (fun)->max_args < XINT (numargs)))
2131 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2133 else if (XSUBR (fun)->max_args == UNEVALLED)
2134 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2135 else if (XSUBR (fun)->max_args == MANY)
2137 /* Pass a vector of evaluated arguments. */
2138 Lisp_Object *vals;
2139 ptrdiff_t argnum = 0;
2140 USE_SAFE_ALLOCA;
2142 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2144 GCPRO3 (args_left, fun, fun);
2145 gcpro3.var = vals;
2146 gcpro3.nvars = 0;
2148 while (!NILP (args_left))
2150 vals[argnum++] = eval_sub (Fcar (args_left));
2151 args_left = Fcdr (args_left);
2152 gcpro3.nvars = argnum;
2155 set_backtrace_args (specpdl_ptr - 1, vals);
2156 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2158 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2159 UNGCPRO;
2160 SAFE_FREE ();
2162 else
2164 GCPRO3 (args_left, fun, fun);
2165 gcpro3.var = argvals;
2166 gcpro3.nvars = 0;
2168 maxargs = XSUBR (fun)->max_args;
2169 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2171 argvals[i] = eval_sub (Fcar (args_left));
2172 gcpro3.nvars = ++i;
2175 UNGCPRO;
2177 set_backtrace_args (specpdl_ptr - 1, argvals);
2178 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2180 switch (i)
2182 case 0:
2183 val = (XSUBR (fun)->function.a0 ());
2184 break;
2185 case 1:
2186 val = (XSUBR (fun)->function.a1 (argvals[0]));
2187 break;
2188 case 2:
2189 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2190 break;
2191 case 3:
2192 val = (XSUBR (fun)->function.a3
2193 (argvals[0], argvals[1], argvals[2]));
2194 break;
2195 case 4:
2196 val = (XSUBR (fun)->function.a4
2197 (argvals[0], argvals[1], argvals[2], argvals[3]));
2198 break;
2199 case 5:
2200 val = (XSUBR (fun)->function.a5
2201 (argvals[0], argvals[1], argvals[2], argvals[3],
2202 argvals[4]));
2203 break;
2204 case 6:
2205 val = (XSUBR (fun)->function.a6
2206 (argvals[0], argvals[1], argvals[2], argvals[3],
2207 argvals[4], argvals[5]));
2208 break;
2209 case 7:
2210 val = (XSUBR (fun)->function.a7
2211 (argvals[0], argvals[1], argvals[2], argvals[3],
2212 argvals[4], argvals[5], argvals[6]));
2213 break;
2215 case 8:
2216 val = (XSUBR (fun)->function.a8
2217 (argvals[0], argvals[1], argvals[2], argvals[3],
2218 argvals[4], argvals[5], argvals[6], argvals[7]));
2219 break;
2221 default:
2222 /* Someone has created a subr that takes more arguments than
2223 is supported by this code. We need to either rewrite the
2224 subr to use a different argument protocol, or add more
2225 cases to this switch. */
2226 emacs_abort ();
2230 else if (COMPILEDP (fun))
2231 val = apply_lambda (fun, original_args);
2232 else
2234 if (NILP (fun))
2235 xsignal1 (Qvoid_function, original_fun);
2236 if (!CONSP (fun))
2237 xsignal1 (Qinvalid_function, original_fun);
2238 funcar = XCAR (fun);
2239 if (!SYMBOLP (funcar))
2240 xsignal1 (Qinvalid_function, original_fun);
2241 if (EQ (funcar, Qautoload))
2243 Fautoload_do_load (fun, original_fun, Qnil);
2244 goto retry;
2246 if (EQ (funcar, Qmacro))
2248 ptrdiff_t count = SPECPDL_INDEX ();
2249 Lisp_Object exp;
2250 /* Bind lexical-binding during expansion of the macro, so the
2251 macro can know reliably if the code it outputs will be
2252 interpreted using lexical-binding or not. */
2253 specbind (Qlexical_binding,
2254 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2255 exp = apply1 (Fcdr (fun), original_args);
2256 unbind_to (count, Qnil);
2257 val = eval_sub (exp);
2259 else if (EQ (funcar, Qlambda)
2260 || EQ (funcar, Qclosure))
2261 val = apply_lambda (fun, original_args);
2262 else
2263 xsignal1 (Qinvalid_function, original_fun);
2265 check_cons_list ();
2267 lisp_eval_depth--;
2268 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2269 val = call_debugger (list2 (Qexit, val));
2270 specpdl_ptr--;
2272 return val;
2275 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2276 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2277 Then return the value FUNCTION returns.
2278 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2279 usage: (apply FUNCTION &rest ARGUMENTS) */)
2280 (ptrdiff_t nargs, Lisp_Object *args)
2282 ptrdiff_t i;
2283 EMACS_INT numargs;
2284 register Lisp_Object spread_arg;
2285 register Lisp_Object *funcall_args;
2286 Lisp_Object fun, retval;
2287 struct gcpro gcpro1;
2288 USE_SAFE_ALLOCA;
2290 fun = args [0];
2291 funcall_args = 0;
2292 spread_arg = args [nargs - 1];
2293 CHECK_LIST (spread_arg);
2295 numargs = XINT (Flength (spread_arg));
2297 if (numargs == 0)
2298 return Ffuncall (nargs - 1, args);
2299 else if (numargs == 1)
2301 args [nargs - 1] = XCAR (spread_arg);
2302 return Ffuncall (nargs, args);
2305 numargs += nargs - 2;
2307 /* Optimize for no indirection. */
2308 if (SYMBOLP (fun) && !NILP (fun)
2309 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2310 fun = indirect_function (fun);
2311 if (NILP (fun))
2313 /* Let funcall get the error. */
2314 fun = args[0];
2315 goto funcall;
2318 if (SUBRP (fun))
2320 if (numargs < XSUBR (fun)->min_args
2321 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2322 goto funcall; /* Let funcall get the error. */
2323 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2325 /* Avoid making funcall cons up a yet another new vector of arguments
2326 by explicitly supplying nil's for optional values. */
2327 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2328 for (i = numargs; i < XSUBR (fun)->max_args;)
2329 funcall_args[++i] = Qnil;
2330 GCPRO1 (*funcall_args);
2331 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2334 funcall:
2335 /* We add 1 to numargs because funcall_args includes the
2336 function itself as well as its arguments. */
2337 if (!funcall_args)
2339 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2340 GCPRO1 (*funcall_args);
2341 gcpro1.nvars = 1 + numargs;
2344 memcpy (funcall_args, args, nargs * word_size);
2345 /* Spread the last arg we got. Its first element goes in
2346 the slot that it used to occupy, hence this value of I. */
2347 i = nargs - 1;
2348 while (!NILP (spread_arg))
2350 funcall_args [i++] = XCAR (spread_arg);
2351 spread_arg = XCDR (spread_arg);
2354 /* By convention, the caller needs to gcpro Ffuncall's args. */
2355 retval = Ffuncall (gcpro1.nvars, funcall_args);
2356 UNGCPRO;
2357 SAFE_FREE ();
2359 return retval;
2362 /* Run hook variables in various ways. */
2364 static Lisp_Object
2365 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2367 Ffuncall (nargs, args);
2368 return Qnil;
2371 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2372 doc: /* Run each hook in HOOKS.
2373 Each argument should be a symbol, a hook variable.
2374 These symbols are processed in the order specified.
2375 If a hook symbol has a non-nil value, that value may be a function
2376 or a list of functions to be called to run the hook.
2377 If the value is a function, it is called with no arguments.
2378 If it is a list, the elements are called, in order, with no arguments.
2380 Major modes should not use this function directly to run their mode
2381 hook; they should use `run-mode-hooks' instead.
2383 Do not use `make-local-variable' to make a hook variable buffer-local.
2384 Instead, use `add-hook' and specify t for the LOCAL argument.
2385 usage: (run-hooks &rest HOOKS) */)
2386 (ptrdiff_t nargs, Lisp_Object *args)
2388 Lisp_Object hook[1];
2389 ptrdiff_t i;
2391 for (i = 0; i < nargs; i++)
2393 hook[0] = args[i];
2394 run_hook_with_args (1, hook, funcall_nil);
2397 return Qnil;
2400 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2401 Srun_hook_with_args, 1, MANY, 0,
2402 doc: /* Run HOOK with the specified arguments ARGS.
2403 HOOK should be a symbol, a hook variable. The value of HOOK
2404 may be nil, a function, or a list of functions. Call each
2405 function in order with arguments ARGS. The final return value
2406 is unspecified.
2408 Do not use `make-local-variable' to make a hook variable buffer-local.
2409 Instead, use `add-hook' and specify t for the LOCAL argument.
2410 usage: (run-hook-with-args HOOK &rest ARGS) */)
2411 (ptrdiff_t nargs, Lisp_Object *args)
2413 return run_hook_with_args (nargs, args, funcall_nil);
2416 /* NB this one still documents a specific non-nil return value.
2417 (As did run-hook-with-args and run-hook-with-args-until-failure
2418 until they were changed in 24.1.) */
2419 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2420 Srun_hook_with_args_until_success, 1, MANY, 0,
2421 doc: /* Run HOOK with the specified arguments ARGS.
2422 HOOK should be a symbol, a hook variable. The value of HOOK
2423 may be nil, a function, or a list of functions. Call each
2424 function in order with arguments ARGS, stopping at the first
2425 one that returns non-nil, and return that value. Otherwise (if
2426 all functions return nil, or if there are no functions to call),
2427 return nil.
2429 Do not use `make-local-variable' to make a hook variable buffer-local.
2430 Instead, use `add-hook' and specify t for the LOCAL argument.
2431 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2432 (ptrdiff_t nargs, Lisp_Object *args)
2434 return run_hook_with_args (nargs, args, Ffuncall);
2437 static Lisp_Object
2438 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2440 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2443 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2444 Srun_hook_with_args_until_failure, 1, MANY, 0,
2445 doc: /* Run HOOK with the specified arguments ARGS.
2446 HOOK should be a symbol, a hook variable. The value of HOOK
2447 may be nil, a function, or a list of functions. Call each
2448 function in order with arguments ARGS, stopping at the first
2449 one that returns nil, and return nil. Otherwise (if all functions
2450 return non-nil, or if there are no functions to call), return non-nil
2451 \(do not rely on the precise return value in this case).
2453 Do not use `make-local-variable' to make a hook variable buffer-local.
2454 Instead, use `add-hook' and specify t for the LOCAL argument.
2455 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2456 (ptrdiff_t nargs, Lisp_Object *args)
2458 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2461 static Lisp_Object
2462 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2464 Lisp_Object tmp = args[0], ret;
2465 args[0] = args[1];
2466 args[1] = tmp;
2467 ret = Ffuncall (nargs, args);
2468 args[1] = args[0];
2469 args[0] = tmp;
2470 return ret;
2473 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2474 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2475 I.e. instead of calling each function FUN directly with arguments ARGS,
2476 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2477 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2478 aborts and returns that value.
2479 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2480 (ptrdiff_t nargs, Lisp_Object *args)
2482 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2485 /* ARGS[0] should be a hook symbol.
2486 Call each of the functions in the hook value, passing each of them
2487 as arguments all the rest of ARGS (all NARGS - 1 elements).
2488 FUNCALL specifies how to call each function on the hook.
2489 The caller (or its caller, etc) must gcpro all of ARGS,
2490 except that it isn't necessary to gcpro ARGS[0]. */
2492 Lisp_Object
2493 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2494 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2496 Lisp_Object sym, val, ret = Qnil;
2497 struct gcpro gcpro1, gcpro2, gcpro3;
2499 /* If we are dying or still initializing,
2500 don't do anything--it would probably crash if we tried. */
2501 if (NILP (Vrun_hooks))
2502 return Qnil;
2504 sym = args[0];
2505 val = find_symbol_value (sym);
2507 if (EQ (val, Qunbound) || NILP (val))
2508 return ret;
2509 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2511 args[0] = val;
2512 return funcall (nargs, args);
2514 else
2516 Lisp_Object global_vals = Qnil;
2517 GCPRO3 (sym, val, global_vals);
2519 for (;
2520 CONSP (val) && NILP (ret);
2521 val = XCDR (val))
2523 if (EQ (XCAR (val), Qt))
2525 /* t indicates this hook has a local binding;
2526 it means to run the global binding too. */
2527 global_vals = Fdefault_value (sym);
2528 if (NILP (global_vals)) continue;
2530 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2532 args[0] = global_vals;
2533 ret = funcall (nargs, args);
2535 else
2537 for (;
2538 CONSP (global_vals) && NILP (ret);
2539 global_vals = XCDR (global_vals))
2541 args[0] = XCAR (global_vals);
2542 /* In a global value, t should not occur. If it does, we
2543 must ignore it to avoid an endless loop. */
2544 if (!EQ (args[0], Qt))
2545 ret = funcall (nargs, args);
2549 else
2551 args[0] = XCAR (val);
2552 ret = funcall (nargs, args);
2556 UNGCPRO;
2557 return ret;
2561 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2563 void
2564 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2566 Lisp_Object temp[3];
2567 temp[0] = hook;
2568 temp[1] = arg1;
2569 temp[2] = arg2;
2571 Frun_hook_with_args (3, temp);
2574 /* Apply fn to arg. */
2575 Lisp_Object
2576 apply1 (Lisp_Object fn, Lisp_Object arg)
2578 struct gcpro gcpro1;
2580 GCPRO1 (fn);
2581 if (NILP (arg))
2582 RETURN_UNGCPRO (Ffuncall (1, &fn));
2583 gcpro1.nvars = 2;
2585 Lisp_Object args[2];
2586 args[0] = fn;
2587 args[1] = arg;
2588 gcpro1.var = args;
2589 RETURN_UNGCPRO (Fapply (2, args));
2593 /* Call function fn on no arguments. */
2594 Lisp_Object
2595 call0 (Lisp_Object fn)
2597 struct gcpro gcpro1;
2599 GCPRO1 (fn);
2600 RETURN_UNGCPRO (Ffuncall (1, &fn));
2603 /* Call function fn with 1 argument arg1. */
2604 /* ARGSUSED */
2605 Lisp_Object
2606 call1 (Lisp_Object fn, Lisp_Object arg1)
2608 struct gcpro gcpro1;
2609 Lisp_Object args[2];
2611 args[0] = fn;
2612 args[1] = arg1;
2613 GCPRO1 (args[0]);
2614 gcpro1.nvars = 2;
2615 RETURN_UNGCPRO (Ffuncall (2, args));
2618 /* Call function fn with 2 arguments arg1, arg2. */
2619 /* ARGSUSED */
2620 Lisp_Object
2621 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2623 struct gcpro gcpro1;
2624 Lisp_Object args[3];
2625 args[0] = fn;
2626 args[1] = arg1;
2627 args[2] = arg2;
2628 GCPRO1 (args[0]);
2629 gcpro1.nvars = 3;
2630 RETURN_UNGCPRO (Ffuncall (3, args));
2633 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2634 /* ARGSUSED */
2635 Lisp_Object
2636 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2638 struct gcpro gcpro1;
2639 Lisp_Object args[4];
2640 args[0] = fn;
2641 args[1] = arg1;
2642 args[2] = arg2;
2643 args[3] = arg3;
2644 GCPRO1 (args[0]);
2645 gcpro1.nvars = 4;
2646 RETURN_UNGCPRO (Ffuncall (4, args));
2649 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2650 /* ARGSUSED */
2651 Lisp_Object
2652 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2653 Lisp_Object arg4)
2655 struct gcpro gcpro1;
2656 Lisp_Object args[5];
2657 args[0] = fn;
2658 args[1] = arg1;
2659 args[2] = arg2;
2660 args[3] = arg3;
2661 args[4] = arg4;
2662 GCPRO1 (args[0]);
2663 gcpro1.nvars = 5;
2664 RETURN_UNGCPRO (Ffuncall (5, args));
2667 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2668 /* ARGSUSED */
2669 Lisp_Object
2670 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2671 Lisp_Object arg4, Lisp_Object arg5)
2673 struct gcpro gcpro1;
2674 Lisp_Object args[6];
2675 args[0] = fn;
2676 args[1] = arg1;
2677 args[2] = arg2;
2678 args[3] = arg3;
2679 args[4] = arg4;
2680 args[5] = arg5;
2681 GCPRO1 (args[0]);
2682 gcpro1.nvars = 6;
2683 RETURN_UNGCPRO (Ffuncall (6, args));
2686 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2687 /* ARGSUSED */
2688 Lisp_Object
2689 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2690 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2692 struct gcpro gcpro1;
2693 Lisp_Object args[7];
2694 args[0] = fn;
2695 args[1] = arg1;
2696 args[2] = arg2;
2697 args[3] = arg3;
2698 args[4] = arg4;
2699 args[5] = arg5;
2700 args[6] = arg6;
2701 GCPRO1 (args[0]);
2702 gcpro1.nvars = 7;
2703 RETURN_UNGCPRO (Ffuncall (7, args));
2706 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2707 /* ARGSUSED */
2708 Lisp_Object
2709 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2710 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2712 struct gcpro gcpro1;
2713 Lisp_Object args[8];
2714 args[0] = fn;
2715 args[1] = arg1;
2716 args[2] = arg2;
2717 args[3] = arg3;
2718 args[4] = arg4;
2719 args[5] = arg5;
2720 args[6] = arg6;
2721 args[7] = arg7;
2722 GCPRO1 (args[0]);
2723 gcpro1.nvars = 8;
2724 RETURN_UNGCPRO (Ffuncall (8, args));
2727 /* The caller should GCPRO all the elements of ARGS. */
2729 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2730 doc: /* Non-nil if OBJECT is a function. */)
2731 (Lisp_Object object)
2733 if (FUNCTIONP (object))
2734 return Qt;
2735 return Qnil;
2738 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2739 doc: /* Call first argument as a function, passing remaining arguments to it.
2740 Return the value that function returns.
2741 Thus, (funcall 'cons 'x 'y) returns (x . y).
2742 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2743 (ptrdiff_t nargs, Lisp_Object *args)
2745 Lisp_Object fun, original_fun;
2746 Lisp_Object funcar;
2747 ptrdiff_t numargs = nargs - 1;
2748 Lisp_Object lisp_numargs;
2749 Lisp_Object val;
2750 register Lisp_Object *internal_args;
2751 ptrdiff_t i;
2753 QUIT;
2755 if (++lisp_eval_depth > max_lisp_eval_depth)
2757 if (max_lisp_eval_depth < 100)
2758 max_lisp_eval_depth = 100;
2759 if (lisp_eval_depth > max_lisp_eval_depth)
2760 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2763 /* This also GCPROs them. */
2764 record_in_backtrace (args[0], &args[1], nargs - 1);
2766 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2767 maybe_gc ();
2769 if (debug_on_next_call)
2770 do_debug_on_call (Qlambda);
2772 check_cons_list ();
2774 original_fun = args[0];
2776 retry:
2778 /* Optimize for no indirection. */
2779 fun = original_fun;
2780 if (SYMBOLP (fun) && !NILP (fun)
2781 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2782 fun = indirect_function (fun);
2784 if (SUBRP (fun))
2786 if (numargs < XSUBR (fun)->min_args
2787 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2789 XSETFASTINT (lisp_numargs, numargs);
2790 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2793 else if (XSUBR (fun)->max_args == UNEVALLED)
2794 xsignal1 (Qinvalid_function, original_fun);
2796 else if (XSUBR (fun)->max_args == MANY)
2797 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2798 else
2800 if (XSUBR (fun)->max_args > numargs)
2802 internal_args = alloca (XSUBR (fun)->max_args
2803 * sizeof *internal_args);
2804 memcpy (internal_args, args + 1, numargs * word_size);
2805 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2806 internal_args[i] = Qnil;
2808 else
2809 internal_args = args + 1;
2810 switch (XSUBR (fun)->max_args)
2812 case 0:
2813 val = (XSUBR (fun)->function.a0 ());
2814 break;
2815 case 1:
2816 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2817 break;
2818 case 2:
2819 val = (XSUBR (fun)->function.a2
2820 (internal_args[0], internal_args[1]));
2821 break;
2822 case 3:
2823 val = (XSUBR (fun)->function.a3
2824 (internal_args[0], internal_args[1], internal_args[2]));
2825 break;
2826 case 4:
2827 val = (XSUBR (fun)->function.a4
2828 (internal_args[0], internal_args[1], internal_args[2],
2829 internal_args[3]));
2830 break;
2831 case 5:
2832 val = (XSUBR (fun)->function.a5
2833 (internal_args[0], internal_args[1], internal_args[2],
2834 internal_args[3], internal_args[4]));
2835 break;
2836 case 6:
2837 val = (XSUBR (fun)->function.a6
2838 (internal_args[0], internal_args[1], internal_args[2],
2839 internal_args[3], internal_args[4], internal_args[5]));
2840 break;
2841 case 7:
2842 val = (XSUBR (fun)->function.a7
2843 (internal_args[0], internal_args[1], internal_args[2],
2844 internal_args[3], internal_args[4], internal_args[5],
2845 internal_args[6]));
2846 break;
2848 case 8:
2849 val = (XSUBR (fun)->function.a8
2850 (internal_args[0], internal_args[1], internal_args[2],
2851 internal_args[3], internal_args[4], internal_args[5],
2852 internal_args[6], internal_args[7]));
2853 break;
2855 default:
2857 /* If a subr takes more than 8 arguments without using MANY
2858 or UNEVALLED, we need to extend this function to support it.
2859 Until this is done, there is no way to call the function. */
2860 emacs_abort ();
2864 else if (COMPILEDP (fun))
2865 val = funcall_lambda (fun, numargs, args + 1);
2866 else
2868 if (NILP (fun))
2869 xsignal1 (Qvoid_function, original_fun);
2870 if (!CONSP (fun))
2871 xsignal1 (Qinvalid_function, original_fun);
2872 funcar = XCAR (fun);
2873 if (!SYMBOLP (funcar))
2874 xsignal1 (Qinvalid_function, original_fun);
2875 if (EQ (funcar, Qlambda)
2876 || EQ (funcar, Qclosure))
2877 val = funcall_lambda (fun, numargs, args + 1);
2878 else if (EQ (funcar, Qautoload))
2880 Fautoload_do_load (fun, original_fun, Qnil);
2881 check_cons_list ();
2882 goto retry;
2884 else
2885 xsignal1 (Qinvalid_function, original_fun);
2887 check_cons_list ();
2888 lisp_eval_depth--;
2889 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2890 val = call_debugger (list2 (Qexit, val));
2891 specpdl_ptr--;
2892 return val;
2895 static Lisp_Object
2896 apply_lambda (Lisp_Object fun, Lisp_Object args)
2898 Lisp_Object args_left;
2899 ptrdiff_t i;
2900 EMACS_INT numargs;
2901 register Lisp_Object *arg_vector;
2902 struct gcpro gcpro1, gcpro2, gcpro3;
2903 register Lisp_Object tem;
2904 USE_SAFE_ALLOCA;
2906 numargs = XFASTINT (Flength (args));
2907 SAFE_ALLOCA_LISP (arg_vector, numargs);
2908 args_left = args;
2910 GCPRO3 (*arg_vector, args_left, fun);
2911 gcpro1.nvars = 0;
2913 for (i = 0; i < numargs; )
2915 tem = Fcar (args_left), args_left = Fcdr (args_left);
2916 tem = eval_sub (tem);
2917 arg_vector[i++] = tem;
2918 gcpro1.nvars = i;
2921 UNGCPRO;
2923 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2924 set_backtrace_nargs (specpdl_ptr - 1, i);
2925 tem = funcall_lambda (fun, numargs, arg_vector);
2927 /* Do the debug-on-exit now, while arg_vector still exists. */
2928 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2930 /* Don't do it again when we return to eval. */
2931 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2932 tem = call_debugger (list2 (Qexit, tem));
2934 SAFE_FREE ();
2935 return tem;
2938 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2939 and return the result of evaluation.
2940 FUN must be either a lambda-expression or a compiled-code object. */
2942 static Lisp_Object
2943 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2944 register Lisp_Object *arg_vector)
2946 Lisp_Object val, syms_left, next, lexenv;
2947 ptrdiff_t count = SPECPDL_INDEX ();
2948 ptrdiff_t i;
2949 bool optional, rest;
2951 if (CONSP (fun))
2953 if (EQ (XCAR (fun), Qclosure))
2955 fun = XCDR (fun); /* Drop `closure'. */
2956 lexenv = XCAR (fun);
2957 CHECK_LIST_CONS (fun, fun);
2959 else
2960 lexenv = Qnil;
2961 syms_left = XCDR (fun);
2962 if (CONSP (syms_left))
2963 syms_left = XCAR (syms_left);
2964 else
2965 xsignal1 (Qinvalid_function, fun);
2967 else if (COMPILEDP (fun))
2969 syms_left = AREF (fun, COMPILED_ARGLIST);
2970 if (INTEGERP (syms_left))
2971 /* A byte-code object with a non-nil `push args' slot means we
2972 shouldn't bind any arguments, instead just call the byte-code
2973 interpreter directly; it will push arguments as necessary.
2975 Byte-code objects with either a non-existent, or a nil value for
2976 the `push args' slot (the default), have dynamically-bound
2977 arguments, and use the argument-binding code below instead (as do
2978 all interpreted functions, even lexically bound ones). */
2980 /* If we have not actually read the bytecode string
2981 and constants vector yet, fetch them from the file. */
2982 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2983 Ffetch_bytecode (fun);
2984 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2985 AREF (fun, COMPILED_CONSTANTS),
2986 AREF (fun, COMPILED_STACK_DEPTH),
2987 syms_left,
2988 nargs, arg_vector);
2990 lexenv = Qnil;
2992 else
2993 emacs_abort ();
2995 i = optional = rest = 0;
2996 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2998 QUIT;
3000 next = XCAR (syms_left);
3001 if (!SYMBOLP (next))
3002 xsignal1 (Qinvalid_function, fun);
3004 if (EQ (next, Qand_rest))
3005 rest = 1;
3006 else if (EQ (next, Qand_optional))
3007 optional = 1;
3008 else
3010 Lisp_Object arg;
3011 if (rest)
3013 arg = Flist (nargs - i, &arg_vector[i]);
3014 i = nargs;
3016 else if (i < nargs)
3017 arg = arg_vector[i++];
3018 else if (!optional)
3019 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3020 else
3021 arg = Qnil;
3023 /* Bind the argument. */
3024 if (!NILP (lexenv) && SYMBOLP (next))
3025 /* Lexically bind NEXT by adding it to the lexenv alist. */
3026 lexenv = Fcons (Fcons (next, arg), lexenv);
3027 else
3028 /* Dynamically bind NEXT. */
3029 specbind (next, arg);
3033 if (!NILP (syms_left))
3034 xsignal1 (Qinvalid_function, fun);
3035 else if (i < nargs)
3036 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3038 if (!EQ (lexenv, Vinternal_interpreter_environment))
3039 /* Instantiate a new lexical environment. */
3040 specbind (Qinternal_interpreter_environment, lexenv);
3042 if (CONSP (fun))
3043 val = Fprogn (XCDR (XCDR (fun)));
3044 else
3046 /* If we have not actually read the bytecode string
3047 and constants vector yet, fetch them from the file. */
3048 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3049 Ffetch_bytecode (fun);
3050 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3051 AREF (fun, COMPILED_CONSTANTS),
3052 AREF (fun, COMPILED_STACK_DEPTH),
3053 Qnil, 0, 0);
3056 return unbind_to (count, val);
3059 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3060 1, 1, 0,
3061 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3062 (Lisp_Object object)
3064 Lisp_Object tem;
3066 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3068 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3069 if (!CONSP (tem))
3071 tem = AREF (object, COMPILED_BYTECODE);
3072 if (CONSP (tem) && STRINGP (XCAR (tem)))
3073 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3074 else
3075 error ("Invalid byte code");
3077 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3078 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3080 return object;
3083 /* Return true if SYMBOL currently has a let-binding
3084 which was made in the buffer that is now current. */
3086 bool
3087 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3089 union specbinding *p;
3090 Lisp_Object buf = Fcurrent_buffer ();
3092 for (p = specpdl_ptr; p > specpdl; )
3093 if ((--p)->kind > SPECPDL_LET)
3095 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3096 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3097 if (symbol == let_bound_symbol
3098 && EQ (specpdl_where (p), buf))
3099 return 1;
3102 return 0;
3105 bool
3106 let_shadows_global_binding_p (Lisp_Object symbol)
3108 union specbinding *p;
3110 for (p = specpdl_ptr; p > specpdl; )
3111 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3112 return 1;
3114 return 0;
3117 /* `specpdl_ptr->symbol' is a field which describes which variable is
3118 let-bound, so it can be properly undone when we unbind_to.
3119 It can have the following two shapes:
3120 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3121 a symbol that is not buffer-local (at least at the time
3122 the let binding started). Note also that it should not be
3123 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3124 to record V2 here).
3125 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3126 variable SYMBOL which can be buffer-local. WHERE tells us
3127 which buffer is affected (or nil if the let-binding affects the
3128 global value of the variable) and BUFFER tells us which buffer was
3129 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3130 BUFFER did not yet have a buffer-local value). */
3132 void
3133 specbind (Lisp_Object symbol, Lisp_Object value)
3135 struct Lisp_Symbol *sym;
3137 CHECK_SYMBOL (symbol);
3138 sym = XSYMBOL (symbol);
3140 start:
3141 switch (sym->redirect)
3143 case SYMBOL_VARALIAS:
3144 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3145 case SYMBOL_PLAINVAL:
3146 /* The most common case is that of a non-constant symbol with a
3147 trivial value. Make that as fast as we can. */
3148 specpdl_ptr->let.kind = SPECPDL_LET;
3149 specpdl_ptr->let.symbol = symbol;
3150 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3151 grow_specpdl ();
3152 if (!sym->constant)
3153 SET_SYMBOL_VAL (sym, value);
3154 else
3155 set_internal (symbol, value, Qnil, 1);
3156 break;
3157 case SYMBOL_LOCALIZED:
3158 if (SYMBOL_BLV (sym)->frame_local)
3159 error ("Frame-local vars cannot be let-bound");
3160 case SYMBOL_FORWARDED:
3162 Lisp_Object ovalue = find_symbol_value (symbol);
3163 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3164 specpdl_ptr->let.symbol = symbol;
3165 specpdl_ptr->let.old_value = ovalue;
3166 specpdl_ptr->let.where = Fcurrent_buffer ();
3168 eassert (sym->redirect != SYMBOL_LOCALIZED
3169 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3171 if (sym->redirect == SYMBOL_LOCALIZED)
3173 if (!blv_found (SYMBOL_BLV (sym)))
3174 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3176 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3178 /* If SYMBOL is a per-buffer variable which doesn't have a
3179 buffer-local value here, make the `let' change the global
3180 value by changing the value of SYMBOL in all buffers not
3181 having their own value. This is consistent with what
3182 happens with other buffer-local variables. */
3183 if (NILP (Flocal_variable_p (symbol, Qnil)))
3185 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3186 grow_specpdl ();
3187 Fset_default (symbol, value);
3188 return;
3191 else
3192 specpdl_ptr->let.kind = SPECPDL_LET;
3194 grow_specpdl ();
3195 set_internal (symbol, value, Qnil, 1);
3196 break;
3198 default: emacs_abort ();
3202 /* Push unwind-protect entries of various types. */
3204 void
3205 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3207 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3208 specpdl_ptr->unwind.func = function;
3209 specpdl_ptr->unwind.arg = arg;
3210 grow_specpdl ();
3213 void
3214 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3216 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3217 specpdl_ptr->unwind_ptr.func = function;
3218 specpdl_ptr->unwind_ptr.arg = arg;
3219 grow_specpdl ();
3222 void
3223 record_unwind_protect_int (void (*function) (int), int arg)
3225 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3226 specpdl_ptr->unwind_int.func = function;
3227 specpdl_ptr->unwind_int.arg = arg;
3228 grow_specpdl ();
3231 void
3232 record_unwind_protect_void (void (*function) (void))
3234 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3235 specpdl_ptr->unwind_void.func = function;
3236 grow_specpdl ();
3239 static void
3240 do_nothing (void)
3243 /* Push an unwind-protect entry that does nothing, so that
3244 set_unwind_protect_ptr can overwrite it later. */
3246 void
3247 record_unwind_protect_nothing (void)
3249 record_unwind_protect_void (do_nothing);
3252 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3253 It need not be at the top of the stack. */
3255 void
3256 clear_unwind_protect (ptrdiff_t count)
3258 union specbinding *p = specpdl + count;
3259 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3260 p->unwind_void.func = do_nothing;
3263 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3264 It need not be at the top of the stack. Discard the entry's
3265 previous value without invoking it. */
3267 void
3268 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3270 union specbinding *p = specpdl + count;
3271 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3272 p->unwind_ptr.func = func;
3273 p->unwind_ptr.arg = arg;
3276 /* Pop and execute entries from the unwind-protect stack until the
3277 depth COUNT is reached. Return VALUE. */
3279 Lisp_Object
3280 unbind_to (ptrdiff_t count, Lisp_Object value)
3282 Lisp_Object quitf = Vquit_flag;
3283 struct gcpro gcpro1, gcpro2;
3285 GCPRO2 (value, quitf);
3286 Vquit_flag = Qnil;
3288 while (specpdl_ptr != specpdl + count)
3290 /* Decrement specpdl_ptr before we do the work to unbind it, so
3291 that an error in unbinding won't try to unbind the same entry
3292 again. Take care to copy any parts of the binding needed
3293 before invoking any code that can make more bindings. */
3295 specpdl_ptr--;
3297 switch (specpdl_ptr->kind)
3299 case SPECPDL_UNWIND:
3300 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3301 break;
3302 case SPECPDL_UNWIND_PTR:
3303 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3304 break;
3305 case SPECPDL_UNWIND_INT:
3306 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3307 break;
3308 case SPECPDL_UNWIND_VOID:
3309 specpdl_ptr->unwind_void.func ();
3310 break;
3311 case SPECPDL_BACKTRACE:
3312 break;
3313 case SPECPDL_LET:
3314 /* If variable has a trivial value (no forwarding), we can
3315 just set it. No need to check for constant symbols here,
3316 since that was already done by specbind. */
3317 if (XSYMBOL (specpdl_symbol (specpdl_ptr))->redirect
3318 == SYMBOL_PLAINVAL)
3319 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (specpdl_ptr)),
3320 specpdl_old_value (specpdl_ptr));
3321 else
3322 /* NOTE: we only ever come here if make_local_foo was used for
3323 the first time on this var within this let. */
3324 Fset_default (specpdl_symbol (specpdl_ptr),
3325 specpdl_old_value (specpdl_ptr));
3326 break;
3327 case SPECPDL_LET_DEFAULT:
3328 Fset_default (specpdl_symbol (specpdl_ptr),
3329 specpdl_old_value (specpdl_ptr));
3330 break;
3331 case SPECPDL_LET_LOCAL:
3333 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3334 Lisp_Object where = specpdl_where (specpdl_ptr);
3335 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3336 eassert (BUFFERP (where));
3338 /* If this was a local binding, reset the value in the appropriate
3339 buffer, but only if that buffer's binding still exists. */
3340 if (!NILP (Flocal_variable_p (symbol, where)))
3341 set_internal (symbol, old_value, where, 1);
3343 break;
3347 if (NILP (Vquit_flag) && !NILP (quitf))
3348 Vquit_flag = quitf;
3350 UNGCPRO;
3351 return value;
3354 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3355 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3356 A special variable is one that will be bound dynamically, even in a
3357 context where binding is lexical by default. */)
3358 (Lisp_Object symbol)
3360 CHECK_SYMBOL (symbol);
3361 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3365 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3366 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3367 The debugger is entered when that frame exits, if the flag is non-nil. */)
3368 (Lisp_Object level, Lisp_Object flag)
3370 union specbinding *pdl = backtrace_top ();
3371 register EMACS_INT i;
3373 CHECK_NUMBER (level);
3375 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3376 pdl = backtrace_next (pdl);
3378 if (backtrace_p (pdl))
3379 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3381 return flag;
3384 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3385 doc: /* Print a trace of Lisp function calls currently active.
3386 Output stream used is value of `standard-output'. */)
3387 (void)
3389 union specbinding *pdl = backtrace_top ();
3390 Lisp_Object tem;
3391 Lisp_Object old_print_level = Vprint_level;
3393 if (NILP (Vprint_level))
3394 XSETFASTINT (Vprint_level, 8);
3396 while (backtrace_p (pdl))
3398 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3399 if (backtrace_nargs (pdl) == UNEVALLED)
3401 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3402 Qnil);
3403 write_string ("\n", -1);
3405 else
3407 tem = backtrace_function (pdl);
3408 Fprin1 (tem, Qnil); /* This can QUIT. */
3409 write_string ("(", -1);
3411 ptrdiff_t i;
3412 for (i = 0; i < backtrace_nargs (pdl); i++)
3414 if (i) write_string (" ", -1);
3415 Fprin1 (backtrace_args (pdl)[i], Qnil);
3418 write_string (")\n", -1);
3420 pdl = backtrace_next (pdl);
3423 Vprint_level = old_print_level;
3424 return Qnil;
3427 static union specbinding *
3428 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3430 union specbinding *pdl = backtrace_top ();
3431 register EMACS_INT i;
3433 CHECK_NATNUM (nframes);
3435 if (!NILP (base))
3436 { /* Skip up to `base'. */
3437 base = Findirect_function (base, Qt);
3438 while (backtrace_p (pdl)
3439 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3440 pdl = backtrace_next (pdl);
3443 /* Find the frame requested. */
3444 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3445 pdl = backtrace_next (pdl);
3447 return pdl;
3450 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3451 doc: /* Return the function and arguments NFRAMES up from current execution point.
3452 If that frame has not evaluated the arguments yet (or is a special form),
3453 the value is (nil FUNCTION ARG-FORMS...).
3454 If that frame has evaluated its arguments and called its function already,
3455 the value is (t FUNCTION ARG-VALUES...).
3456 A &rest arg is represented as the tail of the list ARG-VALUES.
3457 FUNCTION is whatever was supplied as car of evaluated list,
3458 or a lambda expression for macro calls.
3459 If NFRAMES is more than the number of frames, the value is nil.
3460 If BASE is non-nil, it should be a function and NFRAMES counts from its
3461 nearest activation frame. */)
3462 (Lisp_Object nframes, Lisp_Object base)
3464 union specbinding *pdl = get_backtrace_frame (nframes, base);
3466 if (!backtrace_p (pdl))
3467 return Qnil;
3468 if (backtrace_nargs (pdl) == UNEVALLED)
3469 return Fcons (Qnil,
3470 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3471 else
3473 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3475 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3479 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3480 the specpdl stack, and then rewind them. We store the pre-unwind values
3481 directly in the pre-existing specpdl elements (i.e. we swap the current
3482 value and the old value stored in the specpdl), kind of like the inplace
3483 pointer-reversal trick. As it turns out, the rewind does the same as the
3484 unwind, except it starts from the other end of the specpdl stack, so we use
3485 the same function for both unwind and rewind. */
3486 static void
3487 backtrace_eval_unrewind (int distance)
3489 union specbinding *tmp = specpdl_ptr;
3490 int step = -1;
3491 if (distance < 0)
3492 { /* It's a rewind rather than unwind. */
3493 tmp += distance - 1;
3494 step = 1;
3495 distance = -distance;
3498 for (; distance > 0; distance--)
3500 tmp += step;
3501 /* */
3502 switch (tmp->kind)
3504 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3505 unwind_protect, but the problem is that we don't know how to
3506 rewind them afterwards. */
3507 case SPECPDL_UNWIND:
3508 case SPECPDL_UNWIND_PTR:
3509 case SPECPDL_UNWIND_INT:
3510 case SPECPDL_UNWIND_VOID:
3511 case SPECPDL_BACKTRACE:
3512 break;
3513 case SPECPDL_LET:
3514 /* If variable has a trivial value (no forwarding), we can
3515 just set it. No need to check for constant symbols here,
3516 since that was already done by specbind. */
3517 if (XSYMBOL (specpdl_symbol (tmp))->redirect
3518 == SYMBOL_PLAINVAL)
3520 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
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
3528 /* FALLTHROUGH!
3529 NOTE: we only ever come here if make_local_foo was used for
3530 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 void
3584 mark_specpdl (void)
3586 union specbinding *pdl;
3587 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3589 switch (pdl->kind)
3591 case SPECPDL_UNWIND:
3592 mark_object (specpdl_arg (pdl));
3593 break;
3595 case SPECPDL_BACKTRACE:
3597 ptrdiff_t nargs = backtrace_nargs (pdl);
3598 mark_object (backtrace_function (pdl));
3599 if (nargs == UNEVALLED)
3600 nargs = 1;
3601 while (nargs--)
3602 mark_object (backtrace_args (pdl)[nargs]);
3604 break;
3606 case SPECPDL_LET_DEFAULT:
3607 case SPECPDL_LET_LOCAL:
3608 mark_object (specpdl_where (pdl));
3609 /* Fall through. */
3610 case SPECPDL_LET:
3611 mark_object (specpdl_symbol (pdl));
3612 mark_object (specpdl_old_value (pdl));
3613 break;
3618 void
3619 get_backtrace (Lisp_Object array)
3621 union specbinding *pdl = backtrace_next (backtrace_top ());
3622 ptrdiff_t i = 0, asize = ASIZE (array);
3624 /* Copy the backtrace contents into working memory. */
3625 for (; i < asize; i++)
3627 if (backtrace_p (pdl))
3629 ASET (array, i, backtrace_function (pdl));
3630 pdl = backtrace_next (pdl);
3632 else
3633 ASET (array, i, Qnil);
3637 Lisp_Object backtrace_top_function (void)
3639 union specbinding *pdl = backtrace_top ();
3640 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3643 void
3644 syms_of_eval (void)
3646 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3647 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3648 If Lisp code tries to increase the total number past this amount,
3649 an error is signaled.
3650 You can safely use a value considerably larger than the default value,
3651 if that proves inconveniently small. However, if you increase it too far,
3652 Emacs could run out of memory trying to make the stack bigger. */);
3654 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3655 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3657 This limit serves to catch infinite recursions for you before they cause
3658 actual stack overflow in C, which would be fatal for Emacs.
3659 You can safely make it considerably larger than its default value,
3660 if that proves inconveniently small. However, if you increase it too far,
3661 Emacs could overflow the real C stack, and crash. */);
3663 DEFVAR_LISP ("quit-flag", Vquit_flag,
3664 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3665 If the value is t, that means do an ordinary quit.
3666 If the value equals `throw-on-input', that means quit by throwing
3667 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3668 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3669 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3670 Vquit_flag = Qnil;
3672 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3673 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3674 Note that `quit-flag' will still be set by typing C-g,
3675 so a quit will be signaled as soon as `inhibit-quit' is nil.
3676 To prevent this happening, set `quit-flag' to nil
3677 before making `inhibit-quit' nil. */);
3678 Vinhibit_quit = Qnil;
3680 DEFSYM (Qinhibit_quit, "inhibit-quit");
3681 DEFSYM (Qautoload, "autoload");
3682 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3683 DEFSYM (Qmacro, "macro");
3684 DEFSYM (Qdeclare, "declare");
3686 /* Note that the process handling also uses Qexit, but we don't want
3687 to staticpro it twice, so we just do it here. */
3688 DEFSYM (Qexit, "exit");
3690 DEFSYM (Qinteractive, "interactive");
3691 DEFSYM (Qcommandp, "commandp");
3692 DEFSYM (Qand_rest, "&rest");
3693 DEFSYM (Qand_optional, "&optional");
3694 DEFSYM (Qclosure, "closure");
3695 DEFSYM (Qdebug, "debug");
3697 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3698 doc: /* Non-nil means never enter the debugger.
3699 Normally set while the debugger is already active, to avoid recursive
3700 invocations. */);
3701 Vinhibit_debugger = Qnil;
3703 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3704 doc: /* Non-nil means enter debugger if an error is signaled.
3705 Does not apply to errors handled by `condition-case' or those
3706 matched by `debug-ignored-errors'.
3707 If the value is a list, an error only means to enter the debugger
3708 if one of its condition symbols appears in the list.
3709 When you evaluate an expression interactively, this variable
3710 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3711 The command `toggle-debug-on-error' toggles this.
3712 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3713 Vdebug_on_error = Qnil;
3715 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3716 doc: /* List of errors for which the debugger should not be called.
3717 Each element may be a condition-name or a regexp that matches error messages.
3718 If any element applies to a given error, that error skips the debugger
3719 and just returns to top level.
3720 This overrides the variable `debug-on-error'.
3721 It does not apply to errors handled by `condition-case'. */);
3722 Vdebug_ignored_errors = Qnil;
3724 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3725 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3726 Does not apply if quit is handled by a `condition-case'. */);
3727 debug_on_quit = 0;
3729 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3730 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3732 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3733 doc: /* Non-nil means debugger may continue execution.
3734 This is nil when the debugger is called under circumstances where it
3735 might not be safe to continue. */);
3736 debugger_may_continue = 1;
3738 DEFVAR_LISP ("debugger", Vdebugger,
3739 doc: /* Function to call to invoke debugger.
3740 If due to frame exit, args are `exit' and the value being returned;
3741 this function's value will be returned instead of that.
3742 If due to error, args are `error' and a list of the args to `signal'.
3743 If due to `apply' or `funcall' entry, one arg, `lambda'.
3744 If due to `eval' entry, one arg, t. */);
3745 Vdebugger = Qnil;
3747 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3748 doc: /* If non-nil, this is a function for `signal' to call.
3749 It receives the same arguments that `signal' was given.
3750 The Edebug package uses this to regain control. */);
3751 Vsignal_hook_function = Qnil;
3753 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3754 doc: /* Non-nil means call the debugger regardless of condition handlers.
3755 Note that `debug-on-error', `debug-on-quit' and friends
3756 still determine whether to handle the particular condition. */);
3757 Vdebug_on_signal = Qnil;
3759 /* When lexical binding is being used,
3760 Vinternal_interpreter_environment is non-nil, and contains an alist
3761 of lexically-bound variable, or (t), indicating an empty
3762 environment. The lisp name of this variable would be
3763 `internal-interpreter-environment' if it weren't hidden.
3764 Every element of this list can be either a cons (VAR . VAL)
3765 specifying a lexical binding, or a single symbol VAR indicating
3766 that this variable should use dynamic scoping. */
3767 DEFSYM (Qinternal_interpreter_environment,
3768 "internal-interpreter-environment");
3769 DEFVAR_LISP ("internal-interpreter-environment",
3770 Vinternal_interpreter_environment,
3771 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3772 When lexical binding is not being used, this variable is nil.
3773 A value of `(t)' indicates an empty environment, otherwise it is an
3774 alist of active lexical bindings. */);
3775 Vinternal_interpreter_environment = Qnil;
3776 /* Don't export this variable to Elisp, so no one can mess with it
3777 (Just imagine if someone makes it buffer-local). */
3778 Funintern (Qinternal_interpreter_environment, Qnil);
3780 DEFSYM (Vrun_hooks, "run-hooks");
3782 staticpro (&Vautoload_queue);
3783 Vautoload_queue = Qnil;
3784 staticpro (&Vsignaling_function);
3785 Vsignaling_function = Qnil;
3787 inhibit_lisp_code = Qnil;
3789 defsubr (&Sor);
3790 defsubr (&Sand);
3791 defsubr (&Sif);
3792 defsubr (&Scond);
3793 defsubr (&Sprogn);
3794 defsubr (&Sprog1);
3795 defsubr (&Sprog2);
3796 defsubr (&Ssetq);
3797 defsubr (&Squote);
3798 defsubr (&Sfunction);
3799 defsubr (&Sdefvar);
3800 defsubr (&Sdefvaralias);
3801 defsubr (&Sdefconst);
3802 defsubr (&Smake_var_non_special);
3803 defsubr (&Slet);
3804 defsubr (&SletX);
3805 defsubr (&Swhile);
3806 defsubr (&Smacroexpand);
3807 defsubr (&Scatch);
3808 defsubr (&Sthrow);
3809 defsubr (&Sunwind_protect);
3810 defsubr (&Scondition_case);
3811 defsubr (&Ssignal);
3812 defsubr (&Scommandp);
3813 defsubr (&Sautoload);
3814 defsubr (&Sautoload_do_load);
3815 defsubr (&Seval);
3816 defsubr (&Sapply);
3817 defsubr (&Sfuncall);
3818 defsubr (&Srun_hooks);
3819 defsubr (&Srun_hook_with_args);
3820 defsubr (&Srun_hook_with_args_until_success);
3821 defsubr (&Srun_hook_with_args_until_failure);
3822 defsubr (&Srun_hook_wrapped);
3823 defsubr (&Sfetch_bytecode);
3824 defsubr (&Sbacktrace_debug);
3825 defsubr (&Sbacktrace);
3826 defsubr (&Sbacktrace_frame);
3827 defsubr (&Sbacktrace_eval);
3828 defsubr (&Sspecial_variable_p);
3829 defsubr (&Sfunctionp);