1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
37 struct backtrace
*next
;
38 Lisp_Object
*function
;
39 Lisp_Object
*args
; /* Points to vector of args. */
40 ptrdiff_t nargs
; /* Length of vector. */
41 /* Nonzero means call value of debugger when done with this operation. */
42 unsigned int debug_on_exit
: 1;
45 static struct backtrace
*backtrace_list
;
50 struct catchtag
*catchlist
;
52 /* Chain of condition handlers currently in effect.
53 The elements of this chain are contained in the stack frames
54 of Fcondition_case and internal_condition_case.
55 When an error is signaled (by calling Fsignal, below),
56 this chain is searched for an element that applies. */
61 struct handler
*handlerlist
;
64 /* Count levels of GCPRO to detect failure to UNGCPRO. */
68 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
69 Lisp_Object Qinhibit_quit
;
70 Lisp_Object Qand_rest
;
71 static Lisp_Object Qand_optional
;
72 static Lisp_Object Qdebug_on_error
;
73 static Lisp_Object Qdeclare
;
74 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
76 static Lisp_Object Qdebug
;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
82 Lisp_Object Vrun_hooks
;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue
;
91 /* Current number of specbindings allocated in specpdl. */
93 EMACS_INT specpdl_size
;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding
*specpdl
;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding
*specpdl_ptr
;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth
;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
114 static int when_entered_debugger
;
116 /* The function from which the last `signal' was called. Set in
119 Lisp_Object Vsignaling_function
;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
127 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
128 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
129 static int interactive_p (int);
130 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
131 static Lisp_Object
Ffetch_bytecode (Lisp_Object
);
134 init_eval_once (void)
137 specpdl
= (struct specbinding
*) xmalloc (size
* sizeof (struct specbinding
));
139 specpdl_ptr
= specpdl
;
140 /* Don't forget to update docs (lispref node "Local Variables"). */
141 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
142 max_lisp_eval_depth
= 600;
150 specpdl_ptr
= specpdl
;
155 debug_on_next_call
= 0;
160 /* This is less than the initial value of num_nonmacro_input_events. */
161 when_entered_debugger
= -1;
164 /* Unwind-protect function used by call_debugger. */
167 restore_stack_limits (Lisp_Object data
)
169 max_specpdl_size
= XINT (XCAR (data
));
170 max_lisp_eval_depth
= XINT (XCDR (data
));
174 /* Call the Lisp debugger, giving it argument ARG. */
177 call_debugger (Lisp_Object arg
)
179 int debug_while_redisplaying
;
180 int count
= SPECPDL_INDEX ();
182 EMACS_INT old_max
= max_specpdl_size
;
184 /* Temporarily bump up the stack limits,
185 so the debugger won't run out of stack. */
187 max_specpdl_size
+= 1;
188 record_unwind_protect (restore_stack_limits
,
189 Fcons (make_number (old_max
),
190 make_number (max_lisp_eval_depth
)));
191 max_specpdl_size
= old_max
;
193 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
194 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
196 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
197 max_specpdl_size
= SPECPDL_INDEX () + 100;
199 #ifdef HAVE_WINDOW_SYSTEM
200 if (display_hourglass_p
)
204 debug_on_next_call
= 0;
205 when_entered_debugger
= num_nonmacro_input_events
;
207 /* Resetting redisplaying_p to 0 makes sure that debug output is
208 displayed if the debugger is invoked during redisplay. */
209 debug_while_redisplaying
= redisplaying_p
;
211 specbind (intern ("debugger-may-continue"),
212 debug_while_redisplaying
? Qnil
: Qt
);
213 specbind (Qinhibit_redisplay
, Qnil
);
214 specbind (Qdebug_on_error
, Qnil
);
216 #if 0 /* Binding this prevents execution of Lisp code during
217 redisplay, which necessarily leads to display problems. */
218 specbind (Qinhibit_eval_during_redisplay
, Qt
);
221 val
= apply1 (Vdebugger
, arg
);
223 /* Interrupting redisplay and resuming it later is not safe under
224 all circumstances. So, when the debugger returns, abort the
225 interrupted redisplay by going back to the top-level. */
226 if (debug_while_redisplaying
)
229 return unbind_to (count
, val
);
233 do_debug_on_call (Lisp_Object code
)
235 debug_on_next_call
= 0;
236 backtrace_list
->debug_on_exit
= 1;
237 call_debugger (Fcons (code
, Qnil
));
240 /* NOTE!!! Every function that can call EVAL must protect its args
241 and temporaries from garbage collection while it needs them.
242 The definition of `For' shows what you have to do. */
244 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
245 doc
: /* Eval args until one of them yields non-nil, then return that value.
246 The remaining args are not evalled at all.
247 If all args return nil, return nil.
248 usage: (or CONDITIONS...) */)
251 register Lisp_Object val
= Qnil
;
258 val
= eval_sub (XCAR (args
));
268 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
269 doc
: /* Eval args until one of them yields nil, then return nil.
270 The remaining args are not evalled at all.
271 If no arg yields nil, return the last arg's value.
272 usage: (and CONDITIONS...) */)
275 register Lisp_Object val
= Qt
;
282 val
= eval_sub (XCAR (args
));
292 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
293 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
294 Returns the value of THEN or the value of the last of the ELSE's.
295 THEN must be one expression, but ELSE... can be zero or more expressions.
296 If COND yields nil, and there are no ELSE's, the value is nil.
297 usage: (if COND THEN ELSE...) */)
300 register Lisp_Object cond
;
304 cond
= eval_sub (Fcar (args
));
308 return eval_sub (Fcar (Fcdr (args
)));
309 return Fprogn (Fcdr (Fcdr (args
)));
312 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
313 doc
: /* Try each clause until one succeeds.
314 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
315 and, if the value is non-nil, this clause succeeds:
316 then the expressions in BODY are evaluated and the last one's
317 value is the value of the cond-form.
318 If no clause succeeds, cond returns nil.
319 If a clause has one element, as in (CONDITION),
320 CONDITION's value if non-nil is returned from the cond-form.
321 usage: (cond CLAUSES...) */)
324 register Lisp_Object clause
, val
;
331 clause
= Fcar (args
);
332 val
= eval_sub (Fcar (clause
));
335 if (!EQ (XCDR (clause
), Qnil
))
336 val
= Fprogn (XCDR (clause
));
346 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
347 doc
: /* Eval BODY forms sequentially and return value of last one.
348 usage: (progn BODY...) */)
351 register Lisp_Object val
= Qnil
;
358 val
= eval_sub (XCAR (args
));
366 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
367 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
368 The value of FIRST is saved during the evaluation of the remaining args,
369 whose values are discarded.
370 usage: (prog1 FIRST BODY...) */)
374 register Lisp_Object args_left
;
375 struct gcpro gcpro1
, gcpro2
;
376 register int argnum
= 0;
387 Lisp_Object tem
= eval_sub (XCAR (args_left
));
390 args_left
= XCDR (args_left
);
392 while (CONSP (args_left
));
398 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
399 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
400 The value of FORM2 is saved during the evaluation of the
401 remaining args, whose values are discarded.
402 usage: (prog2 FORM1 FORM2 BODY...) */)
406 register Lisp_Object args_left
;
407 struct gcpro gcpro1
, gcpro2
;
408 register int argnum
= -1;
421 Lisp_Object tem
= eval_sub (XCAR (args_left
));
424 args_left
= XCDR (args_left
);
426 while (CONSP (args_left
));
432 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
433 doc
: /* Set each SYM to the value of its VAL.
434 The symbols SYM are variables; they are literal (not evaluated).
435 The values VAL are expressions; they are evaluated.
436 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
437 The second VAL is not computed until after the first SYM is set, and so on;
438 each VAL can use the new value of variables set earlier in the `setq'.
439 The return value of the `setq' form is the value of the last VAL.
440 usage: (setq [SYM VAL]...) */)
443 register Lisp_Object args_left
;
444 register Lisp_Object val
, sym
, lex_binding
;
455 val
= eval_sub (Fcar (Fcdr (args_left
)));
456 sym
= Fcar (args_left
);
458 /* Like for eval_sub, we do not check declared_special here since
459 it's been done when let-binding. */
460 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
462 && !NILP (lex_binding
463 = Fassq (sym
, Vinternal_interpreter_environment
)))
464 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
466 Fset (sym
, val
); /* SYM is dynamically bound. */
468 args_left
= Fcdr (Fcdr (args_left
));
470 while (!NILP (args_left
));
476 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
477 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
478 Warning: `quote' does not construct its return value, but just returns
479 the value that was pre-constructed by the Lisp reader (see info node
480 `(elisp)Printed Representation').
481 This means that '(a . b) is not identical to (cons 'a 'b): the former
482 does not cons. Quoting should be reserved for constants that will
483 never be modified by side-effects, unless you like self-modifying code.
484 See the common pitfall in info node `(elisp)Rearrangement' for an example
485 of unexpected results when a quoted object is modified.
486 usage: (quote ARG) */)
489 if (!NILP (Fcdr (args
)))
490 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
494 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
495 doc
: /* Like `quote', but preferred for objects which are functions.
496 In byte compilation, `function' causes its argument to be compiled.
497 `quote' cannot do that.
498 usage: (function ARG) */)
501 Lisp_Object quoted
= XCAR (args
);
503 if (!NILP (Fcdr (args
)))
504 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
506 if (!NILP (Vinternal_interpreter_environment
)
508 && EQ (XCAR (quoted
), Qlambda
))
509 /* This is a lambda expression within a lexical environment;
510 return an interpreted closure instead of a simple lambda. */
511 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
514 /* Simply quote the argument. */
519 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
520 doc
: /* Return t if the containing function was run directly by user input.
521 This means that the function was called with `call-interactively'
522 \(which includes being called as the binding of a key)
523 and input is currently coming from the keyboard (not a keyboard macro),
524 and Emacs is not running in batch mode (`noninteractive' is nil).
526 The only known proper use of `interactive-p' is in deciding whether to
527 display a helpful message, or how to display it. If you're thinking
528 of using it for any other purpose, it is quite likely that you're
529 making a mistake. Think: what do you want to do when the command is
530 called from a keyboard macro?
532 To test whether your function was called with `call-interactively',
533 either (i) add an extra optional argument and give it an `interactive'
534 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
535 use `called-interactively-p'. */)
538 return interactive_p (1) ? Qt
: Qnil
;
542 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
543 doc
: /* Return t if the containing function was called by `call-interactively'.
544 If KIND is `interactive', then only return t if the call was made
545 interactively by the user, i.e. not in `noninteractive' mode nor
546 when `executing-kbd-macro'.
547 If KIND is `any', on the other hand, it will return t for any kind of
548 interactive call, including being called as the binding of a key, or
549 from a keyboard macro, or in `noninteractive' mode.
551 The only known proper use of `interactive' for KIND is in deciding
552 whether to display a helpful message, or how to display it. If you're
553 thinking of using it for any other purpose, it is quite likely that
554 you're making a mistake. Think: what do you want to do when the
555 command is called from a keyboard macro?
557 This function is meant for implementing advice and other
558 function-modifying features. Instead of using this, it is sometimes
559 cleaner to give your function an extra optional argument whose
560 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
561 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
564 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
565 && interactive_p (1)) ? Qt
: Qnil
;
569 /* Return 1 if function in which this appears was called using
572 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
573 called is a built-in. */
576 interactive_p (int exclude_subrs_p
)
578 struct backtrace
*btp
;
581 btp
= backtrace_list
;
583 /* If this isn't a byte-compiled function, there may be a frame at
584 the top for Finteractive_p. If so, skip it. */
585 fun
= Findirect_function (*btp
->function
, Qnil
);
586 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
587 || XSUBR (fun
) == &Scalled_interactively_p
))
590 /* If we're running an Emacs 18-style byte-compiled function, there
591 may be a frame for Fbytecode at the top level. In any version of
592 Emacs there can be Fbytecode frames for subexpressions evaluated
593 inside catch and condition-case. Skip past them.
595 If this isn't a byte-compiled function, then we may now be
596 looking at several frames for special forms. Skip past them. */
598 && (EQ (*btp
->function
, Qbytecode
)
599 || btp
->nargs
== UNEVALLED
))
602 /* `btp' now points at the frame of the innermost function that isn't
603 a special form, ignoring frames for Finteractive_p and/or
604 Fbytecode at the top. If this frame is for a built-in function
605 (such as load or eval-region) return nil. */
606 fun
= Findirect_function (*btp
->function
, Qnil
);
607 if (exclude_subrs_p
&& SUBRP (fun
))
610 /* `btp' points to the frame of a Lisp function that called interactive-p.
611 Return t if that function was called interactively. */
612 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
618 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
619 doc
: /* Define NAME as a function.
620 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
621 See also the function `interactive'.
622 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
625 register Lisp_Object fn_name
;
626 register Lisp_Object defn
;
628 fn_name
= Fcar (args
);
629 CHECK_SYMBOL (fn_name
);
630 defn
= Fcons (Qlambda
, Fcdr (args
));
631 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
632 defn
= Ffunction (Fcons (defn
, Qnil
));
633 if (!NILP (Vpurify_flag
))
634 defn
= Fpurecopy (defn
);
635 if (CONSP (XSYMBOL (fn_name
)->function
)
636 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
637 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
638 Ffset (fn_name
, defn
);
639 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
643 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
644 doc
: /* Define NAME as a macro.
645 The actual definition looks like
646 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
647 When the macro is called, as in (NAME ARGS...),
648 the function (lambda ARGLIST BODY...) is applied to
649 the list ARGS... as it appears in the expression,
650 and the result should be a form to be evaluated instead of the original.
652 DECL is a declaration, optional, which can specify how to indent
653 calls to this macro, how Edebug should handle it, and which argument
654 should be treated as documentation. It looks like this:
656 The elements can look like this:
658 Set NAME's `lisp-indent-function' property to INDENT.
661 Set NAME's `edebug-form-spec' property to DEBUG. (This is
662 equivalent to writing a `def-edebug-spec' for the macro.)
665 Set NAME's `doc-string-elt' property to ELT.
667 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
670 register Lisp_Object fn_name
;
671 register Lisp_Object defn
;
672 Lisp_Object lambda_list
, doc
, tail
;
674 fn_name
= Fcar (args
);
675 CHECK_SYMBOL (fn_name
);
676 lambda_list
= Fcar (Fcdr (args
));
677 tail
= Fcdr (Fcdr (args
));
680 if (STRINGP (Fcar (tail
)))
686 if (CONSP (Fcar (tail
))
687 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
689 if (!NILP (Vmacro_declaration_function
))
693 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
701 tail
= Fcons (lambda_list
, tail
);
703 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
705 defn
= Fcons (Qlambda
, tail
);
706 if (!NILP (Vinternal_interpreter_environment
)) /* Mere optimization! */
707 defn
= Ffunction (Fcons (defn
, Qnil
));
708 defn
= Fcons (Qmacro
, defn
);
710 if (!NILP (Vpurify_flag
))
711 defn
= Fpurecopy (defn
);
712 if (CONSP (XSYMBOL (fn_name
)->function
)
713 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
714 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
715 Ffset (fn_name
, defn
);
716 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
721 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
722 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
723 Aliased variables always have the same value; setting one sets the other.
724 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
725 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
726 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
727 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
728 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
729 The return value is BASE-VARIABLE. */)
730 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
732 struct Lisp_Symbol
*sym
;
734 CHECK_SYMBOL (new_alias
);
735 CHECK_SYMBOL (base_variable
);
737 sym
= XSYMBOL (new_alias
);
740 /* Not sure why, but why not? */
741 error ("Cannot make a constant an alias");
743 switch (sym
->redirect
)
745 case SYMBOL_FORWARDED
:
746 error ("Cannot make an internal variable an alias");
747 case SYMBOL_LOCALIZED
:
748 error ("Don't know how to make a localized variable an alias");
751 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
752 If n_a is bound, but b_v is not, set the value of b_v to n_a,
753 so that old-code that affects n_a before the aliasing is setup
755 if (NILP (Fboundp (base_variable
)))
756 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
759 struct specbinding
*p
;
761 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
764 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
765 error ("Don't know how to make a let-bound variable an alias");
768 sym
->declared_special
= 1;
769 XSYMBOL (base_variable
)->declared_special
= 1;
770 sym
->redirect
= SYMBOL_VARALIAS
;
771 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
772 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
773 LOADHIST_ATTACH (new_alias
);
774 /* Even if docstring is nil: remove old docstring. */
775 Fput (new_alias
, Qvariable_documentation
, docstring
);
777 return base_variable
;
781 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
782 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
783 You are not required to define a variable in order to use it,
784 but the definition can supply documentation and an initial value
785 in a way that tags can recognize.
787 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
788 If SYMBOL is buffer-local, its default value is what is set;
789 buffer-local values are not affected.
790 INITVALUE and DOCSTRING are optional.
791 If DOCSTRING starts with *, this variable is identified as a user option.
792 This means that M-x set-variable recognizes it.
793 See also `user-variable-p'.
794 If INITVALUE is missing, SYMBOL's value is not set.
796 If SYMBOL has a local binding, then this form affects the local
797 binding. This is usually not what you want. Thus, if you need to
798 load a file defining variables, with this form or with `defconst' or
799 `defcustom', you should always load that file _outside_ any bindings
800 for these variables. \(`defconst' and `defcustom' behave similarly in
802 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
805 register Lisp_Object sym
, tem
, tail
;
809 if (!NILP (Fcdr (Fcdr (tail
))))
810 error ("Too many arguments");
812 tem
= Fdefault_boundp (sym
);
815 /* Do it before evaluating the initial value, for self-references. */
816 XSYMBOL (sym
)->declared_special
= 1;
818 if (SYMBOL_CONSTANT_P (sym
))
820 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
821 Lisp_Object tem1
= Fcar (tail
);
823 && EQ (XCAR (tem1
), Qquote
)
824 && CONSP (XCDR (tem1
))
825 && EQ (XCAR (XCDR (tem1
)), sym
)))
826 error ("Constant symbol `%s' specified in defvar",
827 SDATA (SYMBOL_NAME (sym
)));
831 Fset_default (sym
, eval_sub (Fcar (tail
)));
833 { /* Check if there is really a global binding rather than just a let
834 binding that shadows the global unboundness of the var. */
835 volatile struct specbinding
*pdl
= specpdl_ptr
;
836 while (--pdl
>= specpdl
)
838 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
839 && EQ (pdl
->old_value
, Qunbound
))
841 message_with_string ("Warning: defvar ignored because %s is let-bound",
842 SYMBOL_NAME (sym
), 1);
851 if (!NILP (Vpurify_flag
))
852 tem
= Fpurecopy (tem
);
853 Fput (sym
, Qvariable_documentation
, tem
);
855 LOADHIST_ATTACH (sym
);
857 else if (!NILP (Vinternal_interpreter_environment
)
858 && !XSYMBOL (sym
)->declared_special
)
859 /* A simple (defvar foo) with lexical scoping does "nothing" except
860 declare that var to be dynamically scoped *locally* (i.e. within
861 the current file or let-block). */
862 Vinternal_interpreter_environment
=
863 Fcons (sym
, Vinternal_interpreter_environment
);
866 /* Simple (defvar <var>) should not count as a definition at all.
867 It could get in the way of other definitions, and unloading this
868 package could try to make the variable unbound. */
874 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
875 doc
: /* Define SYMBOL as a constant variable.
876 The intent is that neither programs nor users should ever change this value.
877 Always sets the value of SYMBOL to the result of evalling INITVALUE.
878 If SYMBOL is buffer-local, its default value is what is set;
879 buffer-local values are not affected.
880 DOCSTRING is optional.
882 If SYMBOL has a local binding, then this form sets the local binding's
883 value. However, you should normally not make local bindings for
884 variables defined with this form.
885 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
888 register Lisp_Object sym
, tem
;
891 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
892 error ("Too many arguments");
894 tem
= eval_sub (Fcar (Fcdr (args
)));
895 if (!NILP (Vpurify_flag
))
896 tem
= Fpurecopy (tem
);
897 Fset_default (sym
, tem
);
898 XSYMBOL (sym
)->declared_special
= 1;
899 tem
= Fcar (Fcdr (Fcdr (args
)));
902 if (!NILP (Vpurify_flag
))
903 tem
= Fpurecopy (tem
);
904 Fput (sym
, Qvariable_documentation
, tem
);
906 Fput (sym
, Qrisky_local_variable
, Qt
);
907 LOADHIST_ATTACH (sym
);
911 /* Error handler used in Fuser_variable_p. */
913 user_variable_p_eh (Lisp_Object ignore
)
919 lisp_indirect_variable (Lisp_Object sym
)
921 struct Lisp_Symbol
*s
= indirect_variable (XSYMBOL (sym
));
926 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
927 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
928 \(The alternative is a variable used internally in a Lisp program.)
929 A variable is a user variable if
930 \(1) the first character of its documentation is `*', or
931 \(2) it is customizable (its property list contains a non-nil value
932 of `standard-value' or `custom-autoload'), or
933 \(3) it is an alias for another user variable.
934 Return nil if VARIABLE is an alias and there is a loop in the
935 chain of symbols. */)
936 (Lisp_Object variable
)
938 Lisp_Object documentation
;
940 if (!SYMBOLP (variable
))
943 /* If indirect and there's an alias loop, don't check anything else. */
944 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
945 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
946 Qt
, user_variable_p_eh
)))
951 documentation
= Fget (variable
, Qvariable_documentation
);
952 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
954 if (STRINGP (documentation
)
955 && ((unsigned char) SREF (documentation
, 0) == '*'))
957 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
958 if (CONSP (documentation
)
959 && STRINGP (XCAR (documentation
))
960 && INTEGERP (XCDR (documentation
))
961 && XINT (XCDR (documentation
)) < 0)
963 /* Customizable? See `custom-variable-p'. */
964 if ((!NILP (Fget (variable
, intern ("standard-value"))))
965 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
968 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
971 /* An indirect variable? Let's follow the chain. */
972 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
976 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
977 doc
: /* Bind variables according to VARLIST then eval BODY.
978 The value of the last form in BODY is returned.
979 Each element of VARLIST is a symbol (which is bound to nil)
980 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
981 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
982 usage: (let* VARLIST BODY...) */)
985 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
986 int count
= SPECPDL_INDEX ();
987 struct gcpro gcpro1
, gcpro2
, gcpro3
;
989 GCPRO3 (args
, elt
, varlist
);
991 lexenv
= Vinternal_interpreter_environment
;
993 varlist
= Fcar (args
);
994 while (CONSP (varlist
))
998 elt
= XCAR (varlist
);
1004 else if (! NILP (Fcdr (Fcdr (elt
))))
1005 signal_error ("`let' bindings can have only one value-form", elt
);
1009 val
= eval_sub (Fcar (Fcdr (elt
)));
1012 if (!NILP (lexenv
) && SYMBOLP (var
)
1013 && !XSYMBOL (var
)->declared_special
1014 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
1015 /* Lexically bind VAR by adding it to the interpreter's binding
1019 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
1020 if (EQ (Vinternal_interpreter_environment
, lexenv
))
1021 /* Save the old lexical environment on the specpdl stack,
1022 but only for the first lexical binding, since we'll never
1023 need to revert to one of the intermediate ones. */
1024 specbind (Qinternal_interpreter_environment
, newenv
);
1026 Vinternal_interpreter_environment
= newenv
;
1029 specbind (var
, val
);
1031 varlist
= XCDR (varlist
);
1034 val
= Fprogn (Fcdr (args
));
1035 return unbind_to (count
, val
);
1038 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
1039 doc
: /* Bind variables according to VARLIST then eval BODY.
1040 The value of the last form in BODY is returned.
1041 Each element of VARLIST is a symbol (which is bound to nil)
1042 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1043 All the VALUEFORMs are evalled before any symbols are bound.
1044 usage: (let VARLIST BODY...) */)
1047 Lisp_Object
*temps
, tem
, lexenv
;
1048 register Lisp_Object elt
, varlist
;
1049 int count
= SPECPDL_INDEX ();
1051 struct gcpro gcpro1
, gcpro2
;
1054 varlist
= Fcar (args
);
1056 /* Make space to hold the values to give the bound variables. */
1057 elt
= Flength (varlist
);
1058 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
1060 /* Compute the values and store them in `temps'. */
1062 GCPRO2 (args
, *temps
);
1065 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1068 elt
= XCAR (varlist
);
1070 temps
[argnum
++] = Qnil
;
1071 else if (! NILP (Fcdr (Fcdr (elt
))))
1072 signal_error ("`let' bindings can have only one value-form", elt
);
1074 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
1075 gcpro2
.nvars
= argnum
;
1079 lexenv
= Vinternal_interpreter_environment
;
1081 varlist
= Fcar (args
);
1082 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
1086 elt
= XCAR (varlist
);
1087 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
1088 tem
= temps
[argnum
++];
1090 if (!NILP (lexenv
) && SYMBOLP (var
)
1091 && !XSYMBOL (var
)->declared_special
1092 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
1093 /* Lexically bind VAR by adding it to the lexenv alist. */
1094 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
1096 /* Dynamically bind VAR. */
1097 specbind (var
, tem
);
1100 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
1101 /* Instantiate a new lexical environment. */
1102 specbind (Qinternal_interpreter_environment
, lexenv
);
1104 elt
= Fprogn (Fcdr (args
));
1106 return unbind_to (count
, elt
);
1109 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1110 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1111 The order of execution is thus TEST, BODY, TEST, BODY and so on
1112 until TEST returns nil.
1113 usage: (while TEST BODY...) */)
1116 Lisp_Object test
, body
;
1117 struct gcpro gcpro1
, gcpro2
;
1119 GCPRO2 (test
, body
);
1123 while (!NILP (eval_sub (test
)))
1133 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1134 doc
: /* Return result of expanding macros at top level of FORM.
1135 If FORM is not a macro call, it is returned unchanged.
1136 Otherwise, the macro is expanded and the expansion is considered
1137 in place of FORM. When a non-macro-call results, it is returned.
1139 The second optional arg ENVIRONMENT specifies an environment of macro
1140 definitions to shadow the loaded ones for use in file byte-compilation. */)
1141 (Lisp_Object form
, Lisp_Object environment
)
1143 /* With cleanups from Hallvard Furuseth. */
1144 register Lisp_Object expander
, sym
, def
, tem
;
1148 /* Come back here each time we expand a macro call,
1149 in case it expands into another macro call. */
1152 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1153 def
= sym
= XCAR (form
);
1155 /* Trace symbols aliases to other symbols
1156 until we get a symbol that is not an alias. */
1157 while (SYMBOLP (def
))
1161 tem
= Fassq (sym
, environment
);
1164 def
= XSYMBOL (sym
)->function
;
1165 if (!EQ (def
, Qunbound
))
1170 /* Right now TEM is the result from SYM in ENVIRONMENT,
1171 and if TEM is nil then DEF is SYM's function definition. */
1174 /* SYM is not mentioned in ENVIRONMENT.
1175 Look at its function definition. */
1176 if (EQ (def
, Qunbound
) || !CONSP (def
))
1177 /* Not defined or definition not suitable. */
1179 if (EQ (XCAR (def
), Qautoload
))
1181 /* Autoloading function: will it be a macro when loaded? */
1182 tem
= Fnth (make_number (4), def
);
1183 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1184 /* Yes, load it and try again. */
1186 struct gcpro gcpro1
;
1188 do_autoload (def
, sym
);
1195 else if (!EQ (XCAR (def
), Qmacro
))
1197 else expander
= XCDR (def
);
1201 expander
= XCDR (tem
);
1202 if (NILP (expander
))
1205 form
= apply1 (expander
, XCDR (form
));
1210 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1211 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1212 TAG is evalled to get the tag to use; it must not be nil.
1214 Then the BODY is executed.
1215 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1216 If no throw happens, `catch' returns the value of the last BODY form.
1217 If a throw happens, it specifies the value to return from `catch'.
1218 usage: (catch TAG BODY...) */)
1221 register Lisp_Object tag
;
1222 struct gcpro gcpro1
;
1225 tag
= eval_sub (Fcar (args
));
1227 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1230 /* Set up a catch, then call C function FUNC on argument ARG.
1231 FUNC should return a Lisp_Object.
1232 This is how catches are done from within C code. */
1235 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1237 /* This structure is made part of the chain `catchlist'. */
1240 /* Fill in the components of c, and put it on the list. */
1244 c
.backlist
= backtrace_list
;
1245 c
.handlerlist
= handlerlist
;
1246 c
.lisp_eval_depth
= lisp_eval_depth
;
1247 c
.pdlcount
= SPECPDL_INDEX ();
1248 c
.poll_suppress_count
= poll_suppress_count
;
1249 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1250 c
.gcpro
= gcprolist
;
1251 c
.byte_stack
= byte_stack_list
;
1255 if (! _setjmp (c
.jmp
))
1256 c
.val
= (*func
) (arg
);
1258 /* Throw works by a longjmp that comes right here. */
1263 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1264 jump to that CATCH, returning VALUE as the value of that catch.
1266 This is the guts Fthrow and Fsignal; they differ only in the way
1267 they choose the catch tag to throw to. A catch tag for a
1268 condition-case form has a TAG of Qnil.
1270 Before each catch is discarded, unbind all special bindings and
1271 execute all unwind-protect clauses made above that catch. Unwind
1272 the handler stack as we go, so that the proper handlers are in
1273 effect for each unwind-protect clause we run. At the end, restore
1274 some static info saved in CATCH, and longjmp to the location
1277 This is used for correct unwinding in Fthrow and Fsignal. */
1280 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1282 register int last_time
;
1284 /* Save the value in the tag. */
1287 /* Restore certain special C variables. */
1288 set_poll_suppress_count (catch->poll_suppress_count
);
1289 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1290 handling_signal
= 0;
1295 last_time
= catchlist
== catch;
1297 /* Unwind the specpdl stack, and then restore the proper set of
1299 unbind_to (catchlist
->pdlcount
, Qnil
);
1300 handlerlist
= catchlist
->handlerlist
;
1301 catchlist
= catchlist
->next
;
1303 while (! last_time
);
1306 /* If x_catch_errors was done, turn it off now.
1307 (First we give unbind_to a chance to do that.) */
1308 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1309 The catch must remain in effect during that delicate
1310 state. --lorentey */
1311 x_fully_uncatch_errors ();
1315 byte_stack_list
= catch->byte_stack
;
1316 gcprolist
= catch->gcpro
;
1318 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1320 backtrace_list
= catch->backlist
;
1321 lisp_eval_depth
= catch->lisp_eval_depth
;
1323 _longjmp (catch->jmp
, 1);
1326 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1327 doc
: /* Throw to the catch for TAG and return VALUE from it.
1328 Both TAG and VALUE are evalled. */)
1329 (register Lisp_Object tag
, Lisp_Object value
)
1331 register struct catchtag
*c
;
1334 for (c
= catchlist
; c
; c
= c
->next
)
1336 if (EQ (c
->tag
, tag
))
1337 unwind_to_catch (c
, value
);
1339 xsignal2 (Qno_catch
, tag
, value
);
1343 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1344 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1345 If BODYFORM completes normally, its value is returned
1346 after executing the UNWINDFORMS.
1347 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1348 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1352 int count
= SPECPDL_INDEX ();
1354 record_unwind_protect (Fprogn
, Fcdr (args
));
1355 val
= eval_sub (Fcar (args
));
1356 return unbind_to (count
, val
);
1359 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1360 doc
: /* Regain control when an error is signaled.
1361 Executes BODYFORM and returns its value if no error happens.
1362 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1363 where the BODY is made of Lisp expressions.
1365 A handler is applicable to an error
1366 if CONDITION-NAME is one of the error's condition names.
1367 If an error happens, the first applicable handler is run.
1369 The car of a handler may be a list of condition names instead of a
1370 single condition name; then it handles all of them. If the special
1371 condition name `debug' is present in this list, it allows another
1372 condition in the list to run the debugger if `debug-on-error' and the
1373 other usual mechanisms says it should (otherwise, `condition-case'
1374 suppresses the debugger).
1376 When a handler handles an error, control returns to the `condition-case'
1377 and it executes the handler's BODY...
1378 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1379 \(If VAR is nil, the handler can't access that information.)
1380 Then the value of the last BODY form is returned from the `condition-case'
1383 See also the function `signal' for more info.
1384 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1387 register Lisp_Object bodyform
, handlers
;
1388 volatile Lisp_Object var
;
1391 bodyform
= Fcar (Fcdr (args
));
1392 handlers
= Fcdr (Fcdr (args
));
1394 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1397 /* Like Fcondition_case, but the args are separate
1398 rather than passed in a list. Used by Fbyte_code. */
1401 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1402 Lisp_Object handlers
)
1410 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1416 && (SYMBOLP (XCAR (tem
))
1417 || CONSP (XCAR (tem
))))))
1418 error ("Invalid condition handler: %s",
1419 SDATA (Fprin1_to_string (tem
, Qt
)));
1424 c
.backlist
= backtrace_list
;
1425 c
.handlerlist
= handlerlist
;
1426 c
.lisp_eval_depth
= lisp_eval_depth
;
1427 c
.pdlcount
= SPECPDL_INDEX ();
1428 c
.poll_suppress_count
= poll_suppress_count
;
1429 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1430 c
.gcpro
= gcprolist
;
1431 c
.byte_stack
= byte_stack_list
;
1432 if (_setjmp (c
.jmp
))
1435 specbind (h
.var
, c
.val
);
1436 val
= Fprogn (Fcdr (h
.chosen_clause
));
1438 /* Note that this just undoes the binding of h.var; whoever
1439 longjumped to us unwound the stack to c.pdlcount before
1441 unbind_to (c
.pdlcount
, Qnil
);
1448 h
.handler
= handlers
;
1449 h
.next
= handlerlist
;
1453 val
= eval_sub (bodyform
);
1455 handlerlist
= h
.next
;
1459 /* Call the function BFUN with no arguments, catching errors within it
1460 according to HANDLERS. If there is an error, call HFUN with
1461 one argument which is the data that describes the error:
1464 HANDLERS can be a list of conditions to catch.
1465 If HANDLERS is Qt, catch all errors.
1466 If HANDLERS is Qerror, catch all errors
1467 but allow the debugger to run if that is enabled. */
1470 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1471 Lisp_Object (*hfun
) (Lisp_Object
))
1479 c
.backlist
= backtrace_list
;
1480 c
.handlerlist
= handlerlist
;
1481 c
.lisp_eval_depth
= lisp_eval_depth
;
1482 c
.pdlcount
= SPECPDL_INDEX ();
1483 c
.poll_suppress_count
= poll_suppress_count
;
1484 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1485 c
.gcpro
= gcprolist
;
1486 c
.byte_stack
= byte_stack_list
;
1487 if (_setjmp (c
.jmp
))
1489 return (*hfun
) (c
.val
);
1493 h
.handler
= handlers
;
1495 h
.next
= handlerlist
;
1501 handlerlist
= h
.next
;
1505 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1508 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1509 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1517 c
.backlist
= backtrace_list
;
1518 c
.handlerlist
= handlerlist
;
1519 c
.lisp_eval_depth
= lisp_eval_depth
;
1520 c
.pdlcount
= SPECPDL_INDEX ();
1521 c
.poll_suppress_count
= poll_suppress_count
;
1522 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1523 c
.gcpro
= gcprolist
;
1524 c
.byte_stack
= byte_stack_list
;
1525 if (_setjmp (c
.jmp
))
1527 return (*hfun
) (c
.val
);
1531 h
.handler
= handlers
;
1533 h
.next
= handlerlist
;
1537 val
= (*bfun
) (arg
);
1539 handlerlist
= h
.next
;
1543 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1547 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1550 Lisp_Object handlers
,
1551 Lisp_Object (*hfun
) (Lisp_Object
))
1559 c
.backlist
= backtrace_list
;
1560 c
.handlerlist
= handlerlist
;
1561 c
.lisp_eval_depth
= lisp_eval_depth
;
1562 c
.pdlcount
= SPECPDL_INDEX ();
1563 c
.poll_suppress_count
= poll_suppress_count
;
1564 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1565 c
.gcpro
= gcprolist
;
1566 c
.byte_stack
= byte_stack_list
;
1567 if (_setjmp (c
.jmp
))
1569 return (*hfun
) (c
.val
);
1573 h
.handler
= handlers
;
1575 h
.next
= handlerlist
;
1579 val
= (*bfun
) (arg1
, arg2
);
1581 handlerlist
= h
.next
;
1585 /* Like internal_condition_case but call BFUN with NARGS as first,
1586 and ARGS as second argument. */
1589 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1592 Lisp_Object handlers
,
1593 Lisp_Object (*hfun
) (Lisp_Object
))
1601 c
.backlist
= backtrace_list
;
1602 c
.handlerlist
= handlerlist
;
1603 c
.lisp_eval_depth
= lisp_eval_depth
;
1604 c
.pdlcount
= SPECPDL_INDEX ();
1605 c
.poll_suppress_count
= poll_suppress_count
;
1606 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1607 c
.gcpro
= gcprolist
;
1608 c
.byte_stack
= byte_stack_list
;
1609 if (_setjmp (c
.jmp
))
1611 return (*hfun
) (c
.val
);
1615 h
.handler
= handlers
;
1617 h
.next
= handlerlist
;
1621 val
= (*bfun
) (nargs
, args
);
1623 handlerlist
= h
.next
;
1628 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1629 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1632 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1633 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1634 This function does not return.
1636 An error symbol is a symbol with an `error-conditions' property
1637 that is a list of condition names.
1638 A handler for any of those names will get to handle this signal.
1639 The symbol `error' should normally be one of them.
1641 DATA should be a list. Its elements are printed as part of the error message.
1642 See Info anchor `(elisp)Definition of signal' for some details on how this
1643 error message is constructed.
1644 If the signal is handled, DATA is made available to the handler.
1645 See also the function `condition-case'. */)
1646 (Lisp_Object error_symbol
, Lisp_Object data
)
1648 /* When memory is full, ERROR-SYMBOL is nil,
1649 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1650 That is a special case--don't do this in other situations. */
1651 Lisp_Object conditions
;
1653 Lisp_Object real_error_symbol
1654 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1655 register Lisp_Object clause
= Qnil
;
1657 struct backtrace
*bp
;
1659 immediate_quit
= handling_signal
= 0;
1661 if (gc_in_progress
|| waiting_for_input
)
1664 #if 0 /* rms: I don't know why this was here,
1665 but it is surely wrong for an error that is handled. */
1666 #ifdef HAVE_WINDOW_SYSTEM
1667 if (display_hourglass_p
)
1668 cancel_hourglass ();
1672 /* This hook is used by edebug. */
1673 if (! NILP (Vsignal_hook_function
)
1674 && ! NILP (error_symbol
))
1676 /* Edebug takes care of restoring these variables when it exits. */
1677 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1678 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1680 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1681 max_specpdl_size
= SPECPDL_INDEX () + 40;
1683 call2 (Vsignal_hook_function
, error_symbol
, data
);
1686 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1688 /* Remember from where signal was called. Skip over the frame for
1689 `signal' itself. If a frame for `error' follows, skip that,
1690 too. Don't do this when ERROR_SYMBOL is nil, because that
1691 is a memory-full error. */
1692 Vsignaling_function
= Qnil
;
1693 if (backtrace_list
&& !NILP (error_symbol
))
1695 bp
= backtrace_list
->next
;
1696 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1698 if (bp
&& bp
->function
)
1699 Vsignaling_function
= *bp
->function
;
1702 for (h
= handlerlist
; h
; h
= h
->next
)
1704 clause
= find_handler_clause (h
->handler
, conditions
);
1709 if (/* Don't run the debugger for a memory-full error.
1710 (There is no room in memory to do that!) */
1711 !NILP (error_symbol
)
1712 && (!NILP (Vdebug_on_signal
)
1713 /* If no handler is present now, try to run the debugger. */
1715 /* A `debug' symbol in the handler list disables the normal
1716 suppression of the debugger. */
1717 || (CONSP (clause
) && CONSP (XCAR (clause
))
1718 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1719 /* Special handler that means "print a message and run debugger
1721 || EQ (h
->handler
, Qerror
)))
1724 = maybe_call_debugger (conditions
, error_symbol
, data
);
1725 /* We can't return values to code which signaled an error, but we
1726 can continue code which has signaled a quit. */
1727 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1733 Lisp_Object unwind_data
1734 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1736 h
->chosen_clause
= clause
;
1737 unwind_to_catch (h
->tag
, unwind_data
);
1742 Fthrow (Qtop_level
, Qt
);
1745 if (! NILP (error_symbol
))
1746 data
= Fcons (error_symbol
, data
);
1748 string
= Ferror_message_string (data
);
1749 fatal ("%s", SDATA (string
));
1752 /* Internal version of Fsignal that never returns.
1753 Used for anything but Qquit (which can return from Fsignal). */
1756 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1758 Fsignal (error_symbol
, data
);
1762 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1765 xsignal0 (Lisp_Object error_symbol
)
1767 xsignal (error_symbol
, Qnil
);
1771 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1773 xsignal (error_symbol
, list1 (arg
));
1777 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1779 xsignal (error_symbol
, list2 (arg1
, arg2
));
1783 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1785 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1788 /* Signal `error' with message S, and additional arg ARG.
1789 If ARG is not a genuine list, make it a one-element list. */
1792 signal_error (const char *s
, Lisp_Object arg
)
1794 Lisp_Object tortoise
, hare
;
1796 hare
= tortoise
= arg
;
1797 while (CONSP (hare
))
1804 tortoise
= XCDR (tortoise
);
1806 if (EQ (hare
, tortoise
))
1811 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1813 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1817 /* Return nonzero if LIST is a non-nil atom or
1818 a list containing one of CONDITIONS. */
1821 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1828 while (CONSP (conditions
))
1830 Lisp_Object
this, tail
;
1831 this = XCAR (conditions
);
1832 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1833 if (EQ (XCAR (tail
), this))
1835 conditions
= XCDR (conditions
);
1840 /* Return 1 if an error with condition-symbols CONDITIONS,
1841 and described by SIGNAL-DATA, should skip the debugger
1842 according to debugger-ignored-errors. */
1845 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1848 int first_string
= 1;
1849 Lisp_Object error_message
;
1851 error_message
= Qnil
;
1852 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1854 if (STRINGP (XCAR (tail
)))
1858 error_message
= Ferror_message_string (data
);
1862 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1867 Lisp_Object contail
;
1869 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1870 if (EQ (XCAR (tail
), XCAR (contail
)))
1878 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1879 SIG and DATA describe the signal. There are two ways to pass them:
1880 = SIG is the error symbol, and DATA is the rest of the data.
1881 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1882 This is for memory-full errors only. */
1884 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1886 Lisp_Object combined_data
;
1888 combined_data
= Fcons (sig
, data
);
1891 /* Don't try to run the debugger with interrupts blocked.
1892 The editing loop would return anyway. */
1894 /* Does user want to enter debugger for this kind of error? */
1897 : wants_debugger (Vdebug_on_error
, conditions
))
1898 && ! skip_debugger (conditions
, combined_data
)
1899 /* RMS: What's this for? */
1900 && when_entered_debugger
< num_nonmacro_input_events
)
1902 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1910 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1912 register Lisp_Object h
;
1914 /* t is used by handlers for all conditions, set up by C code. */
1915 if (EQ (handlers
, Qt
))
1918 /* error is used similarly, but means print an error message
1919 and run the debugger if that is enabled. */
1920 if (EQ (handlers
, Qerror
))
1923 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1925 Lisp_Object handler
= XCAR (h
);
1926 Lisp_Object condit
, tem
;
1928 if (!CONSP (handler
))
1930 condit
= XCAR (handler
);
1931 /* Handle a single condition name in handler HANDLER. */
1932 if (SYMBOLP (condit
))
1934 tem
= Fmemq (Fcar (handler
), conditions
);
1938 /* Handle a list of condition names in handler HANDLER. */
1939 else if (CONSP (condit
))
1942 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1944 tem
= Fmemq (XCAR (tail
), conditions
);
1955 /* Dump an error message; called like vprintf. */
1957 verror (const char *m
, va_list ap
)
1960 ptrdiff_t size
= sizeof buf
;
1961 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1966 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1967 string
= make_string (buffer
, used
);
1971 xsignal1 (Qerror
, string
);
1975 /* Dump an error message; called like printf. */
1979 error (const char *m
, ...)
1987 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1988 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1989 This means it contains a description for how to read arguments to give it.
1990 The value is nil for an invalid function or a symbol with no function
1993 Interactively callable functions include strings and vectors (treated
1994 as keyboard macros), lambda-expressions that contain a top-level call
1995 to `interactive', autoload definitions made by `autoload' with non-nil
1996 fourth argument, and some of the built-in functions of Lisp.
1998 Also, a symbol satisfies `commandp' if its function definition does so.
2000 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2001 then strings and vectors are not accepted. */)
2002 (Lisp_Object function
, Lisp_Object for_call_interactively
)
2004 register Lisp_Object fun
;
2005 register Lisp_Object funcar
;
2006 Lisp_Object if_prop
= Qnil
;
2010 fun
= indirect_function (fun
); /* Check cycles. */
2011 if (NILP (fun
) || EQ (fun
, Qunbound
))
2014 /* Check an `interactive-form' property if present, analogous to the
2015 function-documentation property. */
2017 while (SYMBOLP (fun
))
2019 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
2022 fun
= Fsymbol_function (fun
);
2025 /* Emacs primitives are interactive if their DEFUN specifies an
2026 interactive spec. */
2028 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
2030 /* Bytecode objects are interactive if they are long enough to
2031 have an element whose index is COMPILED_INTERACTIVE, which is
2032 where the interactive spec is stored. */
2033 else if (COMPILEDP (fun
))
2034 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
2037 /* Strings and vectors are keyboard macros. */
2038 if (STRINGP (fun
) || VECTORP (fun
))
2039 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
2041 /* Lists may represent commands. */
2044 funcar
= XCAR (fun
);
2045 if (EQ (funcar
, Qclosure
))
2046 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
2048 else if (EQ (funcar
, Qlambda
))
2049 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2050 else if (EQ (funcar
, Qautoload
))
2051 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2056 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2057 doc
: /* Define FUNCTION to autoload from FILE.
2058 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2059 Third arg DOCSTRING is documentation for the function.
2060 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2061 Fifth arg TYPE indicates the type of the object:
2062 nil or omitted says FUNCTION is a function,
2063 `keymap' says FUNCTION is really a keymap, and
2064 `macro' or t says FUNCTION is really a macro.
2065 Third through fifth args give info about the real definition.
2066 They default to nil.
2067 If FUNCTION is already defined other than as an autoload,
2068 this does nothing and returns nil. */)
2069 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2071 CHECK_SYMBOL (function
);
2072 CHECK_STRING (file
);
2074 /* If function is defined and not as an autoload, don't override. */
2075 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2076 && !(CONSP (XSYMBOL (function
)->function
)
2077 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2080 if (NILP (Vpurify_flag
))
2081 /* Only add entries after dumping, because the ones before are
2082 not useful and else we get loads of them from the loaddefs.el. */
2083 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2085 /* We don't want the docstring in purespace (instead,
2086 Snarf-documentation should (hopefully) overwrite it).
2087 We used to use 0 here, but that leads to accidental sharing in
2088 purecopy's hash-consing, so we use a (hopefully) unique integer
2090 docstring
= make_number (XPNTR (function
));
2091 return Ffset (function
,
2092 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2093 interactive
, type
)));
2097 un_autoload (Lisp_Object oldqueue
)
2099 register Lisp_Object queue
, first
, second
;
2101 /* Queue to unwind is current value of Vautoload_queue.
2102 oldqueue is the shadowed value to leave in Vautoload_queue. */
2103 queue
= Vautoload_queue
;
2104 Vautoload_queue
= oldqueue
;
2105 while (CONSP (queue
))
2107 first
= XCAR (queue
);
2108 second
= Fcdr (first
);
2109 first
= Fcar (first
);
2110 if (EQ (first
, make_number (0)))
2113 Ffset (first
, second
);
2114 queue
= XCDR (queue
);
2119 /* Load an autoloaded function.
2120 FUNNAME is the symbol which is the function's name.
2121 FUNDEF is the autoload definition (a list). */
2124 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2126 int count
= SPECPDL_INDEX ();
2128 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2130 /* This is to make sure that loadup.el gives a clear picture
2131 of what files are preloaded and when. */
2132 if (! NILP (Vpurify_flag
))
2133 error ("Attempt to autoload %s while preparing to dump",
2134 SDATA (SYMBOL_NAME (funname
)));
2137 CHECK_SYMBOL (funname
);
2138 GCPRO3 (fun
, funname
, fundef
);
2140 /* Preserve the match data. */
2141 record_unwind_save_match_data ();
2143 /* If autoloading gets an error (which includes the error of failing
2144 to define the function being called), we use Vautoload_queue
2145 to undo function definitions and `provide' calls made by
2146 the function. We do this in the specific case of autoloading
2147 because autoloading is not an explicit request "load this file",
2148 but rather a request to "call this function".
2150 The value saved here is to be restored into Vautoload_queue. */
2151 record_unwind_protect (un_autoload
, Vautoload_queue
);
2152 Vautoload_queue
= Qt
;
2153 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2155 /* Once loading finishes, don't undo it. */
2156 Vautoload_queue
= Qt
;
2157 unbind_to (count
, Qnil
);
2159 fun
= Findirect_function (fun
, Qnil
);
2161 if (!NILP (Fequal (fun
, fundef
)))
2162 error ("Autoloading failed to define function %s",
2163 SDATA (SYMBOL_NAME (funname
)));
2168 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2169 doc
: /* Evaluate FORM and return its value.
2170 If LEXICAL is t, evaluate using lexical scoping. */)
2171 (Lisp_Object form
, Lisp_Object lexical
)
2173 int count
= SPECPDL_INDEX ();
2174 specbind (Qinternal_interpreter_environment
,
2175 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2176 return unbind_to (count
, eval_sub (form
));
2179 /* Eval a sub-expression of the current expression (i.e. in the same
2182 eval_sub (Lisp_Object form
)
2184 Lisp_Object fun
, val
, original_fun
, original_args
;
2186 struct backtrace backtrace
;
2187 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2189 if (handling_signal
)
2194 /* Look up its binding in the lexical environment.
2195 We do not pay attention to the declared_special flag here, since we
2196 already did that when let-binding the variable. */
2197 Lisp_Object lex_binding
2198 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2199 ? Fassq (form
, Vinternal_interpreter_environment
)
2201 if (CONSP (lex_binding
))
2202 return XCDR (lex_binding
);
2204 return Fsymbol_value (form
);
2211 if ((consing_since_gc
> gc_cons_threshold
2212 && consing_since_gc
> gc_relative_threshold
)
2214 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2217 Fgarbage_collect ();
2221 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2223 if (max_lisp_eval_depth
< 100)
2224 max_lisp_eval_depth
= 100;
2225 if (lisp_eval_depth
> max_lisp_eval_depth
)
2226 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2229 original_fun
= Fcar (form
);
2230 original_args
= Fcdr (form
);
2232 backtrace
.next
= backtrace_list
;
2233 backtrace_list
= &backtrace
;
2234 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2235 backtrace
.args
= &original_args
;
2236 backtrace
.nargs
= UNEVALLED
;
2237 backtrace
.debug_on_exit
= 0;
2239 if (debug_on_next_call
)
2240 do_debug_on_call (Qt
);
2242 /* At this point, only original_fun and original_args
2243 have values that will be used below. */
2246 /* Optimize for no indirection. */
2248 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2249 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2250 fun
= indirect_function (fun
);
2254 Lisp_Object numargs
;
2255 Lisp_Object argvals
[8];
2256 Lisp_Object args_left
;
2257 register int i
, maxargs
;
2259 args_left
= original_args
;
2260 numargs
= Flength (args_left
);
2264 if (XINT (numargs
) < XSUBR (fun
)->min_args
2265 || (XSUBR (fun
)->max_args
>= 0
2266 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2267 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2269 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2270 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2271 else if (XSUBR (fun
)->max_args
== MANY
)
2273 /* Pass a vector of evaluated arguments. */
2275 ptrdiff_t argnum
= 0;
2278 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2280 GCPRO3 (args_left
, fun
, fun
);
2284 while (!NILP (args_left
))
2286 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2287 args_left
= Fcdr (args_left
);
2288 gcpro3
.nvars
= argnum
;
2291 backtrace
.args
= vals
;
2292 backtrace
.nargs
= XINT (numargs
);
2294 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2300 GCPRO3 (args_left
, fun
, fun
);
2301 gcpro3
.var
= argvals
;
2304 maxargs
= XSUBR (fun
)->max_args
;
2305 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2307 argvals
[i
] = eval_sub (Fcar (args_left
));
2313 backtrace
.args
= argvals
;
2314 backtrace
.nargs
= XINT (numargs
);
2319 val
= (XSUBR (fun
)->function
.a0 ());
2322 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2325 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2328 val
= (XSUBR (fun
)->function
.a3
2329 (argvals
[0], argvals
[1], argvals
[2]));
2332 val
= (XSUBR (fun
)->function
.a4
2333 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2336 val
= (XSUBR (fun
)->function
.a5
2337 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2341 val
= (XSUBR (fun
)->function
.a6
2342 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2343 argvals
[4], argvals
[5]));
2346 val
= (XSUBR (fun
)->function
.a7
2347 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2348 argvals
[4], argvals
[5], argvals
[6]));
2352 val
= (XSUBR (fun
)->function
.a8
2353 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2354 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2358 /* Someone has created a subr that takes more arguments than
2359 is supported by this code. We need to either rewrite the
2360 subr to use a different argument protocol, or add more
2361 cases to this switch. */
2366 else if (COMPILEDP (fun
))
2367 val
= apply_lambda (fun
, original_args
);
2370 if (EQ (fun
, Qunbound
))
2371 xsignal1 (Qvoid_function
, original_fun
);
2373 xsignal1 (Qinvalid_function
, original_fun
);
2374 funcar
= XCAR (fun
);
2375 if (!SYMBOLP (funcar
))
2376 xsignal1 (Qinvalid_function
, original_fun
);
2377 if (EQ (funcar
, Qautoload
))
2379 do_autoload (fun
, original_fun
);
2382 if (EQ (funcar
, Qmacro
))
2383 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2384 else if (EQ (funcar
, Qlambda
)
2385 || EQ (funcar
, Qclosure
))
2386 val
= apply_lambda (fun
, original_args
);
2388 xsignal1 (Qinvalid_function
, original_fun
);
2393 if (backtrace
.debug_on_exit
)
2394 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2395 backtrace_list
= backtrace
.next
;
2400 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2401 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2402 Then return the value FUNCTION returns.
2403 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2404 usage: (apply FUNCTION &rest ARGUMENTS) */)
2405 (ptrdiff_t nargs
, Lisp_Object
*args
)
2407 ptrdiff_t i
, numargs
;
2408 register Lisp_Object spread_arg
;
2409 register Lisp_Object
*funcall_args
;
2410 Lisp_Object fun
, retval
;
2411 struct gcpro gcpro1
;
2416 spread_arg
= args
[nargs
- 1];
2417 CHECK_LIST (spread_arg
);
2419 numargs
= XINT (Flength (spread_arg
));
2422 return Ffuncall (nargs
- 1, args
);
2423 else if (numargs
== 1)
2425 args
[nargs
- 1] = XCAR (spread_arg
);
2426 return Ffuncall (nargs
, args
);
2429 numargs
+= nargs
- 2;
2431 /* Optimize for no indirection. */
2432 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2433 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2434 fun
= indirect_function (fun
);
2435 if (EQ (fun
, Qunbound
))
2437 /* Let funcall get the error. */
2444 if (numargs
< XSUBR (fun
)->min_args
2445 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2446 goto funcall
; /* Let funcall get the error. */
2447 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2449 /* Avoid making funcall cons up a yet another new vector of arguments
2450 by explicitly supplying nil's for optional values. */
2451 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2452 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2453 funcall_args
[++i
] = Qnil
;
2454 GCPRO1 (*funcall_args
);
2455 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2459 /* We add 1 to numargs because funcall_args includes the
2460 function itself as well as its arguments. */
2463 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2464 GCPRO1 (*funcall_args
);
2465 gcpro1
.nvars
= 1 + numargs
;
2468 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2469 /* Spread the last arg we got. Its first element goes in
2470 the slot that it used to occupy, hence this value of I. */
2472 while (!NILP (spread_arg
))
2474 funcall_args
[i
++] = XCAR (spread_arg
);
2475 spread_arg
= XCDR (spread_arg
);
2478 /* By convention, the caller needs to gcpro Ffuncall's args. */
2479 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2486 /* Run hook variables in various ways. */
2489 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2491 Ffuncall (nargs
, args
);
2495 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2496 doc
: /* Run each hook in HOOKS.
2497 Each argument should be a symbol, a hook variable.
2498 These symbols are processed in the order specified.
2499 If a hook symbol has a non-nil value, that value may be a function
2500 or a list of functions to be called to run the hook.
2501 If the value is a function, it is called with no arguments.
2502 If it is a list, the elements are called, in order, with no arguments.
2504 Major modes should not use this function directly to run their mode
2505 hook; they should use `run-mode-hooks' instead.
2507 Do not use `make-local-variable' to make a hook variable buffer-local.
2508 Instead, use `add-hook' and specify t for the LOCAL argument.
2509 usage: (run-hooks &rest HOOKS) */)
2510 (ptrdiff_t nargs
, Lisp_Object
*args
)
2512 Lisp_Object hook
[1];
2515 for (i
= 0; i
< nargs
; i
++)
2518 run_hook_with_args (1, hook
, funcall_nil
);
2524 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2525 Srun_hook_with_args
, 1, MANY
, 0,
2526 doc
: /* Run HOOK with the specified arguments ARGS.
2527 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2528 value, that value may be a function or a list of functions to be
2529 called to run the hook. If the value is a function, it is called with
2530 the given arguments and its return value is returned. If it is a list
2531 of functions, those functions are called, in order,
2532 with the given arguments ARGS.
2533 It is best not to depend on the value returned by `run-hook-with-args',
2536 Do not use `make-local-variable' to make a hook variable buffer-local.
2537 Instead, use `add-hook' and specify t for the LOCAL argument.
2538 usage: (run-hook-with-args HOOK &rest ARGS) */)
2539 (ptrdiff_t nargs
, Lisp_Object
*args
)
2541 return run_hook_with_args (nargs
, args
, funcall_nil
);
2544 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2545 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2546 doc
: /* Run HOOK with the specified arguments ARGS.
2547 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2548 value, that value may be a function or a list of functions to be
2549 called to run the hook. If the value is a function, it is called with
2550 the given arguments and its return value is returned.
2551 If it is a list of functions, those functions are called, in order,
2552 with the given arguments ARGS, until one of them
2553 returns a non-nil value. Then we return that value.
2554 However, if they all return nil, we return nil.
2556 Do not use `make-local-variable' to make a hook variable buffer-local.
2557 Instead, use `add-hook' and specify t for the LOCAL argument.
2558 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2559 (ptrdiff_t nargs
, Lisp_Object
*args
)
2561 return run_hook_with_args (nargs
, args
, Ffuncall
);
2565 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2567 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2570 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2571 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2572 doc
: /* Run HOOK with the specified arguments ARGS.
2573 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2574 value, that value may be a function or a list of functions to be
2575 called to run the hook. If the value is a function, it is called with
2576 the given arguments and its return value is returned.
2577 If it is a list of functions, those functions are called, in order,
2578 with the given arguments ARGS, until one of them returns nil.
2579 Then we return nil. However, if they all return non-nil, we return non-nil.
2581 Do not use `make-local-variable' to make a hook variable buffer-local.
2582 Instead, use `add-hook' and specify t for the LOCAL argument.
2583 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2584 (ptrdiff_t nargs
, Lisp_Object
*args
)
2586 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2590 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2592 Lisp_Object tmp
= args
[0], ret
;
2595 ret
= Ffuncall (nargs
, args
);
2601 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2602 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2603 I.e. instead of calling each function FUN directly with arguments ARGS,
2604 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2605 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2606 aborts and returns that value.
2607 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2608 (ptrdiff_t nargs
, Lisp_Object
*args
)
2610 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2613 /* ARGS[0] should be a hook symbol.
2614 Call each of the functions in the hook value, passing each of them
2615 as arguments all the rest of ARGS (all NARGS - 1 elements).
2616 FUNCALL specifies how to call each function on the hook.
2617 The caller (or its caller, etc) must gcpro all of ARGS,
2618 except that it isn't necessary to gcpro ARGS[0]. */
2621 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2622 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2624 Lisp_Object sym
, val
, ret
= Qnil
;
2625 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2627 /* If we are dying or still initializing,
2628 don't do anything--it would probably crash if we tried. */
2629 if (NILP (Vrun_hooks
))
2633 val
= find_symbol_value (sym
);
2635 if (EQ (val
, Qunbound
) || NILP (val
))
2637 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2640 return funcall (nargs
, args
);
2644 Lisp_Object global_vals
= Qnil
;
2645 GCPRO3 (sym
, val
, global_vals
);
2648 CONSP (val
) && NILP (ret
);
2651 if (EQ (XCAR (val
), Qt
))
2653 /* t indicates this hook has a local binding;
2654 it means to run the global binding too. */
2655 global_vals
= Fdefault_value (sym
);
2656 if (NILP (global_vals
)) continue;
2658 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2660 args
[0] = global_vals
;
2661 ret
= funcall (nargs
, args
);
2666 CONSP (global_vals
) && NILP (ret
);
2667 global_vals
= XCDR (global_vals
))
2669 args
[0] = XCAR (global_vals
);
2670 /* In a global value, t should not occur. If it does, we
2671 must ignore it to avoid an endless loop. */
2672 if (!EQ (args
[0], Qt
))
2673 ret
= funcall (nargs
, args
);
2679 args
[0] = XCAR (val
);
2680 ret
= funcall (nargs
, args
);
2689 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2692 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2694 Lisp_Object temp
[3];
2699 Frun_hook_with_args (3, temp
);
2702 /* Apply fn to arg. */
2704 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2706 struct gcpro gcpro1
;
2710 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2713 Lisp_Object args
[2];
2717 RETURN_UNGCPRO (Fapply (2, args
));
2721 /* Call function fn on no arguments. */
2723 call0 (Lisp_Object fn
)
2725 struct gcpro gcpro1
;
2728 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2731 /* Call function fn with 1 argument arg1. */
2734 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2736 struct gcpro gcpro1
;
2737 Lisp_Object args
[2];
2743 RETURN_UNGCPRO (Ffuncall (2, args
));
2746 /* Call function fn with 2 arguments arg1, arg2. */
2749 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2751 struct gcpro gcpro1
;
2752 Lisp_Object args
[3];
2758 RETURN_UNGCPRO (Ffuncall (3, args
));
2761 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2764 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2766 struct gcpro gcpro1
;
2767 Lisp_Object args
[4];
2774 RETURN_UNGCPRO (Ffuncall (4, args
));
2777 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2780 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2783 struct gcpro gcpro1
;
2784 Lisp_Object args
[5];
2792 RETURN_UNGCPRO (Ffuncall (5, args
));
2795 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2798 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2799 Lisp_Object arg4
, Lisp_Object arg5
)
2801 struct gcpro gcpro1
;
2802 Lisp_Object args
[6];
2811 RETURN_UNGCPRO (Ffuncall (6, args
));
2814 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2817 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2818 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2820 struct gcpro gcpro1
;
2821 Lisp_Object args
[7];
2831 RETURN_UNGCPRO (Ffuncall (7, args
));
2834 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2837 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2838 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2840 struct gcpro gcpro1
;
2841 Lisp_Object args
[8];
2852 RETURN_UNGCPRO (Ffuncall (8, args
));
2855 /* The caller should GCPRO all the elements of ARGS. */
2857 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2858 doc
: /* Non-nil if OBJECT is a function. */)
2859 (Lisp_Object object
)
2861 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2863 object
= Findirect_function (object
, Qt
);
2865 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2867 /* Autoloaded symbols are functions, except if they load
2868 macros or keymaps. */
2870 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2871 object
= XCDR (object
);
2873 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2878 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2879 else if (COMPILEDP (object
))
2881 else if (CONSP (object
))
2883 Lisp_Object car
= XCAR (object
);
2884 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2890 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2891 doc
: /* Call first argument as a function, passing remaining arguments to it.
2892 Return the value that function returns.
2893 Thus, (funcall 'cons 'x 'y) returns (x . y).
2894 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2895 (ptrdiff_t nargs
, Lisp_Object
*args
)
2897 Lisp_Object fun
, original_fun
;
2899 ptrdiff_t numargs
= nargs
- 1;
2900 Lisp_Object lisp_numargs
;
2902 struct backtrace backtrace
;
2903 register Lisp_Object
*internal_args
;
2907 if ((consing_since_gc
> gc_cons_threshold
2908 && consing_since_gc
> gc_relative_threshold
)
2910 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2911 Fgarbage_collect ();
2913 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2915 if (max_lisp_eval_depth
< 100)
2916 max_lisp_eval_depth
= 100;
2917 if (lisp_eval_depth
> max_lisp_eval_depth
)
2918 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2921 backtrace
.next
= backtrace_list
;
2922 backtrace_list
= &backtrace
;
2923 backtrace
.function
= &args
[0];
2924 backtrace
.args
= &args
[1];
2925 backtrace
.nargs
= nargs
- 1;
2926 backtrace
.debug_on_exit
= 0;
2928 if (debug_on_next_call
)
2929 do_debug_on_call (Qlambda
);
2933 original_fun
= args
[0];
2937 /* Optimize for no indirection. */
2939 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2940 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2941 fun
= indirect_function (fun
);
2945 if (numargs
< XSUBR (fun
)->min_args
2946 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2948 XSETFASTINT (lisp_numargs
, numargs
);
2949 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2952 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2953 xsignal1 (Qinvalid_function
, original_fun
);
2955 else if (XSUBR (fun
)->max_args
== MANY
)
2956 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2959 if (XSUBR (fun
)->max_args
> numargs
)
2961 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2962 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2963 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2964 internal_args
[i
] = Qnil
;
2967 internal_args
= args
+ 1;
2968 switch (XSUBR (fun
)->max_args
)
2971 val
= (XSUBR (fun
)->function
.a0 ());
2974 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2977 val
= (XSUBR (fun
)->function
.a2
2978 (internal_args
[0], internal_args
[1]));
2981 val
= (XSUBR (fun
)->function
.a3
2982 (internal_args
[0], internal_args
[1], internal_args
[2]));
2985 val
= (XSUBR (fun
)->function
.a4
2986 (internal_args
[0], internal_args
[1], internal_args
[2],
2990 val
= (XSUBR (fun
)->function
.a5
2991 (internal_args
[0], internal_args
[1], internal_args
[2],
2992 internal_args
[3], internal_args
[4]));
2995 val
= (XSUBR (fun
)->function
.a6
2996 (internal_args
[0], internal_args
[1], internal_args
[2],
2997 internal_args
[3], internal_args
[4], internal_args
[5]));
3000 val
= (XSUBR (fun
)->function
.a7
3001 (internal_args
[0], internal_args
[1], internal_args
[2],
3002 internal_args
[3], internal_args
[4], internal_args
[5],
3007 val
= (XSUBR (fun
)->function
.a8
3008 (internal_args
[0], internal_args
[1], internal_args
[2],
3009 internal_args
[3], internal_args
[4], internal_args
[5],
3010 internal_args
[6], internal_args
[7]));
3015 /* If a subr takes more than 8 arguments without using MANY
3016 or UNEVALLED, we need to extend this function to support it.
3017 Until this is done, there is no way to call the function. */
3022 else if (COMPILEDP (fun
))
3023 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3026 if (EQ (fun
, Qunbound
))
3027 xsignal1 (Qvoid_function
, original_fun
);
3029 xsignal1 (Qinvalid_function
, original_fun
);
3030 funcar
= XCAR (fun
);
3031 if (!SYMBOLP (funcar
))
3032 xsignal1 (Qinvalid_function
, original_fun
);
3033 if (EQ (funcar
, Qlambda
)
3034 || EQ (funcar
, Qclosure
))
3035 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3036 else if (EQ (funcar
, Qautoload
))
3038 do_autoload (fun
, original_fun
);
3043 xsignal1 (Qinvalid_function
, original_fun
);
3047 if (backtrace
.debug_on_exit
)
3048 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3049 backtrace_list
= backtrace
.next
;
3054 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
3056 Lisp_Object args_left
;
3057 ptrdiff_t i
, numargs
;
3058 register Lisp_Object
*arg_vector
;
3059 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3060 register Lisp_Object tem
;
3063 numargs
= XFASTINT (Flength (args
));
3064 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
3067 GCPRO3 (*arg_vector
, args_left
, fun
);
3070 for (i
= 0; i
< numargs
; )
3072 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3073 tem
= eval_sub (tem
);
3074 arg_vector
[i
++] = tem
;
3080 backtrace_list
->args
= arg_vector
;
3081 backtrace_list
->nargs
= i
;
3082 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
3084 /* Do the debug-on-exit now, while arg_vector still exists. */
3085 if (backtrace_list
->debug_on_exit
)
3086 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3087 /* Don't do it again when we return to eval. */
3088 backtrace_list
->debug_on_exit
= 0;
3093 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3094 and return the result of evaluation.
3095 FUN must be either a lambda-expression or a compiled-code object. */
3098 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
3099 register Lisp_Object
*arg_vector
)
3101 Lisp_Object val
, syms_left
, next
, lexenv
;
3102 int count
= SPECPDL_INDEX ();
3108 if (EQ (XCAR (fun
), Qclosure
))
3110 fun
= XCDR (fun
); /* Drop `closure'. */
3111 lexenv
= XCAR (fun
);
3112 CHECK_LIST_CONS (fun
, fun
);
3116 syms_left
= XCDR (fun
);
3117 if (CONSP (syms_left
))
3118 syms_left
= XCAR (syms_left
);
3120 xsignal1 (Qinvalid_function
, fun
);
3122 else if (COMPILEDP (fun
))
3124 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3125 if (INTEGERP (syms_left
))
3126 /* A byte-code object with a non-nil `push args' slot means we
3127 shouldn't bind any arguments, instead just call the byte-code
3128 interpreter directly; it will push arguments as necessary.
3130 Byte-code objects with either a non-existent, or a nil value for
3131 the `push args' slot (the default), have dynamically-bound
3132 arguments, and use the argument-binding code below instead (as do
3133 all interpreted functions, even lexically bound ones). */
3135 /* If we have not actually read the bytecode string
3136 and constants vector yet, fetch them from the file. */
3137 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3138 Ffetch_bytecode (fun
);
3139 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3140 AREF (fun
, COMPILED_CONSTANTS
),
3141 AREF (fun
, COMPILED_STACK_DEPTH
),
3150 i
= optional
= rest
= 0;
3151 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3155 next
= XCAR (syms_left
);
3156 if (!SYMBOLP (next
))
3157 xsignal1 (Qinvalid_function
, fun
);
3159 if (EQ (next
, Qand_rest
))
3161 else if (EQ (next
, Qand_optional
))
3168 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3172 arg
= arg_vector
[i
++];
3174 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3178 /* Bind the argument. */
3179 if (!NILP (lexenv
) && SYMBOLP (next
))
3180 /* Lexically bind NEXT by adding it to the lexenv alist. */
3181 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3183 /* Dynamically bind NEXT. */
3184 specbind (next
, arg
);
3188 if (!NILP (syms_left
))
3189 xsignal1 (Qinvalid_function
, fun
);
3191 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3193 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3194 /* Instantiate a new lexical environment. */
3195 specbind (Qinternal_interpreter_environment
, lexenv
);
3198 val
= Fprogn (XCDR (XCDR (fun
)));
3201 /* If we have not actually read the bytecode string
3202 and constants vector yet, fetch them from the file. */
3203 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3204 Ffetch_bytecode (fun
);
3205 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3206 AREF (fun
, COMPILED_CONSTANTS
),
3207 AREF (fun
, COMPILED_STACK_DEPTH
),
3211 return unbind_to (count
, val
);
3214 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3216 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3217 (Lisp_Object object
)
3221 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3223 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3226 tem
= AREF (object
, COMPILED_BYTECODE
);
3227 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3228 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3230 error ("Invalid byte code");
3232 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3233 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3241 register int count
= SPECPDL_INDEX ();
3243 min (max_specpdl_size
,
3244 min (max (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (struct specbinding
),
3247 if (max_size
<= specpdl_size
)
3249 if (max_specpdl_size
< 400)
3250 max_size
= max_specpdl_size
= 400;
3251 if (max_size
<= specpdl_size
)
3252 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3254 size
= specpdl_size
< max_size
/ 2 ? 2 * specpdl_size
: max_size
;
3255 specpdl
= xnrealloc (specpdl
, size
, sizeof *specpdl
);
3256 specpdl_size
= size
;
3257 specpdl_ptr
= specpdl
+ count
;
3260 /* `specpdl_ptr->symbol' is a field which describes which variable is
3261 let-bound, so it can be properly undone when we unbind_to.
3262 It can have the following two shapes:
3263 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3264 a symbol that is not buffer-local (at least at the time
3265 the let binding started). Note also that it should not be
3266 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3268 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3269 variable SYMBOL which can be buffer-local. WHERE tells us
3270 which buffer is affected (or nil if the let-binding affects the
3271 global value of the variable) and BUFFER tells us which buffer was
3272 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3273 BUFFER did not yet have a buffer-local value). */
3276 specbind (Lisp_Object symbol
, Lisp_Object value
)
3278 struct Lisp_Symbol
*sym
;
3280 eassert (!handling_signal
);
3282 CHECK_SYMBOL (symbol
);
3283 sym
= XSYMBOL (symbol
);
3284 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3288 switch (sym
->redirect
)
3290 case SYMBOL_VARALIAS
:
3291 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3292 case SYMBOL_PLAINVAL
:
3293 /* The most common case is that of a non-constant symbol with a
3294 trivial value. Make that as fast as we can. */
3295 specpdl_ptr
->symbol
= symbol
;
3296 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3297 specpdl_ptr
->func
= NULL
;
3300 SET_SYMBOL_VAL (sym
, value
);
3302 set_internal (symbol
, value
, Qnil
, 1);
3304 case SYMBOL_LOCALIZED
:
3305 if (SYMBOL_BLV (sym
)->frame_local
)
3306 error ("Frame-local vars cannot be let-bound");
3307 case SYMBOL_FORWARDED
:
3309 Lisp_Object ovalue
= find_symbol_value (symbol
);
3310 specpdl_ptr
->func
= 0;
3311 specpdl_ptr
->old_value
= ovalue
;
3313 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3314 || (EQ (SYMBOL_BLV (sym
)->where
,
3315 SYMBOL_BLV (sym
)->frame_local
?
3316 Fselected_frame () : Fcurrent_buffer ())));
3318 if (sym
->redirect
== SYMBOL_LOCALIZED
3319 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3321 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3323 /* For a local variable, record both the symbol and which
3324 buffer's or frame's value we are saving. */
3325 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3327 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3328 || (BLV_FOUND (SYMBOL_BLV (sym
))
3329 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3332 else if (sym
->redirect
== SYMBOL_LOCALIZED
3333 && BLV_FOUND (SYMBOL_BLV (sym
)))
3334 where
= SYMBOL_BLV (sym
)->where
;
3338 /* We're not using the `unused' slot in the specbinding
3339 structure because this would mean we have to do more
3340 work for simple variables. */
3341 /* FIXME: The third value `current_buffer' is only used in
3342 let_shadows_buffer_binding_p which is itself only used
3343 in set_internal for local_if_set. */
3344 eassert (NILP (where
) || EQ (where
, cur_buf
));
3345 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3347 /* If SYMBOL is a per-buffer variable which doesn't have a
3348 buffer-local value here, make the `let' change the global
3349 value by changing the value of SYMBOL in all buffers not
3350 having their own value. This is consistent with what
3351 happens with other buffer-local variables. */
3353 && sym
->redirect
== SYMBOL_FORWARDED
)
3355 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3357 Fset_default (symbol
, value
);
3362 specpdl_ptr
->symbol
= symbol
;
3365 set_internal (symbol
, value
, Qnil
, 1);
3373 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3375 eassert (!handling_signal
);
3377 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3379 specpdl_ptr
->func
= function
;
3380 specpdl_ptr
->symbol
= Qnil
;
3381 specpdl_ptr
->old_value
= arg
;
3386 unbind_to (int count
, Lisp_Object value
)
3388 Lisp_Object quitf
= Vquit_flag
;
3389 struct gcpro gcpro1
, gcpro2
;
3391 GCPRO2 (value
, quitf
);
3394 while (specpdl_ptr
!= specpdl
+ count
)
3396 /* Copy the binding, and decrement specpdl_ptr, before we do
3397 the work to unbind it. We decrement first
3398 so that an error in unbinding won't try to unbind
3399 the same entry again, and we copy the binding first
3400 in case more bindings are made during some of the code we run. */
3402 struct specbinding this_binding
;
3403 this_binding
= *--specpdl_ptr
;
3405 if (this_binding
.func
!= 0)
3406 (*this_binding
.func
) (this_binding
.old_value
);
3407 /* If the symbol is a list, it is really (SYMBOL WHERE
3408 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3409 frame. If WHERE is a buffer or frame, this indicates we
3410 bound a variable that had a buffer-local or frame-local
3411 binding. WHERE nil means that the variable had the default
3412 value when it was bound. CURRENT-BUFFER is the buffer that
3413 was current when the variable was bound. */
3414 else if (CONSP (this_binding
.symbol
))
3416 Lisp_Object symbol
, where
;
3418 symbol
= XCAR (this_binding
.symbol
);
3419 where
= XCAR (XCDR (this_binding
.symbol
));
3422 Fset_default (symbol
, this_binding
.old_value
);
3423 /* If `where' is non-nil, reset the value in the appropriate
3424 local binding, but only if that binding still exists. */
3425 else if (BUFFERP (where
)
3426 ? !NILP (Flocal_variable_p (symbol
, where
))
3427 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3428 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3430 /* If variable has a trivial value (no forwarding), we can
3431 just set it. No need to check for constant symbols here,
3432 since that was already done by specbind. */
3433 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3434 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3435 this_binding
.old_value
);
3437 /* NOTE: we only ever come here if make_local_foo was used for
3438 the first time on this var within this let. */
3439 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3442 if (NILP (Vquit_flag
) && !NILP (quitf
))
3449 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3450 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3451 A special variable is one that will be bound dynamically, even in a
3452 context where binding is lexical by default. */)
3453 (Lisp_Object symbol
)
3455 CHECK_SYMBOL (symbol
);
3456 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3460 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3461 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3462 The debugger is entered when that frame exits, if the flag is non-nil. */)
3463 (Lisp_Object level
, Lisp_Object flag
)
3465 register struct backtrace
*backlist
= backtrace_list
;
3468 CHECK_NUMBER (level
);
3470 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3472 backlist
= backlist
->next
;
3476 backlist
->debug_on_exit
= !NILP (flag
);
3481 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3482 doc
: /* Print a trace of Lisp function calls currently active.
3483 Output stream used is value of `standard-output'. */)
3486 register struct backtrace
*backlist
= backtrace_list
;
3489 struct gcpro gcpro1
;
3490 Lisp_Object old_print_level
= Vprint_level
;
3492 if (NILP (Vprint_level
))
3493 XSETFASTINT (Vprint_level
, 8);
3500 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3501 if (backlist
->nargs
== UNEVALLED
)
3503 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3504 write_string ("\n", -1);
3508 tem
= *backlist
->function
;
3509 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3510 write_string ("(", -1);
3511 if (backlist
->nargs
== MANY
)
3512 { /* FIXME: Can this happen? */
3514 for (tail
= *backlist
->args
, i
= 0;
3516 tail
= Fcdr (tail
), i
= 1)
3518 if (i
) write_string (" ", -1);
3519 Fprin1 (Fcar (tail
), Qnil
);
3525 for (i
= 0; i
< backlist
->nargs
; i
++)
3527 if (i
) write_string (" ", -1);
3528 Fprin1 (backlist
->args
[i
], Qnil
);
3531 write_string (")\n", -1);
3533 backlist
= backlist
->next
;
3536 Vprint_level
= old_print_level
;
3541 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3542 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3543 If that frame has not evaluated the arguments yet (or is a special form),
3544 the value is (nil FUNCTION ARG-FORMS...).
3545 If that frame has evaluated its arguments and called its function already,
3546 the value is (t FUNCTION ARG-VALUES...).
3547 A &rest arg is represented as the tail of the list ARG-VALUES.
3548 FUNCTION is whatever was supplied as car of evaluated list,
3549 or a lambda expression for macro calls.
3550 If NFRAMES is more than the number of frames, the value is nil. */)
3551 (Lisp_Object nframes
)
3553 register struct backtrace
*backlist
= backtrace_list
;
3554 register EMACS_INT i
;
3557 CHECK_NATNUM (nframes
);
3559 /* Find the frame requested. */
3560 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3561 backlist
= backlist
->next
;
3565 if (backlist
->nargs
== UNEVALLED
)
3566 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3569 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3570 tem
= *backlist
->args
;
3572 tem
= Flist (backlist
->nargs
, backlist
->args
);
3574 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3581 mark_backtrace (void)
3583 register struct backtrace
*backlist
;
3586 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3588 mark_object (*backlist
->function
);
3590 if (backlist
->nargs
== UNEVALLED
3591 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3594 i
= backlist
->nargs
;
3596 mark_object (backlist
->args
[i
]);
3604 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3605 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3606 If Lisp code tries to increase the total number past this amount,
3607 an error is signaled.
3608 You can safely use a value considerably larger than the default value,
3609 if that proves inconveniently small. However, if you increase it too far,
3610 Emacs could run out of memory trying to make the stack bigger. */);
3612 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3613 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3615 This limit serves to catch infinite recursions for you before they cause
3616 actual stack overflow in C, which would be fatal for Emacs.
3617 You can safely make it considerably larger than its default value,
3618 if that proves inconveniently small. However, if you increase it too far,
3619 Emacs could overflow the real C stack, and crash. */);
3621 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3622 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3623 If the value is t, that means do an ordinary quit.
3624 If the value equals `throw-on-input', that means quit by throwing
3625 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3626 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3627 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3630 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3631 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3632 Note that `quit-flag' will still be set by typing C-g,
3633 so a quit will be signaled as soon as `inhibit-quit' is nil.
3634 To prevent this happening, set `quit-flag' to nil
3635 before making `inhibit-quit' nil. */);
3636 Vinhibit_quit
= Qnil
;
3638 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3639 DEFSYM (Qautoload
, "autoload");
3640 DEFSYM (Qdebug_on_error
, "debug-on-error");
3641 DEFSYM (Qmacro
, "macro");
3642 DEFSYM (Qdeclare
, "declare");
3644 /* Note that the process handling also uses Qexit, but we don't want
3645 to staticpro it twice, so we just do it here. */
3646 DEFSYM (Qexit
, "exit");
3648 DEFSYM (Qinteractive
, "interactive");
3649 DEFSYM (Qcommandp
, "commandp");
3650 DEFSYM (Qdefun
, "defun");
3651 DEFSYM (Qand_rest
, "&rest");
3652 DEFSYM (Qand_optional
, "&optional");
3653 DEFSYM (Qclosure
, "closure");
3654 DEFSYM (Qdebug
, "debug");
3656 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3657 doc
: /* *Non-nil means enter debugger if an error is signaled.
3658 Does not apply to errors handled by `condition-case' or those
3659 matched by `debug-ignored-errors'.
3660 If the value is a list, an error only means to enter the debugger
3661 if one of its condition symbols appears in the list.
3662 When you evaluate an expression interactively, this variable
3663 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3664 The command `toggle-debug-on-error' toggles this.
3665 See also the variable `debug-on-quit'. */);
3666 Vdebug_on_error
= Qnil
;
3668 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3669 doc
: /* *List of errors for which the debugger should not be called.
3670 Each element may be a condition-name or a regexp that matches error messages.
3671 If any element applies to a given error, that error skips the debugger
3672 and just returns to top level.
3673 This overrides the variable `debug-on-error'.
3674 It does not apply to errors handled by `condition-case'. */);
3675 Vdebug_ignored_errors
= Qnil
;
3677 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3678 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3679 Does not apply if quit is handled by a `condition-case'. */);
3682 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3683 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3685 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3686 doc
: /* Non-nil means debugger may continue execution.
3687 This is nil when the debugger is called under circumstances where it
3688 might not be safe to continue. */);
3689 debugger_may_continue
= 1;
3691 DEFVAR_LISP ("debugger", Vdebugger
,
3692 doc
: /* Function to call to invoke debugger.
3693 If due to frame exit, args are `exit' and the value being returned;
3694 this function's value will be returned instead of that.
3695 If due to error, args are `error' and a list of the args to `signal'.
3696 If due to `apply' or `funcall' entry, one arg, `lambda'.
3697 If due to `eval' entry, one arg, t. */);
3700 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3701 doc
: /* If non-nil, this is a function for `signal' to call.
3702 It receives the same arguments that `signal' was given.
3703 The Edebug package uses this to regain control. */);
3704 Vsignal_hook_function
= Qnil
;
3706 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3707 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3708 Note that `debug-on-error', `debug-on-quit' and friends
3709 still determine whether to handle the particular condition. */);
3710 Vdebug_on_signal
= Qnil
;
3712 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3713 doc
: /* Function to process declarations in a macro definition.
3714 The function will be called with two args MACRO and DECL.
3715 MACRO is the name of the macro being defined.
3716 DECL is a list `(declare ...)' containing the declarations.
3717 The value the function returns is not used. */);
3718 Vmacro_declaration_function
= Qnil
;
3720 /* When lexical binding is being used,
3721 vinternal_interpreter_environment is non-nil, and contains an alist
3722 of lexically-bound variable, or (t), indicating an empty
3723 environment. The lisp name of this variable would be
3724 `internal-interpreter-environment' if it weren't hidden.
3725 Every element of this list can be either a cons (VAR . VAL)
3726 specifying a lexical binding, or a single symbol VAR indicating
3727 that this variable should use dynamic scoping. */
3728 DEFSYM (Qinternal_interpreter_environment
, "internal-interpreter-environment");
3729 DEFVAR_LISP ("internal-interpreter-environment",
3730 Vinternal_interpreter_environment
,
3731 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3732 When lexical binding is not being used, this variable is nil.
3733 A value of `(t)' indicates an empty environment, otherwise it is an
3734 alist of active lexical bindings. */);
3735 Vinternal_interpreter_environment
= Qnil
;
3736 /* Don't export this variable to Elisp, so noone can mess with it
3737 (Just imagine if someone makes it buffer-local). */
3738 Funintern (Qinternal_interpreter_environment
, Qnil
);
3740 DEFSYM (Vrun_hooks
, "run-hooks");
3742 staticpro (&Vautoload_queue
);
3743 Vautoload_queue
= Qnil
;
3744 staticpro (&Vsignaling_function
);
3745 Vsignaling_function
= Qnil
;
3756 defsubr (&Sfunction
);
3758 defsubr (&Sdefmacro
);
3760 defsubr (&Sdefvaralias
);
3761 defsubr (&Sdefconst
);
3762 defsubr (&Suser_variable_p
);
3766 defsubr (&Smacroexpand
);
3769 defsubr (&Sunwind_protect
);
3770 defsubr (&Scondition_case
);
3772 defsubr (&Sinteractive_p
);
3773 defsubr (&Scalled_interactively_p
);
3774 defsubr (&Scommandp
);
3775 defsubr (&Sautoload
);
3778 defsubr (&Sfuncall
);
3779 defsubr (&Srun_hooks
);
3780 defsubr (&Srun_hook_with_args
);
3781 defsubr (&Srun_hook_with_args_until_success
);
3782 defsubr (&Srun_hook_with_args_until_failure
);
3783 defsubr (&Srun_hook_wrapped
);
3784 defsubr (&Sfetch_bytecode
);
3785 defsubr (&Sbacktrace_debug
);
3786 defsubr (&Sbacktrace
);
3787 defsubr (&Sbacktrace_frame
);
3788 defsubr (&Sspecial_variable_p
);
3789 defsubr (&Sfunctionp
);