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/>. */
23 #include "blockinput.h"
26 #include "dispextern.h"
27 #include "frame.h" /* For XFRAME. */
33 /* This definition is duplicated in alloc.c and keyboard.c */
34 /* Putting it in lisp.h makes cc bomb out! */
38 struct backtrace
*next
;
39 Lisp_Object
*function
;
40 Lisp_Object
*args
; /* Points to vector of args. */
41 int nargs
; /* Length of vector.
42 If nargs is UNEVALLED, args points to slot holding
43 list of unevalled args */
45 /* Nonzero means call value of debugger when done with this operation. */
49 struct backtrace
*backtrace_list
;
51 struct catchtag
*catchlist
;
54 /* Count levels of GCPRO to detect failure to UNGCPRO. */
58 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
59 Lisp_Object Qinhibit_quit
;
60 Lisp_Object Qand_rest
, Qand_optional
;
61 Lisp_Object Qdebug_on_error
;
63 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
67 /* This holds either the symbol `run-hooks' or nil.
68 It is nil at an early stage of startup, and when Emacs
71 Lisp_Object Vrun_hooks
;
73 /* Non-nil means record all fset's and provide's, to be undone
74 if the file being autoloaded is not fully loaded.
75 They are recorded by being consed onto the front of Vautoload_queue:
76 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
78 Lisp_Object Vautoload_queue
;
80 /* Current number of specbindings allocated in specpdl. */
82 EMACS_INT specpdl_size
;
84 /* Pointer to beginning of specpdl. */
86 struct specbinding
*specpdl
;
88 /* Pointer to first unused element in specpdl. */
90 struct specbinding
*specpdl_ptr
;
92 /* Depth in Lisp evaluations and function calls. */
94 EMACS_INT lisp_eval_depth
;
96 /* The value of num_nonmacro_input_events as of the last time we
97 started to enter the debugger. If we decide to enter the debugger
98 again when this is still equal to num_nonmacro_input_events, then we
99 know that the debugger itself has an error, and we should just
100 signal the error instead of entering an infinite loop of debugger
103 int when_entered_debugger
;
105 /* The function from which the last `signal' was called. Set in
108 Lisp_Object Vsignaling_function
;
110 /* Set to non-zero while processing X events. Checked in Feval to
111 make sure the Lisp interpreter isn't called from a signal handler,
112 which is unsafe because the interpreter isn't reentrant. */
116 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
117 static Lisp_Object
funcall_lambda (Lisp_Object
, int, Lisp_Object
*);
118 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
119 static int interactive_p (int);
122 init_eval_once (void)
125 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
126 specpdl_ptr
= specpdl
;
127 /* Don't forget to update docs (lispref node "Local Variables"). */
128 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
129 max_lisp_eval_depth
= 600;
137 specpdl_ptr
= specpdl
;
142 debug_on_next_call
= 0;
147 /* This is less than the initial value of num_nonmacro_input_events. */
148 when_entered_debugger
= -1;
151 /* unwind-protect function used by call_debugger. */
154 restore_stack_limits (Lisp_Object data
)
156 max_specpdl_size
= XINT (XCAR (data
));
157 max_lisp_eval_depth
= XINT (XCDR (data
));
161 /* Call the Lisp debugger, giving it argument ARG. */
164 call_debugger (Lisp_Object arg
)
166 int debug_while_redisplaying
;
167 int count
= SPECPDL_INDEX ();
169 EMACS_INT old_max
= max_specpdl_size
;
171 /* Temporarily bump up the stack limits,
172 so the debugger won't run out of stack. */
174 max_specpdl_size
+= 1;
175 record_unwind_protect (restore_stack_limits
,
176 Fcons (make_number (old_max
),
177 make_number (max_lisp_eval_depth
)));
178 max_specpdl_size
= old_max
;
180 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
181 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
183 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
184 max_specpdl_size
= SPECPDL_INDEX () + 100;
186 #ifdef HAVE_WINDOW_SYSTEM
187 if (display_hourglass_p
)
191 debug_on_next_call
= 0;
192 when_entered_debugger
= num_nonmacro_input_events
;
194 /* Resetting redisplaying_p to 0 makes sure that debug output is
195 displayed if the debugger is invoked during redisplay. */
196 debug_while_redisplaying
= redisplaying_p
;
198 specbind (intern ("debugger-may-continue"),
199 debug_while_redisplaying
? Qnil
: Qt
);
200 specbind (Qinhibit_redisplay
, Qnil
);
201 specbind (Qdebug_on_error
, Qnil
);
203 #if 0 /* Binding this prevents execution of Lisp code during
204 redisplay, which necessarily leads to display problems. */
205 specbind (Qinhibit_eval_during_redisplay
, Qt
);
208 val
= apply1 (Vdebugger
, arg
);
210 /* Interrupting redisplay and resuming it later is not safe under
211 all circumstances. So, when the debugger returns, abort the
212 interrupted redisplay by going back to the top-level. */
213 if (debug_while_redisplaying
)
216 return unbind_to (count
, val
);
220 do_debug_on_call (Lisp_Object code
)
222 debug_on_next_call
= 0;
223 backtrace_list
->debug_on_exit
= 1;
224 call_debugger (Fcons (code
, Qnil
));
227 /* NOTE!!! Every function that can call EVAL must protect its args
228 and temporaries from garbage collection while it needs them.
229 The definition of `For' shows what you have to do. */
231 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
232 doc
: /* Eval args until one of them yields non-nil, then return that value.
233 The remaining args are not evalled at all.
234 If all args return nil, return nil.
235 usage: (or CONDITIONS...) */)
238 register Lisp_Object val
= Qnil
;
245 val
= eval_sub (XCAR (args
));
255 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
256 doc
: /* Eval args until one of them yields nil, then return nil.
257 The remaining args are not evalled at all.
258 If no arg yields nil, return the last arg's value.
259 usage: (and CONDITIONS...) */)
262 register Lisp_Object val
= Qt
;
269 val
= eval_sub (XCAR (args
));
279 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
280 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
281 Returns the value of THEN or the value of the last of the ELSE's.
282 THEN must be one expression, but ELSE... can be zero or more expressions.
283 If COND yields nil, and there are no ELSE's, the value is nil.
284 usage: (if COND THEN ELSE...) */)
287 register Lisp_Object cond
;
291 cond
= eval_sub (Fcar (args
));
295 return eval_sub (Fcar (Fcdr (args
)));
296 return Fprogn (Fcdr (Fcdr (args
)));
299 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
300 doc
: /* Try each clause until one succeeds.
301 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
302 and, if the value is non-nil, this clause succeeds:
303 then the expressions in BODY are evaluated and the last one's
304 value is the value of the cond-form.
305 If no clause succeeds, cond returns nil.
306 If a clause has one element, as in (CONDITION),
307 CONDITION's value if non-nil is returned from the cond-form.
308 usage: (cond CLAUSES...) */)
311 register Lisp_Object clause
, val
;
318 clause
= Fcar (args
);
319 val
= eval_sub (Fcar (clause
));
322 if (!EQ (XCDR (clause
), Qnil
))
323 val
= Fprogn (XCDR (clause
));
333 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
334 doc
: /* Eval BODY forms sequentially and return value of last one.
335 usage: (progn BODY...) */)
338 register Lisp_Object val
= Qnil
;
345 val
= eval_sub (XCAR (args
));
353 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
354 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
355 The value of FIRST is saved during the evaluation of the remaining args,
356 whose values are discarded.
357 usage: (prog1 FIRST BODY...) */)
361 register Lisp_Object args_left
;
362 struct gcpro gcpro1
, gcpro2
;
363 register int argnum
= 0;
374 Lisp_Object tem
= eval_sub (XCAR (args_left
));
377 args_left
= XCDR (args_left
);
379 while (CONSP (args_left
));
385 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
386 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
387 The value of FORM2 is saved during the evaluation of the
388 remaining args, whose values are discarded.
389 usage: (prog2 FORM1 FORM2 BODY...) */)
393 register Lisp_Object args_left
;
394 struct gcpro gcpro1
, gcpro2
;
395 register int argnum
= -1;
408 Lisp_Object tem
= eval_sub (XCAR (args_left
));
411 args_left
= XCDR (args_left
);
413 while (CONSP (args_left
));
419 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
420 doc
: /* Set each SYM to the value of its VAL.
421 The symbols SYM are variables; they are literal (not evaluated).
422 The values VAL are expressions; they are evaluated.
423 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
424 The second VAL is not computed until after the first SYM is set, and so on;
425 each VAL can use the new value of variables set earlier in the `setq'.
426 The return value of the `setq' form is the value of the last VAL.
427 usage: (setq [SYM VAL]...) */)
430 register Lisp_Object args_left
;
431 register Lisp_Object val
, sym
, lex_binding
;
442 val
= eval_sub (Fcar (Fcdr (args_left
)));
443 sym
= Fcar (args_left
);
445 /* Like for eval_sub, we do not check declared_special here since
446 it's been done when let-binding. */
447 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
449 && !NILP (lex_binding
450 = Fassq (sym
, Vinternal_interpreter_environment
)))
451 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
453 Fset (sym
, val
); /* SYM is dynamically bound. */
455 args_left
= Fcdr (Fcdr (args_left
));
457 while (!NILP(args_left
));
463 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
464 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
465 usage: (quote ARG) */)
468 if (!NILP (Fcdr (args
)))
469 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
473 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
474 doc
: /* Like `quote', but preferred for objects which are functions.
475 In byte compilation, `function' causes its argument to be compiled.
476 `quote' cannot do that.
477 usage: (function ARG) */)
480 Lisp_Object quoted
= XCAR (args
);
482 if (!NILP (Fcdr (args
)))
483 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
485 if (!NILP (Vinternal_interpreter_environment
)
487 && EQ (XCAR (quoted
), Qlambda
))
488 /* This is a lambda expression within a lexical environment;
489 return an interpreted closure instead of a simple lambda. */
490 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
493 /* Simply quote the argument. */
498 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
499 doc
: /* Return t if the containing function was run directly by user input.
500 This means that the function was called with `call-interactively'
501 \(which includes being called as the binding of a key)
502 and input is currently coming from the keyboard (not a keyboard macro),
503 and Emacs is not running in batch mode (`noninteractive' is nil).
505 The only known proper use of `interactive-p' is in deciding whether to
506 display a helpful message, or how to display it. If you're thinking
507 of using it for any other purpose, it is quite likely that you're
508 making a mistake. Think: what do you want to do when the command is
509 called from a keyboard macro?
511 To test whether your function was called with `call-interactively',
512 either (i) add an extra optional argument and give it an `interactive'
513 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
514 use `called-interactively-p'. */)
517 return interactive_p (1) ? Qt
: Qnil
;
521 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
522 doc
: /* Return t if the containing function was called by `call-interactively'.
523 If KIND is `interactive', then only return t if the call was made
524 interactively by the user, i.e. not in `noninteractive' mode nor
525 when `executing-kbd-macro'.
526 If KIND is `any', on the other hand, it will return t for any kind of
527 interactive call, including being called as the binding of a key, or
528 from a keyboard macro, or in `noninteractive' mode.
530 The only known proper use of `interactive' for KIND is in deciding
531 whether to display a helpful message, or how to display it. If you're
532 thinking of using it for any other purpose, it is quite likely that
533 you're making a mistake. Think: what do you want to do when the
534 command is called from a keyboard macro?
536 This function is meant for implementing advice and other
537 function-modifying features. Instead of using this, it is sometimes
538 cleaner to give your function an extra optional argument whose
539 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
540 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
543 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
544 && interactive_p (1)) ? Qt
: Qnil
;
548 /* Return 1 if function in which this appears was called using
551 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
552 called is a built-in. */
555 interactive_p (int exclude_subrs_p
)
557 struct backtrace
*btp
;
560 btp
= backtrace_list
;
562 /* If this isn't a byte-compiled function, there may be a frame at
563 the top for Finteractive_p. If so, skip it. */
564 fun
= Findirect_function (*btp
->function
, Qnil
);
565 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
566 || XSUBR (fun
) == &Scalled_interactively_p
))
569 /* If we're running an Emacs 18-style byte-compiled function, there
570 may be a frame for Fbytecode at the top level. In any version of
571 Emacs there can be Fbytecode frames for subexpressions evaluated
572 inside catch and condition-case. Skip past them.
574 If this isn't a byte-compiled function, then we may now be
575 looking at several frames for special forms. Skip past them. */
577 && (EQ (*btp
->function
, Qbytecode
)
578 || btp
->nargs
== UNEVALLED
))
581 /* btp now points at the frame of the innermost function that isn't
582 a special form, ignoring frames for Finteractive_p and/or
583 Fbytecode at the top. If this frame is for a built-in function
584 (such as load or eval-region) return nil. */
585 fun
= Findirect_function (*btp
->function
, Qnil
);
586 if (exclude_subrs_p
&& SUBRP (fun
))
589 /* btp points to the frame of a Lisp function that called interactive-p.
590 Return t if that function was called interactively. */
591 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
597 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
598 doc
: /* Define NAME as a function.
599 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
600 See also the function `interactive'.
601 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
604 register Lisp_Object fn_name
;
605 register Lisp_Object defn
;
607 fn_name
= Fcar (args
);
608 CHECK_SYMBOL (fn_name
);
609 defn
= Fcons (Qlambda
, Fcdr (args
));
610 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
611 defn
= Ffunction (Fcons (defn
, Qnil
));
612 if (!NILP (Vpurify_flag
))
613 defn
= Fpurecopy (defn
);
614 if (CONSP (XSYMBOL (fn_name
)->function
)
615 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
616 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
617 Ffset (fn_name
, defn
);
618 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
622 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
623 doc
: /* Define NAME as a macro.
624 The actual definition looks like
625 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
626 When the macro is called, as in (NAME ARGS...),
627 the function (lambda ARGLIST BODY...) is applied to
628 the list ARGS... as it appears in the expression,
629 and the result should be a form to be evaluated instead of the original.
631 DECL is a declaration, optional, which can specify how to indent
632 calls to this macro, how Edebug should handle it, and which argument
633 should be treated as documentation. It looks like this:
635 The elements can look like this:
637 Set NAME's `lisp-indent-function' property to INDENT.
640 Set NAME's `edebug-form-spec' property to DEBUG. (This is
641 equivalent to writing a `def-edebug-spec' for the macro.)
644 Set NAME's `doc-string-elt' property to ELT.
646 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
649 register Lisp_Object fn_name
;
650 register Lisp_Object defn
;
651 Lisp_Object lambda_list
, doc
, tail
;
653 fn_name
= Fcar (args
);
654 CHECK_SYMBOL (fn_name
);
655 lambda_list
= Fcar (Fcdr (args
));
656 tail
= Fcdr (Fcdr (args
));
659 if (STRINGP (Fcar (tail
)))
665 if (CONSP (Fcar (tail
))
666 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
668 if (!NILP (Vmacro_declaration_function
))
672 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
680 tail
= Fcons (lambda_list
, tail
);
682 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
684 defn
= Fcons (Qlambda
, tail
);
685 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
686 defn
= Ffunction (Fcons (defn
, Qnil
));
687 defn
= Fcons (Qmacro
, defn
);
689 if (!NILP (Vpurify_flag
))
690 defn
= Fpurecopy (defn
);
691 if (CONSP (XSYMBOL (fn_name
)->function
)
692 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
693 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
694 Ffset (fn_name
, defn
);
695 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
700 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
701 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
702 Aliased variables always have the same value; setting one sets the other.
703 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
704 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
705 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
706 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
707 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
708 The return value is BASE-VARIABLE. */)
709 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
711 struct Lisp_Symbol
*sym
;
713 CHECK_SYMBOL (new_alias
);
714 CHECK_SYMBOL (base_variable
);
716 sym
= XSYMBOL (new_alias
);
719 /* Not sure why, but why not? */
720 error ("Cannot make a constant an alias");
722 switch (sym
->redirect
)
724 case SYMBOL_FORWARDED
:
725 error ("Cannot make an internal variable an alias");
726 case SYMBOL_LOCALIZED
:
727 error ("Don't know how to make a localized variable an alias");
730 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
731 If n_a is bound, but b_v is not, set the value of b_v to n_a,
732 so that old-code that affects n_a before the aliasing is setup
734 if (NILP (Fboundp (base_variable
)))
735 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
738 struct specbinding
*p
;
740 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
743 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
744 error ("Don't know how to make a let-bound variable an alias");
747 sym
->declared_special
= 1;
748 sym
->redirect
= SYMBOL_VARALIAS
;
749 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
750 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
751 LOADHIST_ATTACH (new_alias
);
752 /* Even if docstring is nil: remove old docstring. */
753 Fput (new_alias
, Qvariable_documentation
, docstring
);
755 return base_variable
;
759 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
760 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
761 You are not required to define a variable in order to use it,
762 but the definition can supply documentation and an initial value
763 in a way that tags can recognize.
765 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
766 If SYMBOL is buffer-local, its default value is what is set;
767 buffer-local values are not affected.
768 INITVALUE and DOCSTRING are optional.
769 If DOCSTRING starts with *, this variable is identified as a user option.
770 This means that M-x set-variable recognizes it.
771 See also `user-variable-p'.
772 If INITVALUE is missing, SYMBOL's value is not set.
774 If SYMBOL has a local binding, then this form affects the local
775 binding. This is usually not what you want. Thus, if you need to
776 load a file defining variables, with this form or with `defconst' or
777 `defcustom', you should always load that file _outside_ any bindings
778 for these variables. \(`defconst' and `defcustom' behave similarly in
780 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
783 register Lisp_Object sym
, tem
, tail
;
787 if (!NILP (Fcdr (Fcdr (tail
))))
788 error ("Too many arguments");
790 tem
= Fdefault_boundp (sym
);
793 /* Do it before evaluating the initial value, for self-references. */
794 XSYMBOL (sym
)->declared_special
= 1;
796 if (SYMBOL_CONSTANT_P (sym
))
798 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
799 Lisp_Object tem1
= Fcar (tail
);
801 && EQ (XCAR (tem1
), Qquote
)
802 && CONSP (XCDR (tem1
))
803 && EQ (XCAR (XCDR (tem1
)), sym
)))
804 error ("Constant symbol `%s' specified in defvar",
805 SDATA (SYMBOL_NAME (sym
)));
809 Fset_default (sym
, eval_sub (Fcar (tail
)));
811 { /* Check if there is really a global binding rather than just a let
812 binding that shadows the global unboundness of the var. */
813 volatile struct specbinding
*pdl
= specpdl_ptr
;
814 while (--pdl
>= specpdl
)
816 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
817 && EQ (pdl
->old_value
, Qunbound
))
819 message_with_string ("Warning: defvar ignored because %s is let-bound",
820 SYMBOL_NAME (sym
), 1);
829 if (!NILP (Vpurify_flag
))
830 tem
= Fpurecopy (tem
);
831 Fput (sym
, Qvariable_documentation
, tem
);
833 LOADHIST_ATTACH (sym
);
835 else if (!NILP (Vinternal_interpreter_environment
)
836 && !XSYMBOL (sym
)->declared_special
)
837 /* A simple (defvar foo) with lexical scoping does "nothing" except
838 declare that var to be dynamically scoped *locally* (i.e. within
839 the current file or let-block). */
840 Vinternal_interpreter_environment
=
841 Fcons (sym
, Vinternal_interpreter_environment
);
844 /* Simple (defvar <var>) should not count as a definition at all.
845 It could get in the way of other definitions, and unloading this
846 package could try to make the variable unbound. */
852 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
853 doc
: /* Define SYMBOL as a constant variable.
854 The intent is that neither programs nor users should ever change this value.
855 Always sets the value of SYMBOL to the result of evalling INITVALUE.
856 If SYMBOL is buffer-local, its default value is what is set;
857 buffer-local values are not affected.
858 DOCSTRING is optional.
860 If SYMBOL has a local binding, then this form sets the local binding's
861 value. However, you should normally not make local bindings for
862 variables defined with this form.
863 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
866 register Lisp_Object sym
, tem
;
869 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
870 error ("Too many arguments");
872 tem
= eval_sub (Fcar (Fcdr (args
)));
873 if (!NILP (Vpurify_flag
))
874 tem
= Fpurecopy (tem
);
875 Fset_default (sym
, tem
);
876 XSYMBOL (sym
)->declared_special
= 1;
877 tem
= Fcar (Fcdr (Fcdr (args
)));
880 if (!NILP (Vpurify_flag
))
881 tem
= Fpurecopy (tem
);
882 Fput (sym
, Qvariable_documentation
, tem
);
884 Fput (sym
, Qrisky_local_variable
, Qt
);
885 LOADHIST_ATTACH (sym
);
889 /* Error handler used in Fuser_variable_p. */
891 user_variable_p_eh (Lisp_Object ignore
)
897 lisp_indirect_variable (Lisp_Object sym
)
899 struct Lisp_Symbol
*s
= indirect_variable (XSYMBOL (sym
));
904 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
905 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
906 \(The alternative is a variable used internally in a Lisp program.)
907 A variable is a user variable if
908 \(1) the first character of its documentation is `*', or
909 \(2) it is customizable (its property list contains a non-nil value
910 of `standard-value' or `custom-autoload'), or
911 \(3) it is an alias for another user variable.
912 Return nil if VARIABLE is an alias and there is a loop in the
913 chain of symbols. */)
914 (Lisp_Object variable
)
916 Lisp_Object documentation
;
918 if (!SYMBOLP (variable
))
921 /* If indirect and there's an alias loop, don't check anything else. */
922 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
923 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
924 Qt
, user_variable_p_eh
)))
929 documentation
= Fget (variable
, Qvariable_documentation
);
930 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
932 if (STRINGP (documentation
)
933 && ((unsigned char) SREF (documentation
, 0) == '*'))
935 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
936 if (CONSP (documentation
)
937 && STRINGP (XCAR (documentation
))
938 && INTEGERP (XCDR (documentation
))
939 && XINT (XCDR (documentation
)) < 0)
941 /* Customizable? See `custom-variable-p'. */
942 if ((!NILP (Fget (variable
, intern ("standard-value"))))
943 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
946 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
949 /* An indirect variable? Let's follow the chain. */
950 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
954 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
955 doc
: /* Bind variables according to VARLIST then eval BODY.
956 The value of the last form in BODY is returned.
957 Each element of VARLIST is a symbol (which is bound to nil)
958 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
959 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
960 usage: (let* VARLIST BODY...) */)
963 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
964 int count
= SPECPDL_INDEX ();
965 struct gcpro gcpro1
, gcpro2
, gcpro3
;
967 GCPRO3 (args
, elt
, varlist
);
969 lexenv
= Vinternal_interpreter_environment
;
971 varlist
= Fcar (args
);
972 while (CONSP (varlist
))
976 elt
= XCAR (varlist
);
982 else if (! NILP (Fcdr (Fcdr (elt
))))
983 signal_error ("`let' bindings can have only one value-form", elt
);
987 val
= eval_sub (Fcar (Fcdr (elt
)));
990 if (!NILP (lexenv
) && SYMBOLP (var
)
991 && !XSYMBOL (var
)->declared_special
992 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
993 /* Lexically bind VAR by adding it to the interpreter's binding
997 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
998 if (EQ (Vinternal_interpreter_environment
, lexenv
))
999 /* Save the old lexical environment on the specpdl stack,
1000 but only for the first lexical binding, since we'll never
1001 need to revert to one of the intermediate ones. */
1002 specbind (Qinternal_interpreter_environment
, newenv
);
1004 Vinternal_interpreter_environment
= newenv
;
1007 specbind (var
, val
);
1009 varlist
= XCDR (varlist
);
1014 val
= Fprogn (Fcdr (args
));
1016 return unbind_to (count
, val
);
1019 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1020 doc
: /* Bind variables according to VARLIST then eval BODY.
1021 The value of the last form in BODY is returned.
1022 Each element of VARLIST is a symbol (which is bound to nil)
1023 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1024 All the VALUEFORMs are evalled before any symbols are bound.
1025 usage: (let VARLIST BODY...) */)
1028 Lisp_Object
*temps
, tem
, lexenv
;
1029 register Lisp_Object elt
, varlist
;
1030 int count
= SPECPDL_INDEX ();
1031 register int argnum
;
1032 struct gcpro gcpro1
, gcpro2
;
1035 varlist
= Fcar (args
);
1037 /* Make space to hold the values to give the bound variables */
1038 elt
= Flength (varlist
);
1039 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
1041 /* Compute the values and store them in `temps' */
1043 GCPRO2 (args
, *temps
);
1046 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1049 elt
= XCAR (varlist
);
1051 temps
[argnum
++] = Qnil
;
1052 else if (! NILP (Fcdr (Fcdr (elt
))))
1053 signal_error ("`let' bindings can have only one value-form", elt
);
1055 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
1056 gcpro2
.nvars
= argnum
;
1060 lexenv
= Vinternal_interpreter_environment
;
1062 varlist
= Fcar (args
);
1063 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1067 elt
= XCAR (varlist
);
1068 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
1069 tem
= temps
[argnum
++];
1071 if (!NILP (lexenv
) && SYMBOLP (var
)
1072 && !XSYMBOL (var
)->declared_special
1073 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
1074 /* Lexically bind VAR by adding it to the lexenv alist. */
1075 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
1077 /* Dynamically bind VAR. */
1078 specbind (var
, tem
);
1081 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
1082 /* Instantiate a new lexical environment. */
1083 specbind (Qinternal_interpreter_environment
, lexenv
);
1085 elt
= Fprogn (Fcdr (args
));
1087 return unbind_to (count
, elt
);
1090 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1091 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1092 The order of execution is thus TEST, BODY, TEST, BODY and so on
1093 until TEST returns nil.
1094 usage: (while TEST BODY...) */)
1097 Lisp_Object test
, body
;
1098 struct gcpro gcpro1
, gcpro2
;
1100 GCPRO2 (test
, body
);
1104 while (!NILP (eval_sub (test
)))
1114 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1115 doc
: /* Return result of expanding macros at top level of FORM.
1116 If FORM is not a macro call, it is returned unchanged.
1117 Otherwise, the macro is expanded and the expansion is considered
1118 in place of FORM. When a non-macro-call results, it is returned.
1120 The second optional arg ENVIRONMENT specifies an environment of macro
1121 definitions to shadow the loaded ones for use in file byte-compilation. */)
1122 (Lisp_Object form
, Lisp_Object environment
)
1124 /* With cleanups from Hallvard Furuseth. */
1125 register Lisp_Object expander
, sym
, def
, tem
;
1129 /* Come back here each time we expand a macro call,
1130 in case it expands into another macro call. */
1133 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1134 def
= sym
= XCAR (form
);
1136 /* Trace symbols aliases to other symbols
1137 until we get a symbol that is not an alias. */
1138 while (SYMBOLP (def
))
1142 tem
= Fassq (sym
, environment
);
1145 def
= XSYMBOL (sym
)->function
;
1146 if (!EQ (def
, Qunbound
))
1151 /* Right now TEM is the result from SYM in ENVIRONMENT,
1152 and if TEM is nil then DEF is SYM's function definition. */
1155 /* SYM is not mentioned in ENVIRONMENT.
1156 Look at its function definition. */
1157 if (EQ (def
, Qunbound
) || !CONSP (def
))
1158 /* Not defined or definition not suitable */
1160 if (EQ (XCAR (def
), Qautoload
))
1162 /* Autoloading function: will it be a macro when loaded? */
1163 tem
= Fnth (make_number (4), def
);
1164 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1165 /* Yes, load it and try again. */
1167 struct gcpro gcpro1
;
1169 do_autoload (def
, sym
);
1176 else if (!EQ (XCAR (def
), Qmacro
))
1178 else expander
= XCDR (def
);
1182 expander
= XCDR (tem
);
1183 if (NILP (expander
))
1186 form
= apply1 (expander
, XCDR (form
));
1191 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1192 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1193 TAG is evalled to get the tag to use; it must not be nil.
1195 Then the BODY is executed.
1196 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1197 If no throw happens, `catch' returns the value of the last BODY form.
1198 If a throw happens, it specifies the value to return from `catch'.
1199 usage: (catch TAG BODY...) */)
1202 register Lisp_Object tag
;
1203 struct gcpro gcpro1
;
1206 tag
= eval_sub (Fcar (args
));
1208 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1211 /* Set up a catch, then call C function FUNC on argument ARG.
1212 FUNC should return a Lisp_Object.
1213 This is how catches are done from within C code. */
1216 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1218 /* This structure is made part of the chain `catchlist'. */
1221 /* Fill in the components of c, and put it on the list. */
1225 c
.backlist
= backtrace_list
;
1226 c
.handlerlist
= handlerlist
;
1227 c
.lisp_eval_depth
= lisp_eval_depth
;
1228 c
.pdlcount
= SPECPDL_INDEX ();
1229 c
.poll_suppress_count
= poll_suppress_count
;
1230 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1231 c
.gcpro
= gcprolist
;
1232 c
.byte_stack
= byte_stack_list
;
1236 if (! _setjmp (c
.jmp
))
1237 c
.val
= (*func
) (arg
);
1239 /* Throw works by a longjmp that comes right here. */
1244 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1245 jump to that CATCH, returning VALUE as the value of that catch.
1247 This is the guts Fthrow and Fsignal; they differ only in the way
1248 they choose the catch tag to throw to. A catch tag for a
1249 condition-case form has a TAG of Qnil.
1251 Before each catch is discarded, unbind all special bindings and
1252 execute all unwind-protect clauses made above that catch. Unwind
1253 the handler stack as we go, so that the proper handlers are in
1254 effect for each unwind-protect clause we run. At the end, restore
1255 some static info saved in CATCH, and longjmp to the location
1258 This is used for correct unwinding in Fthrow and Fsignal. */
1261 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1263 register int last_time
;
1265 /* Save the value in the tag. */
1268 /* Restore certain special C variables. */
1269 set_poll_suppress_count (catch->poll_suppress_count
);
1270 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1271 handling_signal
= 0;
1276 last_time
= catchlist
== catch;
1278 /* Unwind the specpdl stack, and then restore the proper set of
1280 unbind_to (catchlist
->pdlcount
, Qnil
);
1281 handlerlist
= catchlist
->handlerlist
;
1282 catchlist
= catchlist
->next
;
1284 while (! last_time
);
1287 /* If x_catch_errors was done, turn it off now.
1288 (First we give unbind_to a chance to do that.) */
1289 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1290 The catch must remain in effect during that delicate
1291 state. --lorentey */
1292 x_fully_uncatch_errors ();
1296 byte_stack_list
= catch->byte_stack
;
1297 gcprolist
= catch->gcpro
;
1300 gcpro_level
= gcprolist
->level
+ 1;
1304 backtrace_list
= catch->backlist
;
1305 lisp_eval_depth
= catch->lisp_eval_depth
;
1307 _longjmp (catch->jmp
, 1);
1310 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1311 doc
: /* Throw to the catch for TAG and return VALUE from it.
1312 Both TAG and VALUE are evalled. */)
1313 (register Lisp_Object tag
, Lisp_Object value
)
1315 register struct catchtag
*c
;
1318 for (c
= catchlist
; c
; c
= c
->next
)
1320 if (EQ (c
->tag
, tag
))
1321 unwind_to_catch (c
, value
);
1323 xsignal2 (Qno_catch
, tag
, value
);
1327 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1328 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1329 If BODYFORM completes normally, its value is returned
1330 after executing the UNWINDFORMS.
1331 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1332 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1336 int count
= SPECPDL_INDEX ();
1338 record_unwind_protect (Fprogn
, Fcdr (args
));
1339 val
= eval_sub (Fcar (args
));
1340 return unbind_to (count
, val
);
1343 /* Chain of condition handlers currently in effect.
1344 The elements of this chain are contained in the stack frames
1345 of Fcondition_case and internal_condition_case.
1346 When an error is signaled (by calling Fsignal, below),
1347 this chain is searched for an element that applies. */
1349 struct handler
*handlerlist
;
1351 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1352 doc
: /* Regain control when an error is signaled.
1353 Executes BODYFORM and returns its value if no error happens.
1354 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1355 where the BODY is made of Lisp expressions.
1357 A handler is applicable to an error
1358 if CONDITION-NAME is one of the error's condition names.
1359 If an error happens, the first applicable handler is run.
1361 The car of a handler may be a list of condition names
1362 instead of a single condition name. Then it handles all of them.
1364 When a handler handles an error, control returns to the `condition-case'
1365 and it executes the handler's BODY...
1366 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1367 \(If VAR is nil, the handler can't access that information.)
1368 Then the value of the last BODY form is returned from the `condition-case'
1371 See also the function `signal' for more info.
1372 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1375 register Lisp_Object bodyform
, handlers
;
1376 volatile Lisp_Object var
;
1379 bodyform
= Fcar (Fcdr (args
));
1380 handlers
= Fcdr (Fcdr (args
));
1382 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1385 /* Like Fcondition_case, but the args are separate
1386 rather than passed in a list. Used by Fbyte_code. */
1389 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1390 Lisp_Object handlers
)
1398 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1404 && (SYMBOLP (XCAR (tem
))
1405 || CONSP (XCAR (tem
))))))
1406 error ("Invalid condition handler", tem
);
1411 c
.backlist
= backtrace_list
;
1412 c
.handlerlist
= handlerlist
;
1413 c
.lisp_eval_depth
= lisp_eval_depth
;
1414 c
.pdlcount
= SPECPDL_INDEX ();
1415 c
.poll_suppress_count
= poll_suppress_count
;
1416 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1417 c
.gcpro
= gcprolist
;
1418 c
.byte_stack
= byte_stack_list
;
1419 if (_setjmp (c
.jmp
))
1422 specbind (h
.var
, c
.val
);
1423 val
= Fprogn (Fcdr (h
.chosen_clause
));
1425 /* Note that this just undoes the binding of h.var; whoever
1426 longjumped to us unwound the stack to c.pdlcount before
1428 unbind_to (c
.pdlcount
, Qnil
);
1435 h
.handler
= handlers
;
1436 h
.next
= handlerlist
;
1440 val
= eval_sub (bodyform
);
1442 handlerlist
= h
.next
;
1446 /* Call the function BFUN with no arguments, catching errors within it
1447 according to HANDLERS. If there is an error, call HFUN with
1448 one argument which is the data that describes the error:
1451 HANDLERS can be a list of conditions to catch.
1452 If HANDLERS is Qt, catch all errors.
1453 If HANDLERS is Qerror, catch all errors
1454 but allow the debugger to run if that is enabled. */
1457 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1458 Lisp_Object (*hfun
) (Lisp_Object
))
1464 /* Since Fsignal will close off all calls to x_catch_errors,
1465 we will get the wrong results if some are not closed now. */
1467 if (x_catching_errors ())
1473 c
.backlist
= backtrace_list
;
1474 c
.handlerlist
= handlerlist
;
1475 c
.lisp_eval_depth
= lisp_eval_depth
;
1476 c
.pdlcount
= SPECPDL_INDEX ();
1477 c
.poll_suppress_count
= poll_suppress_count
;
1478 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1479 c
.gcpro
= gcprolist
;
1480 c
.byte_stack
= byte_stack_list
;
1481 if (_setjmp (c
.jmp
))
1483 return (*hfun
) (c
.val
);
1487 h
.handler
= handlers
;
1489 h
.next
= handlerlist
;
1495 handlerlist
= h
.next
;
1499 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1502 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1503 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1509 /* Since Fsignal will close off all calls to x_catch_errors,
1510 we will get the wrong results if some are not closed now. */
1512 if (x_catching_errors ())
1518 c
.backlist
= backtrace_list
;
1519 c
.handlerlist
= handlerlist
;
1520 c
.lisp_eval_depth
= lisp_eval_depth
;
1521 c
.pdlcount
= SPECPDL_INDEX ();
1522 c
.poll_suppress_count
= poll_suppress_count
;
1523 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1524 c
.gcpro
= gcprolist
;
1525 c
.byte_stack
= byte_stack_list
;
1526 if (_setjmp (c
.jmp
))
1528 return (*hfun
) (c
.val
);
1532 h
.handler
= handlers
;
1534 h
.next
= handlerlist
;
1538 val
= (*bfun
) (arg
);
1540 handlerlist
= h
.next
;
1544 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1548 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1551 Lisp_Object handlers
,
1552 Lisp_Object (*hfun
) (Lisp_Object
))
1558 /* Since Fsignal will close off all calls to x_catch_errors,
1559 we will get the wrong results if some are not closed now. */
1561 if (x_catching_errors ())
1567 c
.backlist
= backtrace_list
;
1568 c
.handlerlist
= handlerlist
;
1569 c
.lisp_eval_depth
= lisp_eval_depth
;
1570 c
.pdlcount
= SPECPDL_INDEX ();
1571 c
.poll_suppress_count
= poll_suppress_count
;
1572 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1573 c
.gcpro
= gcprolist
;
1574 c
.byte_stack
= byte_stack_list
;
1575 if (_setjmp (c
.jmp
))
1577 return (*hfun
) (c
.val
);
1581 h
.handler
= handlers
;
1583 h
.next
= handlerlist
;
1587 val
= (*bfun
) (arg1
, arg2
);
1589 handlerlist
= h
.next
;
1593 /* Like internal_condition_case but call BFUN with NARGS as first,
1594 and ARGS as second argument. */
1597 internal_condition_case_n (Lisp_Object (*bfun
) (int, Lisp_Object
*),
1600 Lisp_Object handlers
,
1601 Lisp_Object (*hfun
) (Lisp_Object
))
1607 /* Since Fsignal will close off all calls to x_catch_errors,
1608 we will get the wrong results if some are not closed now. */
1610 if (x_catching_errors ())
1616 c
.backlist
= backtrace_list
;
1617 c
.handlerlist
= handlerlist
;
1618 c
.lisp_eval_depth
= lisp_eval_depth
;
1619 c
.pdlcount
= SPECPDL_INDEX ();
1620 c
.poll_suppress_count
= poll_suppress_count
;
1621 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1622 c
.gcpro
= gcprolist
;
1623 c
.byte_stack
= byte_stack_list
;
1624 if (_setjmp (c
.jmp
))
1626 return (*hfun
) (c
.val
);
1630 h
.handler
= handlers
;
1632 h
.next
= handlerlist
;
1636 val
= (*bfun
) (nargs
, args
);
1638 handlerlist
= h
.next
;
1643 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1644 Lisp_Object
, Lisp_Object
);
1645 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1648 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1649 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1650 This function does not return.
1652 An error symbol is a symbol with an `error-conditions' property
1653 that is a list of condition names.
1654 A handler for any of those names will get to handle this signal.
1655 The symbol `error' should normally be one of them.
1657 DATA should be a list. Its elements are printed as part of the error message.
1658 See Info anchor `(elisp)Definition of signal' for some details on how this
1659 error message is constructed.
1660 If the signal is handled, DATA is made available to the handler.
1661 See also the function `condition-case'. */)
1662 (Lisp_Object error_symbol
, Lisp_Object data
)
1664 /* When memory is full, ERROR-SYMBOL is nil,
1665 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1666 That is a special case--don't do this in other situations. */
1667 Lisp_Object conditions
;
1669 Lisp_Object real_error_symbol
1670 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1671 register Lisp_Object clause
= Qnil
;
1673 struct backtrace
*bp
;
1675 immediate_quit
= handling_signal
= 0;
1677 if (gc_in_progress
|| waiting_for_input
)
1680 #if 0 /* rms: I don't know why this was here,
1681 but it is surely wrong for an error that is handled. */
1682 #ifdef HAVE_WINDOW_SYSTEM
1683 if (display_hourglass_p
)
1684 cancel_hourglass ();
1688 /* This hook is used by edebug. */
1689 if (! NILP (Vsignal_hook_function
)
1690 && ! NILP (error_symbol
))
1692 /* Edebug takes care of restoring these variables when it exits. */
1693 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1694 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1696 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1697 max_specpdl_size
= SPECPDL_INDEX () + 40;
1699 call2 (Vsignal_hook_function
, error_symbol
, data
);
1702 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1704 /* Remember from where signal was called. Skip over the frame for
1705 `signal' itself. If a frame for `error' follows, skip that,
1706 too. Don't do this when ERROR_SYMBOL is nil, because that
1707 is a memory-full error. */
1708 Vsignaling_function
= Qnil
;
1709 if (backtrace_list
&& !NILP (error_symbol
))
1711 bp
= backtrace_list
->next
;
1712 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1714 if (bp
&& bp
->function
)
1715 Vsignaling_function
= *bp
->function
;
1718 for (h
= handlerlist
; h
; h
= h
->next
)
1720 clause
= find_handler_clause (h
->handler
, conditions
,
1721 error_symbol
, data
);
1726 if (/* Don't run the debugger for a memory-full error.
1727 (There is no room in memory to do that!) */
1728 !NILP (error_symbol
)
1729 && (!NILP (Vdebug_on_signal
)
1730 /* If no handler is present now, try to run the debugger. */
1732 /* Special handler that means "print a message and run debugger
1734 || EQ (h
->handler
, Qerror
)))
1737 = maybe_call_debugger (conditions
, error_symbol
, data
);
1738 /* We can't return values to code which signaled an error, but we
1739 can continue code which has signaled a quit. */
1740 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1746 Lisp_Object unwind_data
1747 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1749 h
->chosen_clause
= clause
;
1750 unwind_to_catch (h
->tag
, unwind_data
);
1755 Fthrow (Qtop_level
, Qt
);
1758 if (! NILP (error_symbol
))
1759 data
= Fcons (error_symbol
, data
);
1761 string
= Ferror_message_string (data
);
1762 fatal ("%s", SDATA (string
), 0);
1765 /* Internal version of Fsignal that never returns.
1766 Used for anything but Qquit (which can return from Fsignal). */
1769 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1771 Fsignal (error_symbol
, data
);
1775 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1778 xsignal0 (Lisp_Object error_symbol
)
1780 xsignal (error_symbol
, Qnil
);
1784 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1786 xsignal (error_symbol
, list1 (arg
));
1790 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1792 xsignal (error_symbol
, list2 (arg1
, arg2
));
1796 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1798 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1801 /* Signal `error' with message S, and additional arg ARG.
1802 If ARG is not a genuine list, make it a one-element list. */
1805 signal_error (const char *s
, Lisp_Object arg
)
1807 Lisp_Object tortoise
, hare
;
1809 hare
= tortoise
= arg
;
1810 while (CONSP (hare
))
1817 tortoise
= XCDR (tortoise
);
1819 if (EQ (hare
, tortoise
))
1824 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1826 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1830 /* Return nonzero if LIST is a non-nil atom or
1831 a list containing one of CONDITIONS. */
1834 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1841 while (CONSP (conditions
))
1843 Lisp_Object
this, tail
;
1844 this = XCAR (conditions
);
1845 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1846 if (EQ (XCAR (tail
), this))
1848 conditions
= XCDR (conditions
);
1853 /* Return 1 if an error with condition-symbols CONDITIONS,
1854 and described by SIGNAL-DATA, should skip the debugger
1855 according to debugger-ignored-errors. */
1858 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1861 int first_string
= 1;
1862 Lisp_Object error_message
;
1864 error_message
= Qnil
;
1865 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1867 if (STRINGP (XCAR (tail
)))
1871 error_message
= Ferror_message_string (data
);
1875 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1880 Lisp_Object contail
;
1882 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1883 if (EQ (XCAR (tail
), XCAR (contail
)))
1891 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1892 SIG and DATA describe the signal, as in find_handler_clause. */
1895 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1897 Lisp_Object combined_data
;
1899 combined_data
= Fcons (sig
, data
);
1902 /* Don't try to run the debugger with interrupts blocked.
1903 The editing loop would return anyway. */
1905 /* Does user want to enter debugger for this kind of error? */
1908 : wants_debugger (Vdebug_on_error
, conditions
))
1909 && ! skip_debugger (conditions
, combined_data
)
1910 /* rms: what's this for? */
1911 && when_entered_debugger
< num_nonmacro_input_events
)
1913 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1920 /* Value of Qlambda means we have called debugger and user has continued.
1921 There are two ways to pass SIG and DATA:
1922 = SIG is the error symbol, and DATA is the rest of the data.
1923 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1924 This is for memory-full errors only.
1926 We need to increase max_specpdl_size temporarily around
1927 anything we do that can push on the specpdl, so as not to get
1928 a second error here in case we're handling specpdl overflow. */
1931 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1932 Lisp_Object sig
, Lisp_Object data
)
1934 register Lisp_Object h
;
1936 /* t is used by handlers for all conditions, set up by C code. */
1937 if (EQ (handlers
, Qt
))
1940 /* error is used similarly, but means print an error message
1941 and run the debugger if that is enabled. */
1942 if (EQ (handlers
, Qerror
))
1945 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1947 Lisp_Object handler
= XCAR (h
);
1948 Lisp_Object condit
, tem
;
1950 if (!CONSP (handler
))
1952 condit
= XCAR (handler
);
1953 /* Handle a single condition name in handler HANDLER. */
1954 if (SYMBOLP (condit
))
1956 tem
= Fmemq (Fcar (handler
), conditions
);
1960 /* Handle a list of condition names in handler HANDLER. */
1961 else if (CONSP (condit
))
1964 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1966 tem
= Fmemq (XCAR (tail
), conditions
);
1977 /* dump an error message; called like vprintf */
1979 verror (const char *m
, va_list ap
)
1982 EMACS_INT size
= 200;
1993 used
= doprnt (buffer
, size
, m
, m
+ mlen
, ap
);
1998 buffer
= (char *) xrealloc (buffer
, size
);
2001 buffer
= (char *) xmalloc (size
);
2006 string
= build_string (buffer
);
2010 xsignal1 (Qerror
, string
);
2014 /* dump an error message; called like printf */
2018 error (const char *m
, ...)
2026 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
2027 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
2028 This means it contains a description for how to read arguments to give it.
2029 The value is nil for an invalid function or a symbol with no function
2032 Interactively callable functions include strings and vectors (treated
2033 as keyboard macros), lambda-expressions that contain a top-level call
2034 to `interactive', autoload definitions made by `autoload' with non-nil
2035 fourth argument, and some of the built-in functions of Lisp.
2037 Also, a symbol satisfies `commandp' if its function definition does so.
2039 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2040 then strings and vectors are not accepted. */)
2041 (Lisp_Object function
, Lisp_Object for_call_interactively
)
2043 register Lisp_Object fun
;
2044 register Lisp_Object funcar
;
2045 Lisp_Object if_prop
= Qnil
;
2049 fun
= indirect_function (fun
); /* Check cycles. */
2050 if (NILP (fun
) || EQ (fun
, Qunbound
))
2053 /* Check an `interactive-form' property if present, analogous to the
2054 function-documentation property. */
2056 while (SYMBOLP (fun
))
2058 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2061 fun
= Fsymbol_function (fun
);
2064 /* Emacs primitives are interactive if their DEFUN specifies an
2065 interactive spec. */
2067 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2069 /* Bytecode objects are interactive if they are long enough to
2070 have an element whose index is COMPILED_INTERACTIVE, which is
2071 where the interactive spec is stored. */
2072 else if (COMPILEDP (fun
))
2073 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2076 /* Strings and vectors are keyboard macros. */
2077 if (STRINGP (fun
) || VECTORP (fun
))
2078 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2080 /* Lists may represent commands. */
2083 funcar
= XCAR (fun
);
2084 if (EQ (funcar
, Qclosure
))
2085 return !NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2086 else if (EQ (funcar
, Qlambda
))
2087 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2088 else if (EQ (funcar
, Qautoload
))
2089 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2094 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2095 doc
: /* Define FUNCTION to autoload from FILE.
2096 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2097 Third arg DOCSTRING is documentation for the function.
2098 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2099 Fifth arg TYPE indicates the type of the object:
2100 nil or omitted says FUNCTION is a function,
2101 `keymap' says FUNCTION is really a keymap, and
2102 `macro' or t says FUNCTION is really a macro.
2103 Third through fifth args give info about the real definition.
2104 They default to nil.
2105 If FUNCTION is already defined other than as an autoload,
2106 this does nothing and returns nil. */)
2107 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2109 CHECK_SYMBOL (function
);
2110 CHECK_STRING (file
);
2112 /* If function is defined and not as an autoload, don't override */
2113 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2114 && !(CONSP (XSYMBOL (function
)->function
)
2115 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2118 if (NILP (Vpurify_flag
))
2119 /* Only add entries after dumping, because the ones before are
2120 not useful and else we get loads of them from the loaddefs.el. */
2121 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2123 /* We don't want the docstring in purespace (instead,
2124 Snarf-documentation should (hopefully) overwrite it).
2125 We used to use 0 here, but that leads to accidental sharing in
2126 purecopy's hash-consing, so we use a (hopefully) unique integer
2128 docstring
= make_number (XHASH (function
));
2129 return Ffset (function
,
2130 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2131 interactive
, type
)));
2135 un_autoload (Lisp_Object oldqueue
)
2137 register Lisp_Object queue
, first
, second
;
2139 /* Queue to unwind is current value of Vautoload_queue.
2140 oldqueue is the shadowed value to leave in Vautoload_queue. */
2141 queue
= Vautoload_queue
;
2142 Vautoload_queue
= oldqueue
;
2143 while (CONSP (queue
))
2145 first
= XCAR (queue
);
2146 second
= Fcdr (first
);
2147 first
= Fcar (first
);
2148 if (EQ (first
, make_number (0)))
2151 Ffset (first
, second
);
2152 queue
= XCDR (queue
);
2157 /* Load an autoloaded function.
2158 FUNNAME is the symbol which is the function's name.
2159 FUNDEF is the autoload definition (a list). */
2162 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2164 int count
= SPECPDL_INDEX ();
2166 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2168 /* This is to make sure that loadup.el gives a clear picture
2169 of what files are preloaded and when. */
2170 if (! NILP (Vpurify_flag
))
2171 error ("Attempt to autoload %s while preparing to dump",
2172 SDATA (SYMBOL_NAME (funname
)));
2175 CHECK_SYMBOL (funname
);
2176 GCPRO3 (fun
, funname
, fundef
);
2178 /* Preserve the match data. */
2179 record_unwind_save_match_data ();
2181 /* If autoloading gets an error (which includes the error of failing
2182 to define the function being called), we use Vautoload_queue
2183 to undo function definitions and `provide' calls made by
2184 the function. We do this in the specific case of autoloading
2185 because autoloading is not an explicit request "load this file",
2186 but rather a request to "call this function".
2188 The value saved here is to be restored into Vautoload_queue. */
2189 record_unwind_protect (un_autoload
, Vautoload_queue
);
2190 Vautoload_queue
= Qt
;
2191 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2193 /* Once loading finishes, don't undo it. */
2194 Vautoload_queue
= Qt
;
2195 unbind_to (count
, Qnil
);
2197 fun
= Findirect_function (fun
, Qnil
);
2199 if (!NILP (Fequal (fun
, fundef
)))
2200 error ("Autoloading failed to define function %s",
2201 SDATA (SYMBOL_NAME (funname
)));
2206 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2207 doc
: /* Evaluate FORM and return its value.
2208 If LEXICAL is t, evaluate using lexical scoping. */)
2209 (Lisp_Object form
, Lisp_Object lexical
)
2211 int count
= SPECPDL_INDEX ();
2212 specbind (Qinternal_interpreter_environment
,
2213 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2214 return unbind_to (count
, eval_sub (form
));
2217 /* Eval a sub-expression of the current expression (i.e. in the same
2220 eval_sub (Lisp_Object form
)
2222 Lisp_Object fun
, val
, original_fun
, original_args
;
2224 struct backtrace backtrace
;
2225 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2227 if (handling_signal
)
2232 /* Look up its binding in the lexical environment.
2233 We do not pay attention to the declared_special flag here, since we
2234 already did that when let-binding the variable. */
2235 Lisp_Object lex_binding
2236 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2237 ? Fassq (form
, Vinternal_interpreter_environment
)
2239 if (CONSP (lex_binding
))
2240 return XCDR (lex_binding
);
2242 return Fsymbol_value (form
);
2249 if ((consing_since_gc
> gc_cons_threshold
2250 && consing_since_gc
> gc_relative_threshold
)
2252 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2255 Fgarbage_collect ();
2259 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2261 if (max_lisp_eval_depth
< 100)
2262 max_lisp_eval_depth
= 100;
2263 if (lisp_eval_depth
> max_lisp_eval_depth
)
2264 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2267 original_fun
= Fcar (form
);
2268 original_args
= Fcdr (form
);
2270 backtrace
.next
= backtrace_list
;
2271 backtrace_list
= &backtrace
;
2272 backtrace
.function
= &original_fun
; /* This also protects them from gc */
2273 backtrace
.args
= &original_args
;
2274 backtrace
.nargs
= UNEVALLED
;
2275 backtrace
.evalargs
= 1;
2276 backtrace
.debug_on_exit
= 0;
2278 if (debug_on_next_call
)
2279 do_debug_on_call (Qt
);
2281 /* At this point, only original_fun and original_args
2282 have values that will be used below */
2285 /* Optimize for no indirection. */
2287 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2288 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2289 fun
= indirect_function (fun
);
2293 Lisp_Object numargs
;
2294 Lisp_Object argvals
[8];
2295 Lisp_Object args_left
;
2296 register int i
, maxargs
;
2298 args_left
= original_args
;
2299 numargs
= Flength (args_left
);
2303 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
2304 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2305 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2307 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2309 backtrace
.evalargs
= 0;
2310 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2312 else if (XSUBR (fun
)->max_args
== MANY
)
2314 /* Pass a vector of evaluated arguments */
2316 register int argnum
= 0;
2319 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2321 GCPRO3 (args_left
, fun
, fun
);
2325 while (!NILP (args_left
))
2327 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2328 args_left
= Fcdr (args_left
);
2329 gcpro3
.nvars
= argnum
;
2332 backtrace
.args
= vals
;
2333 backtrace
.nargs
= XINT (numargs
);
2335 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2341 GCPRO3 (args_left
, fun
, fun
);
2342 gcpro3
.var
= argvals
;
2345 maxargs
= XSUBR (fun
)->max_args
;
2346 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2348 argvals
[i
] = eval_sub (Fcar (args_left
));
2354 backtrace
.args
= argvals
;
2355 backtrace
.nargs
= XINT (numargs
);
2360 val
= (XSUBR (fun
)->function
.a0 ());
2363 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2366 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2369 val
= (XSUBR (fun
)->function
.a3
2370 (argvals
[0], argvals
[1], argvals
[2]));
2373 val
= (XSUBR (fun
)->function
.a4
2374 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2377 val
= (XSUBR (fun
)->function
.a5
2378 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2382 val
= (XSUBR (fun
)->function
.a6
2383 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2384 argvals
[4], argvals
[5]));
2387 val
= (XSUBR (fun
)->function
.a7
2388 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2389 argvals
[4], argvals
[5], argvals
[6]));
2393 val
= (XSUBR (fun
)->function
.a8
2394 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2395 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2399 /* Someone has created a subr that takes more arguments than
2400 is supported by this code. We need to either rewrite the
2401 subr to use a different argument protocol, or add more
2402 cases to this switch. */
2407 else if (COMPILEDP (fun
))
2408 val
= apply_lambda (fun
, original_args
);
2411 if (EQ (fun
, Qunbound
))
2412 xsignal1 (Qvoid_function
, original_fun
);
2414 xsignal1 (Qinvalid_function
, original_fun
);
2415 funcar
= XCAR (fun
);
2416 if (!SYMBOLP (funcar
))
2417 xsignal1 (Qinvalid_function
, original_fun
);
2418 if (EQ (funcar
, Qautoload
))
2420 do_autoload (fun
, original_fun
);
2423 if (EQ (funcar
, Qmacro
))
2424 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2425 else if (EQ (funcar
, Qlambda
)
2426 || EQ (funcar
, Qclosure
))
2427 val
= apply_lambda (fun
, original_args
);
2429 xsignal1 (Qinvalid_function
, original_fun
);
2434 if (backtrace
.debug_on_exit
)
2435 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2436 backtrace_list
= backtrace
.next
;
2441 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2442 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2443 Then return the value FUNCTION returns.
2444 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2445 usage: (apply FUNCTION &rest ARGUMENTS) */)
2446 (int nargs
, Lisp_Object
*args
)
2448 register int i
, numargs
;
2449 register Lisp_Object spread_arg
;
2450 register Lisp_Object
*funcall_args
;
2451 Lisp_Object fun
, retval
;
2452 struct gcpro gcpro1
;
2457 spread_arg
= args
[nargs
- 1];
2458 CHECK_LIST (spread_arg
);
2460 numargs
= XINT (Flength (spread_arg
));
2463 return Ffuncall (nargs
- 1, args
);
2464 else if (numargs
== 1)
2466 args
[nargs
- 1] = XCAR (spread_arg
);
2467 return Ffuncall (nargs
, args
);
2470 numargs
+= nargs
- 2;
2472 /* Optimize for no indirection. */
2473 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2474 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2475 fun
= indirect_function (fun
);
2476 if (EQ (fun
, Qunbound
))
2478 /* Let funcall get the error */
2485 if (numargs
< XSUBR (fun
)->min_args
2486 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2487 goto funcall
; /* Let funcall get the error */
2488 else if (XSUBR (fun
)->max_args
> numargs
)
2490 /* Avoid making funcall cons up a yet another new vector of arguments
2491 by explicitly supplying nil's for optional values */
2492 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2493 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2494 funcall_args
[++i
] = Qnil
;
2495 GCPRO1 (*funcall_args
);
2496 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2500 /* We add 1 to numargs because funcall_args includes the
2501 function itself as well as its arguments. */
2504 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2505 GCPRO1 (*funcall_args
);
2506 gcpro1
.nvars
= 1 + numargs
;
2509 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2510 /* Spread the last arg we got. Its first element goes in
2511 the slot that it used to occupy, hence this value of I. */
2513 while (!NILP (spread_arg
))
2515 funcall_args
[i
++] = XCAR (spread_arg
);
2516 spread_arg
= XCDR (spread_arg
);
2519 /* By convention, the caller needs to gcpro Ffuncall's args. */
2520 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2527 /* Run hook variables in various ways. */
2529 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
2530 static Lisp_Object
run_hook_with_args (int, Lisp_Object
*,
2531 enum run_hooks_condition
);
2533 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2534 doc
: /* Run each hook in HOOKS.
2535 Each argument should be a symbol, a hook variable.
2536 These symbols are processed in the order specified.
2537 If a hook symbol has a non-nil value, that value may be a function
2538 or a list of functions to be called to run the hook.
2539 If the value is a function, it is called with no arguments.
2540 If it is a list, the elements are called, in order, with no arguments.
2542 Major modes should not use this function directly to run their mode
2543 hook; they should use `run-mode-hooks' instead.
2545 Do not use `make-local-variable' to make a hook variable buffer-local.
2546 Instead, use `add-hook' and specify t for the LOCAL argument.
2547 usage: (run-hooks &rest HOOKS) */)
2548 (int nargs
, Lisp_Object
*args
)
2550 Lisp_Object hook
[1];
2553 for (i
= 0; i
< nargs
; i
++)
2556 run_hook_with_args (1, hook
, to_completion
);
2562 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2563 Srun_hook_with_args
, 1, MANY
, 0,
2564 doc
: /* Run HOOK with the specified arguments ARGS.
2565 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2566 value, that value may be a function or a list of functions to be
2567 called to run the hook. If the value is a function, it is called with
2568 the given arguments and its return value is returned. If it is a list
2569 of functions, those functions are called, in order,
2570 with the given arguments ARGS.
2571 It is best not to depend on the value returned by `run-hook-with-args',
2574 Do not use `make-local-variable' to make a hook variable buffer-local.
2575 Instead, use `add-hook' and specify t for the LOCAL argument.
2576 usage: (run-hook-with-args HOOK &rest ARGS) */)
2577 (int nargs
, Lisp_Object
*args
)
2579 return run_hook_with_args (nargs
, args
, to_completion
);
2582 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2583 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2584 doc
: /* Run HOOK with the specified arguments ARGS.
2585 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2586 value, that value may be a function or a list of functions to be
2587 called to run the hook. If the value is a function, it is called with
2588 the given arguments and its return value is returned.
2589 If it is a list of functions, those functions are called, in order,
2590 with the given arguments ARGS, until one of them
2591 returns a non-nil value. Then we return that value.
2592 However, if they all return nil, we return nil.
2594 Do not use `make-local-variable' to make a hook variable buffer-local.
2595 Instead, use `add-hook' and specify t for the LOCAL argument.
2596 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2597 (int nargs
, Lisp_Object
*args
)
2599 return run_hook_with_args (nargs
, args
, until_success
);
2602 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2603 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2604 doc
: /* Run HOOK with the specified arguments ARGS.
2605 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2606 value, that value may be a function or a list of functions to be
2607 called to run the hook. If the value is a function, it is called with
2608 the given arguments and its return value is returned.
2609 If it is a list of functions, those functions are called, in order,
2610 with the given arguments ARGS, until one of them returns nil.
2611 Then we return nil. However, if they all return non-nil, we return non-nil.
2613 Do not use `make-local-variable' to make a hook variable buffer-local.
2614 Instead, use `add-hook' and specify t for the LOCAL argument.
2615 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2616 (int nargs
, Lisp_Object
*args
)
2618 return run_hook_with_args (nargs
, args
, until_failure
);
2621 /* ARGS[0] should be a hook symbol.
2622 Call each of the functions in the hook value, passing each of them
2623 as arguments all the rest of ARGS (all NARGS - 1 elements).
2624 COND specifies a condition to test after each call
2625 to decide whether to stop.
2626 The caller (or its caller, etc) must gcpro all of ARGS,
2627 except that it isn't necessary to gcpro ARGS[0]. */
2630 run_hook_with_args (int nargs
, Lisp_Object
*args
, enum run_hooks_condition cond
)
2632 Lisp_Object sym
, val
, ret
;
2633 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2635 /* If we are dying or still initializing,
2636 don't do anything--it would probably crash if we tried. */
2637 if (NILP (Vrun_hooks
))
2641 val
= find_symbol_value (sym
);
2642 ret
= (cond
== until_failure
? Qt
: Qnil
);
2644 if (EQ (val
, Qunbound
) || NILP (val
))
2646 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2649 return Ffuncall (nargs
, args
);
2653 Lisp_Object global_vals
= Qnil
;
2654 GCPRO3 (sym
, val
, global_vals
);
2657 CONSP (val
) && ((cond
== to_completion
)
2658 || (cond
== until_success
? NILP (ret
)
2662 if (EQ (XCAR (val
), Qt
))
2664 /* t indicates this hook has a local binding;
2665 it means to run the global binding too. */
2666 global_vals
= Fdefault_value (sym
);
2667 if (NILP (global_vals
)) continue;
2669 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2671 args
[0] = global_vals
;
2672 ret
= Ffuncall (nargs
, args
);
2677 (CONSP (global_vals
)
2678 && (cond
== to_completion
2679 || (cond
== until_success
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
= Ffuncall (nargs
, args
);
2694 args
[0] = XCAR (val
);
2695 ret
= Ffuncall (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
: /* Return non-nil if OBJECT is a type of object that can be called as 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 (int nargs
, Lisp_Object
*args
)
2912 Lisp_Object fun
, original_fun
;
2914 int 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
.evalargs
= 0;
2942 backtrace
.debug_on_exit
= 0;
2944 if (debug_on_next_call
)
2945 do_debug_on_call (Qlambda
);
2949 original_fun
= args
[0];
2953 /* Optimize for no indirection. */
2955 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2956 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2957 fun
= indirect_function (fun
);
2961 if (numargs
< XSUBR (fun
)->min_args
2962 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2964 XSETFASTINT (lisp_numargs
, numargs
);
2965 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2968 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2969 xsignal1 (Qinvalid_function
, original_fun
);
2971 else if (XSUBR (fun
)->max_args
== MANY
)
2972 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2975 if (XSUBR (fun
)->max_args
> numargs
)
2977 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2978 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2979 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2980 internal_args
[i
] = Qnil
;
2983 internal_args
= args
+ 1;
2984 switch (XSUBR (fun
)->max_args
)
2987 val
= (XSUBR (fun
)->function
.a0 ());
2990 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2993 val
= (XSUBR (fun
)->function
.a2
2994 (internal_args
[0], internal_args
[1]));
2997 val
= (XSUBR (fun
)->function
.a3
2998 (internal_args
[0], internal_args
[1], internal_args
[2]));
3001 val
= (XSUBR (fun
)->function
.a4
3002 (internal_args
[0], internal_args
[1], internal_args
[2],
3006 val
= (XSUBR (fun
)->function
.a5
3007 (internal_args
[0], internal_args
[1], internal_args
[2],
3008 internal_args
[3], internal_args
[4]));
3011 val
= (XSUBR (fun
)->function
.a6
3012 (internal_args
[0], internal_args
[1], internal_args
[2],
3013 internal_args
[3], internal_args
[4], internal_args
[5]));
3016 val
= (XSUBR (fun
)->function
.a7
3017 (internal_args
[0], internal_args
[1], internal_args
[2],
3018 internal_args
[3], internal_args
[4], internal_args
[5],
3023 val
= (XSUBR (fun
)->function
.a8
3024 (internal_args
[0], internal_args
[1], internal_args
[2],
3025 internal_args
[3], internal_args
[4], internal_args
[5],
3026 internal_args
[6], internal_args
[7]));
3031 /* If a subr takes more than 8 arguments without using MANY
3032 or UNEVALLED, we need to extend this function to support it.
3033 Until this is done, there is no way to call the function. */
3038 else if (COMPILEDP (fun
))
3039 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3042 if (EQ (fun
, Qunbound
))
3043 xsignal1 (Qvoid_function
, original_fun
);
3045 xsignal1 (Qinvalid_function
, original_fun
);
3046 funcar
= XCAR (fun
);
3047 if (!SYMBOLP (funcar
))
3048 xsignal1 (Qinvalid_function
, original_fun
);
3049 if (EQ (funcar
, Qlambda
)
3050 || EQ (funcar
, Qclosure
))
3051 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3052 else if (EQ (funcar
, Qautoload
))
3054 do_autoload (fun
, original_fun
);
3059 xsignal1 (Qinvalid_function
, original_fun
);
3063 if (backtrace
.debug_on_exit
)
3064 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3065 backtrace_list
= backtrace
.next
;
3070 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
3072 Lisp_Object args_left
;
3073 Lisp_Object numargs
;
3074 register Lisp_Object
*arg_vector
;
3075 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3077 register Lisp_Object tem
;
3080 numargs
= Flength (args
);
3081 SAFE_ALLOCA_LISP (arg_vector
, XINT (numargs
));
3084 GCPRO3 (*arg_vector
, args_left
, fun
);
3087 for (i
= 0; i
< XINT (numargs
);)
3089 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3090 tem
= eval_sub (tem
);
3091 arg_vector
[i
++] = tem
;
3097 backtrace_list
->args
= arg_vector
;
3098 backtrace_list
->nargs
= i
;
3099 backtrace_list
->evalargs
= 0;
3100 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
3102 /* Do the debug-on-exit now, while arg_vector still exists. */
3103 if (backtrace_list
->debug_on_exit
)
3104 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3105 /* Don't do it again when we return to eval. */
3106 backtrace_list
->debug_on_exit
= 0;
3111 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3112 and return the result of evaluation.
3113 FUN must be either a lambda-expression or a compiled-code object. */
3116 funcall_lambda (Lisp_Object fun
, int nargs
,
3117 register Lisp_Object
*arg_vector
)
3119 Lisp_Object val
, syms_left
, next
, lexenv
;
3120 int count
= SPECPDL_INDEX ();
3121 int i
, optional
, rest
;
3125 if (EQ (XCAR (fun
), Qclosure
))
3127 fun
= XCDR (fun
); /* Drop `closure'. */
3128 lexenv
= XCAR (fun
);
3129 CHECK_LIST_CONS (fun
, fun
);
3133 syms_left
= XCDR (fun
);
3134 if (CONSP (syms_left
))
3135 syms_left
= XCAR (syms_left
);
3137 xsignal1 (Qinvalid_function
, fun
);
3139 else if (COMPILEDP (fun
))
3141 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3142 if (INTEGERP (syms_left
))
3143 /* A byte-code object with a non-nil `push args' slot means we
3144 shouldn't bind any arguments, instead just call the byte-code
3145 interpreter directly; it will push arguments as necessary.
3147 Byte-code objects with either a non-existant, or a nil value for
3148 the `push args' slot (the default), have dynamically-bound
3149 arguments, and use the argument-binding code below instead (as do
3150 all interpreted functions, even lexically bound ones). */
3152 /* If we have not actually read the bytecode string
3153 and constants vector yet, fetch them from the file. */
3154 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3155 Ffetch_bytecode (fun
);
3156 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3157 AREF (fun
, COMPILED_CONSTANTS
),
3158 AREF (fun
, COMPILED_STACK_DEPTH
),
3167 i
= optional
= rest
= 0;
3168 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3172 next
= XCAR (syms_left
);
3173 if (!SYMBOLP (next
))
3174 xsignal1 (Qinvalid_function
, fun
);
3176 if (EQ (next
, Qand_rest
))
3178 else if (EQ (next
, Qand_optional
))
3185 val
= Flist (nargs
- i
, &arg_vector
[i
]);
3189 val
= arg_vector
[i
++];
3191 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3195 /* Bind the argument. */
3196 if (!NILP (lexenv
) && SYMBOLP (next
))
3197 /* Lexically bind NEXT by adding it to the lexenv alist. */
3198 lexenv
= Fcons (Fcons (next
, val
), lexenv
);
3200 /* Dynamically bind NEXT. */
3201 specbind (next
, val
);
3205 if (!NILP (syms_left
))
3206 xsignal1 (Qinvalid_function
, fun
);
3208 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3210 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3211 /* Instantiate a new lexical environment. */
3212 specbind (Qinternal_interpreter_environment
, lexenv
);
3215 val
= Fprogn (XCDR (XCDR (fun
)));
3218 /* If we have not actually read the bytecode string
3219 and constants vector yet, fetch them from the file. */
3220 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3221 Ffetch_bytecode (fun
);
3222 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3223 AREF (fun
, COMPILED_CONSTANTS
),
3224 AREF (fun
, COMPILED_STACK_DEPTH
),
3228 return unbind_to (count
, val
);
3231 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3233 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3234 (Lisp_Object object
)
3238 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3240 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3243 tem
= AREF (object
, COMPILED_BYTECODE
);
3244 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3245 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3247 error ("Invalid byte code");
3249 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3250 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3258 register int count
= SPECPDL_INDEX ();
3259 if (specpdl_size
>= max_specpdl_size
)
3261 if (max_specpdl_size
< 400)
3262 max_specpdl_size
= 400;
3263 if (specpdl_size
>= max_specpdl_size
)
3264 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3267 if (specpdl_size
> max_specpdl_size
)
3268 specpdl_size
= max_specpdl_size
;
3269 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3270 specpdl_ptr
= specpdl
+ count
;
3273 /* specpdl_ptr->symbol is a field which describes which variable is
3274 let-bound, so it can be properly undone when we unbind_to.
3275 It can have the following two shapes:
3276 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3277 a symbol that is not buffer-local (at least at the time
3278 the let binding started). Note also that it should not be
3279 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3281 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3282 variable SYMBOL which can be buffer-local. WHERE tells us
3283 which buffer is affected (or nil if the let-binding affects the
3284 global value of the variable) and BUFFER tells us which buffer was
3285 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3286 BUFFER did not yet have a buffer-local value). */
3289 specbind (Lisp_Object symbol
, Lisp_Object value
)
3291 struct Lisp_Symbol
*sym
;
3293 eassert (!handling_signal
);
3295 CHECK_SYMBOL (symbol
);
3296 sym
= XSYMBOL (symbol
);
3297 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3301 switch (sym
->redirect
)
3303 case SYMBOL_VARALIAS
:
3304 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3305 case SYMBOL_PLAINVAL
:
3306 /* The most common case is that of a non-constant symbol with a
3307 trivial value. Make that as fast as we can. */
3308 specpdl_ptr
->symbol
= symbol
;
3309 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3310 specpdl_ptr
->func
= NULL
;
3313 SET_SYMBOL_VAL (sym
, value
);
3315 set_internal (symbol
, value
, Qnil
, 1);
3317 case SYMBOL_LOCALIZED
:
3318 if (SYMBOL_BLV (sym
)->frame_local
)
3319 error ("Frame-local vars cannot be let-bound");
3320 case SYMBOL_FORWARDED
:
3322 Lisp_Object ovalue
= find_symbol_value (symbol
);
3323 specpdl_ptr
->func
= 0;
3324 specpdl_ptr
->old_value
= ovalue
;
3326 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3327 || (EQ (SYMBOL_BLV (sym
)->where
,
3328 SYMBOL_BLV (sym
)->frame_local
?
3329 Fselected_frame () : Fcurrent_buffer ())));
3331 if (sym
->redirect
== SYMBOL_LOCALIZED
3332 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3334 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3336 /* For a local variable, record both the symbol and which
3337 buffer's or frame's value we are saving. */
3338 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3340 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3341 || (BLV_FOUND (SYMBOL_BLV (sym
))
3342 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3345 else if (sym
->redirect
== SYMBOL_LOCALIZED
3346 && BLV_FOUND (SYMBOL_BLV (sym
)))
3347 where
= SYMBOL_BLV (sym
)->where
;
3351 /* We're not using the `unused' slot in the specbinding
3352 structure because this would mean we have to do more
3353 work for simple variables. */
3354 /* FIXME: The third value `current_buffer' is only used in
3355 let_shadows_buffer_binding_p which is itself only used
3356 in set_internal for local_if_set. */
3357 eassert (NILP (where
) || EQ (where
, cur_buf
));
3358 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3360 /* If SYMBOL is a per-buffer variable which doesn't have a
3361 buffer-local value here, make the `let' change the global
3362 value by changing the value of SYMBOL in all buffers not
3363 having their own value. This is consistent with what
3364 happens with other buffer-local variables. */
3366 && sym
->redirect
== SYMBOL_FORWARDED
)
3368 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3370 Fset_default (symbol
, value
);
3375 specpdl_ptr
->symbol
= symbol
;
3378 set_internal (symbol
, value
, Qnil
, 1);
3386 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3388 eassert (!handling_signal
);
3390 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3392 specpdl_ptr
->func
= function
;
3393 specpdl_ptr
->symbol
= Qnil
;
3394 specpdl_ptr
->old_value
= arg
;
3399 unbind_to (int count
, Lisp_Object value
)
3401 Lisp_Object quitf
= Vquit_flag
;
3402 struct gcpro gcpro1
, gcpro2
;
3404 GCPRO2 (value
, quitf
);
3407 while (specpdl_ptr
!= specpdl
+ count
)
3409 /* Copy the binding, and decrement specpdl_ptr, before we do
3410 the work to unbind it. We decrement first
3411 so that an error in unbinding won't try to unbind
3412 the same entry again, and we copy the binding first
3413 in case more bindings are made during some of the code we run. */
3415 struct specbinding this_binding
;
3416 this_binding
= *--specpdl_ptr
;
3418 if (this_binding
.func
!= 0)
3419 (*this_binding
.func
) (this_binding
.old_value
);
3420 /* If the symbol is a list, it is really (SYMBOL WHERE
3421 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3422 frame. If WHERE is a buffer or frame, this indicates we
3423 bound a variable that had a buffer-local or frame-local
3424 binding. WHERE nil means that the variable had the default
3425 value when it was bound. CURRENT-BUFFER is the buffer that
3426 was current when the variable was bound. */
3427 else if (CONSP (this_binding
.symbol
))
3429 Lisp_Object symbol
, where
;
3431 symbol
= XCAR (this_binding
.symbol
);
3432 where
= XCAR (XCDR (this_binding
.symbol
));
3435 Fset_default (symbol
, this_binding
.old_value
);
3436 /* If `where' is non-nil, reset the value in the appropriate
3437 local binding, but only if that binding still exists. */
3438 else if (BUFFERP (where
)
3439 ? !NILP (Flocal_variable_p (symbol
, where
))
3440 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3441 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3443 /* If variable has a trivial value (no forwarding), we can
3444 just set it. No need to check for constant symbols here,
3445 since that was already done by specbind. */
3446 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3447 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3448 this_binding
.old_value
);
3450 /* NOTE: we only ever come here if make_local_foo was used for
3451 the first time on this var within this let. */
3452 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3455 if (NILP (Vquit_flag
) && !NILP (quitf
))
3464 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3465 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3466 A special variable is one that will be bound dynamically, even in a
3467 context where binding is lexical by default. */)
3468 (Lisp_Object symbol
)
3470 CHECK_SYMBOL (symbol
);
3471 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3476 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3477 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3478 The debugger is entered when that frame exits, if the flag is non-nil. */)
3479 (Lisp_Object level
, Lisp_Object flag
)
3481 register struct backtrace
*backlist
= backtrace_list
;
3484 CHECK_NUMBER (level
);
3486 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3488 backlist
= backlist
->next
;
3492 backlist
->debug_on_exit
= !NILP (flag
);
3497 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3498 doc
: /* Print a trace of Lisp function calls currently active.
3499 Output stream used is value of `standard-output'. */)
3502 register struct backtrace
*backlist
= backtrace_list
;
3506 struct gcpro gcpro1
;
3507 Lisp_Object old_print_level
= Vprint_level
;
3509 if (NILP (Vprint_level
))
3510 XSETFASTINT (Vprint_level
, 8);
3517 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3518 if (backlist
->nargs
== UNEVALLED
)
3520 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3521 write_string ("\n", -1);
3525 tem
= *backlist
->function
;
3526 Fprin1 (tem
, Qnil
); /* This can QUIT */
3527 write_string ("(", -1);
3528 if (backlist
->nargs
== MANY
)
3530 for (tail
= *backlist
->args
, i
= 0;
3532 tail
= Fcdr (tail
), i
++)
3534 if (i
) write_string (" ", -1);
3535 Fprin1 (Fcar (tail
), Qnil
);
3540 for (i
= 0; i
< backlist
->nargs
; i
++)
3542 if (i
) write_string (" ", -1);
3543 Fprin1 (backlist
->args
[i
], Qnil
);
3546 write_string (")\n", -1);
3548 backlist
= backlist
->next
;
3551 Vprint_level
= old_print_level
;
3556 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3557 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3558 If that frame has not evaluated the arguments yet (or is a special form),
3559 the value is (nil FUNCTION ARG-FORMS...).
3560 If that frame has evaluated its arguments and called its function already,
3561 the value is (t FUNCTION ARG-VALUES...).
3562 A &rest arg is represented as the tail of the list ARG-VALUES.
3563 FUNCTION is whatever was supplied as car of evaluated list,
3564 or a lambda expression for macro calls.
3565 If NFRAMES is more than the number of frames, the value is nil. */)
3566 (Lisp_Object nframes
)
3568 register struct backtrace
*backlist
= backtrace_list
;
3572 CHECK_NATNUM (nframes
);
3574 /* Find the frame requested. */
3575 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3576 backlist
= backlist
->next
;
3580 if (backlist
->nargs
== UNEVALLED
)
3581 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3584 if (backlist
->nargs
== MANY
)
3585 tem
= *backlist
->args
;
3587 tem
= Flist (backlist
->nargs
, backlist
->args
);
3589 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3595 mark_backtrace (void)
3597 register struct backtrace
*backlist
;
3600 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3602 mark_object (*backlist
->function
);
3604 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
3607 i
= backlist
->nargs
- 1;
3609 mark_object (backlist
->args
[i
]);
3613 EXFUN (Funintern
, 2);
3618 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3619 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3620 If Lisp code tries to increase the total number past this amount,
3621 an error is signaled.
3622 You can safely use a value considerably larger than the default value,
3623 if that proves inconveniently small. However, if you increase it too far,
3624 Emacs could run out of memory trying to make the stack bigger. */);
3626 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3627 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3629 This limit serves to catch infinite recursions for you before they cause
3630 actual stack overflow in C, which would be fatal for Emacs.
3631 You can safely make it considerably larger than its default value,
3632 if that proves inconveniently small. However, if you increase it too far,
3633 Emacs could overflow the real C stack, and crash. */);
3635 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3636 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3637 If the value is t, that means do an ordinary quit.
3638 If the value equals `throw-on-input', that means quit by throwing
3639 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3640 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3641 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3644 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3645 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3646 Note that `quit-flag' will still be set by typing C-g,
3647 so a quit will be signaled as soon as `inhibit-quit' is nil.
3648 To prevent this happening, set `quit-flag' to nil
3649 before making `inhibit-quit' nil. */);
3650 Vinhibit_quit
= Qnil
;
3652 Qinhibit_quit
= intern_c_string ("inhibit-quit");
3653 staticpro (&Qinhibit_quit
);
3655 Qautoload
= intern_c_string ("autoload");
3656 staticpro (&Qautoload
);
3658 Qdebug_on_error
= intern_c_string ("debug-on-error");
3659 staticpro (&Qdebug_on_error
);
3661 Qmacro
= intern_c_string ("macro");
3662 staticpro (&Qmacro
);
3664 Qdeclare
= intern_c_string ("declare");
3665 staticpro (&Qdeclare
);
3667 /* Note that the process handling also uses Qexit, but we don't want
3668 to staticpro it twice, so we just do it here. */
3669 Qexit
= intern_c_string ("exit");
3672 Qinteractive
= intern_c_string ("interactive");
3673 staticpro (&Qinteractive
);
3675 Qcommandp
= intern_c_string ("commandp");
3676 staticpro (&Qcommandp
);
3678 Qdefun
= intern_c_string ("defun");
3679 staticpro (&Qdefun
);
3681 Qand_rest
= intern_c_string ("&rest");
3682 staticpro (&Qand_rest
);
3684 Qand_optional
= intern_c_string ("&optional");
3685 staticpro (&Qand_optional
);
3687 Qclosure
= intern_c_string ("closure");
3688 staticpro (&Qclosure
);
3690 Qdebug
= intern_c_string ("debug");
3691 staticpro (&Qdebug
);
3693 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3694 doc
: /* *Non-nil means enter debugger if an error is signaled.
3695 Does not apply to errors handled by `condition-case' or those
3696 matched by `debug-ignored-errors'.
3697 If the value is a list, an error only means to enter the debugger
3698 if one of its condition symbols appears in the list.
3699 When you evaluate an expression interactively, this variable
3700 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3701 The command `toggle-debug-on-error' toggles this.
3702 See also the variable `debug-on-quit'. */);
3703 Vdebug_on_error
= Qnil
;
3705 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3706 doc
: /* *List of errors for which the debugger should not be called.
3707 Each element may be a condition-name or a regexp that matches error messages.
3708 If any element applies to a given error, that error skips the debugger
3709 and just returns to top level.
3710 This overrides the variable `debug-on-error'.
3711 It does not apply to errors handled by `condition-case'. */);
3712 Vdebug_ignored_errors
= Qnil
;
3714 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3715 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3716 Does not apply if quit is handled by a `condition-case'. */);
3719 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3720 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3722 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3723 doc
: /* Non-nil means debugger may continue execution.
3724 This is nil when the debugger is called under circumstances where it
3725 might not be safe to continue. */);
3726 debugger_may_continue
= 1;
3728 DEFVAR_LISP ("debugger", Vdebugger
,
3729 doc
: /* Function to call to invoke debugger.
3730 If due to frame exit, args are `exit' and the value being returned;
3731 this function's value will be returned instead of that.
3732 If due to error, args are `error' and a list of the args to `signal'.
3733 If due to `apply' or `funcall' entry, one arg, `lambda'.
3734 If due to `eval' entry, one arg, t. */);
3737 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3738 doc
: /* If non-nil, this is a function for `signal' to call.
3739 It receives the same arguments that `signal' was given.
3740 The Edebug package uses this to regain control. */);
3741 Vsignal_hook_function
= Qnil
;
3743 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3744 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3745 Note that `debug-on-error', `debug-on-quit' and friends
3746 still determine whether to handle the particular condition. */);
3747 Vdebug_on_signal
= Qnil
;
3749 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3750 doc
: /* Function to process declarations in a macro definition.
3751 The function will be called with two args MACRO and DECL.
3752 MACRO is the name of the macro being defined.
3753 DECL is a list `(declare ...)' containing the declarations.
3754 The value the function returns is not used. */);
3755 Vmacro_declaration_function
= Qnil
;
3757 /* When lexical binding is being used,
3758 vinternal_interpreter_environment is non-nil, and contains an alist
3759 of lexically-bound variable, or (t), indicating an empty
3760 environment. The lisp name of this variable would be
3761 `internal-interpreter-environment' if it weren't hidden.
3762 Every element of this list can be either a cons (VAR . VAL)
3763 specifying a lexical binding, or a single symbol VAR indicating
3764 that this variable should use dynamic scoping. */
3765 Qinternal_interpreter_environment
3766 = intern_c_string ("internal-interpreter-environment");
3767 staticpro (&Qinternal_interpreter_environment
);
3768 DEFVAR_LISP ("internal-interpreter-environment",
3769 Vinternal_interpreter_environment
,
3770 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3771 When lexical binding is not being used, this variable is nil.
3772 A value of `(t)' indicates an empty environment, otherwise it is an
3773 alist of active lexical bindings. */);
3774 Vinternal_interpreter_environment
= Qnil
;
3775 /* Don't export this variable to Elisp, so noone can mess with it
3776 (Just imagine if someone makes it buffer-local). */
3777 Funintern (Qinternal_interpreter_environment
, Qnil
);
3779 Vrun_hooks
= intern_c_string ("run-hooks");
3780 staticpro (&Vrun_hooks
);
3782 staticpro (&Vautoload_queue
);
3783 Vautoload_queue
= Qnil
;
3784 staticpro (&Vsignaling_function
);
3785 Vsignaling_function
= Qnil
;
3796 defsubr (&Sfunction
);
3798 defsubr (&Sdefmacro
);
3800 defsubr (&Sdefvaralias
);
3801 defsubr (&Sdefconst
);
3802 defsubr (&Suser_variable_p
);
3806 defsubr (&Smacroexpand
);
3809 defsubr (&Sunwind_protect
);
3810 defsubr (&Scondition_case
);
3812 defsubr (&Sinteractive_p
);
3813 defsubr (&Scalled_interactively_p
);
3814 defsubr (&Scommandp
);
3815 defsubr (&Sautoload
);
3818 defsubr (&Sfuncall
);
3819 defsubr (&Srun_hooks
);
3820 defsubr (&Srun_hook_with_args
);
3821 defsubr (&Srun_hook_with_args_until_success
);
3822 defsubr (&Srun_hook_with_args_until_failure
);
3823 defsubr (&Sfetch_bytecode
);
3824 defsubr (&Sbacktrace_debug
);
3825 defsubr (&Sbacktrace
);
3826 defsubr (&Sbacktrace_frame
);
3827 defsubr (&Sspecial_variable_p
);
3828 defsubr (&Sfunctionp
);