1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987 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 list of unevalled args */
47 /* Nonzero means call value of debugger when done with this operation. */
51 struct backtrace
*backtrace_list
;
57 struct catchtag
*next
;
60 struct backtrace
*backlist
;
61 struct handler
*handlerlist
;
64 int poll_suppress_count
;
67 struct catchtag
*catchlist
;
69 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
70 Lisp_Object Qinhibit_quit
, Vinhibit_quit
, Vquit_flag
;
71 Lisp_Object Qmocklisp_arguments
, Vmocklisp_arguments
, Qmocklisp
;
72 Lisp_Object Qand_rest
, Qand_optional
;
73 Lisp_Object Qdebug_on_error
;
75 Lisp_Object Vrun_hooks
;
77 /* Non-nil means record all fset's and provide's, to be undone
78 if the file being autoloaded is not fully loaded.
79 They are recorded by being consed onto the front of Vautoload_queue:
80 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
82 Lisp_Object Vautoload_queue
;
84 /* Current number of specbindings allocated in specpdl. */
87 /* Pointer to beginning of specpdl. */
88 struct specbinding
*specpdl
;
90 /* Pointer to first unused element in specpdl. */
91 struct specbinding
*specpdl_ptr
;
93 /* Maximum size allowed for specpdl allocation */
96 /* Depth in Lisp evaluations and function calls. */
99 /* Maximum allowed depth in Lisp evaluations and function calls. */
100 int max_lisp_eval_depth
;
102 /* Nonzero means enter debugger before next function call */
103 int debug_on_next_call
;
105 /* Nonzero means display a backtrace if an error
106 is handled by the command loop's error handler. */
107 int stack_trace_on_error
;
109 /* Nonzero means enter debugger if an error
110 is handled by the command loop's error handler. */
113 /* Nonzero means enter debugger if a quit signal
114 is handled by the command loop's error handler. */
117 /* Nonzero means we are trying to enter the debugger.
118 This is to prevent recursive attempts. */
119 int entering_debugger
;
121 Lisp_Object Vdebugger
;
123 void specbind (), record_unwind_protect ();
125 Lisp_Object
funcall_lambda ();
126 extern Lisp_Object
ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
131 specpdl
= (struct specbinding
*) malloc (specpdl_size
* sizeof (struct specbinding
));
132 max_specpdl_size
= 600;
133 max_lisp_eval_depth
= 200;
138 specpdl_ptr
= specpdl
;
143 debug_on_next_call
= 0;
145 entering_debugger
= 0;
152 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
153 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
154 if (specpdl_size
+ 40 > max_specpdl_size
)
155 max_specpdl_size
= specpdl_size
+ 40;
156 debug_on_next_call
= 0;
157 entering_debugger
= 1;
158 return apply1 (Vdebugger
, arg
);
161 do_debug_on_call (code
)
164 debug_on_next_call
= 0;
165 backtrace_list
->debug_on_exit
= 1;
166 call_debugger (Fcons (code
, Qnil
));
169 /* NOTE!!! Every function that can call EVAL must protect its args
170 and temporaries from garbage collection while it needs them.
171 The definition of `For' shows what you have to do. */
173 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
174 "Eval args until one of them yields non-nil, then return that value.\n\
175 The remaining args are not evalled at all.\n\
176 If all args return nil, return nil.")
180 register Lisp_Object val
;
181 Lisp_Object args_left
;
192 val
= Feval (Fcar (args_left
));
195 args_left
= Fcdr (args_left
);
197 while (!NILP(args_left
));
203 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
204 "Eval args until one of them yields nil, then return nil.\n\
205 The remaining args are not evalled at all.\n\
206 If no arg yields nil, return the last arg's value.")
210 register Lisp_Object val
;
211 Lisp_Object args_left
;
222 val
= Feval (Fcar (args_left
));
225 args_left
= Fcdr (args_left
);
227 while (!NILP(args_left
));
233 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
234 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
235 Returns the value of THEN or the value of the last of the ELSE's.\n\
236 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
237 If COND yields nil, and there are no ELSE's, the value is nil.")
241 register Lisp_Object cond
;
245 cond
= Feval (Fcar (args
));
249 return Feval (Fcar (Fcdr (args
)));
250 return Fprogn (Fcdr (Fcdr (args
)));
253 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
254 "(cond CLAUSES...): try each clause until one succeeds.\n\
255 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
256 and, if the value is non-nil, this clause succeeds:\n\
257 then the expressions in BODY are evaluated and the last one's\n\
258 value is the value of the cond-form.\n\
259 If no clause succeeds, cond returns nil.\n\
260 If a clause has one element, as in (CONDITION),\n\
261 CONDITION's value if non-nil is returned from the cond-form.")
265 register Lisp_Object clause
, val
;
272 clause
= Fcar (args
);
273 val
= Feval (Fcar (clause
));
276 if (!EQ (XCONS (clause
)->cdr
, Qnil
))
277 val
= Fprogn (XCONS (clause
)->cdr
);
280 args
= XCONS (args
)->cdr
;
287 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
288 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
292 register Lisp_Object val
, tem
;
293 Lisp_Object args_left
;
296 /* In Mocklisp code, symbols at the front of the progn arglist
297 are to be bound to zero. */
298 if (!EQ (Vmocklisp_arguments
, Qt
))
300 val
= make_number (0);
301 while (!NILP (args
) && (tem
= Fcar (args
), XTYPE (tem
) == Lisp_Symbol
))
304 specbind (tem
, val
), args
= Fcdr (args
);
316 val
= Feval (Fcar (args_left
));
317 args_left
= Fcdr (args_left
);
319 while (!NILP(args_left
));
325 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
326 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
327 The value of FIRST is saved during the evaluation of the remaining args,\n\
328 whose values are discarded.")
333 register Lisp_Object args_left
;
334 struct gcpro gcpro1
, gcpro2
;
335 register int argnum
= 0;
347 val
= Feval (Fcar (args_left
));
349 Feval (Fcar (args_left
));
350 args_left
= Fcdr (args_left
);
352 while (!NILP(args_left
));
358 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
359 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
360 The value of Y is saved during the evaluation of the remaining args,\n\
361 whose values are discarded.")
366 register Lisp_Object args_left
;
367 struct gcpro gcpro1
, gcpro2
;
368 register int argnum
= -1;
382 val
= Feval (Fcar (args_left
));
384 Feval (Fcar (args_left
));
385 args_left
= Fcdr (args_left
);
387 while (!NILP(args_left
));
393 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
394 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
395 The SYMs are not evaluated. Thus (setq x y) sets x to the value of y.\n\
396 Each SYM is set before the next VAL is computed.")
400 register Lisp_Object args_left
;
401 register Lisp_Object val
, sym
;
412 val
= Feval (Fcar (Fcdr (args_left
)));
413 sym
= Fcar (args_left
);
415 args_left
= Fcdr (Fcdr (args_left
));
417 while (!NILP(args_left
));
423 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
424 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
431 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
432 "Like `quote', but preferred for objects which are functions.\n\
433 In byte compilation, `function' causes its argument to be compiled.\n\
434 `quote' cannot do that.")
441 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
442 "Return t if function in which this appears was called interactively.\n\
443 This means that the function was called with call-interactively (which\n\
444 includes being called as the binding of a key)\n\
445 and input is currently coming from the keyboard (not in keyboard macro).")
448 register struct backtrace
*btp
;
449 register Lisp_Object fun
;
454 /* Unless the object was compiled, skip the frame of interactive-p itself
455 (if interpreted) or the frame of byte-code (if called from
456 compiled function). */
457 btp
= backtrace_list
;
458 if (XTYPE (*btp
->function
) != Lisp_Compiled
)
461 && (btp
->nargs
== UNEVALLED
|| EQ (*btp
->function
, Qbytecode
)))
464 /* btp now points at the frame of the innermost function
465 that DOES eval its args.
466 If it is a built-in function (such as load or eval-region)
468 fun
= *btp
->function
;
469 while (XTYPE (fun
) == Lisp_Symbol
)
472 fun
= Fsymbol_function (fun
);
474 if (XTYPE (fun
) == Lisp_Subr
)
476 /* btp points to the frame of a Lisp function that called interactive-p.
477 Return t if that function was called interactively. */
478 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
483 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
484 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
485 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
486 See also the function `interactive'.")
490 register Lisp_Object fn_name
;
491 register Lisp_Object defn
;
493 fn_name
= Fcar (args
);
494 defn
= Fcons (Qlambda
, Fcdr (args
));
495 if (!NILP (Vpurify_flag
))
496 defn
= Fpurecopy (defn
);
497 Ffset (fn_name
, defn
);
501 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
502 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
503 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
504 When the macro is called, as in (NAME ARGS...),\n\
505 the function (lambda ARGLIST BODY...) is applied to\n\
506 the list ARGS... as it appears in the expression,\n\
507 and the result should be a form to be evaluated instead of the original.")
511 register Lisp_Object fn_name
;
512 register Lisp_Object defn
;
514 fn_name
= Fcar (args
);
515 defn
= Fcons (Qmacro
, Fcons (Qlambda
, Fcdr (args
)));
516 if (!NILP (Vpurify_flag
))
517 defn
= Fpurecopy (defn
);
518 Ffset (fn_name
, defn
);
522 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
523 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
524 You are not required to define a variable in order to use it,\n\
525 but the definition can supply documentation and an initial value\n\
526 in a way that tags can recognize.\n\n\
527 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
528 If SYMBOL is buffer-local, its default value is initialized in this way.\n\
529 INITVALUE and DOCSTRING are optional.\n\
530 If DOCSTRING starts with *, this variable is identified as a user option.\n\
531 This means that M-x set-variable and M-x edit-options recognize it.\n\
532 If INITVALUE is missing, SYMBOL's value is not set.")
536 register Lisp_Object sym
, tem
;
542 tem
= Fdefault_boundp (sym
);
544 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
546 tem
= Fcar (Fcdr (Fcdr (args
)));
549 if (!NILP (Vpurify_flag
))
550 tem
= Fpurecopy (tem
);
551 Fput (sym
, Qvariable_documentation
, tem
);
556 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
557 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
558 The intent is that programs do not change this value, but users may.\n\
559 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
560 If SYMBOL is buffer-local, its default value is initialized in this way.\n\
561 DOCSTRING is 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\n\
564 Note: do not use `defconst' for user options in libraries that are not\n\
565 normally loaded, since it is useful for users to be able to specify\n\
566 their own values for such variables before loading the library.\n\
567 Since `defconst' unconditionally assigns the variable,\n\
568 it would override the user's choice.")
572 register Lisp_Object sym
, tem
;
575 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
576 tem
= Fcar (Fcdr (Fcdr (args
)));
579 if (!NILP (Vpurify_flag
))
580 tem
= Fpurecopy (tem
);
581 Fput (sym
, Qvariable_documentation
, tem
);
586 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
587 "Returns t if VARIABLE is intended to be set and modified by users.\n\
588 \(The alternative is a variable used internally in a Lisp program.)\n\
589 Determined by whether the first character of the documentation\n\
590 for the variable is \"*\"")
592 Lisp_Object variable
;
594 Lisp_Object documentation
;
596 documentation
= Fget (variable
, Qvariable_documentation
);
597 if (XTYPE (documentation
) == Lisp_Int
&& XINT (documentation
) < 0)
599 if ((XTYPE (documentation
) == Lisp_String
) &&
600 ((unsigned char) XSTRING (documentation
)->data
[0] == '*'))
605 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
606 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
607 The value of the last form in BODY is returned.\n\
608 Each element of VARLIST is a symbol (which is bound to nil)\n\
609 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
610 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
614 Lisp_Object varlist
, val
, elt
;
615 int count
= specpdl_ptr
- specpdl
;
616 struct gcpro gcpro1
, gcpro2
, gcpro3
;
618 GCPRO3 (args
, elt
, varlist
);
620 varlist
= Fcar (args
);
621 while (!NILP (varlist
))
624 elt
= Fcar (varlist
);
625 if (XTYPE (elt
) == Lisp_Symbol
)
626 specbind (elt
, Qnil
);
629 val
= Feval (Fcar (Fcdr (elt
)));
630 specbind (Fcar (elt
), val
);
632 varlist
= Fcdr (varlist
);
635 val
= Fprogn (Fcdr (args
));
636 return unbind_to (count
, val
);
639 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
640 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
641 The value of the last form in BODY is returned.\n\
642 Each element of VARLIST is a symbol (which is bound to nil)\n\
643 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
644 All the VALUEFORMs are evalled before any symbols are bound.")
648 Lisp_Object
*temps
, tem
;
649 register Lisp_Object elt
, varlist
;
650 int count
= specpdl_ptr
- specpdl
;
652 struct gcpro gcpro1
, gcpro2
;
654 varlist
= Fcar (args
);
656 /* Make space to hold the values to give the bound variables */
657 elt
= Flength (varlist
);
658 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
660 /* Compute the values and store them in `temps' */
662 GCPRO2 (args
, *temps
);
665 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
668 elt
= Fcar (varlist
);
669 if (XTYPE (elt
) == Lisp_Symbol
)
670 temps
[argnum
++] = Qnil
;
672 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
673 gcpro2
.nvars
= argnum
;
677 varlist
= Fcar (args
);
678 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
680 elt
= Fcar (varlist
);
681 tem
= temps
[argnum
++];
682 if (XTYPE (elt
) == Lisp_Symbol
)
685 specbind (Fcar (elt
), tem
);
688 elt
= Fprogn (Fcdr (args
));
689 return unbind_to (count
, elt
);
692 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
693 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
694 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
695 until TEST returns nil.")
699 Lisp_Object test
, body
, tem
;
700 struct gcpro gcpro1
, gcpro2
;
706 while (tem
= Feval (test
), !NILP (tem
))
716 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
717 "Return result of expanding macros at top level of FORM.\n\
718 If FORM is not a macro call, it is returned unchanged.\n\
719 Otherwise, the macro is expanded and the expansion is considered\n\
720 in place of FORM. When a non-macro-call results, it is returned.\n\n\
721 The second optional arg ENVIRONMENT species an environment of macro\n\
722 definitions to shadow the loaded ones for use in file byte-compilation.")
724 register Lisp_Object form
;
727 register Lisp_Object expander
, sym
, def
, tem
;
731 /* Come back here each time we expand a macro call,
732 in case it expands into another macro call. */
733 if (XTYPE (form
) != Lisp_Cons
)
735 sym
= XCONS (form
)->car
;
736 /* Detect ((macro lambda ...) ...) */
737 if (XTYPE (sym
) == Lisp_Cons
738 && EQ (XCONS (sym
)->car
, Qmacro
))
740 expander
= XCONS (sym
)->cdr
;
743 if (XTYPE (sym
) != Lisp_Symbol
)
745 /* Trace symbols aliases to other symbols
746 until we get a symbol that is not an alias. */
750 tem
= Fassq (sym
, env
);
753 def
= XSYMBOL (sym
)->function
;
754 if (XTYPE (def
) == Lisp_Symbol
&& !EQ (def
, Qunbound
))
761 #if 0 /* This is turned off because it caused an element (foo . bar)
762 to have the effect of defining foo as an alias for the macro bar.
763 That is inconsistent; bar should be a function to expand foo. */
764 if (XTYPE (tem
) == Lisp_Cons
765 && XTYPE (XCONS (tem
)->cdr
) == Lisp_Symbol
)
766 sym
= XCONS (tem
)->cdr
;
772 /* Right now TEM is the result from SYM in ENV,
773 and if TEM is nil then DEF is SYM's function definition. */
776 /* SYM is not mentioned in ENV.
777 Look at its function definition. */
778 if (EQ (def
, Qunbound
)
779 || XTYPE (def
) != Lisp_Cons
)
780 /* Not defined or definition not suitable */
782 if (EQ (XCONS (def
)->car
, Qautoload
))
784 /* Autoloading function: will it be a macro when loaded? */
785 tem
= Fcar (Fnthcdr (make_number (4), def
));
788 /* Yes, load it and try again. */
789 do_autoload (def
, sym
);
792 else if (!EQ (XCONS (def
)->car
, Qmacro
))
794 else expander
= XCONS (def
)->cdr
;
798 expander
= XCONS (tem
)->cdr
;
803 form
= apply1 (expander
, XCONS (form
)->cdr
);
808 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
809 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
810 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
811 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
812 If no throw happens, `catch' returns the value of the last BODY form.\n\
813 If a throw happens, it specifies the value to return from `catch'.")
817 register Lisp_Object tag
;
821 tag
= Feval (Fcar (args
));
823 return internal_catch (tag
, Fprogn
, Fcdr (args
));
826 /* Set up a catch, then call C function FUNC on argument ARG.
827 FUNC should return a Lisp_Object.
828 This is how catches are done from within C code. */
831 internal_catch (tag
, func
, arg
)
833 Lisp_Object (*func
) ();
836 /* This structure is made part of the chain `catchlist'. */
839 /* Fill in the components of c, and put it on the list. */
843 c
.backlist
= backtrace_list
;
844 c
.handlerlist
= handlerlist
;
845 c
.lisp_eval_depth
= lisp_eval_depth
;
846 c
.pdlcount
= specpdl_ptr
- specpdl
;
847 c
.poll_suppress_count
= poll_suppress_count
;
852 if (! _setjmp (c
.jmp
))
853 c
.val
= (*func
) (arg
);
855 /* Throw works by a longjmp that comes right here. */
860 /* Discard from the catchlist all catch tags back through CATCH.
861 Before each catch is discarded, unbind all special bindings
862 made within that catch. Also, when discarding a catch that
863 corresponds to a condition handler, discard that handler.
865 At the end, restore some static info saved in CATCH.
867 This is used for correct unwinding in Fthrow and Fsignal,
868 before doing the longjmp that actually destroys the stack frames
869 in which these handlers and catches reside. */
873 struct catchtag
*catch;
875 register int last_time
;
879 last_time
= catchlist
== catch;
880 unbind_to (catchlist
->pdlcount
, Qnil
);
881 handlerlist
= catchlist
->handlerlist
;
882 catchlist
= catchlist
->next
;
886 gcprolist
= catch->gcpro
;
887 backtrace_list
= catch->backlist
;
888 lisp_eval_depth
= catch->lisp_eval_depth
;
891 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
892 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
893 Both TAG and VALUE are evalled.")
895 register Lisp_Object tag
, val
;
897 register struct catchtag
*c
;
902 for (c
= catchlist
; c
; c
= c
->next
)
904 if (EQ (c
->tag
, tag
))
906 /* Restore the polling-suppression count. */
907 if (c
->poll_suppress_count
> poll_suppress_count
)
909 while (c
->poll_suppress_count
< poll_suppress_count
)
913 _longjmp (c
->jmp
, 1);
916 tag
= Fsignal (Qno_catch
, Fcons (tag
, Fcons (val
, Qnil
)));
921 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
922 "Do BODYFORM, protecting with UNWINDFORMS.\n\
923 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
924 If BODYFORM completes normally, its value is returned\n\
925 after executing the UNWINDFORMS.\n\
926 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
931 int count
= specpdl_ptr
- specpdl
;
933 record_unwind_protect (0, Fcdr (args
));
934 val
= Feval (Fcar (args
));
935 return unbind_to (count
, val
);
938 /* Chain of condition handlers currently in effect.
939 The elements of this chain are contained in the stack frames
940 of Fcondition_case and internal_condition_case.
941 When an error is signaled (by calling Fsignal, below),
942 this chain is searched for an element that applies. */
944 struct handler
*handlerlist
;
946 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
947 "Regain control when an error is signaled.\n\
948 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
949 executes BODYFORM and returns its value if no error happens.\n\
950 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
951 where the BODY is made of Lisp expressions.\n\n\
952 A handler is applicable to an error\n\
953 if CONDITION-NAME is one of the error's condition names.\n\
954 If an error happens, the first applicable handler is run.\n\
956 When a handler handles an error,\n\
957 control returns to the condition-case and the handler BODY... is executed\n\
958 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
959 VAR may be nil; then you do not get access to the signal information.\n\
961 The value of the last BODY form is returned from the condition-case.\n\
962 See also the function `signal' for more info.")
969 register Lisp_Object tem
;
972 CHECK_SYMBOL (tem
, 0);
976 c
.backlist
= backtrace_list
;
977 c
.handlerlist
= handlerlist
;
978 c
.lisp_eval_depth
= lisp_eval_depth
;
979 c
.pdlcount
= specpdl_ptr
- specpdl
;
980 c
.poll_suppress_count
= poll_suppress_count
;
985 specbind (h
.var
, Fcdr (c
.val
));
986 val
= Fprogn (Fcdr (Fcar (c
.val
)));
987 unbind_to (c
.pdlcount
, Qnil
);
993 h
.handler
= Fcdr (Fcdr (args
));
995 for (val
= h
.handler
; ! NILP (val
); val
= Fcdr (val
))
999 (!CONSP (tem
) || (XTYPE (XCONS (tem
)->car
) != Lisp_Symbol
)))
1000 error ("Invalid condition handler", tem
);
1003 h
.next
= handlerlist
;
1004 h
.poll_suppress_count
= poll_suppress_count
;
1008 val
= Feval (Fcar (Fcdr (args
)));
1010 handlerlist
= h
.next
;
1015 internal_condition_case (bfun
, handlers
, hfun
)
1016 Lisp_Object (*bfun
) ();
1017 Lisp_Object handlers
;
1018 Lisp_Object (*hfun
) ();
1026 c
.backlist
= backtrace_list
;
1027 c
.handlerlist
= handlerlist
;
1028 c
.lisp_eval_depth
= lisp_eval_depth
;
1029 c
.pdlcount
= specpdl_ptr
- specpdl
;
1030 c
.poll_suppress_count
= poll_suppress_count
;
1031 c
.gcpro
= gcprolist
;
1032 if (_setjmp (c
.jmp
))
1034 return (*hfun
) (Fcdr (c
.val
));
1038 h
.handler
= handlers
;
1040 h
.poll_suppress_count
= poll_suppress_count
;
1041 h
.next
= handlerlist
;
1047 handlerlist
= h
.next
;
1051 static Lisp_Object
find_handler_clause ();
1053 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1054 "Signal an error. Args are SIGNAL-NAME, and associated DATA.\n\
1055 This function does not return.\n\n\
1056 A signal name is a symbol with an `error-conditions' property\n\
1057 that is a list of condition names.\n\
1058 A handler for any of those names will get to handle this signal.\n\
1059 The symbol `error' should normally be one of them.\n\
1061 DATA should be a list. Its elements are printed as part of the error message.\n\
1062 If the signal is handled, DATA is made available to the handler.\n\
1063 See also the function `condition-case'.")
1065 Lisp_Object sig
, data
;
1067 register struct handler
*allhandlers
= handlerlist
;
1068 Lisp_Object conditions
;
1069 extern int gc_in_progress
;
1070 extern int waiting_for_input
;
1071 Lisp_Object debugger_value
;
1073 quit_error_check ();
1075 if (gc_in_progress
|| waiting_for_input
)
1078 TOTALLY_UNBLOCK_INPUT
;
1080 conditions
= Fget (sig
, Qerror_conditions
);
1082 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1084 register Lisp_Object clause
;
1085 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1086 sig
, data
, &debugger_value
);
1088 #if 0 /* Most callers are not prepared to handle gc if this returns.
1089 So, since this feature is not very useful, take it out. */
1090 /* If have called debugger and user wants to continue,
1092 if (EQ (clause
, Qlambda
))
1093 return debugger_value
;
1095 if (EQ (clause
, Qlambda
))
1096 error ("Returning a value from an error is no longer supported");
1101 struct handler
*h
= handlerlist
;
1102 /* Restore the polling-suppression count. */
1103 if (h
->poll_suppress_count
> poll_suppress_count
)
1105 while (h
->poll_suppress_count
< poll_suppress_count
)
1107 handlerlist
= allhandlers
;
1108 unbind_catch (h
->tag
);
1109 h
->tag
->val
= Fcons (clause
, Fcons (sig
, data
));
1110 _longjmp (h
->tag
->jmp
, 1);
1114 handlerlist
= allhandlers
;
1115 /* If no handler is present now, try to run the debugger,
1116 and if that fails, throw to top level. */
1117 find_handler_clause (Qerror
, conditions
, sig
, data
, &debugger_value
);
1118 Fthrow (Qtop_level
, Qt
);
1121 /* Value of Qlambda means we have called debugger and
1122 user has continued. Store value returned fromdebugger
1123 into *debugger_value_ptr */
1126 find_handler_clause (handlers
, conditions
, sig
, data
, debugger_value_ptr
)
1127 Lisp_Object handlers
, conditions
, sig
, data
;
1128 Lisp_Object
*debugger_value_ptr
;
1130 register Lisp_Object h
;
1131 register Lisp_Object tem
;
1132 register Lisp_Object tem1
;
1134 if (EQ (handlers
, Qt
)) /* t is used by handlers for all conditions, set up by C code. */
1136 if (EQ (handlers
, Qerror
)) /* error is used similarly, but means display a backtrace too */
1138 if (stack_trace_on_error
)
1139 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace
, Qnil
);
1140 if (!entering_debugger
1141 && EQ (sig
, Qquit
) ? debug_on_quit
: debug_on_error
)
1143 int count
= specpdl_ptr
- specpdl
;
1144 specbind (Qdebug_on_error
, Qnil
);
1145 *debugger_value_ptr
=
1146 call_debugger (Fcons (Qerror
,
1147 Fcons (Fcons (sig
, data
),
1149 return unbind_to (count
, Qlambda
);
1153 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1158 tem
= Fmemq (Fcar (tem1
), conditions
);
1165 /* dump an error message; called like printf */
1169 error (m
, a1
, a2
, a3
)
1173 sprintf (buf
, m
, a1
, a2
, a3
);
1176 Fsignal (Qerror
, Fcons (build_string (buf
), Qnil
));
1179 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 1, 0,
1180 "T if FUNCTION makes provisions for interactive calling.\n\
1181 This means it contains a description for how to read arguments to give it.\n\
1182 The value is nil for an invalid function or a symbol with no function\n\
1185 Interactively callable functions include strings and vectors (treated\n\
1186 as keyboard macros), lambda-expressions that contain a top-level call\n\
1187 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1188 fourth argument, and some of the built-in functions of Lisp.\n\
1190 Also, a symbol satisfies `commandp' if its function definition does so.")
1192 Lisp_Object function
;
1194 register Lisp_Object fun
;
1195 register Lisp_Object funcar
;
1196 register Lisp_Object tem
;
1201 /* Dereference symbols, but avoid infinte loops. Eech. */
1202 while (XTYPE (fun
) == Lisp_Symbol
)
1204 if (++i
> 10) return Qnil
;
1205 tem
= Ffboundp (fun
);
1206 if (NILP (tem
)) return Qnil
;
1207 fun
= Fsymbol_function (fun
);
1210 /* Emacs primitives are interactive if their DEFUN specifies an
1211 interactive spec. */
1212 if (XTYPE (fun
) == Lisp_Subr
)
1214 if (XSUBR (fun
)->prompt
)
1220 /* Bytecode objects are interactive if they are long enough to
1221 have an element whose index is COMPILED_INTERACTIVE, which is
1222 where the interactive spec is stored. */
1223 else if (XTYPE (fun
) == Lisp_Compiled
)
1224 return (XVECTOR (fun
)->size
> COMPILED_INTERACTIVE
1227 /* Strings and vectors are keyboard macros. */
1228 if (XTYPE (fun
) == Lisp_String
1229 || XTYPE (fun
) == Lisp_Vector
)
1232 /* Lists may represent commands. */
1235 funcar
= Fcar (fun
);
1236 if (XTYPE (funcar
) != Lisp_Symbol
)
1237 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1238 if (EQ (funcar
, Qlambda
))
1239 return Fassq (Qinteractive
, Fcdr (Fcdr (fun
)));
1240 if (EQ (funcar
, Qmocklisp
))
1241 return Qt
; /* All mocklisp functions can be called interactively */
1242 if (EQ (funcar
, Qautoload
))
1243 return Fcar (Fcdr (Fcdr (Fcdr (fun
))));
1249 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1250 "Define FUNCTION to autoload from FILE.\n\
1251 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1252 Third arg DOCSTRING is documentation for the function.\n\
1253 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1254 Fifth arg MACRO if non-nil says the function is really a macro.\n\
1255 Third through fifth args give info about the real definition.\n\
1256 They default to nil.\n\
1257 If FUNCTION is already defined other than as an autoload,\n\
1258 this does nothing and returns nil.")
1259 (function
, file
, docstring
, interactive
, macro
)
1260 Lisp_Object function
, file
, docstring
, interactive
, macro
;
1263 Lisp_Object args
[4];
1266 CHECK_SYMBOL (function
, 0);
1267 CHECK_STRING (file
, 1);
1269 /* If function is defined and not as an autoload, don't override */
1270 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1271 && !(XTYPE (XSYMBOL (function
)->function
) == Lisp_Cons
1272 && EQ (XCONS (XSYMBOL (function
)->function
)->car
, Qautoload
)))
1277 args
[1] = docstring
;
1278 args
[2] = interactive
;
1281 return Ffset (function
, Fcons (Qautoload
, Flist (4, &args
[0])));
1282 #else /* NO_ARG_ARRAY */
1283 return Ffset (function
, Fcons (Qautoload
, Flist (4, &file
)));
1284 #endif /* not NO_ARG_ARRAY */
1288 un_autoload (oldqueue
)
1289 Lisp_Object oldqueue
;
1291 register Lisp_Object queue
, first
, second
;
1293 /* Queue to unwind is current value of Vautoload_queue.
1294 oldqueue is the shadowed value to leave in Vautoload_queue. */
1295 queue
= Vautoload_queue
;
1296 Vautoload_queue
= oldqueue
;
1297 while (CONSP (queue
))
1299 first
= Fcar (queue
);
1300 second
= Fcdr (first
);
1301 first
= Fcar (first
);
1302 if (EQ (second
, Qnil
))
1305 Ffset (first
, second
);
1306 queue
= Fcdr (queue
);
1311 do_autoload (fundef
, funname
)
1312 Lisp_Object fundef
, funname
;
1314 int count
= specpdl_ptr
- specpdl
;
1315 Lisp_Object fun
, val
;
1318 CHECK_SYMBOL (funname
, 0);
1320 /* Value saved here is to be restored into Vautoload_queue */
1321 record_unwind_protect (un_autoload
, Vautoload_queue
);
1322 Vautoload_queue
= Qt
;
1323 Fload (Fcar (Fcdr (fundef
)), Qnil
, noninteractive
? Qt
: Qnil
, Qnil
);
1324 /* Once loading finishes, don't undo it. */
1325 Vautoload_queue
= Qt
;
1326 unbind_to (count
, Qnil
);
1328 while (XTYPE (fun
) == Lisp_Symbol
)
1331 val
= XSYMBOL (fun
)->function
;
1332 if (EQ (val
, Qunbound
))
1333 Fsymbol_function (fun
); /* Get the right kind of error! */
1336 if (XTYPE (fun
) == Lisp_Cons
1337 && EQ (XCONS (fun
)->car
, Qautoload
))
1338 error ("Autoloading failed to define function %s",
1339 XSYMBOL (funname
)->name
->data
);
1342 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
1343 "Evaluate FORM and return its value.")
1347 Lisp_Object fun
, val
, original_fun
, original_args
;
1349 struct backtrace backtrace
;
1350 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1352 if (XTYPE (form
) == Lisp_Symbol
)
1354 if (EQ (Vmocklisp_arguments
, Qt
))
1355 return Fsymbol_value (form
);
1356 val
= Fsymbol_value (form
);
1359 else if (EQ (val
, Qt
))
1367 if (consing_since_gc
> gc_cons_threshold
)
1370 Fgarbage_collect ();
1374 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1376 if (max_lisp_eval_depth
< 100)
1377 max_lisp_eval_depth
= 100;
1378 if (lisp_eval_depth
> max_lisp_eval_depth
)
1379 error ("Lisp nesting exceeds max-lisp-eval-depth");
1382 original_fun
= Fcar (form
);
1383 original_args
= Fcdr (form
);
1385 backtrace
.next
= backtrace_list
;
1386 backtrace_list
= &backtrace
;
1387 backtrace
.function
= &original_fun
; /* This also protects them from gc */
1388 backtrace
.args
= &original_args
;
1389 backtrace
.nargs
= UNEVALLED
;
1390 backtrace
.evalargs
= 1;
1391 backtrace
.debug_on_exit
= 0;
1393 if (debug_on_next_call
)
1394 do_debug_on_call (Qt
);
1396 /* At this point, only original_fun and original_args
1397 have values that will be used below */
1400 while (XTYPE (fun
) == Lisp_Symbol
)
1403 val
= XSYMBOL (fun
)->function
;
1404 if (EQ (val
, Qunbound
))
1405 Fsymbol_function (fun
); /* Get the right kind of error! */
1409 if (XTYPE (fun
) == Lisp_Subr
)
1411 Lisp_Object numargs
;
1412 Lisp_Object argvals
[7];
1413 Lisp_Object args_left
;
1414 register int i
, maxargs
;
1416 args_left
= original_args
;
1417 numargs
= Flength (args_left
);
1419 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
1420 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
1421 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1423 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1425 backtrace
.evalargs
= 0;
1426 val
= (*XSUBR (fun
)->function
) (args_left
);
1430 if (XSUBR (fun
)->max_args
== MANY
)
1432 /* Pass a vector of evaluated arguments */
1434 register int argnum
= 0;
1436 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1438 GCPRO3 (args_left
, fun
, fun
);
1442 while (!NILP (args_left
))
1444 vals
[argnum
++] = Feval (Fcar (args_left
));
1445 args_left
= Fcdr (args_left
);
1446 gcpro3
.nvars
= argnum
;
1449 backtrace
.args
= vals
;
1450 backtrace
.nargs
= XINT (numargs
);
1452 val
= (*XSUBR (fun
)->function
) (XINT (numargs
), vals
);
1457 GCPRO3 (args_left
, fun
, fun
);
1458 gcpro3
.var
= argvals
;
1461 maxargs
= XSUBR (fun
)->max_args
;
1462 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
1464 argvals
[i
] = Feval (Fcar (args_left
));
1470 backtrace
.args
= argvals
;
1471 backtrace
.nargs
= XINT (numargs
);
1476 val
= (*XSUBR (fun
)->function
) ();
1479 val
= (*XSUBR (fun
)->function
) (argvals
[0]);
1482 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1]);
1485 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1489 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1490 argvals
[2], argvals
[3]);
1493 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1494 argvals
[3], argvals
[4]);
1497 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1498 argvals
[3], argvals
[4], argvals
[5]);
1502 error ("Ffuncall doesn't handle that number of arguments.");
1506 if (XTYPE (fun
) == Lisp_Compiled
)
1507 val
= apply_lambda (fun
, original_args
, 1);
1511 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1512 funcar
= Fcar (fun
);
1513 if (XTYPE (funcar
) != Lisp_Symbol
)
1514 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1515 if (EQ (funcar
, Qautoload
))
1517 do_autoload (fun
, original_fun
);
1520 if (EQ (funcar
, Qmacro
))
1521 val
= Feval (apply1 (Fcdr (fun
), original_args
));
1522 else if (EQ (funcar
, Qlambda
))
1523 val
= apply_lambda (fun
, original_args
, 1);
1524 else if (EQ (funcar
, Qmocklisp
))
1525 val
= ml_apply (fun
, original_args
);
1527 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1530 if (!EQ (Vmocklisp_arguments
, Qt
))
1534 else if (EQ (val
, Qt
))
1538 if (backtrace
.debug_on_exit
)
1539 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1540 backtrace_list
= backtrace
.next
;
1544 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
1545 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1546 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1551 register int i
, numargs
;
1552 register Lisp_Object spread_arg
;
1553 register Lisp_Object
*funcall_args
;
1555 struct gcpro gcpro1
;
1559 spread_arg
= args
[nargs
- 1];
1560 CHECK_LIST (spread_arg
, nargs
);
1562 numargs
= XINT (Flength (spread_arg
));
1565 return Ffuncall (nargs
- 1, args
);
1566 else if (numargs
== 1)
1568 args
[nargs
- 1] = XCONS (spread_arg
)->car
;
1569 return Ffuncall (nargs
, args
);
1572 numargs
+= nargs
- 2;
1574 while (XTYPE (fun
) == Lisp_Symbol
)
1577 fun
= XSYMBOL (fun
)->function
;
1578 if (EQ (fun
, Qunbound
))
1580 /* Let funcall get the error */
1586 if (XTYPE (fun
) == Lisp_Subr
)
1588 if (numargs
< XSUBR (fun
)->min_args
1589 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1590 goto funcall
; /* Let funcall get the error */
1591 else if (XSUBR (fun
)->max_args
> numargs
)
1593 /* Avoid making funcall cons up a yet another new vector of arguments
1594 by explicitly supplying nil's for optional values */
1595 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
1596 * sizeof (Lisp_Object
));
1597 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
1598 funcall_args
[++i
] = Qnil
;
1599 GCPRO1 (*funcall_args
);
1600 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
1604 /* We add 1 to numargs because funcall_args includes the
1605 function itself as well as its arguments. */
1608 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
1609 * sizeof (Lisp_Object
));
1610 GCPRO1 (*funcall_args
);
1611 gcpro1
.nvars
= 1 + numargs
;
1614 bcopy (args
, funcall_args
, nargs
* sizeof (Lisp_Object
));
1615 /* Spread the last arg we got. Its first element goes in
1616 the slot that it used to occupy, hence this value of I. */
1618 while (!NILP (spread_arg
))
1620 funcall_args
[i
++] = XCONS (spread_arg
)->car
;
1621 spread_arg
= XCONS (spread_arg
)->cdr
;
1624 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
1627 /* Apply fn to arg */
1630 Lisp_Object fn
, arg
;
1632 struct gcpro gcpro1
;
1636 RETURN_UNGCPRO (Ffuncall (1, &fn
));
1640 Lisp_Object args
[2];
1644 RETURN_UNGCPRO (Fapply (2, args
));
1646 #else /* not NO_ARG_ARRAY */
1647 RETURN_UNGCPRO (Fapply (2, &fn
));
1648 #endif /* not NO_ARG_ARRAY */
1651 /* Call function fn on no arguments */
1656 struct gcpro gcpro1
;
1659 RETURN_UNGCPRO (Ffuncall (1, &fn
));
1662 /* Call function fn with argument arg */
1666 Lisp_Object fn
, arg
;
1668 struct gcpro gcpro1
;
1670 Lisp_Object args
[2];
1676 RETURN_UNGCPRO (Ffuncall (2, args
));
1677 #else /* not NO_ARG_ARRAY */
1680 RETURN_UNGCPRO (Ffuncall (2, &fn
));
1681 #endif /* not NO_ARG_ARRAY */
1684 /* Call function fn with arguments arg, arg1 */
1687 call2 (fn
, arg
, arg1
)
1688 Lisp_Object fn
, arg
, arg1
;
1690 struct gcpro gcpro1
;
1692 Lisp_Object args
[3];
1698 RETURN_UNGCPRO (Ffuncall (3, args
));
1699 #else /* not NO_ARG_ARRAY */
1702 RETURN_UNGCPRO (Ffuncall (3, &fn
));
1703 #endif /* not NO_ARG_ARRAY */
1706 /* Call function fn with arguments arg, arg1, arg2 */
1709 call3 (fn
, arg
, arg1
, arg2
)
1710 Lisp_Object fn
, arg
, arg1
, arg2
;
1712 struct gcpro gcpro1
;
1714 Lisp_Object args
[4];
1721 RETURN_UNGCPRO (Ffuncall (4, args
));
1722 #else /* not NO_ARG_ARRAY */
1725 RETURN_UNGCPRO (Ffuncall (4, &fn
));
1726 #endif /* not NO_ARG_ARRAY */
1729 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
1730 "Call first argument as a function, passing remaining arguments to it.\n\
1731 Thus, (funcall 'cons 'x 'y) returns (x . y).")
1738 int numargs
= nargs
- 1;
1739 Lisp_Object lisp_numargs
;
1741 struct backtrace backtrace
;
1742 register Lisp_Object
*internal_args
;
1746 if (consing_since_gc
> gc_cons_threshold
)
1747 Fgarbage_collect ();
1749 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1751 if (max_lisp_eval_depth
< 100)
1752 max_lisp_eval_depth
= 100;
1753 if (lisp_eval_depth
> max_lisp_eval_depth
)
1754 error ("Lisp nesting exceeds max-lisp-eval-depth");
1757 backtrace
.next
= backtrace_list
;
1758 backtrace_list
= &backtrace
;
1759 backtrace
.function
= &args
[0];
1760 backtrace
.args
= &args
[1];
1761 backtrace
.nargs
= nargs
- 1;
1762 backtrace
.evalargs
= 0;
1763 backtrace
.debug_on_exit
= 0;
1765 if (debug_on_next_call
)
1766 do_debug_on_call (Qlambda
);
1771 while (XTYPE (fun
) == Lisp_Symbol
)
1774 val
= XSYMBOL (fun
)->function
;
1775 if (EQ (val
, Qunbound
))
1776 Fsymbol_function (fun
); /* Get the right kind of error! */
1780 if (XTYPE (fun
) == Lisp_Subr
)
1782 if (numargs
< XSUBR (fun
)->min_args
1783 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1785 XFASTINT (lisp_numargs
) = numargs
;
1786 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (lisp_numargs
, Qnil
)));
1789 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1790 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1792 if (XSUBR (fun
)->max_args
== MANY
)
1794 val
= (*XSUBR (fun
)->function
) (numargs
, args
+ 1);
1798 if (XSUBR (fun
)->max_args
> numargs
)
1800 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
1801 bcopy (args
+ 1, internal_args
, numargs
* sizeof (Lisp_Object
));
1802 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
1803 internal_args
[i
] = Qnil
;
1806 internal_args
= args
+ 1;
1807 switch (XSUBR (fun
)->max_args
)
1810 val
= (*XSUBR (fun
)->function
) ();
1813 val
= (*XSUBR (fun
)->function
) (internal_args
[0]);
1816 val
= (*XSUBR (fun
)->function
) (internal_args
[0],
1820 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1824 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1829 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1830 internal_args
[2], internal_args
[3],
1834 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
1835 internal_args
[2], internal_args
[3],
1836 internal_args
[4], internal_args
[5]);
1840 error ("funcall: this number of args not handled.");
1843 if (XTYPE (fun
) == Lisp_Compiled
)
1844 val
= funcall_lambda (fun
, numargs
, args
+ 1);
1848 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1849 funcar
= Fcar (fun
);
1850 if (XTYPE (funcar
) != Lisp_Symbol
)
1851 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1852 if (EQ (funcar
, Qlambda
))
1853 val
= funcall_lambda (fun
, numargs
, args
+ 1);
1854 else if (EQ (funcar
, Qmocklisp
))
1855 val
= ml_apply (fun
, Flist (numargs
, args
+ 1));
1856 else if (EQ (funcar
, Qautoload
))
1858 do_autoload (fun
, args
[0]);
1862 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1866 if (backtrace
.debug_on_exit
)
1867 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1868 backtrace_list
= backtrace
.next
;
1873 apply_lambda (fun
, args
, eval_flag
)
1874 Lisp_Object fun
, args
;
1877 Lisp_Object args_left
;
1878 Lisp_Object numargs
;
1879 register Lisp_Object
*arg_vector
;
1880 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1882 register Lisp_Object tem
;
1884 numargs
= Flength (args
);
1885 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1888 GCPRO3 (*arg_vector
, args_left
, fun
);
1891 for (i
= 0; i
< XINT (numargs
);)
1893 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
1894 if (eval_flag
) tem
= Feval (tem
);
1895 arg_vector
[i
++] = tem
;
1903 backtrace_list
->args
= arg_vector
;
1904 backtrace_list
->nargs
= i
;
1906 backtrace_list
->evalargs
= 0;
1907 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
1909 /* Do the debug-on-exit now, while arg_vector still exists. */
1910 if (backtrace_list
->debug_on_exit
)
1911 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
1912 /* Don't do it again when we return to eval. */
1913 backtrace_list
->debug_on_exit
= 0;
1917 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
1918 and return the result of evaluation.
1919 FUN must be either a lambda-expression or a compiled-code object. */
1922 funcall_lambda (fun
, nargs
, arg_vector
)
1925 register Lisp_Object
*arg_vector
;
1927 Lisp_Object val
, tem
;
1928 register Lisp_Object syms_left
;
1929 Lisp_Object numargs
;
1930 register Lisp_Object next
;
1931 int count
= specpdl_ptr
- specpdl
;
1933 int optional
= 0, rest
= 0;
1935 specbind (Qmocklisp_arguments
, Qt
); /* t means NOT mocklisp! */
1937 XFASTINT (numargs
) = nargs
;
1939 if (XTYPE (fun
) == Lisp_Cons
)
1940 syms_left
= Fcar (Fcdr (fun
));
1941 else if (XTYPE (fun
) == Lisp_Compiled
)
1942 syms_left
= XVECTOR (fun
)->contents
[COMPILED_ARGLIST
];
1946 for (; !NILP (syms_left
); syms_left
= Fcdr (syms_left
))
1949 next
= Fcar (syms_left
);
1950 while (XTYPE (next
) != Lisp_Symbol
)
1951 next
= Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1952 if (EQ (next
, Qand_rest
))
1954 else if (EQ (next
, Qand_optional
))
1958 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
1963 tem
= arg_vector
[i
++];
1964 specbind (next
, tem
);
1967 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1969 specbind (next
, Qnil
);
1973 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1975 if (XTYPE (fun
) == Lisp_Cons
)
1976 val
= Fprogn (Fcdr (Fcdr (fun
)));
1978 val
= Fbyte_code (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
],
1979 XVECTOR (fun
)->contents
[COMPILED_CONSTANTS
],
1980 XVECTOR (fun
)->contents
[COMPILED_STACK_DEPTH
]);
1981 return unbind_to (count
, val
);
1987 register int count
= specpdl_ptr
- specpdl
;
1988 if (specpdl_size
>= max_specpdl_size
)
1990 if (max_specpdl_size
< 400)
1991 max_specpdl_size
= 400;
1992 if (specpdl_size
>= max_specpdl_size
)
1995 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil
));
1996 max_specpdl_size
*= 2;
2000 if (specpdl_size
> max_specpdl_size
)
2001 specpdl_size
= max_specpdl_size
;
2002 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
2003 specpdl_ptr
= specpdl
+ count
;
2007 specbind (symbol
, value
)
2008 Lisp_Object symbol
, value
;
2010 extern void store_symval_forwarding (); /* in eval.c */
2013 CHECK_SYMBOL (symbol
, 0);
2015 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2017 specpdl_ptr
->symbol
= symbol
;
2018 specpdl_ptr
->func
= 0;
2019 ovalue
= XSYMBOL (symbol
)->value
;
2020 specpdl_ptr
->old_value
= EQ (ovalue
, Qunbound
) ? Qunbound
: Fsymbol_value (symbol
);
2022 if (XTYPE (ovalue
) == Lisp_Buffer_Objfwd
)
2023 store_symval_forwarding (symbol
, ovalue
, value
);
2025 Fset (symbol
, value
);
2029 record_unwind_protect (function
, arg
)
2030 Lisp_Object (*function
)();
2033 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2035 specpdl_ptr
->func
= function
;
2036 specpdl_ptr
->symbol
= Qnil
;
2037 specpdl_ptr
->old_value
= arg
;
2042 unbind_to (count
, value
)
2046 int quitf
= !NILP (Vquit_flag
);
2047 struct gcpro gcpro1
;
2053 while (specpdl_ptr
!= specpdl
+ count
)
2056 if (specpdl_ptr
->func
!= 0)
2057 (*specpdl_ptr
->func
) (specpdl_ptr
->old_value
);
2058 /* Note that a "binding" of nil is really an unwind protect,
2059 so in that case the "old value" is a list of forms to evaluate. */
2060 else if (NILP (specpdl_ptr
->symbol
))
2061 Fprogn (specpdl_ptr
->old_value
);
2063 Fset (specpdl_ptr
->symbol
, specpdl_ptr
->old_value
);
2065 if (NILP (Vquit_flag
) && quitf
) Vquit_flag
= Qt
;
2074 /* Get the value of symbol's global binding, even if that binding
2075 is not now dynamically visible. */
2078 top_level_value (symbol
)
2081 register struct specbinding
*ptr
= specpdl
;
2083 CHECK_SYMBOL (symbol
, 0);
2084 for (; ptr
!= specpdl_ptr
; ptr
++)
2086 if (EQ (ptr
->symbol
, symbol
))
2087 return ptr
->old_value
;
2089 return Fsymbol_value (symbol
);
2093 top_level_set (symbol
, newval
)
2094 Lisp_Object symbol
, newval
;
2096 register struct specbinding
*ptr
= specpdl
;
2098 CHECK_SYMBOL (symbol
, 0);
2099 for (; ptr
!= specpdl_ptr
; ptr
++)
2101 if (EQ (ptr
->symbol
, symbol
))
2103 ptr
->old_value
= newval
;
2107 return Fset (symbol
, newval
);
2112 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
2113 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2114 The debugger is entered when that frame exits, if the flag is non-nil.")
2116 Lisp_Object level
, flag
;
2118 register struct backtrace
*backlist
= backtrace_list
;
2121 CHECK_NUMBER (level
, 0);
2123 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
2125 backlist
= backlist
->next
;
2129 backlist
->debug_on_exit
= !NILP (flag
);
2134 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
2135 "Print a trace of Lisp function calls currently active.\n\
2136 Output stream used is value of `standard-output'.")
2139 register struct backtrace
*backlist
= backtrace_list
;
2143 extern Lisp_Object Vprint_level
;
2144 struct gcpro gcpro1
;
2146 entering_debugger
= 0;
2148 XFASTINT (Vprint_level
) = 3;
2155 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
2156 if (backlist
->nargs
== UNEVALLED
)
2158 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
2162 tem
= *backlist
->function
;
2163 Fprin1 (tem
, Qnil
); /* This can QUIT */
2164 write_string ("(", -1);
2165 if (backlist
->nargs
== MANY
)
2167 for (tail
= *backlist
->args
, i
= 0;
2169 tail
= Fcdr (tail
), i
++)
2171 if (i
) write_string (" ", -1);
2172 Fprin1 (Fcar (tail
), Qnil
);
2177 for (i
= 0; i
< backlist
->nargs
; i
++)
2179 if (i
) write_string (" ", -1);
2180 Fprin1 (backlist
->args
[i
], Qnil
);
2184 write_string (")\n", -1);
2185 backlist
= backlist
->next
;
2188 Vprint_level
= Qnil
;
2193 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, "",
2194 "Return the function and arguments N frames up from current execution point.\n\
2195 If that frame has not evaluated the arguments yet (or is a special form),\n\
2196 the value is (nil FUNCTION ARG-FORMS...).\n\
2197 If that frame has evaluated its arguments and called its function already,\n\
2198 the value is (t FUNCTION ARG-VALUES...).\n\
2199 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2200 FUNCTION is whatever was supplied as car of evaluated list,\n\
2201 or a lambda expression for macro calls.\n\
2202 If N is more than the number of frames, the value is nil.")
2204 Lisp_Object nframes
;
2206 register struct backtrace
*backlist
= backtrace_list
;
2210 CHECK_NATNUM (nframes
, 0);
2212 /* Find the frame requested. */
2213 for (i
= 0; i
< XFASTINT (nframes
); i
++)
2214 backlist
= backlist
->next
;
2218 if (backlist
->nargs
== UNEVALLED
)
2219 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
2222 if (backlist
->nargs
== MANY
)
2223 tem
= *backlist
->args
;
2225 tem
= Flist (backlist
->nargs
, backlist
->args
);
2227 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
2233 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
2234 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2236 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
2237 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2238 This limit is to catch infinite recursions for you before they cause\n\
2239 actual stack overflow in C, which would be fatal for Emacs.\n\
2240 You can safely make it considerably larger than its default value,\n\
2241 if that proves inconveniently small.");
2243 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
2244 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2245 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2248 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
2249 "Non-nil inhibits C-g quitting from happening immediately.\n\
2250 Note that `quit-flag' will still be set by typing C-g,\n\
2251 so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2252 To prevent this happening, set `quit-flag' to nil\n\
2253 before making `inhibit-quit' nil.");
2254 Vinhibit_quit
= Qnil
;
2256 Qinhibit_quit
= intern ("inhibit-quit");
2257 staticpro (&Qinhibit_quit
);
2259 Qautoload
= intern ("autoload");
2260 staticpro (&Qautoload
);
2262 Qdebug_on_error
= intern ("debug-on-error");
2263 staticpro (&Qdebug_on_error
);
2265 Qmacro
= intern ("macro");
2266 staticpro (&Qmacro
);
2268 /* Note that the process handling also uses Qexit, but we don't want
2269 to staticpro it twice, so we just do it here. */
2270 Qexit
= intern ("exit");
2273 Qinteractive
= intern ("interactive");
2274 staticpro (&Qinteractive
);
2276 Qcommandp
= intern ("commandp");
2277 staticpro (&Qcommandp
);
2279 Qdefun
= intern ("defun");
2280 staticpro (&Qdefun
);
2282 Qand_rest
= intern ("&rest");
2283 staticpro (&Qand_rest
);
2285 Qand_optional
= intern ("&optional");
2286 staticpro (&Qand_optional
);
2288 DEFVAR_BOOL ("stack-trace-on-error", &stack_trace_on_error
,
2289 "*Non-nil means automatically display a backtrace buffer\n\
2290 after any error that is handled by the editor command loop.");
2291 stack_trace_on_error
= 0;
2293 DEFVAR_BOOL ("debug-on-error", &debug_on_error
,
2294 "*Non-nil means enter debugger if an error is signaled.\n\
2295 Does not apply to errors handled by `condition-case'.\n\
2296 See also variable `debug-on-quit'.");
2299 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
2300 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
2301 Does not apply if quit is handled by a `condition-case'.");
2304 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
2305 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2307 DEFVAR_LISP ("debugger", &Vdebugger
,
2308 "Function to call to invoke debugger.\n\
2309 If due to frame exit, args are `exit' and the value being returned;\n\
2310 this function's value will be returned instead of that.\n\
2311 If due to error, args are `error' and a list of the args to `signal'.\n\
2312 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2313 If due to `eval' entry, one arg, t.");
2316 Qmocklisp_arguments
= intern ("mocklisp-arguments");
2317 staticpro (&Qmocklisp_arguments
);
2318 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments
,
2319 "While in a mocklisp function, the list of its unevaluated args.");
2320 Vmocklisp_arguments
= Qt
;
2322 DEFVAR_LISP ("run-hooks", &Vrun_hooks
,
2323 "Set to the function `run-hooks', if that function has been defined.\n\
2324 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2327 staticpro (&Vautoload_queue
);
2328 Vautoload_queue
= Qnil
;
2339 defsubr (&Sfunction
);
2341 defsubr (&Sdefmacro
);
2343 defsubr (&Sdefconst
);
2344 defsubr (&Suser_variable_p
);
2348 defsubr (&Smacroexpand
);
2351 defsubr (&Sunwind_protect
);
2352 defsubr (&Scondition_case
);
2354 defsubr (&Sinteractive_p
);
2355 defsubr (&Scommandp
);
2356 defsubr (&Sautoload
);
2359 defsubr (&Sfuncall
);
2360 defsubr (&Sbacktrace_debug
);
2361 defsubr (&Sbacktrace
);
2362 defsubr (&Sbacktrace_frame
);