1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 1999 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 2, 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "blockinput.h"
27 #include "dispextern.h"
30 /* This definition is duplicated in alloc.c and keyboard.c */
31 /* Putting it in lisp.h makes cc bomb out! */
35 struct backtrace
*next
;
36 Lisp_Object
*function
;
37 Lisp_Object
*args
; /* Points to vector of args. */
38 int nargs
; /* Length of vector.
39 If nargs is UNEVALLED, args points to slot holding
40 list of unevalled args */
42 /* Nonzero means call value of debugger when done with this operation. */
46 struct backtrace
*backtrace_list
;
48 /* This structure helps implement the `catch' and `throw' control
49 structure. A struct catchtag contains all the information needed
50 to restore the state of the interpreter after a non-local jump.
52 Handlers for error conditions (represented by `struct handler'
53 structures) just point to a catch tag to do the cleanup required
56 catchtag structures are chained together in the C calling stack;
57 the `next' member points to the next outer catchtag.
59 A call like (throw TAG VAL) searches for a catchtag whose `tag'
60 member is TAG, and then unbinds to it. The `val' member is used to
61 hold VAL while the stack is unwound; `val' is returned as the value
64 All the other members are concerned with restoring the interpreter
70 struct catchtag
*next
;
73 struct backtrace
*backlist
;
74 struct handler
*handlerlist
;
77 int poll_suppress_count
;
78 struct byte_stack
*byte_stack
;
81 struct catchtag
*catchlist
;
84 /* Count levels of GCPRO to detect failure to UNGCPRO. */
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 /* This holds either the symbol `run-hooks' or nil.
95 It is nil at an early stage of startup, and when Emacs
97 Lisp_Object Vrun_hooks
;
99 /* Non-nil means record all fset's and provide's, to be undone
100 if the file being autoloaded is not fully loaded.
101 They are recorded by being consed onto the front of Vautoload_queue:
102 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
104 Lisp_Object Vautoload_queue
;
106 /* Current number of specbindings allocated in specpdl. */
109 /* Pointer to beginning of specpdl. */
110 struct specbinding
*specpdl
;
112 /* Pointer to first unused element in specpdl. */
113 struct specbinding
*specpdl_ptr
;
115 /* Maximum size allowed for specpdl allocation */
116 int max_specpdl_size
;
118 /* Depth in Lisp evaluations and function calls. */
121 /* Maximum allowed depth in Lisp evaluations and function calls. */
122 int max_lisp_eval_depth
;
124 /* Nonzero means enter debugger before next function call */
125 int debug_on_next_call
;
127 /* List of conditions (non-nil atom means all) which cause a backtrace
128 if an error is handled by the command loop's error handler. */
129 Lisp_Object Vstack_trace_on_error
;
131 /* List of conditions (non-nil atom means all) which enter the debugger
132 if an error is handled by the command loop's error handler. */
133 Lisp_Object Vdebug_on_error
;
135 /* List of conditions and regexps specifying error messages which
136 do not enter the debugger even if Vdebug_on_errors says they should. */
137 Lisp_Object Vdebug_ignored_errors
;
139 /* Non-nil means call the debugger even if the error will be handled. */
140 Lisp_Object Vdebug_on_signal
;
142 /* Hook for edebug to use. */
143 Lisp_Object Vsignal_hook_function
;
145 /* Nonzero means enter debugger if a quit signal
146 is handled by the command loop's error handler. */
149 /* The value of num_nonmacro_input_events as of the last time we
150 started to enter the debugger. If we decide to enter the debugger
151 again when this is still equal to num_nonmacro_input_events, then we
152 know that the debugger itself has an error, and we should just
153 signal the error instead of entering an infinite loop of debugger
155 int when_entered_debugger
;
157 Lisp_Object Vdebugger
;
159 void specbind (), record_unwind_protect ();
161 Lisp_Object
run_hook_with_args ();
163 Lisp_Object
funcall_lambda ();
164 extern Lisp_Object
ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
170 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
171 specpdl_ptr
= specpdl
;
172 max_specpdl_size
= 600;
173 max_lisp_eval_depth
= 300;
181 specpdl_ptr
= specpdl
;
186 debug_on_next_call
= 0;
191 /* This is less than the initial value of num_nonmacro_input_events. */
192 when_entered_debugger
= -1;
199 int debug_while_redisplaying
;
202 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
203 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
205 if (specpdl_size
+ 40 > max_specpdl_size
)
206 max_specpdl_size
= specpdl_size
+ 40;
208 debug_on_next_call
= 0;
209 when_entered_debugger
= num_nonmacro_input_events
;
211 /* Resetting redisplaying_p to 0 makes sure that debug output is
212 displayed if the debugger is invoked during redisplay. */
213 debug_while_redisplaying
= redisplaying_p
;
216 val
= apply1 (Vdebugger
, arg
);
218 /* Interrupting redisplay and resuming it later is not safe under
219 all circumstances. So, when the debugger returns, abort the
220 interupted redisplay by going back to the top-level. */
221 if (debug_while_redisplaying
)
228 do_debug_on_call (code
)
231 debug_on_next_call
= 0;
232 backtrace_list
->debug_on_exit
= 1;
233 call_debugger (Fcons (code
, Qnil
));
236 /* NOTE!!! Every function that can call EVAL must protect its args
237 and temporaries from garbage collection while it needs them.
238 The definition of `For' shows what you have to do. */
240 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
241 "Eval args until one of them yields non-nil, then return that value.\n\
242 The remaining args are not evalled at all.\n\
243 If all args return nil, return nil.")
247 register Lisp_Object val
;
248 Lisp_Object args_left
;
259 val
= Feval (Fcar (args_left
));
262 args_left
= Fcdr (args_left
);
264 while (!NILP(args_left
));
270 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
271 "Eval args until one of them yields nil, then return nil.\n\
272 The remaining args are not evalled at all.\n\
273 If no arg yields nil, return the last arg's value.")
277 register Lisp_Object val
;
278 Lisp_Object args_left
;
289 val
= Feval (Fcar (args_left
));
292 args_left
= Fcdr (args_left
);
294 while (!NILP(args_left
));
300 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
301 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
302 Returns the value of THEN or the value of the last of the ELSE's.\n\
303 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
304 If COND yields nil, and there are no ELSE's, the value is nil.")
308 register Lisp_Object cond
;
312 cond
= Feval (Fcar (args
));
316 return Feval (Fcar (Fcdr (args
)));
317 return Fprogn (Fcdr (Fcdr (args
)));
320 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
321 "(cond CLAUSES...): try each clause until one succeeds.\n\
322 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
323 and, if the value is non-nil, this clause succeeds:\n\
324 then the expressions in BODY are evaluated and the last one's\n\
325 value is the value of the cond-form.\n\
326 If no clause succeeds, cond returns nil.\n\
327 If a clause has one element, as in (CONDITION),\n\
328 CONDITION's value if non-nil is returned from the cond-form.")
332 register Lisp_Object clause
, val
;
339 clause
= Fcar (args
);
340 val
= Feval (Fcar (clause
));
343 if (!EQ (XCDR (clause
), Qnil
))
344 val
= Fprogn (XCDR (clause
));
354 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
355 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
359 register Lisp_Object val
, tem
;
360 Lisp_Object args_left
;
363 /* In Mocklisp code, symbols at the front of the progn arglist
364 are to be bound to zero. */
365 if (!EQ (Vmocklisp_arguments
, Qt
))
367 val
= make_number (0);
368 while (!NILP (args
) && (tem
= Fcar (args
), SYMBOLP (tem
)))
371 specbind (tem
, val
), args
= Fcdr (args
);
383 val
= Feval (Fcar (args_left
));
384 args_left
= Fcdr (args_left
);
386 while (!NILP(args_left
));
392 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
393 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
394 The value of FIRST is saved during the evaluation of the remaining args,\n\
395 whose values are discarded.")
400 register Lisp_Object args_left
;
401 struct gcpro gcpro1
, gcpro2
;
402 register int argnum
= 0;
414 val
= Feval (Fcar (args_left
));
416 Feval (Fcar (args_left
));
417 args_left
= Fcdr (args_left
);
419 while (!NILP(args_left
));
425 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
426 "(prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
427 The value of Y is saved during the evaluation of the remaining args,\n\
428 whose values are discarded.")
433 register Lisp_Object args_left
;
434 struct gcpro gcpro1
, gcpro2
;
435 register int argnum
= -1;
449 val
= Feval (Fcar (args_left
));
451 Feval (Fcar (args_left
));
452 args_left
= Fcdr (args_left
);
454 while (!NILP (args_left
));
460 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
461 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
462 The symbols SYM are variables; they are literal (not evaluated).\n\
463 The values VAL are expressions; they are evaluated.\n\
464 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
465 The second VAL is not computed until after the first SYM is set, and so on;\n\
466 each VAL can use the new value of variables set earlier in the `setq'.\n\
467 The return value of the `setq' form is the value of the last VAL.")
471 register Lisp_Object args_left
;
472 register Lisp_Object val
, sym
;
483 val
= Feval (Fcar (Fcdr (args_left
)));
484 sym
= Fcar (args_left
);
486 args_left
= Fcdr (Fcdr (args_left
));
488 while (!NILP(args_left
));
494 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
495 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
502 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
503 "Like `quote', but preferred for objects which are functions.\n\
504 In byte compilation, `function' causes its argument to be compiled.\n\
505 `quote' cannot do that.")
512 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
513 "Return t if function in which this appears was called interactively.\n\
514 This means that the function was called with call-interactively (which\n\
515 includes being called as the binding of a key)\n\
516 and input is currently coming from the keyboard (not in keyboard macro).")
519 register struct backtrace
*btp
;
520 register Lisp_Object fun
;
525 btp
= backtrace_list
;
527 /* If this isn't a byte-compiled function, there may be a frame at
528 the top for Finteractive_p itself. If so, skip it. */
529 fun
= Findirect_function (*btp
->function
);
530 if (SUBRP (fun
) && XSUBR (fun
) == &Sinteractive_p
)
533 /* If we're running an Emacs 18-style byte-compiled function, there
534 may be a frame for Fbytecode. Now, given the strictest
535 definition, this function isn't really being called
536 interactively, but because that's the way Emacs 18 always builds
537 byte-compiled functions, we'll accept it for now. */
538 if (EQ (*btp
->function
, Qbytecode
))
541 /* If this isn't a byte-compiled function, then we may now be
542 looking at several frames for special forms. Skip past them. */
544 btp
->nargs
== UNEVALLED
)
547 /* btp now points at the frame of the innermost function that isn't
548 a special form, ignoring frames for Finteractive_p and/or
549 Fbytecode at the top. If this frame is for a built-in function
550 (such as load or eval-region) return nil. */
551 fun
= Findirect_function (*btp
->function
);
554 /* btp points to the frame of a Lisp function that called interactive-p.
555 Return t if that function was called interactively. */
556 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
561 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
562 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
563 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
564 See also the function `interactive'.")
568 register Lisp_Object fn_name
;
569 register Lisp_Object defn
;
571 fn_name
= Fcar (args
);
572 defn
= Fcons (Qlambda
, Fcdr (args
));
573 if (!NILP (Vpurify_flag
))
574 defn
= Fpurecopy (defn
);
575 Ffset (fn_name
, defn
);
576 LOADHIST_ATTACH (fn_name
);
580 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
581 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
582 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
583 When the macro is called, as in (NAME ARGS...),\n\
584 the function (lambda ARGLIST BODY...) is applied to\n\
585 the list ARGS... as it appears in the expression,\n\
586 and the result should be a form to be evaluated instead of the original.")
590 register Lisp_Object fn_name
;
591 register Lisp_Object defn
;
593 fn_name
= Fcar (args
);
594 defn
= Fcons (Qmacro
, Fcons (Qlambda
, Fcdr (args
)));
595 if (!NILP (Vpurify_flag
))
596 defn
= Fpurecopy (defn
);
597 Ffset (fn_name
, defn
);
598 LOADHIST_ATTACH (fn_name
);
602 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
603 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
604 You are not required to define a variable in order to use it,\n\
605 but the definition can supply documentation and an initial value\n\
606 in a way that tags can recognize.\n\n\
607 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
608 If SYMBOL is buffer-local, its default value is what is set;\n\
609 buffer-local values are not affected.\n\
610 INITVALUE and DOCSTRING are optional.\n\
611 If DOCSTRING starts with *, this variable is identified as a user option.\n\
612 This means that M-x set-variable and M-x edit-options recognize it.\n\
613 If INITVALUE is missing, SYMBOL's value is not set.")
617 register Lisp_Object sym
, tem
, tail
;
621 if (!NILP (Fcdr (Fcdr (tail
))))
622 error ("too many arguments");
626 tem
= Fdefault_boundp (sym
);
628 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
630 tail
= Fcdr (Fcdr (args
));
631 if (!NILP (Fcar (tail
)))
634 if (!NILP (Vpurify_flag
))
635 tem
= Fpurecopy (tem
);
636 Fput (sym
, Qvariable_documentation
, tem
);
638 LOADHIST_ATTACH (sym
);
642 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
643 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
644 The intent is that neither programs nor users should ever change this value.\n\
645 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
646 If SYMBOL is buffer-local, its default value is what is set;\n\
647 buffer-local values are not affected.\n\
648 DOCSTRING is optional.")
652 register Lisp_Object sym
, tem
;
655 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
656 error ("too many arguments");
658 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
659 tem
= Fcar (Fcdr (Fcdr (args
)));
662 if (!NILP (Vpurify_flag
))
663 tem
= Fpurecopy (tem
);
664 Fput (sym
, Qvariable_documentation
, tem
);
666 LOADHIST_ATTACH (sym
);
670 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
671 "Returns t if VARIABLE is intended to be set and modified by users.\n\
672 \(The alternative is a variable used internally in a Lisp program.)\n\
673 Determined by whether the first character of the documentation\n\
674 for the variable is `*'.")
676 Lisp_Object variable
;
678 Lisp_Object documentation
;
680 if (!SYMBOLP (variable
))
683 documentation
= Fget (variable
, Qvariable_documentation
);
684 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
686 if (STRINGP (documentation
)
687 && ((unsigned char) XSTRING (documentation
)->data
[0] == '*'))
689 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
690 if (CONSP (documentation
)
691 && STRINGP (XCAR (documentation
))
692 && INTEGERP (XCDR (documentation
))
693 && XINT (XCDR (documentation
)) < 0)
698 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
699 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
700 The value of the last form in BODY is returned.\n\
701 Each element of VARLIST is a symbol (which is bound to nil)\n\
702 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
703 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
707 Lisp_Object varlist
, val
, elt
;
708 int count
= specpdl_ptr
- specpdl
;
709 struct gcpro gcpro1
, gcpro2
, gcpro3
;
711 GCPRO3 (args
, elt
, varlist
);
713 varlist
= Fcar (args
);
714 while (!NILP (varlist
))
717 elt
= Fcar (varlist
);
719 specbind (elt
, Qnil
);
720 else if (! NILP (Fcdr (Fcdr (elt
))))
722 Fcons (build_string ("`let' bindings can have only one value-form"),
726 val
= Feval (Fcar (Fcdr (elt
)));
727 specbind (Fcar (elt
), val
);
729 varlist
= Fcdr (varlist
);
732 val
= Fprogn (Fcdr (args
));
733 return unbind_to (count
, val
);
736 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
737 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
738 The value of the last form in BODY is returned.\n\
739 Each element of VARLIST is a symbol (which is bound to nil)\n\
740 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
741 All the VALUEFORMs are evalled before any symbols are bound.")
745 Lisp_Object
*temps
, tem
;
746 register Lisp_Object elt
, varlist
;
747 int count
= specpdl_ptr
- specpdl
;
749 struct gcpro gcpro1
, gcpro2
;
751 varlist
= Fcar (args
);
753 /* Make space to hold the values to give the bound variables */
754 elt
= Flength (varlist
);
755 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
757 /* Compute the values and store them in `temps' */
759 GCPRO2 (args
, *temps
);
762 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
765 elt
= Fcar (varlist
);
767 temps
[argnum
++] = Qnil
;
768 else if (! NILP (Fcdr (Fcdr (elt
))))
770 Fcons (build_string ("`let' bindings can have only one value-form"),
773 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
774 gcpro2
.nvars
= argnum
;
778 varlist
= Fcar (args
);
779 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
781 elt
= Fcar (varlist
);
782 tem
= temps
[argnum
++];
786 specbind (Fcar (elt
), tem
);
789 elt
= Fprogn (Fcdr (args
));
790 return unbind_to (count
, elt
);
793 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
794 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
795 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
796 until TEST returns nil.")
800 Lisp_Object test
, body
, tem
;
801 struct gcpro gcpro1
, gcpro2
;
807 while (tem
= Feval (test
),
808 (!EQ (Vmocklisp_arguments
, Qt
) ? XINT (tem
) : !NILP (tem
)))
818 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
819 "Return result of expanding macros at top level of FORM.\n\
820 If FORM is not a macro call, it is returned unchanged.\n\
821 Otherwise, the macro is expanded and the expansion is considered\n\
822 in place of FORM. When a non-macro-call results, it is returned.\n\n\
823 The second optional arg ENVIRONMENT species an environment of macro\n\
824 definitions to shadow the loaded ones for use in file byte-compilation.")
827 Lisp_Object environment
;
829 /* With cleanups from Hallvard Furuseth. */
830 register Lisp_Object expander
, sym
, def
, tem
;
834 /* Come back here each time we expand a macro call,
835 in case it expands into another macro call. */
838 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
839 def
= sym
= XCAR (form
);
841 /* Trace symbols aliases to other symbols
842 until we get a symbol that is not an alias. */
843 while (SYMBOLP (def
))
847 tem
= Fassq (sym
, environment
);
850 def
= XSYMBOL (sym
)->function
;
851 if (!EQ (def
, Qunbound
))
856 /* Right now TEM is the result from SYM in ENVIRONMENT,
857 and if TEM is nil then DEF is SYM's function definition. */
860 /* SYM is not mentioned in ENVIRONMENT.
861 Look at its function definition. */
862 if (EQ (def
, Qunbound
) || !CONSP (def
))
863 /* Not defined or definition not suitable */
865 if (EQ (XCAR (def
), Qautoload
))
867 /* Autoloading function: will it be a macro when loaded? */
868 tem
= Fnth (make_number (4), def
);
869 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
870 /* Yes, load it and try again. */
874 do_autoload (def
, sym
);
881 else if (!EQ (XCAR (def
), Qmacro
))
883 else expander
= XCDR (def
);
887 expander
= XCDR (tem
);
891 form
= apply1 (expander
, XCDR (form
));
896 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
897 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
898 TAG is evalled to get the tag to use; it must not be nil.\n\
900 Then the BODY is executed.\n\
901 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
902 If no throw happens, `catch' returns the value of the last BODY form.\n\
903 If a throw happens, it specifies the value to return from `catch'.")
907 register Lisp_Object tag
;
911 tag
= Feval (Fcar (args
));
913 return internal_catch (tag
, Fprogn
, Fcdr (args
));
916 /* Set up a catch, then call C function FUNC on argument ARG.
917 FUNC should return a Lisp_Object.
918 This is how catches are done from within C code. */
921 internal_catch (tag
, func
, arg
)
923 Lisp_Object (*func
) ();
926 /* This structure is made part of the chain `catchlist'. */
929 /* Fill in the components of c, and put it on the list. */
933 c
.backlist
= backtrace_list
;
934 c
.handlerlist
= handlerlist
;
935 c
.lisp_eval_depth
= lisp_eval_depth
;
936 c
.pdlcount
= specpdl_ptr
- specpdl
;
937 c
.poll_suppress_count
= poll_suppress_count
;
939 c
.byte_stack
= byte_stack_list
;
943 if (! _setjmp (c
.jmp
))
944 c
.val
= (*func
) (arg
);
946 /* Throw works by a longjmp that comes right here. */
951 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
952 jump to that CATCH, returning VALUE as the value of that catch.
954 This is the guts Fthrow and Fsignal; they differ only in the way
955 they choose the catch tag to throw to. A catch tag for a
956 condition-case form has a TAG of Qnil.
958 Before each catch is discarded, unbind all special bindings and
959 execute all unwind-protect clauses made above that catch. Unwind
960 the handler stack as we go, so that the proper handlers are in
961 effect for each unwind-protect clause we run. At the end, restore
962 some static info saved in CATCH, and longjmp to the location
965 This is used for correct unwinding in Fthrow and Fsignal. */
968 unwind_to_catch (catch, value
)
969 struct catchtag
*catch;
972 register int last_time
;
974 /* Save the value in the tag. */
977 /* Restore the polling-suppression count. */
978 set_poll_suppress_count (catch->poll_suppress_count
);
982 last_time
= catchlist
== catch;
984 /* Unwind the specpdl stack, and then restore the proper set of
986 unbind_to (catchlist
->pdlcount
, Qnil
);
987 handlerlist
= catchlist
->handlerlist
;
988 catchlist
= catchlist
->next
;
992 byte_stack_list
= catch->byte_stack
;
993 gcprolist
= catch->gcpro
;
996 gcpro_level
= gcprolist
->level
+ 1;
1000 backtrace_list
= catch->backlist
;
1001 lisp_eval_depth
= catch->lisp_eval_depth
;
1003 _longjmp (catch->jmp
, 1);
1006 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1007 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
1008 Both TAG and VALUE are evalled.")
1010 register Lisp_Object tag
, value
;
1012 register struct catchtag
*c
;
1017 for (c
= catchlist
; c
; c
= c
->next
)
1019 if (EQ (c
->tag
, tag
))
1020 unwind_to_catch (c
, value
);
1022 tag
= Fsignal (Qno_catch
, Fcons (tag
, Fcons (value
, Qnil
)));
1027 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1028 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1029 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
1030 If BODYFORM completes normally, its value is returned\n\
1031 after executing the UNWINDFORMS.\n\
1032 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1037 int count
= specpdl_ptr
- specpdl
;
1039 record_unwind_protect (0, Fcdr (args
));
1040 val
= Feval (Fcar (args
));
1041 return unbind_to (count
, val
);
1044 /* Chain of condition handlers currently in effect.
1045 The elements of this chain are contained in the stack frames
1046 of Fcondition_case and internal_condition_case.
1047 When an error is signaled (by calling Fsignal, below),
1048 this chain is searched for an element that applies. */
1050 struct handler
*handlerlist
;
1052 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1053 "Regain control when an error is signaled.\n\
1054 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
1055 executes BODYFORM and returns its value if no error happens.\n\
1056 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1057 where the BODY is made of Lisp expressions.\n\n\
1058 A handler is applicable to an error\n\
1059 if CONDITION-NAME is one of the error's condition names.\n\
1060 If an error happens, the first applicable handler is run.\n\
1062 The car of a handler may be a list of condition names\n\
1063 instead of a single condition name.\n\
1065 When a handler handles an error,\n\
1066 control returns to the condition-case and the handler BODY... is executed\n\
1067 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1068 VAR may be nil; then you do not get access to the signal information.\n\
1070 The value of the last BODY form is returned from the condition-case.\n\
1071 See also the function `signal' for more info.")
1078 register Lisp_Object var
, bodyform
, handlers
;
1081 bodyform
= Fcar (Fcdr (args
));
1082 handlers
= Fcdr (Fcdr (args
));
1083 CHECK_SYMBOL (var
, 0);
1085 for (val
= handlers
; ! NILP (val
); val
= Fcdr (val
))
1091 && (SYMBOLP (XCAR (tem
))
1092 || CONSP (XCAR (tem
))))))
1093 error ("Invalid condition handler", tem
);
1098 c
.backlist
= backtrace_list
;
1099 c
.handlerlist
= handlerlist
;
1100 c
.lisp_eval_depth
= lisp_eval_depth
;
1101 c
.pdlcount
= specpdl_ptr
- specpdl
;
1102 c
.poll_suppress_count
= poll_suppress_count
;
1103 c
.gcpro
= gcprolist
;
1104 c
.byte_stack
= byte_stack_list
;
1105 if (_setjmp (c
.jmp
))
1108 specbind (h
.var
, c
.val
);
1109 val
= Fprogn (Fcdr (h
.chosen_clause
));
1111 /* Note that this just undoes the binding of h.var; whoever
1112 longjumped to us unwound the stack to c.pdlcount before
1114 unbind_to (c
.pdlcount
, Qnil
);
1121 h
.handler
= handlers
;
1122 h
.next
= handlerlist
;
1126 val
= Feval (bodyform
);
1128 handlerlist
= h
.next
;
1132 /* Call the function BFUN with no arguments, catching errors within it
1133 according to HANDLERS. If there is an error, call HFUN with
1134 one argument which is the data that describes the error:
1137 HANDLERS can be a list of conditions to catch.
1138 If HANDLERS is Qt, catch all errors.
1139 If HANDLERS is Qerror, catch all errors
1140 but allow the debugger to run if that is enabled. */
1143 internal_condition_case (bfun
, handlers
, hfun
)
1144 Lisp_Object (*bfun
) ();
1145 Lisp_Object handlers
;
1146 Lisp_Object (*hfun
) ();
1152 /* Since Fsignal resets this to 0, it had better be 0 now
1153 or else we have a potential bug. */
1154 if (interrupt_input_blocked
!= 0)
1159 c
.backlist
= backtrace_list
;
1160 c
.handlerlist
= handlerlist
;
1161 c
.lisp_eval_depth
= lisp_eval_depth
;
1162 c
.pdlcount
= specpdl_ptr
- specpdl
;
1163 c
.poll_suppress_count
= poll_suppress_count
;
1164 c
.gcpro
= gcprolist
;
1165 c
.byte_stack
= byte_stack_list
;
1166 if (_setjmp (c
.jmp
))
1168 return (*hfun
) (c
.val
);
1172 h
.handler
= handlers
;
1174 h
.next
= handlerlist
;
1180 handlerlist
= h
.next
;
1184 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1187 internal_condition_case_1 (bfun
, arg
, handlers
, hfun
)
1188 Lisp_Object (*bfun
) ();
1190 Lisp_Object handlers
;
1191 Lisp_Object (*hfun
) ();
1199 c
.backlist
= backtrace_list
;
1200 c
.handlerlist
= handlerlist
;
1201 c
.lisp_eval_depth
= lisp_eval_depth
;
1202 c
.pdlcount
= specpdl_ptr
- specpdl
;
1203 c
.poll_suppress_count
= poll_suppress_count
;
1204 c
.gcpro
= gcprolist
;
1205 c
.byte_stack
= byte_stack_list
;
1206 if (_setjmp (c
.jmp
))
1208 return (*hfun
) (c
.val
);
1212 h
.handler
= handlers
;
1214 h
.next
= handlerlist
;
1218 val
= (*bfun
) (arg
);
1220 handlerlist
= h
.next
;
1224 static Lisp_Object
find_handler_clause ();
1226 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1227 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1228 This function does not return.\n\n\
1229 An error symbol is a symbol with an `error-conditions' property\n\
1230 that is a list of condition names.\n\
1231 A handler for any of those names will get to handle this signal.\n\
1232 The symbol `error' should normally be one of them.\n\
1234 DATA should be a list. Its elements are printed as part of the error message.\n\
1235 If the signal is handled, DATA is made available to the handler.\n\
1236 See also the function `condition-case'.")
1237 (error_symbol
, data
)
1238 Lisp_Object error_symbol
, data
;
1240 /* When memory is full, ERROR-SYMBOL is nil,
1241 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1242 register struct handler
*allhandlers
= handlerlist
;
1243 Lisp_Object conditions
;
1244 extern int gc_in_progress
;
1245 extern int waiting_for_input
;
1246 Lisp_Object debugger_value
;
1248 Lisp_Object real_error_symbol
;
1249 extern int display_busy_cursor_p
;
1252 if (gc_in_progress
|| waiting_for_input
)
1255 TOTALLY_UNBLOCK_INPUT
;
1257 if (NILP (error_symbol
))
1258 real_error_symbol
= Fcar (data
);
1260 real_error_symbol
= error_symbol
;
1262 #ifdef HAVE_X_WINDOWS
1263 if (display_busy_cursor_p
)
1264 Fx_hide_busy_cursor (Qt
);
1267 /* This hook is used by edebug. */
1268 if (! NILP (Vsignal_hook_function
))
1269 call2 (Vsignal_hook_function
, error_symbol
, data
);
1271 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1273 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1275 register Lisp_Object clause
;
1276 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1277 error_symbol
, data
, &debugger_value
);
1279 #if 0 /* Most callers are not prepared to handle gc if this returns.
1280 So, since this feature is not very useful, take it out. */
1281 /* If have called debugger and user wants to continue,
1283 if (EQ (clause
, Qlambda
))
1284 return debugger_value
;
1286 if (EQ (clause
, Qlambda
))
1288 /* We can't return values to code which signaled an error, but we
1289 can continue code which has signaled a quit. */
1290 if (EQ (real_error_symbol
, Qquit
))
1293 error ("Cannot return from the debugger in an error");
1299 Lisp_Object unwind_data
;
1300 struct handler
*h
= handlerlist
;
1302 handlerlist
= allhandlers
;
1304 if (NILP (error_symbol
))
1307 unwind_data
= Fcons (error_symbol
, data
);
1308 h
->chosen_clause
= clause
;
1309 unwind_to_catch (h
->tag
, unwind_data
);
1313 handlerlist
= allhandlers
;
1314 /* If no handler is present now, try to run the debugger,
1315 and if that fails, throw to top level. */
1316 find_handler_clause (Qerror
, conditions
, error_symbol
, data
, &debugger_value
);
1318 Fthrow (Qtop_level
, Qt
);
1320 if (! NILP (error_symbol
))
1321 data
= Fcons (error_symbol
, data
);
1323 string
= Ferror_message_string (data
);
1324 fatal ("%s", XSTRING (string
)->data
, 0);
1327 /* Return nonzero iff LIST is a non-nil atom or
1328 a list containing one of CONDITIONS. */
1331 wants_debugger (list
, conditions
)
1332 Lisp_Object list
, conditions
;
1339 while (CONSP (conditions
))
1341 Lisp_Object
this, tail
;
1342 this = XCAR (conditions
);
1343 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1344 if (EQ (XCAR (tail
), this))
1346 conditions
= XCDR (conditions
);
1351 /* Return 1 if an error with condition-symbols CONDITIONS,
1352 and described by SIGNAL-DATA, should skip the debugger
1353 according to debugger-ignore-errors. */
1356 skip_debugger (conditions
, data
)
1357 Lisp_Object conditions
, data
;
1360 int first_string
= 1;
1361 Lisp_Object error_message
;
1363 for (tail
= Vdebug_ignored_errors
; CONSP (tail
);
1366 if (STRINGP (XCAR (tail
)))
1370 error_message
= Ferror_message_string (data
);
1373 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1378 Lisp_Object contail
;
1380 for (contail
= conditions
; CONSP (contail
);
1381 contail
= XCDR (contail
))
1382 if (EQ (XCAR (tail
), XCAR (contail
)))
1390 /* Value of Qlambda means we have called debugger and user has continued.
1391 There are two ways to pass SIG and DATA:
1392 = SIG is the error symbol, and DATA is the rest of the data.
1393 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1394 This is for memory-full errors only.
1396 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1399 find_handler_clause (handlers
, conditions
, sig
, data
, debugger_value_ptr
)
1400 Lisp_Object handlers
, conditions
, sig
, data
;
1401 Lisp_Object
*debugger_value_ptr
;
1403 register Lisp_Object h
;
1404 register Lisp_Object tem
;
1406 if (EQ (handlers
, Qt
)) /* t is used by handlers for all conditions, set up by C code. */
1408 /* error is used similarly, but means print an error message
1409 and run the debugger if that is enabled. */
1410 if (EQ (handlers
, Qerror
)
1411 || !NILP (Vdebug_on_signal
)) /* This says call debugger even if
1412 there is a handler. */
1414 int count
= specpdl_ptr
- specpdl
;
1415 int debugger_called
= 0;
1416 Lisp_Object sig_symbol
, combined_data
;
1417 /* This is set to 1 if we are handling a memory-full error,
1418 because these must not run the debugger.
1419 (There is no room in memory to do that!) */
1420 int no_debugger
= 0;
1424 combined_data
= data
;
1425 sig_symbol
= Fcar (data
);
1430 combined_data
= Fcons (sig
, data
);
1434 if (wants_debugger (Vstack_trace_on_error
, conditions
))
1437 internal_with_output_to_temp_buffer ("*Backtrace*",
1438 (Lisp_Object (*) (Lisp_Object
)) Fbacktrace
,
1441 internal_with_output_to_temp_buffer ("*Backtrace*",
1446 && (EQ (sig_symbol
, Qquit
)
1448 : wants_debugger (Vdebug_on_error
, conditions
))
1449 && ! skip_debugger (conditions
, combined_data
)
1450 && when_entered_debugger
< num_nonmacro_input_events
)
1452 specbind (Qdebug_on_error
, Qnil
);
1454 = call_debugger (Fcons (Qerror
,
1455 Fcons (combined_data
, Qnil
)));
1456 debugger_called
= 1;
1458 /* If there is no handler, return saying whether we ran the debugger. */
1459 if (EQ (handlers
, Qerror
))
1461 if (debugger_called
)
1462 return unbind_to (count
, Qlambda
);
1466 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1468 Lisp_Object handler
, condit
;
1471 if (!CONSP (handler
))
1473 condit
= Fcar (handler
);
1474 /* Handle a single condition name in handler HANDLER. */
1475 if (SYMBOLP (condit
))
1477 tem
= Fmemq (Fcar (handler
), conditions
);
1481 /* Handle a list of condition names in handler HANDLER. */
1482 else if (CONSP (condit
))
1484 while (CONSP (condit
))
1486 tem
= Fmemq (Fcar (condit
), conditions
);
1489 condit
= XCDR (condit
);
1496 /* dump an error message; called like printf */
1500 error (m
, a1
, a2
, a3
)
1520 int used
= doprnt (buffer
, size
, m
, m
+ mlen
, 3, args
);
1525 buffer
= (char *) xrealloc (buffer
, size
);
1528 buffer
= (char *) xmalloc (size
);
1533 string
= build_string (buffer
);
1537 Fsignal (Qerror
, Fcons (string
, Qnil
));
1540 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 1, 0,
1541 "T if FUNCTION makes provisions for interactive calling.\n\
1542 This means it contains a description for how to read arguments to give it.\n\
1543 The value is nil for an invalid function or a symbol with no function\n\
1546 Interactively callable functions include strings and vectors (treated\n\
1547 as keyboard macros), lambda-expressions that contain a top-level call\n\
1548 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1549 fourth argument, and some of the built-in functions of Lisp.\n\
1551 Also, a symbol satisfies `commandp' if its function definition does so.")
1553 Lisp_Object function
;
1555 register Lisp_Object fun
;
1556 register Lisp_Object funcar
;
1560 fun
= indirect_function (fun
);
1561 if (EQ (fun
, Qunbound
))
1564 /* Emacs primitives are interactive if their DEFUN specifies an
1565 interactive spec. */
1568 if (XSUBR (fun
)->prompt
)
1574 /* Bytecode objects are interactive if they are long enough to
1575 have an element whose index is COMPILED_INTERACTIVE, which is
1576 where the interactive spec is stored. */
1577 else if (COMPILEDP (fun
))
1578 return ((XVECTOR (fun
)->size
& PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1581 /* Strings and vectors are keyboard macros. */
1582 if (STRINGP (fun
) || VECTORP (fun
))
1585 /* Lists may represent commands. */
1588 funcar
= Fcar (fun
);
1589 if (!SYMBOLP (funcar
))
1590 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1591 if (EQ (funcar
, Qlambda
))
1592 return Fassq (Qinteractive
, Fcdr (Fcdr (fun
)));
1593 if (EQ (funcar
, Qmocklisp
))
1594 return Qt
; /* All mocklisp functions can be called interactively */
1595 if (EQ (funcar
, Qautoload
))
1596 return Fcar (Fcdr (Fcdr (Fcdr (fun
))));
1602 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1603 "Define FUNCTION to autoload from FILE.\n\
1604 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1605 Third arg DOCSTRING is documentation for the function.\n\
1606 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1607 Fifth arg TYPE indicates the type of the object:\n\
1608 nil or omitted says FUNCTION is a function,\n\
1609 `keymap' says FUNCTION is really a keymap, and\n\
1610 `macro' or t says FUNCTION is really a macro.\n\
1611 Third through fifth args give info about the real definition.\n\
1612 They default to nil.\n\
1613 If FUNCTION is already defined other than as an autoload,\n\
1614 this does nothing and returns nil.")
1615 (function
, file
, docstring
, interactive
, type
)
1616 Lisp_Object function
, file
, docstring
, interactive
, type
;
1619 Lisp_Object args
[4];
1622 CHECK_SYMBOL (function
, 0);
1623 CHECK_STRING (file
, 1);
1625 /* If function is defined and not as an autoload, don't override */
1626 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1627 && !(CONSP (XSYMBOL (function
)->function
)
1628 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1633 args
[1] = docstring
;
1634 args
[2] = interactive
;
1637 return Ffset (function
, Fcons (Qautoload
, Flist (4, &args
[0])));
1638 #else /* NO_ARG_ARRAY */
1639 return Ffset (function
, Fcons (Qautoload
, Flist (4, &file
)));
1640 #endif /* not NO_ARG_ARRAY */
1644 un_autoload (oldqueue
)
1645 Lisp_Object oldqueue
;
1647 register Lisp_Object queue
, first
, second
;
1649 /* Queue to unwind is current value of Vautoload_queue.
1650 oldqueue is the shadowed value to leave in Vautoload_queue. */
1651 queue
= Vautoload_queue
;
1652 Vautoload_queue
= oldqueue
;
1653 while (CONSP (queue
))
1655 first
= Fcar (queue
);
1656 second
= Fcdr (first
);
1657 first
= Fcar (first
);
1658 if (EQ (second
, Qnil
))
1661 Ffset (first
, second
);
1662 queue
= Fcdr (queue
);
1667 /* Load an autoloaded function.
1668 FUNNAME is the symbol which is the function's name.
1669 FUNDEF is the autoload definition (a list). */
1672 do_autoload (fundef
, funname
)
1673 Lisp_Object fundef
, funname
;
1675 int count
= specpdl_ptr
- specpdl
;
1676 Lisp_Object fun
, queue
, first
, second
;
1677 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1680 CHECK_SYMBOL (funname
, 0);
1681 GCPRO3 (fun
, funname
, fundef
);
1683 /* Preserve the match data. */
1684 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
1686 /* Value saved here is to be restored into Vautoload_queue. */
1687 record_unwind_protect (un_autoload
, Vautoload_queue
);
1688 Vautoload_queue
= Qt
;
1689 Fload (Fcar (Fcdr (fundef
)), Qnil
, noninteractive
? Qt
: Qnil
, Qnil
, Qt
);
1691 /* Save the old autoloads, in case we ever do an unload. */
1692 queue
= Vautoload_queue
;
1693 while (CONSP (queue
))
1695 first
= Fcar (queue
);
1696 second
= Fcdr (first
);
1697 first
= Fcar (first
);
1699 /* Note: This test is subtle. The cdr of an autoload-queue entry
1700 may be an atom if the autoload entry was generated by a defalias
1703 Fput (first
, Qautoload
, (Fcdr (second
)));
1705 queue
= Fcdr (queue
);
1708 /* Once loading finishes, don't undo it. */
1709 Vautoload_queue
= Qt
;
1710 unbind_to (count
, Qnil
);
1712 fun
= Findirect_function (fun
);
1714 if (!NILP (Fequal (fun
, fundef
)))
1715 error ("Autoloading failed to define function %s",
1716 XSYMBOL (funname
)->name
->data
);
1720 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
1721 "Evaluate FORM and return its value.")
1725 Lisp_Object fun
, val
, original_fun
, original_args
;
1727 struct backtrace backtrace
;
1728 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1730 /* Since Fsignal resets this to 0, it had better be 0 now
1731 or else we have a potential bug. */
1732 if (interrupt_input_blocked
!= 0)
1737 if (EQ (Vmocklisp_arguments
, Qt
))
1738 return Fsymbol_value (form
);
1739 val
= Fsymbol_value (form
);
1741 XSETFASTINT (val
, 0);
1742 else if (EQ (val
, Qt
))
1743 XSETFASTINT (val
, 1);
1750 if (consing_since_gc
> gc_cons_threshold
)
1753 Fgarbage_collect ();
1757 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1759 if (max_lisp_eval_depth
< 100)
1760 max_lisp_eval_depth
= 100;
1761 if (lisp_eval_depth
> max_lisp_eval_depth
)
1762 error ("Lisp nesting exceeds max-lisp-eval-depth");
1765 original_fun
= Fcar (form
);
1766 original_args
= Fcdr (form
);
1768 backtrace
.next
= backtrace_list
;
1769 backtrace_list
= &backtrace
;
1770 backtrace
.function
= &original_fun
; /* This also protects them from gc */
1771 backtrace
.args
= &original_args
;
1772 backtrace
.nargs
= UNEVALLED
;
1773 backtrace
.evalargs
= 1;
1774 backtrace
.debug_on_exit
= 0;
1776 if (debug_on_next_call
)
1777 do_debug_on_call (Qt
);
1779 /* At this point, only original_fun and original_args
1780 have values that will be used below */
1782 fun
= Findirect_function (original_fun
);
1786 Lisp_Object numargs
;
1787 Lisp_Object argvals
[8];
1788 Lisp_Object args_left
;
1789 register int i
, maxargs
;
1791 args_left
= original_args
;
1792 numargs
= Flength (args_left
);
1794 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
1795 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
1796 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1798 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1800 backtrace
.evalargs
= 0;
1801 val
= (*XSUBR (fun
)->function
) (args_left
);
1805 if (XSUBR (fun
)->max_args
== MANY
)
1807 /* Pass a vector of evaluated arguments */
1809 register int argnum
= 0;
1811 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1813 GCPRO3 (args_left
, fun
, fun
);
1817 while (!NILP (args_left
))
1819 vals
[argnum
++] = Feval (Fcar (args_left
));
1820 args_left
= Fcdr (args_left
);
1821 gcpro3
.nvars
= argnum
;
1824 backtrace
.args
= vals
;
1825 backtrace
.nargs
= XINT (numargs
);
1827 val
= (*XSUBR (fun
)->function
) (XINT (numargs
), vals
);
1832 GCPRO3 (args_left
, fun
, fun
);
1833 gcpro3
.var
= argvals
;
1836 maxargs
= XSUBR (fun
)->max_args
;
1837 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
1839 argvals
[i
] = Feval (Fcar (args_left
));
1845 backtrace
.args
= argvals
;
1846 backtrace
.nargs
= XINT (numargs
);
1851 val
= (*XSUBR (fun
)->function
) ();
1854 val
= (*XSUBR (fun
)->function
) (argvals
[0]);
1857 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1]);
1860 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1864 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1865 argvals
[2], argvals
[3]);
1868 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1869 argvals
[3], argvals
[4]);
1872 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1873 argvals
[3], argvals
[4], argvals
[5]);
1876 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1877 argvals
[3], argvals
[4], argvals
[5],
1882 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1883 argvals
[3], argvals
[4], argvals
[5],
1884 argvals
[6], argvals
[7]);
1888 /* Someone has created a subr that takes more arguments than
1889 is supported by this code. We need to either rewrite the
1890 subr to use a different argument protocol, or add more
1891 cases to this switch. */
1895 if (COMPILEDP (fun
))
1896 val
= apply_lambda (fun
, original_args
, 1);
1900 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1901 funcar
= Fcar (fun
);
1902 if (!SYMBOLP (funcar
))
1903 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1904 if (EQ (funcar
, Qautoload
))
1906 do_autoload (fun
, original_fun
);
1909 if (EQ (funcar
, Qmacro
))
1910 val
= Feval (apply1 (Fcdr (fun
), original_args
));
1911 else if (EQ (funcar
, Qlambda
))
1912 val
= apply_lambda (fun
, original_args
, 1);
1913 else if (EQ (funcar
, Qmocklisp
))
1914 val
= ml_apply (fun
, original_args
);
1916 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1919 if (!EQ (Vmocklisp_arguments
, Qt
))
1922 XSETFASTINT (val
, 0);
1923 else if (EQ (val
, Qt
))
1924 XSETFASTINT (val
, 1);
1927 if (backtrace
.debug_on_exit
)
1928 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1929 backtrace_list
= backtrace
.next
;
1933 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
1934 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1935 Then return the value FUNCTION returns.\n\
1936 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1941 register int i
, numargs
;
1942 register Lisp_Object spread_arg
;
1943 register Lisp_Object
*funcall_args
;
1945 struct gcpro gcpro1
;
1949 spread_arg
= args
[nargs
- 1];
1950 CHECK_LIST (spread_arg
, nargs
);
1952 numargs
= XINT (Flength (spread_arg
));
1955 return Ffuncall (nargs
- 1, args
);
1956 else if (numargs
== 1)
1958 args
[nargs
- 1] = XCAR (spread_arg
);
1959 return Ffuncall (nargs
, args
);
1962 numargs
+= nargs
- 2;
1964 fun
= indirect_function (fun
);
1965 if (EQ (fun
, Qunbound
))
1967 /* Let funcall get the error */
1974 if (numargs
< XSUBR (fun
)->min_args
1975 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1976 goto funcall
; /* Let funcall get the error */
1977 else if (XSUBR (fun
)->max_args
> numargs
)
1979 /* Avoid making funcall cons up a yet another new vector of arguments
1980 by explicitly supplying nil's for optional values */
1981 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
1982 * sizeof (Lisp_Object
));
1983 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
1984 funcall_args
[++i
] = Qnil
;
1985 GCPRO1 (*funcall_args
);
1986 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
1990 /* We add 1 to numargs because funcall_args includes the
1991 function itself as well as its arguments. */
1994 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
1995 * sizeof (Lisp_Object
));
1996 GCPRO1 (*funcall_args
);
1997 gcpro1
.nvars
= 1 + numargs
;
2000 bcopy (args
, funcall_args
, nargs
* sizeof (Lisp_Object
));
2001 /* Spread the last arg we got. Its first element goes in
2002 the slot that it used to occupy, hence this value of I. */
2004 while (!NILP (spread_arg
))
2006 funcall_args
[i
++] = XCAR (spread_arg
);
2007 spread_arg
= XCDR (spread_arg
);
2010 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
2013 /* Run hook variables in various ways. */
2015 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
2017 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 1, MANY
, 0,
2018 "Run each hook in HOOKS. Major mode functions use this.\n\
2019 Each argument should be a symbol, a hook variable.\n\
2020 These symbols are processed in the order specified.\n\
2021 If a hook symbol has a non-nil value, that value may be a function\n\
2022 or a list of functions to be called to run the hook.\n\
2023 If the value is a function, it is called with no arguments.\n\
2024 If it is a list, the elements are called, in order, with no arguments.\n\
2026 To make a hook variable buffer-local, use `make-local-hook',\n\
2027 not `make-local-variable'.")
2032 Lisp_Object hook
[1];
2035 for (i
= 0; i
< nargs
; i
++)
2038 run_hook_with_args (1, hook
, to_completion
);
2044 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2045 Srun_hook_with_args
, 1, MANY
, 0,
2046 "Run HOOK with the specified arguments ARGS.\n\
2047 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2048 value, that value may be a function or a list of functions to be\n\
2049 called to run the hook. If the value is a function, it is called with\n\
2050 the given arguments and its return value is returned. If it is a list\n\
2051 of functions, those functions are called, in order,\n\
2052 with the given arguments ARGS.\n\
2053 It is best not to depend on the value return by `run-hook-with-args',\n\
2054 as that may change.\n\
2056 To make a hook variable buffer-local, use `make-local-hook',\n\
2057 not `make-local-variable'.")
2062 return run_hook_with_args (nargs
, args
, to_completion
);
2065 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2066 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2067 "Run HOOK with the specified arguments ARGS.\n\
2068 HOOK should be a symbol, a hook variable. Its value should\n\
2069 be a list of functions. We call those functions, one by one,\n\
2070 passing arguments ARGS to each of them, until one of them\n\
2071 returns a non-nil value. Then we return that value.\n\
2072 If all the functions return nil, we return nil.\n\
2074 To make a hook variable buffer-local, use `make-local-hook',\n\
2075 not `make-local-variable'.")
2080 return run_hook_with_args (nargs
, args
, until_success
);
2083 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2084 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2085 "Run HOOK with the specified arguments ARGS.\n\
2086 HOOK should be a symbol, a hook variable. Its value should\n\
2087 be a list of functions. We call those functions, one by one,\n\
2088 passing arguments ARGS to each of them, until one of them\n\
2089 returns nil. Then we return nil.\n\
2090 If all the functions return non-nil, we return non-nil.\n\
2092 To make a hook variable buffer-local, use `make-local-hook',\n\
2093 not `make-local-variable'.")
2098 return run_hook_with_args (nargs
, args
, until_failure
);
2101 /* ARGS[0] should be a hook symbol.
2102 Call each of the functions in the hook value, passing each of them
2103 as arguments all the rest of ARGS (all NARGS - 1 elements).
2104 COND specifies a condition to test after each call
2105 to decide whether to stop.
2106 The caller (or its caller, etc) must gcpro all of ARGS,
2107 except that it isn't necessary to gcpro ARGS[0]. */
2110 run_hook_with_args (nargs
, args
, cond
)
2113 enum run_hooks_condition cond
;
2115 Lisp_Object sym
, val
, ret
;
2116 Lisp_Object globals
;
2117 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2119 /* If we are dying or still initializing,
2120 don't do anything--it would probably crash if we tried. */
2121 if (NILP (Vrun_hooks
))
2125 val
= find_symbol_value (sym
);
2126 ret
= (cond
== until_failure
? Qt
: Qnil
);
2128 if (EQ (val
, Qunbound
) || NILP (val
))
2130 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2133 return Ffuncall (nargs
, args
);
2138 GCPRO3 (sym
, val
, globals
);
2141 CONSP (val
) && ((cond
== to_completion
)
2142 || (cond
== until_success
? NILP (ret
)
2146 if (EQ (XCAR (val
), Qt
))
2148 /* t indicates this hook has a local binding;
2149 it means to run the global binding too. */
2151 for (globals
= Fdefault_value (sym
);
2152 CONSP (globals
) && ((cond
== to_completion
)
2153 || (cond
== until_success
? NILP (ret
)
2155 globals
= XCDR (globals
))
2157 args
[0] = XCAR (globals
);
2158 /* In a global value, t should not occur. If it does, we
2159 must ignore it to avoid an endless loop. */
2160 if (!EQ (args
[0], Qt
))
2161 ret
= Ffuncall (nargs
, args
);
2166 args
[0] = XCAR (val
);
2167 ret
= Ffuncall (nargs
, args
);
2176 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2177 present value of that symbol.
2178 Call each element of FUNLIST,
2179 passing each of them the rest of ARGS.
2180 The caller (or its caller, etc) must gcpro all of ARGS,
2181 except that it isn't necessary to gcpro ARGS[0]. */
2184 run_hook_list_with_args (funlist
, nargs
, args
)
2185 Lisp_Object funlist
;
2191 Lisp_Object globals
;
2192 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2196 GCPRO3 (sym
, val
, globals
);
2198 for (val
= funlist
; CONSP (val
); val
= XCDR (val
))
2200 if (EQ (XCAR (val
), Qt
))
2202 /* t indicates this hook has a local binding;
2203 it means to run the global binding too. */
2205 for (globals
= Fdefault_value (sym
);
2207 globals
= XCDR (globals
))
2209 args
[0] = XCAR (globals
);
2210 /* In a global value, t should not occur. If it does, we
2211 must ignore it to avoid an endless loop. */
2212 if (!EQ (args
[0], Qt
))
2213 Ffuncall (nargs
, args
);
2218 args
[0] = XCAR (val
);
2219 Ffuncall (nargs
, args
);
2226 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2229 run_hook_with_args_2 (hook
, arg1
, arg2
)
2230 Lisp_Object hook
, arg1
, arg2
;
2232 Lisp_Object temp
[3];
2237 Frun_hook_with_args (3, temp
);
2240 /* Apply fn to arg */
2243 Lisp_Object fn
, arg
;
2245 struct gcpro gcpro1
;
2249 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2253 Lisp_Object args
[2];
2257 RETURN_UNGCPRO (Fapply (2, args
));
2259 #else /* not NO_ARG_ARRAY */
2260 RETURN_UNGCPRO (Fapply (2, &fn
));
2261 #endif /* not NO_ARG_ARRAY */
2264 /* Call function fn on no arguments */
2269 struct gcpro gcpro1
;
2272 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2275 /* Call function fn with 1 argument arg1 */
2279 Lisp_Object fn
, arg1
;
2281 struct gcpro gcpro1
;
2283 Lisp_Object args
[2];
2289 RETURN_UNGCPRO (Ffuncall (2, args
));
2290 #else /* not NO_ARG_ARRAY */
2293 RETURN_UNGCPRO (Ffuncall (2, &fn
));
2294 #endif /* not NO_ARG_ARRAY */
2297 /* Call function fn with 2 arguments arg1, arg2 */
2300 call2 (fn
, arg1
, arg2
)
2301 Lisp_Object fn
, arg1
, arg2
;
2303 struct gcpro gcpro1
;
2305 Lisp_Object args
[3];
2311 RETURN_UNGCPRO (Ffuncall (3, args
));
2312 #else /* not NO_ARG_ARRAY */
2315 RETURN_UNGCPRO (Ffuncall (3, &fn
));
2316 #endif /* not NO_ARG_ARRAY */
2319 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2322 call3 (fn
, arg1
, arg2
, arg3
)
2323 Lisp_Object fn
, arg1
, arg2
, arg3
;
2325 struct gcpro gcpro1
;
2327 Lisp_Object args
[4];
2334 RETURN_UNGCPRO (Ffuncall (4, args
));
2335 #else /* not NO_ARG_ARRAY */
2338 RETURN_UNGCPRO (Ffuncall (4, &fn
));
2339 #endif /* not NO_ARG_ARRAY */
2342 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2345 call4 (fn
, arg1
, arg2
, arg3
, arg4
)
2346 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
;
2348 struct gcpro gcpro1
;
2350 Lisp_Object args
[5];
2358 RETURN_UNGCPRO (Ffuncall (5, args
));
2359 #else /* not NO_ARG_ARRAY */
2362 RETURN_UNGCPRO (Ffuncall (5, &fn
));
2363 #endif /* not NO_ARG_ARRAY */
2366 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2369 call5 (fn
, arg1
, arg2
, arg3
, arg4
, arg5
)
2370 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
, arg5
;
2372 struct gcpro gcpro1
;
2374 Lisp_Object args
[6];
2383 RETURN_UNGCPRO (Ffuncall (6, args
));
2384 #else /* not NO_ARG_ARRAY */
2387 RETURN_UNGCPRO (Ffuncall (6, &fn
));
2388 #endif /* not NO_ARG_ARRAY */
2391 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2394 call6 (fn
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
2395 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
2397 struct gcpro gcpro1
;
2399 Lisp_Object args
[7];
2409 RETURN_UNGCPRO (Ffuncall (7, args
));
2410 #else /* not NO_ARG_ARRAY */
2413 RETURN_UNGCPRO (Ffuncall (7, &fn
));
2414 #endif /* not NO_ARG_ARRAY */
2417 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2418 "Call first argument as a function, passing remaining arguments to it.\n\
2419 Return the value that function returns.\n\
2420 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2427 int numargs
= nargs
- 1;
2428 Lisp_Object lisp_numargs
;
2430 struct backtrace backtrace
;
2431 register Lisp_Object
*internal_args
;
2435 if (consing_since_gc
> gc_cons_threshold
)
2436 Fgarbage_collect ();
2438 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2440 if (max_lisp_eval_depth
< 100)
2441 max_lisp_eval_depth
= 100;
2442 if (lisp_eval_depth
> max_lisp_eval_depth
)
2443 error ("Lisp nesting exceeds max-lisp-eval-depth");
2446 backtrace
.next
= backtrace_list
;
2447 backtrace_list
= &backtrace
;
2448 backtrace
.function
= &args
[0];
2449 backtrace
.args
= &args
[1];
2450 backtrace
.nargs
= nargs
- 1;
2451 backtrace
.evalargs
= 0;
2452 backtrace
.debug_on_exit
= 0;
2454 if (debug_on_next_call
)
2455 do_debug_on_call (Qlambda
);
2461 fun
= Findirect_function (fun
);
2465 if (numargs
< XSUBR (fun
)->min_args
2466 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2468 XSETFASTINT (lisp_numargs
, numargs
);
2469 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (lisp_numargs
, Qnil
)));
2472 if (XSUBR (fun
)->max_args
== UNEVALLED
)
2473 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2475 if (XSUBR (fun
)->max_args
== MANY
)
2477 val
= (*XSUBR (fun
)->function
) (numargs
, args
+ 1);
2481 if (XSUBR (fun
)->max_args
> numargs
)
2483 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2484 bcopy (args
+ 1, internal_args
, numargs
* sizeof (Lisp_Object
));
2485 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2486 internal_args
[i
] = Qnil
;
2489 internal_args
= args
+ 1;
2490 switch (XSUBR (fun
)->max_args
)
2493 val
= (*XSUBR (fun
)->function
) ();
2496 val
= (*XSUBR (fun
)->function
) (internal_args
[0]);
2499 val
= (*XSUBR (fun
)->function
) (internal_args
[0],
2503 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2507 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2512 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2513 internal_args
[2], internal_args
[3],
2517 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2518 internal_args
[2], internal_args
[3],
2519 internal_args
[4], internal_args
[5]);
2522 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2523 internal_args
[2], internal_args
[3],
2524 internal_args
[4], internal_args
[5],
2529 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2530 internal_args
[2], internal_args
[3],
2531 internal_args
[4], internal_args
[5],
2532 internal_args
[6], internal_args
[7]);
2537 /* If a subr takes more than 8 arguments without using MANY
2538 or UNEVALLED, we need to extend this function to support it.
2539 Until this is done, there is no way to call the function. */
2543 if (COMPILEDP (fun
))
2544 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2548 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2549 funcar
= Fcar (fun
);
2550 if (!SYMBOLP (funcar
))
2551 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2552 if (EQ (funcar
, Qlambda
))
2553 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2554 else if (EQ (funcar
, Qmocklisp
))
2555 val
= ml_apply (fun
, Flist (numargs
, args
+ 1));
2556 else if (EQ (funcar
, Qautoload
))
2558 do_autoload (fun
, args
[0]);
2562 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2566 if (backtrace
.debug_on_exit
)
2567 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2568 backtrace_list
= backtrace
.next
;
2573 apply_lambda (fun
, args
, eval_flag
)
2574 Lisp_Object fun
, args
;
2577 Lisp_Object args_left
;
2578 Lisp_Object numargs
;
2579 register Lisp_Object
*arg_vector
;
2580 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2582 register Lisp_Object tem
;
2584 numargs
= Flength (args
);
2585 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
2588 GCPRO3 (*arg_vector
, args_left
, fun
);
2591 for (i
= 0; i
< XINT (numargs
);)
2593 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2594 if (eval_flag
) tem
= Feval (tem
);
2595 arg_vector
[i
++] = tem
;
2603 backtrace_list
->args
= arg_vector
;
2604 backtrace_list
->nargs
= i
;
2606 backtrace_list
->evalargs
= 0;
2607 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
2609 /* Do the debug-on-exit now, while arg_vector still exists. */
2610 if (backtrace_list
->debug_on_exit
)
2611 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2612 /* Don't do it again when we return to eval. */
2613 backtrace_list
->debug_on_exit
= 0;
2617 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2618 and return the result of evaluation.
2619 FUN must be either a lambda-expression or a compiled-code object. */
2622 funcall_lambda (fun
, nargs
, arg_vector
)
2625 register Lisp_Object
*arg_vector
;
2627 Lisp_Object val
, tem
;
2628 register Lisp_Object syms_left
;
2629 Lisp_Object numargs
;
2630 register Lisp_Object next
;
2631 int count
= specpdl_ptr
- specpdl
;
2633 int optional
= 0, rest
= 0;
2635 specbind (Qmocklisp_arguments
, Qt
); /* t means NOT mocklisp! */
2637 XSETFASTINT (numargs
, nargs
);
2640 syms_left
= Fcar (Fcdr (fun
));
2641 else if (COMPILEDP (fun
))
2642 syms_left
= XVECTOR (fun
)->contents
[COMPILED_ARGLIST
];
2646 for (; !NILP (syms_left
); syms_left
= Fcdr (syms_left
))
2649 next
= Fcar (syms_left
);
2650 while (!SYMBOLP (next
))
2651 next
= Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2652 if (EQ (next
, Qand_rest
))
2654 else if (EQ (next
, Qand_optional
))
2658 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
2663 tem
= arg_vector
[i
++];
2664 specbind (next
, tem
);
2667 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2669 specbind (next
, Qnil
);
2673 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2676 val
= Fprogn (Fcdr (Fcdr (fun
)));
2679 /* If we have not actually read the bytecode string
2680 and constants vector yet, fetch them from the file. */
2681 if (CONSP (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
]))
2682 Ffetch_bytecode (fun
);
2683 val
= Fbyte_code (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
],
2684 XVECTOR (fun
)->contents
[COMPILED_CONSTANTS
],
2685 XVECTOR (fun
)->contents
[COMPILED_STACK_DEPTH
]);
2687 return unbind_to (count
, val
);
2690 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2692 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2698 if (COMPILEDP (object
)
2699 && CONSP (XVECTOR (object
)->contents
[COMPILED_BYTECODE
]))
2701 tem
= read_doc_string (XVECTOR (object
)->contents
[COMPILED_BYTECODE
]);
2703 error ("invalid byte code");
2704 XVECTOR (object
)->contents
[COMPILED_BYTECODE
] = XCAR (tem
);
2705 XVECTOR (object
)->contents
[COMPILED_CONSTANTS
] = XCDR (tem
);
2713 register int count
= specpdl_ptr
- specpdl
;
2714 if (specpdl_size
>= max_specpdl_size
)
2716 if (max_specpdl_size
< 400)
2717 max_specpdl_size
= 400;
2718 if (specpdl_size
>= max_specpdl_size
)
2720 if (!NILP (Vdebug_on_error
))
2721 /* Leave room for some specpdl in the debugger. */
2722 max_specpdl_size
= specpdl_size
+ 100;
2724 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil
));
2728 if (specpdl_size
> max_specpdl_size
)
2729 specpdl_size
= max_specpdl_size
;
2730 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
2731 specpdl_ptr
= specpdl
+ count
;
2735 specbind (symbol
, value
)
2736 Lisp_Object symbol
, value
;
2740 CHECK_SYMBOL (symbol
, 0);
2742 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2744 specpdl_ptr
->symbol
= symbol
;
2745 specpdl_ptr
->func
= 0;
2746 specpdl_ptr
->old_value
= ovalue
= find_symbol_value (symbol
);
2748 if (BUFFER_OBJFWDP (ovalue
) || KBOARD_OBJFWDP (ovalue
))
2749 store_symval_forwarding (symbol
, ovalue
, value
);
2751 set_internal (symbol
, value
, 1);
2755 record_unwind_protect (function
, arg
)
2756 Lisp_Object (*function
) P_ ((Lisp_Object
));
2759 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2761 specpdl_ptr
->func
= function
;
2762 specpdl_ptr
->symbol
= Qnil
;
2763 specpdl_ptr
->old_value
= arg
;
2768 unbind_to (count
, value
)
2772 int quitf
= !NILP (Vquit_flag
);
2773 struct gcpro gcpro1
;
2779 while (specpdl_ptr
!= specpdl
+ count
)
2782 if (specpdl_ptr
->func
!= 0)
2783 (*specpdl_ptr
->func
) (specpdl_ptr
->old_value
);
2784 /* Note that a "binding" of nil is really an unwind protect,
2785 so in that case the "old value" is a list of forms to evaluate. */
2786 else if (NILP (specpdl_ptr
->symbol
))
2787 Fprogn (specpdl_ptr
->old_value
);
2789 set_internal (specpdl_ptr
->symbol
, specpdl_ptr
->old_value
, 1);
2791 if (NILP (Vquit_flag
) && quitf
) Vquit_flag
= Qt
;
2800 /* Get the value of symbol's global binding, even if that binding
2801 is not now dynamically visible. */
2804 top_level_value (symbol
)
2807 register struct specbinding
*ptr
= specpdl
;
2809 CHECK_SYMBOL (symbol
, 0);
2810 for (; ptr
!= specpdl_ptr
; ptr
++)
2812 if (EQ (ptr
->symbol
, symbol
))
2813 return ptr
->old_value
;
2815 return Fsymbol_value (symbol
);
2819 top_level_set (symbol
, newval
)
2820 Lisp_Object symbol
, newval
;
2822 register struct specbinding
*ptr
= specpdl
;
2824 CHECK_SYMBOL (symbol
, 0);
2825 for (; ptr
!= specpdl_ptr
; ptr
++)
2827 if (EQ (ptr
->symbol
, symbol
))
2829 ptr
->old_value
= newval
;
2833 return Fset (symbol
, newval
);
2838 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
2839 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2840 The debugger is entered when that frame exits, if the flag is non-nil.")
2842 Lisp_Object level
, flag
;
2844 register struct backtrace
*backlist
= backtrace_list
;
2847 CHECK_NUMBER (level
, 0);
2849 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
2851 backlist
= backlist
->next
;
2855 backlist
->debug_on_exit
= !NILP (flag
);
2860 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
2861 "Print a trace of Lisp function calls currently active.\n\
2862 Output stream used is value of `standard-output'.")
2865 register struct backtrace
*backlist
= backtrace_list
;
2869 extern Lisp_Object Vprint_level
;
2870 struct gcpro gcpro1
;
2872 XSETFASTINT (Vprint_level
, 3);
2879 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
2880 if (backlist
->nargs
== UNEVALLED
)
2882 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
2883 write_string ("\n", -1);
2887 tem
= *backlist
->function
;
2888 Fprin1 (tem
, Qnil
); /* This can QUIT */
2889 write_string ("(", -1);
2890 if (backlist
->nargs
== MANY
)
2892 for (tail
= *backlist
->args
, i
= 0;
2894 tail
= Fcdr (tail
), i
++)
2896 if (i
) write_string (" ", -1);
2897 Fprin1 (Fcar (tail
), Qnil
);
2902 for (i
= 0; i
< backlist
->nargs
; i
++)
2904 if (i
) write_string (" ", -1);
2905 Fprin1 (backlist
->args
[i
], Qnil
);
2908 write_string (")\n", -1);
2910 backlist
= backlist
->next
;
2913 Vprint_level
= Qnil
;
2918 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, "",
2919 "Return the function and arguments NFRAMES up from current execution point.\n\
2920 If that frame has not evaluated the arguments yet (or is a special form),\n\
2921 the value is (nil FUNCTION ARG-FORMS...).\n\
2922 If that frame has evaluated its arguments and called its function already,\n\
2923 the value is (t FUNCTION ARG-VALUES...).\n\
2924 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2925 FUNCTION is whatever was supplied as car of evaluated list,\n\
2926 or a lambda expression for macro calls.\n\
2927 If NFRAMES is more than the number of frames, the value is nil.")
2929 Lisp_Object nframes
;
2931 register struct backtrace
*backlist
= backtrace_list
;
2935 CHECK_NATNUM (nframes
, 0);
2937 /* Find the frame requested. */
2938 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
2939 backlist
= backlist
->next
;
2943 if (backlist
->nargs
== UNEVALLED
)
2944 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
2947 if (backlist
->nargs
== MANY
)
2948 tem
= *backlist
->args
;
2950 tem
= Flist (backlist
->nargs
, backlist
->args
);
2952 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
2959 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
2960 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
2961 If Lisp code tries to make more than this many at once,\n\
2962 an error is signaled.");
2964 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
2965 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
2966 This limit is to catch infinite recursions for you before they cause\n\
2967 actual stack overflow in C, which would be fatal for Emacs.\n\
2968 You can safely make it considerably larger than its default value,\n\
2969 if that proves inconveniently small.");
2971 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
2972 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2973 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2976 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
2977 "Non-nil inhibits C-g quitting from happening immediately.\n\
2978 Note that `quit-flag' will still be set by typing C-g,\n\
2979 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
2980 To prevent this happening, set `quit-flag' to nil\n\
2981 before making `inhibit-quit' nil.");
2982 Vinhibit_quit
= Qnil
;
2984 Qinhibit_quit
= intern ("inhibit-quit");
2985 staticpro (&Qinhibit_quit
);
2987 Qautoload
= intern ("autoload");
2988 staticpro (&Qautoload
);
2990 Qdebug_on_error
= intern ("debug-on-error");
2991 staticpro (&Qdebug_on_error
);
2993 Qmacro
= intern ("macro");
2994 staticpro (&Qmacro
);
2996 /* Note that the process handling also uses Qexit, but we don't want
2997 to staticpro it twice, so we just do it here. */
2998 Qexit
= intern ("exit");
3001 Qinteractive
= intern ("interactive");
3002 staticpro (&Qinteractive
);
3004 Qcommandp
= intern ("commandp");
3005 staticpro (&Qcommandp
);
3007 Qdefun
= intern ("defun");
3008 staticpro (&Qdefun
);
3010 Qand_rest
= intern ("&rest");
3011 staticpro (&Qand_rest
);
3013 Qand_optional
= intern ("&optional");
3014 staticpro (&Qand_optional
);
3016 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error
,
3017 "*Non-nil means automatically display a backtrace buffer\n\
3018 after any error that is handled by the editor command loop.\n\
3019 If the value is a list, an error only means to display a backtrace\n\
3020 if one of its condition symbols appears in the list.");
3021 Vstack_trace_on_error
= Qnil
;
3023 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error
,
3024 "*Non-nil means enter debugger if an error is signaled.\n\
3025 Does not apply to errors handled by `condition-case'.\n\
3026 If the value is a list, an error only means to enter the debugger\n\
3027 if one of its condition symbols appears in the list.\n\
3028 See also variable `debug-on-quit'.");
3029 Vdebug_on_error
= Qnil
;
3031 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors
,
3032 "*List of errors for which the debugger should not be called.\n\
3033 Each element may be a condition-name or a regexp that matches error messages.\n\
3034 If any element applies to a given error, that error skips the debugger\n\
3035 and just returns to top level.\n\
3036 This overrides the variable `debug-on-error'.\n\
3037 It does not apply to errors handled by `condition-case'.");
3038 Vdebug_ignored_errors
= Qnil
;
3040 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
3041 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3042 Does not apply if quit is handled by a `condition-case'.");
3045 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
3046 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3048 DEFVAR_LISP ("debugger", &Vdebugger
,
3049 "Function to call to invoke debugger.\n\
3050 If due to frame exit, args are `exit' and the value being returned;\n\
3051 this function's value will be returned instead of that.\n\
3052 If due to error, args are `error' and a list of the args to `signal'.\n\
3053 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3054 If due to `eval' entry, one arg, t.");
3057 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function
,
3058 "If non-nil, this is a function for `signal' to call.\n\
3059 It receives the same arguments that `signal' was given.\n\
3060 The Edebug package uses this to regain control.");
3061 Vsignal_hook_function
= Qnil
;
3063 Qmocklisp_arguments
= intern ("mocklisp-arguments");
3064 staticpro (&Qmocklisp_arguments
);
3065 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments
,
3066 "While in a mocklisp function, the list of its unevaluated args.");
3067 Vmocklisp_arguments
= Qt
;
3069 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal
,
3070 "*Non-nil means call the debugger regardless of condition handlers.\n\
3071 Note that `debug-on-error', `debug-on-quit' and friends\n\
3072 still determine whether to handle the particular condition.");
3073 Vdebug_on_signal
= Qnil
;
3075 Vrun_hooks
= intern ("run-hooks");
3076 staticpro (&Vrun_hooks
);
3078 staticpro (&Vautoload_queue
);
3079 Vautoload_queue
= Qnil
;
3090 defsubr (&Sfunction
);
3092 defsubr (&Sdefmacro
);
3094 defsubr (&Sdefconst
);
3095 defsubr (&Suser_variable_p
);
3099 defsubr (&Smacroexpand
);
3102 defsubr (&Sunwind_protect
);
3103 defsubr (&Scondition_case
);
3105 defsubr (&Sinteractive_p
);
3106 defsubr (&Scommandp
);
3107 defsubr (&Sautoload
);
3110 defsubr (&Sfuncall
);
3111 defsubr (&Srun_hooks
);
3112 defsubr (&Srun_hook_with_args
);
3113 defsubr (&Srun_hook_with_args_until_success
);
3114 defsubr (&Srun_hook_with_args_until_failure
);
3115 defsubr (&Sfetch_bytecode
);
3116 defsubr (&Sbacktrace_debug
);
3117 defsubr (&Sbacktrace
);
3118 defsubr (&Sbacktrace_frame
);