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
;
67 /* This holds either the symbol `run-hooks' or nil.
68 It is nil at an early stage of startup, and when Emacs
71 Lisp_Object Vrun_hooks
;
73 /* Non-nil means record all fset's and provide's, to be undone
74 if the file being autoloaded is not fully loaded.
75 They are recorded by being consed onto the front of Vautoload_queue:
76 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
78 Lisp_Object Vautoload_queue
;
80 /* Current number of specbindings allocated in specpdl. */
84 /* Pointer to beginning of specpdl. */
86 struct specbinding
*specpdl
;
88 /* Pointer to first unused element in specpdl. */
90 struct specbinding
*specpdl_ptr
;
92 /* Maximum size allowed for specpdl allocation */
94 EMACS_INT max_specpdl_size
;
96 /* Depth in Lisp evaluations and function calls. */
100 /* Maximum allowed depth in Lisp evaluations and function calls. */
102 EMACS_INT max_lisp_eval_depth
;
104 /* Nonzero means enter debugger before next function call */
106 int debug_on_next_call
;
108 /* Non-zero means debugger may continue. This is zero when the
109 debugger is called during redisplay, where it might not be safe to
110 continue the interrupted redisplay. */
112 int debugger_may_continue
;
114 /* List of conditions (non-nil atom means all) which cause a backtrace
115 if an error is handled by the command loop's error handler. */
117 Lisp_Object Vstack_trace_on_error
;
119 /* List of conditions (non-nil atom means all) which enter the debugger
120 if an error is handled by the command loop's error handler. */
122 Lisp_Object Vdebug_on_error
;
124 /* List of conditions and regexps specifying error messages which
125 do not enter the debugger even if Vdebug_on_error says they should. */
127 Lisp_Object Vdebug_ignored_errors
;
129 /* Non-nil means call the debugger even if the error will be handled. */
131 Lisp_Object Vdebug_on_signal
;
133 /* Hook for edebug to use. */
135 Lisp_Object Vsignal_hook_function
;
137 /* Nonzero means enter debugger if a quit signal
138 is handled by the command loop's error handler. */
142 /* The value of num_nonmacro_input_events as of the last time we
143 started to enter the debugger. If we decide to enter the debugger
144 again when this is still equal to num_nonmacro_input_events, then we
145 know that the debugger itself has an error, and we should just
146 signal the error instead of entering an infinite loop of debugger
149 int when_entered_debugger
;
151 Lisp_Object Vdebugger
;
153 /* The function from which the last `signal' was called. Set in
156 Lisp_Object Vsignaling_function
;
158 /* Set to non-zero while processing X events. Checked in Feval to
159 make sure the Lisp interpreter isn't called from a signal handler,
160 which is unsafe because the interpreter isn't reentrant. */
164 /* Function to process declarations in defmacro forms. */
166 Lisp_Object Vmacro_declaration_function
;
168 static Lisp_Object
funcall_lambda (Lisp_Object
, int, Lisp_Object
*);
169 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
172 init_eval_once (void)
175 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
176 specpdl_ptr
= specpdl
;
177 /* Don't forget to update docs (lispref node "Local Variables"). */
178 max_specpdl_size
= 1000;
179 max_lisp_eval_depth
= 500;
187 specpdl_ptr
= specpdl
;
192 debug_on_next_call
= 0;
197 /* This is less than the initial value of num_nonmacro_input_events. */
198 when_entered_debugger
= -1;
201 /* unwind-protect function used by call_debugger. */
204 restore_stack_limits (Lisp_Object data
)
206 max_specpdl_size
= XINT (XCAR (data
));
207 max_lisp_eval_depth
= XINT (XCDR (data
));
211 /* Call the Lisp debugger, giving it argument ARG. */
214 call_debugger (Lisp_Object arg
)
216 int debug_while_redisplaying
;
217 int count
= SPECPDL_INDEX ();
219 int old_max
= max_specpdl_size
;
221 /* Temporarily bump up the stack limits,
222 so the debugger won't run out of stack. */
224 max_specpdl_size
+= 1;
225 record_unwind_protect (restore_stack_limits
,
226 Fcons (make_number (old_max
),
227 make_number (max_lisp_eval_depth
)));
228 max_specpdl_size
= old_max
;
230 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
231 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
233 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
234 max_specpdl_size
= SPECPDL_INDEX () + 100;
236 #ifdef HAVE_WINDOW_SYSTEM
237 if (display_hourglass_p
)
241 debug_on_next_call
= 0;
242 when_entered_debugger
= num_nonmacro_input_events
;
244 /* Resetting redisplaying_p to 0 makes sure that debug output is
245 displayed if the debugger is invoked during redisplay. */
246 debug_while_redisplaying
= redisplaying_p
;
248 specbind (intern ("debugger-may-continue"),
249 debug_while_redisplaying
? Qnil
: Qt
);
250 specbind (Qinhibit_redisplay
, Qnil
);
251 specbind (Qdebug_on_error
, Qnil
);
253 #if 0 /* Binding this prevents execution of Lisp code during
254 redisplay, which necessarily leads to display problems. */
255 specbind (Qinhibit_eval_during_redisplay
, Qt
);
258 val
= apply1 (Vdebugger
, arg
);
260 /* Interrupting redisplay and resuming it later is not safe under
261 all circumstances. So, when the debugger returns, abort the
262 interrupted redisplay by going back to the top-level. */
263 if (debug_while_redisplaying
)
266 return unbind_to (count
, val
);
270 do_debug_on_call (Lisp_Object code
)
272 debug_on_next_call
= 0;
273 backtrace_list
->debug_on_exit
= 1;
274 call_debugger (Fcons (code
, Qnil
));
277 /* NOTE!!! Every function that can call EVAL must protect its args
278 and temporaries from garbage collection while it needs them.
279 The definition of `For' shows what you have to do. */
281 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
282 doc
: /* Eval args until one of them yields non-nil, then return that value.
283 The remaining args are not evalled at all.
284 If all args return nil, return nil.
285 usage: (or CONDITIONS...) */)
288 register Lisp_Object val
= Qnil
;
295 val
= Feval (XCAR (args
));
305 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
306 doc
: /* Eval args until one of them yields nil, then return nil.
307 The remaining args are not evalled at all.
308 If no arg yields nil, return the last arg's value.
309 usage: (and CONDITIONS...) */)
312 register Lisp_Object val
= Qt
;
319 val
= Feval (XCAR (args
));
329 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
330 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
331 Returns the value of THEN or the value of the last of the ELSE's.
332 THEN must be one expression, but ELSE... can be zero or more expressions.
333 If COND yields nil, and there are no ELSE's, the value is nil.
334 usage: (if COND THEN ELSE...) */)
337 register Lisp_Object cond
;
341 cond
= Feval (Fcar (args
));
345 return Feval (Fcar (Fcdr (args
)));
346 return Fprogn (Fcdr (Fcdr (args
)));
349 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
350 doc
: /* Try each clause until one succeeds.
351 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
352 and, if the value is non-nil, this clause succeeds:
353 then the expressions in BODY are evaluated and the last one's
354 value is the value of the cond-form.
355 If no clause succeeds, cond returns nil.
356 If a clause has one element, as in (CONDITION),
357 CONDITION's value if non-nil is returned from the cond-form.
358 usage: (cond CLAUSES...) */)
361 register Lisp_Object clause
, val
;
368 clause
= Fcar (args
);
369 val
= Feval (Fcar (clause
));
372 if (!EQ (XCDR (clause
), Qnil
))
373 val
= Fprogn (XCDR (clause
));
383 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
384 doc
: /* Eval BODY forms sequentially and return value of last one.
385 usage: (progn BODY...) */)
388 register Lisp_Object val
= Qnil
;
395 val
= Feval (XCAR (args
));
403 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
404 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
405 The value of FIRST is saved during the evaluation of the remaining args,
406 whose values are discarded.
407 usage: (prog1 FIRST BODY...) */)
411 register Lisp_Object args_left
;
412 struct gcpro gcpro1
, gcpro2
;
413 register int argnum
= 0;
425 val
= Feval (Fcar (args_left
));
427 Feval (Fcar (args_left
));
428 args_left
= Fcdr (args_left
);
430 while (!NILP(args_left
));
436 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
437 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
438 The value of FORM2 is saved during the evaluation of the
439 remaining args, whose values are discarded.
440 usage: (prog2 FORM1 FORM2 BODY...) */)
444 register Lisp_Object args_left
;
445 struct gcpro gcpro1
, gcpro2
;
446 register int argnum
= -1;
460 val
= Feval (Fcar (args_left
));
462 Feval (Fcar (args_left
));
463 args_left
= Fcdr (args_left
);
465 while (!NILP (args_left
));
471 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
472 doc
: /* Set each SYM to the value of its VAL.
473 The symbols SYM are variables; they are literal (not evaluated).
474 The values VAL are expressions; they are evaluated.
475 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
476 The second VAL is not computed until after the first SYM is set, and so on;
477 each VAL can use the new value of variables set earlier in the `setq'.
478 The return value of the `setq' form is the value of the last VAL.
479 usage: (setq [SYM VAL]...) */)
482 register Lisp_Object args_left
;
483 register Lisp_Object val
, sym
;
494 val
= Feval (Fcar (Fcdr (args_left
)));
495 sym
= Fcar (args_left
);
497 args_left
= Fcdr (Fcdr (args_left
));
499 while (!NILP(args_left
));
505 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
506 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
507 usage: (quote ARG) */)
510 if (!NILP (Fcdr (args
)))
511 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
515 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
516 doc
: /* Like `quote', but preferred for objects which are functions.
517 In byte compilation, `function' causes its argument to be compiled.
518 `quote' cannot do that.
519 usage: (function ARG) */)
522 if (!NILP (Fcdr (args
)))
523 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
528 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
529 doc
: /* Return t if the containing function was run directly by user input.
530 This means that the function was called with `call-interactively'
531 \(which includes being called as the binding of a key)
532 and input is currently coming from the keyboard (not a keyboard macro),
533 and Emacs is not running in batch mode (`noninteractive' is nil).
535 The only known proper use of `interactive-p' is in deciding whether to
536 display a helpful message, or how to display it. If you're thinking
537 of using it for any other purpose, it is quite likely that you're
538 making a mistake. Think: what do you want to do when the command is
539 called from a keyboard macro?
541 To test whether your function was called with `call-interactively',
542 either (i) add an extra optional argument and give it an `interactive'
543 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
544 use `called-interactively-p'. */)
547 return (INTERACTIVE
&& interactive_p (1)) ? Qt
: Qnil
;
551 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
552 doc
: /* Return t if the containing function was called by `call-interactively'.
553 If KIND is `interactive', then only return t if the call was made
554 interactively by the user, i.e. not in `noninteractive' mode nor
555 when `executing-kbd-macro'.
556 If KIND is `any', on the other hand, it will return t for any kind of
557 interactive call, including being called as the binding of a key, or
558 from a keyboard macro, or in `noninteractive' mode.
560 The only known proper use of `interactive' for KIND is in deciding
561 whether to display a helpful message, or how to display it. If you're
562 thinking of using it for any other purpose, it is quite likely that
563 you're making a mistake. Think: what do you want to do when the
564 command is called from a keyboard macro?
566 This function is meant for implementing advice and other
567 function-modifying features. Instead of using this, it is sometimes
568 cleaner to give your function an extra optional argument whose
569 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
570 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
573 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
574 && interactive_p (1)) ? Qt
: Qnil
;
578 /* Return 1 if function in which this appears was called using
581 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
582 called is a built-in. */
585 interactive_p (int exclude_subrs_p
)
587 struct backtrace
*btp
;
590 btp
= backtrace_list
;
592 /* If this isn't a byte-compiled function, there may be a frame at
593 the top for Finteractive_p. If so, skip it. */
594 fun
= Findirect_function (*btp
->function
, Qnil
);
595 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
596 || XSUBR (fun
) == &Scalled_interactively_p
))
599 /* If we're running an Emacs 18-style byte-compiled function, there
600 may be a frame for Fbytecode at the top level. In any version of
601 Emacs there can be Fbytecode frames for subexpressions evaluated
602 inside catch and condition-case. Skip past them.
604 If this isn't a byte-compiled function, then we may now be
605 looking at several frames for special forms. Skip past them. */
607 && (EQ (*btp
->function
, Qbytecode
)
608 || btp
->nargs
== UNEVALLED
))
611 /* btp now points at the frame of the innermost function that isn't
612 a special form, ignoring frames for Finteractive_p and/or
613 Fbytecode at the top. If this frame is for a built-in function
614 (such as load or eval-region) return nil. */
615 fun
= Findirect_function (*btp
->function
, Qnil
);
616 if (exclude_subrs_p
&& SUBRP (fun
))
619 /* btp points to the frame of a Lisp function that called interactive-p.
620 Return t if that function was called interactively. */
621 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
627 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
628 doc
: /* Define NAME as a function.
629 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
630 See also the function `interactive'.
631 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
634 register Lisp_Object fn_name
;
635 register Lisp_Object defn
;
637 fn_name
= Fcar (args
);
638 CHECK_SYMBOL (fn_name
);
639 defn
= Fcons (Qlambda
, Fcdr (args
));
640 if (!NILP (Vpurify_flag
))
641 defn
= Fpurecopy (defn
);
642 if (CONSP (XSYMBOL (fn_name
)->function
)
643 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
644 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
645 Ffset (fn_name
, defn
);
646 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
650 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
651 doc
: /* Define NAME as a macro.
652 The actual definition looks like
653 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
654 When the macro is called, as in (NAME ARGS...),
655 the function (lambda ARGLIST BODY...) is applied to
656 the list ARGS... as it appears in the expression,
657 and the result should be a form to be evaluated instead of the original.
659 DECL is a declaration, optional, which can specify how to indent
660 calls to this macro, how Edebug should handle it, and which argument
661 should be treated as documentation. It looks like this:
663 The elements can look like this:
665 Set NAME's `lisp-indent-function' property to INDENT.
668 Set NAME's `edebug-form-spec' property to DEBUG. (This is
669 equivalent to writing a `def-edebug-spec' for the macro.)
672 Set NAME's `doc-string-elt' property to ELT.
674 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
677 register Lisp_Object fn_name
;
678 register Lisp_Object defn
;
679 Lisp_Object lambda_list
, doc
, tail
;
681 fn_name
= Fcar (args
);
682 CHECK_SYMBOL (fn_name
);
683 lambda_list
= Fcar (Fcdr (args
));
684 tail
= Fcdr (Fcdr (args
));
687 if (STRINGP (Fcar (tail
)))
693 while (CONSP (Fcar (tail
))
694 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
696 if (!NILP (Vmacro_declaration_function
))
700 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
708 tail
= Fcons (lambda_list
, tail
);
710 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
711 defn
= Fcons (Qmacro
, Fcons (Qlambda
, tail
));
713 if (!NILP (Vpurify_flag
))
714 defn
= Fpurecopy (defn
);
715 if (CONSP (XSYMBOL (fn_name
)->function
)
716 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
717 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
718 Ffset (fn_name
, defn
);
719 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
724 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
725 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
726 Aliased variables always have the same value; setting one sets the other.
727 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
728 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
729 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
730 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
731 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
732 The return value is BASE-VARIABLE. */)
733 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
735 struct Lisp_Symbol
*sym
;
737 CHECK_SYMBOL (new_alias
);
738 CHECK_SYMBOL (base_variable
);
740 sym
= XSYMBOL (new_alias
);
743 /* Not sure why, but why not? */
744 error ("Cannot make a constant an alias");
746 switch (sym
->redirect
)
748 case SYMBOL_FORWARDED
:
749 error ("Cannot make an internal variable an alias");
750 case SYMBOL_LOCALIZED
:
751 error ("Don't know how to make a localized variable an alias");
754 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
755 If n_a is bound, but b_v is not, set the value of b_v to n_a,
756 so that old-code that affects n_a before the aliasing is setup
758 if (NILP (Fboundp (base_variable
)))
759 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
762 struct specbinding
*p
;
764 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
767 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
768 error ("Don't know how to make a let-bound variable an alias");
771 sym
->redirect
= SYMBOL_VARALIAS
;
772 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
773 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
774 LOADHIST_ATTACH (new_alias
);
775 /* Even if docstring is nil: remove old docstring. */
776 Fput (new_alias
, Qvariable_documentation
, docstring
);
778 return base_variable
;
782 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
783 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
784 You are not required to define a variable in order to use it,
785 but the definition can supply documentation and an initial value
786 in a way that tags can recognize.
788 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
789 If SYMBOL is buffer-local, its default value is what is set;
790 buffer-local values are not affected.
791 INITVALUE and DOCSTRING are optional.
792 If DOCSTRING starts with *, this variable is identified as a user option.
793 This means that M-x set-variable recognizes it.
794 See also `user-variable-p'.
795 If INITVALUE is missing, SYMBOL's value is not set.
797 If SYMBOL has a local binding, then this form affects the local
798 binding. This is usually not what you want. Thus, if you need to
799 load a file defining variables, with this form or with `defconst' or
800 `defcustom', you should always load that file _outside_ any bindings
801 for these variables. \(`defconst' and `defcustom' behave similarly in
803 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
806 register Lisp_Object sym
, tem
, tail
;
810 if (!NILP (Fcdr (Fcdr (tail
))))
811 error ("Too many arguments");
813 tem
= Fdefault_boundp (sym
);
816 if (SYMBOL_CONSTANT_P (sym
))
818 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
819 Lisp_Object tem
= Fcar (tail
);
821 && EQ (XCAR (tem
), Qquote
)
822 && CONSP (XCDR (tem
))
823 && EQ (XCAR (XCDR (tem
)), sym
)))
824 error ("Constant symbol `%s' specified in defvar",
825 SDATA (SYMBOL_NAME (sym
)));
829 Fset_default (sym
, Feval (Fcar (tail
)));
831 { /* Check if there is really a global binding rather than just a let
832 binding that shadows the global unboundness of the var. */
833 volatile struct specbinding
*pdl
= specpdl_ptr
;
834 while (--pdl
>= specpdl
)
836 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
837 && EQ (pdl
->old_value
, Qunbound
))
839 message_with_string ("Warning: defvar ignored because %s is let-bound",
840 SYMBOL_NAME (sym
), 1);
849 if (!NILP (Vpurify_flag
))
850 tem
= Fpurecopy (tem
);
851 Fput (sym
, Qvariable_documentation
, tem
);
853 LOADHIST_ATTACH (sym
);
856 /* Simple (defvar <var>) should not count as a definition at all.
857 It could get in the way of other definitions, and unloading this
858 package could try to make the variable unbound. */
864 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
865 doc
: /* Define SYMBOL as a constant variable.
866 The intent is that neither programs nor users should ever change this value.
867 Always sets the value of SYMBOL to the result of evalling INITVALUE.
868 If SYMBOL is buffer-local, its default value is what is set;
869 buffer-local values are not affected.
870 DOCSTRING is optional.
872 If SYMBOL has a local binding, then this form sets the local binding's
873 value. However, you should normally not make local bindings for
874 variables defined with this form.
875 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
878 register Lisp_Object sym
, tem
;
881 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
882 error ("Too many arguments");
884 tem
= Feval (Fcar (Fcdr (args
)));
885 if (!NILP (Vpurify_flag
))
886 tem
= Fpurecopy (tem
);
887 Fset_default (sym
, tem
);
888 tem
= Fcar (Fcdr (Fcdr (args
)));
891 if (!NILP (Vpurify_flag
))
892 tem
= Fpurecopy (tem
);
893 Fput (sym
, Qvariable_documentation
, tem
);
895 Fput (sym
, Qrisky_local_variable
, Qt
);
896 LOADHIST_ATTACH (sym
);
900 /* Error handler used in Fuser_variable_p. */
902 user_variable_p_eh (Lisp_Object ignore
)
908 lisp_indirect_variable (Lisp_Object sym
)
910 XSETSYMBOL (sym
, indirect_variable (XSYMBOL (sym
)));
914 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
915 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
916 \(The alternative is a variable used internally in a Lisp program.)
917 A variable is a user variable if
918 \(1) the first character of its documentation is `*', or
919 \(2) it is customizable (its property list contains a non-nil value
920 of `standard-value' or `custom-autoload'), or
921 \(3) it is an alias for another user variable.
922 Return nil if VARIABLE is an alias and there is a loop in the
923 chain of symbols. */)
924 (Lisp_Object variable
)
926 Lisp_Object documentation
;
928 if (!SYMBOLP (variable
))
931 /* If indirect and there's an alias loop, don't check anything else. */
932 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
933 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
934 Qt
, user_variable_p_eh
)))
939 documentation
= Fget (variable
, Qvariable_documentation
);
940 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
942 if (STRINGP (documentation
)
943 && ((unsigned char) SREF (documentation
, 0) == '*'))
945 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
946 if (CONSP (documentation
)
947 && STRINGP (XCAR (documentation
))
948 && INTEGERP (XCDR (documentation
))
949 && XINT (XCDR (documentation
)) < 0)
951 /* Customizable? See `custom-variable-p'. */
952 if ((!NILP (Fget (variable
, intern ("standard-value"))))
953 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
956 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
959 /* An indirect variable? Let's follow the chain. */
960 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
964 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
965 doc
: /* Bind variables according to VARLIST then eval BODY.
966 The value of the last form in BODY is returned.
967 Each element of VARLIST is a symbol (which is bound to nil)
968 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
969 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
970 usage: (let* VARLIST BODY...) */)
973 Lisp_Object varlist
, val
, elt
;
974 int count
= SPECPDL_INDEX ();
975 struct gcpro gcpro1
, gcpro2
, gcpro3
;
977 GCPRO3 (args
, elt
, varlist
);
979 varlist
= Fcar (args
);
980 while (!NILP (varlist
))
983 elt
= Fcar (varlist
);
985 specbind (elt
, Qnil
);
986 else if (! NILP (Fcdr (Fcdr (elt
))))
987 signal_error ("`let' bindings can have only one value-form", elt
);
990 val
= Feval (Fcar (Fcdr (elt
)));
991 specbind (Fcar (elt
), val
);
993 varlist
= Fcdr (varlist
);
996 val
= Fprogn (Fcdr (args
));
997 return unbind_to (count
, val
);
1000 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1001 doc
: /* Bind variables according to VARLIST then eval BODY.
1002 The value of the last form in BODY is returned.
1003 Each element of VARLIST is a symbol (which is bound to nil)
1004 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1005 All the VALUEFORMs are evalled before any symbols are bound.
1006 usage: (let VARLIST BODY...) */)
1009 Lisp_Object
*temps
, tem
;
1010 register Lisp_Object elt
, varlist
;
1011 int count
= SPECPDL_INDEX ();
1012 register int argnum
;
1013 struct gcpro gcpro1
, gcpro2
;
1015 varlist
= Fcar (args
);
1017 /* Make space to hold the values to give the bound variables */
1018 elt
= Flength (varlist
);
1019 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
1021 /* Compute the values and store them in `temps' */
1023 GCPRO2 (args
, *temps
);
1026 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1029 elt
= XCAR (varlist
);
1031 temps
[argnum
++] = Qnil
;
1032 else if (! NILP (Fcdr (Fcdr (elt
))))
1033 signal_error ("`let' bindings can have only one value-form", elt
);
1035 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
1036 gcpro2
.nvars
= argnum
;
1040 varlist
= Fcar (args
);
1041 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1043 elt
= XCAR (varlist
);
1044 tem
= temps
[argnum
++];
1046 specbind (elt
, tem
);
1048 specbind (Fcar (elt
), tem
);
1051 elt
= Fprogn (Fcdr (args
));
1052 return unbind_to (count
, elt
);
1055 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1056 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1057 The order of execution is thus TEST, BODY, TEST, BODY and so on
1058 until TEST returns nil.
1059 usage: (while TEST BODY...) */)
1062 Lisp_Object test
, body
;
1063 struct gcpro gcpro1
, gcpro2
;
1065 GCPRO2 (test
, body
);
1069 while (!NILP (Feval (test
)))
1079 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1080 doc
: /* Return result of expanding macros at top level of FORM.
1081 If FORM is not a macro call, it is returned unchanged.
1082 Otherwise, the macro is expanded and the expansion is considered
1083 in place of FORM. When a non-macro-call results, it is returned.
1085 The second optional arg ENVIRONMENT specifies an environment of macro
1086 definitions to shadow the loaded ones for use in file byte-compilation. */)
1087 (Lisp_Object form
, Lisp_Object environment
)
1089 /* With cleanups from Hallvard Furuseth. */
1090 register Lisp_Object expander
, sym
, def
, tem
;
1094 /* Come back here each time we expand a macro call,
1095 in case it expands into another macro call. */
1098 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1099 def
= sym
= XCAR (form
);
1101 /* Trace symbols aliases to other symbols
1102 until we get a symbol that is not an alias. */
1103 while (SYMBOLP (def
))
1107 tem
= Fassq (sym
, environment
);
1110 def
= XSYMBOL (sym
)->function
;
1111 if (!EQ (def
, Qunbound
))
1116 /* Right now TEM is the result from SYM in ENVIRONMENT,
1117 and if TEM is nil then DEF is SYM's function definition. */
1120 /* SYM is not mentioned in ENVIRONMENT.
1121 Look at its function definition. */
1122 if (EQ (def
, Qunbound
) || !CONSP (def
))
1123 /* Not defined or definition not suitable */
1125 if (EQ (XCAR (def
), Qautoload
))
1127 /* Autoloading function: will it be a macro when loaded? */
1128 tem
= Fnth (make_number (4), def
);
1129 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1130 /* Yes, load it and try again. */
1132 struct gcpro gcpro1
;
1134 do_autoload (def
, sym
);
1141 else if (!EQ (XCAR (def
), Qmacro
))
1143 else expander
= XCDR (def
);
1147 expander
= XCDR (tem
);
1148 if (NILP (expander
))
1151 form
= apply1 (expander
, XCDR (form
));
1156 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1157 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1158 TAG is evalled to get the tag to use; it must not be nil.
1160 Then the BODY is executed.
1161 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1162 If no throw happens, `catch' returns the value of the last BODY form.
1163 If a throw happens, it specifies the value to return from `catch'.
1164 usage: (catch TAG BODY...) */)
1167 register Lisp_Object tag
;
1168 struct gcpro gcpro1
;
1171 tag
= Feval (Fcar (args
));
1173 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1176 /* Set up a catch, then call C function FUNC on argument ARG.
1177 FUNC should return a Lisp_Object.
1178 This is how catches are done from within C code. */
1181 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1183 /* This structure is made part of the chain `catchlist'. */
1186 /* Fill in the components of c, and put it on the list. */
1190 c
.backlist
= backtrace_list
;
1191 c
.handlerlist
= handlerlist
;
1192 c
.lisp_eval_depth
= lisp_eval_depth
;
1193 c
.pdlcount
= SPECPDL_INDEX ();
1194 c
.poll_suppress_count
= poll_suppress_count
;
1195 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1196 c
.gcpro
= gcprolist
;
1197 c
.byte_stack
= byte_stack_list
;
1201 if (! _setjmp (c
.jmp
))
1202 c
.val
= (*func
) (arg
);
1204 /* Throw works by a longjmp that comes right here. */
1209 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1210 jump to that CATCH, returning VALUE as the value of that catch.
1212 This is the guts Fthrow and Fsignal; they differ only in the way
1213 they choose the catch tag to throw to. A catch tag for a
1214 condition-case form has a TAG of Qnil.
1216 Before each catch is discarded, unbind all special bindings and
1217 execute all unwind-protect clauses made above that catch. Unwind
1218 the handler stack as we go, so that the proper handlers are in
1219 effect for each unwind-protect clause we run. At the end, restore
1220 some static info saved in CATCH, and longjmp to the location
1223 This is used for correct unwinding in Fthrow and Fsignal. */
1226 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1228 register int last_time
;
1230 /* Save the value in the tag. */
1233 /* Restore certain special C variables. */
1234 set_poll_suppress_count (catch->poll_suppress_count
);
1235 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1236 handling_signal
= 0;
1241 last_time
= catchlist
== catch;
1243 /* Unwind the specpdl stack, and then restore the proper set of
1245 unbind_to (catchlist
->pdlcount
, Qnil
);
1246 handlerlist
= catchlist
->handlerlist
;
1247 catchlist
= catchlist
->next
;
1249 while (! last_time
);
1252 /* If x_catch_errors was done, turn it off now.
1253 (First we give unbind_to a chance to do that.) */
1254 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1255 The catch must remain in effect during that delicate
1256 state. --lorentey */
1257 x_fully_uncatch_errors ();
1261 byte_stack_list
= catch->byte_stack
;
1262 gcprolist
= catch->gcpro
;
1265 gcpro_level
= gcprolist
->level
+ 1;
1269 backtrace_list
= catch->backlist
;
1270 lisp_eval_depth
= catch->lisp_eval_depth
;
1272 _longjmp (catch->jmp
, 1);
1275 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1276 doc
: /* Throw to the catch for TAG and return VALUE from it.
1277 Both TAG and VALUE are evalled. */)
1278 (register Lisp_Object tag
, Lisp_Object value
)
1280 register struct catchtag
*c
;
1283 for (c
= catchlist
; c
; c
= c
->next
)
1285 if (EQ (c
->tag
, tag
))
1286 unwind_to_catch (c
, value
);
1288 xsignal2 (Qno_catch
, tag
, value
);
1292 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1293 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1294 If BODYFORM completes normally, its value is returned
1295 after executing the UNWINDFORMS.
1296 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1297 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1301 int count
= SPECPDL_INDEX ();
1303 record_unwind_protect (Fprogn
, Fcdr (args
));
1304 val
= Feval (Fcar (args
));
1305 return unbind_to (count
, val
);
1308 /* Chain of condition handlers currently in effect.
1309 The elements of this chain are contained in the stack frames
1310 of Fcondition_case and internal_condition_case.
1311 When an error is signaled (by calling Fsignal, below),
1312 this chain is searched for an element that applies. */
1314 struct handler
*handlerlist
;
1316 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1317 doc
: /* Regain control when an error is signaled.
1318 Executes BODYFORM and returns its value if no error happens.
1319 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1320 where the BODY is made of Lisp expressions.
1322 A handler is applicable to an error
1323 if CONDITION-NAME is one of the error's condition names.
1324 If an error happens, the first applicable handler is run.
1326 The car of a handler may be a list of condition names
1327 instead of a single condition name. Then it handles all of them.
1329 When a handler handles an error, control returns to the `condition-case'
1330 and it executes the handler's BODY...
1331 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1332 \(If VAR is nil, the handler can't access that information.)
1333 Then the value of the last BODY form is returned from the `condition-case'
1336 See also the function `signal' for more info.
1337 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1340 register Lisp_Object bodyform
, handlers
;
1341 volatile Lisp_Object var
;
1344 bodyform
= Fcar (Fcdr (args
));
1345 handlers
= Fcdr (Fcdr (args
));
1347 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1350 /* Like Fcondition_case, but the args are separate
1351 rather than passed in a list. Used by Fbyte_code. */
1354 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1355 Lisp_Object handlers
)
1363 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1369 && (SYMBOLP (XCAR (tem
))
1370 || CONSP (XCAR (tem
))))))
1371 error ("Invalid condition handler", tem
);
1376 c
.backlist
= backtrace_list
;
1377 c
.handlerlist
= handlerlist
;
1378 c
.lisp_eval_depth
= lisp_eval_depth
;
1379 c
.pdlcount
= SPECPDL_INDEX ();
1380 c
.poll_suppress_count
= poll_suppress_count
;
1381 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1382 c
.gcpro
= gcprolist
;
1383 c
.byte_stack
= byte_stack_list
;
1384 if (_setjmp (c
.jmp
))
1387 specbind (h
.var
, c
.val
);
1388 val
= Fprogn (Fcdr (h
.chosen_clause
));
1390 /* Note that this just undoes the binding of h.var; whoever
1391 longjumped to us unwound the stack to c.pdlcount before
1393 unbind_to (c
.pdlcount
, Qnil
);
1400 h
.handler
= handlers
;
1401 h
.next
= handlerlist
;
1405 val
= Feval (bodyform
);
1407 handlerlist
= h
.next
;
1411 /* Call the function BFUN with no arguments, catching errors within it
1412 according to HANDLERS. If there is an error, call HFUN with
1413 one argument which is the data that describes the error:
1416 HANDLERS can be a list of conditions to catch.
1417 If HANDLERS is Qt, catch all errors.
1418 If HANDLERS is Qerror, catch all errors
1419 but allow the debugger to run if that is enabled. */
1422 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1423 Lisp_Object (*hfun
) (Lisp_Object
))
1429 /* Since Fsignal will close off all calls to x_catch_errors,
1430 we will get the wrong results if some are not closed now. */
1432 if (x_catching_errors ())
1438 c
.backlist
= backtrace_list
;
1439 c
.handlerlist
= handlerlist
;
1440 c
.lisp_eval_depth
= lisp_eval_depth
;
1441 c
.pdlcount
= SPECPDL_INDEX ();
1442 c
.poll_suppress_count
= poll_suppress_count
;
1443 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1444 c
.gcpro
= gcprolist
;
1445 c
.byte_stack
= byte_stack_list
;
1446 if (_setjmp (c
.jmp
))
1448 return (*hfun
) (c
.val
);
1452 h
.handler
= handlers
;
1454 h
.next
= handlerlist
;
1460 handlerlist
= h
.next
;
1464 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1467 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1468 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1474 /* Since Fsignal will close off all calls to x_catch_errors,
1475 we will get the wrong results if some are not closed now. */
1477 if (x_catching_errors ())
1483 c
.backlist
= backtrace_list
;
1484 c
.handlerlist
= handlerlist
;
1485 c
.lisp_eval_depth
= lisp_eval_depth
;
1486 c
.pdlcount
= SPECPDL_INDEX ();
1487 c
.poll_suppress_count
= poll_suppress_count
;
1488 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1489 c
.gcpro
= gcprolist
;
1490 c
.byte_stack
= byte_stack_list
;
1491 if (_setjmp (c
.jmp
))
1493 return (*hfun
) (c
.val
);
1497 h
.handler
= handlers
;
1499 h
.next
= handlerlist
;
1503 val
= (*bfun
) (arg
);
1505 handlerlist
= h
.next
;
1509 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1513 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1516 Lisp_Object handlers
,
1517 Lisp_Object (*hfun
) (Lisp_Object
))
1523 /* Since Fsignal will close off all calls to x_catch_errors,
1524 we will get the wrong results if some are not closed now. */
1526 if (x_catching_errors ())
1532 c
.backlist
= backtrace_list
;
1533 c
.handlerlist
= handlerlist
;
1534 c
.lisp_eval_depth
= lisp_eval_depth
;
1535 c
.pdlcount
= SPECPDL_INDEX ();
1536 c
.poll_suppress_count
= poll_suppress_count
;
1537 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1538 c
.gcpro
= gcprolist
;
1539 c
.byte_stack
= byte_stack_list
;
1540 if (_setjmp (c
.jmp
))
1542 return (*hfun
) (c
.val
);
1546 h
.handler
= handlers
;
1548 h
.next
= handlerlist
;
1552 val
= (*bfun
) (arg1
, arg2
);
1554 handlerlist
= h
.next
;
1558 /* Like internal_condition_case but call BFUN with NARGS as first,
1559 and ARGS as second argument. */
1562 internal_condition_case_n (Lisp_Object (*bfun
) (int, Lisp_Object
*),
1565 Lisp_Object handlers
,
1566 Lisp_Object (*hfun
) (Lisp_Object
))
1572 /* Since Fsignal will close off all calls to x_catch_errors,
1573 we will get the wrong results if some are not closed now. */
1575 if (x_catching_errors ())
1581 c
.backlist
= backtrace_list
;
1582 c
.handlerlist
= handlerlist
;
1583 c
.lisp_eval_depth
= lisp_eval_depth
;
1584 c
.pdlcount
= SPECPDL_INDEX ();
1585 c
.poll_suppress_count
= poll_suppress_count
;
1586 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1587 c
.gcpro
= gcprolist
;
1588 c
.byte_stack
= byte_stack_list
;
1589 if (_setjmp (c
.jmp
))
1591 return (*hfun
) (c
.val
);
1595 h
.handler
= handlers
;
1597 h
.next
= handlerlist
;
1601 val
= (*bfun
) (nargs
, args
);
1603 handlerlist
= h
.next
;
1608 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1609 Lisp_Object
, Lisp_Object
);
1611 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1612 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1613 This function does not return.
1615 An error symbol is a symbol with an `error-conditions' property
1616 that is a list of condition names.
1617 A handler for any of those names will get to handle this signal.
1618 The symbol `error' should normally be one of them.
1620 DATA should be a list. Its elements are printed as part of the error message.
1621 See Info anchor `(elisp)Definition of signal' for some details on how this
1622 error message is constructed.
1623 If the signal is handled, DATA is made available to the handler.
1624 See also the function `condition-case'. */)
1625 (Lisp_Object error_symbol
, Lisp_Object data
)
1627 /* When memory is full, ERROR-SYMBOL is nil,
1628 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1629 That is a special case--don't do this in other situations. */
1630 register struct handler
*allhandlers
= handlerlist
;
1631 Lisp_Object conditions
;
1632 extern int waiting_for_input
;
1634 Lisp_Object real_error_symbol
;
1635 struct backtrace
*bp
;
1637 immediate_quit
= handling_signal
= 0;
1639 if (gc_in_progress
|| waiting_for_input
)
1642 if (NILP (error_symbol
))
1643 real_error_symbol
= Fcar (data
);
1645 real_error_symbol
= error_symbol
;
1647 #if 0 /* rms: I don't know why this was here,
1648 but it is surely wrong for an error that is handled. */
1649 #ifdef HAVE_WINDOW_SYSTEM
1650 if (display_hourglass_p
)
1651 cancel_hourglass ();
1655 /* This hook is used by edebug. */
1656 if (! NILP (Vsignal_hook_function
)
1657 && ! NILP (error_symbol
))
1659 /* Edebug takes care of restoring these variables when it exits. */
1660 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1661 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1663 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1664 max_specpdl_size
= SPECPDL_INDEX () + 40;
1666 call2 (Vsignal_hook_function
, error_symbol
, data
);
1669 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1671 /* Remember from where signal was called. Skip over the frame for
1672 `signal' itself. If a frame for `error' follows, skip that,
1673 too. Don't do this when ERROR_SYMBOL is nil, because that
1674 is a memory-full error. */
1675 Vsignaling_function
= Qnil
;
1676 if (backtrace_list
&& !NILP (error_symbol
))
1678 bp
= backtrace_list
->next
;
1679 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1681 if (bp
&& bp
->function
)
1682 Vsignaling_function
= *bp
->function
;
1685 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1687 register Lisp_Object clause
;
1689 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1690 error_symbol
, data
);
1692 if (EQ (clause
, Qlambda
))
1694 /* We can't return values to code which signaled an error, but we
1695 can continue code which has signaled a quit. */
1696 if (EQ (real_error_symbol
, Qquit
))
1699 error ("Cannot return from the debugger in an error");
1704 Lisp_Object unwind_data
;
1705 struct handler
*h
= handlerlist
;
1707 handlerlist
= allhandlers
;
1709 if (NILP (error_symbol
))
1712 unwind_data
= Fcons (error_symbol
, data
);
1713 h
->chosen_clause
= clause
;
1714 unwind_to_catch (h
->tag
, unwind_data
);
1718 handlerlist
= allhandlers
;
1719 /* If no handler is present now, try to run the debugger,
1720 and if that fails, throw to top level. */
1721 find_handler_clause (Qerror
, conditions
, error_symbol
, data
);
1723 Fthrow (Qtop_level
, Qt
);
1725 if (! NILP (error_symbol
))
1726 data
= Fcons (error_symbol
, data
);
1728 string
= Ferror_message_string (data
);
1729 fatal ("%s", SDATA (string
), 0);
1732 /* Internal version of Fsignal that never returns.
1733 Used for anything but Qquit (which can return from Fsignal). */
1736 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1738 Fsignal (error_symbol
, data
);
1742 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1745 xsignal0 (Lisp_Object error_symbol
)
1747 xsignal (error_symbol
, Qnil
);
1751 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1753 xsignal (error_symbol
, list1 (arg
));
1757 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1759 xsignal (error_symbol
, list2 (arg1
, arg2
));
1763 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1765 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1768 /* Signal `error' with message S, and additional arg ARG.
1769 If ARG is not a genuine list, make it a one-element list. */
1772 signal_error (const char *s
, Lisp_Object arg
)
1774 Lisp_Object tortoise
, hare
;
1776 hare
= tortoise
= arg
;
1777 while (CONSP (hare
))
1784 tortoise
= XCDR (tortoise
);
1786 if (EQ (hare
, tortoise
))
1791 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1793 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1797 /* Return nonzero if LIST is a non-nil atom or
1798 a list containing one of CONDITIONS. */
1801 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1808 while (CONSP (conditions
))
1810 Lisp_Object
this, tail
;
1811 this = XCAR (conditions
);
1812 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1813 if (EQ (XCAR (tail
), this))
1815 conditions
= XCDR (conditions
);
1820 /* Return 1 if an error with condition-symbols CONDITIONS,
1821 and described by SIGNAL-DATA, should skip the debugger
1822 according to debugger-ignored-errors. */
1825 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1828 int first_string
= 1;
1829 Lisp_Object error_message
;
1831 error_message
= Qnil
;
1832 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1834 if (STRINGP (XCAR (tail
)))
1838 error_message
= Ferror_message_string (data
);
1842 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1847 Lisp_Object contail
;
1849 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1850 if (EQ (XCAR (tail
), XCAR (contail
)))
1858 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1859 SIG and DATA describe the signal, as in find_handler_clause. */
1862 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1864 Lisp_Object combined_data
;
1866 combined_data
= Fcons (sig
, data
);
1869 /* Don't try to run the debugger with interrupts blocked.
1870 The editing loop would return anyway. */
1872 /* Does user want to enter debugger for this kind of error? */
1875 : wants_debugger (Vdebug_on_error
, conditions
))
1876 && ! skip_debugger (conditions
, combined_data
)
1877 /* rms: what's this for? */
1878 && when_entered_debugger
< num_nonmacro_input_events
)
1880 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1887 /* Value of Qlambda means we have called debugger and user has continued.
1888 There are two ways to pass SIG and DATA:
1889 = SIG is the error symbol, and DATA is the rest of the data.
1890 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1891 This is for memory-full errors only.
1893 We need to increase max_specpdl_size temporarily around
1894 anything we do that can push on the specpdl, so as not to get
1895 a second error here in case we're handling specpdl overflow. */
1898 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1899 Lisp_Object sig
, Lisp_Object data
)
1901 register Lisp_Object h
;
1902 register Lisp_Object tem
;
1903 int debugger_called
= 0;
1904 int debugger_considered
= 0;
1906 /* t is used by handlers for all conditions, set up by C code. */
1907 if (EQ (handlers
, Qt
))
1910 /* Don't run the debugger for a memory-full error.
1911 (There is no room in memory to do that!) */
1913 debugger_considered
= 1;
1915 /* error is used similarly, but means print an error message
1916 and run the debugger if that is enabled. */
1917 if (EQ (handlers
, Qerror
)
1918 || !NILP (Vdebug_on_signal
)) /* This says call debugger even if
1919 there is a handler. */
1921 if (!NILP (sig
) && wants_debugger (Vstack_trace_on_error
, conditions
))
1923 max_lisp_eval_depth
+= 15;
1928 internal_with_output_to_temp_buffer
1930 (Lisp_Object (*) (Lisp_Object
)) Fbacktrace
,
1933 max_lisp_eval_depth
-= 15;
1936 if (!debugger_considered
)
1938 debugger_considered
= 1;
1939 debugger_called
= maybe_call_debugger (conditions
, sig
, data
);
1942 /* If there is no handler, return saying whether we ran the debugger. */
1943 if (EQ (handlers
, Qerror
))
1945 if (debugger_called
)
1951 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1953 Lisp_Object handler
, condit
;
1956 if (!CONSP (handler
))
1958 condit
= Fcar (handler
);
1959 /* Handle a single condition name in handler HANDLER. */
1960 if (SYMBOLP (condit
))
1962 tem
= Fmemq (Fcar (handler
), conditions
);
1966 /* Handle a list of condition names in handler HANDLER. */
1967 else if (CONSP (condit
))
1970 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1972 tem
= Fmemq (Fcar (tail
), conditions
);
1975 /* This handler is going to apply.
1976 Does it allow the debugger to run first? */
1977 if (! debugger_considered
&& !NILP (Fmemq (Qdebug
, condit
)))
1978 maybe_call_debugger (conditions
, sig
, data
);
1989 /* dump an error message; called like vprintf */
1991 verror (const char *m
, va_list ap
)
2006 used
= doprnt (buffer
, size
, m
, m
+ mlen
, ap
);
2011 buffer
= (char *) xrealloc (buffer
, size
);
2014 buffer
= (char *) xmalloc (size
);
2019 string
= build_string (buffer
);
2023 xsignal1 (Qerror
, string
);
2027 /* dump an error message; called like printf */
2031 error (const char *m
, ...)
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
.aUNEVALLED
) (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
.aMANY
) (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
.aMANY
) (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) */