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, not counting
80 the dummy entry specpdl[-1]. */
82 ptrdiff_t specpdl_size
;
84 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
87 union specbinding
*specpdl
;
89 /* Pointer to first unused element in specpdl. */
91 union specbinding
*specpdl_ptr
;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth
;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
104 static EMACS_INT when_entered_debugger
;
106 /* The function from which the last `signal' was called. Set in
108 /* FIXME: We should probably get rid of this! */
109 Lisp_Object Vsignaling_function
;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code
;
117 /* These would ordinarily be static, but they need to be visible to GDB. */
118 bool backtrace_p (union specbinding
*) EXTERNALLY_VISIBLE
;
119 Lisp_Object
*backtrace_args (union specbinding
*) EXTERNALLY_VISIBLE
;
120 Lisp_Object
backtrace_function (union specbinding
*) EXTERNALLY_VISIBLE
;
121 union specbinding
*backtrace_next (union specbinding
*) EXTERNALLY_VISIBLE
;
122 union specbinding
*backtrace_top (void) EXTERNALLY_VISIBLE
;
124 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
125 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
128 specpdl_symbol (union specbinding
*pdl
)
130 eassert (pdl
->kind
>= SPECPDL_LET
);
131 return pdl
->let
.symbol
;
135 specpdl_old_value (union specbinding
*pdl
)
137 eassert (pdl
->kind
>= SPECPDL_LET
);
138 return pdl
->let
.old_value
;
142 specpdl_where (union specbinding
*pdl
)
144 eassert (pdl
->kind
> SPECPDL_LET
);
145 return pdl
->let
.where
;
149 specpdl_arg (union specbinding
*pdl
)
151 eassert (pdl
->kind
== SPECPDL_UNWIND
);
152 return pdl
->unwind
.arg
;
155 static specbinding_func
156 specpdl_func (union specbinding
*pdl
)
158 eassert (pdl
->kind
== SPECPDL_UNWIND
);
159 return pdl
->unwind
.func
;
163 backtrace_function (union specbinding
*pdl
)
165 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
166 return pdl
->bt
.function
;
170 backtrace_nargs (union specbinding
*pdl
)
172 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
173 return pdl
->bt
.nargs
;
177 backtrace_args (union specbinding
*pdl
)
179 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
184 backtrace_debug_on_exit (union specbinding
*pdl
)
186 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
187 return pdl
->bt
.debug_on_exit
;
190 /* Functions to modify slots of backtrace records. */
193 set_backtrace_args (union specbinding
*pdl
, Lisp_Object
*args
)
195 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
200 set_backtrace_nargs (union specbinding
*pdl
, ptrdiff_t n
)
202 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
207 set_backtrace_debug_on_exit (union specbinding
*pdl
, bool doe
)
209 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
210 pdl
->bt
.debug_on_exit
= doe
;
213 /* Helper functions to scan the backtrace. */
216 backtrace_p (union specbinding
*pdl
)
217 { return pdl
>= specpdl
; }
222 union specbinding
*pdl
= specpdl_ptr
- 1;
223 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
229 backtrace_next (union specbinding
*pdl
)
232 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
239 init_eval_once (void)
242 union specbinding
*pdlvec
= xmalloc ((size
+ 1) * sizeof *specpdl
);
244 specpdl
= specpdl_ptr
= pdlvec
+ 1;
245 /* Don't forget to update docs (lispref node "Local Variables"). */
246 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
247 max_lisp_eval_depth
= 600;
255 specpdl_ptr
= specpdl
;
259 debug_on_next_call
= 0;
264 /* This is less than the initial value of num_nonmacro_input_events. */
265 when_entered_debugger
= -1;
268 /* Unwind-protect function used by call_debugger. */
271 restore_stack_limits (Lisp_Object data
)
273 max_specpdl_size
= XINT (XCAR (data
));
274 max_lisp_eval_depth
= XINT (XCDR (data
));
278 /* Call the Lisp debugger, giving it argument ARG. */
281 call_debugger (Lisp_Object arg
)
283 bool debug_while_redisplaying
;
284 ptrdiff_t count
= SPECPDL_INDEX ();
286 EMACS_INT old_max
= max_specpdl_size
;
288 /* Temporarily bump up the stack limits,
289 so the debugger won't run out of stack. */
291 max_specpdl_size
+= 1;
292 record_unwind_protect (restore_stack_limits
,
293 Fcons (make_number (old_max
),
294 make_number (max_lisp_eval_depth
)));
295 max_specpdl_size
= old_max
;
297 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
298 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
300 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
301 max_specpdl_size
= SPECPDL_INDEX () + 100;
303 #ifdef HAVE_WINDOW_SYSTEM
304 if (display_hourglass_p
)
308 debug_on_next_call
= 0;
309 when_entered_debugger
= num_nonmacro_input_events
;
311 /* Resetting redisplaying_p to 0 makes sure that debug output is
312 displayed if the debugger is invoked during redisplay. */
313 debug_while_redisplaying
= redisplaying_p
;
315 specbind (intern ("debugger-may-continue"),
316 debug_while_redisplaying
? Qnil
: Qt
);
317 specbind (Qinhibit_redisplay
, Qnil
);
318 specbind (Qinhibit_debugger
, Qt
);
320 #if 0 /* Binding this prevents execution of Lisp code during
321 redisplay, which necessarily leads to display problems. */
322 specbind (Qinhibit_eval_during_redisplay
, Qt
);
325 val
= apply1 (Vdebugger
, arg
);
327 /* Interrupting redisplay and resuming it later is not safe under
328 all circumstances. So, when the debugger returns, abort the
329 interrupted redisplay by going back to the top-level. */
330 if (debug_while_redisplaying
)
333 return unbind_to (count
, val
);
337 do_debug_on_call (Lisp_Object code
)
339 debug_on_next_call
= 0;
340 set_backtrace_debug_on_exit (specpdl_ptr
- 1, true);
341 call_debugger (Fcons (code
, Qnil
));
344 /* NOTE!!! Every function that can call EVAL must protect its args
345 and temporaries from garbage collection while it needs them.
346 The definition of `For' shows what you have to do. */
348 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
349 doc
: /* Eval args until one of them yields non-nil, then return that value.
350 The remaining args are not evalled at all.
351 If all args return nil, return nil.
352 usage: (or CONDITIONS...) */)
355 register Lisp_Object val
= Qnil
;
362 val
= eval_sub (XCAR (args
));
372 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
373 doc
: /* Eval args until one of them yields nil, then return nil.
374 The remaining args are not evalled at all.
375 If no arg yields nil, return the last arg's value.
376 usage: (and CONDITIONS...) */)
379 register Lisp_Object val
= Qt
;
386 val
= eval_sub (XCAR (args
));
396 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
397 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
398 Returns the value of THEN or the value of the last of the ELSE's.
399 THEN must be one expression, but ELSE... can be zero or more expressions.
400 If COND yields nil, and there are no ELSE's, the value is nil.
401 usage: (if COND THEN ELSE...) */)
404 register Lisp_Object cond
;
408 cond
= eval_sub (Fcar (args
));
412 return eval_sub (Fcar (Fcdr (args
)));
413 return Fprogn (Fcdr (Fcdr (args
)));
416 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
417 doc
: /* Try each clause until one succeeds.
418 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
419 and, if the value is non-nil, this clause succeeds:
420 then the expressions in BODY are evaluated and the last one's
421 value is the value of the cond-form.
422 If no clause succeeds, cond returns nil.
423 If a clause has one element, as in (CONDITION),
424 CONDITION's value if non-nil is returned from the cond-form.
425 usage: (cond CLAUSES...) */)
428 register Lisp_Object clause
, val
;
435 clause
= Fcar (args
);
436 val
= eval_sub (Fcar (clause
));
439 if (!EQ (XCDR (clause
), Qnil
))
440 val
= Fprogn (XCDR (clause
));
450 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
451 doc
: /* Eval BODY forms sequentially and return value of last one.
452 usage: (progn BODY...) */)
455 register Lisp_Object val
= Qnil
;
462 val
= eval_sub (XCAR (args
));
470 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
471 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
472 The value of FIRST is saved during the evaluation of the remaining args,
473 whose values are discarded.
474 usage: (prog1 FIRST BODY...) */)
478 register Lisp_Object args_left
;
479 struct gcpro gcpro1
, gcpro2
;
485 val
= eval_sub (XCAR (args_left
));
486 while (CONSP (args_left
= XCDR (args_left
)))
487 eval_sub (XCAR (args_left
));
493 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
494 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
495 The value of FORM2 is saved during the evaluation of the
496 remaining args, whose values are discarded.
497 usage: (prog2 FORM1 FORM2 BODY...) */)
503 eval_sub (XCAR (args
));
505 return Fprog1 (XCDR (args
));
508 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
509 doc
: /* Set each SYM to the value of its VAL.
510 The symbols SYM are variables; they are literal (not evaluated).
511 The values VAL are expressions; they are evaluated.
512 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
513 The second VAL is not computed until after the first SYM is set, and so on;
514 each VAL can use the new value of variables set earlier in the `setq'.
515 The return value of the `setq' form is the value of the last VAL.
516 usage: (setq [SYM VAL]...) */)
519 register Lisp_Object args_left
;
520 register Lisp_Object val
, sym
, lex_binding
;
531 val
= eval_sub (Fcar (Fcdr (args_left
)));
532 sym
= Fcar (args_left
);
534 /* Like for eval_sub, we do not check declared_special here since
535 it's been done when let-binding. */
536 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
538 && !NILP (lex_binding
539 = Fassq (sym
, Vinternal_interpreter_environment
)))
540 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
542 Fset (sym
, val
); /* SYM is dynamically bound. */
544 args_left
= Fcdr (Fcdr (args_left
));
546 while (!NILP (args_left
));
552 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
553 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
554 Warning: `quote' does not construct its return value, but just returns
555 the value that was pre-constructed by the Lisp reader (see info node
556 `(elisp)Printed Representation').
557 This means that '(a . b) is not identical to (cons 'a 'b): the former
558 does not cons. Quoting should be reserved for constants that will
559 never be modified by side-effects, unless you like self-modifying code.
560 See the common pitfall in info node `(elisp)Rearrangement' for an example
561 of unexpected results when a quoted object is modified.
562 usage: (quote ARG) */)
565 if (!NILP (Fcdr (args
)))
566 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
570 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
571 doc
: /* Like `quote', but preferred for objects which are functions.
572 In byte compilation, `function' causes its argument to be compiled.
573 `quote' cannot do that.
574 usage: (function ARG) */)
577 Lisp_Object quoted
= XCAR (args
);
579 if (!NILP (Fcdr (args
)))
580 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
582 if (!NILP (Vinternal_interpreter_environment
)
584 && EQ (XCAR (quoted
), Qlambda
))
585 /* This is a lambda expression within a lexical environment;
586 return an interpreted closure instead of a simple lambda. */
587 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
590 /* Simply quote the argument. */
595 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
596 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
597 Aliased variables always have the same value; setting one sets the other.
598 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
599 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
600 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
601 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
602 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
603 The return value is BASE-VARIABLE. */)
604 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
606 struct Lisp_Symbol
*sym
;
608 CHECK_SYMBOL (new_alias
);
609 CHECK_SYMBOL (base_variable
);
611 sym
= XSYMBOL (new_alias
);
614 /* Not sure why, but why not? */
615 error ("Cannot make a constant an alias");
617 switch (sym
->redirect
)
619 case SYMBOL_FORWARDED
:
620 error ("Cannot make an internal variable an alias");
621 case SYMBOL_LOCALIZED
:
622 error ("Don't know how to make a localized variable an alias");
625 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
626 If n_a is bound, but b_v is not, set the value of b_v to n_a,
627 so that old-code that affects n_a before the aliasing is setup
629 if (NILP (Fboundp (base_variable
)))
630 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
633 union specbinding
*p
;
635 for (p
= specpdl_ptr
; p
> specpdl
; )
636 if ((--p
)->kind
>= SPECPDL_LET
637 && (EQ (new_alias
, specpdl_symbol (p
))))
638 error ("Don't know how to make a let-bound variable an alias");
641 sym
->declared_special
= 1;
642 XSYMBOL (base_variable
)->declared_special
= 1;
643 sym
->redirect
= SYMBOL_VARALIAS
;
644 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
645 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
646 LOADHIST_ATTACH (new_alias
);
647 /* Even if docstring is nil: remove old docstring. */
648 Fput (new_alias
, Qvariable_documentation
, docstring
);
650 return base_variable
;
654 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
655 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
656 You are not required to define a variable in order to use it, but
657 defining it lets you supply an initial value and documentation, which
658 can be referred to by the Emacs help facilities and other programming
659 tools. The `defvar' form also declares the variable as \"special\",
660 so that it is always dynamically bound even if `lexical-binding' is t.
662 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
663 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
664 default value is what is set; buffer-local values are not affected.
665 If INITVALUE is missing, SYMBOL's value is not set.
667 If SYMBOL has a local binding, then this form affects the local
668 binding. This is usually not what you want. Thus, if you need to
669 load a file defining variables, with this form or with `defconst' or
670 `defcustom', you should always load that file _outside_ any bindings
671 for these variables. \(`defconst' and `defcustom' behave similarly in
674 The optional argument DOCSTRING is a documentation string for the
677 To define a user option, use `defcustom' instead of `defvar'.
678 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
681 register Lisp_Object sym
, tem
, tail
;
685 if (!NILP (Fcdr (Fcdr (tail
))))
686 error ("Too many arguments");
688 tem
= Fdefault_boundp (sym
);
691 /* Do it before evaluating the initial value, for self-references. */
692 XSYMBOL (sym
)->declared_special
= 1;
695 Fset_default (sym
, eval_sub (Fcar (tail
)));
697 { /* Check if there is really a global binding rather than just a let
698 binding that shadows the global unboundness of the var. */
699 union specbinding
*pdl
= specpdl_ptr
;
700 while (pdl
> specpdl
)
702 if ((--pdl
)->kind
>= SPECPDL_LET
703 && EQ (specpdl_symbol (pdl
), sym
)
704 && EQ (specpdl_old_value (pdl
), Qunbound
))
707 ("Warning: defvar ignored because %s is let-bound",
708 SYMBOL_NAME (sym
), 1);
717 if (!NILP (Vpurify_flag
))
718 tem
= Fpurecopy (tem
);
719 Fput (sym
, Qvariable_documentation
, tem
);
721 LOADHIST_ATTACH (sym
);
723 else if (!NILP (Vinternal_interpreter_environment
)
724 && !XSYMBOL (sym
)->declared_special
)
725 /* A simple (defvar foo) with lexical scoping does "nothing" except
726 declare that var to be dynamically scoped *locally* (i.e. within
727 the current file or let-block). */
728 Vinternal_interpreter_environment
729 = Fcons (sym
, Vinternal_interpreter_environment
);
732 /* Simple (defvar <var>) should not count as a definition at all.
733 It could get in the way of other definitions, and unloading this
734 package could try to make the variable unbound. */
740 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
741 doc
: /* Define SYMBOL as a constant variable.
742 This declares that neither programs nor users should ever change the
743 value. This constancy is not actually enforced by Emacs Lisp, but
744 SYMBOL is marked as a special variable so that it is never lexically
747 The `defconst' form always sets the value of SYMBOL to the result of
748 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
749 what is set; buffer-local values are not affected. If SYMBOL has a
750 local binding, then this form sets the local binding's value.
751 However, you should normally not make local bindings for variables
752 defined with this form.
754 The optional DOCSTRING specifies the variable's documentation string.
755 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
758 register Lisp_Object sym
, tem
;
761 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
762 error ("Too many arguments");
764 tem
= eval_sub (Fcar (Fcdr (args
)));
765 if (!NILP (Vpurify_flag
))
766 tem
= Fpurecopy (tem
);
767 Fset_default (sym
, tem
);
768 XSYMBOL (sym
)->declared_special
= 1;
769 tem
= Fcar (Fcdr (Fcdr (args
)));
772 if (!NILP (Vpurify_flag
))
773 tem
= Fpurecopy (tem
);
774 Fput (sym
, Qvariable_documentation
, tem
);
776 Fput (sym
, Qrisky_local_variable
, Qt
);
777 LOADHIST_ATTACH (sym
);
781 /* Make SYMBOL lexically scoped. */
782 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
783 Smake_var_non_special
, 1, 1, 0,
784 doc
: /* Internal function. */)
787 CHECK_SYMBOL (symbol
);
788 XSYMBOL (symbol
)->declared_special
= 0;
793 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
794 doc
: /* Bind variables according to VARLIST then eval BODY.
795 The value of the last form in BODY is returned.
796 Each element of VARLIST is a symbol (which is bound to nil)
797 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
798 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
799 usage: (let* VARLIST BODY...) */)
802 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
803 ptrdiff_t count
= SPECPDL_INDEX ();
804 struct gcpro gcpro1
, gcpro2
, gcpro3
;
806 GCPRO3 (args
, elt
, varlist
);
808 lexenv
= Vinternal_interpreter_environment
;
810 varlist
= Fcar (args
);
811 while (CONSP (varlist
))
815 elt
= XCAR (varlist
);
821 else if (! NILP (Fcdr (Fcdr (elt
))))
822 signal_error ("`let' bindings can have only one value-form", elt
);
826 val
= eval_sub (Fcar (Fcdr (elt
)));
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 interpreter's binding
836 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
837 if (EQ (Vinternal_interpreter_environment
, lexenv
))
838 /* Save the old lexical environment on the specpdl stack,
839 but only for the first lexical binding, since we'll never
840 need to revert to one of the intermediate ones. */
841 specbind (Qinternal_interpreter_environment
, newenv
);
843 Vinternal_interpreter_environment
= newenv
;
848 varlist
= XCDR (varlist
);
851 val
= Fprogn (Fcdr (args
));
852 return unbind_to (count
, val
);
855 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
856 doc
: /* Bind variables according to VARLIST then eval BODY.
857 The value of the last form in BODY is returned.
858 Each element of VARLIST is a symbol (which is bound to nil)
859 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
860 All the VALUEFORMs are evalled before any symbols are bound.
861 usage: (let VARLIST BODY...) */)
864 Lisp_Object
*temps
, tem
, lexenv
;
865 register Lisp_Object elt
, varlist
;
866 ptrdiff_t count
= SPECPDL_INDEX ();
868 struct gcpro gcpro1
, gcpro2
;
871 varlist
= Fcar (args
);
873 /* Make space to hold the values to give the bound variables. */
874 elt
= Flength (varlist
);
875 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
877 /* Compute the values and store them in `temps'. */
879 GCPRO2 (args
, *temps
);
882 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
885 elt
= XCAR (varlist
);
887 temps
[argnum
++] = Qnil
;
888 else if (! NILP (Fcdr (Fcdr (elt
))))
889 signal_error ("`let' bindings can have only one value-form", elt
);
891 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
892 gcpro2
.nvars
= argnum
;
896 lexenv
= Vinternal_interpreter_environment
;
898 varlist
= Fcar (args
);
899 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
903 elt
= XCAR (varlist
);
904 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
905 tem
= temps
[argnum
++];
907 if (!NILP (lexenv
) && SYMBOLP (var
)
908 && !XSYMBOL (var
)->declared_special
909 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
910 /* Lexically bind VAR by adding it to the lexenv alist. */
911 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
913 /* Dynamically bind VAR. */
917 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
918 /* Instantiate a new lexical environment. */
919 specbind (Qinternal_interpreter_environment
, lexenv
);
921 elt
= Fprogn (Fcdr (args
));
923 return unbind_to (count
, elt
);
926 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
927 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
928 The order of execution is thus TEST, BODY, TEST, BODY and so on
929 until TEST returns nil.
930 usage: (while TEST BODY...) */)
933 Lisp_Object test
, body
;
934 struct gcpro gcpro1
, gcpro2
;
940 while (!NILP (eval_sub (test
)))
950 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
951 doc
: /* Return result of expanding macros at top level of FORM.
952 If FORM is not a macro call, it is returned unchanged.
953 Otherwise, the macro is expanded and the expansion is considered
954 in place of FORM. When a non-macro-call results, it is returned.
956 The second optional arg ENVIRONMENT specifies an environment of macro
957 definitions to shadow the loaded ones for use in file byte-compilation. */)
958 (Lisp_Object form
, Lisp_Object environment
)
960 /* With cleanups from Hallvard Furuseth. */
961 register Lisp_Object expander
, sym
, def
, tem
;
965 /* Come back here each time we expand a macro call,
966 in case it expands into another macro call. */
969 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
970 def
= sym
= XCAR (form
);
972 /* Trace symbols aliases to other symbols
973 until we get a symbol that is not an alias. */
974 while (SYMBOLP (def
))
978 tem
= Fassq (sym
, environment
);
981 def
= XSYMBOL (sym
)->function
;
987 /* Right now TEM is the result from SYM in ENVIRONMENT,
988 and if TEM is nil then DEF is SYM's function definition. */
991 /* SYM is not mentioned in ENVIRONMENT.
992 Look at its function definition. */
995 def
= Fautoload_do_load (def
, sym
, Qmacro
);
998 /* Not defined or definition not suitable. */
1000 if (!EQ (XCAR (def
), Qmacro
))
1002 else expander
= XCDR (def
);
1006 expander
= XCDR (tem
);
1007 if (NILP (expander
))
1011 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1012 if (EQ (form
, newform
))
1021 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1022 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1023 TAG is evalled to get the tag to use; it must not be nil.
1025 Then the BODY is executed.
1026 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1027 If no throw happens, `catch' returns the value of the last BODY form.
1028 If a throw happens, it specifies the value to return from `catch'.
1029 usage: (catch TAG BODY...) */)
1032 register Lisp_Object tag
;
1033 struct gcpro gcpro1
;
1036 tag
= eval_sub (Fcar (args
));
1038 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1041 /* Set up a catch, then call C function FUNC on argument ARG.
1042 FUNC should return a Lisp_Object.
1043 This is how catches are done from within C code. */
1046 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1048 /* This structure is made part of the chain `catchlist'. */
1051 /* Fill in the components of c, and put it on the list. */
1055 c
.handlerlist
= handlerlist
;
1056 c
.lisp_eval_depth
= lisp_eval_depth
;
1057 c
.pdlcount
= SPECPDL_INDEX ();
1058 c
.poll_suppress_count
= poll_suppress_count
;
1059 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1060 c
.gcpro
= gcprolist
;
1061 c
.byte_stack
= byte_stack_list
;
1065 if (! sys_setjmp (c
.jmp
))
1066 c
.val
= (*func
) (arg
);
1068 /* Throw works by a longjmp that comes right here. */
1073 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1074 jump to that CATCH, returning VALUE as the value of that catch.
1076 This is the guts of Fthrow and Fsignal; they differ only in the way
1077 they choose the catch tag to throw to. A catch tag for a
1078 condition-case form has a TAG of Qnil.
1080 Before each catch is discarded, unbind all special bindings and
1081 execute all unwind-protect clauses made above that catch. Unwind
1082 the handler stack as we go, so that the proper handlers are in
1083 effect for each unwind-protect clause we run. At the end, restore
1084 some static info saved in CATCH, and longjmp to the location
1087 This is used for correct unwinding in Fthrow and Fsignal. */
1089 static _Noreturn
void
1090 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1094 /* Save the value in the tag. */
1097 /* Restore certain special C variables. */
1098 set_poll_suppress_count (catch->poll_suppress_count
);
1099 unblock_input_to (catch->interrupt_input_blocked
);
1104 last_time
= catchlist
== catch;
1106 /* Unwind the specpdl stack, and then restore the proper set of
1108 unbind_to (catchlist
->pdlcount
, Qnil
);
1109 handlerlist
= catchlist
->handlerlist
;
1110 catchlist
= catchlist
->next
;
1112 while (! last_time
);
1114 byte_stack_list
= catch->byte_stack
;
1115 gcprolist
= catch->gcpro
;
1117 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1119 lisp_eval_depth
= catch->lisp_eval_depth
;
1121 sys_longjmp (catch->jmp
, 1);
1124 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1125 doc
: /* Throw to the catch for TAG and return VALUE from it.
1126 Both TAG and VALUE are evalled. */)
1127 (register Lisp_Object tag
, Lisp_Object value
)
1129 register struct catchtag
*c
;
1132 for (c
= catchlist
; c
; c
= c
->next
)
1134 if (EQ (c
->tag
, tag
))
1135 unwind_to_catch (c
, value
);
1137 xsignal2 (Qno_catch
, tag
, value
);
1141 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1142 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1143 If BODYFORM completes normally, its value is returned
1144 after executing the UNWINDFORMS.
1145 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1146 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1150 ptrdiff_t count
= SPECPDL_INDEX ();
1152 record_unwind_protect (Fprogn
, Fcdr (args
));
1153 val
= eval_sub (Fcar (args
));
1154 return unbind_to (count
, val
);
1157 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1158 doc
: /* Regain control when an error is signaled.
1159 Executes BODYFORM and returns its value if no error happens.
1160 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1161 where the BODY is made of Lisp expressions.
1163 A handler is applicable to an error
1164 if CONDITION-NAME is one of the error's condition names.
1165 If an error happens, the first applicable handler is run.
1167 The car of a handler may be a list of condition names instead of a
1168 single condition name; then it handles all of them. If the special
1169 condition name `debug' is present in this list, it allows another
1170 condition in the list to run the debugger if `debug-on-error' and the
1171 other usual mechanisms says it should (otherwise, `condition-case'
1172 suppresses the debugger).
1174 When a handler handles an error, control returns to the `condition-case'
1175 and it executes the handler's BODY...
1176 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1177 \(If VAR is nil, the handler can't access that information.)
1178 Then the value of the last BODY form is returned from the `condition-case'
1181 See also the function `signal' for more info.
1182 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1185 Lisp_Object var
= Fcar (args
);
1186 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1187 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1189 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1192 /* Like Fcondition_case, but the args are separate
1193 rather than passed in a list. Used by Fbyte_code. */
1196 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1197 Lisp_Object handlers
)
1205 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1211 && (SYMBOLP (XCAR (tem
))
1212 || CONSP (XCAR (tem
))))))
1213 error ("Invalid condition handler: %s",
1214 SDATA (Fprin1_to_string (tem
, Qt
)));
1219 c
.handlerlist
= handlerlist
;
1220 c
.lisp_eval_depth
= lisp_eval_depth
;
1221 c
.pdlcount
= SPECPDL_INDEX ();
1222 c
.poll_suppress_count
= poll_suppress_count
;
1223 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1224 c
.gcpro
= gcprolist
;
1225 c
.byte_stack
= byte_stack_list
;
1226 if (sys_setjmp (c
.jmp
))
1229 specbind (h
.var
, c
.val
);
1230 val
= Fprogn (Fcdr (h
.chosen_clause
));
1232 /* Note that this just undoes the binding of h.var; whoever
1233 longjumped to us unwound the stack to c.pdlcount before
1235 unbind_to (c
.pdlcount
, Qnil
);
1242 h
.handler
= handlers
;
1243 h
.next
= handlerlist
;
1247 val
= eval_sub (bodyform
);
1249 handlerlist
= h
.next
;
1253 /* Call the function BFUN with no arguments, catching errors within it
1254 according to HANDLERS. If there is an error, call HFUN with
1255 one argument which is the data that describes the error:
1258 HANDLERS can be a list of conditions to catch.
1259 If HANDLERS is Qt, catch all errors.
1260 If HANDLERS is Qerror, catch all errors
1261 but allow the debugger to run if that is enabled. */
1264 internal_condition_case (Lisp_Object (*bfun
) (void), 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
;
1294 handlerlist
= h
.next
;
1298 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1301 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1302 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1310 c
.handlerlist
= handlerlist
;
1311 c
.lisp_eval_depth
= lisp_eval_depth
;
1312 c
.pdlcount
= SPECPDL_INDEX ();
1313 c
.poll_suppress_count
= poll_suppress_count
;
1314 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1315 c
.gcpro
= gcprolist
;
1316 c
.byte_stack
= byte_stack_list
;
1317 if (sys_setjmp (c
.jmp
))
1319 return (*hfun
) (c
.val
);
1323 h
.handler
= handlers
;
1325 h
.next
= handlerlist
;
1329 val
= (*bfun
) (arg
);
1331 handlerlist
= h
.next
;
1335 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1339 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1342 Lisp_Object handlers
,
1343 Lisp_Object (*hfun
) (Lisp_Object
))
1351 c
.handlerlist
= handlerlist
;
1352 c
.lisp_eval_depth
= lisp_eval_depth
;
1353 c
.pdlcount
= SPECPDL_INDEX ();
1354 c
.poll_suppress_count
= poll_suppress_count
;
1355 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1356 c
.gcpro
= gcprolist
;
1357 c
.byte_stack
= byte_stack_list
;
1358 if (sys_setjmp (c
.jmp
))
1360 return (*hfun
) (c
.val
);
1364 h
.handler
= handlers
;
1366 h
.next
= handlerlist
;
1370 val
= (*bfun
) (arg1
, arg2
);
1372 handlerlist
= h
.next
;
1376 /* Like internal_condition_case but call BFUN with NARGS as first,
1377 and ARGS as second argument. */
1380 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1383 Lisp_Object handlers
,
1384 Lisp_Object (*hfun
) (Lisp_Object err
,
1394 c
.handlerlist
= handlerlist
;
1395 c
.lisp_eval_depth
= lisp_eval_depth
;
1396 c
.pdlcount
= SPECPDL_INDEX ();
1397 c
.poll_suppress_count
= poll_suppress_count
;
1398 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1399 c
.gcpro
= gcprolist
;
1400 c
.byte_stack
= byte_stack_list
;
1401 if (sys_setjmp (c
.jmp
))
1403 return (*hfun
) (c
.val
, nargs
, args
);
1407 h
.handler
= handlers
;
1409 h
.next
= handlerlist
;
1413 val
= (*bfun
) (nargs
, args
);
1415 handlerlist
= h
.next
;
1420 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1421 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1425 process_quit_flag (void)
1427 Lisp_Object flag
= Vquit_flag
;
1429 if (EQ (flag
, Qkill_emacs
))
1431 if (EQ (Vthrow_on_input
, flag
))
1432 Fthrow (Vthrow_on_input
, Qt
);
1433 Fsignal (Qquit
, Qnil
);
1436 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1437 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1438 This function does not return.
1440 An error symbol is a symbol with an `error-conditions' property
1441 that is a list of condition names.
1442 A handler for any of those names will get to handle this signal.
1443 The symbol `error' should normally be one of them.
1445 DATA should be a list. Its elements are printed as part of the error message.
1446 See Info anchor `(elisp)Definition of signal' for some details on how this
1447 error message is constructed.
1448 If the signal is handled, DATA is made available to the handler.
1449 See also the function `condition-case'. */)
1450 (Lisp_Object error_symbol
, Lisp_Object data
)
1452 /* When memory is full, ERROR-SYMBOL is nil,
1453 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1454 That is a special case--don't do this in other situations. */
1455 Lisp_Object conditions
;
1457 Lisp_Object real_error_symbol
1458 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1459 register Lisp_Object clause
= Qnil
;
1464 if (gc_in_progress
|| waiting_for_input
)
1467 #if 0 /* rms: I don't know why this was here,
1468 but it is surely wrong for an error that is handled. */
1469 #ifdef HAVE_WINDOW_SYSTEM
1470 if (display_hourglass_p
)
1471 cancel_hourglass ();
1475 /* This hook is used by edebug. */
1476 if (! NILP (Vsignal_hook_function
)
1477 && ! NILP (error_symbol
))
1479 /* Edebug takes care of restoring these variables when it exits. */
1480 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1481 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1483 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1484 max_specpdl_size
= SPECPDL_INDEX () + 40;
1486 call2 (Vsignal_hook_function
, error_symbol
, data
);
1489 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1491 /* Remember from where signal was called. Skip over the frame for
1492 `signal' itself. If a frame for `error' follows, skip that,
1493 too. Don't do this when ERROR_SYMBOL is nil, because that
1494 is a memory-full error. */
1495 Vsignaling_function
= Qnil
;
1496 if (!NILP (error_symbol
))
1498 union specbinding
*pdl
= backtrace_next (backtrace_top ());
1499 if (backtrace_p (pdl
) && EQ (backtrace_function (pdl
), Qerror
))
1500 pdl
= backtrace_next (pdl
);
1501 if (backtrace_p (pdl
))
1502 Vsignaling_function
= backtrace_function (pdl
);
1505 for (h
= handlerlist
; h
; h
= h
->next
)
1507 clause
= find_handler_clause (h
->handler
, conditions
);
1512 if (/* Don't run the debugger for a memory-full error.
1513 (There is no room in memory to do that!) */
1514 !NILP (error_symbol
)
1515 && (!NILP (Vdebug_on_signal
)
1516 /* If no handler is present now, try to run the debugger. */
1518 /* A `debug' symbol in the handler list disables the normal
1519 suppression of the debugger. */
1520 || (CONSP (clause
) && CONSP (XCAR (clause
))
1521 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1522 /* Special handler that means "print a message and run debugger
1524 || EQ (h
->handler
, Qerror
)))
1526 bool debugger_called
1527 = maybe_call_debugger (conditions
, error_symbol
, data
);
1528 /* We can't return values to code which signaled an error, but we
1529 can continue code which has signaled a quit. */
1530 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1536 Lisp_Object unwind_data
1537 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1539 h
->chosen_clause
= clause
;
1540 unwind_to_catch (h
->tag
, unwind_data
);
1545 Fthrow (Qtop_level
, Qt
);
1548 if (! NILP (error_symbol
))
1549 data
= Fcons (error_symbol
, data
);
1551 string
= Ferror_message_string (data
);
1552 fatal ("%s", SDATA (string
));
1555 /* Internal version of Fsignal that never returns.
1556 Used for anything but Qquit (which can return from Fsignal). */
1559 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1561 Fsignal (error_symbol
, data
);
1565 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1568 xsignal0 (Lisp_Object error_symbol
)
1570 xsignal (error_symbol
, Qnil
);
1574 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1576 xsignal (error_symbol
, list1 (arg
));
1580 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1582 xsignal (error_symbol
, list2 (arg1
, arg2
));
1586 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1588 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1591 /* Signal `error' with message S, and additional arg ARG.
1592 If ARG is not a genuine list, make it a one-element list. */
1595 signal_error (const char *s
, Lisp_Object arg
)
1597 Lisp_Object tortoise
, hare
;
1599 hare
= tortoise
= arg
;
1600 while (CONSP (hare
))
1607 tortoise
= XCDR (tortoise
);
1609 if (EQ (hare
, tortoise
))
1614 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1616 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1620 /* Return true if LIST is a non-nil atom or
1621 a list containing one of CONDITIONS. */
1624 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1631 while (CONSP (conditions
))
1633 Lisp_Object
this, tail
;
1634 this = XCAR (conditions
);
1635 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1636 if (EQ (XCAR (tail
), this))
1638 conditions
= XCDR (conditions
);
1643 /* Return true if an error with condition-symbols CONDITIONS,
1644 and described by SIGNAL-DATA, should skip the debugger
1645 according to debugger-ignored-errors. */
1648 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1651 bool first_string
= 1;
1652 Lisp_Object error_message
;
1654 error_message
= Qnil
;
1655 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1657 if (STRINGP (XCAR (tail
)))
1661 error_message
= Ferror_message_string (data
);
1665 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1670 Lisp_Object contail
;
1672 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1673 if (EQ (XCAR (tail
), XCAR (contail
)))
1681 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1682 SIG and DATA describe the signal. There are two ways to pass them:
1683 = SIG is the error symbol, and DATA is the rest of the data.
1684 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1685 This is for memory-full errors only. */
1687 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1689 Lisp_Object combined_data
;
1691 combined_data
= Fcons (sig
, data
);
1694 /* Don't try to run the debugger with interrupts blocked.
1695 The editing loop would return anyway. */
1696 ! input_blocked_p ()
1697 && NILP (Vinhibit_debugger
)
1698 /* Does user want to enter debugger for this kind of error? */
1701 : wants_debugger (Vdebug_on_error
, conditions
))
1702 && ! skip_debugger (conditions
, combined_data
)
1703 /* RMS: What's this for? */
1704 && when_entered_debugger
< num_nonmacro_input_events
)
1706 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1714 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1716 register Lisp_Object h
;
1718 /* t is used by handlers for all conditions, set up by C code. */
1719 if (EQ (handlers
, Qt
))
1722 /* error is used similarly, but means print an error message
1723 and run the debugger if that is enabled. */
1724 if (EQ (handlers
, Qerror
))
1727 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1729 Lisp_Object handler
= XCAR (h
);
1730 Lisp_Object condit
, tem
;
1732 if (!CONSP (handler
))
1734 condit
= XCAR (handler
);
1735 /* Handle a single condition name in handler HANDLER. */
1736 if (SYMBOLP (condit
))
1738 tem
= Fmemq (Fcar (handler
), conditions
);
1742 /* Handle a list of condition names in handler HANDLER. */
1743 else if (CONSP (condit
))
1746 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1748 tem
= Fmemq (XCAR (tail
), conditions
);
1759 /* Dump an error message; called like vprintf. */
1761 verror (const char *m
, va_list ap
)
1764 ptrdiff_t size
= sizeof buf
;
1765 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1770 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1771 string
= make_string (buffer
, used
);
1775 xsignal1 (Qerror
, string
);
1779 /* Dump an error message; called like printf. */
1783 error (const char *m
, ...)
1790 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1791 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1792 This means it contains a description for how to read arguments to give it.
1793 The value is nil for an invalid function or a symbol with no function
1796 Interactively callable functions include strings and vectors (treated
1797 as keyboard macros), lambda-expressions that contain a top-level call
1798 to `interactive', autoload definitions made by `autoload' with non-nil
1799 fourth argument, and some of the built-in functions of Lisp.
1801 Also, a symbol satisfies `commandp' if its function definition does so.
1803 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1804 then strings and vectors are not accepted. */)
1805 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1807 register Lisp_Object fun
;
1808 register Lisp_Object funcar
;
1809 Lisp_Object if_prop
= Qnil
;
1813 fun
= indirect_function (fun
); /* Check cycles. */
1817 /* Check an `interactive-form' property if present, analogous to the
1818 function-documentation property. */
1820 while (SYMBOLP (fun
))
1822 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1825 fun
= Fsymbol_function (fun
);
1828 /* Emacs primitives are interactive if their DEFUN specifies an
1829 interactive spec. */
1831 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1833 /* Bytecode objects are interactive if they are long enough to
1834 have an element whose index is COMPILED_INTERACTIVE, which is
1835 where the interactive spec is stored. */
1836 else if (COMPILEDP (fun
))
1837 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1840 /* Strings and vectors are keyboard macros. */
1841 if (STRINGP (fun
) || VECTORP (fun
))
1842 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1844 /* Lists may represent commands. */
1847 funcar
= XCAR (fun
);
1848 if (EQ (funcar
, Qclosure
))
1849 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1851 else if (EQ (funcar
, Qlambda
))
1852 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1853 else if (EQ (funcar
, Qautoload
))
1854 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1859 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1860 doc
: /* Define FUNCTION to autoload from FILE.
1861 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1862 Third arg DOCSTRING is documentation for the function.
1863 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1864 Fifth arg TYPE indicates the type of the object:
1865 nil or omitted says FUNCTION is a function,
1866 `keymap' says FUNCTION is really a keymap, and
1867 `macro' or t says FUNCTION is really a macro.
1868 Third through fifth args give info about the real definition.
1869 They default to nil.
1870 If FUNCTION is already defined other than as an autoload,
1871 this does nothing and returns nil. */)
1872 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1874 CHECK_SYMBOL (function
);
1875 CHECK_STRING (file
);
1877 /* If function is defined and not as an autoload, don't override. */
1878 if (!NILP (XSYMBOL (function
)->function
)
1879 && !AUTOLOADP (XSYMBOL (function
)->function
))
1882 if (!NILP (Vpurify_flag
) && EQ (docstring
, make_number (0)))
1883 /* `read1' in lread.c has found the docstring starting with "\
1884 and assumed the docstring will be provided by Snarf-documentation, so it
1885 passed us 0 instead. But that leads to accidental sharing in purecopy's
1886 hash-consing, so we use a (hopefully) unique integer instead. */
1887 docstring
= make_number (XHASH (function
));
1888 return Fdefalias (function
,
1889 list5 (Qautoload
, file
, docstring
, interactive
, type
),
1894 un_autoload (Lisp_Object oldqueue
)
1896 register Lisp_Object queue
, first
, second
;
1898 /* Queue to unwind is current value of Vautoload_queue.
1899 oldqueue is the shadowed value to leave in Vautoload_queue. */
1900 queue
= Vautoload_queue
;
1901 Vautoload_queue
= oldqueue
;
1902 while (CONSP (queue
))
1904 first
= XCAR (queue
);
1905 second
= Fcdr (first
);
1906 first
= Fcar (first
);
1907 if (EQ (first
, make_number (0)))
1910 Ffset (first
, second
);
1911 queue
= XCDR (queue
);
1916 /* Load an autoloaded function.
1917 FUNNAME is the symbol which is the function's name.
1918 FUNDEF is the autoload definition (a list). */
1920 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1921 doc
: /* Load FUNDEF which should be an autoload.
1922 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1923 in which case the function returns the new autoloaded function value.
1924 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1925 it is defines a macro. */)
1926 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1928 ptrdiff_t count
= SPECPDL_INDEX ();
1929 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1931 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1934 if (EQ (macro_only
, Qmacro
))
1936 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1937 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1941 /* This is to make sure that loadup.el gives a clear picture
1942 of what files are preloaded and when. */
1943 if (! NILP (Vpurify_flag
))
1944 error ("Attempt to autoload %s while preparing to dump",
1945 SDATA (SYMBOL_NAME (funname
)));
1947 CHECK_SYMBOL (funname
);
1948 GCPRO3 (funname
, fundef
, macro_only
);
1950 /* Preserve the match data. */
1951 record_unwind_save_match_data ();
1953 /* If autoloading gets an error (which includes the error of failing
1954 to define the function being called), we use Vautoload_queue
1955 to undo function definitions and `provide' calls made by
1956 the function. We do this in the specific case of autoloading
1957 because autoloading is not an explicit request "load this file",
1958 but rather a request to "call this function".
1960 The value saved here is to be restored into Vautoload_queue. */
1961 record_unwind_protect (un_autoload
, Vautoload_queue
);
1962 Vautoload_queue
= Qt
;
1963 /* If `macro_only', assume this autoload to be a "best-effort",
1964 so don't signal an error if autoloading fails. */
1965 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
1967 /* Once loading finishes, don't undo it. */
1968 Vautoload_queue
= Qt
;
1969 unbind_to (count
, Qnil
);
1977 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
1979 if (!NILP (Fequal (fun
, fundef
)))
1980 error ("Autoloading failed to define function %s",
1981 SDATA (SYMBOL_NAME (funname
)));
1988 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
1989 doc
: /* Evaluate FORM and return its value.
1990 If LEXICAL is t, evaluate using lexical scoping. */)
1991 (Lisp_Object form
, Lisp_Object lexical
)
1993 ptrdiff_t count
= SPECPDL_INDEX ();
1994 specbind (Qinternal_interpreter_environment
,
1995 CONSP (lexical
) || NILP (lexical
) ? lexical
: Fcons (Qt
, Qnil
));
1996 return unbind_to (count
, eval_sub (form
));
2002 ptrdiff_t count
= SPECPDL_INDEX ();
2003 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
- 1000);
2004 union specbinding
*pdlvec
= specpdl
- 1;
2005 ptrdiff_t pdlvecsize
= specpdl_size
+ 1;
2006 if (max_size
<= specpdl_size
)
2008 if (max_specpdl_size
< 400)
2009 max_size
= max_specpdl_size
= 400;
2010 if (max_size
<= specpdl_size
)
2011 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
2013 pdlvec
= xpalloc (pdlvec
, &pdlvecsize
, 1, max_size
+ 1, sizeof *specpdl
);
2014 specpdl
= pdlvec
+ 1;
2015 specpdl_size
= pdlvecsize
- 1;
2016 specpdl_ptr
= specpdl
+ count
;
2020 record_in_backtrace (Lisp_Object function
, Lisp_Object
*args
, ptrdiff_t nargs
)
2022 eassert (nargs
>= UNEVALLED
);
2023 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2025 specpdl_ptr
->bt
.kind
= SPECPDL_BACKTRACE
;
2026 specpdl_ptr
->bt
.debug_on_exit
= false;
2027 specpdl_ptr
->bt
.function
= function
;
2028 specpdl_ptr
->bt
.args
= args
;
2029 specpdl_ptr
->bt
.nargs
= nargs
;
2033 /* Eval a sub-expression of the current expression (i.e. in the same
2036 eval_sub (Lisp_Object form
)
2038 Lisp_Object fun
, val
, original_fun
, original_args
;
2040 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2044 /* Look up its binding in the lexical environment.
2045 We do not pay attention to the declared_special flag here, since we
2046 already did that when let-binding the variable. */
2047 Lisp_Object lex_binding
2048 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2049 ? Fassq (form
, Vinternal_interpreter_environment
)
2051 if (CONSP (lex_binding
))
2052 return XCDR (lex_binding
);
2054 return Fsymbol_value (form
);
2066 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2068 if (max_lisp_eval_depth
< 100)
2069 max_lisp_eval_depth
= 100;
2070 if (lisp_eval_depth
> max_lisp_eval_depth
)
2071 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2074 original_fun
= XCAR (form
);
2075 original_args
= XCDR (form
);
2077 /* This also protects them from gc. */
2078 record_in_backtrace (original_fun
, &original_args
, UNEVALLED
);
2080 if (debug_on_next_call
)
2081 do_debug_on_call (Qt
);
2083 /* At this point, only original_fun and original_args
2084 have values that will be used below. */
2087 /* Optimize for no indirection. */
2089 if (SYMBOLP (fun
) && !NILP (fun
)
2090 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2091 fun
= indirect_function (fun
);
2095 Lisp_Object numargs
;
2096 Lisp_Object argvals
[8];
2097 Lisp_Object args_left
;
2098 register int i
, maxargs
;
2100 args_left
= original_args
;
2101 numargs
= Flength (args_left
);
2105 if (XINT (numargs
) < XSUBR (fun
)->min_args
2106 || (XSUBR (fun
)->max_args
>= 0
2107 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2108 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2110 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2111 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2112 else if (XSUBR (fun
)->max_args
== MANY
)
2114 /* Pass a vector of evaluated arguments. */
2116 ptrdiff_t argnum
= 0;
2119 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2121 GCPRO3 (args_left
, fun
, fun
);
2125 while (!NILP (args_left
))
2127 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2128 args_left
= Fcdr (args_left
);
2129 gcpro3
.nvars
= argnum
;
2132 set_backtrace_args (specpdl_ptr
- 1, vals
);
2133 set_backtrace_nargs (specpdl_ptr
- 1, XINT (numargs
));
2135 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2141 GCPRO3 (args_left
, fun
, fun
);
2142 gcpro3
.var
= argvals
;
2145 maxargs
= XSUBR (fun
)->max_args
;
2146 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2148 argvals
[i
] = eval_sub (Fcar (args_left
));
2154 set_backtrace_args (specpdl_ptr
- 1, argvals
);
2155 set_backtrace_nargs (specpdl_ptr
- 1, XINT (numargs
));
2160 val
= (XSUBR (fun
)->function
.a0 ());
2163 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2166 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2169 val
= (XSUBR (fun
)->function
.a3
2170 (argvals
[0], argvals
[1], argvals
[2]));
2173 val
= (XSUBR (fun
)->function
.a4
2174 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2177 val
= (XSUBR (fun
)->function
.a5
2178 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2182 val
= (XSUBR (fun
)->function
.a6
2183 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2184 argvals
[4], argvals
[5]));
2187 val
= (XSUBR (fun
)->function
.a7
2188 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2189 argvals
[4], argvals
[5], argvals
[6]));
2193 val
= (XSUBR (fun
)->function
.a8
2194 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2195 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2199 /* Someone has created a subr that takes more arguments than
2200 is supported by this code. We need to either rewrite the
2201 subr to use a different argument protocol, or add more
2202 cases to this switch. */
2207 else if (COMPILEDP (fun
))
2208 val
= apply_lambda (fun
, original_args
);
2212 xsignal1 (Qvoid_function
, original_fun
);
2214 xsignal1 (Qinvalid_function
, original_fun
);
2215 funcar
= XCAR (fun
);
2216 if (!SYMBOLP (funcar
))
2217 xsignal1 (Qinvalid_function
, original_fun
);
2218 if (EQ (funcar
, Qautoload
))
2220 Fautoload_do_load (fun
, original_fun
, Qnil
);
2223 if (EQ (funcar
, Qmacro
))
2225 ptrdiff_t count
= SPECPDL_INDEX ();
2227 /* Bind lexical-binding during expansion of the macro, so the
2228 macro can know reliably if the code it outputs will be
2229 interpreted using lexical-binding or not. */
2230 specbind (Qlexical_binding
,
2231 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2232 exp
= apply1 (Fcdr (fun
), original_args
);
2233 unbind_to (count
, Qnil
);
2234 val
= eval_sub (exp
);
2236 else if (EQ (funcar
, Qlambda
)
2237 || EQ (funcar
, Qclosure
))
2238 val
= apply_lambda (fun
, original_args
);
2240 xsignal1 (Qinvalid_function
, original_fun
);
2245 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2246 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2252 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2253 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2254 Then return the value FUNCTION returns.
2255 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2256 usage: (apply FUNCTION &rest ARGUMENTS) */)
2257 (ptrdiff_t nargs
, Lisp_Object
*args
)
2261 register Lisp_Object spread_arg
;
2262 register Lisp_Object
*funcall_args
;
2263 Lisp_Object fun
, retval
;
2264 struct gcpro gcpro1
;
2269 spread_arg
= args
[nargs
- 1];
2270 CHECK_LIST (spread_arg
);
2272 numargs
= XINT (Flength (spread_arg
));
2275 return Ffuncall (nargs
- 1, args
);
2276 else if (numargs
== 1)
2278 args
[nargs
- 1] = XCAR (spread_arg
);
2279 return Ffuncall (nargs
, args
);
2282 numargs
+= nargs
- 2;
2284 /* Optimize for no indirection. */
2285 if (SYMBOLP (fun
) && !NILP (fun
)
2286 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2287 fun
= indirect_function (fun
);
2290 /* Let funcall get the error. */
2297 if (numargs
< XSUBR (fun
)->min_args
2298 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2299 goto funcall
; /* Let funcall get the error. */
2300 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2302 /* Avoid making funcall cons up a yet another new vector of arguments
2303 by explicitly supplying nil's for optional values. */
2304 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2305 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2306 funcall_args
[++i
] = Qnil
;
2307 GCPRO1 (*funcall_args
);
2308 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2312 /* We add 1 to numargs because funcall_args includes the
2313 function itself as well as its arguments. */
2316 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2317 GCPRO1 (*funcall_args
);
2318 gcpro1
.nvars
= 1 + numargs
;
2321 memcpy (funcall_args
, args
, nargs
* word_size
);
2322 /* Spread the last arg we got. Its first element goes in
2323 the slot that it used to occupy, hence this value of I. */
2325 while (!NILP (spread_arg
))
2327 funcall_args
[i
++] = XCAR (spread_arg
);
2328 spread_arg
= XCDR (spread_arg
);
2331 /* By convention, the caller needs to gcpro Ffuncall's args. */
2332 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2339 /* Run hook variables in various ways. */
2342 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2344 Ffuncall (nargs
, args
);
2348 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2349 doc
: /* Run each hook in HOOKS.
2350 Each argument should be a symbol, a hook variable.
2351 These symbols are processed in the order specified.
2352 If a hook symbol has a non-nil value, that value may be a function
2353 or a list of functions to be called to run the hook.
2354 If the value is a function, it is called with no arguments.
2355 If it is a list, the elements are called, in order, with no arguments.
2357 Major modes should not use this function directly to run their mode
2358 hook; they should use `run-mode-hooks' instead.
2360 Do not use `make-local-variable' to make a hook variable buffer-local.
2361 Instead, use `add-hook' and specify t for the LOCAL argument.
2362 usage: (run-hooks &rest HOOKS) */)
2363 (ptrdiff_t nargs
, Lisp_Object
*args
)
2365 Lisp_Object hook
[1];
2368 for (i
= 0; i
< nargs
; i
++)
2371 run_hook_with_args (1, hook
, funcall_nil
);
2377 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2378 Srun_hook_with_args
, 1, MANY
, 0,
2379 doc
: /* Run HOOK with the specified arguments ARGS.
2380 HOOK should be a symbol, a hook variable. The value of HOOK
2381 may be nil, a function, or a list of functions. Call each
2382 function in order with arguments ARGS. The final return value
2385 Do not use `make-local-variable' to make a hook variable buffer-local.
2386 Instead, use `add-hook' and specify t for the LOCAL argument.
2387 usage: (run-hook-with-args HOOK &rest ARGS) */)
2388 (ptrdiff_t nargs
, Lisp_Object
*args
)
2390 return run_hook_with_args (nargs
, args
, funcall_nil
);
2393 /* NB this one still documents a specific non-nil return value.
2394 (As did run-hook-with-args and run-hook-with-args-until-failure
2395 until they were changed in 24.1.) */
2396 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2397 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2398 doc
: /* Run HOOK with the specified arguments ARGS.
2399 HOOK should be a symbol, a hook variable. The value of HOOK
2400 may be nil, a function, or a list of functions. Call each
2401 function in order with arguments ARGS, stopping at the first
2402 one that returns non-nil, and return that value. Otherwise (if
2403 all functions return nil, or if there are no functions to call),
2406 Do not use `make-local-variable' to make a hook variable buffer-local.
2407 Instead, use `add-hook' and specify t for the LOCAL argument.
2408 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2409 (ptrdiff_t nargs
, Lisp_Object
*args
)
2411 return run_hook_with_args (nargs
, args
, Ffuncall
);
2415 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2417 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2420 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2421 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2422 doc
: /* Run HOOK with the specified arguments ARGS.
2423 HOOK should be a symbol, a hook variable. The value of HOOK
2424 may be nil, a function, or a list of functions. Call each
2425 function in order with arguments ARGS, stopping at the first
2426 one that returns nil, and return nil. Otherwise (if all functions
2427 return non-nil, or if there are no functions to call), return non-nil
2428 \(do not rely on the precise return value in this case).
2430 Do not use `make-local-variable' to make a hook variable buffer-local.
2431 Instead, use `add-hook' and specify t for the LOCAL argument.
2432 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2433 (ptrdiff_t nargs
, Lisp_Object
*args
)
2435 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2439 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2441 Lisp_Object tmp
= args
[0], ret
;
2444 ret
= Ffuncall (nargs
, args
);
2450 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2451 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2452 I.e. instead of calling each function FUN directly with arguments ARGS,
2453 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2454 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2455 aborts and returns that value.
2456 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2457 (ptrdiff_t nargs
, Lisp_Object
*args
)
2459 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2462 /* ARGS[0] should be a hook symbol.
2463 Call each of the functions in the hook value, passing each of them
2464 as arguments all the rest of ARGS (all NARGS - 1 elements).
2465 FUNCALL specifies how to call each function on the hook.
2466 The caller (or its caller, etc) must gcpro all of ARGS,
2467 except that it isn't necessary to gcpro ARGS[0]. */
2470 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2471 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2473 Lisp_Object sym
, val
, ret
= Qnil
;
2474 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2476 /* If we are dying or still initializing,
2477 don't do anything--it would probably crash if we tried. */
2478 if (NILP (Vrun_hooks
))
2482 val
= find_symbol_value (sym
);
2484 if (EQ (val
, Qunbound
) || NILP (val
))
2486 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2489 return funcall (nargs
, args
);
2493 Lisp_Object global_vals
= Qnil
;
2494 GCPRO3 (sym
, val
, global_vals
);
2497 CONSP (val
) && NILP (ret
);
2500 if (EQ (XCAR (val
), Qt
))
2502 /* t indicates this hook has a local binding;
2503 it means to run the global binding too. */
2504 global_vals
= Fdefault_value (sym
);
2505 if (NILP (global_vals
)) continue;
2507 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2509 args
[0] = global_vals
;
2510 ret
= funcall (nargs
, args
);
2515 CONSP (global_vals
) && NILP (ret
);
2516 global_vals
= XCDR (global_vals
))
2518 args
[0] = XCAR (global_vals
);
2519 /* In a global value, t should not occur. If it does, we
2520 must ignore it to avoid an endless loop. */
2521 if (!EQ (args
[0], Qt
))
2522 ret
= funcall (nargs
, args
);
2528 args
[0] = XCAR (val
);
2529 ret
= funcall (nargs
, args
);
2538 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2541 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2543 Lisp_Object temp
[3];
2548 Frun_hook_with_args (3, temp
);
2551 /* Apply fn to arg. */
2553 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2555 struct gcpro gcpro1
;
2559 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2562 Lisp_Object args
[2];
2566 RETURN_UNGCPRO (Fapply (2, args
));
2570 /* Call function fn on no arguments. */
2572 call0 (Lisp_Object fn
)
2574 struct gcpro gcpro1
;
2577 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2580 /* Call function fn with 1 argument arg1. */
2583 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2585 struct gcpro gcpro1
;
2586 Lisp_Object args
[2];
2592 RETURN_UNGCPRO (Ffuncall (2, args
));
2595 /* Call function fn with 2 arguments arg1, arg2. */
2598 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2600 struct gcpro gcpro1
;
2601 Lisp_Object args
[3];
2607 RETURN_UNGCPRO (Ffuncall (3, args
));
2610 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2613 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2615 struct gcpro gcpro1
;
2616 Lisp_Object args
[4];
2623 RETURN_UNGCPRO (Ffuncall (4, args
));
2626 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2629 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2632 struct gcpro gcpro1
;
2633 Lisp_Object args
[5];
2641 RETURN_UNGCPRO (Ffuncall (5, args
));
2644 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2647 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2648 Lisp_Object arg4
, Lisp_Object arg5
)
2650 struct gcpro gcpro1
;
2651 Lisp_Object args
[6];
2660 RETURN_UNGCPRO (Ffuncall (6, args
));
2663 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2666 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2667 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2669 struct gcpro gcpro1
;
2670 Lisp_Object args
[7];
2680 RETURN_UNGCPRO (Ffuncall (7, args
));
2683 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2686 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2687 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2689 struct gcpro gcpro1
;
2690 Lisp_Object args
[8];
2701 RETURN_UNGCPRO (Ffuncall (8, args
));
2704 /* The caller should GCPRO all the elements of ARGS. */
2706 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2707 doc
: /* Non-nil if OBJECT is a function. */)
2708 (Lisp_Object object
)
2710 if (FUNCTIONP (object
))
2715 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2716 doc
: /* Call first argument as a function, passing remaining arguments to it.
2717 Return the value that function returns.
2718 Thus, (funcall 'cons 'x 'y) returns (x . y).
2719 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2720 (ptrdiff_t nargs
, Lisp_Object
*args
)
2722 Lisp_Object fun
, original_fun
;
2724 ptrdiff_t numargs
= nargs
- 1;
2725 Lisp_Object lisp_numargs
;
2727 register Lisp_Object
*internal_args
;
2732 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2734 if (max_lisp_eval_depth
< 100)
2735 max_lisp_eval_depth
= 100;
2736 if (lisp_eval_depth
> max_lisp_eval_depth
)
2737 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2740 /* This also GCPROs them. */
2741 record_in_backtrace (args
[0], &args
[1], nargs
- 1);
2743 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2746 if (debug_on_next_call
)
2747 do_debug_on_call (Qlambda
);
2751 original_fun
= args
[0];
2755 /* Optimize for no indirection. */
2757 if (SYMBOLP (fun
) && !NILP (fun
)
2758 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2759 fun
= indirect_function (fun
);
2763 if (numargs
< XSUBR (fun
)->min_args
2764 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2766 XSETFASTINT (lisp_numargs
, numargs
);
2767 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2770 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2771 xsignal1 (Qinvalid_function
, original_fun
);
2773 else if (XSUBR (fun
)->max_args
== MANY
)
2774 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2777 if (XSUBR (fun
)->max_args
> numargs
)
2779 internal_args
= alloca (XSUBR (fun
)->max_args
2780 * sizeof *internal_args
);
2781 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2782 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2783 internal_args
[i
] = Qnil
;
2786 internal_args
= args
+ 1;
2787 switch (XSUBR (fun
)->max_args
)
2790 val
= (XSUBR (fun
)->function
.a0 ());
2793 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2796 val
= (XSUBR (fun
)->function
.a2
2797 (internal_args
[0], internal_args
[1]));
2800 val
= (XSUBR (fun
)->function
.a3
2801 (internal_args
[0], internal_args
[1], internal_args
[2]));
2804 val
= (XSUBR (fun
)->function
.a4
2805 (internal_args
[0], internal_args
[1], internal_args
[2],
2809 val
= (XSUBR (fun
)->function
.a5
2810 (internal_args
[0], internal_args
[1], internal_args
[2],
2811 internal_args
[3], internal_args
[4]));
2814 val
= (XSUBR (fun
)->function
.a6
2815 (internal_args
[0], internal_args
[1], internal_args
[2],
2816 internal_args
[3], internal_args
[4], internal_args
[5]));
2819 val
= (XSUBR (fun
)->function
.a7
2820 (internal_args
[0], internal_args
[1], internal_args
[2],
2821 internal_args
[3], internal_args
[4], internal_args
[5],
2826 val
= (XSUBR (fun
)->function
.a8
2827 (internal_args
[0], internal_args
[1], internal_args
[2],
2828 internal_args
[3], internal_args
[4], internal_args
[5],
2829 internal_args
[6], internal_args
[7]));
2834 /* If a subr takes more than 8 arguments without using MANY
2835 or UNEVALLED, we need to extend this function to support it.
2836 Until this is done, there is no way to call the function. */
2841 else if (COMPILEDP (fun
))
2842 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2846 xsignal1 (Qvoid_function
, original_fun
);
2848 xsignal1 (Qinvalid_function
, original_fun
);
2849 funcar
= XCAR (fun
);
2850 if (!SYMBOLP (funcar
))
2851 xsignal1 (Qinvalid_function
, original_fun
);
2852 if (EQ (funcar
, Qlambda
)
2853 || EQ (funcar
, Qclosure
))
2854 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2855 else if (EQ (funcar
, Qautoload
))
2857 Fautoload_do_load (fun
, original_fun
, Qnil
);
2862 xsignal1 (Qinvalid_function
, original_fun
);
2866 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2867 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2873 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2875 Lisp_Object args_left
;
2878 register Lisp_Object
*arg_vector
;
2879 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2880 register Lisp_Object tem
;
2883 numargs
= XFASTINT (Flength (args
));
2884 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2887 GCPRO3 (*arg_vector
, args_left
, fun
);
2890 for (i
= 0; i
< numargs
; )
2892 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2893 tem
= eval_sub (tem
);
2894 arg_vector
[i
++] = tem
;
2900 set_backtrace_args (specpdl_ptr
- 1, arg_vector
);
2901 set_backtrace_nargs (specpdl_ptr
- 1, i
);
2902 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2904 /* Do the debug-on-exit now, while arg_vector still exists. */
2905 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2907 /* Don't do it again when we return to eval. */
2908 set_backtrace_debug_on_exit (specpdl_ptr
- 1, false);
2909 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2915 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2916 and return the result of evaluation.
2917 FUN must be either a lambda-expression or a compiled-code object. */
2920 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2921 register Lisp_Object
*arg_vector
)
2923 Lisp_Object val
, syms_left
, next
, lexenv
;
2924 ptrdiff_t count
= SPECPDL_INDEX ();
2926 bool optional
, rest
;
2930 if (EQ (XCAR (fun
), Qclosure
))
2932 fun
= XCDR (fun
); /* Drop `closure'. */
2933 lexenv
= XCAR (fun
);
2934 CHECK_LIST_CONS (fun
, fun
);
2938 syms_left
= XCDR (fun
);
2939 if (CONSP (syms_left
))
2940 syms_left
= XCAR (syms_left
);
2942 xsignal1 (Qinvalid_function
, fun
);
2944 else if (COMPILEDP (fun
))
2946 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2947 if (INTEGERP (syms_left
))
2948 /* A byte-code object with a non-nil `push args' slot means we
2949 shouldn't bind any arguments, instead just call the byte-code
2950 interpreter directly; it will push arguments as necessary.
2952 Byte-code objects with either a non-existent, or a nil value for
2953 the `push args' slot (the default), have dynamically-bound
2954 arguments, and use the argument-binding code below instead (as do
2955 all interpreted functions, even lexically bound ones). */
2957 /* If we have not actually read the bytecode string
2958 and constants vector yet, fetch them from the file. */
2959 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2960 Ffetch_bytecode (fun
);
2961 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2962 AREF (fun
, COMPILED_CONSTANTS
),
2963 AREF (fun
, COMPILED_STACK_DEPTH
),
2972 i
= optional
= rest
= 0;
2973 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2977 next
= XCAR (syms_left
);
2978 if (!SYMBOLP (next
))
2979 xsignal1 (Qinvalid_function
, fun
);
2981 if (EQ (next
, Qand_rest
))
2983 else if (EQ (next
, Qand_optional
))
2990 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2994 arg
= arg_vector
[i
++];
2996 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3000 /* Bind the argument. */
3001 if (!NILP (lexenv
) && SYMBOLP (next
))
3002 /* Lexically bind NEXT by adding it to the lexenv alist. */
3003 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3005 /* Dynamically bind NEXT. */
3006 specbind (next
, arg
);
3010 if (!NILP (syms_left
))
3011 xsignal1 (Qinvalid_function
, fun
);
3013 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3015 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3016 /* Instantiate a new lexical environment. */
3017 specbind (Qinternal_interpreter_environment
, lexenv
);
3020 val
= Fprogn (XCDR (XCDR (fun
)));
3023 /* If we have not actually read the bytecode string
3024 and constants vector yet, fetch them from the file. */
3025 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3026 Ffetch_bytecode (fun
);
3027 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3028 AREF (fun
, COMPILED_CONSTANTS
),
3029 AREF (fun
, COMPILED_STACK_DEPTH
),
3033 return unbind_to (count
, val
);
3036 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3038 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3039 (Lisp_Object object
)
3043 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3045 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3048 tem
= AREF (object
, COMPILED_BYTECODE
);
3049 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3050 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3052 error ("Invalid byte code");
3054 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3055 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3060 /* Return true if SYMBOL currently has a let-binding
3061 which was made in the buffer that is now current. */
3064 let_shadows_buffer_binding_p (struct Lisp_Symbol
*symbol
)
3066 union specbinding
*p
;
3067 Lisp_Object buf
= Fcurrent_buffer ();
3069 for (p
= specpdl_ptr
; p
> specpdl
; )
3070 if ((--p
)->kind
> SPECPDL_LET
)
3072 struct Lisp_Symbol
*let_bound_symbol
= XSYMBOL (specpdl_symbol (p
));
3073 eassert (let_bound_symbol
->redirect
!= SYMBOL_VARALIAS
);
3074 if (symbol
== let_bound_symbol
3075 && EQ (specpdl_where (p
), buf
))
3083 let_shadows_global_binding_p (Lisp_Object symbol
)
3085 union specbinding
*p
;
3087 for (p
= specpdl_ptr
; p
> specpdl
; )
3088 if ((--p
)->kind
>= SPECPDL_LET
&& EQ (specpdl_symbol (p
), symbol
))
3094 /* `specpdl_ptr->symbol' is a field which describes which variable is
3095 let-bound, so it can be properly undone when we unbind_to.
3096 It can have the following two shapes:
3097 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3098 a symbol that is not buffer-local (at least at the time
3099 the let binding started). Note also that it should not be
3100 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3102 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3103 variable SYMBOL which can be buffer-local. WHERE tells us
3104 which buffer is affected (or nil if the let-binding affects the
3105 global value of the variable) and BUFFER tells us which buffer was
3106 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3107 BUFFER did not yet have a buffer-local value). */
3110 specbind (Lisp_Object symbol
, Lisp_Object value
)
3112 struct Lisp_Symbol
*sym
;
3114 CHECK_SYMBOL (symbol
);
3115 sym
= XSYMBOL (symbol
);
3116 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3120 switch (sym
->redirect
)
3122 case SYMBOL_VARALIAS
:
3123 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3124 case SYMBOL_PLAINVAL
:
3125 /* The most common case is that of a non-constant symbol with a
3126 trivial value. Make that as fast as we can. */
3127 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3128 specpdl_ptr
->let
.symbol
= symbol
;
3129 specpdl_ptr
->let
.old_value
= SYMBOL_VAL (sym
);
3132 SET_SYMBOL_VAL (sym
, value
);
3134 set_internal (symbol
, value
, Qnil
, 1);
3136 case SYMBOL_LOCALIZED
:
3137 if (SYMBOL_BLV (sym
)->frame_local
)
3138 error ("Frame-local vars cannot be let-bound");
3139 case SYMBOL_FORWARDED
:
3141 Lisp_Object ovalue
= find_symbol_value (symbol
);
3142 specpdl_ptr
->let
.kind
= SPECPDL_LET_LOCAL
;
3143 specpdl_ptr
->let
.symbol
= symbol
;
3144 specpdl_ptr
->let
.old_value
= ovalue
;
3145 specpdl_ptr
->let
.where
= Fcurrent_buffer ();
3147 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3148 || (EQ (SYMBOL_BLV (sym
)->where
, Fcurrent_buffer ())));
3150 if (sym
->redirect
== SYMBOL_LOCALIZED
)
3152 if (!blv_found (SYMBOL_BLV (sym
)))
3153 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3155 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3157 /* If SYMBOL is a per-buffer variable which doesn't have a
3158 buffer-local value here, make the `let' change the global
3159 value by changing the value of SYMBOL in all buffers not
3160 having their own value. This is consistent with what
3161 happens with other buffer-local variables. */
3162 if (NILP (Flocal_variable_p (symbol
, Qnil
)))
3164 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3166 Fset_default (symbol
, value
);
3171 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3174 set_internal (symbol
, value
, Qnil
, 1);
3177 default: emacs_abort ();
3182 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3184 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3186 specpdl_ptr
->unwind
.kind
= SPECPDL_UNWIND
;
3187 specpdl_ptr
->unwind
.func
= function
;
3188 specpdl_ptr
->unwind
.arg
= arg
;
3193 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3195 Lisp_Object quitf
= Vquit_flag
;
3196 struct gcpro gcpro1
, gcpro2
;
3198 GCPRO2 (value
, quitf
);
3201 while (specpdl_ptr
!= specpdl
+ count
)
3203 /* Decrement specpdl_ptr before we do the work to unbind it, so
3204 that an error in unbinding won't try to unbind the same entry
3205 again. Take care to copy any parts of the binding needed
3206 before invoking any code that can make more bindings. */
3210 switch (specpdl_ptr
->kind
)
3212 case SPECPDL_UNWIND
:
3213 specpdl_func (specpdl_ptr
) (specpdl_arg (specpdl_ptr
));
3216 /* If variable has a trivial value (no forwarding), we can
3217 just set it. No need to check for constant symbols here,
3218 since that was already done by specbind. */
3219 if (XSYMBOL (specpdl_symbol (specpdl_ptr
))->redirect
3221 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (specpdl_ptr
)),
3222 specpdl_old_value (specpdl_ptr
));
3224 /* NOTE: we only ever come here if make_local_foo was used for
3225 the first time on this var within this let. */
3226 Fset_default (specpdl_symbol (specpdl_ptr
),
3227 specpdl_old_value (specpdl_ptr
));
3229 case SPECPDL_BACKTRACE
:
3231 case SPECPDL_LET_LOCAL
:
3232 case SPECPDL_LET_DEFAULT
:
3233 { /* If the symbol is a list, it is really (SYMBOL WHERE
3234 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3235 frame. If WHERE is a buffer or frame, this indicates we
3236 bound a variable that had a buffer-local or frame-local
3237 binding. WHERE nil means that the variable had the default
3238 value when it was bound. CURRENT-BUFFER is the buffer that
3239 was current when the variable was bound. */
3240 Lisp_Object symbol
= specpdl_symbol (specpdl_ptr
);
3241 Lisp_Object where
= specpdl_where (specpdl_ptr
);
3242 Lisp_Object old_value
= specpdl_old_value (specpdl_ptr
);
3243 eassert (BUFFERP (where
));
3245 if (specpdl_ptr
->kind
== SPECPDL_LET_DEFAULT
)
3246 Fset_default (symbol
, old_value
);
3247 /* If this was a local binding, reset the value in the appropriate
3248 buffer, but only if that buffer's binding still exists. */
3249 else if (!NILP (Flocal_variable_p (symbol
, where
)))
3250 set_internal (symbol
, old_value
, where
, 1);
3256 if (NILP (Vquit_flag
) && !NILP (quitf
))
3263 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3264 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3265 A special variable is one that will be bound dynamically, even in a
3266 context where binding is lexical by default. */)
3267 (Lisp_Object symbol
)
3269 CHECK_SYMBOL (symbol
);
3270 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3274 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3275 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3276 The debugger is entered when that frame exits, if the flag is non-nil. */)
3277 (Lisp_Object level
, Lisp_Object flag
)
3279 union specbinding
*pdl
= backtrace_top ();
3280 register EMACS_INT i
;
3282 CHECK_NUMBER (level
);
3284 for (i
= 0; backtrace_p (pdl
) && i
< XINT (level
); i
++)
3285 pdl
= backtrace_next (pdl
);
3287 if (backtrace_p (pdl
))
3288 set_backtrace_debug_on_exit (pdl
, !NILP (flag
));
3293 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3294 doc
: /* Print a trace of Lisp function calls currently active.
3295 Output stream used is value of `standard-output'. */)
3298 union specbinding
*pdl
= backtrace_top ();
3300 Lisp_Object old_print_level
= Vprint_level
;
3302 if (NILP (Vprint_level
))
3303 XSETFASTINT (Vprint_level
, 8);
3305 while (backtrace_p (pdl
))
3307 write_string (backtrace_debug_on_exit (pdl
) ? "* " : " ", 2);
3308 if (backtrace_nargs (pdl
) == UNEVALLED
)
3310 Fprin1 (Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)),
3312 write_string ("\n", -1);
3316 tem
= backtrace_function (pdl
);
3317 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3318 write_string ("(", -1);
3321 for (i
= 0; i
< backtrace_nargs (pdl
); i
++)
3323 if (i
) write_string (" ", -1);
3324 Fprin1 (backtrace_args (pdl
)[i
], Qnil
);
3327 write_string (")\n", -1);
3329 pdl
= backtrace_next (pdl
);
3332 Vprint_level
= old_print_level
;
3336 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3337 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3338 If that frame has not evaluated the arguments yet (or is a special form),
3339 the value is (nil FUNCTION ARG-FORMS...).
3340 If that frame has evaluated its arguments and called its function already,
3341 the value is (t FUNCTION ARG-VALUES...).
3342 A &rest arg is represented as the tail of the list ARG-VALUES.
3343 FUNCTION is whatever was supplied as car of evaluated list,
3344 or a lambda expression for macro calls.
3345 If NFRAMES is more than the number of frames, the value is nil. */)
3346 (Lisp_Object nframes
)
3348 union specbinding
*pdl
= backtrace_top ();
3349 register EMACS_INT i
;
3351 CHECK_NATNUM (nframes
);
3353 /* Find the frame requested. */
3354 for (i
= 0; backtrace_p (pdl
) && i
< XFASTINT (nframes
); i
++)
3355 pdl
= backtrace_next (pdl
);
3357 if (!backtrace_p (pdl
))
3359 if (backtrace_nargs (pdl
) == UNEVALLED
)
3361 Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)));
3364 Lisp_Object tem
= Flist (backtrace_nargs (pdl
), backtrace_args (pdl
));
3366 return Fcons (Qt
, Fcons (backtrace_function (pdl
), tem
));
3374 union specbinding
*pdl
;
3375 for (pdl
= specpdl
; pdl
!= specpdl_ptr
; pdl
++)
3379 case SPECPDL_UNWIND
:
3380 mark_object (specpdl_arg (pdl
));
3383 case SPECPDL_BACKTRACE
:
3385 ptrdiff_t nargs
= backtrace_nargs (pdl
);
3386 mark_object (backtrace_function (pdl
));
3387 if (nargs
== UNEVALLED
)
3390 mark_object (backtrace_args (pdl
)[nargs
]);
3394 case SPECPDL_LET_DEFAULT
:
3395 case SPECPDL_LET_LOCAL
:
3396 mark_object (specpdl_where (pdl
));
3399 mark_object (specpdl_symbol (pdl
));
3400 mark_object (specpdl_old_value (pdl
));
3407 get_backtrace (Lisp_Object array
)
3409 union specbinding
*pdl
= backtrace_next (backtrace_top ());
3410 ptrdiff_t i
= 0, asize
= ASIZE (array
);
3412 /* Copy the backtrace contents into working memory. */
3413 for (; i
< asize
; i
++)
3415 if (backtrace_p (pdl
))
3417 ASET (array
, i
, backtrace_function (pdl
));
3418 pdl
= backtrace_next (pdl
);
3421 ASET (array
, i
, Qnil
);
3425 Lisp_Object
backtrace_top_function (void)
3427 union specbinding
*pdl
= backtrace_top ();
3428 return (backtrace_p (pdl
) ? backtrace_function (pdl
) : Qnil
);
3434 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3435 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3436 If Lisp code tries to increase the total number past this amount,
3437 an error is signaled.
3438 You can safely use a value considerably larger than the default value,
3439 if that proves inconveniently small. However, if you increase it too far,
3440 Emacs could run out of memory trying to make the stack bigger. */);
3442 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3443 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3445 This limit serves to catch infinite recursions for you before they cause
3446 actual stack overflow in C, which would be fatal for Emacs.
3447 You can safely make it considerably larger than its default value,
3448 if that proves inconveniently small. However, if you increase it too far,
3449 Emacs could overflow the real C stack, and crash. */);
3451 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3452 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3453 If the value is t, that means do an ordinary quit.
3454 If the value equals `throw-on-input', that means quit by throwing
3455 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3456 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3457 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3460 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3461 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3462 Note that `quit-flag' will still be set by typing C-g,
3463 so a quit will be signaled as soon as `inhibit-quit' is nil.
3464 To prevent this happening, set `quit-flag' to nil
3465 before making `inhibit-quit' nil. */);
3466 Vinhibit_quit
= Qnil
;
3468 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3469 DEFSYM (Qautoload
, "autoload");
3470 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3471 DEFSYM (Qmacro
, "macro");
3472 DEFSYM (Qdeclare
, "declare");
3474 /* Note that the process handling also uses Qexit, but we don't want
3475 to staticpro it twice, so we just do it here. */
3476 DEFSYM (Qexit
, "exit");
3478 DEFSYM (Qinteractive
, "interactive");
3479 DEFSYM (Qcommandp
, "commandp");
3480 DEFSYM (Qand_rest
, "&rest");
3481 DEFSYM (Qand_optional
, "&optional");
3482 DEFSYM (Qclosure
, "closure");
3483 DEFSYM (Qdebug
, "debug");
3485 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3486 doc
: /* Non-nil means never enter the debugger.
3487 Normally set while the debugger is already active, to avoid recursive
3489 Vinhibit_debugger
= Qnil
;
3491 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3492 doc
: /* Non-nil means enter debugger if an error is signaled.
3493 Does not apply to errors handled by `condition-case' or those
3494 matched by `debug-ignored-errors'.
3495 If the value is a list, an error only means to enter the debugger
3496 if one of its condition symbols appears in the list.
3497 When you evaluate an expression interactively, this variable
3498 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3499 The command `toggle-debug-on-error' toggles this.
3500 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3501 Vdebug_on_error
= Qnil
;
3503 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3504 doc
: /* List of errors for which the debugger should not be called.
3505 Each element may be a condition-name or a regexp that matches error messages.
3506 If any element applies to a given error, that error skips the debugger
3507 and just returns to top level.
3508 This overrides the variable `debug-on-error'.
3509 It does not apply to errors handled by `condition-case'. */);
3510 Vdebug_ignored_errors
= Qnil
;
3512 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3513 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3514 Does not apply if quit is handled by a `condition-case'. */);
3517 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3518 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3520 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3521 doc
: /* Non-nil means debugger may continue execution.
3522 This is nil when the debugger is called under circumstances where it
3523 might not be safe to continue. */);
3524 debugger_may_continue
= 1;
3526 DEFVAR_LISP ("debugger", Vdebugger
,
3527 doc
: /* Function to call to invoke debugger.
3528 If due to frame exit, args are `exit' and the value being returned;
3529 this function's value will be returned instead of that.
3530 If due to error, args are `error' and a list of the args to `signal'.
3531 If due to `apply' or `funcall' entry, one arg, `lambda'.
3532 If due to `eval' entry, one arg, t. */);
3535 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3536 doc
: /* If non-nil, this is a function for `signal' to call.
3537 It receives the same arguments that `signal' was given.
3538 The Edebug package uses this to regain control. */);
3539 Vsignal_hook_function
= Qnil
;
3541 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3542 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3543 Note that `debug-on-error', `debug-on-quit' and friends
3544 still determine whether to handle the particular condition. */);
3545 Vdebug_on_signal
= Qnil
;
3547 /* When lexical binding is being used,
3548 Vinternal_interpreter_environment is non-nil, and contains an alist
3549 of lexically-bound variable, or (t), indicating an empty
3550 environment. The lisp name of this variable would be
3551 `internal-interpreter-environment' if it weren't hidden.
3552 Every element of this list can be either a cons (VAR . VAL)
3553 specifying a lexical binding, or a single symbol VAR indicating
3554 that this variable should use dynamic scoping. */
3555 DEFSYM (Qinternal_interpreter_environment
,
3556 "internal-interpreter-environment");
3557 DEFVAR_LISP ("internal-interpreter-environment",
3558 Vinternal_interpreter_environment
,
3559 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3560 When lexical binding is not being used, this variable is nil.
3561 A value of `(t)' indicates an empty environment, otherwise it is an
3562 alist of active lexical bindings. */);
3563 Vinternal_interpreter_environment
= Qnil
;
3564 /* Don't export this variable to Elisp, so no one can mess with it
3565 (Just imagine if someone makes it buffer-local). */
3566 Funintern (Qinternal_interpreter_environment
, Qnil
);
3568 DEFSYM (Vrun_hooks
, "run-hooks");
3570 staticpro (&Vautoload_queue
);
3571 Vautoload_queue
= Qnil
;
3572 staticpro (&Vsignaling_function
);
3573 Vsignaling_function
= Qnil
;
3575 inhibit_lisp_code
= Qnil
;
3586 defsubr (&Sfunction
);
3588 defsubr (&Sdefvaralias
);
3589 defsubr (&Sdefconst
);
3590 defsubr (&Smake_var_non_special
);
3594 defsubr (&Smacroexpand
);
3597 defsubr (&Sunwind_protect
);
3598 defsubr (&Scondition_case
);
3600 defsubr (&Scommandp
);
3601 defsubr (&Sautoload
);
3602 defsubr (&Sautoload_do_load
);
3605 defsubr (&Sfuncall
);
3606 defsubr (&Srun_hooks
);
3607 defsubr (&Srun_hook_with_args
);
3608 defsubr (&Srun_hook_with_args_until_success
);
3609 defsubr (&Srun_hook_with_args_until_failure
);
3610 defsubr (&Srun_hook_wrapped
);
3611 defsubr (&Sfetch_bytecode
);
3612 defsubr (&Sbacktrace_debug
);
3613 defsubr (&Sbacktrace
);
3614 defsubr (&Sbacktrace_frame
);
3615 defsubr (&Sspecial_variable_p
);
3616 defsubr (&Sfunctionp
);