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 ptrdiff_t 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 EMACS_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 ptrdiff_t 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
; p
> specpdl
; p
)
762 if ((--p
)->func
== NULL
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 ptrdiff_t 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 ptrdiff_t 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 ptrdiff_t 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 ptrdiff_t 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 ptrdiff_t 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
)
2409 register Lisp_Object spread_arg
;
2410 register Lisp_Object
*funcall_args
;
2411 Lisp_Object fun
, retval
;
2412 struct gcpro gcpro1
;
2417 spread_arg
= args
[nargs
- 1];
2418 CHECK_LIST (spread_arg
);
2420 numargs
= XINT (Flength (spread_arg
));
2423 return Ffuncall (nargs
- 1, args
);
2424 else if (numargs
== 1)
2426 args
[nargs
- 1] = XCAR (spread_arg
);
2427 return Ffuncall (nargs
, args
);
2430 numargs
+= nargs
- 2;
2432 /* Optimize for no indirection. */
2433 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2434 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2435 fun
= indirect_function (fun
);
2436 if (EQ (fun
, Qunbound
))
2438 /* Let funcall get the error. */
2445 if (numargs
< XSUBR (fun
)->min_args
2446 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2447 goto funcall
; /* Let funcall get the error. */
2448 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2450 /* Avoid making funcall cons up a yet another new vector of arguments
2451 by explicitly supplying nil's for optional values. */
2452 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2453 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2454 funcall_args
[++i
] = Qnil
;
2455 GCPRO1 (*funcall_args
);
2456 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2460 /* We add 1 to numargs because funcall_args includes the
2461 function itself as well as its arguments. */
2464 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2465 GCPRO1 (*funcall_args
);
2466 gcpro1
.nvars
= 1 + numargs
;
2469 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2470 /* Spread the last arg we got. Its first element goes in
2471 the slot that it used to occupy, hence this value of I. */
2473 while (!NILP (spread_arg
))
2475 funcall_args
[i
++] = XCAR (spread_arg
);
2476 spread_arg
= XCDR (spread_arg
);
2479 /* By convention, the caller needs to gcpro Ffuncall's args. */
2480 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2487 /* Run hook variables in various ways. */
2490 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2492 Ffuncall (nargs
, args
);
2496 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2497 doc
: /* Run each hook in HOOKS.
2498 Each argument should be a symbol, a hook variable.
2499 These symbols are processed in the order specified.
2500 If a hook symbol has a non-nil value, that value may be a function
2501 or a list of functions to be called to run the hook.
2502 If the value is a function, it is called with no arguments.
2503 If it is a list, the elements are called, in order, with no arguments.
2505 Major modes should not use this function directly to run their mode
2506 hook; they should use `run-mode-hooks' instead.
2508 Do not use `make-local-variable' to make a hook variable buffer-local.
2509 Instead, use `add-hook' and specify t for the LOCAL argument.
2510 usage: (run-hooks &rest HOOKS) */)
2511 (ptrdiff_t nargs
, Lisp_Object
*args
)
2513 Lisp_Object hook
[1];
2516 for (i
= 0; i
< nargs
; i
++)
2519 run_hook_with_args (1, hook
, funcall_nil
);
2525 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2526 Srun_hook_with_args
, 1, MANY
, 0,
2527 doc
: /* Run HOOK with the specified arguments ARGS.
2528 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2529 value, that value may be a function or a list of functions to be
2530 called to run the hook. If the value is a function, it is called with
2531 the given arguments and its return value is returned. If it is a list
2532 of functions, those functions are called, in order,
2533 with the given arguments ARGS.
2534 It is best not to depend on the value returned by `run-hook-with-args',
2537 Do not use `make-local-variable' to make a hook variable buffer-local.
2538 Instead, use `add-hook' and specify t for the LOCAL argument.
2539 usage: (run-hook-with-args HOOK &rest ARGS) */)
2540 (ptrdiff_t nargs
, Lisp_Object
*args
)
2542 return run_hook_with_args (nargs
, args
, funcall_nil
);
2545 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2546 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2547 doc
: /* Run HOOK with the specified arguments ARGS.
2548 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2549 value, that value may be a function or a list of functions to be
2550 called to run the hook. If the value is a function, it is called with
2551 the given arguments and its return value is returned.
2552 If it is a list of functions, those functions are called, in order,
2553 with the given arguments ARGS, until one of them
2554 returns a non-nil value. Then we return that value.
2555 However, if they all return nil, we return nil.
2557 Do not use `make-local-variable' to make a hook variable buffer-local.
2558 Instead, use `add-hook' and specify t for the LOCAL argument.
2559 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2560 (ptrdiff_t nargs
, Lisp_Object
*args
)
2562 return run_hook_with_args (nargs
, args
, Ffuncall
);
2566 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2568 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2571 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2572 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2573 doc
: /* Run HOOK with the specified arguments ARGS.
2574 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2575 value, that value may be a function or a list of functions to be
2576 called to run the hook. If the value is a function, it is called with
2577 the given arguments and its return value is returned.
2578 If it is a list of functions, those functions are called, in order,
2579 with the given arguments ARGS, until one of them returns nil.
2580 Then we return nil. However, if they all return non-nil, we return non-nil.
2582 Do not use `make-local-variable' to make a hook variable buffer-local.
2583 Instead, use `add-hook' and specify t for the LOCAL argument.
2584 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2585 (ptrdiff_t nargs
, Lisp_Object
*args
)
2587 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2591 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2593 Lisp_Object tmp
= args
[0], ret
;
2596 ret
= Ffuncall (nargs
, args
);
2602 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2603 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2604 I.e. instead of calling each function FUN directly with arguments ARGS,
2605 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2606 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2607 aborts and returns that value.
2608 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2609 (ptrdiff_t nargs
, Lisp_Object
*args
)
2611 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2614 /* ARGS[0] should be a hook symbol.
2615 Call each of the functions in the hook value, passing each of them
2616 as arguments all the rest of ARGS (all NARGS - 1 elements).
2617 FUNCALL specifies how to call each function on the hook.
2618 The caller (or its caller, etc) must gcpro all of ARGS,
2619 except that it isn't necessary to gcpro ARGS[0]. */
2622 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2623 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2625 Lisp_Object sym
, val
, ret
= Qnil
;
2626 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2628 /* If we are dying or still initializing,
2629 don't do anything--it would probably crash if we tried. */
2630 if (NILP (Vrun_hooks
))
2634 val
= find_symbol_value (sym
);
2636 if (EQ (val
, Qunbound
) || NILP (val
))
2638 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2641 return funcall (nargs
, args
);
2645 Lisp_Object global_vals
= Qnil
;
2646 GCPRO3 (sym
, val
, global_vals
);
2649 CONSP (val
) && NILP (ret
);
2652 if (EQ (XCAR (val
), Qt
))
2654 /* t indicates this hook has a local binding;
2655 it means to run the global binding too. */
2656 global_vals
= Fdefault_value (sym
);
2657 if (NILP (global_vals
)) continue;
2659 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2661 args
[0] = global_vals
;
2662 ret
= funcall (nargs
, args
);
2667 CONSP (global_vals
) && NILP (ret
);
2668 global_vals
= XCDR (global_vals
))
2670 args
[0] = XCAR (global_vals
);
2671 /* In a global value, t should not occur. If it does, we
2672 must ignore it to avoid an endless loop. */
2673 if (!EQ (args
[0], Qt
))
2674 ret
= funcall (nargs
, args
);
2680 args
[0] = XCAR (val
);
2681 ret
= funcall (nargs
, args
);
2690 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2693 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2695 Lisp_Object temp
[3];
2700 Frun_hook_with_args (3, temp
);
2703 /* Apply fn to arg. */
2705 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2707 struct gcpro gcpro1
;
2711 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2714 Lisp_Object args
[2];
2718 RETURN_UNGCPRO (Fapply (2, args
));
2722 /* Call function fn on no arguments. */
2724 call0 (Lisp_Object fn
)
2726 struct gcpro gcpro1
;
2729 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2732 /* Call function fn with 1 argument arg1. */
2735 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2737 struct gcpro gcpro1
;
2738 Lisp_Object args
[2];
2744 RETURN_UNGCPRO (Ffuncall (2, args
));
2747 /* Call function fn with 2 arguments arg1, arg2. */
2750 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2752 struct gcpro gcpro1
;
2753 Lisp_Object args
[3];
2759 RETURN_UNGCPRO (Ffuncall (3, args
));
2762 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2765 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2767 struct gcpro gcpro1
;
2768 Lisp_Object args
[4];
2775 RETURN_UNGCPRO (Ffuncall (4, args
));
2778 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2781 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2784 struct gcpro gcpro1
;
2785 Lisp_Object args
[5];
2793 RETURN_UNGCPRO (Ffuncall (5, args
));
2796 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2799 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2800 Lisp_Object arg4
, Lisp_Object arg5
)
2802 struct gcpro gcpro1
;
2803 Lisp_Object args
[6];
2812 RETURN_UNGCPRO (Ffuncall (6, args
));
2815 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2818 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2819 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2821 struct gcpro gcpro1
;
2822 Lisp_Object args
[7];
2832 RETURN_UNGCPRO (Ffuncall (7, args
));
2835 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2838 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2839 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2841 struct gcpro gcpro1
;
2842 Lisp_Object args
[8];
2853 RETURN_UNGCPRO (Ffuncall (8, args
));
2856 /* The caller should GCPRO all the elements of ARGS. */
2858 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2859 doc
: /* Non-nil if OBJECT is a function. */)
2860 (Lisp_Object object
)
2862 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2864 object
= Findirect_function (object
, Qt
);
2866 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2868 /* Autoloaded symbols are functions, except if they load
2869 macros or keymaps. */
2871 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2872 object
= XCDR (object
);
2874 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2879 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2880 else if (COMPILEDP (object
))
2882 else if (CONSP (object
))
2884 Lisp_Object car
= XCAR (object
);
2885 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2891 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2892 doc
: /* Call first argument as a function, passing remaining arguments to it.
2893 Return the value that function returns.
2894 Thus, (funcall 'cons 'x 'y) returns (x . y).
2895 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2896 (ptrdiff_t nargs
, Lisp_Object
*args
)
2898 Lisp_Object fun
, original_fun
;
2900 ptrdiff_t numargs
= nargs
- 1;
2901 Lisp_Object lisp_numargs
;
2903 struct backtrace backtrace
;
2904 register Lisp_Object
*internal_args
;
2908 if ((consing_since_gc
> gc_cons_threshold
2909 && consing_since_gc
> gc_relative_threshold
)
2911 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2912 Fgarbage_collect ();
2914 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2916 if (max_lisp_eval_depth
< 100)
2917 max_lisp_eval_depth
= 100;
2918 if (lisp_eval_depth
> max_lisp_eval_depth
)
2919 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2922 backtrace
.next
= backtrace_list
;
2923 backtrace_list
= &backtrace
;
2924 backtrace
.function
= &args
[0];
2925 backtrace
.args
= &args
[1];
2926 backtrace
.nargs
= nargs
- 1;
2927 backtrace
.debug_on_exit
= 0;
2929 if (debug_on_next_call
)
2930 do_debug_on_call (Qlambda
);
2934 original_fun
= args
[0];
2938 /* Optimize for no indirection. */
2940 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2941 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2942 fun
= indirect_function (fun
);
2946 if (numargs
< XSUBR (fun
)->min_args
2947 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2949 XSETFASTINT (lisp_numargs
, numargs
);
2950 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2953 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2954 xsignal1 (Qinvalid_function
, original_fun
);
2956 else if (XSUBR (fun
)->max_args
== MANY
)
2957 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2960 if (XSUBR (fun
)->max_args
> numargs
)
2962 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2963 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2964 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2965 internal_args
[i
] = Qnil
;
2968 internal_args
= args
+ 1;
2969 switch (XSUBR (fun
)->max_args
)
2972 val
= (XSUBR (fun
)->function
.a0 ());
2975 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2978 val
= (XSUBR (fun
)->function
.a2
2979 (internal_args
[0], internal_args
[1]));
2982 val
= (XSUBR (fun
)->function
.a3
2983 (internal_args
[0], internal_args
[1], internal_args
[2]));
2986 val
= (XSUBR (fun
)->function
.a4
2987 (internal_args
[0], internal_args
[1], internal_args
[2],
2991 val
= (XSUBR (fun
)->function
.a5
2992 (internal_args
[0], internal_args
[1], internal_args
[2],
2993 internal_args
[3], internal_args
[4]));
2996 val
= (XSUBR (fun
)->function
.a6
2997 (internal_args
[0], internal_args
[1], internal_args
[2],
2998 internal_args
[3], internal_args
[4], internal_args
[5]));
3001 val
= (XSUBR (fun
)->function
.a7
3002 (internal_args
[0], internal_args
[1], internal_args
[2],
3003 internal_args
[3], internal_args
[4], internal_args
[5],
3008 val
= (XSUBR (fun
)->function
.a8
3009 (internal_args
[0], internal_args
[1], internal_args
[2],
3010 internal_args
[3], internal_args
[4], internal_args
[5],
3011 internal_args
[6], internal_args
[7]));
3016 /* If a subr takes more than 8 arguments without using MANY
3017 or UNEVALLED, we need to extend this function to support it.
3018 Until this is done, there is no way to call the function. */
3023 else if (COMPILEDP (fun
))
3024 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3027 if (EQ (fun
, Qunbound
))
3028 xsignal1 (Qvoid_function
, original_fun
);
3030 xsignal1 (Qinvalid_function
, original_fun
);
3031 funcar
= XCAR (fun
);
3032 if (!SYMBOLP (funcar
))
3033 xsignal1 (Qinvalid_function
, original_fun
);
3034 if (EQ (funcar
, Qlambda
)
3035 || EQ (funcar
, Qclosure
))
3036 val
= funcall_lambda (fun
, numargs
, args
+ 1);
3037 else if (EQ (funcar
, Qautoload
))
3039 do_autoload (fun
, original_fun
);
3044 xsignal1 (Qinvalid_function
, original_fun
);
3048 if (backtrace
.debug_on_exit
)
3049 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
3050 backtrace_list
= backtrace
.next
;
3055 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
3057 Lisp_Object args_left
;
3060 register Lisp_Object
*arg_vector
;
3061 struct gcpro gcpro1
, gcpro2
, gcpro3
;
3062 register Lisp_Object tem
;
3065 numargs
= XFASTINT (Flength (args
));
3066 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
3069 GCPRO3 (*arg_vector
, args_left
, fun
);
3072 for (i
= 0; i
< numargs
; )
3074 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
3075 tem
= eval_sub (tem
);
3076 arg_vector
[i
++] = tem
;
3082 backtrace_list
->args
= arg_vector
;
3083 backtrace_list
->nargs
= i
;
3084 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
3086 /* Do the debug-on-exit now, while arg_vector still exists. */
3087 if (backtrace_list
->debug_on_exit
)
3088 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
3089 /* Don't do it again when we return to eval. */
3090 backtrace_list
->debug_on_exit
= 0;
3095 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3096 and return the result of evaluation.
3097 FUN must be either a lambda-expression or a compiled-code object. */
3100 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
3101 register Lisp_Object
*arg_vector
)
3103 Lisp_Object val
, syms_left
, next
, lexenv
;
3104 ptrdiff_t count
= SPECPDL_INDEX ();
3110 if (EQ (XCAR (fun
), Qclosure
))
3112 fun
= XCDR (fun
); /* Drop `closure'. */
3113 lexenv
= XCAR (fun
);
3114 CHECK_LIST_CONS (fun
, fun
);
3118 syms_left
= XCDR (fun
);
3119 if (CONSP (syms_left
))
3120 syms_left
= XCAR (syms_left
);
3122 xsignal1 (Qinvalid_function
, fun
);
3124 else if (COMPILEDP (fun
))
3126 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
3127 if (INTEGERP (syms_left
))
3128 /* A byte-code object with a non-nil `push args' slot means we
3129 shouldn't bind any arguments, instead just call the byte-code
3130 interpreter directly; it will push arguments as necessary.
3132 Byte-code objects with either a non-existent, or a nil value for
3133 the `push args' slot (the default), have dynamically-bound
3134 arguments, and use the argument-binding code below instead (as do
3135 all interpreted functions, even lexically bound ones). */
3137 /* If we have not actually read the bytecode string
3138 and constants vector yet, fetch them from the file. */
3139 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3140 Ffetch_bytecode (fun
);
3141 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3142 AREF (fun
, COMPILED_CONSTANTS
),
3143 AREF (fun
, COMPILED_STACK_DEPTH
),
3152 i
= optional
= rest
= 0;
3153 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3157 next
= XCAR (syms_left
);
3158 if (!SYMBOLP (next
))
3159 xsignal1 (Qinvalid_function
, fun
);
3161 if (EQ (next
, Qand_rest
))
3163 else if (EQ (next
, Qand_optional
))
3170 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3174 arg
= arg_vector
[i
++];
3176 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3180 /* Bind the argument. */
3181 if (!NILP (lexenv
) && SYMBOLP (next
))
3182 /* Lexically bind NEXT by adding it to the lexenv alist. */
3183 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3185 /* Dynamically bind NEXT. */
3186 specbind (next
, arg
);
3190 if (!NILP (syms_left
))
3191 xsignal1 (Qinvalid_function
, fun
);
3193 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3195 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3196 /* Instantiate a new lexical environment. */
3197 specbind (Qinternal_interpreter_environment
, lexenv
);
3200 val
= Fprogn (XCDR (XCDR (fun
)));
3203 /* If we have not actually read the bytecode string
3204 and constants vector yet, fetch them from the file. */
3205 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3206 Ffetch_bytecode (fun
);
3207 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3208 AREF (fun
, COMPILED_CONSTANTS
),
3209 AREF (fun
, COMPILED_STACK_DEPTH
),
3213 return unbind_to (count
, val
);
3216 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3218 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3219 (Lisp_Object object
)
3223 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3225 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3228 tem
= AREF (object
, COMPILED_BYTECODE
);
3229 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3230 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3232 error ("Invalid byte code");
3234 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3235 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3243 register ptrdiff_t count
= SPECPDL_INDEX ();
3244 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3245 if (max_size
<= specpdl_size
)
3247 if (max_specpdl_size
< 400)
3248 max_size
= max_specpdl_size
= 400;
3249 if (max_size
<= specpdl_size
)
3250 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3252 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3253 specpdl_ptr
= specpdl
+ count
;
3256 /* `specpdl_ptr->symbol' is a field which describes which variable is
3257 let-bound, so it can be properly undone when we unbind_to.
3258 It can have the following two shapes:
3259 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3260 a symbol that is not buffer-local (at least at the time
3261 the let binding started). Note also that it should not be
3262 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3264 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3265 variable SYMBOL which can be buffer-local. WHERE tells us
3266 which buffer is affected (or nil if the let-binding affects the
3267 global value of the variable) and BUFFER tells us which buffer was
3268 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3269 BUFFER did not yet have a buffer-local value). */
3272 specbind (Lisp_Object symbol
, Lisp_Object value
)
3274 struct Lisp_Symbol
*sym
;
3276 eassert (!handling_signal
);
3278 CHECK_SYMBOL (symbol
);
3279 sym
= XSYMBOL (symbol
);
3280 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3284 switch (sym
->redirect
)
3286 case SYMBOL_VARALIAS
:
3287 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3288 case SYMBOL_PLAINVAL
:
3289 /* The most common case is that of a non-constant symbol with a
3290 trivial value. Make that as fast as we can. */
3291 specpdl_ptr
->symbol
= symbol
;
3292 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3293 specpdl_ptr
->func
= NULL
;
3296 SET_SYMBOL_VAL (sym
, value
);
3298 set_internal (symbol
, value
, Qnil
, 1);
3300 case SYMBOL_LOCALIZED
:
3301 if (SYMBOL_BLV (sym
)->frame_local
)
3302 error ("Frame-local vars cannot be let-bound");
3303 case SYMBOL_FORWARDED
:
3305 Lisp_Object ovalue
= find_symbol_value (symbol
);
3306 specpdl_ptr
->func
= 0;
3307 specpdl_ptr
->old_value
= ovalue
;
3309 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3310 || (EQ (SYMBOL_BLV (sym
)->where
,
3311 SYMBOL_BLV (sym
)->frame_local
?
3312 Fselected_frame () : Fcurrent_buffer ())));
3314 if (sym
->redirect
== SYMBOL_LOCALIZED
3315 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3317 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3319 /* For a local variable, record both the symbol and which
3320 buffer's or frame's value we are saving. */
3321 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3323 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3324 || (BLV_FOUND (SYMBOL_BLV (sym
))
3325 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3328 else if (sym
->redirect
== SYMBOL_LOCALIZED
3329 && BLV_FOUND (SYMBOL_BLV (sym
)))
3330 where
= SYMBOL_BLV (sym
)->where
;
3334 /* We're not using the `unused' slot in the specbinding
3335 structure because this would mean we have to do more
3336 work for simple variables. */
3337 /* FIXME: The third value `current_buffer' is only used in
3338 let_shadows_buffer_binding_p which is itself only used
3339 in set_internal for local_if_set. */
3340 eassert (NILP (where
) || EQ (where
, cur_buf
));
3341 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3343 /* If SYMBOL is a per-buffer variable which doesn't have a
3344 buffer-local value here, make the `let' change the global
3345 value by changing the value of SYMBOL in all buffers not
3346 having their own value. This is consistent with what
3347 happens with other buffer-local variables. */
3349 && sym
->redirect
== SYMBOL_FORWARDED
)
3351 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3353 Fset_default (symbol
, value
);
3358 specpdl_ptr
->symbol
= symbol
;
3361 set_internal (symbol
, value
, Qnil
, 1);
3369 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3371 eassert (!handling_signal
);
3373 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3375 specpdl_ptr
->func
= function
;
3376 specpdl_ptr
->symbol
= Qnil
;
3377 specpdl_ptr
->old_value
= arg
;
3382 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3384 Lisp_Object quitf
= Vquit_flag
;
3385 struct gcpro gcpro1
, gcpro2
;
3387 GCPRO2 (value
, quitf
);
3390 while (specpdl_ptr
!= specpdl
+ count
)
3392 /* Copy the binding, and decrement specpdl_ptr, before we do
3393 the work to unbind it. We decrement first
3394 so that an error in unbinding won't try to unbind
3395 the same entry again, and we copy the binding first
3396 in case more bindings are made during some of the code we run. */
3398 struct specbinding this_binding
;
3399 this_binding
= *--specpdl_ptr
;
3401 if (this_binding
.func
!= 0)
3402 (*this_binding
.func
) (this_binding
.old_value
);
3403 /* If the symbol is a list, it is really (SYMBOL WHERE
3404 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3405 frame. If WHERE is a buffer or frame, this indicates we
3406 bound a variable that had a buffer-local or frame-local
3407 binding. WHERE nil means that the variable had the default
3408 value when it was bound. CURRENT-BUFFER is the buffer that
3409 was current when the variable was bound. */
3410 else if (CONSP (this_binding
.symbol
))
3412 Lisp_Object symbol
, where
;
3414 symbol
= XCAR (this_binding
.symbol
);
3415 where
= XCAR (XCDR (this_binding
.symbol
));
3418 Fset_default (symbol
, this_binding
.old_value
);
3419 /* If `where' is non-nil, reset the value in the appropriate
3420 local binding, but only if that binding still exists. */
3421 else if (BUFFERP (where
)
3422 ? !NILP (Flocal_variable_p (symbol
, where
))
3423 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3424 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3426 /* If variable has a trivial value (no forwarding), we can
3427 just set it. No need to check for constant symbols here,
3428 since that was already done by specbind. */
3429 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3430 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3431 this_binding
.old_value
);
3433 /* NOTE: we only ever come here if make_local_foo was used for
3434 the first time on this var within this let. */
3435 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3438 if (NILP (Vquit_flag
) && !NILP (quitf
))
3445 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3446 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3447 A special variable is one that will be bound dynamically, even in a
3448 context where binding is lexical by default. */)
3449 (Lisp_Object symbol
)
3451 CHECK_SYMBOL (symbol
);
3452 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3456 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3457 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3458 The debugger is entered when that frame exits, if the flag is non-nil. */)
3459 (Lisp_Object level
, Lisp_Object flag
)
3461 register struct backtrace
*backlist
= backtrace_list
;
3462 register EMACS_INT i
;
3464 CHECK_NUMBER (level
);
3466 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3468 backlist
= backlist
->next
;
3472 backlist
->debug_on_exit
= !NILP (flag
);
3477 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3478 doc
: /* Print a trace of Lisp function calls currently active.
3479 Output stream used is value of `standard-output'. */)
3482 register struct backtrace
*backlist
= backtrace_list
;
3485 struct gcpro gcpro1
;
3486 Lisp_Object old_print_level
= Vprint_level
;
3488 if (NILP (Vprint_level
))
3489 XSETFASTINT (Vprint_level
, 8);
3496 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3497 if (backlist
->nargs
== UNEVALLED
)
3499 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3500 write_string ("\n", -1);
3504 tem
= *backlist
->function
;
3505 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3506 write_string ("(", -1);
3507 if (backlist
->nargs
== MANY
)
3508 { /* FIXME: Can this happen? */
3510 for (tail
= *backlist
->args
, i
= 0;
3512 tail
= Fcdr (tail
), i
= 1)
3514 if (i
) write_string (" ", -1);
3515 Fprin1 (Fcar (tail
), Qnil
);
3521 for (i
= 0; i
< backlist
->nargs
; i
++)
3523 if (i
) write_string (" ", -1);
3524 Fprin1 (backlist
->args
[i
], Qnil
);
3527 write_string (")\n", -1);
3529 backlist
= backlist
->next
;
3532 Vprint_level
= old_print_level
;
3537 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3538 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3539 If that frame has not evaluated the arguments yet (or is a special form),
3540 the value is (nil FUNCTION ARG-FORMS...).
3541 If that frame has evaluated its arguments and called its function already,
3542 the value is (t FUNCTION ARG-VALUES...).
3543 A &rest arg is represented as the tail of the list ARG-VALUES.
3544 FUNCTION is whatever was supplied as car of evaluated list,
3545 or a lambda expression for macro calls.
3546 If NFRAMES is more than the number of frames, the value is nil. */)
3547 (Lisp_Object nframes
)
3549 register struct backtrace
*backlist
= backtrace_list
;
3550 register EMACS_INT i
;
3553 CHECK_NATNUM (nframes
);
3555 /* Find the frame requested. */
3556 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3557 backlist
= backlist
->next
;
3561 if (backlist
->nargs
== UNEVALLED
)
3562 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3565 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3566 tem
= *backlist
->args
;
3568 tem
= Flist (backlist
->nargs
, backlist
->args
);
3570 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3577 mark_backtrace (void)
3579 register struct backtrace
*backlist
;
3582 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3584 mark_object (*backlist
->function
);
3586 if (backlist
->nargs
== UNEVALLED
3587 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3590 i
= backlist
->nargs
;
3592 mark_object (backlist
->args
[i
]);
3600 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3601 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3602 If Lisp code tries to increase the total number past this amount,
3603 an error is signaled.
3604 You can safely use a value considerably larger than the default value,
3605 if that proves inconveniently small. However, if you increase it too far,
3606 Emacs could run out of memory trying to make the stack bigger. */);
3608 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3609 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3611 This limit serves to catch infinite recursions for you before they cause
3612 actual stack overflow in C, which would be fatal for Emacs.
3613 You can safely make it considerably larger than its default value,
3614 if that proves inconveniently small. However, if you increase it too far,
3615 Emacs could overflow the real C stack, and crash. */);
3617 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3618 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3619 If the value is t, that means do an ordinary quit.
3620 If the value equals `throw-on-input', that means quit by throwing
3621 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3622 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3623 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3626 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3627 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3628 Note that `quit-flag' will still be set by typing C-g,
3629 so a quit will be signaled as soon as `inhibit-quit' is nil.
3630 To prevent this happening, set `quit-flag' to nil
3631 before making `inhibit-quit' nil. */);
3632 Vinhibit_quit
= Qnil
;
3634 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3635 DEFSYM (Qautoload
, "autoload");
3636 DEFSYM (Qdebug_on_error
, "debug-on-error");
3637 DEFSYM (Qmacro
, "macro");
3638 DEFSYM (Qdeclare
, "declare");
3640 /* Note that the process handling also uses Qexit, but we don't want
3641 to staticpro it twice, so we just do it here. */
3642 DEFSYM (Qexit
, "exit");
3644 DEFSYM (Qinteractive
, "interactive");
3645 DEFSYM (Qcommandp
, "commandp");
3646 DEFSYM (Qdefun
, "defun");
3647 DEFSYM (Qand_rest
, "&rest");
3648 DEFSYM (Qand_optional
, "&optional");
3649 DEFSYM (Qclosure
, "closure");
3650 DEFSYM (Qdebug
, "debug");
3652 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3653 doc
: /* *Non-nil means enter debugger if an error is signaled.
3654 Does not apply to errors handled by `condition-case' or those
3655 matched by `debug-ignored-errors'.
3656 If the value is a list, an error only means to enter the debugger
3657 if one of its condition symbols appears in the list.
3658 When you evaluate an expression interactively, this variable
3659 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3660 The command `toggle-debug-on-error' toggles this.
3661 See also the variable `debug-on-quit'. */);
3662 Vdebug_on_error
= Qnil
;
3664 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3665 doc
: /* *List of errors for which the debugger should not be called.
3666 Each element may be a condition-name or a regexp that matches error messages.
3667 If any element applies to a given error, that error skips the debugger
3668 and just returns to top level.
3669 This overrides the variable `debug-on-error'.
3670 It does not apply to errors handled by `condition-case'. */);
3671 Vdebug_ignored_errors
= Qnil
;
3673 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3674 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3675 Does not apply if quit is handled by a `condition-case'. */);
3678 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3679 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3681 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3682 doc
: /* Non-nil means debugger may continue execution.
3683 This is nil when the debugger is called under circumstances where it
3684 might not be safe to continue. */);
3685 debugger_may_continue
= 1;
3687 DEFVAR_LISP ("debugger", Vdebugger
,
3688 doc
: /* Function to call to invoke debugger.
3689 If due to frame exit, args are `exit' and the value being returned;
3690 this function's value will be returned instead of that.
3691 If due to error, args are `error' and a list of the args to `signal'.
3692 If due to `apply' or `funcall' entry, one arg, `lambda'.
3693 If due to `eval' entry, one arg, t. */);
3696 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3697 doc
: /* If non-nil, this is a function for `signal' to call.
3698 It receives the same arguments that `signal' was given.
3699 The Edebug package uses this to regain control. */);
3700 Vsignal_hook_function
= Qnil
;
3702 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3703 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3704 Note that `debug-on-error', `debug-on-quit' and friends
3705 still determine whether to handle the particular condition. */);
3706 Vdebug_on_signal
= Qnil
;
3708 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3709 doc
: /* Function to process declarations in a macro definition.
3710 The function will be called with two args MACRO and DECL.
3711 MACRO is the name of the macro being defined.
3712 DECL is a list `(declare ...)' containing the declarations.
3713 The value the function returns is not used. */);
3714 Vmacro_declaration_function
= Qnil
;
3716 /* When lexical binding is being used,
3717 vinternal_interpreter_environment is non-nil, and contains an alist
3718 of lexically-bound variable, or (t), indicating an empty
3719 environment. The lisp name of this variable would be
3720 `internal-interpreter-environment' if it weren't hidden.
3721 Every element of this list can be either a cons (VAR . VAL)
3722 specifying a lexical binding, or a single symbol VAR indicating
3723 that this variable should use dynamic scoping. */
3724 DEFSYM (Qinternal_interpreter_environment
, "internal-interpreter-environment");
3725 DEFVAR_LISP ("internal-interpreter-environment",
3726 Vinternal_interpreter_environment
,
3727 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3728 When lexical binding is not being used, this variable is nil.
3729 A value of `(t)' indicates an empty environment, otherwise it is an
3730 alist of active lexical bindings. */);
3731 Vinternal_interpreter_environment
= Qnil
;
3732 /* Don't export this variable to Elisp, so noone can mess with it
3733 (Just imagine if someone makes it buffer-local). */
3734 Funintern (Qinternal_interpreter_environment
, Qnil
);
3736 DEFSYM (Vrun_hooks
, "run-hooks");
3738 staticpro (&Vautoload_queue
);
3739 Vautoload_queue
= Qnil
;
3740 staticpro (&Vsignaling_function
);
3741 Vsignaling_function
= Qnil
;
3752 defsubr (&Sfunction
);
3754 defsubr (&Sdefmacro
);
3756 defsubr (&Sdefvaralias
);
3757 defsubr (&Sdefconst
);
3758 defsubr (&Suser_variable_p
);
3762 defsubr (&Smacroexpand
);
3765 defsubr (&Sunwind_protect
);
3766 defsubr (&Scondition_case
);
3768 defsubr (&Sinteractive_p
);
3769 defsubr (&Scalled_interactively_p
);
3770 defsubr (&Scommandp
);
3771 defsubr (&Sautoload
);
3774 defsubr (&Sfuncall
);
3775 defsubr (&Srun_hooks
);
3776 defsubr (&Srun_hook_with_args
);
3777 defsubr (&Srun_hook_with_args_until_success
);
3778 defsubr (&Srun_hook_with_args_until_failure
);
3779 defsubr (&Srun_hook_wrapped
);
3780 defsubr (&Sfetch_bytecode
);
3781 defsubr (&Sbacktrace_debug
);
3782 defsubr (&Sbacktrace
);
3783 defsubr (&Sbacktrace_frame
);
3784 defsubr (&Sspecial_variable_p
);
3785 defsubr (&Sfunctionp
);