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. */
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. */
49 struct handler
*handlerlist
;
52 /* Count levels of GCPRO to detect failure to UNGCPRO. */
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
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. */
81 ptrdiff_t specpdl_size
;
83 /* Pointer to beginning of specpdl. */
85 struct specbinding
*specpdl
;
87 /* Pointer to first unused element in specpdl. */
89 struct specbinding
*specpdl_ptr
;
91 /* Depth in Lisp evaluations and function calls. */
93 static EMACS_INT lisp_eval_depth
;
95 /* The value of num_nonmacro_input_events as of the last time we
96 started to enter the debugger. If we decide to enter the debugger
97 again when this is still equal to num_nonmacro_input_events, then we
98 know that the debugger itself has an error, and we should just
99 signal the error instead of entering an infinite loop of debugger
102 static EMACS_INT when_entered_debugger
;
104 /* The function from which the last `signal' was called. Set in
106 /* FIXME: We should probably get rid of this! */
107 Lisp_Object Vsignaling_function
;
109 /* If non-nil, Lisp code must not be run since some part of Emacs is
110 in an inconsistent state. Currently, x-create-frame uses this to
111 avoid triggering window-configuration-change-hook while the new
112 frame is half-initialized. */
113 Lisp_Object inhibit_lisp_code
;
115 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
116 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
118 /* Functions to modify slots of backtrace records. */
121 set_backtrace_args (struct specbinding
*pdl
, Lisp_Object
*args
)
122 { eassert (pdl
->kind
== SPECPDL_BACKTRACE
); pdl
->v
.bt
.args
= args
; }
125 set_backtrace_nargs (struct specbinding
*pdl
, ptrdiff_t n
)
126 { eassert (pdl
->kind
== SPECPDL_BACKTRACE
); pdl
->v
.bt
.nargs
= n
; }
129 set_backtrace_debug_on_exit (struct specbinding
*pdl
, bool doe
)
130 { eassert (pdl
->kind
== SPECPDL_BACKTRACE
); pdl
->v
.bt
.debug_on_exit
= doe
; }
132 /* Helper functions to scan the backtrace. */
134 bool backtrace_p (struct specbinding
*) EXTERNALLY_VISIBLE
;
135 struct specbinding
*backtrace_top (void) EXTERNALLY_VISIBLE
;
136 struct specbinding
*backtrace_next (struct specbinding
*pdl
) EXTERNALLY_VISIBLE
;
138 bool backtrace_p (struct specbinding
*pdl
)
139 { return pdl
>= specpdl
; }
144 struct specbinding
*pdl
= specpdl_ptr
- 1;
145 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
151 backtrace_next (struct specbinding
*pdl
)
154 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
161 init_eval_once (void)
164 specpdl
= xmalloc (size
* sizeof *specpdl
);
166 specpdl_ptr
= specpdl
;
167 /* Don't forget to update docs (lispref node "Local Variables"). */
168 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
169 max_lisp_eval_depth
= 600;
177 specpdl_ptr
= specpdl
;
181 debug_on_next_call
= 0;
186 /* This is less than the initial value of num_nonmacro_input_events. */
187 when_entered_debugger
= -1;
190 /* Unwind-protect function used by call_debugger. */
193 restore_stack_limits (Lisp_Object data
)
195 max_specpdl_size
= XINT (XCAR (data
));
196 max_lisp_eval_depth
= XINT (XCDR (data
));
200 /* Call the Lisp debugger, giving it argument ARG. */
203 call_debugger (Lisp_Object arg
)
205 bool debug_while_redisplaying
;
206 ptrdiff_t count
= SPECPDL_INDEX ();
208 EMACS_INT old_max
= max_specpdl_size
;
210 /* Temporarily bump up the stack limits,
211 so the debugger won't run out of stack. */
213 max_specpdl_size
+= 1;
214 record_unwind_protect (restore_stack_limits
,
215 Fcons (make_number (old_max
),
216 make_number (max_lisp_eval_depth
)));
217 max_specpdl_size
= old_max
;
219 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
220 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
222 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
223 max_specpdl_size
= SPECPDL_INDEX () + 100;
225 #ifdef HAVE_WINDOW_SYSTEM
226 if (display_hourglass_p
)
230 debug_on_next_call
= 0;
231 when_entered_debugger
= num_nonmacro_input_events
;
233 /* Resetting redisplaying_p to 0 makes sure that debug output is
234 displayed if the debugger is invoked during redisplay. */
235 debug_while_redisplaying
= redisplaying_p
;
237 specbind (intern ("debugger-may-continue"),
238 debug_while_redisplaying
? Qnil
: Qt
);
239 specbind (Qinhibit_redisplay
, Qnil
);
240 specbind (Qinhibit_debugger
, Qt
);
242 #if 0 /* Binding this prevents execution of Lisp code during
243 redisplay, which necessarily leads to display problems. */
244 specbind (Qinhibit_eval_during_redisplay
, Qt
);
247 val
= apply1 (Vdebugger
, arg
);
249 /* Interrupting redisplay and resuming it later is not safe under
250 all circumstances. So, when the debugger returns, abort the
251 interrupted redisplay by going back to the top-level. */
252 if (debug_while_redisplaying
)
255 return unbind_to (count
, val
);
259 do_debug_on_call (Lisp_Object code
)
261 debug_on_next_call
= 0;
262 set_backtrace_debug_on_exit (specpdl_ptr
- 1, true);
263 call_debugger (Fcons (code
, Qnil
));
266 /* NOTE!!! Every function that can call EVAL must protect its args
267 and temporaries from garbage collection while it needs them.
268 The definition of `For' shows what you have to do. */
270 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
271 doc
: /* Eval args until one of them yields non-nil, then return that value.
272 The remaining args are not evalled at all.
273 If all args return nil, return nil.
274 usage: (or CONDITIONS...) */)
277 register Lisp_Object val
= Qnil
;
284 val
= eval_sub (XCAR (args
));
294 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
295 doc
: /* Eval args until one of them yields nil, then return nil.
296 The remaining args are not evalled at all.
297 If no arg yields nil, return the last arg's value.
298 usage: (and CONDITIONS...) */)
301 register Lisp_Object val
= Qt
;
308 val
= eval_sub (XCAR (args
));
318 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
319 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
320 Returns the value of THEN or the value of the last of the ELSE's.
321 THEN must be one expression, but ELSE... can be zero or more expressions.
322 If COND yields nil, and there are no ELSE's, the value is nil.
323 usage: (if COND THEN ELSE...) */)
326 register Lisp_Object cond
;
330 cond
= eval_sub (Fcar (args
));
334 return eval_sub (Fcar (Fcdr (args
)));
335 return Fprogn (Fcdr (Fcdr (args
)));
338 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
339 doc
: /* Try each clause until one succeeds.
340 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
341 and, if the value is non-nil, this clause succeeds:
342 then the expressions in BODY are evaluated and the last one's
343 value is the value of the cond-form.
344 If no clause succeeds, cond returns nil.
345 If a clause has one element, as in (CONDITION),
346 CONDITION's value if non-nil is returned from the cond-form.
347 usage: (cond CLAUSES...) */)
350 register Lisp_Object clause
, val
;
357 clause
= Fcar (args
);
358 val
= eval_sub (Fcar (clause
));
361 if (!EQ (XCDR (clause
), Qnil
))
362 val
= Fprogn (XCDR (clause
));
372 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
373 doc
: /* Eval BODY forms sequentially and return value of last one.
374 usage: (progn BODY...) */)
377 register Lisp_Object val
= Qnil
;
384 val
= eval_sub (XCAR (args
));
392 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
393 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
394 The value of FIRST is saved during the evaluation of the remaining args,
395 whose values are discarded.
396 usage: (prog1 FIRST BODY...) */)
400 register Lisp_Object args_left
;
401 struct gcpro gcpro1
, gcpro2
;
407 val
= eval_sub (XCAR (args_left
));
408 while (CONSP (args_left
= XCDR (args_left
)))
409 eval_sub (XCAR (args_left
));
415 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
416 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
417 The value of FORM2 is saved during the evaluation of the
418 remaining args, whose values are discarded.
419 usage: (prog2 FORM1 FORM2 BODY...) */)
425 eval_sub (XCAR (args
));
427 return Fprog1 (XCDR (args
));
430 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
431 doc
: /* Set each SYM to the value of its VAL.
432 The symbols SYM are variables; they are literal (not evaluated).
433 The values VAL are expressions; they are evaluated.
434 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
435 The second VAL is not computed until after the first SYM is set, and so on;
436 each VAL can use the new value of variables set earlier in the `setq'.
437 The return value of the `setq' form is the value of the last VAL.
438 usage: (setq [SYM VAL]...) */)
441 register Lisp_Object args_left
;
442 register Lisp_Object val
, sym
, lex_binding
;
453 val
= eval_sub (Fcar (Fcdr (args_left
)));
454 sym
= Fcar (args_left
);
456 /* Like for eval_sub, we do not check declared_special here since
457 it's been done when let-binding. */
458 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
460 && !NILP (lex_binding
461 = Fassq (sym
, Vinternal_interpreter_environment
)))
462 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
464 Fset (sym
, val
); /* SYM is dynamically bound. */
466 args_left
= Fcdr (Fcdr (args_left
));
468 while (!NILP (args_left
));
474 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
475 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
476 Warning: `quote' does not construct its return value, but just returns
477 the value that was pre-constructed by the Lisp reader (see info node
478 `(elisp)Printed Representation').
479 This means that '(a . b) is not identical to (cons 'a 'b): the former
480 does not cons. Quoting should be reserved for constants that will
481 never be modified by side-effects, unless you like self-modifying code.
482 See the common pitfall in info node `(elisp)Rearrangement' for an example
483 of unexpected results when a quoted object is modified.
484 usage: (quote ARG) */)
487 if (!NILP (Fcdr (args
)))
488 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
492 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
493 doc
: /* Like `quote', but preferred for objects which are functions.
494 In byte compilation, `function' causes its argument to be compiled.
495 `quote' cannot do that.
496 usage: (function ARG) */)
499 Lisp_Object quoted
= XCAR (args
);
501 if (!NILP (Fcdr (args
)))
502 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
504 if (!NILP (Vinternal_interpreter_environment
)
506 && EQ (XCAR (quoted
), Qlambda
))
507 /* This is a lambda expression within a lexical environment;
508 return an interpreted closure instead of a simple lambda. */
509 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
512 /* Simply quote the argument. */
517 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
518 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
519 Aliased variables always have the same value; setting one sets the other.
520 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
521 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
522 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
523 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
524 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
525 The return value is BASE-VARIABLE. */)
526 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
528 struct Lisp_Symbol
*sym
;
530 CHECK_SYMBOL (new_alias
);
531 CHECK_SYMBOL (base_variable
);
533 sym
= XSYMBOL (new_alias
);
536 /* Not sure why, but why not? */
537 error ("Cannot make a constant an alias");
539 switch (sym
->redirect
)
541 case SYMBOL_FORWARDED
:
542 error ("Cannot make an internal variable an alias");
543 case SYMBOL_LOCALIZED
:
544 error ("Don't know how to make a localized variable an alias");
547 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
548 If n_a is bound, but b_v is not, set the value of b_v to n_a,
549 so that old-code that affects n_a before the aliasing is setup
551 if (NILP (Fboundp (base_variable
)))
552 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
555 struct specbinding
*p
;
557 for (p
= specpdl_ptr
; p
> specpdl
; )
558 if ((--p
)->kind
>= SPECPDL_LET
559 && (EQ (new_alias
, specpdl_symbol (p
))))
560 error ("Don't know how to make a let-bound variable an alias");
563 sym
->declared_special
= 1;
564 XSYMBOL (base_variable
)->declared_special
= 1;
565 sym
->redirect
= SYMBOL_VARALIAS
;
566 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
567 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
568 LOADHIST_ATTACH (new_alias
);
569 /* Even if docstring is nil: remove old docstring. */
570 Fput (new_alias
, Qvariable_documentation
, docstring
);
572 return base_variable
;
576 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
577 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
578 You are not required to define a variable in order to use it, but
579 defining it lets you supply an initial value and documentation, which
580 can be referred to by the Emacs help facilities and other programming
581 tools. The `defvar' form also declares the variable as \"special\",
582 so that it is always dynamically bound even if `lexical-binding' is t.
584 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
585 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
586 default value is what is set; buffer-local values are not affected.
587 If INITVALUE is missing, SYMBOL's value is not set.
589 If SYMBOL has a local binding, then this form affects the local
590 binding. This is usually not what you want. Thus, if you need to
591 load a file defining variables, with this form or with `defconst' or
592 `defcustom', you should always load that file _outside_ any bindings
593 for these variables. \(`defconst' and `defcustom' behave similarly in
596 The optional argument DOCSTRING is a documentation string for the
599 To define a user option, use `defcustom' instead of `defvar'.
600 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
603 register Lisp_Object sym
, tem
, tail
;
607 if (!NILP (Fcdr (Fcdr (tail
))))
608 error ("Too many arguments");
610 tem
= Fdefault_boundp (sym
);
613 /* Do it before evaluating the initial value, for self-references. */
614 XSYMBOL (sym
)->declared_special
= 1;
617 Fset_default (sym
, eval_sub (Fcar (tail
)));
619 { /* Check if there is really a global binding rather than just a let
620 binding that shadows the global unboundness of the var. */
621 struct specbinding
*pdl
= specpdl_ptr
;
622 while (pdl
> specpdl
)
624 if ((--pdl
)->kind
>= SPECPDL_LET
625 && EQ (specpdl_symbol (pdl
), sym
)
626 && EQ (specpdl_old_value (pdl
), Qunbound
))
629 ("Warning: defvar ignored because %s is let-bound",
630 SYMBOL_NAME (sym
), 1);
639 if (!NILP (Vpurify_flag
))
640 tem
= Fpurecopy (tem
);
641 Fput (sym
, Qvariable_documentation
, tem
);
643 LOADHIST_ATTACH (sym
);
645 else if (!NILP (Vinternal_interpreter_environment
)
646 && !XSYMBOL (sym
)->declared_special
)
647 /* A simple (defvar foo) with lexical scoping does "nothing" except
648 declare that var to be dynamically scoped *locally* (i.e. within
649 the current file or let-block). */
650 Vinternal_interpreter_environment
651 = Fcons (sym
, Vinternal_interpreter_environment
);
654 /* Simple (defvar <var>) should not count as a definition at all.
655 It could get in the way of other definitions, and unloading this
656 package could try to make the variable unbound. */
662 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
663 doc
: /* Define SYMBOL as a constant variable.
664 This declares that neither programs nor users should ever change the
665 value. This constancy is not actually enforced by Emacs Lisp, but
666 SYMBOL is marked as a special variable so that it is never lexically
669 The `defconst' form always sets the value of SYMBOL to the result of
670 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
671 what is set; buffer-local values are not affected. If SYMBOL has a
672 local binding, then this form sets the local binding's value.
673 However, you should normally not make local bindings for variables
674 defined with this form.
676 The optional DOCSTRING specifies the variable's documentation string.
677 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
680 register Lisp_Object sym
, tem
;
683 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
684 error ("Too many arguments");
686 tem
= eval_sub (Fcar (Fcdr (args
)));
687 if (!NILP (Vpurify_flag
))
688 tem
= Fpurecopy (tem
);
689 Fset_default (sym
, tem
);
690 XSYMBOL (sym
)->declared_special
= 1;
691 tem
= Fcar (Fcdr (Fcdr (args
)));
694 if (!NILP (Vpurify_flag
))
695 tem
= Fpurecopy (tem
);
696 Fput (sym
, Qvariable_documentation
, tem
);
698 Fput (sym
, Qrisky_local_variable
, Qt
);
699 LOADHIST_ATTACH (sym
);
703 /* Make SYMBOL lexically scoped. */
704 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
705 Smake_var_non_special
, 1, 1, 0,
706 doc
: /* Internal function. */)
709 CHECK_SYMBOL (symbol
);
710 XSYMBOL (symbol
)->declared_special
= 0;
715 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
716 doc
: /* Bind variables according to VARLIST then eval BODY.
717 The value of the last form in BODY is returned.
718 Each element of VARLIST is a symbol (which is bound to nil)
719 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
720 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
721 usage: (let* VARLIST BODY...) */)
724 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
725 ptrdiff_t count
= SPECPDL_INDEX ();
726 struct gcpro gcpro1
, gcpro2
, gcpro3
;
728 GCPRO3 (args
, elt
, varlist
);
730 lexenv
= Vinternal_interpreter_environment
;
732 varlist
= Fcar (args
);
733 while (CONSP (varlist
))
737 elt
= XCAR (varlist
);
743 else if (! NILP (Fcdr (Fcdr (elt
))))
744 signal_error ("`let' bindings can have only one value-form", elt
);
748 val
= eval_sub (Fcar (Fcdr (elt
)));
751 if (!NILP (lexenv
) && SYMBOLP (var
)
752 && !XSYMBOL (var
)->declared_special
753 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
754 /* Lexically bind VAR by adding it to the interpreter's binding
758 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
759 if (EQ (Vinternal_interpreter_environment
, lexenv
))
760 /* Save the old lexical environment on the specpdl stack,
761 but only for the first lexical binding, since we'll never
762 need to revert to one of the intermediate ones. */
763 specbind (Qinternal_interpreter_environment
, newenv
);
765 Vinternal_interpreter_environment
= newenv
;
770 varlist
= XCDR (varlist
);
773 val
= Fprogn (Fcdr (args
));
774 return unbind_to (count
, val
);
777 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
778 doc
: /* Bind variables according to VARLIST then eval BODY.
779 The value of the last form in BODY is returned.
780 Each element of VARLIST is a symbol (which is bound to nil)
781 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
782 All the VALUEFORMs are evalled before any symbols are bound.
783 usage: (let VARLIST BODY...) */)
786 Lisp_Object
*temps
, tem
, lexenv
;
787 register Lisp_Object elt
, varlist
;
788 ptrdiff_t count
= SPECPDL_INDEX ();
790 struct gcpro gcpro1
, gcpro2
;
793 varlist
= Fcar (args
);
795 /* Make space to hold the values to give the bound variables. */
796 elt
= Flength (varlist
);
797 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
799 /* Compute the values and store them in `temps'. */
801 GCPRO2 (args
, *temps
);
804 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
807 elt
= XCAR (varlist
);
809 temps
[argnum
++] = Qnil
;
810 else if (! NILP (Fcdr (Fcdr (elt
))))
811 signal_error ("`let' bindings can have only one value-form", elt
);
813 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
814 gcpro2
.nvars
= argnum
;
818 lexenv
= Vinternal_interpreter_environment
;
820 varlist
= Fcar (args
);
821 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
825 elt
= XCAR (varlist
);
826 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
827 tem
= temps
[argnum
++];
829 if (!NILP (lexenv
) && SYMBOLP (var
)
830 && !XSYMBOL (var
)->declared_special
831 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
832 /* Lexically bind VAR by adding it to the lexenv alist. */
833 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
835 /* Dynamically bind VAR. */
839 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
840 /* Instantiate a new lexical environment. */
841 specbind (Qinternal_interpreter_environment
, lexenv
);
843 elt
= Fprogn (Fcdr (args
));
845 return unbind_to (count
, elt
);
848 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
849 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
850 The order of execution is thus TEST, BODY, TEST, BODY and so on
851 until TEST returns nil.
852 usage: (while TEST BODY...) */)
855 Lisp_Object test
, body
;
856 struct gcpro gcpro1
, gcpro2
;
862 while (!NILP (eval_sub (test
)))
872 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
873 doc
: /* Return result of expanding macros at top level of FORM.
874 If FORM is not a macro call, it is returned unchanged.
875 Otherwise, the macro is expanded and the expansion is considered
876 in place of FORM. When a non-macro-call results, it is returned.
878 The second optional arg ENVIRONMENT specifies an environment of macro
879 definitions to shadow the loaded ones for use in file byte-compilation. */)
880 (Lisp_Object form
, Lisp_Object environment
)
882 /* With cleanups from Hallvard Furuseth. */
883 register Lisp_Object expander
, sym
, def
, tem
;
887 /* Come back here each time we expand a macro call,
888 in case it expands into another macro call. */
891 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
892 def
= sym
= XCAR (form
);
894 /* Trace symbols aliases to other symbols
895 until we get a symbol that is not an alias. */
896 while (SYMBOLP (def
))
900 tem
= Fassq (sym
, environment
);
903 def
= XSYMBOL (sym
)->function
;
909 /* Right now TEM is the result from SYM in ENVIRONMENT,
910 and if TEM is nil then DEF is SYM's function definition. */
913 /* SYM is not mentioned in ENVIRONMENT.
914 Look at its function definition. */
917 def
= Fautoload_do_load (def
, sym
, Qmacro
);
920 /* Not defined or definition not suitable. */
922 if (!EQ (XCAR (def
), Qmacro
))
924 else expander
= XCDR (def
);
928 expander
= XCDR (tem
);
933 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
934 if (EQ (form
, newform
))
943 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
944 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
945 TAG is evalled to get the tag to use; it must not be nil.
947 Then the BODY is executed.
948 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
949 If no throw happens, `catch' returns the value of the last BODY form.
950 If a throw happens, it specifies the value to return from `catch'.
951 usage: (catch TAG BODY...) */)
954 register Lisp_Object tag
;
958 tag
= eval_sub (Fcar (args
));
960 return internal_catch (tag
, Fprogn
, Fcdr (args
));
963 /* Set up a catch, then call C function FUNC on argument ARG.
964 FUNC should return a Lisp_Object.
965 This is how catches are done from within C code. */
968 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
970 /* This structure is made part of the chain `catchlist'. */
973 /* Fill in the components of c, and put it on the list. */
977 c
.handlerlist
= handlerlist
;
978 c
.lisp_eval_depth
= lisp_eval_depth
;
979 c
.pdlcount
= SPECPDL_INDEX ();
980 c
.poll_suppress_count
= poll_suppress_count
;
981 c
.interrupt_input_blocked
= interrupt_input_blocked
;
983 c
.byte_stack
= byte_stack_list
;
987 if (! sys_setjmp (c
.jmp
))
988 c
.val
= (*func
) (arg
);
990 /* Throw works by a longjmp that comes right here. */
995 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
996 jump to that CATCH, returning VALUE as the value of that catch.
998 This is the guts of Fthrow and Fsignal; they differ only in the way
999 they choose the catch tag to throw to. A catch tag for a
1000 condition-case form has a TAG of Qnil.
1002 Before each catch is discarded, unbind all special bindings and
1003 execute all unwind-protect clauses made above that catch. Unwind
1004 the handler stack as we go, so that the proper handlers are in
1005 effect for each unwind-protect clause we run. At the end, restore
1006 some static info saved in CATCH, and longjmp to the location
1009 This is used for correct unwinding in Fthrow and Fsignal. */
1011 static _Noreturn
void
1012 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1016 /* Save the value in the tag. */
1019 /* Restore certain special C variables. */
1020 set_poll_suppress_count (catch->poll_suppress_count
);
1021 unblock_input_to (catch->interrupt_input_blocked
);
1026 last_time
= catchlist
== catch;
1028 /* Unwind the specpdl stack, and then restore the proper set of
1030 unbind_to (catchlist
->pdlcount
, Qnil
);
1031 handlerlist
= catchlist
->handlerlist
;
1032 catchlist
= catchlist
->next
;
1034 while (! last_time
);
1036 byte_stack_list
= catch->byte_stack
;
1037 gcprolist
= catch->gcpro
;
1039 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1041 lisp_eval_depth
= catch->lisp_eval_depth
;
1043 sys_longjmp (catch->jmp
, 1);
1046 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1047 doc
: /* Throw to the catch for TAG and return VALUE from it.
1048 Both TAG and VALUE are evalled. */)
1049 (register Lisp_Object tag
, Lisp_Object value
)
1051 register struct catchtag
*c
;
1054 for (c
= catchlist
; c
; c
= c
->next
)
1056 if (EQ (c
->tag
, tag
))
1057 unwind_to_catch (c
, value
);
1059 xsignal2 (Qno_catch
, tag
, value
);
1063 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1064 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1065 If BODYFORM completes normally, its value is returned
1066 after executing the UNWINDFORMS.
1067 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1068 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1072 ptrdiff_t count
= SPECPDL_INDEX ();
1074 record_unwind_protect (Fprogn
, Fcdr (args
));
1075 val
= eval_sub (Fcar (args
));
1076 return unbind_to (count
, val
);
1079 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1080 doc
: /* Regain control when an error is signaled.
1081 Executes BODYFORM and returns its value if no error happens.
1082 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1083 where the BODY is made of Lisp expressions.
1085 A handler is applicable to an error
1086 if CONDITION-NAME is one of the error's condition names.
1087 If an error happens, the first applicable handler is run.
1089 The car of a handler may be a list of condition names instead of a
1090 single condition name; then it handles all of them. If the special
1091 condition name `debug' is present in this list, it allows another
1092 condition in the list to run the debugger if `debug-on-error' and the
1093 other usual mechanisms says it should (otherwise, `condition-case'
1094 suppresses the debugger).
1096 When a handler handles an error, control returns to the `condition-case'
1097 and it executes the handler's BODY...
1098 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1099 \(If VAR is nil, the handler can't access that information.)
1100 Then the value of the last BODY form is returned from the `condition-case'
1103 See also the function `signal' for more info.
1104 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1107 Lisp_Object var
= Fcar (args
);
1108 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1109 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1111 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1114 /* Like Fcondition_case, but the args are separate
1115 rather than passed in a list. Used by Fbyte_code. */
1118 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1119 Lisp_Object handlers
)
1127 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1133 && (SYMBOLP (XCAR (tem
))
1134 || CONSP (XCAR (tem
))))))
1135 error ("Invalid condition handler: %s",
1136 SDATA (Fprin1_to_string (tem
, Qt
)));
1141 c
.handlerlist
= handlerlist
;
1142 c
.lisp_eval_depth
= lisp_eval_depth
;
1143 c
.pdlcount
= SPECPDL_INDEX ();
1144 c
.poll_suppress_count
= poll_suppress_count
;
1145 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1146 c
.gcpro
= gcprolist
;
1147 c
.byte_stack
= byte_stack_list
;
1148 if (sys_setjmp (c
.jmp
))
1151 specbind (h
.var
, c
.val
);
1152 val
= Fprogn (Fcdr (h
.chosen_clause
));
1154 /* Note that this just undoes the binding of h.var; whoever
1155 longjumped to us unwound the stack to c.pdlcount before
1157 unbind_to (c
.pdlcount
, Qnil
);
1164 h
.handler
= handlers
;
1165 h
.next
= handlerlist
;
1169 val
= eval_sub (bodyform
);
1171 handlerlist
= h
.next
;
1175 /* Call the function BFUN with no arguments, catching errors within it
1176 according to HANDLERS. If there is an error, call HFUN with
1177 one argument which is the data that describes the error:
1180 HANDLERS can be a list of conditions to catch.
1181 If HANDLERS is Qt, catch all errors.
1182 If HANDLERS is Qerror, catch all errors
1183 but allow the debugger to run if that is enabled. */
1186 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1187 Lisp_Object (*hfun
) (Lisp_Object
))
1195 c
.handlerlist
= handlerlist
;
1196 c
.lisp_eval_depth
= lisp_eval_depth
;
1197 c
.pdlcount
= SPECPDL_INDEX ();
1198 c
.poll_suppress_count
= poll_suppress_count
;
1199 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1200 c
.gcpro
= gcprolist
;
1201 c
.byte_stack
= byte_stack_list
;
1202 if (sys_setjmp (c
.jmp
))
1204 return (*hfun
) (c
.val
);
1208 h
.handler
= handlers
;
1210 h
.next
= handlerlist
;
1216 handlerlist
= h
.next
;
1220 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1223 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1224 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1232 c
.handlerlist
= handlerlist
;
1233 c
.lisp_eval_depth
= lisp_eval_depth
;
1234 c
.pdlcount
= SPECPDL_INDEX ();
1235 c
.poll_suppress_count
= poll_suppress_count
;
1236 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1237 c
.gcpro
= gcprolist
;
1238 c
.byte_stack
= byte_stack_list
;
1239 if (sys_setjmp (c
.jmp
))
1241 return (*hfun
) (c
.val
);
1245 h
.handler
= handlers
;
1247 h
.next
= handlerlist
;
1251 val
= (*bfun
) (arg
);
1253 handlerlist
= h
.next
;
1257 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1261 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1264 Lisp_Object handlers
,
1265 Lisp_Object (*hfun
) (Lisp_Object
))
1273 c
.handlerlist
= handlerlist
;
1274 c
.lisp_eval_depth
= lisp_eval_depth
;
1275 c
.pdlcount
= SPECPDL_INDEX ();
1276 c
.poll_suppress_count
= poll_suppress_count
;
1277 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1278 c
.gcpro
= gcprolist
;
1279 c
.byte_stack
= byte_stack_list
;
1280 if (sys_setjmp (c
.jmp
))
1282 return (*hfun
) (c
.val
);
1286 h
.handler
= handlers
;
1288 h
.next
= handlerlist
;
1292 val
= (*bfun
) (arg1
, arg2
);
1294 handlerlist
= h
.next
;
1298 /* Like internal_condition_case but call BFUN with NARGS as first,
1299 and ARGS as second argument. */
1302 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1305 Lisp_Object handlers
,
1306 Lisp_Object (*hfun
) (Lisp_Object err
,
1316 c
.handlerlist
= handlerlist
;
1317 c
.lisp_eval_depth
= lisp_eval_depth
;
1318 c
.pdlcount
= SPECPDL_INDEX ();
1319 c
.poll_suppress_count
= poll_suppress_count
;
1320 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1321 c
.gcpro
= gcprolist
;
1322 c
.byte_stack
= byte_stack_list
;
1323 if (sys_setjmp (c
.jmp
))
1325 return (*hfun
) (c
.val
, nargs
, args
);
1329 h
.handler
= handlers
;
1331 h
.next
= handlerlist
;
1335 val
= (*bfun
) (nargs
, args
);
1337 handlerlist
= h
.next
;
1342 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1343 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1347 process_quit_flag (void)
1349 Lisp_Object flag
= Vquit_flag
;
1351 if (EQ (flag
, Qkill_emacs
))
1353 if (EQ (Vthrow_on_input
, flag
))
1354 Fthrow (Vthrow_on_input
, Qt
);
1355 Fsignal (Qquit
, Qnil
);
1358 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1359 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1360 This function does not return.
1362 An error symbol is a symbol with an `error-conditions' property
1363 that is a list of condition names.
1364 A handler for any of those names will get to handle this signal.
1365 The symbol `error' should normally be one of them.
1367 DATA should be a list. Its elements are printed as part of the error message.
1368 See Info anchor `(elisp)Definition of signal' for some details on how this
1369 error message is constructed.
1370 If the signal is handled, DATA is made available to the handler.
1371 See also the function `condition-case'. */)
1372 (Lisp_Object error_symbol
, Lisp_Object data
)
1374 /* When memory is full, ERROR-SYMBOL is nil,
1375 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1376 That is a special case--don't do this in other situations. */
1377 Lisp_Object conditions
;
1379 Lisp_Object real_error_symbol
1380 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1381 register Lisp_Object clause
= Qnil
;
1386 if (gc_in_progress
|| waiting_for_input
)
1389 #if 0 /* rms: I don't know why this was here,
1390 but it is surely wrong for an error that is handled. */
1391 #ifdef HAVE_WINDOW_SYSTEM
1392 if (display_hourglass_p
)
1393 cancel_hourglass ();
1397 /* This hook is used by edebug. */
1398 if (! NILP (Vsignal_hook_function
)
1399 && ! NILP (error_symbol
))
1401 /* Edebug takes care of restoring these variables when it exits. */
1402 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1403 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1405 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1406 max_specpdl_size
= SPECPDL_INDEX () + 40;
1408 call2 (Vsignal_hook_function
, error_symbol
, data
);
1411 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1413 /* Remember from where signal was called. Skip over the frame for
1414 `signal' itself. If a frame for `error' follows, skip that,
1415 too. Don't do this when ERROR_SYMBOL is nil, because that
1416 is a memory-full error. */
1417 Vsignaling_function
= Qnil
;
1418 if (!NILP (error_symbol
))
1420 struct specbinding
*pdl
= backtrace_next (backtrace_top ());
1421 if (backtrace_p (pdl
) && EQ (backtrace_function (pdl
), Qerror
))
1422 pdl
= backtrace_next (pdl
);
1423 if (backtrace_p (pdl
))
1424 Vsignaling_function
= backtrace_function (pdl
);
1427 for (h
= handlerlist
; h
; h
= h
->next
)
1429 clause
= find_handler_clause (h
->handler
, conditions
);
1434 if (/* Don't run the debugger for a memory-full error.
1435 (There is no room in memory to do that!) */
1436 !NILP (error_symbol
)
1437 && (!NILP (Vdebug_on_signal
)
1438 /* If no handler is present now, try to run the debugger. */
1440 /* A `debug' symbol in the handler list disables the normal
1441 suppression of the debugger. */
1442 || (CONSP (clause
) && CONSP (XCAR (clause
))
1443 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1444 /* Special handler that means "print a message and run debugger
1446 || EQ (h
->handler
, Qerror
)))
1448 bool debugger_called
1449 = maybe_call_debugger (conditions
, error_symbol
, data
);
1450 /* We can't return values to code which signaled an error, but we
1451 can continue code which has signaled a quit. */
1452 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1458 Lisp_Object unwind_data
1459 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1461 h
->chosen_clause
= clause
;
1462 unwind_to_catch (h
->tag
, unwind_data
);
1467 Fthrow (Qtop_level
, Qt
);
1470 if (! NILP (error_symbol
))
1471 data
= Fcons (error_symbol
, data
);
1473 string
= Ferror_message_string (data
);
1474 fatal ("%s", SDATA (string
));
1477 /* Internal version of Fsignal that never returns.
1478 Used for anything but Qquit (which can return from Fsignal). */
1481 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1483 Fsignal (error_symbol
, data
);
1487 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1490 xsignal0 (Lisp_Object error_symbol
)
1492 xsignal (error_symbol
, Qnil
);
1496 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1498 xsignal (error_symbol
, list1 (arg
));
1502 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1504 xsignal (error_symbol
, list2 (arg1
, arg2
));
1508 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1510 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1513 /* Signal `error' with message S, and additional arg ARG.
1514 If ARG is not a genuine list, make it a one-element list. */
1517 signal_error (const char *s
, Lisp_Object arg
)
1519 Lisp_Object tortoise
, hare
;
1521 hare
= tortoise
= arg
;
1522 while (CONSP (hare
))
1529 tortoise
= XCDR (tortoise
);
1531 if (EQ (hare
, tortoise
))
1536 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1538 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1542 /* Return true if LIST is a non-nil atom or
1543 a list containing one of CONDITIONS. */
1546 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1553 while (CONSP (conditions
))
1555 Lisp_Object
this, tail
;
1556 this = XCAR (conditions
);
1557 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1558 if (EQ (XCAR (tail
), this))
1560 conditions
= XCDR (conditions
);
1565 /* Return true if an error with condition-symbols CONDITIONS,
1566 and described by SIGNAL-DATA, should skip the debugger
1567 according to debugger-ignored-errors. */
1570 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1573 bool first_string
= 1;
1574 Lisp_Object error_message
;
1576 error_message
= Qnil
;
1577 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1579 if (STRINGP (XCAR (tail
)))
1583 error_message
= Ferror_message_string (data
);
1587 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1592 Lisp_Object contail
;
1594 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1595 if (EQ (XCAR (tail
), XCAR (contail
)))
1603 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1604 SIG and DATA describe the signal. There are two ways to pass them:
1605 = SIG is the error symbol, and DATA is the rest of the data.
1606 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1607 This is for memory-full errors only. */
1609 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1611 Lisp_Object combined_data
;
1613 combined_data
= Fcons (sig
, data
);
1616 /* Don't try to run the debugger with interrupts blocked.
1617 The editing loop would return anyway. */
1618 ! input_blocked_p ()
1619 && NILP (Vinhibit_debugger
)
1620 /* Does user want to enter debugger for this kind of error? */
1623 : wants_debugger (Vdebug_on_error
, conditions
))
1624 && ! skip_debugger (conditions
, combined_data
)
1625 /* RMS: What's this for? */
1626 && when_entered_debugger
< num_nonmacro_input_events
)
1628 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1636 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1638 register Lisp_Object h
;
1640 /* t is used by handlers for all conditions, set up by C code. */
1641 if (EQ (handlers
, Qt
))
1644 /* error is used similarly, but means print an error message
1645 and run the debugger if that is enabled. */
1646 if (EQ (handlers
, Qerror
))
1649 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1651 Lisp_Object handler
= XCAR (h
);
1652 Lisp_Object condit
, tem
;
1654 if (!CONSP (handler
))
1656 condit
= XCAR (handler
);
1657 /* Handle a single condition name in handler HANDLER. */
1658 if (SYMBOLP (condit
))
1660 tem
= Fmemq (Fcar (handler
), conditions
);
1664 /* Handle a list of condition names in handler HANDLER. */
1665 else if (CONSP (condit
))
1668 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1670 tem
= Fmemq (XCAR (tail
), conditions
);
1681 /* Dump an error message; called like vprintf. */
1683 verror (const char *m
, va_list ap
)
1686 ptrdiff_t size
= sizeof buf
;
1687 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1692 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1693 string
= make_string (buffer
, used
);
1697 xsignal1 (Qerror
, string
);
1701 /* Dump an error message; called like printf. */
1705 error (const char *m
, ...)
1712 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1713 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1714 This means it contains a description for how to read arguments to give it.
1715 The value is nil for an invalid function or a symbol with no function
1718 Interactively callable functions include strings and vectors (treated
1719 as keyboard macros), lambda-expressions that contain a top-level call
1720 to `interactive', autoload definitions made by `autoload' with non-nil
1721 fourth argument, and some of the built-in functions of Lisp.
1723 Also, a symbol satisfies `commandp' if its function definition does so.
1725 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1726 then strings and vectors are not accepted. */)
1727 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1729 register Lisp_Object fun
;
1730 register Lisp_Object funcar
;
1731 Lisp_Object if_prop
= Qnil
;
1735 fun
= indirect_function (fun
); /* Check cycles. */
1739 /* Check an `interactive-form' property if present, analogous to the
1740 function-documentation property. */
1742 while (SYMBOLP (fun
))
1744 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1747 fun
= Fsymbol_function (fun
);
1750 /* Emacs primitives are interactive if their DEFUN specifies an
1751 interactive spec. */
1753 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1755 /* Bytecode objects are interactive if they are long enough to
1756 have an element whose index is COMPILED_INTERACTIVE, which is
1757 where the interactive spec is stored. */
1758 else if (COMPILEDP (fun
))
1759 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1762 /* Strings and vectors are keyboard macros. */
1763 if (STRINGP (fun
) || VECTORP (fun
))
1764 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1766 /* Lists may represent commands. */
1769 funcar
= XCAR (fun
);
1770 if (EQ (funcar
, Qclosure
))
1771 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1773 else if (EQ (funcar
, Qlambda
))
1774 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1775 else if (EQ (funcar
, Qautoload
))
1776 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1781 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1782 doc
: /* Define FUNCTION to autoload from FILE.
1783 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1784 Third arg DOCSTRING is documentation for the function.
1785 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1786 Fifth arg TYPE indicates the type of the object:
1787 nil or omitted says FUNCTION is a function,
1788 `keymap' says FUNCTION is really a keymap, and
1789 `macro' or t says FUNCTION is really a macro.
1790 Third through fifth args give info about the real definition.
1791 They default to nil.
1792 If FUNCTION is already defined other than as an autoload,
1793 this does nothing and returns nil. */)
1794 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1796 CHECK_SYMBOL (function
);
1797 CHECK_STRING (file
);
1799 /* If function is defined and not as an autoload, don't override. */
1800 if (!NILP (XSYMBOL (function
)->function
)
1801 && !AUTOLOADP (XSYMBOL (function
)->function
))
1804 if (!NILP (Vpurify_flag
) && EQ (docstring
, make_number (0)))
1805 /* `read1' in lread.c has found the docstring starting with "\
1806 and assumed the docstring will be provided by Snarf-documentation, so it
1807 passed us 0 instead. But that leads to accidental sharing in purecopy's
1808 hash-consing, so we use a (hopefully) unique integer instead. */
1809 docstring
= make_number (XHASH (function
));
1810 return Fdefalias (function
,
1811 list5 (Qautoload
, file
, docstring
, interactive
, type
),
1816 un_autoload (Lisp_Object oldqueue
)
1818 register Lisp_Object queue
, first
, second
;
1820 /* Queue to unwind is current value of Vautoload_queue.
1821 oldqueue is the shadowed value to leave in Vautoload_queue. */
1822 queue
= Vautoload_queue
;
1823 Vautoload_queue
= oldqueue
;
1824 while (CONSP (queue
))
1826 first
= XCAR (queue
);
1827 second
= Fcdr (first
);
1828 first
= Fcar (first
);
1829 if (EQ (first
, make_number (0)))
1832 Ffset (first
, second
);
1833 queue
= XCDR (queue
);
1838 /* Load an autoloaded function.
1839 FUNNAME is the symbol which is the function's name.
1840 FUNDEF is the autoload definition (a list). */
1842 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1843 doc
: /* Load FUNDEF which should be an autoload.
1844 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1845 in which case the function returns the new autoloaded function value.
1846 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1847 it is defines a macro. */)
1848 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1850 ptrdiff_t count
= SPECPDL_INDEX ();
1851 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1853 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1856 if (EQ (macro_only
, Qmacro
))
1858 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1859 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1863 /* This is to make sure that loadup.el gives a clear picture
1864 of what files are preloaded and when. */
1865 if (! NILP (Vpurify_flag
))
1866 error ("Attempt to autoload %s while preparing to dump",
1867 SDATA (SYMBOL_NAME (funname
)));
1869 CHECK_SYMBOL (funname
);
1870 GCPRO3 (funname
, fundef
, macro_only
);
1872 /* Preserve the match data. */
1873 record_unwind_save_match_data ();
1875 /* If autoloading gets an error (which includes the error of failing
1876 to define the function being called), we use Vautoload_queue
1877 to undo function definitions and `provide' calls made by
1878 the function. We do this in the specific case of autoloading
1879 because autoloading is not an explicit request "load this file",
1880 but rather a request to "call this function".
1882 The value saved here is to be restored into Vautoload_queue. */
1883 record_unwind_protect (un_autoload
, Vautoload_queue
);
1884 Vautoload_queue
= Qt
;
1885 /* If `macro_only', assume this autoload to be a "best-effort",
1886 so don't signal an error if autoloading fails. */
1887 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
1889 /* Once loading finishes, don't undo it. */
1890 Vautoload_queue
= Qt
;
1891 unbind_to (count
, Qnil
);
1899 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
1901 if (!NILP (Fequal (fun
, fundef
)))
1902 error ("Autoloading failed to define function %s",
1903 SDATA (SYMBOL_NAME (funname
)));
1910 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
1911 doc
: /* Evaluate FORM and return its value.
1912 If LEXICAL is t, evaluate using lexical scoping. */)
1913 (Lisp_Object form
, Lisp_Object lexical
)
1915 ptrdiff_t count
= SPECPDL_INDEX ();
1916 specbind (Qinternal_interpreter_environment
,
1917 CONSP (lexical
) || NILP (lexical
) ? lexical
: Fcons (Qt
, Qnil
));
1918 return unbind_to (count
, eval_sub (form
));
1924 register ptrdiff_t count
= SPECPDL_INDEX ();
1925 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
1926 if (max_size
<= specpdl_size
)
1928 if (max_specpdl_size
< 400)
1929 max_size
= max_specpdl_size
= 400;
1930 if (max_size
<= specpdl_size
)
1931 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
1933 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
1934 specpdl_ptr
= specpdl
+ count
;
1938 record_in_backtrace (Lisp_Object function
, Lisp_Object
*args
, ptrdiff_t nargs
)
1940 eassert (nargs
>= UNEVALLED
);
1941 if (specpdl_ptr
== specpdl
+ specpdl_size
)
1943 specpdl_ptr
->kind
= SPECPDL_BACKTRACE
;
1944 specpdl_ptr
->v
.bt
.function
= function
;
1945 specpdl_ptr
->v
.bt
.args
= args
;
1946 specpdl_ptr
->v
.bt
.nargs
= nargs
;
1947 specpdl_ptr
->v
.bt
.debug_on_exit
= false;
1951 /* Eval a sub-expression of the current expression (i.e. in the same
1954 eval_sub (Lisp_Object form
)
1956 Lisp_Object fun
, val
, original_fun
, original_args
;
1958 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1962 /* Look up its binding in the lexical environment.
1963 We do not pay attention to the declared_special flag here, since we
1964 already did that when let-binding the variable. */
1965 Lisp_Object lex_binding
1966 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
1967 ? Fassq (form
, Vinternal_interpreter_environment
)
1969 if (CONSP (lex_binding
))
1970 return XCDR (lex_binding
);
1972 return Fsymbol_value (form
);
1984 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1986 if (max_lisp_eval_depth
< 100)
1987 max_lisp_eval_depth
= 100;
1988 if (lisp_eval_depth
> max_lisp_eval_depth
)
1989 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
1992 original_fun
= XCAR (form
);
1993 original_args
= XCDR (form
);
1995 /* This also protects them from gc. */
1996 record_in_backtrace (original_fun
, &original_args
, UNEVALLED
);
1998 if (debug_on_next_call
)
1999 do_debug_on_call (Qt
);
2001 /* At this point, only original_fun and original_args
2002 have values that will be used below. */
2005 /* Optimize for no indirection. */
2007 if (SYMBOLP (fun
) && !NILP (fun
)
2008 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2009 fun
= indirect_function (fun
);
2013 Lisp_Object numargs
;
2014 Lisp_Object argvals
[8];
2015 Lisp_Object args_left
;
2016 register int i
, maxargs
;
2018 args_left
= original_args
;
2019 numargs
= Flength (args_left
);
2023 if (XINT (numargs
) < XSUBR (fun
)->min_args
2024 || (XSUBR (fun
)->max_args
>= 0
2025 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2026 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2028 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2029 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2030 else if (XSUBR (fun
)->max_args
== MANY
)
2032 /* Pass a vector of evaluated arguments. */
2034 ptrdiff_t argnum
= 0;
2037 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2039 GCPRO3 (args_left
, fun
, fun
);
2043 while (!NILP (args_left
))
2045 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2046 args_left
= Fcdr (args_left
);
2047 gcpro3
.nvars
= argnum
;
2050 set_backtrace_args (specpdl_ptr
- 1, vals
);
2051 set_backtrace_nargs (specpdl_ptr
- 1, XINT (numargs
));
2053 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2059 GCPRO3 (args_left
, fun
, fun
);
2060 gcpro3
.var
= argvals
;
2063 maxargs
= XSUBR (fun
)->max_args
;
2064 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2066 argvals
[i
] = eval_sub (Fcar (args_left
));
2072 set_backtrace_args (specpdl_ptr
- 1, argvals
);
2073 set_backtrace_nargs (specpdl_ptr
- 1, XINT (numargs
));
2078 val
= (XSUBR (fun
)->function
.a0 ());
2081 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2084 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2087 val
= (XSUBR (fun
)->function
.a3
2088 (argvals
[0], argvals
[1], argvals
[2]));
2091 val
= (XSUBR (fun
)->function
.a4
2092 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2095 val
= (XSUBR (fun
)->function
.a5
2096 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2100 val
= (XSUBR (fun
)->function
.a6
2101 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2102 argvals
[4], argvals
[5]));
2105 val
= (XSUBR (fun
)->function
.a7
2106 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2107 argvals
[4], argvals
[5], argvals
[6]));
2111 val
= (XSUBR (fun
)->function
.a8
2112 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2113 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2117 /* Someone has created a subr that takes more arguments than
2118 is supported by this code. We need to either rewrite the
2119 subr to use a different argument protocol, or add more
2120 cases to this switch. */
2125 else if (COMPILEDP (fun
))
2126 val
= apply_lambda (fun
, original_args
);
2130 xsignal1 (Qvoid_function
, original_fun
);
2132 xsignal1 (Qinvalid_function
, original_fun
);
2133 funcar
= XCAR (fun
);
2134 if (!SYMBOLP (funcar
))
2135 xsignal1 (Qinvalid_function
, original_fun
);
2136 if (EQ (funcar
, Qautoload
))
2138 Fautoload_do_load (fun
, original_fun
, Qnil
);
2141 if (EQ (funcar
, Qmacro
))
2143 ptrdiff_t count
= SPECPDL_INDEX ();
2145 /* Bind lexical-binding during expansion of the macro, so the
2146 macro can know reliably if the code it outputs will be
2147 interpreted using lexical-binding or not. */
2148 specbind (Qlexical_binding
,
2149 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2150 exp
= apply1 (Fcdr (fun
), original_args
);
2151 unbind_to (count
, Qnil
);
2152 val
= eval_sub (exp
);
2154 else if (EQ (funcar
, Qlambda
)
2155 || EQ (funcar
, Qclosure
))
2156 val
= apply_lambda (fun
, original_args
);
2158 xsignal1 (Qinvalid_function
, original_fun
);
2163 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2164 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2170 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2171 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2172 Then return the value FUNCTION returns.
2173 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2174 usage: (apply FUNCTION &rest ARGUMENTS) */)
2175 (ptrdiff_t nargs
, Lisp_Object
*args
)
2179 register Lisp_Object spread_arg
;
2180 register Lisp_Object
*funcall_args
;
2181 Lisp_Object fun
, retval
;
2182 struct gcpro gcpro1
;
2187 spread_arg
= args
[nargs
- 1];
2188 CHECK_LIST (spread_arg
);
2190 numargs
= XINT (Flength (spread_arg
));
2193 return Ffuncall (nargs
- 1, args
);
2194 else if (numargs
== 1)
2196 args
[nargs
- 1] = XCAR (spread_arg
);
2197 return Ffuncall (nargs
, args
);
2200 numargs
+= nargs
- 2;
2202 /* Optimize for no indirection. */
2203 if (SYMBOLP (fun
) && !NILP (fun
)
2204 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2205 fun
= indirect_function (fun
);
2208 /* Let funcall get the error. */
2215 if (numargs
< XSUBR (fun
)->min_args
2216 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2217 goto funcall
; /* Let funcall get the error. */
2218 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2220 /* Avoid making funcall cons up a yet another new vector of arguments
2221 by explicitly supplying nil's for optional values. */
2222 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2223 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2224 funcall_args
[++i
] = Qnil
;
2225 GCPRO1 (*funcall_args
);
2226 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2230 /* We add 1 to numargs because funcall_args includes the
2231 function itself as well as its arguments. */
2234 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2235 GCPRO1 (*funcall_args
);
2236 gcpro1
.nvars
= 1 + numargs
;
2239 memcpy (funcall_args
, args
, nargs
* word_size
);
2240 /* Spread the last arg we got. Its first element goes in
2241 the slot that it used to occupy, hence this value of I. */
2243 while (!NILP (spread_arg
))
2245 funcall_args
[i
++] = XCAR (spread_arg
);
2246 spread_arg
= XCDR (spread_arg
);
2249 /* By convention, the caller needs to gcpro Ffuncall's args. */
2250 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2257 /* Run hook variables in various ways. */
2260 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2262 Ffuncall (nargs
, args
);
2266 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2267 doc
: /* Run each hook in HOOKS.
2268 Each argument should be a symbol, a hook variable.
2269 These symbols are processed in the order specified.
2270 If a hook symbol has a non-nil value, that value may be a function
2271 or a list of functions to be called to run the hook.
2272 If the value is a function, it is called with no arguments.
2273 If it is a list, the elements are called, in order, with no arguments.
2275 Major modes should not use this function directly to run their mode
2276 hook; they should use `run-mode-hooks' instead.
2278 Do not use `make-local-variable' to make a hook variable buffer-local.
2279 Instead, use `add-hook' and specify t for the LOCAL argument.
2280 usage: (run-hooks &rest HOOKS) */)
2281 (ptrdiff_t nargs
, Lisp_Object
*args
)
2283 Lisp_Object hook
[1];
2286 for (i
= 0; i
< nargs
; i
++)
2289 run_hook_with_args (1, hook
, funcall_nil
);
2295 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2296 Srun_hook_with_args
, 1, MANY
, 0,
2297 doc
: /* Run HOOK with the specified arguments ARGS.
2298 HOOK should be a symbol, a hook variable. The value of HOOK
2299 may be nil, a function, or a list of functions. Call each
2300 function in order with arguments ARGS. The final return value
2303 Do not use `make-local-variable' to make a hook variable buffer-local.
2304 Instead, use `add-hook' and specify t for the LOCAL argument.
2305 usage: (run-hook-with-args HOOK &rest ARGS) */)
2306 (ptrdiff_t nargs
, Lisp_Object
*args
)
2308 return run_hook_with_args (nargs
, args
, funcall_nil
);
2311 /* NB this one still documents a specific non-nil return value.
2312 (As did run-hook-with-args and run-hook-with-args-until-failure
2313 until they were changed in 24.1.) */
2314 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2315 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2316 doc
: /* Run HOOK with the specified arguments ARGS.
2317 HOOK should be a symbol, a hook variable. The value of HOOK
2318 may be nil, a function, or a list of functions. Call each
2319 function in order with arguments ARGS, stopping at the first
2320 one that returns non-nil, and return that value. Otherwise (if
2321 all functions return nil, or if there are no functions to call),
2324 Do not use `make-local-variable' to make a hook variable buffer-local.
2325 Instead, use `add-hook' and specify t for the LOCAL argument.
2326 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2327 (ptrdiff_t nargs
, Lisp_Object
*args
)
2329 return run_hook_with_args (nargs
, args
, Ffuncall
);
2333 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2335 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2338 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2339 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2340 doc
: /* Run HOOK with the specified arguments ARGS.
2341 HOOK should be a symbol, a hook variable. The value of HOOK
2342 may be nil, a function, or a list of functions. Call each
2343 function in order with arguments ARGS, stopping at the first
2344 one that returns nil, and return nil. Otherwise (if all functions
2345 return non-nil, or if there are no functions to call), return non-nil
2346 \(do not rely on the precise return value in this case).
2348 Do not use `make-local-variable' to make a hook variable buffer-local.
2349 Instead, use `add-hook' and specify t for the LOCAL argument.
2350 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2351 (ptrdiff_t nargs
, Lisp_Object
*args
)
2353 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2357 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2359 Lisp_Object tmp
= args
[0], ret
;
2362 ret
= Ffuncall (nargs
, args
);
2368 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2369 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2370 I.e. instead of calling each function FUN directly with arguments ARGS,
2371 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2372 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2373 aborts and returns that value.
2374 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2375 (ptrdiff_t nargs
, Lisp_Object
*args
)
2377 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2380 /* ARGS[0] should be a hook symbol.
2381 Call each of the functions in the hook value, passing each of them
2382 as arguments all the rest of ARGS (all NARGS - 1 elements).
2383 FUNCALL specifies how to call each function on the hook.
2384 The caller (or its caller, etc) must gcpro all of ARGS,
2385 except that it isn't necessary to gcpro ARGS[0]. */
2388 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2389 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2391 Lisp_Object sym
, val
, ret
= Qnil
;
2392 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2394 /* If we are dying or still initializing,
2395 don't do anything--it would probably crash if we tried. */
2396 if (NILP (Vrun_hooks
))
2400 val
= find_symbol_value (sym
);
2402 if (EQ (val
, Qunbound
) || NILP (val
))
2404 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2407 return funcall (nargs
, args
);
2411 Lisp_Object global_vals
= Qnil
;
2412 GCPRO3 (sym
, val
, global_vals
);
2415 CONSP (val
) && NILP (ret
);
2418 if (EQ (XCAR (val
), Qt
))
2420 /* t indicates this hook has a local binding;
2421 it means to run the global binding too. */
2422 global_vals
= Fdefault_value (sym
);
2423 if (NILP (global_vals
)) continue;
2425 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2427 args
[0] = global_vals
;
2428 ret
= funcall (nargs
, args
);
2433 CONSP (global_vals
) && NILP (ret
);
2434 global_vals
= XCDR (global_vals
))
2436 args
[0] = XCAR (global_vals
);
2437 /* In a global value, t should not occur. If it does, we
2438 must ignore it to avoid an endless loop. */
2439 if (!EQ (args
[0], Qt
))
2440 ret
= funcall (nargs
, args
);
2446 args
[0] = XCAR (val
);
2447 ret
= funcall (nargs
, args
);
2456 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2459 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2461 Lisp_Object temp
[3];
2466 Frun_hook_with_args (3, temp
);
2469 /* Apply fn to arg. */
2471 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2473 struct gcpro gcpro1
;
2477 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2480 Lisp_Object args
[2];
2484 RETURN_UNGCPRO (Fapply (2, args
));
2488 /* Call function fn on no arguments. */
2490 call0 (Lisp_Object fn
)
2492 struct gcpro gcpro1
;
2495 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2498 /* Call function fn with 1 argument arg1. */
2501 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2503 struct gcpro gcpro1
;
2504 Lisp_Object args
[2];
2510 RETURN_UNGCPRO (Ffuncall (2, args
));
2513 /* Call function fn with 2 arguments arg1, arg2. */
2516 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2518 struct gcpro gcpro1
;
2519 Lisp_Object args
[3];
2525 RETURN_UNGCPRO (Ffuncall (3, args
));
2528 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2531 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2533 struct gcpro gcpro1
;
2534 Lisp_Object args
[4];
2541 RETURN_UNGCPRO (Ffuncall (4, args
));
2544 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2547 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2550 struct gcpro gcpro1
;
2551 Lisp_Object args
[5];
2559 RETURN_UNGCPRO (Ffuncall (5, args
));
2562 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2565 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2566 Lisp_Object arg4
, Lisp_Object arg5
)
2568 struct gcpro gcpro1
;
2569 Lisp_Object args
[6];
2578 RETURN_UNGCPRO (Ffuncall (6, args
));
2581 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2584 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2585 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2587 struct gcpro gcpro1
;
2588 Lisp_Object args
[7];
2598 RETURN_UNGCPRO (Ffuncall (7, args
));
2601 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2604 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2605 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2607 struct gcpro gcpro1
;
2608 Lisp_Object args
[8];
2619 RETURN_UNGCPRO (Ffuncall (8, args
));
2622 /* The caller should GCPRO all the elements of ARGS. */
2624 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2625 doc
: /* Non-nil if OBJECT is a function. */)
2626 (Lisp_Object object
)
2628 if (FUNCTIONP (object
))
2633 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2634 doc
: /* Call first argument as a function, passing remaining arguments to it.
2635 Return the value that function returns.
2636 Thus, (funcall 'cons 'x 'y) returns (x . y).
2637 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2638 (ptrdiff_t nargs
, Lisp_Object
*args
)
2640 Lisp_Object fun
, original_fun
;
2642 ptrdiff_t numargs
= nargs
- 1;
2643 Lisp_Object lisp_numargs
;
2645 register Lisp_Object
*internal_args
;
2650 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2652 if (max_lisp_eval_depth
< 100)
2653 max_lisp_eval_depth
= 100;
2654 if (lisp_eval_depth
> max_lisp_eval_depth
)
2655 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2658 /* This also GCPROs them. */
2659 record_in_backtrace (args
[0], &args
[1], nargs
- 1);
2661 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2664 if (debug_on_next_call
)
2665 do_debug_on_call (Qlambda
);
2669 original_fun
= args
[0];
2673 /* Optimize for no indirection. */
2675 if (SYMBOLP (fun
) && !NILP (fun
)
2676 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2677 fun
= indirect_function (fun
);
2681 if (numargs
< XSUBR (fun
)->min_args
2682 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2684 XSETFASTINT (lisp_numargs
, numargs
);
2685 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2688 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2689 xsignal1 (Qinvalid_function
, original_fun
);
2691 else if (XSUBR (fun
)->max_args
== MANY
)
2692 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2695 if (XSUBR (fun
)->max_args
> numargs
)
2697 internal_args
= alloca (XSUBR (fun
)->max_args
2698 * sizeof *internal_args
);
2699 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2700 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2701 internal_args
[i
] = Qnil
;
2704 internal_args
= args
+ 1;
2705 switch (XSUBR (fun
)->max_args
)
2708 val
= (XSUBR (fun
)->function
.a0 ());
2711 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2714 val
= (XSUBR (fun
)->function
.a2
2715 (internal_args
[0], internal_args
[1]));
2718 val
= (XSUBR (fun
)->function
.a3
2719 (internal_args
[0], internal_args
[1], internal_args
[2]));
2722 val
= (XSUBR (fun
)->function
.a4
2723 (internal_args
[0], internal_args
[1], internal_args
[2],
2727 val
= (XSUBR (fun
)->function
.a5
2728 (internal_args
[0], internal_args
[1], internal_args
[2],
2729 internal_args
[3], internal_args
[4]));
2732 val
= (XSUBR (fun
)->function
.a6
2733 (internal_args
[0], internal_args
[1], internal_args
[2],
2734 internal_args
[3], internal_args
[4], internal_args
[5]));
2737 val
= (XSUBR (fun
)->function
.a7
2738 (internal_args
[0], internal_args
[1], internal_args
[2],
2739 internal_args
[3], internal_args
[4], internal_args
[5],
2744 val
= (XSUBR (fun
)->function
.a8
2745 (internal_args
[0], internal_args
[1], internal_args
[2],
2746 internal_args
[3], internal_args
[4], internal_args
[5],
2747 internal_args
[6], internal_args
[7]));
2752 /* If a subr takes more than 8 arguments without using MANY
2753 or UNEVALLED, we need to extend this function to support it.
2754 Until this is done, there is no way to call the function. */
2759 else if (COMPILEDP (fun
))
2760 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2764 xsignal1 (Qvoid_function
, original_fun
);
2766 xsignal1 (Qinvalid_function
, original_fun
);
2767 funcar
= XCAR (fun
);
2768 if (!SYMBOLP (funcar
))
2769 xsignal1 (Qinvalid_function
, original_fun
);
2770 if (EQ (funcar
, Qlambda
)
2771 || EQ (funcar
, Qclosure
))
2772 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2773 else if (EQ (funcar
, Qautoload
))
2775 Fautoload_do_load (fun
, original_fun
, Qnil
);
2780 xsignal1 (Qinvalid_function
, original_fun
);
2784 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2785 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2791 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2793 Lisp_Object args_left
;
2796 register Lisp_Object
*arg_vector
;
2797 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2798 register Lisp_Object tem
;
2801 numargs
= XFASTINT (Flength (args
));
2802 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2805 GCPRO3 (*arg_vector
, args_left
, fun
);
2808 for (i
= 0; i
< numargs
; )
2810 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2811 tem
= eval_sub (tem
);
2812 arg_vector
[i
++] = tem
;
2818 set_backtrace_args (specpdl_ptr
- 1, arg_vector
);
2819 set_backtrace_nargs (specpdl_ptr
- 1, i
);
2820 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2822 /* Do the debug-on-exit now, while arg_vector still exists. */
2823 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2825 /* Don't do it again when we return to eval. */
2826 set_backtrace_debug_on_exit (specpdl_ptr
- 1, false);
2827 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2833 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2834 and return the result of evaluation.
2835 FUN must be either a lambda-expression or a compiled-code object. */
2838 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2839 register Lisp_Object
*arg_vector
)
2841 Lisp_Object val
, syms_left
, next
, lexenv
;
2842 ptrdiff_t count
= SPECPDL_INDEX ();
2844 bool optional
, rest
;
2848 if (EQ (XCAR (fun
), Qclosure
))
2850 fun
= XCDR (fun
); /* Drop `closure'. */
2851 lexenv
= XCAR (fun
);
2852 CHECK_LIST_CONS (fun
, fun
);
2856 syms_left
= XCDR (fun
);
2857 if (CONSP (syms_left
))
2858 syms_left
= XCAR (syms_left
);
2860 xsignal1 (Qinvalid_function
, fun
);
2862 else if (COMPILEDP (fun
))
2864 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2865 if (INTEGERP (syms_left
))
2866 /* A byte-code object with a non-nil `push args' slot means we
2867 shouldn't bind any arguments, instead just call the byte-code
2868 interpreter directly; it will push arguments as necessary.
2870 Byte-code objects with either a non-existent, or a nil value for
2871 the `push args' slot (the default), have dynamically-bound
2872 arguments, and use the argument-binding code below instead (as do
2873 all interpreted functions, even lexically bound ones). */
2875 /* If we have not actually read the bytecode string
2876 and constants vector yet, fetch them from the file. */
2877 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2878 Ffetch_bytecode (fun
);
2879 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2880 AREF (fun
, COMPILED_CONSTANTS
),
2881 AREF (fun
, COMPILED_STACK_DEPTH
),
2890 i
= optional
= rest
= 0;
2891 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2895 next
= XCAR (syms_left
);
2896 if (!SYMBOLP (next
))
2897 xsignal1 (Qinvalid_function
, fun
);
2899 if (EQ (next
, Qand_rest
))
2901 else if (EQ (next
, Qand_optional
))
2908 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2912 arg
= arg_vector
[i
++];
2914 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2918 /* Bind the argument. */
2919 if (!NILP (lexenv
) && SYMBOLP (next
))
2920 /* Lexically bind NEXT by adding it to the lexenv alist. */
2921 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
2923 /* Dynamically bind NEXT. */
2924 specbind (next
, arg
);
2928 if (!NILP (syms_left
))
2929 xsignal1 (Qinvalid_function
, fun
);
2931 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2933 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
2934 /* Instantiate a new lexical environment. */
2935 specbind (Qinternal_interpreter_environment
, lexenv
);
2938 val
= Fprogn (XCDR (XCDR (fun
)));
2941 /* If we have not actually read the bytecode string
2942 and constants vector yet, fetch them from the file. */
2943 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2944 Ffetch_bytecode (fun
);
2945 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2946 AREF (fun
, COMPILED_CONSTANTS
),
2947 AREF (fun
, COMPILED_STACK_DEPTH
),
2951 return unbind_to (count
, val
);
2954 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2956 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2957 (Lisp_Object object
)
2961 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
2963 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
2966 tem
= AREF (object
, COMPILED_BYTECODE
);
2967 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
2968 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
2970 error ("Invalid byte code");
2972 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
2973 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
2978 /* Return true if SYMBOL currently has a let-binding
2979 which was made in the buffer that is now current. */
2982 let_shadows_buffer_binding_p (struct Lisp_Symbol
*symbol
)
2984 struct specbinding
*p
;
2985 Lisp_Object buf
= Fcurrent_buffer ();
2987 for (p
= specpdl_ptr
; p
> specpdl
; )
2988 if ((--p
)->kind
> SPECPDL_LET
)
2990 struct Lisp_Symbol
*let_bound_symbol
= XSYMBOL (specpdl_symbol (p
));
2991 eassert (let_bound_symbol
->redirect
!= SYMBOL_VARALIAS
);
2992 if (symbol
== let_bound_symbol
2993 && EQ (specpdl_where (p
), buf
))
3001 let_shadows_global_binding_p (Lisp_Object symbol
)
3003 struct specbinding
*p
;
3005 for (p
= specpdl_ptr
; p
> specpdl
; )
3006 if ((--p
)->kind
>= SPECPDL_LET
&& EQ (specpdl_symbol (p
), symbol
))
3012 /* `specpdl_ptr->symbol' is a field which describes which variable is
3013 let-bound, so it can be properly undone when we unbind_to.
3014 It can have the following two shapes:
3015 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3016 a symbol that is not buffer-local (at least at the time
3017 the let binding started). Note also that it should not be
3018 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3020 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3021 variable SYMBOL which can be buffer-local. WHERE tells us
3022 which buffer is affected (or nil if the let-binding affects the
3023 global value of the variable) and BUFFER tells us which buffer was
3024 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3025 BUFFER did not yet have a buffer-local value). */
3028 specbind (Lisp_Object symbol
, Lisp_Object value
)
3030 struct Lisp_Symbol
*sym
;
3032 CHECK_SYMBOL (symbol
);
3033 sym
= XSYMBOL (symbol
);
3034 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3038 switch (sym
->redirect
)
3040 case SYMBOL_VARALIAS
:
3041 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3042 case SYMBOL_PLAINVAL
:
3043 /* The most common case is that of a non-constant symbol with a
3044 trivial value. Make that as fast as we can. */
3045 specpdl_ptr
->kind
= SPECPDL_LET
;
3046 specpdl_ptr
->v
.let
.symbol
= symbol
;
3047 specpdl_ptr
->v
.let
.old_value
= SYMBOL_VAL (sym
);
3050 SET_SYMBOL_VAL (sym
, value
);
3052 set_internal (symbol
, value
, Qnil
, 1);
3054 case SYMBOL_LOCALIZED
:
3055 if (SYMBOL_BLV (sym
)->frame_local
)
3056 error ("Frame-local vars cannot be let-bound");
3057 case SYMBOL_FORWARDED
:
3059 Lisp_Object ovalue
= find_symbol_value (symbol
);
3060 specpdl_ptr
->kind
= SPECPDL_LET_LOCAL
;
3061 specpdl_ptr
->v
.let
.symbol
= symbol
;
3062 specpdl_ptr
->v
.let
.old_value
= ovalue
;
3063 specpdl_ptr
->v
.let
.where
= Fcurrent_buffer ();
3065 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3066 || (EQ (SYMBOL_BLV (sym
)->where
, Fcurrent_buffer ())));
3068 if (sym
->redirect
== SYMBOL_LOCALIZED
)
3070 if (!blv_found (SYMBOL_BLV (sym
)))
3071 specpdl_ptr
->kind
= SPECPDL_LET_DEFAULT
;
3073 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3075 /* If SYMBOL is a per-buffer variable which doesn't have a
3076 buffer-local value here, make the `let' change the global
3077 value by changing the value of SYMBOL in all buffers not
3078 having their own value. This is consistent with what
3079 happens with other buffer-local variables. */
3080 if (NILP (Flocal_variable_p (symbol
, Qnil
)))
3082 specpdl_ptr
->kind
= SPECPDL_LET_DEFAULT
;
3084 Fset_default (symbol
, value
);
3089 specpdl_ptr
->kind
= SPECPDL_LET
;
3092 set_internal (symbol
, value
, Qnil
, 1);
3095 default: emacs_abort ();
3100 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3102 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3104 specpdl_ptr
->kind
= SPECPDL_UNWIND
;
3105 specpdl_ptr
->v
.unwind
.func
= function
;
3106 specpdl_ptr
->v
.unwind
.arg
= arg
;
3111 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3113 Lisp_Object quitf
= Vquit_flag
;
3114 struct gcpro gcpro1
, gcpro2
;
3116 GCPRO2 (value
, quitf
);
3119 while (specpdl_ptr
!= specpdl
+ count
)
3121 /* Copy the binding, and decrement specpdl_ptr, before we do
3122 the work to unbind it. We decrement first
3123 so that an error in unbinding won't try to unbind
3124 the same entry again, and we copy the binding first
3125 in case more bindings are made during some of the code we run. */
3127 struct specbinding this_binding
;
3128 this_binding
= *--specpdl_ptr
;
3130 switch (this_binding
.kind
)
3132 case SPECPDL_UNWIND
:
3133 (*specpdl_func (&this_binding
)) (specpdl_arg (&this_binding
));
3136 /* If variable has a trivial value (no forwarding), we can
3137 just set it. No need to check for constant symbols here,
3138 since that was already done by specbind. */
3139 if (XSYMBOL (specpdl_symbol (&this_binding
))->redirect
3141 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (&this_binding
)),
3142 specpdl_old_value (&this_binding
));
3144 /* NOTE: we only ever come here if make_local_foo was used for
3145 the first time on this var within this let. */
3146 Fset_default (specpdl_symbol (&this_binding
),
3147 specpdl_old_value (&this_binding
));
3149 case SPECPDL_BACKTRACE
:
3151 case SPECPDL_LET_LOCAL
:
3152 case SPECPDL_LET_DEFAULT
:
3153 { /* If the symbol is a list, it is really (SYMBOL WHERE
3154 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3155 frame. If WHERE is a buffer or frame, this indicates we
3156 bound a variable that had a buffer-local or frame-local
3157 binding. WHERE nil means that the variable had the default
3158 value when it was bound. CURRENT-BUFFER is the buffer that
3159 was current when the variable was bound. */
3160 Lisp_Object symbol
= specpdl_symbol (&this_binding
);
3161 Lisp_Object where
= specpdl_where (&this_binding
);
3162 eassert (BUFFERP (where
));
3164 if (this_binding
.kind
== SPECPDL_LET_DEFAULT
)
3165 Fset_default (symbol
, specpdl_old_value (&this_binding
));
3166 /* If this was a local binding, reset the value in the appropriate
3167 buffer, but only if that buffer's binding still exists. */
3168 else if (!NILP (Flocal_variable_p (symbol
, where
)))
3169 set_internal (symbol
, specpdl_old_value (&this_binding
),
3176 if (NILP (Vquit_flag
) && !NILP (quitf
))
3183 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3184 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3185 A special variable is one that will be bound dynamically, even in a
3186 context where binding is lexical by default. */)
3187 (Lisp_Object symbol
)
3189 CHECK_SYMBOL (symbol
);
3190 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3194 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3195 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3196 The debugger is entered when that frame exits, if the flag is non-nil. */)
3197 (Lisp_Object level
, Lisp_Object flag
)
3199 struct specbinding
*pdl
= backtrace_top ();
3200 register EMACS_INT i
;
3202 CHECK_NUMBER (level
);
3204 for (i
= 0; backtrace_p (pdl
) && i
< XINT (level
); i
++)
3205 pdl
= backtrace_next (pdl
);
3207 if (backtrace_p (pdl
))
3208 set_backtrace_debug_on_exit (pdl
, !NILP (flag
));
3213 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3214 doc
: /* Print a trace of Lisp function calls currently active.
3215 Output stream used is value of `standard-output'. */)
3218 struct specbinding
*pdl
= backtrace_top ();
3220 Lisp_Object old_print_level
= Vprint_level
;
3222 if (NILP (Vprint_level
))
3223 XSETFASTINT (Vprint_level
, 8);
3225 while (backtrace_p (pdl
))
3227 write_string (backtrace_debug_on_exit (pdl
) ? "* " : " ", 2);
3228 if (backtrace_nargs (pdl
) == UNEVALLED
)
3230 Fprin1 (Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)),
3232 write_string ("\n", -1);
3236 tem
= backtrace_function (pdl
);
3237 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3238 write_string ("(", -1);
3241 for (i
= 0; i
< backtrace_nargs (pdl
); i
++)
3243 if (i
) write_string (" ", -1);
3244 Fprin1 (backtrace_args (pdl
)[i
], Qnil
);
3247 write_string (")\n", -1);
3249 pdl
= backtrace_next (pdl
);
3252 Vprint_level
= old_print_level
;
3256 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3257 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3258 If that frame has not evaluated the arguments yet (or is a special form),
3259 the value is (nil FUNCTION ARG-FORMS...).
3260 If that frame has evaluated its arguments and called its function already,
3261 the value is (t FUNCTION ARG-VALUES...).
3262 A &rest arg is represented as the tail of the list ARG-VALUES.
3263 FUNCTION is whatever was supplied as car of evaluated list,
3264 or a lambda expression for macro calls.
3265 If NFRAMES is more than the number of frames, the value is nil. */)
3266 (Lisp_Object nframes
)
3268 struct specbinding
*pdl
= backtrace_top ();
3269 register EMACS_INT i
;
3271 CHECK_NATNUM (nframes
);
3273 /* Find the frame requested. */
3274 for (i
= 0; backtrace_p (pdl
) && i
< XFASTINT (nframes
); i
++)
3275 pdl
= backtrace_next (pdl
);
3277 if (!backtrace_p (pdl
))
3279 if (backtrace_nargs (pdl
) == UNEVALLED
)
3281 Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)));
3284 Lisp_Object tem
= Flist (backtrace_nargs (pdl
), backtrace_args (pdl
));
3286 return Fcons (Qt
, Fcons (backtrace_function (pdl
), tem
));
3294 struct specbinding
*pdl
;
3295 for (pdl
= specpdl
; pdl
!= specpdl_ptr
; pdl
++)
3299 case SPECPDL_UNWIND
:
3300 mark_object (specpdl_arg (pdl
));
3302 case SPECPDL_BACKTRACE
:
3304 ptrdiff_t nargs
= backtrace_nargs (pdl
);
3305 mark_object (backtrace_function (pdl
));
3306 if (nargs
== UNEVALLED
)
3309 mark_object (backtrace_args (pdl
)[nargs
]);
3312 case SPECPDL_LET_DEFAULT
:
3313 case SPECPDL_LET_LOCAL
:
3314 mark_object (specpdl_where (pdl
));
3316 mark_object (specpdl_symbol (pdl
));
3317 mark_object (specpdl_old_value (pdl
));
3323 get_backtrace (Lisp_Object array
)
3325 struct specbinding
*pdl
= backtrace_next (backtrace_top ());
3326 ptrdiff_t i
= 0, asize
= ASIZE (array
);
3328 /* Copy the backtrace contents into working memory. */
3329 for (; i
< asize
; i
++)
3331 if (backtrace_p (pdl
))
3333 ASET (array
, i
, backtrace_function (pdl
));
3334 pdl
= backtrace_next (pdl
);
3337 ASET (array
, i
, Qnil
);
3341 Lisp_Object
backtrace_top_function (void)
3343 struct specbinding
*pdl
= backtrace_top ();
3344 return (backtrace_p (pdl
) ? backtrace_function (pdl
) : Qnil
);
3350 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3351 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3352 If Lisp code tries to increase the total number past this amount,
3353 an error is signaled.
3354 You can safely use a value considerably larger than the default value,
3355 if that proves inconveniently small. However, if you increase it too far,
3356 Emacs could run out of memory trying to make the stack bigger. */);
3358 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3359 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3361 This limit serves to catch infinite recursions for you before they cause
3362 actual stack overflow in C, which would be fatal for Emacs.
3363 You can safely make it considerably larger than its default value,
3364 if that proves inconveniently small. However, if you increase it too far,
3365 Emacs could overflow the real C stack, and crash. */);
3367 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3368 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3369 If the value is t, that means do an ordinary quit.
3370 If the value equals `throw-on-input', that means quit by throwing
3371 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3372 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3373 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3376 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3377 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3378 Note that `quit-flag' will still be set by typing C-g,
3379 so a quit will be signaled as soon as `inhibit-quit' is nil.
3380 To prevent this happening, set `quit-flag' to nil
3381 before making `inhibit-quit' nil. */);
3382 Vinhibit_quit
= Qnil
;
3384 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3385 DEFSYM (Qautoload
, "autoload");
3386 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3387 DEFSYM (Qmacro
, "macro");
3388 DEFSYM (Qdeclare
, "declare");
3390 /* Note that the process handling also uses Qexit, but we don't want
3391 to staticpro it twice, so we just do it here. */
3392 DEFSYM (Qexit
, "exit");
3394 DEFSYM (Qinteractive
, "interactive");
3395 DEFSYM (Qcommandp
, "commandp");
3396 DEFSYM (Qand_rest
, "&rest");
3397 DEFSYM (Qand_optional
, "&optional");
3398 DEFSYM (Qclosure
, "closure");
3399 DEFSYM (Qdebug
, "debug");
3401 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3402 doc
: /* Non-nil means never enter the debugger.
3403 Normally set while the debugger is already active, to avoid recursive
3405 Vinhibit_debugger
= Qnil
;
3407 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3408 doc
: /* Non-nil means enter debugger if an error is signaled.
3409 Does not apply to errors handled by `condition-case' or those
3410 matched by `debug-ignored-errors'.
3411 If the value is a list, an error only means to enter the debugger
3412 if one of its condition symbols appears in the list.
3413 When you evaluate an expression interactively, this variable
3414 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3415 The command `toggle-debug-on-error' toggles this.
3416 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3417 Vdebug_on_error
= Qnil
;
3419 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3420 doc
: /* List of errors for which the debugger should not be called.
3421 Each element may be a condition-name or a regexp that matches error messages.
3422 If any element applies to a given error, that error skips the debugger
3423 and just returns to top level.
3424 This overrides the variable `debug-on-error'.
3425 It does not apply to errors handled by `condition-case'. */);
3426 Vdebug_ignored_errors
= Qnil
;
3428 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3429 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3430 Does not apply if quit is handled by a `condition-case'. */);
3433 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3434 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3436 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3437 doc
: /* Non-nil means debugger may continue execution.
3438 This is nil when the debugger is called under circumstances where it
3439 might not be safe to continue. */);
3440 debugger_may_continue
= 1;
3442 DEFVAR_LISP ("debugger", Vdebugger
,
3443 doc
: /* Function to call to invoke debugger.
3444 If due to frame exit, args are `exit' and the value being returned;
3445 this function's value will be returned instead of that.
3446 If due to error, args are `error' and a list of the args to `signal'.
3447 If due to `apply' or `funcall' entry, one arg, `lambda'.
3448 If due to `eval' entry, one arg, t. */);
3451 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3452 doc
: /* If non-nil, this is a function for `signal' to call.
3453 It receives the same arguments that `signal' was given.
3454 The Edebug package uses this to regain control. */);
3455 Vsignal_hook_function
= Qnil
;
3457 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3458 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3459 Note that `debug-on-error', `debug-on-quit' and friends
3460 still determine whether to handle the particular condition. */);
3461 Vdebug_on_signal
= Qnil
;
3463 /* When lexical binding is being used,
3464 Vinternal_interpreter_environment is non-nil, and contains an alist
3465 of lexically-bound variable, or (t), indicating an empty
3466 environment. The lisp name of this variable would be
3467 `internal-interpreter-environment' if it weren't hidden.
3468 Every element of this list can be either a cons (VAR . VAL)
3469 specifying a lexical binding, or a single symbol VAR indicating
3470 that this variable should use dynamic scoping. */
3471 DEFSYM (Qinternal_interpreter_environment
,
3472 "internal-interpreter-environment");
3473 DEFVAR_LISP ("internal-interpreter-environment",
3474 Vinternal_interpreter_environment
,
3475 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3476 When lexical binding is not being used, this variable is nil.
3477 A value of `(t)' indicates an empty environment, otherwise it is an
3478 alist of active lexical bindings. */);
3479 Vinternal_interpreter_environment
= Qnil
;
3480 /* Don't export this variable to Elisp, so no one can mess with it
3481 (Just imagine if someone makes it buffer-local). */
3482 Funintern (Qinternal_interpreter_environment
, Qnil
);
3484 DEFSYM (Vrun_hooks
, "run-hooks");
3486 staticpro (&Vautoload_queue
);
3487 Vautoload_queue
= Qnil
;
3488 staticpro (&Vsignaling_function
);
3489 Vsignaling_function
= Qnil
;
3491 inhibit_lisp_code
= Qnil
;
3502 defsubr (&Sfunction
);
3504 defsubr (&Sdefvaralias
);
3505 defsubr (&Sdefconst
);
3506 defsubr (&Smake_var_non_special
);
3510 defsubr (&Smacroexpand
);
3513 defsubr (&Sunwind_protect
);
3514 defsubr (&Scondition_case
);
3516 defsubr (&Scommandp
);
3517 defsubr (&Sautoload
);
3518 defsubr (&Sautoload_do_load
);
3521 defsubr (&Sfuncall
);
3522 defsubr (&Srun_hooks
);
3523 defsubr (&Srun_hook_with_args
);
3524 defsubr (&Srun_hook_with_args_until_success
);
3525 defsubr (&Srun_hook_with_args_until_failure
);
3526 defsubr (&Srun_hook_wrapped
);
3527 defsubr (&Sfetch_bytecode
);
3528 defsubr (&Sbacktrace_debug
);
3529 defsubr (&Sbacktrace
);
3530 defsubr (&Sbacktrace_frame
);
3531 defsubr (&Sspecial_variable_p
);
3532 defsubr (&Sfunctionp
);