1 /* Evaluator for GNU Emacs Lisp interpreter.
3 Copyright (C) 1985-1987, 1993-1995, 1999-2014 Free Software Foundation,
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include "blockinput.h"
29 #include "dispextern.h"
32 static void unbind_once (void *ignore
);
34 /* Chain of condition and catch handlers currently in effect. */
36 struct handler
*handlerlist
;
39 /* Count levels of GCPRO to detect failure to UNGCPRO. */
43 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
44 Lisp_Object Qinhibit_quit
;
45 Lisp_Object Qand_rest
;
46 static Lisp_Object Qand_optional
;
47 static Lisp_Object Qinhibit_debugger
;
48 static Lisp_Object Qdeclare
;
49 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
51 static Lisp_Object Qdebug
;
53 /* This holds either the symbol `run-hooks' or nil.
54 It is nil at an early stage of startup, and when Emacs
57 Lisp_Object Vrun_hooks
;
59 /* Non-nil means record all fset's and provide's, to be undone
60 if the file being autoloaded is not fully loaded.
61 They are recorded by being consed onto the front of Vautoload_queue:
62 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
64 Lisp_Object Vautoload_queue
;
66 /* Current number of specbindings allocated in specpdl, not counting
67 the dummy entry specpdl[-1]. */
69 ptrdiff_t specpdl_size
;
71 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
72 only so that its address can be taken. */
74 union specbinding
*specpdl
;
76 /* Pointer to first unused element in specpdl. */
78 union specbinding
*specpdl_ptr
;
80 /* Depth in Lisp evaluations and function calls. */
82 EMACS_INT lisp_eval_depth
;
84 /* The value of num_nonmacro_input_events as of the last time we
85 started to enter the debugger. If we decide to enter the debugger
86 again when this is still equal to num_nonmacro_input_events, then we
87 know that the debugger itself has an error, and we should just
88 signal the error instead of entering an infinite loop of debugger
91 static EMACS_INT when_entered_debugger
;
93 /* The function from which the last `signal' was called. Set in
95 /* FIXME: We should probably get rid of this! */
96 Lisp_Object Vsignaling_function
;
98 /* If non-nil, Lisp code must not be run since some part of Emacs is
99 in an inconsistent state. Currently, x-create-frame uses this to
100 avoid triggering window-configuration-change-hook while the new
101 frame is half-initialized. */
102 Lisp_Object inhibit_lisp_code
;
104 /* These would ordinarily be static, but they need to be visible to GDB. */
105 bool backtrace_p (union specbinding
*) EXTERNALLY_VISIBLE
;
106 Lisp_Object
*backtrace_args (union specbinding
*) EXTERNALLY_VISIBLE
;
107 Lisp_Object
backtrace_function (union specbinding
*) EXTERNALLY_VISIBLE
;
108 union specbinding
*backtrace_next (union specbinding
*) EXTERNALLY_VISIBLE
;
109 union specbinding
*backtrace_top (void) EXTERNALLY_VISIBLE
;
111 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
112 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
115 specpdl_symbol (union specbinding
*pdl
)
117 eassert (pdl
->kind
>= SPECPDL_LET
);
118 return pdl
->let
.symbol
;
122 specpdl_old_value (union specbinding
*pdl
)
124 eassert (pdl
->kind
>= SPECPDL_LET
);
125 return pdl
->let
.old_value
;
129 set_specpdl_old_value (union specbinding
*pdl
, Lisp_Object val
)
131 eassert (pdl
->kind
>= SPECPDL_LET
);
132 pdl
->let
.old_value
= val
;
136 specpdl_where (union specbinding
*pdl
)
138 eassert (pdl
->kind
> SPECPDL_LET
);
139 return pdl
->let
.where
;
143 backtrace_function (union specbinding
*pdl
)
145 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
146 return pdl
->bt
.function
;
150 backtrace_nargs (union specbinding
*pdl
)
152 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
153 return pdl
->bt
.nargs
;
157 backtrace_args (union specbinding
*pdl
)
159 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
164 backtrace_debug_on_exit (union specbinding
*pdl
)
166 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
167 return pdl
->bt
.debug_on_exit
;
170 /* Functions to modify slots of backtrace records. */
173 set_backtrace_args (union specbinding
*pdl
, Lisp_Object
*args
)
175 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
180 set_backtrace_nargs (union specbinding
*pdl
, ptrdiff_t n
)
182 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
187 set_backtrace_debug_on_exit (union specbinding
*pdl
, bool doe
)
189 eassert (pdl
->kind
== SPECPDL_BACKTRACE
);
190 pdl
->bt
.debug_on_exit
= doe
;
193 /* Helper functions to scan the backtrace. */
196 backtrace_p (union specbinding
*pdl
)
197 { return pdl
>= specpdl
; }
202 union specbinding
*pdl
= specpdl_ptr
- 1;
203 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
209 backtrace_next (union specbinding
*pdl
)
212 while (backtrace_p (pdl
) && pdl
->kind
!= SPECPDL_BACKTRACE
)
218 make_catch_handler (Lisp_Object tag
)
220 struct handler
*c
= xmalloc (sizeof (*c
));
226 c
->next
= handlerlist
;
227 c
->lisp_eval_depth
= lisp_eval_depth
;
228 c
->interrupt_input_blocked
= interrupt_input_blocked
;
229 c
->ptag
= make_prompt_tag ();
234 make_condition_handler (Lisp_Object tag
)
236 struct handler
*c
= xmalloc (sizeof (*c
));
237 c
->type
= CONDITION_CASE
;
242 c
->next
= handlerlist
;
243 c
->lisp_eval_depth
= lisp_eval_depth
;
244 c
->interrupt_input_blocked
= interrupt_input_blocked
;
245 c
->ptag
= make_prompt_tag ();
250 init_eval_once (void)
253 union specbinding
*pdlvec
= xmalloc ((size
+ 1) * sizeof *specpdl
);
255 specpdl
= specpdl_ptr
= pdlvec
+ 1;
256 /* Don't forget to update docs (lispref node "Local Variables"). */
257 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
258 max_lisp_eval_depth
= 600;
263 static struct handler
*handlerlist_sentinel
;
268 specpdl_ptr
= specpdl
;
269 handlerlist_sentinel
= make_catch_handler (Qunbound
);
270 handlerlist
= handlerlist_sentinel
;
272 debug_on_next_call
= 0;
277 /* This is less than the initial value of num_nonmacro_input_events. */
278 when_entered_debugger
= -1;
281 /* Unwind-protect function used by call_debugger. */
284 restore_stack_limits (Lisp_Object data
)
286 max_specpdl_size
= XINT (XCAR (data
));
287 max_lisp_eval_depth
= XINT (XCDR (data
));
290 static void grow_specpdl (void);
292 /* Call the Lisp debugger, giving it argument ARG. */
295 call_debugger (Lisp_Object arg
)
297 bool debug_while_redisplaying
;
300 EMACS_INT old_depth
= max_lisp_eval_depth
;
301 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
302 EMACS_INT old_max
= max_specpdl_size
;
304 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
305 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
307 /* Restore limits after leaving the debugger. */
308 record_unwind_protect (restore_stack_limits
,
309 Fcons (make_number (old_max
),
310 make_number (old_depth
)));
312 #ifdef HAVE_WINDOW_SYSTEM
313 if (display_hourglass_p
)
317 debug_on_next_call
= 0;
318 when_entered_debugger
= num_nonmacro_input_events
;
320 /* Resetting redisplaying_p to 0 makes sure that debug output is
321 displayed if the debugger is invoked during redisplay. */
322 debug_while_redisplaying
= redisplaying_p
;
324 specbind (intern ("debugger-may-continue"),
325 debug_while_redisplaying
? Qnil
: Qt
);
326 specbind (Qinhibit_redisplay
, Qnil
);
327 specbind (Qinhibit_debugger
, Qt
);
329 #if 0 /* Binding this prevents execution of Lisp code during
330 redisplay, which necessarily leads to display problems. */
331 specbind (Qinhibit_eval_during_redisplay
, Qt
);
334 val
= apply1 (Vdebugger
, arg
);
336 /* Interrupting redisplay and resuming it later is not safe under
337 all circumstances. So, when the debugger returns, abort the
338 interrupted redisplay by going back to the top-level. */
339 if (debug_while_redisplaying
)
347 do_debug_on_call (Lisp_Object code
)
349 debug_on_next_call
= 0;
350 set_backtrace_debug_on_exit (specpdl_ptr
- 1, true);
351 call_debugger (list1 (code
));
354 /* NOTE!!! Every function that can call EVAL must protect its args
355 and temporaries from garbage collection while it needs them.
356 The definition of `For' shows what you have to do. */
358 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
359 doc
: /* Eval args until one of them yields non-nil, then return that value.
360 The remaining args are not evalled at all.
361 If all args return nil, return nil.
362 usage: (or CONDITIONS...) */)
365 register Lisp_Object val
= Qnil
;
372 val
= eval_sub (XCAR (args
));
382 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
383 doc
: /* Eval args until one of them yields nil, then return nil.
384 The remaining args are not evalled at all.
385 If no arg yields nil, return the last arg's value.
386 usage: (and CONDITIONS...) */)
389 register Lisp_Object val
= Qt
;
396 val
= eval_sub (XCAR (args
));
406 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
407 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
408 Returns the value of THEN or the value of the last of the ELSE's.
409 THEN must be one expression, but ELSE... can be zero or more expressions.
410 If COND yields nil, and there are no ELSE's, the value is nil.
411 usage: (if COND THEN ELSE...) */)
418 cond
= eval_sub (XCAR (args
));
422 return eval_sub (Fcar (XCDR (args
)));
423 return Fprogn (XCDR (XCDR (args
)));
426 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
427 doc
: /* Try each clause until one succeeds.
428 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
429 and, if the value is non-nil, this clause succeeds:
430 then the expressions in BODY are evaluated and the last one's
431 value is the value of the cond-form.
432 If a clause has one element, as in (CONDITION), then the cond-form
433 returns CONDITION's value, if that is non-nil.
434 If no clause succeeds, cond returns nil.
435 usage: (cond CLAUSES...) */)
438 Lisp_Object val
= args
;
444 Lisp_Object clause
= XCAR (args
);
445 val
= eval_sub (Fcar (clause
));
448 if (!NILP (XCDR (clause
)))
449 val
= Fprogn (XCDR (clause
));
459 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
460 doc
: /* Eval BODY forms sequentially and return value of last one.
461 usage: (progn BODY...) */)
464 Lisp_Object val
= Qnil
;
471 val
= eval_sub (XCAR (body
));
479 /* Evaluate BODY sequentially, discarding its value. Suitable for
480 record_unwind_protect. */
483 unwind_body (Lisp_Object body
)
488 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
489 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
490 The value of FIRST is saved during the evaluation of the remaining args,
491 whose values are discarded.
492 usage: (prog1 FIRST BODY...) */)
496 Lisp_Object args_left
;
497 struct gcpro gcpro1
, gcpro2
;
503 val
= eval_sub (XCAR (args_left
));
504 while (CONSP (args_left
= XCDR (args_left
)))
505 eval_sub (XCAR (args_left
));
511 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
512 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
513 The value of FORM2 is saved during the evaluation of the
514 remaining args, whose values are discarded.
515 usage: (prog2 FORM1 FORM2 BODY...) */)
521 eval_sub (XCAR (args
));
523 return Fprog1 (XCDR (args
));
526 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
527 doc
: /* Set each SYM to the value of its VAL.
528 The symbols SYM are variables; they are literal (not evaluated).
529 The values VAL are expressions; they are evaluated.
530 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
531 The second VAL is not computed until after the first SYM is set, and so on;
532 each VAL can use the new value of variables set earlier in the `setq'.
533 The return value of the `setq' form is the value of the last VAL.
534 usage: (setq [SYM VAL]...) */)
537 Lisp_Object val
, sym
, lex_binding
;
542 Lisp_Object args_left
= args
;
548 val
= eval_sub (Fcar (XCDR (args_left
)));
549 sym
= XCAR (args_left
);
551 /* Like for eval_sub, we do not check declared_special here since
552 it's been done when let-binding. */
553 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
555 && !NILP (lex_binding
556 = Fassq (sym
, Vinternal_interpreter_environment
)))
557 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
559 Fset (sym
, val
); /* SYM is dynamically bound. */
561 args_left
= Fcdr (XCDR (args_left
));
563 while (CONSP (args_left
));
571 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
572 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
573 Warning: `quote' does not construct its return value, but just returns
574 the value that was pre-constructed by the Lisp reader (see info node
575 `(elisp)Printed Representation').
576 This means that '(a . b) is not identical to (cons 'a 'b): the former
577 does not cons. Quoting should be reserved for constants that will
578 never be modified by side-effects, unless you like self-modifying code.
579 See the common pitfall in info node `(elisp)Rearrangement' for an example
580 of unexpected results when a quoted object is modified.
581 usage: (quote ARG) */)
584 if (CONSP (XCDR (args
)))
585 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
589 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
590 doc
: /* Like `quote', but preferred for objects which are functions.
591 In byte compilation, `function' causes its argument to be compiled.
592 `quote' cannot do that.
593 usage: (function ARG) */)
596 Lisp_Object quoted
= XCAR (args
);
598 if (CONSP (XCDR (args
)))
599 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
601 if (!NILP (Vinternal_interpreter_environment
)
603 && EQ (XCAR (quoted
), Qlambda
))
604 /* This is a lambda expression within a lexical environment;
605 return an interpreted closure instead of a simple lambda. */
606 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
609 /* Simply quote the argument. */
614 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
615 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
616 Aliased variables always have the same value; setting one sets the other.
617 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
618 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
619 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
620 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
621 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
622 The return value is BASE-VARIABLE. */)
623 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
627 CHECK_SYMBOL (new_alias
);
628 CHECK_SYMBOL (base_variable
);
630 sym
= XSYMBOL (new_alias
);
632 if (SYMBOL_CONSTANT (sym
))
633 /* Not sure why, but why not? */
634 error ("Cannot make a constant an alias");
636 switch (SYMBOL_REDIRECT (sym
))
638 case SYMBOL_FORWARDED
:
639 error ("Cannot make an internal variable an alias");
640 case SYMBOL_LOCALIZED
:
641 error ("Don't know how to make a localized variable an alias");
644 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
645 If n_a is bound, but b_v is not, set the value of b_v to n_a,
646 so that old-code that affects n_a before the aliasing is setup
648 if (NILP (Fboundp (base_variable
)))
649 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
652 union specbinding
*p
;
654 for (p
= specpdl_ptr
; p
> specpdl
; )
655 if ((--p
)->kind
>= SPECPDL_LET
656 && (EQ (new_alias
, specpdl_symbol (p
))))
657 error ("Don't know how to make a let-bound variable an alias");
660 SET_SYMBOL_DECLARED_SPECIAL (sym
, 1);
661 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (base_variable
), 1);
662 SET_SYMBOL_REDIRECT (sym
, SYMBOL_VARALIAS
);
663 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
664 SET_SYMBOL_CONSTANT (sym
, SYMBOL_CONSTANT_P (base_variable
));
665 LOADHIST_ATTACH (new_alias
);
666 /* Even if docstring is nil: remove old docstring. */
667 Fput (new_alias
, Qvariable_documentation
, docstring
);
669 return base_variable
;
672 static union specbinding
*
673 default_toplevel_binding (Lisp_Object symbol
)
675 union specbinding
*binding
= NULL
;
676 union specbinding
*pdl
= specpdl_ptr
;
677 while (pdl
> specpdl
)
679 switch ((--pdl
)->kind
)
681 case SPECPDL_LET_DEFAULT
:
683 if (EQ (specpdl_symbol (pdl
), symbol
))
691 DEFUN ("default-toplevel-value", Fdefault_toplevel_value
, Sdefault_toplevel_value
, 1, 1, 0,
692 doc
: /* Return SYMBOL's toplevel default value.
693 "Toplevel" means outside of any let binding. */)
696 union specbinding
*binding
= default_toplevel_binding (symbol
);
698 = binding
? specpdl_old_value (binding
) : Fdefault_value (symbol
);
699 if (!EQ (value
, Qunbound
))
701 xsignal1 (Qvoid_variable
, symbol
);
704 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value
,
705 Sset_default_toplevel_value
, 2, 2, 0,
706 doc
: /* Set SYMBOL's toplevel default value to VALUE.
707 "Toplevel" means outside of any let binding. */)
708 (Lisp_Object symbol
, Lisp_Object value
)
710 union specbinding
*binding
= default_toplevel_binding (symbol
);
712 set_specpdl_old_value (binding
, value
);
714 Fset_default (symbol
, value
);
718 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
719 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
720 You are not required to define a variable in order to use it, but
721 defining it lets you supply an initial value and documentation, which
722 can be referred to by the Emacs help facilities and other programming
723 tools. The `defvar' form also declares the variable as \"special\",
724 so that it is always dynamically bound even if `lexical-binding' is t.
726 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
727 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
728 default value is what is set; buffer-local values are not affected.
729 If INITVALUE is missing, SYMBOL's value is not set.
731 If SYMBOL has a local binding, then this form affects the local
732 binding. This is usually not what you want. Thus, if you need to
733 load a file defining variables, with this form or with `defconst' or
734 `defcustom', you should always load that file _outside_ any bindings
735 for these variables. \(`defconst' and `defcustom' behave similarly in
738 The optional argument DOCSTRING is a documentation string for the
741 To define a user option, use `defcustom' instead of `defvar'.
742 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
745 Lisp_Object sym
, tem
, tail
;
752 if (CONSP (XCDR (tail
)) && CONSP (XCDR (XCDR (tail
))))
753 error ("Too many arguments");
755 tem
= Fdefault_boundp (sym
);
757 /* Do it before evaluating the initial value, for self-references. */
758 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym
), 1);
761 Fset_default (sym
, eval_sub (XCAR (tail
)));
763 { /* Check if there is really a global binding rather than just a let
764 binding that shadows the global unboundness of the var. */
765 union specbinding
*binding
= default_toplevel_binding (sym
);
766 if (binding
&& EQ (specpdl_old_value (binding
), Qunbound
))
768 set_specpdl_old_value (binding
, eval_sub (XCAR (tail
)));
775 if (!NILP (Vpurify_flag
))
776 tem
= Fpurecopy (tem
);
777 Fput (sym
, Qvariable_documentation
, tem
);
779 LOADHIST_ATTACH (sym
);
781 else if (!NILP (Vinternal_interpreter_environment
)
782 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym
)))
783 /* A simple (defvar foo) with lexical scoping does "nothing" except
784 declare that var to be dynamically scoped *locally* (i.e. within
785 the current file or let-block). */
786 Vinternal_interpreter_environment
787 = Fcons (sym
, Vinternal_interpreter_environment
);
790 /* Simple (defvar <var>) should not count as a definition at all.
791 It could get in the way of other definitions, and unloading this
792 package could try to make the variable unbound. */
798 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
799 doc
: /* Define SYMBOL as a constant variable.
800 This declares that neither programs nor users should ever change the
801 value. This constancy is not actually enforced by Emacs Lisp, but
802 SYMBOL is marked as a special variable so that it is never lexically
805 The `defconst' form always sets the value of SYMBOL to the result of
806 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
807 what is set; buffer-local values are not affected. If SYMBOL has a
808 local binding, then this form sets the local binding's value.
809 However, you should normally not make local bindings for variables
810 defined with this form.
812 The optional DOCSTRING specifies the variable's documentation string.
813 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
816 Lisp_Object sym
, tem
;
819 if (CONSP (Fcdr (XCDR (XCDR (args
)))))
820 error ("Too many arguments");
822 tem
= eval_sub (Fcar (XCDR (args
)));
823 if (!NILP (Vpurify_flag
))
824 tem
= Fpurecopy (tem
);
825 Fset_default (sym
, tem
);
826 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym
), 1);
827 tem
= Fcar (XCDR (XCDR (args
)));
830 if (!NILP (Vpurify_flag
))
831 tem
= Fpurecopy (tem
);
832 Fput (sym
, Qvariable_documentation
, tem
);
834 Fput (sym
, Qrisky_local_variable
, Qt
);
835 LOADHIST_ATTACH (sym
);
839 /* Make SYMBOL lexically scoped. */
840 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
841 Smake_var_non_special
, 1, 1, 0,
842 doc
: /* Internal function. */)
845 CHECK_SYMBOL (symbol
);
846 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol
), 0);
851 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
852 doc
: /* Bind variables according to VARLIST then eval BODY.
853 The value of the last form in BODY is returned.
854 Each element of VARLIST is a symbol (which is bound to nil)
855 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
856 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
857 usage: (let* VARLIST BODY...) */)
860 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
862 struct gcpro gcpro1
, gcpro2
, gcpro3
;
864 GCPRO3 (args
, elt
, varlist
);
866 lexenv
= Vinternal_interpreter_environment
;
868 varlist
= XCAR (args
);
869 while (CONSP (varlist
))
873 elt
= XCAR (varlist
);
879 else if (! NILP (Fcdr (Fcdr (elt
))))
880 signal_error ("`let' bindings can have only one value-form", elt
);
884 val
= eval_sub (Fcar (Fcdr (elt
)));
887 if (!NILP (lexenv
) && SYMBOLP (var
)
888 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (var
))
889 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
890 /* Lexically bind VAR by adding it to the interpreter's binding
894 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
895 if (EQ (Vinternal_interpreter_environment
, lexenv
))
896 /* Save the old lexical environment on the specpdl stack,
897 but only for the first lexical binding, since we'll never
898 need to revert to one of the intermediate ones. */
899 specbind (Qinternal_interpreter_environment
, newenv
);
901 Vinternal_interpreter_environment
= newenv
;
906 varlist
= XCDR (varlist
);
909 val
= Fprogn (XCDR (args
));
914 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
915 doc
: /* Bind variables according to VARLIST then eval BODY.
916 The value of the last form in BODY is returned.
917 Each element of VARLIST is a symbol (which is bound to nil)
918 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
919 All the VALUEFORMs are evalled before any symbols are bound.
920 usage: (let VARLIST BODY...) */)
923 Lisp_Object
*temps
, tem
, lexenv
;
924 register Lisp_Object elt
, varlist
;
927 struct gcpro gcpro1
, gcpro2
;
930 varlist
= XCAR (args
);
932 /* Make space to hold the values to give the bound variables. */
933 elt
= Flength (varlist
);
934 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
936 /* Compute the values and store them in `temps'. */
938 GCPRO2 (args
, *temps
);
941 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
944 elt
= XCAR (varlist
);
946 temps
[argnum
++] = Qnil
;
947 else if (! NILP (Fcdr (Fcdr (elt
))))
948 signal_error ("`let' bindings can have only one value-form", elt
);
950 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
951 gcpro2
.nvars
= argnum
;
955 lexenv
= Vinternal_interpreter_environment
;
957 varlist
= XCAR (args
);
958 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
962 elt
= XCAR (varlist
);
963 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
964 tem
= temps
[argnum
++];
966 if (!NILP (lexenv
) && SYMBOLP (var
)
967 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (var
))
968 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
969 /* Lexically bind VAR by adding it to the lexenv alist. */
970 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
972 /* Dynamically bind VAR. */
976 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
977 /* Instantiate a new lexical environment. */
978 specbind (Qinternal_interpreter_environment
, lexenv
);
980 elt
= Fprogn (XCDR (args
));
986 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
987 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
988 The order of execution is thus TEST, BODY, TEST, BODY and so on
989 until TEST returns nil.
990 usage: (while TEST BODY...) */)
993 Lisp_Object test
, body
;
994 struct gcpro gcpro1
, gcpro2
;
1000 while (!NILP (eval_sub (test
)))
1010 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1011 doc
: /* Return result of expanding macros at top level of FORM.
1012 If FORM is not a macro call, it is returned unchanged.
1013 Otherwise, the macro is expanded and the expansion is considered
1014 in place of FORM. When a non-macro-call results, it is returned.
1016 The second optional arg ENVIRONMENT specifies an environment of macro
1017 definitions to shadow the loaded ones for use in file byte-compilation. */)
1018 (Lisp_Object form
, Lisp_Object environment
)
1020 /* With cleanups from Hallvard Furuseth. */
1021 register Lisp_Object expander
, sym
, def
, tem
;
1025 /* Come back here each time we expand a macro call,
1026 in case it expands into another macro call. */
1029 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1030 def
= sym
= XCAR (form
);
1032 /* Trace symbols aliases to other symbols
1033 until we get a symbol that is not an alias. */
1034 while (SYMBOLP (def
))
1038 tem
= Fassq (sym
, environment
);
1041 def
= SYMBOL_FUNCTION (sym
);
1047 /* Right now TEM is the result from SYM in ENVIRONMENT,
1048 and if TEM is nil then DEF is SYM's function definition. */
1051 /* SYM is not mentioned in ENVIRONMENT.
1052 Look at its function definition. */
1053 struct gcpro gcpro1
;
1055 def
= Fautoload_do_load (def
, sym
, Qmacro
);
1058 /* Not defined or definition not suitable. */
1060 if (!EQ (XCAR (def
), Qmacro
))
1062 else expander
= XCDR (def
);
1066 expander
= XCDR (tem
);
1067 if (NILP (expander
))
1071 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1072 if (EQ (form
, newform
))
1081 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1082 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1083 TAG is evalled to get the tag to use; it must not be nil.
1085 Then the BODY is executed.
1086 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1087 If no throw happens, `catch' returns the value of the last BODY form.
1088 If a throw happens, it specifies the value to return from `catch'.
1089 usage: (catch TAG BODY...) */)
1092 register Lisp_Object tag
;
1093 struct gcpro gcpro1
;
1096 tag
= eval_sub (XCAR (args
));
1098 return internal_catch (tag
, Fprogn
, XCDR (args
));
1101 /* Assert that E is true, as a comment only. Use this instead of
1102 eassert (E) when E contains variables that might be clobbered by a
1105 #define clobbered_eassert(E) ((void) 0)
1108 set_handlerlist (void *data
)
1114 restore_handler (void *data
)
1116 struct handler
*c
= data
;
1117 unblock_input_to (c
->interrupt_input_blocked
);
1121 struct icc_thunk_env
1123 enum { ICC_0
, ICC_1
, ICC_2
, ICC_3
, ICC_N
} type
;
1126 Lisp_Object (*fun0
) (void);
1127 Lisp_Object (*fun1
) (Lisp_Object
);
1128 Lisp_Object (*fun2
) (Lisp_Object
, Lisp_Object
);
1129 Lisp_Object (*fun3
) (Lisp_Object
, Lisp_Object
, Lisp_Object
);
1130 Lisp_Object (*funn
) (ptrdiff_t, Lisp_Object
*);
1150 icc_thunk (void *data
)
1153 struct icc_thunk_env
*e
= data
;
1154 scm_dynwind_begin (0);
1155 scm_dynwind_unwind_handler (restore_handler
, e
->c
, 0);
1156 scm_dynwind_unwind_handler (set_handlerlist
,
1158 SCM_F_WIND_EXPLICITLY
);
1166 tem
= e
->fun1 (e
->arg1
);
1169 tem
= e
->fun2 (e
->arg1
, e
->arg2
);
1172 tem
= e
->fun3 (e
->arg1
, e
->arg2
, e
->arg3
);
1175 tem
= e
->funn (e
->nargs
, e
->args
);
1185 icc_handler (void *data
, Lisp_Object k
, Lisp_Object v
)
1187 Lisp_Object (*f
) (Lisp_Object
) = data
;
1191 struct icc_handler_n_env
1193 Lisp_Object (*fun
) (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
1199 icc_handler_n (void *data
, Lisp_Object k
, Lisp_Object v
)
1201 struct icc_handler_n_env
*e
= data
;
1202 return e
->fun (v
, e
->nargs
, e
->args
);
1206 icc_lisp_handler (void *data
, Lisp_Object k
, Lisp_Object val
)
1209 struct handler
*h
= data
;
1210 Lisp_Object var
= h
->var
;
1211 scm_dynwind_begin (0);
1214 if (!NILP (Vinternal_interpreter_environment
))
1215 specbind (Qinternal_interpreter_environment
,
1216 Fcons (Fcons (var
, val
),
1217 Vinternal_interpreter_environment
));
1219 specbind (var
, val
);
1221 tem
= Fprogn (h
->body
);
1226 /* Set up a catch, then call C function FUNC on argument ARG.
1227 FUNC should return a Lisp_Object.
1228 This is how catches are done from within C code. */
1231 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1233 struct handler
*c
= make_catch_handler (tag
);
1234 struct icc_thunk_env env
= { .type
= ICC_1
,
1238 return call_with_prompt (c
->ptag
,
1239 make_c_closure (icc_thunk
, &env
, 0, 0),
1240 make_c_closure (icc_handler
, Fidentity
, 2, 0));
1243 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1244 jump to that CATCH, returning VALUE as the value of that catch.
1246 This is the guts of Fthrow and Fsignal; they differ only in the way
1247 they choose the catch tag to throw to. A catch tag for a
1248 condition-case form has a TAG of Qnil.
1250 Before each catch is discarded, unbind all special bindings and
1251 execute all unwind-protect clauses made above that catch. Unwind
1252 the handler stack as we go, so that the proper handlers are in
1253 effect for each unwind-protect clause we run. At the end, restore
1254 some static info saved in CATCH, and longjmp to the location
1257 This is used for correct unwinding in Fthrow and Fsignal. */
1259 static Lisp_Object
unbind_to_1 (ptrdiff_t, Lisp_Object
, bool);
1261 static _Noreturn
void
1262 unwind_to_catch (struct handler
*catch, Lisp_Object value
)
1264 abort_to_prompt (catch->ptag
, scm_list_1 (value
));
1267 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1268 doc
: /* Throw to the catch for TAG and return VALUE from it.
1269 Both TAG and VALUE are evalled. */)
1270 (register Lisp_Object tag
, Lisp_Object value
)
1275 for (c
= handlerlist
; c
; c
= c
->next
)
1277 if (c
->type
== CATCHER
&& EQ (c
->tag_or_ch
, tag
))
1278 unwind_to_catch (c
, value
);
1280 xsignal2 (Qno_catch
, tag
, value
);
1284 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1285 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1286 If BODYFORM completes normally, its value is returned
1287 after executing the UNWINDFORMS.
1288 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1289 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1295 record_unwind_protect (unwind_body
, XCDR (args
));
1296 val
= eval_sub (XCAR (args
));
1301 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1302 doc
: /* Regain control when an error is signaled.
1303 Executes BODYFORM and returns its value if no error happens.
1304 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1305 where the BODY is made of Lisp expressions.
1307 A handler is applicable to an error
1308 if CONDITION-NAME is one of the error's condition names.
1309 If an error happens, the first applicable handler is run.
1311 The car of a handler may be a list of condition names instead of a
1312 single condition name; then it handles all of them. If the special
1313 condition name `debug' is present in this list, it allows another
1314 condition in the list to run the debugger if `debug-on-error' and the
1315 other usual mechanisms says it should (otherwise, `condition-case'
1316 suppresses the debugger).
1318 When a handler handles an error, control returns to the `condition-case'
1319 and it executes the handler's BODY...
1320 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1321 \(If VAR is nil, the handler can't access that information.)
1322 Then the value of the last BODY form is returned from the `condition-case'
1325 See also the function `signal' for more info.
1326 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1329 Lisp_Object var
= XCAR (args
);
1330 Lisp_Object bodyform
= XCAR (XCDR (args
));
1331 Lisp_Object handlers
= XCDR (XCDR (args
));
1333 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1337 ilcc1 (Lisp_Object var
, Lisp_Object bodyform
, Lisp_Object handlers
)
1339 if (CONSP (handlers
))
1341 Lisp_Object clause
= XCAR (handlers
);
1342 Lisp_Object condition
= XCAR (clause
);
1343 Lisp_Object body
= XCDR (clause
);
1344 if (!CONSP (condition
))
1345 condition
= Fcons (condition
, Qnil
);
1346 struct handler
*c
= make_condition_handler (condition
);
1349 struct icc_thunk_env env
= { .type
= ICC_3
,
1353 .arg3
= XCDR (handlers
),
1355 return call_with_prompt (c
->ptag
,
1356 make_c_closure (icc_thunk
, &env
, 0, 0),
1357 make_c_closure (icc_lisp_handler
, c
, 2, 0));
1361 return eval_sub (bodyform
);
1365 /* Like Fcondition_case, but the args are separate
1366 rather than passed in a list. Used by Fbyte_code. */
1369 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1370 Lisp_Object handlers
)
1374 struct handler
*oldhandlerlist
= handlerlist
;
1378 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1380 Lisp_Object tem
= XCAR (val
);
1383 && (SYMBOLP (XCAR (tem
))
1384 || CONSP (XCAR (tem
))))))
1385 error ("Invalid condition handler: %s",
1386 SDATA (Fprin1_to_string (tem
, Qt
)));
1389 return ilcc1 (var
, bodyform
, Freverse (handlers
));
1392 /* Call the function BFUN with no arguments, catching errors within it
1393 according to HANDLERS. If there is an error, call HFUN with
1394 one argument which is the data that describes the error:
1397 HANDLERS can be a list of conditions to catch.
1398 If HANDLERS is Qt, catch all errors.
1399 If HANDLERS is Qerror, catch all errors
1400 but allow the debugger to run if that is enabled. */
1403 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1404 Lisp_Object (*hfun
) (Lisp_Object
))
1407 struct handler
*c
= make_condition_handler (handlers
);
1409 struct icc_thunk_env env
= { .type
= ICC_0
, .fun0
= bfun
, .c
= c
};
1410 return call_with_prompt (c
->ptag
,
1411 make_c_closure (icc_thunk
, &env
, 0, 0),
1412 make_c_closure (icc_handler
, hfun
, 2, 0));
1415 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1418 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1419 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1422 struct handler
*c
= make_condition_handler (handlers
);
1424 struct icc_thunk_env env
= { .type
= ICC_1
,
1428 return call_with_prompt (c
->ptag
,
1429 make_c_closure (icc_thunk
, &env
, 0, 0),
1430 make_c_closure (icc_handler
, hfun
, 2, 0));
1433 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1437 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1440 Lisp_Object handlers
,
1441 Lisp_Object (*hfun
) (Lisp_Object
))
1444 struct handler
*c
= make_condition_handler (handlers
);
1445 struct icc_thunk_env env
= { .type
= ICC_2
,
1450 return call_with_prompt (c
->ptag
,
1451 make_c_closure (icc_thunk
, &env
, 0, 0),
1452 make_c_closure (icc_handler
, hfun
, 2, 0));
1455 /* Like internal_condition_case but call BFUN with NARGS as first,
1456 and ARGS as second argument. */
1459 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1462 Lisp_Object handlers
,
1463 Lisp_Object (*hfun
) (Lisp_Object err
,
1468 struct handler
*c
= make_condition_handler (handlers
);
1470 struct icc_thunk_env env
= { .type
= ICC_N
,
1475 struct icc_handler_n_env henv
= { .fun
= hfun
, .nargs
= nargs
, .args
= args
};
1476 return call_with_prompt (c
->ptag
,
1477 make_c_closure (icc_thunk
, &env
, 0, 0),
1478 make_c_closure (icc_handler_n
, &henv
, 2, 0));
1482 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1483 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1487 process_quit_flag (void)
1489 Lisp_Object flag
= Vquit_flag
;
1491 if (EQ (flag
, Qkill_emacs
))
1493 if (EQ (Vthrow_on_input
, flag
))
1494 Fthrow (Vthrow_on_input
, Qt
);
1495 Fsignal (Qquit
, Qnil
);
1498 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1499 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1500 This function does not return.
1502 An error symbol is a symbol with an `error-conditions' property
1503 that is a list of condition names.
1504 A handler for any of those names will get to handle this signal.
1505 The symbol `error' should normally be one of them.
1507 DATA should be a list. Its elements are printed as part of the error message.
1508 See Info anchor `(elisp)Definition of signal' for some details on how this
1509 error message is constructed.
1510 If the signal is handled, DATA is made available to the handler.
1511 See also the function `condition-case'. */)
1512 (Lisp_Object error_symbol
, Lisp_Object data
)
1514 /* When memory is full, ERROR-SYMBOL is nil,
1515 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1516 That is a special case--don't do this in other situations. */
1517 Lisp_Object conditions
;
1519 Lisp_Object real_error_symbol
1520 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1521 register Lisp_Object clause
= Qnil
;
1525 if (waiting_for_input
)
1528 #if 0 /* rms: I don't know why this was here,
1529 but it is surely wrong for an error that is handled. */
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 if (display_hourglass_p
)
1532 cancel_hourglass ();
1536 /* This hook is used by edebug. */
1537 if (! NILP (Vsignal_hook_function
)
1538 && ! NILP (error_symbol
))
1540 /* Edebug takes care of restoring these variables when it exits. */
1541 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1542 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1544 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1545 max_specpdl_size
= SPECPDL_INDEX () + 40;
1547 call2 (Vsignal_hook_function
, error_symbol
, data
);
1550 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1552 /* Remember from where signal was called. Skip over the frame for
1553 `signal' itself. If a frame for `error' follows, skip that,
1554 too. Don't do this when ERROR_SYMBOL is nil, because that
1555 is a memory-full error. */
1556 Vsignaling_function
= Qnil
;
1557 if (!NILP (error_symbol
))
1559 union specbinding
*pdl
= backtrace_next (backtrace_top ());
1560 if (backtrace_p (pdl
) && EQ (backtrace_function (pdl
), Qerror
))
1561 pdl
= backtrace_next (pdl
);
1562 if (backtrace_p (pdl
))
1563 Vsignaling_function
= backtrace_function (pdl
);
1566 for (h
= handlerlist
; h
; h
= h
->next
)
1568 if (h
->type
!= CONDITION_CASE
)
1570 clause
= find_handler_clause (h
->tag_or_ch
, conditions
);
1575 if (/* Don't run the debugger for a memory-full error.
1576 (There is no room in memory to do that!) */
1577 !NILP (error_symbol
)
1578 && (!NILP (Vdebug_on_signal
)
1579 /* If no handler is present now, try to run the debugger. */
1581 /* A `debug' symbol in the handler list disables the normal
1582 suppression of the debugger. */
1583 || (CONSP (clause
) && CONSP (clause
)
1584 && !NILP (Fmemq (Qdebug
, clause
)))
1585 /* Special handler that means "print a message and run debugger
1587 || EQ (h
->tag_or_ch
, Qerror
)))
1589 bool debugger_called
1590 = maybe_call_debugger (conditions
, error_symbol
, data
);
1591 /* We can't return values to code which signaled an error, but we
1592 can continue code which has signaled a quit. */
1593 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1599 Lisp_Object unwind_data
1600 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1602 unwind_to_catch (h
, unwind_data
);
1606 if (handlerlist
!= handlerlist_sentinel
)
1607 /* FIXME: This will come right back here if there's no `top-level'
1608 catcher. A better solution would be to abort here, and instead
1609 add a catch-all condition handler so we never come here. */
1610 Fthrow (Qtop_level
, Qt
);
1613 if (! NILP (error_symbol
))
1614 data
= Fcons (error_symbol
, data
);
1616 string
= Ferror_message_string (data
);
1617 fatal ("%s", SDATA (string
));
1620 /* Internal version of Fsignal that never returns.
1621 Used for anything but Qquit (which can return from Fsignal). */
1624 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1626 Fsignal (error_symbol
, data
);
1630 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1633 xsignal0 (Lisp_Object error_symbol
)
1635 xsignal (error_symbol
, Qnil
);
1639 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1641 xsignal (error_symbol
, list1 (arg
));
1645 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1647 xsignal (error_symbol
, list2 (arg1
, arg2
));
1651 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1653 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1656 /* Signal `error' with message S, and additional arg ARG.
1657 If ARG is not a genuine list, make it a one-element list. */
1660 signal_error (const char *s
, Lisp_Object arg
)
1662 Lisp_Object tortoise
, hare
;
1664 hare
= tortoise
= arg
;
1665 while (CONSP (hare
))
1672 tortoise
= XCDR (tortoise
);
1674 if (EQ (hare
, tortoise
))
1681 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1685 /* Return true if LIST is a non-nil atom or
1686 a list containing one of CONDITIONS. */
1689 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1696 while (CONSP (conditions
))
1698 Lisp_Object
this, tail
;
1699 this = XCAR (conditions
);
1700 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1701 if (EQ (XCAR (tail
), this))
1703 conditions
= XCDR (conditions
);
1708 /* Return true if an error with condition-symbols CONDITIONS,
1709 and described by SIGNAL-DATA, should skip the debugger
1710 according to debugger-ignored-errors. */
1713 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1716 bool first_string
= 1;
1717 Lisp_Object error_message
;
1719 error_message
= Qnil
;
1720 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1722 if (STRINGP (XCAR (tail
)))
1726 error_message
= Ferror_message_string (data
);
1730 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1735 Lisp_Object contail
;
1737 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1738 if (EQ (XCAR (tail
), XCAR (contail
)))
1746 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1747 SIG and DATA describe the signal. There are two ways to pass them:
1748 = SIG is the error symbol, and DATA is the rest of the data.
1749 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1750 This is for memory-full errors only. */
1752 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1754 Lisp_Object combined_data
;
1756 combined_data
= Fcons (sig
, data
);
1759 /* Don't try to run the debugger with interrupts blocked.
1760 The editing loop would return anyway. */
1761 ! input_blocked_p ()
1762 && NILP (Vinhibit_debugger
)
1763 /* Does user want to enter debugger for this kind of error? */
1766 : wants_debugger (Vdebug_on_error
, conditions
))
1767 && ! skip_debugger (conditions
, combined_data
)
1768 /* RMS: What's this for? */
1769 && when_entered_debugger
< num_nonmacro_input_events
)
1771 call_debugger (list2 (Qerror
, combined_data
));
1779 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1781 register Lisp_Object h
;
1783 /* t is used by handlers for all conditions, set up by C code. */
1784 if (EQ (handlers
, Qt
))
1787 /* error is used similarly, but means print an error message
1788 and run the debugger if that is enabled. */
1789 if (EQ (handlers
, Qerror
))
1792 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1794 Lisp_Object handler
= XCAR (h
);
1795 if (!NILP (Fmemq (handler
, conditions
)))
1803 /* Dump an error message; called like vprintf. */
1805 verror (const char *m
, va_list ap
)
1808 ptrdiff_t size
= sizeof buf
;
1809 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1814 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1815 string
= make_string (buffer
, used
);
1819 xsignal1 (Qerror
, string
);
1823 /* Dump an error message; called like printf. */
1827 error (const char *m
, ...)
1834 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1835 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1836 This means it contains a description for how to read arguments to give it.
1837 The value is nil for an invalid function or a symbol with no function
1840 Interactively callable functions include strings and vectors (treated
1841 as keyboard macros), lambda-expressions that contain a top-level call
1842 to `interactive', autoload definitions made by `autoload' with non-nil
1843 fourth argument, and some of the built-in functions of Lisp.
1845 Also, a symbol satisfies `commandp' if its function definition does so.
1847 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1848 then strings and vectors are not accepted. */)
1849 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1851 register Lisp_Object fun
;
1852 register Lisp_Object funcar
;
1853 Lisp_Object if_prop
= Qnil
;
1857 fun
= indirect_function (fun
); /* Check cycles. */
1861 /* Check an `interactive-form' property if present, analogous to the
1862 function-documentation property. */
1864 while (SYMBOLP (fun
))
1866 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1869 fun
= Fsymbol_function (fun
);
1872 if (scm_is_true (scm_procedure_p (fun
)))
1873 return (scm_is_true (scm_procedure_property (fun
, Qinteractive_form
))
1875 /* Bytecode objects are interactive if they are long enough to
1876 have an element whose index is COMPILED_INTERACTIVE, which is
1877 where the interactive spec is stored. */
1878 else if (COMPILEDP (fun
))
1879 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1882 /* Strings and vectors are keyboard macros. */
1883 if (STRINGP (fun
) || VECTORP (fun
))
1884 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1886 /* Lists may represent commands. */
1889 funcar
= XCAR (fun
);
1890 if (EQ (funcar
, Qclosure
))
1891 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1893 else if (EQ (funcar
, Qlambda
))
1894 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1895 else if (EQ (funcar
, Qautoload
))
1896 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1901 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1902 doc
: /* Define FUNCTION to autoload from FILE.
1903 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1904 Third arg DOCSTRING is documentation for the function.
1905 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1906 Fifth arg TYPE indicates the type of the object:
1907 nil or omitted says FUNCTION is a function,
1908 `keymap' says FUNCTION is really a keymap, and
1909 `macro' or t says FUNCTION is really a macro.
1910 Third through fifth args give info about the real definition.
1911 They default to nil.
1912 If FUNCTION is already defined other than as an autoload,
1913 this does nothing and returns nil. */)
1914 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1916 CHECK_SYMBOL (function
);
1917 CHECK_STRING (file
);
1919 /* If function is defined and not as an autoload, don't override. */
1920 if (!NILP (SYMBOL_FUNCTION (function
))
1921 && !AUTOLOADP (SYMBOL_FUNCTION (function
)))
1924 return Fdefalias (function
,
1925 list5 (Qautoload
, file
, docstring
, interactive
, type
),
1930 un_autoload (Lisp_Object oldqueue
)
1932 Lisp_Object queue
, first
, second
;
1934 /* Queue to unwind is current value of Vautoload_queue.
1935 oldqueue is the shadowed value to leave in Vautoload_queue. */
1936 queue
= Vautoload_queue
;
1937 Vautoload_queue
= oldqueue
;
1938 while (CONSP (queue
))
1940 first
= XCAR (queue
);
1941 second
= Fcdr (first
);
1942 first
= Fcar (first
);
1943 if (EQ (first
, make_number (0)))
1946 Ffset (first
, second
);
1947 queue
= XCDR (queue
);
1951 /* Load an autoloaded function.
1952 FUNNAME is the symbol which is the function's name.
1953 FUNDEF is the autoload definition (a list). */
1955 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1956 doc
: /* Load FUNDEF which should be an autoload.
1957 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1958 in which case the function returns the new autoloaded function value.
1959 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1960 it is defines a macro. */)
1961 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1964 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1966 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
))) {
1971 if (EQ (macro_only
, Qmacro
))
1973 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1974 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
))) {
1980 /* This is to make sure that loadup.el gives a clear picture
1981 of what files are preloaded and when. */
1982 if (! NILP (Vpurify_flag
))
1983 error ("Attempt to autoload %s while preparing to dump",
1984 SDATA (SYMBOL_NAME (funname
)));
1986 CHECK_SYMBOL (funname
);
1987 GCPRO3 (funname
, fundef
, macro_only
);
1989 /* Preserve the match data. */
1990 record_unwind_save_match_data ();
1992 /* If autoloading gets an error (which includes the error of failing
1993 to define the function being called), we use Vautoload_queue
1994 to undo function definitions and `provide' calls made by
1995 the function. We do this in the specific case of autoloading
1996 because autoloading is not an explicit request "load this file",
1997 but rather a request to "call this function".
1999 The value saved here is to be restored into Vautoload_queue. */
2000 record_unwind_protect (un_autoload
, Vautoload_queue
);
2001 Vautoload_queue
= Qt
;
2002 /* If `macro_only', assume this autoload to be a "best-effort",
2003 so don't signal an error if autoloading fails. */
2004 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
2006 /* Once loading finishes, don't undo it. */
2007 Vautoload_queue
= Qt
;
2016 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2018 if (!NILP (Fequal (fun
, fundef
)))
2019 error ("Autoloading failed to define function %s",
2020 SDATA (SYMBOL_NAME (funname
)));
2027 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2028 doc
: /* Evaluate FORM and return its value.
2029 If LEXICAL is t, evaluate using lexical scoping.
2030 LEXICAL can also be an actual lexical environment, in the form of an
2031 alist mapping symbols to their value. */)
2032 (Lisp_Object form
, Lisp_Object lexical
)
2035 specbind (Qinternal_interpreter_environment
,
2036 CONSP (lexical
) || NILP (lexical
) ? lexical
: list1 (Qt
));
2037 Lisp_Object tem0
= eval_sub (form
);
2042 /* Grow the specpdl stack by one entry.
2043 The caller should have already initialized the entry.
2044 Signal an error on stack overflow.
2046 Make sure that there is always one unused entry past the top of the
2047 stack, so that the just-initialized entry is safely unwound if
2048 memory exhausted and an error is signaled here. Also, allocate a
2049 never-used entry just before the bottom of the stack; sometimes its
2050 address is taken. */
2057 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2059 ptrdiff_t count
= SPECPDL_INDEX ();
2060 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
- 1000);
2061 union specbinding
*pdlvec
= specpdl
- 1;
2062 ptrdiff_t pdlvecsize
= specpdl_size
+ 1;
2063 if (max_size
<= specpdl_size
)
2065 if (max_specpdl_size
< 400)
2066 max_size
= max_specpdl_size
= 400;
2067 if (max_size
<= specpdl_size
)
2068 signal_error ("Variable binding depth exceeds max-specpdl-size",
2071 pdlvec
= xpalloc (pdlvec
, &pdlvecsize
, 1, max_size
+ 1, sizeof *specpdl
);
2072 specpdl
= pdlvec
+ 1;
2073 specpdl_size
= pdlvecsize
- 1;
2074 specpdl_ptr
= specpdl
+ count
;
2079 record_in_backtrace (Lisp_Object function
, Lisp_Object
*args
, ptrdiff_t nargs
)
2081 eassert (nargs
>= UNEVALLED
);
2082 specpdl_ptr
->bt
.kind
= SPECPDL_BACKTRACE
;
2083 specpdl_ptr
->bt
.debug_on_exit
= false;
2084 specpdl_ptr
->bt
.function
= function
;
2085 specpdl_ptr
->bt
.args
= args
;
2086 specpdl_ptr
->bt
.nargs
= nargs
;
2088 scm_dynwind_unwind_handler (unbind_once
, NULL
, SCM_F_WIND_EXPLICITLY
);
2092 set_lisp_eval_depth (void *data
)
2094 EMACS_INT n
= (EMACS_INT
) data
;
2095 lisp_eval_depth
= n
;
2098 /* Eval a sub-expression of the current expression (i.e. in the same
2101 eval_sub_1 (Lisp_Object form
)
2103 Lisp_Object fun
, val
, original_fun
, original_args
;
2105 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2109 /* Look up its binding in the lexical environment.
2110 We do not pay attention to the declared_special flag here, since we
2111 already did that when let-binding the variable. */
2112 Lisp_Object lex_binding
2113 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2114 ? Fassq (form
, Vinternal_interpreter_environment
)
2116 if (CONSP (lex_binding
))
2117 return XCDR (lex_binding
);
2119 return Fsymbol_value (form
);
2131 scm_dynwind_begin (0);
2132 scm_dynwind_unwind_handler (set_lisp_eval_depth
,
2133 (void *) lisp_eval_depth
,
2134 SCM_F_WIND_EXPLICITLY
);
2136 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2138 if (max_lisp_eval_depth
< 100)
2139 max_lisp_eval_depth
= 100;
2140 if (lisp_eval_depth
> max_lisp_eval_depth
)
2141 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2144 original_fun
= XCAR (form
);
2145 original_args
= XCDR (form
);
2147 /* This also protects them from gc. */
2148 record_in_backtrace (original_fun
, &original_args
, UNEVALLED
);
2150 if (debug_on_next_call
)
2151 do_debug_on_call (Qt
);
2153 /* At this point, only original_fun and original_args
2154 have values that will be used below. */
2157 /* Optimize for no indirection. */
2160 fun
= Ffunction (Fcons (fun
, Qnil
));
2161 else if (!NILP (fun
) && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2162 fun
= indirect_function (fun
);
2164 if (scm_is_true (scm_procedure_p (fun
)))
2166 Lisp_Object args_left
= original_args
;
2167 Lisp_Object nargs
= Flength (args_left
);
2171 SAFE_ALLOCA_LISP (args
, XINT (nargs
));
2173 while (! NILP (args_left
))
2175 args
[argnum
++] = eval_sub (Fcar (args_left
));
2176 args_left
= Fcdr (args_left
);
2178 set_backtrace_args (specpdl_ptr
- 1, args
);
2179 set_backtrace_nargs (specpdl_ptr
- 1, argnum
);
2180 val
= scm_call_n (fun
, args
, argnum
);
2182 else if (CONSP (fun
) && EQ (XCAR (fun
), Qspecial_operator
))
2184 val
= scm_apply_0 (XCDR (fun
), original_args
);
2186 else if (COMPILEDP (fun
))
2187 val
= apply_lambda (fun
, original_args
);
2191 xsignal1 (Qvoid_function
, original_fun
);
2193 xsignal1 (Qinvalid_function
, original_fun
);
2194 funcar
= XCAR (fun
);
2195 if (!SYMBOLP (funcar
))
2196 xsignal1 (Qinvalid_function
, original_fun
);
2197 if (EQ (funcar
, Qautoload
))
2199 Fautoload_do_load (fun
, original_fun
, Qnil
);
2202 if (EQ (funcar
, Qmacro
))
2206 /* Bind lexical-binding during expansion of the macro, so the
2207 macro can know reliably if the code it outputs will be
2208 interpreted using lexical-binding or not. */
2209 specbind (Qlexical_binding
,
2210 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2211 exp
= apply1 (Fcdr (fun
), original_args
);
2213 val
= eval_sub (exp
);
2215 else if (EQ (funcar
, Qlambda
)
2216 || EQ (funcar
, Qclosure
))
2217 val
= apply_lambda (fun
, original_args
);
2219 xsignal1 (Qinvalid_function
, original_fun
);
2222 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2223 val
= call_debugger (list2 (Qexit
, val
));
2230 eval_sub (Lisp_Object form
)
2232 return scm_c_value_ref (eval_sub_1 (form
), 0);
2236 values_to_list (Lisp_Object values
)
2238 Lisp_Object list
= Qnil
;
2239 for (int i
= scm_c_nvalues (values
) - 1; i
>= 0; i
--)
2240 list
= Fcons (scm_c_value_ref (values
, i
), list
);
2244 DEFUN ("multiple-value-call", Fmultiple_value_call
, Smultiple_value_call
,
2246 doc
: /* Call with multiple values.
2247 usage: (multiple-value-call FUNCTION-FORM FORM) */)
2250 Lisp_Object function_form
= eval_sub (XCAR (args
));
2251 Lisp_Object values
= Qnil
;
2252 while (CONSP (args
= XCDR (args
)))
2253 values
= nconc2 (Fnreverse (values_to_list (eval_sub_1 (XCAR (args
)))),
2255 return apply1 (function_form
, Fnreverse (values
));
2258 DEFUN ("values", Fvalues
, Svalues
, 0, MANY
, 0,
2259 doc
: /* Return multiple values. */)
2260 (ptrdiff_t nargs
, Lisp_Object
*args
)
2262 return scm_c_values (args
, nargs
);
2265 DEFUN ("bind-symbol", Fbind_symbol
, Sbind_symbol
, 3, 3, 0,
2266 doc
: /* Bind symbol. */)
2267 (Lisp_Object symbol
, Lisp_Object value
, Lisp_Object thunk
)
2271 specbind (symbol
, value
);
2272 val
= call0 (thunk
);
2277 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2278 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2279 Then return the value FUNCTION returns.
2280 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2281 usage: (apply FUNCTION &rest ARGUMENTS) */)
2282 (ptrdiff_t nargs
, Lisp_Object
*args
)
2286 register Lisp_Object spread_arg
;
2287 register Lisp_Object
*funcall_args
;
2288 Lisp_Object fun
, retval
;
2289 struct gcpro gcpro1
;
2294 spread_arg
= args
[nargs
- 1];
2295 CHECK_LIST (spread_arg
);
2297 numargs
= XINT (Flength (spread_arg
));
2300 return Ffuncall (nargs
- 1, args
);
2301 else if (numargs
== 1)
2303 args
[nargs
- 1] = XCAR (spread_arg
);
2304 return Ffuncall (nargs
, args
);
2307 numargs
+= nargs
- 2;
2309 /* Optimize for no indirection. */
2310 if (SYMBOLP (fun
) && !NILP (fun
)
2311 && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2312 fun
= indirect_function (fun
);
2315 /* Let funcall get the error. */
2319 /* We add 1 to numargs because funcall_args includes the
2320 function itself as well as its arguments. */
2323 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2324 GCPRO1 (*funcall_args
);
2325 gcpro1
.nvars
= 1 + numargs
;
2328 memcpy (funcall_args
, args
, nargs
* word_size
);
2329 /* Spread the last arg we got. Its first element goes in
2330 the slot that it used to occupy, hence this value of I. */
2332 while (!NILP (spread_arg
))
2334 funcall_args
[i
++] = XCAR (spread_arg
);
2335 spread_arg
= XCDR (spread_arg
);
2338 /* By convention, the caller needs to gcpro Ffuncall's args. */
2339 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2346 /* Run hook variables in various ways. */
2349 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2351 Ffuncall (nargs
, args
);
2355 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2356 doc
: /* Run each hook in HOOKS.
2357 Each argument should be a symbol, a hook variable.
2358 These symbols are processed in the order specified.
2359 If a hook symbol has a non-nil value, that value may be a function
2360 or a list of functions to be called to run the hook.
2361 If the value is a function, it is called with no arguments.
2362 If it is a list, the elements are called, in order, with no arguments.
2364 Major modes should not use this function directly to run their mode
2365 hook; they should use `run-mode-hooks' instead.
2367 Do not use `make-local-variable' to make a hook variable buffer-local.
2368 Instead, use `add-hook' and specify t for the LOCAL argument.
2369 usage: (run-hooks &rest HOOKS) */)
2370 (ptrdiff_t nargs
, Lisp_Object
*args
)
2372 Lisp_Object hook
[1];
2375 for (i
= 0; i
< nargs
; i
++)
2378 run_hook_with_args (1, hook
, funcall_nil
);
2384 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2385 Srun_hook_with_args
, 1, MANY
, 0,
2386 doc
: /* Run HOOK with the specified arguments ARGS.
2387 HOOK should be a symbol, a hook variable. The value of HOOK
2388 may be nil, a function, or a list of functions. Call each
2389 function in order with arguments ARGS. The final return value
2392 Do not use `make-local-variable' to make a hook variable buffer-local.
2393 Instead, use `add-hook' and specify t for the LOCAL argument.
2394 usage: (run-hook-with-args HOOK &rest ARGS) */)
2395 (ptrdiff_t nargs
, Lisp_Object
*args
)
2397 return run_hook_with_args (nargs
, args
, funcall_nil
);
2400 /* NB this one still documents a specific non-nil return value.
2401 (As did run-hook-with-args and run-hook-with-args-until-failure
2402 until they were changed in 24.1.) */
2403 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2404 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2405 doc
: /* Run HOOK with the specified arguments ARGS.
2406 HOOK should be a symbol, a hook variable. The value of HOOK
2407 may be nil, a function, or a list of functions. Call each
2408 function in order with arguments ARGS, stopping at the first
2409 one that returns non-nil, and return that value. Otherwise (if
2410 all functions return nil, or if there are no functions to call),
2413 Do not use `make-local-variable' to make a hook variable buffer-local.
2414 Instead, use `add-hook' and specify t for the LOCAL argument.
2415 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2416 (ptrdiff_t nargs
, Lisp_Object
*args
)
2418 return run_hook_with_args (nargs
, args
, Ffuncall
);
2422 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2424 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2427 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2428 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2429 doc
: /* Run HOOK with the specified arguments ARGS.
2430 HOOK should be a symbol, a hook variable. The value of HOOK
2431 may be nil, a function, or a list of functions. Call each
2432 function in order with arguments ARGS, stopping at the first
2433 one that returns nil, and return nil. Otherwise (if all functions
2434 return non-nil, or if there are no functions to call), return non-nil
2435 \(do not rely on the precise return value in this case).
2437 Do not use `make-local-variable' to make a hook variable buffer-local.
2438 Instead, use `add-hook' and specify t for the LOCAL argument.
2439 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2440 (ptrdiff_t nargs
, Lisp_Object
*args
)
2442 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2446 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2448 Lisp_Object tmp
= args
[0], ret
;
2451 ret
= Ffuncall (nargs
, args
);
2457 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2458 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2459 I.e. instead of calling each function FUN directly with arguments ARGS,
2460 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2461 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2462 aborts and returns that value.
2463 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2464 (ptrdiff_t nargs
, Lisp_Object
*args
)
2466 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2469 /* ARGS[0] should be a hook symbol.
2470 Call each of the functions in the hook value, passing each of them
2471 as arguments all the rest of ARGS (all NARGS - 1 elements).
2472 FUNCALL specifies how to call each function on the hook.
2473 The caller (or its caller, etc) must gcpro all of ARGS,
2474 except that it isn't necessary to gcpro ARGS[0]. */
2477 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2478 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2480 Lisp_Object sym
, val
, ret
= Qnil
;
2481 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2483 /* If we are dying or still initializing,
2484 don't do anything--it would probably crash if we tried. */
2485 if (NILP (Vrun_hooks
))
2489 val
= find_symbol_value (sym
);
2491 if (EQ (val
, Qunbound
) || NILP (val
))
2493 else if (!CONSP (val
) || FUNCTIONP (val
))
2496 return funcall (nargs
, args
);
2500 Lisp_Object global_vals
= Qnil
;
2501 GCPRO3 (sym
, val
, global_vals
);
2504 CONSP (val
) && NILP (ret
);
2507 if (EQ (XCAR (val
), Qt
))
2509 /* t indicates this hook has a local binding;
2510 it means to run the global binding too. */
2511 global_vals
= Fdefault_value (sym
);
2512 if (NILP (global_vals
)) continue;
2514 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2516 args
[0] = global_vals
;
2517 ret
= funcall (nargs
, args
);
2522 CONSP (global_vals
) && NILP (ret
);
2523 global_vals
= XCDR (global_vals
))
2525 args
[0] = XCAR (global_vals
);
2526 /* In a global value, t should not occur. If it does, we
2527 must ignore it to avoid an endless loop. */
2528 if (!EQ (args
[0], Qt
))
2529 ret
= funcall (nargs
, args
);
2535 args
[0] = XCAR (val
);
2536 ret
= funcall (nargs
, args
);
2545 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2548 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2550 Lisp_Object temp
[3];
2555 Frun_hook_with_args (3, temp
);
2558 /* Apply fn to arg. */
2560 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2562 struct gcpro gcpro1
;
2566 return Ffuncall (1, &fn
);
2569 Lisp_Object args
[2];
2573 return Fapply (2, args
);
2577 /* Call function fn on no arguments. */
2579 call0 (Lisp_Object fn
)
2581 struct gcpro gcpro1
;
2584 return Ffuncall (1, &fn
);
2587 /* Call function fn with 1 argument arg1. */
2590 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2592 struct gcpro gcpro1
;
2593 Lisp_Object args
[2];
2599 return Ffuncall (2, args
);
2602 /* Call function fn with 2 arguments arg1, arg2. */
2605 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2607 struct gcpro gcpro1
;
2608 Lisp_Object args
[3];
2614 return Ffuncall (3, args
);
2617 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2620 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2622 struct gcpro gcpro1
;
2623 Lisp_Object args
[4];
2630 return Ffuncall (4, args
);
2633 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2636 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2639 struct gcpro gcpro1
;
2640 Lisp_Object args
[5];
2648 return Ffuncall (5, args
);
2651 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2654 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2655 Lisp_Object arg4
, Lisp_Object arg5
)
2657 struct gcpro gcpro1
;
2658 Lisp_Object args
[6];
2667 return Ffuncall (6, args
);
2670 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2673 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2674 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2676 struct gcpro gcpro1
;
2677 Lisp_Object args
[7];
2687 return Ffuncall (7, args
);
2690 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2693 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2694 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2696 struct gcpro gcpro1
;
2697 Lisp_Object args
[8];
2708 return Ffuncall (8, args
);
2711 /* The caller should GCPRO all the elements of ARGS. */
2713 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2714 doc
: /* Non-nil if OBJECT is a function. */)
2715 (Lisp_Object object
)
2717 if (FUNCTIONP (object
))
2722 DEFUN ("funcall", Ffuncall1
, Sfuncall
, 1, MANY
, 0,
2723 doc
: /* Call first argument as a function, passing remaining arguments to it.
2724 Return the value that function returns.
2725 Thus, (funcall 'cons 'x 'y) returns (x . y).
2726 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2727 (ptrdiff_t nargs
, Lisp_Object
*args
)
2729 Lisp_Object fun
, original_fun
;
2731 ptrdiff_t numargs
= nargs
- 1;
2732 Lisp_Object lisp_numargs
;
2734 register Lisp_Object
*internal_args
;
2739 scm_dynwind_begin (0);
2740 scm_dynwind_unwind_handler (set_lisp_eval_depth
,
2741 (void *) lisp_eval_depth
,
2742 SCM_F_WIND_EXPLICITLY
);
2744 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2746 if (max_lisp_eval_depth
< 100)
2747 max_lisp_eval_depth
= 100;
2748 if (lisp_eval_depth
> max_lisp_eval_depth
)
2749 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2752 /* This also GCPROs them. */
2753 record_in_backtrace (args
[0], &args
[1], nargs
- 1);
2755 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2758 if (debug_on_next_call
)
2759 do_debug_on_call (Qlambda
);
2761 original_fun
= args
[0];
2765 /* Optimize for no indirection. */
2767 if (SYMBOLP (fun
) && !NILP (fun
)
2768 && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2769 fun
= indirect_function (fun
);
2771 if (scm_is_true (scm_procedure_p (fun
)))
2773 val
= scm_call_n (fun
, args
+ 1, numargs
);
2775 else if (COMPILEDP (fun
))
2776 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2780 xsignal1 (Qvoid_function
, original_fun
);
2782 xsignal1 (Qinvalid_function
, original_fun
);
2783 funcar
= XCAR (fun
);
2784 if (!SYMBOLP (funcar
))
2785 xsignal1 (Qinvalid_function
, original_fun
);
2786 if (EQ (funcar
, Qlambda
)
2787 || EQ (funcar
, Qclosure
))
2788 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2789 else if (EQ (funcar
, Qautoload
))
2791 Fautoload_do_load (fun
, original_fun
, Qnil
);
2795 xsignal1 (Qinvalid_function
, original_fun
);
2797 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2798 val
= call_debugger (list2 (Qexit
, val
));
2804 Ffuncall (ptrdiff_t nargs
, Lisp_Object
*args
)
2806 return scm_c_value_ref (Ffuncall1 (nargs
, args
), 0);
2810 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2812 Lisp_Object args_left
;
2815 register Lisp_Object
*arg_vector
;
2816 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2817 register Lisp_Object tem
;
2820 numargs
= XFASTINT (Flength (args
));
2821 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2824 GCPRO3 (*arg_vector
, args_left
, fun
);
2827 for (i
= 0; i
< numargs
; )
2829 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2830 tem
= eval_sub (tem
);
2831 arg_vector
[i
++] = tem
;
2837 set_backtrace_args (specpdl_ptr
- 1, arg_vector
);
2838 set_backtrace_nargs (specpdl_ptr
- 1, i
);
2839 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2841 /* Do the debug-on-exit now, while arg_vector still exists. */
2842 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2844 /* Don't do it again when we return to eval. */
2845 set_backtrace_debug_on_exit (specpdl_ptr
- 1, false);
2846 tem
= call_debugger (list2 (Qexit
, tem
));
2852 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2853 and return the result of evaluation.
2854 FUN must be either a lambda-expression or a compiled-code object. */
2857 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2858 register Lisp_Object
*arg_vector
)
2860 Lisp_Object val
, syms_left
, next
, lexenv
;
2863 bool optional
, rest
;
2867 if (EQ (XCAR (fun
), Qclosure
))
2869 fun
= XCDR (fun
); /* Drop `closure'. */
2870 lexenv
= XCAR (fun
);
2871 CHECK_LIST_CONS (fun
, fun
);
2875 syms_left
= XCDR (fun
);
2876 if (CONSP (syms_left
))
2877 syms_left
= XCAR (syms_left
);
2879 xsignal1 (Qinvalid_function
, fun
);
2881 else if (COMPILEDP (fun
))
2883 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2884 if (INTEGERP (syms_left
))
2885 /* A byte-code object with a non-nil `push args' slot means we
2886 shouldn't bind any arguments, instead just call the byte-code
2887 interpreter directly; it will push arguments as necessary.
2889 Byte-code objects with either a non-existent, or a nil value for
2890 the `push args' slot (the default), have dynamically-bound
2891 arguments, and use the argument-binding code below instead (as do
2892 all interpreted functions, even lexically bound ones). */
2894 /* If we have not actually read the bytecode string
2895 and constants vector yet, fetch them from the file. */
2896 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2897 Ffetch_bytecode (fun
);
2899 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2900 AREF (fun
, COMPILED_CONSTANTS
),
2901 AREF (fun
, COMPILED_STACK_DEPTH
),
2910 i
= optional
= rest
= 0;
2911 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2915 next
= XCAR (syms_left
);
2916 if (!SYMBOLP (next
))
2917 xsignal1 (Qinvalid_function
, fun
);
2919 if (EQ (next
, Qand_rest
))
2921 else if (EQ (next
, Qand_optional
))
2928 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2932 arg
= arg_vector
[i
++];
2934 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2938 /* Bind the argument. */
2939 if (!NILP (lexenv
) && SYMBOLP (next
))
2940 /* Lexically bind NEXT by adding it to the lexenv alist. */
2941 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
2943 /* Dynamically bind NEXT. */
2944 specbind (next
, arg
);
2948 if (!NILP (syms_left
))
2949 xsignal1 (Qinvalid_function
, fun
);
2951 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2953 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
2954 /* Instantiate a new lexical environment. */
2955 specbind (Qinternal_interpreter_environment
, lexenv
);
2958 val
= Fprogn (XCDR (XCDR (fun
)));
2961 /* If we have not actually read the bytecode string
2962 and constants vector yet, fetch them from the file. */
2963 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2964 Ffetch_bytecode (fun
);
2965 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2966 AREF (fun
, COMPILED_CONSTANTS
),
2967 AREF (fun
, COMPILED_STACK_DEPTH
),
2975 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2977 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2978 (Lisp_Object object
)
2982 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
2984 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
2987 tem
= AREF (object
, COMPILED_BYTECODE
);
2988 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
2989 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
2991 error ("Invalid byte code");
2993 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
2994 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
2999 /* Return true if SYMBOL currently has a let-binding
3000 which was made in the buffer that is now current. */
3003 let_shadows_buffer_binding_p (sym_t symbol
)
3005 union specbinding
*p
;
3006 Lisp_Object buf
= Fcurrent_buffer ();
3008 for (p
= specpdl_ptr
; p
> specpdl
; )
3009 if ((--p
)->kind
> SPECPDL_LET
)
3011 sym_t let_bound_symbol
= XSYMBOL (specpdl_symbol (p
));
3012 eassert (SYMBOL_REDIRECT (let_bound_symbol
) != SYMBOL_VARALIAS
);
3013 if (symbol
== let_bound_symbol
3014 && EQ (specpdl_where (p
), buf
))
3022 let_shadows_global_binding_p (Lisp_Object symbol
)
3024 union specbinding
*p
;
3026 for (p
= specpdl_ptr
; p
> specpdl
; )
3027 if ((--p
)->kind
>= SPECPDL_LET
&& EQ (specpdl_symbol (p
), symbol
))
3033 /* `specpdl_ptr' describes which variable is
3034 let-bound, so it can be properly undone when we unbind_to.
3035 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3036 - SYMBOL is the variable being bound. Note that it should not be
3037 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3039 - WHERE tells us in which buffer the binding took place.
3040 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3041 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3042 i.e. bindings to the default value of a variable which can be
3046 specbind (Lisp_Object symbol
, Lisp_Object value
)
3050 CHECK_SYMBOL (symbol
);
3051 sym
= XSYMBOL (symbol
);
3054 switch (SYMBOL_REDIRECT (sym
))
3056 case SYMBOL_VARALIAS
:
3057 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3058 case SYMBOL_PLAINVAL
:
3059 /* The most common case is that of a non-constant symbol with a
3060 trivial value. Make that as fast as we can. */
3061 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3062 specpdl_ptr
->let
.symbol
= symbol
;
3063 specpdl_ptr
->let
.old_value
= SYMBOL_VAL (sym
);
3065 if (! SYMBOL_CONSTANT (sym
))
3066 SET_SYMBOL_VAL (sym
, value
);
3068 set_internal (symbol
, value
, Qnil
, 1);
3070 case SYMBOL_LOCALIZED
:
3071 if (SYMBOL_BLV (sym
)->frame_local
)
3072 error ("Frame-local vars cannot be let-bound");
3073 case SYMBOL_FORWARDED
:
3075 Lisp_Object ovalue
= find_symbol_value (symbol
);
3076 specpdl_ptr
->let
.kind
= SPECPDL_LET_LOCAL
;
3077 specpdl_ptr
->let
.symbol
= symbol
;
3078 specpdl_ptr
->let
.old_value
= ovalue
;
3079 specpdl_ptr
->let
.where
= Fcurrent_buffer ();
3081 eassert (SYMBOL_REDIRECT (sym
) != SYMBOL_LOCALIZED
3082 || (EQ (SYMBOL_BLV (sym
)->where
, Fcurrent_buffer ())));
3084 if (SYMBOL_REDIRECT (sym
) == SYMBOL_LOCALIZED
)
3086 if (!blv_found (SYMBOL_BLV (sym
)))
3087 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3089 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3091 /* If SYMBOL is a per-buffer variable which doesn't have a
3092 buffer-local value here, make the `let' change the global
3093 value by changing the value of SYMBOL in all buffers not
3094 having their own value. This is consistent with what
3095 happens with other buffer-local variables. */
3096 if (NILP (Flocal_variable_p (symbol
, Qnil
)))
3098 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3100 Fset_default (symbol
, value
);
3105 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3108 set_internal (symbol
, value
, Qnil
, 1);
3111 default: emacs_abort ();
3115 scm_dynwind_unwind_handler (unbind_once
, NULL
, SCM_F_WIND_EXPLICITLY
);
3118 /* Push unwind-protect entries of various types. */
3121 record_unwind_protect_1 (void (*function
) (Lisp_Object
), Lisp_Object arg
,
3122 bool wind_explicitly
)
3124 record_unwind_protect_ptr_1 (function
, arg
, wind_explicitly
);
3128 record_unwind_protect (void (*function
) (Lisp_Object
), Lisp_Object arg
)
3130 record_unwind_protect_1 (function
, arg
, true);
3134 record_unwind_protect_ptr_1 (void (*function
) (void *), void *arg
,
3135 bool wind_explicitly
)
3137 scm_dynwind_unwind_handler (function
,
3140 ? SCM_F_WIND_EXPLICITLY
3145 record_unwind_protect_ptr (void (*function
) (void *), void *arg
)
3147 record_unwind_protect_ptr_1 (function
, arg
, true);
3151 record_unwind_protect_int_1 (void (*function
) (int), int arg
,
3152 bool wind_explicitly
)
3154 record_unwind_protect_ptr_1 (function
, arg
, wind_explicitly
);
3158 record_unwind_protect_int (void (*function
) (int), int arg
)
3160 record_unwind_protect_int_1 (function
, arg
, true);
3164 call_void (void *data
)
3166 ((void (*) (void)) data
) ();
3170 record_unwind_protect_void_1 (void (*function
) (void),
3171 bool wind_explicitly
)
3173 record_unwind_protect_ptr_1 (call_void
, function
, wind_explicitly
);
3177 record_unwind_protect_void (void (*function
) (void))
3179 record_unwind_protect_void_1 (function
, true);
3183 unbind_once (void *ignore
)
3185 /* Decrement specpdl_ptr before we do the work to unbind it, so
3186 that an error in unbinding won't try to unbind the same entry
3187 again. Take care to copy any parts of the binding needed
3188 before invoking any code that can make more bindings. */
3192 switch (specpdl_ptr
->kind
)
3194 case SPECPDL_BACKTRACE
:
3197 { /* If variable has a trivial value (no forwarding), we can
3198 just set it. No need to check for constant symbols here,
3199 since that was already done by specbind. */
3200 sym_t sym
= XSYMBOL (specpdl_symbol (specpdl_ptr
));
3201 if (SYMBOL_REDIRECT (sym
) == SYMBOL_PLAINVAL
)
3203 SET_SYMBOL_VAL (sym
, specpdl_old_value (specpdl_ptr
));
3208 NOTE: we only ever come here if make_local_foo was used for
3209 the first time on this var within this let. */
3212 case SPECPDL_LET_DEFAULT
:
3213 Fset_default (specpdl_symbol (specpdl_ptr
),
3214 specpdl_old_value (specpdl_ptr
));
3216 case SPECPDL_LET_LOCAL
:
3218 Lisp_Object symbol
= specpdl_symbol (specpdl_ptr
);
3219 Lisp_Object where
= specpdl_where (specpdl_ptr
);
3220 Lisp_Object old_value
= specpdl_old_value (specpdl_ptr
);
3221 eassert (BUFFERP (where
));
3223 /* If this was a local binding, reset the value in the appropriate
3224 buffer, but only if that buffer's binding still exists. */
3225 if (!NILP (Flocal_variable_p (symbol
, where
)))
3226 set_internal (symbol
, old_value
, where
, 1);
3233 dynwind_begin (void)
3235 scm_dynwind_begin (0);
3244 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3245 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3246 A special variable is one that will be bound dynamically, even in a
3247 context where binding is lexical by default. */)
3248 (Lisp_Object symbol
)
3250 CHECK_SYMBOL (symbol
);
3251 return SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol
)) ? Qt
: Qnil
;
3255 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3256 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3257 The debugger is entered when that frame exits, if the flag is non-nil. */)
3258 (Lisp_Object level
, Lisp_Object flag
)
3260 union specbinding
*pdl
= backtrace_top ();
3261 register EMACS_INT i
;
3263 CHECK_NUMBER (level
);
3265 for (i
= 0; backtrace_p (pdl
) && i
< XINT (level
); i
++)
3266 pdl
= backtrace_next (pdl
);
3268 if (backtrace_p (pdl
))
3269 set_backtrace_debug_on_exit (pdl
, !NILP (flag
));
3274 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3275 doc
: /* Print a trace of Lisp function calls currently active.
3276 Output stream used is value of `standard-output'. */)
3279 union specbinding
*pdl
= backtrace_top ();
3281 Lisp_Object old_print_level
= Vprint_level
;
3283 if (NILP (Vprint_level
))
3284 XSETFASTINT (Vprint_level
, 8);
3286 while (backtrace_p (pdl
))
3288 write_string (backtrace_debug_on_exit (pdl
) ? "* " : " ", 2);
3289 if (backtrace_nargs (pdl
) == UNEVALLED
)
3291 Fprin1 (Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)),
3293 write_string ("\n", -1);
3297 tem
= backtrace_function (pdl
);
3298 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3299 write_string ("(", -1);
3302 for (i
= 0; i
< backtrace_nargs (pdl
); i
++)
3304 if (i
) write_string (" ", -1);
3305 Fprin1 (backtrace_args (pdl
)[i
], Qnil
);
3308 write_string (")\n", -1);
3310 pdl
= backtrace_next (pdl
);
3313 Vprint_level
= old_print_level
;
3317 static union specbinding
*
3318 get_backtrace_frame (Lisp_Object nframes
, Lisp_Object base
)
3320 union specbinding
*pdl
= backtrace_top ();
3321 register EMACS_INT i
;
3323 CHECK_NATNUM (nframes
);
3326 { /* Skip up to `base'. */
3327 base
= Findirect_function (base
, Qt
);
3328 while (backtrace_p (pdl
)
3329 && !EQ (base
, Findirect_function (backtrace_function (pdl
), Qt
)))
3330 pdl
= backtrace_next (pdl
);
3333 /* Find the frame requested. */
3334 for (i
= XFASTINT (nframes
); i
> 0 && backtrace_p (pdl
); i
--)
3335 pdl
= backtrace_next (pdl
);
3340 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 2, NULL
,
3341 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3342 If that frame has not evaluated the arguments yet (or is a special form),
3343 the value is (nil FUNCTION ARG-FORMS...).
3344 If that frame has evaluated its arguments and called its function already,
3345 the value is (t FUNCTION ARG-VALUES...).
3346 A &rest arg is represented as the tail of the list ARG-VALUES.
3347 FUNCTION is whatever was supplied as car of evaluated list,
3348 or a lambda expression for macro calls.
3349 If NFRAMES is more than the number of frames, the value is nil.
3350 If BASE is non-nil, it should be a function and NFRAMES counts from its
3351 nearest activation frame. */)
3352 (Lisp_Object nframes
, Lisp_Object base
)
3354 union specbinding
*pdl
= get_backtrace_frame (nframes
, base
);
3356 if (!backtrace_p (pdl
))
3358 if (backtrace_nargs (pdl
) == UNEVALLED
)
3360 Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)));
3363 Lisp_Object tem
= Flist (backtrace_nargs (pdl
), backtrace_args (pdl
));
3365 return Fcons (Qt
, Fcons (backtrace_function (pdl
), tem
));
3369 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3370 the specpdl stack, and then rewind them. We store the pre-unwind values
3371 directly in the pre-existing specpdl elements (i.e. we swap the current
3372 value and the old value stored in the specpdl), kind of like the inplace
3373 pointer-reversal trick. As it turns out, the rewind does the same as the
3374 unwind, except it starts from the other end of the specpdl stack, so we use
3375 the same function for both unwind and rewind. */
3377 backtrace_eval_unrewind (int distance
)
3379 union specbinding
*tmp
= specpdl_ptr
;
3382 { /* It's a rewind rather than unwind. */
3383 tmp
+= distance
- 1;
3385 distance
= -distance
;
3388 for (; distance
> 0; distance
--)
3394 case SPECPDL_BACKTRACE
:
3397 { /* If variable has a trivial value (no forwarding), we can
3398 just set it. No need to check for constant symbols here,
3399 since that was already done by specbind. */
3400 sym_t sym
= XSYMBOL (specpdl_symbol (tmp
));
3401 if (SYMBOL_REDIRECT (sym
) == SYMBOL_PLAINVAL
)
3403 Lisp_Object old_value
= specpdl_old_value (tmp
);
3404 set_specpdl_old_value (tmp
, SYMBOL_VAL (sym
));
3405 SET_SYMBOL_VAL (sym
, old_value
);
3410 NOTE: we only ever come here if make_local_foo was used for
3411 the first time on this var within this let. */
3414 case SPECPDL_LET_DEFAULT
:
3416 Lisp_Object sym
= specpdl_symbol (tmp
);
3417 Lisp_Object old_value
= specpdl_old_value (tmp
);
3418 set_specpdl_old_value (tmp
, Fdefault_value (sym
));
3419 Fset_default (sym
, old_value
);
3422 case SPECPDL_LET_LOCAL
:
3424 Lisp_Object symbol
= specpdl_symbol (tmp
);
3425 Lisp_Object where
= specpdl_where (tmp
);
3426 Lisp_Object old_value
= specpdl_old_value (tmp
);
3427 eassert (BUFFERP (where
));
3429 /* If this was a local binding, reset the value in the appropriate
3430 buffer, but only if that buffer's binding still exists. */
3431 if (!NILP (Flocal_variable_p (symbol
, where
)))
3433 set_specpdl_old_value
3434 (tmp
, Fbuffer_local_value (symbol
, where
));
3435 set_internal (symbol
, old_value
, where
, 1);
3443 DEFUN ("backtrace-eval", Fbacktrace_eval
, Sbacktrace_eval
, 2, 3, NULL
,
3444 doc
: /* Evaluate EXP in the context of some activation frame.
3445 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3446 (Lisp_Object exp
, Lisp_Object nframes
, Lisp_Object base
)
3448 union specbinding
*pdl
= get_backtrace_frame (nframes
, base
);
3450 ptrdiff_t distance
= specpdl_ptr
- pdl
;
3451 eassert (distance
>= 0);
3453 if (!backtrace_p (pdl
))
3454 error ("Activation frame not found!");
3456 backtrace_eval_unrewind (distance
);
3457 record_unwind_protect_int (backtrace_eval_unrewind
, -distance
);
3459 /* Use eval_sub rather than Feval since the main motivation behind
3460 backtrace-eval is to be able to get/set the value of lexical variables
3461 from the debugger. */
3462 Lisp_Object tem1
= eval_sub (exp
);
3467 DEFUN ("backtrace--locals", Fbacktrace__locals
, Sbacktrace__locals
, 1, 2, NULL
,
3468 doc
: /* Return names and values of local variables of a stack frame.
3469 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3470 (Lisp_Object nframes
, Lisp_Object base
)
3472 union specbinding
*frame
= get_backtrace_frame (nframes
, base
);
3473 union specbinding
*prevframe
3474 = get_backtrace_frame (make_number (XFASTINT (nframes
) - 1), base
);
3475 ptrdiff_t distance
= specpdl_ptr
- frame
;
3476 Lisp_Object result
= Qnil
;
3477 eassert (distance
>= 0);
3479 if (!backtrace_p (prevframe
))
3480 error ("Activation frame not found!");
3481 if (!backtrace_p (frame
))
3482 error ("Activation frame not found!");
3484 /* The specpdl entries normally contain the symbol being bound along with its
3485 `old_value', so it can be restored. The new value to which it is bound is
3486 available in one of two places: either in the current value of the
3487 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3488 next specpdl entry for it.
3489 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3490 and "new value", so we abuse it here, to fetch the new value.
3491 It's ugly (we'd rather not modify global data) and a bit inefficient,
3492 but it does the job for now. */
3493 backtrace_eval_unrewind (distance
);
3497 union specbinding
*tmp
= prevframe
;
3498 for (; tmp
> frame
; tmp
--)
3503 case SPECPDL_LET_DEFAULT
:
3504 case SPECPDL_LET_LOCAL
:
3506 Lisp_Object sym
= specpdl_symbol (tmp
);
3507 Lisp_Object val
= specpdl_old_value (tmp
);
3508 if (EQ (sym
, Qinternal_interpreter_environment
))
3510 Lisp_Object env
= val
;
3511 for (; CONSP (env
); env
= XCDR (env
))
3513 Lisp_Object binding
= XCAR (env
);
3514 if (CONSP (binding
))
3515 result
= Fcons (Fcons (XCAR (binding
),
3521 result
= Fcons (Fcons (sym
, val
), result
);
3527 /* Restore values from specpdl to original place. */
3528 backtrace_eval_unrewind (-distance
);
3535 get_backtrace (Lisp_Object array
)
3537 union specbinding
*pdl
= backtrace_next (backtrace_top ());
3538 ptrdiff_t i
= 0, asize
= ASIZE (array
);
3540 /* Copy the backtrace contents into working memory. */
3541 for (; i
< asize
; i
++)
3543 if (backtrace_p (pdl
))
3545 ASET (array
, i
, backtrace_function (pdl
));
3546 pdl
= backtrace_next (pdl
);
3549 ASET (array
, i
, Qnil
);
3553 Lisp_Object
backtrace_top_function (void)
3555 union specbinding
*pdl
= backtrace_top ();
3556 return (backtrace_p (pdl
) ? backtrace_function (pdl
) : Qnil
);
3560 abort_to_prompt (SCM tag
, SCM arglst
)
3562 static SCM var
= SCM_UNDEFINED
;
3563 if (SCM_UNBNDP (var
))
3564 var
= scm_c_public_lookup ("guile", "abort-to-prompt");
3566 scm_apply_1 (scm_variable_ref (var
), tag
, arglst
);
3571 call_with_prompt (SCM tag
, SCM thunk
, SCM handler
)
3573 static SCM var
= SCM_UNDEFINED
;
3574 if (SCM_UNBNDP (var
))
3575 var
= scm_c_public_lookup ("guile", "call-with-prompt");
3577 return scm_call_3 (scm_variable_ref (var
), tag
, thunk
, handler
);
3581 make_prompt_tag (void)
3583 static SCM var
= SCM_UNDEFINED
;
3584 if (SCM_UNBNDP (var
))
3585 var
= scm_c_public_lookup ("guile", "make-prompt-tag");
3587 return scm_call_0 (scm_variable_ref (var
));
3595 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3596 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3597 If Lisp code tries to increase the total number past this amount,
3598 an error is signaled.
3599 You can safely use a value considerably larger than the default value,
3600 if that proves inconveniently small. However, if you increase it too far,
3601 Emacs could run out of memory trying to make the stack bigger.
3602 Note that this limit may be silently increased by the debugger
3603 if `debug-on-error' or `debug-on-quit' is set. */);
3605 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3606 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3608 This limit serves to catch infinite recursions for you before they cause
3609 actual stack overflow in C, which would be fatal for Emacs.
3610 You can safely make it considerably larger than its default value,
3611 if that proves inconveniently small. However, if you increase it too far,
3612 Emacs could overflow the real C stack, and crash. */);
3614 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3615 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3616 If the value is t, that means do an ordinary quit.
3617 If the value equals `throw-on-input', that means quit by throwing
3618 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3619 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3620 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3623 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3624 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3625 Note that `quit-flag' will still be set by typing C-g,
3626 so a quit will be signaled as soon as `inhibit-quit' is nil.
3627 To prevent this happening, set `quit-flag' to nil
3628 before making `inhibit-quit' nil. */);
3629 Vinhibit_quit
= Qnil
;
3631 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3632 DEFSYM (Qautoload
, "autoload");
3633 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3634 DEFSYM (Qmacro
, "macro");
3635 DEFSYM (Qdeclare
, "declare");
3637 /* Note that the process handling also uses Qexit, but we don't want
3638 to staticpro it twice, so we just do it here. */
3639 DEFSYM (Qexit
, "exit");
3641 DEFSYM (Qinteractive
, "interactive");
3642 DEFSYM (Qcommandp
, "commandp");
3643 DEFSYM (Qand_rest
, "&rest");
3644 DEFSYM (Qand_optional
, "&optional");
3645 DEFSYM (Qclosure
, "closure");
3646 DEFSYM (Qdebug
, "debug");
3648 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3649 doc
: /* Non-nil means never enter the debugger.
3650 Normally set while the debugger is already active, to avoid recursive
3652 Vinhibit_debugger
= Qnil
;
3654 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3655 doc
: /* Non-nil means enter debugger if an error is signaled.
3656 Does not apply to errors handled by `condition-case' or those
3657 matched by `debug-ignored-errors'.
3658 If the value is a list, an error only means to enter the debugger
3659 if one of its condition symbols appears in the list.
3660 When you evaluate an expression interactively, this variable
3661 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3662 The command `toggle-debug-on-error' toggles this.
3663 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3664 Vdebug_on_error
= Qnil
;
3666 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3667 doc
: /* List of errors for which the debugger should not be called.
3668 Each element may be a condition-name or a regexp that matches error messages.
3669 If any element applies to a given error, that error skips the debugger
3670 and just returns to top level.
3671 This overrides the variable `debug-on-error'.
3672 It does not apply to errors handled by `condition-case'. */);
3673 Vdebug_ignored_errors
= Qnil
;
3675 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3676 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3677 Does not apply if quit is handled by a `condition-case'. */);
3680 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3681 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3683 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3684 doc
: /* Non-nil means debugger may continue execution.
3685 This is nil when the debugger is called under circumstances where it
3686 might not be safe to continue. */);
3687 debugger_may_continue
= 1;
3689 DEFVAR_LISP ("debugger", Vdebugger
,
3690 doc
: /* Function to call to invoke debugger.
3691 If due to frame exit, args are `exit' and the value being returned;
3692 this function's value will be returned instead of that.
3693 If due to error, args are `error' and a list of the args to `signal'.
3694 If due to `apply' or `funcall' entry, one arg, `lambda'.
3695 If due to `eval' entry, one arg, t. */);
3698 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3699 doc
: /* If non-nil, this is a function for `signal' to call.
3700 It receives the same arguments that `signal' was given.
3701 The Edebug package uses this to regain control. */);
3702 Vsignal_hook_function
= Qnil
;
3704 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3705 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3706 Note that `debug-on-error', `debug-on-quit' and friends
3707 still determine whether to handle the particular condition. */);
3708 Vdebug_on_signal
= Qnil
;
3710 /* When lexical binding is being used,
3711 Vinternal_interpreter_environment is non-nil, and contains an alist
3712 of lexically-bound variable, or (t), indicating an empty
3713 environment. The lisp name of this variable would be
3714 `internal-interpreter-environment' if it weren't hidden.
3715 Every element of this list can be either a cons (VAR . VAL)
3716 specifying a lexical binding, or a single symbol VAR indicating
3717 that this variable should use dynamic scoping. */
3718 DEFSYM (Qinternal_interpreter_environment
,
3719 "internal-interpreter-environment");
3720 DEFVAR_LISP ("internal-interpreter-environment",
3721 Vinternal_interpreter_environment
,
3722 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3723 When lexical binding is not being used, this variable is nil.
3724 A value of `(t)' indicates an empty environment, otherwise it is an
3725 alist of active lexical bindings. */);
3726 Vinternal_interpreter_environment
= Qnil
;
3727 /* Don't export this variable to Elisp, so no one can mess with it
3728 (Just imagine if someone makes it buffer-local). */
3729 Funintern (Qinternal_interpreter_environment
, Qnil
);
3731 DEFSYM (Vrun_hooks
, "run-hooks");
3733 staticpro (&Vautoload_queue
);
3734 Vautoload_queue
= Qnil
;
3735 staticpro (&Vsignaling_function
);
3736 Vsignaling_function
= Qnil
;
3738 inhibit_lisp_code
= Qnil
;