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 instead of a
1361 single condition name; then it handles all of them. If the special
1362 condition name `debug' is present in this list, it allows another
1363 condition in the list to run the debugger if `debug-on-error' and the
1364 other usual mechanisms says it should (otherwise, `condition-case'
1365 suppresses the debugger).
1367 When a handler handles an error, control returns to the `condition-case'
1368 and it executes the handler's BODY...
1369 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1370 \(If VAR is nil, the handler can't access that information.)
1371 Then the value of the last BODY form is returned from the `condition-case'
1374 See also the function `signal' for more info.
1375 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1378 register Lisp_Object bodyform
, handlers
;
1379 volatile Lisp_Object var
;
1382 bodyform
= Fcar (Fcdr (args
));
1383 handlers
= Fcdr (Fcdr (args
));
1385 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1388 /* Like Fcondition_case, but the args are separate
1389 rather than passed in a list. Used by Fbyte_code. */
1392 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1393 Lisp_Object handlers
)
1401 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1407 && (SYMBOLP (XCAR (tem
))
1408 || CONSP (XCAR (tem
))))))
1409 error ("Invalid condition handler: %s",
1410 SDATA (Fprin1_to_string (tem
, Qt
)));
1415 c
.backlist
= backtrace_list
;
1416 c
.handlerlist
= handlerlist
;
1417 c
.lisp_eval_depth
= lisp_eval_depth
;
1418 c
.pdlcount
= SPECPDL_INDEX ();
1419 c
.poll_suppress_count
= poll_suppress_count
;
1420 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1421 c
.gcpro
= gcprolist
;
1422 c
.byte_stack
= byte_stack_list
;
1423 if (_setjmp (c
.jmp
))
1426 specbind (h
.var
, c
.val
);
1427 val
= Fprogn (Fcdr (h
.chosen_clause
));
1429 /* Note that this just undoes the binding of h.var; whoever
1430 longjumped to us unwound the stack to c.pdlcount before
1432 unbind_to (c
.pdlcount
, Qnil
);
1439 h
.handler
= handlers
;
1440 h
.next
= handlerlist
;
1444 val
= eval_sub (bodyform
);
1446 handlerlist
= h
.next
;
1450 /* Call the function BFUN with no arguments, catching errors within it
1451 according to HANDLERS. If there is an error, call HFUN with
1452 one argument which is the data that describes the error:
1455 HANDLERS can be a list of conditions to catch.
1456 If HANDLERS is Qt, catch all errors.
1457 If HANDLERS is Qerror, catch all errors
1458 but allow the debugger to run if that is enabled. */
1461 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1462 Lisp_Object (*hfun
) (Lisp_Object
))
1470 c
.backlist
= backtrace_list
;
1471 c
.handlerlist
= handlerlist
;
1472 c
.lisp_eval_depth
= lisp_eval_depth
;
1473 c
.pdlcount
= SPECPDL_INDEX ();
1474 c
.poll_suppress_count
= poll_suppress_count
;
1475 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1476 c
.gcpro
= gcprolist
;
1477 c
.byte_stack
= byte_stack_list
;
1478 if (_setjmp (c
.jmp
))
1480 return (*hfun
) (c
.val
);
1484 h
.handler
= handlers
;
1486 h
.next
= handlerlist
;
1492 handlerlist
= h
.next
;
1496 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1499 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1500 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1508 c
.backlist
= backtrace_list
;
1509 c
.handlerlist
= handlerlist
;
1510 c
.lisp_eval_depth
= lisp_eval_depth
;
1511 c
.pdlcount
= SPECPDL_INDEX ();
1512 c
.poll_suppress_count
= poll_suppress_count
;
1513 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1514 c
.gcpro
= gcprolist
;
1515 c
.byte_stack
= byte_stack_list
;
1516 if (_setjmp (c
.jmp
))
1518 return (*hfun
) (c
.val
);
1522 h
.handler
= handlers
;
1524 h
.next
= handlerlist
;
1528 val
= (*bfun
) (arg
);
1530 handlerlist
= h
.next
;
1534 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1538 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1541 Lisp_Object handlers
,
1542 Lisp_Object (*hfun
) (Lisp_Object
))
1550 c
.backlist
= backtrace_list
;
1551 c
.handlerlist
= handlerlist
;
1552 c
.lisp_eval_depth
= lisp_eval_depth
;
1553 c
.pdlcount
= SPECPDL_INDEX ();
1554 c
.poll_suppress_count
= poll_suppress_count
;
1555 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1556 c
.gcpro
= gcprolist
;
1557 c
.byte_stack
= byte_stack_list
;
1558 if (_setjmp (c
.jmp
))
1560 return (*hfun
) (c
.val
);
1564 h
.handler
= handlers
;
1566 h
.next
= handlerlist
;
1570 val
= (*bfun
) (arg1
, arg2
);
1572 handlerlist
= h
.next
;
1576 /* Like internal_condition_case but call BFUN with NARGS as first,
1577 and ARGS as second argument. */
1580 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1583 Lisp_Object handlers
,
1584 Lisp_Object (*hfun
) (Lisp_Object
))
1592 c
.backlist
= backtrace_list
;
1593 c
.handlerlist
= handlerlist
;
1594 c
.lisp_eval_depth
= lisp_eval_depth
;
1595 c
.pdlcount
= SPECPDL_INDEX ();
1596 c
.poll_suppress_count
= poll_suppress_count
;
1597 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1598 c
.gcpro
= gcprolist
;
1599 c
.byte_stack
= byte_stack_list
;
1600 if (_setjmp (c
.jmp
))
1602 return (*hfun
) (c
.val
);
1606 h
.handler
= handlers
;
1608 h
.next
= handlerlist
;
1612 val
= (*bfun
) (nargs
, args
);
1614 handlerlist
= h
.next
;
1619 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1620 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1623 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1624 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1625 This function does not return.
1627 An error symbol is a symbol with an `error-conditions' property
1628 that is a list of condition names.
1629 A handler for any of those names will get to handle this signal.
1630 The symbol `error' should normally be one of them.
1632 DATA should be a list. Its elements are printed as part of the error message.
1633 See Info anchor `(elisp)Definition of signal' for some details on how this
1634 error message is constructed.
1635 If the signal is handled, DATA is made available to the handler.
1636 See also the function `condition-case'. */)
1637 (Lisp_Object error_symbol
, Lisp_Object data
)
1639 /* When memory is full, ERROR-SYMBOL is nil,
1640 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1641 That is a special case--don't do this in other situations. */
1642 Lisp_Object conditions
;
1644 Lisp_Object real_error_symbol
1645 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1646 register Lisp_Object clause
= Qnil
;
1648 struct backtrace
*bp
;
1650 immediate_quit
= handling_signal
= 0;
1652 if (gc_in_progress
|| waiting_for_input
)
1655 #if 0 /* rms: I don't know why this was here,
1656 but it is surely wrong for an error that is handled. */
1657 #ifdef HAVE_WINDOW_SYSTEM
1658 if (display_hourglass_p
)
1659 cancel_hourglass ();
1663 /* This hook is used by edebug. */
1664 if (! NILP (Vsignal_hook_function
)
1665 && ! NILP (error_symbol
))
1667 /* Edebug takes care of restoring these variables when it exits. */
1668 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1669 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1671 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1672 max_specpdl_size
= SPECPDL_INDEX () + 40;
1674 call2 (Vsignal_hook_function
, error_symbol
, data
);
1677 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1679 /* Remember from where signal was called. Skip over the frame for
1680 `signal' itself. If a frame for `error' follows, skip that,
1681 too. Don't do this when ERROR_SYMBOL is nil, because that
1682 is a memory-full error. */
1683 Vsignaling_function
= Qnil
;
1684 if (backtrace_list
&& !NILP (error_symbol
))
1686 bp
= backtrace_list
->next
;
1687 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1689 if (bp
&& bp
->function
)
1690 Vsignaling_function
= *bp
->function
;
1693 for (h
= handlerlist
; h
; h
= h
->next
)
1695 clause
= find_handler_clause (h
->handler
, conditions
);
1700 if (/* Don't run the debugger for a memory-full error.
1701 (There is no room in memory to do that!) */
1702 !NILP (error_symbol
)
1703 && (!NILP (Vdebug_on_signal
)
1704 /* If no handler is present now, try to run the debugger. */
1706 /* A `debug' symbol in the handler list disables the normal
1707 suppression of the debugger. */
1708 || (CONSP (clause
) && CONSP (XCAR (clause
))
1709 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1710 /* Special handler that means "print a message and run debugger
1712 || EQ (h
->handler
, Qerror
)))
1715 = maybe_call_debugger (conditions
, error_symbol
, data
);
1716 /* We can't return values to code which signaled an error, but we
1717 can continue code which has signaled a quit. */
1718 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1724 Lisp_Object unwind_data
1725 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1727 h
->chosen_clause
= clause
;
1728 unwind_to_catch (h
->tag
, unwind_data
);
1733 Fthrow (Qtop_level
, Qt
);
1736 if (! NILP (error_symbol
))
1737 data
= Fcons (error_symbol
, data
);
1739 string
= Ferror_message_string (data
);
1740 fatal ("%s", SDATA (string
));
1743 /* Internal version of Fsignal that never returns.
1744 Used for anything but Qquit (which can return from Fsignal). */
1747 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1749 Fsignal (error_symbol
, data
);
1753 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1756 xsignal0 (Lisp_Object error_symbol
)
1758 xsignal (error_symbol
, Qnil
);
1762 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1764 xsignal (error_symbol
, list1 (arg
));
1768 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1770 xsignal (error_symbol
, list2 (arg1
, arg2
));
1774 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1776 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1779 /* Signal `error' with message S, and additional arg ARG.
1780 If ARG is not a genuine list, make it a one-element list. */
1783 signal_error (const char *s
, Lisp_Object arg
)
1785 Lisp_Object tortoise
, hare
;
1787 hare
= tortoise
= arg
;
1788 while (CONSP (hare
))
1795 tortoise
= XCDR (tortoise
);
1797 if (EQ (hare
, tortoise
))
1802 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1804 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1808 /* Return nonzero if LIST is a non-nil atom or
1809 a list containing one of CONDITIONS. */
1812 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1819 while (CONSP (conditions
))
1821 Lisp_Object
this, tail
;
1822 this = XCAR (conditions
);
1823 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1824 if (EQ (XCAR (tail
), this))
1826 conditions
= XCDR (conditions
);
1831 /* Return 1 if an error with condition-symbols CONDITIONS,
1832 and described by SIGNAL-DATA, should skip the debugger
1833 according to debugger-ignored-errors. */
1836 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1839 int first_string
= 1;
1840 Lisp_Object error_message
;
1842 error_message
= Qnil
;
1843 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1845 if (STRINGP (XCAR (tail
)))
1849 error_message
= Ferror_message_string (data
);
1853 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1858 Lisp_Object contail
;
1860 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1861 if (EQ (XCAR (tail
), XCAR (contail
)))
1869 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1870 SIG and DATA describe the signal. There are two ways to pass them:
1871 = SIG is the error symbol, and DATA is the rest of the data.
1872 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1873 This is for memory-full errors only. */
1875 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1877 Lisp_Object combined_data
;
1879 combined_data
= Fcons (sig
, data
);
1882 /* Don't try to run the debugger with interrupts blocked.
1883 The editing loop would return anyway. */
1885 /* Does user want to enter debugger for this kind of error? */
1888 : wants_debugger (Vdebug_on_error
, conditions
))
1889 && ! skip_debugger (conditions
, combined_data
)
1890 /* RMS: What's this for? */
1891 && when_entered_debugger
< num_nonmacro_input_events
)
1893 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1901 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1903 register Lisp_Object h
;
1905 /* t is used by handlers for all conditions, set up by C code. */
1906 if (EQ (handlers
, Qt
))
1909 /* error is used similarly, but means print an error message
1910 and run the debugger if that is enabled. */
1911 if (EQ (handlers
, Qerror
))
1914 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1916 Lisp_Object handler
= XCAR (h
);
1917 Lisp_Object condit
, tem
;
1919 if (!CONSP (handler
))
1921 condit
= XCAR (handler
);
1922 /* Handle a single condition name in handler HANDLER. */
1923 if (SYMBOLP (condit
))
1925 tem
= Fmemq (Fcar (handler
), conditions
);
1929 /* Handle a list of condition names in handler HANDLER. */
1930 else if (CONSP (condit
))
1933 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1935 tem
= Fmemq (XCAR (tail
), conditions
);
1946 /* Dump an error message; called like vprintf. */
1948 verror (const char *m
, va_list ap
)
1951 ptrdiff_t size
= sizeof buf
;
1952 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1953 char const *m_end
= m
+ strlen (m
);
1961 va_copy (ap_copy
, ap
);
1962 used
= doprnt (buffer
, size
, m
, m_end
, ap_copy
);
1965 /* Note: the -1 below is because `doprnt' returns the number of bytes
1966 excluding the terminating null byte, and it always terminates with a
1967 null byte, even when producing a truncated message. */
1968 if (used
< size
- 1)
1970 if (size
<= size_max
/ 2)
1972 else if (size
< size_max
)
1975 break; /* and leave the message truncated */
1979 buffer
= (char *) xmalloc (size
);
1982 string
= make_string (buffer
, used
);
1986 xsignal1 (Qerror
, string
);
1990 /* Dump an error message; called like printf. */
1994 error (const char *m
, ...)
2002 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
2003 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
2004 This means it contains a description for how to read arguments to give it.
2005 The value is nil for an invalid function or a symbol with no function
2008 Interactively callable functions include strings and vectors (treated
2009 as keyboard macros), lambda-expressions that contain a top-level call
2010 to `interactive', autoload definitions made by `autoload' with non-nil
2011 fourth argument, and some of the built-in functions of Lisp.
2013 Also, a symbol satisfies `commandp' if its function definition does so.
2015 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2016 then strings and vectors are not accepted. */)
2017 (Lisp_Object function
, Lisp_Object for_call_interactively
)
2019 register Lisp_Object fun
;
2020 register Lisp_Object funcar
;
2021 Lisp_Object if_prop
= Qnil
;
2025 fun
= indirect_function (fun
); /* Check cycles. */
2026 if (NILP (fun
) || EQ (fun
, Qunbound
))
2029 /* Check an `interactive-form' property if present, analogous to the
2030 function-documentation property. */
2032 while (SYMBOLP (fun
))
2034 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2037 fun
= Fsymbol_function (fun
);
2040 /* Emacs primitives are interactive if their DEFUN specifies an
2041 interactive spec. */
2043 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2045 /* Bytecode objects are interactive if they are long enough to
2046 have an element whose index is COMPILED_INTERACTIVE, which is
2047 where the interactive spec is stored. */
2048 else if (COMPILEDP (fun
))
2049 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2052 /* Strings and vectors are keyboard macros. */
2053 if (STRINGP (fun
) || VECTORP (fun
))
2054 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2056 /* Lists may represent commands. */
2059 funcar
= XCAR (fun
);
2060 if (EQ (funcar
, Qclosure
))
2061 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
2063 else if (EQ (funcar
, Qlambda
))
2064 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2065 else if (EQ (funcar
, Qautoload
))
2066 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2071 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2072 doc
: /* Define FUNCTION to autoload from FILE.
2073 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2074 Third arg DOCSTRING is documentation for the function.
2075 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2076 Fifth arg TYPE indicates the type of the object:
2077 nil or omitted says FUNCTION is a function,
2078 `keymap' says FUNCTION is really a keymap, and
2079 `macro' or t says FUNCTION is really a macro.
2080 Third through fifth args give info about the real definition.
2081 They default to nil.
2082 If FUNCTION is already defined other than as an autoload,
2083 this does nothing and returns nil. */)
2084 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2086 CHECK_SYMBOL (function
);
2087 CHECK_STRING (file
);
2089 /* If function is defined and not as an autoload, don't override. */
2090 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2091 && !(CONSP (XSYMBOL (function
)->function
)
2092 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2095 if (NILP (Vpurify_flag
))
2096 /* Only add entries after dumping, because the ones before are
2097 not useful and else we get loads of them from the loaddefs.el. */
2098 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2100 /* We don't want the docstring in purespace (instead,
2101 Snarf-documentation should (hopefully) overwrite it).
2102 We used to use 0 here, but that leads to accidental sharing in
2103 purecopy's hash-consing, so we use a (hopefully) unique integer
2105 docstring
= make_number (XPNTR (function
));
2106 return Ffset (function
,
2107 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2108 interactive
, type
)));
2112 un_autoload (Lisp_Object oldqueue
)
2114 register Lisp_Object queue
, first
, second
;
2116 /* Queue to unwind is current value of Vautoload_queue.
2117 oldqueue is the shadowed value to leave in Vautoload_queue. */
2118 queue
= Vautoload_queue
;
2119 Vautoload_queue
= oldqueue
;
2120 while (CONSP (queue
))
2122 first
= XCAR (queue
);
2123 second
= Fcdr (first
);
2124 first
= Fcar (first
);
2125 if (EQ (first
, make_number (0)))
2128 Ffset (first
, second
);
2129 queue
= XCDR (queue
);
2134 /* Load an autoloaded function.
2135 FUNNAME is the symbol which is the function's name.
2136 FUNDEF is the autoload definition (a list). */
2139 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2141 int count
= SPECPDL_INDEX ();
2143 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2145 /* This is to make sure that loadup.el gives a clear picture
2146 of what files are preloaded and when. */
2147 if (! NILP (Vpurify_flag
))
2148 error ("Attempt to autoload %s while preparing to dump",
2149 SDATA (SYMBOL_NAME (funname
)));
2152 CHECK_SYMBOL (funname
);
2153 GCPRO3 (fun
, funname
, fundef
);
2155 /* Preserve the match data. */
2156 record_unwind_save_match_data ();
2158 /* If autoloading gets an error (which includes the error of failing
2159 to define the function being called), we use Vautoload_queue
2160 to undo function definitions and `provide' calls made by
2161 the function. We do this in the specific case of autoloading
2162 because autoloading is not an explicit request "load this file",
2163 but rather a request to "call this function".
2165 The value saved here is to be restored into Vautoload_queue. */
2166 record_unwind_protect (un_autoload
, Vautoload_queue
);
2167 Vautoload_queue
= Qt
;
2168 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2170 /* Once loading finishes, don't undo it. */
2171 Vautoload_queue
= Qt
;
2172 unbind_to (count
, Qnil
);
2174 fun
= Findirect_function (fun
, Qnil
);
2176 if (!NILP (Fequal (fun
, fundef
)))
2177 error ("Autoloading failed to define function %s",
2178 SDATA (SYMBOL_NAME (funname
)));
2183 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2184 doc
: /* Evaluate FORM and return its value.
2185 If LEXICAL is t, evaluate using lexical scoping. */)
2186 (Lisp_Object form
, Lisp_Object lexical
)
2188 int count
= SPECPDL_INDEX ();
2189 specbind (Qinternal_interpreter_environment
,
2190 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2191 return unbind_to (count
, eval_sub (form
));
2194 /* Eval a sub-expression of the current expression (i.e. in the same
2197 eval_sub (Lisp_Object form
)
2199 Lisp_Object fun
, val
, original_fun
, original_args
;
2201 struct backtrace backtrace
;
2202 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2204 if (handling_signal
)
2209 /* Look up its binding in the lexical environment.
2210 We do not pay attention to the declared_special flag here, since we
2211 already did that when let-binding the variable. */
2212 Lisp_Object lex_binding
2213 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2214 ? Fassq (form
, Vinternal_interpreter_environment
)
2216 if (CONSP (lex_binding
))
2217 return XCDR (lex_binding
);
2219 return Fsymbol_value (form
);
2226 if ((consing_since_gc
> gc_cons_threshold
2227 && consing_since_gc
> gc_relative_threshold
)
2229 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2232 Fgarbage_collect ();
2236 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2238 if (max_lisp_eval_depth
< 100)
2239 max_lisp_eval_depth
= 100;
2240 if (lisp_eval_depth
> max_lisp_eval_depth
)
2241 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2244 original_fun
= Fcar (form
);
2245 original_args
= Fcdr (form
);
2247 backtrace
.next
= backtrace_list
;
2248 backtrace_list
= &backtrace
;
2249 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2250 backtrace
.args
= &original_args
;
2251 backtrace
.nargs
= UNEVALLED
;
2252 backtrace
.debug_on_exit
= 0;
2254 if (debug_on_next_call
)
2255 do_debug_on_call (Qt
);
2257 /* At this point, only original_fun and original_args
2258 have values that will be used below. */
2261 /* Optimize for no indirection. */
2263 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2264 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2265 fun
= indirect_function (fun
);
2269 Lisp_Object numargs
;
2270 Lisp_Object argvals
[8];
2271 Lisp_Object args_left
;
2272 register int i
, maxargs
;
2274 args_left
= original_args
;
2275 numargs
= Flength (args_left
);
2279 if (XINT (numargs
) < XSUBR (fun
)->min_args
2280 || (XSUBR (fun
)->max_args
>= 0
2281 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2282 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2284 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2285 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2286 else if (XSUBR (fun
)->max_args
== MANY
)
2288 /* Pass a vector of evaluated arguments. */
2290 ptrdiff_t argnum
= 0;
2293 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2295 GCPRO3 (args_left
, fun
, fun
);
2299 while (!NILP (args_left
))
2301 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2302 args_left
= Fcdr (args_left
);
2303 gcpro3
.nvars
= argnum
;
2306 backtrace
.args
= vals
;
2307 backtrace
.nargs
= XINT (numargs
);
2309 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2315 GCPRO3 (args_left
, fun
, fun
);
2316 gcpro3
.var
= argvals
;
2319 maxargs
= XSUBR (fun
)->max_args
;
2320 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2322 argvals
[i
] = eval_sub (Fcar (args_left
));
2328 backtrace
.args
= argvals
;
2329 backtrace
.nargs
= XINT (numargs
);
2334 val
= (XSUBR (fun
)->function
.a0 ());
2337 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2340 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2343 val
= (XSUBR (fun
)->function
.a3
2344 (argvals
[0], argvals
[1], argvals
[2]));
2347 val
= (XSUBR (fun
)->function
.a4
2348 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2351 val
= (XSUBR (fun
)->function
.a5
2352 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2356 val
= (XSUBR (fun
)->function
.a6
2357 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2358 argvals
[4], argvals
[5]));
2361 val
= (XSUBR (fun
)->function
.a7
2362 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2363 argvals
[4], argvals
[5], argvals
[6]));
2367 val
= (XSUBR (fun
)->function
.a8
2368 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2369 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2373 /* Someone has created a subr that takes more arguments than
2374 is supported by this code. We need to either rewrite the
2375 subr to use a different argument protocol, or add more
2376 cases to this switch. */
2381 else if (COMPILEDP (fun
))
2382 val
= apply_lambda (fun
, original_args
);
2385 if (EQ (fun
, Qunbound
))
2386 xsignal1 (Qvoid_function
, original_fun
);
2388 xsignal1 (Qinvalid_function
, original_fun
);
2389 funcar
= XCAR (fun
);
2390 if (!SYMBOLP (funcar
))
2391 xsignal1 (Qinvalid_function
, original_fun
);
2392 if (EQ (funcar
, Qautoload
))
2394 do_autoload (fun
, original_fun
);
2397 if (EQ (funcar
, Qmacro
))
2398 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2399 else if (EQ (funcar
, Qlambda
)
2400 || EQ (funcar
, Qclosure
))
2401 val
= apply_lambda (fun
, original_args
);
2403 xsignal1 (Qinvalid_function
, original_fun
);
2408 if (backtrace
.debug_on_exit
)
2409 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2410 backtrace_list
= backtrace
.next
;
2415 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2416 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2417 Then return the value FUNCTION returns.
2418 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2419 usage: (apply FUNCTION &rest ARGUMENTS) */)
2420 (ptrdiff_t nargs
, Lisp_Object
*args
)
2422 ptrdiff_t i
, numargs
;
2423 register Lisp_Object spread_arg
;
2424 register Lisp_Object
*funcall_args
;
2425 Lisp_Object fun
, retval
;
2426 struct gcpro gcpro1
;
2431 spread_arg
= args
[nargs
- 1];
2432 CHECK_LIST (spread_arg
);
2434 numargs
= XINT (Flength (spread_arg
));
2437 return Ffuncall (nargs
- 1, args
);
2438 else if (numargs
== 1)
2440 args
[nargs
- 1] = XCAR (spread_arg
);
2441 return Ffuncall (nargs
, args
);
2444 numargs
+= nargs
- 2;
2446 /* Optimize for no indirection. */
2447 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2448 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2449 fun
= indirect_function (fun
);
2450 if (EQ (fun
, Qunbound
))
2452 /* Let funcall get the error. */
2459 if (numargs
< XSUBR (fun
)->min_args
2460 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2461 goto funcall
; /* Let funcall get the error. */
2462 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2464 /* Avoid making funcall cons up a yet another new vector of arguments
2465 by explicitly supplying nil's for optional values. */
2466 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2467 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2468 funcall_args
[++i
] = Qnil
;
2469 GCPRO1 (*funcall_args
);
2470 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2474 /* We add 1 to numargs because funcall_args includes the
2475 function itself as well as its arguments. */
2478 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2479 GCPRO1 (*funcall_args
);
2480 gcpro1
.nvars
= 1 + numargs
;
2483 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2484 /* Spread the last arg we got. Its first element goes in
2485 the slot that it used to occupy, hence this value of I. */
2487 while (!NILP (spread_arg
))
2489 funcall_args
[i
++] = XCAR (spread_arg
);
2490 spread_arg
= XCDR (spread_arg
);
2493 /* By convention, the caller needs to gcpro Ffuncall's args. */
2494 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2501 /* Run hook variables in various ways. */
2504 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2506 Ffuncall (nargs
, args
);
2510 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2511 doc
: /* Run each hook in HOOKS.
2512 Each argument should be a symbol, a hook variable.
2513 These symbols are processed in the order specified.
2514 If a hook symbol has a non-nil value, that value may be a function
2515 or a list of functions to be called to run the hook.
2516 If the value is a function, it is called with no arguments.
2517 If it is a list, the elements are called, in order, with no arguments.
2519 Major modes should not use this function directly to run their mode
2520 hook; they should use `run-mode-hooks' instead.
2522 Do not use `make-local-variable' to make a hook variable buffer-local.
2523 Instead, use `add-hook' and specify t for the LOCAL argument.
2524 usage: (run-hooks &rest HOOKS) */)
2525 (ptrdiff_t nargs
, Lisp_Object
*args
)
2527 Lisp_Object hook
[1];
2530 for (i
= 0; i
< nargs
; i
++)
2533 run_hook_with_args (1, hook
, funcall_nil
);
2539 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2540 Srun_hook_with_args
, 1, MANY
, 0,
2541 doc
: /* Run HOOK with the specified arguments ARGS.
2542 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2543 value, that value may be a function or a list of functions to be
2544 called to run the hook. If the value is a function, it is called with
2545 the given arguments and its return value is returned. If it is a list
2546 of functions, those functions are called, in order,
2547 with the given arguments ARGS.
2548 It is best not to depend on the value returned by `run-hook-with-args',
2551 Do not use `make-local-variable' to make a hook variable buffer-local.
2552 Instead, use `add-hook' and specify t for the LOCAL argument.
2553 usage: (run-hook-with-args HOOK &rest ARGS) */)
2554 (ptrdiff_t nargs
, Lisp_Object
*args
)
2556 return run_hook_with_args (nargs
, args
, funcall_nil
);
2559 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2560 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2561 doc
: /* Run HOOK with the specified arguments ARGS.
2562 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2563 value, that value may be a function or a list of functions to be
2564 called to run the hook. If the value is a function, it is called with
2565 the given arguments and its return value is returned.
2566 If it is a list of functions, those functions are called, in order,
2567 with the given arguments ARGS, until one of them
2568 returns a non-nil value. Then we return that value.
2569 However, if they all return nil, we return nil.
2571 Do not use `make-local-variable' to make a hook variable buffer-local.
2572 Instead, use `add-hook' and specify t for the LOCAL argument.
2573 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2574 (ptrdiff_t nargs
, Lisp_Object
*args
)
2576 return run_hook_with_args (nargs
, args
, Ffuncall
);
2580 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2582 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2585 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2586 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2587 doc
: /* Run HOOK with the specified arguments ARGS.
2588 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2589 value, that value may be a function or a list of functions to be
2590 called to run the hook. If the value is a function, it is called with
2591 the given arguments and its return value is returned.
2592 If it is a list of functions, those functions are called, in order,
2593 with the given arguments ARGS, until one of them returns nil.
2594 Then we return nil. However, if they all return non-nil, we return non-nil.
2596 Do not use `make-local-variable' to make a hook variable buffer-local.
2597 Instead, use `add-hook' and specify t for the LOCAL argument.
2598 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2599 (ptrdiff_t nargs
, Lisp_Object
*args
)
2601 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2605 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2607 Lisp_Object tmp
= args
[0], ret
;
2610 ret
= Ffuncall (nargs
, args
);
2616 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2617 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2618 I.e. instead of calling each function FUN directly with arguments ARGS,
2619 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2620 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2621 aborts and returns that value.
2622 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2623 (ptrdiff_t nargs
, Lisp_Object
*args
)
2625 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2628 /* ARGS[0] should be a hook symbol.
2629 Call each of the functions in the hook value, passing each of them
2630 as arguments all the rest of ARGS (all NARGS - 1 elements).
2631 FUNCALL specifies how to call each function on the hook.
2632 The caller (or its caller, etc) must gcpro all of ARGS,
2633 except that it isn't necessary to gcpro ARGS[0]. */
2636 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2637 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2639 Lisp_Object sym
, val
, ret
= Qnil
;
2640 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2642 /* If we are dying or still initializing,
2643 don't do anything--it would probably crash if we tried. */
2644 if (NILP (Vrun_hooks
))
2648 val
= find_symbol_value (sym
);
2650 if (EQ (val
, Qunbound
) || NILP (val
))
2652 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2655 return funcall (nargs
, args
);
2659 Lisp_Object global_vals
= Qnil
;
2660 GCPRO3 (sym
, val
, global_vals
);
2663 CONSP (val
) && NILP (ret
);
2666 if (EQ (XCAR (val
), Qt
))
2668 /* t indicates this hook has a local binding;
2669 it means to run the global binding too. */
2670 global_vals
= Fdefault_value (sym
);
2671 if (NILP (global_vals
)) continue;
2673 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2675 args
[0] = global_vals
;
2676 ret
= funcall (nargs
, args
);
2681 CONSP (global_vals
) && NILP (ret
);
2682 global_vals
= XCDR (global_vals
))
2684 args
[0] = XCAR (global_vals
);
2685 /* In a global value, t should not occur. If it does, we
2686 must ignore it to avoid an endless loop. */
2687 if (!EQ (args
[0], Qt
))
2688 ret
= funcall (nargs
, args
);
2694 args
[0] = XCAR (val
);
2695 ret
= funcall (nargs
, args
);
2704 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2707 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2709 Lisp_Object temp
[3];
2714 Frun_hook_with_args (3, temp
);
2717 /* Apply fn to arg. */
2719 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2721 struct gcpro gcpro1
;
2725 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2728 Lisp_Object args
[2];
2732 RETURN_UNGCPRO (Fapply (2, args
));
2736 /* Call function fn on no arguments. */
2738 call0 (Lisp_Object fn
)
2740 struct gcpro gcpro1
;
2743 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2746 /* Call function fn with 1 argument arg1. */
2749 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2751 struct gcpro gcpro1
;
2752 Lisp_Object args
[2];
2758 RETURN_UNGCPRO (Ffuncall (2, args
));
2761 /* Call function fn with 2 arguments arg1, arg2. */
2764 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2766 struct gcpro gcpro1
;
2767 Lisp_Object args
[3];
2773 RETURN_UNGCPRO (Ffuncall (3, args
));
2776 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2779 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2781 struct gcpro gcpro1
;
2782 Lisp_Object args
[4];
2789 RETURN_UNGCPRO (Ffuncall (4, args
));
2792 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2795 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2798 struct gcpro gcpro1
;
2799 Lisp_Object args
[5];
2807 RETURN_UNGCPRO (Ffuncall (5, args
));
2810 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2813 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2814 Lisp_Object arg4
, Lisp_Object arg5
)
2816 struct gcpro gcpro1
;
2817 Lisp_Object args
[6];
2826 RETURN_UNGCPRO (Ffuncall (6, args
));
2829 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2832 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2833 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2835 struct gcpro gcpro1
;
2836 Lisp_Object args
[7];
2846 RETURN_UNGCPRO (Ffuncall (7, args
));
2849 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2852 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2853 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2855 struct gcpro gcpro1
;
2856 Lisp_Object args
[8];
2867 RETURN_UNGCPRO (Ffuncall (8, args
));
2870 /* The caller should GCPRO all the elements of ARGS. */
2872 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2873 doc
: /* Non-nil if OBJECT is a function. */)
2874 (Lisp_Object object
)
2876 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2878 object
= Findirect_function (object
, Qt
);
2880 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2882 /* Autoloaded symbols are functions, except if they load
2883 macros or keymaps. */
2885 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2886 object
= XCDR (object
);
2888 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2893 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2894 else if (COMPILEDP (object
))
2896 else if (CONSP (object
))
2898 Lisp_Object car
= XCAR (object
);
2899 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2905 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2906 doc
: /* Call first argument as a function, passing remaining arguments to it.
2907 Return the value that function returns.
2908 Thus, (funcall 'cons 'x 'y) returns (x . y).
2909 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2910 (ptrdiff_t nargs
, Lisp_Object
*args
)
2912 Lisp_Object fun
, original_fun
;
2914 ptrdiff_t numargs
= nargs
- 1;
2915 Lisp_Object lisp_numargs
;
2917 struct backtrace backtrace
;
2918 register Lisp_Object
*internal_args
;
2922 if ((consing_since_gc
> gc_cons_threshold
2923 && consing_since_gc
> gc_relative_threshold
)
2925 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2926 Fgarbage_collect ();
2928 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2930 if (max_lisp_eval_depth
< 100)
2931 max_lisp_eval_depth
= 100;
2932 if (lisp_eval_depth
> max_lisp_eval_depth
)
2933 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2936 backtrace
.next
= backtrace_list
;
2937 backtrace_list
= &backtrace
;
2938 backtrace
.function
= &args
[0];
2939 backtrace
.args
= &args
[1];
2940 backtrace
.nargs
= nargs
- 1;
2941 backtrace
.debug_on_exit
= 0;
2943 if (debug_on_next_call
)
2944 do_debug_on_call (Qlambda
);
2948 original_fun
= args
[0];
2952 /* Optimize for no indirection. */
2954 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2955 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2956 fun
= indirect_function (fun
);
2960 if (numargs
< XSUBR (fun
)->min_args
2961 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2963 XSETFASTINT (lisp_numargs
, numargs
);
2964 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2967 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2968 xsignal1 (Qinvalid_function
, original_fun
);
2970 else if (XSUBR (fun
)->max_args
== MANY
)
2971 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2974 if (XSUBR (fun
)->max_args
> numargs
)
2976 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2977 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2978 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2979 internal_args
[i
] = Qnil
;
2982 internal_args
= args
+ 1;
2983 switch (XSUBR (fun
)->max_args
)
2986 val
= (XSUBR (fun
)->function
.a0 ());
2989 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2992 val
= (XSUBR (fun
)->function
.a2
2993 (internal_args
[0], internal_args
[1]));
2996 val
= (XSUBR (fun
)->function
.a3
2997 (internal_args
[0], internal_args
[1], internal_args
[2]));
3000 val
= (XSUBR (fun
)->function
.a4
3001 (internal_args
[0], internal_args
[1], internal_args
[2],
3005 val
= (XSUBR (fun
)->function
.a5
3006 (internal_args
[0], internal_args
[1], internal_args
[2],
3007 internal_args
[3], internal_args
[4]));
3010 val
= (XSUBR (fun
)->function
.a6
3011 (internal_args
[0], internal_args
[1], internal_args
[2],
3012 internal_args
[3], internal_args
[4], internal_args
[5]));
3015 val
= (XSUBR (fun
)->function
.a7
3016 (internal_args
[0], internal_args
[1], internal_args
[2],
3017 internal_args
[3], internal_args
[4], internal_args
[5],
3022 val
= (XSUBR (fun
)->function
.a8
3023 (internal_args
[0], internal_args
[1], internal_args
[2],
3024 internal_args
[3], internal_args
[4], internal_args
[5],
3025 internal_args
[6], internal_args
[7]));
3030 /* If a subr takes more than 8 arguments without using MANY
3031 or UNEVALLED, we need to extend this function to support it.
3032 Until this is done, there is no way to call the function. */
3037 else if (COMPILEDP (fun
))
3038 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3041 if (EQ (fun
, Qunbound
))
3042 xsignal1 (Qvoid_function
, original_fun
);
3044 xsignal1 (Qinvalid_function
, original_fun
);
3045 funcar
= XCAR (fun
);
3046 if (!SYMBOLP (funcar
))
3047 xsignal1 (Qinvalid_function
, original_fun
);
3048 if (EQ (funcar
, Qlambda
)
3049 || EQ (funcar
, Qclosure
))
3050 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3051 else if (EQ (funcar
, Qautoload
))
3053 do_autoload (fun
, original_fun
);
3058 xsignal1 (Qinvalid_function
, original_fun
);
3062 if (backtrace
.debug_on_exit
)
3063 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3064 backtrace_list
= backtrace
.next
;
3069 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
3071 Lisp_Object args_left
;
3072 ptrdiff_t i
, numargs
;
3073 register Lisp_Object
*arg_vector
;
3074 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3075 register Lisp_Object tem
;
3078 numargs
= XFASTINT (Flength (args
));
3079 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
3082 GCPRO3 (*arg_vector
, args_left
, fun
);
3085 for (i
= 0; i
< numargs
; )
3087 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3088 tem
= eval_sub (tem
);
3089 arg_vector
[i
++] = tem
;
3095 backtrace_list
->args
= arg_vector
;
3096 backtrace_list
->nargs
= i
;
3097 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
3099 /* Do the debug-on-exit now, while arg_vector still exists. */
3100 if (backtrace_list
->debug_on_exit
)
3101 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3102 /* Don't do it again when we return to eval. */
3103 backtrace_list
->debug_on_exit
= 0;
3108 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3109 and return the result of evaluation.
3110 FUN must be either a lambda-expression or a compiled-code object. */
3113 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
3114 register Lisp_Object
*arg_vector
)
3116 Lisp_Object val
, syms_left
, next
, lexenv
;
3117 int count
= SPECPDL_INDEX ();
3123 if (EQ (XCAR (fun
), Qclosure
))
3125 fun
= XCDR (fun
); /* Drop `closure'. */
3126 lexenv
= XCAR (fun
);
3127 CHECK_LIST_CONS (fun
, fun
);
3131 syms_left
= XCDR (fun
);
3132 if (CONSP (syms_left
))
3133 syms_left
= XCAR (syms_left
);
3135 xsignal1 (Qinvalid_function
, fun
);
3137 else if (COMPILEDP (fun
))
3139 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3140 if (INTEGERP (syms_left
))
3141 /* A byte-code object with a non-nil `push args' slot means we
3142 shouldn't bind any arguments, instead just call the byte-code
3143 interpreter directly; it will push arguments as necessary.
3145 Byte-code objects with either a non-existent, or a nil value for
3146 the `push args' slot (the default), have dynamically-bound
3147 arguments, and use the argument-binding code below instead (as do
3148 all interpreted functions, even lexically bound ones). */
3150 /* If we have not actually read the bytecode string
3151 and constants vector yet, fetch them from the file. */
3152 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3153 Ffetch_bytecode (fun
);
3154 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3155 AREF (fun
, COMPILED_CONSTANTS
),
3156 AREF (fun
, COMPILED_STACK_DEPTH
),
3165 i
= optional
= rest
= 0;
3166 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3170 next
= XCAR (syms_left
);
3171 if (!SYMBOLP (next
))
3172 xsignal1 (Qinvalid_function
, fun
);
3174 if (EQ (next
, Qand_rest
))
3176 else if (EQ (next
, Qand_optional
))
3183 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3187 arg
= arg_vector
[i
++];
3189 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3193 /* Bind the argument. */
3194 if (!NILP (lexenv
) && SYMBOLP (next
))
3195 /* Lexically bind NEXT by adding it to the lexenv alist. */
3196 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3198 /* Dynamically bind NEXT. */
3199 specbind (next
, arg
);
3203 if (!NILP (syms_left
))
3204 xsignal1 (Qinvalid_function
, fun
);
3206 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3208 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3209 /* Instantiate a new lexical environment. */
3210 specbind (Qinternal_interpreter_environment
, lexenv
);
3213 val
= Fprogn (XCDR (XCDR (fun
)));
3216 /* If we have not actually read the bytecode string
3217 and constants vector yet, fetch them from the file. */
3218 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3219 Ffetch_bytecode (fun
);
3220 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3221 AREF (fun
, COMPILED_CONSTANTS
),
3222 AREF (fun
, COMPILED_STACK_DEPTH
),
3226 return unbind_to (count
, val
);
3229 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3231 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3232 (Lisp_Object object
)
3236 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3238 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3241 tem
= AREF (object
, COMPILED_BYTECODE
);
3242 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3243 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3245 error ("Invalid byte code");
3247 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3248 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3256 register int count
= SPECPDL_INDEX ();
3257 if (specpdl_size
>= max_specpdl_size
)
3259 if (max_specpdl_size
< 400)
3260 max_specpdl_size
= 400;
3261 if (specpdl_size
>= max_specpdl_size
)
3262 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3265 if (specpdl_size
> max_specpdl_size
)
3266 specpdl_size
= max_specpdl_size
;
3267 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3268 specpdl_ptr
= specpdl
+ count
;
3271 /* `specpdl_ptr->symbol' is a field which describes which variable is
3272 let-bound, so it can be properly undone when we unbind_to.
3273 It can have the following two shapes:
3274 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3275 a symbol that is not buffer-local (at least at the time
3276 the let binding started). Note also that it should not be
3277 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3279 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3280 variable SYMBOL which can be buffer-local. WHERE tells us
3281 which buffer is affected (or nil if the let-binding affects the
3282 global value of the variable) and BUFFER tells us which buffer was
3283 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3284 BUFFER did not yet have a buffer-local value). */
3287 specbind (Lisp_Object symbol
, Lisp_Object value
)
3289 struct Lisp_Symbol
*sym
;
3291 eassert (!handling_signal
);
3293 CHECK_SYMBOL (symbol
);
3294 sym
= XSYMBOL (symbol
);
3295 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3299 switch (sym
->redirect
)
3301 case SYMBOL_VARALIAS
:
3302 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3303 case SYMBOL_PLAINVAL
:
3304 /* The most common case is that of a non-constant symbol with a
3305 trivial value. Make that as fast as we can. */
3306 specpdl_ptr
->symbol
= symbol
;
3307 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3308 specpdl_ptr
->func
= NULL
;
3311 SET_SYMBOL_VAL (sym
, value
);
3313 set_internal (symbol
, value
, Qnil
, 1);
3315 case SYMBOL_LOCALIZED
:
3316 if (SYMBOL_BLV (sym
)->frame_local
)
3317 error ("Frame-local vars cannot be let-bound");
3318 case SYMBOL_FORWARDED
:
3320 Lisp_Object ovalue
= find_symbol_value (symbol
);
3321 specpdl_ptr
->func
= 0;
3322 specpdl_ptr
->old_value
= ovalue
;
3324 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3325 || (EQ (SYMBOL_BLV (sym
)->where
,
3326 SYMBOL_BLV (sym
)->frame_local
?
3327 Fselected_frame () : Fcurrent_buffer ())));
3329 if (sym
->redirect
== SYMBOL_LOCALIZED
3330 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3332 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3334 /* For a local variable, record both the symbol and which
3335 buffer's or frame's value we are saving. */
3336 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3338 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3339 || (BLV_FOUND (SYMBOL_BLV (sym
))
3340 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3343 else if (sym
->redirect
== SYMBOL_LOCALIZED
3344 && BLV_FOUND (SYMBOL_BLV (sym
)))
3345 where
= SYMBOL_BLV (sym
)->where
;
3349 /* We're not using the `unused' slot in the specbinding
3350 structure because this would mean we have to do more
3351 work for simple variables. */
3352 /* FIXME: The third value `current_buffer' is only used in
3353 let_shadows_buffer_binding_p which is itself only used
3354 in set_internal for local_if_set. */
3355 eassert (NILP (where
) || EQ (where
, cur_buf
));
3356 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3358 /* If SYMBOL is a per-buffer variable which doesn't have a
3359 buffer-local value here, make the `let' change the global
3360 value by changing the value of SYMBOL in all buffers not
3361 having their own value. This is consistent with what
3362 happens with other buffer-local variables. */
3364 && sym
->redirect
== SYMBOL_FORWARDED
)
3366 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3368 Fset_default (symbol
, value
);
3373 specpdl_ptr
->symbol
= symbol
;
3376 set_internal (symbol
, value
, Qnil
, 1);
3384 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3386 eassert (!handling_signal
);
3388 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3390 specpdl_ptr
->func
= function
;
3391 specpdl_ptr
->symbol
= Qnil
;
3392 specpdl_ptr
->old_value
= arg
;
3397 unbind_to (int count
, Lisp_Object value
)
3399 Lisp_Object quitf
= Vquit_flag
;
3400 struct gcpro gcpro1
, gcpro2
;
3402 GCPRO2 (value
, quitf
);
3405 while (specpdl_ptr
!= specpdl
+ count
)
3407 /* Copy the binding, and decrement specpdl_ptr, before we do
3408 the work to unbind it. We decrement first
3409 so that an error in unbinding won't try to unbind
3410 the same entry again, and we copy the binding first
3411 in case more bindings are made during some of the code we run. */
3413 struct specbinding this_binding
;
3414 this_binding
= *--specpdl_ptr
;
3416 if (this_binding
.func
!= 0)
3417 (*this_binding
.func
) (this_binding
.old_value
);
3418 /* If the symbol is a list, it is really (SYMBOL WHERE
3419 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3420 frame. If WHERE is a buffer or frame, this indicates we
3421 bound a variable that had a buffer-local or frame-local
3422 binding. WHERE nil means that the variable had the default
3423 value when it was bound. CURRENT-BUFFER is the buffer that
3424 was current when the variable was bound. */
3425 else if (CONSP (this_binding
.symbol
))
3427 Lisp_Object symbol
, where
;
3429 symbol
= XCAR (this_binding
.symbol
);
3430 where
= XCAR (XCDR (this_binding
.symbol
));
3433 Fset_default (symbol
, this_binding
.old_value
);
3434 /* If `where' is non-nil, reset the value in the appropriate
3435 local binding, but only if that binding still exists. */
3436 else if (BUFFERP (where
)
3437 ? !NILP (Flocal_variable_p (symbol
, where
))
3438 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3439 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3441 /* If variable has a trivial value (no forwarding), we can
3442 just set it. No need to check for constant symbols here,
3443 since that was already done by specbind. */
3444 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3445 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3446 this_binding
.old_value
);
3448 /* NOTE: we only ever come here if make_local_foo was used for
3449 the first time on this var within this let. */
3450 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3453 if (NILP (Vquit_flag
) && !NILP (quitf
))
3460 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3461 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3462 A special variable is one that will be bound dynamically, even in a
3463 context where binding is lexical by default. */)
3464 (Lisp_Object symbol
)
3466 CHECK_SYMBOL (symbol
);
3467 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3471 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3472 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3473 The debugger is entered when that frame exits, if the flag is non-nil. */)
3474 (Lisp_Object level
, Lisp_Object flag
)
3476 register struct backtrace
*backlist
= backtrace_list
;
3479 CHECK_NUMBER (level
);
3481 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3483 backlist
= backlist
->next
;
3487 backlist
->debug_on_exit
= !NILP (flag
);
3492 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3493 doc
: /* Print a trace of Lisp function calls currently active.
3494 Output stream used is value of `standard-output'. */)
3497 register struct backtrace
*backlist
= backtrace_list
;
3500 struct gcpro gcpro1
;
3501 Lisp_Object old_print_level
= Vprint_level
;
3503 if (NILP (Vprint_level
))
3504 XSETFASTINT (Vprint_level
, 8);
3511 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3512 if (backlist
->nargs
== UNEVALLED
)
3514 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3515 write_string ("\n", -1);
3519 tem
= *backlist
->function
;
3520 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3521 write_string ("(", -1);
3522 if (backlist
->nargs
== MANY
)
3523 { /* FIXME: Can this happen? */
3525 for (tail
= *backlist
->args
, i
= 0;
3527 tail
= Fcdr (tail
), i
= 1)
3529 if (i
) write_string (" ", -1);
3530 Fprin1 (Fcar (tail
), Qnil
);
3536 for (i
= 0; i
< backlist
->nargs
; i
++)
3538 if (i
) write_string (" ", -1);
3539 Fprin1 (backlist
->args
[i
], Qnil
);
3542 write_string (")\n", -1);
3544 backlist
= backlist
->next
;
3547 Vprint_level
= old_print_level
;
3552 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3553 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3554 If that frame has not evaluated the arguments yet (or is a special form),
3555 the value is (nil FUNCTION ARG-FORMS...).
3556 If that frame has evaluated its arguments and called its function already,
3557 the value is (t FUNCTION ARG-VALUES...).
3558 A &rest arg is represented as the tail of the list ARG-VALUES.
3559 FUNCTION is whatever was supplied as car of evaluated list,
3560 or a lambda expression for macro calls.
3561 If NFRAMES is more than the number of frames, the value is nil. */)
3562 (Lisp_Object nframes
)
3564 register struct backtrace
*backlist
= backtrace_list
;
3565 register EMACS_INT i
;
3568 CHECK_NATNUM (nframes
);
3570 /* Find the frame requested. */
3571 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3572 backlist
= backlist
->next
;
3576 if (backlist
->nargs
== UNEVALLED
)
3577 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3580 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3581 tem
= *backlist
->args
;
3583 tem
= Flist (backlist
->nargs
, backlist
->args
);
3585 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3592 mark_backtrace (void)
3594 register struct backtrace
*backlist
;
3597 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3599 mark_object (*backlist
->function
);
3601 if (backlist
->nargs
== UNEVALLED
3602 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3605 i
= backlist
->nargs
;
3607 mark_object (backlist
->args
[i
]);
3615 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3616 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3617 If Lisp code tries to increase the total number past this amount,
3618 an error is signaled.
3619 You can safely use a value considerably larger than the default value,
3620 if that proves inconveniently small. However, if you increase it too far,
3621 Emacs could run out of memory trying to make the stack bigger. */);
3623 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3624 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3626 This limit serves to catch infinite recursions for you before they cause
3627 actual stack overflow in C, which would be fatal for Emacs.
3628 You can safely make it considerably larger than its default value,
3629 if that proves inconveniently small. However, if you increase it too far,
3630 Emacs could overflow the real C stack, and crash. */);
3632 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3633 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3634 If the value is t, that means do an ordinary quit.
3635 If the value equals `throw-on-input', that means quit by throwing
3636 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3637 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3638 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3641 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3642 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3643 Note that `quit-flag' will still be set by typing C-g,
3644 so a quit will be signaled as soon as `inhibit-quit' is nil.
3645 To prevent this happening, set `quit-flag' to nil
3646 before making `inhibit-quit' nil. */);
3647 Vinhibit_quit
= Qnil
;
3649 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3650 DEFSYM (Qautoload
, "autoload");
3651 DEFSYM (Qdebug_on_error
, "debug-on-error");
3652 DEFSYM (Qmacro
, "macro");
3653 DEFSYM (Qdeclare
, "declare");
3655 /* Note that the process handling also uses Qexit, but we don't want
3656 to staticpro it twice, so we just do it here. */
3657 DEFSYM (Qexit
, "exit");
3659 DEFSYM (Qinteractive
, "interactive");
3660 DEFSYM (Qcommandp
, "commandp");
3661 DEFSYM (Qdefun
, "defun");
3662 DEFSYM (Qand_rest
, "&rest");
3663 DEFSYM (Qand_optional
, "&optional");
3664 DEFSYM (Qclosure
, "closure");
3665 DEFSYM (Qdebug
, "debug");
3667 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3668 doc
: /* *Non-nil means enter debugger if an error is signaled.
3669 Does not apply to errors handled by `condition-case' or those
3670 matched by `debug-ignored-errors'.
3671 If the value is a list, an error only means to enter the debugger
3672 if one of its condition symbols appears in the list.
3673 When you evaluate an expression interactively, this variable
3674 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3675 The command `toggle-debug-on-error' toggles this.
3676 See also the variable `debug-on-quit'. */);
3677 Vdebug_on_error
= Qnil
;
3679 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3680 doc
: /* *List of errors for which the debugger should not be called.
3681 Each element may be a condition-name or a regexp that matches error messages.
3682 If any element applies to a given error, that error skips the debugger
3683 and just returns to top level.
3684 This overrides the variable `debug-on-error'.
3685 It does not apply to errors handled by `condition-case'. */);
3686 Vdebug_ignored_errors
= Qnil
;
3688 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3689 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3690 Does not apply if quit is handled by a `condition-case'. */);
3693 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3694 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3696 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3697 doc
: /* Non-nil means debugger may continue execution.
3698 This is nil when the debugger is called under circumstances where it
3699 might not be safe to continue. */);
3700 debugger_may_continue
= 1;
3702 DEFVAR_LISP ("debugger", Vdebugger
,
3703 doc
: /* Function to call to invoke debugger.
3704 If due to frame exit, args are `exit' and the value being returned;
3705 this function's value will be returned instead of that.
3706 If due to error, args are `error' and a list of the args to `signal'.
3707 If due to `apply' or `funcall' entry, one arg, `lambda'.
3708 If due to `eval' entry, one arg, t. */);
3711 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3712 doc
: /* If non-nil, this is a function for `signal' to call.
3713 It receives the same arguments that `signal' was given.
3714 The Edebug package uses this to regain control. */);
3715 Vsignal_hook_function
= Qnil
;
3717 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3718 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3719 Note that `debug-on-error', `debug-on-quit' and friends
3720 still determine whether to handle the particular condition. */);
3721 Vdebug_on_signal
= Qnil
;
3723 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3724 doc
: /* Function to process declarations in a macro definition.
3725 The function will be called with two args MACRO and DECL.
3726 MACRO is the name of the macro being defined.
3727 DECL is a list `(declare ...)' containing the declarations.
3728 The value the function returns is not used. */);
3729 Vmacro_declaration_function
= Qnil
;
3731 /* When lexical binding is being used,
3732 vinternal_interpreter_environment is non-nil, and contains an alist
3733 of lexically-bound variable, or (t), indicating an empty
3734 environment. The lisp name of this variable would be
3735 `internal-interpreter-environment' if it weren't hidden.
3736 Every element of this list can be either a cons (VAR . VAL)
3737 specifying a lexical binding, or a single symbol VAR indicating
3738 that this variable should use dynamic scoping. */
3739 DEFSYM (Qinternal_interpreter_environment
, "internal-interpreter-environment");
3740 DEFVAR_LISP ("internal-interpreter-environment",
3741 Vinternal_interpreter_environment
,
3742 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3743 When lexical binding is not being used, this variable is nil.
3744 A value of `(t)' indicates an empty environment, otherwise it is an
3745 alist of active lexical bindings. */);
3746 Vinternal_interpreter_environment
= Qnil
;
3747 /* Don't export this variable to Elisp, so noone can mess with it
3748 (Just imagine if someone makes it buffer-local). */
3749 Funintern (Qinternal_interpreter_environment
, Qnil
);
3751 DEFSYM (Vrun_hooks
, "run-hooks");
3753 staticpro (&Vautoload_queue
);
3754 Vautoload_queue
= Qnil
;
3755 staticpro (&Vsignaling_function
);
3756 Vsignaling_function
= Qnil
;
3767 defsubr (&Sfunction
);
3769 defsubr (&Sdefmacro
);
3771 defsubr (&Sdefvaralias
);
3772 defsubr (&Sdefconst
);
3773 defsubr (&Suser_variable_p
);
3777 defsubr (&Smacroexpand
);
3780 defsubr (&Sunwind_protect
);
3781 defsubr (&Scondition_case
);
3783 defsubr (&Sinteractive_p
);
3784 defsubr (&Scalled_interactively_p
);
3785 defsubr (&Scommandp
);
3786 defsubr (&Sautoload
);
3789 defsubr (&Sfuncall
);
3790 defsubr (&Srun_hooks
);
3791 defsubr (&Srun_hook_with_args
);
3792 defsubr (&Srun_hook_with_args_until_success
);
3793 defsubr (&Srun_hook_with_args_until_failure
);
3794 defsubr (&Srun_hook_wrapped
);
3795 defsubr (&Sfetch_bytecode
);
3796 defsubr (&Sbacktrace_debug
);
3797 defsubr (&Sbacktrace
);
3798 defsubr (&Sbacktrace_frame
);
3799 defsubr (&Sspecial_variable_p
);
3800 defsubr (&Sfunctionp
);