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
->poll_suppress_count
= poll_suppress_count
;
229 c
->interrupt_input_blocked
= interrupt_input_blocked
;
230 c
->ptag
= make_prompt_tag ();
235 make_condition_handler (Lisp_Object tag
)
237 struct handler
*c
= xmalloc (sizeof (*c
));
238 c
->type
= CONDITION_CASE
;
243 c
->next
= handlerlist
;
244 c
->lisp_eval_depth
= lisp_eval_depth
;
245 c
->poll_suppress_count
= poll_suppress_count
;
246 c
->interrupt_input_blocked
= interrupt_input_blocked
;
247 c
->ptag
= make_prompt_tag ();
252 init_eval_once (void)
255 union specbinding
*pdlvec
= xmalloc ((size
+ 1) * sizeof *specpdl
);
257 specpdl
= specpdl_ptr
= pdlvec
+ 1;
258 /* Don't forget to update docs (lispref node "Local Variables"). */
259 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
260 max_lisp_eval_depth
= 600;
265 static struct handler
*handlerlist_sentinel
;
270 specpdl_ptr
= specpdl
;
271 handlerlist_sentinel
= make_catch_handler (Qunbound
);
272 handlerlist
= handlerlist_sentinel
;
274 debug_on_next_call
= 0;
279 /* This is less than the initial value of num_nonmacro_input_events. */
280 when_entered_debugger
= -1;
283 /* Unwind-protect function used by call_debugger. */
286 restore_stack_limits (Lisp_Object data
)
288 max_specpdl_size
= XINT (XCAR (data
));
289 max_lisp_eval_depth
= XINT (XCDR (data
));
292 static void grow_specpdl (void);
294 /* Call the Lisp debugger, giving it argument ARG. */
297 call_debugger (Lisp_Object arg
)
299 bool debug_while_redisplaying
;
302 EMACS_INT old_depth
= max_lisp_eval_depth
;
303 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
304 EMACS_INT old_max
= max_specpdl_size
;
306 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
307 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
309 /* Restore limits after leaving the debugger. */
310 record_unwind_protect (restore_stack_limits
,
311 Fcons (make_number (old_max
),
312 make_number (old_depth
)));
314 #ifdef HAVE_WINDOW_SYSTEM
315 if (display_hourglass_p
)
319 debug_on_next_call
= 0;
320 when_entered_debugger
= num_nonmacro_input_events
;
322 /* Resetting redisplaying_p to 0 makes sure that debug output is
323 displayed if the debugger is invoked during redisplay. */
324 debug_while_redisplaying
= redisplaying_p
;
326 specbind (intern ("debugger-may-continue"),
327 debug_while_redisplaying
? Qnil
: Qt
);
328 specbind (Qinhibit_redisplay
, Qnil
);
329 specbind (Qinhibit_debugger
, Qt
);
331 #if 0 /* Binding this prevents execution of Lisp code during
332 redisplay, which necessarily leads to display problems. */
333 specbind (Qinhibit_eval_during_redisplay
, Qt
);
336 val
= apply1 (Vdebugger
, arg
);
338 /* Interrupting redisplay and resuming it later is not safe under
339 all circumstances. So, when the debugger returns, abort the
340 interrupted redisplay by going back to the top-level. */
341 if (debug_while_redisplaying
)
349 do_debug_on_call (Lisp_Object code
)
351 debug_on_next_call
= 0;
352 set_backtrace_debug_on_exit (specpdl_ptr
- 1, true);
353 call_debugger (list1 (code
));
356 /* NOTE!!! Every function that can call EVAL must protect its args
357 and temporaries from garbage collection while it needs them.
358 The definition of `For' shows what you have to do. */
360 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
361 doc
: /* Eval args until one of them yields non-nil, then return that value.
362 The remaining args are not evalled at all.
363 If all args return nil, return nil.
364 usage: (or CONDITIONS...) */)
367 register Lisp_Object val
= Qnil
;
374 val
= eval_sub (XCAR (args
));
384 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
385 doc
: /* Eval args until one of them yields nil, then return nil.
386 The remaining args are not evalled at all.
387 If no arg yields nil, return the last arg's value.
388 usage: (and CONDITIONS...) */)
391 register Lisp_Object val
= Qt
;
398 val
= eval_sub (XCAR (args
));
408 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
409 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
410 Returns the value of THEN or the value of the last of the ELSE's.
411 THEN must be one expression, but ELSE... can be zero or more expressions.
412 If COND yields nil, and there are no ELSE's, the value is nil.
413 usage: (if COND THEN ELSE...) */)
420 cond
= eval_sub (XCAR (args
));
424 return eval_sub (Fcar (XCDR (args
)));
425 return Fprogn (XCDR (XCDR (args
)));
428 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
429 doc
: /* Try each clause until one succeeds.
430 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
431 and, if the value is non-nil, this clause succeeds:
432 then the expressions in BODY are evaluated and the last one's
433 value is the value of the cond-form.
434 If a clause has one element, as in (CONDITION), then the cond-form
435 returns CONDITION's value, if that is non-nil.
436 If no clause succeeds, cond returns nil.
437 usage: (cond CLAUSES...) */)
440 Lisp_Object val
= args
;
446 Lisp_Object clause
= XCAR (args
);
447 val
= eval_sub (Fcar (clause
));
450 if (!NILP (XCDR (clause
)))
451 val
= Fprogn (XCDR (clause
));
461 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
462 doc
: /* Eval BODY forms sequentially and return value of last one.
463 usage: (progn BODY...) */)
466 Lisp_Object val
= Qnil
;
473 val
= eval_sub (XCAR (body
));
481 /* Evaluate BODY sequentially, discarding its value. Suitable for
482 record_unwind_protect. */
485 unwind_body (Lisp_Object body
)
490 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
491 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
492 The value of FIRST is saved during the evaluation of the remaining args,
493 whose values are discarded.
494 usage: (prog1 FIRST BODY...) */)
498 Lisp_Object args_left
;
499 struct gcpro gcpro1
, gcpro2
;
505 val
= eval_sub (XCAR (args_left
));
506 while (CONSP (args_left
= XCDR (args_left
)))
507 eval_sub (XCAR (args_left
));
513 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
514 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
515 The value of FORM2 is saved during the evaluation of the
516 remaining args, whose values are discarded.
517 usage: (prog2 FORM1 FORM2 BODY...) */)
523 eval_sub (XCAR (args
));
525 return Fprog1 (XCDR (args
));
528 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
529 doc
: /* Set each SYM to the value of its VAL.
530 The symbols SYM are variables; they are literal (not evaluated).
531 The values VAL are expressions; they are evaluated.
532 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
533 The second VAL is not computed until after the first SYM is set, and so on;
534 each VAL can use the new value of variables set earlier in the `setq'.
535 The return value of the `setq' form is the value of the last VAL.
536 usage: (setq [SYM VAL]...) */)
539 Lisp_Object val
, sym
, lex_binding
;
544 Lisp_Object args_left
= args
;
550 val
= eval_sub (Fcar (XCDR (args_left
)));
551 sym
= XCAR (args_left
);
553 /* Like for eval_sub, we do not check declared_special here since
554 it's been done when let-binding. */
555 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
557 && !NILP (lex_binding
558 = Fassq (sym
, Vinternal_interpreter_environment
)))
559 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
561 Fset (sym
, val
); /* SYM is dynamically bound. */
563 args_left
= Fcdr (XCDR (args_left
));
565 while (CONSP (args_left
));
573 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
574 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
575 Warning: `quote' does not construct its return value, but just returns
576 the value that was pre-constructed by the Lisp reader (see info node
577 `(elisp)Printed Representation').
578 This means that '(a . b) is not identical to (cons 'a 'b): the former
579 does not cons. Quoting should be reserved for constants that will
580 never be modified by side-effects, unless you like self-modifying code.
581 See the common pitfall in info node `(elisp)Rearrangement' for an example
582 of unexpected results when a quoted object is modified.
583 usage: (quote ARG) */)
586 if (CONSP (XCDR (args
)))
587 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
591 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
592 doc
: /* Like `quote', but preferred for objects which are functions.
593 In byte compilation, `function' causes its argument to be compiled.
594 `quote' cannot do that.
595 usage: (function ARG) */)
598 Lisp_Object quoted
= XCAR (args
);
600 if (CONSP (XCDR (args
)))
601 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
603 if (!NILP (Vinternal_interpreter_environment
)
605 && EQ (XCAR (quoted
), Qlambda
))
606 /* This is a lambda expression within a lexical environment;
607 return an interpreted closure instead of a simple lambda. */
608 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
611 /* Simply quote the argument. */
616 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
617 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
618 Aliased variables always have the same value; setting one sets the other.
619 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
620 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
621 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
622 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
623 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
624 The return value is BASE-VARIABLE. */)
625 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
627 struct Lisp_Symbol
*sym
;
629 CHECK_SYMBOL (new_alias
);
630 CHECK_SYMBOL (base_variable
);
632 sym
= XSYMBOL (new_alias
);
635 /* Not sure why, but why not? */
636 error ("Cannot make a constant an alias");
638 switch (sym
->redirect
)
640 case SYMBOL_FORWARDED
:
641 error ("Cannot make an internal variable an alias");
642 case SYMBOL_LOCALIZED
:
643 error ("Don't know how to make a localized variable an alias");
646 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
647 If n_a is bound, but b_v is not, set the value of b_v to n_a,
648 so that old-code that affects n_a before the aliasing is setup
650 if (NILP (Fboundp (base_variable
)))
651 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
654 union specbinding
*p
;
656 for (p
= specpdl_ptr
; p
> specpdl
; )
657 if ((--p
)->kind
>= SPECPDL_LET
658 && (EQ (new_alias
, specpdl_symbol (p
))))
659 error ("Don't know how to make a let-bound variable an alias");
662 sym
->declared_special
= 1;
663 XSYMBOL (base_variable
)->declared_special
= 1;
664 sym
->redirect
= SYMBOL_VARALIAS
;
665 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
666 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
667 LOADHIST_ATTACH (new_alias
);
668 /* Even if docstring is nil: remove old docstring. */
669 Fput (new_alias
, Qvariable_documentation
, docstring
);
671 return base_variable
;
674 static union specbinding
*
675 default_toplevel_binding (Lisp_Object symbol
)
677 union specbinding
*binding
= NULL
;
678 union specbinding
*pdl
= specpdl_ptr
;
679 while (pdl
> specpdl
)
681 switch ((--pdl
)->kind
)
683 case SPECPDL_LET_DEFAULT
:
685 if (EQ (specpdl_symbol (pdl
), symbol
))
693 DEFUN ("default-toplevel-value", Fdefault_toplevel_value
, Sdefault_toplevel_value
, 1, 1, 0,
694 doc
: /* Return SYMBOL's toplevel default value.
695 "Toplevel" means outside of any let binding. */)
698 union specbinding
*binding
= default_toplevel_binding (symbol
);
700 = binding
? specpdl_old_value (binding
) : Fdefault_value (symbol
);
701 if (!EQ (value
, Qunbound
))
703 xsignal1 (Qvoid_variable
, symbol
);
706 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value
,
707 Sset_default_toplevel_value
, 2, 2, 0,
708 doc
: /* Set SYMBOL's toplevel default value to VALUE.
709 "Toplevel" means outside of any let binding. */)
710 (Lisp_Object symbol
, Lisp_Object value
)
712 union specbinding
*binding
= default_toplevel_binding (symbol
);
714 set_specpdl_old_value (binding
, value
);
716 Fset_default (symbol
, value
);
720 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
721 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
722 You are not required to define a variable in order to use it, but
723 defining it lets you supply an initial value and documentation, which
724 can be referred to by the Emacs help facilities and other programming
725 tools. The `defvar' form also declares the variable as \"special\",
726 so that it is always dynamically bound even if `lexical-binding' is t.
728 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
729 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
730 default value is what is set; buffer-local values are not affected.
731 If INITVALUE is missing, SYMBOL's value is not set.
733 If SYMBOL has a local binding, then this form affects the local
734 binding. This is usually not what you want. Thus, if you need to
735 load a file defining variables, with this form or with `defconst' or
736 `defcustom', you should always load that file _outside_ any bindings
737 for these variables. \(`defconst' and `defcustom' behave similarly in
740 The optional argument DOCSTRING is a documentation string for the
743 To define a user option, use `defcustom' instead of `defvar'.
744 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
747 Lisp_Object sym
, tem
, tail
;
754 if (CONSP (XCDR (tail
)) && CONSP (XCDR (XCDR (tail
))))
755 error ("Too many arguments");
757 tem
= Fdefault_boundp (sym
);
759 /* Do it before evaluating the initial value, for self-references. */
760 XSYMBOL (sym
)->declared_special
= 1;
763 Fset_default (sym
, eval_sub (XCAR (tail
)));
765 { /* Check if there is really a global binding rather than just a let
766 binding that shadows the global unboundness of the var. */
767 union specbinding
*binding
= default_toplevel_binding (sym
);
768 if (binding
&& EQ (specpdl_old_value (binding
), Qunbound
))
770 set_specpdl_old_value (binding
, eval_sub (XCAR (tail
)));
777 if (!NILP (Vpurify_flag
))
778 tem
= Fpurecopy (tem
);
779 Fput (sym
, Qvariable_documentation
, tem
);
781 LOADHIST_ATTACH (sym
);
783 else if (!NILP (Vinternal_interpreter_environment
)
784 && !XSYMBOL (sym
)->declared_special
)
785 /* A simple (defvar foo) with lexical scoping does "nothing" except
786 declare that var to be dynamically scoped *locally* (i.e. within
787 the current file or let-block). */
788 Vinternal_interpreter_environment
789 = Fcons (sym
, Vinternal_interpreter_environment
);
792 /* Simple (defvar <var>) should not count as a definition at all.
793 It could get in the way of other definitions, and unloading this
794 package could try to make the variable unbound. */
800 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
801 doc
: /* Define SYMBOL as a constant variable.
802 This declares that neither programs nor users should ever change the
803 value. This constancy is not actually enforced by Emacs Lisp, but
804 SYMBOL is marked as a special variable so that it is never lexically
807 The `defconst' form always sets the value of SYMBOL to the result of
808 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
809 what is set; buffer-local values are not affected. If SYMBOL has a
810 local binding, then this form sets the local binding's value.
811 However, you should normally not make local bindings for variables
812 defined with this form.
814 The optional DOCSTRING specifies the variable's documentation string.
815 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
818 Lisp_Object sym
, tem
;
821 if (CONSP (Fcdr (XCDR (XCDR (args
)))))
822 error ("Too many arguments");
824 tem
= eval_sub (Fcar (XCDR (args
)));
825 if (!NILP (Vpurify_flag
))
826 tem
= Fpurecopy (tem
);
827 Fset_default (sym
, tem
);
828 XSYMBOL (sym
)->declared_special
= 1;
829 tem
= Fcar (XCDR (XCDR (args
)));
832 if (!NILP (Vpurify_flag
))
833 tem
= Fpurecopy (tem
);
834 Fput (sym
, Qvariable_documentation
, tem
);
836 Fput (sym
, Qrisky_local_variable
, Qt
);
837 LOADHIST_ATTACH (sym
);
841 /* Make SYMBOL lexically scoped. */
842 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
843 Smake_var_non_special
, 1, 1, 0,
844 doc
: /* Internal function. */)
847 CHECK_SYMBOL (symbol
);
848 XSYMBOL (symbol
)->declared_special
= 0;
853 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
854 doc
: /* Bind variables according to VARLIST then eval BODY.
855 The value of the last form in BODY is returned.
856 Each element of VARLIST is a symbol (which is bound to nil)
857 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
858 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
859 usage: (let* VARLIST BODY...) */)
862 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
864 struct gcpro gcpro1
, gcpro2
, gcpro3
;
866 GCPRO3 (args
, elt
, varlist
);
868 lexenv
= Vinternal_interpreter_environment
;
870 varlist
= XCAR (args
);
871 while (CONSP (varlist
))
875 elt
= XCAR (varlist
);
881 else if (! NILP (Fcdr (Fcdr (elt
))))
882 signal_error ("`let' bindings can have only one value-form", elt
);
886 val
= eval_sub (Fcar (Fcdr (elt
)));
889 if (!NILP (lexenv
) && SYMBOLP (var
)
890 && !XSYMBOL (var
)->declared_special
891 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
892 /* Lexically bind VAR by adding it to the interpreter's binding
896 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
897 if (EQ (Vinternal_interpreter_environment
, lexenv
))
898 /* Save the old lexical environment on the specpdl stack,
899 but only for the first lexical binding, since we'll never
900 need to revert to one of the intermediate ones. */
901 specbind (Qinternal_interpreter_environment
, newenv
);
903 Vinternal_interpreter_environment
= newenv
;
908 varlist
= XCDR (varlist
);
911 val
= Fprogn (XCDR (args
));
916 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
917 doc
: /* Bind variables according to VARLIST then eval BODY.
918 The value of the last form in BODY is returned.
919 Each element of VARLIST is a symbol (which is bound to nil)
920 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
921 All the VALUEFORMs are evalled before any symbols are bound.
922 usage: (let VARLIST BODY...) */)
925 Lisp_Object
*temps
, tem
, lexenv
;
926 register Lisp_Object elt
, varlist
;
929 struct gcpro gcpro1
, gcpro2
;
932 varlist
= XCAR (args
);
934 /* Make space to hold the values to give the bound variables. */
935 elt
= Flength (varlist
);
936 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
938 /* Compute the values and store them in `temps'. */
940 GCPRO2 (args
, *temps
);
943 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
946 elt
= XCAR (varlist
);
948 temps
[argnum
++] = Qnil
;
949 else if (! NILP (Fcdr (Fcdr (elt
))))
950 signal_error ("`let' bindings can have only one value-form", elt
);
952 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
953 gcpro2
.nvars
= argnum
;
957 lexenv
= Vinternal_interpreter_environment
;
959 varlist
= XCAR (args
);
960 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
964 elt
= XCAR (varlist
);
965 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
966 tem
= temps
[argnum
++];
968 if (!NILP (lexenv
) && SYMBOLP (var
)
969 && !XSYMBOL (var
)->declared_special
970 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
971 /* Lexically bind VAR by adding it to the lexenv alist. */
972 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
974 /* Dynamically bind VAR. */
978 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
979 /* Instantiate a new lexical environment. */
980 specbind (Qinternal_interpreter_environment
, lexenv
);
982 elt
= Fprogn (XCDR (args
));
988 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
989 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
990 The order of execution is thus TEST, BODY, TEST, BODY and so on
991 until TEST returns nil.
992 usage: (while TEST BODY...) */)
995 Lisp_Object test
, body
;
996 struct gcpro gcpro1
, gcpro2
;
1002 while (!NILP (eval_sub (test
)))
1012 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1013 doc
: /* Return result of expanding macros at top level of FORM.
1014 If FORM is not a macro call, it is returned unchanged.
1015 Otherwise, the macro is expanded and the expansion is considered
1016 in place of FORM. When a non-macro-call results, it is returned.
1018 The second optional arg ENVIRONMENT specifies an environment of macro
1019 definitions to shadow the loaded ones for use in file byte-compilation. */)
1020 (Lisp_Object form
, Lisp_Object environment
)
1022 /* With cleanups from Hallvard Furuseth. */
1023 register Lisp_Object expander
, sym
, def
, tem
;
1027 /* Come back here each time we expand a macro call,
1028 in case it expands into another macro call. */
1031 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1032 def
= sym
= XCAR (form
);
1034 /* Trace symbols aliases to other symbols
1035 until we get a symbol that is not an alias. */
1036 while (SYMBOLP (def
))
1040 tem
= Fassq (sym
, environment
);
1043 def
= SYMBOL_FUNCTION (sym
);
1049 /* Right now TEM is the result from SYM in ENVIRONMENT,
1050 and if TEM is nil then DEF is SYM's function definition. */
1053 /* SYM is not mentioned in ENVIRONMENT.
1054 Look at its function definition. */
1055 struct gcpro gcpro1
;
1057 def
= Fautoload_do_load (def
, sym
, Qmacro
);
1060 /* Not defined or definition not suitable. */
1062 if (!EQ (XCAR (def
), Qmacro
))
1064 else expander
= XCDR (def
);
1068 expander
= XCDR (tem
);
1069 if (NILP (expander
))
1073 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1074 if (EQ (form
, newform
))
1083 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1084 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1085 TAG is evalled to get the tag to use; it must not be nil.
1087 Then the BODY is executed.
1088 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1089 If no throw happens, `catch' returns the value of the last BODY form.
1090 If a throw happens, it specifies the value to return from `catch'.
1091 usage: (catch TAG BODY...) */)
1094 register Lisp_Object tag
;
1095 struct gcpro gcpro1
;
1098 tag
= eval_sub (XCAR (args
));
1100 return internal_catch (tag
, Fprogn
, XCDR (args
));
1103 /* Assert that E is true, as a comment only. Use this instead of
1104 eassert (E) when E contains variables that might be clobbered by a
1107 #define clobbered_eassert(E) ((void) 0)
1110 set_handlerlist (void *data
)
1116 restore_handler (void *data
)
1118 struct handler
*c
= data
;
1119 set_poll_suppress_count (c
->poll_suppress_count
);
1120 unblock_input_to (c
->interrupt_input_blocked
);
1124 struct icc_thunk_env
1126 enum { ICC_0
, ICC_1
, ICC_2
, ICC_3
, ICC_N
} type
;
1129 Lisp_Object (*fun0
) (void);
1130 Lisp_Object (*fun1
) (Lisp_Object
);
1131 Lisp_Object (*fun2
) (Lisp_Object
, Lisp_Object
);
1132 Lisp_Object (*fun3
) (Lisp_Object
, Lisp_Object
, Lisp_Object
);
1133 Lisp_Object (*funn
) (ptrdiff_t, Lisp_Object
*);
1153 icc_thunk (void *data
)
1156 struct icc_thunk_env
*e
= data
;
1157 scm_dynwind_begin (0);
1158 scm_dynwind_unwind_handler (restore_handler
, e
->c
, 0);
1159 scm_dynwind_unwind_handler (set_handlerlist
,
1161 SCM_F_WIND_EXPLICITLY
);
1169 tem
= e
->fun1 (e
->arg1
);
1172 tem
= e
->fun2 (e
->arg1
, e
->arg2
);
1175 tem
= e
->fun3 (e
->arg1
, e
->arg2
, e
->arg3
);
1178 tem
= e
->funn (e
->nargs
, e
->args
);
1188 icc_handler (void *data
, Lisp_Object k
, Lisp_Object v
)
1190 Lisp_Object (*f
) (Lisp_Object
) = data
;
1194 struct icc_handler_n_env
1196 Lisp_Object (*fun
) (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
1202 icc_handler_n (void *data
, Lisp_Object k
, Lisp_Object v
)
1204 struct icc_handler_n_env
*e
= data
;
1205 return e
->fun (v
, e
->nargs
, e
->args
);
1209 icc_lisp_handler (void *data
, Lisp_Object k
, Lisp_Object val
)
1212 struct handler
*h
= data
;
1213 Lisp_Object var
= h
->var
;
1214 scm_dynwind_begin (0);
1217 if (!NILP (Vinternal_interpreter_environment
))
1218 specbind (Qinternal_interpreter_environment
,
1219 Fcons (Fcons (var
, val
),
1220 Vinternal_interpreter_environment
));
1222 specbind (var
, val
);
1224 tem
= Fprogn (h
->body
);
1229 /* Set up a catch, then call C function FUNC on argument ARG.
1230 FUNC should return a Lisp_Object.
1231 This is how catches are done from within C code. */
1234 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1236 struct handler
*c
= make_catch_handler (tag
);
1237 struct icc_thunk_env env
= { .type
= ICC_1
,
1241 return call_with_prompt (c
->ptag
,
1242 make_c_closure (icc_thunk
, &env
, 0, 0),
1243 make_c_closure (icc_handler
, Fidentity
, 2, 0));
1246 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1247 jump to that CATCH, returning VALUE as the value of that catch.
1249 This is the guts of Fthrow and Fsignal; they differ only in the way
1250 they choose the catch tag to throw to. A catch tag for a
1251 condition-case form has a TAG of Qnil.
1253 Before each catch is discarded, unbind all special bindings and
1254 execute all unwind-protect clauses made above that catch. Unwind
1255 the handler stack as we go, so that the proper handlers are in
1256 effect for each unwind-protect clause we run. At the end, restore
1257 some static info saved in CATCH, and longjmp to the location
1260 This is used for correct unwinding in Fthrow and Fsignal. */
1262 static Lisp_Object
unbind_to_1 (ptrdiff_t, Lisp_Object
, bool);
1264 static _Noreturn
void
1265 unwind_to_catch (struct handler
*catch, Lisp_Object value
)
1267 abort_to_prompt (catch->ptag
, scm_list_1 (value
));
1270 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1271 doc
: /* Throw to the catch for TAG and return VALUE from it.
1272 Both TAG and VALUE are evalled. */)
1273 (register Lisp_Object tag
, Lisp_Object value
)
1278 for (c
= handlerlist
; c
; c
= c
->next
)
1280 if (c
->type
== CATCHER
&& EQ (c
->tag_or_ch
, tag
))
1281 unwind_to_catch (c
, value
);
1283 xsignal2 (Qno_catch
, tag
, value
);
1287 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1288 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1289 If BODYFORM completes normally, its value is returned
1290 after executing the UNWINDFORMS.
1291 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1292 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1298 record_unwind_protect (unwind_body
, XCDR (args
));
1299 val
= eval_sub (XCAR (args
));
1304 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1305 doc
: /* Regain control when an error is signaled.
1306 Executes BODYFORM and returns its value if no error happens.
1307 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1308 where the BODY is made of Lisp expressions.
1310 A handler is applicable to an error
1311 if CONDITION-NAME is one of the error's condition names.
1312 If an error happens, the first applicable handler is run.
1314 The car of a handler may be a list of condition names instead of a
1315 single condition name; then it handles all of them. If the special
1316 condition name `debug' is present in this list, it allows another
1317 condition in the list to run the debugger if `debug-on-error' and the
1318 other usual mechanisms says it should (otherwise, `condition-case'
1319 suppresses the debugger).
1321 When a handler handles an error, control returns to the `condition-case'
1322 and it executes the handler's BODY...
1323 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1324 \(If VAR is nil, the handler can't access that information.)
1325 Then the value of the last BODY form is returned from the `condition-case'
1328 See also the function `signal' for more info.
1329 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1332 Lisp_Object var
= XCAR (args
);
1333 Lisp_Object bodyform
= XCAR (XCDR (args
));
1334 Lisp_Object handlers
= XCDR (XCDR (args
));
1336 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1340 ilcc1 (Lisp_Object var
, Lisp_Object bodyform
, Lisp_Object handlers
)
1342 if (CONSP (handlers
))
1344 Lisp_Object clause
= XCAR (handlers
);
1345 Lisp_Object condition
= XCAR (clause
);
1346 Lisp_Object body
= XCDR (clause
);
1347 if (!CONSP (condition
))
1348 condition
= Fcons (condition
, Qnil
);
1349 struct handler
*c
= make_condition_handler (condition
);
1352 struct icc_thunk_env env
= { .type
= ICC_3
,
1356 .arg3
= XCDR (handlers
),
1358 return call_with_prompt (c
->ptag
,
1359 make_c_closure (icc_thunk
, &env
, 0, 0),
1360 make_c_closure (icc_lisp_handler
, c
, 2, 0));
1364 return eval_sub (bodyform
);
1368 /* Like Fcondition_case, but the args are separate
1369 rather than passed in a list. Used by Fbyte_code. */
1372 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1373 Lisp_Object handlers
)
1377 struct handler
*oldhandlerlist
= handlerlist
;
1381 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1383 Lisp_Object tem
= XCAR (val
);
1386 && (SYMBOLP (XCAR (tem
))
1387 || CONSP (XCAR (tem
))))))
1388 error ("Invalid condition handler: %s",
1389 SDATA (Fprin1_to_string (tem
, Qt
)));
1392 return ilcc1 (var
, bodyform
, Freverse (handlers
));
1395 /* Call the function BFUN with no arguments, catching errors within it
1396 according to HANDLERS. If there is an error, call HFUN with
1397 one argument which is the data that describes the error:
1400 HANDLERS can be a list of conditions to catch.
1401 If HANDLERS is Qt, catch all errors.
1402 If HANDLERS is Qerror, catch all errors
1403 but allow the debugger to run if that is enabled. */
1406 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1407 Lisp_Object (*hfun
) (Lisp_Object
))
1410 struct handler
*c
= make_condition_handler (handlers
);
1412 struct icc_thunk_env env
= { .type
= ICC_0
, .fun0
= bfun
, .c
= c
};
1413 return call_with_prompt (c
->ptag
,
1414 make_c_closure (icc_thunk
, &env
, 0, 0),
1415 make_c_closure (icc_handler
, hfun
, 2, 0));
1418 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1421 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1422 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1425 struct handler
*c
= make_condition_handler (handlers
);
1427 struct icc_thunk_env env
= { .type
= ICC_1
,
1431 return call_with_prompt (c
->ptag
,
1432 make_c_closure (icc_thunk
, &env
, 0, 0),
1433 make_c_closure (icc_handler
, hfun
, 2, 0));
1436 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1440 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1443 Lisp_Object handlers
,
1444 Lisp_Object (*hfun
) (Lisp_Object
))
1447 struct handler
*c
= make_condition_handler (handlers
);
1448 struct icc_thunk_env env
= { .type
= ICC_2
,
1453 return call_with_prompt (c
->ptag
,
1454 make_c_closure (icc_thunk
, &env
, 0, 0),
1455 make_c_closure (icc_handler
, hfun
, 2, 0));
1458 /* Like internal_condition_case but call BFUN with NARGS as first,
1459 and ARGS as second argument. */
1462 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1465 Lisp_Object handlers
,
1466 Lisp_Object (*hfun
) (Lisp_Object err
,
1471 struct handler
*c
= make_condition_handler (handlers
);
1473 struct icc_thunk_env env
= { .type
= ICC_N
,
1478 struct icc_handler_n_env henv
= { .fun
= hfun
, .nargs
= nargs
, .args
= args
};
1479 return call_with_prompt (c
->ptag
,
1480 make_c_closure (icc_thunk
, &env
, 0, 0),
1481 make_c_closure (icc_handler_n
, &henv
, 2, 0));
1485 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1486 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1490 process_quit_flag (void)
1492 Lisp_Object flag
= Vquit_flag
;
1494 if (EQ (flag
, Qkill_emacs
))
1496 if (EQ (Vthrow_on_input
, flag
))
1497 Fthrow (Vthrow_on_input
, Qt
);
1498 Fsignal (Qquit
, Qnil
);
1501 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1502 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1503 This function does not return.
1505 An error symbol is a symbol with an `error-conditions' property
1506 that is a list of condition names.
1507 A handler for any of those names will get to handle this signal.
1508 The symbol `error' should normally be one of them.
1510 DATA should be a list. Its elements are printed as part of the error message.
1511 See Info anchor `(elisp)Definition of signal' for some details on how this
1512 error message is constructed.
1513 If the signal is handled, DATA is made available to the handler.
1514 See also the function `condition-case'. */)
1515 (Lisp_Object error_symbol
, Lisp_Object data
)
1517 /* When memory is full, ERROR-SYMBOL is nil,
1518 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1519 That is a special case--don't do this in other situations. */
1520 Lisp_Object conditions
;
1522 Lisp_Object real_error_symbol
1523 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1524 register Lisp_Object clause
= Qnil
;
1528 if (waiting_for_input
)
1531 #if 0 /* rms: I don't know why this was here,
1532 but it is surely wrong for an error that is handled. */
1533 #ifdef HAVE_WINDOW_SYSTEM
1534 if (display_hourglass_p
)
1535 cancel_hourglass ();
1539 /* This hook is used by edebug. */
1540 if (! NILP (Vsignal_hook_function
)
1541 && ! NILP (error_symbol
))
1543 /* Edebug takes care of restoring these variables when it exits. */
1544 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1545 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1547 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1548 max_specpdl_size
= SPECPDL_INDEX () + 40;
1550 call2 (Vsignal_hook_function
, error_symbol
, data
);
1553 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1555 /* Remember from where signal was called. Skip over the frame for
1556 `signal' itself. If a frame for `error' follows, skip that,
1557 too. Don't do this when ERROR_SYMBOL is nil, because that
1558 is a memory-full error. */
1559 Vsignaling_function
= Qnil
;
1560 if (!NILP (error_symbol
))
1562 union specbinding
*pdl
= backtrace_next (backtrace_top ());
1563 if (backtrace_p (pdl
) && EQ (backtrace_function (pdl
), Qerror
))
1564 pdl
= backtrace_next (pdl
);
1565 if (backtrace_p (pdl
))
1566 Vsignaling_function
= backtrace_function (pdl
);
1569 for (h
= handlerlist
; h
; h
= h
->next
)
1571 if (h
->type
!= CONDITION_CASE
)
1573 clause
= find_handler_clause (h
->tag_or_ch
, conditions
);
1578 if (/* Don't run the debugger for a memory-full error.
1579 (There is no room in memory to do that!) */
1580 !NILP (error_symbol
)
1581 && (!NILP (Vdebug_on_signal
)
1582 /* If no handler is present now, try to run the debugger. */
1584 /* A `debug' symbol in the handler list disables the normal
1585 suppression of the debugger. */
1586 || (CONSP (clause
) && CONSP (clause
)
1587 && !NILP (Fmemq (Qdebug
, clause
)))
1588 /* Special handler that means "print a message and run debugger
1590 || EQ (h
->tag_or_ch
, Qerror
)))
1592 bool debugger_called
1593 = maybe_call_debugger (conditions
, error_symbol
, data
);
1594 /* We can't return values to code which signaled an error, but we
1595 can continue code which has signaled a quit. */
1596 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1602 Lisp_Object unwind_data
1603 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1605 unwind_to_catch (h
, unwind_data
);
1609 if (handlerlist
!= handlerlist_sentinel
)
1610 /* FIXME: This will come right back here if there's no `top-level'
1611 catcher. A better solution would be to abort here, and instead
1612 add a catch-all condition handler so we never come here. */
1613 Fthrow (Qtop_level
, Qt
);
1616 if (! NILP (error_symbol
))
1617 data
= Fcons (error_symbol
, data
);
1619 string
= Ferror_message_string (data
);
1620 fatal ("%s", SDATA (string
));
1623 /* Internal version of Fsignal that never returns.
1624 Used for anything but Qquit (which can return from Fsignal). */
1627 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1629 Fsignal (error_symbol
, data
);
1633 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1636 xsignal0 (Lisp_Object error_symbol
)
1638 xsignal (error_symbol
, Qnil
);
1642 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1644 xsignal (error_symbol
, list1 (arg
));
1648 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1650 xsignal (error_symbol
, list2 (arg1
, arg2
));
1654 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1656 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1659 /* Signal `error' with message S, and additional arg ARG.
1660 If ARG is not a genuine list, make it a one-element list. */
1663 signal_error (const char *s
, Lisp_Object arg
)
1665 Lisp_Object tortoise
, hare
;
1667 hare
= tortoise
= arg
;
1668 while (CONSP (hare
))
1675 tortoise
= XCDR (tortoise
);
1677 if (EQ (hare
, tortoise
))
1684 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1688 /* Return true if LIST is a non-nil atom or
1689 a list containing one of CONDITIONS. */
1692 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1699 while (CONSP (conditions
))
1701 Lisp_Object
this, tail
;
1702 this = XCAR (conditions
);
1703 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1704 if (EQ (XCAR (tail
), this))
1706 conditions
= XCDR (conditions
);
1711 /* Return true if an error with condition-symbols CONDITIONS,
1712 and described by SIGNAL-DATA, should skip the debugger
1713 according to debugger-ignored-errors. */
1716 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1719 bool first_string
= 1;
1720 Lisp_Object error_message
;
1722 error_message
= Qnil
;
1723 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1725 if (STRINGP (XCAR (tail
)))
1729 error_message
= Ferror_message_string (data
);
1733 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1738 Lisp_Object contail
;
1740 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1741 if (EQ (XCAR (tail
), XCAR (contail
)))
1749 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1750 SIG and DATA describe the signal. There are two ways to pass them:
1751 = SIG is the error symbol, and DATA is the rest of the data.
1752 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1753 This is for memory-full errors only. */
1755 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1757 Lisp_Object combined_data
;
1759 combined_data
= Fcons (sig
, data
);
1762 /* Don't try to run the debugger with interrupts blocked.
1763 The editing loop would return anyway. */
1764 ! input_blocked_p ()
1765 && NILP (Vinhibit_debugger
)
1766 /* Does user want to enter debugger for this kind of error? */
1769 : wants_debugger (Vdebug_on_error
, conditions
))
1770 && ! skip_debugger (conditions
, combined_data
)
1771 /* RMS: What's this for? */
1772 && when_entered_debugger
< num_nonmacro_input_events
)
1774 call_debugger (list2 (Qerror
, combined_data
));
1782 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1784 register Lisp_Object h
;
1786 /* t is used by handlers for all conditions, set up by C code. */
1787 if (EQ (handlers
, Qt
))
1790 /* error is used similarly, but means print an error message
1791 and run the debugger if that is enabled. */
1792 if (EQ (handlers
, Qerror
))
1795 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1797 Lisp_Object handler
= XCAR (h
);
1798 if (!NILP (Fmemq (handler
, conditions
)))
1806 /* Dump an error message; called like vprintf. */
1808 verror (const char *m
, va_list ap
)
1811 ptrdiff_t size
= sizeof buf
;
1812 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1817 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1818 string
= make_string (buffer
, used
);
1822 xsignal1 (Qerror
, string
);
1826 /* Dump an error message; called like printf. */
1830 error (const char *m
, ...)
1837 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1838 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1839 This means it contains a description for how to read arguments to give it.
1840 The value is nil for an invalid function or a symbol with no function
1843 Interactively callable functions include strings and vectors (treated
1844 as keyboard macros), lambda-expressions that contain a top-level call
1845 to `interactive', autoload definitions made by `autoload' with non-nil
1846 fourth argument, and some of the built-in functions of Lisp.
1848 Also, a symbol satisfies `commandp' if its function definition does so.
1850 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1851 then strings and vectors are not accepted. */)
1852 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1854 register Lisp_Object fun
;
1855 register Lisp_Object funcar
;
1856 Lisp_Object if_prop
= Qnil
;
1860 fun
= indirect_function (fun
); /* Check cycles. */
1864 /* Check an `interactive-form' property if present, analogous to the
1865 function-documentation property. */
1867 while (SYMBOLP (fun
))
1869 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1872 fun
= Fsymbol_function (fun
);
1875 if (scm_is_true (scm_procedure_p (fun
)))
1876 return (scm_is_true (scm_procedure_property (fun
, Qinteractive_form
))
1878 /* Bytecode objects are interactive if they are long enough to
1879 have an element whose index is COMPILED_INTERACTIVE, which is
1880 where the interactive spec is stored. */
1881 else if (COMPILEDP (fun
))
1882 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1885 /* Strings and vectors are keyboard macros. */
1886 if (STRINGP (fun
) || VECTORP (fun
))
1887 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1889 /* Lists may represent commands. */
1892 funcar
= XCAR (fun
);
1893 if (EQ (funcar
, Qclosure
))
1894 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1896 else if (EQ (funcar
, Qlambda
))
1897 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1898 else if (EQ (funcar
, Qautoload
))
1899 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1904 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1905 doc
: /* Define FUNCTION to autoload from FILE.
1906 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1907 Third arg DOCSTRING is documentation for the function.
1908 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1909 Fifth arg TYPE indicates the type of the object:
1910 nil or omitted says FUNCTION is a function,
1911 `keymap' says FUNCTION is really a keymap, and
1912 `macro' or t says FUNCTION is really a macro.
1913 Third through fifth args give info about the real definition.
1914 They default to nil.
1915 If FUNCTION is already defined other than as an autoload,
1916 this does nothing and returns nil. */)
1917 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1919 CHECK_SYMBOL (function
);
1920 CHECK_STRING (file
);
1922 /* If function is defined and not as an autoload, don't override. */
1923 if (!NILP (SYMBOL_FUNCTION (function
))
1924 && !AUTOLOADP (SYMBOL_FUNCTION (function
)))
1927 return Fdefalias (function
,
1928 list5 (Qautoload
, file
, docstring
, interactive
, type
),
1933 un_autoload (Lisp_Object oldqueue
)
1935 Lisp_Object queue
, first
, second
;
1937 /* Queue to unwind is current value of Vautoload_queue.
1938 oldqueue is the shadowed value to leave in Vautoload_queue. */
1939 queue
= Vautoload_queue
;
1940 Vautoload_queue
= oldqueue
;
1941 while (CONSP (queue
))
1943 first
= XCAR (queue
);
1944 second
= Fcdr (first
);
1945 first
= Fcar (first
);
1946 if (EQ (first
, make_number (0)))
1949 Ffset (first
, second
);
1950 queue
= XCDR (queue
);
1954 /* Load an autoloaded function.
1955 FUNNAME is the symbol which is the function's name.
1956 FUNDEF is the autoload definition (a list). */
1958 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1959 doc
: /* Load FUNDEF which should be an autoload.
1960 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1961 in which case the function returns the new autoloaded function value.
1962 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1963 it is defines a macro. */)
1964 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1967 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1969 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
))) {
1974 if (EQ (macro_only
, Qmacro
))
1976 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1977 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
))) {
1983 /* This is to make sure that loadup.el gives a clear picture
1984 of what files are preloaded and when. */
1985 if (! NILP (Vpurify_flag
))
1986 error ("Attempt to autoload %s while preparing to dump",
1987 SDATA (SYMBOL_NAME (funname
)));
1989 CHECK_SYMBOL (funname
);
1990 GCPRO3 (funname
, fundef
, macro_only
);
1992 /* Preserve the match data. */
1993 record_unwind_save_match_data ();
1995 /* If autoloading gets an error (which includes the error of failing
1996 to define the function being called), we use Vautoload_queue
1997 to undo function definitions and `provide' calls made by
1998 the function. We do this in the specific case of autoloading
1999 because autoloading is not an explicit request "load this file",
2000 but rather a request to "call this function".
2002 The value saved here is to be restored into Vautoload_queue. */
2003 record_unwind_protect (un_autoload
, Vautoload_queue
);
2004 Vautoload_queue
= Qt
;
2005 /* If `macro_only', assume this autoload to be a "best-effort",
2006 so don't signal an error if autoloading fails. */
2007 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
2009 /* Once loading finishes, don't undo it. */
2010 Vautoload_queue
= Qt
;
2019 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2021 if (!NILP (Fequal (fun
, fundef
)))
2022 error ("Autoloading failed to define function %s",
2023 SDATA (SYMBOL_NAME (funname
)));
2030 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2031 doc
: /* Evaluate FORM and return its value.
2032 If LEXICAL is t, evaluate using lexical scoping.
2033 LEXICAL can also be an actual lexical environment, in the form of an
2034 alist mapping symbols to their value. */)
2035 (Lisp_Object form
, Lisp_Object lexical
)
2038 specbind (Qinternal_interpreter_environment
,
2039 CONSP (lexical
) || NILP (lexical
) ? lexical
: list1 (Qt
));
2040 Lisp_Object tem0
= eval_sub (form
);
2045 /* Grow the specpdl stack by one entry.
2046 The caller should have already initialized the entry.
2047 Signal an error on stack overflow.
2049 Make sure that there is always one unused entry past the top of the
2050 stack, so that the just-initialized entry is safely unwound if
2051 memory exhausted and an error is signaled here. Also, allocate a
2052 never-used entry just before the bottom of the stack; sometimes its
2053 address is taken. */
2060 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2062 ptrdiff_t count
= SPECPDL_INDEX ();
2063 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
- 1000);
2064 union specbinding
*pdlvec
= specpdl
- 1;
2065 ptrdiff_t pdlvecsize
= specpdl_size
+ 1;
2066 if (max_size
<= specpdl_size
)
2068 if (max_specpdl_size
< 400)
2069 max_size
= max_specpdl_size
= 400;
2070 if (max_size
<= specpdl_size
)
2071 signal_error ("Variable binding depth exceeds max-specpdl-size",
2074 pdlvec
= xpalloc (pdlvec
, &pdlvecsize
, 1, max_size
+ 1, sizeof *specpdl
);
2075 specpdl
= pdlvec
+ 1;
2076 specpdl_size
= pdlvecsize
- 1;
2077 specpdl_ptr
= specpdl
+ count
;
2082 record_in_backtrace (Lisp_Object function
, Lisp_Object
*args
, ptrdiff_t nargs
)
2084 eassert (nargs
>= UNEVALLED
);
2085 specpdl_ptr
->bt
.kind
= SPECPDL_BACKTRACE
;
2086 specpdl_ptr
->bt
.debug_on_exit
= false;
2087 specpdl_ptr
->bt
.function
= function
;
2088 specpdl_ptr
->bt
.args
= args
;
2089 specpdl_ptr
->bt
.nargs
= nargs
;
2091 scm_dynwind_unwind_handler (unbind_once
, NULL
, SCM_F_WIND_EXPLICITLY
);
2095 set_lisp_eval_depth (void *data
)
2097 EMACS_INT n
= (EMACS_INT
) data
;
2098 lisp_eval_depth
= n
;
2101 /* Eval a sub-expression of the current expression (i.e. in the same
2104 eval_sub_1 (Lisp_Object form
)
2106 Lisp_Object fun
, val
, original_fun
, original_args
;
2108 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2112 /* Look up its binding in the lexical environment.
2113 We do not pay attention to the declared_special flag here, since we
2114 already did that when let-binding the variable. */
2115 Lisp_Object lex_binding
2116 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2117 ? Fassq (form
, Vinternal_interpreter_environment
)
2119 if (CONSP (lex_binding
))
2120 return XCDR (lex_binding
);
2122 return Fsymbol_value (form
);
2134 scm_dynwind_begin (0);
2135 scm_dynwind_unwind_handler (set_lisp_eval_depth
,
2136 (void *) lisp_eval_depth
,
2137 SCM_F_WIND_EXPLICITLY
);
2139 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2141 if (max_lisp_eval_depth
< 100)
2142 max_lisp_eval_depth
= 100;
2143 if (lisp_eval_depth
> max_lisp_eval_depth
)
2144 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2147 original_fun
= XCAR (form
);
2148 original_args
= XCDR (form
);
2150 /* This also protects them from gc. */
2151 record_in_backtrace (original_fun
, &original_args
, UNEVALLED
);
2153 if (debug_on_next_call
)
2154 do_debug_on_call (Qt
);
2156 /* At this point, only original_fun and original_args
2157 have values that will be used below. */
2160 /* Optimize for no indirection. */
2163 fun
= Ffunction (Fcons (fun
, Qnil
));
2164 else if (!NILP (fun
) && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2165 fun
= indirect_function (fun
);
2167 if (scm_is_true (scm_procedure_p (fun
)))
2169 Lisp_Object args_left
= original_args
;
2170 Lisp_Object nargs
= Flength (args_left
);
2174 SAFE_ALLOCA_LISP (args
, XINT (nargs
));
2176 while (! NILP (args_left
))
2178 args
[argnum
++] = eval_sub (Fcar (args_left
));
2179 args_left
= Fcdr (args_left
);
2181 set_backtrace_args (specpdl_ptr
- 1, args
);
2182 set_backtrace_nargs (specpdl_ptr
- 1, argnum
);
2183 val
= scm_call_n (fun
, args
, argnum
);
2185 else if (CONSP (fun
) && EQ (XCAR (fun
), Qspecial_operator
))
2187 val
= scm_apply_0 (XCDR (fun
), original_args
);
2189 else if (COMPILEDP (fun
))
2190 val
= apply_lambda (fun
, original_args
);
2194 xsignal1 (Qvoid_function
, original_fun
);
2196 xsignal1 (Qinvalid_function
, original_fun
);
2197 funcar
= XCAR (fun
);
2198 if (!SYMBOLP (funcar
))
2199 xsignal1 (Qinvalid_function
, original_fun
);
2200 if (EQ (funcar
, Qautoload
))
2202 Fautoload_do_load (fun
, original_fun
, Qnil
);
2205 if (EQ (funcar
, Qmacro
))
2209 /* Bind lexical-binding during expansion of the macro, so the
2210 macro can know reliably if the code it outputs will be
2211 interpreted using lexical-binding or not. */
2212 specbind (Qlexical_binding
,
2213 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2214 exp
= apply1 (Fcdr (fun
), original_args
);
2216 val
= eval_sub (exp
);
2218 else if (EQ (funcar
, Qlambda
)
2219 || EQ (funcar
, Qclosure
))
2220 val
= apply_lambda (fun
, original_args
);
2222 xsignal1 (Qinvalid_function
, original_fun
);
2225 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2226 val
= call_debugger (list2 (Qexit
, val
));
2233 eval_sub (Lisp_Object form
)
2235 return scm_c_value_ref (eval_sub_1 (form
), 0);
2239 values_to_list (Lisp_Object values
)
2241 Lisp_Object list
= Qnil
;
2242 for (int i
= scm_c_nvalues (values
) - 1; i
>= 0; i
--)
2243 list
= Fcons (scm_c_value_ref (values
, i
), list
);
2247 DEFUN ("multiple-value-call", Fmultiple_value_call
, Smultiple_value_call
,
2249 doc
: /* Call with multiple values.
2250 usage: (multiple-value-call FUNCTION-FORM FORM) */)
2253 Lisp_Object function_form
= eval_sub (XCAR (args
));
2254 Lisp_Object values
= Qnil
;
2255 while (CONSP (args
= XCDR (args
)))
2256 values
= nconc2 (Fnreverse (values_to_list (eval_sub_1 (XCAR (args
)))),
2258 return apply1 (function_form
, Fnreverse (values
));
2261 DEFUN ("values", Fvalues
, Svalues
, 0, MANY
, 0,
2262 doc
: /* Return multiple values. */)
2263 (ptrdiff_t nargs
, Lisp_Object
*args
)
2265 return scm_c_values (args
, nargs
);
2268 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2269 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2270 Then return the value FUNCTION returns.
2271 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2272 usage: (apply FUNCTION &rest ARGUMENTS) */)
2273 (ptrdiff_t nargs
, Lisp_Object
*args
)
2277 register Lisp_Object spread_arg
;
2278 register Lisp_Object
*funcall_args
;
2279 Lisp_Object fun
, retval
;
2280 struct gcpro gcpro1
;
2285 spread_arg
= args
[nargs
- 1];
2286 CHECK_LIST (spread_arg
);
2288 numargs
= XINT (Flength (spread_arg
));
2291 return Ffuncall (nargs
- 1, args
);
2292 else if (numargs
== 1)
2294 args
[nargs
- 1] = XCAR (spread_arg
);
2295 return Ffuncall (nargs
, args
);
2298 numargs
+= nargs
- 2;
2300 /* Optimize for no indirection. */
2301 if (SYMBOLP (fun
) && !NILP (fun
)
2302 && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2303 fun
= indirect_function (fun
);
2306 /* Let funcall get the error. */
2310 /* We add 1 to numargs because funcall_args includes the
2311 function itself as well as its arguments. */
2314 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2315 GCPRO1 (*funcall_args
);
2316 gcpro1
.nvars
= 1 + numargs
;
2319 memcpy (funcall_args
, args
, nargs
* word_size
);
2320 /* Spread the last arg we got. Its first element goes in
2321 the slot that it used to occupy, hence this value of I. */
2323 while (!NILP (spread_arg
))
2325 funcall_args
[i
++] = XCAR (spread_arg
);
2326 spread_arg
= XCDR (spread_arg
);
2329 /* By convention, the caller needs to gcpro Ffuncall's args. */
2330 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2337 /* Run hook variables in various ways. */
2340 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2342 Ffuncall (nargs
, args
);
2346 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2347 doc
: /* Run each hook in HOOKS.
2348 Each argument should be a symbol, a hook variable.
2349 These symbols are processed in the order specified.
2350 If a hook symbol has a non-nil value, that value may be a function
2351 or a list of functions to be called to run the hook.
2352 If the value is a function, it is called with no arguments.
2353 If it is a list, the elements are called, in order, with no arguments.
2355 Major modes should not use this function directly to run their mode
2356 hook; they should use `run-mode-hooks' instead.
2358 Do not use `make-local-variable' to make a hook variable buffer-local.
2359 Instead, use `add-hook' and specify t for the LOCAL argument.
2360 usage: (run-hooks &rest HOOKS) */)
2361 (ptrdiff_t nargs
, Lisp_Object
*args
)
2363 Lisp_Object hook
[1];
2366 for (i
= 0; i
< nargs
; i
++)
2369 run_hook_with_args (1, hook
, funcall_nil
);
2375 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2376 Srun_hook_with_args
, 1, MANY
, 0,
2377 doc
: /* Run HOOK with the specified arguments ARGS.
2378 HOOK should be a symbol, a hook variable. The value of HOOK
2379 may be nil, a function, or a list of functions. Call each
2380 function in order with arguments ARGS. The final return value
2383 Do not use `make-local-variable' to make a hook variable buffer-local.
2384 Instead, use `add-hook' and specify t for the LOCAL argument.
2385 usage: (run-hook-with-args HOOK &rest ARGS) */)
2386 (ptrdiff_t nargs
, Lisp_Object
*args
)
2388 return run_hook_with_args (nargs
, args
, funcall_nil
);
2391 /* NB this one still documents a specific non-nil return value.
2392 (As did run-hook-with-args and run-hook-with-args-until-failure
2393 until they were changed in 24.1.) */
2394 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2395 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2396 doc
: /* Run HOOK with the specified arguments ARGS.
2397 HOOK should be a symbol, a hook variable. The value of HOOK
2398 may be nil, a function, or a list of functions. Call each
2399 function in order with arguments ARGS, stopping at the first
2400 one that returns non-nil, and return that value. Otherwise (if
2401 all functions return nil, or if there are no functions to call),
2404 Do not use `make-local-variable' to make a hook variable buffer-local.
2405 Instead, use `add-hook' and specify t for the LOCAL argument.
2406 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2407 (ptrdiff_t nargs
, Lisp_Object
*args
)
2409 return run_hook_with_args (nargs
, args
, Ffuncall
);
2413 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2415 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2418 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2419 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2420 doc
: /* Run HOOK with the specified arguments ARGS.
2421 HOOK should be a symbol, a hook variable. The value of HOOK
2422 may be nil, a function, or a list of functions. Call each
2423 function in order with arguments ARGS, stopping at the first
2424 one that returns nil, and return nil. Otherwise (if all functions
2425 return non-nil, or if there are no functions to call), return non-nil
2426 \(do not rely on the precise return value in this case).
2428 Do not use `make-local-variable' to make a hook variable buffer-local.
2429 Instead, use `add-hook' and specify t for the LOCAL argument.
2430 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2431 (ptrdiff_t nargs
, Lisp_Object
*args
)
2433 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2437 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2439 Lisp_Object tmp
= args
[0], ret
;
2442 ret
= Ffuncall (nargs
, args
);
2448 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2449 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2450 I.e. instead of calling each function FUN directly with arguments ARGS,
2451 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2452 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2453 aborts and returns that value.
2454 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2455 (ptrdiff_t nargs
, Lisp_Object
*args
)
2457 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2460 /* ARGS[0] should be a hook symbol.
2461 Call each of the functions in the hook value, passing each of them
2462 as arguments all the rest of ARGS (all NARGS - 1 elements).
2463 FUNCALL specifies how to call each function on the hook.
2464 The caller (or its caller, etc) must gcpro all of ARGS,
2465 except that it isn't necessary to gcpro ARGS[0]. */
2468 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2469 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2471 Lisp_Object sym
, val
, ret
= Qnil
;
2472 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2474 /* If we are dying or still initializing,
2475 don't do anything--it would probably crash if we tried. */
2476 if (NILP (Vrun_hooks
))
2480 val
= find_symbol_value (sym
);
2482 if (EQ (val
, Qunbound
) || NILP (val
))
2484 else if (!CONSP (val
) || FUNCTIONP (val
))
2487 return funcall (nargs
, args
);
2491 Lisp_Object global_vals
= Qnil
;
2492 GCPRO3 (sym
, val
, global_vals
);
2495 CONSP (val
) && NILP (ret
);
2498 if (EQ (XCAR (val
), Qt
))
2500 /* t indicates this hook has a local binding;
2501 it means to run the global binding too. */
2502 global_vals
= Fdefault_value (sym
);
2503 if (NILP (global_vals
)) continue;
2505 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2507 args
[0] = global_vals
;
2508 ret
= funcall (nargs
, args
);
2513 CONSP (global_vals
) && NILP (ret
);
2514 global_vals
= XCDR (global_vals
))
2516 args
[0] = XCAR (global_vals
);
2517 /* In a global value, t should not occur. If it does, we
2518 must ignore it to avoid an endless loop. */
2519 if (!EQ (args
[0], Qt
))
2520 ret
= funcall (nargs
, args
);
2526 args
[0] = XCAR (val
);
2527 ret
= funcall (nargs
, args
);
2536 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2539 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2541 Lisp_Object temp
[3];
2546 Frun_hook_with_args (3, temp
);
2549 /* Apply fn to arg. */
2551 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2553 struct gcpro gcpro1
;
2557 return Ffuncall (1, &fn
);
2560 Lisp_Object args
[2];
2564 return Fapply (2, args
);
2568 /* Call function fn on no arguments. */
2570 call0 (Lisp_Object fn
)
2572 struct gcpro gcpro1
;
2575 return Ffuncall (1, &fn
);
2578 /* Call function fn with 1 argument arg1. */
2581 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2583 struct gcpro gcpro1
;
2584 Lisp_Object args
[2];
2590 return Ffuncall (2, args
);
2593 /* Call function fn with 2 arguments arg1, arg2. */
2596 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2598 struct gcpro gcpro1
;
2599 Lisp_Object args
[3];
2605 return Ffuncall (3, args
);
2608 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2611 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2613 struct gcpro gcpro1
;
2614 Lisp_Object args
[4];
2621 return Ffuncall (4, args
);
2624 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2627 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2630 struct gcpro gcpro1
;
2631 Lisp_Object args
[5];
2639 return Ffuncall (5, args
);
2642 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2645 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2646 Lisp_Object arg4
, Lisp_Object arg5
)
2648 struct gcpro gcpro1
;
2649 Lisp_Object args
[6];
2658 return Ffuncall (6, args
);
2661 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2664 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2665 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2667 struct gcpro gcpro1
;
2668 Lisp_Object args
[7];
2678 return Ffuncall (7, args
);
2681 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2684 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2685 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2687 struct gcpro gcpro1
;
2688 Lisp_Object args
[8];
2699 return Ffuncall (8, args
);
2702 /* The caller should GCPRO all the elements of ARGS. */
2704 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2705 doc
: /* Non-nil if OBJECT is a function. */)
2706 (Lisp_Object object
)
2708 if (FUNCTIONP (object
))
2713 DEFUN ("funcall", Ffuncall1
, Sfuncall
, 1, MANY
, 0,
2714 doc
: /* Call first argument as a function, passing remaining arguments to it.
2715 Return the value that function returns.
2716 Thus, (funcall 'cons 'x 'y) returns (x . y).
2717 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2718 (ptrdiff_t nargs
, Lisp_Object
*args
)
2720 Lisp_Object fun
, original_fun
;
2722 ptrdiff_t numargs
= nargs
- 1;
2723 Lisp_Object lisp_numargs
;
2725 register Lisp_Object
*internal_args
;
2730 scm_dynwind_begin (0);
2731 scm_dynwind_unwind_handler (set_lisp_eval_depth
,
2732 (void *) lisp_eval_depth
,
2733 SCM_F_WIND_EXPLICITLY
);
2735 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2737 if (max_lisp_eval_depth
< 100)
2738 max_lisp_eval_depth
= 100;
2739 if (lisp_eval_depth
> max_lisp_eval_depth
)
2740 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2743 /* This also GCPROs them. */
2744 record_in_backtrace (args
[0], &args
[1], nargs
- 1);
2746 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2749 if (debug_on_next_call
)
2750 do_debug_on_call (Qlambda
);
2752 original_fun
= args
[0];
2756 /* Optimize for no indirection. */
2758 if (SYMBOLP (fun
) && !NILP (fun
)
2759 && (fun
= SYMBOL_FUNCTION (fun
), SYMBOLP (fun
)))
2760 fun
= indirect_function (fun
);
2762 if (scm_is_true (scm_procedure_p (fun
)))
2764 val
= scm_call_n (fun
, args
+ 1, numargs
);
2766 else if (COMPILEDP (fun
))
2767 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2771 xsignal1 (Qvoid_function
, original_fun
);
2773 xsignal1 (Qinvalid_function
, original_fun
);
2774 funcar
= XCAR (fun
);
2775 if (!SYMBOLP (funcar
))
2776 xsignal1 (Qinvalid_function
, original_fun
);
2777 if (EQ (funcar
, Qlambda
)
2778 || EQ (funcar
, Qclosure
))
2779 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2780 else if (EQ (funcar
, Qautoload
))
2782 Fautoload_do_load (fun
, original_fun
, Qnil
);
2786 xsignal1 (Qinvalid_function
, original_fun
);
2788 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2789 val
= call_debugger (list2 (Qexit
, val
));
2795 Ffuncall (ptrdiff_t nargs
, Lisp_Object
*args
)
2797 return scm_c_value_ref (Ffuncall1 (nargs
, args
), 0);
2801 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2803 Lisp_Object args_left
;
2806 register Lisp_Object
*arg_vector
;
2807 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2808 register Lisp_Object tem
;
2811 numargs
= XFASTINT (Flength (args
));
2812 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2815 GCPRO3 (*arg_vector
, args_left
, fun
);
2818 for (i
= 0; i
< numargs
; )
2820 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2821 tem
= eval_sub (tem
);
2822 arg_vector
[i
++] = tem
;
2828 set_backtrace_args (specpdl_ptr
- 1, arg_vector
);
2829 set_backtrace_nargs (specpdl_ptr
- 1, i
);
2830 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2832 /* Do the debug-on-exit now, while arg_vector still exists. */
2833 if (backtrace_debug_on_exit (specpdl_ptr
- 1))
2835 /* Don't do it again when we return to eval. */
2836 set_backtrace_debug_on_exit (specpdl_ptr
- 1, false);
2837 tem
= call_debugger (list2 (Qexit
, tem
));
2843 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2844 and return the result of evaluation.
2845 FUN must be either a lambda-expression or a compiled-code object. */
2848 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2849 register Lisp_Object
*arg_vector
)
2851 Lisp_Object val
, syms_left
, next
, lexenv
;
2854 bool optional
, rest
;
2858 if (EQ (XCAR (fun
), Qclosure
))
2860 fun
= XCDR (fun
); /* Drop `closure'. */
2861 lexenv
= XCAR (fun
);
2862 CHECK_LIST_CONS (fun
, fun
);
2866 syms_left
= XCDR (fun
);
2867 if (CONSP (syms_left
))
2868 syms_left
= XCAR (syms_left
);
2870 xsignal1 (Qinvalid_function
, fun
);
2872 else if (COMPILEDP (fun
))
2874 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2875 if (INTEGERP (syms_left
))
2876 /* A byte-code object with a non-nil `push args' slot means we
2877 shouldn't bind any arguments, instead just call the byte-code
2878 interpreter directly; it will push arguments as necessary.
2880 Byte-code objects with either a non-existent, or a nil value for
2881 the `push args' slot (the default), have dynamically-bound
2882 arguments, and use the argument-binding code below instead (as do
2883 all interpreted functions, even lexically bound ones). */
2885 /* If we have not actually read the bytecode string
2886 and constants vector yet, fetch them from the file. */
2887 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2888 Ffetch_bytecode (fun
);
2890 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2891 AREF (fun
, COMPILED_CONSTANTS
),
2892 AREF (fun
, COMPILED_STACK_DEPTH
),
2901 i
= optional
= rest
= 0;
2902 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2906 next
= XCAR (syms_left
);
2907 if (!SYMBOLP (next
))
2908 xsignal1 (Qinvalid_function
, fun
);
2910 if (EQ (next
, Qand_rest
))
2912 else if (EQ (next
, Qand_optional
))
2919 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2923 arg
= arg_vector
[i
++];
2925 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2929 /* Bind the argument. */
2930 if (!NILP (lexenv
) && SYMBOLP (next
))
2931 /* Lexically bind NEXT by adding it to the lexenv alist. */
2932 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
2934 /* Dynamically bind NEXT. */
2935 specbind (next
, arg
);
2939 if (!NILP (syms_left
))
2940 xsignal1 (Qinvalid_function
, fun
);
2942 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2944 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
2945 /* Instantiate a new lexical environment. */
2946 specbind (Qinternal_interpreter_environment
, lexenv
);
2949 val
= Fprogn (XCDR (XCDR (fun
)));
2952 /* If we have not actually read the bytecode string
2953 and constants vector yet, fetch them from the file. */
2954 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2955 Ffetch_bytecode (fun
);
2956 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2957 AREF (fun
, COMPILED_CONSTANTS
),
2958 AREF (fun
, COMPILED_STACK_DEPTH
),
2966 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2968 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2969 (Lisp_Object object
)
2973 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
2975 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
2978 tem
= AREF (object
, COMPILED_BYTECODE
);
2979 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
2980 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
2982 error ("Invalid byte code");
2984 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
2985 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
2990 /* Return true if SYMBOL currently has a let-binding
2991 which was made in the buffer that is now current. */
2994 let_shadows_buffer_binding_p (struct Lisp_Symbol
*symbol
)
2996 union specbinding
*p
;
2997 Lisp_Object buf
= Fcurrent_buffer ();
2999 for (p
= specpdl_ptr
; p
> specpdl
; )
3000 if ((--p
)->kind
> SPECPDL_LET
)
3002 struct Lisp_Symbol
*let_bound_symbol
= XSYMBOL (specpdl_symbol (p
));
3003 eassert (let_bound_symbol
->redirect
!= SYMBOL_VARALIAS
);
3004 if (symbol
== let_bound_symbol
3005 && EQ (specpdl_where (p
), buf
))
3013 let_shadows_global_binding_p (Lisp_Object symbol
)
3015 union specbinding
*p
;
3017 for (p
= specpdl_ptr
; p
> specpdl
; )
3018 if ((--p
)->kind
>= SPECPDL_LET
&& EQ (specpdl_symbol (p
), symbol
))
3024 /* `specpdl_ptr' describes which variable is
3025 let-bound, so it can be properly undone when we unbind_to.
3026 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3027 - SYMBOL is the variable being bound. Note that it should not be
3028 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3030 - WHERE tells us in which buffer the binding took place.
3031 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3032 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3033 i.e. bindings to the default value of a variable which can be
3037 specbind (Lisp_Object symbol
, Lisp_Object value
)
3039 struct Lisp_Symbol
*sym
;
3041 CHECK_SYMBOL (symbol
);
3042 sym
= XSYMBOL (symbol
);
3045 switch (sym
->redirect
)
3047 case SYMBOL_VARALIAS
:
3048 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3049 case SYMBOL_PLAINVAL
:
3050 /* The most common case is that of a non-constant symbol with a
3051 trivial value. Make that as fast as we can. */
3052 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3053 specpdl_ptr
->let
.symbol
= symbol
;
3054 specpdl_ptr
->let
.old_value
= SYMBOL_VAL (sym
);
3057 SET_SYMBOL_VAL (sym
, value
);
3059 set_internal (symbol
, value
, Qnil
, 1);
3061 case SYMBOL_LOCALIZED
:
3062 if (SYMBOL_BLV (sym
)->frame_local
)
3063 error ("Frame-local vars cannot be let-bound");
3064 case SYMBOL_FORWARDED
:
3066 Lisp_Object ovalue
= find_symbol_value (symbol
);
3067 specpdl_ptr
->let
.kind
= SPECPDL_LET_LOCAL
;
3068 specpdl_ptr
->let
.symbol
= symbol
;
3069 specpdl_ptr
->let
.old_value
= ovalue
;
3070 specpdl_ptr
->let
.where
= Fcurrent_buffer ();
3072 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3073 || (EQ (SYMBOL_BLV (sym
)->where
, Fcurrent_buffer ())));
3075 if (sym
->redirect
== SYMBOL_LOCALIZED
)
3077 if (!blv_found (SYMBOL_BLV (sym
)))
3078 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3080 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3082 /* If SYMBOL is a per-buffer variable which doesn't have a
3083 buffer-local value here, make the `let' change the global
3084 value by changing the value of SYMBOL in all buffers not
3085 having their own value. This is consistent with what
3086 happens with other buffer-local variables. */
3087 if (NILP (Flocal_variable_p (symbol
, Qnil
)))
3089 specpdl_ptr
->let
.kind
= SPECPDL_LET_DEFAULT
;
3091 Fset_default (symbol
, value
);
3096 specpdl_ptr
->let
.kind
= SPECPDL_LET
;
3099 set_internal (symbol
, value
, Qnil
, 1);
3102 default: emacs_abort ();
3106 scm_dynwind_unwind_handler (unbind_once
, NULL
, SCM_F_WIND_EXPLICITLY
);
3109 /* Push unwind-protect entries of various types. */
3112 record_unwind_protect_1 (void (*function
) (Lisp_Object
), Lisp_Object arg
,
3113 bool wind_explicitly
)
3115 record_unwind_protect_ptr_1 (function
, arg
, wind_explicitly
);
3119 record_unwind_protect (void (*function
) (Lisp_Object
), Lisp_Object arg
)
3121 record_unwind_protect_1 (function
, arg
, true);
3125 record_unwind_protect_ptr_1 (void (*function
) (void *), void *arg
,
3126 bool wind_explicitly
)
3128 scm_dynwind_unwind_handler (function
,
3131 ? SCM_F_WIND_EXPLICITLY
3136 record_unwind_protect_ptr (void (*function
) (void *), void *arg
)
3138 record_unwind_protect_ptr_1 (function
, arg
, true);
3142 record_unwind_protect_int_1 (void (*function
) (int), int arg
,
3143 bool wind_explicitly
)
3145 record_unwind_protect_ptr_1 (function
, arg
, wind_explicitly
);
3149 record_unwind_protect_int (void (*function
) (int), int arg
)
3151 record_unwind_protect_int_1 (function
, arg
, true);
3155 call_void (void *data
)
3157 ((void (*) (void)) data
) ();
3161 record_unwind_protect_void_1 (void (*function
) (void),
3162 bool wind_explicitly
)
3164 record_unwind_protect_ptr_1 (call_void
, function
, wind_explicitly
);
3168 record_unwind_protect_void (void (*function
) (void))
3170 record_unwind_protect_void_1 (function
, true);
3174 unbind_once (void *ignore
)
3176 /* Decrement specpdl_ptr before we do the work to unbind it, so
3177 that an error in unbinding won't try to unbind the same entry
3178 again. Take care to copy any parts of the binding needed
3179 before invoking any code that can make more bindings. */
3183 switch (specpdl_ptr
->kind
)
3185 case SPECPDL_BACKTRACE
:
3188 { /* If variable has a trivial value (no forwarding), we can
3189 just set it. No need to check for constant symbols here,
3190 since that was already done by specbind. */
3191 struct Lisp_Symbol
*sym
= XSYMBOL (specpdl_symbol (specpdl_ptr
));
3192 if (sym
->redirect
== SYMBOL_PLAINVAL
)
3194 SET_SYMBOL_VAL (sym
, specpdl_old_value (specpdl_ptr
));
3199 NOTE: we only ever come here if make_local_foo was used for
3200 the first time on this var within this let. */
3203 case SPECPDL_LET_DEFAULT
:
3204 Fset_default (specpdl_symbol (specpdl_ptr
),
3205 specpdl_old_value (specpdl_ptr
));
3207 case SPECPDL_LET_LOCAL
:
3209 Lisp_Object symbol
= specpdl_symbol (specpdl_ptr
);
3210 Lisp_Object where
= specpdl_where (specpdl_ptr
);
3211 Lisp_Object old_value
= specpdl_old_value (specpdl_ptr
);
3212 eassert (BUFFERP (where
));
3214 /* If this was a local binding, reset the value in the appropriate
3215 buffer, but only if that buffer's binding still exists. */
3216 if (!NILP (Flocal_variable_p (symbol
, where
)))
3217 set_internal (symbol
, old_value
, where
, 1);
3224 dynwind_begin (void)
3226 scm_dynwind_begin (0);
3235 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3236 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3237 A special variable is one that will be bound dynamically, even in a
3238 context where binding is lexical by default. */)
3239 (Lisp_Object symbol
)
3241 CHECK_SYMBOL (symbol
);
3242 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3246 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3247 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3248 The debugger is entered when that frame exits, if the flag is non-nil. */)
3249 (Lisp_Object level
, Lisp_Object flag
)
3251 union specbinding
*pdl
= backtrace_top ();
3252 register EMACS_INT i
;
3254 CHECK_NUMBER (level
);
3256 for (i
= 0; backtrace_p (pdl
) && i
< XINT (level
); i
++)
3257 pdl
= backtrace_next (pdl
);
3259 if (backtrace_p (pdl
))
3260 set_backtrace_debug_on_exit (pdl
, !NILP (flag
));
3265 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3266 doc
: /* Print a trace of Lisp function calls currently active.
3267 Output stream used is value of `standard-output'. */)
3270 union specbinding
*pdl
= backtrace_top ();
3272 Lisp_Object old_print_level
= Vprint_level
;
3274 if (NILP (Vprint_level
))
3275 XSETFASTINT (Vprint_level
, 8);
3277 while (backtrace_p (pdl
))
3279 write_string (backtrace_debug_on_exit (pdl
) ? "* " : " ", 2);
3280 if (backtrace_nargs (pdl
) == UNEVALLED
)
3282 Fprin1 (Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)),
3284 write_string ("\n", -1);
3288 tem
= backtrace_function (pdl
);
3289 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3290 write_string ("(", -1);
3293 for (i
= 0; i
< backtrace_nargs (pdl
); i
++)
3295 if (i
) write_string (" ", -1);
3296 Fprin1 (backtrace_args (pdl
)[i
], Qnil
);
3299 write_string (")\n", -1);
3301 pdl
= backtrace_next (pdl
);
3304 Vprint_level
= old_print_level
;
3308 static union specbinding
*
3309 get_backtrace_frame (Lisp_Object nframes
, Lisp_Object base
)
3311 union specbinding
*pdl
= backtrace_top ();
3312 register EMACS_INT i
;
3314 CHECK_NATNUM (nframes
);
3317 { /* Skip up to `base'. */
3318 base
= Findirect_function (base
, Qt
);
3319 while (backtrace_p (pdl
)
3320 && !EQ (base
, Findirect_function (backtrace_function (pdl
), Qt
)))
3321 pdl
= backtrace_next (pdl
);
3324 /* Find the frame requested. */
3325 for (i
= XFASTINT (nframes
); i
> 0 && backtrace_p (pdl
); i
--)
3326 pdl
= backtrace_next (pdl
);
3331 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 2, NULL
,
3332 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3333 If that frame has not evaluated the arguments yet (or is a special form),
3334 the value is (nil FUNCTION ARG-FORMS...).
3335 If that frame has evaluated its arguments and called its function already,
3336 the value is (t FUNCTION ARG-VALUES...).
3337 A &rest arg is represented as the tail of the list ARG-VALUES.
3338 FUNCTION is whatever was supplied as car of evaluated list,
3339 or a lambda expression for macro calls.
3340 If NFRAMES is more than the number of frames, the value is nil.
3341 If BASE is non-nil, it should be a function and NFRAMES counts from its
3342 nearest activation frame. */)
3343 (Lisp_Object nframes
, Lisp_Object base
)
3345 union specbinding
*pdl
= get_backtrace_frame (nframes
, base
);
3347 if (!backtrace_p (pdl
))
3349 if (backtrace_nargs (pdl
) == UNEVALLED
)
3351 Fcons (backtrace_function (pdl
), *backtrace_args (pdl
)));
3354 Lisp_Object tem
= Flist (backtrace_nargs (pdl
), backtrace_args (pdl
));
3356 return Fcons (Qt
, Fcons (backtrace_function (pdl
), tem
));
3360 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3361 the specpdl stack, and then rewind them. We store the pre-unwind values
3362 directly in the pre-existing specpdl elements (i.e. we swap the current
3363 value and the old value stored in the specpdl), kind of like the inplace
3364 pointer-reversal trick. As it turns out, the rewind does the same as the
3365 unwind, except it starts from the other end of the specpdl stack, so we use
3366 the same function for both unwind and rewind. */
3368 backtrace_eval_unrewind (int distance
)
3370 union specbinding
*tmp
= specpdl_ptr
;
3373 { /* It's a rewind rather than unwind. */
3374 tmp
+= distance
- 1;
3376 distance
= -distance
;
3379 for (; distance
> 0; distance
--)
3385 case SPECPDL_BACKTRACE
:
3388 { /* If variable has a trivial value (no forwarding), we can
3389 just set it. No need to check for constant symbols here,
3390 since that was already done by specbind. */
3391 struct Lisp_Symbol
*sym
= XSYMBOL (specpdl_symbol (tmp
));
3392 if (sym
->redirect
== SYMBOL_PLAINVAL
)
3394 Lisp_Object old_value
= specpdl_old_value (tmp
);
3395 set_specpdl_old_value (tmp
, SYMBOL_VAL (sym
));
3396 SET_SYMBOL_VAL (sym
, old_value
);
3401 NOTE: we only ever come here if make_local_foo was used for
3402 the first time on this var within this let. */
3405 case SPECPDL_LET_DEFAULT
:
3407 Lisp_Object sym
= specpdl_symbol (tmp
);
3408 Lisp_Object old_value
= specpdl_old_value (tmp
);
3409 set_specpdl_old_value (tmp
, Fdefault_value (sym
));
3410 Fset_default (sym
, old_value
);
3413 case SPECPDL_LET_LOCAL
:
3415 Lisp_Object symbol
= specpdl_symbol (tmp
);
3416 Lisp_Object where
= specpdl_where (tmp
);
3417 Lisp_Object old_value
= specpdl_old_value (tmp
);
3418 eassert (BUFFERP (where
));
3420 /* If this was a local binding, reset the value in the appropriate
3421 buffer, but only if that buffer's binding still exists. */
3422 if (!NILP (Flocal_variable_p (symbol
, where
)))
3424 set_specpdl_old_value
3425 (tmp
, Fbuffer_local_value (symbol
, where
));
3426 set_internal (symbol
, old_value
, where
, 1);
3434 DEFUN ("backtrace-eval", Fbacktrace_eval
, Sbacktrace_eval
, 2, 3, NULL
,
3435 doc
: /* Evaluate EXP in the context of some activation frame.
3436 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3437 (Lisp_Object exp
, Lisp_Object nframes
, Lisp_Object base
)
3439 union specbinding
*pdl
= get_backtrace_frame (nframes
, base
);
3441 ptrdiff_t distance
= specpdl_ptr
- pdl
;
3442 eassert (distance
>= 0);
3444 if (!backtrace_p (pdl
))
3445 error ("Activation frame not found!");
3447 backtrace_eval_unrewind (distance
);
3448 record_unwind_protect_int (backtrace_eval_unrewind
, -distance
);
3450 /* Use eval_sub rather than Feval since the main motivation behind
3451 backtrace-eval is to be able to get/set the value of lexical variables
3452 from the debugger. */
3453 Lisp_Object tem1
= eval_sub (exp
);
3458 DEFUN ("backtrace--locals", Fbacktrace__locals
, Sbacktrace__locals
, 1, 2, NULL
,
3459 doc
: /* Return names and values of local variables of a stack frame.
3460 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3461 (Lisp_Object nframes
, Lisp_Object base
)
3463 union specbinding
*frame
= get_backtrace_frame (nframes
, base
);
3464 union specbinding
*prevframe
3465 = get_backtrace_frame (make_number (XFASTINT (nframes
) - 1), base
);
3466 ptrdiff_t distance
= specpdl_ptr
- frame
;
3467 Lisp_Object result
= Qnil
;
3468 eassert (distance
>= 0);
3470 if (!backtrace_p (prevframe
))
3471 error ("Activation frame not found!");
3472 if (!backtrace_p (frame
))
3473 error ("Activation frame not found!");
3475 /* The specpdl entries normally contain the symbol being bound along with its
3476 `old_value', so it can be restored. The new value to which it is bound is
3477 available in one of two places: either in the current value of the
3478 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3479 next specpdl entry for it.
3480 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3481 and "new value", so we abuse it here, to fetch the new value.
3482 It's ugly (we'd rather not modify global data) and a bit inefficient,
3483 but it does the job for now. */
3484 backtrace_eval_unrewind (distance
);
3488 union specbinding
*tmp
= prevframe
;
3489 for (; tmp
> frame
; tmp
--)
3494 case SPECPDL_LET_DEFAULT
:
3495 case SPECPDL_LET_LOCAL
:
3497 Lisp_Object sym
= specpdl_symbol (tmp
);
3498 Lisp_Object val
= specpdl_old_value (tmp
);
3499 if (EQ (sym
, Qinternal_interpreter_environment
))
3501 Lisp_Object env
= val
;
3502 for (; CONSP (env
); env
= XCDR (env
))
3504 Lisp_Object binding
= XCAR (env
);
3505 if (CONSP (binding
))
3506 result
= Fcons (Fcons (XCAR (binding
),
3512 result
= Fcons (Fcons (sym
, val
), result
);
3518 /* Restore values from specpdl to original place. */
3519 backtrace_eval_unrewind (-distance
);
3526 get_backtrace (Lisp_Object array
)
3528 union specbinding
*pdl
= backtrace_next (backtrace_top ());
3529 ptrdiff_t i
= 0, asize
= ASIZE (array
);
3531 /* Copy the backtrace contents into working memory. */
3532 for (; i
< asize
; i
++)
3534 if (backtrace_p (pdl
))
3536 ASET (array
, i
, backtrace_function (pdl
));
3537 pdl
= backtrace_next (pdl
);
3540 ASET (array
, i
, Qnil
);
3544 Lisp_Object
backtrace_top_function (void)
3546 union specbinding
*pdl
= backtrace_top ();
3547 return (backtrace_p (pdl
) ? backtrace_function (pdl
) : Qnil
);
3551 abort_to_prompt (SCM tag
, SCM arglst
)
3553 static SCM var
= SCM_UNDEFINED
;
3554 if (SCM_UNBNDP (var
))
3555 var
= scm_c_public_lookup ("guile", "abort-to-prompt");
3557 scm_apply_1 (scm_variable_ref (var
), tag
, arglst
);
3562 call_with_prompt (SCM tag
, SCM thunk
, SCM handler
)
3564 static SCM var
= SCM_UNDEFINED
;
3565 if (SCM_UNBNDP (var
))
3566 var
= scm_c_public_lookup ("guile", "call-with-prompt");
3568 return scm_call_3 (scm_variable_ref (var
), tag
, thunk
, handler
);
3572 make_prompt_tag (void)
3574 static SCM var
= SCM_UNDEFINED
;
3575 if (SCM_UNBNDP (var
))
3576 var
= scm_c_public_lookup ("guile", "make-prompt-tag");
3578 return scm_call_0 (scm_variable_ref (var
));
3586 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3587 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3588 If Lisp code tries to increase the total number past this amount,
3589 an error is signaled.
3590 You can safely use a value considerably larger than the default value,
3591 if that proves inconveniently small. However, if you increase it too far,
3592 Emacs could run out of memory trying to make the stack bigger.
3593 Note that this limit may be silently increased by the debugger
3594 if `debug-on-error' or `debug-on-quit' is set. */);
3596 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3597 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3599 This limit serves to catch infinite recursions for you before they cause
3600 actual stack overflow in C, which would be fatal for Emacs.
3601 You can safely make it considerably larger than its default value,
3602 if that proves inconveniently small. However, if you increase it too far,
3603 Emacs could overflow the real C stack, and crash. */);
3605 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3606 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3607 If the value is t, that means do an ordinary quit.
3608 If the value equals `throw-on-input', that means quit by throwing
3609 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3610 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3611 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3614 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3615 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3616 Note that `quit-flag' will still be set by typing C-g,
3617 so a quit will be signaled as soon as `inhibit-quit' is nil.
3618 To prevent this happening, set `quit-flag' to nil
3619 before making `inhibit-quit' nil. */);
3620 Vinhibit_quit
= Qnil
;
3622 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3623 DEFSYM (Qautoload
, "autoload");
3624 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3625 DEFSYM (Qmacro
, "macro");
3626 DEFSYM (Qdeclare
, "declare");
3628 /* Note that the process handling also uses Qexit, but we don't want
3629 to staticpro it twice, so we just do it here. */
3630 DEFSYM (Qexit
, "exit");
3632 DEFSYM (Qinteractive
, "interactive");
3633 DEFSYM (Qcommandp
, "commandp");
3634 DEFSYM (Qand_rest
, "&rest");
3635 DEFSYM (Qand_optional
, "&optional");
3636 DEFSYM (Qclosure
, "closure");
3637 DEFSYM (Qdebug
, "debug");
3639 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3640 doc
: /* Non-nil means never enter the debugger.
3641 Normally set while the debugger is already active, to avoid recursive
3643 Vinhibit_debugger
= Qnil
;
3645 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3646 doc
: /* Non-nil means enter debugger if an error is signaled.
3647 Does not apply to errors handled by `condition-case' or those
3648 matched by `debug-ignored-errors'.
3649 If the value is a list, an error only means to enter the debugger
3650 if one of its condition symbols appears in the list.
3651 When you evaluate an expression interactively, this variable
3652 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3653 The command `toggle-debug-on-error' toggles this.
3654 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3655 Vdebug_on_error
= Qnil
;
3657 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3658 doc
: /* List of errors for which the debugger should not be called.
3659 Each element may be a condition-name or a regexp that matches error messages.
3660 If any element applies to a given error, that error skips the debugger
3661 and just returns to top level.
3662 This overrides the variable `debug-on-error'.
3663 It does not apply to errors handled by `condition-case'. */);
3664 Vdebug_ignored_errors
= Qnil
;
3666 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3667 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3668 Does not apply if quit is handled by a `condition-case'. */);
3671 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3672 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3674 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3675 doc
: /* Non-nil means debugger may continue execution.
3676 This is nil when the debugger is called under circumstances where it
3677 might not be safe to continue. */);
3678 debugger_may_continue
= 1;
3680 DEFVAR_LISP ("debugger", Vdebugger
,
3681 doc
: /* Function to call to invoke debugger.
3682 If due to frame exit, args are `exit' and the value being returned;
3683 this function's value will be returned instead of that.
3684 If due to error, args are `error' and a list of the args to `signal'.
3685 If due to `apply' or `funcall' entry, one arg, `lambda'.
3686 If due to `eval' entry, one arg, t. */);
3689 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3690 doc
: /* If non-nil, this is a function for `signal' to call.
3691 It receives the same arguments that `signal' was given.
3692 The Edebug package uses this to regain control. */);
3693 Vsignal_hook_function
= Qnil
;
3695 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3696 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3697 Note that `debug-on-error', `debug-on-quit' and friends
3698 still determine whether to handle the particular condition. */);
3699 Vdebug_on_signal
= Qnil
;
3701 /* When lexical binding is being used,
3702 Vinternal_interpreter_environment is non-nil, and contains an alist
3703 of lexically-bound variable, or (t), indicating an empty
3704 environment. The lisp name of this variable would be
3705 `internal-interpreter-environment' if it weren't hidden.
3706 Every element of this list can be either a cons (VAR . VAL)
3707 specifying a lexical binding, or a single symbol VAR indicating
3708 that this variable should use dynamic scoping. */
3709 DEFSYM (Qinternal_interpreter_environment
,
3710 "internal-interpreter-environment");
3711 DEFVAR_LISP ("internal-interpreter-environment",
3712 Vinternal_interpreter_environment
,
3713 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3714 When lexical binding is not being used, this variable is nil.
3715 A value of `(t)' indicates an empty environment, otherwise it is an
3716 alist of active lexical bindings. */);
3717 Vinternal_interpreter_environment
= Qnil
;
3718 /* Don't export this variable to Elisp, so no one can mess with it
3719 (Just imagine if someone makes it buffer-local). */
3720 Funintern (Qinternal_interpreter_environment
, Qnil
);
3722 DEFSYM (Vrun_hooks
, "run-hooks");
3724 staticpro (&Vautoload_queue
);
3725 Vautoload_queue
= Qnil
;
3726 staticpro (&Vsignaling_function
);
3727 Vsignaling_function
= Qnil
;
3729 inhibit_lisp_code
= Qnil
;