1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
35 /* This definition is duplicated in alloc.c and keyboard.c */
36 /* Putting it in lisp.h makes cc bomb out! */
40 struct backtrace
*next
;
41 Lisp_Object
*function
;
42 Lisp_Object
*args
; /* Points to vector of args. */
43 int nargs
; /* Length of vector.
44 If nargs is UNEVALLED, args points to slot holding
45 list of unevalled args */
47 /* Nonzero means call value of debugger when done with this operation. */
51 struct backtrace
*backtrace_list
;
53 struct catchtag
*catchlist
;
56 /* Count levels of GCPRO to detect failure to UNGCPRO. */
60 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
61 Lisp_Object Qinhibit_quit
, Vinhibit_quit
, Vquit_flag
;
62 Lisp_Object Qand_rest
, Qand_optional
;
63 Lisp_Object Qdebug_on_error
;
66 extern Lisp_Object Qinteractive_form
;
68 /* This holds either the symbol `run-hooks' or nil.
69 It is nil at an early stage of startup, and when Emacs
72 Lisp_Object Vrun_hooks
;
74 /* Non-nil means record all fset's and provide's, to be undone
75 if the file being autoloaded is not fully loaded.
76 They are recorded by being consed onto the front of Vautoload_queue:
77 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
79 Lisp_Object Vautoload_queue
;
81 /* Current number of specbindings allocated in specpdl. */
85 /* Pointer to beginning of specpdl. */
87 struct specbinding
*specpdl
;
89 /* Pointer to first unused element in specpdl. */
91 struct specbinding
*specpdl_ptr
;
93 /* Maximum size allowed for specpdl allocation */
95 EMACS_INT max_specpdl_size
;
97 /* Depth in Lisp evaluations and function calls. */
101 /* Maximum allowed depth in Lisp evaluations and function calls. */
103 EMACS_INT max_lisp_eval_depth
;
105 /* Nonzero means enter debugger before next function call */
107 int debug_on_next_call
;
109 /* Non-zero means debugger may continue. This is zero when the
110 debugger is called during redisplay, where it might not be safe to
111 continue the interrupted redisplay. */
113 int debugger_may_continue
;
115 /* List of conditions (non-nil atom means all) which cause a backtrace
116 if an error is handled by the command loop's error handler. */
118 Lisp_Object Vstack_trace_on_error
;
120 /* List of conditions (non-nil atom means all) which enter the debugger
121 if an error is handled by the command loop's error handler. */
123 Lisp_Object Vdebug_on_error
;
125 /* List of conditions and regexps specifying error messages which
126 do not enter the debugger even if Vdebug_on_error says they should. */
128 Lisp_Object Vdebug_ignored_errors
;
130 /* Non-nil means call the debugger even if the error will be handled. */
132 Lisp_Object Vdebug_on_signal
;
134 /* Hook for edebug to use. */
136 Lisp_Object Vsignal_hook_function
;
138 /* Nonzero means enter debugger if a quit signal
139 is handled by the command loop's error handler. */
143 /* The value of num_nonmacro_input_events as of the last time we
144 started to enter the debugger. If we decide to enter the debugger
145 again when this is still equal to num_nonmacro_input_events, then we
146 know that the debugger itself has an error, and we should just
147 signal the error instead of entering an infinite loop of debugger
150 int when_entered_debugger
;
152 Lisp_Object Vdebugger
;
154 /* The function from which the last `signal' was called. Set in
157 Lisp_Object Vsignaling_function
;
159 /* Set to non-zero while processing X events. Checked in Feval to
160 make sure the Lisp interpreter isn't called from a signal handler,
161 which is unsafe because the interpreter isn't reentrant. */
165 /* Function to process declarations in defmacro forms. */
167 Lisp_Object Vmacro_declaration_function
;
169 extern Lisp_Object Qrisky_local_variable
;
171 extern Lisp_Object Qfunction
;
173 static Lisp_Object
funcall_lambda (Lisp_Object
, int, Lisp_Object
*);
174 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
177 init_eval_once (void)
180 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
181 specpdl_ptr
= specpdl
;
182 /* Don't forget to update docs (lispref node "Local Variables"). */
183 max_specpdl_size
= 1000;
184 max_lisp_eval_depth
= 500;
192 specpdl_ptr
= specpdl
;
197 debug_on_next_call
= 0;
202 /* This is less than the initial value of num_nonmacro_input_events. */
203 when_entered_debugger
= -1;
206 /* unwind-protect function used by call_debugger. */
209 restore_stack_limits (Lisp_Object data
)
211 max_specpdl_size
= XINT (XCAR (data
));
212 max_lisp_eval_depth
= XINT (XCDR (data
));
216 /* Call the Lisp debugger, giving it argument ARG. */
219 call_debugger (Lisp_Object arg
)
221 int debug_while_redisplaying
;
222 int count
= SPECPDL_INDEX ();
224 int old_max
= max_specpdl_size
;
226 /* Temporarily bump up the stack limits,
227 so the debugger won't run out of stack. */
229 max_specpdl_size
+= 1;
230 record_unwind_protect (restore_stack_limits
,
231 Fcons (make_number (old_max
),
232 make_number (max_lisp_eval_depth
)));
233 max_specpdl_size
= old_max
;
235 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
236 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
238 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
239 max_specpdl_size
= SPECPDL_INDEX () + 100;
241 #ifdef HAVE_WINDOW_SYSTEM
242 if (display_hourglass_p
)
246 debug_on_next_call
= 0;
247 when_entered_debugger
= num_nonmacro_input_events
;
249 /* Resetting redisplaying_p to 0 makes sure that debug output is
250 displayed if the debugger is invoked during redisplay. */
251 debug_while_redisplaying
= redisplaying_p
;
253 specbind (intern ("debugger-may-continue"),
254 debug_while_redisplaying
? Qnil
: Qt
);
255 specbind (Qinhibit_redisplay
, Qnil
);
256 specbind (Qdebug_on_error
, Qnil
);
258 #if 0 /* Binding this prevents execution of Lisp code during
259 redisplay, which necessarily leads to display problems. */
260 specbind (Qinhibit_eval_during_redisplay
, Qt
);
263 val
= apply1 (Vdebugger
, arg
);
265 /* Interrupting redisplay and resuming it later is not safe under
266 all circumstances. So, when the debugger returns, abort the
267 interrupted redisplay by going back to the top-level. */
268 if (debug_while_redisplaying
)
271 return unbind_to (count
, val
);
275 do_debug_on_call (Lisp_Object code
)
277 debug_on_next_call
= 0;
278 backtrace_list
->debug_on_exit
= 1;
279 call_debugger (Fcons (code
, Qnil
));
282 /* NOTE!!! Every function that can call EVAL must protect its args
283 and temporaries from garbage collection while it needs them.
284 The definition of `For' shows what you have to do. */
286 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
287 doc
: /* Eval args until one of them yields non-nil, then return that value.
288 The remaining args are not evalled at all.
289 If all args return nil, return nil.
290 usage: (or CONDITIONS...) */)
293 register Lisp_Object val
= Qnil
;
300 val
= Feval (XCAR (args
));
310 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
311 doc
: /* Eval args until one of them yields nil, then return nil.
312 The remaining args are not evalled at all.
313 If no arg yields nil, return the last arg's value.
314 usage: (and CONDITIONS...) */)
317 register Lisp_Object val
= Qt
;
324 val
= Feval (XCAR (args
));
334 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
335 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
336 Returns the value of THEN or the value of the last of the ELSE's.
337 THEN must be one expression, but ELSE... can be zero or more expressions.
338 If COND yields nil, and there are no ELSE's, the value is nil.
339 usage: (if COND THEN ELSE...) */)
342 register Lisp_Object cond
;
346 cond
= Feval (Fcar (args
));
350 return Feval (Fcar (Fcdr (args
)));
351 return Fprogn (Fcdr (Fcdr (args
)));
354 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
355 doc
: /* Try each clause until one succeeds.
356 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
357 and, if the value is non-nil, this clause succeeds:
358 then the expressions in BODY are evaluated and the last one's
359 value is the value of the cond-form.
360 If no clause succeeds, cond returns nil.
361 If a clause has one element, as in (CONDITION),
362 CONDITION's value if non-nil is returned from the cond-form.
363 usage: (cond CLAUSES...) */)
366 register Lisp_Object clause
, val
;
373 clause
= Fcar (args
);
374 val
= Feval (Fcar (clause
));
377 if (!EQ (XCDR (clause
), Qnil
))
378 val
= Fprogn (XCDR (clause
));
388 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
389 doc
: /* Eval BODY forms sequentially and return value of last one.
390 usage: (progn BODY...) */)
393 register Lisp_Object val
= Qnil
;
400 val
= Feval (XCAR (args
));
408 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
409 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
410 The value of FIRST is saved during the evaluation of the remaining args,
411 whose values are discarded.
412 usage: (prog1 FIRST BODY...) */)
416 register Lisp_Object args_left
;
417 struct gcpro gcpro1
, gcpro2
;
418 register int argnum
= 0;
430 val
= Feval (Fcar (args_left
));
432 Feval (Fcar (args_left
));
433 args_left
= Fcdr (args_left
);
435 while (!NILP(args_left
));
441 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
442 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
443 The value of FORM2 is saved during the evaluation of the
444 remaining args, whose values are discarded.
445 usage: (prog2 FORM1 FORM2 BODY...) */)
449 register Lisp_Object args_left
;
450 struct gcpro gcpro1
, gcpro2
;
451 register int argnum
= -1;
465 val
= Feval (Fcar (args_left
));
467 Feval (Fcar (args_left
));
468 args_left
= Fcdr (args_left
);
470 while (!NILP (args_left
));
476 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
477 doc
: /* Set each SYM to the value of its VAL.
478 The symbols SYM are variables; they are literal (not evaluated).
479 The values VAL are expressions; they are evaluated.
480 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
481 The second VAL is not computed until after the first SYM is set, and so on;
482 each VAL can use the new value of variables set earlier in the `setq'.
483 The return value of the `setq' form is the value of the last VAL.
484 usage: (setq [SYM VAL]...) */)
487 register Lisp_Object args_left
;
488 register Lisp_Object val
, sym
;
499 val
= Feval (Fcar (Fcdr (args_left
)));
500 sym
= Fcar (args_left
);
502 args_left
= Fcdr (Fcdr (args_left
));
504 while (!NILP(args_left
));
510 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
511 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
512 usage: (quote ARG) */)
515 if (!NILP (Fcdr (args
)))
516 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
520 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
521 doc
: /* Like `quote', but preferred for objects which are functions.
522 In byte compilation, `function' causes its argument to be compiled.
523 `quote' cannot do that.
524 usage: (function ARG) */)
527 if (!NILP (Fcdr (args
)))
528 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
533 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
534 doc
: /* Return t if the containing function was run directly by user input.
535 This means that the function was called with `call-interactively'
536 \(which includes being called as the binding of a key)
537 and input is currently coming from the keyboard (not a keyboard macro),
538 and Emacs is not running in batch mode (`noninteractive' is nil).
540 The only known proper use of `interactive-p' is in deciding whether to
541 display a helpful message, or how to display it. If you're thinking
542 of using it for any other purpose, it is quite likely that you're
543 making a mistake. Think: what do you want to do when the command is
544 called from a keyboard macro?
546 To test whether your function was called with `call-interactively',
547 either (i) add an extra optional argument and give it an `interactive'
548 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
549 use `called-interactively-p'. */)
552 return (INTERACTIVE
&& interactive_p (1)) ? Qt
: Qnil
;
556 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
557 doc
: /* Return t if the containing function was called by `call-interactively'.
558 If KIND is `interactive', then only return t if the call was made
559 interactively by the user, i.e. not in `noninteractive' mode nor
560 when `executing-kbd-macro'.
561 If KIND is `any', on the other hand, it will return t for any kind of
562 interactive call, including being called as the binding of a key, or
563 from a keyboard macro, or in `noninteractive' mode.
565 The only known proper use of `interactive' for KIND is in deciding
566 whether to display a helpful message, or how to display it. If you're
567 thinking of using it for any other purpose, it is quite likely that
568 you're making a mistake. Think: what do you want to do when the
569 command is called from a keyboard macro?
571 This function is meant for implementing advice and other
572 function-modifying features. Instead of using this, it is sometimes
573 cleaner to give your function an extra optional argument whose
574 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
575 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
578 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
579 && interactive_p (1)) ? Qt
: Qnil
;
583 /* Return 1 if function in which this appears was called using
586 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
587 called is a built-in. */
590 interactive_p (int exclude_subrs_p
)
592 struct backtrace
*btp
;
595 btp
= backtrace_list
;
597 /* If this isn't a byte-compiled function, there may be a frame at
598 the top for Finteractive_p. If so, skip it. */
599 fun
= Findirect_function (*btp
->function
, Qnil
);
600 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
601 || XSUBR (fun
) == &Scalled_interactively_p
))
604 /* If we're running an Emacs 18-style byte-compiled function, there
605 may be a frame for Fbytecode at the top level. In any version of
606 Emacs there can be Fbytecode frames for subexpressions evaluated
607 inside catch and condition-case. Skip past them.
609 If this isn't a byte-compiled function, then we may now be
610 looking at several frames for special forms. Skip past them. */
612 && (EQ (*btp
->function
, Qbytecode
)
613 || btp
->nargs
== UNEVALLED
))
616 /* btp now points at the frame of the innermost function that isn't
617 a special form, ignoring frames for Finteractive_p and/or
618 Fbytecode at the top. If this frame is for a built-in function
619 (such as load or eval-region) return nil. */
620 fun
= Findirect_function (*btp
->function
, Qnil
);
621 if (exclude_subrs_p
&& SUBRP (fun
))
624 /* btp points to the frame of a Lisp function that called interactive-p.
625 Return t if that function was called interactively. */
626 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
632 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
633 doc
: /* Define NAME as a function.
634 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
635 See also the function `interactive'.
636 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
639 register Lisp_Object fn_name
;
640 register Lisp_Object defn
;
642 fn_name
= Fcar (args
);
643 CHECK_SYMBOL (fn_name
);
644 defn
= Fcons (Qlambda
, Fcdr (args
));
645 if (!NILP (Vpurify_flag
))
646 defn
= Fpurecopy (defn
);
647 if (CONSP (XSYMBOL (fn_name
)->function
)
648 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
649 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
650 Ffset (fn_name
, defn
);
651 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
655 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
656 doc
: /* Define NAME as a macro.
657 The actual definition looks like
658 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
659 When the macro is called, as in (NAME ARGS...),
660 the function (lambda ARGLIST BODY...) is applied to
661 the list ARGS... as it appears in the expression,
662 and the result should be a form to be evaluated instead of the original.
664 DECL is a declaration, optional, which can specify how to indent
665 calls to this macro, how Edebug should handle it, and which argument
666 should be treated as documentation. It looks like this:
668 The elements can look like this:
670 Set NAME's `lisp-indent-function' property to INDENT.
673 Set NAME's `edebug-form-spec' property to DEBUG. (This is
674 equivalent to writing a `def-edebug-spec' for the macro.)
677 Set NAME's `doc-string-elt' property to ELT.
679 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
682 register Lisp_Object fn_name
;
683 register Lisp_Object defn
;
684 Lisp_Object lambda_list
, doc
, tail
;
686 fn_name
= Fcar (args
);
687 CHECK_SYMBOL (fn_name
);
688 lambda_list
= Fcar (Fcdr (args
));
689 tail
= Fcdr (Fcdr (args
));
692 if (STRINGP (Fcar (tail
)))
698 while (CONSP (Fcar (tail
))
699 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
701 if (!NILP (Vmacro_declaration_function
))
705 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
713 tail
= Fcons (lambda_list
, tail
);
715 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
716 defn
= Fcons (Qmacro
, Fcons (Qlambda
, tail
));
718 if (!NILP (Vpurify_flag
))
719 defn
= Fpurecopy (defn
);
720 if (CONSP (XSYMBOL (fn_name
)->function
)
721 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
722 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
723 Ffset (fn_name
, defn
);
724 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
729 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
730 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
731 Aliased variables always have the same value; setting one sets the other.
732 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
733 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
734 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
735 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
736 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
737 The return value is BASE-VARIABLE. */)
738 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
740 struct Lisp_Symbol
*sym
;
742 CHECK_SYMBOL (new_alias
);
743 CHECK_SYMBOL (base_variable
);
745 sym
= XSYMBOL (new_alias
);
748 /* Not sure why, but why not? */
749 error ("Cannot make a constant an alias");
751 switch (sym
->redirect
)
753 case SYMBOL_FORWARDED
:
754 error ("Cannot make an internal variable an alias");
755 case SYMBOL_LOCALIZED
:
756 error ("Don't know how to make a localized variable an alias");
759 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
760 If n_a is bound, but b_v is not, set the value of b_v to n_a,
761 so that old-code that affects n_a before the aliasing is setup
763 if (NILP (Fboundp (base_variable
)))
764 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
767 struct specbinding
*p
;
769 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
772 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
773 error ("Don't know how to make a let-bound variable an alias");
776 sym
->redirect
= SYMBOL_VARALIAS
;
777 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
778 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
779 LOADHIST_ATTACH (new_alias
);
780 /* Even if docstring is nil: remove old docstring. */
781 Fput (new_alias
, Qvariable_documentation
, docstring
);
783 return base_variable
;
787 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
788 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
789 You are not required to define a variable in order to use it,
790 but the definition can supply documentation and an initial value
791 in a way that tags can recognize.
793 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
794 If SYMBOL is buffer-local, its default value is what is set;
795 buffer-local values are not affected.
796 INITVALUE and DOCSTRING are optional.
797 If DOCSTRING starts with *, this variable is identified as a user option.
798 This means that M-x set-variable recognizes it.
799 See also `user-variable-p'.
800 If INITVALUE is missing, SYMBOL's value is not set.
802 If SYMBOL has a local binding, then this form affects the local
803 binding. This is usually not what you want. Thus, if you need to
804 load a file defining variables, with this form or with `defconst' or
805 `defcustom', you should always load that file _outside_ any bindings
806 for these variables. \(`defconst' and `defcustom' behave similarly in
808 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
811 register Lisp_Object sym
, tem
, tail
;
815 if (!NILP (Fcdr (Fcdr (tail
))))
816 error ("Too many arguments");
818 tem
= Fdefault_boundp (sym
);
821 if (SYMBOL_CONSTANT_P (sym
))
823 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
824 Lisp_Object tem
= Fcar (tail
);
826 && EQ (XCAR (tem
), Qquote
)
827 && CONSP (XCDR (tem
))
828 && EQ (XCAR (XCDR (tem
)), sym
)))
829 error ("Constant symbol `%s' specified in defvar",
830 SDATA (SYMBOL_NAME (sym
)));
834 Fset_default (sym
, Feval (Fcar (tail
)));
836 { /* Check if there is really a global binding rather than just a let
837 binding that shadows the global unboundness of the var. */
838 volatile struct specbinding
*pdl
= specpdl_ptr
;
839 while (--pdl
>= specpdl
)
841 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
842 && EQ (pdl
->old_value
, Qunbound
))
844 message_with_string ("Warning: defvar ignored because %s is let-bound",
845 SYMBOL_NAME (sym
), 1);
854 if (!NILP (Vpurify_flag
))
855 tem
= Fpurecopy (tem
);
856 Fput (sym
, Qvariable_documentation
, tem
);
858 LOADHIST_ATTACH (sym
);
861 /* Simple (defvar <var>) should not count as a definition at all.
862 It could get in the way of other definitions, and unloading this
863 package could try to make the variable unbound. */
869 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
870 doc
: /* Define SYMBOL as a constant variable.
871 The intent is that neither programs nor users should ever change this value.
872 Always sets the value of SYMBOL to the result of evalling INITVALUE.
873 If SYMBOL is buffer-local, its default value is what is set;
874 buffer-local values are not affected.
875 DOCSTRING is optional.
877 If SYMBOL has a local binding, then this form sets the local binding's
878 value. However, you should normally not make local bindings for
879 variables defined with this form.
880 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
883 register Lisp_Object sym
, tem
;
886 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
887 error ("Too many arguments");
889 tem
= Feval (Fcar (Fcdr (args
)));
890 if (!NILP (Vpurify_flag
))
891 tem
= Fpurecopy (tem
);
892 Fset_default (sym
, tem
);
893 tem
= Fcar (Fcdr (Fcdr (args
)));
896 if (!NILP (Vpurify_flag
))
897 tem
= Fpurecopy (tem
);
898 Fput (sym
, Qvariable_documentation
, tem
);
900 Fput (sym
, Qrisky_local_variable
, Qt
);
901 LOADHIST_ATTACH (sym
);
905 /* Error handler used in Fuser_variable_p. */
907 user_variable_p_eh (Lisp_Object ignore
)
913 lisp_indirect_variable (Lisp_Object sym
)
915 XSETSYMBOL (sym
, indirect_variable (XSYMBOL (sym
)));
919 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
920 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
921 \(The alternative is a variable used internally in a Lisp program.)
922 A variable is a user variable if
923 \(1) the first character of its documentation is `*', or
924 \(2) it is customizable (its property list contains a non-nil value
925 of `standard-value' or `custom-autoload'), or
926 \(3) it is an alias for another user variable.
927 Return nil if VARIABLE is an alias and there is a loop in the
928 chain of symbols. */)
929 (Lisp_Object variable
)
931 Lisp_Object documentation
;
933 if (!SYMBOLP (variable
))
936 /* If indirect and there's an alias loop, don't check anything else. */
937 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
938 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
939 Qt
, user_variable_p_eh
)))
944 documentation
= Fget (variable
, Qvariable_documentation
);
945 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
947 if (STRINGP (documentation
)
948 && ((unsigned char) SREF (documentation
, 0) == '*'))
950 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
951 if (CONSP (documentation
)
952 && STRINGP (XCAR (documentation
))
953 && INTEGERP (XCDR (documentation
))
954 && XINT (XCDR (documentation
)) < 0)
956 /* Customizable? See `custom-variable-p'. */
957 if ((!NILP (Fget (variable
, intern ("standard-value"))))
958 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
961 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
964 /* An indirect variable? Let's follow the chain. */
965 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
969 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
970 doc
: /* Bind variables according to VARLIST then eval BODY.
971 The value of the last form in BODY is returned.
972 Each element of VARLIST is a symbol (which is bound to nil)
973 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
974 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
975 usage: (let* VARLIST BODY...) */)
978 Lisp_Object varlist
, val
, elt
;
979 int count
= SPECPDL_INDEX ();
980 struct gcpro gcpro1
, gcpro2
, gcpro3
;
982 GCPRO3 (args
, elt
, varlist
);
984 varlist
= Fcar (args
);
985 while (!NILP (varlist
))
988 elt
= Fcar (varlist
);
990 specbind (elt
, Qnil
);
991 else if (! NILP (Fcdr (Fcdr (elt
))))
992 signal_error ("`let' bindings can have only one value-form", elt
);
995 val
= Feval (Fcar (Fcdr (elt
)));
996 specbind (Fcar (elt
), val
);
998 varlist
= Fcdr (varlist
);
1001 val
= Fprogn (Fcdr (args
));
1002 return unbind_to (count
, val
);
1005 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1006 doc
: /* Bind variables according to VARLIST then eval BODY.
1007 The value of the last form in BODY is returned.
1008 Each element of VARLIST is a symbol (which is bound to nil)
1009 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1010 All the VALUEFORMs are evalled before any symbols are bound.
1011 usage: (let VARLIST BODY...) */)
1014 Lisp_Object
*temps
, tem
;
1015 register Lisp_Object elt
, varlist
;
1016 int count
= SPECPDL_INDEX ();
1017 register int argnum
;
1018 struct gcpro gcpro1
, gcpro2
;
1020 varlist
= Fcar (args
);
1022 /* Make space to hold the values to give the bound variables */
1023 elt
= Flength (varlist
);
1024 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
1026 /* Compute the values and store them in `temps' */
1028 GCPRO2 (args
, *temps
);
1031 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1034 elt
= XCAR (varlist
);
1036 temps
[argnum
++] = Qnil
;
1037 else if (! NILP (Fcdr (Fcdr (elt
))))
1038 signal_error ("`let' bindings can have only one value-form", elt
);
1040 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
1041 gcpro2
.nvars
= argnum
;
1045 varlist
= Fcar (args
);
1046 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1048 elt
= XCAR (varlist
);
1049 tem
= temps
[argnum
++];
1051 specbind (elt
, tem
);
1053 specbind (Fcar (elt
), tem
);
1056 elt
= Fprogn (Fcdr (args
));
1057 return unbind_to (count
, elt
);
1060 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1061 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1062 The order of execution is thus TEST, BODY, TEST, BODY and so on
1063 until TEST returns nil.
1064 usage: (while TEST BODY...) */)
1067 Lisp_Object test
, body
;
1068 struct gcpro gcpro1
, gcpro2
;
1070 GCPRO2 (test
, body
);
1074 while (!NILP (Feval (test
)))
1084 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1085 doc
: /* Return result of expanding macros at top level of FORM.
1086 If FORM is not a macro call, it is returned unchanged.
1087 Otherwise, the macro is expanded and the expansion is considered
1088 in place of FORM. When a non-macro-call results, it is returned.
1090 The second optional arg ENVIRONMENT specifies an environment of macro
1091 definitions to shadow the loaded ones for use in file byte-compilation. */)
1092 (Lisp_Object form
, Lisp_Object environment
)
1094 /* With cleanups from Hallvard Furuseth. */
1095 register Lisp_Object expander
, sym
, def
, tem
;
1099 /* Come back here each time we expand a macro call,
1100 in case it expands into another macro call. */
1103 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1104 def
= sym
= XCAR (form
);
1106 /* Trace symbols aliases to other symbols
1107 until we get a symbol that is not an alias. */
1108 while (SYMBOLP (def
))
1112 tem
= Fassq (sym
, environment
);
1115 def
= XSYMBOL (sym
)->function
;
1116 if (!EQ (def
, Qunbound
))
1121 /* Right now TEM is the result from SYM in ENVIRONMENT,
1122 and if TEM is nil then DEF is SYM's function definition. */
1125 /* SYM is not mentioned in ENVIRONMENT.
1126 Look at its function definition. */
1127 if (EQ (def
, Qunbound
) || !CONSP (def
))
1128 /* Not defined or definition not suitable */
1130 if (EQ (XCAR (def
), Qautoload
))
1132 /* Autoloading function: will it be a macro when loaded? */
1133 tem
= Fnth (make_number (4), def
);
1134 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1135 /* Yes, load it and try again. */
1137 struct gcpro gcpro1
;
1139 do_autoload (def
, sym
);
1146 else if (!EQ (XCAR (def
), Qmacro
))
1148 else expander
= XCDR (def
);
1152 expander
= XCDR (tem
);
1153 if (NILP (expander
))
1156 form
= apply1 (expander
, XCDR (form
));
1161 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1162 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1163 TAG is evalled to get the tag to use; it must not be nil.
1165 Then the BODY is executed.
1166 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1167 If no throw happens, `catch' returns the value of the last BODY form.
1168 If a throw happens, it specifies the value to return from `catch'.
1169 usage: (catch TAG BODY...) */)
1172 register Lisp_Object tag
;
1173 struct gcpro gcpro1
;
1176 tag
= Feval (Fcar (args
));
1178 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1181 /* Set up a catch, then call C function FUNC on argument ARG.
1182 FUNC should return a Lisp_Object.
1183 This is how catches are done from within C code. */
1186 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1188 /* This structure is made part of the chain `catchlist'. */
1191 /* Fill in the components of c, and put it on the list. */
1195 c
.backlist
= backtrace_list
;
1196 c
.handlerlist
= handlerlist
;
1197 c
.lisp_eval_depth
= lisp_eval_depth
;
1198 c
.pdlcount
= SPECPDL_INDEX ();
1199 c
.poll_suppress_count
= poll_suppress_count
;
1200 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1201 c
.gcpro
= gcprolist
;
1202 c
.byte_stack
= byte_stack_list
;
1206 if (! _setjmp (c
.jmp
))
1207 c
.val
= (*func
) (arg
);
1209 /* Throw works by a longjmp that comes right here. */
1214 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1215 jump to that CATCH, returning VALUE as the value of that catch.
1217 This is the guts Fthrow and Fsignal; they differ only in the way
1218 they choose the catch tag to throw to. A catch tag for a
1219 condition-case form has a TAG of Qnil.
1221 Before each catch is discarded, unbind all special bindings and
1222 execute all unwind-protect clauses made above that catch. Unwind
1223 the handler stack as we go, so that the proper handlers are in
1224 effect for each unwind-protect clause we run. At the end, restore
1225 some static info saved in CATCH, and longjmp to the location
1228 This is used for correct unwinding in Fthrow and Fsignal. */
1231 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1233 register int last_time
;
1235 /* Save the value in the tag. */
1238 /* Restore certain special C variables. */
1239 set_poll_suppress_count (catch->poll_suppress_count
);
1240 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1241 handling_signal
= 0;
1246 last_time
= catchlist
== catch;
1248 /* Unwind the specpdl stack, and then restore the proper set of
1250 unbind_to (catchlist
->pdlcount
, Qnil
);
1251 handlerlist
= catchlist
->handlerlist
;
1252 catchlist
= catchlist
->next
;
1254 while (! last_time
);
1257 /* If x_catch_errors was done, turn it off now.
1258 (First we give unbind_to a chance to do that.) */
1259 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1260 The catch must remain in effect during that delicate
1261 state. --lorentey */
1262 x_fully_uncatch_errors ();
1266 byte_stack_list
= catch->byte_stack
;
1267 gcprolist
= catch->gcpro
;
1270 gcpro_level
= gcprolist
->level
+ 1;
1274 backtrace_list
= catch->backlist
;
1275 lisp_eval_depth
= catch->lisp_eval_depth
;
1277 _longjmp (catch->jmp
, 1);
1280 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1281 doc
: /* Throw to the catch for TAG and return VALUE from it.
1282 Both TAG and VALUE are evalled. */)
1283 (register Lisp_Object tag
, Lisp_Object value
)
1285 register struct catchtag
*c
;
1288 for (c
= catchlist
; c
; c
= c
->next
)
1290 if (EQ (c
->tag
, tag
))
1291 unwind_to_catch (c
, value
);
1293 xsignal2 (Qno_catch
, tag
, value
);
1297 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1298 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1299 If BODYFORM completes normally, its value is returned
1300 after executing the UNWINDFORMS.
1301 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1302 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1306 int count
= SPECPDL_INDEX ();
1308 record_unwind_protect (Fprogn
, Fcdr (args
));
1309 val
= Feval (Fcar (args
));
1310 return unbind_to (count
, val
);
1313 /* Chain of condition handlers currently in effect.
1314 The elements of this chain are contained in the stack frames
1315 of Fcondition_case and internal_condition_case.
1316 When an error is signaled (by calling Fsignal, below),
1317 this chain is searched for an element that applies. */
1319 struct handler
*handlerlist
;
1321 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1322 doc
: /* Regain control when an error is signaled.
1323 Executes BODYFORM and returns its value if no error happens.
1324 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1325 where the BODY is made of Lisp expressions.
1327 A handler is applicable to an error
1328 if CONDITION-NAME is one of the error's condition names.
1329 If an error happens, the first applicable handler is run.
1331 The car of a handler may be a list of condition names
1332 instead of a single condition name. Then it handles all of them.
1334 When a handler handles an error, control returns to the `condition-case'
1335 and it executes the handler's BODY...
1336 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1337 \(If VAR is nil, the handler can't access that information.)
1338 Then the value of the last BODY form is returned from the `condition-case'
1341 See also the function `signal' for more info.
1342 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1345 register Lisp_Object bodyform
, handlers
;
1346 volatile Lisp_Object var
;
1349 bodyform
= Fcar (Fcdr (args
));
1350 handlers
= Fcdr (Fcdr (args
));
1352 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1355 /* Like Fcondition_case, but the args are separate
1356 rather than passed in a list. Used by Fbyte_code. */
1359 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1360 Lisp_Object handlers
)
1368 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1374 && (SYMBOLP (XCAR (tem
))
1375 || CONSP (XCAR (tem
))))))
1376 error ("Invalid condition handler", tem
);
1381 c
.backlist
= backtrace_list
;
1382 c
.handlerlist
= handlerlist
;
1383 c
.lisp_eval_depth
= lisp_eval_depth
;
1384 c
.pdlcount
= SPECPDL_INDEX ();
1385 c
.poll_suppress_count
= poll_suppress_count
;
1386 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1387 c
.gcpro
= gcprolist
;
1388 c
.byte_stack
= byte_stack_list
;
1389 if (_setjmp (c
.jmp
))
1392 specbind (h
.var
, c
.val
);
1393 val
= Fprogn (Fcdr (h
.chosen_clause
));
1395 /* Note that this just undoes the binding of h.var; whoever
1396 longjumped to us unwound the stack to c.pdlcount before
1398 unbind_to (c
.pdlcount
, Qnil
);
1405 h
.handler
= handlers
;
1406 h
.next
= handlerlist
;
1410 val
= Feval (bodyform
);
1412 handlerlist
= h
.next
;
1416 /* Call the function BFUN with no arguments, catching errors within it
1417 according to HANDLERS. If there is an error, call HFUN with
1418 one argument which is the data that describes the error:
1421 HANDLERS can be a list of conditions to catch.
1422 If HANDLERS is Qt, catch all errors.
1423 If HANDLERS is Qerror, catch all errors
1424 but allow the debugger to run if that is enabled. */
1427 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1428 Lisp_Object (*hfun
) (Lisp_Object
))
1434 /* Since Fsignal will close off all calls to x_catch_errors,
1435 we will get the wrong results if some are not closed now. */
1437 if (x_catching_errors ())
1443 c
.backlist
= backtrace_list
;
1444 c
.handlerlist
= handlerlist
;
1445 c
.lisp_eval_depth
= lisp_eval_depth
;
1446 c
.pdlcount
= SPECPDL_INDEX ();
1447 c
.poll_suppress_count
= poll_suppress_count
;
1448 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1449 c
.gcpro
= gcprolist
;
1450 c
.byte_stack
= byte_stack_list
;
1451 if (_setjmp (c
.jmp
))
1453 return (*hfun
) (c
.val
);
1457 h
.handler
= handlers
;
1459 h
.next
= handlerlist
;
1465 handlerlist
= h
.next
;
1469 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1472 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1473 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1479 /* Since Fsignal will close off all calls to x_catch_errors,
1480 we will get the wrong results if some are not closed now. */
1482 if (x_catching_errors ())
1488 c
.backlist
= backtrace_list
;
1489 c
.handlerlist
= handlerlist
;
1490 c
.lisp_eval_depth
= lisp_eval_depth
;
1491 c
.pdlcount
= SPECPDL_INDEX ();
1492 c
.poll_suppress_count
= poll_suppress_count
;
1493 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1494 c
.gcpro
= gcprolist
;
1495 c
.byte_stack
= byte_stack_list
;
1496 if (_setjmp (c
.jmp
))
1498 return (*hfun
) (c
.val
);
1502 h
.handler
= handlers
;
1504 h
.next
= handlerlist
;
1508 val
= (*bfun
) (arg
);
1510 handlerlist
= h
.next
;
1514 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1518 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1521 Lisp_Object handlers
,
1522 Lisp_Object (*hfun
) (Lisp_Object
))
1528 /* Since Fsignal will close off all calls to x_catch_errors,
1529 we will get the wrong results if some are not closed now. */
1531 if (x_catching_errors ())
1537 c
.backlist
= backtrace_list
;
1538 c
.handlerlist
= handlerlist
;
1539 c
.lisp_eval_depth
= lisp_eval_depth
;
1540 c
.pdlcount
= SPECPDL_INDEX ();
1541 c
.poll_suppress_count
= poll_suppress_count
;
1542 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1543 c
.gcpro
= gcprolist
;
1544 c
.byte_stack
= byte_stack_list
;
1545 if (_setjmp (c
.jmp
))
1547 return (*hfun
) (c
.val
);
1551 h
.handler
= handlers
;
1553 h
.next
= handlerlist
;
1557 val
= (*bfun
) (arg1
, arg2
);
1559 handlerlist
= h
.next
;
1563 /* Like internal_condition_case but call BFUN with NARGS as first,
1564 and ARGS as second argument. */
1567 internal_condition_case_n (Lisp_Object (*bfun
) (int, Lisp_Object
*),
1570 Lisp_Object handlers
,
1571 Lisp_Object (*hfun
) (Lisp_Object
))
1577 /* Since Fsignal will close off all calls to x_catch_errors,
1578 we will get the wrong results if some are not closed now. */
1580 if (x_catching_errors ())
1586 c
.backlist
= backtrace_list
;
1587 c
.handlerlist
= handlerlist
;
1588 c
.lisp_eval_depth
= lisp_eval_depth
;
1589 c
.pdlcount
= SPECPDL_INDEX ();
1590 c
.poll_suppress_count
= poll_suppress_count
;
1591 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1592 c
.gcpro
= gcprolist
;
1593 c
.byte_stack
= byte_stack_list
;
1594 if (_setjmp (c
.jmp
))
1596 return (*hfun
) (c
.val
);
1600 h
.handler
= handlers
;
1602 h
.next
= handlerlist
;
1606 val
= (*bfun
) (nargs
, args
);
1608 handlerlist
= h
.next
;
1613 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1614 Lisp_Object
, Lisp_Object
);
1616 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1617 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1618 This function does not return.
1620 An error symbol is a symbol with an `error-conditions' property
1621 that is a list of condition names.
1622 A handler for any of those names will get to handle this signal.
1623 The symbol `error' should normally be one of them.
1625 DATA should be a list. Its elements are printed as part of the error message.
1626 See Info anchor `(elisp)Definition of signal' for some details on how this
1627 error message is constructed.
1628 If the signal is handled, DATA is made available to the handler.
1629 See also the function `condition-case'. */)
1630 (Lisp_Object error_symbol
, Lisp_Object data
)
1632 /* When memory is full, ERROR-SYMBOL is nil,
1633 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1634 That is a special case--don't do this in other situations. */
1635 register struct handler
*allhandlers
= handlerlist
;
1636 Lisp_Object conditions
;
1637 extern int gc_in_progress
;
1638 extern int waiting_for_input
;
1640 Lisp_Object real_error_symbol
;
1641 struct backtrace
*bp
;
1643 immediate_quit
= handling_signal
= 0;
1645 if (gc_in_progress
|| waiting_for_input
)
1648 if (NILP (error_symbol
))
1649 real_error_symbol
= Fcar (data
);
1651 real_error_symbol
= error_symbol
;
1653 #if 0 /* rms: I don't know why this was here,
1654 but it is surely wrong for an error that is handled. */
1655 #ifdef HAVE_WINDOW_SYSTEM
1656 if (display_hourglass_p
)
1657 cancel_hourglass ();
1661 /* This hook is used by edebug. */
1662 if (! NILP (Vsignal_hook_function
)
1663 && ! NILP (error_symbol
))
1665 /* Edebug takes care of restoring these variables when it exits. */
1666 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1667 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1669 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1670 max_specpdl_size
= SPECPDL_INDEX () + 40;
1672 call2 (Vsignal_hook_function
, error_symbol
, data
);
1675 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1677 /* Remember from where signal was called. Skip over the frame for
1678 `signal' itself. If a frame for `error' follows, skip that,
1679 too. Don't do this when ERROR_SYMBOL is nil, because that
1680 is a memory-full error. */
1681 Vsignaling_function
= Qnil
;
1682 if (backtrace_list
&& !NILP (error_symbol
))
1684 bp
= backtrace_list
->next
;
1685 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1687 if (bp
&& bp
->function
)
1688 Vsignaling_function
= *bp
->function
;
1691 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1693 register Lisp_Object clause
;
1695 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1696 error_symbol
, data
);
1698 if (EQ (clause
, Qlambda
))
1700 /* We can't return values to code which signaled an error, but we
1701 can continue code which has signaled a quit. */
1702 if (EQ (real_error_symbol
, Qquit
))
1705 error ("Cannot return from the debugger in an error");
1710 Lisp_Object unwind_data
;
1711 struct handler
*h
= handlerlist
;
1713 handlerlist
= allhandlers
;
1715 if (NILP (error_symbol
))
1718 unwind_data
= Fcons (error_symbol
, data
);
1719 h
->chosen_clause
= clause
;
1720 unwind_to_catch (h
->tag
, unwind_data
);
1724 handlerlist
= allhandlers
;
1725 /* If no handler is present now, try to run the debugger,
1726 and if that fails, throw to top level. */
1727 find_handler_clause (Qerror
, conditions
, error_symbol
, data
);
1729 Fthrow (Qtop_level
, Qt
);
1731 if (! NILP (error_symbol
))
1732 data
= Fcons (error_symbol
, data
);
1734 string
= Ferror_message_string (data
);
1735 fatal ("%s", SDATA (string
), 0);
1738 /* Internal version of Fsignal that never returns.
1739 Used for anything but Qquit (which can return from Fsignal). */
1742 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1744 Fsignal (error_symbol
, data
);
1748 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1751 xsignal0 (Lisp_Object error_symbol
)
1753 xsignal (error_symbol
, Qnil
);
1757 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1759 xsignal (error_symbol
, list1 (arg
));
1763 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1765 xsignal (error_symbol
, list2 (arg1
, arg2
));
1769 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1771 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1774 /* Signal `error' with message S, and additional arg ARG.
1775 If ARG is not a genuine list, make it a one-element list. */
1778 signal_error (const char *s
, Lisp_Object arg
)
1780 Lisp_Object tortoise
, hare
;
1782 hare
= tortoise
= arg
;
1783 while (CONSP (hare
))
1790 tortoise
= XCDR (tortoise
);
1792 if (EQ (hare
, tortoise
))
1797 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1799 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1803 /* Return nonzero if LIST is a non-nil atom or
1804 a list containing one of CONDITIONS. */
1807 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1814 while (CONSP (conditions
))
1816 Lisp_Object
this, tail
;
1817 this = XCAR (conditions
);
1818 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1819 if (EQ (XCAR (tail
), this))
1821 conditions
= XCDR (conditions
);
1826 /* Return 1 if an error with condition-symbols CONDITIONS,
1827 and described by SIGNAL-DATA, should skip the debugger
1828 according to debugger-ignored-errors. */
1831 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1834 int first_string
= 1;
1835 Lisp_Object error_message
;
1837 error_message
= Qnil
;
1838 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1840 if (STRINGP (XCAR (tail
)))
1844 error_message
= Ferror_message_string (data
);
1848 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1853 Lisp_Object contail
;
1855 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1856 if (EQ (XCAR (tail
), XCAR (contail
)))
1864 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1865 SIG and DATA describe the signal, as in find_handler_clause. */
1868 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1870 Lisp_Object combined_data
;
1872 combined_data
= Fcons (sig
, data
);
1875 /* Don't try to run the debugger with interrupts blocked.
1876 The editing loop would return anyway. */
1878 /* Does user want to enter debugger for this kind of error? */
1881 : wants_debugger (Vdebug_on_error
, conditions
))
1882 && ! skip_debugger (conditions
, combined_data
)
1883 /* rms: what's this for? */
1884 && when_entered_debugger
< num_nonmacro_input_events
)
1886 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1893 /* Value of Qlambda means we have called debugger and user has continued.
1894 There are two ways to pass SIG and DATA:
1895 = SIG is the error symbol, and DATA is the rest of the data.
1896 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1897 This is for memory-full errors only.
1899 We need to increase max_specpdl_size temporarily around
1900 anything we do that can push on the specpdl, so as not to get
1901 a second error here in case we're handling specpdl overflow. */
1904 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1905 Lisp_Object sig
, Lisp_Object data
)
1907 register Lisp_Object h
;
1908 register Lisp_Object tem
;
1909 int debugger_called
= 0;
1910 int debugger_considered
= 0;
1912 /* t is used by handlers for all conditions, set up by C code. */
1913 if (EQ (handlers
, Qt
))
1916 /* Don't run the debugger for a memory-full error.
1917 (There is no room in memory to do that!) */
1919 debugger_considered
= 1;
1921 /* error is used similarly, but means print an error message
1922 and run the debugger if that is enabled. */
1923 if (EQ (handlers
, Qerror
)
1924 || !NILP (Vdebug_on_signal
)) /* This says call debugger even if
1925 there is a handler. */
1927 if (!NILP (sig
) && wants_debugger (Vstack_trace_on_error
, conditions
))
1929 max_lisp_eval_depth
+= 15;
1934 internal_with_output_to_temp_buffer
1936 (Lisp_Object (*) (Lisp_Object
)) Fbacktrace
,
1939 max_lisp_eval_depth
-= 15;
1942 if (!debugger_considered
)
1944 debugger_considered
= 1;
1945 debugger_called
= maybe_call_debugger (conditions
, sig
, data
);
1948 /* If there is no handler, return saying whether we ran the debugger. */
1949 if (EQ (handlers
, Qerror
))
1951 if (debugger_called
)
1957 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1959 Lisp_Object handler
, condit
;
1962 if (!CONSP (handler
))
1964 condit
= Fcar (handler
);
1965 /* Handle a single condition name in handler HANDLER. */
1966 if (SYMBOLP (condit
))
1968 tem
= Fmemq (Fcar (handler
), conditions
);
1972 /* Handle a list of condition names in handler HANDLER. */
1973 else if (CONSP (condit
))
1976 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1978 tem
= Fmemq (Fcar (tail
), conditions
);
1981 /* This handler is going to apply.
1982 Does it allow the debugger to run first? */
1983 if (! debugger_considered
&& !NILP (Fmemq (Qdebug
, condit
)))
1984 maybe_call_debugger (conditions
, sig
, data
);
1994 /* dump an error message; called like printf */
1998 error (const char *m
, ...)
2015 /* A va_list can't be reused if we have to go around the loop
2016 again; we need to "reinitialize" it each time. */
2018 used
= doprnt (buffer
, size
, m
, m
+ mlen
, ap
);
2024 buffer
= (char *) xrealloc (buffer
, size
);
2027 buffer
= (char *) xmalloc (size
);
2032 string
= build_string (buffer
);
2036 xsignal1 (Qerror
, string
);
2039 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
2040 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
2041 This means it contains a description for how to read arguments to give it.
2042 The value is nil for an invalid function or a symbol with no function
2045 Interactively callable functions include strings and vectors (treated
2046 as keyboard macros), lambda-expressions that contain a top-level call
2047 to `interactive', autoload definitions made by `autoload' with non-nil
2048 fourth argument, and some of the built-in functions of Lisp.
2050 Also, a symbol satisfies `commandp' if its function definition does so.
2052 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2053 then strings and vectors are not accepted. */)
2054 (Lisp_Object function
, Lisp_Object for_call_interactively
)
2056 register Lisp_Object fun
;
2057 register Lisp_Object funcar
;
2058 Lisp_Object if_prop
= Qnil
;
2062 fun
= indirect_function (fun
); /* Check cycles. */
2063 if (NILP (fun
) || EQ (fun
, Qunbound
))
2066 /* Check an `interactive-form' property if present, analogous to the
2067 function-documentation property. */
2069 while (SYMBOLP (fun
))
2071 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2074 fun
= Fsymbol_function (fun
);
2077 /* Emacs primitives are interactive if their DEFUN specifies an
2078 interactive spec. */
2080 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2082 /* Bytecode objects are interactive if they are long enough to
2083 have an element whose index is COMPILED_INTERACTIVE, which is
2084 where the interactive spec is stored. */
2085 else if (COMPILEDP (fun
))
2086 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2089 /* Strings and vectors are keyboard macros. */
2090 if (STRINGP (fun
) || VECTORP (fun
))
2091 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2093 /* Lists may represent commands. */
2096 funcar
= XCAR (fun
);
2097 if (EQ (funcar
, Qlambda
))
2098 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2099 if (EQ (funcar
, Qautoload
))
2100 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2105 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2106 doc
: /* Define FUNCTION to autoload from FILE.
2107 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2108 Third arg DOCSTRING is documentation for the function.
2109 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2110 Fifth arg TYPE indicates the type of the object:
2111 nil or omitted says FUNCTION is a function,
2112 `keymap' says FUNCTION is really a keymap, and
2113 `macro' or t says FUNCTION is really a macro.
2114 Third through fifth args give info about the real definition.
2115 They default to nil.
2116 If FUNCTION is already defined other than as an autoload,
2117 this does nothing and returns nil. */)
2118 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2120 CHECK_SYMBOL (function
);
2121 CHECK_STRING (file
);
2123 /* If function is defined and not as an autoload, don't override */
2124 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2125 && !(CONSP (XSYMBOL (function
)->function
)
2126 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2129 if (NILP (Vpurify_flag
))
2130 /* Only add entries after dumping, because the ones before are
2131 not useful and else we get loads of them from the loaddefs.el. */
2132 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2134 /* We don't want the docstring in purespace (instead,
2135 Snarf-documentation should (hopefully) overwrite it).
2136 We used to use 0 here, but that leads to accidental sharing in
2137 purecopy's hash-consing, so we use a (hopefully) unique integer
2139 docstring
= make_number (XHASH (function
));
2140 return Ffset (function
,
2141 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2142 interactive
, type
)));
2146 un_autoload (Lisp_Object oldqueue
)
2148 register Lisp_Object queue
, first
, second
;
2150 /* Queue to unwind is current value of Vautoload_queue.
2151 oldqueue is the shadowed value to leave in Vautoload_queue. */
2152 queue
= Vautoload_queue
;
2153 Vautoload_queue
= oldqueue
;
2154 while (CONSP (queue
))
2156 first
= XCAR (queue
);
2157 second
= Fcdr (first
);
2158 first
= Fcar (first
);
2159 if (EQ (first
, make_number (0)))
2162 Ffset (first
, second
);
2163 queue
= XCDR (queue
);
2168 /* Load an autoloaded function.
2169 FUNNAME is the symbol which is the function's name.
2170 FUNDEF is the autoload definition (a list). */
2173 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2175 int count
= SPECPDL_INDEX ();
2177 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2179 /* This is to make sure that loadup.el gives a clear picture
2180 of what files are preloaded and when. */
2181 if (! NILP (Vpurify_flag
))
2182 error ("Attempt to autoload %s while preparing to dump",
2183 SDATA (SYMBOL_NAME (funname
)));
2186 CHECK_SYMBOL (funname
);
2187 GCPRO3 (fun
, funname
, fundef
);
2189 /* Preserve the match data. */
2190 record_unwind_save_match_data ();
2192 /* If autoloading gets an error (which includes the error of failing
2193 to define the function being called), we use Vautoload_queue
2194 to undo function definitions and `provide' calls made by
2195 the function. We do this in the specific case of autoloading
2196 because autoloading is not an explicit request "load this file",
2197 but rather a request to "call this function".
2199 The value saved here is to be restored into Vautoload_queue. */
2200 record_unwind_protect (un_autoload
, Vautoload_queue
);
2201 Vautoload_queue
= Qt
;
2202 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2204 /* Once loading finishes, don't undo it. */
2205 Vautoload_queue
= Qt
;
2206 unbind_to (count
, Qnil
);
2208 fun
= Findirect_function (fun
, Qnil
);
2210 if (!NILP (Fequal (fun
, fundef
)))
2211 error ("Autoloading failed to define function %s",
2212 SDATA (SYMBOL_NAME (funname
)));
2217 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
2218 doc
: /* Evaluate FORM and return its value. */)
2221 Lisp_Object fun
, val
, original_fun
, original_args
;
2223 struct backtrace backtrace
;
2224 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2226 if (handling_signal
)
2230 return Fsymbol_value (form
);
2235 if ((consing_since_gc
> gc_cons_threshold
2236 && consing_since_gc
> gc_relative_threshold
)
2238 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2241 Fgarbage_collect ();
2245 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2247 if (max_lisp_eval_depth
< 100)
2248 max_lisp_eval_depth
= 100;
2249 if (lisp_eval_depth
> max_lisp_eval_depth
)
2250 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2253 original_fun
= Fcar (form
);
2254 original_args
= Fcdr (form
);
2256 backtrace
.next
= backtrace_list
;
2257 backtrace_list
= &backtrace
;
2258 backtrace
.function
= &original_fun
; /* This also protects them from gc */
2259 backtrace
.args
= &original_args
;
2260 backtrace
.nargs
= UNEVALLED
;
2261 backtrace
.evalargs
= 1;
2262 backtrace
.debug_on_exit
= 0;
2264 if (debug_on_next_call
)
2265 do_debug_on_call (Qt
);
2267 /* At this point, only original_fun and original_args
2268 have values that will be used below */
2271 /* Optimize for no indirection. */
2273 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2274 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2275 fun
= indirect_function (fun
);
2279 Lisp_Object numargs
;
2280 Lisp_Object argvals
[8];
2281 Lisp_Object args_left
;
2282 register int i
, maxargs
;
2284 args_left
= original_args
;
2285 numargs
= Flength (args_left
);
2289 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
2290 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2291 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2293 if (XSUBR (fun
)->max_args
== UNEVALLED
)
2295 backtrace
.evalargs
= 0;
2296 val
= (XSUBR (fun
)->function
.a1
) (args_left
);
2300 if (XSUBR (fun
)->max_args
== MANY
)
2302 /* Pass a vector of evaluated arguments */
2304 register int argnum
= 0;
2306 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
2308 GCPRO3 (args_left
, fun
, fun
);
2312 while (!NILP (args_left
))
2314 vals
[argnum
++] = Feval (Fcar (args_left
));
2315 args_left
= Fcdr (args_left
);
2316 gcpro3
.nvars
= argnum
;
2319 backtrace
.args
= vals
;
2320 backtrace
.nargs
= XINT (numargs
);
2322 val
= (XSUBR (fun
)->function
.am
) (XINT (numargs
), vals
);
2327 GCPRO3 (args_left
, fun
, fun
);
2328 gcpro3
.var
= argvals
;
2331 maxargs
= XSUBR (fun
)->max_args
;
2332 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2334 argvals
[i
] = Feval (Fcar (args_left
));
2340 backtrace
.args
= argvals
;
2341 backtrace
.nargs
= XINT (numargs
);
2346 val
= (XSUBR (fun
)->function
.a0
) ();
2349 val
= (XSUBR (fun
)->function
.a1
) (argvals
[0]);
2352 val
= (XSUBR (fun
)->function
.a2
) (argvals
[0], argvals
[1]);
2355 val
= (XSUBR (fun
)->function
.a3
) (argvals
[0], argvals
[1],
2359 val
= (XSUBR (fun
)->function
.a4
) (argvals
[0], argvals
[1],
2360 argvals
[2], argvals
[3]);
2363 val
= (XSUBR (fun
)->function
.a5
) (argvals
[0], argvals
[1], argvals
[2],
2364 argvals
[3], argvals
[4]);
2367 val
= (XSUBR (fun
)->function
.a6
) (argvals
[0], argvals
[1], argvals
[2],
2368 argvals
[3], argvals
[4], argvals
[5]);
2371 val
= (XSUBR (fun
)->function
.a7
) (argvals
[0], argvals
[1], argvals
[2],
2372 argvals
[3], argvals
[4], argvals
[5],
2377 val
= (XSUBR (fun
)->function
.a8
) (argvals
[0], argvals
[1], argvals
[2],
2378 argvals
[3], argvals
[4], argvals
[5],
2379 argvals
[6], argvals
[7]);
2383 /* Someone has created a subr that takes more arguments than
2384 is supported by this code. We need to either rewrite the
2385 subr to use a different argument protocol, or add more
2386 cases to this switch. */
2390 if (COMPILEDP (fun
))
2391 val
= apply_lambda (fun
, original_args
, 1);
2394 if (EQ (fun
, Qunbound
))
2395 xsignal1 (Qvoid_function
, original_fun
);
2397 xsignal1 (Qinvalid_function
, original_fun
);
2398 funcar
= XCAR (fun
);
2399 if (!SYMBOLP (funcar
))
2400 xsignal1 (Qinvalid_function
, original_fun
);
2401 if (EQ (funcar
, Qautoload
))
2403 do_autoload (fun
, original_fun
);
2406 if (EQ (funcar
, Qmacro
))
2407 val
= Feval (apply1 (Fcdr (fun
), original_args
));
2408 else if (EQ (funcar
, Qlambda
))
2409 val
= apply_lambda (fun
, original_args
, 1);
2411 xsignal1 (Qinvalid_function
, original_fun
);
2417 if (backtrace
.debug_on_exit
)
2418 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2419 backtrace_list
= backtrace
.next
;
2424 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2425 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2426 Then return the value FUNCTION returns.
2427 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2428 usage: (apply FUNCTION &rest ARGUMENTS) */)
2429 (int nargs
, Lisp_Object
*args
)
2431 register int i
, numargs
;
2432 register Lisp_Object spread_arg
;
2433 register Lisp_Object
*funcall_args
;
2435 struct gcpro gcpro1
;
2439 spread_arg
= args
[nargs
- 1];
2440 CHECK_LIST (spread_arg
);
2442 numargs
= XINT (Flength (spread_arg
));
2445 return Ffuncall (nargs
- 1, args
);
2446 else if (numargs
== 1)
2448 args
[nargs
- 1] = XCAR (spread_arg
);
2449 return Ffuncall (nargs
, args
);
2452 numargs
+= nargs
- 2;
2454 /* Optimize for no indirection. */
2455 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2456 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2457 fun
= indirect_function (fun
);
2458 if (EQ (fun
, Qunbound
))
2460 /* Let funcall get the error */
2467 if (numargs
< XSUBR (fun
)->min_args
2468 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2469 goto funcall
; /* Let funcall get the error */
2470 else if (XSUBR (fun
)->max_args
> numargs
)
2472 /* Avoid making funcall cons up a yet another new vector of arguments
2473 by explicitly supplying nil's for optional values */
2474 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
2475 * sizeof (Lisp_Object
));
2476 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2477 funcall_args
[++i
] = Qnil
;
2478 GCPRO1 (*funcall_args
);
2479 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2483 /* We add 1 to numargs because funcall_args includes the
2484 function itself as well as its arguments. */
2487 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
2488 * sizeof (Lisp_Object
));
2489 GCPRO1 (*funcall_args
);
2490 gcpro1
.nvars
= 1 + numargs
;
2493 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2494 /* Spread the last arg we got. Its first element goes in
2495 the slot that it used to occupy, hence this value of I. */
2497 while (!NILP (spread_arg
))
2499 funcall_args
[i
++] = XCAR (spread_arg
);
2500 spread_arg
= XCDR (spread_arg
);
2503 /* By convention, the caller needs to gcpro Ffuncall's args. */
2504 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
2507 /* Run hook variables in various ways. */
2509 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
2510 static Lisp_Object
run_hook_with_args (int, Lisp_Object
*,
2511 enum run_hooks_condition
);
2513 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2514 doc
: /* Run each hook in HOOKS.
2515 Each argument should be a symbol, a hook variable.
2516 These symbols are processed in the order specified.
2517 If a hook symbol has a non-nil value, that value may be a function
2518 or a list of functions to be called to run the hook.
2519 If the value is a function, it is called with no arguments.
2520 If it is a list, the elements are called, in order, with no arguments.
2522 Major modes should not use this function directly to run their mode
2523 hook; they should use `run-mode-hooks' instead.
2525 Do not use `make-local-variable' to make a hook variable buffer-local.
2526 Instead, use `add-hook' and specify t for the LOCAL argument.
2527 usage: (run-hooks &rest HOOKS) */)
2528 (int nargs
, Lisp_Object
*args
)
2530 Lisp_Object hook
[1];
2533 for (i
= 0; i
< nargs
; i
++)
2536 run_hook_with_args (1, hook
, to_completion
);
2542 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2543 Srun_hook_with_args
, 1, MANY
, 0,
2544 doc
: /* Run HOOK with the specified arguments ARGS.
2545 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2546 value, that value may be a function or a list of functions to be
2547 called to run the hook. If the value is a function, it is called with
2548 the given arguments and its return value is returned. If it is a list
2549 of functions, those functions are called, in order,
2550 with the given arguments ARGS.
2551 It is best not to depend on the value returned by `run-hook-with-args',
2554 Do not use `make-local-variable' to make a hook variable buffer-local.
2555 Instead, use `add-hook' and specify t for the LOCAL argument.
2556 usage: (run-hook-with-args HOOK &rest ARGS) */)
2557 (int nargs
, Lisp_Object
*args
)
2559 return run_hook_with_args (nargs
, args
, to_completion
);
2562 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2563 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2564 doc
: /* Run HOOK with the specified arguments ARGS.
2565 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2566 value, that value may be a function or a list of functions to be
2567 called to run the hook. If the value is a function, it is called with
2568 the given arguments and its return value is returned.
2569 If it is a list of functions, those functions are called, in order,
2570 with the given arguments ARGS, until one of them
2571 returns a non-nil value. Then we return that value.
2572 However, if they all return nil, we return nil.
2574 Do not use `make-local-variable' to make a hook variable buffer-local.
2575 Instead, use `add-hook' and specify t for the LOCAL argument.
2576 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2577 (int nargs
, Lisp_Object
*args
)
2579 return run_hook_with_args (nargs
, args
, until_success
);
2582 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2583 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2584 doc
: /* Run HOOK with the specified arguments ARGS.
2585 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2586 value, that value may be a function or a list of functions to be
2587 called to run the hook. If the value is a function, it is called with
2588 the given arguments and its return value is returned.
2589 If it is a list of functions, those functions are called, in order,
2590 with the given arguments ARGS, until one of them returns nil.
2591 Then we return nil. However, if they all return non-nil, we return non-nil.
2593 Do not use `make-local-variable' to make a hook variable buffer-local.
2594 Instead, use `add-hook' and specify t for the LOCAL argument.
2595 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2596 (int nargs
, Lisp_Object
*args
)
2598 return run_hook_with_args (nargs
, args
, until_failure
);
2601 /* ARGS[0] should be a hook symbol.
2602 Call each of the functions in the hook value, passing each of them
2603 as arguments all the rest of ARGS (all NARGS - 1 elements).
2604 COND specifies a condition to test after each call
2605 to decide whether to stop.
2606 The caller (or its caller, etc) must gcpro all of ARGS,
2607 except that it isn't necessary to gcpro ARGS[0]. */
2610 run_hook_with_args (int nargs
, Lisp_Object
*args
, enum run_hooks_condition cond
)
2612 Lisp_Object sym
, val
, ret
;
2613 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2615 /* If we are dying or still initializing,
2616 don't do anything--it would probably crash if we tried. */
2617 if (NILP (Vrun_hooks
))
2621 val
= find_symbol_value (sym
);
2622 ret
= (cond
== until_failure
? Qt
: Qnil
);
2624 if (EQ (val
, Qunbound
) || NILP (val
))
2626 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2629 return Ffuncall (nargs
, args
);
2633 Lisp_Object globals
= Qnil
;
2634 GCPRO3 (sym
, val
, globals
);
2637 CONSP (val
) && ((cond
== to_completion
)
2638 || (cond
== until_success
? NILP (ret
)
2642 if (EQ (XCAR (val
), Qt
))
2644 /* t indicates this hook has a local binding;
2645 it means to run the global binding too. */
2646 globals
= Fdefault_value (sym
);
2647 if (NILP (globals
)) continue;
2649 if (!CONSP (globals
) || EQ (XCAR (globals
), Qlambda
))
2652 ret
= Ffuncall (nargs
, args
);
2657 CONSP (globals
) && ((cond
== to_completion
)
2658 || (cond
== until_success
? NILP (ret
)
2660 globals
= XCDR (globals
))
2662 args
[0] = XCAR (globals
);
2663 /* In a global value, t should not occur. If it does, we
2664 must ignore it to avoid an endless loop. */
2665 if (!EQ (args
[0], Qt
))
2666 ret
= Ffuncall (nargs
, args
);
2672 args
[0] = XCAR (val
);
2673 ret
= Ffuncall (nargs
, args
);
2682 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2683 present value of that symbol.
2684 Call each element of FUNLIST,
2685 passing each of them the rest of ARGS.
2686 The caller (or its caller, etc) must gcpro all of ARGS,
2687 except that it isn't necessary to gcpro ARGS[0]. */
2690 run_hook_list_with_args (Lisp_Object funlist
, int nargs
, Lisp_Object
*args
)
2694 Lisp_Object globals
;
2695 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2699 GCPRO3 (sym
, val
, globals
);
2701 for (val
= funlist
; CONSP (val
); val
= XCDR (val
))
2703 if (EQ (XCAR (val
), Qt
))
2705 /* t indicates this hook has a local binding;
2706 it means to run the global binding too. */
2708 for (globals
= Fdefault_value (sym
);
2710 globals
= XCDR (globals
))
2712 args
[0] = XCAR (globals
);
2713 /* In a global value, t should not occur. If it does, we
2714 must ignore it to avoid an endless loop. */
2715 if (!EQ (args
[0], Qt
))
2716 Ffuncall (nargs
, args
);
2721 args
[0] = XCAR (val
);
2722 Ffuncall (nargs
, args
);
2729 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2732 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2734 Lisp_Object temp
[3];
2739 Frun_hook_with_args (3, temp
);
2742 /* Apply fn to arg */
2744 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2746 struct gcpro gcpro1
;
2750 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2753 Lisp_Object args
[2];
2757 RETURN_UNGCPRO (Fapply (2, args
));
2761 /* Call function fn on no arguments */
2763 call0 (Lisp_Object fn
)
2765 struct gcpro gcpro1
;
2768 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2771 /* Call function fn with 1 argument arg1 */
2774 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2776 struct gcpro gcpro1
;
2777 Lisp_Object args
[2];
2783 RETURN_UNGCPRO (Ffuncall (2, args
));
2786 /* Call function fn with 2 arguments arg1, arg2 */
2789 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2791 struct gcpro gcpro1
;
2792 Lisp_Object args
[3];
2798 RETURN_UNGCPRO (Ffuncall (3, args
));
2801 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2804 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2806 struct gcpro gcpro1
;
2807 Lisp_Object args
[4];
2814 RETURN_UNGCPRO (Ffuncall (4, args
));
2817 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2820 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2823 struct gcpro gcpro1
;
2824 Lisp_Object args
[5];
2832 RETURN_UNGCPRO (Ffuncall (5, args
));
2835 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2838 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2839 Lisp_Object arg4
, Lisp_Object arg5
)
2841 struct gcpro gcpro1
;
2842 Lisp_Object args
[6];
2851 RETURN_UNGCPRO (Ffuncall (6, args
));
2854 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2857 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2858 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2860 struct gcpro gcpro1
;
2861 Lisp_Object args
[7];
2871 RETURN_UNGCPRO (Ffuncall (7, args
));
2874 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7 */
2877 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2878 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2880 struct gcpro gcpro1
;
2881 Lisp_Object args
[8];
2892 RETURN_UNGCPRO (Ffuncall (8, args
));
2895 /* The caller should GCPRO all the elements of ARGS. */
2897 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2898 doc
: /* Call first argument as a function, passing remaining arguments to it.
2899 Return the value that function returns.
2900 Thus, (funcall 'cons 'x 'y) returns (x . y).
2901 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2902 (int nargs
, Lisp_Object
*args
)
2904 Lisp_Object fun
, original_fun
;
2906 int numargs
= nargs
- 1;
2907 Lisp_Object lisp_numargs
;
2909 struct backtrace backtrace
;
2910 register Lisp_Object
*internal_args
;
2914 if ((consing_since_gc
> gc_cons_threshold
2915 && consing_since_gc
> gc_relative_threshold
)
2917 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2918 Fgarbage_collect ();
2920 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2922 if (max_lisp_eval_depth
< 100)
2923 max_lisp_eval_depth
= 100;
2924 if (lisp_eval_depth
> max_lisp_eval_depth
)
2925 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2928 backtrace
.next
= backtrace_list
;
2929 backtrace_list
= &backtrace
;
2930 backtrace
.function
= &args
[0];
2931 backtrace
.args
= &args
[1];
2932 backtrace
.nargs
= nargs
- 1;
2933 backtrace
.evalargs
= 0;
2934 backtrace
.debug_on_exit
= 0;
2936 if (debug_on_next_call
)
2937 do_debug_on_call (Qlambda
);
2941 original_fun
= args
[0];
2945 /* Optimize for no indirection. */
2947 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2948 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2949 fun
= indirect_function (fun
);
2953 if (numargs
< XSUBR (fun
)->min_args
2954 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2956 XSETFASTINT (lisp_numargs
, numargs
);
2957 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2960 if (XSUBR (fun
)->max_args
== UNEVALLED
)
2961 xsignal1 (Qinvalid_function
, original_fun
);
2963 if (XSUBR (fun
)->max_args
== MANY
)
2965 val
= (XSUBR (fun
)->function
.am
) (numargs
, args
+ 1);
2969 if (XSUBR (fun
)->max_args
> numargs
)
2971 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2972 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2973 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2974 internal_args
[i
] = Qnil
;
2977 internal_args
= args
+ 1;
2978 switch (XSUBR (fun
)->max_args
)
2981 val
= (XSUBR (fun
)->function
.a0
) ();
2984 val
= (XSUBR (fun
)->function
.a1
) (internal_args
[0]);
2987 val
= (XSUBR (fun
)->function
.a2
) (internal_args
[0], internal_args
[1]);
2990 val
= (XSUBR (fun
)->function
.a3
) (internal_args
[0], internal_args
[1],
2994 val
= (XSUBR (fun
)->function
.a4
) (internal_args
[0], internal_args
[1],
2995 internal_args
[2], internal_args
[3]);
2998 val
= (XSUBR (fun
)->function
.a5
) (internal_args
[0], internal_args
[1],
2999 internal_args
[2], internal_args
[3],
3003 val
= (XSUBR (fun
)->function
.a6
) (internal_args
[0], internal_args
[1],
3004 internal_args
[2], internal_args
[3],
3005 internal_args
[4], internal_args
[5]);
3008 val
= (XSUBR (fun
)->function
.a7
) (internal_args
[0], internal_args
[1],
3009 internal_args
[2], internal_args
[3],
3010 internal_args
[4], internal_args
[5],
3015 val
= (XSUBR (fun
)->function
.a8
) (internal_args
[0], internal_args
[1],
3016 internal_args
[2], internal_args
[3],
3017 internal_args
[4], internal_args
[5],
3018 internal_args
[6], internal_args
[7]);
3023 /* If a subr takes more than 8 arguments without using MANY
3024 or UNEVALLED, we need to extend this function to support it.
3025 Until this is done, there is no way to call the function. */
3029 if (COMPILEDP (fun
))
3030 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3033 if (EQ (fun
, Qunbound
))
3034 xsignal1 (Qvoid_function
, original_fun
);
3036 xsignal1 (Qinvalid_function
, original_fun
);
3037 funcar
= XCAR (fun
);
3038 if (!SYMBOLP (funcar
))
3039 xsignal1 (Qinvalid_function
, original_fun
);
3040 if (EQ (funcar
, Qlambda
))
3041 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3042 else if (EQ (funcar
, Qautoload
))
3044 do_autoload (fun
, original_fun
);
3049 xsignal1 (Qinvalid_function
, original_fun
);
3054 if (backtrace
.debug_on_exit
)
3055 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3056 backtrace_list
= backtrace
.next
;
3061 apply_lambda (Lisp_Object fun
, Lisp_Object args
, int eval_flag
)
3063 Lisp_Object args_left
;
3064 Lisp_Object numargs
;
3065 register Lisp_Object
*arg_vector
;
3066 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3068 register Lisp_Object tem
;
3070 numargs
= Flength (args
);
3071 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
3074 GCPRO3 (*arg_vector
, args_left
, fun
);
3077 for (i
= 0; i
< XINT (numargs
);)
3079 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3080 if (eval_flag
) tem
= Feval (tem
);
3081 arg_vector
[i
++] = tem
;
3089 backtrace_list
->args
= arg_vector
;
3090 backtrace_list
->nargs
= i
;
3092 backtrace_list
->evalargs
= 0;
3093 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
3095 /* Do the debug-on-exit now, while arg_vector still exists. */
3096 if (backtrace_list
->debug_on_exit
)
3097 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3098 /* Don't do it again when we return to eval. */
3099 backtrace_list
->debug_on_exit
= 0;
3103 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3104 and return the result of evaluation.
3105 FUN must be either a lambda-expression or a compiled-code object. */
3108 funcall_lambda (Lisp_Object fun
, int nargs
, register Lisp_Object
*arg_vector
)
3110 Lisp_Object val
, syms_left
, next
;
3111 int count
= SPECPDL_INDEX ();
3112 int i
, optional
, rest
;
3116 syms_left
= XCDR (fun
);
3117 if (CONSP (syms_left
))
3118 syms_left
= XCAR (syms_left
);
3120 xsignal1 (Qinvalid_function
, fun
);
3122 else if (COMPILEDP (fun
))
3123 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3127 i
= optional
= rest
= 0;
3128 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3132 next
= XCAR (syms_left
);
3133 if (!SYMBOLP (next
))
3134 xsignal1 (Qinvalid_function
, fun
);
3136 if (EQ (next
, Qand_rest
))
3138 else if (EQ (next
, Qand_optional
))
3142 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
3146 specbind (next
, arg_vector
[i
++]);
3148 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3150 specbind (next
, Qnil
);
3153 if (!NILP (syms_left
))
3154 xsignal1 (Qinvalid_function
, fun
);
3156 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3159 val
= Fprogn (XCDR (XCDR (fun
)));
3162 /* If we have not actually read the bytecode string
3163 and constants vector yet, fetch them from the file. */
3164 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3165 Ffetch_bytecode (fun
);
3166 val
= Fbyte_code (AREF (fun
, COMPILED_BYTECODE
),
3167 AREF (fun
, COMPILED_CONSTANTS
),
3168 AREF (fun
, COMPILED_STACK_DEPTH
));
3171 return unbind_to (count
, val
);
3174 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3176 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3177 (Lisp_Object object
)
3181 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3183 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3186 tem
= AREF (object
, COMPILED_BYTECODE
);
3187 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3188 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3190 error ("Invalid byte code");
3192 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3193 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3201 register int count
= SPECPDL_INDEX ();
3202 if (specpdl_size
>= max_specpdl_size
)
3204 if (max_specpdl_size
< 400)
3205 max_specpdl_size
= 400;
3206 if (specpdl_size
>= max_specpdl_size
)
3207 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3210 if (specpdl_size
> max_specpdl_size
)
3211 specpdl_size
= max_specpdl_size
;
3212 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3213 specpdl_ptr
= specpdl
+ count
;
3216 /* specpdl_ptr->symbol is a field which describes which variable is
3217 let-bound, so it can be properly undone when we unbind_to.
3218 It can have the following two shapes:
3219 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3220 a symbol that is not buffer-local (at least at the time
3221 the let binding started). Note also that it should not be
3222 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3224 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3225 variable SYMBOL which can be buffer-local. WHERE tells us
3226 which buffer is affected (or nil if the let-binding affects the
3227 global value of the variable) and BUFFER tells us which buffer was
3228 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3229 BUFFER did not yet have a buffer-local value). */
3232 specbind (Lisp_Object symbol
, Lisp_Object value
)
3234 struct Lisp_Symbol
*sym
;
3236 eassert (!handling_signal
);
3238 CHECK_SYMBOL (symbol
);
3239 sym
= XSYMBOL (symbol
);
3240 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3244 switch (sym
->redirect
)
3246 case SYMBOL_VARALIAS
:
3247 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3248 case SYMBOL_PLAINVAL
:
3249 /* The most common case is that of a non-constant symbol with a
3250 trivial value. Make that as fast as we can. */
3251 specpdl_ptr
->symbol
= symbol
;
3252 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3253 specpdl_ptr
->func
= NULL
;
3256 SET_SYMBOL_VAL (sym
, value
);
3258 set_internal (symbol
, value
, Qnil
, 1);
3260 case SYMBOL_LOCALIZED
:
3261 if (SYMBOL_BLV (sym
)->frame_local
)
3262 error ("Frame-local vars cannot be let-bound");
3263 case SYMBOL_FORWARDED
:
3265 Lisp_Object ovalue
= find_symbol_value (symbol
);
3266 specpdl_ptr
->func
= 0;
3267 specpdl_ptr
->old_value
= ovalue
;
3269 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3270 || (EQ (SYMBOL_BLV (sym
)->where
,
3271 SYMBOL_BLV (sym
)->frame_local
?
3272 Fselected_frame () : Fcurrent_buffer ())));
3274 if (sym
->redirect
== SYMBOL_LOCALIZED
3275 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3277 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3279 /* For a local variable, record both the symbol and which
3280 buffer's or frame's value we are saving. */
3281 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3283 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3284 || (BLV_FOUND (SYMBOL_BLV (sym
))
3285 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3288 else if (sym
->redirect
== SYMBOL_LOCALIZED
3289 && BLV_FOUND (SYMBOL_BLV (sym
)))
3290 where
= SYMBOL_BLV (sym
)->where
;
3294 /* We're not using the `unused' slot in the specbinding
3295 structure because this would mean we have to do more
3296 work for simple variables. */
3297 /* FIXME: The third value `current_buffer' is only used in
3298 let_shadows_buffer_binding_p which is itself only used
3299 in set_internal for local_if_set. */
3300 eassert (NILP (where
) || EQ (where
, cur_buf
));
3301 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3303 /* If SYMBOL is a per-buffer variable which doesn't have a
3304 buffer-local value here, make the `let' change the global
3305 value by changing the value of SYMBOL in all buffers not
3306 having their own value. This is consistent with what
3307 happens with other buffer-local variables. */
3309 && sym
->redirect
== SYMBOL_FORWARDED
)
3311 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3313 Fset_default (symbol
, value
);
3318 specpdl_ptr
->symbol
= symbol
;
3321 set_internal (symbol
, value
, Qnil
, 1);
3329 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3331 eassert (!handling_signal
);
3333 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3335 specpdl_ptr
->func
= function
;
3336 specpdl_ptr
->symbol
= Qnil
;
3337 specpdl_ptr
->old_value
= arg
;
3342 unbind_to (int count
, Lisp_Object value
)
3344 Lisp_Object quitf
= Vquit_flag
;
3345 struct gcpro gcpro1
, gcpro2
;
3347 GCPRO2 (value
, quitf
);
3350 while (specpdl_ptr
!= specpdl
+ count
)
3352 /* Copy the binding, and decrement specpdl_ptr, before we do
3353 the work to unbind it. We decrement first
3354 so that an error in unbinding won't try to unbind
3355 the same entry again, and we copy the binding first
3356 in case more bindings are made during some of the code we run. */
3358 struct specbinding this_binding
;
3359 this_binding
= *--specpdl_ptr
;
3361 if (this_binding
.func
!= 0)
3362 (*this_binding
.func
) (this_binding
.old_value
);
3363 /* If the symbol is a list, it is really (SYMBOL WHERE
3364 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3365 frame. If WHERE is a buffer or frame, this indicates we
3366 bound a variable that had a buffer-local or frame-local
3367 binding. WHERE nil means that the variable had the default
3368 value when it was bound. CURRENT-BUFFER is the buffer that
3369 was current when the variable was bound. */
3370 else if (CONSP (this_binding
.symbol
))
3372 Lisp_Object symbol
, where
;
3374 symbol
= XCAR (this_binding
.symbol
);
3375 where
= XCAR (XCDR (this_binding
.symbol
));
3378 Fset_default (symbol
, this_binding
.old_value
);
3379 /* If `where' is non-nil, reset the value in the appropriate
3380 local binding, but only if that binding still exists. */
3381 else if (BUFFERP (where
)
3382 ? !NILP (Flocal_variable_p (symbol
, where
))
3383 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3384 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3386 /* If variable has a trivial value (no forwarding), we can
3387 just set it. No need to check for constant symbols here,
3388 since that was already done by specbind. */
3389 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3390 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3391 this_binding
.old_value
);
3393 /* NOTE: we only ever come here if make_local_foo was used for
3394 the first time on this var within this let. */
3395 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3398 if (NILP (Vquit_flag
) && !NILP (quitf
))
3405 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3406 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3407 The debugger is entered when that frame exits, if the flag is non-nil. */)
3408 (Lisp_Object level
, Lisp_Object flag
)
3410 register struct backtrace
*backlist
= backtrace_list
;
3413 CHECK_NUMBER (level
);
3415 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3417 backlist
= backlist
->next
;
3421 backlist
->debug_on_exit
= !NILP (flag
);
3426 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3427 doc
: /* Print a trace of Lisp function calls currently active.
3428 Output stream used is value of `standard-output'. */)
3431 register struct backtrace
*backlist
= backtrace_list
;
3435 extern Lisp_Object Vprint_level
;
3436 struct gcpro gcpro1
;
3438 XSETFASTINT (Vprint_level
, 3);
3445 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3446 if (backlist
->nargs
== UNEVALLED
)
3448 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3449 write_string ("\n", -1);
3453 tem
= *backlist
->function
;
3454 Fprin1 (tem
, Qnil
); /* This can QUIT */
3455 write_string ("(", -1);
3456 if (backlist
->nargs
== MANY
)
3458 for (tail
= *backlist
->args
, i
= 0;
3460 tail
= Fcdr (tail
), i
++)
3462 if (i
) write_string (" ", -1);
3463 Fprin1 (Fcar (tail
), Qnil
);
3468 for (i
= 0; i
< backlist
->nargs
; i
++)
3470 if (i
) write_string (" ", -1);
3471 Fprin1 (backlist
->args
[i
], Qnil
);
3474 write_string (")\n", -1);
3476 backlist
= backlist
->next
;
3479 Vprint_level
= Qnil
;
3484 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3485 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3486 If that frame has not evaluated the arguments yet (or is a special form),
3487 the value is (nil FUNCTION ARG-FORMS...).
3488 If that frame has evaluated its arguments and called its function already,
3489 the value is (t FUNCTION ARG-VALUES...).
3490 A &rest arg is represented as the tail of the list ARG-VALUES.
3491 FUNCTION is whatever was supplied as car of evaluated list,
3492 or a lambda expression for macro calls.
3493 If NFRAMES is more than the number of frames, the value is nil. */)
3494 (Lisp_Object nframes
)
3496 register struct backtrace
*backlist
= backtrace_list
;
3500 CHECK_NATNUM (nframes
);
3502 /* Find the frame requested. */
3503 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3504 backlist
= backlist
->next
;
3508 if (backlist
->nargs
== UNEVALLED
)
3509 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3512 if (backlist
->nargs
== MANY
)
3513 tem
= *backlist
->args
;
3515 tem
= Flist (backlist
->nargs
, backlist
->args
);
3517 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3523 mark_backtrace (void)
3525 register struct backtrace
*backlist
;
3528 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3530 mark_object (*backlist
->function
);
3532 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
3535 i
= backlist
->nargs
- 1;
3537 mark_object (backlist
->args
[i
]);
3544 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
3545 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3546 If Lisp code tries to increase the total number past this amount,
3547 an error is signaled.
3548 You can safely use a value considerably larger than the default value,
3549 if that proves inconveniently small. However, if you increase it too far,
3550 Emacs could run out of memory trying to make the stack bigger. */);
3552 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
3553 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3555 This limit serves to catch infinite recursions for you before they cause
3556 actual stack overflow in C, which would be fatal for Emacs.
3557 You can safely make it considerably larger than its default value,
3558 if that proves inconveniently small. However, if you increase it too far,
3559 Emacs could overflow the real C stack, and crash. */);
3561 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
3562 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3563 If the value is t, that means do an ordinary quit.
3564 If the value equals `throw-on-input', that means quit by throwing
3565 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3566 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3567 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3570 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
3571 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3572 Note that `quit-flag' will still be set by typing C-g,
3573 so a quit will be signaled as soon as `inhibit-quit' is nil.
3574 To prevent this happening, set `quit-flag' to nil
3575 before making `inhibit-quit' nil. */);
3576 Vinhibit_quit
= Qnil
;
3578 Qinhibit_quit
= intern_c_string ("inhibit-quit");
3579 staticpro (&Qinhibit_quit
);
3581 Qautoload
= intern_c_string ("autoload");
3582 staticpro (&Qautoload
);
3584 Qdebug_on_error
= intern_c_string ("debug-on-error");
3585 staticpro (&Qdebug_on_error
);
3587 Qmacro
= intern_c_string ("macro");
3588 staticpro (&Qmacro
);
3590 Qdeclare
= intern_c_string ("declare");
3591 staticpro (&Qdeclare
);
3593 /* Note that the process handling also uses Qexit, but we don't want
3594 to staticpro it twice, so we just do it here. */
3595 Qexit
= intern_c_string ("exit");
3598 Qinteractive
= intern_c_string ("interactive");
3599 staticpro (&Qinteractive
);
3601 Qcommandp
= intern_c_string ("commandp");
3602 staticpro (&Qcommandp
);
3604 Qdefun
= intern_c_string ("defun");
3605 staticpro (&Qdefun
);
3607 Qand_rest
= intern_c_string ("&rest");
3608 staticpro (&Qand_rest
);
3610 Qand_optional
= intern_c_string ("&optional");
3611 staticpro (&Qand_optional
);
3613 Qdebug
= intern_c_string ("debug");
3614 staticpro (&Qdebug
);
3616 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error
,
3617 doc
: /* *Non-nil means errors display a backtrace buffer.
3618 More precisely, this happens for any error that is handled
3619 by the editor command loop.
3620 If the value is a list, an error only means to display a backtrace
3621 if one of its condition symbols appears in the list. */);
3622 Vstack_trace_on_error
= Qnil
;
3624 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error
,
3625 doc
: /* *Non-nil means enter debugger if an error is signaled.
3626 Does not apply to errors handled by `condition-case' or those
3627 matched by `debug-ignored-errors'.
3628 If the value is a list, an error only means to enter the debugger
3629 if one of its condition symbols appears in the list.
3630 When you evaluate an expression interactively, this variable
3631 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3632 The command `toggle-debug-on-error' toggles this.
3633 See also the variable `debug-on-quit'. */);
3634 Vdebug_on_error
= Qnil
;
3636 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors
,
3637 doc
: /* *List of errors for which the debugger should not be called.
3638 Each element may be a condition-name or a regexp that matches error messages.
3639 If any element applies to a given error, that error skips the debugger
3640 and just returns to top level.
3641 This overrides the variable `debug-on-error'.
3642 It does not apply to errors handled by `condition-case'. */);
3643 Vdebug_ignored_errors
= Qnil
;
3645 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
3646 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3647 Does not apply if quit is handled by a `condition-case'. */);
3650 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
3651 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3653 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue
,
3654 doc
: /* Non-nil means debugger may continue execution.
3655 This is nil when the debugger is called under circumstances where it
3656 might not be safe to continue. */);
3657 debugger_may_continue
= 1;
3659 DEFVAR_LISP ("debugger", &Vdebugger
,
3660 doc
: /* Function to call to invoke debugger.
3661 If due to frame exit, args are `exit' and the value being returned;
3662 this function's value will be returned instead of that.
3663 If due to error, args are `error' and a list of the args to `signal'.
3664 If due to `apply' or `funcall' entry, one arg, `lambda'.
3665 If due to `eval' entry, one arg, t. */);
3668 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function
,
3669 doc
: /* If non-nil, this is a function for `signal' to call.
3670 It receives the same arguments that `signal' was given.
3671 The Edebug package uses this to regain control. */);
3672 Vsignal_hook_function
= Qnil
;
3674 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal
,
3675 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3676 Note that `debug-on-error', `debug-on-quit' and friends
3677 still determine whether to handle the particular condition. */);
3678 Vdebug_on_signal
= Qnil
;
3680 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function
,
3681 doc
: /* Function to process declarations in a macro definition.
3682 The function will be called with two args MACRO and DECL.
3683 MACRO is the name of the macro being defined.
3684 DECL is a list `(declare ...)' containing the declarations.
3685 The value the function returns is not used. */);
3686 Vmacro_declaration_function
= Qnil
;
3688 Vrun_hooks
= intern_c_string ("run-hooks");
3689 staticpro (&Vrun_hooks
);
3691 staticpro (&Vautoload_queue
);
3692 Vautoload_queue
= Qnil
;
3693 staticpro (&Vsignaling_function
);
3694 Vsignaling_function
= Qnil
;
3705 defsubr (&Sfunction
);
3707 defsubr (&Sdefmacro
);
3709 defsubr (&Sdefvaralias
);
3710 defsubr (&Sdefconst
);
3711 defsubr (&Suser_variable_p
);
3715 defsubr (&Smacroexpand
);
3718 defsubr (&Sunwind_protect
);
3719 defsubr (&Scondition_case
);
3721 defsubr (&Sinteractive_p
);
3722 defsubr (&Scalled_interactively_p
);
3723 defsubr (&Scommandp
);
3724 defsubr (&Sautoload
);
3727 defsubr (&Sfuncall
);
3728 defsubr (&Srun_hooks
);
3729 defsubr (&Srun_hook_with_args
);
3730 defsubr (&Srun_hook_with_args_until_success
);
3731 defsubr (&Srun_hook_with_args_until_failure
);
3732 defsubr (&Sfetch_bytecode
);
3733 defsubr (&Sbacktrace_debug
);
3734 defsubr (&Sbacktrace
);
3735 defsubr (&Sbacktrace_frame
);
3738 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
3739 (do not change this comment) */