1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 #include "blockinput.h"
27 #include "dispextern.h"
28 #include "frame.h" /* For XFRAME. */
36 struct backtrace
*next
;
37 Lisp_Object
*function
;
38 Lisp_Object
*args
; /* Points to vector of args. */
39 ptrdiff_t nargs
; /* Length of vector. */
40 /* Nonzero means call value of debugger when done with this operation. */
41 unsigned int debug_on_exit
: 1;
44 static struct backtrace
*backtrace_list
;
49 struct catchtag
*catchlist
;
51 /* Chain of condition handlers currently in effect.
52 The elements of this chain are contained in the stack frames
53 of Fcondition_case and internal_condition_case.
54 When an error is signaled (by calling Fsignal, below),
55 this chain is searched for an element that applies. */
60 struct handler
*handlerlist
;
63 /* Count levels of GCPRO to detect failure to UNGCPRO. */
67 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
68 Lisp_Object Qinhibit_quit
;
69 Lisp_Object Qand_rest
;
70 static Lisp_Object Qand_optional
;
71 static Lisp_Object Qinhibit_debugger
;
72 static Lisp_Object Qdeclare
;
73 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
75 static Lisp_Object Qdebug
;
77 /* This holds either the symbol `run-hooks' or nil.
78 It is nil at an early stage of startup, and when Emacs
81 Lisp_Object Vrun_hooks
;
83 /* Non-nil means record all fset's and provide's, to be undone
84 if the file being autoloaded is not fully loaded.
85 They are recorded by being consed onto the front of Vautoload_queue:
86 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
88 Lisp_Object Vautoload_queue
;
90 /* Current number of specbindings allocated in specpdl. */
92 ptrdiff_t specpdl_size
;
94 /* Pointer to beginning of specpdl. */
96 struct specbinding
*specpdl
;
98 /* Pointer to first unused element in specpdl. */
100 struct specbinding
*specpdl_ptr
;
102 /* Depth in Lisp evaluations and function calls. */
104 static EMACS_INT lisp_eval_depth
;
106 /* The value of num_nonmacro_input_events as of the last time we
107 started to enter the debugger. If we decide to enter the debugger
108 again when this is still equal to num_nonmacro_input_events, then we
109 know that the debugger itself has an error, and we should just
110 signal the error instead of entering an infinite loop of debugger
113 static EMACS_INT when_entered_debugger
;
115 /* The function from which the last `signal' was called. Set in
118 Lisp_Object Vsignaling_function
;
120 /* If non-nil, Lisp code must not be run since some part of Emacs is
121 in an inconsistent state. Currently, x-create-frame uses this to
122 avoid triggering window-configuration-change-hook while the new
123 frame is half-initialized. */
124 Lisp_Object inhibit_lisp_code
;
126 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
127 static bool interactive_p (void);
128 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
130 /* Functions to set Lisp_Object slots of struct specbinding. */
133 set_specpdl_symbol (Lisp_Object symbol
)
135 specpdl_ptr
->symbol
= symbol
;
139 set_specpdl_old_value (Lisp_Object oldval
)
141 specpdl_ptr
->old_value
= oldval
;
145 init_eval_once (void)
148 specpdl
= xmalloc (size
* sizeof *specpdl
);
150 specpdl_ptr
= specpdl
;
151 /* Don't forget to update docs (lispref node "Local Variables"). */
152 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
153 max_lisp_eval_depth
= 600;
161 specpdl_ptr
= specpdl
;
166 debug_on_next_call
= 0;
171 /* This is less than the initial value of num_nonmacro_input_events. */
172 when_entered_debugger
= -1;
175 /* Unwind-protect function used by call_debugger. */
178 restore_stack_limits (Lisp_Object data
)
180 max_specpdl_size
= XINT (XCAR (data
));
181 max_lisp_eval_depth
= XINT (XCDR (data
));
185 /* Call the Lisp debugger, giving it argument ARG. */
188 call_debugger (Lisp_Object arg
)
190 bool debug_while_redisplaying
;
191 ptrdiff_t count
= SPECPDL_INDEX ();
193 EMACS_INT old_max
= max_specpdl_size
;
195 /* Temporarily bump up the stack limits,
196 so the debugger won't run out of stack. */
198 max_specpdl_size
+= 1;
199 record_unwind_protect (restore_stack_limits
,
200 Fcons (make_number (old_max
),
201 make_number (max_lisp_eval_depth
)));
202 max_specpdl_size
= old_max
;
204 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
205 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
207 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
208 max_specpdl_size
= SPECPDL_INDEX () + 100;
210 #ifdef HAVE_WINDOW_SYSTEM
211 if (display_hourglass_p
)
215 debug_on_next_call
= 0;
216 when_entered_debugger
= num_nonmacro_input_events
;
218 /* Resetting redisplaying_p to 0 makes sure that debug output is
219 displayed if the debugger is invoked during redisplay. */
220 debug_while_redisplaying
= redisplaying_p
;
222 specbind (intern ("debugger-may-continue"),
223 debug_while_redisplaying
? Qnil
: Qt
);
224 specbind (Qinhibit_redisplay
, Qnil
);
225 specbind (Qinhibit_debugger
, Qt
);
227 #if 0 /* Binding this prevents execution of Lisp code during
228 redisplay, which necessarily leads to display problems. */
229 specbind (Qinhibit_eval_during_redisplay
, Qt
);
232 val
= apply1 (Vdebugger
, arg
);
234 /* Interrupting redisplay and resuming it later is not safe under
235 all circumstances. So, when the debugger returns, abort the
236 interrupted redisplay by going back to the top-level. */
237 if (debug_while_redisplaying
)
240 return unbind_to (count
, val
);
244 do_debug_on_call (Lisp_Object code
)
246 debug_on_next_call
= 0;
247 backtrace_list
->debug_on_exit
= 1;
248 call_debugger (Fcons (code
, Qnil
));
251 /* NOTE!!! Every function that can call EVAL must protect its args
252 and temporaries from garbage collection while it needs them.
253 The definition of `For' shows what you have to do. */
255 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
256 doc
: /* Eval args until one of them yields non-nil, then return that value.
257 The remaining args are not evalled at all.
258 If all args return nil, return nil.
259 usage: (or CONDITIONS...) */)
262 register Lisp_Object val
= Qnil
;
269 val
= eval_sub (XCAR (args
));
279 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
280 doc
: /* Eval args until one of them yields nil, then return nil.
281 The remaining args are not evalled at all.
282 If no arg yields nil, return the last arg's value.
283 usage: (and CONDITIONS...) */)
286 register Lisp_Object val
= Qt
;
293 val
= eval_sub (XCAR (args
));
303 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
304 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
305 Returns the value of THEN or the value of the last of the ELSE's.
306 THEN must be one expression, but ELSE... can be zero or more expressions.
307 If COND yields nil, and there are no ELSE's, the value is nil.
308 usage: (if COND THEN ELSE...) */)
311 register Lisp_Object cond
;
315 cond
= eval_sub (Fcar (args
));
319 return eval_sub (Fcar (Fcdr (args
)));
320 return Fprogn (Fcdr (Fcdr (args
)));
323 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
324 doc
: /* Try each clause until one succeeds.
325 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
326 and, if the value is non-nil, this clause succeeds:
327 then the expressions in BODY are evaluated and the last one's
328 value is the value of the cond-form.
329 If no clause succeeds, cond returns nil.
330 If a clause has one element, as in (CONDITION),
331 CONDITION's value if non-nil is returned from the cond-form.
332 usage: (cond CLAUSES...) */)
335 register Lisp_Object clause
, val
;
342 clause
= Fcar (args
);
343 val
= eval_sub (Fcar (clause
));
346 if (!EQ (XCDR (clause
), Qnil
))
347 val
= Fprogn (XCDR (clause
));
357 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
358 doc
: /* Eval BODY forms sequentially and return value of last one.
359 usage: (progn BODY...) */)
362 register Lisp_Object val
= Qnil
;
369 val
= eval_sub (XCAR (args
));
377 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
378 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
379 The value of FIRST is saved during the evaluation of the remaining args,
380 whose values are discarded.
381 usage: (prog1 FIRST BODY...) */)
385 register Lisp_Object args_left
;
386 struct gcpro gcpro1
, gcpro2
;
392 val
= eval_sub (XCAR (args_left
));
393 while (CONSP (args_left
= XCDR (args_left
)))
394 eval_sub (XCAR (args_left
));
400 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
401 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
402 The value of FORM2 is saved during the evaluation of the
403 remaining args, whose values are discarded.
404 usage: (prog2 FORM1 FORM2 BODY...) */)
410 eval_sub (XCAR (args
));
412 return Fprog1 (XCDR (args
));
415 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
416 doc
: /* Set each SYM to the value of its VAL.
417 The symbols SYM are variables; they are literal (not evaluated).
418 The values VAL are expressions; they are evaluated.
419 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
420 The second VAL is not computed until after the first SYM is set, and so on;
421 each VAL can use the new value of variables set earlier in the `setq'.
422 The return value of the `setq' form is the value of the last VAL.
423 usage: (setq [SYM VAL]...) */)
426 register Lisp_Object args_left
;
427 register Lisp_Object val
, sym
, lex_binding
;
438 val
= eval_sub (Fcar (Fcdr (args_left
)));
439 sym
= Fcar (args_left
);
441 /* Like for eval_sub, we do not check declared_special here since
442 it's been done when let-binding. */
443 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
445 && !NILP (lex_binding
446 = Fassq (sym
, Vinternal_interpreter_environment
)))
447 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
449 Fset (sym
, val
); /* SYM is dynamically bound. */
451 args_left
= Fcdr (Fcdr (args_left
));
453 while (!NILP (args_left
));
459 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
460 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
461 Warning: `quote' does not construct its return value, but just returns
462 the value that was pre-constructed by the Lisp reader (see info node
463 `(elisp)Printed Representation').
464 This means that '(a . b) is not identical to (cons 'a 'b): the former
465 does not cons. Quoting should be reserved for constants that will
466 never be modified by side-effects, unless you like self-modifying code.
467 See the common pitfall in info node `(elisp)Rearrangement' for an example
468 of unexpected results when a quoted object is modified.
469 usage: (quote ARG) */)
472 if (!NILP (Fcdr (args
)))
473 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
477 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
478 doc
: /* Like `quote', but preferred for objects which are functions.
479 In byte compilation, `function' causes its argument to be compiled.
480 `quote' cannot do that.
481 usage: (function ARG) */)
484 Lisp_Object quoted
= XCAR (args
);
486 if (!NILP (Fcdr (args
)))
487 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
489 if (!NILP (Vinternal_interpreter_environment
)
491 && EQ (XCAR (quoted
), Qlambda
))
492 /* This is a lambda expression within a lexical environment;
493 return an interpreted closure instead of a simple lambda. */
494 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
497 /* Simply quote the argument. */
502 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
503 doc
: /* Return t if the containing function was run directly by user input.
504 This means that the function was called with `call-interactively'
505 \(which includes being called as the binding of a key)
506 and input is currently coming from the keyboard (not a keyboard macro),
507 and Emacs is not running in batch mode (`noninteractive' is nil).
509 The only known proper use of `interactive-p' is in deciding whether to
510 display a helpful message, or how to display it. If you're thinking
511 of using it for any other purpose, it is quite likely that you're
512 making a mistake. Think: what do you want to do when the command is
513 called from a keyboard macro?
515 To test whether your function was called with `call-interactively',
516 either (i) add an extra optional argument and give it an `interactive'
517 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
518 use `called-interactively-p'. */)
521 return interactive_p () ? Qt
: Qnil
;
525 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
526 doc
: /* Return t if the containing function was called by `call-interactively'.
527 If KIND is `interactive', then only return t if the call was made
528 interactively by the user, i.e. not in `noninteractive' mode nor
529 when `executing-kbd-macro'.
530 If KIND is `any', on the other hand, it will return t for any kind of
531 interactive call, including being called as the binding of a key, or
532 from a keyboard macro, or in `noninteractive' mode.
534 The only known proper use of `interactive' for KIND is in deciding
535 whether to display a helpful message, or how to display it. If you're
536 thinking of using it for any other purpose, it is quite likely that
537 you're making a mistake. Think: what do you want to do when the
538 command is called from a keyboard macro?
540 Instead of using this function, it is sometimes cleaner to give your
541 function an extra optional argument whose `interactive' spec specifies
542 non-nil unconditionally (\"p\" is a good way to do this), or via
543 \(not (or executing-kbd-macro noninteractive)). */)
546 return (((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
552 /* Return true if function in which this appears was called using
553 call-interactively and is not a built-in. */
558 struct backtrace
*btp
;
561 btp
= backtrace_list
;
563 /* If this isn't a byte-compiled function, there may be a frame at
564 the top for Finteractive_p. If so, skip it. */
565 fun
= Findirect_function (*btp
->function
, Qnil
);
566 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
567 || XSUBR (fun
) == &Scalled_interactively_p
))
570 /* If we're running an Emacs 18-style byte-compiled function, there
571 may be a frame for Fbytecode at the top level. In any version of
572 Emacs there can be Fbytecode frames for subexpressions evaluated
573 inside catch and condition-case. Skip past them.
575 If this isn't a byte-compiled function, then we may now be
576 looking at several frames for special forms. Skip past them. */
578 && (EQ (*btp
->function
, Qbytecode
)
579 || btp
->nargs
== UNEVALLED
))
582 /* `btp' now points at the frame of the innermost function that isn't
583 a special form, ignoring frames for Finteractive_p and/or
584 Fbytecode at the top. If this frame is for a built-in function
585 (such as load or eval-region) return false. */
586 fun
= Findirect_function (*btp
->function
, Qnil
);
590 /* `btp' points to the frame of a Lisp function that called interactive-p.
591 Return t if that function was called interactively. */
592 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
598 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
599 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
600 Aliased variables always have the same value; setting one sets the other.
601 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
602 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
603 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
604 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
605 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
606 The return value is BASE-VARIABLE. */)
607 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
609 struct Lisp_Symbol
*sym
;
611 CHECK_SYMBOL (new_alias
);
612 CHECK_SYMBOL (base_variable
);
614 sym
= XSYMBOL (new_alias
);
617 /* Not sure why, but why not? */
618 error ("Cannot make a constant an alias");
620 switch (sym
->redirect
)
622 case SYMBOL_FORWARDED
:
623 error ("Cannot make an internal variable an alias");
624 case SYMBOL_LOCALIZED
:
625 error ("Don't know how to make a localized variable an alias");
628 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
629 If n_a is bound, but b_v is not, set the value of b_v to n_a,
630 so that old-code that affects n_a before the aliasing is setup
632 if (NILP (Fboundp (base_variable
)))
633 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
636 struct specbinding
*p
;
638 for (p
= specpdl_ptr
; p
> specpdl
; )
639 if ((--p
)->func
== NULL
641 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
642 error ("Don't know how to make a let-bound variable an alias");
645 sym
->declared_special
= 1;
646 XSYMBOL (base_variable
)->declared_special
= 1;
647 sym
->redirect
= SYMBOL_VARALIAS
;
648 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
649 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
650 LOADHIST_ATTACH (new_alias
);
651 /* Even if docstring is nil: remove old docstring. */
652 Fput (new_alias
, Qvariable_documentation
, docstring
);
654 return base_variable
;
658 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
659 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
660 You are not required to define a variable in order to use it, but
661 defining it lets you supply an initial value and documentation, which
662 can be referred to by the Emacs help facilities and other programming
663 tools. The `defvar' form also declares the variable as \"special\",
664 so that it is always dynamically bound even if `lexical-binding' is t.
666 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
667 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
668 default value is what is set; buffer-local values are not affected.
669 If INITVALUE is missing, SYMBOL's value is not set.
671 If SYMBOL has a local binding, then this form affects the local
672 binding. This is usually not what you want. Thus, if you need to
673 load a file defining variables, with this form or with `defconst' or
674 `defcustom', you should always load that file _outside_ any bindings
675 for these variables. \(`defconst' and `defcustom' behave similarly in
678 The optional argument DOCSTRING is a documentation string for the
681 To define a user option, use `defcustom' instead of `defvar'.
682 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
685 register Lisp_Object sym
, tem
, tail
;
689 if (!NILP (Fcdr (Fcdr (tail
))))
690 error ("Too many arguments");
692 tem
= Fdefault_boundp (sym
);
695 /* Do it before evaluating the initial value, for self-references. */
696 XSYMBOL (sym
)->declared_special
= 1;
699 Fset_default (sym
, eval_sub (Fcar (tail
)));
701 { /* Check if there is really a global binding rather than just a let
702 binding that shadows the global unboundness of the var. */
703 struct specbinding
*pdl
= specpdl_ptr
;
704 while (pdl
> specpdl
)
706 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
707 && EQ (pdl
->old_value
, Qunbound
))
709 message_with_string ("Warning: defvar ignored because %s is let-bound",
710 SYMBOL_NAME (sym
), 1);
719 if (!NILP (Vpurify_flag
))
720 tem
= Fpurecopy (tem
);
721 Fput (sym
, Qvariable_documentation
, tem
);
723 LOADHIST_ATTACH (sym
);
725 else if (!NILP (Vinternal_interpreter_environment
)
726 && !XSYMBOL (sym
)->declared_special
)
727 /* A simple (defvar foo) with lexical scoping does "nothing" except
728 declare that var to be dynamically scoped *locally* (i.e. within
729 the current file or let-block). */
730 Vinternal_interpreter_environment
=
731 Fcons (sym
, Vinternal_interpreter_environment
);
734 /* Simple (defvar <var>) should not count as a definition at all.
735 It could get in the way of other definitions, and unloading this
736 package could try to make the variable unbound. */
742 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
743 doc
: /* Define SYMBOL as a constant variable.
744 This declares that neither programs nor users should ever change the
745 value. This constancy is not actually enforced by Emacs Lisp, but
746 SYMBOL is marked as a special variable so that it is never lexically
749 The `defconst' form always sets the value of SYMBOL to the result of
750 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
751 what is set; buffer-local values are not affected. If SYMBOL has a
752 local binding, then this form sets the local binding's value.
753 However, you should normally not make local bindings for variables
754 defined with this form.
756 The optional DOCSTRING specifies the variable's documentation string.
757 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
760 register Lisp_Object sym
, tem
;
763 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
764 error ("Too many arguments");
766 tem
= eval_sub (Fcar (Fcdr (args
)));
767 if (!NILP (Vpurify_flag
))
768 tem
= Fpurecopy (tem
);
769 Fset_default (sym
, tem
);
770 XSYMBOL (sym
)->declared_special
= 1;
771 tem
= Fcar (Fcdr (Fcdr (args
)));
774 if (!NILP (Vpurify_flag
))
775 tem
= Fpurecopy (tem
);
776 Fput (sym
, Qvariable_documentation
, tem
);
778 Fput (sym
, Qrisky_local_variable
, Qt
);
779 LOADHIST_ATTACH (sym
);
783 /* Make SYMBOL lexically scoped. */
784 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
785 Smake_var_non_special
, 1, 1, 0,
786 doc
: /* Internal function. */)
789 CHECK_SYMBOL (symbol
);
790 XSYMBOL (symbol
)->declared_special
= 0;
795 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
796 doc
: /* Bind variables according to VARLIST then eval BODY.
797 The value of the last form in BODY is returned.
798 Each element of VARLIST is a symbol (which is bound to nil)
799 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
800 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
801 usage: (let* VARLIST BODY...) */)
804 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
805 ptrdiff_t count
= SPECPDL_INDEX ();
806 struct gcpro gcpro1
, gcpro2
, gcpro3
;
808 GCPRO3 (args
, elt
, varlist
);
810 lexenv
= Vinternal_interpreter_environment
;
812 varlist
= Fcar (args
);
813 while (CONSP (varlist
))
817 elt
= XCAR (varlist
);
823 else if (! NILP (Fcdr (Fcdr (elt
))))
824 signal_error ("`let' bindings can have only one value-form", elt
);
828 val
= eval_sub (Fcar (Fcdr (elt
)));
831 if (!NILP (lexenv
) && SYMBOLP (var
)
832 && !XSYMBOL (var
)->declared_special
833 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
834 /* Lexically bind VAR by adding it to the interpreter's binding
838 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
839 if (EQ (Vinternal_interpreter_environment
, lexenv
))
840 /* Save the old lexical environment on the specpdl stack,
841 but only for the first lexical binding, since we'll never
842 need to revert to one of the intermediate ones. */
843 specbind (Qinternal_interpreter_environment
, newenv
);
845 Vinternal_interpreter_environment
= newenv
;
850 varlist
= XCDR (varlist
);
853 val
= Fprogn (Fcdr (args
));
854 return unbind_to (count
, val
);
857 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
858 doc
: /* Bind variables according to VARLIST then eval BODY.
859 The value of the last form in BODY is returned.
860 Each element of VARLIST is a symbol (which is bound to nil)
861 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
862 All the VALUEFORMs are evalled before any symbols are bound.
863 usage: (let VARLIST BODY...) */)
866 Lisp_Object
*temps
, tem
, lexenv
;
867 register Lisp_Object elt
, varlist
;
868 ptrdiff_t count
= SPECPDL_INDEX ();
870 struct gcpro gcpro1
, gcpro2
;
873 varlist
= Fcar (args
);
875 /* Make space to hold the values to give the bound variables. */
876 elt
= Flength (varlist
);
877 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
879 /* Compute the values and store them in `temps'. */
881 GCPRO2 (args
, *temps
);
884 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
887 elt
= XCAR (varlist
);
889 temps
[argnum
++] = Qnil
;
890 else if (! NILP (Fcdr (Fcdr (elt
))))
891 signal_error ("`let' bindings can have only one value-form", elt
);
893 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
894 gcpro2
.nvars
= argnum
;
898 lexenv
= Vinternal_interpreter_environment
;
900 varlist
= Fcar (args
);
901 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
905 elt
= XCAR (varlist
);
906 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
907 tem
= temps
[argnum
++];
909 if (!NILP (lexenv
) && SYMBOLP (var
)
910 && !XSYMBOL (var
)->declared_special
911 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
912 /* Lexically bind VAR by adding it to the lexenv alist. */
913 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
915 /* Dynamically bind VAR. */
919 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
920 /* Instantiate a new lexical environment. */
921 specbind (Qinternal_interpreter_environment
, lexenv
);
923 elt
= Fprogn (Fcdr (args
));
925 return unbind_to (count
, elt
);
928 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
929 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
930 The order of execution is thus TEST, BODY, TEST, BODY and so on
931 until TEST returns nil.
932 usage: (while TEST BODY...) */)
935 Lisp_Object test
, body
;
936 struct gcpro gcpro1
, gcpro2
;
942 while (!NILP (eval_sub (test
)))
952 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
953 doc
: /* Return result of expanding macros at top level of FORM.
954 If FORM is not a macro call, it is returned unchanged.
955 Otherwise, the macro is expanded and the expansion is considered
956 in place of FORM. When a non-macro-call results, it is returned.
958 The second optional arg ENVIRONMENT specifies an environment of macro
959 definitions to shadow the loaded ones for use in file byte-compilation. */)
960 (Lisp_Object form
, Lisp_Object environment
)
962 /* With cleanups from Hallvard Furuseth. */
963 register Lisp_Object expander
, sym
, def
, tem
;
967 /* Come back here each time we expand a macro call,
968 in case it expands into another macro call. */
971 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
972 def
= sym
= XCAR (form
);
974 /* Trace symbols aliases to other symbols
975 until we get a symbol that is not an alias. */
976 while (SYMBOLP (def
))
980 tem
= Fassq (sym
, environment
);
983 def
= XSYMBOL (sym
)->function
;
984 if (!EQ (def
, Qunbound
))
989 /* Right now TEM is the result from SYM in ENVIRONMENT,
990 and if TEM is nil then DEF is SYM's function definition. */
993 /* SYM is not mentioned in ENVIRONMENT.
994 Look at its function definition. */
997 def
= Fautoload_do_load (def
, sym
, Qmacro
);
999 if (EQ (def
, Qunbound
) || !CONSP (def
))
1000 /* Not defined or definition not suitable. */
1002 if (!EQ (XCAR (def
), Qmacro
))
1004 else expander
= XCDR (def
);
1008 expander
= XCDR (tem
);
1009 if (NILP (expander
))
1013 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1014 if (EQ (form
, newform
))
1023 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1024 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1025 TAG is evalled to get the tag to use; it must not be nil.
1027 Then the BODY is executed.
1028 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1029 If no throw happens, `catch' returns the value of the last BODY form.
1030 If a throw happens, it specifies the value to return from `catch'.
1031 usage: (catch TAG BODY...) */)
1034 register Lisp_Object tag
;
1035 struct gcpro gcpro1
;
1038 tag
= eval_sub (Fcar (args
));
1040 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1043 /* Set up a catch, then call C function FUNC on argument ARG.
1044 FUNC should return a Lisp_Object.
1045 This is how catches are done from within C code. */
1048 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1050 /* This structure is made part of the chain `catchlist'. */
1053 /* Fill in the components of c, and put it on the list. */
1057 c
.backlist
= backtrace_list
;
1058 c
.handlerlist
= handlerlist
;
1059 c
.lisp_eval_depth
= lisp_eval_depth
;
1060 c
.pdlcount
= SPECPDL_INDEX ();
1061 c
.poll_suppress_count
= poll_suppress_count
;
1062 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1063 c
.gcpro
= gcprolist
;
1064 c
.byte_stack
= byte_stack_list
;
1068 if (! sys_setjmp (c
.jmp
))
1069 c
.val
= (*func
) (arg
);
1071 /* Throw works by a longjmp that comes right here. */
1076 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1077 jump to that CATCH, returning VALUE as the value of that catch.
1079 This is the guts Fthrow and Fsignal; they differ only in the way
1080 they choose the catch tag to throw to. A catch tag for a
1081 condition-case form has a TAG of Qnil.
1083 Before each catch is discarded, unbind all special bindings and
1084 execute all unwind-protect clauses made above that catch. Unwind
1085 the handler stack as we go, so that the proper handlers are in
1086 effect for each unwind-protect clause we run. At the end, restore
1087 some static info saved in CATCH, and longjmp to the location
1090 This is used for correct unwinding in Fthrow and Fsignal. */
1092 static _Noreturn
void
1093 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1097 /* Save the value in the tag. */
1100 /* Restore certain special C variables. */
1101 set_poll_suppress_count (catch->poll_suppress_count
);
1102 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1107 last_time
= catchlist
== catch;
1109 /* Unwind the specpdl stack, and then restore the proper set of
1111 unbind_to (catchlist
->pdlcount
, Qnil
);
1112 handlerlist
= catchlist
->handlerlist
;
1113 catchlist
= catchlist
->next
;
1115 while (! last_time
);
1118 /* If x_catch_errors was done, turn it off now.
1119 (First we give unbind_to a chance to do that.) */
1120 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1121 The catch must remain in effect during that delicate
1122 state. --lorentey */
1123 x_fully_uncatch_errors ();
1127 byte_stack_list
= catch->byte_stack
;
1128 gcprolist
= catch->gcpro
;
1130 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1132 backtrace_list
= catch->backlist
;
1133 lisp_eval_depth
= catch->lisp_eval_depth
;
1135 sys_longjmp (catch->jmp
, 1);
1138 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1139 doc
: /* Throw to the catch for TAG and return VALUE from it.
1140 Both TAG and VALUE are evalled. */)
1141 (register Lisp_Object tag
, Lisp_Object value
)
1143 register struct catchtag
*c
;
1146 for (c
= catchlist
; c
; c
= c
->next
)
1148 if (EQ (c
->tag
, tag
))
1149 unwind_to_catch (c
, value
);
1151 xsignal2 (Qno_catch
, tag
, value
);
1155 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1156 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1157 If BODYFORM completes normally, its value is returned
1158 after executing the UNWINDFORMS.
1159 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1160 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1164 ptrdiff_t count
= SPECPDL_INDEX ();
1166 record_unwind_protect (Fprogn
, Fcdr (args
));
1167 val
= eval_sub (Fcar (args
));
1168 return unbind_to (count
, val
);
1171 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1172 doc
: /* Regain control when an error is signaled.
1173 Executes BODYFORM and returns its value if no error happens.
1174 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1175 where the BODY is made of Lisp expressions.
1177 A handler is applicable to an error
1178 if CONDITION-NAME is one of the error's condition names.
1179 If an error happens, the first applicable handler is run.
1181 The car of a handler may be a list of condition names instead of a
1182 single condition name; then it handles all of them. If the special
1183 condition name `debug' is present in this list, it allows another
1184 condition in the list to run the debugger if `debug-on-error' and the
1185 other usual mechanisms says it should (otherwise, `condition-case'
1186 suppresses the debugger).
1188 When a handler handles an error, control returns to the `condition-case'
1189 and it executes the handler's BODY...
1190 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1191 \(If VAR is nil, the handler can't access that information.)
1192 Then the value of the last BODY form is returned from the `condition-case'
1195 See also the function `signal' for more info.
1196 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1199 Lisp_Object var
= Fcar (args
);
1200 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1201 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1203 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1206 /* Like Fcondition_case, but the args are separate
1207 rather than passed in a list. Used by Fbyte_code. */
1210 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1211 Lisp_Object handlers
)
1219 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1225 && (SYMBOLP (XCAR (tem
))
1226 || CONSP (XCAR (tem
))))))
1227 error ("Invalid condition handler: %s",
1228 SDATA (Fprin1_to_string (tem
, Qt
)));
1233 c
.backlist
= backtrace_list
;
1234 c
.handlerlist
= handlerlist
;
1235 c
.lisp_eval_depth
= lisp_eval_depth
;
1236 c
.pdlcount
= SPECPDL_INDEX ();
1237 c
.poll_suppress_count
= poll_suppress_count
;
1238 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1239 c
.gcpro
= gcprolist
;
1240 c
.byte_stack
= byte_stack_list
;
1241 if (sys_setjmp (c
.jmp
))
1244 specbind (h
.var
, c
.val
);
1245 val
= Fprogn (Fcdr (h
.chosen_clause
));
1247 /* Note that this just undoes the binding of h.var; whoever
1248 longjumped to us unwound the stack to c.pdlcount before
1250 unbind_to (c
.pdlcount
, Qnil
);
1257 h
.handler
= handlers
;
1258 h
.next
= handlerlist
;
1262 val
= eval_sub (bodyform
);
1264 handlerlist
= h
.next
;
1268 /* Call the function BFUN with no arguments, catching errors within it
1269 according to HANDLERS. If there is an error, call HFUN with
1270 one argument which is the data that describes the error:
1273 HANDLERS can be a list of conditions to catch.
1274 If HANDLERS is Qt, catch all errors.
1275 If HANDLERS is Qerror, catch all errors
1276 but allow the debugger to run if that is enabled. */
1279 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1280 Lisp_Object (*hfun
) (Lisp_Object
))
1288 c
.backlist
= backtrace_list
;
1289 c
.handlerlist
= handlerlist
;
1290 c
.lisp_eval_depth
= lisp_eval_depth
;
1291 c
.pdlcount
= SPECPDL_INDEX ();
1292 c
.poll_suppress_count
= poll_suppress_count
;
1293 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1294 c
.gcpro
= gcprolist
;
1295 c
.byte_stack
= byte_stack_list
;
1296 if (sys_setjmp (c
.jmp
))
1298 return (*hfun
) (c
.val
);
1302 h
.handler
= handlers
;
1304 h
.next
= handlerlist
;
1310 handlerlist
= h
.next
;
1314 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1317 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1318 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1326 c
.backlist
= backtrace_list
;
1327 c
.handlerlist
= handlerlist
;
1328 c
.lisp_eval_depth
= lisp_eval_depth
;
1329 c
.pdlcount
= SPECPDL_INDEX ();
1330 c
.poll_suppress_count
= poll_suppress_count
;
1331 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1332 c
.gcpro
= gcprolist
;
1333 c
.byte_stack
= byte_stack_list
;
1334 if (sys_setjmp (c
.jmp
))
1336 return (*hfun
) (c
.val
);
1340 h
.handler
= handlers
;
1342 h
.next
= handlerlist
;
1346 val
= (*bfun
) (arg
);
1348 handlerlist
= h
.next
;
1352 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1356 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1359 Lisp_Object handlers
,
1360 Lisp_Object (*hfun
) (Lisp_Object
))
1368 c
.backlist
= backtrace_list
;
1369 c
.handlerlist
= handlerlist
;
1370 c
.lisp_eval_depth
= lisp_eval_depth
;
1371 c
.pdlcount
= SPECPDL_INDEX ();
1372 c
.poll_suppress_count
= poll_suppress_count
;
1373 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1374 c
.gcpro
= gcprolist
;
1375 c
.byte_stack
= byte_stack_list
;
1376 if (sys_setjmp (c
.jmp
))
1378 return (*hfun
) (c
.val
);
1382 h
.handler
= handlers
;
1384 h
.next
= handlerlist
;
1388 val
= (*bfun
) (arg1
, arg2
);
1390 handlerlist
= h
.next
;
1394 /* Like internal_condition_case but call BFUN with NARGS as first,
1395 and ARGS as second argument. */
1398 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1401 Lisp_Object handlers
,
1402 Lisp_Object (*hfun
) (Lisp_Object err
,
1412 c
.backlist
= backtrace_list
;
1413 c
.handlerlist
= handlerlist
;
1414 c
.lisp_eval_depth
= lisp_eval_depth
;
1415 c
.pdlcount
= SPECPDL_INDEX ();
1416 c
.poll_suppress_count
= poll_suppress_count
;
1417 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1418 c
.gcpro
= gcprolist
;
1419 c
.byte_stack
= byte_stack_list
;
1420 if (sys_setjmp (c
.jmp
))
1422 return (*hfun
) (c
.val
, nargs
, args
);
1426 h
.handler
= handlers
;
1428 h
.next
= handlerlist
;
1432 val
= (*bfun
) (nargs
, args
);
1434 handlerlist
= h
.next
;
1439 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1440 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1444 process_quit_flag (void)
1446 Lisp_Object flag
= Vquit_flag
;
1448 if (EQ (flag
, Qkill_emacs
))
1450 if (EQ (Vthrow_on_input
, flag
))
1451 Fthrow (Vthrow_on_input
, Qt
);
1452 Fsignal (Qquit
, Qnil
);
1455 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1456 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1457 This function does not return.
1459 An error symbol is a symbol with an `error-conditions' property
1460 that is a list of condition names.
1461 A handler for any of those names will get to handle this signal.
1462 The symbol `error' should normally be one of them.
1464 DATA should be a list. Its elements are printed as part of the error message.
1465 See Info anchor `(elisp)Definition of signal' for some details on how this
1466 error message is constructed.
1467 If the signal is handled, DATA is made available to the handler.
1468 See also the function `condition-case'. */)
1469 (Lisp_Object error_symbol
, Lisp_Object data
)
1471 /* When memory is full, ERROR-SYMBOL is nil,
1472 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1473 That is a special case--don't do this in other situations. */
1474 Lisp_Object conditions
;
1476 Lisp_Object real_error_symbol
1477 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1478 register Lisp_Object clause
= Qnil
;
1480 struct backtrace
*bp
;
1484 if (gc_in_progress
|| waiting_for_input
)
1487 #if 0 /* rms: I don't know why this was here,
1488 but it is surely wrong for an error that is handled. */
1489 #ifdef HAVE_WINDOW_SYSTEM
1490 if (display_hourglass_p
)
1491 cancel_hourglass ();
1495 /* This hook is used by edebug. */
1496 if (! NILP (Vsignal_hook_function
)
1497 && ! NILP (error_symbol
))
1499 /* Edebug takes care of restoring these variables when it exits. */
1500 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1501 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1503 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1504 max_specpdl_size
= SPECPDL_INDEX () + 40;
1506 call2 (Vsignal_hook_function
, error_symbol
, data
);
1509 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1511 /* Remember from where signal was called. Skip over the frame for
1512 `signal' itself. If a frame for `error' follows, skip that,
1513 too. Don't do this when ERROR_SYMBOL is nil, because that
1514 is a memory-full error. */
1515 Vsignaling_function
= Qnil
;
1516 if (backtrace_list
&& !NILP (error_symbol
))
1518 bp
= backtrace_list
->next
;
1519 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1521 if (bp
&& bp
->function
)
1522 Vsignaling_function
= *bp
->function
;
1525 for (h
= handlerlist
; h
; h
= h
->next
)
1527 clause
= find_handler_clause (h
->handler
, conditions
);
1532 if (/* Don't run the debugger for a memory-full error.
1533 (There is no room in memory to do that!) */
1534 !NILP (error_symbol
)
1535 && (!NILP (Vdebug_on_signal
)
1536 /* If no handler is present now, try to run the debugger. */
1538 /* A `debug' symbol in the handler list disables the normal
1539 suppression of the debugger. */
1540 || (CONSP (clause
) && CONSP (XCAR (clause
))
1541 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1542 /* Special handler that means "print a message and run debugger
1544 || EQ (h
->handler
, Qerror
)))
1546 bool debugger_called
1547 = maybe_call_debugger (conditions
, error_symbol
, data
);
1548 /* We can't return values to code which signaled an error, but we
1549 can continue code which has signaled a quit. */
1550 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1556 Lisp_Object unwind_data
1557 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1559 h
->chosen_clause
= clause
;
1560 unwind_to_catch (h
->tag
, unwind_data
);
1565 Fthrow (Qtop_level
, Qt
);
1568 if (! NILP (error_symbol
))
1569 data
= Fcons (error_symbol
, data
);
1571 string
= Ferror_message_string (data
);
1572 fatal ("%s", SDATA (string
));
1575 /* Internal version of Fsignal that never returns.
1576 Used for anything but Qquit (which can return from Fsignal). */
1579 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1581 Fsignal (error_symbol
, data
);
1585 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1588 xsignal0 (Lisp_Object error_symbol
)
1590 xsignal (error_symbol
, Qnil
);
1594 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1596 xsignal (error_symbol
, list1 (arg
));
1600 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1602 xsignal (error_symbol
, list2 (arg1
, arg2
));
1606 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1608 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1611 /* Signal `error' with message S, and additional arg ARG.
1612 If ARG is not a genuine list, make it a one-element list. */
1615 signal_error (const char *s
, Lisp_Object arg
)
1617 Lisp_Object tortoise
, hare
;
1619 hare
= tortoise
= arg
;
1620 while (CONSP (hare
))
1627 tortoise
= XCDR (tortoise
);
1629 if (EQ (hare
, tortoise
))
1634 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1636 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1640 /* Return true if LIST is a non-nil atom or
1641 a list containing one of CONDITIONS. */
1644 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1651 while (CONSP (conditions
))
1653 Lisp_Object
this, tail
;
1654 this = XCAR (conditions
);
1655 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1656 if (EQ (XCAR (tail
), this))
1658 conditions
= XCDR (conditions
);
1663 /* Return true if an error with condition-symbols CONDITIONS,
1664 and described by SIGNAL-DATA, should skip the debugger
1665 according to debugger-ignored-errors. */
1668 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1671 bool first_string
= 1;
1672 Lisp_Object error_message
;
1674 error_message
= Qnil
;
1675 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1677 if (STRINGP (XCAR (tail
)))
1681 error_message
= Ferror_message_string (data
);
1685 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1690 Lisp_Object contail
;
1692 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1693 if (EQ (XCAR (tail
), XCAR (contail
)))
1701 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1702 SIG and DATA describe the signal. There are two ways to pass them:
1703 = SIG is the error symbol, and DATA is the rest of the data.
1704 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1705 This is for memory-full errors only. */
1707 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1709 Lisp_Object combined_data
;
1711 combined_data
= Fcons (sig
, data
);
1714 /* Don't try to run the debugger with interrupts blocked.
1715 The editing loop would return anyway. */
1717 && NILP (Vinhibit_debugger
)
1718 /* Does user want to enter debugger for this kind of error? */
1721 : wants_debugger (Vdebug_on_error
, conditions
))
1722 && ! skip_debugger (conditions
, combined_data
)
1723 /* RMS: What's this for? */
1724 && when_entered_debugger
< num_nonmacro_input_events
)
1726 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1734 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1736 register Lisp_Object h
;
1738 /* t is used by handlers for all conditions, set up by C code. */
1739 if (EQ (handlers
, Qt
))
1742 /* error is used similarly, but means print an error message
1743 and run the debugger if that is enabled. */
1744 if (EQ (handlers
, Qerror
))
1747 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1749 Lisp_Object handler
= XCAR (h
);
1750 Lisp_Object condit
, tem
;
1752 if (!CONSP (handler
))
1754 condit
= XCAR (handler
);
1755 /* Handle a single condition name in handler HANDLER. */
1756 if (SYMBOLP (condit
))
1758 tem
= Fmemq (Fcar (handler
), conditions
);
1762 /* Handle a list of condition names in handler HANDLER. */
1763 else if (CONSP (condit
))
1766 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1768 tem
= Fmemq (XCAR (tail
), conditions
);
1779 /* Dump an error message; called like vprintf. */
1781 verror (const char *m
, va_list ap
)
1784 ptrdiff_t size
= sizeof buf
;
1785 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1790 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1791 string
= make_string (buffer
, used
);
1795 xsignal1 (Qerror
, string
);
1799 /* Dump an error message; called like printf. */
1803 error (const char *m
, ...)
1811 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1812 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1813 This means it contains a description for how to read arguments to give it.
1814 The value is nil for an invalid function or a symbol with no function
1817 Interactively callable functions include strings and vectors (treated
1818 as keyboard macros), lambda-expressions that contain a top-level call
1819 to `interactive', autoload definitions made by `autoload' with non-nil
1820 fourth argument, and some of the built-in functions of Lisp.
1822 Also, a symbol satisfies `commandp' if its function definition does so.
1824 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1825 then strings and vectors are not accepted. */)
1826 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1828 register Lisp_Object fun
;
1829 register Lisp_Object funcar
;
1830 Lisp_Object if_prop
= Qnil
;
1834 fun
= indirect_function (fun
); /* Check cycles. */
1835 if (NILP (fun
) || EQ (fun
, Qunbound
))
1838 /* Check an `interactive-form' property if present, analogous to the
1839 function-documentation property. */
1841 while (SYMBOLP (fun
))
1843 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1846 fun
= Fsymbol_function (fun
);
1849 /* Emacs primitives are interactive if their DEFUN specifies an
1850 interactive spec. */
1852 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1854 /* Bytecode objects are interactive if they are long enough to
1855 have an element whose index is COMPILED_INTERACTIVE, which is
1856 where the interactive spec is stored. */
1857 else if (COMPILEDP (fun
))
1858 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1861 /* Strings and vectors are keyboard macros. */
1862 if (STRINGP (fun
) || VECTORP (fun
))
1863 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1865 /* Lists may represent commands. */
1868 funcar
= XCAR (fun
);
1869 if (EQ (funcar
, Qclosure
))
1870 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1872 else if (EQ (funcar
, Qlambda
))
1873 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1874 else if (EQ (funcar
, Qautoload
))
1875 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1880 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1881 doc
: /* Define FUNCTION to autoload from FILE.
1882 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1883 Third arg DOCSTRING is documentation for the function.
1884 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1885 Fifth arg TYPE indicates the type of the object:
1886 nil or omitted says FUNCTION is a function,
1887 `keymap' says FUNCTION is really a keymap, and
1888 `macro' or t says FUNCTION is really a macro.
1889 Third through fifth args give info about the real definition.
1890 They default to nil.
1891 If FUNCTION is already defined other than as an autoload,
1892 this does nothing and returns nil. */)
1893 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1895 CHECK_SYMBOL (function
);
1896 CHECK_STRING (file
);
1898 /* If function is defined and not as an autoload, don't override. */
1899 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1900 && !(CONSP (XSYMBOL (function
)->function
)
1901 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1904 if (NILP (Vpurify_flag
))
1905 /* Only add entries after dumping, because the ones before are
1906 not useful and else we get loads of them from the loaddefs.el. */
1907 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1908 else if (EQ (docstring
, make_number (0)))
1909 /* `read1' in lread.c has found the docstring starting with "\
1910 and assumed the docstring will be provided by Snarf-documentation, so it
1911 passed us 0 instead. But that leads to accidental sharing in purecopy's
1912 hash-consing, so we use a (hopefully) unique integer instead. */
1913 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1914 return Ffset (function
,
1915 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1916 interactive
, type
)));
1920 un_autoload (Lisp_Object oldqueue
)
1922 register Lisp_Object queue
, first
, second
;
1924 /* Queue to unwind is current value of Vautoload_queue.
1925 oldqueue is the shadowed value to leave in Vautoload_queue. */
1926 queue
= Vautoload_queue
;
1927 Vautoload_queue
= oldqueue
;
1928 while (CONSP (queue
))
1930 first
= XCAR (queue
);
1931 second
= Fcdr (first
);
1932 first
= Fcar (first
);
1933 if (EQ (first
, make_number (0)))
1936 Ffset (first
, second
);
1937 queue
= XCDR (queue
);
1942 /* Load an autoloaded function.
1943 FUNNAME is the symbol which is the function's name.
1944 FUNDEF is the autoload definition (a list). */
1946 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1947 doc
: /* Load FUNDEF which should be an autoload.
1948 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1949 in which case the function returns the new autoloaded function value.
1950 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1951 it is defines a macro. */)
1952 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1954 ptrdiff_t count
= SPECPDL_INDEX ();
1955 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1957 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1960 if (EQ (macro_only
, Qmacro
))
1962 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1963 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1967 /* This is to make sure that loadup.el gives a clear picture
1968 of what files are preloaded and when. */
1969 if (! NILP (Vpurify_flag
))
1970 error ("Attempt to autoload %s while preparing to dump",
1971 SDATA (SYMBOL_NAME (funname
)));
1973 CHECK_SYMBOL (funname
);
1974 GCPRO3 (funname
, fundef
, macro_only
);
1976 /* Preserve the match data. */
1977 record_unwind_save_match_data ();
1979 /* If autoloading gets an error (which includes the error of failing
1980 to define the function being called), we use Vautoload_queue
1981 to undo function definitions and `provide' calls made by
1982 the function. We do this in the specific case of autoloading
1983 because autoloading is not an explicit request "load this file",
1984 but rather a request to "call this function".
1986 The value saved here is to be restored into Vautoload_queue. */
1987 record_unwind_protect (un_autoload
, Vautoload_queue
);
1988 Vautoload_queue
= Qt
;
1989 /* If `macro_only', assume this autoload to be a "best-effort",
1990 so don't signal an error if autoloading fails. */
1991 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
1993 /* Once loading finishes, don't undo it. */
1994 Vautoload_queue
= Qt
;
1995 unbind_to (count
, Qnil
);
2003 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2005 if (!NILP (Fequal (fun
, fundef
)))
2006 error ("Autoloading failed to define function %s",
2007 SDATA (SYMBOL_NAME (funname
)));
2014 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2015 doc
: /* Evaluate FORM and return its value.
2016 If LEXICAL is t, evaluate using lexical scoping. */)
2017 (Lisp_Object form
, Lisp_Object lexical
)
2019 ptrdiff_t count
= SPECPDL_INDEX ();
2020 specbind (Qinternal_interpreter_environment
,
2021 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2022 return unbind_to (count
, eval_sub (form
));
2025 /* Eval a sub-expression of the current expression (i.e. in the same
2028 eval_sub (Lisp_Object form
)
2030 Lisp_Object fun
, val
, original_fun
, original_args
;
2032 struct backtrace backtrace
;
2033 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2037 /* Look up its binding in the lexical environment.
2038 We do not pay attention to the declared_special flag here, since we
2039 already did that when let-binding the variable. */
2040 Lisp_Object lex_binding
2041 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2042 ? Fassq (form
, Vinternal_interpreter_environment
)
2044 if (CONSP (lex_binding
))
2045 return XCDR (lex_binding
);
2047 return Fsymbol_value (form
);
2056 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2058 if (max_lisp_eval_depth
< 100)
2059 max_lisp_eval_depth
= 100;
2060 if (lisp_eval_depth
> max_lisp_eval_depth
)
2061 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2064 original_fun
= XCAR (form
);
2065 original_args
= XCDR (form
);
2067 backtrace
.next
= backtrace_list
;
2068 backtrace_list
= &backtrace
;
2069 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2070 backtrace
.args
= &original_args
;
2071 backtrace
.nargs
= UNEVALLED
;
2072 backtrace
.debug_on_exit
= 0;
2074 if (debug_on_next_call
)
2075 do_debug_on_call (Qt
);
2077 /* At this point, only original_fun and original_args
2078 have values that will be used below. */
2081 /* Optimize for no indirection. */
2083 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2084 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2085 fun
= indirect_function (fun
);
2089 Lisp_Object numargs
;
2090 Lisp_Object argvals
[8];
2091 Lisp_Object args_left
;
2092 register int i
, maxargs
;
2094 args_left
= original_args
;
2095 numargs
= Flength (args_left
);
2099 if (XINT (numargs
) < XSUBR (fun
)->min_args
2100 || (XSUBR (fun
)->max_args
>= 0
2101 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2102 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2104 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2105 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2106 else if (XSUBR (fun
)->max_args
== MANY
)
2108 /* Pass a vector of evaluated arguments. */
2110 ptrdiff_t argnum
= 0;
2113 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2115 GCPRO3 (args_left
, fun
, fun
);
2119 while (!NILP (args_left
))
2121 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2122 args_left
= Fcdr (args_left
);
2123 gcpro3
.nvars
= argnum
;
2126 backtrace
.args
= vals
;
2127 backtrace
.nargs
= XINT (numargs
);
2129 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2135 GCPRO3 (args_left
, fun
, fun
);
2136 gcpro3
.var
= argvals
;
2139 maxargs
= XSUBR (fun
)->max_args
;
2140 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2142 argvals
[i
] = eval_sub (Fcar (args_left
));
2148 backtrace
.args
= argvals
;
2149 backtrace
.nargs
= XINT (numargs
);
2154 val
= (XSUBR (fun
)->function
.a0 ());
2157 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2160 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2163 val
= (XSUBR (fun
)->function
.a3
2164 (argvals
[0], argvals
[1], argvals
[2]));
2167 val
= (XSUBR (fun
)->function
.a4
2168 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2171 val
= (XSUBR (fun
)->function
.a5
2172 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2176 val
= (XSUBR (fun
)->function
.a6
2177 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2178 argvals
[4], argvals
[5]));
2181 val
= (XSUBR (fun
)->function
.a7
2182 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2183 argvals
[4], argvals
[5], argvals
[6]));
2187 val
= (XSUBR (fun
)->function
.a8
2188 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2189 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2193 /* Someone has created a subr that takes more arguments than
2194 is supported by this code. We need to either rewrite the
2195 subr to use a different argument protocol, or add more
2196 cases to this switch. */
2201 else if (COMPILEDP (fun
))
2202 val
= apply_lambda (fun
, original_args
);
2205 if (EQ (fun
, Qunbound
))
2206 xsignal1 (Qvoid_function
, original_fun
);
2208 xsignal1 (Qinvalid_function
, original_fun
);
2209 funcar
= XCAR (fun
);
2210 if (!SYMBOLP (funcar
))
2211 xsignal1 (Qinvalid_function
, original_fun
);
2212 if (EQ (funcar
, Qautoload
))
2214 Fautoload_do_load (fun
, original_fun
, Qnil
);
2217 if (EQ (funcar
, Qmacro
))
2219 ptrdiff_t count
= SPECPDL_INDEX ();
2221 /* Bind lexical-binding during expansion of the macro, so the
2222 macro can know reliably if the code it outputs will be
2223 interpreted using lexical-binding or not. */
2224 specbind (Qlexical_binding
,
2225 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2226 exp
= apply1 (Fcdr (fun
), original_args
);
2227 unbind_to (count
, Qnil
);
2228 val
= eval_sub (exp
);
2230 else if (EQ (funcar
, Qlambda
)
2231 || EQ (funcar
, Qclosure
))
2232 val
= apply_lambda (fun
, original_args
);
2234 xsignal1 (Qinvalid_function
, original_fun
);
2239 if (backtrace
.debug_on_exit
)
2240 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2241 backtrace_list
= backtrace
.next
;
2246 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2247 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2248 Then return the value FUNCTION returns.
2249 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2250 usage: (apply FUNCTION &rest ARGUMENTS) */)
2251 (ptrdiff_t nargs
, Lisp_Object
*args
)
2255 register Lisp_Object spread_arg
;
2256 register Lisp_Object
*funcall_args
;
2257 Lisp_Object fun
, retval
;
2258 struct gcpro gcpro1
;
2263 spread_arg
= args
[nargs
- 1];
2264 CHECK_LIST (spread_arg
);
2266 numargs
= XINT (Flength (spread_arg
));
2269 return Ffuncall (nargs
- 1, args
);
2270 else if (numargs
== 1)
2272 args
[nargs
- 1] = XCAR (spread_arg
);
2273 return Ffuncall (nargs
, args
);
2276 numargs
+= nargs
- 2;
2278 /* Optimize for no indirection. */
2279 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2280 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2281 fun
= indirect_function (fun
);
2282 if (EQ (fun
, Qunbound
))
2284 /* Let funcall get the error. */
2291 if (numargs
< XSUBR (fun
)->min_args
2292 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2293 goto funcall
; /* Let funcall get the error. */
2294 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2296 /* Avoid making funcall cons up a yet another new vector of arguments
2297 by explicitly supplying nil's for optional values. */
2298 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2299 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2300 funcall_args
[++i
] = Qnil
;
2301 GCPRO1 (*funcall_args
);
2302 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2306 /* We add 1 to numargs because funcall_args includes the
2307 function itself as well as its arguments. */
2310 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2311 GCPRO1 (*funcall_args
);
2312 gcpro1
.nvars
= 1 + numargs
;
2315 memcpy (funcall_args
, args
, nargs
* word_size
);
2316 /* Spread the last arg we got. Its first element goes in
2317 the slot that it used to occupy, hence this value of I. */
2319 while (!NILP (spread_arg
))
2321 funcall_args
[i
++] = XCAR (spread_arg
);
2322 spread_arg
= XCDR (spread_arg
);
2325 /* By convention, the caller needs to gcpro Ffuncall's args. */
2326 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2333 /* Run hook variables in various ways. */
2336 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2338 Ffuncall (nargs
, args
);
2342 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2343 doc
: /* Run each hook in HOOKS.
2344 Each argument should be a symbol, a hook variable.
2345 These symbols are processed in the order specified.
2346 If a hook symbol has a non-nil value, that value may be a function
2347 or a list of functions to be called to run the hook.
2348 If the value is a function, it is called with no arguments.
2349 If it is a list, the elements are called, in order, with no arguments.
2351 Major modes should not use this function directly to run their mode
2352 hook; they should use `run-mode-hooks' instead.
2354 Do not use `make-local-variable' to make a hook variable buffer-local.
2355 Instead, use `add-hook' and specify t for the LOCAL argument.
2356 usage: (run-hooks &rest HOOKS) */)
2357 (ptrdiff_t nargs
, Lisp_Object
*args
)
2359 Lisp_Object hook
[1];
2362 for (i
= 0; i
< nargs
; i
++)
2365 run_hook_with_args (1, hook
, funcall_nil
);
2371 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2372 Srun_hook_with_args
, 1, MANY
, 0,
2373 doc
: /* Run HOOK with the specified arguments ARGS.
2374 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2375 value, that value may be a function or a list of functions to be
2376 called to run the hook. If the value is a function, it is called with
2377 the given arguments and its return value is returned. If it is a list
2378 of functions, those functions are called, in order,
2379 with the given arguments ARGS.
2380 It is best not to depend on the value returned by `run-hook-with-args',
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 HOOK &rest ARGS) */)
2386 (ptrdiff_t nargs
, Lisp_Object
*args
)
2388 return run_hook_with_args (nargs
, args
, funcall_nil
);
2391 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2392 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2393 doc
: /* Run HOOK with the specified arguments ARGS.
2394 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2395 value, that value may be a function or a list of functions to be
2396 called to run the hook. If the value is a function, it is called with
2397 the given arguments and its return value is returned.
2398 If it is a list of functions, those functions are called, in order,
2399 with the given arguments ARGS, until one of them
2400 returns a non-nil value. Then we return that value.
2401 However, if they all return nil, we return nil.
2403 Do not use `make-local-variable' to make a hook variable buffer-local.
2404 Instead, use `add-hook' and specify t for the LOCAL argument.
2405 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2406 (ptrdiff_t nargs
, Lisp_Object
*args
)
2408 return run_hook_with_args (nargs
, args
, Ffuncall
);
2412 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2414 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2417 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2418 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2419 doc
: /* Run HOOK with the specified arguments ARGS.
2420 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2421 value, that value may be a function or a list of functions to be
2422 called to run the hook. If the value is a function, it is called with
2423 the given arguments and its return value is returned.
2424 If it is a list of functions, those functions are called, in order,
2425 with the given arguments ARGS, until one of them returns nil.
2426 Then we return nil. However, if they all return non-nil, we return non-nil.
2428 Do not use `make-local-variable' to make a hook variable buffer-local.
2429 Instead, use `add-hook' and specify t for the LOCAL argument.
2430 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2431 (ptrdiff_t nargs
, Lisp_Object
*args
)
2433 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2437 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2439 Lisp_Object tmp
= args
[0], ret
;
2442 ret
= Ffuncall (nargs
, args
);
2448 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2449 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2450 I.e. instead of calling each function FUN directly with arguments ARGS,
2451 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2452 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2453 aborts and returns that value.
2454 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2455 (ptrdiff_t nargs
, Lisp_Object
*args
)
2457 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2460 /* ARGS[0] should be a hook symbol.
2461 Call each of the functions in the hook value, passing each of them
2462 as arguments all the rest of ARGS (all NARGS - 1 elements).
2463 FUNCALL specifies how to call each function on the hook.
2464 The caller (or its caller, etc) must gcpro all of ARGS,
2465 except that it isn't necessary to gcpro ARGS[0]. */
2468 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2469 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2471 Lisp_Object sym
, val
, ret
= Qnil
;
2472 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2474 /* If we are dying or still initializing,
2475 don't do anything--it would probably crash if we tried. */
2476 if (NILP (Vrun_hooks
))
2480 val
= find_symbol_value (sym
);
2482 if (EQ (val
, Qunbound
) || NILP (val
))
2484 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2487 return funcall (nargs
, args
);
2491 Lisp_Object global_vals
= Qnil
;
2492 GCPRO3 (sym
, val
, global_vals
);
2495 CONSP (val
) && NILP (ret
);
2498 if (EQ (XCAR (val
), Qt
))
2500 /* t indicates this hook has a local binding;
2501 it means to run the global binding too. */
2502 global_vals
= Fdefault_value (sym
);
2503 if (NILP (global_vals
)) continue;
2505 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2507 args
[0] = global_vals
;
2508 ret
= funcall (nargs
, args
);
2513 CONSP (global_vals
) && NILP (ret
);
2514 global_vals
= XCDR (global_vals
))
2516 args
[0] = XCAR (global_vals
);
2517 /* In a global value, t should not occur. If it does, we
2518 must ignore it to avoid an endless loop. */
2519 if (!EQ (args
[0], Qt
))
2520 ret
= funcall (nargs
, args
);
2526 args
[0] = XCAR (val
);
2527 ret
= funcall (nargs
, args
);
2536 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2539 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2541 Lisp_Object temp
[3];
2546 Frun_hook_with_args (3, temp
);
2549 /* Apply fn to arg. */
2551 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2553 struct gcpro gcpro1
;
2557 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2560 Lisp_Object args
[2];
2564 RETURN_UNGCPRO (Fapply (2, args
));
2568 /* Call function fn on no arguments. */
2570 call0 (Lisp_Object fn
)
2572 struct gcpro gcpro1
;
2575 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2578 /* Call function fn with 1 argument arg1. */
2581 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2583 struct gcpro gcpro1
;
2584 Lisp_Object args
[2];
2590 RETURN_UNGCPRO (Ffuncall (2, args
));
2593 /* Call function fn with 2 arguments arg1, arg2. */
2596 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2598 struct gcpro gcpro1
;
2599 Lisp_Object args
[3];
2605 RETURN_UNGCPRO (Ffuncall (3, args
));
2608 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2611 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2613 struct gcpro gcpro1
;
2614 Lisp_Object args
[4];
2621 RETURN_UNGCPRO (Ffuncall (4, args
));
2624 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2627 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2630 struct gcpro gcpro1
;
2631 Lisp_Object args
[5];
2639 RETURN_UNGCPRO (Ffuncall (5, args
));
2642 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2645 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2646 Lisp_Object arg4
, Lisp_Object arg5
)
2648 struct gcpro gcpro1
;
2649 Lisp_Object args
[6];
2658 RETURN_UNGCPRO (Ffuncall (6, args
));
2661 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2664 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2665 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2667 struct gcpro gcpro1
;
2668 Lisp_Object args
[7];
2678 RETURN_UNGCPRO (Ffuncall (7, args
));
2681 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2684 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2685 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2687 struct gcpro gcpro1
;
2688 Lisp_Object args
[8];
2699 RETURN_UNGCPRO (Ffuncall (8, args
));
2702 /* The caller should GCPRO all the elements of ARGS. */
2704 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2705 doc
: /* Non-nil if OBJECT is a function. */)
2706 (Lisp_Object object
)
2708 if (FUNCTIONP (object
))
2713 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2714 doc
: /* Call first argument as a function, passing remaining arguments to it.
2715 Return the value that function returns.
2716 Thus, (funcall 'cons 'x 'y) returns (x . y).
2717 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2718 (ptrdiff_t nargs
, Lisp_Object
*args
)
2720 Lisp_Object fun
, original_fun
;
2722 ptrdiff_t numargs
= nargs
- 1;
2723 Lisp_Object lisp_numargs
;
2725 struct backtrace backtrace
;
2726 register Lisp_Object
*internal_args
;
2731 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2733 if (max_lisp_eval_depth
< 100)
2734 max_lisp_eval_depth
= 100;
2735 if (lisp_eval_depth
> max_lisp_eval_depth
)
2736 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2739 backtrace
.next
= backtrace_list
;
2740 backtrace_list
= &backtrace
;
2741 backtrace
.function
= &args
[0];
2742 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2743 backtrace
.nargs
= nargs
- 1;
2744 backtrace
.debug_on_exit
= 0;
2746 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2749 if (debug_on_next_call
)
2750 do_debug_on_call (Qlambda
);
2754 original_fun
= args
[0];
2758 /* Optimize for no indirection. */
2760 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2761 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2762 fun
= indirect_function (fun
);
2766 if (numargs
< XSUBR (fun
)->min_args
2767 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2769 XSETFASTINT (lisp_numargs
, numargs
);
2770 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2773 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2774 xsignal1 (Qinvalid_function
, original_fun
);
2776 else if (XSUBR (fun
)->max_args
== MANY
)
2777 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2780 if (XSUBR (fun
)->max_args
> numargs
)
2782 internal_args
= alloca (XSUBR (fun
)->max_args
2783 * sizeof *internal_args
);
2784 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2785 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2786 internal_args
[i
] = Qnil
;
2789 internal_args
= args
+ 1;
2790 switch (XSUBR (fun
)->max_args
)
2793 val
= (XSUBR (fun
)->function
.a0 ());
2796 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2799 val
= (XSUBR (fun
)->function
.a2
2800 (internal_args
[0], internal_args
[1]));
2803 val
= (XSUBR (fun
)->function
.a3
2804 (internal_args
[0], internal_args
[1], internal_args
[2]));
2807 val
= (XSUBR (fun
)->function
.a4
2808 (internal_args
[0], internal_args
[1], internal_args
[2],
2812 val
= (XSUBR (fun
)->function
.a5
2813 (internal_args
[0], internal_args
[1], internal_args
[2],
2814 internal_args
[3], internal_args
[4]));
2817 val
= (XSUBR (fun
)->function
.a6
2818 (internal_args
[0], internal_args
[1], internal_args
[2],
2819 internal_args
[3], internal_args
[4], internal_args
[5]));
2822 val
= (XSUBR (fun
)->function
.a7
2823 (internal_args
[0], internal_args
[1], internal_args
[2],
2824 internal_args
[3], internal_args
[4], internal_args
[5],
2829 val
= (XSUBR (fun
)->function
.a8
2830 (internal_args
[0], internal_args
[1], internal_args
[2],
2831 internal_args
[3], internal_args
[4], internal_args
[5],
2832 internal_args
[6], internal_args
[7]));
2837 /* If a subr takes more than 8 arguments without using MANY
2838 or UNEVALLED, we need to extend this function to support it.
2839 Until this is done, there is no way to call the function. */
2844 else if (COMPILEDP (fun
))
2845 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2848 if (EQ (fun
, Qunbound
))
2849 xsignal1 (Qvoid_function
, original_fun
);
2851 xsignal1 (Qinvalid_function
, original_fun
);
2852 funcar
= XCAR (fun
);
2853 if (!SYMBOLP (funcar
))
2854 xsignal1 (Qinvalid_function
, original_fun
);
2855 if (EQ (funcar
, Qlambda
)
2856 || EQ (funcar
, Qclosure
))
2857 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2858 else if (EQ (funcar
, Qautoload
))
2860 Fautoload_do_load (fun
, original_fun
, Qnil
);
2865 xsignal1 (Qinvalid_function
, original_fun
);
2869 if (backtrace
.debug_on_exit
)
2870 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2871 backtrace_list
= backtrace
.next
;
2876 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2878 Lisp_Object args_left
;
2881 register Lisp_Object
*arg_vector
;
2882 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2883 register Lisp_Object tem
;
2886 numargs
= XFASTINT (Flength (args
));
2887 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2890 GCPRO3 (*arg_vector
, args_left
, fun
);
2893 for (i
= 0; i
< numargs
; )
2895 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2896 tem
= eval_sub (tem
);
2897 arg_vector
[i
++] = tem
;
2903 backtrace_list
->args
= arg_vector
;
2904 backtrace_list
->nargs
= i
;
2905 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2907 /* Do the debug-on-exit now, while arg_vector still exists. */
2908 if (backtrace_list
->debug_on_exit
)
2909 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2910 /* Don't do it again when we return to eval. */
2911 backtrace_list
->debug_on_exit
= 0;
2916 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2917 and return the result of evaluation.
2918 FUN must be either a lambda-expression or a compiled-code object. */
2921 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2922 register Lisp_Object
*arg_vector
)
2924 Lisp_Object val
, syms_left
, next
, lexenv
;
2925 ptrdiff_t count
= SPECPDL_INDEX ();
2927 bool optional
, rest
;
2931 if (EQ (XCAR (fun
), Qclosure
))
2933 fun
= XCDR (fun
); /* Drop `closure'. */
2934 lexenv
= XCAR (fun
);
2935 CHECK_LIST_CONS (fun
, fun
);
2939 syms_left
= XCDR (fun
);
2940 if (CONSP (syms_left
))
2941 syms_left
= XCAR (syms_left
);
2943 xsignal1 (Qinvalid_function
, fun
);
2945 else if (COMPILEDP (fun
))
2947 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2948 if (INTEGERP (syms_left
))
2949 /* A byte-code object with a non-nil `push args' slot means we
2950 shouldn't bind any arguments, instead just call the byte-code
2951 interpreter directly; it will push arguments as necessary.
2953 Byte-code objects with either a non-existent, or a nil value for
2954 the `push args' slot (the default), have dynamically-bound
2955 arguments, and use the argument-binding code below instead (as do
2956 all interpreted functions, even lexically bound ones). */
2958 /* If we have not actually read the bytecode string
2959 and constants vector yet, fetch them from the file. */
2960 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2961 Ffetch_bytecode (fun
);
2962 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2963 AREF (fun
, COMPILED_CONSTANTS
),
2964 AREF (fun
, COMPILED_STACK_DEPTH
),
2973 i
= optional
= rest
= 0;
2974 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2978 next
= XCAR (syms_left
);
2979 if (!SYMBOLP (next
))
2980 xsignal1 (Qinvalid_function
, fun
);
2982 if (EQ (next
, Qand_rest
))
2984 else if (EQ (next
, Qand_optional
))
2991 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
2995 arg
= arg_vector
[i
++];
2997 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3001 /* Bind the argument. */
3002 if (!NILP (lexenv
) && SYMBOLP (next
))
3003 /* Lexically bind NEXT by adding it to the lexenv alist. */
3004 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3006 /* Dynamically bind NEXT. */
3007 specbind (next
, arg
);
3011 if (!NILP (syms_left
))
3012 xsignal1 (Qinvalid_function
, fun
);
3014 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3016 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3017 /* Instantiate a new lexical environment. */
3018 specbind (Qinternal_interpreter_environment
, lexenv
);
3021 val
= Fprogn (XCDR (XCDR (fun
)));
3024 /* If we have not actually read the bytecode string
3025 and constants vector yet, fetch them from the file. */
3026 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3027 Ffetch_bytecode (fun
);
3028 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3029 AREF (fun
, COMPILED_CONSTANTS
),
3030 AREF (fun
, COMPILED_STACK_DEPTH
),
3034 return unbind_to (count
, val
);
3037 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3039 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3040 (Lisp_Object object
)
3044 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3046 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3049 tem
= AREF (object
, COMPILED_BYTECODE
);
3050 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3051 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3053 error ("Invalid byte code");
3055 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3056 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3064 register ptrdiff_t count
= SPECPDL_INDEX ();
3065 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3066 if (max_size
<= specpdl_size
)
3068 if (max_specpdl_size
< 400)
3069 max_size
= max_specpdl_size
= 400;
3070 if (max_size
<= specpdl_size
)
3071 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3073 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3074 specpdl_ptr
= specpdl
+ count
;
3077 /* `specpdl_ptr->symbol' is a field which describes which variable is
3078 let-bound, so it can be properly undone when we unbind_to.
3079 It can have the following two shapes:
3080 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3081 a symbol that is not buffer-local (at least at the time
3082 the let binding started). Note also that it should not be
3083 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3085 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3086 variable SYMBOL which can be buffer-local. WHERE tells us
3087 which buffer is affected (or nil if the let-binding affects the
3088 global value of the variable) and BUFFER tells us which buffer was
3089 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3090 BUFFER did not yet have a buffer-local value). */
3093 specbind (Lisp_Object symbol
, Lisp_Object value
)
3095 struct Lisp_Symbol
*sym
;
3097 CHECK_SYMBOL (symbol
);
3098 sym
= XSYMBOL (symbol
);
3099 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3103 switch (sym
->redirect
)
3105 case SYMBOL_VARALIAS
:
3106 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3107 case SYMBOL_PLAINVAL
:
3108 /* The most common case is that of a non-constant symbol with a
3109 trivial value. Make that as fast as we can. */
3110 set_specpdl_symbol (symbol
);
3111 set_specpdl_old_value (SYMBOL_VAL (sym
));
3112 specpdl_ptr
->func
= NULL
;
3115 SET_SYMBOL_VAL (sym
, value
);
3117 set_internal (symbol
, value
, Qnil
, 1);
3119 case SYMBOL_LOCALIZED
:
3120 if (SYMBOL_BLV (sym
)->frame_local
)
3121 error ("Frame-local vars cannot be let-bound");
3122 case SYMBOL_FORWARDED
:
3124 Lisp_Object ovalue
= find_symbol_value (symbol
);
3125 specpdl_ptr
->func
= 0;
3126 set_specpdl_old_value (ovalue
);
3128 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3129 || (EQ (SYMBOL_BLV (sym
)->where
,
3130 SYMBOL_BLV (sym
)->frame_local
?
3131 Fselected_frame () : Fcurrent_buffer ())));
3133 if (sym
->redirect
== SYMBOL_LOCALIZED
3134 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3136 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3138 /* For a local variable, record both the symbol and which
3139 buffer's or frame's value we are saving. */
3140 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3142 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3143 || (blv_found (SYMBOL_BLV (sym
))
3144 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3147 else if (sym
->redirect
== SYMBOL_LOCALIZED
3148 && blv_found (SYMBOL_BLV (sym
)))
3149 where
= SYMBOL_BLV (sym
)->where
;
3153 /* We're not using the `unused' slot in the specbinding
3154 structure because this would mean we have to do more
3155 work for simple variables. */
3156 /* FIXME: The third value `current_buffer' is only used in
3157 let_shadows_buffer_binding_p which is itself only used
3158 in set_internal for local_if_set. */
3159 eassert (NILP (where
) || EQ (where
, cur_buf
));
3160 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3162 /* If SYMBOL is a per-buffer variable which doesn't have a
3163 buffer-local value here, make the `let' change the global
3164 value by changing the value of SYMBOL in all buffers not
3165 having their own value. This is consistent with what
3166 happens with other buffer-local variables. */
3168 && sym
->redirect
== SYMBOL_FORWARDED
)
3170 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3172 Fset_default (symbol
, value
);
3177 set_specpdl_symbol (symbol
);
3180 set_internal (symbol
, value
, Qnil
, 1);
3183 default: emacs_abort ();
3188 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3190 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3192 specpdl_ptr
->func
= function
;
3193 set_specpdl_symbol (Qnil
);
3194 set_specpdl_old_value (arg
);
3199 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3201 Lisp_Object quitf
= Vquit_flag
;
3202 struct gcpro gcpro1
, gcpro2
;
3204 GCPRO2 (value
, quitf
);
3207 while (specpdl_ptr
!= specpdl
+ count
)
3209 /* Copy the binding, and decrement specpdl_ptr, before we do
3210 the work to unbind it. We decrement first
3211 so that an error in unbinding won't try to unbind
3212 the same entry again, and we copy the binding first
3213 in case more bindings are made during some of the code we run. */
3215 struct specbinding this_binding
;
3216 this_binding
= *--specpdl_ptr
;
3218 if (this_binding
.func
!= 0)
3219 (*this_binding
.func
) (this_binding
.old_value
);
3220 /* If the symbol is a list, it is really (SYMBOL WHERE
3221 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3222 frame. If WHERE is a buffer or frame, this indicates we
3223 bound a variable that had a buffer-local or frame-local
3224 binding. WHERE nil means that the variable had the default
3225 value when it was bound. CURRENT-BUFFER is the buffer that
3226 was current when the variable was bound. */
3227 else if (CONSP (this_binding
.symbol
))
3229 Lisp_Object symbol
, where
;
3231 symbol
= XCAR (this_binding
.symbol
);
3232 where
= XCAR (XCDR (this_binding
.symbol
));
3235 Fset_default (symbol
, this_binding
.old_value
);
3236 /* If `where' is non-nil, reset the value in the appropriate
3237 local binding, but only if that binding still exists. */
3238 else if (BUFFERP (where
)
3239 ? !NILP (Flocal_variable_p (symbol
, where
))
3240 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3241 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3243 /* If variable has a trivial value (no forwarding), we can
3244 just set it. No need to check for constant symbols here,
3245 since that was already done by specbind. */
3246 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3247 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3248 this_binding
.old_value
);
3250 /* NOTE: we only ever come here if make_local_foo was used for
3251 the first time on this var within this let. */
3252 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3255 if (NILP (Vquit_flag
) && !NILP (quitf
))
3262 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3263 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3264 A special variable is one that will be bound dynamically, even in a
3265 context where binding is lexical by default. */)
3266 (Lisp_Object symbol
)
3268 CHECK_SYMBOL (symbol
);
3269 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3273 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3274 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3275 The debugger is entered when that frame exits, if the flag is non-nil. */)
3276 (Lisp_Object level
, Lisp_Object flag
)
3278 register struct backtrace
*backlist
= backtrace_list
;
3279 register EMACS_INT i
;
3281 CHECK_NUMBER (level
);
3283 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3285 backlist
= backlist
->next
;
3289 backlist
->debug_on_exit
= !NILP (flag
);
3294 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3295 doc
: /* Print a trace of Lisp function calls currently active.
3296 Output stream used is value of `standard-output'. */)
3299 register struct backtrace
*backlist
= backtrace_list
;
3302 struct gcpro gcpro1
;
3303 Lisp_Object old_print_level
= Vprint_level
;
3305 if (NILP (Vprint_level
))
3306 XSETFASTINT (Vprint_level
, 8);
3313 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3314 if (backlist
->nargs
== UNEVALLED
)
3316 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3317 write_string ("\n", -1);
3321 tem
= *backlist
->function
;
3322 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3323 write_string ("(", -1);
3324 if (backlist
->nargs
== MANY
)
3325 { /* FIXME: Can this happen? */
3327 for (tail
= *backlist
->args
; !NILP (tail
); tail
= Fcdr (tail
))
3330 write_string (" ", -1);
3331 Fprin1 (Fcar (tail
), Qnil
);
3338 for (i
= 0; i
< backlist
->nargs
; i
++)
3340 if (i
) write_string (" ", -1);
3341 Fprin1 (backlist
->args
[i
], Qnil
);
3344 write_string (")\n", -1);
3346 backlist
= backlist
->next
;
3349 Vprint_level
= old_print_level
;
3354 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3355 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3356 If that frame has not evaluated the arguments yet (or is a special form),
3357 the value is (nil FUNCTION ARG-FORMS...).
3358 If that frame has evaluated its arguments and called its function already,
3359 the value is (t FUNCTION ARG-VALUES...).
3360 A &rest arg is represented as the tail of the list ARG-VALUES.
3361 FUNCTION is whatever was supplied as car of evaluated list,
3362 or a lambda expression for macro calls.
3363 If NFRAMES is more than the number of frames, the value is nil. */)
3364 (Lisp_Object nframes
)
3366 register struct backtrace
*backlist
= backtrace_list
;
3367 register EMACS_INT i
;
3370 CHECK_NATNUM (nframes
);
3372 /* Find the frame requested. */
3373 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3374 backlist
= backlist
->next
;
3378 if (backlist
->nargs
== UNEVALLED
)
3379 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3382 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3383 tem
= *backlist
->args
;
3385 tem
= Flist (backlist
->nargs
, backlist
->args
);
3387 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3394 mark_backtrace (void)
3396 register struct backtrace
*backlist
;
3399 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3401 mark_object (*backlist
->function
);
3403 if (backlist
->nargs
== UNEVALLED
3404 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3407 i
= backlist
->nargs
;
3409 mark_object (backlist
->args
[i
]);
3417 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3418 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3419 If Lisp code tries to increase the total number past this amount,
3420 an error is signaled.
3421 You can safely use a value considerably larger than the default value,
3422 if that proves inconveniently small. However, if you increase it too far,
3423 Emacs could run out of memory trying to make the stack bigger. */);
3425 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3426 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3428 This limit serves to catch infinite recursions for you before they cause
3429 actual stack overflow in C, which would be fatal for Emacs.
3430 You can safely make it considerably larger than its default value,
3431 if that proves inconveniently small. However, if you increase it too far,
3432 Emacs could overflow the real C stack, and crash. */);
3434 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3435 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3436 If the value is t, that means do an ordinary quit.
3437 If the value equals `throw-on-input', that means quit by throwing
3438 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3439 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3440 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3443 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3444 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3445 Note that `quit-flag' will still be set by typing C-g,
3446 so a quit will be signaled as soon as `inhibit-quit' is nil.
3447 To prevent this happening, set `quit-flag' to nil
3448 before making `inhibit-quit' nil. */);
3449 Vinhibit_quit
= Qnil
;
3451 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3452 DEFSYM (Qautoload
, "autoload");
3453 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3454 DEFSYM (Qmacro
, "macro");
3455 DEFSYM (Qdeclare
, "declare");
3457 /* Note that the process handling also uses Qexit, but we don't want
3458 to staticpro it twice, so we just do it here. */
3459 DEFSYM (Qexit
, "exit");
3461 DEFSYM (Qinteractive
, "interactive");
3462 DEFSYM (Qcommandp
, "commandp");
3463 DEFSYM (Qand_rest
, "&rest");
3464 DEFSYM (Qand_optional
, "&optional");
3465 DEFSYM (Qclosure
, "closure");
3466 DEFSYM (Qdebug
, "debug");
3468 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3469 doc
: /* Non-nil means never enter the debugger.
3470 Normally set while the debugger is already active, to avoid recursive
3472 Vinhibit_debugger
= Qnil
;
3474 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3475 doc
: /* Non-nil means enter debugger if an error is signaled.
3476 Does not apply to errors handled by `condition-case' or those
3477 matched by `debug-ignored-errors'.
3478 If the value is a list, an error only means to enter the debugger
3479 if one of its condition symbols appears in the list.
3480 When you evaluate an expression interactively, this variable
3481 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3482 The command `toggle-debug-on-error' toggles this.
3483 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3484 Vdebug_on_error
= Qnil
;
3486 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3487 doc
: /* List of errors for which the debugger should not be called.
3488 Each element may be a condition-name or a regexp that matches error messages.
3489 If any element applies to a given error, that error skips the debugger
3490 and just returns to top level.
3491 This overrides the variable `debug-on-error'.
3492 It does not apply to errors handled by `condition-case'. */);
3493 Vdebug_ignored_errors
= Qnil
;
3495 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3496 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3497 Does not apply if quit is handled by a `condition-case'. */);
3500 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3501 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3503 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3504 doc
: /* Non-nil means debugger may continue execution.
3505 This is nil when the debugger is called under circumstances where it
3506 might not be safe to continue. */);
3507 debugger_may_continue
= 1;
3509 DEFVAR_LISP ("debugger", Vdebugger
,
3510 doc
: /* Function to call to invoke debugger.
3511 If due to frame exit, args are `exit' and the value being returned;
3512 this function's value will be returned instead of that.
3513 If due to error, args are `error' and a list of the args to `signal'.
3514 If due to `apply' or `funcall' entry, one arg, `lambda'.
3515 If due to `eval' entry, one arg, t. */);
3518 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3519 doc
: /* If non-nil, this is a function for `signal' to call.
3520 It receives the same arguments that `signal' was given.
3521 The Edebug package uses this to regain control. */);
3522 Vsignal_hook_function
= Qnil
;
3524 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3525 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3526 Note that `debug-on-error', `debug-on-quit' and friends
3527 still determine whether to handle the particular condition. */);
3528 Vdebug_on_signal
= Qnil
;
3530 /* When lexical binding is being used,
3531 Vinternal_interpreter_environment is non-nil, and contains an alist
3532 of lexically-bound variable, or (t), indicating an empty
3533 environment. The lisp name of this variable would be
3534 `internal-interpreter-environment' if it weren't hidden.
3535 Every element of this list can be either a cons (VAR . VAL)
3536 specifying a lexical binding, or a single symbol VAR indicating
3537 that this variable should use dynamic scoping. */
3538 DEFSYM (Qinternal_interpreter_environment
,
3539 "internal-interpreter-environment");
3540 DEFVAR_LISP ("internal-interpreter-environment",
3541 Vinternal_interpreter_environment
,
3542 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3543 When lexical binding is not being used, this variable is nil.
3544 A value of `(t)' indicates an empty environment, otherwise it is an
3545 alist of active lexical bindings. */);
3546 Vinternal_interpreter_environment
= Qnil
;
3547 /* Don't export this variable to Elisp, so no one can mess with it
3548 (Just imagine if someone makes it buffer-local). */
3549 Funintern (Qinternal_interpreter_environment
, Qnil
);
3551 DEFSYM (Vrun_hooks
, "run-hooks");
3553 staticpro (&Vautoload_queue
);
3554 Vautoload_queue
= Qnil
;
3555 staticpro (&Vsignaling_function
);
3556 Vsignaling_function
= Qnil
;
3558 inhibit_lisp_code
= Qnil
;
3569 defsubr (&Sfunction
);
3571 defsubr (&Sdefvaralias
);
3572 defsubr (&Sdefconst
);
3573 defsubr (&Smake_var_non_special
);
3577 defsubr (&Smacroexpand
);
3580 defsubr (&Sunwind_protect
);
3581 defsubr (&Scondition_case
);
3583 defsubr (&Sinteractive_p
);
3584 defsubr (&Scalled_interactively_p
);
3585 defsubr (&Scommandp
);
3586 defsubr (&Sautoload
);
3587 defsubr (&Sautoload_do_load
);
3590 defsubr (&Sfuncall
);
3591 defsubr (&Srun_hooks
);
3592 defsubr (&Srun_hook_with_args
);
3593 defsubr (&Srun_hook_with_args_until_success
);
3594 defsubr (&Srun_hook_with_args_until_failure
);
3595 defsubr (&Srun_hook_wrapped
);
3596 defsubr (&Sfetch_bytecode
);
3597 defsubr (&Sbacktrace_debug
);
3598 defsubr (&Sbacktrace
);
3599 defsubr (&Sbacktrace_frame
);
3600 defsubr (&Sspecial_variable_p
);
3601 defsubr (&Sfunctionp
);