1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1992 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 1, or (at your option)
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; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 /* This definition is duplicated in alloc.c and keyboard.c */
37 /* Putting it in lisp.h makes cc bomb out! */
41 struct backtrace
*next
;
42 Lisp_Object
*function
;
43 Lisp_Object
*args
; /* Points to vector of args. */
44 int nargs
; /* Length of vector.
45 If nargs is UNEVALLED, args points to slot holding
46 list of unevalled args */
48 /* Nonzero means call value of debugger when done with this operation. */
52 struct backtrace
*backtrace_list
;
54 /* This structure helps implement the `catch' and `throw' control
55 structure. A struct catchtag contains all the information needed
56 to restore the state of the interpreter after a non-local jump.
58 Handlers for error conditions (represented by `struct handler'
59 structures) just point to a catch tag to do the cleanup required
62 catchtag structures are chained together in the C calling stack;
63 the `next' member points to the next outer catchtag.
65 A call like (throw TAG VAL) searches for a catchtag whose `tag'
66 member is TAG, and then unbinds to it. The `val' member is used to
67 hold VAL while the stack is unwound; `val' is returned as the value
70 All the other members are concerned with restoring the interpreter
76 struct catchtag
*next
;
79 struct backtrace
*backlist
;
80 struct handler
*handlerlist
;
83 int poll_suppress_count
;
86 struct catchtag
*catchlist
;
88 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
89 Lisp_Object Qinhibit_quit
, Vinhibit_quit
, Vquit_flag
;
90 Lisp_Object Qmocklisp_arguments
, Vmocklisp_arguments
, Qmocklisp
;
91 Lisp_Object Qand_rest
, Qand_optional
;
92 Lisp_Object Qdebug_on_error
;
94 Lisp_Object Vrun_hooks
;
96 /* Non-nil means record all fset's and provide's, to be undone
97 if the file being autoloaded is not fully loaded.
98 They are recorded by being consed onto the front of Vautoload_queue:
99 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
101 Lisp_Object Vautoload_queue
;
103 /* Current number of specbindings allocated in specpdl. */
106 /* Pointer to beginning of specpdl. */
107 struct specbinding
*specpdl
;
109 /* Pointer to first unused element in specpdl. */
110 struct specbinding
*specpdl_ptr
;
112 /* Maximum size allowed for specpdl allocation */
113 int max_specpdl_size
;
115 /* Depth in Lisp evaluations and function calls. */
118 /* Maximum allowed depth in Lisp evaluations and function calls. */
119 int max_lisp_eval_depth
;
121 /* Nonzero means enter debugger before next function call */
122 int debug_on_next_call
;
124 /* List of conditions (non-nil atom means all) which cause a backtrace
125 if an error is handled by the command loop's error handler. */
126 Lisp_Object Vstack_trace_on_error
;
128 /* List of conditions (non-nil atom means all) which enter the debugger
129 if an error is handled by the command loop's error handler. */
130 Lisp_Object Vdebug_on_error
;
132 /* Nonzero means enter debugger if a quit signal
133 is handled by the command loop's error handler. */
136 /* The value of num_nonmacro_input_chars as of the last time we
137 started to enter the debugger. If we decide to enter the debugger
138 again when this is still equal to num_nonmacro_input_chars, then we
139 know that the debugger itself has an error, and we should just
140 signal the error instead of entering an infinite loop of debugger
142 int when_entered_debugger
;
144 Lisp_Object Vdebugger
;
146 void specbind (), record_unwind_protect ();
148 Lisp_Object
funcall_lambda ();
149 extern Lisp_Object
ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
154 specpdl
= (struct specbinding
*) malloc (specpdl_size
* sizeof (struct specbinding
));
155 max_specpdl_size
= 600;
156 max_lisp_eval_depth
= 200;
161 specpdl_ptr
= specpdl
;
166 debug_on_next_call
= 0;
168 when_entered_debugger
= 0;
175 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
176 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
177 if (specpdl_size
+ 40 > max_specpdl_size
)
178 max_specpdl_size
= specpdl_size
+ 40;
179 debug_on_next_call
= 0;
180 when_entered_debugger
= num_nonmacro_input_chars
;
181 return apply1 (Vdebugger
, arg
);
184 do_debug_on_call (code
)
187 debug_on_next_call
= 0;
188 backtrace_list
->debug_on_exit
= 1;
189 call_debugger (Fcons (code
, Qnil
));
192 /* NOTE!!! Every function that can call EVAL must protect its args
193 and temporaries from garbage collection while it needs them.
194 The definition of `For' shows what you have to do. */
196 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
197 "Eval args until one of them yields non-nil, then return that value.\n\
198 The remaining args are not evalled at all.\n\
199 If all args return nil, return nil.")
203 register Lisp_Object val
;
204 Lisp_Object args_left
;
215 val
= Feval (Fcar (args_left
));
218 args_left
= Fcdr (args_left
);
220 while (!NILP(args_left
));
226 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
227 "Eval args until one of them yields nil, then return nil.\n\
228 The remaining args are not evalled at all.\n\
229 If no arg yields nil, return the last arg's value.")
233 register Lisp_Object val
;
234 Lisp_Object args_left
;
245 val
= Feval (Fcar (args_left
));
248 args_left
= Fcdr (args_left
);
250 while (!NILP(args_left
));
256 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
257 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
258 Returns the value of THEN or the value of the last of the ELSE's.\n\
259 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
260 If COND yields nil, and there are no ELSE's, the value is nil.")
264 register Lisp_Object cond
;
268 cond
= Feval (Fcar (args
));
272 return Feval (Fcar (Fcdr (args
)));
273 return Fprogn (Fcdr (Fcdr (args
)));
276 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
277 "(cond CLAUSES...): try each clause until one succeeds.\n\
278 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
279 and, if the value is non-nil, this clause succeeds:\n\
280 then the expressions in BODY are evaluated and the last one's\n\
281 value is the value of the cond-form.\n\
282 If no clause succeeds, cond returns nil.\n\
283 If a clause has one element, as in (CONDITION),\n\
284 CONDITION's value if non-nil is returned from the cond-form.")
288 register Lisp_Object clause
, val
;
295 clause
= Fcar (args
);
296 val
= Feval (Fcar (clause
));
299 if (!EQ (XCONS (clause
)->cdr
, Qnil
))
300 val
= Fprogn (XCONS (clause
)->cdr
);
303 args
= XCONS (args
)->cdr
;
310 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
311 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
315 register Lisp_Object val
, tem
;
316 Lisp_Object args_left
;
319 /* In Mocklisp code, symbols at the front of the progn arglist
320 are to be bound to zero. */
321 if (!EQ (Vmocklisp_arguments
, Qt
))
323 val
= make_number (0);
324 while (!NILP (args
) && (tem
= Fcar (args
), XTYPE (tem
) == Lisp_Symbol
))
327 specbind (tem
, val
), args
= Fcdr (args
);
339 val
= Feval (Fcar (args_left
));
340 args_left
= Fcdr (args_left
);
342 while (!NILP(args_left
));
348 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
349 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
350 The value of FIRST is saved during the evaluation of the remaining args,\n\
351 whose values are discarded.")
356 register Lisp_Object args_left
;
357 struct gcpro gcpro1
, gcpro2
;
358 register int argnum
= 0;
370 val
= Feval (Fcar (args_left
));
372 Feval (Fcar (args_left
));
373 args_left
= Fcdr (args_left
);
375 while (!NILP(args_left
));
381 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
382 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
383 The value of Y is saved during the evaluation of the remaining args,\n\
384 whose values are discarded.")
389 register Lisp_Object args_left
;
390 struct gcpro gcpro1
, gcpro2
;
391 register int argnum
= -1;
405 val
= Feval (Fcar (args_left
));
407 Feval (Fcar (args_left
));
408 args_left
= Fcdr (args_left
);
410 while (!NILP(args_left
));
416 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
417 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
418 The SYMs are not evaluated. Thus (setq x y) sets x to the value of y.\n\
419 Each SYM is set before the next VAL is computed.")
423 register Lisp_Object args_left
;
424 register Lisp_Object val
, sym
;
435 val
= Feval (Fcar (Fcdr (args_left
)));
436 sym
= Fcar (args_left
);
438 args_left
= Fcdr (Fcdr (args_left
));
440 while (!NILP(args_left
));
446 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
447 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
454 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
455 "Like `quote', but preferred for objects which are functions.\n\
456 In byte compilation, `function' causes its argument to be compiled.\n\
457 `quote' cannot do that.")
464 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
465 "Return t if function in which this appears was called interactively.\n\
466 This means that the function was called with call-interactively (which\n\
467 includes being called as the binding of a key)\n\
468 and input is currently coming from the keyboard (not in keyboard macro).")
471 register struct backtrace
*btp
;
472 register Lisp_Object fun
;
477 btp
= backtrace_list
;
479 /* If this isn't a byte-compiled function, there may be a frame at
480 the top for Finteractive_p itself. If so, skip it. */
481 fun
= Findirect_function (*btp
->function
);
482 if (XTYPE (fun
) == Lisp_Subr
483 && (struct Lisp_Subr
*) XPNTR (fun
) == &Sinteractive_p
)
486 /* If we're running an Emacs 18-style byte-compiled function, there
487 may be a frame for Fbytecode. Now, given the strictest
488 definition, this function isn't really being called
489 interactively, but because that's the way Emacs 18 always builds
490 byte-compiled functions, we'll accept it for now. */
491 if (EQ (*btp
->function
, Qbytecode
))
494 /* If this isn't a byte-compiled function, then we may now be
495 looking at several frames for special forms. Skip past them. */
497 btp
->nargs
== UNEVALLED
)
500 /* btp now points at the frame of the innermost function that isn't
501 a special form, ignoring frames for Finteractive_p and/or
502 Fbytecode at the top. If this frame is for a built-in function
503 (such as load or eval-region) return nil. */
504 fun
= Findirect_function (*btp
->function
);
505 if (XTYPE (fun
) == Lisp_Subr
)
507 /* btp points to the frame of a Lisp function that called interactive-p.
508 Return t if that function was called interactively. */
509 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
514 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
515 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
516 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
517 See also the function `interactive'.")
521 register Lisp_Object fn_name
;
522 register Lisp_Object defn
;
524 fn_name
= Fcar (args
);
525 defn
= Fcons (Qlambda
, Fcdr (args
));
526 if (!NILP (Vpurify_flag
))
527 defn
= Fpurecopy (defn
);
528 Ffset (fn_name
, defn
);
532 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
533 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
534 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
535 When the macro is called, as in (NAME ARGS...),\n\
536 the function (lambda ARGLIST BODY...) is applied to\n\
537 the list ARGS... as it appears in the expression,\n\
538 and the result should be a form to be evaluated instead of the original.")
542 register Lisp_Object fn_name
;
543 register Lisp_Object defn
;
545 fn_name
= Fcar (args
);
546 defn
= Fcons (Qmacro
, Fcons (Qlambda
, Fcdr (args
)));
547 if (!NILP (Vpurify_flag
))
548 defn
= Fpurecopy (defn
);
549 Ffset (fn_name
, defn
);
553 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
554 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
555 You are not required to define a variable in order to use it,\n\
556 but the definition can supply documentation and an initial value\n\
557 in a way that tags can recognize.\n\n\
558 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
559 If SYMBOL is buffer-local, its default value is what is set;\n\
560 buffer-local values are not affected.\n\
561 INITVALUE and DOCSTRING are optional.\n\
562 If DOCSTRING starts with *, this variable is identified as a user option.\n\
563 This means that M-x set-variable and M-x edit-options recognize it.\n\
564 If INITVALUE is missing, SYMBOL's value is not set.")
568 register Lisp_Object sym
, tem
;
574 tem
= Fdefault_boundp (sym
);
576 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
578 tem
= Fcar (Fcdr (Fcdr (args
)));
581 if (!NILP (Vpurify_flag
))
582 tem
= Fpurecopy (tem
);
583 Fput (sym
, Qvariable_documentation
, tem
);
588 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
589 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
590 The intent is that programs do not change this value, but users may.\n\
591 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
592 If SYMBOL is buffer-local, its default value is what is set;\n\
593 buffer-local values are not affected.\n\
594 DOCSTRING is optional.\n\
595 If DOCSTRING starts with *, this variable is identified as a user option.\n\
596 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
597 Note: do not use `defconst' for user options in libraries that are not\n\
598 normally loaded, since it is useful for users to be able to specify\n\
599 their own values for such variables before loading the library.\n\
600 Since `defconst' unconditionally assigns the variable,\n\
601 it would override the user's choice.")
605 register Lisp_Object sym
, tem
;
608 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
609 tem
= Fcar (Fcdr (Fcdr (args
)));
612 if (!NILP (Vpurify_flag
))
613 tem
= Fpurecopy (tem
);
614 Fput (sym
, Qvariable_documentation
, tem
);
619 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
620 "Returns t if VARIABLE is intended to be set and modified by users.\n\
621 \(The alternative is a variable used internally in a Lisp program.)\n\
622 Determined by whether the first character of the documentation\n\
623 for the variable is \"*\"")
625 Lisp_Object variable
;
627 Lisp_Object documentation
;
629 documentation
= Fget (variable
, Qvariable_documentation
);
630 if (XTYPE (documentation
) == Lisp_Int
&& XINT (documentation
) < 0)
632 if ((XTYPE (documentation
) == Lisp_String
) &&
633 ((unsigned char) XSTRING (documentation
)->data
[0] == '*'))
638 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
639 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
640 The value of the last form in BODY is returned.\n\
641 Each element of VARLIST is a symbol (which is bound to nil)\n\
642 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
643 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
647 Lisp_Object varlist
, val
, elt
;
648 int count
= specpdl_ptr
- specpdl
;
649 struct gcpro gcpro1
, gcpro2
, gcpro3
;
651 GCPRO3 (args
, elt
, varlist
);
653 varlist
= Fcar (args
);
654 while (!NILP (varlist
))
657 elt
= Fcar (varlist
);
658 if (XTYPE (elt
) == Lisp_Symbol
)
659 specbind (elt
, Qnil
);
660 else if (! NILP (Fcdr (Fcdr (elt
))))
662 Fcons (build_string ("`let' bindings can have only one value-form"),
666 val
= Feval (Fcar (Fcdr (elt
)));
667 specbind (Fcar (elt
), val
);
669 varlist
= Fcdr (varlist
);
672 val
= Fprogn (Fcdr (args
));
673 return unbind_to (count
, val
);
676 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
677 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
678 The value of the last form in BODY is returned.\n\
679 Each element of VARLIST is a symbol (which is bound to nil)\n\
680 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
681 All the VALUEFORMs are evalled before any symbols are bound.")
685 Lisp_Object
*temps
, tem
;
686 register Lisp_Object elt
, varlist
;
687 int count
= specpdl_ptr
- specpdl
;
689 struct gcpro gcpro1
, gcpro2
;
691 varlist
= Fcar (args
);
693 /* Make space to hold the values to give the bound variables */
694 elt
= Flength (varlist
);
695 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
697 /* Compute the values and store them in `temps' */
699 GCPRO2 (args
, *temps
);
702 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
705 elt
= Fcar (varlist
);
706 if (XTYPE (elt
) == Lisp_Symbol
)
707 temps
[argnum
++] = Qnil
;
708 else if (! NILP (Fcdr (Fcdr (elt
))))
710 Fcons (build_string ("`let' bindings can have only one value-form"),
713 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
714 gcpro2
.nvars
= argnum
;
718 varlist
= Fcar (args
);
719 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
721 elt
= Fcar (varlist
);
722 tem
= temps
[argnum
++];
723 if (XTYPE (elt
) == Lisp_Symbol
)
726 specbind (Fcar (elt
), tem
);
729 elt
= Fprogn (Fcdr (args
));
730 return unbind_to (count
, elt
);
733 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
734 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
735 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
736 until TEST returns nil.")
740 Lisp_Object test
, body
, tem
;
741 struct gcpro gcpro1
, gcpro2
;
747 while (tem
= Feval (test
), !NILP (tem
))
757 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
758 "Return result of expanding macros at top level of FORM.\n\
759 If FORM is not a macro call, it is returned unchanged.\n\
760 Otherwise, the macro is expanded and the expansion is considered\n\
761 in place of FORM. When a non-macro-call results, it is returned.\n\n\
762 The second optional arg ENVIRONMENT species an environment of macro\n\
763 definitions to shadow the loaded ones for use in file byte-compilation.")
765 register Lisp_Object form
;
768 /* With cleanups from Hallvard Furuseth. */
769 register Lisp_Object expander
, sym
, def
, tem
;
773 /* Come back here each time we expand a macro call,
774 in case it expands into another macro call. */
775 if (XTYPE (form
) != Lisp_Cons
)
777 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
778 def
= sym
= XCONS (form
)->car
;
780 /* Trace symbols aliases to other symbols
781 until we get a symbol that is not an alias. */
782 while (XTYPE (def
) == Lisp_Symbol
)
786 tem
= Fassq (sym
, env
);
789 def
= XSYMBOL (sym
)->function
;
790 if (!EQ (def
, Qunbound
))
795 /* Right now TEM is the result from SYM in ENV,
796 and if TEM is nil then DEF is SYM's function definition. */
799 /* SYM is not mentioned in ENV.
800 Look at its function definition. */
801 if (EQ (def
, Qunbound
)
802 || XTYPE (def
) != Lisp_Cons
)
803 /* Not defined or definition not suitable */
805 if (EQ (XCONS (def
)->car
, Qautoload
))
807 /* Autoloading function: will it be a macro when loaded? */
808 tem
= Fnth (make_number (4), def
);
809 if (EQ (XCONS (tem
)->car
, Qt
)
810 || EQ (XCONS (tem
)->car
, Qmacro
))
811 /* Yes, load it and try again. */
813 do_autoload (def
, sym
);
819 else if (!EQ (XCONS (def
)->car
, Qmacro
))
821 else expander
= XCONS (def
)->cdr
;
825 expander
= XCONS (tem
)->cdr
;
829 form
= apply1 (expander
, XCONS (form
)->cdr
);
834 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
835 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
836 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
837 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
838 If no throw happens, `catch' returns the value of the last BODY form.\n\
839 If a throw happens, it specifies the value to return from `catch'.")
843 register Lisp_Object tag
;
847 tag
= Feval (Fcar (args
));
849 return internal_catch (tag
, Fprogn
, Fcdr (args
));
852 /* Set up a catch, then call C function FUNC on argument ARG.
853 FUNC should return a Lisp_Object.
854 This is how catches are done from within C code. */
857 internal_catch (tag
, func
, arg
)
859 Lisp_Object (*func
) ();
862 /* This structure is made part of the chain `catchlist'. */
865 /* Fill in the components of c, and put it on the list. */
869 c
.backlist
= backtrace_list
;
870 c
.handlerlist
= handlerlist
;
871 c
.lisp_eval_depth
= lisp_eval_depth
;
872 c
.pdlcount
= specpdl_ptr
- specpdl
;
873 c
.poll_suppress_count
= poll_suppress_count
;
878 if (! _setjmp (c
.jmp
))
879 c
.val
= (*func
) (arg
);
881 /* Throw works by a longjmp that comes right here. */
886 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
887 jump to that CATCH, returning VALUE as the value of that catch.
889 This is the guts Fthrow and Fsignal; they differ only in the way
890 they choose the catch tag to throw to. A catch tag for a
891 condition-case form has a TAG of Qnil.
893 Before each catch is discarded, unbind all special bindings and
894 execute all unwind-protect clauses made above that catch. Unwind
895 the handler stack as we go, so that the proper handlers are in
896 effect for each unwind-protect clause we run. At the end, restore
897 some static info saved in CATCH, and longjmp to the location
900 This is used for correct unwinding in Fthrow and Fsignal. */
903 unwind_to_catch (catch, value
)
904 struct catchtag
*catch;
907 register int last_time
;
909 /* Save the value in the tag. */
912 /* Restore the polling-suppression count. */
913 if (catch->poll_suppress_count
> poll_suppress_count
)
915 while (catch->poll_suppress_count
< poll_suppress_count
)
920 last_time
= catchlist
== catch;
922 /* Unwind the specpdl stack, and then restore the proper set of
924 unbind_to (catchlist
->pdlcount
, Qnil
);
925 handlerlist
= catchlist
->handlerlist
;
926 catchlist
= catchlist
->next
;
930 gcprolist
= catch->gcpro
;
931 backtrace_list
= catch->backlist
;
932 lisp_eval_depth
= catch->lisp_eval_depth
;
934 _longjmp (catch->jmp
, 1);
937 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
938 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
939 Both TAG and VALUE are evalled.")
941 register Lisp_Object tag
, val
;
943 register struct catchtag
*c
;
948 for (c
= catchlist
; c
; c
= c
->next
)
950 if (EQ (c
->tag
, tag
))
951 unwind_to_catch (c
, val
);
953 tag
= Fsignal (Qno_catch
, Fcons (tag
, Fcons (val
, Qnil
)));
958 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
959 "Do BODYFORM, protecting with UNWINDFORMS.\n\
960 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
961 If BODYFORM completes normally, its value is returned\n\
962 after executing the UNWINDFORMS.\n\
963 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
968 int count
= specpdl_ptr
- specpdl
;
970 record_unwind_protect (0, Fcdr (args
));
971 val
= Feval (Fcar (args
));
972 return unbind_to (count
, val
);
975 /* Chain of condition handlers currently in effect.
976 The elements of this chain are contained in the stack frames
977 of Fcondition_case and internal_condition_case.
978 When an error is signaled (by calling Fsignal, below),
979 this chain is searched for an element that applies. */
981 struct handler
*handlerlist
;
983 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
984 "Regain control when an error is signaled.\n\
985 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
986 executes BODYFORM and returns its value if no error happens.\n\
987 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
988 where the BODY is made of Lisp expressions.\n\n\
989 A handler is applicable to an error\n\
990 if CONDITION-NAME is one of the error's condition names.\n\
991 If an error happens, the first applicable handler is run.\n\
993 When a handler handles an error,\n\
994 control returns to the condition-case and the handler BODY... is executed\n\
995 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
996 VAR may be nil; then you do not get access to the signal information.\n\
998 The value of the last BODY form is returned from the condition-case.\n\
999 See also the function `signal' for more info.")
1006 register Lisp_Object var
, bodyform
, handlers
;
1009 bodyform
= Fcar (Fcdr (args
));
1010 handlers
= Fcdr (Fcdr (args
));
1011 CHECK_SYMBOL (var
, 0);
1013 for (val
= handlers
; ! NILP (val
); val
= Fcdr (val
))
1017 if ((!NILP (tem
)) &&
1018 (!CONSP (tem
) || (XTYPE (XCONS (tem
)->car
) != Lisp_Symbol
)))
1019 error ("Invalid condition handler", tem
);
1024 c
.backlist
= backtrace_list
;
1025 c
.handlerlist
= handlerlist
;
1026 c
.lisp_eval_depth
= lisp_eval_depth
;
1027 c
.pdlcount
= specpdl_ptr
- specpdl
;
1028 c
.poll_suppress_count
= poll_suppress_count
;
1029 c
.gcpro
= gcprolist
;
1030 if (_setjmp (c
.jmp
))
1033 specbind (h
.var
, Fcdr (c
.val
));
1034 val
= Fprogn (Fcdr (Fcar (c
.val
)));
1036 /* Note that this just undoes the binding of h.var; whoever
1037 longjumped to us unwound the stack to c.pdlcount before
1039 unbind_to (c
.pdlcount
, Qnil
);
1046 h
.handler
= handlers
;
1047 h
.next
= handlerlist
;
1051 val
= Feval (bodyform
);
1053 handlerlist
= h
.next
;
1058 internal_condition_case (bfun
, handlers
, hfun
)
1059 Lisp_Object (*bfun
) ();
1060 Lisp_Object handlers
;
1061 Lisp_Object (*hfun
) ();
1069 c
.backlist
= backtrace_list
;
1070 c
.handlerlist
= handlerlist
;
1071 c
.lisp_eval_depth
= lisp_eval_depth
;
1072 c
.pdlcount
= specpdl_ptr
- specpdl
;
1073 c
.poll_suppress_count
= poll_suppress_count
;
1074 c
.gcpro
= gcprolist
;
1075 if (_setjmp (c
.jmp
))
1077 return (*hfun
) (Fcdr (c
.val
));
1081 h
.handler
= handlers
;
1083 h
.next
= handlerlist
;
1089 handlerlist
= h
.next
;
1093 static Lisp_Object
find_handler_clause ();
1095 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1096 "Signal an error. Args are SIGNAL-NAME, and associated DATA.\n\
1097 This function does not return.\n\n\
1098 A signal name is a symbol with an `error-conditions' property\n\
1099 that is a list of condition names.\n\
1100 A handler for any of those names will get to handle this signal.\n\
1101 The symbol `error' should normally be one of them.\n\
1103 DATA should be a list. Its elements are printed as part of the error message.\n\
1104 If the signal is handled, DATA is made available to the handler.\n\
1105 See also the function `condition-case'.")
1107 Lisp_Object sig
, data
;
1109 register struct handler
*allhandlers
= handlerlist
;
1110 Lisp_Object conditions
;
1111 extern int gc_in_progress
;
1112 extern int waiting_for_input
;
1113 Lisp_Object debugger_value
;
1115 quit_error_check ();
1117 if (gc_in_progress
|| waiting_for_input
)
1120 #ifdef HAVE_X_WINDOWS
1121 TOTALLY_UNBLOCK_INPUT
;
1124 conditions
= Fget (sig
, Qerror_conditions
);
1126 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1128 register Lisp_Object clause
;
1129 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1130 sig
, data
, &debugger_value
);
1132 #if 0 /* Most callers are not prepared to handle gc if this returns.
1133 So, since this feature is not very useful, take it out. */
1134 /* If have called debugger and user wants to continue,
1136 if (EQ (clause
, Qlambda
))
1137 return debugger_value
;
1139 if (EQ (clause
, Qlambda
))
1141 /* We can't return values to code which signalled an error, but we
1142 can continue code which has signalled a quit. */
1143 if (EQ (sig
, Qquit
))
1146 error ("Returning a value from an error is no longer supported");
1152 struct handler
*h
= handlerlist
;
1153 handlerlist
= allhandlers
;
1154 unwind_to_catch (h
->tag
, Fcons (clause
, Fcons (sig
, data
)));
1158 handlerlist
= allhandlers
;
1159 /* If no handler is present now, try to run the debugger,
1160 and if that fails, throw to top level. */
1161 find_handler_clause (Qerror
, conditions
, sig
, data
, &debugger_value
);
1162 Fthrow (Qtop_level
, Qt
);
1165 /* Return nonzero iff LIST is a non-nil atom or
1166 a list containing one of CONDITIONS. */
1169 wants_debugger (list
, conditions
)
1170 Lisp_Object list
, conditions
;
1177 while (CONSP (conditions
))
1179 Lisp_Object
this, tail
;
1180 this = XCONS (conditions
)->car
;
1181 for (tail
= list
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
1182 if (EQ (XCONS (tail
)->car
, this))
1184 conditions
= XCONS (conditions
)->cdr
;
1189 /* Value of Qlambda means we have called debugger and user has continued.
1190 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1193 find_handler_clause (handlers
, conditions
, sig
, data
, debugger_value_ptr
)
1194 Lisp_Object handlers
, conditions
, sig
, data
;
1195 Lisp_Object
*debugger_value_ptr
;
1197 register Lisp_Object h
;
1198 register Lisp_Object tem
;
1199 register Lisp_Object tem1
;
1201 if (EQ (handlers
, Qt
)) /* t is used by handlers for all conditions, set up by C code. */
1203 if (EQ (handlers
, Qerror
)) /* error is used similarly, but means display a backtrace too */
1205 if (wants_debugger (Vstack_trace_on_error
, conditions
))
1206 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace
, Qnil
);
1207 if ((EQ (sig
, Qquit
)
1209 : wants_debugger (Vdebug_on_error
, conditions
))
1210 && when_entered_debugger
< num_nonmacro_input_chars
)
1212 int count
= specpdl_ptr
- specpdl
;
1213 specbind (Qdebug_on_error
, Qnil
);
1214 *debugger_value_ptr
=
1215 call_debugger (Fcons (Qerror
,
1216 Fcons (Fcons (sig
, data
),
1218 return unbind_to (count
, Qlambda
);
1222 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1227 tem
= Fmemq (Fcar (tem1
), conditions
);
1234 /* dump an error message; called like printf */
1238 error (m
, a1
, a2
, a3
)
1242 sprintf (buf
, m
, a1
, a2
, a3
);
1245 Fsignal (Qerror
, Fcons (build_string (buf
), Qnil
));
1248 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 1, 0,
1249 "T if FUNCTION makes provisions for interactive calling.\n\
1250 This means it contains a description for how to read arguments to give it.\n\
1251 The value is nil for an invalid function or a symbol with no function\n\
1254 Interactively callable functions include strings and vectors (treated\n\
1255 as keyboard macros), lambda-expressions that contain a top-level call\n\
1256 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1257 fourth argument, and some of the built-in functions of Lisp.\n\
1259 Also, a symbol satisfies `commandp' if its function definition does so.")
1261 Lisp_Object function
;
1263 register Lisp_Object fun
;
1264 register Lisp_Object funcar
;
1265 register Lisp_Object tem
;
1270 fun
= indirect_function (fun
);
1271 if (EQ (fun
, Qunbound
))
1274 /* Emacs primitives are interactive if their DEFUN specifies an
1275 interactive spec. */
1276 if (XTYPE (fun
) == Lisp_Subr
)
1278 if (XSUBR (fun
)->prompt
)
1284 /* Bytecode objects are interactive if they are long enough to
1285 have an element whose index is COMPILED_INTERACTIVE, which is
1286 where the interactive spec is stored. */
1287 else if (XTYPE (fun
) == Lisp_Compiled
)
1288 return (XVECTOR (fun
)->size
> COMPILED_INTERACTIVE
1291 /* Strings and vectors are keyboard macros. */
1292 if (XTYPE (fun
) == Lisp_String
1293 || XTYPE (fun
) == Lisp_Vector
)
1296 /* Lists may represent commands. */
1299 funcar
= Fcar (fun
);
1300 if (XTYPE (funcar
) != Lisp_Symbol
)
1301 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1302 if (EQ (funcar
, Qlambda
))
1303 return Fassq (Qinteractive
, Fcdr (Fcdr (fun
)));
1304 if (EQ (funcar
, Qmocklisp
))
1305 return Qt
; /* All mocklisp functions can be called interactively */
1306 if (EQ (funcar
, Qautoload
))
1307 return Fcar (Fcdr (Fcdr (Fcdr (fun
))));
1313 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1314 "Define FUNCTION to autoload from FILE.\n\
1315 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1316 Third arg DOCSTRING is documentation for the function.\n\
1317 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1318 Fifth arg TYPE indicates the type of the object:\n\
1319 nil or omitted says FUNCTION is a function,\n\
1320 `keymap' says FUNCTION is really a keymap, and\n\
1321 `macro' or t says FUNCTION is really a macro.\n\
1322 Third through fifth args give info about the real definition.\n\
1323 They default to nil.\n\
1324 If FUNCTION is already defined other than as an autoload,\n\
1325 this does nothing and returns nil.")
1326 (function
, file
, docstring
, interactive
, type
)
1327 Lisp_Object function
, file
, docstring
, interactive
, type
;
1330 Lisp_Object args
[4];
1333 CHECK_SYMBOL (function
, 0);
1334 CHECK_STRING (file
, 1);
1336 /* If function is defined and not as an autoload, don't override */
1337 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1338 && !(XTYPE (XSYMBOL (function
)->function
) == Lisp_Cons
1339 && EQ (XCONS (XSYMBOL (function
)->function
)->car
, Qautoload
)))
1344 args
[1] = docstring
;
1345 args
[2] = interactive
;
1348 return Ffset (function
, Fcons (Qautoload
, Flist (4, &args
[0])));
1349 #else /* NO_ARG_ARRAY */
1350 return Ffset (function
, Fcons (Qautoload
, Flist (4, &file
)));
1351 #endif /* not NO_ARG_ARRAY */
1355 un_autoload (oldqueue
)
1356 Lisp_Object oldqueue
;
1358 register Lisp_Object queue
, first
, second
;
1360 /* Queue to unwind is current value of Vautoload_queue.
1361 oldqueue is the shadowed value to leave in Vautoload_queue. */
1362 queue
= Vautoload_queue
;
1363 Vautoload_queue
= oldqueue
;
1364 while (CONSP (queue
))
1366 first
= Fcar (queue
);
1367 second
= Fcdr (first
);
1368 first
= Fcar (first
);
1369 if (EQ (second
, Qnil
))
1372 Ffset (first
, second
);
1373 queue
= Fcdr (queue
);
1378 do_autoload (fundef
, funname
)
1379 Lisp_Object fundef
, funname
;
1381 int count
= specpdl_ptr
- specpdl
;
1382 Lisp_Object fun
, val
;
1385 CHECK_SYMBOL (funname
, 0);
1387 /* Value saved here is to be restored into Vautoload_queue */
1388 record_unwind_protect (un_autoload
, Vautoload_queue
);
1389 Vautoload_queue
= Qt
;
1390 Fload (Fcar (Fcdr (fundef
)), Qnil
, noninteractive
? Qt
: Qnil
, Qnil
);
1391 /* Once loading finishes, don't undo it. */
1392 Vautoload_queue
= Qt
;
1393 unbind_to (count
, Qnil
);
1395 fun
= Findirect_function (fun
);
1397 if (XTYPE (fun
) == Lisp_Cons
1398 && EQ (XCONS (fun
)->car
, Qautoload
))
1399 error ("Autoloading failed to define function %s",
1400 XSYMBOL (funname
)->name
->data
);
1403 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
1404 "Evaluate FORM and return its value.")
1408 Lisp_Object fun
, val
, original_fun
, original_args
;
1410 struct backtrace backtrace
;
1411 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1413 if (XTYPE (form
) == Lisp_Symbol
)
1415 if (EQ (Vmocklisp_arguments
, Qt
))
1416 return Fsymbol_value (form
);
1417 val
= Fsymbol_value (form
);
1420 else if (EQ (val
, Qt
))
1428 if (consing_since_gc
> gc_cons_threshold
)
1431 Fgarbage_collect ();
1435 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1437 if (max_lisp_eval_depth
< 100)
1438 max_lisp_eval_depth
= 100;
1439 if (lisp_eval_depth
> max_lisp_eval_depth
)
1440 error ("Lisp nesting exceeds max-lisp-eval-depth");
1443 original_fun
= Fcar (form
);
1444 original_args
= Fcdr (form
);
1446 backtrace
.next
= backtrace_list
;
1447 backtrace_list
= &backtrace
;
1448 backtrace
.function
= &original_fun
; /* This also protects them from gc */
1449 backtrace
.args
= &original_args
;
1450 backtrace
.nargs
= UNEVALLED
;
1451 backtrace
.evalargs
= 1;
1452 backtrace
.debug_on_exit
= 0;
1454 if (debug_on_next_call
)
1455 do_debug_on_call (Qt
);
1457 /* At this point, only original_fun and original_args
1458 have values that will be used below */
1460 fun
= Findirect_function (original_fun
);
1462 if (XTYPE (fun
) == Lisp_Subr
)
1464 Lisp_Object numargs
;
1465 Lisp_Object argvals
[7];
1466 Lisp_Object args_left
;
1467 register int i
, maxargs
;
1469 args_left
= original_args
;
1470 numargs
= Flength (args_left
);
1472 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
1473 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
1474 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1476 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1478 backtrace
.evalargs
= 0;
1479 val
= (*XSUBR (fun
)->function
) (args_left
);
1483 if (XSUBR (fun
)->max_args
== MANY
)
1485 /* Pass a vector of evaluated arguments */
1487 register int argnum
= 0;
1489 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1491 GCPRO3 (args_left
, fun
, fun
);
1495 while (!NILP (args_left
))
1497 vals
[argnum
++] = Feval (Fcar (args_left
));
1498 args_left
= Fcdr (args_left
);
1499 gcpro3
.nvars
= argnum
;
1502 backtrace
.args
= vals
;
1503 backtrace
.nargs
= XINT (numargs
);
1505 val
= (*XSUBR (fun
)->function
) (XINT (numargs
), vals
);
1510 GCPRO3 (args_left
, fun
, fun
);
1511 gcpro3
.var
= argvals
;
1514 maxargs
= XSUBR (fun
)->max_args
;
1515 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
1517 argvals
[i
] = Feval (Fcar (args_left
));
1523 backtrace
.args
= argvals
;
1524 backtrace
.nargs
= XINT (numargs
);
1529 val
= (*XSUBR (fun
)->function
) ();
1532 val
= (*XSUBR (fun
)->function
) (argvals
[0]);
1535 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1]);
1538 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1542 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1543 argvals
[2], argvals
[3]);
1546 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1547 argvals
[3], argvals
[4]);
1550 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1551 argvals
[3], argvals
[4], argvals
[5]);
1554 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1555 argvals
[3], argvals
[4], argvals
[5],
1560 /* Someone has created a subr that takes more arguments than
1561 is supported by this code. We need to either rewrite the
1562 subr to use a different argument protocol, or add more
1563 cases to this switch. */
1567 if (XTYPE (fun
) == Lisp_Compiled
)
1568 val
= apply_lambda (fun
, original_args
, 1);
1572 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1573 funcar
= Fcar (fun
);
1574 if (XTYPE (funcar
) != Lisp_Symbol
)
1575 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1576 if (EQ (funcar
, Qautoload
))
1578 do_autoload (fun
, original_fun
);
1581 if (EQ (funcar
, Qmacro
))
1582 val
= Feval (apply1 (Fcdr (fun
), original_args
));
1583 else if (EQ (funcar
, Qlambda
))
1584 val
= apply_lambda (fun
, original_args
, 1);
1585 else if (EQ (funcar
, Qmocklisp
))
1586 val
= ml_apply (fun
, original_args
);
1588 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1591 if (!EQ (Vmocklisp_arguments
, Qt
))
1595 else if (EQ (val
, Qt
))
1599 if (backtrace
.debug_on_exit
)
1600 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1601 backtrace_list
= backtrace
.next
;
1605 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
1606 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1607 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1612 register int i
, numargs
;
1613 register Lisp_Object spread_arg
;
1614 register Lisp_Object
*funcall_args
;
1616 struct gcpro gcpro1
;
1620 spread_arg
= args
[nargs
- 1];
1621 CHECK_LIST (spread_arg
, nargs
);
1623 numargs
= XINT (Flength (spread_arg
));
1626 return Ffuncall (nargs
- 1, args
);
1627 else if (numargs
== 1)
1629 args
[nargs
- 1] = XCONS (spread_arg
)->car
;
1630 return Ffuncall (nargs
, args
);
1633 numargs
+= nargs
- 2;
1635 fun
= indirect_function (fun
);
1636 if (EQ (fun
, Qunbound
))
1638 /* Let funcall get the error */
1643 if (XTYPE (fun
) == Lisp_Subr
)
1645 if (numargs
< XSUBR (fun
)->min_args
1646 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1647 goto funcall
; /* Let funcall get the error */
1648 else if (XSUBR (fun
)->max_args
> numargs
)
1650 /* Avoid making funcall cons up a yet another new vector of arguments
1651 by explicitly supplying nil's for optional values */
1652 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
1653 * sizeof (Lisp_Object
));
1654 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
1655 funcall_args
[++i
] = Qnil
;
1656 GCPRO1 (*funcall_args
);
1657 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
1661 /* We add 1 to numargs because funcall_args includes the
1662 function itself as well as its arguments. */
1665 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
1666 * sizeof (Lisp_Object
));
1667 GCPRO1 (*funcall_args
);
1668 gcpro1
.nvars
= 1 + numargs
;
1671 bcopy (args
, funcall_args
, nargs
* sizeof (Lisp_Object
));
1672 /* Spread the last arg we got. Its first element goes in
1673 the slot that it used to occupy, hence this value of I. */
1675 while (!NILP (spread_arg
))
1677 funcall_args
[i
++] = XCONS (spread_arg
)->car
;
1678 spread_arg
= XCONS (spread_arg
)->cdr
;
1681 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
1684 /* Apply fn to arg */
1687 Lisp_Object fn
, arg
;
1689 struct gcpro gcpro1
;
1693 RETURN_UNGCPRO (Ffuncall (1, &fn
));
1697 Lisp_Object args
[2];
1701 RETURN_UNGCPRO (Fapply (2, args
));
1703 #else /* not NO_ARG_ARRAY */
1704 RETURN_UNGCPRO (Fapply (2, &fn
));
1705 #endif /* not NO_ARG_ARRAY */
1708 /* Call function fn on no arguments */
1713 struct gcpro gcpro1
;
1716 RETURN_UNGCPRO (Ffuncall (1, &fn
));
1719 /* Call function fn with argument arg */
1723 Lisp_Object fn
, arg
;
1725 struct gcpro gcpro1
;
1727 Lisp_Object args
[2];
1733 RETURN_UNGCPRO (Ffuncall (2, args
));
1734 #else /* not NO_ARG_ARRAY */
1737 RETURN_UNGCPRO (Ffuncall (2, &fn
));
1738 #endif /* not NO_ARG_ARRAY */
1741 /* Call function fn with arguments arg, arg1 */
1744 call2 (fn
, arg
, arg1
)
1745 Lisp_Object fn
, arg
, arg1
;
1747 struct gcpro gcpro1
;
1749 Lisp_Object args
[3];
1755 RETURN_UNGCPRO (Ffuncall (3, args
));
1756 #else /* not NO_ARG_ARRAY */
1759 RETURN_UNGCPRO (Ffuncall (3, &fn
));
1760 #endif /* not NO_ARG_ARRAY */
1763 /* Call function fn with arguments arg, arg1, arg2 */
1766 call3 (fn
, arg
, arg1
, arg2
)
1767 Lisp_Object fn
, arg
, arg1
, arg2
;
1769 struct gcpro gcpro1
;
1771 Lisp_Object args
[4];
1778 RETURN_UNGCPRO (Ffuncall (4, args
));
1779 #else /* not NO_ARG_ARRAY */
1782 RETURN_UNGCPRO (Ffuncall (4, &fn
));
1783 #endif /* not NO_ARG_ARRAY */
1786 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
1787 "Call first argument as a function, passing remaining arguments to it.\n\
1788 Thus, (funcall 'cons 'x 'y) returns (x . y).")
1795 int numargs
= nargs
- 1;
1796 Lisp_Object lisp_numargs
;
1798 struct backtrace backtrace
;
1799 register Lisp_Object
*internal_args
;
1803 if (consing_since_gc
> gc_cons_threshold
)
1804 Fgarbage_collect ();
1806 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1808 if (max_lisp_eval_depth
< 100)
1809 max_lisp_eval_depth
= 100;
1810 if (lisp_eval_depth
> max_lisp_eval_depth
)
1811 error ("Lisp nesting exceeds max-lisp-eval-depth");
1814 backtrace
.next
= backtrace_list
;
1815 backtrace_list
= &backtrace
;
1816 backtrace
.function
= &args
[0];
1817 backtrace
.args
= &args
[1];
1818 backtrace
.nargs
= nargs
- 1;
1819 backtrace
.evalargs
= 0;
1820 backtrace
.debug_on_exit
= 0;
1822 if (debug_on_next_call
)
1823 do_debug_on_call (Qlambda
);
1829 fun
= Findirect_function (fun
);
1831 if (XTYPE (fun
) == Lisp_Subr
)
1833 if (numargs
< XSUBR (fun
)->min_args
1834 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1836 XFASTINT (lisp_numargs
) = numargs
;
1837 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (lisp_numargs
, Qnil
)));
1840 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1841 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1843 if (XSUBR (fun
)->max_args
== MANY
)
1845 val
= (*XSUBR (fun
)->function
) (numargs
, args
+ 1);
1849 if (XSUBR (fun
)->max_args
> numargs
)
1851 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
1852 bcopy (args
+ 1, internal_args
, numargs
* sizeof (Lisp_Object
));
1853 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
1854 internal_args
[i
] = Qnil
;
1857 internal_args
= args
+ 1;
1858 switch (XSUBR (fun
)->max_args
)
1861 val
= (*XSUBR (fun
)->function
) ();
1864 val
= (*XSUBR (fun
)->function
) (internal_args
[0]);
1867 val
= (*XSUBR (fun
)->function
) (internal_args
[0],
1871 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1875 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1880 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1881 internal_args
[2], internal_args
[3],
1885 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1886 internal_args
[2], internal_args
[3],
1887 internal_args
[4], internal_args
[5]);
1890 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1891 internal_args
[2], internal_args
[3],
1892 internal_args
[4], internal_args
[5],
1898 /* If a subr takes more than 6 arguments without using MANY
1899 or UNEVALLED, we need to extend this function to support it.
1900 Until this is done, there is no way to call the function. */
1904 if (XTYPE (fun
) == Lisp_Compiled
)
1905 val
= funcall_lambda (fun
, numargs
, args
+ 1);
1909 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1910 funcar
= Fcar (fun
);
1911 if (XTYPE (funcar
) != Lisp_Symbol
)
1912 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1913 if (EQ (funcar
, Qlambda
))
1914 val
= funcall_lambda (fun
, numargs
, args
+ 1);
1915 else if (EQ (funcar
, Qmocklisp
))
1916 val
= ml_apply (fun
, Flist (numargs
, args
+ 1));
1917 else if (EQ (funcar
, Qautoload
))
1919 do_autoload (fun
, args
[0]);
1923 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1927 if (backtrace
.debug_on_exit
)
1928 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1929 backtrace_list
= backtrace
.next
;
1934 apply_lambda (fun
, args
, eval_flag
)
1935 Lisp_Object fun
, args
;
1938 Lisp_Object args_left
;
1939 Lisp_Object numargs
;
1940 register Lisp_Object
*arg_vector
;
1941 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1943 register Lisp_Object tem
;
1945 numargs
= Flength (args
);
1946 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1949 GCPRO3 (*arg_vector
, args_left
, fun
);
1952 for (i
= 0; i
< XINT (numargs
);)
1954 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
1955 if (eval_flag
) tem
= Feval (tem
);
1956 arg_vector
[i
++] = tem
;
1964 backtrace_list
->args
= arg_vector
;
1965 backtrace_list
->nargs
= i
;
1967 backtrace_list
->evalargs
= 0;
1968 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
1970 /* Do the debug-on-exit now, while arg_vector still exists. */
1971 if (backtrace_list
->debug_on_exit
)
1972 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
1973 /* Don't do it again when we return to eval. */
1974 backtrace_list
->debug_on_exit
= 0;
1978 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
1979 and return the result of evaluation.
1980 FUN must be either a lambda-expression or a compiled-code object. */
1983 funcall_lambda (fun
, nargs
, arg_vector
)
1986 register Lisp_Object
*arg_vector
;
1988 Lisp_Object val
, tem
;
1989 register Lisp_Object syms_left
;
1990 Lisp_Object numargs
;
1991 register Lisp_Object next
;
1992 int count
= specpdl_ptr
- specpdl
;
1994 int optional
= 0, rest
= 0;
1996 specbind (Qmocklisp_arguments
, Qt
); /* t means NOT mocklisp! */
1998 XFASTINT (numargs
) = nargs
;
2000 if (XTYPE (fun
) == Lisp_Cons
)
2001 syms_left
= Fcar (Fcdr (fun
));
2002 else if (XTYPE (fun
) == Lisp_Compiled
)
2003 syms_left
= XVECTOR (fun
)->contents
[COMPILED_ARGLIST
];
2007 for (; !NILP (syms_left
); syms_left
= Fcdr (syms_left
))
2010 next
= Fcar (syms_left
);
2011 while (XTYPE (next
) != Lisp_Symbol
)
2012 next
= Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2013 if (EQ (next
, Qand_rest
))
2015 else if (EQ (next
, Qand_optional
))
2019 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
2024 tem
= arg_vector
[i
++];
2025 specbind (next
, tem
);
2028 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2030 specbind (next
, Qnil
);
2034 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2036 if (XTYPE (fun
) == Lisp_Cons
)
2037 val
= Fprogn (Fcdr (Fcdr (fun
)));
2039 val
= Fbyte_code (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
],
2040 XVECTOR (fun
)->contents
[COMPILED_CONSTANTS
],
2041 XVECTOR (fun
)->contents
[COMPILED_STACK_DEPTH
]);
2042 return unbind_to (count
, val
);
2048 register int count
= specpdl_ptr
- specpdl
;
2049 if (specpdl_size
>= max_specpdl_size
)
2051 if (max_specpdl_size
< 400)
2052 max_specpdl_size
= 400;
2053 if (specpdl_size
>= max_specpdl_size
)
2055 if (!NILP (Vdebug_on_error
))
2056 /* Leave room for some specpdl in the debugger. */
2057 max_specpdl_size
= specpdl_size
+ 100;
2059 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil
));
2063 if (specpdl_size
> max_specpdl_size
)
2064 specpdl_size
= max_specpdl_size
;
2065 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
2066 specpdl_ptr
= specpdl
+ count
;
2070 specbind (symbol
, value
)
2071 Lisp_Object symbol
, value
;
2073 extern void store_symval_forwarding (); /* in eval.c */
2076 CHECK_SYMBOL (symbol
, 0);
2078 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2080 specpdl_ptr
->symbol
= symbol
;
2081 specpdl_ptr
->func
= 0;
2082 ovalue
= XSYMBOL (symbol
)->value
;
2083 specpdl_ptr
->old_value
= EQ (ovalue
, Qunbound
) ? Qunbound
: Fsymbol_value (symbol
);
2085 if (XTYPE (ovalue
) == Lisp_Buffer_Objfwd
)
2086 store_symval_forwarding (symbol
, ovalue
, value
);
2088 Fset (symbol
, value
);
2092 record_unwind_protect (function
, arg
)
2093 Lisp_Object (*function
)();
2096 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2098 specpdl_ptr
->func
= function
;
2099 specpdl_ptr
->symbol
= Qnil
;
2100 specpdl_ptr
->old_value
= arg
;
2105 unbind_to (count
, value
)
2109 int quitf
= !NILP (Vquit_flag
);
2110 struct gcpro gcpro1
;
2116 while (specpdl_ptr
!= specpdl
+ count
)
2119 if (specpdl_ptr
->func
!= 0)
2120 (*specpdl_ptr
->func
) (specpdl_ptr
->old_value
);
2121 /* Note that a "binding" of nil is really an unwind protect,
2122 so in that case the "old value" is a list of forms to evaluate. */
2123 else if (NILP (specpdl_ptr
->symbol
))
2124 Fprogn (specpdl_ptr
->old_value
);
2126 Fset (specpdl_ptr
->symbol
, specpdl_ptr
->old_value
);
2128 if (NILP (Vquit_flag
) && quitf
) Vquit_flag
= Qt
;
2137 /* Get the value of symbol's global binding, even if that binding
2138 is not now dynamically visible. */
2141 top_level_value (symbol
)
2144 register struct specbinding
*ptr
= specpdl
;
2146 CHECK_SYMBOL (symbol
, 0);
2147 for (; ptr
!= specpdl_ptr
; ptr
++)
2149 if (EQ (ptr
->symbol
, symbol
))
2150 return ptr
->old_value
;
2152 return Fsymbol_value (symbol
);
2156 top_level_set (symbol
, newval
)
2157 Lisp_Object symbol
, newval
;
2159 register struct specbinding
*ptr
= specpdl
;
2161 CHECK_SYMBOL (symbol
, 0);
2162 for (; ptr
!= specpdl_ptr
; ptr
++)
2164 if (EQ (ptr
->symbol
, symbol
))
2166 ptr
->old_value
= newval
;
2170 return Fset (symbol
, newval
);
2175 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
2176 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2177 The debugger is entered when that frame exits, if the flag is non-nil.")
2179 Lisp_Object level
, flag
;
2181 register struct backtrace
*backlist
= backtrace_list
;
2184 CHECK_NUMBER (level
, 0);
2186 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
2188 backlist
= backlist
->next
;
2192 backlist
->debug_on_exit
= !NILP (flag
);
2197 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
2198 "Print a trace of Lisp function calls currently active.\n\
2199 Output stream used is value of `standard-output'.")
2202 register struct backtrace
*backlist
= backtrace_list
;
2206 extern Lisp_Object Vprint_level
;
2207 struct gcpro gcpro1
;
2209 XFASTINT (Vprint_level
) = 3;
2216 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
2217 if (backlist
->nargs
== UNEVALLED
)
2219 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
2223 tem
= *backlist
->function
;
2224 Fprin1 (tem
, Qnil
); /* This can QUIT */
2225 write_string ("(", -1);
2226 if (backlist
->nargs
== MANY
)
2228 for (tail
= *backlist
->args
, i
= 0;
2230 tail
= Fcdr (tail
), i
++)
2232 if (i
) write_string (" ", -1);
2233 Fprin1 (Fcar (tail
), Qnil
);
2238 for (i
= 0; i
< backlist
->nargs
; i
++)
2240 if (i
) write_string (" ", -1);
2241 Fprin1 (backlist
->args
[i
], Qnil
);
2245 write_string (")\n", -1);
2246 backlist
= backlist
->next
;
2249 Vprint_level
= Qnil
;
2254 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, "",
2255 "Return the function and arguments N frames up from current execution point.\n\
2256 If that frame has not evaluated the arguments yet (or is a special form),\n\
2257 the value is (nil FUNCTION ARG-FORMS...).\n\
2258 If that frame has evaluated its arguments and called its function already,\n\
2259 the value is (t FUNCTION ARG-VALUES...).\n\
2260 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2261 FUNCTION is whatever was supplied as car of evaluated list,\n\
2262 or a lambda expression for macro calls.\n\
2263 If N is more than the number of frames, the value is nil.")
2265 Lisp_Object nframes
;
2267 register struct backtrace
*backlist
= backtrace_list
;
2271 CHECK_NATNUM (nframes
, 0);
2273 /* Find the frame requested. */
2274 for (i
= 0; i
< XFASTINT (nframes
); i
++)
2275 backlist
= backlist
->next
;
2279 if (backlist
->nargs
== UNEVALLED
)
2280 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
2283 if (backlist
->nargs
== MANY
)
2284 tem
= *backlist
->args
;
2286 tem
= Flist (backlist
->nargs
, backlist
->args
);
2288 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
2294 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
2295 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2297 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
2298 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2299 This limit is to catch infinite recursions for you before they cause\n\
2300 actual stack overflow in C, which would be fatal for Emacs.\n\
2301 You can safely make it considerably larger than its default value,\n\
2302 if that proves inconveniently small.");
2304 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
2305 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2306 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2309 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
2310 "Non-nil inhibits C-g quitting from happening immediately.\n\
2311 Note that `quit-flag' will still be set by typing C-g,\n\
2312 so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2313 To prevent this happening, set `quit-flag' to nil\n\
2314 before making `inhibit-quit' nil.");
2315 Vinhibit_quit
= Qnil
;
2317 Qinhibit_quit
= intern ("inhibit-quit");
2318 staticpro (&Qinhibit_quit
);
2320 Qautoload
= intern ("autoload");
2321 staticpro (&Qautoload
);
2323 Qdebug_on_error
= intern ("debug-on-error");
2324 staticpro (&Qdebug_on_error
);
2326 Qmacro
= intern ("macro");
2327 staticpro (&Qmacro
);
2329 /* Note that the process handling also uses Qexit, but we don't want
2330 to staticpro it twice, so we just do it here. */
2331 Qexit
= intern ("exit");
2334 Qinteractive
= intern ("interactive");
2335 staticpro (&Qinteractive
);
2337 Qcommandp
= intern ("commandp");
2338 staticpro (&Qcommandp
);
2340 Qdefun
= intern ("defun");
2341 staticpro (&Qdefun
);
2343 Qand_rest
= intern ("&rest");
2344 staticpro (&Qand_rest
);
2346 Qand_optional
= intern ("&optional");
2347 staticpro (&Qand_optional
);
2349 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error
,
2350 "*Non-nil means automatically display a backtrace buffer\n\
2351 after any error that is handled by the editor command loop.\n\
2352 If the value is a list, an error only means to display a backtrace\n\
2353 if one of its condition symbols appears in the list.");
2354 Vstack_trace_on_error
= Qnil
;
2356 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error
,
2357 "*Non-nil means enter debugger if an error is signaled.\n\
2358 Does not apply to errors handled by `condition-case'.\n\
2359 If the value is a list, an error only means to enter the debugger\n\
2360 if one of its condition symbols appears in the list.\n\
2361 See also variable `debug-on-quit'.");
2362 Vdebug_on_error
= Qnil
;
2364 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
2365 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
2366 Does not apply if quit is handled by a `condition-case'.");
2369 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
2370 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2372 DEFVAR_LISP ("debugger", &Vdebugger
,
2373 "Function to call to invoke debugger.\n\
2374 If due to frame exit, args are `exit' and the value being returned;\n\
2375 this function's value will be returned instead of that.\n\
2376 If due to error, args are `error' and a list of the args to `signal'.\n\
2377 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2378 If due to `eval' entry, one arg, t.");
2381 Qmocklisp_arguments
= intern ("mocklisp-arguments");
2382 staticpro (&Qmocklisp_arguments
);
2383 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments
,
2384 "While in a mocklisp function, the list of its unevaluated args.");
2385 Vmocklisp_arguments
= Qt
;
2387 DEFVAR_LISP ("run-hooks", &Vrun_hooks
,
2388 "Set to the function `run-hooks', if that function has been defined.\n\
2389 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2392 staticpro (&Vautoload_queue
);
2393 Vautoload_queue
= Qnil
;
2404 defsubr (&Sfunction
);
2406 defsubr (&Sdefmacro
);
2408 defsubr (&Sdefconst
);
2409 defsubr (&Suser_variable_p
);
2413 defsubr (&Smacroexpand
);
2416 defsubr (&Sunwind_protect
);
2417 defsubr (&Scondition_case
);
2419 defsubr (&Sinteractive_p
);
2420 defsubr (&Scommandp
);
2421 defsubr (&Sautoload
);
2424 defsubr (&Sfuncall
);
2425 defsubr (&Sbacktrace_debug
);
2426 defsubr (&Sbacktrace
);
2427 defsubr (&Sbacktrace_frame
);