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. */
25 #include "blockinput.h"
36 /* This definition is duplicated in alloc.c and keyboard.c */
37 /* Putting it in lisp.h makes cc bomb out! */
41 struct backtrace
*next
;
42 Lisp_Object
*function
;
43 Lisp_Object
*args
; /* Points to vector of args. */
44 int nargs
; /* Length of vector.
45 If nargs is UNEVALLED, args points to slot holding
46 list of unevalled args */
48 /* Nonzero means call value of debugger when done with this operation. */
52 struct backtrace
*backtrace_list
;
54 /* This structure helps implement the `catch' and `throw' control
55 structure. A struct catchtag contains all the information needed
56 to restore the state of the interpreter after a non-local jump.
58 Handlers for error conditions (represented by `struct handler'
59 structures) just point to a catch tag to do the cleanup required
62 catchtag structures are chained together in the C calling stack;
63 the `next' member points to the next outer catchtag.
65 A call like (throw TAG VAL) searches for a catchtag whose `tag'
66 member is TAG, and then unbinds to it. The `val' member is used to
67 hold VAL while the stack is unwound; `val' is returned as the value
70 All the other members are concerned with restoring the interpreter
76 struct catchtag
*next
;
79 struct backtrace
*backlist
;
80 struct handler
*handlerlist
;
83 int poll_suppress_count
;
86 struct catchtag
*catchlist
;
88 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
89 Lisp_Object Qinhibit_quit
, Vinhibit_quit
, Vquit_flag
;
90 Lisp_Object Qmocklisp_arguments
, Vmocklisp_arguments
, Qmocklisp
;
91 Lisp_Object Qand_rest
, Qand_optional
;
92 Lisp_Object Qdebug_on_error
;
94 /* 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;
188 /* This is less than the initial value of num_nonmacro_input_events. */
189 when_entered_debugger
= -1;
196 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
197 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
198 if (specpdl_size
+ 40 > max_specpdl_size
)
199 max_specpdl_size
= specpdl_size
+ 40;
200 debug_on_next_call
= 0;
201 when_entered_debugger
= num_nonmacro_input_events
;
202 return apply1 (Vdebugger
, arg
);
206 do_debug_on_call (code
)
209 debug_on_next_call
= 0;
210 backtrace_list
->debug_on_exit
= 1;
211 call_debugger (Fcons (code
, Qnil
));
214 /* NOTE!!! Every function that can call EVAL must protect its args
215 and temporaries from garbage collection while it needs them.
216 The definition of `For' shows what you have to do. */
218 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
219 "Eval args until one of them yields non-nil, then return that value.\n\
220 The remaining args are not evalled at all.\n\
221 If all args return nil, return nil.")
225 register Lisp_Object val
;
226 Lisp_Object args_left
;
237 val
= Feval (Fcar (args_left
));
240 args_left
= Fcdr (args_left
);
242 while (!NILP(args_left
));
248 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
249 "Eval args until one of them yields nil, then return nil.\n\
250 The remaining args are not evalled at all.\n\
251 If no arg yields nil, return the last arg's value.")
255 register Lisp_Object val
;
256 Lisp_Object args_left
;
267 val
= Feval (Fcar (args_left
));
270 args_left
= Fcdr (args_left
);
272 while (!NILP(args_left
));
278 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
279 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
280 Returns the value of THEN or the value of the last of the ELSE's.\n\
281 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
282 If COND yields nil, and there are no ELSE's, the value is nil.")
286 register Lisp_Object cond
;
290 cond
= Feval (Fcar (args
));
294 return Feval (Fcar (Fcdr (args
)));
295 return Fprogn (Fcdr (Fcdr (args
)));
298 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
299 "(cond CLAUSES...): try each clause until one succeeds.\n\
300 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
301 and, if the value is non-nil, this clause succeeds:\n\
302 then the expressions in BODY are evaluated and the last one's\n\
303 value is the value of the cond-form.\n\
304 If no clause succeeds, cond returns nil.\n\
305 If a clause has one element, as in (CONDITION),\n\
306 CONDITION's value if non-nil is returned from the cond-form.")
310 register Lisp_Object clause
, val
;
317 clause
= Fcar (args
);
318 val
= Feval (Fcar (clause
));
321 if (!EQ (XCDR (clause
), Qnil
))
322 val
= Fprogn (XCDR (clause
));
332 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
333 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
337 register Lisp_Object val
, tem
;
338 Lisp_Object args_left
;
341 /* In Mocklisp code, symbols at the front of the progn arglist
342 are to be bound to zero. */
343 if (!EQ (Vmocklisp_arguments
, Qt
))
345 val
= make_number (0);
346 while (!NILP (args
) && (tem
= Fcar (args
), SYMBOLP (tem
)))
349 specbind (tem
, val
), args
= Fcdr (args
);
361 val
= Feval (Fcar (args_left
));
362 args_left
= Fcdr (args_left
);
364 while (!NILP(args_left
));
370 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
371 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
372 The value of FIRST is saved during the evaluation of the remaining args,\n\
373 whose values are discarded.")
378 register Lisp_Object args_left
;
379 struct gcpro gcpro1
, gcpro2
;
380 register int argnum
= 0;
392 val
= Feval (Fcar (args_left
));
394 Feval (Fcar (args_left
));
395 args_left
= Fcdr (args_left
);
397 while (!NILP(args_left
));
403 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
404 "(prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
405 The value of Y is saved during the evaluation of the remaining args,\n\
406 whose values are discarded.")
411 register Lisp_Object args_left
;
412 struct gcpro gcpro1
, gcpro2
;
413 register int argnum
= -1;
427 val
= Feval (Fcar (args_left
));
429 Feval (Fcar (args_left
));
430 args_left
= Fcdr (args_left
);
432 while (!NILP (args_left
));
438 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
439 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
440 The symbols SYM are variables; they are literal (not evaluated).\n\
441 The values VAL are expressions; they are evaluated.\n\
442 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
443 The second VAL is not computed until after the first SYM is set, and so on;\n\
444 each VAL can use the new value of variables set earlier in the `setq'.\n\
445 The return value of the `setq' form is the value of the last VAL.")
449 register Lisp_Object args_left
;
450 register Lisp_Object val
, sym
;
461 val
= Feval (Fcar (Fcdr (args_left
)));
462 sym
= Fcar (args_left
);
464 args_left
= Fcdr (Fcdr (args_left
));
466 while (!NILP(args_left
));
472 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
473 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
480 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
481 "Like `quote', but preferred for objects which are functions.\n\
482 In byte compilation, `function' causes its argument to be compiled.\n\
483 `quote' cannot do that.")
490 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
491 "Return t if function in which this appears was called interactively.\n\
492 This means that the function was called with call-interactively (which\n\
493 includes being called as the binding of a key)\n\
494 and input is currently coming from the keyboard (not in keyboard macro).")
497 register struct backtrace
*btp
;
498 register Lisp_Object fun
;
503 btp
= backtrace_list
;
505 /* If this isn't a byte-compiled function, there may be a frame at
506 the top for Finteractive_p itself. If so, skip it. */
507 fun
= Findirect_function (*btp
->function
);
508 if (SUBRP (fun
) && XSUBR (fun
) == &Sinteractive_p
)
511 /* If we're running an Emacs 18-style byte-compiled function, there
512 may be a frame for Fbytecode. Now, given the strictest
513 definition, this function isn't really being called
514 interactively, but because that's the way Emacs 18 always builds
515 byte-compiled functions, we'll accept it for now. */
516 if (EQ (*btp
->function
, Qbytecode
))
519 /* If this isn't a byte-compiled function, then we may now be
520 looking at several frames for special forms. Skip past them. */
522 btp
->nargs
== UNEVALLED
)
525 /* btp now points at the frame of the innermost function that isn't
526 a special form, ignoring frames for Finteractive_p and/or
527 Fbytecode at the top. If this frame is for a built-in function
528 (such as load or eval-region) return nil. */
529 fun
= Findirect_function (*btp
->function
);
532 /* btp points to the frame of a Lisp function that called interactive-p.
533 Return t if that function was called interactively. */
534 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
539 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
540 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
541 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
542 See also the function `interactive'.")
546 register Lisp_Object fn_name
;
547 register Lisp_Object defn
;
549 fn_name
= Fcar (args
);
550 defn
= Fcons (Qlambda
, Fcdr (args
));
551 if (!NILP (Vpurify_flag
))
552 defn
= Fpurecopy (defn
);
553 Ffset (fn_name
, defn
);
554 LOADHIST_ATTACH (fn_name
);
558 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
559 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
560 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
561 When the macro is called, as in (NAME ARGS...),\n\
562 the function (lambda ARGLIST BODY...) is applied to\n\
563 the list ARGS... as it appears in the expression,\n\
564 and the result should be a form to be evaluated instead of the original.")
568 register Lisp_Object fn_name
;
569 register Lisp_Object defn
;
571 fn_name
= Fcar (args
);
572 defn
= Fcons (Qmacro
, 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 ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
581 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
582 You are not required to define a variable in order to use it,\n\
583 but the definition can supply documentation and an initial value\n\
584 in a way that tags can recognize.\n\n\
585 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
586 If SYMBOL is buffer-local, its default value is what is set;\n\
587 buffer-local values are not affected.\n\
588 INITVALUE and DOCSTRING are optional.\n\
589 If DOCSTRING starts with *, this variable is identified as a user option.\n\
590 This means that M-x set-variable and M-x edit-options recognize it.\n\
591 If INITVALUE is missing, SYMBOL's value is not set.")
595 register Lisp_Object sym
, tem
, tail
;
599 if (!NILP (Fcdr (Fcdr (tail
))))
600 error ("too many arguments");
604 tem
= Fdefault_boundp (sym
);
606 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
608 tail
= Fcdr (Fcdr (args
));
609 if (!NILP (Fcar (tail
)))
612 if (!NILP (Vpurify_flag
))
613 tem
= Fpurecopy (tem
);
614 Fput (sym
, Qvariable_documentation
, tem
);
616 LOADHIST_ATTACH (sym
);
620 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
621 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
622 The intent is that neither programs nor users should ever change this value.\n\
623 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
624 If SYMBOL is buffer-local, its default value is what is set;\n\
625 buffer-local values are not affected.\n\
626 DOCSTRING is optional.")
630 register Lisp_Object sym
, tem
;
633 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
634 error ("too many arguments");
636 Fset_default (sym
, Feval (Fcar (Fcdr (args
))));
637 tem
= Fcar (Fcdr (Fcdr (args
)));
640 if (!NILP (Vpurify_flag
))
641 tem
= Fpurecopy (tem
);
642 Fput (sym
, Qvariable_documentation
, tem
);
644 LOADHIST_ATTACH (sym
);
648 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
649 "Returns t if VARIABLE is intended to be set and modified by users.\n\
650 \(The alternative is a variable used internally in a Lisp program.)\n\
651 Determined by whether the first character of the documentation\n\
652 for the variable is `*'.")
654 Lisp_Object variable
;
656 Lisp_Object documentation
;
658 if (!SYMBOLP (variable
))
661 documentation
= Fget (variable
, Qvariable_documentation
);
662 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
664 if (STRINGP (documentation
)
665 && ((unsigned char) XSTRING (documentation
)->data
[0] == '*'))
667 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
668 if (CONSP (documentation
)
669 && STRINGP (XCAR (documentation
))
670 && INTEGERP (XCDR (documentation
))
671 && XINT (XCDR (documentation
)) < 0)
676 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
677 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
678 The value of the last form in BODY is returned.\n\
679 Each element of VARLIST is a symbol (which is bound to nil)\n\
680 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
681 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
685 Lisp_Object varlist
, val
, elt
;
686 int count
= specpdl_ptr
- specpdl
;
687 struct gcpro gcpro1
, gcpro2
, gcpro3
;
689 GCPRO3 (args
, elt
, varlist
);
691 varlist
= Fcar (args
);
692 while (!NILP (varlist
))
695 elt
= Fcar (varlist
);
697 specbind (elt
, Qnil
);
698 else if (! NILP (Fcdr (Fcdr (elt
))))
700 Fcons (build_string ("`let' bindings can have only one value-form"),
704 val
= Feval (Fcar (Fcdr (elt
)));
705 specbind (Fcar (elt
), val
);
707 varlist
= Fcdr (varlist
);
710 val
= Fprogn (Fcdr (args
));
711 return unbind_to (count
, val
);
714 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
715 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
716 The value of the last form in BODY is returned.\n\
717 Each element of VARLIST is a symbol (which is bound to nil)\n\
718 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
719 All the VALUEFORMs are evalled before any symbols are bound.")
723 Lisp_Object
*temps
, tem
;
724 register Lisp_Object elt
, varlist
;
725 int count
= specpdl_ptr
- specpdl
;
727 struct gcpro gcpro1
, gcpro2
;
729 varlist
= Fcar (args
);
731 /* Make space to hold the values to give the bound variables */
732 elt
= Flength (varlist
);
733 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
735 /* Compute the values and store them in `temps' */
737 GCPRO2 (args
, *temps
);
740 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
743 elt
= Fcar (varlist
);
745 temps
[argnum
++] = Qnil
;
746 else if (! NILP (Fcdr (Fcdr (elt
))))
748 Fcons (build_string ("`let' bindings can have only one value-form"),
751 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
752 gcpro2
.nvars
= argnum
;
756 varlist
= Fcar (args
);
757 for (argnum
= 0; !NILP (varlist
); varlist
= Fcdr (varlist
))
759 elt
= Fcar (varlist
);
760 tem
= temps
[argnum
++];
764 specbind (Fcar (elt
), tem
);
767 elt
= Fprogn (Fcdr (args
));
768 return unbind_to (count
, elt
);
771 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
772 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
773 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
774 until TEST returns nil.")
778 Lisp_Object test
, body
, tem
;
779 struct gcpro gcpro1
, gcpro2
;
785 while (tem
= Feval (test
),
786 (!EQ (Vmocklisp_arguments
, Qt
) ? XINT (tem
) : !NILP (tem
)))
796 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
797 "Return result of expanding macros at top level of FORM.\n\
798 If FORM is not a macro call, it is returned unchanged.\n\
799 Otherwise, the macro is expanded and the expansion is considered\n\
800 in place of FORM. When a non-macro-call results, it is returned.\n\n\
801 The second optional arg ENVIRONMENT species an environment of macro\n\
802 definitions to shadow the loaded ones for use in file byte-compilation.")
805 Lisp_Object environment
;
807 /* With cleanups from Hallvard Furuseth. */
808 register Lisp_Object expander
, sym
, def
, tem
;
812 /* Come back here each time we expand a macro call,
813 in case it expands into another macro call. */
816 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
817 def
= sym
= XCAR (form
);
819 /* Trace symbols aliases to other symbols
820 until we get a symbol that is not an alias. */
821 while (SYMBOLP (def
))
825 tem
= Fassq (sym
, environment
);
828 def
= XSYMBOL (sym
)->function
;
829 if (!EQ (def
, Qunbound
))
834 /* Right now TEM is the result from SYM in ENVIRONMENT,
835 and if TEM is nil then DEF is SYM's function definition. */
838 /* SYM is not mentioned in ENVIRONMENT.
839 Look at its function definition. */
840 if (EQ (def
, Qunbound
) || !CONSP (def
))
841 /* Not defined or definition not suitable */
843 if (EQ (XCAR (def
), Qautoload
))
845 /* Autoloading function: will it be a macro when loaded? */
846 tem
= Fnth (make_number (4), def
);
847 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
848 /* Yes, load it and try again. */
852 do_autoload (def
, sym
);
859 else if (!EQ (XCAR (def
), Qmacro
))
861 else expander
= XCDR (def
);
865 expander
= XCDR (tem
);
869 form
= apply1 (expander
, XCDR (form
));
874 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
875 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
876 TAG is evalled to get the tag to use; it must not be nil.\n\
878 Then the BODY is executed.\n\
879 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
880 If no throw happens, `catch' returns the value of the last BODY form.\n\
881 If a throw happens, it specifies the value to return from `catch'.")
885 register Lisp_Object tag
;
889 tag
= Feval (Fcar (args
));
891 return internal_catch (tag
, Fprogn
, Fcdr (args
));
894 /* Set up a catch, then call C function FUNC on argument ARG.
895 FUNC should return a Lisp_Object.
896 This is how catches are done from within C code. */
899 internal_catch (tag
, func
, arg
)
901 Lisp_Object (*func
) ();
904 /* This structure is made part of the chain `catchlist'. */
907 /* Fill in the components of c, and put it on the list. */
911 c
.backlist
= backtrace_list
;
912 c
.handlerlist
= handlerlist
;
913 c
.lisp_eval_depth
= lisp_eval_depth
;
914 c
.pdlcount
= specpdl_ptr
- specpdl
;
915 c
.poll_suppress_count
= poll_suppress_count
;
920 if (! _setjmp (c
.jmp
))
921 c
.val
= (*func
) (arg
);
923 /* Throw works by a longjmp that comes right here. */
928 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
929 jump to that CATCH, returning VALUE as the value of that catch.
931 This is the guts Fthrow and Fsignal; they differ only in the way
932 they choose the catch tag to throw to. A catch tag for a
933 condition-case form has a TAG of Qnil.
935 Before each catch is discarded, unbind all special bindings and
936 execute all unwind-protect clauses made above that catch. Unwind
937 the handler stack as we go, so that the proper handlers are in
938 effect for each unwind-protect clause we run. At the end, restore
939 some static info saved in CATCH, and longjmp to the location
942 This is used for correct unwinding in Fthrow and Fsignal. */
945 unwind_to_catch (catch, value
)
946 struct catchtag
*catch;
949 register int last_time
;
951 /* Save the value in the tag. */
954 /* Restore the polling-suppression count. */
955 set_poll_suppress_count (catch->poll_suppress_count
);
959 last_time
= catchlist
== catch;
961 /* Unwind the specpdl stack, and then restore the proper set of
963 unbind_to (catchlist
->pdlcount
, Qnil
);
964 handlerlist
= catchlist
->handlerlist
;
965 catchlist
= catchlist
->next
;
969 gcprolist
= catch->gcpro
;
970 backtrace_list
= catch->backlist
;
971 lisp_eval_depth
= catch->lisp_eval_depth
;
973 _longjmp (catch->jmp
, 1);
976 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
977 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
978 Both TAG and VALUE are evalled.")
980 register Lisp_Object tag
, value
;
982 register struct catchtag
*c
;
987 for (c
= catchlist
; c
; c
= c
->next
)
989 if (EQ (c
->tag
, tag
))
990 unwind_to_catch (c
, value
);
992 tag
= Fsignal (Qno_catch
, Fcons (tag
, Fcons (value
, Qnil
)));
997 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
998 "Do BODYFORM, protecting with UNWINDFORMS.\n\
999 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
1000 If BODYFORM completes normally, its value is returned\n\
1001 after executing the UNWINDFORMS.\n\
1002 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1007 int count
= specpdl_ptr
- specpdl
;
1009 record_unwind_protect (0, Fcdr (args
));
1010 val
= Feval (Fcar (args
));
1011 return unbind_to (count
, val
);
1014 /* Chain of condition handlers currently in effect.
1015 The elements of this chain are contained in the stack frames
1016 of Fcondition_case and internal_condition_case.
1017 When an error is signaled (by calling Fsignal, below),
1018 this chain is searched for an element that applies. */
1020 struct handler
*handlerlist
;
1022 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1023 "Regain control when an error is signaled.\n\
1024 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
1025 executes BODYFORM and returns its value if no error happens.\n\
1026 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1027 where the BODY is made of Lisp expressions.\n\n\
1028 A handler is applicable to an error\n\
1029 if CONDITION-NAME is one of the error's condition names.\n\
1030 If an error happens, the first applicable handler is run.\n\
1032 The car of a handler may be a list of condition names\n\
1033 instead of a single condition name.\n\
1035 When a handler handles an error,\n\
1036 control returns to the condition-case and the handler BODY... is executed\n\
1037 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1038 VAR may be nil; then you do not get access to the signal information.\n\
1040 The value of the last BODY form is returned from the condition-case.\n\
1041 See also the function `signal' for more info.")
1048 register Lisp_Object var
, bodyform
, handlers
;
1051 bodyform
= Fcar (Fcdr (args
));
1052 handlers
= Fcdr (Fcdr (args
));
1053 CHECK_SYMBOL (var
, 0);
1055 for (val
= handlers
; ! NILP (val
); val
= Fcdr (val
))
1061 && (SYMBOLP (XCAR (tem
))
1062 || CONSP (XCAR (tem
))))))
1063 error ("Invalid condition handler", tem
);
1068 c
.backlist
= backtrace_list
;
1069 c
.handlerlist
= handlerlist
;
1070 c
.lisp_eval_depth
= lisp_eval_depth
;
1071 c
.pdlcount
= specpdl_ptr
- specpdl
;
1072 c
.poll_suppress_count
= poll_suppress_count
;
1073 c
.gcpro
= gcprolist
;
1074 if (_setjmp (c
.jmp
))
1077 specbind (h
.var
, c
.val
);
1078 val
= Fprogn (Fcdr (h
.chosen_clause
));
1080 /* Note that this just undoes the binding of h.var; whoever
1081 longjumped to us unwound the stack to c.pdlcount before
1083 unbind_to (c
.pdlcount
, Qnil
);
1090 h
.handler
= handlers
;
1091 h
.next
= handlerlist
;
1095 val
= Feval (bodyform
);
1097 handlerlist
= h
.next
;
1101 /* Call the function BFUN with no arguments, catching errors within it
1102 according to HANDLERS. If there is an error, call HFUN with
1103 one argument which is the data that describes the error:
1106 HANDLERS can be a list of conditions to catch.
1107 If HANDLERS is Qt, catch all errors.
1108 If HANDLERS is Qerror, catch all errors
1109 but allow the debugger to run if that is enabled. */
1112 internal_condition_case (bfun
, handlers
, hfun
)
1113 Lisp_Object (*bfun
) ();
1114 Lisp_Object handlers
;
1115 Lisp_Object (*hfun
) ();
1121 /* Since Fsignal resets this to 0, it had better be 0 now
1122 or else we have a potential bug. */
1123 if (interrupt_input_blocked
!= 0)
1128 c
.backlist
= backtrace_list
;
1129 c
.handlerlist
= handlerlist
;
1130 c
.lisp_eval_depth
= lisp_eval_depth
;
1131 c
.pdlcount
= specpdl_ptr
- specpdl
;
1132 c
.poll_suppress_count
= poll_suppress_count
;
1133 c
.gcpro
= gcprolist
;
1134 if (_setjmp (c
.jmp
))
1136 return (*hfun
) (c
.val
);
1140 h
.handler
= handlers
;
1142 h
.next
= handlerlist
;
1148 handlerlist
= h
.next
;
1152 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1155 internal_condition_case_1 (bfun
, arg
, handlers
, hfun
)
1156 Lisp_Object (*bfun
) ();
1158 Lisp_Object handlers
;
1159 Lisp_Object (*hfun
) ();
1167 c
.backlist
= backtrace_list
;
1168 c
.handlerlist
= handlerlist
;
1169 c
.lisp_eval_depth
= lisp_eval_depth
;
1170 c
.pdlcount
= specpdl_ptr
- specpdl
;
1171 c
.poll_suppress_count
= poll_suppress_count
;
1172 c
.gcpro
= gcprolist
;
1173 if (_setjmp (c
.jmp
))
1175 return (*hfun
) (c
.val
);
1179 h
.handler
= handlers
;
1181 h
.next
= handlerlist
;
1185 val
= (*bfun
) (arg
);
1187 handlerlist
= h
.next
;
1191 static Lisp_Object
find_handler_clause ();
1193 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1194 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1195 This function does not return.\n\n\
1196 An error symbol is a symbol with an `error-conditions' property\n\
1197 that is a list of condition names.\n\
1198 A handler for any of those names will get to handle this signal.\n\
1199 The symbol `error' should normally be one of them.\n\
1201 DATA should be a list. Its elements are printed as part of the error message.\n\
1202 If the signal is handled, DATA is made available to the handler.\n\
1203 See also the function `condition-case'.")
1204 (error_symbol
, data
)
1205 Lisp_Object error_symbol
, data
;
1207 /* When memory is full, ERROR-SYMBOL is nil,
1208 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1209 register struct handler
*allhandlers
= handlerlist
;
1210 Lisp_Object conditions
;
1211 extern int gc_in_progress
;
1212 extern int waiting_for_input
;
1213 Lisp_Object debugger_value
;
1215 Lisp_Object real_error_symbol
;
1216 extern int display_busy_cursor_p
;
1219 if (gc_in_progress
|| waiting_for_input
)
1222 TOTALLY_UNBLOCK_INPUT
;
1224 if (NILP (error_symbol
))
1225 real_error_symbol
= Fcar (data
);
1227 real_error_symbol
= error_symbol
;
1229 #ifdef HAVE_X_WINDOWS
1230 if (display_busy_cursor_p
)
1231 Fx_hide_busy_cursor (Qt
);
1234 /* This hook is used by edebug. */
1235 if (! NILP (Vsignal_hook_function
))
1236 call2 (Vsignal_hook_function
, error_symbol
, data
);
1238 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1240 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1242 register Lisp_Object clause
;
1243 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1244 error_symbol
, data
, &debugger_value
);
1246 #if 0 /* Most callers are not prepared to handle gc if this returns.
1247 So, since this feature is not very useful, take it out. */
1248 /* If have called debugger and user wants to continue,
1250 if (EQ (clause
, Qlambda
))
1251 return debugger_value
;
1253 if (EQ (clause
, Qlambda
))
1255 /* We can't return values to code which signaled an error, but we
1256 can continue code which has signaled a quit. */
1257 if (EQ (real_error_symbol
, Qquit
))
1260 error ("Cannot return from the debugger in an error");
1266 Lisp_Object unwind_data
;
1267 struct handler
*h
= handlerlist
;
1269 handlerlist
= allhandlers
;
1271 if (NILP (error_symbol
))
1274 unwind_data
= Fcons (error_symbol
, data
);
1275 h
->chosen_clause
= clause
;
1276 unwind_to_catch (h
->tag
, unwind_data
);
1280 handlerlist
= allhandlers
;
1281 /* If no handler is present now, try to run the debugger,
1282 and if that fails, throw to top level. */
1283 find_handler_clause (Qerror
, conditions
, error_symbol
, data
, &debugger_value
);
1285 Fthrow (Qtop_level
, Qt
);
1287 if (! NILP (error_symbol
))
1288 data
= Fcons (error_symbol
, data
);
1290 string
= Ferror_message_string (data
);
1291 fatal ("%s", XSTRING (string
)->data
, 0);
1294 /* Return nonzero iff LIST is a non-nil atom or
1295 a list containing one of CONDITIONS. */
1298 wants_debugger (list
, conditions
)
1299 Lisp_Object list
, conditions
;
1306 while (CONSP (conditions
))
1308 Lisp_Object
this, tail
;
1309 this = XCAR (conditions
);
1310 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1311 if (EQ (XCAR (tail
), this))
1313 conditions
= XCDR (conditions
);
1318 /* Return 1 if an error with condition-symbols CONDITIONS,
1319 and described by SIGNAL-DATA, should skip the debugger
1320 according to debugger-ignore-errors. */
1323 skip_debugger (conditions
, data
)
1324 Lisp_Object conditions
, data
;
1327 int first_string
= 1;
1328 Lisp_Object error_message
;
1330 for (tail
= Vdebug_ignored_errors
; CONSP (tail
);
1333 if (STRINGP (XCAR (tail
)))
1337 error_message
= Ferror_message_string (data
);
1340 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1345 Lisp_Object contail
;
1347 for (contail
= conditions
; CONSP (contail
);
1348 contail
= XCDR (contail
))
1349 if (EQ (XCAR (tail
), XCAR (contail
)))
1357 /* Value of Qlambda means we have called debugger and user has continued.
1358 There are two ways to pass SIG and DATA:
1359 = SIG is the error symbol, and DATA is the rest of the data.
1360 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1361 This is for memory-full errors only.
1363 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1366 find_handler_clause (handlers
, conditions
, sig
, data
, debugger_value_ptr
)
1367 Lisp_Object handlers
, conditions
, sig
, data
;
1368 Lisp_Object
*debugger_value_ptr
;
1370 register Lisp_Object h
;
1371 register Lisp_Object tem
;
1373 if (EQ (handlers
, Qt
)) /* t is used by handlers for all conditions, set up by C code. */
1375 /* error is used similarly, but means print an error message
1376 and run the debugger if that is enabled. */
1377 if (EQ (handlers
, Qerror
)
1378 || !NILP (Vdebug_on_signal
)) /* This says call debugger even if
1379 there is a handler. */
1381 int count
= specpdl_ptr
- specpdl
;
1382 int debugger_called
= 0;
1383 Lisp_Object sig_symbol
, combined_data
;
1384 /* This is set to 1 if we are handling a memory-full error,
1385 because these must not run the debugger.
1386 (There is no room in memory to do that!) */
1387 int no_debugger
= 0;
1391 combined_data
= data
;
1392 sig_symbol
= Fcar (data
);
1397 combined_data
= Fcons (sig
, data
);
1401 if (wants_debugger (Vstack_trace_on_error
, conditions
))
1404 internal_with_output_to_temp_buffer ("*Backtrace*",
1405 (Lisp_Object (*) (Lisp_Object
)) Fbacktrace
,
1408 internal_with_output_to_temp_buffer ("*Backtrace*",
1413 && (EQ (sig_symbol
, Qquit
)
1415 : wants_debugger (Vdebug_on_error
, conditions
))
1416 && ! skip_debugger (conditions
, combined_data
)
1417 && when_entered_debugger
< num_nonmacro_input_events
)
1419 specbind (Qdebug_on_error
, Qnil
);
1421 = call_debugger (Fcons (Qerror
,
1422 Fcons (combined_data
, Qnil
)));
1423 debugger_called
= 1;
1425 /* If there is no handler, return saying whether we ran the debugger. */
1426 if (EQ (handlers
, Qerror
))
1428 if (debugger_called
)
1429 return unbind_to (count
, Qlambda
);
1433 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1435 Lisp_Object handler
, condit
;
1438 if (!CONSP (handler
))
1440 condit
= Fcar (handler
);
1441 /* Handle a single condition name in handler HANDLER. */
1442 if (SYMBOLP (condit
))
1444 tem
= Fmemq (Fcar (handler
), conditions
);
1448 /* Handle a list of condition names in handler HANDLER. */
1449 else if (CONSP (condit
))
1451 while (CONSP (condit
))
1453 tem
= Fmemq (Fcar (condit
), conditions
);
1456 condit
= XCDR (condit
);
1463 /* dump an error message; called like printf */
1467 error (m
, a1
, a2
, a3
)
1487 int used
= doprnt (buffer
, size
, m
, m
+ mlen
, 3, args
);
1492 buffer
= (char *) xrealloc (buffer
, size
);
1495 buffer
= (char *) xmalloc (size
);
1500 string
= build_string (buffer
);
1504 Fsignal (Qerror
, Fcons (string
, Qnil
));
1507 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 1, 0,
1508 "T if FUNCTION makes provisions for interactive calling.\n\
1509 This means it contains a description for how to read arguments to give it.\n\
1510 The value is nil for an invalid function or a symbol with no function\n\
1513 Interactively callable functions include strings and vectors (treated\n\
1514 as keyboard macros), lambda-expressions that contain a top-level call\n\
1515 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1516 fourth argument, and some of the built-in functions of Lisp.\n\
1518 Also, a symbol satisfies `commandp' if its function definition does so.")
1520 Lisp_Object function
;
1522 register Lisp_Object fun
;
1523 register Lisp_Object funcar
;
1527 fun
= indirect_function (fun
);
1528 if (EQ (fun
, Qunbound
))
1531 /* Emacs primitives are interactive if their DEFUN specifies an
1532 interactive spec. */
1535 if (XSUBR (fun
)->prompt
)
1541 /* Bytecode objects are interactive if they are long enough to
1542 have an element whose index is COMPILED_INTERACTIVE, which is
1543 where the interactive spec is stored. */
1544 else if (COMPILEDP (fun
))
1545 return ((XVECTOR (fun
)->size
& PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1548 /* Strings and vectors are keyboard macros. */
1549 if (STRINGP (fun
) || VECTORP (fun
))
1552 /* Lists may represent commands. */
1555 funcar
= Fcar (fun
);
1556 if (!SYMBOLP (funcar
))
1557 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1558 if (EQ (funcar
, Qlambda
))
1559 return Fassq (Qinteractive
, Fcdr (Fcdr (fun
)));
1560 if (EQ (funcar
, Qmocklisp
))
1561 return Qt
; /* All mocklisp functions can be called interactively */
1562 if (EQ (funcar
, Qautoload
))
1563 return Fcar (Fcdr (Fcdr (Fcdr (fun
))));
1569 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1570 "Define FUNCTION to autoload from FILE.\n\
1571 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1572 Third arg DOCSTRING is documentation for the function.\n\
1573 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1574 Fifth arg TYPE indicates the type of the object:\n\
1575 nil or omitted says FUNCTION is a function,\n\
1576 `keymap' says FUNCTION is really a keymap, and\n\
1577 `macro' or t says FUNCTION is really a macro.\n\
1578 Third through fifth args give info about the real definition.\n\
1579 They default to nil.\n\
1580 If FUNCTION is already defined other than as an autoload,\n\
1581 this does nothing and returns nil.")
1582 (function
, file
, docstring
, interactive
, type
)
1583 Lisp_Object function
, file
, docstring
, interactive
, type
;
1586 Lisp_Object args
[4];
1589 CHECK_SYMBOL (function
, 0);
1590 CHECK_STRING (file
, 1);
1592 /* If function is defined and not as an autoload, don't override */
1593 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1594 && !(CONSP (XSYMBOL (function
)->function
)
1595 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1600 args
[1] = docstring
;
1601 args
[2] = interactive
;
1604 return Ffset (function
, Fcons (Qautoload
, Flist (4, &args
[0])));
1605 #else /* NO_ARG_ARRAY */
1606 return Ffset (function
, Fcons (Qautoload
, Flist (4, &file
)));
1607 #endif /* not NO_ARG_ARRAY */
1611 un_autoload (oldqueue
)
1612 Lisp_Object oldqueue
;
1614 register Lisp_Object queue
, first
, second
;
1616 /* Queue to unwind is current value of Vautoload_queue.
1617 oldqueue is the shadowed value to leave in Vautoload_queue. */
1618 queue
= Vautoload_queue
;
1619 Vautoload_queue
= oldqueue
;
1620 while (CONSP (queue
))
1622 first
= Fcar (queue
);
1623 second
= Fcdr (first
);
1624 first
= Fcar (first
);
1625 if (EQ (second
, Qnil
))
1628 Ffset (first
, second
);
1629 queue
= Fcdr (queue
);
1634 /* Load an autoloaded function.
1635 FUNNAME is the symbol which is the function's name.
1636 FUNDEF is the autoload definition (a list). */
1639 do_autoload (fundef
, funname
)
1640 Lisp_Object fundef
, funname
;
1642 int count
= specpdl_ptr
- specpdl
;
1643 Lisp_Object fun
, queue
, first
, second
;
1644 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1647 CHECK_SYMBOL (funname
, 0);
1648 GCPRO3 (fun
, funname
, fundef
);
1650 /* Preserve the match data. */
1651 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
1653 /* Value saved here is to be restored into Vautoload_queue. */
1654 record_unwind_protect (un_autoload
, Vautoload_queue
);
1655 Vautoload_queue
= Qt
;
1656 Fload (Fcar (Fcdr (fundef
)), Qnil
, noninteractive
? Qt
: Qnil
, Qnil
, Qt
);
1658 /* Save the old autoloads, in case we ever do an unload. */
1659 queue
= Vautoload_queue
;
1660 while (CONSP (queue
))
1662 first
= Fcar (queue
);
1663 second
= Fcdr (first
);
1664 first
= Fcar (first
);
1666 /* Note: This test is subtle. The cdr of an autoload-queue entry
1667 may be an atom if the autoload entry was generated by a defalias
1670 Fput (first
, Qautoload
, (Fcdr (second
)));
1672 queue
= Fcdr (queue
);
1675 /* Once loading finishes, don't undo it. */
1676 Vautoload_queue
= Qt
;
1677 unbind_to (count
, Qnil
);
1679 fun
= Findirect_function (fun
);
1681 if (!NILP (Fequal (fun
, fundef
)))
1682 error ("Autoloading failed to define function %s",
1683 XSYMBOL (funname
)->name
->data
);
1687 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
1688 "Evaluate FORM and return its value.")
1692 Lisp_Object fun
, val
, original_fun
, original_args
;
1694 struct backtrace backtrace
;
1695 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1697 /* Since Fsignal resets this to 0, it had better be 0 now
1698 or else we have a potential bug. */
1699 if (interrupt_input_blocked
!= 0)
1704 if (EQ (Vmocklisp_arguments
, Qt
))
1705 return Fsymbol_value (form
);
1706 val
= Fsymbol_value (form
);
1708 XSETFASTINT (val
, 0);
1709 else if (EQ (val
, Qt
))
1710 XSETFASTINT (val
, 1);
1717 if (consing_since_gc
> gc_cons_threshold
)
1720 Fgarbage_collect ();
1724 if (++lisp_eval_depth
> max_lisp_eval_depth
)
1726 if (max_lisp_eval_depth
< 100)
1727 max_lisp_eval_depth
= 100;
1728 if (lisp_eval_depth
> max_lisp_eval_depth
)
1729 error ("Lisp nesting exceeds max-lisp-eval-depth");
1732 original_fun
= Fcar (form
);
1733 original_args
= Fcdr (form
);
1735 backtrace
.next
= backtrace_list
;
1736 backtrace_list
= &backtrace
;
1737 backtrace
.function
= &original_fun
; /* This also protects them from gc */
1738 backtrace
.args
= &original_args
;
1739 backtrace
.nargs
= UNEVALLED
;
1740 backtrace
.evalargs
= 1;
1741 backtrace
.debug_on_exit
= 0;
1743 if (debug_on_next_call
)
1744 do_debug_on_call (Qt
);
1746 /* At this point, only original_fun and original_args
1747 have values that will be used below */
1749 fun
= Findirect_function (original_fun
);
1753 Lisp_Object numargs
;
1754 Lisp_Object argvals
[8];
1755 Lisp_Object args_left
;
1756 register int i
, maxargs
;
1758 args_left
= original_args
;
1759 numargs
= Flength (args_left
);
1761 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
1762 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
1763 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
1765 if (XSUBR (fun
)->max_args
== UNEVALLED
)
1767 backtrace
.evalargs
= 0;
1768 val
= (*XSUBR (fun
)->function
) (args_left
);
1772 if (XSUBR (fun
)->max_args
== MANY
)
1774 /* Pass a vector of evaluated arguments */
1776 register int argnum
= 0;
1778 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
1780 GCPRO3 (args_left
, fun
, fun
);
1784 while (!NILP (args_left
))
1786 vals
[argnum
++] = Feval (Fcar (args_left
));
1787 args_left
= Fcdr (args_left
);
1788 gcpro3
.nvars
= argnum
;
1791 backtrace
.args
= vals
;
1792 backtrace
.nargs
= XINT (numargs
);
1794 val
= (*XSUBR (fun
)->function
) (XINT (numargs
), vals
);
1799 GCPRO3 (args_left
, fun
, fun
);
1800 gcpro3
.var
= argvals
;
1803 maxargs
= XSUBR (fun
)->max_args
;
1804 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
1806 argvals
[i
] = Feval (Fcar (args_left
));
1812 backtrace
.args
= argvals
;
1813 backtrace
.nargs
= XINT (numargs
);
1818 val
= (*XSUBR (fun
)->function
) ();
1821 val
= (*XSUBR (fun
)->function
) (argvals
[0]);
1824 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1]);
1827 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1831 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1],
1832 argvals
[2], argvals
[3]);
1835 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1836 argvals
[3], argvals
[4]);
1839 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1840 argvals
[3], argvals
[4], argvals
[5]);
1843 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1844 argvals
[3], argvals
[4], argvals
[5],
1849 val
= (*XSUBR (fun
)->function
) (argvals
[0], argvals
[1], argvals
[2],
1850 argvals
[3], argvals
[4], argvals
[5],
1851 argvals
[6], argvals
[7]);
1855 /* Someone has created a subr that takes more arguments than
1856 is supported by this code. We need to either rewrite the
1857 subr to use a different argument protocol, or add more
1858 cases to this switch. */
1862 if (COMPILEDP (fun
))
1863 val
= apply_lambda (fun
, original_args
, 1);
1867 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1868 funcar
= Fcar (fun
);
1869 if (!SYMBOLP (funcar
))
1870 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1871 if (EQ (funcar
, Qautoload
))
1873 do_autoload (fun
, original_fun
);
1876 if (EQ (funcar
, Qmacro
))
1877 val
= Feval (apply1 (Fcdr (fun
), original_args
));
1878 else if (EQ (funcar
, Qlambda
))
1879 val
= apply_lambda (fun
, original_args
, 1);
1880 else if (EQ (funcar
, Qmocklisp
))
1881 val
= ml_apply (fun
, original_args
);
1883 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
1886 if (!EQ (Vmocklisp_arguments
, Qt
))
1889 XSETFASTINT (val
, 0);
1890 else if (EQ (val
, Qt
))
1891 XSETFASTINT (val
, 1);
1894 if (backtrace
.debug_on_exit
)
1895 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
1896 backtrace_list
= backtrace
.next
;
1900 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
1901 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1902 Then return the value FUNCTION returns.\n\
1903 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1908 register int i
, numargs
;
1909 register Lisp_Object spread_arg
;
1910 register Lisp_Object
*funcall_args
;
1912 struct gcpro gcpro1
;
1916 spread_arg
= args
[nargs
- 1];
1917 CHECK_LIST (spread_arg
, nargs
);
1919 numargs
= XINT (Flength (spread_arg
));
1922 return Ffuncall (nargs
- 1, args
);
1923 else if (numargs
== 1)
1925 args
[nargs
- 1] = XCAR (spread_arg
);
1926 return Ffuncall (nargs
, args
);
1929 numargs
+= nargs
- 2;
1931 fun
= indirect_function (fun
);
1932 if (EQ (fun
, Qunbound
))
1934 /* Let funcall get the error */
1941 if (numargs
< XSUBR (fun
)->min_args
1942 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
1943 goto funcall
; /* Let funcall get the error */
1944 else if (XSUBR (fun
)->max_args
> numargs
)
1946 /* Avoid making funcall cons up a yet another new vector of arguments
1947 by explicitly supplying nil's for optional values */
1948 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
1949 * sizeof (Lisp_Object
));
1950 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
1951 funcall_args
[++i
] = Qnil
;
1952 GCPRO1 (*funcall_args
);
1953 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
1957 /* We add 1 to numargs because funcall_args includes the
1958 function itself as well as its arguments. */
1961 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
1962 * sizeof (Lisp_Object
));
1963 GCPRO1 (*funcall_args
);
1964 gcpro1
.nvars
= 1 + numargs
;
1967 bcopy (args
, funcall_args
, nargs
* sizeof (Lisp_Object
));
1968 /* Spread the last arg we got. Its first element goes in
1969 the slot that it used to occupy, hence this value of I. */
1971 while (!NILP (spread_arg
))
1973 funcall_args
[i
++] = XCAR (spread_arg
);
1974 spread_arg
= XCDR (spread_arg
);
1977 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
1980 /* Run hook variables in various ways. */
1982 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
1984 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 1, MANY
, 0,
1985 "Run each hook in HOOKS. Major mode functions use this.\n\
1986 Each argument should be a symbol, a hook variable.\n\
1987 These symbols are processed in the order specified.\n\
1988 If a hook symbol has a non-nil value, that value may be a function\n\
1989 or a list of functions to be called to run the hook.\n\
1990 If the value is a function, it is called with no arguments.\n\
1991 If it is a list, the elements are called, in order, with no arguments.\n\
1993 To make a hook variable buffer-local, use `make-local-hook',\n\
1994 not `make-local-variable'.")
1999 Lisp_Object hook
[1];
2002 for (i
= 0; i
< nargs
; i
++)
2005 run_hook_with_args (1, hook
, to_completion
);
2011 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2012 Srun_hook_with_args
, 1, MANY
, 0,
2013 "Run HOOK with the specified arguments ARGS.\n\
2014 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2015 value, that value may be a function or a list of functions to be\n\
2016 called to run the hook. If the value is a function, it is called with\n\
2017 the given arguments and its return value is returned. If it is a list\n\
2018 of functions, those functions are called, in order,\n\
2019 with the given arguments ARGS.\n\
2020 It is best not to depend on the value return by `run-hook-with-args',\n\
2021 as that may change.\n\
2023 To make a hook variable buffer-local, use `make-local-hook',\n\
2024 not `make-local-variable'.")
2029 return run_hook_with_args (nargs
, args
, to_completion
);
2032 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2033 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2034 "Run HOOK with the specified arguments ARGS.\n\
2035 HOOK should be a symbol, a hook variable. Its value should\n\
2036 be a list of functions. We call those functions, one by one,\n\
2037 passing arguments ARGS to each of them, until one of them\n\
2038 returns a non-nil value. Then we return that value.\n\
2039 If all the functions return nil, we return nil.\n\
2041 To make a hook variable buffer-local, use `make-local-hook',\n\
2042 not `make-local-variable'.")
2047 return run_hook_with_args (nargs
, args
, until_success
);
2050 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2051 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2052 "Run HOOK with the specified arguments ARGS.\n\
2053 HOOK should be a symbol, a hook variable. Its value should\n\
2054 be a list of functions. We call those functions, one by one,\n\
2055 passing arguments ARGS to each of them, until one of them\n\
2056 returns nil. Then we return nil.\n\
2057 If all the functions return non-nil, we return non-nil.\n\
2059 To make a hook variable buffer-local, use `make-local-hook',\n\
2060 not `make-local-variable'.")
2065 return run_hook_with_args (nargs
, args
, until_failure
);
2068 /* ARGS[0] should be a hook symbol.
2069 Call each of the functions in the hook value, passing each of them
2070 as arguments all the rest of ARGS (all NARGS - 1 elements).
2071 COND specifies a condition to test after each call
2072 to decide whether to stop.
2073 The caller (or its caller, etc) must gcpro all of ARGS,
2074 except that it isn't necessary to gcpro ARGS[0]. */
2077 run_hook_with_args (nargs
, args
, cond
)
2080 enum run_hooks_condition cond
;
2082 Lisp_Object sym
, val
, ret
;
2083 Lisp_Object globals
;
2084 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2086 /* If we are dying or still initializing,
2087 don't do anything--it would probably crash if we tried. */
2088 if (NILP (Vrun_hooks
))
2092 val
= find_symbol_value (sym
);
2093 ret
= (cond
== until_failure
? Qt
: Qnil
);
2095 if (EQ (val
, Qunbound
) || NILP (val
))
2097 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2100 return Ffuncall (nargs
, args
);
2105 GCPRO3 (sym
, val
, globals
);
2108 CONSP (val
) && ((cond
== to_completion
)
2109 || (cond
== until_success
? NILP (ret
)
2113 if (EQ (XCAR (val
), Qt
))
2115 /* t indicates this hook has a local binding;
2116 it means to run the global binding too. */
2118 for (globals
= Fdefault_value (sym
);
2119 CONSP (globals
) && ((cond
== to_completion
)
2120 || (cond
== until_success
? NILP (ret
)
2122 globals
= XCDR (globals
))
2124 args
[0] = XCAR (globals
);
2125 /* In a global value, t should not occur. If it does, we
2126 must ignore it to avoid an endless loop. */
2127 if (!EQ (args
[0], Qt
))
2128 ret
= Ffuncall (nargs
, args
);
2133 args
[0] = XCAR (val
);
2134 ret
= Ffuncall (nargs
, args
);
2143 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2144 present value of that symbol.
2145 Call each element of FUNLIST,
2146 passing each of them the rest of ARGS.
2147 The caller (or its caller, etc) must gcpro all of ARGS,
2148 except that it isn't necessary to gcpro ARGS[0]. */
2151 run_hook_list_with_args (funlist
, nargs
, args
)
2152 Lisp_Object funlist
;
2158 Lisp_Object globals
;
2159 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2163 GCPRO3 (sym
, val
, globals
);
2165 for (val
= funlist
; CONSP (val
); val
= XCDR (val
))
2167 if (EQ (XCAR (val
), Qt
))
2169 /* t indicates this hook has a local binding;
2170 it means to run the global binding too. */
2172 for (globals
= Fdefault_value (sym
);
2174 globals
= XCDR (globals
))
2176 args
[0] = XCAR (globals
);
2177 /* In a global value, t should not occur. If it does, we
2178 must ignore it to avoid an endless loop. */
2179 if (!EQ (args
[0], Qt
))
2180 Ffuncall (nargs
, args
);
2185 args
[0] = XCAR (val
);
2186 Ffuncall (nargs
, args
);
2193 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2196 run_hook_with_args_2 (hook
, arg1
, arg2
)
2197 Lisp_Object hook
, arg1
, arg2
;
2199 Lisp_Object temp
[3];
2204 Frun_hook_with_args (3, temp
);
2207 /* Apply fn to arg */
2210 Lisp_Object fn
, arg
;
2212 struct gcpro gcpro1
;
2216 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2220 Lisp_Object args
[2];
2224 RETURN_UNGCPRO (Fapply (2, args
));
2226 #else /* not NO_ARG_ARRAY */
2227 RETURN_UNGCPRO (Fapply (2, &fn
));
2228 #endif /* not NO_ARG_ARRAY */
2231 /* Call function fn on no arguments */
2236 struct gcpro gcpro1
;
2239 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2242 /* Call function fn with 1 argument arg1 */
2246 Lisp_Object fn
, arg1
;
2248 struct gcpro gcpro1
;
2250 Lisp_Object args
[2];
2256 RETURN_UNGCPRO (Ffuncall (2, args
));
2257 #else /* not NO_ARG_ARRAY */
2260 RETURN_UNGCPRO (Ffuncall (2, &fn
));
2261 #endif /* not NO_ARG_ARRAY */
2264 /* Call function fn with 2 arguments arg1, arg2 */
2267 call2 (fn
, arg1
, arg2
)
2268 Lisp_Object fn
, arg1
, arg2
;
2270 struct gcpro gcpro1
;
2272 Lisp_Object args
[3];
2278 RETURN_UNGCPRO (Ffuncall (3, args
));
2279 #else /* not NO_ARG_ARRAY */
2282 RETURN_UNGCPRO (Ffuncall (3, &fn
));
2283 #endif /* not NO_ARG_ARRAY */
2286 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2289 call3 (fn
, arg1
, arg2
, arg3
)
2290 Lisp_Object fn
, arg1
, arg2
, arg3
;
2292 struct gcpro gcpro1
;
2294 Lisp_Object args
[4];
2301 RETURN_UNGCPRO (Ffuncall (4, args
));
2302 #else /* not NO_ARG_ARRAY */
2305 RETURN_UNGCPRO (Ffuncall (4, &fn
));
2306 #endif /* not NO_ARG_ARRAY */
2309 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2312 call4 (fn
, arg1
, arg2
, arg3
, arg4
)
2313 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
;
2315 struct gcpro gcpro1
;
2317 Lisp_Object args
[5];
2325 RETURN_UNGCPRO (Ffuncall (5, args
));
2326 #else /* not NO_ARG_ARRAY */
2329 RETURN_UNGCPRO (Ffuncall (5, &fn
));
2330 #endif /* not NO_ARG_ARRAY */
2333 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2336 call5 (fn
, arg1
, arg2
, arg3
, arg4
, arg5
)
2337 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
, arg5
;
2339 struct gcpro gcpro1
;
2341 Lisp_Object args
[6];
2350 RETURN_UNGCPRO (Ffuncall (6, args
));
2351 #else /* not NO_ARG_ARRAY */
2354 RETURN_UNGCPRO (Ffuncall (6, &fn
));
2355 #endif /* not NO_ARG_ARRAY */
2358 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2361 call6 (fn
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
)
2362 Lisp_Object fn
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
;
2364 struct gcpro gcpro1
;
2366 Lisp_Object args
[7];
2376 RETURN_UNGCPRO (Ffuncall (7, args
));
2377 #else /* not NO_ARG_ARRAY */
2380 RETURN_UNGCPRO (Ffuncall (7, &fn
));
2381 #endif /* not NO_ARG_ARRAY */
2384 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2385 "Call first argument as a function, passing remaining arguments to it.\n\
2386 Return the value that function returns.\n\
2387 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2394 int numargs
= nargs
- 1;
2395 Lisp_Object lisp_numargs
;
2397 struct backtrace backtrace
;
2398 register Lisp_Object
*internal_args
;
2402 if (consing_since_gc
> gc_cons_threshold
)
2403 Fgarbage_collect ();
2405 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2407 if (max_lisp_eval_depth
< 100)
2408 max_lisp_eval_depth
= 100;
2409 if (lisp_eval_depth
> max_lisp_eval_depth
)
2410 error ("Lisp nesting exceeds max-lisp-eval-depth");
2413 backtrace
.next
= backtrace_list
;
2414 backtrace_list
= &backtrace
;
2415 backtrace
.function
= &args
[0];
2416 backtrace
.args
= &args
[1];
2417 backtrace
.nargs
= nargs
- 1;
2418 backtrace
.evalargs
= 0;
2419 backtrace
.debug_on_exit
= 0;
2421 if (debug_on_next_call
)
2422 do_debug_on_call (Qlambda
);
2428 fun
= Findirect_function (fun
);
2432 if (numargs
< XSUBR (fun
)->min_args
2433 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2435 XSETFASTINT (lisp_numargs
, numargs
);
2436 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (lisp_numargs
, Qnil
)));
2439 if (XSUBR (fun
)->max_args
== UNEVALLED
)
2440 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2442 if (XSUBR (fun
)->max_args
== MANY
)
2444 val
= (*XSUBR (fun
)->function
) (numargs
, args
+ 1);
2448 if (XSUBR (fun
)->max_args
> numargs
)
2450 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2451 bcopy (args
+ 1, internal_args
, numargs
* sizeof (Lisp_Object
));
2452 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2453 internal_args
[i
] = Qnil
;
2456 internal_args
= args
+ 1;
2457 switch (XSUBR (fun
)->max_args
)
2460 val
= (*XSUBR (fun
)->function
) ();
2463 val
= (*XSUBR (fun
)->function
) (internal_args
[0]);
2466 val
= (*XSUBR (fun
)->function
) (internal_args
[0],
2470 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2474 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2479 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2480 internal_args
[2], internal_args
[3],
2484 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2485 internal_args
[2], internal_args
[3],
2486 internal_args
[4], internal_args
[5]);
2489 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2490 internal_args
[2], internal_args
[3],
2491 internal_args
[4], internal_args
[5],
2496 val
= (*XSUBR (fun
)->function
) (internal_args
[0], internal_args
[1],
2497 internal_args
[2], internal_args
[3],
2498 internal_args
[4], internal_args
[5],
2499 internal_args
[6], internal_args
[7]);
2504 /* If a subr takes more than 8 arguments without using MANY
2505 or UNEVALLED, we need to extend this function to support it.
2506 Until this is done, there is no way to call the function. */
2510 if (COMPILEDP (fun
))
2511 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2515 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2516 funcar
= Fcar (fun
);
2517 if (!SYMBOLP (funcar
))
2518 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2519 if (EQ (funcar
, Qlambda
))
2520 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2521 else if (EQ (funcar
, Qmocklisp
))
2522 val
= ml_apply (fun
, Flist (numargs
, args
+ 1));
2523 else if (EQ (funcar
, Qautoload
))
2525 do_autoload (fun
, args
[0]);
2529 return Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2533 if (backtrace
.debug_on_exit
)
2534 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2535 backtrace_list
= backtrace
.next
;
2540 apply_lambda (fun
, args
, eval_flag
)
2541 Lisp_Object fun
, args
;
2544 Lisp_Object args_left
;
2545 Lisp_Object numargs
;
2546 register Lisp_Object
*arg_vector
;
2547 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2549 register Lisp_Object tem
;
2551 numargs
= Flength (args
);
2552 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
2555 GCPRO3 (*arg_vector
, args_left
, fun
);
2558 for (i
= 0; i
< XINT (numargs
);)
2560 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2561 if (eval_flag
) tem
= Feval (tem
);
2562 arg_vector
[i
++] = tem
;
2570 backtrace_list
->args
= arg_vector
;
2571 backtrace_list
->nargs
= i
;
2573 backtrace_list
->evalargs
= 0;
2574 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
2576 /* Do the debug-on-exit now, while arg_vector still exists. */
2577 if (backtrace_list
->debug_on_exit
)
2578 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2579 /* Don't do it again when we return to eval. */
2580 backtrace_list
->debug_on_exit
= 0;
2584 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2585 and return the result of evaluation.
2586 FUN must be either a lambda-expression or a compiled-code object. */
2589 funcall_lambda (fun
, nargs
, arg_vector
)
2592 register Lisp_Object
*arg_vector
;
2594 Lisp_Object val
, tem
;
2595 register Lisp_Object syms_left
;
2596 Lisp_Object numargs
;
2597 register Lisp_Object next
;
2598 int count
= specpdl_ptr
- specpdl
;
2600 int optional
= 0, rest
= 0;
2602 specbind (Qmocklisp_arguments
, Qt
); /* t means NOT mocklisp! */
2604 XSETFASTINT (numargs
, nargs
);
2607 syms_left
= Fcar (Fcdr (fun
));
2608 else if (COMPILEDP (fun
))
2609 syms_left
= XVECTOR (fun
)->contents
[COMPILED_ARGLIST
];
2613 for (; !NILP (syms_left
); syms_left
= Fcdr (syms_left
))
2616 next
= Fcar (syms_left
);
2617 while (!SYMBOLP (next
))
2618 next
= Fsignal (Qinvalid_function
, Fcons (fun
, Qnil
));
2619 if (EQ (next
, Qand_rest
))
2621 else if (EQ (next
, Qand_optional
))
2625 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
2630 tem
= arg_vector
[i
++];
2631 specbind (next
, tem
);
2634 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2636 specbind (next
, Qnil
);
2640 return Fsignal (Qwrong_number_of_arguments
, Fcons (fun
, Fcons (numargs
, Qnil
)));
2643 val
= Fprogn (Fcdr (Fcdr (fun
)));
2646 /* If we have not actually read the bytecode string
2647 and constants vector yet, fetch them from the file. */
2648 if (CONSP (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
]))
2649 Ffetch_bytecode (fun
);
2650 val
= Fbyte_code (XVECTOR (fun
)->contents
[COMPILED_BYTECODE
],
2651 XVECTOR (fun
)->contents
[COMPILED_CONSTANTS
],
2652 XVECTOR (fun
)->contents
[COMPILED_STACK_DEPTH
]);
2654 return unbind_to (count
, val
);
2657 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
2659 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2665 if (COMPILEDP (object
)
2666 && CONSP (XVECTOR (object
)->contents
[COMPILED_BYTECODE
]))
2668 tem
= read_doc_string (XVECTOR (object
)->contents
[COMPILED_BYTECODE
]);
2670 error ("invalid byte code");
2671 XVECTOR (object
)->contents
[COMPILED_BYTECODE
] = XCAR (tem
);
2672 XVECTOR (object
)->contents
[COMPILED_CONSTANTS
] = XCDR (tem
);
2680 register int count
= specpdl_ptr
- specpdl
;
2681 if (specpdl_size
>= max_specpdl_size
)
2683 if (max_specpdl_size
< 400)
2684 max_specpdl_size
= 400;
2685 if (specpdl_size
>= max_specpdl_size
)
2687 if (!NILP (Vdebug_on_error
))
2688 /* Leave room for some specpdl in the debugger. */
2689 max_specpdl_size
= specpdl_size
+ 100;
2691 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil
));
2695 if (specpdl_size
> max_specpdl_size
)
2696 specpdl_size
= max_specpdl_size
;
2697 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
2698 specpdl_ptr
= specpdl
+ count
;
2702 specbind (symbol
, value
)
2703 Lisp_Object symbol
, value
;
2707 CHECK_SYMBOL (symbol
, 0);
2709 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2711 specpdl_ptr
->symbol
= symbol
;
2712 specpdl_ptr
->func
= 0;
2713 specpdl_ptr
->old_value
= ovalue
= find_symbol_value (symbol
);
2715 if (BUFFER_OBJFWDP (ovalue
) || KBOARD_OBJFWDP (ovalue
))
2716 store_symval_forwarding (symbol
, ovalue
, value
);
2718 set_internal (symbol
, value
, 1);
2722 record_unwind_protect (function
, arg
)
2723 Lisp_Object (*function
) P_ ((Lisp_Object
));
2726 if (specpdl_ptr
== specpdl
+ specpdl_size
)
2728 specpdl_ptr
->func
= function
;
2729 specpdl_ptr
->symbol
= Qnil
;
2730 specpdl_ptr
->old_value
= arg
;
2735 unbind_to (count
, value
)
2739 int quitf
= !NILP (Vquit_flag
);
2740 struct gcpro gcpro1
;
2746 while (specpdl_ptr
!= specpdl
+ count
)
2749 if (specpdl_ptr
->func
!= 0)
2750 (*specpdl_ptr
->func
) (specpdl_ptr
->old_value
);
2751 /* Note that a "binding" of nil is really an unwind protect,
2752 so in that case the "old value" is a list of forms to evaluate. */
2753 else if (NILP (specpdl_ptr
->symbol
))
2754 Fprogn (specpdl_ptr
->old_value
);
2756 set_internal (specpdl_ptr
->symbol
, specpdl_ptr
->old_value
, 1);
2758 if (NILP (Vquit_flag
) && quitf
) Vquit_flag
= Qt
;
2767 /* Get the value of symbol's global binding, even if that binding
2768 is not now dynamically visible. */
2771 top_level_value (symbol
)
2774 register struct specbinding
*ptr
= specpdl
;
2776 CHECK_SYMBOL (symbol
, 0);
2777 for (; ptr
!= specpdl_ptr
; ptr
++)
2779 if (EQ (ptr
->symbol
, symbol
))
2780 return ptr
->old_value
;
2782 return Fsymbol_value (symbol
);
2786 top_level_set (symbol
, newval
)
2787 Lisp_Object symbol
, newval
;
2789 register struct specbinding
*ptr
= specpdl
;
2791 CHECK_SYMBOL (symbol
, 0);
2792 for (; ptr
!= specpdl_ptr
; ptr
++)
2794 if (EQ (ptr
->symbol
, symbol
))
2796 ptr
->old_value
= newval
;
2800 return Fset (symbol
, newval
);
2805 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
2806 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2807 The debugger is entered when that frame exits, if the flag is non-nil.")
2809 Lisp_Object level
, flag
;
2811 register struct backtrace
*backlist
= backtrace_list
;
2814 CHECK_NUMBER (level
, 0);
2816 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
2818 backlist
= backlist
->next
;
2822 backlist
->debug_on_exit
= !NILP (flag
);
2827 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
2828 "Print a trace of Lisp function calls currently active.\n\
2829 Output stream used is value of `standard-output'.")
2832 register struct backtrace
*backlist
= backtrace_list
;
2836 extern Lisp_Object Vprint_level
;
2837 struct gcpro gcpro1
;
2839 XSETFASTINT (Vprint_level
, 3);
2846 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
2847 if (backlist
->nargs
== UNEVALLED
)
2849 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
2850 write_string ("\n", -1);
2854 tem
= *backlist
->function
;
2855 Fprin1 (tem
, Qnil
); /* This can QUIT */
2856 write_string ("(", -1);
2857 if (backlist
->nargs
== MANY
)
2859 for (tail
= *backlist
->args
, i
= 0;
2861 tail
= Fcdr (tail
), i
++)
2863 if (i
) write_string (" ", -1);
2864 Fprin1 (Fcar (tail
), Qnil
);
2869 for (i
= 0; i
< backlist
->nargs
; i
++)
2871 if (i
) write_string (" ", -1);
2872 Fprin1 (backlist
->args
[i
], Qnil
);
2875 write_string (")\n", -1);
2877 backlist
= backlist
->next
;
2880 Vprint_level
= Qnil
;
2885 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, "",
2886 "Return the function and arguments NFRAMES up from current execution point.\n\
2887 If that frame has not evaluated the arguments yet (or is a special form),\n\
2888 the value is (nil FUNCTION ARG-FORMS...).\n\
2889 If that frame has evaluated its arguments and called its function already,\n\
2890 the value is (t FUNCTION ARG-VALUES...).\n\
2891 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2892 FUNCTION is whatever was supplied as car of evaluated list,\n\
2893 or a lambda expression for macro calls.\n\
2894 If NFRAMES is more than the number of frames, the value is nil.")
2896 Lisp_Object nframes
;
2898 register struct backtrace
*backlist
= backtrace_list
;
2902 CHECK_NATNUM (nframes
, 0);
2904 /* Find the frame requested. */
2905 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
2906 backlist
= backlist
->next
;
2910 if (backlist
->nargs
== UNEVALLED
)
2911 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
2914 if (backlist
->nargs
== MANY
)
2915 tem
= *backlist
->args
;
2917 tem
= Flist (backlist
->nargs
, backlist
->args
);
2919 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
2926 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
2927 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
2928 If Lisp code tries to make more than this many at once,\n\
2929 an error is signaled.");
2931 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
2932 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
2933 This limit is to catch infinite recursions for you before they cause\n\
2934 actual stack overflow in C, which would be fatal for Emacs.\n\
2935 You can safely make it considerably larger than its default value,\n\
2936 if that proves inconveniently small.");
2938 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
2939 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2940 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2943 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
2944 "Non-nil inhibits C-g quitting from happening immediately.\n\
2945 Note that `quit-flag' will still be set by typing C-g,\n\
2946 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
2947 To prevent this happening, set `quit-flag' to nil\n\
2948 before making `inhibit-quit' nil.");
2949 Vinhibit_quit
= Qnil
;
2951 Qinhibit_quit
= intern ("inhibit-quit");
2952 staticpro (&Qinhibit_quit
);
2954 Qautoload
= intern ("autoload");
2955 staticpro (&Qautoload
);
2957 Qdebug_on_error
= intern ("debug-on-error");
2958 staticpro (&Qdebug_on_error
);
2960 Qmacro
= intern ("macro");
2961 staticpro (&Qmacro
);
2963 /* Note that the process handling also uses Qexit, but we don't want
2964 to staticpro it twice, so we just do it here. */
2965 Qexit
= intern ("exit");
2968 Qinteractive
= intern ("interactive");
2969 staticpro (&Qinteractive
);
2971 Qcommandp
= intern ("commandp");
2972 staticpro (&Qcommandp
);
2974 Qdefun
= intern ("defun");
2975 staticpro (&Qdefun
);
2977 Qand_rest
= intern ("&rest");
2978 staticpro (&Qand_rest
);
2980 Qand_optional
= intern ("&optional");
2981 staticpro (&Qand_optional
);
2983 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error
,
2984 "*Non-nil means automatically display a backtrace buffer\n\
2985 after any error that is handled by the editor command loop.\n\
2986 If the value is a list, an error only means to display a backtrace\n\
2987 if one of its condition symbols appears in the list.");
2988 Vstack_trace_on_error
= Qnil
;
2990 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error
,
2991 "*Non-nil means enter debugger if an error is signaled.\n\
2992 Does not apply to errors handled by `condition-case'.\n\
2993 If the value is a list, an error only means to enter the debugger\n\
2994 if one of its condition symbols appears in the list.\n\
2995 See also variable `debug-on-quit'.");
2996 Vdebug_on_error
= Qnil
;
2998 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors
,
2999 "*List of errors for which the debugger should not be called.\n\
3000 Each element may be a condition-name or a regexp that matches error messages.\n\
3001 If any element applies to a given error, that error skips the debugger\n\
3002 and just returns to top level.\n\
3003 This overrides the variable `debug-on-error'.\n\
3004 It does not apply to errors handled by `condition-case'.");
3005 Vdebug_ignored_errors
= Qnil
;
3007 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
3008 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3009 Does not apply if quit is handled by a `condition-case'.");
3012 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
3013 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3015 DEFVAR_LISP ("debugger", &Vdebugger
,
3016 "Function to call to invoke debugger.\n\
3017 If due to frame exit, args are `exit' and the value being returned;\n\
3018 this function's value will be returned instead of that.\n\
3019 If due to error, args are `error' and a list of the args to `signal'.\n\
3020 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3021 If due to `eval' entry, one arg, t.");
3024 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function
,
3025 "If non-nil, this is a function for `signal' to call.\n\
3026 It receives the same arguments that `signal' was given.\n\
3027 The Edebug package uses this to regain control.");
3028 Vsignal_hook_function
= Qnil
;
3030 Qmocklisp_arguments
= intern ("mocklisp-arguments");
3031 staticpro (&Qmocklisp_arguments
);
3032 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments
,
3033 "While in a mocklisp function, the list of its unevaluated args.");
3034 Vmocklisp_arguments
= Qt
;
3036 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal
,
3037 "*Non-nil means call the debugger regardless of condition handlers.\n\
3038 Note that `debug-on-error', `debug-on-quit' and friends\n\
3039 still determine whether to handle the particular condition.");
3040 Vdebug_on_signal
= Qnil
;
3042 Vrun_hooks
= intern ("run-hooks");
3043 staticpro (&Vrun_hooks
);
3045 staticpro (&Vautoload_queue
);
3046 Vautoload_queue
= Qnil
;
3057 defsubr (&Sfunction
);
3059 defsubr (&Sdefmacro
);
3061 defsubr (&Sdefconst
);
3062 defsubr (&Suser_variable_p
);
3066 defsubr (&Smacroexpand
);
3069 defsubr (&Sunwind_protect
);
3070 defsubr (&Scondition_case
);
3072 defsubr (&Sinteractive_p
);
3073 defsubr (&Scommandp
);
3074 defsubr (&Sautoload
);
3077 defsubr (&Sfuncall
);
3078 defsubr (&Srun_hooks
);
3079 defsubr (&Srun_hook_with_args
);
3080 defsubr (&Srun_hook_with_args_until_success
);
3081 defsubr (&Srun_hook_with_args_until_failure
);
3082 defsubr (&Sfetch_bytecode
);
3083 defsubr (&Sbacktrace_debug
);
3084 defsubr (&Sbacktrace
);
3085 defsubr (&Sbacktrace_frame
);