1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
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/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
35 struct backtrace
*backtrace_list
;
40 struct catchtag
*catchlist
;
42 /* Chain of condition handlers currently in effect.
43 The elements of this chain are contained in the stack frames
44 of Fcondition_case and internal_condition_case.
45 When an error is signaled (by calling Fsignal, below),
46 this chain is searched for an element that applies. */
51 struct handler
*handlerlist
;
54 /* Count levels of GCPRO to detect failure to UNGCPRO. */
58 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
59 Lisp_Object Qinhibit_quit
;
60 Lisp_Object Qand_rest
;
61 static Lisp_Object Qand_optional
;
62 static Lisp_Object Qinhibit_debugger
;
63 static Lisp_Object Qdeclare
;
64 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
66 static Lisp_Object Qdebug
;
68 /* This holds either the symbol `run-hooks' or nil.
69 It is nil at an early stage of startup, and when Emacs
72 Lisp_Object Vrun_hooks
;
74 /* Non-nil means record all fset's and provide's, to be undone
75 if the file being autoloaded is not fully loaded.
76 They are recorded by being consed onto the front of Vautoload_queue:
77 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
79 Lisp_Object Vautoload_queue
;
81 /* Current number of specbindings allocated in specpdl. */
83 ptrdiff_t specpdl_size
;
85 /* Pointer to beginning of specpdl. */
87 struct specbinding
*specpdl
;
89 /* Pointer to first unused element in specpdl. */
91 struct 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
104 static EMACS_INT when_entered_debugger
;
106 /* The function from which the last `signal' was called. Set in
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 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
118 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
120 /* Functions to set Lisp_Object slots of struct specbinding. */
123 set_specpdl_symbol (Lisp_Object symbol
)
125 specpdl_ptr
->symbol
= symbol
;
129 set_specpdl_old_value (Lisp_Object oldval
)
131 specpdl_ptr
->old_value
= oldval
;
135 init_eval_once (void)
138 specpdl
= xmalloc (size
* sizeof *specpdl
);
140 specpdl_ptr
= specpdl
;
141 /* Don't forget to update docs (lispref node "Local Variables"). */
142 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
143 max_lisp_eval_depth
= 600;
151 specpdl_ptr
= specpdl
;
156 debug_on_next_call
= 0;
161 /* This is less than the initial value of num_nonmacro_input_events. */
162 when_entered_debugger
= -1;
165 /* Unwind-protect function used by call_debugger. */
168 restore_stack_limits (Lisp_Object data
)
170 max_specpdl_size
= XINT (XCAR (data
));
171 max_lisp_eval_depth
= XINT (XCDR (data
));
175 /* Call the Lisp debugger, giving it argument ARG. */
178 call_debugger (Lisp_Object arg
)
180 bool debug_while_redisplaying
;
181 ptrdiff_t count
= SPECPDL_INDEX ();
183 EMACS_INT old_max
= max_specpdl_size
;
185 /* Temporarily bump up the stack limits,
186 so the debugger won't run out of stack. */
188 max_specpdl_size
+= 1;
189 record_unwind_protect (restore_stack_limits
,
190 Fcons (make_number (old_max
),
191 make_number (max_lisp_eval_depth
)));
192 max_specpdl_size
= old_max
;
194 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
195 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
197 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
198 max_specpdl_size
= SPECPDL_INDEX () + 100;
200 #ifdef HAVE_WINDOW_SYSTEM
201 if (display_hourglass_p
)
205 debug_on_next_call
= 0;
206 when_entered_debugger
= num_nonmacro_input_events
;
208 /* Resetting redisplaying_p to 0 makes sure that debug output is
209 displayed if the debugger is invoked during redisplay. */
210 debug_while_redisplaying
= redisplaying_p
;
212 specbind (intern ("debugger-may-continue"),
213 debug_while_redisplaying
? Qnil
: Qt
);
214 specbind (Qinhibit_redisplay
, Qnil
);
215 specbind (Qinhibit_debugger
, Qt
);
217 #if 0 /* Binding this prevents execution of Lisp code during
218 redisplay, which necessarily leads to display problems. */
219 specbind (Qinhibit_eval_during_redisplay
, Qt
);
222 val
= apply1 (Vdebugger
, arg
);
224 /* Interrupting redisplay and resuming it later is not safe under
225 all circumstances. So, when the debugger returns, abort the
226 interrupted redisplay by going back to the top-level. */
227 if (debug_while_redisplaying
)
230 return unbind_to (count
, val
);
234 do_debug_on_call (Lisp_Object code
)
236 debug_on_next_call
= 0;
237 backtrace_list
->debug_on_exit
= 1;
238 call_debugger (Fcons (code
, Qnil
));
241 /* NOTE!!! Every function that can call EVAL must protect its args
242 and temporaries from garbage collection while it needs them.
243 The definition of `For' shows what you have to do. */
245 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
246 doc
: /* Eval args until one of them yields non-nil, then return that value.
247 The remaining args are not evalled at all.
248 If all args return nil, return nil.
249 usage: (or CONDITIONS...) */)
252 register Lisp_Object val
= Qnil
;
259 val
= eval_sub (XCAR (args
));
269 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
270 doc
: /* Eval args until one of them yields nil, then return nil.
271 The remaining args are not evalled at all.
272 If no arg yields nil, return the last arg's value.
273 usage: (and CONDITIONS...) */)
276 register Lisp_Object val
= Qt
;
283 val
= eval_sub (XCAR (args
));
293 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
294 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
295 Returns the value of THEN or the value of the last of the ELSE's.
296 THEN must be one expression, but ELSE... can be zero or more expressions.
297 If COND yields nil, and there are no ELSE's, the value is nil.
298 usage: (if COND THEN ELSE...) */)
301 register Lisp_Object cond
;
305 cond
= eval_sub (Fcar (args
));
309 return eval_sub (Fcar (Fcdr (args
)));
310 return Fprogn (Fcdr (Fcdr (args
)));
313 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
314 doc
: /* Try each clause until one succeeds.
315 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
316 and, if the value is non-nil, this clause succeeds:
317 then the expressions in BODY are evaluated and the last one's
318 value is the value of the cond-form.
319 If no clause succeeds, cond returns nil.
320 If a clause has one element, as in (CONDITION),
321 CONDITION's value if non-nil is returned from the cond-form.
322 usage: (cond CLAUSES...) */)
325 register Lisp_Object clause
, val
;
332 clause
= Fcar (args
);
333 val
= eval_sub (Fcar (clause
));
336 if (!EQ (XCDR (clause
), Qnil
))
337 val
= Fprogn (XCDR (clause
));
347 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
348 doc
: /* Eval BODY forms sequentially and return value of last one.
349 usage: (progn BODY...) */)
352 register Lisp_Object val
= Qnil
;
359 val
= eval_sub (XCAR (args
));
367 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
368 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
369 The value of FIRST is saved during the evaluation of the remaining args,
370 whose values are discarded.
371 usage: (prog1 FIRST BODY...) */)
375 register Lisp_Object args_left
;
376 struct gcpro gcpro1
, gcpro2
;
382 val
= eval_sub (XCAR (args_left
));
383 while (CONSP (args_left
= XCDR (args_left
)))
384 eval_sub (XCAR (args_left
));
390 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
391 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
392 The value of FORM2 is saved during the evaluation of the
393 remaining args, whose values are discarded.
394 usage: (prog2 FORM1 FORM2 BODY...) */)
400 eval_sub (XCAR (args
));
402 return Fprog1 (XCDR (args
));
405 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
406 doc
: /* Set each SYM to the value of its VAL.
407 The symbols SYM are variables; they are literal (not evaluated).
408 The values VAL are expressions; they are evaluated.
409 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
410 The second VAL is not computed until after the first SYM is set, and so on;
411 each VAL can use the new value of variables set earlier in the `setq'.
412 The return value of the `setq' form is the value of the last VAL.
413 usage: (setq [SYM VAL]...) */)
416 register Lisp_Object args_left
;
417 register Lisp_Object val
, sym
, lex_binding
;
428 val
= eval_sub (Fcar (Fcdr (args_left
)));
429 sym
= Fcar (args_left
);
431 /* Like for eval_sub, we do not check declared_special here since
432 it's been done when let-binding. */
433 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
435 && !NILP (lex_binding
436 = Fassq (sym
, Vinternal_interpreter_environment
)))
437 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
439 Fset (sym
, val
); /* SYM is dynamically bound. */
441 args_left
= Fcdr (Fcdr (args_left
));
443 while (!NILP (args_left
));
449 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
450 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
451 Warning: `quote' does not construct its return value, but just returns
452 the value that was pre-constructed by the Lisp reader (see info node
453 `(elisp)Printed Representation').
454 This means that '(a . b) is not identical to (cons 'a 'b): the former
455 does not cons. Quoting should be reserved for constants that will
456 never be modified by side-effects, unless you like self-modifying code.
457 See the common pitfall in info node `(elisp)Rearrangement' for an example
458 of unexpected results when a quoted object is modified.
459 usage: (quote ARG) */)
462 if (!NILP (Fcdr (args
)))
463 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
467 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
468 doc
: /* Like `quote', but preferred for objects which are functions.
469 In byte compilation, `function' causes its argument to be compiled.
470 `quote' cannot do that.
471 usage: (function ARG) */)
474 Lisp_Object quoted
= XCAR (args
);
476 if (!NILP (Fcdr (args
)))
477 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
479 if (!NILP (Vinternal_interpreter_environment
)
481 && EQ (XCAR (quoted
), Qlambda
))
482 /* This is a lambda expression within a lexical environment;
483 return an interpreted closure instead of a simple lambda. */
484 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
487 /* Simply quote the argument. */
492 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
493 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
494 Aliased variables always have the same value; setting one sets the other.
495 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
496 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
497 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
498 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
499 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
500 The return value is BASE-VARIABLE. */)
501 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
503 struct Lisp_Symbol
*sym
;
505 CHECK_SYMBOL (new_alias
);
506 CHECK_SYMBOL (base_variable
);
508 sym
= XSYMBOL (new_alias
);
511 /* Not sure why, but why not? */
512 error ("Cannot make a constant an alias");
514 switch (sym
->redirect
)
516 case SYMBOL_FORWARDED
:
517 error ("Cannot make an internal variable an alias");
518 case SYMBOL_LOCALIZED
:
519 error ("Don't know how to make a localized variable an alias");
522 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
523 If n_a is bound, but b_v is not, set the value of b_v to n_a,
524 so that old-code that affects n_a before the aliasing is setup
526 if (NILP (Fboundp (base_variable
)))
527 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
530 struct specbinding
*p
;
532 for (p
= specpdl_ptr
; p
> specpdl
; )
533 if ((--p
)->func
== NULL
535 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
536 error ("Don't know how to make a let-bound variable an alias");
539 sym
->declared_special
= 1;
540 XSYMBOL (base_variable
)->declared_special
= 1;
541 sym
->redirect
= SYMBOL_VARALIAS
;
542 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
543 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
544 LOADHIST_ATTACH (new_alias
);
545 /* Even if docstring is nil: remove old docstring. */
546 Fput (new_alias
, Qvariable_documentation
, docstring
);
548 return base_variable
;
552 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
553 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
554 You are not required to define a variable in order to use it, but
555 defining it lets you supply an initial value and documentation, which
556 can be referred to by the Emacs help facilities and other programming
557 tools. The `defvar' form also declares the variable as \"special\",
558 so that it is always dynamically bound even if `lexical-binding' is t.
560 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
561 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
562 default value is what is set; buffer-local values are not affected.
563 If INITVALUE is missing, SYMBOL's value is not set.
565 If SYMBOL has a local binding, then this form affects the local
566 binding. This is usually not what you want. Thus, if you need to
567 load a file defining variables, with this form or with `defconst' or
568 `defcustom', you should always load that file _outside_ any bindings
569 for these variables. \(`defconst' and `defcustom' behave similarly in
572 The optional argument DOCSTRING is a documentation string for the
575 To define a user option, use `defcustom' instead of `defvar'.
576 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
579 register Lisp_Object sym
, tem
, tail
;
583 if (!NILP (Fcdr (Fcdr (tail
))))
584 error ("Too many arguments");
586 tem
= Fdefault_boundp (sym
);
589 /* Do it before evaluating the initial value, for self-references. */
590 XSYMBOL (sym
)->declared_special
= 1;
593 Fset_default (sym
, eval_sub (Fcar (tail
)));
595 { /* Check if there is really a global binding rather than just a let
596 binding that shadows the global unboundness of the var. */
597 struct specbinding
*pdl
= specpdl_ptr
;
598 while (pdl
> specpdl
)
600 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
601 && EQ (pdl
->old_value
, Qunbound
))
604 ("Warning: defvar ignored because %s is let-bound",
605 SYMBOL_NAME (sym
), 1);
614 if (!NILP (Vpurify_flag
))
615 tem
= Fpurecopy (tem
);
616 Fput (sym
, Qvariable_documentation
, tem
);
618 LOADHIST_ATTACH (sym
);
620 else if (!NILP (Vinternal_interpreter_environment
)
621 && !XSYMBOL (sym
)->declared_special
)
622 /* A simple (defvar foo) with lexical scoping does "nothing" except
623 declare that var to be dynamically scoped *locally* (i.e. within
624 the current file or let-block). */
625 Vinternal_interpreter_environment
626 = Fcons (sym
, Vinternal_interpreter_environment
);
629 /* Simple (defvar <var>) should not count as a definition at all.
630 It could get in the way of other definitions, and unloading this
631 package could try to make the variable unbound. */
637 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
638 doc
: /* Define SYMBOL as a constant variable.
639 This declares that neither programs nor users should ever change the
640 value. This constancy is not actually enforced by Emacs Lisp, but
641 SYMBOL is marked as a special variable so that it is never lexically
644 The `defconst' form always sets the value of SYMBOL to the result of
645 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
646 what is set; buffer-local values are not affected. If SYMBOL has a
647 local binding, then this form sets the local binding's value.
648 However, you should normally not make local bindings for variables
649 defined with this form.
651 The optional DOCSTRING specifies the variable's documentation string.
652 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
655 register Lisp_Object sym
, tem
;
658 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
659 error ("Too many arguments");
661 tem
= eval_sub (Fcar (Fcdr (args
)));
662 if (!NILP (Vpurify_flag
))
663 tem
= Fpurecopy (tem
);
664 Fset_default (sym
, tem
);
665 XSYMBOL (sym
)->declared_special
= 1;
666 tem
= Fcar (Fcdr (Fcdr (args
)));
669 if (!NILP (Vpurify_flag
))
670 tem
= Fpurecopy (tem
);
671 Fput (sym
, Qvariable_documentation
, tem
);
673 Fput (sym
, Qrisky_local_variable
, Qt
);
674 LOADHIST_ATTACH (sym
);
678 /* Make SYMBOL lexically scoped. */
679 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
680 Smake_var_non_special
, 1, 1, 0,
681 doc
: /* Internal function. */)
684 CHECK_SYMBOL (symbol
);
685 XSYMBOL (symbol
)->declared_special
= 0;
690 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
691 doc
: /* Bind variables according to VARLIST then eval BODY.
692 The value of the last form in BODY is returned.
693 Each element of VARLIST is a symbol (which is bound to nil)
694 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
695 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
696 usage: (let* VARLIST BODY...) */)
699 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
700 ptrdiff_t count
= SPECPDL_INDEX ();
701 struct gcpro gcpro1
, gcpro2
, gcpro3
;
703 GCPRO3 (args
, elt
, varlist
);
705 lexenv
= Vinternal_interpreter_environment
;
707 varlist
= Fcar (args
);
708 while (CONSP (varlist
))
712 elt
= XCAR (varlist
);
718 else if (! NILP (Fcdr (Fcdr (elt
))))
719 signal_error ("`let' bindings can have only one value-form", elt
);
723 val
= eval_sub (Fcar (Fcdr (elt
)));
726 if (!NILP (lexenv
) && SYMBOLP (var
)
727 && !XSYMBOL (var
)->declared_special
728 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
729 /* Lexically bind VAR by adding it to the interpreter's binding
733 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
734 if (EQ (Vinternal_interpreter_environment
, lexenv
))
735 /* Save the old lexical environment on the specpdl stack,
736 but only for the first lexical binding, since we'll never
737 need to revert to one of the intermediate ones. */
738 specbind (Qinternal_interpreter_environment
, newenv
);
740 Vinternal_interpreter_environment
= newenv
;
745 varlist
= XCDR (varlist
);
748 val
= Fprogn (Fcdr (args
));
749 return unbind_to (count
, val
);
752 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
753 doc
: /* Bind variables according to VARLIST then eval BODY.
754 The value of the last form in BODY is returned.
755 Each element of VARLIST is a symbol (which is bound to nil)
756 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
757 All the VALUEFORMs are evalled before any symbols are bound.
758 usage: (let VARLIST BODY...) */)
761 Lisp_Object
*temps
, tem
, lexenv
;
762 register Lisp_Object elt
, varlist
;
763 ptrdiff_t count
= SPECPDL_INDEX ();
765 struct gcpro gcpro1
, gcpro2
;
768 varlist
= Fcar (args
);
770 /* Make space to hold the values to give the bound variables. */
771 elt
= Flength (varlist
);
772 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
774 /* Compute the values and store them in `temps'. */
776 GCPRO2 (args
, *temps
);
779 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
782 elt
= XCAR (varlist
);
784 temps
[argnum
++] = Qnil
;
785 else if (! NILP (Fcdr (Fcdr (elt
))))
786 signal_error ("`let' bindings can have only one value-form", elt
);
788 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
789 gcpro2
.nvars
= argnum
;
793 lexenv
= Vinternal_interpreter_environment
;
795 varlist
= Fcar (args
);
796 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
800 elt
= XCAR (varlist
);
801 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
802 tem
= temps
[argnum
++];
804 if (!NILP (lexenv
) && SYMBOLP (var
)
805 && !XSYMBOL (var
)->declared_special
806 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
807 /* Lexically bind VAR by adding it to the lexenv alist. */
808 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
810 /* Dynamically bind VAR. */
814 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
815 /* Instantiate a new lexical environment. */
816 specbind (Qinternal_interpreter_environment
, lexenv
);
818 elt
= Fprogn (Fcdr (args
));
820 return unbind_to (count
, elt
);
823 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
824 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
825 The order of execution is thus TEST, BODY, TEST, BODY and so on
826 until TEST returns nil.
827 usage: (while TEST BODY...) */)
830 Lisp_Object test
, body
;
831 struct gcpro gcpro1
, gcpro2
;
837 while (!NILP (eval_sub (test
)))
847 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
848 doc
: /* Return result of expanding macros at top level of FORM.
849 If FORM is not a macro call, it is returned unchanged.
850 Otherwise, the macro is expanded and the expansion is considered
851 in place of FORM. When a non-macro-call results, it is returned.
853 The second optional arg ENVIRONMENT specifies an environment of macro
854 definitions to shadow the loaded ones for use in file byte-compilation. */)
855 (Lisp_Object form
, Lisp_Object environment
)
857 /* With cleanups from Hallvard Furuseth. */
858 register Lisp_Object expander
, sym
, def
, tem
;
862 /* Come back here each time we expand a macro call,
863 in case it expands into another macro call. */
866 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
867 def
= sym
= XCAR (form
);
869 /* Trace symbols aliases to other symbols
870 until we get a symbol that is not an alias. */
871 while (SYMBOLP (def
))
875 tem
= Fassq (sym
, environment
);
878 def
= XSYMBOL (sym
)->function
;
884 /* Right now TEM is the result from SYM in ENVIRONMENT,
885 and if TEM is nil then DEF is SYM's function definition. */
888 /* SYM is not mentioned in ENVIRONMENT.
889 Look at its function definition. */
892 def
= Fautoload_do_load (def
, sym
, Qmacro
);
895 /* Not defined or definition not suitable. */
897 if (!EQ (XCAR (def
), Qmacro
))
899 else expander
= XCDR (def
);
903 expander
= XCDR (tem
);
908 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
909 if (EQ (form
, newform
))
918 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
919 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
920 TAG is evalled to get the tag to use; it must not be nil.
922 Then the BODY is executed.
923 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
924 If no throw happens, `catch' returns the value of the last BODY form.
925 If a throw happens, it specifies the value to return from `catch'.
926 usage: (catch TAG BODY...) */)
929 register Lisp_Object tag
;
933 tag
= eval_sub (Fcar (args
));
935 return internal_catch (tag
, Fprogn
, Fcdr (args
));
938 /* Set up a catch, then call C function FUNC on argument ARG.
939 FUNC should return a Lisp_Object.
940 This is how catches are done from within C code. */
943 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
945 /* This structure is made part of the chain `catchlist'. */
948 /* Fill in the components of c, and put it on the list. */
952 c
.backlist
= backtrace_list
;
953 c
.handlerlist
= handlerlist
;
954 c
.lisp_eval_depth
= lisp_eval_depth
;
955 c
.pdlcount
= SPECPDL_INDEX ();
956 c
.poll_suppress_count
= poll_suppress_count
;
957 c
.interrupt_input_blocked
= interrupt_input_blocked
;
959 c
.byte_stack
= byte_stack_list
;
963 if (! sys_setjmp (c
.jmp
))
964 c
.val
= (*func
) (arg
);
966 /* Throw works by a longjmp that comes right here. */
971 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
972 jump to that CATCH, returning VALUE as the value of that catch.
974 This is the guts of Fthrow and Fsignal; they differ only in the way
975 they choose the catch tag to throw to. A catch tag for a
976 condition-case form has a TAG of Qnil.
978 Before each catch is discarded, unbind all special bindings and
979 execute all unwind-protect clauses made above that catch. Unwind
980 the handler stack as we go, so that the proper handlers are in
981 effect for each unwind-protect clause we run. At the end, restore
982 some static info saved in CATCH, and longjmp to the location
985 This is used for correct unwinding in Fthrow and Fsignal. */
987 static _Noreturn
void
988 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
992 /* Save the value in the tag. */
995 /* Restore certain special C variables. */
996 set_poll_suppress_count (catch->poll_suppress_count
);
997 unblock_input_to (catch->interrupt_input_blocked
);
1002 last_time
= catchlist
== catch;
1004 /* Unwind the specpdl stack, and then restore the proper set of
1006 unbind_to (catchlist
->pdlcount
, Qnil
);
1007 handlerlist
= catchlist
->handlerlist
;
1008 catchlist
= catchlist
->next
;
1010 while (! last_time
);
1012 byte_stack_list
= catch->byte_stack
;
1013 gcprolist
= catch->gcpro
;
1015 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1017 backtrace_list
= catch->backlist
;
1018 lisp_eval_depth
= catch->lisp_eval_depth
;
1020 sys_longjmp (catch->jmp
, 1);
1023 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1024 doc
: /* Throw to the catch for TAG and return VALUE from it.
1025 Both TAG and VALUE are evalled. */)
1026 (register Lisp_Object tag
, Lisp_Object value
)
1028 register struct catchtag
*c
;
1031 for (c
= catchlist
; c
; c
= c
->next
)
1033 if (EQ (c
->tag
, tag
))
1034 unwind_to_catch (c
, value
);
1036 xsignal2 (Qno_catch
, tag
, value
);
1040 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1041 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1042 If BODYFORM completes normally, its value is returned
1043 after executing the UNWINDFORMS.
1044 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1045 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1049 ptrdiff_t count
= SPECPDL_INDEX ();
1051 record_unwind_protect (Fprogn
, Fcdr (args
));
1052 val
= eval_sub (Fcar (args
));
1053 return unbind_to (count
, val
);
1056 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1057 doc
: /* Regain control when an error is signaled.
1058 Executes BODYFORM and returns its value if no error happens.
1059 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1060 where the BODY is made of Lisp expressions.
1062 A handler is applicable to an error
1063 if CONDITION-NAME is one of the error's condition names.
1064 If an error happens, the first applicable handler is run.
1066 The car of a handler may be a list of condition names instead of a
1067 single condition name; then it handles all of them. If the special
1068 condition name `debug' is present in this list, it allows another
1069 condition in the list to run the debugger if `debug-on-error' and the
1070 other usual mechanisms says it should (otherwise, `condition-case'
1071 suppresses the debugger).
1073 When a handler handles an error, control returns to the `condition-case'
1074 and it executes the handler's BODY...
1075 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1076 \(If VAR is nil, the handler can't access that information.)
1077 Then the value of the last BODY form is returned from the `condition-case'
1080 See also the function `signal' for more info.
1081 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1084 Lisp_Object var
= Fcar (args
);
1085 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1086 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1088 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1091 /* Like Fcondition_case, but the args are separate
1092 rather than passed in a list. Used by Fbyte_code. */
1095 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1096 Lisp_Object handlers
)
1104 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1110 && (SYMBOLP (XCAR (tem
))
1111 || CONSP (XCAR (tem
))))))
1112 error ("Invalid condition handler: %s",
1113 SDATA (Fprin1_to_string (tem
, Qt
)));
1118 c
.backlist
= backtrace_list
;
1119 c
.handlerlist
= handlerlist
;
1120 c
.lisp_eval_depth
= lisp_eval_depth
;
1121 c
.pdlcount
= SPECPDL_INDEX ();
1122 c
.poll_suppress_count
= poll_suppress_count
;
1123 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1124 c
.gcpro
= gcprolist
;
1125 c
.byte_stack
= byte_stack_list
;
1126 if (sys_setjmp (c
.jmp
))
1129 specbind (h
.var
, c
.val
);
1130 val
= Fprogn (Fcdr (h
.chosen_clause
));
1132 /* Note that this just undoes the binding of h.var; whoever
1133 longjumped to us unwound the stack to c.pdlcount before
1135 unbind_to (c
.pdlcount
, Qnil
);
1142 h
.handler
= handlers
;
1143 h
.next
= handlerlist
;
1147 val
= eval_sub (bodyform
);
1149 handlerlist
= h
.next
;
1153 /* Call the function BFUN with no arguments, catching errors within it
1154 according to HANDLERS. If there is an error, call HFUN with
1155 one argument which is the data that describes the error:
1158 HANDLERS can be a list of conditions to catch.
1159 If HANDLERS is Qt, catch all errors.
1160 If HANDLERS is Qerror, catch all errors
1161 but allow the debugger to run if that is enabled. */
1164 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1165 Lisp_Object (*hfun
) (Lisp_Object
))
1173 c
.backlist
= backtrace_list
;
1174 c
.handlerlist
= handlerlist
;
1175 c
.lisp_eval_depth
= lisp_eval_depth
;
1176 c
.pdlcount
= SPECPDL_INDEX ();
1177 c
.poll_suppress_count
= poll_suppress_count
;
1178 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1179 c
.gcpro
= gcprolist
;
1180 c
.byte_stack
= byte_stack_list
;
1181 if (sys_setjmp (c
.jmp
))
1183 return (*hfun
) (c
.val
);
1187 h
.handler
= handlers
;
1189 h
.next
= handlerlist
;
1195 handlerlist
= h
.next
;
1199 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1202 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1203 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1211 c
.backlist
= backtrace_list
;
1212 c
.handlerlist
= handlerlist
;
1213 c
.lisp_eval_depth
= lisp_eval_depth
;
1214 c
.pdlcount
= SPECPDL_INDEX ();
1215 c
.poll_suppress_count
= poll_suppress_count
;
1216 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1217 c
.gcpro
= gcprolist
;
1218 c
.byte_stack
= byte_stack_list
;
1219 if (sys_setjmp (c
.jmp
))
1221 return (*hfun
) (c
.val
);
1225 h
.handler
= handlers
;
1227 h
.next
= handlerlist
;
1231 val
= (*bfun
) (arg
);
1233 handlerlist
= h
.next
;
1237 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1241 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1244 Lisp_Object handlers
,
1245 Lisp_Object (*hfun
) (Lisp_Object
))
1253 c
.backlist
= backtrace_list
;
1254 c
.handlerlist
= handlerlist
;
1255 c
.lisp_eval_depth
= lisp_eval_depth
;
1256 c
.pdlcount
= SPECPDL_INDEX ();
1257 c
.poll_suppress_count
= poll_suppress_count
;
1258 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1259 c
.gcpro
= gcprolist
;
1260 c
.byte_stack
= byte_stack_list
;
1261 if (sys_setjmp (c
.jmp
))
1263 return (*hfun
) (c
.val
);
1267 h
.handler
= handlers
;
1269 h
.next
= handlerlist
;
1273 val
= (*bfun
) (arg1
, arg2
);
1275 handlerlist
= h
.next
;
1279 /* Like internal_condition_case but call BFUN with NARGS as first,
1280 and ARGS as second argument. */
1283 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1286 Lisp_Object handlers
,
1287 Lisp_Object (*hfun
) (Lisp_Object err
,
1297 c
.backlist
= backtrace_list
;
1298 c
.handlerlist
= handlerlist
;
1299 c
.lisp_eval_depth
= lisp_eval_depth
;
1300 c
.pdlcount
= SPECPDL_INDEX ();
1301 c
.poll_suppress_count
= poll_suppress_count
;
1302 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1303 c
.gcpro
= gcprolist
;
1304 c
.byte_stack
= byte_stack_list
;
1305 if (sys_setjmp (c
.jmp
))
1307 return (*hfun
) (c
.val
, nargs
, args
);
1311 h
.handler
= handlers
;
1313 h
.next
= handlerlist
;
1317 val
= (*bfun
) (nargs
, args
);
1319 handlerlist
= h
.next
;
1324 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1325 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1329 process_quit_flag (void)
1331 Lisp_Object flag
= Vquit_flag
;
1333 if (EQ (flag
, Qkill_emacs
))
1335 if (EQ (Vthrow_on_input
, flag
))
1336 Fthrow (Vthrow_on_input
, Qt
);
1337 Fsignal (Qquit
, Qnil
);
1340 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1341 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1342 This function does not return.
1344 An error symbol is a symbol with an `error-conditions' property
1345 that is a list of condition names.
1346 A handler for any of those names will get to handle this signal.
1347 The symbol `error' should normally be one of them.
1349 DATA should be a list. Its elements are printed as part of the error message.
1350 See Info anchor `(elisp)Definition of signal' for some details on how this
1351 error message is constructed.
1352 If the signal is handled, DATA is made available to the handler.
1353 See also the function `condition-case'. */)
1354 (Lisp_Object error_symbol
, Lisp_Object data
)
1356 /* When memory is full, ERROR-SYMBOL is nil,
1357 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1358 That is a special case--don't do this in other situations. */
1359 Lisp_Object conditions
;
1361 Lisp_Object real_error_symbol
1362 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1363 register Lisp_Object clause
= Qnil
;
1365 struct backtrace
*bp
;
1369 if (gc_in_progress
|| waiting_for_input
)
1372 #if 0 /* rms: I don't know why this was here,
1373 but it is surely wrong for an error that is handled. */
1374 #ifdef HAVE_WINDOW_SYSTEM
1375 if (display_hourglass_p
)
1376 cancel_hourglass ();
1380 /* This hook is used by edebug. */
1381 if (! NILP (Vsignal_hook_function
)
1382 && ! NILP (error_symbol
))
1384 /* Edebug takes care of restoring these variables when it exits. */
1385 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1386 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1388 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1389 max_specpdl_size
= SPECPDL_INDEX () + 40;
1391 call2 (Vsignal_hook_function
, error_symbol
, data
);
1394 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1396 /* Remember from where signal was called. Skip over the frame for
1397 `signal' itself. If a frame for `error' follows, skip that,
1398 too. Don't do this when ERROR_SYMBOL is nil, because that
1399 is a memory-full error. */
1400 Vsignaling_function
= Qnil
;
1401 if (backtrace_list
&& !NILP (error_symbol
))
1403 bp
= backtrace_list
->next
;
1404 if (bp
&& EQ (bp
->function
, Qerror
))
1407 Vsignaling_function
= bp
->function
;
1410 for (h
= handlerlist
; h
; h
= h
->next
)
1412 clause
= find_handler_clause (h
->handler
, conditions
);
1417 if (/* Don't run the debugger for a memory-full error.
1418 (There is no room in memory to do that!) */
1419 !NILP (error_symbol
)
1420 && (!NILP (Vdebug_on_signal
)
1421 /* If no handler is present now, try to run the debugger. */
1423 /* A `debug' symbol in the handler list disables the normal
1424 suppression of the debugger. */
1425 || (CONSP (clause
) && CONSP (XCAR (clause
))
1426 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1427 /* Special handler that means "print a message and run debugger
1429 || EQ (h
->handler
, Qerror
)))
1431 bool debugger_called
1432 = maybe_call_debugger (conditions
, error_symbol
, data
);
1433 /* We can't return values to code which signaled an error, but we
1434 can continue code which has signaled a quit. */
1435 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1441 Lisp_Object unwind_data
1442 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1444 h
->chosen_clause
= clause
;
1445 unwind_to_catch (h
->tag
, unwind_data
);
1450 Fthrow (Qtop_level
, Qt
);
1453 if (! NILP (error_symbol
))
1454 data
= Fcons (error_symbol
, data
);
1456 string
= Ferror_message_string (data
);
1457 fatal ("%s", SDATA (string
));
1460 /* Internal version of Fsignal that never returns.
1461 Used for anything but Qquit (which can return from Fsignal). */
1464 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1466 Fsignal (error_symbol
, data
);
1470 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1473 xsignal0 (Lisp_Object error_symbol
)
1475 xsignal (error_symbol
, Qnil
);
1479 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1481 xsignal (error_symbol
, list1 (arg
));
1485 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1487 xsignal (error_symbol
, list2 (arg1
, arg2
));
1491 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1493 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1496 /* Signal `error' with message S, and additional arg ARG.
1497 If ARG is not a genuine list, make it a one-element list. */
1500 signal_error (const char *s
, Lisp_Object arg
)
1502 Lisp_Object tortoise
, hare
;
1504 hare
= tortoise
= arg
;
1505 while (CONSP (hare
))
1512 tortoise
= XCDR (tortoise
);
1514 if (EQ (hare
, tortoise
))
1519 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1521 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1525 /* Return true if LIST is a non-nil atom or
1526 a list containing one of CONDITIONS. */
1529 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1536 while (CONSP (conditions
))
1538 Lisp_Object
this, tail
;
1539 this = XCAR (conditions
);
1540 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1541 if (EQ (XCAR (tail
), this))
1543 conditions
= XCDR (conditions
);
1548 /* Return true if an error with condition-symbols CONDITIONS,
1549 and described by SIGNAL-DATA, should skip the debugger
1550 according to debugger-ignored-errors. */
1553 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1556 bool first_string
= 1;
1557 Lisp_Object error_message
;
1559 error_message
= Qnil
;
1560 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1562 if (STRINGP (XCAR (tail
)))
1566 error_message
= Ferror_message_string (data
);
1570 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1575 Lisp_Object contail
;
1577 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1578 if (EQ (XCAR (tail
), XCAR (contail
)))
1586 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1587 SIG and DATA describe the signal. There are two ways to pass them:
1588 = SIG is the error symbol, and DATA is the rest of the data.
1589 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1590 This is for memory-full errors only. */
1592 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1594 Lisp_Object combined_data
;
1596 combined_data
= Fcons (sig
, data
);
1599 /* Don't try to run the debugger with interrupts blocked.
1600 The editing loop would return anyway. */
1601 ! input_blocked_p ()
1602 && NILP (Vinhibit_debugger
)
1603 /* Does user want to enter debugger for this kind of error? */
1606 : wants_debugger (Vdebug_on_error
, conditions
))
1607 && ! skip_debugger (conditions
, combined_data
)
1608 /* RMS: What's this for? */
1609 && when_entered_debugger
< num_nonmacro_input_events
)
1611 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1619 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1621 register Lisp_Object h
;
1623 /* t is used by handlers for all conditions, set up by C code. */
1624 if (EQ (handlers
, Qt
))
1627 /* error is used similarly, but means print an error message
1628 and run the debugger if that is enabled. */
1629 if (EQ (handlers
, Qerror
))
1632 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1634 Lisp_Object handler
= XCAR (h
);
1635 Lisp_Object condit
, tem
;
1637 if (!CONSP (handler
))
1639 condit
= XCAR (handler
);
1640 /* Handle a single condition name in handler HANDLER. */
1641 if (SYMBOLP (condit
))
1643 tem
= Fmemq (Fcar (handler
), conditions
);
1647 /* Handle a list of condition names in handler HANDLER. */
1648 else if (CONSP (condit
))
1651 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1653 tem
= Fmemq (XCAR (tail
), conditions
);
1664 /* Dump an error message; called like vprintf. */
1666 verror (const char *m
, va_list ap
)
1669 ptrdiff_t size
= sizeof buf
;
1670 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1675 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1676 string
= make_string (buffer
, used
);
1680 xsignal1 (Qerror
, string
);
1684 /* Dump an error message; called like printf. */
1688 error (const char *m
, ...)
1695 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1696 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1697 This means it contains a description for how to read arguments to give it.
1698 The value is nil for an invalid function or a symbol with no function
1701 Interactively callable functions include strings and vectors (treated
1702 as keyboard macros), lambda-expressions that contain a top-level call
1703 to `interactive', autoload definitions made by `autoload' with non-nil
1704 fourth argument, and some of the built-in functions of Lisp.
1706 Also, a symbol satisfies `commandp' if its function definition does so.
1708 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1709 then strings and vectors are not accepted. */)
1710 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1712 register Lisp_Object fun
;
1713 register Lisp_Object funcar
;
1714 Lisp_Object if_prop
= Qnil
;
1718 fun
= indirect_function (fun
); /* Check cycles. */
1722 /* Check an `interactive-form' property if present, analogous to the
1723 function-documentation property. */
1725 while (SYMBOLP (fun
))
1727 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1730 fun
= Fsymbol_function (fun
);
1733 /* Emacs primitives are interactive if their DEFUN specifies an
1734 interactive spec. */
1736 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1738 /* Bytecode objects are interactive if they are long enough to
1739 have an element whose index is COMPILED_INTERACTIVE, which is
1740 where the interactive spec is stored. */
1741 else if (COMPILEDP (fun
))
1742 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1745 /* Strings and vectors are keyboard macros. */
1746 if (STRINGP (fun
) || VECTORP (fun
))
1747 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1749 /* Lists may represent commands. */
1752 funcar
= XCAR (fun
);
1753 if (EQ (funcar
, Qclosure
))
1754 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1756 else if (EQ (funcar
, Qlambda
))
1757 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1758 else if (EQ (funcar
, Qautoload
))
1759 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1764 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1765 doc
: /* Define FUNCTION to autoload from FILE.
1766 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1767 Third arg DOCSTRING is documentation for the function.
1768 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1769 Fifth arg TYPE indicates the type of the object:
1770 nil or omitted says FUNCTION is a function,
1771 `keymap' says FUNCTION is really a keymap, and
1772 `macro' or t says FUNCTION is really a macro.
1773 Third through fifth args give info about the real definition.
1774 They default to nil.
1775 If FUNCTION is already defined other than as an autoload,
1776 this does nothing and returns nil. */)
1777 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1779 CHECK_SYMBOL (function
);
1780 CHECK_STRING (file
);
1782 /* If function is defined and not as an autoload, don't override. */
1783 if (!NILP (XSYMBOL (function
)->function
)
1784 && !AUTOLOADP (XSYMBOL (function
)->function
))
1787 if (!NILP (Vpurify_flag
) && EQ (docstring
, make_number (0)))
1788 /* `read1' in lread.c has found the docstring starting with "\
1789 and assumed the docstring will be provided by Snarf-documentation, so it
1790 passed us 0 instead. But that leads to accidental sharing in purecopy's
1791 hash-consing, so we use a (hopefully) unique integer instead. */
1792 docstring
= make_number (XHASH (function
));
1793 return Fdefalias (function
,
1794 list5 (Qautoload
, file
, docstring
, interactive
, type
),
1799 un_autoload (Lisp_Object oldqueue
)
1801 register Lisp_Object queue
, first
, second
;
1803 /* Queue to unwind is current value of Vautoload_queue.
1804 oldqueue is the shadowed value to leave in Vautoload_queue. */
1805 queue
= Vautoload_queue
;
1806 Vautoload_queue
= oldqueue
;
1807 while (CONSP (queue
))
1809 first
= XCAR (queue
);
1810 second
= Fcdr (first
);
1811 first
= Fcar (first
);
1812 if (EQ (first
, make_number (0)))
1815 Ffset (first
, second
);
1816 queue
= XCDR (queue
);
1821 /* Load an autoloaded function.
1822 FUNNAME is the symbol which is the function's name.
1823 FUNDEF is the autoload definition (a list). */
1825 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1826 doc
: /* Load FUNDEF which should be an autoload.
1827 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1828 in which case the function returns the new autoloaded function value.
1829 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1830 it is defines a macro. */)
1831 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1833 ptrdiff_t count
= SPECPDL_INDEX ();
1834 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1836 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1839 if (EQ (macro_only
, Qmacro
))
1841 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1842 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1846 /* This is to make sure that loadup.el gives a clear picture
1847 of what files are preloaded and when. */
1848 if (! NILP (Vpurify_flag
))
1849 error ("Attempt to autoload %s while preparing to dump",
1850 SDATA (SYMBOL_NAME (funname
)));
1852 CHECK_SYMBOL (funname
);
1853 GCPRO3 (funname
, fundef
, macro_only
);
1855 /* Preserve the match data. */
1856 record_unwind_save_match_data ();
1858 /* If autoloading gets an error (which includes the error of failing
1859 to define the function being called), we use Vautoload_queue
1860 to undo function definitions and `provide' calls made by
1861 the function. We do this in the specific case of autoloading
1862 because autoloading is not an explicit request "load this file",
1863 but rather a request to "call this function".
1865 The value saved here is to be restored into Vautoload_queue. */
1866 record_unwind_protect (un_autoload
, Vautoload_queue
);
1867 Vautoload_queue
= Qt
;
1868 /* If `macro_only', assume this autoload to be a "best-effort",
1869 so don't signal an error if autoloading fails. */
1870 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
1872 /* Once loading finishes, don't undo it. */
1873 Vautoload_queue
= Qt
;
1874 unbind_to (count
, Qnil
);
1882 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
1884 if (!NILP (Fequal (fun
, fundef
)))
1885 error ("Autoloading failed to define function %s",
1886 SDATA (SYMBOL_NAME (funname
)));
1893 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
1894 doc
: /* Evaluate FORM and return its value.
1895 If LEXICAL is t, evaluate using lexical scoping. */)
1896 (Lisp_Object form
, Lisp_Object lexical
)
1898 ptrdiff_t count
= SPECPDL_INDEX ();
1899 specbind (Qinternal_interpreter_environment
,
1900 CONSP (lexical
) || NILP (lexical
) ? lexical
: Fcons (Qt
, Qnil
));
1901 return unbind_to (count
, eval_sub (form
));
1904 /* Eval a sub-expression of the current expression (i.e. in the same
1907 eval_sub (Lisp_Object form
)
1909 Lisp_Object fun
, val
, original_fun
, original_args
;
1911 struct backtrace backtrace
;
1912 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1916 /* Look up its binding in the lexical environment.
1917 We do not pay attention to the declared_special flag here, since we
1918 already did that when let-binding the variable. */
1919 Lisp_Object lex_binding
1920 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
1921 ? Fassq (form
, Vinternal_interpreter_environment
)
1923 if (CONSP (lex_binding
))
1924 return XCDR (lex_binding
);
1926 return Fsymbol_value (form
);
1938 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1940 if (max_lisp_eval_depth
< 100)
1941 max_lisp_eval_depth
= 100;
1942 if (lisp_eval_depth
> max_lisp_eval_depth
)
1943 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
1946 original_fun
= XCAR (form
);
1947 original_args
= XCDR (form
);
1949 backtrace
.next
= backtrace_list
;
1950 backtrace
.function
= original_fun
; /* This also protects them from gc. */
1951 backtrace
.args
= &original_args
;
1952 backtrace
.nargs
= UNEVALLED
;
1953 backtrace
.debug_on_exit
= 0;
1954 backtrace_list
= &backtrace
;
1956 if (debug_on_next_call
)
1957 do_debug_on_call (Qt
);
1959 /* At this point, only original_fun and original_args
1960 have values that will be used below. */
1963 /* Optimize for no indirection. */
1965 if (SYMBOLP (fun
) && !NILP (fun
)
1966 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
1967 fun
= indirect_function (fun
);
1971 Lisp_Object numargs
;
1972 Lisp_Object argvals
[8];
1973 Lisp_Object args_left
;
1974 register int i
, maxargs
;
1976 args_left
= original_args
;
1977 numargs
= Flength (args_left
);
1981 if (XINT (numargs
) < XSUBR (fun
)->min_args
1982 || (XSUBR (fun
)->max_args
>= 0
1983 && XSUBR (fun
)->max_args
< XINT (numargs
)))
1984 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
1986 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
1987 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
1988 else if (XSUBR (fun
)->max_args
== MANY
)
1990 /* Pass a vector of evaluated arguments. */
1992 ptrdiff_t argnum
= 0;
1995 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
1997 GCPRO3 (args_left
, fun
, fun
);
2001 while (!NILP (args_left
))
2003 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2004 args_left
= Fcdr (args_left
);
2005 gcpro3
.nvars
= argnum
;
2008 backtrace
.args
= vals
;
2009 backtrace
.nargs
= XINT (numargs
);
2011 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2017 GCPRO3 (args_left
, fun
, fun
);
2018 gcpro3
.var
= argvals
;
2021 maxargs
= XSUBR (fun
)->max_args
;
2022 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2024 argvals
[i
] = eval_sub (Fcar (args_left
));
2030 backtrace
.args
= argvals
;
2031 backtrace
.nargs
= XINT (numargs
);
2036 val
= (XSUBR (fun
)->function
.a0 ());
2039 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2042 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2045 val
= (XSUBR (fun
)->function
.a3
2046 (argvals
[0], argvals
[1], argvals
[2]));
2049 val
= (XSUBR (fun
)->function
.a4
2050 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2053 val
= (XSUBR (fun
)->function
.a5
2054 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2058 val
= (XSUBR (fun
)->function
.a6
2059 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2060 argvals
[4], argvals
[5]));
2063 val
= (XSUBR (fun
)->function
.a7
2064 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2065 argvals
[4], argvals
[5], argvals
[6]));
2069 val
= (XSUBR (fun
)->function
.a8
2070 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2071 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2075 /* Someone has created a subr that takes more arguments than
2076 is supported by this code. We need to either rewrite the
2077 subr to use a different argument protocol, or add more
2078 cases to this switch. */
2083 else if (COMPILEDP (fun
))
2084 val
= apply_lambda (fun
, original_args
);
2088 xsignal1 (Qvoid_function
, original_fun
);
2090 xsignal1 (Qinvalid_function
, original_fun
);
2091 funcar
= XCAR (fun
);
2092 if (!SYMBOLP (funcar
))
2093 xsignal1 (Qinvalid_function
, original_fun
);
2094 if (EQ (funcar
, Qautoload
))
2096 Fautoload_do_load (fun
, original_fun
, Qnil
);
2099 if (EQ (funcar
, Qmacro
))
2101 ptrdiff_t count
= SPECPDL_INDEX ();
2103 /* Bind lexical-binding during expansion of the macro, so the
2104 macro can know reliably if the code it outputs will be
2105 interpreted using lexical-binding or not. */
2106 specbind (Qlexical_binding
,
2107 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2108 exp
= apply1 (Fcdr (fun
), original_args
);
2109 unbind_to (count
, Qnil
);
2110 val
= eval_sub (exp
);
2112 else if (EQ (funcar
, Qlambda
)
2113 || EQ (funcar
, Qclosure
))
2114 val
= apply_lambda (fun
, original_args
);
2116 xsignal1 (Qinvalid_function
, original_fun
);
2121 if (backtrace
.debug_on_exit
)
2122 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2123 backtrace_list
= backtrace
.next
;
2128 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2129 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2130 Then return the value FUNCTION returns.
2131 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2132 usage: (apply FUNCTION &rest ARGUMENTS) */)
2133 (ptrdiff_t nargs
, Lisp_Object
*args
)
2137 register Lisp_Object spread_arg
;
2138 register Lisp_Object
*funcall_args
;
2139 Lisp_Object fun
, retval
;
2140 struct gcpro gcpro1
;
2145 spread_arg
= args
[nargs
- 1];
2146 CHECK_LIST (spread_arg
);
2148 numargs
= XINT (Flength (spread_arg
));
2151 return Ffuncall (nargs
- 1, args
);
2152 else if (numargs
== 1)
2154 args
[nargs
- 1] = XCAR (spread_arg
);
2155 return Ffuncall (nargs
, args
);
2158 numargs
+= nargs
- 2;
2160 /* Optimize for no indirection. */
2161 if (SYMBOLP (fun
) && !NILP (fun
)
2162 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2163 fun
= indirect_function (fun
);
2166 /* Let funcall get the error. */
2173 if (numargs
< XSUBR (fun
)->min_args
2174 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2175 goto funcall
; /* Let funcall get the error. */
2176 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2178 /* Avoid making funcall cons up a yet another new vector of arguments
2179 by explicitly supplying nil's for optional values. */
2180 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2181 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2182 funcall_args
[++i
] = Qnil
;
2183 GCPRO1 (*funcall_args
);
2184 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2188 /* We add 1 to numargs because funcall_args includes the
2189 function itself as well as its arguments. */
2192 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2193 GCPRO1 (*funcall_args
);
2194 gcpro1
.nvars
= 1 + numargs
;
2197 memcpy (funcall_args
, args
, nargs
* word_size
);
2198 /* Spread the last arg we got. Its first element goes in
2199 the slot that it used to occupy, hence this value of I. */
2201 while (!NILP (spread_arg
))
2203 funcall_args
[i
++] = XCAR (spread_arg
);
2204 spread_arg
= XCDR (spread_arg
);
2207 /* By convention, the caller needs to gcpro Ffuncall's args. */
2208 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2215 /* Run hook variables in various ways. */
2218 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2220 Ffuncall (nargs
, args
);
2224 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2225 doc
: /* Run each hook in HOOKS.
2226 Each argument should be a symbol, a hook variable.
2227 These symbols are processed in the order specified.
2228 If a hook symbol has a non-nil value, that value may be a function
2229 or a list of functions to be called to run the hook.
2230 If the value is a function, it is called with no arguments.
2231 If it is a list, the elements are called, in order, with no arguments.
2233 Major modes should not use this function directly to run their mode
2234 hook; they should use `run-mode-hooks' instead.
2236 Do not use `make-local-variable' to make a hook variable buffer-local.
2237 Instead, use `add-hook' and specify t for the LOCAL argument.
2238 usage: (run-hooks &rest HOOKS) */)
2239 (ptrdiff_t nargs
, Lisp_Object
*args
)
2241 Lisp_Object hook
[1];
2244 for (i
= 0; i
< nargs
; i
++)
2247 run_hook_with_args (1, hook
, funcall_nil
);
2253 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2254 Srun_hook_with_args
, 1, MANY
, 0,
2255 doc
: /* Run HOOK with the specified arguments ARGS.
2256 HOOK should be a symbol, a hook variable. The value of HOOK
2257 may be nil, a function, or a list of functions. Call each
2258 function in order with arguments ARGS. The final return value
2261 Do not use `make-local-variable' to make a hook variable buffer-local.
2262 Instead, use `add-hook' and specify t for the LOCAL argument.
2263 usage: (run-hook-with-args HOOK &rest ARGS) */)
2264 (ptrdiff_t nargs
, Lisp_Object
*args
)
2266 return run_hook_with_args (nargs
, args
, funcall_nil
);
2269 /* NB this one still documents a specific non-nil return value.
2270 (As did run-hook-with-args and run-hook-with-args-until-failure
2271 until they were changed in 24.1.) */
2272 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2273 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2274 doc
: /* Run HOOK with the specified arguments ARGS.
2275 HOOK should be a symbol, a hook variable. The value of HOOK
2276 may be nil, a function, or a list of functions. Call each
2277 function in order with arguments ARGS, stopping at the first
2278 one that returns non-nil, and return that value. Otherwise (if
2279 all functions return nil, or if there are no functions to call),
2282 Do not use `make-local-variable' to make a hook variable buffer-local.
2283 Instead, use `add-hook' and specify t for the LOCAL argument.
2284 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2285 (ptrdiff_t nargs
, Lisp_Object
*args
)
2287 return run_hook_with_args (nargs
, args
, Ffuncall
);
2291 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2293 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2296 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2297 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2298 doc
: /* Run HOOK with the specified arguments ARGS.
2299 HOOK should be a symbol, a hook variable. The value of HOOK
2300 may be nil, a function, or a list of functions. Call each
2301 function in order with arguments ARGS, stopping at the first
2302 one that returns nil, and return nil. Otherwise (if all functions
2303 return non-nil, or if there are no functions to call), return non-nil
2304 \(do not rely on the precise return value in this case).
2306 Do not use `make-local-variable' to make a hook variable buffer-local.
2307 Instead, use `add-hook' and specify t for the LOCAL argument.
2308 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2309 (ptrdiff_t nargs
, Lisp_Object
*args
)
2311 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2315 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2317 Lisp_Object tmp
= args
[0], ret
;
2320 ret
= Ffuncall (nargs
, args
);
2326 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2327 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2328 I.e. instead of calling each function FUN directly with arguments ARGS,
2329 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2330 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2331 aborts and returns that value.
2332 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2333 (ptrdiff_t nargs
, Lisp_Object
*args
)
2335 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2338 /* ARGS[0] should be a hook symbol.
2339 Call each of the functions in the hook value, passing each of them
2340 as arguments all the rest of ARGS (all NARGS - 1 elements).
2341 FUNCALL specifies how to call each function on the hook.
2342 The caller (or its caller, etc) must gcpro all of ARGS,
2343 except that it isn't necessary to gcpro ARGS[0]. */
2346 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2347 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2349 Lisp_Object sym
, val
, ret
= Qnil
;
2350 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2352 /* If we are dying or still initializing,
2353 don't do anything--it would probably crash if we tried. */
2354 if (NILP (Vrun_hooks
))
2358 val
= find_symbol_value (sym
);
2360 if (EQ (val
, Qunbound
) || NILP (val
))
2362 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2365 return funcall (nargs
, args
);
2369 Lisp_Object global_vals
= Qnil
;
2370 GCPRO3 (sym
, val
, global_vals
);
2373 CONSP (val
) && NILP (ret
);
2376 if (EQ (XCAR (val
), Qt
))
2378 /* t indicates this hook has a local binding;
2379 it means to run the global binding too. */
2380 global_vals
= Fdefault_value (sym
);
2381 if (NILP (global_vals
)) continue;
2383 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2385 args
[0] = global_vals
;
2386 ret
= funcall (nargs
, args
);
2391 CONSP (global_vals
) && NILP (ret
);
2392 global_vals
= XCDR (global_vals
))
2394 args
[0] = XCAR (global_vals
);
2395 /* In a global value, t should not occur. If it does, we
2396 must ignore it to avoid an endless loop. */
2397 if (!EQ (args
[0], Qt
))
2398 ret
= funcall (nargs
, args
);
2404 args
[0] = XCAR (val
);
2405 ret
= funcall (nargs
, args
);
2414 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2417 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2419 Lisp_Object temp
[3];
2424 Frun_hook_with_args (3, temp
);
2427 /* Apply fn to arg. */
2429 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2431 struct gcpro gcpro1
;
2435 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2438 Lisp_Object args
[2];
2442 RETURN_UNGCPRO (Fapply (2, args
));
2446 /* Call function fn on no arguments. */
2448 call0 (Lisp_Object fn
)
2450 struct gcpro gcpro1
;
2453 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2456 /* Call function fn with 1 argument arg1. */
2459 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2461 struct gcpro gcpro1
;
2462 Lisp_Object args
[2];
2468 RETURN_UNGCPRO (Ffuncall (2, args
));
2471 /* Call function fn with 2 arguments arg1, arg2. */
2474 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2476 struct gcpro gcpro1
;
2477 Lisp_Object args
[3];
2483 RETURN_UNGCPRO (Ffuncall (3, args
));
2486 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2489 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2491 struct gcpro gcpro1
;
2492 Lisp_Object args
[4];
2499 RETURN_UNGCPRO (Ffuncall (4, args
));
2502 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2505 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2508 struct gcpro gcpro1
;
2509 Lisp_Object args
[5];
2517 RETURN_UNGCPRO (Ffuncall (5, args
));
2520 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2523 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2524 Lisp_Object arg4
, Lisp_Object arg5
)
2526 struct gcpro gcpro1
;
2527 Lisp_Object args
[6];
2536 RETURN_UNGCPRO (Ffuncall (6, args
));
2539 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2542 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2543 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2545 struct gcpro gcpro1
;
2546 Lisp_Object args
[7];
2556 RETURN_UNGCPRO (Ffuncall (7, args
));
2559 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2562 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2563 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2565 struct gcpro gcpro1
;
2566 Lisp_Object args
[8];
2577 RETURN_UNGCPRO (Ffuncall (8, args
));
2580 /* The caller should GCPRO all the elements of ARGS. */
2582 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2583 doc
: /* Non-nil if OBJECT is a function. */)
2584 (Lisp_Object object
)
2586 if (FUNCTIONP (object
))
2591 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2592 doc
: /* Call first argument as a function, passing remaining arguments to it.
2593 Return the value that function returns.
2594 Thus, (funcall 'cons 'x 'y) returns (x . y).
2595 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2596 (ptrdiff_t nargs
, Lisp_Object
*args
)
2598 Lisp_Object fun
, original_fun
;
2600 ptrdiff_t numargs
= nargs
- 1;
2601 Lisp_Object lisp_numargs
;
2603 struct backtrace backtrace
;
2604 register Lisp_Object
*internal_args
;
2609 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2611 if (max_lisp_eval_depth
< 100)
2612 max_lisp_eval_depth
= 100;
2613 if (lisp_eval_depth
> max_lisp_eval_depth
)
2614 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2617 backtrace
.next
= backtrace_list
;
2618 backtrace
.function
= args
[0];
2619 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2620 backtrace
.nargs
= nargs
- 1;
2621 backtrace
.debug_on_exit
= 0;
2622 backtrace_list
= &backtrace
;
2624 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2627 if (debug_on_next_call
)
2628 do_debug_on_call (Qlambda
);
2632 original_fun
= args
[0];
2636 /* Optimize for no indirection. */
2638 if (SYMBOLP (fun
) && !NILP (fun
)
2639 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2640 fun
= indirect_function (fun
);
2644 if (numargs
< XSUBR (fun
)->min_args
2645 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2647 XSETFASTINT (lisp_numargs
, numargs
);
2648 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2651 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2652 xsignal1 (Qinvalid_function
, original_fun
);
2654 else if (XSUBR (fun
)->max_args
== MANY
)
2655 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2658 if (XSUBR (fun
)->max_args
> numargs
)
2660 internal_args
= alloca (XSUBR (fun
)->max_args
2661 * sizeof *internal_args
);
2662 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2663 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2664 internal_args
[i
] = Qnil
;
2667 internal_args
= args
+ 1;
2668 switch (XSUBR (fun
)->max_args
)
2671 val
= (XSUBR (fun
)->function
.a0 ());
2674 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2677 val
= (XSUBR (fun
)->function
.a2
2678 (internal_args
[0], internal_args
[1]));
2681 val
= (XSUBR (fun
)->function
.a3
2682 (internal_args
[0], internal_args
[1], internal_args
[2]));
2685 val
= (XSUBR (fun
)->function
.a4
2686 (internal_args
[0], internal_args
[1], internal_args
[2],
2690 val
= (XSUBR (fun
)->function
.a5
2691 (internal_args
[0], internal_args
[1], internal_args
[2],
2692 internal_args
[3], internal_args
[4]));
2695 val
= (XSUBR (fun
)->function
.a6
2696 (internal_args
[0], internal_args
[1], internal_args
[2],
2697 internal_args
[3], internal_args
[4], internal_args
[5]));
2700 val
= (XSUBR (fun
)->function
.a7
2701 (internal_args
[0], internal_args
[1], internal_args
[2],
2702 internal_args
[3], internal_args
[4], internal_args
[5],
2707 val
= (XSUBR (fun
)->function
.a8
2708 (internal_args
[0], internal_args
[1], internal_args
[2],
2709 internal_args
[3], internal_args
[4], internal_args
[5],
2710 internal_args
[6], internal_args
[7]));
2715 /* If a subr takes more than 8 arguments without using MANY
2716 or UNEVALLED, we need to extend this function to support it.
2717 Until this is done, there is no way to call the function. */
2722 else if (COMPILEDP (fun
))
2723 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2727 xsignal1 (Qvoid_function
, original_fun
);
2729 xsignal1 (Qinvalid_function
, original_fun
);
2730 funcar
= XCAR (fun
);
2731 if (!SYMBOLP (funcar
))
2732 xsignal1 (Qinvalid_function
, original_fun
);
2733 if (EQ (funcar
, Qlambda
)
2734 || EQ (funcar
, Qclosure
))
2735 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2736 else if (EQ (funcar
, Qautoload
))
2738 Fautoload_do_load (fun
, original_fun
, Qnil
);
2743 xsignal1 (Qinvalid_function
, original_fun
);
2747 if (backtrace
.debug_on_exit
)
2748 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2749 backtrace_list
= backtrace
.next
;
2754 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2756 Lisp_Object args_left
;
2759 register Lisp_Object
*arg_vector
;
2760 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2761 register Lisp_Object tem
;
2764 numargs
= XFASTINT (Flength (args
));
2765 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2768 GCPRO3 (*arg_vector
, args_left
, fun
);
2771 for (i
= 0; i
< numargs
; )
2773 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2774 tem
= eval_sub (tem
);
2775 arg_vector
[i
++] = tem
;
2781 backtrace_list
->args
= arg_vector
;
2782 backtrace_list
->nargs
= i
;
2783 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2785 /* Do the debug-on-exit now, while arg_vector still exists. */
2786 if (backtrace_list
->debug_on_exit
)
2787 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2788 /* Don't do it again when we return to eval. */
2789 backtrace_list
->debug_on_exit
= 0;
2794 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2795 and return the result of evaluation.
2796 FUN must be either a lambda-expression or a compiled-code object. */
2799 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2800 register Lisp_Object
*arg_vector
)
2802 Lisp_Object val
, syms_left
, next
, lexenv
;
2803 ptrdiff_t count
= SPECPDL_INDEX ();
2805 bool optional
, rest
;
2809 if (EQ (XCAR (fun
), Qclosure
))
2811 fun
= XCDR (fun
); /* Drop `closure'. */
2812 lexenv
= XCAR (fun
);
2813 CHECK_LIST_CONS (fun
, fun
);
2817 syms_left
= XCDR (fun
);
2818 if (CONSP (syms_left
))
2819 syms_left
= XCAR (syms_left
);
2821 xsignal1 (Qinvalid_function
, fun
);
2823 else if (COMPILEDP (fun
))
2825 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2826 if (INTEGERP (syms_left
))
2827 /* A byte-code object with a non-nil `push args' slot means we
2828 shouldn't bind any arguments, instead just call the byte-code
2829 interpreter directly; it will push arguments as necessary.
2831 Byte-code objects with either a non-existent, or a nil value for
2832 the `push args' slot (the default), have dynamically-bound
2833 arguments, and use the argument-binding code below instead (as do
2834 all interpreted functions, even lexically bound ones). */
2836 /* If we have not actually read the bytecode string
2837 and constants vector yet, fetch them from the file. */
2838 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2839 Ffetch_bytecode (fun
);
2840 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2841 AREF (fun
, COMPILED_CONSTANTS
),
2842 AREF (fun
, COMPILED_STACK_DEPTH
),
2851 i
= optional
= rest
= 0;
2852 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2856 next
= XCAR (syms_left
);
2857 if (!SYMBOLP (next
))
2858 xsignal1 (Qinvalid_function
, fun
);
2860 if (EQ (next
, Qand_rest
))
2862 else if (EQ (next
, Qand_optional
))
2869 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2873 arg
= arg_vector
[i
++];
2875 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2879 /* Bind the argument. */
2880 if (!NILP (lexenv
) && SYMBOLP (next
))
2881 /* Lexically bind NEXT by adding it to the lexenv alist. */
2882 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
2884 /* Dynamically bind NEXT. */
2885 specbind (next
, arg
);
2889 if (!NILP (syms_left
))
2890 xsignal1 (Qinvalid_function
, fun
);
2892 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2894 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
2895 /* Instantiate a new lexical environment. */
2896 specbind (Qinternal_interpreter_environment
, lexenv
);
2899 val
= Fprogn (XCDR (XCDR (fun
)));
2902 /* If we have not actually read the bytecode string
2903 and constants vector yet, fetch them from the file. */
2904 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2905 Ffetch_bytecode (fun
);
2906 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2907 AREF (fun
, COMPILED_CONSTANTS
),
2908 AREF (fun
, COMPILED_STACK_DEPTH
),
2912 return unbind_to (count
, val
);
2915 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2917 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2918 (Lisp_Object object
)
2922 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
2924 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
2927 tem
= AREF (object
, COMPILED_BYTECODE
);
2928 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
2929 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
2931 error ("Invalid byte code");
2933 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
2934 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
2942 register ptrdiff_t count
= SPECPDL_INDEX ();
2943 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
2944 if (max_size
<= specpdl_size
)
2946 if (max_specpdl_size
< 400)
2947 max_size
= max_specpdl_size
= 400;
2948 if (max_size
<= specpdl_size
)
2949 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
2951 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
2952 specpdl_ptr
= specpdl
+ count
;
2955 /* `specpdl_ptr->symbol' is a field which describes which variable is
2956 let-bound, so it can be properly undone when we unbind_to.
2957 It can have the following two shapes:
2958 - SYMBOL : if it's a plain symbol, it means that we have let-bound
2959 a symbol that is not buffer-local (at least at the time
2960 the let binding started). Note also that it should not be
2961 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2963 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
2964 variable SYMBOL which can be buffer-local. WHERE tells us
2965 which buffer is affected (or nil if the let-binding affects the
2966 global value of the variable) and BUFFER tells us which buffer was
2967 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
2968 BUFFER did not yet have a buffer-local value). */
2971 specbind (Lisp_Object symbol
, Lisp_Object value
)
2973 struct Lisp_Symbol
*sym
;
2975 CHECK_SYMBOL (symbol
);
2976 sym
= XSYMBOL (symbol
);
2977 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2981 switch (sym
->redirect
)
2983 case SYMBOL_VARALIAS
:
2984 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
2985 case SYMBOL_PLAINVAL
:
2986 /* The most common case is that of a non-constant symbol with a
2987 trivial value. Make that as fast as we can. */
2988 set_specpdl_symbol (symbol
);
2989 set_specpdl_old_value (SYMBOL_VAL (sym
));
2990 specpdl_ptr
->func
= NULL
;
2993 SET_SYMBOL_VAL (sym
, value
);
2995 set_internal (symbol
, value
, Qnil
, 1);
2997 case SYMBOL_LOCALIZED
:
2998 if (SYMBOL_BLV (sym
)->frame_local
)
2999 error ("Frame-local vars cannot be let-bound");
3000 case SYMBOL_FORWARDED
:
3002 Lisp_Object ovalue
= find_symbol_value (symbol
);
3003 specpdl_ptr
->func
= 0;
3004 set_specpdl_old_value (ovalue
);
3006 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3007 || (EQ (SYMBOL_BLV (sym
)->where
,
3008 SYMBOL_BLV (sym
)->frame_local
?
3009 Fselected_frame () : Fcurrent_buffer ())));
3011 if (sym
->redirect
== SYMBOL_LOCALIZED
3012 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3014 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3016 /* For a local variable, record both the symbol and which
3017 buffer's or frame's value we are saving. */
3018 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3020 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3021 || (blv_found (SYMBOL_BLV (sym
))
3022 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3025 else if (sym
->redirect
== SYMBOL_LOCALIZED
3026 && blv_found (SYMBOL_BLV (sym
)))
3027 where
= SYMBOL_BLV (sym
)->where
;
3031 /* We're not using the `unused' slot in the specbinding
3032 structure because this would mean we have to do more
3033 work for simple variables. */
3034 /* FIXME: The third value `current_buffer' is only used in
3035 let_shadows_buffer_binding_p which is itself only used
3036 in set_internal for local_if_set. */
3037 eassert (NILP (where
) || EQ (where
, cur_buf
));
3038 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3040 /* If SYMBOL is a per-buffer variable which doesn't have a
3041 buffer-local value here, make the `let' change the global
3042 value by changing the value of SYMBOL in all buffers not
3043 having their own value. This is consistent with what
3044 happens with other buffer-local variables. */
3046 && sym
->redirect
== SYMBOL_FORWARDED
)
3048 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3050 Fset_default (symbol
, value
);
3055 set_specpdl_symbol (symbol
);
3058 set_internal (symbol
, value
, Qnil
, 1);
3061 default: emacs_abort ();
3066 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3068 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3070 specpdl_ptr
->func
= function
;
3071 set_specpdl_symbol (Qnil
);
3072 set_specpdl_old_value (arg
);
3077 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3079 Lisp_Object quitf
= Vquit_flag
;
3080 struct gcpro gcpro1
, gcpro2
;
3082 GCPRO2 (value
, quitf
);
3085 while (specpdl_ptr
!= specpdl
+ count
)
3087 /* Copy the binding, and decrement specpdl_ptr, before we do
3088 the work to unbind it. We decrement first
3089 so that an error in unbinding won't try to unbind
3090 the same entry again, and we copy the binding first
3091 in case more bindings are made during some of the code we run. */
3093 struct specbinding this_binding
;
3094 this_binding
= *--specpdl_ptr
;
3096 if (this_binding
.func
!= 0)
3097 (*this_binding
.func
) (this_binding
.old_value
);
3098 /* If the symbol is a list, it is really (SYMBOL WHERE
3099 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3100 frame. If WHERE is a buffer or frame, this indicates we
3101 bound a variable that had a buffer-local or frame-local
3102 binding. WHERE nil means that the variable had the default
3103 value when it was bound. CURRENT-BUFFER is the buffer that
3104 was current when the variable was bound. */
3105 else if (CONSP (this_binding
.symbol
))
3107 Lisp_Object symbol
, where
;
3109 symbol
= XCAR (this_binding
.symbol
);
3110 where
= XCAR (XCDR (this_binding
.symbol
));
3113 Fset_default (symbol
, this_binding
.old_value
);
3114 /* If `where' is non-nil, reset the value in the appropriate
3115 local binding, but only if that binding still exists. */
3116 else if (BUFFERP (where
)
3117 ? !NILP (Flocal_variable_p (symbol
, where
))
3118 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3119 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3121 /* If variable has a trivial value (no forwarding), we can
3122 just set it. No need to check for constant symbols here,
3123 since that was already done by specbind. */
3124 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3125 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3126 this_binding
.old_value
);
3128 /* NOTE: we only ever come here if make_local_foo was used for
3129 the first time on this var within this let. */
3130 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3133 if (NILP (Vquit_flag
) && !NILP (quitf
))
3140 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3141 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3142 A special variable is one that will be bound dynamically, even in a
3143 context where binding is lexical by default. */)
3144 (Lisp_Object symbol
)
3146 CHECK_SYMBOL (symbol
);
3147 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3151 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3152 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3153 The debugger is entered when that frame exits, if the flag is non-nil. */)
3154 (Lisp_Object level
, Lisp_Object flag
)
3156 register struct backtrace
*backlist
= backtrace_list
;
3157 register EMACS_INT i
;
3159 CHECK_NUMBER (level
);
3161 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3163 backlist
= backlist
->next
;
3167 backlist
->debug_on_exit
= !NILP (flag
);
3172 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3173 doc
: /* Print a trace of Lisp function calls currently active.
3174 Output stream used is value of `standard-output'. */)
3177 register struct backtrace
*backlist
= backtrace_list
;
3180 struct gcpro gcpro1
;
3181 Lisp_Object old_print_level
= Vprint_level
;
3183 if (NILP (Vprint_level
))
3184 XSETFASTINT (Vprint_level
, 8);
3191 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3192 if (backlist
->nargs
== UNEVALLED
)
3194 Fprin1 (Fcons (backlist
->function
, *backlist
->args
), Qnil
);
3195 write_string ("\n", -1);
3199 tem
= backlist
->function
;
3200 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3201 write_string ("(", -1);
3202 if (backlist
->nargs
== MANY
)
3203 { /* FIXME: Can this happen? */
3205 for (tail
= *backlist
->args
; !NILP (tail
); tail
= Fcdr (tail
))
3208 write_string (" ", -1);
3209 Fprin1 (Fcar (tail
), Qnil
);
3216 for (i
= 0; i
< backlist
->nargs
; i
++)
3218 if (i
) write_string (" ", -1);
3219 Fprin1 (backlist
->args
[i
], Qnil
);
3222 write_string (")\n", -1);
3224 backlist
= backlist
->next
;
3227 Vprint_level
= old_print_level
;
3232 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3233 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3234 If that frame has not evaluated the arguments yet (or is a special form),
3235 the value is (nil FUNCTION ARG-FORMS...).
3236 If that frame has evaluated its arguments and called its function already,
3237 the value is (t FUNCTION ARG-VALUES...).
3238 A &rest arg is represented as the tail of the list ARG-VALUES.
3239 FUNCTION is whatever was supplied as car of evaluated list,
3240 or a lambda expression for macro calls.
3241 If NFRAMES is more than the number of frames, the value is nil. */)
3242 (Lisp_Object nframes
)
3244 register struct backtrace
*backlist
= backtrace_list
;
3245 register EMACS_INT i
;
3248 CHECK_NATNUM (nframes
);
3250 /* Find the frame requested. */
3251 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3252 backlist
= backlist
->next
;
3256 if (backlist
->nargs
== UNEVALLED
)
3257 return Fcons (Qnil
, Fcons (backlist
->function
, *backlist
->args
));
3260 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3261 tem
= *backlist
->args
;
3263 tem
= Flist (backlist
->nargs
, backlist
->args
);
3265 return Fcons (Qt
, Fcons (backlist
->function
, tem
));
3272 mark_backtrace (void)
3274 register struct backtrace
*backlist
;
3277 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3279 mark_object (backlist
->function
);
3281 if (backlist
->nargs
== UNEVALLED
3282 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3285 i
= backlist
->nargs
;
3287 mark_object (backlist
->args
[i
]);
3295 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3296 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3297 If Lisp code tries to increase the total number past this amount,
3298 an error is signaled.
3299 You can safely use a value considerably larger than the default value,
3300 if that proves inconveniently small. However, if you increase it too far,
3301 Emacs could run out of memory trying to make the stack bigger. */);
3303 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3304 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3306 This limit serves to catch infinite recursions for you before they cause
3307 actual stack overflow in C, which would be fatal for Emacs.
3308 You can safely make it considerably larger than its default value,
3309 if that proves inconveniently small. However, if you increase it too far,
3310 Emacs could overflow the real C stack, and crash. */);
3312 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3313 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3314 If the value is t, that means do an ordinary quit.
3315 If the value equals `throw-on-input', that means quit by throwing
3316 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3317 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3318 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3321 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3322 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3323 Note that `quit-flag' will still be set by typing C-g,
3324 so a quit will be signaled as soon as `inhibit-quit' is nil.
3325 To prevent this happening, set `quit-flag' to nil
3326 before making `inhibit-quit' nil. */);
3327 Vinhibit_quit
= Qnil
;
3329 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3330 DEFSYM (Qautoload
, "autoload");
3331 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3332 DEFSYM (Qmacro
, "macro");
3333 DEFSYM (Qdeclare
, "declare");
3335 /* Note that the process handling also uses Qexit, but we don't want
3336 to staticpro it twice, so we just do it here. */
3337 DEFSYM (Qexit
, "exit");
3339 DEFSYM (Qinteractive
, "interactive");
3340 DEFSYM (Qcommandp
, "commandp");
3341 DEFSYM (Qand_rest
, "&rest");
3342 DEFSYM (Qand_optional
, "&optional");
3343 DEFSYM (Qclosure
, "closure");
3344 DEFSYM (Qdebug
, "debug");
3346 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3347 doc
: /* Non-nil means never enter the debugger.
3348 Normally set while the debugger is already active, to avoid recursive
3350 Vinhibit_debugger
= Qnil
;
3352 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3353 doc
: /* Non-nil means enter debugger if an error is signaled.
3354 Does not apply to errors handled by `condition-case' or those
3355 matched by `debug-ignored-errors'.
3356 If the value is a list, an error only means to enter the debugger
3357 if one of its condition symbols appears in the list.
3358 When you evaluate an expression interactively, this variable
3359 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3360 The command `toggle-debug-on-error' toggles this.
3361 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3362 Vdebug_on_error
= Qnil
;
3364 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3365 doc
: /* List of errors for which the debugger should not be called.
3366 Each element may be a condition-name or a regexp that matches error messages.
3367 If any element applies to a given error, that error skips the debugger
3368 and just returns to top level.
3369 This overrides the variable `debug-on-error'.
3370 It does not apply to errors handled by `condition-case'. */);
3371 Vdebug_ignored_errors
= Qnil
;
3373 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3374 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3375 Does not apply if quit is handled by a `condition-case'. */);
3378 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3379 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3381 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3382 doc
: /* Non-nil means debugger may continue execution.
3383 This is nil when the debugger is called under circumstances where it
3384 might not be safe to continue. */);
3385 debugger_may_continue
= 1;
3387 DEFVAR_LISP ("debugger", Vdebugger
,
3388 doc
: /* Function to call to invoke debugger.
3389 If due to frame exit, args are `exit' and the value being returned;
3390 this function's value will be returned instead of that.
3391 If due to error, args are `error' and a list of the args to `signal'.
3392 If due to `apply' or `funcall' entry, one arg, `lambda'.
3393 If due to `eval' entry, one arg, t. */);
3396 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3397 doc
: /* If non-nil, this is a function for `signal' to call.
3398 It receives the same arguments that `signal' was given.
3399 The Edebug package uses this to regain control. */);
3400 Vsignal_hook_function
= Qnil
;
3402 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3403 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3404 Note that `debug-on-error', `debug-on-quit' and friends
3405 still determine whether to handle the particular condition. */);
3406 Vdebug_on_signal
= Qnil
;
3408 /* When lexical binding is being used,
3409 Vinternal_interpreter_environment is non-nil, and contains an alist
3410 of lexically-bound variable, or (t), indicating an empty
3411 environment. The lisp name of this variable would be
3412 `internal-interpreter-environment' if it weren't hidden.
3413 Every element of this list can be either a cons (VAR . VAL)
3414 specifying a lexical binding, or a single symbol VAR indicating
3415 that this variable should use dynamic scoping. */
3416 DEFSYM (Qinternal_interpreter_environment
,
3417 "internal-interpreter-environment");
3418 DEFVAR_LISP ("internal-interpreter-environment",
3419 Vinternal_interpreter_environment
,
3420 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3421 When lexical binding is not being used, this variable is nil.
3422 A value of `(t)' indicates an empty environment, otherwise it is an
3423 alist of active lexical bindings. */);
3424 Vinternal_interpreter_environment
= Qnil
;
3425 /* Don't export this variable to Elisp, so no one can mess with it
3426 (Just imagine if someone makes it buffer-local). */
3427 Funintern (Qinternal_interpreter_environment
, Qnil
);
3429 DEFSYM (Vrun_hooks
, "run-hooks");
3431 staticpro (&Vautoload_queue
);
3432 Vautoload_queue
= Qnil
;
3433 staticpro (&Vsignaling_function
);
3434 Vsignaling_function
= Qnil
;
3436 inhibit_lisp_code
= Qnil
;
3447 defsubr (&Sfunction
);
3449 defsubr (&Sdefvaralias
);
3450 defsubr (&Sdefconst
);
3451 defsubr (&Smake_var_non_special
);
3455 defsubr (&Smacroexpand
);
3458 defsubr (&Sunwind_protect
);
3459 defsubr (&Scondition_case
);
3461 defsubr (&Scommandp
);
3462 defsubr (&Sautoload
);
3463 defsubr (&Sautoload_do_load
);
3466 defsubr (&Sfuncall
);
3467 defsubr (&Srun_hooks
);
3468 defsubr (&Srun_hook_with_args
);
3469 defsubr (&Srun_hook_with_args_until_success
);
3470 defsubr (&Srun_hook_with_args_until_failure
);
3471 defsubr (&Srun_hook_wrapped
);
3472 defsubr (&Sfetch_bytecode
);
3473 defsubr (&Sbacktrace_debug
);
3474 defsubr (&Sbacktrace
);
3475 defsubr (&Sbacktrace_frame
);
3476 defsubr (&Sspecial_variable_p
);
3477 defsubr (&Sfunctionp
);