1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
37 struct backtrace
*next
;
38 Lisp_Object
*function
;
39 Lisp_Object
*args
; /* Points to vector of args. */
40 ptrdiff_t nargs
; /* Length of vector. */
41 /* Nonzero means call value of debugger when done with this operation. */
42 unsigned int debug_on_exit
: 1;
45 static struct backtrace
*backtrace_list
;
50 struct catchtag
*catchlist
;
52 /* Chain of condition handlers currently in effect.
53 The elements of this chain are contained in the stack frames
54 of Fcondition_case and internal_condition_case.
55 When an error is signaled (by calling Fsignal, below),
56 this chain is searched for an element that applies. */
61 struct handler
*handlerlist
;
64 /* Count levels of GCPRO to detect failure to UNGCPRO. */
68 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
69 Lisp_Object Qinhibit_quit
;
70 Lisp_Object Qand_rest
;
71 static Lisp_Object Qand_optional
;
72 static Lisp_Object Qdebug_on_error
;
73 static Lisp_Object Qdeclare
;
74 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
76 static Lisp_Object Qdebug
;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
82 Lisp_Object Vrun_hooks
;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue
;
91 /* Current number of specbindings allocated in specpdl. */
93 EMACS_INT specpdl_size
;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding
*specpdl
;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding
*specpdl_ptr
;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth
;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
114 static int when_entered_debugger
;
116 /* The function from which the last `signal' was called. Set in
119 Lisp_Object Vsignaling_function
;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
127 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
128 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
129 static int interactive_p (int);
130 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
131 static Lisp_Object
Ffetch_bytecode (Lisp_Object
);
134 init_eval_once (void)
137 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
138 specpdl_ptr
= specpdl
;
139 /* Don't forget to update docs (lispref node "Local Variables"). */
140 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
141 max_lisp_eval_depth
= 600;
149 specpdl_ptr
= specpdl
;
154 debug_on_next_call
= 0;
159 /* This is less than the initial value of num_nonmacro_input_events. */
160 when_entered_debugger
= -1;
163 /* Unwind-protect function used by call_debugger. */
166 restore_stack_limits (Lisp_Object data
)
168 max_specpdl_size
= XINT (XCAR (data
));
169 max_lisp_eval_depth
= XINT (XCDR (data
));
173 /* Call the Lisp debugger, giving it argument ARG. */
176 call_debugger (Lisp_Object arg
)
178 int debug_while_redisplaying
;
179 int count
= SPECPDL_INDEX ();
181 EMACS_INT old_max
= max_specpdl_size
;
183 /* Temporarily bump up the stack limits,
184 so the debugger won't run out of stack. */
186 max_specpdl_size
+= 1;
187 record_unwind_protect (restore_stack_limits
,
188 Fcons (make_number (old_max
),
189 make_number (max_lisp_eval_depth
)));
190 max_specpdl_size
= old_max
;
192 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
193 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
195 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
196 max_specpdl_size
= SPECPDL_INDEX () + 100;
198 #ifdef HAVE_WINDOW_SYSTEM
199 if (display_hourglass_p
)
203 debug_on_next_call
= 0;
204 when_entered_debugger
= num_nonmacro_input_events
;
206 /* Resetting redisplaying_p to 0 makes sure that debug output is
207 displayed if the debugger is invoked during redisplay. */
208 debug_while_redisplaying
= redisplaying_p
;
210 specbind (intern ("debugger-may-continue"),
211 debug_while_redisplaying
? Qnil
: Qt
);
212 specbind (Qinhibit_redisplay
, Qnil
);
213 specbind (Qdebug_on_error
, Qnil
);
215 #if 0 /* Binding this prevents execution of Lisp code during
216 redisplay, which necessarily leads to display problems. */
217 specbind (Qinhibit_eval_during_redisplay
, Qt
);
220 val
= apply1 (Vdebugger
, arg
);
222 /* Interrupting redisplay and resuming it later is not safe under
223 all circumstances. So, when the debugger returns, abort the
224 interrupted redisplay by going back to the top-level. */
225 if (debug_while_redisplaying
)
228 return unbind_to (count
, val
);
232 do_debug_on_call (Lisp_Object code
)
234 debug_on_next_call
= 0;
235 backtrace_list
->debug_on_exit
= 1;
236 call_debugger (Fcons (code
, Qnil
));
239 /* NOTE!!! Every function that can call EVAL must protect its args
240 and temporaries from garbage collection while it needs them.
241 The definition of `For' shows what you have to do. */
243 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
244 doc
: /* Eval args until one of them yields non-nil, then return that value.
245 The remaining args are not evalled at all.
246 If all args return nil, return nil.
247 usage: (or CONDITIONS...) */)
250 register Lisp_Object val
= Qnil
;
257 val
= eval_sub (XCAR (args
));
267 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
268 doc
: /* Eval args until one of them yields nil, then return nil.
269 The remaining args are not evalled at all.
270 If no arg yields nil, return the last arg's value.
271 usage: (and CONDITIONS...) */)
274 register Lisp_Object val
= Qt
;
281 val
= eval_sub (XCAR (args
));
291 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
292 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
293 Returns the value of THEN or the value of the last of the ELSE's.
294 THEN must be one expression, but ELSE... can be zero or more expressions.
295 If COND yields nil, and there are no ELSE's, the value is nil.
296 usage: (if COND THEN ELSE...) */)
299 register Lisp_Object cond
;
303 cond
= eval_sub (Fcar (args
));
307 return eval_sub (Fcar (Fcdr (args
)));
308 return Fprogn (Fcdr (Fcdr (args
)));
311 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
312 doc
: /* Try each clause until one succeeds.
313 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
314 and, if the value is non-nil, this clause succeeds:
315 then the expressions in BODY are evaluated and the last one's
316 value is the value of the cond-form.
317 If no clause succeeds, cond returns nil.
318 If a clause has one element, as in (CONDITION),
319 CONDITION's value if non-nil is returned from the cond-form.
320 usage: (cond CLAUSES...) */)
323 register Lisp_Object clause
, val
;
330 clause
= Fcar (args
);
331 val
= eval_sub (Fcar (clause
));
334 if (!EQ (XCDR (clause
), Qnil
))
335 val
= Fprogn (XCDR (clause
));
345 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
346 doc
: /* Eval BODY forms sequentially and return value of last one.
347 usage: (progn BODY...) */)
350 register Lisp_Object val
= Qnil
;
357 val
= eval_sub (XCAR (args
));
365 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
366 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
367 The value of FIRST is saved during the evaluation of the remaining args,
368 whose values are discarded.
369 usage: (prog1 FIRST BODY...) */)
373 register Lisp_Object args_left
;
374 struct gcpro gcpro1
, gcpro2
;
375 register int argnum
= 0;
386 Lisp_Object tem
= eval_sub (XCAR (args_left
));
389 args_left
= XCDR (args_left
);
391 while (CONSP (args_left
));
397 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
398 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
399 The value of FORM2 is saved during the evaluation of the
400 remaining args, whose values are discarded.
401 usage: (prog2 FORM1 FORM2 BODY...) */)
405 register Lisp_Object args_left
;
406 struct gcpro gcpro1
, gcpro2
;
407 register int argnum
= -1;
420 Lisp_Object tem
= eval_sub (XCAR (args_left
));
423 args_left
= XCDR (args_left
);
425 while (CONSP (args_left
));
431 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
432 doc
: /* Set each SYM to the value of its VAL.
433 The symbols SYM are variables; they are literal (not evaluated).
434 The values VAL are expressions; they are evaluated.
435 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
436 The second VAL is not computed until after the first SYM is set, and so on;
437 each VAL can use the new value of variables set earlier in the `setq'.
438 The return value of the `setq' form is the value of the last VAL.
439 usage: (setq [SYM VAL]...) */)
442 register Lisp_Object args_left
;
443 register Lisp_Object val
, sym
, lex_binding
;
454 val
= eval_sub (Fcar (Fcdr (args_left
)));
455 sym
= Fcar (args_left
);
457 /* Like for eval_sub, we do not check declared_special here since
458 it's been done when let-binding. */
459 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
461 && !NILP (lex_binding
462 = Fassq (sym
, Vinternal_interpreter_environment
)))
463 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
465 Fset (sym
, val
); /* SYM is dynamically bound. */
467 args_left
= Fcdr (Fcdr (args_left
));
469 while (!NILP(args_left
));
475 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
476 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
477 usage: (quote ARG) */)
480 if (!NILP (Fcdr (args
)))
481 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
485 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
486 doc
: /* Like `quote', but preferred for objects which are functions.
487 In byte compilation, `function' causes its argument to be compiled.
488 `quote' cannot do that.
489 usage: (function ARG) */)
492 Lisp_Object quoted
= XCAR (args
);
494 if (!NILP (Fcdr (args
)))
495 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
497 if (!NILP (Vinternal_interpreter_environment
)
499 && EQ (XCAR (quoted
), Qlambda
))
500 /* This is a lambda expression within a lexical environment;
501 return an interpreted closure instead of a simple lambda. */
502 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
505 /* Simply quote the argument. */
510 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
511 doc
: /* Return t if the containing function was run directly by user input.
512 This means that the function was called with `call-interactively'
513 \(which includes being called as the binding of a key)
514 and input is currently coming from the keyboard (not a keyboard macro),
515 and Emacs is not running in batch mode (`noninteractive' is nil).
517 The only known proper use of `interactive-p' is in deciding whether to
518 display a helpful message, or how to display it. If you're thinking
519 of using it for any other purpose, it is quite likely that you're
520 making a mistake. Think: what do you want to do when the command is
521 called from a keyboard macro?
523 To test whether your function was called with `call-interactively',
524 either (i) add an extra optional argument and give it an `interactive'
525 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
526 use `called-interactively-p'. */)
529 return interactive_p (1) ? Qt
: Qnil
;
533 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
534 doc
: /* Return t if the containing function was called by `call-interactively'.
535 If KIND is `interactive', then only return t if the call was made
536 interactively by the user, i.e. not in `noninteractive' mode nor
537 when `executing-kbd-macro'.
538 If KIND is `any', on the other hand, it will return t for any kind of
539 interactive call, including being called as the binding of a key, or
540 from a keyboard macro, or in `noninteractive' mode.
542 The only known proper use of `interactive' for KIND is in deciding
543 whether to display a helpful message, or how to display it. If you're
544 thinking of using it for any other purpose, it is quite likely that
545 you're making a mistake. Think: what do you want to do when the
546 command is called from a keyboard macro?
548 This function is meant for implementing advice and other
549 function-modifying features. Instead of using this, it is sometimes
550 cleaner to give your function an extra optional argument whose
551 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
552 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
555 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
556 && interactive_p (1)) ? Qt
: Qnil
;
560 /* Return 1 if function in which this appears was called using
563 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
564 called is a built-in. */
567 interactive_p (int exclude_subrs_p
)
569 struct backtrace
*btp
;
572 btp
= backtrace_list
;
574 /* If this isn't a byte-compiled function, there may be a frame at
575 the top for Finteractive_p. If so, skip it. */
576 fun
= Findirect_function (*btp
->function
, Qnil
);
577 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
578 || XSUBR (fun
) == &Scalled_interactively_p
))
581 /* If we're running an Emacs 18-style byte-compiled function, there
582 may be a frame for Fbytecode at the top level. In any version of
583 Emacs there can be Fbytecode frames for subexpressions evaluated
584 inside catch and condition-case. Skip past them.
586 If this isn't a byte-compiled function, then we may now be
587 looking at several frames for special forms. Skip past them. */
589 && (EQ (*btp
->function
, Qbytecode
)
590 || btp
->nargs
== UNEVALLED
))
593 /* `btp' now points at the frame of the innermost function that isn't
594 a special form, ignoring frames for Finteractive_p and/or
595 Fbytecode at the top. If this frame is for a built-in function
596 (such as load or eval-region) return nil. */
597 fun
= Findirect_function (*btp
->function
, Qnil
);
598 if (exclude_subrs_p
&& SUBRP (fun
))
601 /* `btp' points to the frame of a Lisp function that called interactive-p.
602 Return t if that function was called interactively. */
603 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
609 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
610 doc
: /* Define NAME as a function.
611 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
612 See also the function `interactive'.
613 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
616 register Lisp_Object fn_name
;
617 register Lisp_Object defn
;
619 fn_name
= Fcar (args
);
620 CHECK_SYMBOL (fn_name
);
621 defn
= Fcons (Qlambda
, Fcdr (args
));
622 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
623 defn
= Ffunction (Fcons (defn
, Qnil
));
624 if (!NILP (Vpurify_flag
))
625 defn
= Fpurecopy (defn
);
626 if (CONSP (XSYMBOL (fn_name
)->function
)
627 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
628 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
629 Ffset (fn_name
, defn
);
630 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
634 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
635 doc
: /* Define NAME as a macro.
636 The actual definition looks like
637 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
638 When the macro is called, as in (NAME ARGS...),
639 the function (lambda ARGLIST BODY...) is applied to
640 the list ARGS... as it appears in the expression,
641 and the result should be a form to be evaluated instead of the original.
643 DECL is a declaration, optional, which can specify how to indent
644 calls to this macro, how Edebug should handle it, and which argument
645 should be treated as documentation. It looks like this:
647 The elements can look like this:
649 Set NAME's `lisp-indent-function' property to INDENT.
652 Set NAME's `edebug-form-spec' property to DEBUG. (This is
653 equivalent to writing a `def-edebug-spec' for the macro.)
656 Set NAME's `doc-string-elt' property to ELT.
658 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
661 register Lisp_Object fn_name
;
662 register Lisp_Object defn
;
663 Lisp_Object lambda_list
, doc
, tail
;
665 fn_name
= Fcar (args
);
666 CHECK_SYMBOL (fn_name
);
667 lambda_list
= Fcar (Fcdr (args
));
668 tail
= Fcdr (Fcdr (args
));
671 if (STRINGP (Fcar (tail
)))
677 if (CONSP (Fcar (tail
))
678 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
680 if (!NILP (Vmacro_declaration_function
))
684 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
692 tail
= Fcons (lambda_list
, tail
);
694 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
696 defn
= Fcons (Qlambda
, tail
);
697 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
698 defn
= Ffunction (Fcons (defn
, Qnil
));
699 defn
= Fcons (Qmacro
, defn
);
701 if (!NILP (Vpurify_flag
))
702 defn
= Fpurecopy (defn
);
703 if (CONSP (XSYMBOL (fn_name
)->function
)
704 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
705 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
706 Ffset (fn_name
, defn
);
707 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
712 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
713 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
714 Aliased variables always have the same value; setting one sets the other.
715 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
716 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
717 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
718 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
719 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
720 The return value is BASE-VARIABLE. */)
721 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
723 struct Lisp_Symbol
*sym
;
725 CHECK_SYMBOL (new_alias
);
726 CHECK_SYMBOL (base_variable
);
728 sym
= XSYMBOL (new_alias
);
731 /* Not sure why, but why not? */
732 error ("Cannot make a constant an alias");
734 switch (sym
->redirect
)
736 case SYMBOL_FORWARDED
:
737 error ("Cannot make an internal variable an alias");
738 case SYMBOL_LOCALIZED
:
739 error ("Don't know how to make a localized variable an alias");
742 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
743 If n_a is bound, but b_v is not, set the value of b_v to n_a,
744 so that old-code that affects n_a before the aliasing is setup
746 if (NILP (Fboundp (base_variable
)))
747 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
750 struct specbinding
*p
;
752 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
755 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
756 error ("Don't know how to make a let-bound variable an alias");
759 sym
->declared_special
= 1;
760 XSYMBOL (base_variable
)->declared_special
= 1;
761 sym
->redirect
= SYMBOL_VARALIAS
;
762 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
763 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
764 LOADHIST_ATTACH (new_alias
);
765 /* Even if docstring is nil: remove old docstring. */
766 Fput (new_alias
, Qvariable_documentation
, docstring
);
768 return base_variable
;
772 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
773 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
774 You are not required to define a variable in order to use it,
775 but the definition can supply documentation and an initial value
776 in a way that tags can recognize.
778 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
779 If SYMBOL is buffer-local, its default value is what is set;
780 buffer-local values are not affected.
781 INITVALUE and DOCSTRING are optional.
782 If DOCSTRING starts with *, this variable is identified as a user option.
783 This means that M-x set-variable recognizes it.
784 See also `user-variable-p'.
785 If INITVALUE is missing, SYMBOL's value is not set.
787 If SYMBOL has a local binding, then this form affects the local
788 binding. This is usually not what you want. Thus, if you need to
789 load a file defining variables, with this form or with `defconst' or
790 `defcustom', you should always load that file _outside_ any bindings
791 for these variables. \(`defconst' and `defcustom' behave similarly in
793 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
796 register Lisp_Object sym
, tem
, tail
;
800 if (!NILP (Fcdr (Fcdr (tail
))))
801 error ("Too many arguments");
803 tem
= Fdefault_boundp (sym
);
806 /* Do it before evaluating the initial value, for self-references. */
807 XSYMBOL (sym
)->declared_special
= 1;
809 if (SYMBOL_CONSTANT_P (sym
))
811 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
812 Lisp_Object tem1
= Fcar (tail
);
814 && EQ (XCAR (tem1
), Qquote
)
815 && CONSP (XCDR (tem1
))
816 && EQ (XCAR (XCDR (tem1
)), sym
)))
817 error ("Constant symbol `%s' specified in defvar",
818 SDATA (SYMBOL_NAME (sym
)));
822 Fset_default (sym
, eval_sub (Fcar (tail
)));
824 { /* Check if there is really a global binding rather than just a let
825 binding that shadows the global unboundness of the var. */
826 volatile struct specbinding
*pdl
= specpdl_ptr
;
827 while (--pdl
>= specpdl
)
829 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
830 && EQ (pdl
->old_value
, Qunbound
))
832 message_with_string ("Warning: defvar ignored because %s is let-bound",
833 SYMBOL_NAME (sym
), 1);
842 if (!NILP (Vpurify_flag
))
843 tem
= Fpurecopy (tem
);
844 Fput (sym
, Qvariable_documentation
, tem
);
846 LOADHIST_ATTACH (sym
);
848 else if (!NILP (Vinternal_interpreter_environment
)
849 && !XSYMBOL (sym
)->declared_special
)
850 /* A simple (defvar foo) with lexical scoping does "nothing" except
851 declare that var to be dynamically scoped *locally* (i.e. within
852 the current file or let-block). */
853 Vinternal_interpreter_environment
=
854 Fcons (sym
, Vinternal_interpreter_environment
);
857 /* Simple (defvar <var>) should not count as a definition at all.
858 It could get in the way of other definitions, and unloading this
859 package could try to make the variable unbound. */
865 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
866 doc
: /* Define SYMBOL as a constant variable.
867 The intent is that neither programs nor users should ever change this value.
868 Always sets the value of SYMBOL to the result of evalling INITVALUE.
869 If SYMBOL is buffer-local, its default value is what is set;
870 buffer-local values are not affected.
871 DOCSTRING is optional.
873 If SYMBOL has a local binding, then this form sets the local binding's
874 value. However, you should normally not make local bindings for
875 variables defined with this form.
876 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
879 register Lisp_Object sym
, tem
;
882 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
883 error ("Too many arguments");
885 tem
= eval_sub (Fcar (Fcdr (args
)));
886 if (!NILP (Vpurify_flag
))
887 tem
= Fpurecopy (tem
);
888 Fset_default (sym
, tem
);
889 XSYMBOL (sym
)->declared_special
= 1;
890 tem
= Fcar (Fcdr (Fcdr (args
)));
893 if (!NILP (Vpurify_flag
))
894 tem
= Fpurecopy (tem
);
895 Fput (sym
, Qvariable_documentation
, tem
);
897 Fput (sym
, Qrisky_local_variable
, Qt
);
898 LOADHIST_ATTACH (sym
);
902 /* Error handler used in Fuser_variable_p. */
904 user_variable_p_eh (Lisp_Object ignore
)
910 lisp_indirect_variable (Lisp_Object sym
)
912 struct Lisp_Symbol
*s
= indirect_variable (XSYMBOL (sym
));
917 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
918 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
919 \(The alternative is a variable used internally in a Lisp program.)
920 A variable is a user variable if
921 \(1) the first character of its documentation is `*', or
922 \(2) it is customizable (its property list contains a non-nil value
923 of `standard-value' or `custom-autoload'), or
924 \(3) it is an alias for another user variable.
925 Return nil if VARIABLE is an alias and there is a loop in the
926 chain of symbols. */)
927 (Lisp_Object variable
)
929 Lisp_Object documentation
;
931 if (!SYMBOLP (variable
))
934 /* If indirect and there's an alias loop, don't check anything else. */
935 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
936 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
937 Qt
, user_variable_p_eh
)))
942 documentation
= Fget (variable
, Qvariable_documentation
);
943 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
945 if (STRINGP (documentation
)
946 && ((unsigned char) SREF (documentation
, 0) == '*'))
948 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
949 if (CONSP (documentation
)
950 && STRINGP (XCAR (documentation
))
951 && INTEGERP (XCDR (documentation
))
952 && XINT (XCDR (documentation
)) < 0)
954 /* Customizable? See `custom-variable-p'. */
955 if ((!NILP (Fget (variable
, intern ("standard-value"))))
956 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
959 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
962 /* An indirect variable? Let's follow the chain. */
963 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
967 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
968 doc
: /* Bind variables according to VARLIST then eval BODY.
969 The value of the last form in BODY is returned.
970 Each element of VARLIST is a symbol (which is bound to nil)
971 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
972 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
973 usage: (let* VARLIST BODY...) */)
976 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
977 int count
= SPECPDL_INDEX ();
978 struct gcpro gcpro1
, gcpro2
, gcpro3
;
980 GCPRO3 (args
, elt
, varlist
);
982 lexenv
= Vinternal_interpreter_environment
;
984 varlist
= Fcar (args
);
985 while (CONSP (varlist
))
989 elt
= XCAR (varlist
);
995 else if (! NILP (Fcdr (Fcdr (elt
))))
996 signal_error ("`let' bindings can have only one value-form", elt
);
1000 val
= eval_sub (Fcar (Fcdr (elt
)));
1003 if (!NILP (lexenv
) && SYMBOLP (var
)
1004 && !XSYMBOL (var
)->declared_special
1005 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
1006 /* Lexically bind VAR by adding it to the interpreter's binding
1010 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
1011 if (EQ (Vinternal_interpreter_environment
, lexenv
))
1012 /* Save the old lexical environment on the specpdl stack,
1013 but only for the first lexical binding, since we'll never
1014 need to revert to one of the intermediate ones. */
1015 specbind (Qinternal_interpreter_environment
, newenv
);
1017 Vinternal_interpreter_environment
= newenv
;
1020 specbind (var
, val
);
1022 varlist
= XCDR (varlist
);
1025 val
= Fprogn (Fcdr (args
));
1026 return unbind_to (count
, val
);
1029 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1030 doc
: /* Bind variables according to VARLIST then eval BODY.
1031 The value of the last form in BODY is returned.
1032 Each element of VARLIST is a symbol (which is bound to nil)
1033 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1034 All the VALUEFORMs are evalled before any symbols are bound.
1035 usage: (let VARLIST BODY...) */)
1038 Lisp_Object
*temps
, tem
, lexenv
;
1039 register Lisp_Object elt
, varlist
;
1040 int count
= SPECPDL_INDEX ();
1042 struct gcpro gcpro1
, gcpro2
;
1045 varlist
= Fcar (args
);
1047 /* Make space to hold the values to give the bound variables. */
1048 elt
= Flength (varlist
);
1049 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
1051 /* Compute the values and store them in `temps'. */
1053 GCPRO2 (args
, *temps
);
1056 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1059 elt
= XCAR (varlist
);
1061 temps
[argnum
++] = Qnil
;
1062 else if (! NILP (Fcdr (Fcdr (elt
))))
1063 signal_error ("`let' bindings can have only one value-form", elt
);
1065 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
1066 gcpro2
.nvars
= argnum
;
1070 lexenv
= Vinternal_interpreter_environment
;
1072 varlist
= Fcar (args
);
1073 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1077 elt
= XCAR (varlist
);
1078 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
1079 tem
= temps
[argnum
++];
1081 if (!NILP (lexenv
) && SYMBOLP (var
)
1082 && !XSYMBOL (var
)->declared_special
1083 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
1084 /* Lexically bind VAR by adding it to the lexenv alist. */
1085 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
1087 /* Dynamically bind VAR. */
1088 specbind (var
, tem
);
1091 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
1092 /* Instantiate a new lexical environment. */
1093 specbind (Qinternal_interpreter_environment
, lexenv
);
1095 elt
= Fprogn (Fcdr (args
));
1097 return unbind_to (count
, elt
);
1100 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1101 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1102 The order of execution is thus TEST, BODY, TEST, BODY and so on
1103 until TEST returns nil.
1104 usage: (while TEST BODY...) */)
1107 Lisp_Object test
, body
;
1108 struct gcpro gcpro1
, gcpro2
;
1110 GCPRO2 (test
, body
);
1114 while (!NILP (eval_sub (test
)))
1124 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1125 doc
: /* Return result of expanding macros at top level of FORM.
1126 If FORM is not a macro call, it is returned unchanged.
1127 Otherwise, the macro is expanded and the expansion is considered
1128 in place of FORM. When a non-macro-call results, it is returned.
1130 The second optional arg ENVIRONMENT specifies an environment of macro
1131 definitions to shadow the loaded ones for use in file byte-compilation. */)
1132 (Lisp_Object form
, Lisp_Object environment
)
1134 /* With cleanups from Hallvard Furuseth. */
1135 register Lisp_Object expander
, sym
, def
, tem
;
1139 /* Come back here each time we expand a macro call,
1140 in case it expands into another macro call. */
1143 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1144 def
= sym
= XCAR (form
);
1146 /* Trace symbols aliases to other symbols
1147 until we get a symbol that is not an alias. */
1148 while (SYMBOLP (def
))
1152 tem
= Fassq (sym
, environment
);
1155 def
= XSYMBOL (sym
)->function
;
1156 if (!EQ (def
, Qunbound
))
1161 /* Right now TEM is the result from SYM in ENVIRONMENT,
1162 and if TEM is nil then DEF is SYM's function definition. */
1165 /* SYM is not mentioned in ENVIRONMENT.
1166 Look at its function definition. */
1167 if (EQ (def
, Qunbound
) || !CONSP (def
))
1168 /* Not defined or definition not suitable. */
1170 if (EQ (XCAR (def
), Qautoload
))
1172 /* Autoloading function: will it be a macro when loaded? */
1173 tem
= Fnth (make_number (4), def
);
1174 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1175 /* Yes, load it and try again. */
1177 struct gcpro gcpro1
;
1179 do_autoload (def
, sym
);
1186 else if (!EQ (XCAR (def
), Qmacro
))
1188 else expander
= XCDR (def
);
1192 expander
= XCDR (tem
);
1193 if (NILP (expander
))
1196 form
= apply1 (expander
, XCDR (form
));
1201 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1202 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1203 TAG is evalled to get the tag to use; it must not be nil.
1205 Then the BODY is executed.
1206 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1207 If no throw happens, `catch' returns the value of the last BODY form.
1208 If a throw happens, it specifies the value to return from `catch'.
1209 usage: (catch TAG BODY...) */)
1212 register Lisp_Object tag
;
1213 struct gcpro gcpro1
;
1216 tag
= eval_sub (Fcar (args
));
1218 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1221 /* Set up a catch, then call C function FUNC on argument ARG.
1222 FUNC should return a Lisp_Object.
1223 This is how catches are done from within C code. */
1226 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1228 /* This structure is made part of the chain `catchlist'. */
1231 /* Fill in the components of c, and put it on the list. */
1235 c
.backlist
= backtrace_list
;
1236 c
.handlerlist
= handlerlist
;
1237 c
.lisp_eval_depth
= lisp_eval_depth
;
1238 c
.pdlcount
= SPECPDL_INDEX ();
1239 c
.poll_suppress_count
= poll_suppress_count
;
1240 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1241 c
.gcpro
= gcprolist
;
1242 c
.byte_stack
= byte_stack_list
;
1246 if (! _setjmp (c
.jmp
))
1247 c
.val
= (*func
) (arg
);
1249 /* Throw works by a longjmp that comes right here. */
1254 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1255 jump to that CATCH, returning VALUE as the value of that catch.
1257 This is the guts Fthrow and Fsignal; they differ only in the way
1258 they choose the catch tag to throw to. A catch tag for a
1259 condition-case form has a TAG of Qnil.
1261 Before each catch is discarded, unbind all special bindings and
1262 execute all unwind-protect clauses made above that catch. Unwind
1263 the handler stack as we go, so that the proper handlers are in
1264 effect for each unwind-protect clause we run. At the end, restore
1265 some static info saved in CATCH, and longjmp to the location
1268 This is used for correct unwinding in Fthrow and Fsignal. */
1271 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1273 register int last_time
;
1275 /* Save the value in the tag. */
1278 /* Restore certain special C variables. */
1279 set_poll_suppress_count (catch->poll_suppress_count
);
1280 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1281 handling_signal
= 0;
1286 last_time
= catchlist
== catch;
1288 /* Unwind the specpdl stack, and then restore the proper set of
1290 unbind_to (catchlist
->pdlcount
, Qnil
);
1291 handlerlist
= catchlist
->handlerlist
;
1292 catchlist
= catchlist
->next
;
1294 while (! last_time
);
1297 /* If x_catch_errors was done, turn it off now.
1298 (First we give unbind_to a chance to do that.) */
1299 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1300 The catch must remain in effect during that delicate
1301 state. --lorentey */
1302 x_fully_uncatch_errors ();
1306 byte_stack_list
= catch->byte_stack
;
1307 gcprolist
= catch->gcpro
;
1309 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1311 backtrace_list
= catch->backlist
;
1312 lisp_eval_depth
= catch->lisp_eval_depth
;
1314 _longjmp (catch->jmp
, 1);
1317 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1318 doc
: /* Throw to the catch for TAG and return VALUE from it.
1319 Both TAG and VALUE are evalled. */)
1320 (register Lisp_Object tag
, Lisp_Object value
)
1322 register struct catchtag
*c
;
1325 for (c
= catchlist
; c
; c
= c
->next
)
1327 if (EQ (c
->tag
, tag
))
1328 unwind_to_catch (c
, value
);
1330 xsignal2 (Qno_catch
, tag
, value
);
1334 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1335 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1336 If BODYFORM completes normally, its value is returned
1337 after executing the UNWINDFORMS.
1338 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1339 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1343 int count
= SPECPDL_INDEX ();
1345 record_unwind_protect (Fprogn
, Fcdr (args
));
1346 val
= eval_sub (Fcar (args
));
1347 return unbind_to (count
, val
);
1350 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1351 doc
: /* Regain control when an error is signaled.
1352 Executes BODYFORM and returns its value if no error happens.
1353 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1354 where the BODY is made of Lisp expressions.
1356 A handler is applicable to an error
1357 if CONDITION-NAME is one of the error's condition names.
1358 If an error happens, the first applicable handler is run.
1360 The car of a handler may be a list of condition names
1361 instead of a single condition name. Then it handles all of them.
1363 When a handler handles an error, control returns to the `condition-case'
1364 and it executes the handler's BODY...
1365 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1366 \(If VAR is nil, the handler can't access that information.)
1367 Then the value of the last BODY form is returned from the `condition-case'
1370 See also the function `signal' for more info.
1371 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1374 register Lisp_Object bodyform
, handlers
;
1375 volatile Lisp_Object var
;
1378 bodyform
= Fcar (Fcdr (args
));
1379 handlers
= Fcdr (Fcdr (args
));
1381 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1384 /* Like Fcondition_case, but the args are separate
1385 rather than passed in a list. Used by Fbyte_code. */
1388 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1389 Lisp_Object handlers
)
1397 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1403 && (SYMBOLP (XCAR (tem
))
1404 || CONSP (XCAR (tem
))))))
1405 error ("Invalid condition handler: %s",
1406 SDATA (Fprin1_to_string (tem
, Qt
)));
1411 c
.backlist
= backtrace_list
;
1412 c
.handlerlist
= handlerlist
;
1413 c
.lisp_eval_depth
= lisp_eval_depth
;
1414 c
.pdlcount
= SPECPDL_INDEX ();
1415 c
.poll_suppress_count
= poll_suppress_count
;
1416 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1417 c
.gcpro
= gcprolist
;
1418 c
.byte_stack
= byte_stack_list
;
1419 if (_setjmp (c
.jmp
))
1422 specbind (h
.var
, c
.val
);
1423 val
= Fprogn (Fcdr (h
.chosen_clause
));
1425 /* Note that this just undoes the binding of h.var; whoever
1426 longjumped to us unwound the stack to c.pdlcount before
1428 unbind_to (c
.pdlcount
, Qnil
);
1435 h
.handler
= handlers
;
1436 h
.next
= handlerlist
;
1440 val
= eval_sub (bodyform
);
1442 handlerlist
= h
.next
;
1446 /* Call the function BFUN with no arguments, catching errors within it
1447 according to HANDLERS. If there is an error, call HFUN with
1448 one argument which is the data that describes the error:
1451 HANDLERS can be a list of conditions to catch.
1452 If HANDLERS is Qt, catch all errors.
1453 If HANDLERS is Qerror, catch all errors
1454 but allow the debugger to run if that is enabled. */
1457 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1458 Lisp_Object (*hfun
) (Lisp_Object
))
1464 /* Since Fsignal will close off all calls to x_catch_errors,
1465 we will get the wrong results if some are not closed now. */
1467 if (x_catching_errors ())
1473 c
.backlist
= backtrace_list
;
1474 c
.handlerlist
= handlerlist
;
1475 c
.lisp_eval_depth
= lisp_eval_depth
;
1476 c
.pdlcount
= SPECPDL_INDEX ();
1477 c
.poll_suppress_count
= poll_suppress_count
;
1478 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1479 c
.gcpro
= gcprolist
;
1480 c
.byte_stack
= byte_stack_list
;
1481 if (_setjmp (c
.jmp
))
1483 return (*hfun
) (c
.val
);
1487 h
.handler
= handlers
;
1489 h
.next
= handlerlist
;
1495 handlerlist
= h
.next
;
1499 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1502 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1503 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1509 /* Since Fsignal will close off all calls to x_catch_errors,
1510 we will get the wrong results if some are not closed now. */
1512 if (x_catching_errors ())
1518 c
.backlist
= backtrace_list
;
1519 c
.handlerlist
= handlerlist
;
1520 c
.lisp_eval_depth
= lisp_eval_depth
;
1521 c
.pdlcount
= SPECPDL_INDEX ();
1522 c
.poll_suppress_count
= poll_suppress_count
;
1523 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1524 c
.gcpro
= gcprolist
;
1525 c
.byte_stack
= byte_stack_list
;
1526 if (_setjmp (c
.jmp
))
1528 return (*hfun
) (c
.val
);
1532 h
.handler
= handlers
;
1534 h
.next
= handlerlist
;
1538 val
= (*bfun
) (arg
);
1540 handlerlist
= h
.next
;
1544 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1548 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1551 Lisp_Object handlers
,
1552 Lisp_Object (*hfun
) (Lisp_Object
))
1558 /* Since Fsignal will close off all calls to x_catch_errors,
1559 we will get the wrong results if some are not closed now. */
1561 if (x_catching_errors ())
1567 c
.backlist
= backtrace_list
;
1568 c
.handlerlist
= handlerlist
;
1569 c
.lisp_eval_depth
= lisp_eval_depth
;
1570 c
.pdlcount
= SPECPDL_INDEX ();
1571 c
.poll_suppress_count
= poll_suppress_count
;
1572 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1573 c
.gcpro
= gcprolist
;
1574 c
.byte_stack
= byte_stack_list
;
1575 if (_setjmp (c
.jmp
))
1577 return (*hfun
) (c
.val
);
1581 h
.handler
= handlers
;
1583 h
.next
= handlerlist
;
1587 val
= (*bfun
) (arg1
, arg2
);
1589 handlerlist
= h
.next
;
1593 /* Like internal_condition_case but call BFUN with NARGS as first,
1594 and ARGS as second argument. */
1597 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1600 Lisp_Object handlers
,
1601 Lisp_Object (*hfun
) (Lisp_Object
))
1607 /* Since Fsignal will close off all calls to x_catch_errors,
1608 we will get the wrong results if some are not closed now. */
1610 if (x_catching_errors ())
1616 c
.backlist
= backtrace_list
;
1617 c
.handlerlist
= handlerlist
;
1618 c
.lisp_eval_depth
= lisp_eval_depth
;
1619 c
.pdlcount
= SPECPDL_INDEX ();
1620 c
.poll_suppress_count
= poll_suppress_count
;
1621 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1622 c
.gcpro
= gcprolist
;
1623 c
.byte_stack
= byte_stack_list
;
1624 if (_setjmp (c
.jmp
))
1626 return (*hfun
) (c
.val
);
1630 h
.handler
= handlers
;
1632 h
.next
= handlerlist
;
1636 val
= (*bfun
) (nargs
, args
);
1638 handlerlist
= h
.next
;
1643 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1644 Lisp_Object
, Lisp_Object
);
1645 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1648 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1649 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1650 This function does not return.
1652 An error symbol is a symbol with an `error-conditions' property
1653 that is a list of condition names.
1654 A handler for any of those names will get to handle this signal.
1655 The symbol `error' should normally be one of them.
1657 DATA should be a list. Its elements are printed as part of the error message.
1658 See Info anchor `(elisp)Definition of signal' for some details on how this
1659 error message is constructed.
1660 If the signal is handled, DATA is made available to the handler.
1661 See also the function `condition-case'. */)
1662 (Lisp_Object error_symbol
, Lisp_Object data
)
1664 /* When memory is full, ERROR-SYMBOL is nil,
1665 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1666 That is a special case--don't do this in other situations. */
1667 Lisp_Object conditions
;
1669 Lisp_Object real_error_symbol
1670 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1671 register Lisp_Object clause
= Qnil
;
1673 struct backtrace
*bp
;
1675 immediate_quit
= handling_signal
= 0;
1677 if (gc_in_progress
|| waiting_for_input
)
1680 #if 0 /* rms: I don't know why this was here,
1681 but it is surely wrong for an error that is handled. */
1682 #ifdef HAVE_WINDOW_SYSTEM
1683 if (display_hourglass_p
)
1684 cancel_hourglass ();
1688 /* This hook is used by edebug. */
1689 if (! NILP (Vsignal_hook_function
)
1690 && ! NILP (error_symbol
))
1692 /* Edebug takes care of restoring these variables when it exits. */
1693 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1694 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1696 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1697 max_specpdl_size
= SPECPDL_INDEX () + 40;
1699 call2 (Vsignal_hook_function
, error_symbol
, data
);
1702 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1704 /* Remember from where signal was called. Skip over the frame for
1705 `signal' itself. If a frame for `error' follows, skip that,
1706 too. Don't do this when ERROR_SYMBOL is nil, because that
1707 is a memory-full error. */
1708 Vsignaling_function
= Qnil
;
1709 if (backtrace_list
&& !NILP (error_symbol
))
1711 bp
= backtrace_list
->next
;
1712 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1714 if (bp
&& bp
->function
)
1715 Vsignaling_function
= *bp
->function
;
1718 for (h
= handlerlist
; h
; h
= h
->next
)
1720 clause
= find_handler_clause (h
->handler
, conditions
,
1721 error_symbol
, data
);
1726 if (/* Don't run the debugger for a memory-full error.
1727 (There is no room in memory to do that!) */
1728 !NILP (error_symbol
)
1729 && (!NILP (Vdebug_on_signal
)
1730 /* If no handler is present now, try to run the debugger. */
1732 /* Special handler that means "print a message and run debugger
1734 || EQ (h
->handler
, Qerror
)))
1737 = maybe_call_debugger (conditions
, error_symbol
, data
);
1738 /* We can't return values to code which signaled an error, but we
1739 can continue code which has signaled a quit. */
1740 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1746 Lisp_Object unwind_data
1747 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1749 h
->chosen_clause
= clause
;
1750 unwind_to_catch (h
->tag
, unwind_data
);
1755 Fthrow (Qtop_level
, Qt
);
1758 if (! NILP (error_symbol
))
1759 data
= Fcons (error_symbol
, data
);
1761 string
= Ferror_message_string (data
);
1762 fatal ("%s", SDATA (string
));
1765 /* Internal version of Fsignal that never returns.
1766 Used for anything but Qquit (which can return from Fsignal). */
1769 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1771 Fsignal (error_symbol
, data
);
1775 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1778 xsignal0 (Lisp_Object error_symbol
)
1780 xsignal (error_symbol
, Qnil
);
1784 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1786 xsignal (error_symbol
, list1 (arg
));
1790 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1792 xsignal (error_symbol
, list2 (arg1
, arg2
));
1796 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1798 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1801 /* Signal `error' with message S, and additional arg ARG.
1802 If ARG is not a genuine list, make it a one-element list. */
1805 signal_error (const char *s
, Lisp_Object arg
)
1807 Lisp_Object tortoise
, hare
;
1809 hare
= tortoise
= arg
;
1810 while (CONSP (hare
))
1817 tortoise
= XCDR (tortoise
);
1819 if (EQ (hare
, tortoise
))
1824 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1826 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1830 /* Return nonzero if LIST is a non-nil atom or
1831 a list containing one of CONDITIONS. */
1834 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1841 while (CONSP (conditions
))
1843 Lisp_Object
this, tail
;
1844 this = XCAR (conditions
);
1845 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1846 if (EQ (XCAR (tail
), this))
1848 conditions
= XCDR (conditions
);
1853 /* Return 1 if an error with condition-symbols CONDITIONS,
1854 and described by SIGNAL-DATA, should skip the debugger
1855 according to debugger-ignored-errors. */
1858 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1861 int first_string
= 1;
1862 Lisp_Object error_message
;
1864 error_message
= Qnil
;
1865 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1867 if (STRINGP (XCAR (tail
)))
1871 error_message
= Ferror_message_string (data
);
1875 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1880 Lisp_Object contail
;
1882 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1883 if (EQ (XCAR (tail
), XCAR (contail
)))
1891 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1892 SIG and DATA describe the signal, as in find_handler_clause. */
1895 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1897 Lisp_Object combined_data
;
1899 combined_data
= Fcons (sig
, data
);
1902 /* Don't try to run the debugger with interrupts blocked.
1903 The editing loop would return anyway. */
1905 /* Does user want to enter debugger for this kind of error? */
1908 : wants_debugger (Vdebug_on_error
, conditions
))
1909 && ! skip_debugger (conditions
, combined_data
)
1910 /* RMS: What's this for? */
1911 && when_entered_debugger
< num_nonmacro_input_events
)
1913 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1920 /* Value of Qlambda means we have called debugger and user has continued.
1921 There are two ways to pass SIG and DATA:
1922 = SIG is the error symbol, and DATA is the rest of the data.
1923 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1924 This is for memory-full errors only.
1926 We need to increase max_specpdl_size temporarily around
1927 anything we do that can push on the specpdl, so as not to get
1928 a second error here in case we're handling specpdl overflow. */
1931 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1932 Lisp_Object sig
, Lisp_Object data
)
1934 register Lisp_Object h
;
1936 /* t is used by handlers for all conditions, set up by C code. */
1937 if (EQ (handlers
, Qt
))
1940 /* error is used similarly, but means print an error message
1941 and run the debugger if that is enabled. */
1942 if (EQ (handlers
, Qerror
))
1945 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1947 Lisp_Object handler
= XCAR (h
);
1948 Lisp_Object condit
, tem
;
1950 if (!CONSP (handler
))
1952 condit
= XCAR (handler
);
1953 /* Handle a single condition name in handler HANDLER. */
1954 if (SYMBOLP (condit
))
1956 tem
= Fmemq (Fcar (handler
), conditions
);
1960 /* Handle a list of condition names in handler HANDLER. */
1961 else if (CONSP (condit
))
1964 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1966 tem
= Fmemq (XCAR (tail
), conditions
);
1977 /* Dump an error message; called like vprintf. */
1979 verror (const char *m
, va_list ap
)
1982 size_t size
= sizeof buf
;
1983 size_t size_max
= STRING_BYTES_BOUND
+ 1;
1984 size_t mlen
= strlen (m
);
1992 va_copy (ap_copy
, ap
);
1993 used
= doprnt (buffer
, size
, m
, m
+ mlen
, ap_copy
);
1996 /* Note: the -1 below is because `doprnt' returns the number of bytes
1997 excluding the terminating null byte, and it always terminates with a
1998 null byte, even when producing a truncated message. */
1999 if (used
< size
- 1)
2001 if (size
<= size_max
/ 2)
2003 else if (size
< size_max
)
2006 break; /* and leave the message truncated */
2010 buffer
= (char *) xmalloc (size
);
2013 string
= make_string (buffer
, used
);
2017 xsignal1 (Qerror
, string
);
2021 /* Dump an error message; called like printf. */
2025 error (const char *m
, ...)
2033 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
2034 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
2035 This means it contains a description for how to read arguments to give it.
2036 The value is nil for an invalid function or a symbol with no function
2039 Interactively callable functions include strings and vectors (treated
2040 as keyboard macros), lambda-expressions that contain a top-level call
2041 to `interactive', autoload definitions made by `autoload' with non-nil
2042 fourth argument, and some of the built-in functions of Lisp.
2044 Also, a symbol satisfies `commandp' if its function definition does so.
2046 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2047 then strings and vectors are not accepted. */)
2048 (Lisp_Object function
, Lisp_Object for_call_interactively
)
2050 register Lisp_Object fun
;
2051 register Lisp_Object funcar
;
2052 Lisp_Object if_prop
= Qnil
;
2056 fun
= indirect_function (fun
); /* Check cycles. */
2057 if (NILP (fun
) || EQ (fun
, Qunbound
))
2060 /* Check an `interactive-form' property if present, analogous to the
2061 function-documentation property. */
2063 while (SYMBOLP (fun
))
2065 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2068 fun
= Fsymbol_function (fun
);
2071 /* Emacs primitives are interactive if their DEFUN specifies an
2072 interactive spec. */
2074 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2076 /* Bytecode objects are interactive if they are long enough to
2077 have an element whose index is COMPILED_INTERACTIVE, which is
2078 where the interactive spec is stored. */
2079 else if (COMPILEDP (fun
))
2080 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2083 /* Strings and vectors are keyboard macros. */
2084 if (STRINGP (fun
) || VECTORP (fun
))
2085 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2087 /* Lists may represent commands. */
2090 funcar
= XCAR (fun
);
2091 if (EQ (funcar
, Qclosure
))
2092 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
2094 else if (EQ (funcar
, Qlambda
))
2095 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2096 else if (EQ (funcar
, Qautoload
))
2097 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2102 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2103 doc
: /* Define FUNCTION to autoload from FILE.
2104 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2105 Third arg DOCSTRING is documentation for the function.
2106 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2107 Fifth arg TYPE indicates the type of the object:
2108 nil or omitted says FUNCTION is a function,
2109 `keymap' says FUNCTION is really a keymap, and
2110 `macro' or t says FUNCTION is really a macro.
2111 Third through fifth args give info about the real definition.
2112 They default to nil.
2113 If FUNCTION is already defined other than as an autoload,
2114 this does nothing and returns nil. */)
2115 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2117 CHECK_SYMBOL (function
);
2118 CHECK_STRING (file
);
2120 /* If function is defined and not as an autoload, don't override. */
2121 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2122 && !(CONSP (XSYMBOL (function
)->function
)
2123 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2126 if (NILP (Vpurify_flag
))
2127 /* Only add entries after dumping, because the ones before are
2128 not useful and else we get loads of them from the loaddefs.el. */
2129 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2131 /* We don't want the docstring in purespace (instead,
2132 Snarf-documentation should (hopefully) overwrite it).
2133 We used to use 0 here, but that leads to accidental sharing in
2134 purecopy's hash-consing, so we use a (hopefully) unique integer
2136 docstring
= make_number (XPNTR (function
));
2137 return Ffset (function
,
2138 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2139 interactive
, type
)));
2143 un_autoload (Lisp_Object oldqueue
)
2145 register Lisp_Object queue
, first
, second
;
2147 /* Queue to unwind is current value of Vautoload_queue.
2148 oldqueue is the shadowed value to leave in Vautoload_queue. */
2149 queue
= Vautoload_queue
;
2150 Vautoload_queue
= oldqueue
;
2151 while (CONSP (queue
))
2153 first
= XCAR (queue
);
2154 second
= Fcdr (first
);
2155 first
= Fcar (first
);
2156 if (EQ (first
, make_number (0)))
2159 Ffset (first
, second
);
2160 queue
= XCDR (queue
);
2165 /* Load an autoloaded function.
2166 FUNNAME is the symbol which is the function's name.
2167 FUNDEF is the autoload definition (a list). */
2170 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2172 int count
= SPECPDL_INDEX ();
2174 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2176 /* This is to make sure that loadup.el gives a clear picture
2177 of what files are preloaded and when. */
2178 if (! NILP (Vpurify_flag
))
2179 error ("Attempt to autoload %s while preparing to dump",
2180 SDATA (SYMBOL_NAME (funname
)));
2183 CHECK_SYMBOL (funname
);
2184 GCPRO3 (fun
, funname
, fundef
);
2186 /* Preserve the match data. */
2187 record_unwind_save_match_data ();
2189 /* If autoloading gets an error (which includes the error of failing
2190 to define the function being called), we use Vautoload_queue
2191 to undo function definitions and `provide' calls made by
2192 the function. We do this in the specific case of autoloading
2193 because autoloading is not an explicit request "load this file",
2194 but rather a request to "call this function".
2196 The value saved here is to be restored into Vautoload_queue. */
2197 record_unwind_protect (un_autoload
, Vautoload_queue
);
2198 Vautoload_queue
= Qt
;
2199 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2201 /* Once loading finishes, don't undo it. */
2202 Vautoload_queue
= Qt
;
2203 unbind_to (count
, Qnil
);
2205 fun
= Findirect_function (fun
, Qnil
);
2207 if (!NILP (Fequal (fun
, fundef
)))
2208 error ("Autoloading failed to define function %s",
2209 SDATA (SYMBOL_NAME (funname
)));
2214 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2215 doc
: /* Evaluate FORM and return its value.
2216 If LEXICAL is t, evaluate using lexical scoping. */)
2217 (Lisp_Object form
, Lisp_Object lexical
)
2219 int count
= SPECPDL_INDEX ();
2220 specbind (Qinternal_interpreter_environment
,
2221 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2222 return unbind_to (count
, eval_sub (form
));
2225 /* Eval a sub-expression of the current expression (i.e. in the same
2228 eval_sub (Lisp_Object form
)
2230 Lisp_Object fun
, val
, original_fun
, original_args
;
2232 struct backtrace backtrace
;
2233 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2235 if (handling_signal
)
2240 /* Look up its binding in the lexical environment.
2241 We do not pay attention to the declared_special flag here, since we
2242 already did that when let-binding the variable. */
2243 Lisp_Object lex_binding
2244 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2245 ? Fassq (form
, Vinternal_interpreter_environment
)
2247 if (CONSP (lex_binding
))
2248 return XCDR (lex_binding
);
2250 return Fsymbol_value (form
);
2257 if ((consing_since_gc
> gc_cons_threshold
2258 && consing_since_gc
> gc_relative_threshold
)
2260 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2263 Fgarbage_collect ();
2267 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2269 if (max_lisp_eval_depth
< 100)
2270 max_lisp_eval_depth
= 100;
2271 if (lisp_eval_depth
> max_lisp_eval_depth
)
2272 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2275 original_fun
= Fcar (form
);
2276 original_args
= Fcdr (form
);
2278 backtrace
.next
= backtrace_list
;
2279 backtrace_list
= &backtrace
;
2280 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2281 backtrace
.args
= &original_args
;
2282 backtrace
.nargs
= UNEVALLED
;
2283 backtrace
.debug_on_exit
= 0;
2285 if (debug_on_next_call
)
2286 do_debug_on_call (Qt
);
2288 /* At this point, only original_fun and original_args
2289 have values that will be used below. */
2292 /* Optimize for no indirection. */
2294 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2295 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2296 fun
= indirect_function (fun
);
2300 Lisp_Object numargs
;
2301 Lisp_Object argvals
[8];
2302 Lisp_Object args_left
;
2303 register int i
, maxargs
;
2305 args_left
= original_args
;
2306 numargs
= Flength (args_left
);
2310 if (XINT (numargs
) < XSUBR (fun
)->min_args
2311 || (XSUBR (fun
)->max_args
>= 0
2312 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2313 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2315 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2316 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2317 else if (XSUBR (fun
)->max_args
== MANY
)
2319 /* Pass a vector of evaluated arguments. */
2321 ptrdiff_t argnum
= 0;
2324 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2326 GCPRO3 (args_left
, fun
, fun
);
2330 while (!NILP (args_left
))
2332 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2333 args_left
= Fcdr (args_left
);
2334 gcpro3
.nvars
= argnum
;
2337 backtrace
.args
= vals
;
2338 backtrace
.nargs
= XINT (numargs
);
2340 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2346 GCPRO3 (args_left
, fun
, fun
);
2347 gcpro3
.var
= argvals
;
2350 maxargs
= XSUBR (fun
)->max_args
;
2351 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2353 argvals
[i
] = eval_sub (Fcar (args_left
));
2359 backtrace
.args
= argvals
;
2360 backtrace
.nargs
= XINT (numargs
);
2365 val
= (XSUBR (fun
)->function
.a0 ());
2368 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2371 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2374 val
= (XSUBR (fun
)->function
.a3
2375 (argvals
[0], argvals
[1], argvals
[2]));
2378 val
= (XSUBR (fun
)->function
.a4
2379 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2382 val
= (XSUBR (fun
)->function
.a5
2383 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2387 val
= (XSUBR (fun
)->function
.a6
2388 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2389 argvals
[4], argvals
[5]));
2392 val
= (XSUBR (fun
)->function
.a7
2393 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2394 argvals
[4], argvals
[5], argvals
[6]));
2398 val
= (XSUBR (fun
)->function
.a8
2399 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2400 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2404 /* Someone has created a subr that takes more arguments than
2405 is supported by this code. We need to either rewrite the
2406 subr to use a different argument protocol, or add more
2407 cases to this switch. */
2412 else if (COMPILEDP (fun
))
2413 val
= apply_lambda (fun
, original_args
);
2416 if (EQ (fun
, Qunbound
))
2417 xsignal1 (Qvoid_function
, original_fun
);
2419 xsignal1 (Qinvalid_function
, original_fun
);
2420 funcar
= XCAR (fun
);
2421 if (!SYMBOLP (funcar
))
2422 xsignal1 (Qinvalid_function
, original_fun
);
2423 if (EQ (funcar
, Qautoload
))
2425 do_autoload (fun
, original_fun
);
2428 if (EQ (funcar
, Qmacro
))
2429 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2430 else if (EQ (funcar
, Qlambda
)
2431 || EQ (funcar
, Qclosure
))
2432 val
= apply_lambda (fun
, original_args
);
2434 xsignal1 (Qinvalid_function
, original_fun
);
2439 if (backtrace
.debug_on_exit
)
2440 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2441 backtrace_list
= backtrace
.next
;
2446 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2447 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2448 Then return the value FUNCTION returns.
2449 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2450 usage: (apply FUNCTION &rest ARGUMENTS) */)
2451 (ptrdiff_t nargs
, Lisp_Object
*args
)
2453 ptrdiff_t i
, numargs
;
2454 register Lisp_Object spread_arg
;
2455 register Lisp_Object
*funcall_args
;
2456 Lisp_Object fun
, retval
;
2457 struct gcpro gcpro1
;
2462 spread_arg
= args
[nargs
- 1];
2463 CHECK_LIST (spread_arg
);
2465 numargs
= XINT (Flength (spread_arg
));
2468 return Ffuncall (nargs
- 1, args
);
2469 else if (numargs
== 1)
2471 args
[nargs
- 1] = XCAR (spread_arg
);
2472 return Ffuncall (nargs
, args
);
2475 numargs
+= nargs
- 2;
2477 /* Optimize for no indirection. */
2478 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2479 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2480 fun
= indirect_function (fun
);
2481 if (EQ (fun
, Qunbound
))
2483 /* Let funcall get the error. */
2490 if (numargs
< XSUBR (fun
)->min_args
2491 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2492 goto funcall
; /* Let funcall get the error. */
2493 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2495 /* Avoid making funcall cons up a yet another new vector of arguments
2496 by explicitly supplying nil's for optional values. */
2497 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2498 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2499 funcall_args
[++i
] = Qnil
;
2500 GCPRO1 (*funcall_args
);
2501 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2505 /* We add 1 to numargs because funcall_args includes the
2506 function itself as well as its arguments. */
2509 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2510 GCPRO1 (*funcall_args
);
2511 gcpro1
.nvars
= 1 + numargs
;
2514 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2515 /* Spread the last arg we got. Its first element goes in
2516 the slot that it used to occupy, hence this value of I. */
2518 while (!NILP (spread_arg
))
2520 funcall_args
[i
++] = XCAR (spread_arg
);
2521 spread_arg
= XCDR (spread_arg
);
2524 /* By convention, the caller needs to gcpro Ffuncall's args. */
2525 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2532 /* Run hook variables in various ways. */
2535 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2537 Ffuncall (nargs
, args
);
2541 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2542 doc
: /* Run each hook in HOOKS.
2543 Each argument should be a symbol, a hook variable.
2544 These symbols are processed in the order specified.
2545 If a hook symbol has a non-nil value, that value may be a function
2546 or a list of functions to be called to run the hook.
2547 If the value is a function, it is called with no arguments.
2548 If it is a list, the elements are called, in order, with no arguments.
2550 Major modes should not use this function directly to run their mode
2551 hook; they should use `run-mode-hooks' instead.
2553 Do not use `make-local-variable' to make a hook variable buffer-local.
2554 Instead, use `add-hook' and specify t for the LOCAL argument.
2555 usage: (run-hooks &rest HOOKS) */)
2556 (ptrdiff_t nargs
, Lisp_Object
*args
)
2558 Lisp_Object hook
[1];
2561 for (i
= 0; i
< nargs
; i
++)
2564 run_hook_with_args (1, hook
, funcall_nil
);
2570 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2571 Srun_hook_with_args
, 1, MANY
, 0,
2572 doc
: /* Run HOOK with the specified arguments ARGS.
2573 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2574 value, that value may be a function or a list of functions to be
2575 called to run the hook. If the value is a function, it is called with
2576 the given arguments and its return value is returned. If it is a list
2577 of functions, those functions are called, in order,
2578 with the given arguments ARGS.
2579 It is best not to depend on the value returned by `run-hook-with-args',
2582 Do not use `make-local-variable' to make a hook variable buffer-local.
2583 Instead, use `add-hook' and specify t for the LOCAL argument.
2584 usage: (run-hook-with-args HOOK &rest ARGS) */)
2585 (ptrdiff_t nargs
, Lisp_Object
*args
)
2587 return run_hook_with_args (nargs
, args
, funcall_nil
);
2590 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2591 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2592 doc
: /* Run HOOK with the specified arguments ARGS.
2593 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2594 value, that value may be a function or a list of functions to be
2595 called to run the hook. If the value is a function, it is called with
2596 the given arguments and its return value is returned.
2597 If it is a list of functions, those functions are called, in order,
2598 with the given arguments ARGS, until one of them
2599 returns a non-nil value. Then we return that value.
2600 However, if they all return nil, we return nil.
2602 Do not use `make-local-variable' to make a hook variable buffer-local.
2603 Instead, use `add-hook' and specify t for the LOCAL argument.
2604 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2605 (ptrdiff_t nargs
, Lisp_Object
*args
)
2607 return run_hook_with_args (nargs
, args
, Ffuncall
);
2611 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2613 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2616 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2617 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2618 doc
: /* Run HOOK with the specified arguments ARGS.
2619 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2620 value, that value may be a function or a list of functions to be
2621 called to run the hook. If the value is a function, it is called with
2622 the given arguments and its return value is returned.
2623 If it is a list of functions, those functions are called, in order,
2624 with the given arguments ARGS, until one of them returns nil.
2625 Then we return nil. However, if they all return non-nil, we return non-nil.
2627 Do not use `make-local-variable' to make a hook variable buffer-local.
2628 Instead, use `add-hook' and specify t for the LOCAL argument.
2629 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2630 (ptrdiff_t nargs
, Lisp_Object
*args
)
2632 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2636 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2638 Lisp_Object tmp
= args
[0], ret
;
2641 ret
= Ffuncall (nargs
, args
);
2647 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2648 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2649 I.e. instead of calling each function FUN directly with arguments ARGS,
2650 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2651 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2652 aborts and returns that value.
2653 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2654 (ptrdiff_t nargs
, Lisp_Object
*args
)
2656 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2659 /* ARGS[0] should be a hook symbol.
2660 Call each of the functions in the hook value, passing each of them
2661 as arguments all the rest of ARGS (all NARGS - 1 elements).
2662 FUNCALL specifies how to call each function on the hook.
2663 The caller (or its caller, etc) must gcpro all of ARGS,
2664 except that it isn't necessary to gcpro ARGS[0]. */
2667 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2668 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2670 Lisp_Object sym
, val
, ret
= Qnil
;
2671 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2673 /* If we are dying or still initializing,
2674 don't do anything--it would probably crash if we tried. */
2675 if (NILP (Vrun_hooks
))
2679 val
= find_symbol_value (sym
);
2681 if (EQ (val
, Qunbound
) || NILP (val
))
2683 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2686 return funcall (nargs
, args
);
2690 Lisp_Object global_vals
= Qnil
;
2691 GCPRO3 (sym
, val
, global_vals
);
2694 CONSP (val
) && NILP (ret
);
2697 if (EQ (XCAR (val
), Qt
))
2699 /* t indicates this hook has a local binding;
2700 it means to run the global binding too. */
2701 global_vals
= Fdefault_value (sym
);
2702 if (NILP (global_vals
)) continue;
2704 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2706 args
[0] = global_vals
;
2707 ret
= funcall (nargs
, args
);
2712 CONSP (global_vals
) && NILP (ret
);
2713 global_vals
= XCDR (global_vals
))
2715 args
[0] = XCAR (global_vals
);
2716 /* In a global value, t should not occur. If it does, we
2717 must ignore it to avoid an endless loop. */
2718 if (!EQ (args
[0], Qt
))
2719 ret
= funcall (nargs
, args
);
2725 args
[0] = XCAR (val
);
2726 ret
= funcall (nargs
, args
);
2735 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2738 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2740 Lisp_Object temp
[3];
2745 Frun_hook_with_args (3, temp
);
2748 /* Apply fn to arg. */
2750 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2752 struct gcpro gcpro1
;
2756 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2759 Lisp_Object args
[2];
2763 RETURN_UNGCPRO (Fapply (2, args
));
2767 /* Call function fn on no arguments. */
2769 call0 (Lisp_Object fn
)
2771 struct gcpro gcpro1
;
2774 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2777 /* Call function fn with 1 argument arg1. */
2780 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2782 struct gcpro gcpro1
;
2783 Lisp_Object args
[2];
2789 RETURN_UNGCPRO (Ffuncall (2, args
));
2792 /* Call function fn with 2 arguments arg1, arg2. */
2795 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2797 struct gcpro gcpro1
;
2798 Lisp_Object args
[3];
2804 RETURN_UNGCPRO (Ffuncall (3, args
));
2807 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2810 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2812 struct gcpro gcpro1
;
2813 Lisp_Object args
[4];
2820 RETURN_UNGCPRO (Ffuncall (4, args
));
2823 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2826 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2829 struct gcpro gcpro1
;
2830 Lisp_Object args
[5];
2838 RETURN_UNGCPRO (Ffuncall (5, args
));
2841 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2844 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2845 Lisp_Object arg4
, Lisp_Object arg5
)
2847 struct gcpro gcpro1
;
2848 Lisp_Object args
[6];
2857 RETURN_UNGCPRO (Ffuncall (6, args
));
2860 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2863 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2864 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2866 struct gcpro gcpro1
;
2867 Lisp_Object args
[7];
2877 RETURN_UNGCPRO (Ffuncall (7, args
));
2880 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2883 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2884 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2886 struct gcpro gcpro1
;
2887 Lisp_Object args
[8];
2898 RETURN_UNGCPRO (Ffuncall (8, args
));
2901 /* The caller should GCPRO all the elements of ARGS. */
2903 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2904 doc
: /* Non-nil if OBJECT is a function. */)
2905 (Lisp_Object object
)
2907 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2909 object
= Findirect_function (object
, Qt
);
2911 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2913 /* Autoloaded symbols are functions, except if they load
2914 macros or keymaps. */
2916 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2917 object
= XCDR (object
);
2919 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2924 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2925 else if (COMPILEDP (object
))
2927 else if (CONSP (object
))
2929 Lisp_Object car
= XCAR (object
);
2930 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2936 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2937 doc
: /* Call first argument as a function, passing remaining arguments to it.
2938 Return the value that function returns.
2939 Thus, (funcall 'cons 'x 'y) returns (x . y).
2940 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2941 (ptrdiff_t nargs
, Lisp_Object
*args
)
2943 Lisp_Object fun
, original_fun
;
2945 ptrdiff_t numargs
= nargs
- 1;
2946 Lisp_Object lisp_numargs
;
2948 struct backtrace backtrace
;
2949 register Lisp_Object
*internal_args
;
2953 if ((consing_since_gc
> gc_cons_threshold
2954 && consing_since_gc
> gc_relative_threshold
)
2956 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2957 Fgarbage_collect ();
2959 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2961 if (max_lisp_eval_depth
< 100)
2962 max_lisp_eval_depth
= 100;
2963 if (lisp_eval_depth
> max_lisp_eval_depth
)
2964 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2967 backtrace
.next
= backtrace_list
;
2968 backtrace_list
= &backtrace
;
2969 backtrace
.function
= &args
[0];
2970 backtrace
.args
= &args
[1];
2971 backtrace
.nargs
= nargs
- 1;
2972 backtrace
.debug_on_exit
= 0;
2974 if (debug_on_next_call
)
2975 do_debug_on_call (Qlambda
);
2979 original_fun
= args
[0];
2983 /* Optimize for no indirection. */
2985 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2986 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2987 fun
= indirect_function (fun
);
2991 if (numargs
< XSUBR (fun
)->min_args
2992 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2994 XSETFASTINT (lisp_numargs
, numargs
);
2995 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2998 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2999 xsignal1 (Qinvalid_function
, original_fun
);
3001 else if (XSUBR (fun
)->max_args
== MANY
)
3002 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
3005 if (XSUBR (fun
)->max_args
> numargs
)
3007 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
3008 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
3009 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
3010 internal_args
[i
] = Qnil
;
3013 internal_args
= args
+ 1;
3014 switch (XSUBR (fun
)->max_args
)
3017 val
= (XSUBR (fun
)->function
.a0 ());
3020 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
3023 val
= (XSUBR (fun
)->function
.a2
3024 (internal_args
[0], internal_args
[1]));
3027 val
= (XSUBR (fun
)->function
.a3
3028 (internal_args
[0], internal_args
[1], internal_args
[2]));
3031 val
= (XSUBR (fun
)->function
.a4
3032 (internal_args
[0], internal_args
[1], internal_args
[2],
3036 val
= (XSUBR (fun
)->function
.a5
3037 (internal_args
[0], internal_args
[1], internal_args
[2],
3038 internal_args
[3], internal_args
[4]));
3041 val
= (XSUBR (fun
)->function
.a6
3042 (internal_args
[0], internal_args
[1], internal_args
[2],
3043 internal_args
[3], internal_args
[4], internal_args
[5]));
3046 val
= (XSUBR (fun
)->function
.a7
3047 (internal_args
[0], internal_args
[1], internal_args
[2],
3048 internal_args
[3], internal_args
[4], internal_args
[5],
3053 val
= (XSUBR (fun
)->function
.a8
3054 (internal_args
[0], internal_args
[1], internal_args
[2],
3055 internal_args
[3], internal_args
[4], internal_args
[5],
3056 internal_args
[6], internal_args
[7]));
3061 /* If a subr takes more than 8 arguments without using MANY
3062 or UNEVALLED, we need to extend this function to support it.
3063 Until this is done, there is no way to call the function. */
3068 else if (COMPILEDP (fun
))
3069 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3072 if (EQ (fun
, Qunbound
))
3073 xsignal1 (Qvoid_function
, original_fun
);
3075 xsignal1 (Qinvalid_function
, original_fun
);
3076 funcar
= XCAR (fun
);
3077 if (!SYMBOLP (funcar
))
3078 xsignal1 (Qinvalid_function
, original_fun
);
3079 if (EQ (funcar
, Qlambda
)
3080 || EQ (funcar
, Qclosure
))
3081 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3082 else if (EQ (funcar
, Qautoload
))
3084 do_autoload (fun
, original_fun
);
3089 xsignal1 (Qinvalid_function
, original_fun
);
3093 if (backtrace
.debug_on_exit
)
3094 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3095 backtrace_list
= backtrace
.next
;
3100 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
3102 Lisp_Object args_left
;
3103 ptrdiff_t i
, numargs
;
3104 register Lisp_Object
*arg_vector
;
3105 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3106 register Lisp_Object tem
;
3109 numargs
= XFASTINT (Flength (args
));
3110 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
3113 GCPRO3 (*arg_vector
, args_left
, fun
);
3116 for (i
= 0; i
< numargs
; )
3118 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3119 tem
= eval_sub (tem
);
3120 arg_vector
[i
++] = tem
;
3126 backtrace_list
->args
= arg_vector
;
3127 backtrace_list
->nargs
= i
;
3128 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
3130 /* Do the debug-on-exit now, while arg_vector still exists. */
3131 if (backtrace_list
->debug_on_exit
)
3132 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3133 /* Don't do it again when we return to eval. */
3134 backtrace_list
->debug_on_exit
= 0;
3139 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3140 and return the result of evaluation.
3141 FUN must be either a lambda-expression or a compiled-code object. */
3144 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
3145 register Lisp_Object
*arg_vector
)
3147 Lisp_Object val
, syms_left
, next
, lexenv
;
3148 int count
= SPECPDL_INDEX ();
3154 if (EQ (XCAR (fun
), Qclosure
))
3156 fun
= XCDR (fun
); /* Drop `closure'. */
3157 lexenv
= XCAR (fun
);
3158 CHECK_LIST_CONS (fun
, fun
);
3162 syms_left
= XCDR (fun
);
3163 if (CONSP (syms_left
))
3164 syms_left
= XCAR (syms_left
);
3166 xsignal1 (Qinvalid_function
, fun
);
3168 else if (COMPILEDP (fun
))
3170 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3171 if (INTEGERP (syms_left
))
3172 /* A byte-code object with a non-nil `push args' slot means we
3173 shouldn't bind any arguments, instead just call the byte-code
3174 interpreter directly; it will push arguments as necessary.
3176 Byte-code objects with either a non-existant, or a nil value for
3177 the `push args' slot (the default), have dynamically-bound
3178 arguments, and use the argument-binding code below instead (as do
3179 all interpreted functions, even lexically bound ones). */
3181 /* If we have not actually read the bytecode string
3182 and constants vector yet, fetch them from the file. */
3183 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3184 Ffetch_bytecode (fun
);
3185 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3186 AREF (fun
, COMPILED_CONSTANTS
),
3187 AREF (fun
, COMPILED_STACK_DEPTH
),
3196 i
= optional
= rest
= 0;
3197 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3201 next
= XCAR (syms_left
);
3202 if (!SYMBOLP (next
))
3203 xsignal1 (Qinvalid_function
, fun
);
3205 if (EQ (next
, Qand_rest
))
3207 else if (EQ (next
, Qand_optional
))
3214 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3218 arg
= arg_vector
[i
++];
3220 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3224 /* Bind the argument. */
3225 if (!NILP (lexenv
) && SYMBOLP (next
))
3226 /* Lexically bind NEXT by adding it to the lexenv alist. */
3227 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3229 /* Dynamically bind NEXT. */
3230 specbind (next
, arg
);
3234 if (!NILP (syms_left
))
3235 xsignal1 (Qinvalid_function
, fun
);
3237 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3239 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3240 /* Instantiate a new lexical environment. */
3241 specbind (Qinternal_interpreter_environment
, lexenv
);
3244 val
= Fprogn (XCDR (XCDR (fun
)));
3247 /* If we have not actually read the bytecode string
3248 and constants vector yet, fetch them from the file. */
3249 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3250 Ffetch_bytecode (fun
);
3251 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3252 AREF (fun
, COMPILED_CONSTANTS
),
3253 AREF (fun
, COMPILED_STACK_DEPTH
),
3257 return unbind_to (count
, val
);
3260 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3262 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3263 (Lisp_Object object
)
3267 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3269 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3272 tem
= AREF (object
, COMPILED_BYTECODE
);
3273 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3274 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3276 error ("Invalid byte code");
3278 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3279 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3287 register int count
= SPECPDL_INDEX ();
3288 if (specpdl_size
>= max_specpdl_size
)
3290 if (max_specpdl_size
< 400)
3291 max_specpdl_size
= 400;
3292 if (specpdl_size
>= max_specpdl_size
)
3293 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3296 if (specpdl_size
> max_specpdl_size
)
3297 specpdl_size
= max_specpdl_size
;
3298 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3299 specpdl_ptr
= specpdl
+ count
;
3302 /* `specpdl_ptr->symbol' is a field which describes which variable is
3303 let-bound, so it can be properly undone when we unbind_to.
3304 It can have the following two shapes:
3305 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3306 a symbol that is not buffer-local (at least at the time
3307 the let binding started). Note also that it should not be
3308 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3310 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3311 variable SYMBOL which can be buffer-local. WHERE tells us
3312 which buffer is affected (or nil if the let-binding affects the
3313 global value of the variable) and BUFFER tells us which buffer was
3314 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3315 BUFFER did not yet have a buffer-local value). */
3318 specbind (Lisp_Object symbol
, Lisp_Object value
)
3320 struct Lisp_Symbol
*sym
;
3322 eassert (!handling_signal
);
3324 CHECK_SYMBOL (symbol
);
3325 sym
= XSYMBOL (symbol
);
3326 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3330 switch (sym
->redirect
)
3332 case SYMBOL_VARALIAS
:
3333 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3334 case SYMBOL_PLAINVAL
:
3335 /* The most common case is that of a non-constant symbol with a
3336 trivial value. Make that as fast as we can. */
3337 specpdl_ptr
->symbol
= symbol
;
3338 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3339 specpdl_ptr
->func
= NULL
;
3342 SET_SYMBOL_VAL (sym
, value
);
3344 set_internal (symbol
, value
, Qnil
, 1);
3346 case SYMBOL_LOCALIZED
:
3347 if (SYMBOL_BLV (sym
)->frame_local
)
3348 error ("Frame-local vars cannot be let-bound");
3349 case SYMBOL_FORWARDED
:
3351 Lisp_Object ovalue
= find_symbol_value (symbol
);
3352 specpdl_ptr
->func
= 0;
3353 specpdl_ptr
->old_value
= ovalue
;
3355 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3356 || (EQ (SYMBOL_BLV (sym
)->where
,
3357 SYMBOL_BLV (sym
)->frame_local
?
3358 Fselected_frame () : Fcurrent_buffer ())));
3360 if (sym
->redirect
== SYMBOL_LOCALIZED
3361 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3363 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3365 /* For a local variable, record both the symbol and which
3366 buffer's or frame's value we are saving. */
3367 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3369 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3370 || (BLV_FOUND (SYMBOL_BLV (sym
))
3371 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3374 else if (sym
->redirect
== SYMBOL_LOCALIZED
3375 && BLV_FOUND (SYMBOL_BLV (sym
)))
3376 where
= SYMBOL_BLV (sym
)->where
;
3380 /* We're not using the `unused' slot in the specbinding
3381 structure because this would mean we have to do more
3382 work for simple variables. */
3383 /* FIXME: The third value `current_buffer' is only used in
3384 let_shadows_buffer_binding_p which is itself only used
3385 in set_internal for local_if_set. */
3386 eassert (NILP (where
) || EQ (where
, cur_buf
));
3387 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3389 /* If SYMBOL is a per-buffer variable which doesn't have a
3390 buffer-local value here, make the `let' change the global
3391 value by changing the value of SYMBOL in all buffers not
3392 having their own value. This is consistent with what
3393 happens with other buffer-local variables. */
3395 && sym
->redirect
== SYMBOL_FORWARDED
)
3397 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3399 Fset_default (symbol
, value
);
3404 specpdl_ptr
->symbol
= symbol
;
3407 set_internal (symbol
, value
, Qnil
, 1);
3415 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3417 eassert (!handling_signal
);
3419 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3421 specpdl_ptr
->func
= function
;
3422 specpdl_ptr
->symbol
= Qnil
;
3423 specpdl_ptr
->old_value
= arg
;
3428 unbind_to (int count
, Lisp_Object value
)
3430 Lisp_Object quitf
= Vquit_flag
;
3431 struct gcpro gcpro1
, gcpro2
;
3433 GCPRO2 (value
, quitf
);
3436 while (specpdl_ptr
!= specpdl
+ count
)
3438 /* Copy the binding, and decrement specpdl_ptr, before we do
3439 the work to unbind it. We decrement first
3440 so that an error in unbinding won't try to unbind
3441 the same entry again, and we copy the binding first
3442 in case more bindings are made during some of the code we run. */
3444 struct specbinding this_binding
;
3445 this_binding
= *--specpdl_ptr
;
3447 if (this_binding
.func
!= 0)
3448 (*this_binding
.func
) (this_binding
.old_value
);
3449 /* If the symbol is a list, it is really (SYMBOL WHERE
3450 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3451 frame. If WHERE is a buffer or frame, this indicates we
3452 bound a variable that had a buffer-local or frame-local
3453 binding. WHERE nil means that the variable had the default
3454 value when it was bound. CURRENT-BUFFER is the buffer that
3455 was current when the variable was bound. */
3456 else if (CONSP (this_binding
.symbol
))
3458 Lisp_Object symbol
, where
;
3460 symbol
= XCAR (this_binding
.symbol
);
3461 where
= XCAR (XCDR (this_binding
.symbol
));
3464 Fset_default (symbol
, this_binding
.old_value
);
3465 /* If `where' is non-nil, reset the value in the appropriate
3466 local binding, but only if that binding still exists. */
3467 else if (BUFFERP (where
)
3468 ? !NILP (Flocal_variable_p (symbol
, where
))
3469 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3470 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3472 /* If variable has a trivial value (no forwarding), we can
3473 just set it. No need to check for constant symbols here,
3474 since that was already done by specbind. */
3475 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3476 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3477 this_binding
.old_value
);
3479 /* NOTE: we only ever come here if make_local_foo was used for
3480 the first time on this var within this let. */
3481 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3484 if (NILP (Vquit_flag
) && !NILP (quitf
))
3491 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3492 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3493 A special variable is one that will be bound dynamically, even in a
3494 context where binding is lexical by default. */)
3495 (Lisp_Object symbol
)
3497 CHECK_SYMBOL (symbol
);
3498 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3502 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3503 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3504 The debugger is entered when that frame exits, if the flag is non-nil. */)
3505 (Lisp_Object level
, Lisp_Object flag
)
3507 register struct backtrace
*backlist
= backtrace_list
;
3510 CHECK_NUMBER (level
);
3512 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3514 backlist
= backlist
->next
;
3518 backlist
->debug_on_exit
= !NILP (flag
);
3523 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3524 doc
: /* Print a trace of Lisp function calls currently active.
3525 Output stream used is value of `standard-output'. */)
3528 register struct backtrace
*backlist
= backtrace_list
;
3531 struct gcpro gcpro1
;
3532 Lisp_Object old_print_level
= Vprint_level
;
3534 if (NILP (Vprint_level
))
3535 XSETFASTINT (Vprint_level
, 8);
3542 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3543 if (backlist
->nargs
== UNEVALLED
)
3545 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3546 write_string ("\n", -1);
3550 tem
= *backlist
->function
;
3551 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3552 write_string ("(", -1);
3553 if (backlist
->nargs
== MANY
)
3554 { /* FIXME: Can this happen? */
3556 for (tail
= *backlist
->args
, i
= 0;
3558 tail
= Fcdr (tail
), i
= 1)
3560 if (i
) write_string (" ", -1);
3561 Fprin1 (Fcar (tail
), Qnil
);
3567 for (i
= 0; i
< backlist
->nargs
; i
++)
3569 if (i
) write_string (" ", -1);
3570 Fprin1 (backlist
->args
[i
], Qnil
);
3573 write_string (")\n", -1);
3575 backlist
= backlist
->next
;
3578 Vprint_level
= old_print_level
;
3583 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3584 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3585 If that frame has not evaluated the arguments yet (or is a special form),
3586 the value is (nil FUNCTION ARG-FORMS...).
3587 If that frame has evaluated its arguments and called its function already,
3588 the value is (t FUNCTION ARG-VALUES...).
3589 A &rest arg is represented as the tail of the list ARG-VALUES.
3590 FUNCTION is whatever was supplied as car of evaluated list,
3591 or a lambda expression for macro calls.
3592 If NFRAMES is more than the number of frames, the value is nil. */)
3593 (Lisp_Object nframes
)
3595 register struct backtrace
*backlist
= backtrace_list
;
3596 register EMACS_INT i
;
3599 CHECK_NATNUM (nframes
);
3601 /* Find the frame requested. */
3602 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3603 backlist
= backlist
->next
;
3607 if (backlist
->nargs
== UNEVALLED
)
3608 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3611 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3612 tem
= *backlist
->args
;
3614 tem
= Flist (backlist
->nargs
, backlist
->args
);
3616 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3623 mark_backtrace (void)
3625 register struct backtrace
*backlist
;
3628 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3630 mark_object (*backlist
->function
);
3632 if (backlist
->nargs
== UNEVALLED
3633 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3636 i
= backlist
->nargs
;
3638 mark_object (backlist
->args
[i
]);
3646 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3647 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3648 If Lisp code tries to increase the total number past this amount,
3649 an error is signaled.
3650 You can safely use a value considerably larger than the default value,
3651 if that proves inconveniently small. However, if you increase it too far,
3652 Emacs could run out of memory trying to make the stack bigger. */);
3654 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3655 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3657 This limit serves to catch infinite recursions for you before they cause
3658 actual stack overflow in C, which would be fatal for Emacs.
3659 You can safely make it considerably larger than its default value,
3660 if that proves inconveniently small. However, if you increase it too far,
3661 Emacs could overflow the real C stack, and crash. */);
3663 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3664 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3665 If the value is t, that means do an ordinary quit.
3666 If the value equals `throw-on-input', that means quit by throwing
3667 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3668 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3669 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3672 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3673 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3674 Note that `quit-flag' will still be set by typing C-g,
3675 so a quit will be signaled as soon as `inhibit-quit' is nil.
3676 To prevent this happening, set `quit-flag' to nil
3677 before making `inhibit-quit' nil. */);
3678 Vinhibit_quit
= Qnil
;
3680 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3681 DEFSYM (Qautoload
, "autoload");
3682 DEFSYM (Qdebug_on_error
, "debug-on-error");
3683 DEFSYM (Qmacro
, "macro");
3684 DEFSYM (Qdeclare
, "declare");
3686 /* Note that the process handling also uses Qexit, but we don't want
3687 to staticpro it twice, so we just do it here. */
3688 DEFSYM (Qexit
, "exit");
3690 DEFSYM (Qinteractive
, "interactive");
3691 DEFSYM (Qcommandp
, "commandp");
3692 DEFSYM (Qdefun
, "defun");
3693 DEFSYM (Qand_rest
, "&rest");
3694 DEFSYM (Qand_optional
, "&optional");
3695 DEFSYM (Qclosure
, "closure");
3696 DEFSYM (Qdebug
, "debug");
3698 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3699 doc
: /* *Non-nil means enter debugger if an error is signaled.
3700 Does not apply to errors handled by `condition-case' or those
3701 matched by `debug-ignored-errors'.
3702 If the value is a list, an error only means to enter the debugger
3703 if one of its condition symbols appears in the list.
3704 When you evaluate an expression interactively, this variable
3705 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3706 The command `toggle-debug-on-error' toggles this.
3707 See also the variable `debug-on-quit'. */);
3708 Vdebug_on_error
= Qnil
;
3710 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3711 doc
: /* *List of errors for which the debugger should not be called.
3712 Each element may be a condition-name or a regexp that matches error messages.
3713 If any element applies to a given error, that error skips the debugger
3714 and just returns to top level.
3715 This overrides the variable `debug-on-error'.
3716 It does not apply to errors handled by `condition-case'. */);
3717 Vdebug_ignored_errors
= Qnil
;
3719 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3720 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3721 Does not apply if quit is handled by a `condition-case'. */);
3724 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3725 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3727 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3728 doc
: /* Non-nil means debugger may continue execution.
3729 This is nil when the debugger is called under circumstances where it
3730 might not be safe to continue. */);
3731 debugger_may_continue
= 1;
3733 DEFVAR_LISP ("debugger", Vdebugger
,
3734 doc
: /* Function to call to invoke debugger.
3735 If due to frame exit, args are `exit' and the value being returned;
3736 this function's value will be returned instead of that.
3737 If due to error, args are `error' and a list of the args to `signal'.
3738 If due to `apply' or `funcall' entry, one arg, `lambda'.
3739 If due to `eval' entry, one arg, t. */);
3742 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3743 doc
: /* If non-nil, this is a function for `signal' to call.
3744 It receives the same arguments that `signal' was given.
3745 The Edebug package uses this to regain control. */);
3746 Vsignal_hook_function
= Qnil
;
3748 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3749 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3750 Note that `debug-on-error', `debug-on-quit' and friends
3751 still determine whether to handle the particular condition. */);
3752 Vdebug_on_signal
= Qnil
;
3754 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3755 doc
: /* Function to process declarations in a macro definition.
3756 The function will be called with two args MACRO and DECL.
3757 MACRO is the name of the macro being defined.
3758 DECL is a list `(declare ...)' containing the declarations.
3759 The value the function returns is not used. */);
3760 Vmacro_declaration_function
= Qnil
;
3762 /* When lexical binding is being used,
3763 vinternal_interpreter_environment is non-nil, and contains an alist
3764 of lexically-bound variable, or (t), indicating an empty
3765 environment. The lisp name of this variable would be
3766 `internal-interpreter-environment' if it weren't hidden.
3767 Every element of this list can be either a cons (VAR . VAL)
3768 specifying a lexical binding, or a single symbol VAR indicating
3769 that this variable should use dynamic scoping. */
3770 DEFSYM (Qinternal_interpreter_environment
, "internal-interpreter-environment");
3771 DEFVAR_LISP ("internal-interpreter-environment",
3772 Vinternal_interpreter_environment
,
3773 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3774 When lexical binding is not being used, this variable is nil.
3775 A value of `(t)' indicates an empty environment, otherwise it is an
3776 alist of active lexical bindings. */);
3777 Vinternal_interpreter_environment
= Qnil
;
3778 /* Don't export this variable to Elisp, so noone can mess with it
3779 (Just imagine if someone makes it buffer-local). */
3780 Funintern (Qinternal_interpreter_environment
, Qnil
);
3782 DEFSYM (Vrun_hooks
, "run-hooks");
3784 staticpro (&Vautoload_queue
);
3785 Vautoload_queue
= Qnil
;
3786 staticpro (&Vsignaling_function
);
3787 Vsignaling_function
= Qnil
;
3798 defsubr (&Sfunction
);
3800 defsubr (&Sdefmacro
);
3802 defsubr (&Sdefvaralias
);
3803 defsubr (&Sdefconst
);
3804 defsubr (&Suser_variable_p
);
3808 defsubr (&Smacroexpand
);
3811 defsubr (&Sunwind_protect
);
3812 defsubr (&Scondition_case
);
3814 defsubr (&Sinteractive_p
);
3815 defsubr (&Scalled_interactively_p
);
3816 defsubr (&Scommandp
);
3817 defsubr (&Sautoload
);
3820 defsubr (&Sfuncall
);
3821 defsubr (&Srun_hooks
);
3822 defsubr (&Srun_hook_with_args
);
3823 defsubr (&Srun_hook_with_args_until_success
);
3824 defsubr (&Srun_hook_with_args_until_failure
);
3825 defsubr (&Srun_hook_wrapped
);
3826 defsubr (&Sfetch_bytecode
);
3827 defsubr (&Sbacktrace_debug
);
3828 defsubr (&Sbacktrace
);
3829 defsubr (&Sbacktrace_frame
);
3830 defsubr (&Sspecial_variable_p
);
3831 defsubr (&Sfunctionp
);