1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
35 struct backtrace
*backtrace_list
;
40 struct catchtag
*catchlist
;
42 /* Chain of condition handlers currently in effect.
43 The elements of this chain are contained in the stack frames
44 of Fcondition_case and internal_condition_case.
45 When an error is signaled (by calling Fsignal, below),
46 this chain is searched for an element that applies. */
51 struct handler
*handlerlist
;
54 /* Count levels of GCPRO to detect failure to UNGCPRO. */
58 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
59 Lisp_Object Qinhibit_quit
;
60 Lisp_Object Qand_rest
;
61 static Lisp_Object Qand_optional
;
62 static Lisp_Object Qinhibit_debugger
;
63 static Lisp_Object Qdeclare
;
64 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
66 static Lisp_Object Qdebug
;
68 /* This holds either the symbol `run-hooks' or nil.
69 It is nil at an early stage of startup, and when Emacs
72 Lisp_Object Vrun_hooks
;
74 /* Non-nil means record all fset's and provide's, to be undone
75 if the file being autoloaded is not fully loaded.
76 They are recorded by being consed onto the front of Vautoload_queue:
77 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
79 Lisp_Object Vautoload_queue
;
81 /* Current number of specbindings allocated in specpdl. */
83 ptrdiff_t specpdl_size
;
85 /* Pointer to beginning of specpdl. */
87 struct specbinding
*specpdl
;
89 /* Pointer to first unused element in specpdl. */
91 struct specbinding
*specpdl_ptr
;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth
;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
104 static EMACS_INT when_entered_debugger
;
106 /* The function from which the last `signal' was called. Set in
109 Lisp_Object Vsignaling_function
;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code
;
117 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
118 static bool interactive_p (void);
119 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
121 /* Functions to set Lisp_Object slots of struct specbinding. */
124 set_specpdl_symbol (Lisp_Object symbol
)
126 specpdl_ptr
->symbol
= symbol
;
130 set_specpdl_old_value (Lisp_Object oldval
)
132 specpdl_ptr
->old_value
= oldval
;
136 init_eval_once (void)
139 specpdl
= xmalloc (size
* sizeof *specpdl
);
141 specpdl_ptr
= specpdl
;
142 /* Don't forget to update docs (lispref node "Local Variables"). */
143 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
144 max_lisp_eval_depth
= 600;
152 specpdl_ptr
= specpdl
;
157 debug_on_next_call
= 0;
162 /* This is less than the initial value of num_nonmacro_input_events. */
163 when_entered_debugger
= -1;
166 /* Unwind-protect function used by call_debugger. */
169 restore_stack_limits (Lisp_Object data
)
171 max_specpdl_size
= XINT (XCAR (data
));
172 max_lisp_eval_depth
= XINT (XCDR (data
));
176 /* Call the Lisp debugger, giving it argument ARG. */
179 call_debugger (Lisp_Object arg
)
181 bool debug_while_redisplaying
;
182 ptrdiff_t count
= SPECPDL_INDEX ();
184 EMACS_INT old_max
= max_specpdl_size
;
186 /* Temporarily bump up the stack limits,
187 so the debugger won't run out of stack. */
189 max_specpdl_size
+= 1;
190 record_unwind_protect (restore_stack_limits
,
191 Fcons (make_number (old_max
),
192 make_number (max_lisp_eval_depth
)));
193 max_specpdl_size
= old_max
;
195 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
196 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
198 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
199 max_specpdl_size
= SPECPDL_INDEX () + 100;
201 #ifdef HAVE_WINDOW_SYSTEM
202 if (display_hourglass_p
)
206 debug_on_next_call
= 0;
207 when_entered_debugger
= num_nonmacro_input_events
;
209 /* Resetting redisplaying_p to 0 makes sure that debug output is
210 displayed if the debugger is invoked during redisplay. */
211 debug_while_redisplaying
= redisplaying_p
;
213 specbind (intern ("debugger-may-continue"),
214 debug_while_redisplaying
? Qnil
: Qt
);
215 specbind (Qinhibit_redisplay
, Qnil
);
216 specbind (Qinhibit_debugger
, Qt
);
218 #if 0 /* Binding this prevents execution of Lisp code during
219 redisplay, which necessarily leads to display problems. */
220 specbind (Qinhibit_eval_during_redisplay
, Qt
);
223 val
= apply1 (Vdebugger
, arg
);
225 /* Interrupting redisplay and resuming it later is not safe under
226 all circumstances. So, when the debugger returns, abort the
227 interrupted redisplay by going back to the top-level. */
228 if (debug_while_redisplaying
)
231 return unbind_to (count
, val
);
235 do_debug_on_call (Lisp_Object code
)
237 debug_on_next_call
= 0;
238 backtrace_list
->debug_on_exit
= 1;
239 call_debugger (Fcons (code
, Qnil
));
242 /* NOTE!!! Every function that can call EVAL must protect its args
243 and temporaries from garbage collection while it needs them.
244 The definition of `For' shows what you have to do. */
246 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
247 doc
: /* Eval args until one of them yields non-nil, then return that value.
248 The remaining args are not evalled at all.
249 If all args return nil, return nil.
250 usage: (or CONDITIONS...) */)
253 register Lisp_Object val
= Qnil
;
260 val
= eval_sub (XCAR (args
));
270 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
271 doc
: /* Eval args until one of them yields nil, then return nil.
272 The remaining args are not evalled at all.
273 If no arg yields nil, return the last arg's value.
274 usage: (and CONDITIONS...) */)
277 register Lisp_Object val
= Qt
;
284 val
= eval_sub (XCAR (args
));
294 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
295 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
296 Returns the value of THEN or the value of the last of the ELSE's.
297 THEN must be one expression, but ELSE... can be zero or more expressions.
298 If COND yields nil, and there are no ELSE's, the value is nil.
299 usage: (if COND THEN ELSE...) */)
302 register Lisp_Object cond
;
306 cond
= eval_sub (Fcar (args
));
310 return eval_sub (Fcar (Fcdr (args
)));
311 return Fprogn (Fcdr (Fcdr (args
)));
314 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
315 doc
: /* Try each clause until one succeeds.
316 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
317 and, if the value is non-nil, this clause succeeds:
318 then the expressions in BODY are evaluated and the last one's
319 value is the value of the cond-form.
320 If no clause succeeds, cond returns nil.
321 If a clause has one element, as in (CONDITION),
322 CONDITION's value if non-nil is returned from the cond-form.
323 usage: (cond CLAUSES...) */)
326 register Lisp_Object clause
, val
;
333 clause
= Fcar (args
);
334 val
= eval_sub (Fcar (clause
));
337 if (!EQ (XCDR (clause
), Qnil
))
338 val
= Fprogn (XCDR (clause
));
348 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
349 doc
: /* Eval BODY forms sequentially and return value of last one.
350 usage: (progn BODY...) */)
353 register Lisp_Object val
= Qnil
;
360 val
= eval_sub (XCAR (args
));
368 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
369 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
370 The value of FIRST is saved during the evaluation of the remaining args,
371 whose values are discarded.
372 usage: (prog1 FIRST BODY...) */)
376 register Lisp_Object args_left
;
377 struct gcpro gcpro1
, gcpro2
;
383 val
= eval_sub (XCAR (args_left
));
384 while (CONSP (args_left
= XCDR (args_left
)))
385 eval_sub (XCAR (args_left
));
391 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
392 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
393 The value of FORM2 is saved during the evaluation of the
394 remaining args, whose values are discarded.
395 usage: (prog2 FORM1 FORM2 BODY...) */)
401 eval_sub (XCAR (args
));
403 return Fprog1 (XCDR (args
));
406 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
407 doc
: /* Set each SYM to the value of its VAL.
408 The symbols SYM are variables; they are literal (not evaluated).
409 The values VAL are expressions; they are evaluated.
410 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
411 The second VAL is not computed until after the first SYM is set, and so on;
412 each VAL can use the new value of variables set earlier in the `setq'.
413 The return value of the `setq' form is the value of the last VAL.
414 usage: (setq [SYM VAL]...) */)
417 register Lisp_Object args_left
;
418 register Lisp_Object val
, sym
, lex_binding
;
429 val
= eval_sub (Fcar (Fcdr (args_left
)));
430 sym
= Fcar (args_left
);
432 /* Like for eval_sub, we do not check declared_special here since
433 it's been done when let-binding. */
434 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
436 && !NILP (lex_binding
437 = Fassq (sym
, Vinternal_interpreter_environment
)))
438 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
440 Fset (sym
, val
); /* SYM is dynamically bound. */
442 args_left
= Fcdr (Fcdr (args_left
));
444 while (!NILP (args_left
));
450 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
451 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
452 Warning: `quote' does not construct its return value, but just returns
453 the value that was pre-constructed by the Lisp reader (see info node
454 `(elisp)Printed Representation').
455 This means that '(a . b) is not identical to (cons 'a 'b): the former
456 does not cons. Quoting should be reserved for constants that will
457 never be modified by side-effects, unless you like self-modifying code.
458 See the common pitfall in info node `(elisp)Rearrangement' for an example
459 of unexpected results when a quoted object is modified.
460 usage: (quote ARG) */)
463 if (!NILP (Fcdr (args
)))
464 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
468 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
469 doc
: /* Like `quote', but preferred for objects which are functions.
470 In byte compilation, `function' causes its argument to be compiled.
471 `quote' cannot do that.
472 usage: (function ARG) */)
475 Lisp_Object quoted
= XCAR (args
);
477 if (!NILP (Fcdr (args
)))
478 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
480 if (!NILP (Vinternal_interpreter_environment
)
482 && EQ (XCAR (quoted
), Qlambda
))
483 /* This is a lambda expression within a lexical environment;
484 return an interpreted closure instead of a simple lambda. */
485 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
488 /* Simply quote the argument. */
493 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
494 doc
: /* Return t if the containing function was run directly by user input.
495 This means that the function was called with `call-interactively'
496 \(which includes being called as the binding of a key)
497 and input is currently coming from the keyboard (not a keyboard macro),
498 and Emacs is not running in batch mode (`noninteractive' is nil).
500 The only known proper use of `interactive-p' is in deciding whether to
501 display a helpful message, or how to display it. If you're thinking
502 of using it for any other purpose, it is quite likely that you're
503 making a mistake. Think: what do you want to do when the command is
504 called from a keyboard macro?
506 To test whether your function was called with `call-interactively',
507 either (i) add an extra optional argument and give it an `interactive'
508 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
509 use `called-interactively-p'. */)
512 return (INTERACTIVE
&& interactive_p ()) ? Qt
: Qnil
;
516 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
517 doc
: /* Return t if the containing function was called by `call-interactively'.
518 If KIND is `interactive', then only return t if the call was made
519 interactively by the user, i.e. not in `noninteractive' mode nor
520 when `executing-kbd-macro'.
521 If KIND is `any', on the other hand, it will return t for any kind of
522 interactive call, including being called as the binding of a key, or
523 from a keyboard macro, or in `noninteractive' mode.
525 The only known proper use of `interactive' for KIND is in deciding
526 whether to display a helpful message, or how to display it. If you're
527 thinking of using it for any other purpose, it is quite likely that
528 you're making a mistake. Think: what do you want to do when the
529 command is called from a keyboard macro?
531 Instead of using this function, it is sometimes cleaner to give your
532 function an extra optional argument whose `interactive' spec specifies
533 non-nil unconditionally (\"p\" is a good way to do this), or via
534 \(not (or executing-kbd-macro noninteractive)). */)
537 return (((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
543 /* Return true if function in which this appears was called using
544 call-interactively and is not a built-in. */
549 struct backtrace
*btp
;
552 btp
= backtrace_list
;
554 /* If this isn't a byte-compiled function, there may be a frame at
555 the top for Finteractive_p. If so, skip it. */
556 fun
= Findirect_function (btp
->function
, Qnil
);
557 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
558 || XSUBR (fun
) == &Scalled_interactively_p
))
561 /* If we're running an Emacs 18-style byte-compiled function, there
562 may be a frame for Fbytecode at the top level. In any version of
563 Emacs there can be Fbytecode frames for subexpressions evaluated
564 inside catch and condition-case. Skip past them.
566 If this isn't a byte-compiled function, then we may now be
567 looking at several frames for special forms. Skip past them. */
569 && (EQ (btp
->function
, Qbytecode
)
570 || btp
->nargs
== UNEVALLED
))
573 /* `btp' now points at the frame of the innermost function that isn't
574 a special form, ignoring frames for Finteractive_p and/or
575 Fbytecode at the top. If this frame is for a built-in function
576 (such as load or eval-region) return false. */
577 fun
= Findirect_function (btp
->function
, Qnil
);
581 /* `btp' points to the frame of a Lisp function that called interactive-p.
582 Return t if that function was called interactively. */
583 if (btp
&& btp
->next
&& EQ (btp
->next
->function
, Qcall_interactively
))
589 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
590 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
591 Aliased variables always have the same value; setting one sets the other.
592 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
593 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
594 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
595 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
596 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
597 The return value is BASE-VARIABLE. */)
598 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
600 struct Lisp_Symbol
*sym
;
602 CHECK_SYMBOL (new_alias
);
603 CHECK_SYMBOL (base_variable
);
605 sym
= XSYMBOL (new_alias
);
608 /* Not sure why, but why not? */
609 error ("Cannot make a constant an alias");
611 switch (sym
->redirect
)
613 case SYMBOL_FORWARDED
:
614 error ("Cannot make an internal variable an alias");
615 case SYMBOL_LOCALIZED
:
616 error ("Don't know how to make a localized variable an alias");
619 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
620 If n_a is bound, but b_v is not, set the value of b_v to n_a,
621 so that old-code that affects n_a before the aliasing is setup
623 if (NILP (Fboundp (base_variable
)))
624 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
627 struct specbinding
*p
;
629 for (p
= specpdl_ptr
; p
> specpdl
; )
630 if ((--p
)->func
== NULL
632 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
633 error ("Don't know how to make a let-bound variable an alias");
636 sym
->declared_special
= 1;
637 XSYMBOL (base_variable
)->declared_special
= 1;
638 sym
->redirect
= SYMBOL_VARALIAS
;
639 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
640 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
641 LOADHIST_ATTACH (new_alias
);
642 /* Even if docstring is nil: remove old docstring. */
643 Fput (new_alias
, Qvariable_documentation
, docstring
);
645 return base_variable
;
649 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
650 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
651 You are not required to define a variable in order to use it, but
652 defining it lets you supply an initial value and documentation, which
653 can be referred to by the Emacs help facilities and other programming
654 tools. The `defvar' form also declares the variable as \"special\",
655 so that it is always dynamically bound even if `lexical-binding' is t.
657 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
658 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
659 default value is what is set; buffer-local values are not affected.
660 If INITVALUE is missing, SYMBOL's value is not set.
662 If SYMBOL has a local binding, then this form affects the local
663 binding. This is usually not what you want. Thus, if you need to
664 load a file defining variables, with this form or with `defconst' or
665 `defcustom', you should always load that file _outside_ any bindings
666 for these variables. \(`defconst' and `defcustom' behave similarly in
669 The optional argument DOCSTRING is a documentation string for the
672 To define a user option, use `defcustom' instead of `defvar'.
673 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
676 register Lisp_Object sym
, tem
, tail
;
680 if (!NILP (Fcdr (Fcdr (tail
))))
681 error ("Too many arguments");
683 tem
= Fdefault_boundp (sym
);
686 /* Do it before evaluating the initial value, for self-references. */
687 XSYMBOL (sym
)->declared_special
= 1;
690 Fset_default (sym
, eval_sub (Fcar (tail
)));
692 { /* Check if there is really a global binding rather than just a let
693 binding that shadows the global unboundness of the var. */
694 struct specbinding
*pdl
= specpdl_ptr
;
695 while (pdl
> specpdl
)
697 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
698 && EQ (pdl
->old_value
, Qunbound
))
700 message_with_string ("Warning: defvar ignored because %s is let-bound",
701 SYMBOL_NAME (sym
), 1);
710 if (!NILP (Vpurify_flag
))
711 tem
= Fpurecopy (tem
);
712 Fput (sym
, Qvariable_documentation
, tem
);
714 LOADHIST_ATTACH (sym
);
716 else if (!NILP (Vinternal_interpreter_environment
)
717 && !XSYMBOL (sym
)->declared_special
)
718 /* A simple (defvar foo) with lexical scoping does "nothing" except
719 declare that var to be dynamically scoped *locally* (i.e. within
720 the current file or let-block). */
721 Vinternal_interpreter_environment
=
722 Fcons (sym
, Vinternal_interpreter_environment
);
725 /* Simple (defvar <var>) should not count as a definition at all.
726 It could get in the way of other definitions, and unloading this
727 package could try to make the variable unbound. */
733 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
734 doc
: /* Define SYMBOL as a constant variable.
735 This declares that neither programs nor users should ever change the
736 value. This constancy is not actually enforced by Emacs Lisp, but
737 SYMBOL is marked as a special variable so that it is never lexically
740 The `defconst' form always sets the value of SYMBOL to the result of
741 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
742 what is set; buffer-local values are not affected. If SYMBOL has a
743 local binding, then this form sets the local binding's value.
744 However, you should normally not make local bindings for variables
745 defined with this form.
747 The optional DOCSTRING specifies the variable's documentation string.
748 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
751 register Lisp_Object sym
, tem
;
754 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
755 error ("Too many arguments");
757 tem
= eval_sub (Fcar (Fcdr (args
)));
758 if (!NILP (Vpurify_flag
))
759 tem
= Fpurecopy (tem
);
760 Fset_default (sym
, tem
);
761 XSYMBOL (sym
)->declared_special
= 1;
762 tem
= Fcar (Fcdr (Fcdr (args
)));
765 if (!NILP (Vpurify_flag
))
766 tem
= Fpurecopy (tem
);
767 Fput (sym
, Qvariable_documentation
, tem
);
769 Fput (sym
, Qrisky_local_variable
, Qt
);
770 LOADHIST_ATTACH (sym
);
774 /* Make SYMBOL lexically scoped. */
775 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
776 Smake_var_non_special
, 1, 1, 0,
777 doc
: /* Internal function. */)
780 CHECK_SYMBOL (symbol
);
781 XSYMBOL (symbol
)->declared_special
= 0;
786 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
787 doc
: /* Bind variables according to VARLIST then eval BODY.
788 The value of the last form in BODY is returned.
789 Each element of VARLIST is a symbol (which is bound to nil)
790 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
791 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
792 usage: (let* VARLIST BODY...) */)
795 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
796 ptrdiff_t count
= SPECPDL_INDEX ();
797 struct gcpro gcpro1
, gcpro2
, gcpro3
;
799 GCPRO3 (args
, elt
, varlist
);
801 lexenv
= Vinternal_interpreter_environment
;
803 varlist
= Fcar (args
);
804 while (CONSP (varlist
))
808 elt
= XCAR (varlist
);
814 else if (! NILP (Fcdr (Fcdr (elt
))))
815 signal_error ("`let' bindings can have only one value-form", elt
);
819 val
= eval_sub (Fcar (Fcdr (elt
)));
822 if (!NILP (lexenv
) && SYMBOLP (var
)
823 && !XSYMBOL (var
)->declared_special
824 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
825 /* Lexically bind VAR by adding it to the interpreter's binding
829 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
830 if (EQ (Vinternal_interpreter_environment
, lexenv
))
831 /* Save the old lexical environment on the specpdl stack,
832 but only for the first lexical binding, since we'll never
833 need to revert to one of the intermediate ones. */
834 specbind (Qinternal_interpreter_environment
, newenv
);
836 Vinternal_interpreter_environment
= newenv
;
841 varlist
= XCDR (varlist
);
844 val
= Fprogn (Fcdr (args
));
845 return unbind_to (count
, val
);
848 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
849 doc
: /* Bind variables according to VARLIST then eval BODY.
850 The value of the last form in BODY is returned.
851 Each element of VARLIST is a symbol (which is bound to nil)
852 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
853 All the VALUEFORMs are evalled before any symbols are bound.
854 usage: (let VARLIST BODY...) */)
857 Lisp_Object
*temps
, tem
, lexenv
;
858 register Lisp_Object elt
, varlist
;
859 ptrdiff_t count
= SPECPDL_INDEX ();
861 struct gcpro gcpro1
, gcpro2
;
864 varlist
= Fcar (args
);
866 /* Make space to hold the values to give the bound variables. */
867 elt
= Flength (varlist
);
868 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
870 /* Compute the values and store them in `temps'. */
872 GCPRO2 (args
, *temps
);
875 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
878 elt
= XCAR (varlist
);
880 temps
[argnum
++] = Qnil
;
881 else if (! NILP (Fcdr (Fcdr (elt
))))
882 signal_error ("`let' bindings can have only one value-form", elt
);
884 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
885 gcpro2
.nvars
= argnum
;
889 lexenv
= Vinternal_interpreter_environment
;
891 varlist
= Fcar (args
);
892 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
896 elt
= XCAR (varlist
);
897 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
898 tem
= temps
[argnum
++];
900 if (!NILP (lexenv
) && SYMBOLP (var
)
901 && !XSYMBOL (var
)->declared_special
902 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
903 /* Lexically bind VAR by adding it to the lexenv alist. */
904 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
906 /* Dynamically bind VAR. */
910 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
911 /* Instantiate a new lexical environment. */
912 specbind (Qinternal_interpreter_environment
, lexenv
);
914 elt
= Fprogn (Fcdr (args
));
916 return unbind_to (count
, elt
);
919 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
920 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
921 The order of execution is thus TEST, BODY, TEST, BODY and so on
922 until TEST returns nil.
923 usage: (while TEST BODY...) */)
926 Lisp_Object test
, body
;
927 struct gcpro gcpro1
, gcpro2
;
933 while (!NILP (eval_sub (test
)))
943 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
944 doc
: /* Return result of expanding macros at top level of FORM.
945 If FORM is not a macro call, it is returned unchanged.
946 Otherwise, the macro is expanded and the expansion is considered
947 in place of FORM. When a non-macro-call results, it is returned.
949 The second optional arg ENVIRONMENT specifies an environment of macro
950 definitions to shadow the loaded ones for use in file byte-compilation. */)
951 (Lisp_Object form
, Lisp_Object environment
)
953 /* With cleanups from Hallvard Furuseth. */
954 register Lisp_Object expander
, sym
, def
, tem
;
958 /* Come back here each time we expand a macro call,
959 in case it expands into another macro call. */
962 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
963 def
= sym
= XCAR (form
);
965 /* Trace symbols aliases to other symbols
966 until we get a symbol that is not an alias. */
967 while (SYMBOLP (def
))
971 tem
= Fassq (sym
, environment
);
974 def
= XSYMBOL (sym
)->function
;
975 if (!EQ (def
, Qunbound
))
980 /* Right now TEM is the result from SYM in ENVIRONMENT,
981 and if TEM is nil then DEF is SYM's function definition. */
984 /* SYM is not mentioned in ENVIRONMENT.
985 Look at its function definition. */
988 def
= Fautoload_do_load (def
, sym
, Qmacro
);
990 if (EQ (def
, Qunbound
) || !CONSP (def
))
991 /* Not defined or definition not suitable. */
993 if (!EQ (XCAR (def
), Qmacro
))
995 else expander
= XCDR (def
);
999 expander
= XCDR (tem
);
1000 if (NILP (expander
))
1004 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1005 if (EQ (form
, newform
))
1014 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1015 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1016 TAG is evalled to get the tag to use; it must not be nil.
1018 Then the BODY is executed.
1019 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1020 If no throw happens, `catch' returns the value of the last BODY form.
1021 If a throw happens, it specifies the value to return from `catch'.
1022 usage: (catch TAG BODY...) */)
1025 register Lisp_Object tag
;
1026 struct gcpro gcpro1
;
1029 tag
= eval_sub (Fcar (args
));
1031 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1034 /* Set up a catch, then call C function FUNC on argument ARG.
1035 FUNC should return a Lisp_Object.
1036 This is how catches are done from within C code. */
1039 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1041 /* This structure is made part of the chain `catchlist'. */
1044 /* Fill in the components of c, and put it on the list. */
1048 c
.backlist
= backtrace_list
;
1049 c
.handlerlist
= handlerlist
;
1050 c
.lisp_eval_depth
= lisp_eval_depth
;
1051 c
.pdlcount
= SPECPDL_INDEX ();
1052 c
.poll_suppress_count
= poll_suppress_count
;
1053 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1054 c
.gcpro
= gcprolist
;
1055 c
.byte_stack
= byte_stack_list
;
1059 if (! sys_setjmp (c
.jmp
))
1060 c
.val
= (*func
) (arg
);
1062 /* Throw works by a longjmp that comes right here. */
1067 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1068 jump to that CATCH, returning VALUE as the value of that catch.
1070 This is the guts of Fthrow and Fsignal; they differ only in the way
1071 they choose the catch tag to throw to. A catch tag for a
1072 condition-case form has a TAG of Qnil.
1074 Before each catch is discarded, unbind all special bindings and
1075 execute all unwind-protect clauses made above that catch. Unwind
1076 the handler stack as we go, so that the proper handlers are in
1077 effect for each unwind-protect clause we run. At the end, restore
1078 some static info saved in CATCH, and longjmp to the location
1081 This is used for correct unwinding in Fthrow and Fsignal. */
1083 static _Noreturn
void
1084 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1088 /* Save the value in the tag. */
1091 /* Restore certain special C variables. */
1092 set_poll_suppress_count (catch->poll_suppress_count
);
1093 unblock_input_to (catch->interrupt_input_blocked
);
1098 last_time
= catchlist
== catch;
1100 /* Unwind the specpdl stack, and then restore the proper set of
1102 unbind_to (catchlist
->pdlcount
, Qnil
);
1103 handlerlist
= catchlist
->handlerlist
;
1104 catchlist
= catchlist
->next
;
1106 while (! last_time
);
1108 byte_stack_list
= catch->byte_stack
;
1109 gcprolist
= catch->gcpro
;
1111 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1113 backtrace_list
= catch->backlist
;
1114 lisp_eval_depth
= catch->lisp_eval_depth
;
1116 sys_longjmp (catch->jmp
, 1);
1119 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1120 doc
: /* Throw to the catch for TAG and return VALUE from it.
1121 Both TAG and VALUE are evalled. */)
1122 (register Lisp_Object tag
, Lisp_Object value
)
1124 register struct catchtag
*c
;
1127 for (c
= catchlist
; c
; c
= c
->next
)
1129 if (EQ (c
->tag
, tag
))
1130 unwind_to_catch (c
, value
);
1132 xsignal2 (Qno_catch
, tag
, value
);
1136 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1137 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1138 If BODYFORM completes normally, its value is returned
1139 after executing the UNWINDFORMS.
1140 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1141 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1145 ptrdiff_t count
= SPECPDL_INDEX ();
1147 record_unwind_protect (Fprogn
, Fcdr (args
));
1148 val
= eval_sub (Fcar (args
));
1149 return unbind_to (count
, val
);
1152 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1153 doc
: /* Regain control when an error is signaled.
1154 Executes BODYFORM and returns its value if no error happens.
1155 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1156 where the BODY is made of Lisp expressions.
1158 A handler is applicable to an error
1159 if CONDITION-NAME is one of the error's condition names.
1160 If an error happens, the first applicable handler is run.
1162 The car of a handler may be a list of condition names instead of a
1163 single condition name; then it handles all of them. If the special
1164 condition name `debug' is present in this list, it allows another
1165 condition in the list to run the debugger if `debug-on-error' and the
1166 other usual mechanisms says it should (otherwise, `condition-case'
1167 suppresses the debugger).
1169 When a handler handles an error, control returns to the `condition-case'
1170 and it executes the handler's BODY...
1171 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1172 \(If VAR is nil, the handler can't access that information.)
1173 Then the value of the last BODY form is returned from the `condition-case'
1176 See also the function `signal' for more info.
1177 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1180 Lisp_Object var
= Fcar (args
);
1181 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1182 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1184 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1187 /* Like Fcondition_case, but the args are separate
1188 rather than passed in a list. Used by Fbyte_code. */
1191 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1192 Lisp_Object handlers
)
1200 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1206 && (SYMBOLP (XCAR (tem
))
1207 || CONSP (XCAR (tem
))))))
1208 error ("Invalid condition handler: %s",
1209 SDATA (Fprin1_to_string (tem
, Qt
)));
1214 c
.backlist
= backtrace_list
;
1215 c
.handlerlist
= handlerlist
;
1216 c
.lisp_eval_depth
= lisp_eval_depth
;
1217 c
.pdlcount
= SPECPDL_INDEX ();
1218 c
.poll_suppress_count
= poll_suppress_count
;
1219 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1220 c
.gcpro
= gcprolist
;
1221 c
.byte_stack
= byte_stack_list
;
1222 if (sys_setjmp (c
.jmp
))
1225 specbind (h
.var
, c
.val
);
1226 val
= Fprogn (Fcdr (h
.chosen_clause
));
1228 /* Note that this just undoes the binding of h.var; whoever
1229 longjumped to us unwound the stack to c.pdlcount before
1231 unbind_to (c
.pdlcount
, Qnil
);
1238 h
.handler
= handlers
;
1239 h
.next
= handlerlist
;
1243 val
= eval_sub (bodyform
);
1245 handlerlist
= h
.next
;
1249 /* Call the function BFUN with no arguments, catching errors within it
1250 according to HANDLERS. If there is an error, call HFUN with
1251 one argument which is the data that describes the error:
1254 HANDLERS can be a list of conditions to catch.
1255 If HANDLERS is Qt, catch all errors.
1256 If HANDLERS is Qerror, catch all errors
1257 but allow the debugger to run if that is enabled. */
1260 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1261 Lisp_Object (*hfun
) (Lisp_Object
))
1269 c
.backlist
= backtrace_list
;
1270 c
.handlerlist
= handlerlist
;
1271 c
.lisp_eval_depth
= lisp_eval_depth
;
1272 c
.pdlcount
= SPECPDL_INDEX ();
1273 c
.poll_suppress_count
= poll_suppress_count
;
1274 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1275 c
.gcpro
= gcprolist
;
1276 c
.byte_stack
= byte_stack_list
;
1277 if (sys_setjmp (c
.jmp
))
1279 return (*hfun
) (c
.val
);
1283 h
.handler
= handlers
;
1285 h
.next
= handlerlist
;
1291 handlerlist
= h
.next
;
1295 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1298 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1299 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1307 c
.backlist
= backtrace_list
;
1308 c
.handlerlist
= handlerlist
;
1309 c
.lisp_eval_depth
= lisp_eval_depth
;
1310 c
.pdlcount
= SPECPDL_INDEX ();
1311 c
.poll_suppress_count
= poll_suppress_count
;
1312 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1313 c
.gcpro
= gcprolist
;
1314 c
.byte_stack
= byte_stack_list
;
1315 if (sys_setjmp (c
.jmp
))
1317 return (*hfun
) (c
.val
);
1321 h
.handler
= handlers
;
1323 h
.next
= handlerlist
;
1327 val
= (*bfun
) (arg
);
1329 handlerlist
= h
.next
;
1333 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1337 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1340 Lisp_Object handlers
,
1341 Lisp_Object (*hfun
) (Lisp_Object
))
1349 c
.backlist
= backtrace_list
;
1350 c
.handlerlist
= handlerlist
;
1351 c
.lisp_eval_depth
= lisp_eval_depth
;
1352 c
.pdlcount
= SPECPDL_INDEX ();
1353 c
.poll_suppress_count
= poll_suppress_count
;
1354 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1355 c
.gcpro
= gcprolist
;
1356 c
.byte_stack
= byte_stack_list
;
1357 if (sys_setjmp (c
.jmp
))
1359 return (*hfun
) (c
.val
);
1363 h
.handler
= handlers
;
1365 h
.next
= handlerlist
;
1369 val
= (*bfun
) (arg1
, arg2
);
1371 handlerlist
= h
.next
;
1375 /* Like internal_condition_case but call BFUN with NARGS as first,
1376 and ARGS as second argument. */
1379 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1382 Lisp_Object handlers
,
1383 Lisp_Object (*hfun
) (Lisp_Object err
,
1393 c
.backlist
= backtrace_list
;
1394 c
.handlerlist
= handlerlist
;
1395 c
.lisp_eval_depth
= lisp_eval_depth
;
1396 c
.pdlcount
= SPECPDL_INDEX ();
1397 c
.poll_suppress_count
= poll_suppress_count
;
1398 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1399 c
.gcpro
= gcprolist
;
1400 c
.byte_stack
= byte_stack_list
;
1401 if (sys_setjmp (c
.jmp
))
1403 return (*hfun
) (c
.val
, nargs
, args
);
1407 h
.handler
= handlers
;
1409 h
.next
= handlerlist
;
1413 val
= (*bfun
) (nargs
, args
);
1415 handlerlist
= h
.next
;
1420 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1421 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1425 process_quit_flag (void)
1427 Lisp_Object flag
= Vquit_flag
;
1429 if (EQ (flag
, Qkill_emacs
))
1431 if (EQ (Vthrow_on_input
, flag
))
1432 Fthrow (Vthrow_on_input
, Qt
);
1433 Fsignal (Qquit
, Qnil
);
1436 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1437 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1438 This function does not return.
1440 An error symbol is a symbol with an `error-conditions' property
1441 that is a list of condition names.
1442 A handler for any of those names will get to handle this signal.
1443 The symbol `error' should normally be one of them.
1445 DATA should be a list. Its elements are printed as part of the error message.
1446 See Info anchor `(elisp)Definition of signal' for some details on how this
1447 error message is constructed.
1448 If the signal is handled, DATA is made available to the handler.
1449 See also the function `condition-case'. */)
1450 (Lisp_Object error_symbol
, Lisp_Object data
)
1452 /* When memory is full, ERROR-SYMBOL is nil,
1453 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1454 That is a special case--don't do this in other situations. */
1455 Lisp_Object conditions
;
1457 Lisp_Object real_error_symbol
1458 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1459 register Lisp_Object clause
= Qnil
;
1461 struct backtrace
*bp
;
1465 if (gc_in_progress
|| waiting_for_input
)
1468 #if 0 /* rms: I don't know why this was here,
1469 but it is surely wrong for an error that is handled. */
1470 #ifdef HAVE_WINDOW_SYSTEM
1471 if (display_hourglass_p
)
1472 cancel_hourglass ();
1476 /* This hook is used by edebug. */
1477 if (! NILP (Vsignal_hook_function
)
1478 && ! NILP (error_symbol
))
1480 /* Edebug takes care of restoring these variables when it exits. */
1481 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1482 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1484 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1485 max_specpdl_size
= SPECPDL_INDEX () + 40;
1487 call2 (Vsignal_hook_function
, error_symbol
, data
);
1490 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1492 /* Remember from where signal was called. Skip over the frame for
1493 `signal' itself. If a frame for `error' follows, skip that,
1494 too. Don't do this when ERROR_SYMBOL is nil, because that
1495 is a memory-full error. */
1496 Vsignaling_function
= Qnil
;
1497 if (backtrace_list
&& !NILP (error_symbol
))
1499 bp
= backtrace_list
->next
;
1500 if (bp
&& EQ (bp
->function
, Qerror
))
1503 Vsignaling_function
= bp
->function
;
1506 for (h
= handlerlist
; h
; h
= h
->next
)
1508 clause
= find_handler_clause (h
->handler
, conditions
);
1513 if (/* Don't run the debugger for a memory-full error.
1514 (There is no room in memory to do that!) */
1515 !NILP (error_symbol
)
1516 && (!NILP (Vdebug_on_signal
)
1517 /* If no handler is present now, try to run the debugger. */
1519 /* A `debug' symbol in the handler list disables the normal
1520 suppression of the debugger. */
1521 || (CONSP (clause
) && CONSP (XCAR (clause
))
1522 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1523 /* Special handler that means "print a message and run debugger
1525 || EQ (h
->handler
, Qerror
)))
1527 bool debugger_called
1528 = maybe_call_debugger (conditions
, error_symbol
, data
);
1529 /* We can't return values to code which signaled an error, but we
1530 can continue code which has signaled a quit. */
1531 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1537 Lisp_Object unwind_data
1538 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1540 h
->chosen_clause
= clause
;
1541 unwind_to_catch (h
->tag
, unwind_data
);
1546 Fthrow (Qtop_level
, Qt
);
1549 if (! NILP (error_symbol
))
1550 data
= Fcons (error_symbol
, data
);
1552 string
= Ferror_message_string (data
);
1553 fatal ("%s", SDATA (string
));
1556 /* Internal version of Fsignal that never returns.
1557 Used for anything but Qquit (which can return from Fsignal). */
1560 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1562 Fsignal (error_symbol
, data
);
1566 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1569 xsignal0 (Lisp_Object error_symbol
)
1571 xsignal (error_symbol
, Qnil
);
1575 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1577 xsignal (error_symbol
, list1 (arg
));
1581 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1583 xsignal (error_symbol
, list2 (arg1
, arg2
));
1587 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1589 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1592 /* Signal `error' with message S, and additional arg ARG.
1593 If ARG is not a genuine list, make it a one-element list. */
1596 signal_error (const char *s
, Lisp_Object arg
)
1598 Lisp_Object tortoise
, hare
;
1600 hare
= tortoise
= arg
;
1601 while (CONSP (hare
))
1608 tortoise
= XCDR (tortoise
);
1610 if (EQ (hare
, tortoise
))
1615 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1617 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1621 /* Return true if LIST is a non-nil atom or
1622 a list containing one of CONDITIONS. */
1625 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1632 while (CONSP (conditions
))
1634 Lisp_Object
this, tail
;
1635 this = XCAR (conditions
);
1636 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1637 if (EQ (XCAR (tail
), this))
1639 conditions
= XCDR (conditions
);
1644 /* Return true if an error with condition-symbols CONDITIONS,
1645 and described by SIGNAL-DATA, should skip the debugger
1646 according to debugger-ignored-errors. */
1649 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1652 bool first_string
= 1;
1653 Lisp_Object error_message
;
1655 error_message
= Qnil
;
1656 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1658 if (STRINGP (XCAR (tail
)))
1662 error_message
= Ferror_message_string (data
);
1666 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1671 Lisp_Object contail
;
1673 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1674 if (EQ (XCAR (tail
), XCAR (contail
)))
1682 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1683 SIG and DATA describe the signal. There are two ways to pass them:
1684 = SIG is the error symbol, and DATA is the rest of the data.
1685 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1686 This is for memory-full errors only. */
1688 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1690 Lisp_Object combined_data
;
1692 combined_data
= Fcons (sig
, data
);
1695 /* Don't try to run the debugger with interrupts blocked.
1696 The editing loop would return anyway. */
1697 ! input_blocked_p ()
1698 && NILP (Vinhibit_debugger
)
1699 /* Does user want to enter debugger for this kind of error? */
1702 : wants_debugger (Vdebug_on_error
, conditions
))
1703 && ! skip_debugger (conditions
, combined_data
)
1704 /* RMS: What's this for? */
1705 && when_entered_debugger
< num_nonmacro_input_events
)
1707 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1715 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1717 register Lisp_Object h
;
1719 /* t is used by handlers for all conditions, set up by C code. */
1720 if (EQ (handlers
, Qt
))
1723 /* error is used similarly, but means print an error message
1724 and run the debugger if that is enabled. */
1725 if (EQ (handlers
, Qerror
))
1728 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1730 Lisp_Object handler
= XCAR (h
);
1731 Lisp_Object condit
, tem
;
1733 if (!CONSP (handler
))
1735 condit
= XCAR (handler
);
1736 /* Handle a single condition name in handler HANDLER. */
1737 if (SYMBOLP (condit
))
1739 tem
= Fmemq (Fcar (handler
), conditions
);
1743 /* Handle a list of condition names in handler HANDLER. */
1744 else if (CONSP (condit
))
1747 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1749 tem
= Fmemq (XCAR (tail
), conditions
);
1760 /* Dump an error message; called like vprintf. */
1762 verror (const char *m
, va_list ap
)
1765 ptrdiff_t size
= sizeof buf
;
1766 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1771 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1772 string
= make_string (buffer
, used
);
1776 xsignal1 (Qerror
, string
);
1780 /* Dump an error message; called like printf. */
1784 error (const char *m
, ...)
1792 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1793 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1794 This means it contains a description for how to read arguments to give it.
1795 The value is nil for an invalid function or a symbol with no function
1798 Interactively callable functions include strings and vectors (treated
1799 as keyboard macros), lambda-expressions that contain a top-level call
1800 to `interactive', autoload definitions made by `autoload' with non-nil
1801 fourth argument, and some of the built-in functions of Lisp.
1803 Also, a symbol satisfies `commandp' if its function definition does so.
1805 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1806 then strings and vectors are not accepted. */)
1807 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1809 register Lisp_Object fun
;
1810 register Lisp_Object funcar
;
1811 Lisp_Object if_prop
= Qnil
;
1815 fun
= indirect_function (fun
); /* Check cycles. */
1816 if (NILP (fun
) || EQ (fun
, Qunbound
))
1819 /* Check an `interactive-form' property if present, analogous to the
1820 function-documentation property. */
1822 while (SYMBOLP (fun
))
1824 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1827 fun
= Fsymbol_function (fun
);
1830 /* Emacs primitives are interactive if their DEFUN specifies an
1831 interactive spec. */
1833 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1835 /* Bytecode objects are interactive if they are long enough to
1836 have an element whose index is COMPILED_INTERACTIVE, which is
1837 where the interactive spec is stored. */
1838 else if (COMPILEDP (fun
))
1839 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1842 /* Strings and vectors are keyboard macros. */
1843 if (STRINGP (fun
) || VECTORP (fun
))
1844 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1846 /* Lists may represent commands. */
1849 funcar
= XCAR (fun
);
1850 if (EQ (funcar
, Qclosure
))
1851 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1853 else if (EQ (funcar
, Qlambda
))
1854 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1855 else if (EQ (funcar
, Qautoload
))
1856 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1861 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1862 doc
: /* Define FUNCTION to autoload from FILE.
1863 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1864 Third arg DOCSTRING is documentation for the function.
1865 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1866 Fifth arg TYPE indicates the type of the object:
1867 nil or omitted says FUNCTION is a function,
1868 `keymap' says FUNCTION is really a keymap, and
1869 `macro' or t says FUNCTION is really a macro.
1870 Third through fifth args give info about the real definition.
1871 They default to nil.
1872 If FUNCTION is already defined other than as an autoload,
1873 this does nothing and returns nil. */)
1874 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1876 CHECK_SYMBOL (function
);
1877 CHECK_STRING (file
);
1879 /* If function is defined and not as an autoload, don't override. */
1880 if ((CONSP (XSYMBOL (function
)->function
)
1881 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1882 /* Remember that the function was already an autoload. */
1883 LOADHIST_ATTACH (Fcons (Qt
, function
));
1884 else if (!EQ (XSYMBOL (function
)->function
, Qunbound
))
1887 if (NILP (Vpurify_flag
))
1888 /* Only add entries after dumping, because the ones before are
1889 not useful and else we get loads of them from the loaddefs.el. */
1890 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1891 else if (EQ (docstring
, make_number (0)))
1892 /* `read1' in lread.c has found the docstring starting with "\
1893 and assumed the docstring will be provided by Snarf-documentation, so it
1894 passed us 0 instead. But that leads to accidental sharing in purecopy's
1895 hash-consing, so we use a (hopefully) unique integer instead. */
1896 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1897 return Ffset (function
,
1898 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1899 interactive
, type
)));
1903 un_autoload (Lisp_Object oldqueue
)
1905 register Lisp_Object queue
, first
, second
;
1907 /* Queue to unwind is current value of Vautoload_queue.
1908 oldqueue is the shadowed value to leave in Vautoload_queue. */
1909 queue
= Vautoload_queue
;
1910 Vautoload_queue
= oldqueue
;
1911 while (CONSP (queue
))
1913 first
= XCAR (queue
);
1914 second
= Fcdr (first
);
1915 first
= Fcar (first
);
1916 if (EQ (first
, make_number (0)))
1919 Ffset (first
, second
);
1920 queue
= XCDR (queue
);
1925 /* Load an autoloaded function.
1926 FUNNAME is the symbol which is the function's name.
1927 FUNDEF is the autoload definition (a list). */
1929 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1930 doc
: /* Load FUNDEF which should be an autoload.
1931 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1932 in which case the function returns the new autoloaded function value.
1933 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1934 it is defines a macro. */)
1935 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1937 ptrdiff_t count
= SPECPDL_INDEX ();
1938 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1940 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1943 if (EQ (macro_only
, Qmacro
))
1945 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1946 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1950 /* This is to make sure that loadup.el gives a clear picture
1951 of what files are preloaded and when. */
1952 if (! NILP (Vpurify_flag
))
1953 error ("Attempt to autoload %s while preparing to dump",
1954 SDATA (SYMBOL_NAME (funname
)));
1956 CHECK_SYMBOL (funname
);
1957 GCPRO3 (funname
, fundef
, macro_only
);
1959 /* Preserve the match data. */
1960 record_unwind_save_match_data ();
1962 /* If autoloading gets an error (which includes the error of failing
1963 to define the function being called), we use Vautoload_queue
1964 to undo function definitions and `provide' calls made by
1965 the function. We do this in the specific case of autoloading
1966 because autoloading is not an explicit request "load this file",
1967 but rather a request to "call this function".
1969 The value saved here is to be restored into Vautoload_queue. */
1970 record_unwind_protect (un_autoload
, Vautoload_queue
);
1971 Vautoload_queue
= Qt
;
1972 /* If `macro_only', assume this autoload to be a "best-effort",
1973 so don't signal an error if autoloading fails. */
1974 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
1976 /* Once loading finishes, don't undo it. */
1977 Vautoload_queue
= Qt
;
1978 unbind_to (count
, Qnil
);
1986 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
1988 if (!NILP (Fequal (fun
, fundef
)))
1989 error ("Autoloading failed to define function %s",
1990 SDATA (SYMBOL_NAME (funname
)));
1997 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
1998 doc
: /* Evaluate FORM and return its value.
1999 If LEXICAL is t, evaluate using lexical scoping. */)
2000 (Lisp_Object form
, Lisp_Object lexical
)
2002 ptrdiff_t count
= SPECPDL_INDEX ();
2003 specbind (Qinternal_interpreter_environment
,
2004 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2005 return unbind_to (count
, eval_sub (form
));
2008 /* Eval a sub-expression of the current expression (i.e. in the same
2011 eval_sub (Lisp_Object form
)
2013 Lisp_Object fun
, val
, original_fun
, original_args
;
2015 struct backtrace backtrace
;
2016 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2020 /* Look up its binding in the lexical environment.
2021 We do not pay attention to the declared_special flag here, since we
2022 already did that when let-binding the variable. */
2023 Lisp_Object lex_binding
2024 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2025 ? Fassq (form
, Vinternal_interpreter_environment
)
2027 if (CONSP (lex_binding
))
2028 return XCDR (lex_binding
);
2030 return Fsymbol_value (form
);
2039 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2041 if (max_lisp_eval_depth
< 100)
2042 max_lisp_eval_depth
= 100;
2043 if (lisp_eval_depth
> max_lisp_eval_depth
)
2044 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2047 original_fun
= XCAR (form
);
2048 original_args
= XCDR (form
);
2050 backtrace
.next
= backtrace_list
;
2051 backtrace
.function
= original_fun
; /* This also protects them from gc. */
2052 backtrace
.args
= &original_args
;
2053 backtrace
.nargs
= UNEVALLED
;
2054 backtrace
.debug_on_exit
= 0;
2055 backtrace_list
= &backtrace
;
2057 if (debug_on_next_call
)
2058 do_debug_on_call (Qt
);
2060 /* At this point, only original_fun and original_args
2061 have values that will be used below. */
2064 /* Optimize for no indirection. */
2066 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2067 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2068 fun
= indirect_function (fun
);
2072 Lisp_Object numargs
;
2073 Lisp_Object argvals
[8];
2074 Lisp_Object args_left
;
2075 register int i
, maxargs
;
2077 args_left
= original_args
;
2078 numargs
= Flength (args_left
);
2082 if (XINT (numargs
) < XSUBR (fun
)->min_args
2083 || (XSUBR (fun
)->max_args
>= 0
2084 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2085 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2087 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2088 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2089 else if (XSUBR (fun
)->max_args
== MANY
)
2091 /* Pass a vector of evaluated arguments. */
2093 ptrdiff_t argnum
= 0;
2096 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2098 GCPRO3 (args_left
, fun
, fun
);
2102 while (!NILP (args_left
))
2104 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2105 args_left
= Fcdr (args_left
);
2106 gcpro3
.nvars
= argnum
;
2109 backtrace
.args
= vals
;
2110 backtrace
.nargs
= XINT (numargs
);
2112 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2118 GCPRO3 (args_left
, fun
, fun
);
2119 gcpro3
.var
= argvals
;
2122 maxargs
= XSUBR (fun
)->max_args
;
2123 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2125 argvals
[i
] = eval_sub (Fcar (args_left
));
2131 backtrace
.args
= argvals
;
2132 backtrace
.nargs
= XINT (numargs
);
2137 val
= (XSUBR (fun
)->function
.a0 ());
2140 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2143 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2146 val
= (XSUBR (fun
)->function
.a3
2147 (argvals
[0], argvals
[1], argvals
[2]));
2150 val
= (XSUBR (fun
)->function
.a4
2151 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2154 val
= (XSUBR (fun
)->function
.a5
2155 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2159 val
= (XSUBR (fun
)->function
.a6
2160 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2161 argvals
[4], argvals
[5]));
2164 val
= (XSUBR (fun
)->function
.a7
2165 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2166 argvals
[4], argvals
[5], argvals
[6]));
2170 val
= (XSUBR (fun
)->function
.a8
2171 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2172 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2176 /* Someone has created a subr that takes more arguments than
2177 is supported by this code. We need to either rewrite the
2178 subr to use a different argument protocol, or add more
2179 cases to this switch. */
2184 else if (COMPILEDP (fun
))
2185 val
= apply_lambda (fun
, original_args
);
2188 if (EQ (fun
, Qunbound
))
2189 xsignal1 (Qvoid_function
, original_fun
);
2191 xsignal1 (Qinvalid_function
, original_fun
);
2192 funcar
= XCAR (fun
);
2193 if (!SYMBOLP (funcar
))
2194 xsignal1 (Qinvalid_function
, original_fun
);
2195 if (EQ (funcar
, Qautoload
))
2197 Fautoload_do_load (fun
, original_fun
, Qnil
);
2200 if (EQ (funcar
, Qmacro
))
2202 ptrdiff_t count
= SPECPDL_INDEX ();
2204 /* Bind lexical-binding during expansion of the macro, so the
2205 macro can know reliably if the code it outputs will be
2206 interpreted using lexical-binding or not. */
2207 specbind (Qlexical_binding
,
2208 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2209 exp
= apply1 (Fcdr (fun
), original_args
);
2210 unbind_to (count
, Qnil
);
2211 val
= eval_sub (exp
);
2213 else if (EQ (funcar
, Qlambda
)
2214 || EQ (funcar
, Qclosure
))
2215 val
= apply_lambda (fun
, original_args
);
2217 xsignal1 (Qinvalid_function
, original_fun
);
2222 if (backtrace
.debug_on_exit
)
2223 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2224 backtrace_list
= backtrace
.next
;
2229 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2230 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2231 Then return the value FUNCTION returns.
2232 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2233 usage: (apply FUNCTION &rest ARGUMENTS) */)
2234 (ptrdiff_t nargs
, Lisp_Object
*args
)
2238 register Lisp_Object spread_arg
;
2239 register Lisp_Object
*funcall_args
;
2240 Lisp_Object fun
, retval
;
2241 struct gcpro gcpro1
;
2246 spread_arg
= args
[nargs
- 1];
2247 CHECK_LIST (spread_arg
);
2249 numargs
= XINT (Flength (spread_arg
));
2252 return Ffuncall (nargs
- 1, args
);
2253 else if (numargs
== 1)
2255 args
[nargs
- 1] = XCAR (spread_arg
);
2256 return Ffuncall (nargs
, args
);
2259 numargs
+= nargs
- 2;
2261 /* Optimize for no indirection. */
2262 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2263 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2264 fun
= indirect_function (fun
);
2265 if (EQ (fun
, Qunbound
))
2267 /* Let funcall get the error. */
2274 if (numargs
< XSUBR (fun
)->min_args
2275 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2276 goto funcall
; /* Let funcall get the error. */
2277 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2279 /* Avoid making funcall cons up a yet another new vector of arguments
2280 by explicitly supplying nil's for optional values. */
2281 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2282 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2283 funcall_args
[++i
] = Qnil
;
2284 GCPRO1 (*funcall_args
);
2285 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2289 /* We add 1 to numargs because funcall_args includes the
2290 function itself as well as its arguments. */
2293 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2294 GCPRO1 (*funcall_args
);
2295 gcpro1
.nvars
= 1 + numargs
;
2298 memcpy (funcall_args
, args
, nargs
* word_size
);
2299 /* Spread the last arg we got. Its first element goes in
2300 the slot that it used to occupy, hence this value of I. */
2302 while (!NILP (spread_arg
))
2304 funcall_args
[i
++] = XCAR (spread_arg
);
2305 spread_arg
= XCDR (spread_arg
);
2308 /* By convention, the caller needs to gcpro Ffuncall's args. */
2309 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2316 /* Run hook variables in various ways. */
2319 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2321 Ffuncall (nargs
, args
);
2325 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2326 doc
: /* Run each hook in HOOKS.
2327 Each argument should be a symbol, a hook variable.
2328 These symbols are processed in the order specified.
2329 If a hook symbol has a non-nil value, that value may be a function
2330 or a list of functions to be called to run the hook.
2331 If the value is a function, it is called with no arguments.
2332 If it is a list, the elements are called, in order, with no arguments.
2334 Major modes should not use this function directly to run their mode
2335 hook; they should use `run-mode-hooks' instead.
2337 Do not use `make-local-variable' to make a hook variable buffer-local.
2338 Instead, use `add-hook' and specify t for the LOCAL argument.
2339 usage: (run-hooks &rest HOOKS) */)
2340 (ptrdiff_t nargs
, Lisp_Object
*args
)
2342 Lisp_Object hook
[1];
2345 for (i
= 0; i
< nargs
; i
++)
2348 run_hook_with_args (1, hook
, funcall_nil
);
2354 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2355 Srun_hook_with_args
, 1, MANY
, 0,
2356 doc
: /* Run HOOK with the specified arguments ARGS.
2357 HOOK should be a symbol, a hook variable. The value of HOOK
2358 may be nil, a function, or a list of functions. Call each
2359 function in order with arguments ARGS. The final return value
2362 Do not use `make-local-variable' to make a hook variable buffer-local.
2363 Instead, use `add-hook' and specify t for the LOCAL argument.
2364 usage: (run-hook-with-args HOOK &rest ARGS) */)
2365 (ptrdiff_t nargs
, Lisp_Object
*args
)
2367 return run_hook_with_args (nargs
, args
, funcall_nil
);
2370 /* NB this one still documents a specific non-nil return value.
2371 (As did run-hook-with-args and run-hook-with-args-until-failure
2372 until they were changed in 24.1.) */
2373 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2374 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2375 doc
: /* Run HOOK with the specified arguments ARGS.
2376 HOOK should be a symbol, a hook variable. The value of HOOK
2377 may be nil, a function, or a list of functions. Call each
2378 function in order with arguments ARGS, stopping at the first
2379 one that returns non-nil, and return that value. Otherwise (if
2380 all functions return nil, or if there are no functions to call),
2383 Do not use `make-local-variable' to make a hook variable buffer-local.
2384 Instead, use `add-hook' and specify t for the LOCAL argument.
2385 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2386 (ptrdiff_t nargs
, Lisp_Object
*args
)
2388 return run_hook_with_args (nargs
, args
, Ffuncall
);
2392 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2394 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2397 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2398 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2399 doc
: /* Run HOOK with the specified arguments ARGS.
2400 HOOK should be a symbol, a hook variable. The value of HOOK
2401 may be nil, a function, or a list of functions. Call each
2402 function in order with arguments ARGS, stopping at the first
2403 one that returns nil, and return nil. Otherwise (if all functions
2404 return non-nil, or if there are no functions to call), return non-nil
2405 \(do not rely on the precise return value in this case).
2407 Do not use `make-local-variable' to make a hook variable buffer-local.
2408 Instead, use `add-hook' and specify t for the LOCAL argument.
2409 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2410 (ptrdiff_t nargs
, Lisp_Object
*args
)
2412 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2416 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2418 Lisp_Object tmp
= args
[0], ret
;
2421 ret
= Ffuncall (nargs
, args
);
2427 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2428 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2429 I.e. instead of calling each function FUN directly with arguments ARGS,
2430 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2431 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2432 aborts and returns that value.
2433 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2434 (ptrdiff_t nargs
, Lisp_Object
*args
)
2436 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2439 /* ARGS[0] should be a hook symbol.
2440 Call each of the functions in the hook value, passing each of them
2441 as arguments all the rest of ARGS (all NARGS - 1 elements).
2442 FUNCALL specifies how to call each function on the hook.
2443 The caller (or its caller, etc) must gcpro all of ARGS,
2444 except that it isn't necessary to gcpro ARGS[0]. */
2447 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2448 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2450 Lisp_Object sym
, val
, ret
= Qnil
;
2451 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2453 /* If we are dying or still initializing,
2454 don't do anything--it would probably crash if we tried. */
2455 if (NILP (Vrun_hooks
))
2459 val
= find_symbol_value (sym
);
2461 if (EQ (val
, Qunbound
) || NILP (val
))
2463 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2466 return funcall (nargs
, args
);
2470 Lisp_Object global_vals
= Qnil
;
2471 GCPRO3 (sym
, val
, global_vals
);
2474 CONSP (val
) && NILP (ret
);
2477 if (EQ (XCAR (val
), Qt
))
2479 /* t indicates this hook has a local binding;
2480 it means to run the global binding too. */
2481 global_vals
= Fdefault_value (sym
);
2482 if (NILP (global_vals
)) continue;
2484 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2486 args
[0] = global_vals
;
2487 ret
= funcall (nargs
, args
);
2492 CONSP (global_vals
) && NILP (ret
);
2493 global_vals
= XCDR (global_vals
))
2495 args
[0] = XCAR (global_vals
);
2496 /* In a global value, t should not occur. If it does, we
2497 must ignore it to avoid an endless loop. */
2498 if (!EQ (args
[0], Qt
))
2499 ret
= funcall (nargs
, args
);
2505 args
[0] = XCAR (val
);
2506 ret
= funcall (nargs
, args
);
2515 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2518 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2520 Lisp_Object temp
[3];
2525 Frun_hook_with_args (3, temp
);
2528 /* Apply fn to arg. */
2530 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2532 struct gcpro gcpro1
;
2536 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2539 Lisp_Object args
[2];
2543 RETURN_UNGCPRO (Fapply (2, args
));
2547 /* Call function fn on no arguments. */
2549 call0 (Lisp_Object fn
)
2551 struct gcpro gcpro1
;
2554 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2557 /* Call function fn with 1 argument arg1. */
2560 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2562 struct gcpro gcpro1
;
2563 Lisp_Object args
[2];
2569 RETURN_UNGCPRO (Ffuncall (2, args
));
2572 /* Call function fn with 2 arguments arg1, arg2. */
2575 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2577 struct gcpro gcpro1
;
2578 Lisp_Object args
[3];
2584 RETURN_UNGCPRO (Ffuncall (3, args
));
2587 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2590 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2592 struct gcpro gcpro1
;
2593 Lisp_Object args
[4];
2600 RETURN_UNGCPRO (Ffuncall (4, args
));
2603 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2606 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2609 struct gcpro gcpro1
;
2610 Lisp_Object args
[5];
2618 RETURN_UNGCPRO (Ffuncall (5, args
));
2621 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2624 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2625 Lisp_Object arg4
, Lisp_Object arg5
)
2627 struct gcpro gcpro1
;
2628 Lisp_Object args
[6];
2637 RETURN_UNGCPRO (Ffuncall (6, args
));
2640 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2643 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2644 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2646 struct gcpro gcpro1
;
2647 Lisp_Object args
[7];
2657 RETURN_UNGCPRO (Ffuncall (7, args
));
2660 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2663 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2664 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2666 struct gcpro gcpro1
;
2667 Lisp_Object args
[8];
2678 RETURN_UNGCPRO (Ffuncall (8, args
));
2681 /* The caller should GCPRO all the elements of ARGS. */
2683 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2684 doc
: /* Non-nil if OBJECT is a function. */)
2685 (Lisp_Object object
)
2687 if (FUNCTIONP (object
))
2692 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2693 doc
: /* Call first argument as a function, passing remaining arguments to it.
2694 Return the value that function returns.
2695 Thus, (funcall 'cons 'x 'y) returns (x . y).
2696 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2697 (ptrdiff_t nargs
, Lisp_Object
*args
)
2699 Lisp_Object fun
, original_fun
;
2701 ptrdiff_t numargs
= nargs
- 1;
2702 Lisp_Object lisp_numargs
;
2704 struct backtrace backtrace
;
2705 register Lisp_Object
*internal_args
;
2710 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2712 if (max_lisp_eval_depth
< 100)
2713 max_lisp_eval_depth
= 100;
2714 if (lisp_eval_depth
> max_lisp_eval_depth
)
2715 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2718 backtrace
.next
= backtrace_list
;
2719 backtrace
.function
= args
[0];
2720 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2721 backtrace
.nargs
= nargs
- 1;
2722 backtrace
.debug_on_exit
= 0;
2723 backtrace_list
= &backtrace
;
2725 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2728 if (debug_on_next_call
)
2729 do_debug_on_call (Qlambda
);
2733 original_fun
= args
[0];
2737 /* Optimize for no indirection. */
2739 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2740 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2741 fun
= indirect_function (fun
);
2745 if (numargs
< XSUBR (fun
)->min_args
2746 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2748 XSETFASTINT (lisp_numargs
, numargs
);
2749 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2752 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2753 xsignal1 (Qinvalid_function
, original_fun
);
2755 else if (XSUBR (fun
)->max_args
== MANY
)
2756 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2759 if (XSUBR (fun
)->max_args
> numargs
)
2761 internal_args
= alloca (XSUBR (fun
)->max_args
2762 * sizeof *internal_args
);
2763 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2764 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2765 internal_args
[i
] = Qnil
;
2768 internal_args
= args
+ 1;
2769 switch (XSUBR (fun
)->max_args
)
2772 val
= (XSUBR (fun
)->function
.a0 ());
2775 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2778 val
= (XSUBR (fun
)->function
.a2
2779 (internal_args
[0], internal_args
[1]));
2782 val
= (XSUBR (fun
)->function
.a3
2783 (internal_args
[0], internal_args
[1], internal_args
[2]));
2786 val
= (XSUBR (fun
)->function
.a4
2787 (internal_args
[0], internal_args
[1], internal_args
[2],
2791 val
= (XSUBR (fun
)->function
.a5
2792 (internal_args
[0], internal_args
[1], internal_args
[2],
2793 internal_args
[3], internal_args
[4]));
2796 val
= (XSUBR (fun
)->function
.a6
2797 (internal_args
[0], internal_args
[1], internal_args
[2],
2798 internal_args
[3], internal_args
[4], internal_args
[5]));
2801 val
= (XSUBR (fun
)->function
.a7
2802 (internal_args
[0], internal_args
[1], internal_args
[2],
2803 internal_args
[3], internal_args
[4], internal_args
[5],
2808 val
= (XSUBR (fun
)->function
.a8
2809 (internal_args
[0], internal_args
[1], internal_args
[2],
2810 internal_args
[3], internal_args
[4], internal_args
[5],
2811 internal_args
[6], internal_args
[7]));
2816 /* If a subr takes more than 8 arguments without using MANY
2817 or UNEVALLED, we need to extend this function to support it.
2818 Until this is done, there is no way to call the function. */
2823 else if (COMPILEDP (fun
))
2824 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2827 if (EQ (fun
, Qunbound
))
2828 xsignal1 (Qvoid_function
, original_fun
);
2830 xsignal1 (Qinvalid_function
, original_fun
);
2831 funcar
= XCAR (fun
);
2832 if (!SYMBOLP (funcar
))
2833 xsignal1 (Qinvalid_function
, original_fun
);
2834 if (EQ (funcar
, Qlambda
)
2835 || EQ (funcar
, Qclosure
))
2836 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2837 else if (EQ (funcar
, Qautoload
))
2839 Fautoload_do_load (fun
, original_fun
, Qnil
);
2844 xsignal1 (Qinvalid_function
, original_fun
);
2848 if (backtrace
.debug_on_exit
)
2849 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2850 backtrace_list
= backtrace
.next
;
2855 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2857 Lisp_Object args_left
;
2860 register Lisp_Object
*arg_vector
;
2861 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2862 register Lisp_Object tem
;
2865 numargs
= XFASTINT (Flength (args
));
2866 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2869 GCPRO3 (*arg_vector
, args_left
, fun
);
2872 for (i
= 0; i
< numargs
; )
2874 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2875 tem
= eval_sub (tem
);
2876 arg_vector
[i
++] = tem
;
2882 backtrace_list
->args
= arg_vector
;
2883 backtrace_list
->nargs
= i
;
2884 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2886 /* Do the debug-on-exit now, while arg_vector still exists. */
2887 if (backtrace_list
->debug_on_exit
)
2888 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2889 /* Don't do it again when we return to eval. */
2890 backtrace_list
->debug_on_exit
= 0;
2895 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2896 and return the result of evaluation.
2897 FUN must be either a lambda-expression or a compiled-code object. */
2900 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2901 register Lisp_Object
*arg_vector
)
2903 Lisp_Object val
, syms_left
, next
, lexenv
;
2904 ptrdiff_t count
= SPECPDL_INDEX ();
2906 bool optional
, rest
;
2910 if (EQ (XCAR (fun
), Qclosure
))
2912 fun
= XCDR (fun
); /* Drop `closure'. */
2913 lexenv
= XCAR (fun
);
2914 CHECK_LIST_CONS (fun
, fun
);
2918 syms_left
= XCDR (fun
);
2919 if (CONSP (syms_left
))
2920 syms_left
= XCAR (syms_left
);
2922 xsignal1 (Qinvalid_function
, fun
);
2924 else if (COMPILEDP (fun
))
2926 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2927 if (INTEGERP (syms_left
))
2928 /* A byte-code object with a non-nil `push args' slot means we
2929 shouldn't bind any arguments, instead just call the byte-code
2930 interpreter directly; it will push arguments as necessary.
2932 Byte-code objects with either a non-existent, or a nil value for
2933 the `push args' slot (the default), have dynamically-bound
2934 arguments, and use the argument-binding code below instead (as do
2935 all interpreted functions, even lexically bound ones). */
2937 /* If we have not actually read the bytecode string
2938 and constants vector yet, fetch them from the file. */
2939 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2940 Ffetch_bytecode (fun
);
2941 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2942 AREF (fun
, COMPILED_CONSTANTS
),
2943 AREF (fun
, COMPILED_STACK_DEPTH
),
2952 i
= optional
= rest
= 0;
2953 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2957 next
= XCAR (syms_left
);
2958 if (!SYMBOLP (next
))
2959 xsignal1 (Qinvalid_function
, fun
);
2961 if (EQ (next
, Qand_rest
))
2963 else if (EQ (next
, Qand_optional
))
2970 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2974 arg
= arg_vector
[i
++];
2976 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2980 /* Bind the argument. */
2981 if (!NILP (lexenv
) && SYMBOLP (next
))
2982 /* Lexically bind NEXT by adding it to the lexenv alist. */
2983 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
2985 /* Dynamically bind NEXT. */
2986 specbind (next
, arg
);
2990 if (!NILP (syms_left
))
2991 xsignal1 (Qinvalid_function
, fun
);
2993 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
2995 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
2996 /* Instantiate a new lexical environment. */
2997 specbind (Qinternal_interpreter_environment
, lexenv
);
3000 val
= Fprogn (XCDR (XCDR (fun
)));
3003 /* If we have not actually read the bytecode string
3004 and constants vector yet, fetch them from the file. */
3005 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3006 Ffetch_bytecode (fun
);
3007 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3008 AREF (fun
, COMPILED_CONSTANTS
),
3009 AREF (fun
, COMPILED_STACK_DEPTH
),
3013 return unbind_to (count
, val
);
3016 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3018 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3019 (Lisp_Object object
)
3023 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3025 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3028 tem
= AREF (object
, COMPILED_BYTECODE
);
3029 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3030 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3032 error ("Invalid byte code");
3034 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3035 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3043 register ptrdiff_t count
= SPECPDL_INDEX ();
3044 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3045 if (max_size
<= specpdl_size
)
3047 if (max_specpdl_size
< 400)
3048 max_size
= max_specpdl_size
= 400;
3049 if (max_size
<= specpdl_size
)
3050 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3052 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3053 specpdl_ptr
= specpdl
+ count
;
3056 /* `specpdl_ptr->symbol' is a field which describes which variable is
3057 let-bound, so it can be properly undone when we unbind_to.
3058 It can have the following two shapes:
3059 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3060 a symbol that is not buffer-local (at least at the time
3061 the let binding started). Note also that it should not be
3062 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3064 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3065 variable SYMBOL which can be buffer-local. WHERE tells us
3066 which buffer is affected (or nil if the let-binding affects the
3067 global value of the variable) and BUFFER tells us which buffer was
3068 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3069 BUFFER did not yet have a buffer-local value). */
3072 specbind (Lisp_Object symbol
, Lisp_Object value
)
3074 struct Lisp_Symbol
*sym
;
3076 CHECK_SYMBOL (symbol
);
3077 sym
= XSYMBOL (symbol
);
3078 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3082 switch (sym
->redirect
)
3084 case SYMBOL_VARALIAS
:
3085 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3086 case SYMBOL_PLAINVAL
:
3087 /* The most common case is that of a non-constant symbol with a
3088 trivial value. Make that as fast as we can. */
3089 set_specpdl_symbol (symbol
);
3090 set_specpdl_old_value (SYMBOL_VAL (sym
));
3091 specpdl_ptr
->func
= NULL
;
3094 SET_SYMBOL_VAL (sym
, value
);
3096 set_internal (symbol
, value
, Qnil
, 1);
3098 case SYMBOL_LOCALIZED
:
3099 if (SYMBOL_BLV (sym
)->frame_local
)
3100 error ("Frame-local vars cannot be let-bound");
3101 case SYMBOL_FORWARDED
:
3103 Lisp_Object ovalue
= find_symbol_value (symbol
);
3104 specpdl_ptr
->func
= 0;
3105 set_specpdl_old_value (ovalue
);
3107 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3108 || (EQ (SYMBOL_BLV (sym
)->where
,
3109 SYMBOL_BLV (sym
)->frame_local
?
3110 Fselected_frame () : Fcurrent_buffer ())));
3112 if (sym
->redirect
== SYMBOL_LOCALIZED
3113 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3115 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3117 /* For a local variable, record both the symbol and which
3118 buffer's or frame's value we are saving. */
3119 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3121 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3122 || (blv_found (SYMBOL_BLV (sym
))
3123 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3126 else if (sym
->redirect
== SYMBOL_LOCALIZED
3127 && blv_found (SYMBOL_BLV (sym
)))
3128 where
= SYMBOL_BLV (sym
)->where
;
3132 /* We're not using the `unused' slot in the specbinding
3133 structure because this would mean we have to do more
3134 work for simple variables. */
3135 /* FIXME: The third value `current_buffer' is only used in
3136 let_shadows_buffer_binding_p which is itself only used
3137 in set_internal for local_if_set. */
3138 eassert (NILP (where
) || EQ (where
, cur_buf
));
3139 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3141 /* If SYMBOL is a per-buffer variable which doesn't have a
3142 buffer-local value here, make the `let' change the global
3143 value by changing the value of SYMBOL in all buffers not
3144 having their own value. This is consistent with what
3145 happens with other buffer-local variables. */
3147 && sym
->redirect
== SYMBOL_FORWARDED
)
3149 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3151 Fset_default (symbol
, value
);
3156 set_specpdl_symbol (symbol
);
3159 set_internal (symbol
, value
, Qnil
, 1);
3162 default: emacs_abort ();
3167 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3169 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3171 specpdl_ptr
->func
= function
;
3172 set_specpdl_symbol (Qnil
);
3173 set_specpdl_old_value (arg
);
3178 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3180 Lisp_Object quitf
= Vquit_flag
;
3181 struct gcpro gcpro1
, gcpro2
;
3183 GCPRO2 (value
, quitf
);
3186 while (specpdl_ptr
!= specpdl
+ count
)
3188 /* Copy the binding, and decrement specpdl_ptr, before we do
3189 the work to unbind it. We decrement first
3190 so that an error in unbinding won't try to unbind
3191 the same entry again, and we copy the binding first
3192 in case more bindings are made during some of the code we run. */
3194 struct specbinding this_binding
;
3195 this_binding
= *--specpdl_ptr
;
3197 if (this_binding
.func
!= 0)
3198 (*this_binding
.func
) (this_binding
.old_value
);
3199 /* If the symbol is a list, it is really (SYMBOL WHERE
3200 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3201 frame. If WHERE is a buffer or frame, this indicates we
3202 bound a variable that had a buffer-local or frame-local
3203 binding. WHERE nil means that the variable had the default
3204 value when it was bound. CURRENT-BUFFER is the buffer that
3205 was current when the variable was bound. */
3206 else if (CONSP (this_binding
.symbol
))
3208 Lisp_Object symbol
, where
;
3210 symbol
= XCAR (this_binding
.symbol
);
3211 where
= XCAR (XCDR (this_binding
.symbol
));
3214 Fset_default (symbol
, this_binding
.old_value
);
3215 /* If `where' is non-nil, reset the value in the appropriate
3216 local binding, but only if that binding still exists. */
3217 else if (BUFFERP (where
)
3218 ? !NILP (Flocal_variable_p (symbol
, where
))
3219 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3220 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3222 /* If variable has a trivial value (no forwarding), we can
3223 just set it. No need to check for constant symbols here,
3224 since that was already done by specbind. */
3225 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3226 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3227 this_binding
.old_value
);
3229 /* NOTE: we only ever come here if make_local_foo was used for
3230 the first time on this var within this let. */
3231 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3234 if (NILP (Vquit_flag
) && !NILP (quitf
))
3241 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3242 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3243 A special variable is one that will be bound dynamically, even in a
3244 context where binding is lexical by default. */)
3245 (Lisp_Object symbol
)
3247 CHECK_SYMBOL (symbol
);
3248 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3252 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3253 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3254 The debugger is entered when that frame exits, if the flag is non-nil. */)
3255 (Lisp_Object level
, Lisp_Object flag
)
3257 register struct backtrace
*backlist
= backtrace_list
;
3258 register EMACS_INT i
;
3260 CHECK_NUMBER (level
);
3262 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3264 backlist
= backlist
->next
;
3268 backlist
->debug_on_exit
= !NILP (flag
);
3273 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3274 doc
: /* Print a trace of Lisp function calls currently active.
3275 Output stream used is value of `standard-output'. */)
3278 register struct backtrace
*backlist
= backtrace_list
;
3281 struct gcpro gcpro1
;
3282 Lisp_Object old_print_level
= Vprint_level
;
3284 if (NILP (Vprint_level
))
3285 XSETFASTINT (Vprint_level
, 8);
3292 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3293 if (backlist
->nargs
== UNEVALLED
)
3295 Fprin1 (Fcons (backlist
->function
, *backlist
->args
), Qnil
);
3296 write_string ("\n", -1);
3300 tem
= backlist
->function
;
3301 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3302 write_string ("(", -1);
3303 if (backlist
->nargs
== MANY
)
3304 { /* FIXME: Can this happen? */
3306 for (tail
= *backlist
->args
; !NILP (tail
); tail
= Fcdr (tail
))
3309 write_string (" ", -1);
3310 Fprin1 (Fcar (tail
), Qnil
);
3317 for (i
= 0; i
< backlist
->nargs
; i
++)
3319 if (i
) write_string (" ", -1);
3320 Fprin1 (backlist
->args
[i
], Qnil
);
3323 write_string (")\n", -1);
3325 backlist
= backlist
->next
;
3328 Vprint_level
= old_print_level
;
3333 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3334 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3335 If that frame has not evaluated the arguments yet (or is a special form),
3336 the value is (nil FUNCTION ARG-FORMS...).
3337 If that frame has evaluated its arguments and called its function already,
3338 the value is (t FUNCTION ARG-VALUES...).
3339 A &rest arg is represented as the tail of the list ARG-VALUES.
3340 FUNCTION is whatever was supplied as car of evaluated list,
3341 or a lambda expression for macro calls.
3342 If NFRAMES is more than the number of frames, the value is nil. */)
3343 (Lisp_Object nframes
)
3345 register struct backtrace
*backlist
= backtrace_list
;
3346 register EMACS_INT i
;
3349 CHECK_NATNUM (nframes
);
3351 /* Find the frame requested. */
3352 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3353 backlist
= backlist
->next
;
3357 if (backlist
->nargs
== UNEVALLED
)
3358 return Fcons (Qnil
, Fcons (backlist
->function
, *backlist
->args
));
3361 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3362 tem
= *backlist
->args
;
3364 tem
= Flist (backlist
->nargs
, backlist
->args
);
3366 return Fcons (Qt
, Fcons (backlist
->function
, tem
));
3373 mark_backtrace (void)
3375 register struct backtrace
*backlist
;
3378 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3380 mark_object (*backlist
->function
);
3382 if (backlist
->nargs
== UNEVALLED
3383 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3386 i
= backlist
->nargs
;
3388 mark_object (backlist
->args
[i
]);
3396 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3397 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3398 If Lisp code tries to increase the total number past this amount,
3399 an error is signaled.
3400 You can safely use a value considerably larger than the default value,
3401 if that proves inconveniently small. However, if you increase it too far,
3402 Emacs could run out of memory trying to make the stack bigger. */);
3404 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3405 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3407 This limit serves to catch infinite recursions for you before they cause
3408 actual stack overflow in C, which would be fatal for Emacs.
3409 You can safely make it considerably larger than its default value,
3410 if that proves inconveniently small. However, if you increase it too far,
3411 Emacs could overflow the real C stack, and crash. */);
3413 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3414 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3415 If the value is t, that means do an ordinary quit.
3416 If the value equals `throw-on-input', that means quit by throwing
3417 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3418 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3419 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3422 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3423 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3424 Note that `quit-flag' will still be set by typing C-g,
3425 so a quit will be signaled as soon as `inhibit-quit' is nil.
3426 To prevent this happening, set `quit-flag' to nil
3427 before making `inhibit-quit' nil. */);
3428 Vinhibit_quit
= Qnil
;
3430 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3431 DEFSYM (Qautoload
, "autoload");
3432 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3433 DEFSYM (Qmacro
, "macro");
3434 DEFSYM (Qdeclare
, "declare");
3436 /* Note that the process handling also uses Qexit, but we don't want
3437 to staticpro it twice, so we just do it here. */
3438 DEFSYM (Qexit
, "exit");
3440 DEFSYM (Qinteractive
, "interactive");
3441 DEFSYM (Qcommandp
, "commandp");
3442 DEFSYM (Qand_rest
, "&rest");
3443 DEFSYM (Qand_optional
, "&optional");
3444 DEFSYM (Qclosure
, "closure");
3445 DEFSYM (Qdebug
, "debug");
3447 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3448 doc
: /* Non-nil means never enter the debugger.
3449 Normally set while the debugger is already active, to avoid recursive
3451 Vinhibit_debugger
= Qnil
;
3453 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3454 doc
: /* Non-nil means enter debugger if an error is signaled.
3455 Does not apply to errors handled by `condition-case' or those
3456 matched by `debug-ignored-errors'.
3457 If the value is a list, an error only means to enter the debugger
3458 if one of its condition symbols appears in the list.
3459 When you evaluate an expression interactively, this variable
3460 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3461 The command `toggle-debug-on-error' toggles this.
3462 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3463 Vdebug_on_error
= Qnil
;
3465 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3466 doc
: /* List of errors for which the debugger should not be called.
3467 Each element may be a condition-name or a regexp that matches error messages.
3468 If any element applies to a given error, that error skips the debugger
3469 and just returns to top level.
3470 This overrides the variable `debug-on-error'.
3471 It does not apply to errors handled by `condition-case'. */);
3472 Vdebug_ignored_errors
= Qnil
;
3474 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3475 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3476 Does not apply if quit is handled by a `condition-case'. */);
3479 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3480 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3482 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3483 doc
: /* Non-nil means debugger may continue execution.
3484 This is nil when the debugger is called under circumstances where it
3485 might not be safe to continue. */);
3486 debugger_may_continue
= 1;
3488 DEFVAR_LISP ("debugger", Vdebugger
,
3489 doc
: /* Function to call to invoke debugger.
3490 If due to frame exit, args are `exit' and the value being returned;
3491 this function's value will be returned instead of that.
3492 If due to error, args are `error' and a list of the args to `signal'.
3493 If due to `apply' or `funcall' entry, one arg, `lambda'.
3494 If due to `eval' entry, one arg, t. */);
3497 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3498 doc
: /* If non-nil, this is a function for `signal' to call.
3499 It receives the same arguments that `signal' was given.
3500 The Edebug package uses this to regain control. */);
3501 Vsignal_hook_function
= Qnil
;
3503 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3504 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3505 Note that `debug-on-error', `debug-on-quit' and friends
3506 still determine whether to handle the particular condition. */);
3507 Vdebug_on_signal
= Qnil
;
3509 /* When lexical binding is being used,
3510 Vinternal_interpreter_environment is non-nil, and contains an alist
3511 of lexically-bound variable, or (t), indicating an empty
3512 environment. The lisp name of this variable would be
3513 `internal-interpreter-environment' if it weren't hidden.
3514 Every element of this list can be either a cons (VAR . VAL)
3515 specifying a lexical binding, or a single symbol VAR indicating
3516 that this variable should use dynamic scoping. */
3517 DEFSYM (Qinternal_interpreter_environment
,
3518 "internal-interpreter-environment");
3519 DEFVAR_LISP ("internal-interpreter-environment",
3520 Vinternal_interpreter_environment
,
3521 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3522 When lexical binding is not being used, this variable is nil.
3523 A value of `(t)' indicates an empty environment, otherwise it is an
3524 alist of active lexical bindings. */);
3525 Vinternal_interpreter_environment
= Qnil
;
3526 /* Don't export this variable to Elisp, so no one can mess with it
3527 (Just imagine if someone makes it buffer-local). */
3528 Funintern (Qinternal_interpreter_environment
, Qnil
);
3530 DEFSYM (Vrun_hooks
, "run-hooks");
3532 staticpro (&Vautoload_queue
);
3533 Vautoload_queue
= Qnil
;
3534 staticpro (&Vsignaling_function
);
3535 Vsignaling_function
= Qnil
;
3537 inhibit_lisp_code
= Qnil
;
3548 defsubr (&Sfunction
);
3550 defsubr (&Sdefvaralias
);
3551 defsubr (&Sdefconst
);
3552 defsubr (&Smake_var_non_special
);
3556 defsubr (&Smacroexpand
);
3559 defsubr (&Sunwind_protect
);
3560 defsubr (&Scondition_case
);
3562 defsubr (&Sinteractive_p
);
3563 defsubr (&Scalled_interactively_p
);
3564 defsubr (&Scommandp
);
3565 defsubr (&Sautoload
);
3566 defsubr (&Sautoload_do_load
);
3569 defsubr (&Sfuncall
);
3570 defsubr (&Srun_hooks
);
3571 defsubr (&Srun_hook_with_args
);
3572 defsubr (&Srun_hook_with_args_until_success
);
3573 defsubr (&Srun_hook_with_args_until_failure
);
3574 defsubr (&Srun_hook_wrapped
);
3575 defsubr (&Sfetch_bytecode
);
3576 defsubr (&Sbacktrace_debug
);
3577 defsubr (&Sbacktrace
);
3578 defsubr (&Sbacktrace_frame
);
3579 defsubr (&Sspecial_variable_p
);
3580 defsubr (&Sfunctionp
);