1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
35 /* This definition is duplicated in alloc.c and keyboard.c */
36 /* Putting it in lisp.h makes cc bomb out! */
40 struct backtrace
*next
;
41 Lisp_Object
*function
;
42 Lisp_Object
*args
; /* Points to vector of args. */
43 int nargs
; /* Length of vector.
44 If nargs is UNEVALLED, args points to slot holding
45 list of unevalled args */
47 /* Nonzero means call value of debugger when done with this operation. */
51 struct backtrace
*backtrace_list
;
53 struct catchtag
*catchlist
;
56 /* Count levels of GCPRO to detect failure to UNGCPRO. */
60 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
61 Lisp_Object Qinhibit_quit
, Vinhibit_quit
, Vquit_flag
;
62 Lisp_Object Qand_rest
, Qand_optional
;
63 Lisp_Object Qdebug_on_error
;
66 extern Lisp_Object Qinteractive_form
;
68 /* This holds either the symbol `run-hooks' or nil.
69 It is nil at an early stage of startup, and when Emacs
72 Lisp_Object Vrun_hooks
;
74 /* Non-nil means record all fset's and provide's, to be undone
75 if the file being autoloaded is not fully loaded.
76 They are recorded by being consed onto the front of Vautoload_queue:
77 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
79 Lisp_Object Vautoload_queue
;
81 /* Current number of specbindings allocated in specpdl. */
85 /* Pointer to beginning of specpdl. */
87 struct specbinding
*specpdl
;
89 /* Pointer to first unused element in specpdl. */
91 struct specbinding
*specpdl_ptr
;
93 /* Maximum size allowed for specpdl allocation */
95 EMACS_INT max_specpdl_size
;
97 /* Depth in Lisp evaluations and function calls. */
101 /* Maximum allowed depth in Lisp evaluations and function calls. */
103 EMACS_INT max_lisp_eval_depth
;
105 /* Nonzero means enter debugger before next function call */
107 int debug_on_next_call
;
109 /* Non-zero means debugger may continue. This is zero when the
110 debugger is called during redisplay, where it might not be safe to
111 continue the interrupted redisplay. */
113 int debugger_may_continue
;
115 /* List of conditions (non-nil atom means all) which cause a backtrace
116 if an error is handled by the command loop's error handler. */
118 Lisp_Object Vstack_trace_on_error
;
120 /* List of conditions (non-nil atom means all) which enter the debugger
121 if an error is handled by the command loop's error handler. */
123 Lisp_Object Vdebug_on_error
;
125 /* List of conditions and regexps specifying error messages which
126 do not enter the debugger even if Vdebug_on_error says they should. */
128 Lisp_Object Vdebug_ignored_errors
;
130 /* Non-nil means call the debugger even if the error will be handled. */
132 Lisp_Object Vdebug_on_signal
;
134 /* Hook for edebug to use. */
136 Lisp_Object Vsignal_hook_function
;
138 /* Nonzero means enter debugger if a quit signal
139 is handled by the command loop's error handler. */
143 /* The value of num_nonmacro_input_events as of the last time we
144 started to enter the debugger. If we decide to enter the debugger
145 again when this is still equal to num_nonmacro_input_events, then we
146 know that the debugger itself has an error, and we should just
147 signal the error instead of entering an infinite loop of debugger
150 int when_entered_debugger
;
152 Lisp_Object Vdebugger
;
154 /* The function from which the last `signal' was called. Set in
157 Lisp_Object Vsignaling_function
;
159 /* Set to non-zero while processing X events. Checked in Feval to
160 make sure the Lisp interpreter isn't called from a signal handler,
161 which is unsafe because the interpreter isn't reentrant. */
165 /* Function to process declarations in defmacro forms. */
167 Lisp_Object Vmacro_declaration_function
;
169 extern Lisp_Object Qrisky_local_variable
;
171 extern Lisp_Object Qfunction
;
173 static Lisp_Object
funcall_lambda (Lisp_Object
, int, Lisp_Object
*);
174 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
177 /* "gcc -O3" enables automatic function inlining, which optimizes out
178 the arguments for the invocations of these functions, whereas they
179 expect these values on the stack. */
180 Lisp_Object
apply1 (Lisp_Object fn
, Lisp_Object arg
) __attribute__((noinline
));
181 Lisp_Object
call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
) __attribute__((noinline
));
185 init_eval_once (void)
188 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
189 specpdl_ptr
= specpdl
;
190 /* Don't forget to update docs (lispref node "Local Variables"). */
191 max_specpdl_size
= 1000;
192 max_lisp_eval_depth
= 500;
200 specpdl_ptr
= specpdl
;
205 debug_on_next_call
= 0;
210 /* This is less than the initial value of num_nonmacro_input_events. */
211 when_entered_debugger
= -1;
214 /* unwind-protect function used by call_debugger. */
217 restore_stack_limits (Lisp_Object data
)
219 max_specpdl_size
= XINT (XCAR (data
));
220 max_lisp_eval_depth
= XINT (XCDR (data
));
224 /* Call the Lisp debugger, giving it argument ARG. */
227 call_debugger (Lisp_Object arg
)
229 int debug_while_redisplaying
;
230 int count
= SPECPDL_INDEX ();
232 int old_max
= max_specpdl_size
;
234 /* Temporarily bump up the stack limits,
235 so the debugger won't run out of stack. */
237 max_specpdl_size
+= 1;
238 record_unwind_protect (restore_stack_limits
,
239 Fcons (make_number (old_max
),
240 make_number (max_lisp_eval_depth
)));
241 max_specpdl_size
= old_max
;
243 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
244 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
246 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
247 max_specpdl_size
= SPECPDL_INDEX () + 100;
249 #ifdef HAVE_WINDOW_SYSTEM
250 if (display_hourglass_p
)
254 debug_on_next_call
= 0;
255 when_entered_debugger
= num_nonmacro_input_events
;
257 /* Resetting redisplaying_p to 0 makes sure that debug output is
258 displayed if the debugger is invoked during redisplay. */
259 debug_while_redisplaying
= redisplaying_p
;
261 specbind (intern ("debugger-may-continue"),
262 debug_while_redisplaying
? Qnil
: Qt
);
263 specbind (Qinhibit_redisplay
, Qnil
);
264 specbind (Qdebug_on_error
, Qnil
);
266 #if 0 /* Binding this prevents execution of Lisp code during
267 redisplay, which necessarily leads to display problems. */
268 specbind (Qinhibit_eval_during_redisplay
, Qt
);
271 val
= apply1 (Vdebugger
, arg
);
273 /* Interrupting redisplay and resuming it later is not safe under
274 all circumstances. So, when the debugger returns, abort the
275 interrupted redisplay by going back to the top-level. */
276 if (debug_while_redisplaying
)
279 return unbind_to (count
, val
);
283 do_debug_on_call (Lisp_Object code
)
285 debug_on_next_call
= 0;
286 backtrace_list
->debug_on_exit
= 1;
287 call_debugger (Fcons (code
, Qnil
));
290 /* NOTE!!! Every function that can call EVAL must protect its args
291 and temporaries from garbage collection while it needs them.
292 The definition of `For' shows what you have to do. */
294 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
295 doc
: /* Eval args until one of them yields non-nil, then return that value.
296 The remaining args are not evalled at all.
297 If all args return nil, return nil.
298 usage: (or CONDITIONS...) */)
302 register Lisp_Object val
= Qnil
;
309 val
= Feval (XCAR (args
));
319 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
320 doc
: /* Eval args until one of them yields nil, then return nil.
321 The remaining args are not evalled at all.
322 If no arg yields nil, return the last arg's value.
323 usage: (and CONDITIONS...) */)
327 register Lisp_Object val
= Qt
;
334 val
= Feval (XCAR (args
));
344 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
345 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
346 Returns the value of THEN or the value of the last of the ELSE's.
347 THEN must be one expression, but ELSE... can be zero or more expressions.
348 If COND yields nil, and there are no ELSE's, the value is nil.
349 usage: (if COND THEN ELSE...) */)
353 register Lisp_Object cond
;
357 cond
= Feval (Fcar (args
));
361 return Feval (Fcar (Fcdr (args
)));
362 return Fprogn (Fcdr (Fcdr (args
)));
365 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
366 doc
: /* Try each clause until one succeeds.
367 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
368 and, if the value is non-nil, this clause succeeds:
369 then the expressions in BODY are evaluated and the last one's
370 value is the value of the cond-form.
371 If no clause succeeds, cond returns nil.
372 If a clause has one element, as in (CONDITION),
373 CONDITION's value if non-nil is returned from the cond-form.
374 usage: (cond CLAUSES...) */)
378 register Lisp_Object clause
, val
;
385 clause
= Fcar (args
);
386 val
= Feval (Fcar (clause
));
389 if (!EQ (XCDR (clause
), Qnil
))
390 val
= Fprogn (XCDR (clause
));
400 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
401 doc
: /* Eval BODY forms sequentially and return value of last one.
402 usage: (progn BODY...) */)
406 register Lisp_Object val
= Qnil
;
413 val
= Feval (XCAR (args
));
421 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
422 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
423 The value of FIRST is saved during the evaluation of the remaining args,
424 whose values are discarded.
425 usage: (prog1 FIRST BODY...) */)
430 register Lisp_Object args_left
;
431 struct gcpro gcpro1
, gcpro2
;
432 register int argnum
= 0;
444 val
= Feval (Fcar (args_left
));
446 Feval (Fcar (args_left
));
447 args_left
= Fcdr (args_left
);
449 while (!NILP(args_left
));
455 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
456 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
457 The value of FORM2 is saved during the evaluation of the
458 remaining args, whose values are discarded.
459 usage: (prog2 FORM1 FORM2 BODY...) */)
464 register Lisp_Object args_left
;
465 struct gcpro gcpro1
, gcpro2
;
466 register int argnum
= -1;
480 val
= Feval (Fcar (args_left
));
482 Feval (Fcar (args_left
));
483 args_left
= Fcdr (args_left
);
485 while (!NILP (args_left
));
491 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
492 doc
: /* Set each SYM to the value of its VAL.
493 The symbols SYM are variables; they are literal (not evaluated).
494 The values VAL are expressions; they are evaluated.
495 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
496 The second VAL is not computed until after the first SYM is set, and so on;
497 each VAL can use the new value of variables set earlier in the `setq'.
498 The return value of the `setq' form is the value of the last VAL.
499 usage: (setq [SYM VAL]...) */)
503 register Lisp_Object args_left
;
504 register Lisp_Object val
, sym
;
515 val
= Feval (Fcar (Fcdr (args_left
)));
516 sym
= Fcar (args_left
);
518 args_left
= Fcdr (Fcdr (args_left
));
520 while (!NILP(args_left
));
526 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
527 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
528 usage: (quote ARG) */)
532 if (!NILP (Fcdr (args
)))
533 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
537 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
538 doc
: /* Like `quote', but preferred for objects which are functions.
539 In byte compilation, `function' causes its argument to be compiled.
540 `quote' cannot do that.
541 usage: (function ARG) */)
545 if (!NILP (Fcdr (args
)))
546 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
551 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
552 doc
: /* Return t if the containing function was run directly by user input.
553 This means that the function was called with `call-interactively'
554 \(which includes being called as the binding of a key)
555 and input is currently coming from the keyboard (not a keyboard macro),
556 and Emacs is not running in batch mode (`noninteractive' is nil).
558 The only known proper use of `interactive-p' is in deciding whether to
559 display a helpful message, or how to display it. If you're thinking
560 of using it for any other purpose, it is quite likely that you're
561 making a mistake. Think: what do you want to do when the command is
562 called from a keyboard macro?
564 To test whether your function was called with `call-interactively',
565 either (i) add an extra optional argument and give it an `interactive'
566 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
567 use `called-interactively-p'. */)
570 return (INTERACTIVE
&& interactive_p (1)) ? Qt
: Qnil
;
574 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
575 doc
: /* Return t if the containing function was called by `call-interactively'.
576 If KIND is `interactive', then only return t if the call was made
577 interactively by the user, i.e. not in `noninteractive' mode nor
578 when `executing-kbd-macro'.
579 If KIND is `any', on the other hand, it will return t for any kind of
580 interactive call, including being called as the binding of a key, or
581 from a keyboard macro, or in `noninteractive' mode.
583 The only known proper use of `interactive' for KIND is in deciding
584 whether to display a helpful message, or how to display it. If you're
585 thinking of using it for any other purpose, it is quite likely that
586 you're making a mistake. Think: what do you want to do when the
587 command is called from a keyboard macro?
589 This function is meant for implementing advice and other
590 function-modifying features. Instead of using this, it is sometimes
591 cleaner to give your function an extra optional argument whose
592 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
593 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
597 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
598 && interactive_p (1)) ? Qt
: Qnil
;
602 /* Return 1 if function in which this appears was called using
605 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
606 called is a built-in. */
609 interactive_p (int exclude_subrs_p
)
611 struct backtrace
*btp
;
614 btp
= backtrace_list
;
616 /* If this isn't a byte-compiled function, there may be a frame at
617 the top for Finteractive_p. If so, skip it. */
618 fun
= Findirect_function (*btp
->function
, Qnil
);
619 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
620 || XSUBR (fun
) == &Scalled_interactively_p
))
623 /* If we're running an Emacs 18-style byte-compiled function, there
624 may be a frame for Fbytecode at the top level. In any version of
625 Emacs there can be Fbytecode frames for subexpressions evaluated
626 inside catch and condition-case. Skip past them.
628 If this isn't a byte-compiled function, then we may now be
629 looking at several frames for special forms. Skip past them. */
631 && (EQ (*btp
->function
, Qbytecode
)
632 || btp
->nargs
== UNEVALLED
))
635 /* btp now points at the frame of the innermost function that isn't
636 a special form, ignoring frames for Finteractive_p and/or
637 Fbytecode at the top. If this frame is for a built-in function
638 (such as load or eval-region) return nil. */
639 fun
= Findirect_function (*btp
->function
, Qnil
);
640 if (exclude_subrs_p
&& SUBRP (fun
))
643 /* btp points to the frame of a Lisp function that called interactive-p.
644 Return t if that function was called interactively. */
645 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
651 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
652 doc
: /* Define NAME as a function.
653 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
654 See also the function `interactive'.
655 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
659 register Lisp_Object fn_name
;
660 register Lisp_Object defn
;
662 fn_name
= Fcar (args
);
663 CHECK_SYMBOL (fn_name
);
664 defn
= Fcons (Qlambda
, Fcdr (args
));
665 if (!NILP (Vpurify_flag
))
666 defn
= Fpurecopy (defn
);
667 if (CONSP (XSYMBOL (fn_name
)->function
)
668 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
669 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
670 Ffset (fn_name
, defn
);
671 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
675 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
676 doc
: /* Define NAME as a macro.
677 The actual definition looks like
678 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
679 When the macro is called, as in (NAME ARGS...),
680 the function (lambda ARGLIST BODY...) is applied to
681 the list ARGS... as it appears in the expression,
682 and the result should be a form to be evaluated instead of the original.
684 DECL is a declaration, optional, which can specify how to indent
685 calls to this macro, how Edebug should handle it, and which argument
686 should be treated as documentation. It looks like this:
688 The elements can look like this:
690 Set NAME's `lisp-indent-function' property to INDENT.
693 Set NAME's `edebug-form-spec' property to DEBUG. (This is
694 equivalent to writing a `def-edebug-spec' for the macro.)
697 Set NAME's `doc-string-elt' property to ELT.
699 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
703 register Lisp_Object fn_name
;
704 register Lisp_Object defn
;
705 Lisp_Object lambda_list
, doc
, tail
;
707 fn_name
= Fcar (args
);
708 CHECK_SYMBOL (fn_name
);
709 lambda_list
= Fcar (Fcdr (args
));
710 tail
= Fcdr (Fcdr (args
));
713 if (STRINGP (Fcar (tail
)))
719 while (CONSP (Fcar (tail
))
720 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
722 if (!NILP (Vmacro_declaration_function
))
726 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
734 tail
= Fcons (lambda_list
, tail
);
736 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
737 defn
= Fcons (Qmacro
, Fcons (Qlambda
, tail
));
739 if (!NILP (Vpurify_flag
))
740 defn
= Fpurecopy (defn
);
741 if (CONSP (XSYMBOL (fn_name
)->function
)
742 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
743 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
744 Ffset (fn_name
, defn
);
745 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
750 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
751 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
752 Aliased variables always have the same value; setting one sets the other.
753 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
754 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
755 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
756 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
757 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
758 The return value is BASE-VARIABLE. */)
759 (new_alias
, base_variable
, docstring
)
760 Lisp_Object new_alias
, base_variable
, docstring
;
762 struct Lisp_Symbol
*sym
;
764 CHECK_SYMBOL (new_alias
);
765 CHECK_SYMBOL (base_variable
);
767 sym
= XSYMBOL (new_alias
);
770 /* Not sure why, but why not? */
771 error ("Cannot make a constant an alias");
773 switch (sym
->redirect
)
775 case SYMBOL_FORWARDED
:
776 error ("Cannot make an internal variable an alias");
777 case SYMBOL_LOCALIZED
:
778 error ("Don't know how to make a localized variable an alias");
781 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
782 If n_a is bound, but b_v is not, set the value of b_v to n_a,
783 so that old-code that affects n_a before the aliasing is setup
785 if (NILP (Fboundp (base_variable
)))
786 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
789 struct specbinding
*p
;
791 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
794 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
795 error ("Don't know how to make a let-bound variable an alias");
798 sym
->redirect
= SYMBOL_VARALIAS
;
799 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
800 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
801 LOADHIST_ATTACH (new_alias
);
802 /* Even if docstring is nil: remove old docstring. */
803 Fput (new_alias
, Qvariable_documentation
, docstring
);
805 return base_variable
;
809 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
810 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
811 You are not required to define a variable in order to use it,
812 but the definition can supply documentation and an initial value
813 in a way that tags can recognize.
815 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
816 If SYMBOL is buffer-local, its default value is what is set;
817 buffer-local values are not affected.
818 INITVALUE and DOCSTRING are optional.
819 If DOCSTRING starts with *, this variable is identified as a user option.
820 This means that M-x set-variable recognizes it.
821 See also `user-variable-p'.
822 If INITVALUE is missing, SYMBOL's value is not set.
824 If SYMBOL has a local binding, then this form affects the local
825 binding. This is usually not what you want. Thus, if you need to
826 load a file defining variables, with this form or with `defconst' or
827 `defcustom', you should always load that file _outside_ any bindings
828 for these variables. \(`defconst' and `defcustom' behave similarly in
830 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
834 register Lisp_Object sym
, tem
, tail
;
838 if (!NILP (Fcdr (Fcdr (tail
))))
839 error ("Too many arguments");
841 tem
= Fdefault_boundp (sym
);
844 if (SYMBOL_CONSTANT_P (sym
))
846 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
847 Lisp_Object tem
= Fcar (tail
);
849 && EQ (XCAR (tem
), Qquote
)
850 && CONSP (XCDR (tem
))
851 && EQ (XCAR (XCDR (tem
)), sym
)))
852 error ("Constant symbol `%s' specified in defvar",
853 SDATA (SYMBOL_NAME (sym
)));
857 Fset_default (sym
, Feval (Fcar (tail
)));
859 { /* Check if there is really a global binding rather than just a let
860 binding that shadows the global unboundness of the var. */
861 volatile struct specbinding
*pdl
= specpdl_ptr
;
862 while (--pdl
>= specpdl
)
864 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
865 && EQ (pdl
->old_value
, Qunbound
))
867 message_with_string ("Warning: defvar ignored because %s is let-bound",
868 SYMBOL_NAME (sym
), 1);
877 if (!NILP (Vpurify_flag
))
878 tem
= Fpurecopy (tem
);
879 Fput (sym
, Qvariable_documentation
, tem
);
881 LOADHIST_ATTACH (sym
);
884 /* Simple (defvar <var>) should not count as a definition at all.
885 It could get in the way of other definitions, and unloading this
886 package could try to make the variable unbound. */
892 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
893 doc
: /* Define SYMBOL as a constant variable.
894 The intent is that neither programs nor users should ever change this value.
895 Always sets the value of SYMBOL to the result of evalling INITVALUE.
896 If SYMBOL is buffer-local, its default value is what is set;
897 buffer-local values are not affected.
898 DOCSTRING is optional.
900 If SYMBOL has a local binding, then this form sets the local binding's
901 value. However, you should normally not make local bindings for
902 variables defined with this form.
903 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
907 register Lisp_Object sym
, tem
;
910 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
911 error ("Too many arguments");
913 tem
= Feval (Fcar (Fcdr (args
)));
914 if (!NILP (Vpurify_flag
))
915 tem
= Fpurecopy (tem
);
916 Fset_default (sym
, tem
);
917 tem
= Fcar (Fcdr (Fcdr (args
)));
920 if (!NILP (Vpurify_flag
))
921 tem
= Fpurecopy (tem
);
922 Fput (sym
, Qvariable_documentation
, tem
);
924 Fput (sym
, Qrisky_local_variable
, Qt
);
925 LOADHIST_ATTACH (sym
);
929 /* Error handler used in Fuser_variable_p. */
931 user_variable_p_eh (Lisp_Object ignore
)
937 lisp_indirect_variable (Lisp_Object sym
)
939 XSETSYMBOL (sym
, indirect_variable (XSYMBOL (sym
)));
943 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
944 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
945 \(The alternative is a variable used internally in a Lisp program.)
946 A variable is a user variable if
947 \(1) the first character of its documentation is `*', or
948 \(2) it is customizable (its property list contains a non-nil value
949 of `standard-value' or `custom-autoload'), or
950 \(3) it is an alias for another user variable.
951 Return nil if VARIABLE is an alias and there is a loop in the
952 chain of symbols. */)
954 Lisp_Object variable
;
956 Lisp_Object documentation
;
958 if (!SYMBOLP (variable
))
961 /* If indirect and there's an alias loop, don't check anything else. */
962 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
963 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
964 Qt
, user_variable_p_eh
)))
969 documentation
= Fget (variable
, Qvariable_documentation
);
970 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
972 if (STRINGP (documentation
)
973 && ((unsigned char) SREF (documentation
, 0) == '*'))
975 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
976 if (CONSP (documentation
)
977 && STRINGP (XCAR (documentation
))
978 && INTEGERP (XCDR (documentation
))
979 && XINT (XCDR (documentation
)) < 0)
981 /* Customizable? See `custom-variable-p'. */
982 if ((!NILP (Fget (variable
, intern ("standard-value"))))
983 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
986 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
989 /* An indirect variable? Let's follow the chain. */
990 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
994 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
995 doc
: /* Bind variables according to VARLIST then eval BODY.
996 The value of the last form in BODY is returned.
997 Each element of VARLIST is a symbol (which is bound to nil)
998 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
999 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
1000 usage: (let* VARLIST BODY...) */)
1004 Lisp_Object varlist
, val
, elt
;
1005 int count
= SPECPDL_INDEX ();
1006 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1008 GCPRO3 (args
, elt
, varlist
);
1010 varlist
= Fcar (args
);
1011 while (!NILP (varlist
))
1014 elt
= Fcar (varlist
);
1016 specbind (elt
, Qnil
);
1017 else if (! NILP (Fcdr (Fcdr (elt
))))
1018 signal_error ("`let' bindings can have only one value-form", elt
);
1021 val
= Feval (Fcar (Fcdr (elt
)));
1022 specbind (Fcar (elt
), val
);
1024 varlist
= Fcdr (varlist
);
1027 val
= Fprogn (Fcdr (args
));
1028 return unbind_to (count
, val
);
1031 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1032 doc
: /* Bind variables according to VARLIST then eval BODY.
1033 The value of the last form in BODY is returned.
1034 Each element of VARLIST is a symbol (which is bound to nil)
1035 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1036 All the VALUEFORMs are evalled before any symbols are bound.
1037 usage: (let VARLIST BODY...) */)
1041 Lisp_Object
*temps
, tem
;
1042 register Lisp_Object elt
, varlist
;
1043 int count
= SPECPDL_INDEX ();
1044 register int argnum
;
1045 struct gcpro gcpro1
, gcpro2
;
1047 varlist
= Fcar (args
);
1049 /* Make space to hold the values to give the bound variables */
1050 elt
= Flength (varlist
);
1051 temps
= (Lisp_Object
*) alloca (XFASTINT (elt
) * sizeof (Lisp_Object
));
1053 /* Compute the values and store them in `temps' */
1055 GCPRO2 (args
, *temps
);
1058 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1061 elt
= XCAR (varlist
);
1063 temps
[argnum
++] = Qnil
;
1064 else if (! NILP (Fcdr (Fcdr (elt
))))
1065 signal_error ("`let' bindings can have only one value-form", elt
);
1067 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
1068 gcpro2
.nvars
= argnum
;
1072 varlist
= Fcar (args
);
1073 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1075 elt
= XCAR (varlist
);
1076 tem
= temps
[argnum
++];
1078 specbind (elt
, tem
);
1080 specbind (Fcar (elt
), tem
);
1083 elt
= Fprogn (Fcdr (args
));
1084 return unbind_to (count
, elt
);
1087 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1088 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1089 The order of execution is thus TEST, BODY, TEST, BODY and so on
1090 until TEST returns nil.
1091 usage: (while TEST BODY...) */)
1095 Lisp_Object test
, body
;
1096 struct gcpro gcpro1
, gcpro2
;
1098 GCPRO2 (test
, body
);
1102 while (!NILP (Feval (test
)))
1112 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1113 doc
: /* Return result of expanding macros at top level of FORM.
1114 If FORM is not a macro call, it is returned unchanged.
1115 Otherwise, the macro is expanded and the expansion is considered
1116 in place of FORM. When a non-macro-call results, it is returned.
1118 The second optional arg ENVIRONMENT specifies an environment of macro
1119 definitions to shadow the loaded ones for use in file byte-compilation. */)
1122 Lisp_Object environment
;
1124 /* With cleanups from Hallvard Furuseth. */
1125 register Lisp_Object expander
, sym
, def
, tem
;
1129 /* Come back here each time we expand a macro call,
1130 in case it expands into another macro call. */
1133 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1134 def
= sym
= XCAR (form
);
1136 /* Trace symbols aliases to other symbols
1137 until we get a symbol that is not an alias. */
1138 while (SYMBOLP (def
))
1142 tem
= Fassq (sym
, environment
);
1145 def
= XSYMBOL (sym
)->function
;
1146 if (!EQ (def
, Qunbound
))
1151 /* Right now TEM is the result from SYM in ENVIRONMENT,
1152 and if TEM is nil then DEF is SYM's function definition. */
1155 /* SYM is not mentioned in ENVIRONMENT.
1156 Look at its function definition. */
1157 if (EQ (def
, Qunbound
) || !CONSP (def
))
1158 /* Not defined or definition not suitable */
1160 if (EQ (XCAR (def
), Qautoload
))
1162 /* Autoloading function: will it be a macro when loaded? */
1163 tem
= Fnth (make_number (4), def
);
1164 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1165 /* Yes, load it and try again. */
1167 struct gcpro gcpro1
;
1169 do_autoload (def
, sym
);
1176 else if (!EQ (XCAR (def
), Qmacro
))
1178 else expander
= XCDR (def
);
1182 expander
= XCDR (tem
);
1183 if (NILP (expander
))
1186 form
= apply1 (expander
, XCDR (form
));
1191 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1192 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1193 TAG is evalled to get the tag to use; it must not be nil.
1195 Then the BODY is executed.
1196 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1197 If no throw happens, `catch' returns the value of the last BODY form.
1198 If a throw happens, it specifies the value to return from `catch'.
1199 usage: (catch TAG BODY...) */)
1203 register Lisp_Object tag
;
1204 struct gcpro gcpro1
;
1207 tag
= Feval (Fcar (args
));
1209 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1212 /* Set up a catch, then call C function FUNC on argument ARG.
1213 FUNC should return a Lisp_Object.
1214 This is how catches are done from within C code. */
1217 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1219 /* This structure is made part of the chain `catchlist'. */
1222 /* Fill in the components of c, and put it on the list. */
1226 c
.backlist
= backtrace_list
;
1227 c
.handlerlist
= handlerlist
;
1228 c
.lisp_eval_depth
= lisp_eval_depth
;
1229 c
.pdlcount
= SPECPDL_INDEX ();
1230 c
.poll_suppress_count
= poll_suppress_count
;
1231 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1232 c
.gcpro
= gcprolist
;
1233 c
.byte_stack
= byte_stack_list
;
1237 if (! _setjmp (c
.jmp
))
1238 c
.val
= (*func
) (arg
);
1240 /* Throw works by a longjmp that comes right here. */
1245 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1246 jump to that CATCH, returning VALUE as the value of that catch.
1248 This is the guts Fthrow and Fsignal; they differ only in the way
1249 they choose the catch tag to throw to. A catch tag for a
1250 condition-case form has a TAG of Qnil.
1252 Before each catch is discarded, unbind all special bindings and
1253 execute all unwind-protect clauses made above that catch. Unwind
1254 the handler stack as we go, so that the proper handlers are in
1255 effect for each unwind-protect clause we run. At the end, restore
1256 some static info saved in CATCH, and longjmp to the location
1259 This is used for correct unwinding in Fthrow and Fsignal. */
1262 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1264 register int last_time
;
1266 /* Save the value in the tag. */
1269 /* Restore certain special C variables. */
1270 set_poll_suppress_count (catch->poll_suppress_count
);
1271 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1272 handling_signal
= 0;
1277 last_time
= catchlist
== catch;
1279 /* Unwind the specpdl stack, and then restore the proper set of
1281 unbind_to (catchlist
->pdlcount
, Qnil
);
1282 handlerlist
= catchlist
->handlerlist
;
1283 catchlist
= catchlist
->next
;
1285 while (! last_time
);
1288 /* If x_catch_errors was done, turn it off now.
1289 (First we give unbind_to a chance to do that.) */
1290 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1291 * The catch must remain in effect during that delicate
1292 * state. --lorentey */
1293 x_fully_uncatch_errors ();
1297 byte_stack_list
= catch->byte_stack
;
1298 gcprolist
= catch->gcpro
;
1301 gcpro_level
= gcprolist
->level
+ 1;
1305 backtrace_list
= catch->backlist
;
1306 lisp_eval_depth
= catch->lisp_eval_depth
;
1308 _longjmp (catch->jmp
, 1);
1311 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1312 doc
: /* Throw to the catch for TAG and return VALUE from it.
1313 Both TAG and VALUE are evalled. */)
1315 register Lisp_Object tag
, value
;
1317 register struct catchtag
*c
;
1320 for (c
= catchlist
; c
; c
= c
->next
)
1322 if (EQ (c
->tag
, tag
))
1323 unwind_to_catch (c
, value
);
1325 xsignal2 (Qno_catch
, tag
, value
);
1329 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1330 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1331 If BODYFORM completes normally, its value is returned
1332 after executing the UNWINDFORMS.
1333 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1334 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1339 int count
= SPECPDL_INDEX ();
1341 record_unwind_protect (Fprogn
, Fcdr (args
));
1342 val
= Feval (Fcar (args
));
1343 return unbind_to (count
, val
);
1346 /* Chain of condition handlers currently in effect.
1347 The elements of this chain are contained in the stack frames
1348 of Fcondition_case and internal_condition_case.
1349 When an error is signaled (by calling Fsignal, below),
1350 this chain is searched for an element that applies. */
1352 struct handler
*handlerlist
;
1354 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1355 doc
: /* Regain control when an error is signaled.
1356 Executes BODYFORM and returns its value if no error happens.
1357 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1358 where the BODY is made of Lisp expressions.
1360 A handler is applicable to an error
1361 if CONDITION-NAME is one of the error's condition names.
1362 If an error happens, the first applicable handler is run.
1364 The car of a handler may be a list of condition names
1365 instead of a single condition name. Then it handles all of them.
1367 When a handler handles an error, control returns to the `condition-case'
1368 and it executes the handler's BODY...
1369 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1370 (If VAR is nil, the handler can't access that information.)
1371 Then the value of the last BODY form is returned from the `condition-case'
1374 See also the function `signal' for more info.
1375 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1379 register Lisp_Object bodyform
, handlers
;
1380 volatile Lisp_Object var
;
1383 bodyform
= Fcar (Fcdr (args
));
1384 handlers
= Fcdr (Fcdr (args
));
1386 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1389 /* Like Fcondition_case, but the args are separate
1390 rather than passed in a list. Used by Fbyte_code. */
1393 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1394 Lisp_Object handlers
)
1402 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1408 && (SYMBOLP (XCAR (tem
))
1409 || CONSP (XCAR (tem
))))))
1410 error ("Invalid condition handler", tem
);
1415 c
.backlist
= backtrace_list
;
1416 c
.handlerlist
= handlerlist
;
1417 c
.lisp_eval_depth
= lisp_eval_depth
;
1418 c
.pdlcount
= SPECPDL_INDEX ();
1419 c
.poll_suppress_count
= poll_suppress_count
;
1420 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1421 c
.gcpro
= gcprolist
;
1422 c
.byte_stack
= byte_stack_list
;
1423 if (_setjmp (c
.jmp
))
1426 specbind (h
.var
, c
.val
);
1427 val
= Fprogn (Fcdr (h
.chosen_clause
));
1429 /* Note that this just undoes the binding of h.var; whoever
1430 longjumped to us unwound the stack to c.pdlcount before
1432 unbind_to (c
.pdlcount
, Qnil
);
1439 h
.handler
= handlers
;
1440 h
.next
= handlerlist
;
1444 val
= Feval (bodyform
);
1446 handlerlist
= h
.next
;
1450 /* Call the function BFUN with no arguments, catching errors within it
1451 according to HANDLERS. If there is an error, call HFUN with
1452 one argument which is the data that describes the error:
1455 HANDLERS can be a list of conditions to catch.
1456 If HANDLERS is Qt, catch all errors.
1457 If HANDLERS is Qerror, catch all errors
1458 but allow the debugger to run if that is enabled. */
1461 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1462 Lisp_Object (*hfun
) (Lisp_Object
))
1468 /* Since Fsignal will close off all calls to x_catch_errors,
1469 we will get the wrong results if some are not closed now. */
1471 if (x_catching_errors ())
1477 c
.backlist
= backtrace_list
;
1478 c
.handlerlist
= handlerlist
;
1479 c
.lisp_eval_depth
= lisp_eval_depth
;
1480 c
.pdlcount
= SPECPDL_INDEX ();
1481 c
.poll_suppress_count
= poll_suppress_count
;
1482 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1483 c
.gcpro
= gcprolist
;
1484 c
.byte_stack
= byte_stack_list
;
1485 if (_setjmp (c
.jmp
))
1487 return (*hfun
) (c
.val
);
1491 h
.handler
= handlers
;
1493 h
.next
= handlerlist
;
1499 handlerlist
= h
.next
;
1503 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1506 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1507 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1513 /* Since Fsignal will close off all calls to x_catch_errors,
1514 we will get the wrong results if some are not closed now. */
1516 if (x_catching_errors ())
1522 c
.backlist
= backtrace_list
;
1523 c
.handlerlist
= handlerlist
;
1524 c
.lisp_eval_depth
= lisp_eval_depth
;
1525 c
.pdlcount
= SPECPDL_INDEX ();
1526 c
.poll_suppress_count
= poll_suppress_count
;
1527 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1528 c
.gcpro
= gcprolist
;
1529 c
.byte_stack
= byte_stack_list
;
1530 if (_setjmp (c
.jmp
))
1532 return (*hfun
) (c
.val
);
1536 h
.handler
= handlers
;
1538 h
.next
= handlerlist
;
1542 val
= (*bfun
) (arg
);
1544 handlerlist
= h
.next
;
1548 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1552 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1555 Lisp_Object handlers
,
1556 Lisp_Object (*hfun
) (Lisp_Object
))
1562 /* Since Fsignal will close off all calls to x_catch_errors,
1563 we will get the wrong results if some are not closed now. */
1565 if (x_catching_errors ())
1571 c
.backlist
= backtrace_list
;
1572 c
.handlerlist
= handlerlist
;
1573 c
.lisp_eval_depth
= lisp_eval_depth
;
1574 c
.pdlcount
= SPECPDL_INDEX ();
1575 c
.poll_suppress_count
= poll_suppress_count
;
1576 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1577 c
.gcpro
= gcprolist
;
1578 c
.byte_stack
= byte_stack_list
;
1579 if (_setjmp (c
.jmp
))
1581 return (*hfun
) (c
.val
);
1585 h
.handler
= handlers
;
1587 h
.next
= handlerlist
;
1591 val
= (*bfun
) (arg1
, arg2
);
1593 handlerlist
= h
.next
;
1597 /* Like internal_condition_case but call BFUN with NARGS as first,
1598 and ARGS as second argument. */
1601 internal_condition_case_n (Lisp_Object (*bfun
) (int, Lisp_Object
*),
1604 Lisp_Object handlers
,
1605 Lisp_Object (*hfun
) (Lisp_Object
))
1611 /* Since Fsignal will close off all calls to x_catch_errors,
1612 we will get the wrong results if some are not closed now. */
1614 if (x_catching_errors ())
1620 c
.backlist
= backtrace_list
;
1621 c
.handlerlist
= handlerlist
;
1622 c
.lisp_eval_depth
= lisp_eval_depth
;
1623 c
.pdlcount
= SPECPDL_INDEX ();
1624 c
.poll_suppress_count
= poll_suppress_count
;
1625 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1626 c
.gcpro
= gcprolist
;
1627 c
.byte_stack
= byte_stack_list
;
1628 if (_setjmp (c
.jmp
))
1630 return (*hfun
) (c
.val
);
1634 h
.handler
= handlers
;
1636 h
.next
= handlerlist
;
1640 val
= (*bfun
) (nargs
, args
);
1642 handlerlist
= h
.next
;
1647 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1648 Lisp_Object
, Lisp_Object
);
1650 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1651 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1652 This function does not return.
1654 An error symbol is a symbol with an `error-conditions' property
1655 that is a list of condition names.
1656 A handler for any of those names will get to handle this signal.
1657 The symbol `error' should normally be one of them.
1659 DATA should be a list. Its elements are printed as part of the error message.
1660 See Info anchor `(elisp)Definition of signal' for some details on how this
1661 error message is constructed.
1662 If the signal is handled, DATA is made available to the handler.
1663 See also the function `condition-case'. */)
1664 (error_symbol
, data
)
1665 Lisp_Object error_symbol
, data
;
1667 /* When memory is full, ERROR-SYMBOL is nil,
1668 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1669 That is a special case--don't do this in other situations. */
1670 register struct handler
*allhandlers
= handlerlist
;
1671 Lisp_Object conditions
;
1672 extern int gc_in_progress
;
1673 extern int waiting_for_input
;
1675 Lisp_Object real_error_symbol
;
1676 struct backtrace
*bp
;
1678 immediate_quit
= handling_signal
= 0;
1680 if (gc_in_progress
|| waiting_for_input
)
1683 if (NILP (error_symbol
))
1684 real_error_symbol
= Fcar (data
);
1686 real_error_symbol
= error_symbol
;
1688 #if 0 /* rms: I don't know why this was here,
1689 but it is surely wrong for an error that is handled. */
1690 #ifdef HAVE_WINDOW_SYSTEM
1691 if (display_hourglass_p
)
1692 cancel_hourglass ();
1696 /* This hook is used by edebug. */
1697 if (! NILP (Vsignal_hook_function
)
1698 && ! NILP (error_symbol
))
1700 /* Edebug takes care of restoring these variables when it exits. */
1701 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1702 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1704 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1705 max_specpdl_size
= SPECPDL_INDEX () + 40;
1707 call2 (Vsignal_hook_function
, error_symbol
, data
);
1710 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1712 /* Remember from where signal was called. Skip over the frame for
1713 `signal' itself. If a frame for `error' follows, skip that,
1714 too. Don't do this when ERROR_SYMBOL is nil, because that
1715 is a memory-full error. */
1716 Vsignaling_function
= Qnil
;
1717 if (backtrace_list
&& !NILP (error_symbol
))
1719 bp
= backtrace_list
->next
;
1720 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1722 if (bp
&& bp
->function
)
1723 Vsignaling_function
= *bp
->function
;
1726 for (; handlerlist
; handlerlist
= handlerlist
->next
)
1728 register Lisp_Object clause
;
1730 clause
= find_handler_clause (handlerlist
->handler
, conditions
,
1731 error_symbol
, data
);
1733 if (EQ (clause
, Qlambda
))
1735 /* We can't return values to code which signaled an error, but we
1736 can continue code which has signaled a quit. */
1737 if (EQ (real_error_symbol
, Qquit
))
1740 error ("Cannot return from the debugger in an error");
1745 Lisp_Object unwind_data
;
1746 struct handler
*h
= handlerlist
;
1748 handlerlist
= allhandlers
;
1750 if (NILP (error_symbol
))
1753 unwind_data
= Fcons (error_symbol
, data
);
1754 h
->chosen_clause
= clause
;
1755 unwind_to_catch (h
->tag
, unwind_data
);
1759 handlerlist
= allhandlers
;
1760 /* If no handler is present now, try to run the debugger,
1761 and if that fails, throw to top level. */
1762 find_handler_clause (Qerror
, conditions
, error_symbol
, data
);
1764 Fthrow (Qtop_level
, Qt
);
1766 if (! NILP (error_symbol
))
1767 data
= Fcons (error_symbol
, data
);
1769 string
= Ferror_message_string (data
);
1770 fatal ("%s", SDATA (string
), 0);
1773 /* Internal version of Fsignal that never returns.
1774 Used for anything but Qquit (which can return from Fsignal). */
1777 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1779 Fsignal (error_symbol
, data
);
1783 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1786 xsignal0 (Lisp_Object error_symbol
)
1788 xsignal (error_symbol
, Qnil
);
1792 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1794 xsignal (error_symbol
, list1 (arg
));
1798 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1800 xsignal (error_symbol
, list2 (arg1
, arg2
));
1804 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1806 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1809 /* Signal `error' with message S, and additional arg ARG.
1810 If ARG is not a genuine list, make it a one-element list. */
1813 signal_error (char *s
, Lisp_Object arg
)
1815 Lisp_Object tortoise
, hare
;
1817 hare
= tortoise
= arg
;
1818 while (CONSP (hare
))
1825 tortoise
= XCDR (tortoise
);
1827 if (EQ (hare
, tortoise
))
1832 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1834 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1838 /* Return nonzero if LIST is a non-nil atom or
1839 a list containing one of CONDITIONS. */
1842 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1849 while (CONSP (conditions
))
1851 Lisp_Object
this, tail
;
1852 this = XCAR (conditions
);
1853 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1854 if (EQ (XCAR (tail
), this))
1856 conditions
= XCDR (conditions
);
1861 /* Return 1 if an error with condition-symbols CONDITIONS,
1862 and described by SIGNAL-DATA, should skip the debugger
1863 according to debugger-ignored-errors. */
1866 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1869 int first_string
= 1;
1870 Lisp_Object error_message
;
1872 error_message
= Qnil
;
1873 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1875 if (STRINGP (XCAR (tail
)))
1879 error_message
= Ferror_message_string (data
);
1883 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1888 Lisp_Object contail
;
1890 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1891 if (EQ (XCAR (tail
), XCAR (contail
)))
1899 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1900 SIG and DATA describe the signal, as in find_handler_clause. */
1903 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1905 Lisp_Object combined_data
;
1907 combined_data
= Fcons (sig
, data
);
1910 /* Don't try to run the debugger with interrupts blocked.
1911 The editing loop would return anyway. */
1913 /* Does user want to enter debugger for this kind of error? */
1916 : wants_debugger (Vdebug_on_error
, conditions
))
1917 && ! skip_debugger (conditions
, combined_data
)
1918 /* rms: what's this for? */
1919 && when_entered_debugger
< num_nonmacro_input_events
)
1921 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1928 /* Value of Qlambda means we have called debugger and user has continued.
1929 There are two ways to pass SIG and DATA:
1930 = SIG is the error symbol, and DATA is the rest of the data.
1931 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1932 This is for memory-full errors only.
1934 We need to increase max_specpdl_size temporarily around
1935 anything we do that can push on the specpdl, so as not to get
1936 a second error here in case we're handling specpdl overflow. */
1939 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1940 Lisp_Object sig
, Lisp_Object data
)
1942 register Lisp_Object h
;
1943 register Lisp_Object tem
;
1944 int debugger_called
= 0;
1945 int debugger_considered
= 0;
1947 /* t is used by handlers for all conditions, set up by C code. */
1948 if (EQ (handlers
, Qt
))
1951 /* Don't run the debugger for a memory-full error.
1952 (There is no room in memory to do that!) */
1954 debugger_considered
= 1;
1956 /* error is used similarly, but means print an error message
1957 and run the debugger if that is enabled. */
1958 if (EQ (handlers
, Qerror
)
1959 || !NILP (Vdebug_on_signal
)) /* This says call debugger even if
1960 there is a handler. */
1962 if (!NILP (sig
) && wants_debugger (Vstack_trace_on_error
, conditions
))
1964 max_lisp_eval_depth
+= 15;
1969 internal_with_output_to_temp_buffer
1971 (Lisp_Object (*) (Lisp_Object
)) Fbacktrace
,
1974 max_lisp_eval_depth
-= 15;
1977 if (!debugger_considered
)
1979 debugger_considered
= 1;
1980 debugger_called
= maybe_call_debugger (conditions
, sig
, data
);
1983 /* If there is no handler, return saying whether we ran the debugger. */
1984 if (EQ (handlers
, Qerror
))
1986 if (debugger_called
)
1992 for (h
= handlers
; CONSP (h
); h
= Fcdr (h
))
1994 Lisp_Object handler
, condit
;
1997 if (!CONSP (handler
))
1999 condit
= Fcar (handler
);
2000 /* Handle a single condition name in handler HANDLER. */
2001 if (SYMBOLP (condit
))
2003 tem
= Fmemq (Fcar (handler
), conditions
);
2007 /* Handle a list of condition names in handler HANDLER. */
2008 else if (CONSP (condit
))
2011 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
2013 tem
= Fmemq (Fcar (tail
), conditions
);
2016 /* This handler is going to apply.
2017 Does it allow the debugger to run first? */
2018 if (! debugger_considered
&& !NILP (Fmemq (Qdebug
, condit
)))
2019 maybe_call_debugger (conditions
, sig
, data
);
2029 /* dump an error message; called like printf */
2033 error (m
, a1
, a2
, a3
)
2053 int used
= doprnt (buffer
, size
, m
, m
+ mlen
, 3, args
);
2058 buffer
= (char *) xrealloc (buffer
, size
);
2061 buffer
= (char *) xmalloc (size
);
2066 string
= build_string (buffer
);
2070 xsignal1 (Qerror
, string
);
2073 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
2074 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
2075 This means it contains a description for how to read arguments to give it.
2076 The value is nil for an invalid function or a symbol with no function
2079 Interactively callable functions include strings and vectors (treated
2080 as keyboard macros), lambda-expressions that contain a top-level call
2081 to `interactive', autoload definitions made by `autoload' with non-nil
2082 fourth argument, and some of the built-in functions of Lisp.
2084 Also, a symbol satisfies `commandp' if its function definition does so.
2086 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2087 then strings and vectors are not accepted. */)
2088 (function
, for_call_interactively
)
2089 Lisp_Object function
, for_call_interactively
;
2091 register Lisp_Object fun
;
2092 register Lisp_Object funcar
;
2093 Lisp_Object if_prop
= Qnil
;
2097 fun
= indirect_function (fun
); /* Check cycles. */
2098 if (NILP (fun
) || EQ (fun
, Qunbound
))
2101 /* Check an `interactive-form' property if present, analogous to the
2102 function-documentation property. */
2104 while (SYMBOLP (fun
))
2106 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2109 fun
= Fsymbol_function (fun
);
2112 /* Emacs primitives are interactive if their DEFUN specifies an
2113 interactive spec. */
2115 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2117 /* Bytecode objects are interactive if they are long enough to
2118 have an element whose index is COMPILED_INTERACTIVE, which is
2119 where the interactive spec is stored. */
2120 else if (COMPILEDP (fun
))
2121 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2124 /* Strings and vectors are keyboard macros. */
2125 if (STRINGP (fun
) || VECTORP (fun
))
2126 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2128 /* Lists may represent commands. */
2131 funcar
= XCAR (fun
);
2132 if (EQ (funcar
, Qlambda
))
2133 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2134 if (EQ (funcar
, Qautoload
))
2135 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2140 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2141 doc
: /* Define FUNCTION to autoload from FILE.
2142 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2143 Third arg DOCSTRING is documentation for the function.
2144 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2145 Fifth arg TYPE indicates the type of the object:
2146 nil or omitted says FUNCTION is a function,
2147 `keymap' says FUNCTION is really a keymap, and
2148 `macro' or t says FUNCTION is really a macro.
2149 Third through fifth args give info about the real definition.
2150 They default to nil.
2151 If FUNCTION is already defined other than as an autoload,
2152 this does nothing and returns nil. */)
2153 (function
, file
, docstring
, interactive
, type
)
2154 Lisp_Object function
, file
, docstring
, interactive
, type
;
2156 CHECK_SYMBOL (function
);
2157 CHECK_STRING (file
);
2159 /* If function is defined and not as an autoload, don't override */
2160 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2161 && !(CONSP (XSYMBOL (function
)->function
)
2162 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2165 if (NILP (Vpurify_flag
))
2166 /* Only add entries after dumping, because the ones before are
2167 not useful and else we get loads of them from the loaddefs.el. */
2168 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2170 /* We don't want the docstring in purespace (instead,
2171 Snarf-documentation should (hopefully) overwrite it).
2172 We used to use 0 here, but that leads to accidental sharing in
2173 purecopy's hash-consing, so we use a (hopefully) unique integer
2175 docstring
= make_number (XHASH (function
));
2176 return Ffset (function
,
2177 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2178 interactive
, type
)));
2182 un_autoload (Lisp_Object oldqueue
)
2184 register Lisp_Object queue
, first
, second
;
2186 /* Queue to unwind is current value of Vautoload_queue.
2187 oldqueue is the shadowed value to leave in Vautoload_queue. */
2188 queue
= Vautoload_queue
;
2189 Vautoload_queue
= oldqueue
;
2190 while (CONSP (queue
))
2192 first
= XCAR (queue
);
2193 second
= Fcdr (first
);
2194 first
= Fcar (first
);
2195 if (EQ (first
, make_number (0)))
2198 Ffset (first
, second
);
2199 queue
= XCDR (queue
);
2204 /* Load an autoloaded function.
2205 FUNNAME is the symbol which is the function's name.
2206 FUNDEF is the autoload definition (a list). */
2209 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2211 int count
= SPECPDL_INDEX ();
2213 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2215 /* This is to make sure that loadup.el gives a clear picture
2216 of what files are preloaded and when. */
2217 if (! NILP (Vpurify_flag
))
2218 error ("Attempt to autoload %s while preparing to dump",
2219 SDATA (SYMBOL_NAME (funname
)));
2222 CHECK_SYMBOL (funname
);
2223 GCPRO3 (fun
, funname
, fundef
);
2225 /* Preserve the match data. */
2226 record_unwind_save_match_data ();
2228 /* If autoloading gets an error (which includes the error of failing
2229 to define the function being called), we use Vautoload_queue
2230 to undo function definitions and `provide' calls made by
2231 the function. We do this in the specific case of autoloading
2232 because autoloading is not an explicit request "load this file",
2233 but rather a request to "call this function".
2235 The value saved here is to be restored into Vautoload_queue. */
2236 record_unwind_protect (un_autoload
, Vautoload_queue
);
2237 Vautoload_queue
= Qt
;
2238 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2240 /* Once loading finishes, don't undo it. */
2241 Vautoload_queue
= Qt
;
2242 unbind_to (count
, Qnil
);
2244 fun
= Findirect_function (fun
, Qnil
);
2246 if (!NILP (Fequal (fun
, fundef
)))
2247 error ("Autoloading failed to define function %s",
2248 SDATA (SYMBOL_NAME (funname
)));
2253 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
2254 doc
: /* Evaluate FORM and return its value. */)
2258 Lisp_Object fun
, val
, original_fun
, original_args
;
2260 struct backtrace backtrace
;
2261 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2263 if (handling_signal
)
2267 return Fsymbol_value (form
);
2272 if ((consing_since_gc
> gc_cons_threshold
2273 && consing_since_gc
> gc_relative_threshold
)
2275 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2278 Fgarbage_collect ();
2282 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2284 if (max_lisp_eval_depth
< 100)
2285 max_lisp_eval_depth
= 100;
2286 if (lisp_eval_depth
> max_lisp_eval_depth
)
2287 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2290 original_fun
= Fcar (form
);
2291 original_args
= Fcdr (form
);
2293 backtrace
.next
= backtrace_list
;
2294 backtrace_list
= &backtrace
;
2295 backtrace
.function
= &original_fun
; /* This also protects them from gc */
2296 backtrace
.args
= &original_args
;
2297 backtrace
.nargs
= UNEVALLED
;
2298 backtrace
.evalargs
= 1;
2299 backtrace
.debug_on_exit
= 0;
2301 if (debug_on_next_call
)
2302 do_debug_on_call (Qt
);
2304 /* At this point, only original_fun and original_args
2305 have values that will be used below */
2308 /* Optimize for no indirection. */
2310 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2311 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2312 fun
= indirect_function (fun
);
2316 Lisp_Object numargs
;
2317 Lisp_Object argvals
[8];
2318 Lisp_Object args_left
;
2319 register int i
, maxargs
;
2321 args_left
= original_args
;
2322 numargs
= Flength (args_left
);
2326 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
2327 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2328 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2330 if (XSUBR (fun
)->max_args
== UNEVALLED
)
2332 backtrace
.evalargs
= 0;
2333 val
= (XSUBR (fun
)->function
.a1
) (args_left
);
2337 if (XSUBR (fun
)->max_args
== MANY
)
2339 /* Pass a vector of evaluated arguments */
2341 register int argnum
= 0;
2343 vals
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
2345 GCPRO3 (args_left
, fun
, fun
);
2349 while (!NILP (args_left
))
2351 vals
[argnum
++] = Feval (Fcar (args_left
));
2352 args_left
= Fcdr (args_left
);
2353 gcpro3
.nvars
= argnum
;
2356 backtrace
.args
= vals
;
2357 backtrace
.nargs
= XINT (numargs
);
2359 val
= (XSUBR (fun
)->function
.am
) (XINT (numargs
), vals
);
2364 GCPRO3 (args_left
, fun
, fun
);
2365 gcpro3
.var
= argvals
;
2368 maxargs
= XSUBR (fun
)->max_args
;
2369 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2371 argvals
[i
] = Feval (Fcar (args_left
));
2377 backtrace
.args
= argvals
;
2378 backtrace
.nargs
= XINT (numargs
);
2383 val
= (XSUBR (fun
)->function
.a0
) ();
2386 val
= (XSUBR (fun
)->function
.a1
) (argvals
[0]);
2389 val
= (XSUBR (fun
)->function
.a2
) (argvals
[0], argvals
[1]);
2392 val
= (XSUBR (fun
)->function
.a3
) (argvals
[0], argvals
[1],
2396 val
= (XSUBR (fun
)->function
.a4
) (argvals
[0], argvals
[1],
2397 argvals
[2], argvals
[3]);
2400 val
= (XSUBR (fun
)->function
.a5
) (argvals
[0], argvals
[1], argvals
[2],
2401 argvals
[3], argvals
[4]);
2404 val
= (XSUBR (fun
)->function
.a6
) (argvals
[0], argvals
[1], argvals
[2],
2405 argvals
[3], argvals
[4], argvals
[5]);
2408 val
= (XSUBR (fun
)->function
.a7
) (argvals
[0], argvals
[1], argvals
[2],
2409 argvals
[3], argvals
[4], argvals
[5],
2414 val
= (XSUBR (fun
)->function
.a8
) (argvals
[0], argvals
[1], argvals
[2],
2415 argvals
[3], argvals
[4], argvals
[5],
2416 argvals
[6], argvals
[7]);
2420 /* Someone has created a subr that takes more arguments than
2421 is supported by this code. We need to either rewrite the
2422 subr to use a different argument protocol, or add more
2423 cases to this switch. */
2427 if (COMPILEDP (fun
))
2428 val
= apply_lambda (fun
, original_args
, 1);
2431 if (EQ (fun
, Qunbound
))
2432 xsignal1 (Qvoid_function
, original_fun
);
2434 xsignal1 (Qinvalid_function
, original_fun
);
2435 funcar
= XCAR (fun
);
2436 if (!SYMBOLP (funcar
))
2437 xsignal1 (Qinvalid_function
, original_fun
);
2438 if (EQ (funcar
, Qautoload
))
2440 do_autoload (fun
, original_fun
);
2443 if (EQ (funcar
, Qmacro
))
2444 val
= Feval (apply1 (Fcdr (fun
), original_args
));
2445 else if (EQ (funcar
, Qlambda
))
2446 val
= apply_lambda (fun
, original_args
, 1);
2448 xsignal1 (Qinvalid_function
, original_fun
);
2454 if (backtrace
.debug_on_exit
)
2455 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2456 backtrace_list
= backtrace
.next
;
2461 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2462 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2463 Then return the value FUNCTION returns.
2464 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2465 usage: (apply FUNCTION &rest ARGUMENTS) */)
2470 register int i
, numargs
;
2471 register Lisp_Object spread_arg
;
2472 register Lisp_Object
*funcall_args
;
2474 struct gcpro gcpro1
;
2478 spread_arg
= args
[nargs
- 1];
2479 CHECK_LIST (spread_arg
);
2481 numargs
= XINT (Flength (spread_arg
));
2484 return Ffuncall (nargs
- 1, args
);
2485 else if (numargs
== 1)
2487 args
[nargs
- 1] = XCAR (spread_arg
);
2488 return Ffuncall (nargs
, args
);
2491 numargs
+= nargs
- 2;
2493 /* Optimize for no indirection. */
2494 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2495 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2496 fun
= indirect_function (fun
);
2497 if (EQ (fun
, Qunbound
))
2499 /* Let funcall get the error */
2506 if (numargs
< XSUBR (fun
)->min_args
2507 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2508 goto funcall
; /* Let funcall get the error */
2509 else if (XSUBR (fun
)->max_args
> numargs
)
2511 /* Avoid making funcall cons up a yet another new vector of arguments
2512 by explicitly supplying nil's for optional values */
2513 funcall_args
= (Lisp_Object
*) alloca ((1 + XSUBR (fun
)->max_args
)
2514 * sizeof (Lisp_Object
));
2515 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2516 funcall_args
[++i
] = Qnil
;
2517 GCPRO1 (*funcall_args
);
2518 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2522 /* We add 1 to numargs because funcall_args includes the
2523 function itself as well as its arguments. */
2526 funcall_args
= (Lisp_Object
*) alloca ((1 + numargs
)
2527 * sizeof (Lisp_Object
));
2528 GCPRO1 (*funcall_args
);
2529 gcpro1
.nvars
= 1 + numargs
;
2532 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2533 /* Spread the last arg we got. Its first element goes in
2534 the slot that it used to occupy, hence this value of I. */
2536 while (!NILP (spread_arg
))
2538 funcall_args
[i
++] = XCAR (spread_arg
);
2539 spread_arg
= XCDR (spread_arg
);
2542 /* By convention, the caller needs to gcpro Ffuncall's args. */
2543 RETURN_UNGCPRO (Ffuncall (gcpro1
.nvars
, funcall_args
));
2546 /* Run hook variables in various ways. */
2548 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
2549 static Lisp_Object
run_hook_with_args (int, Lisp_Object
*,
2550 enum run_hooks_condition
);
2552 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2553 doc
: /* Run each hook in HOOKS.
2554 Each argument should be a symbol, a hook variable.
2555 These symbols are processed in the order specified.
2556 If a hook symbol has a non-nil value, that value may be a function
2557 or a list of functions to be called to run the hook.
2558 If the value is a function, it is called with no arguments.
2559 If it is a list, the elements are called, in order, with no arguments.
2561 Major modes should not use this function directly to run their mode
2562 hook; they should use `run-mode-hooks' instead.
2564 Do not use `make-local-variable' to make a hook variable buffer-local.
2565 Instead, use `add-hook' and specify t for the LOCAL argument.
2566 usage: (run-hooks &rest HOOKS) */)
2571 Lisp_Object hook
[1];
2574 for (i
= 0; i
< nargs
; i
++)
2577 run_hook_with_args (1, hook
, to_completion
);
2583 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2584 Srun_hook_with_args
, 1, MANY
, 0,
2585 doc
: /* Run HOOK with the specified arguments ARGS.
2586 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2587 value, that value may be a function or a list of functions to be
2588 called to run the hook. If the value is a function, it is called with
2589 the given arguments and its return value is returned. If it is a list
2590 of functions, those functions are called, in order,
2591 with the given arguments ARGS.
2592 It is best not to depend on the value returned by `run-hook-with-args',
2595 Do not use `make-local-variable' to make a hook variable buffer-local.
2596 Instead, use `add-hook' and specify t for the LOCAL argument.
2597 usage: (run-hook-with-args HOOK &rest ARGS) */)
2602 return run_hook_with_args (nargs
, args
, to_completion
);
2605 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2606 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2607 doc
: /* Run HOOK with the specified arguments ARGS.
2608 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2609 value, that value may be a function or a list of functions to be
2610 called to run the hook. If the value is a function, it is called with
2611 the given arguments and its return value is returned.
2612 If it is a list of functions, those functions are called, in order,
2613 with the given arguments ARGS, until one of them
2614 returns a non-nil value. Then we return that value.
2615 However, if they all return nil, we return nil.
2617 Do not use `make-local-variable' to make a hook variable buffer-local.
2618 Instead, use `add-hook' and specify t for the LOCAL argument.
2619 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2624 return run_hook_with_args (nargs
, args
, until_success
);
2627 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2628 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2629 doc
: /* Run HOOK with the specified arguments ARGS.
2630 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2631 value, that value may be a function or a list of functions to be
2632 called to run the hook. If the value is a function, it is called with
2633 the given arguments and its return value is returned.
2634 If it is a list of functions, those functions are called, in order,
2635 with the given arguments ARGS, until one of them returns nil.
2636 Then we return nil. However, if they all return non-nil, we return non-nil.
2638 Do not use `make-local-variable' to make a hook variable buffer-local.
2639 Instead, use `add-hook' and specify t for the LOCAL argument.
2640 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2645 return run_hook_with_args (nargs
, args
, until_failure
);
2648 /* ARGS[0] should be a hook symbol.
2649 Call each of the functions in the hook value, passing each of them
2650 as arguments all the rest of ARGS (all NARGS - 1 elements).
2651 COND specifies a condition to test after each call
2652 to decide whether to stop.
2653 The caller (or its caller, etc) must gcpro all of ARGS,
2654 except that it isn't necessary to gcpro ARGS[0]. */
2657 run_hook_with_args (int nargs
, Lisp_Object
*args
, enum run_hooks_condition cond
)
2659 Lisp_Object sym
, val
, ret
;
2660 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2662 /* If we are dying or still initializing,
2663 don't do anything--it would probably crash if we tried. */
2664 if (NILP (Vrun_hooks
))
2668 val
= find_symbol_value (sym
);
2669 ret
= (cond
== until_failure
? Qt
: Qnil
);
2671 if (EQ (val
, Qunbound
) || NILP (val
))
2673 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2676 return Ffuncall (nargs
, args
);
2680 Lisp_Object globals
= Qnil
;
2681 GCPRO3 (sym
, val
, globals
);
2684 CONSP (val
) && ((cond
== to_completion
)
2685 || (cond
== until_success
? NILP (ret
)
2689 if (EQ (XCAR (val
), Qt
))
2691 /* t indicates this hook has a local binding;
2692 it means to run the global binding too. */
2693 globals
= Fdefault_value (sym
);
2694 if (NILP (globals
)) continue;
2696 if (!CONSP (globals
) || EQ (XCAR (globals
), Qlambda
))
2699 ret
= Ffuncall (nargs
, args
);
2704 CONSP (globals
) && ((cond
== to_completion
)
2705 || (cond
== until_success
? NILP (ret
)
2707 globals
= XCDR (globals
))
2709 args
[0] = XCAR (globals
);
2710 /* In a global value, t should not occur. If it does, we
2711 must ignore it to avoid an endless loop. */
2712 if (!EQ (args
[0], Qt
))
2713 ret
= Ffuncall (nargs
, args
);
2719 args
[0] = XCAR (val
);
2720 ret
= Ffuncall (nargs
, args
);
2729 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2730 present value of that symbol.
2731 Call each element of FUNLIST,
2732 passing each of them the rest of ARGS.
2733 The caller (or its caller, etc) must gcpro all of ARGS,
2734 except that it isn't necessary to gcpro ARGS[0]. */
2737 run_hook_list_with_args (Lisp_Object funlist
, int nargs
, Lisp_Object
*args
)
2741 Lisp_Object globals
;
2742 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2746 GCPRO3 (sym
, val
, globals
);
2748 for (val
= funlist
; CONSP (val
); val
= XCDR (val
))
2750 if (EQ (XCAR (val
), Qt
))
2752 /* t indicates this hook has a local binding;
2753 it means to run the global binding too. */
2755 for (globals
= Fdefault_value (sym
);
2757 globals
= XCDR (globals
))
2759 args
[0] = XCAR (globals
);
2760 /* In a global value, t should not occur. If it does, we
2761 must ignore it to avoid an endless loop. */
2762 if (!EQ (args
[0], Qt
))
2763 Ffuncall (nargs
, args
);
2768 args
[0] = XCAR (val
);
2769 Ffuncall (nargs
, args
);
2776 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2779 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2781 Lisp_Object temp
[3];
2786 Frun_hook_with_args (3, temp
);
2789 /* Apply fn to arg */
2791 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2793 struct gcpro gcpro1
;
2797 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2800 Lisp_Object args
[2];
2804 RETURN_UNGCPRO (Fapply (2, args
));
2808 /* Call function fn on no arguments */
2810 call0 (Lisp_Object fn
)
2812 struct gcpro gcpro1
;
2815 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2818 /* Call function fn with 1 argument arg1 */
2821 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2823 struct gcpro gcpro1
;
2824 Lisp_Object args
[2];
2830 RETURN_UNGCPRO (Ffuncall (2, args
));
2833 /* Call function fn with 2 arguments arg1, arg2 */
2836 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2838 struct gcpro gcpro1
;
2839 Lisp_Object args
[3];
2845 RETURN_UNGCPRO (Ffuncall (3, args
));
2848 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2851 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2853 struct gcpro gcpro1
;
2854 Lisp_Object args
[4];
2861 RETURN_UNGCPRO (Ffuncall (4, args
));
2864 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2867 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2870 struct gcpro gcpro1
;
2871 Lisp_Object args
[5];
2879 RETURN_UNGCPRO (Ffuncall (5, args
));
2882 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2885 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2886 Lisp_Object arg4
, Lisp_Object arg5
)
2888 struct gcpro gcpro1
;
2889 Lisp_Object args
[6];
2898 RETURN_UNGCPRO (Ffuncall (6, args
));
2901 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2904 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2905 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2907 struct gcpro gcpro1
;
2908 Lisp_Object args
[7];
2918 RETURN_UNGCPRO (Ffuncall (7, args
));
2921 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7 */
2924 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2925 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2927 struct gcpro gcpro1
;
2928 Lisp_Object args
[8];
2939 RETURN_UNGCPRO (Ffuncall (8, args
));
2942 /* The caller should GCPRO all the elements of ARGS. */
2944 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2945 doc
: /* Call first argument as a function, passing remaining arguments to it.
2946 Return the value that function returns.
2947 Thus, (funcall 'cons 'x 'y) returns (x . y).
2948 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2953 Lisp_Object fun
, original_fun
;
2955 int numargs
= nargs
- 1;
2956 Lisp_Object lisp_numargs
;
2958 struct backtrace backtrace
;
2959 register Lisp_Object
*internal_args
;
2963 if ((consing_since_gc
> gc_cons_threshold
2964 && consing_since_gc
> gc_relative_threshold
)
2966 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2967 Fgarbage_collect ();
2969 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2971 if (max_lisp_eval_depth
< 100)
2972 max_lisp_eval_depth
= 100;
2973 if (lisp_eval_depth
> max_lisp_eval_depth
)
2974 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2977 backtrace
.next
= backtrace_list
;
2978 backtrace_list
= &backtrace
;
2979 backtrace
.function
= &args
[0];
2980 backtrace
.args
= &args
[1];
2981 backtrace
.nargs
= nargs
- 1;
2982 backtrace
.evalargs
= 0;
2983 backtrace
.debug_on_exit
= 0;
2985 if (debug_on_next_call
)
2986 do_debug_on_call (Qlambda
);
2990 original_fun
= args
[0];
2994 /* Optimize for no indirection. */
2996 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2997 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2998 fun
= indirect_function (fun
);
3002 if (numargs
< XSUBR (fun
)->min_args
3003 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
3005 XSETFASTINT (lisp_numargs
, numargs
);
3006 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
3009 if (XSUBR (fun
)->max_args
== UNEVALLED
)
3010 xsignal1 (Qinvalid_function
, original_fun
);
3012 if (XSUBR (fun
)->max_args
== MANY
)
3014 val
= (XSUBR (fun
)->function
.am
) (numargs
, args
+ 1);
3018 if (XSUBR (fun
)->max_args
> numargs
)
3020 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
3021 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
3022 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
3023 internal_args
[i
] = Qnil
;
3026 internal_args
= args
+ 1;
3027 switch (XSUBR (fun
)->max_args
)
3030 val
= (XSUBR (fun
)->function
.a0
) ();
3033 val
= (XSUBR (fun
)->function
.a1
) (internal_args
[0]);
3036 val
= (XSUBR (fun
)->function
.a2
) (internal_args
[0], internal_args
[1]);
3039 val
= (XSUBR (fun
)->function
.a3
) (internal_args
[0], internal_args
[1],
3043 val
= (XSUBR (fun
)->function
.a4
) (internal_args
[0], internal_args
[1],
3044 internal_args
[2], internal_args
[3]);
3047 val
= (XSUBR (fun
)->function
.a5
) (internal_args
[0], internal_args
[1],
3048 internal_args
[2], internal_args
[3],
3052 val
= (XSUBR (fun
)->function
.a6
) (internal_args
[0], internal_args
[1],
3053 internal_args
[2], internal_args
[3],
3054 internal_args
[4], internal_args
[5]);
3057 val
= (XSUBR (fun
)->function
.a7
) (internal_args
[0], internal_args
[1],
3058 internal_args
[2], internal_args
[3],
3059 internal_args
[4], internal_args
[5],
3064 val
= (XSUBR (fun
)->function
.a8
) (internal_args
[0], internal_args
[1],
3065 internal_args
[2], internal_args
[3],
3066 internal_args
[4], internal_args
[5],
3067 internal_args
[6], internal_args
[7]);
3072 /* If a subr takes more than 8 arguments without using MANY
3073 or UNEVALLED, we need to extend this function to support it.
3074 Until this is done, there is no way to call the function. */
3078 if (COMPILEDP (fun
))
3079 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3082 if (EQ (fun
, Qunbound
))
3083 xsignal1 (Qvoid_function
, original_fun
);
3085 xsignal1 (Qinvalid_function
, original_fun
);
3086 funcar
= XCAR (fun
);
3087 if (!SYMBOLP (funcar
))
3088 xsignal1 (Qinvalid_function
, original_fun
);
3089 if (EQ (funcar
, Qlambda
))
3090 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3091 else if (EQ (funcar
, Qautoload
))
3093 do_autoload (fun
, original_fun
);
3098 xsignal1 (Qinvalid_function
, original_fun
);
3103 if (backtrace
.debug_on_exit
)
3104 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3105 backtrace_list
= backtrace
.next
;
3110 apply_lambda (Lisp_Object fun
, Lisp_Object args
, int eval_flag
)
3112 Lisp_Object args_left
;
3113 Lisp_Object numargs
;
3114 register Lisp_Object
*arg_vector
;
3115 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3117 register Lisp_Object tem
;
3119 numargs
= Flength (args
);
3120 arg_vector
= (Lisp_Object
*) alloca (XINT (numargs
) * sizeof (Lisp_Object
));
3123 GCPRO3 (*arg_vector
, args_left
, fun
);
3126 for (i
= 0; i
< XINT (numargs
);)
3128 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3129 if (eval_flag
) tem
= Feval (tem
);
3130 arg_vector
[i
++] = tem
;
3138 backtrace_list
->args
= arg_vector
;
3139 backtrace_list
->nargs
= i
;
3141 backtrace_list
->evalargs
= 0;
3142 tem
= funcall_lambda (fun
, XINT (numargs
), arg_vector
);
3144 /* Do the debug-on-exit now, while arg_vector still exists. */
3145 if (backtrace_list
->debug_on_exit
)
3146 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3147 /* Don't do it again when we return to eval. */
3148 backtrace_list
->debug_on_exit
= 0;
3152 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3153 and return the result of evaluation.
3154 FUN must be either a lambda-expression or a compiled-code object. */
3157 funcall_lambda (Lisp_Object fun
, int nargs
, register Lisp_Object
*arg_vector
)
3159 Lisp_Object val
, syms_left
, next
;
3160 int count
= SPECPDL_INDEX ();
3161 int i
, optional
, rest
;
3165 syms_left
= XCDR (fun
);
3166 if (CONSP (syms_left
))
3167 syms_left
= XCAR (syms_left
);
3169 xsignal1 (Qinvalid_function
, fun
);
3171 else if (COMPILEDP (fun
))
3172 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3176 i
= optional
= rest
= 0;
3177 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3181 next
= XCAR (syms_left
);
3182 if (!SYMBOLP (next
))
3183 xsignal1 (Qinvalid_function
, fun
);
3185 if (EQ (next
, Qand_rest
))
3187 else if (EQ (next
, Qand_optional
))
3191 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
3195 specbind (next
, arg_vector
[i
++]);
3197 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3199 specbind (next
, Qnil
);
3202 if (!NILP (syms_left
))
3203 xsignal1 (Qinvalid_function
, fun
);
3205 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3208 val
= Fprogn (XCDR (XCDR (fun
)));
3211 /* If we have not actually read the bytecode string
3212 and constants vector yet, fetch them from the file. */
3213 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3214 Ffetch_bytecode (fun
);
3215 val
= Fbyte_code (AREF (fun
, COMPILED_BYTECODE
),
3216 AREF (fun
, COMPILED_CONSTANTS
),
3217 AREF (fun
, COMPILED_STACK_DEPTH
));
3220 return unbind_to (count
, val
);
3223 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3225 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3231 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3233 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3236 tem
= AREF (object
, COMPILED_BYTECODE
);
3237 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3238 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3240 error ("Invalid byte code");
3242 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3243 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3251 register int count
= SPECPDL_INDEX ();
3252 if (specpdl_size
>= max_specpdl_size
)
3254 if (max_specpdl_size
< 400)
3255 max_specpdl_size
= 400;
3256 if (specpdl_size
>= max_specpdl_size
)
3257 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3260 if (specpdl_size
> max_specpdl_size
)
3261 specpdl_size
= max_specpdl_size
;
3262 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3263 specpdl_ptr
= specpdl
+ count
;
3266 /* specpdl_ptr->symbol is a field which describes which variable is
3267 let-bound, so it can be properly undone when we unbind_to.
3268 It can have the following two shapes:
3269 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3270 a symbol that is not buffer-local (at least at the time
3271 the let binding started). Note also that it should not be
3272 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3274 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3275 variable SYMBOL which can be buffer-local. WHERE tells us
3276 which buffer is affected (or nil if the let-binding affects the
3277 global value of the variable) and BUFFER tells us which buffer was
3278 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3279 BUFFER did not yet have a buffer-local value). */
3282 specbind (Lisp_Object symbol
, Lisp_Object value
)
3284 struct Lisp_Symbol
*sym
;
3286 eassert (!handling_signal
);
3288 CHECK_SYMBOL (symbol
);
3289 sym
= XSYMBOL (symbol
);
3290 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3294 switch (sym
->redirect
)
3296 case SYMBOL_VARALIAS
:
3297 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3298 case SYMBOL_PLAINVAL
:
3299 { /* The most common case is that of a non-constant symbol with a
3300 trivial value. Make that as fast as we can. */
3301 specpdl_ptr
->symbol
= symbol
;
3302 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3303 specpdl_ptr
->func
= NULL
;
3306 SET_SYMBOL_VAL (sym
, value
);
3308 set_internal (symbol
, value
, Qnil
, 1);
3311 case SYMBOL_LOCALIZED
:
3312 if (SYMBOL_BLV (sym
)->frame_local
)
3313 error ("Frame-local vars cannot be let-bound");
3314 case SYMBOL_FORWARDED
:
3316 Lisp_Object ovalue
= find_symbol_value (symbol
);
3317 specpdl_ptr
->func
= 0;
3318 specpdl_ptr
->old_value
= ovalue
;
3320 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3321 || (EQ (SYMBOL_BLV (sym
)->where
,
3322 SYMBOL_BLV (sym
)->frame_local
?
3323 Fselected_frame () : Fcurrent_buffer ())));
3325 if (sym
->redirect
== SYMBOL_LOCALIZED
3326 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3328 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3330 /* For a local variable, record both the symbol and which
3331 buffer's or frame's value we are saving. */
3332 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3334 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3335 || (BLV_FOUND (SYMBOL_BLV (sym
))
3336 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3339 else if (sym
->redirect
== SYMBOL_LOCALIZED
3340 && BLV_FOUND (SYMBOL_BLV (sym
)))
3341 where
= SYMBOL_BLV (sym
)->where
;
3345 /* We're not using the `unused' slot in the specbinding
3346 structure because this would mean we have to do more
3347 work for simple variables. */
3348 /* FIXME: The third value `current_buffer' is only used in
3349 let_shadows_buffer_binding_p which is itself only used
3350 in set_internal for local_if_set. */
3351 eassert (NILP (where
) || EQ (where
, cur_buf
));
3352 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3354 /* If SYMBOL is a per-buffer variable which doesn't have a
3355 buffer-local value here, make the `let' change the global
3356 value by changing the value of SYMBOL in all buffers not
3357 having their own value. This is consistent with what
3358 happens with other buffer-local variables. */
3360 && sym
->redirect
== SYMBOL_FORWARDED
)
3362 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3364 Fset_default (symbol
, value
);
3369 specpdl_ptr
->symbol
= symbol
;
3372 set_internal (symbol
, value
, Qnil
, 1);
3380 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3382 eassert (!handling_signal
);
3384 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3386 specpdl_ptr
->func
= function
;
3387 specpdl_ptr
->symbol
= Qnil
;
3388 specpdl_ptr
->old_value
= arg
;
3393 unbind_to (int count
, Lisp_Object value
)
3395 Lisp_Object quitf
= Vquit_flag
;
3396 struct gcpro gcpro1
, gcpro2
;
3398 GCPRO2 (value
, quitf
);
3401 while (specpdl_ptr
!= specpdl
+ count
)
3403 /* Copy the binding, and decrement specpdl_ptr, before we do
3404 the work to unbind it. We decrement first
3405 so that an error in unbinding won't try to unbind
3406 the same entry again, and we copy the binding first
3407 in case more bindings are made during some of the code we run. */
3409 struct specbinding this_binding
;
3410 this_binding
= *--specpdl_ptr
;
3412 if (this_binding
.func
!= 0)
3413 (*this_binding
.func
) (this_binding
.old_value
);
3414 /* If the symbol is a list, it is really (SYMBOL WHERE
3415 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3416 frame. If WHERE is a buffer or frame, this indicates we
3417 bound a variable that had a buffer-local or frame-local
3418 binding. WHERE nil means that the variable had the default
3419 value when it was bound. CURRENT-BUFFER is the buffer that
3420 was current when the variable was bound. */
3421 else if (CONSP (this_binding
.symbol
))
3423 Lisp_Object symbol
, where
;
3425 symbol
= XCAR (this_binding
.symbol
);
3426 where
= XCAR (XCDR (this_binding
.symbol
));
3429 Fset_default (symbol
, this_binding
.old_value
);
3430 /* If `where' is non-nil, reset the value in the appropriate
3431 local binding, but only if that binding still exists. */
3432 else if (BUFFERP (where
)
3433 ? !NILP (Flocal_variable_p (symbol
, where
))
3434 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3435 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3437 /* If variable has a trivial value (no forwarding), we can
3438 just set it. No need to check for constant symbols here,
3439 since that was already done by specbind. */
3440 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3441 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3442 this_binding
.old_value
);
3444 /* NOTE: we only ever come here if make_local_foo was used for
3445 the first time on this var within this let. */
3446 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3449 if (NILP (Vquit_flag
) && !NILP (quitf
))
3456 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3457 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3458 The debugger is entered when that frame exits, if the flag is non-nil. */)
3460 Lisp_Object level
, flag
;
3462 register struct backtrace
*backlist
= backtrace_list
;
3465 CHECK_NUMBER (level
);
3467 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3469 backlist
= backlist
->next
;
3473 backlist
->debug_on_exit
= !NILP (flag
);
3478 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3479 doc
: /* Print a trace of Lisp function calls currently active.
3480 Output stream used is value of `standard-output'. */)
3483 register struct backtrace
*backlist
= backtrace_list
;
3487 extern Lisp_Object Vprint_level
;
3488 struct gcpro gcpro1
;
3490 XSETFASTINT (Vprint_level
, 3);
3497 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3498 if (backlist
->nargs
== UNEVALLED
)
3500 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3501 write_string ("\n", -1);
3505 tem
= *backlist
->function
;
3506 Fprin1 (tem
, Qnil
); /* This can QUIT */
3507 write_string ("(", -1);
3508 if (backlist
->nargs
== MANY
)
3510 for (tail
= *backlist
->args
, i
= 0;
3512 tail
= Fcdr (tail
), i
++)
3514 if (i
) write_string (" ", -1);
3515 Fprin1 (Fcar (tail
), Qnil
);
3520 for (i
= 0; i
< backlist
->nargs
; i
++)
3522 if (i
) write_string (" ", -1);
3523 Fprin1 (backlist
->args
[i
], Qnil
);
3526 write_string (")\n", -1);
3528 backlist
= backlist
->next
;
3531 Vprint_level
= Qnil
;
3536 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3537 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3538 If that frame has not evaluated the arguments yet (or is a special form),
3539 the value is (nil FUNCTION ARG-FORMS...).
3540 If that frame has evaluated its arguments and called its function already,
3541 the value is (t FUNCTION ARG-VALUES...).
3542 A &rest arg is represented as the tail of the list ARG-VALUES.
3543 FUNCTION is whatever was supplied as car of evaluated list,
3544 or a lambda expression for macro calls.
3545 If NFRAMES is more than the number of frames, the value is nil. */)
3547 Lisp_Object nframes
;
3549 register struct backtrace
*backlist
= backtrace_list
;
3553 CHECK_NATNUM (nframes
);
3555 /* Find the frame requested. */
3556 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3557 backlist
= backlist
->next
;
3561 if (backlist
->nargs
== UNEVALLED
)
3562 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3565 if (backlist
->nargs
== MANY
)
3566 tem
= *backlist
->args
;
3568 tem
= Flist (backlist
->nargs
, backlist
->args
);
3570 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3576 mark_backtrace (void)
3578 register struct backtrace
*backlist
;
3581 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3583 mark_object (*backlist
->function
);
3585 if (backlist
->nargs
== UNEVALLED
|| backlist
->nargs
== MANY
)
3588 i
= backlist
->nargs
- 1;
3590 mark_object (backlist
->args
[i
]);
3597 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size
,
3598 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3599 If Lisp code tries to increase the total number past this amount,
3600 an error is signaled.
3601 You can safely use a value considerably larger than the default value,
3602 if that proves inconveniently small. However, if you increase it too far,
3603 Emacs could run out of memory trying to make the stack bigger. */);
3605 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth
,
3606 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3608 This limit serves to catch infinite recursions for you before they cause
3609 actual stack overflow in C, which would be fatal for Emacs.
3610 You can safely make it considerably larger than its default value,
3611 if that proves inconveniently small. However, if you increase it too far,
3612 Emacs could overflow the real C stack, and crash. */);
3614 DEFVAR_LISP ("quit-flag", &Vquit_flag
,
3615 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3616 If the value is t, that means do an ordinary quit.
3617 If the value equals `throw-on-input', that means quit by throwing
3618 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3619 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3620 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3623 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit
,
3624 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3625 Note that `quit-flag' will still be set by typing C-g,
3626 so a quit will be signaled as soon as `inhibit-quit' is nil.
3627 To prevent this happening, set `quit-flag' to nil
3628 before making `inhibit-quit' nil. */);
3629 Vinhibit_quit
= Qnil
;
3631 Qinhibit_quit
= intern_c_string ("inhibit-quit");
3632 staticpro (&Qinhibit_quit
);
3634 Qautoload
= intern_c_string ("autoload");
3635 staticpro (&Qautoload
);
3637 Qdebug_on_error
= intern_c_string ("debug-on-error");
3638 staticpro (&Qdebug_on_error
);
3640 Qmacro
= intern_c_string ("macro");
3641 staticpro (&Qmacro
);
3643 Qdeclare
= intern_c_string ("declare");
3644 staticpro (&Qdeclare
);
3646 /* Note that the process handling also uses Qexit, but we don't want
3647 to staticpro it twice, so we just do it here. */
3648 Qexit
= intern_c_string ("exit");
3651 Qinteractive
= intern_c_string ("interactive");
3652 staticpro (&Qinteractive
);
3654 Qcommandp
= intern_c_string ("commandp");
3655 staticpro (&Qcommandp
);
3657 Qdefun
= intern_c_string ("defun");
3658 staticpro (&Qdefun
);
3660 Qand_rest
= intern_c_string ("&rest");
3661 staticpro (&Qand_rest
);
3663 Qand_optional
= intern_c_string ("&optional");
3664 staticpro (&Qand_optional
);
3666 Qdebug
= intern_c_string ("debug");
3667 staticpro (&Qdebug
);
3669 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error
,
3670 doc
: /* *Non-nil means errors display a backtrace buffer.
3671 More precisely, this happens for any error that is handled
3672 by the editor command loop.
3673 If the value is a list, an error only means to display a backtrace
3674 if one of its condition symbols appears in the list. */);
3675 Vstack_trace_on_error
= Qnil
;
3677 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error
,
3678 doc
: /* *Non-nil means enter debugger if an error is signaled.
3679 Does not apply to errors handled by `condition-case' or those
3680 matched by `debug-ignored-errors'.
3681 If the value is a list, an error only means to enter the debugger
3682 if one of its condition symbols appears in the list.
3683 When you evaluate an expression interactively, this variable
3684 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3685 The command `toggle-debug-on-error' toggles this.
3686 See also the variable `debug-on-quit'. */);
3687 Vdebug_on_error
= Qnil
;
3689 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors
,
3690 doc
: /* *List of errors for which the debugger should not be called.
3691 Each element may be a condition-name or a regexp that matches error messages.
3692 If any element applies to a given error, that error skips the debugger
3693 and just returns to top level.
3694 This overrides the variable `debug-on-error'.
3695 It does not apply to errors handled by `condition-case'. */);
3696 Vdebug_ignored_errors
= Qnil
;
3698 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit
,
3699 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3700 Does not apply if quit is handled by a `condition-case'. */);
3703 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call
,
3704 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3706 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue
,
3707 doc
: /* Non-nil means debugger may continue execution.
3708 This is nil when the debugger is called under circumstances where it
3709 might not be safe to continue. */);
3710 debugger_may_continue
= 1;
3712 DEFVAR_LISP ("debugger", &Vdebugger
,
3713 doc
: /* Function to call to invoke debugger.
3714 If due to frame exit, args are `exit' and the value being returned;
3715 this function's value will be returned instead of that.
3716 If due to error, args are `error' and a list of the args to `signal'.
3717 If due to `apply' or `funcall' entry, one arg, `lambda'.
3718 If due to `eval' entry, one arg, t. */);
3721 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function
,
3722 doc
: /* If non-nil, this is a function for `signal' to call.
3723 It receives the same arguments that `signal' was given.
3724 The Edebug package uses this to regain control. */);
3725 Vsignal_hook_function
= Qnil
;
3727 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal
,
3728 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3729 Note that `debug-on-error', `debug-on-quit' and friends
3730 still determine whether to handle the particular condition. */);
3731 Vdebug_on_signal
= Qnil
;
3733 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function
,
3734 doc
: /* Function to process declarations in a macro definition.
3735 The function will be called with two args MACRO and DECL.
3736 MACRO is the name of the macro being defined.
3737 DECL is a list `(declare ...)' containing the declarations.
3738 The value the function returns is not used. */);
3739 Vmacro_declaration_function
= Qnil
;
3741 Vrun_hooks
= intern_c_string ("run-hooks");
3742 staticpro (&Vrun_hooks
);
3744 staticpro (&Vautoload_queue
);
3745 Vautoload_queue
= Qnil
;
3746 staticpro (&Vsignaling_function
);
3747 Vsignaling_function
= Qnil
;
3758 defsubr (&Sfunction
);
3760 defsubr (&Sdefmacro
);
3762 defsubr (&Sdefvaralias
);
3763 defsubr (&Sdefconst
);
3764 defsubr (&Suser_variable_p
);
3768 defsubr (&Smacroexpand
);
3771 defsubr (&Sunwind_protect
);
3772 defsubr (&Scondition_case
);
3774 defsubr (&Sinteractive_p
);
3775 defsubr (&Scalled_interactively_p
);
3776 defsubr (&Scommandp
);
3777 defsubr (&Sautoload
);
3780 defsubr (&Sfuncall
);
3781 defsubr (&Srun_hooks
);
3782 defsubr (&Srun_hook_with_args
);
3783 defsubr (&Srun_hook_with_args_until_success
);
3784 defsubr (&Srun_hook_with_args_until_failure
);
3785 defsubr (&Sfetch_bytecode
);
3786 defsubr (&Sbacktrace_debug
);
3787 defsubr (&Sbacktrace
);
3788 defsubr (&Sbacktrace_frame
);
3791 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
3792 (do not change this comment) */