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 /* Set to non-zero while processing X events. Checked in Feval to
121 make sure the Lisp interpreter isn't called from a signal handler,
122 which is unsafe because the interpreter isn't reentrant. */
126 /* If non-nil, Lisp code must not be run since some part of Emacs is
127 in an inconsistent state. Currently, x-create-frame uses this to
128 avoid triggering window-configuration-change-hook while the new
129 frame is half-initialized. */
130 Lisp_Object inhibit_lisp_code
;
132 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
133 static bool interactive_p (void);
134 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
136 /* Functions to set Lisp_Object slots of struct specbinding. */
139 set_specpdl_symbol (Lisp_Object symbol
)
141 specpdl_ptr
->symbol
= symbol
;
145 set_specpdl_old_value (Lisp_Object oldval
)
147 specpdl_ptr
->old_value
= oldval
;
151 init_eval_once (void)
154 specpdl
= xmalloc (size
* sizeof *specpdl
);
156 specpdl_ptr
= specpdl
;
157 /* Don't forget to update docs (lispref node "Local Variables"). */
158 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
159 max_lisp_eval_depth
= 600;
167 specpdl_ptr
= specpdl
;
172 debug_on_next_call
= 0;
177 /* This is less than the initial value of num_nonmacro_input_events. */
178 when_entered_debugger
= -1;
181 /* Unwind-protect function used by call_debugger. */
184 restore_stack_limits (Lisp_Object data
)
186 max_specpdl_size
= XINT (XCAR (data
));
187 max_lisp_eval_depth
= XINT (XCDR (data
));
191 /* Call the Lisp debugger, giving it argument ARG. */
194 call_debugger (Lisp_Object arg
)
196 bool debug_while_redisplaying
;
197 ptrdiff_t count
= SPECPDL_INDEX ();
199 EMACS_INT old_max
= max_specpdl_size
;
201 /* Temporarily bump up the stack limits,
202 so the debugger won't run out of stack. */
204 max_specpdl_size
+= 1;
205 record_unwind_protect (restore_stack_limits
,
206 Fcons (make_number (old_max
),
207 make_number (max_lisp_eval_depth
)));
208 max_specpdl_size
= old_max
;
210 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
211 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
213 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
214 max_specpdl_size
= SPECPDL_INDEX () + 100;
216 #ifdef HAVE_WINDOW_SYSTEM
217 if (display_hourglass_p
)
221 debug_on_next_call
= 0;
222 when_entered_debugger
= num_nonmacro_input_events
;
224 /* Resetting redisplaying_p to 0 makes sure that debug output is
225 displayed if the debugger is invoked during redisplay. */
226 debug_while_redisplaying
= redisplaying_p
;
228 specbind (intern ("debugger-may-continue"),
229 debug_while_redisplaying
? Qnil
: Qt
);
230 specbind (Qinhibit_redisplay
, Qnil
);
231 specbind (Qinhibit_debugger
, Qt
);
233 #if 0 /* Binding this prevents execution of Lisp code during
234 redisplay, which necessarily leads to display problems. */
235 specbind (Qinhibit_eval_during_redisplay
, Qt
);
238 val
= apply1 (Vdebugger
, arg
);
240 /* Interrupting redisplay and resuming it later is not safe under
241 all circumstances. So, when the debugger returns, abort the
242 interrupted redisplay by going back to the top-level. */
243 if (debug_while_redisplaying
)
246 return unbind_to (count
, val
);
250 do_debug_on_call (Lisp_Object code
)
252 debug_on_next_call
= 0;
253 backtrace_list
->debug_on_exit
= 1;
254 call_debugger (Fcons (code
, Qnil
));
257 /* NOTE!!! Every function that can call EVAL must protect its args
258 and temporaries from garbage collection while it needs them.
259 The definition of `For' shows what you have to do. */
261 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
262 doc
: /* Eval args until one of them yields non-nil, then return that value.
263 The remaining args are not evalled at all.
264 If all args return nil, return nil.
265 usage: (or CONDITIONS...) */)
268 register Lisp_Object val
= Qnil
;
275 val
= eval_sub (XCAR (args
));
285 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
286 doc
: /* Eval args until one of them yields nil, then return nil.
287 The remaining args are not evalled at all.
288 If no arg yields nil, return the last arg's value.
289 usage: (and CONDITIONS...) */)
292 register Lisp_Object val
= Qt
;
299 val
= eval_sub (XCAR (args
));
309 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
310 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
311 Returns the value of THEN or the value of the last of the ELSE's.
312 THEN must be one expression, but ELSE... can be zero or more expressions.
313 If COND yields nil, and there are no ELSE's, the value is nil.
314 usage: (if COND THEN ELSE...) */)
317 register Lisp_Object cond
;
321 cond
= eval_sub (Fcar (args
));
325 return eval_sub (Fcar (Fcdr (args
)));
326 return Fprogn (Fcdr (Fcdr (args
)));
329 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
330 doc
: /* Try each clause until one succeeds.
331 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
332 and, if the value is non-nil, this clause succeeds:
333 then the expressions in BODY are evaluated and the last one's
334 value is the value of the cond-form.
335 If no clause succeeds, cond returns nil.
336 If a clause has one element, as in (CONDITION),
337 CONDITION's value if non-nil is returned from the cond-form.
338 usage: (cond CLAUSES...) */)
341 register Lisp_Object clause
, val
;
348 clause
= Fcar (args
);
349 val
= eval_sub (Fcar (clause
));
352 if (!EQ (XCDR (clause
), Qnil
))
353 val
= Fprogn (XCDR (clause
));
363 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
364 doc
: /* Eval BODY forms sequentially and return value of last one.
365 usage: (progn BODY...) */)
368 register Lisp_Object val
= Qnil
;
375 val
= eval_sub (XCAR (args
));
383 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
384 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
385 The value of FIRST is saved during the evaluation of the remaining args,
386 whose values are discarded.
387 usage: (prog1 FIRST BODY...) */)
391 register Lisp_Object args_left
;
392 struct gcpro gcpro1
, gcpro2
;
398 val
= eval_sub (XCAR (args_left
));
399 while (CONSP (args_left
= XCDR (args_left
)))
400 eval_sub (XCAR (args_left
));
406 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
407 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
408 The value of FORM2 is saved during the evaluation of the
409 remaining args, whose values are discarded.
410 usage: (prog2 FORM1 FORM2 BODY...) */)
416 eval_sub (XCAR (args
));
418 return Fprog1 (XCDR (args
));
421 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
422 doc
: /* Set each SYM to the value of its VAL.
423 The symbols SYM are variables; they are literal (not evaluated).
424 The values VAL are expressions; they are evaluated.
425 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
426 The second VAL is not computed until after the first SYM is set, and so on;
427 each VAL can use the new value of variables set earlier in the `setq'.
428 The return value of the `setq' form is the value of the last VAL.
429 usage: (setq [SYM VAL]...) */)
432 register Lisp_Object args_left
;
433 register Lisp_Object val
, sym
, lex_binding
;
444 val
= eval_sub (Fcar (Fcdr (args_left
)));
445 sym
= Fcar (args_left
);
447 /* Like for eval_sub, we do not check declared_special here since
448 it's been done when let-binding. */
449 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
451 && !NILP (lex_binding
452 = Fassq (sym
, Vinternal_interpreter_environment
)))
453 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
455 Fset (sym
, val
); /* SYM is dynamically bound. */
457 args_left
= Fcdr (Fcdr (args_left
));
459 while (!NILP (args_left
));
465 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
466 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
467 Warning: `quote' does not construct its return value, but just returns
468 the value that was pre-constructed by the Lisp reader (see info node
469 `(elisp)Printed Representation').
470 This means that '(a . b) is not identical to (cons 'a 'b): the former
471 does not cons. Quoting should be reserved for constants that will
472 never be modified by side-effects, unless you like self-modifying code.
473 See the common pitfall in info node `(elisp)Rearrangement' for an example
474 of unexpected results when a quoted object is modified.
475 usage: (quote ARG) */)
478 if (!NILP (Fcdr (args
)))
479 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
483 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
484 doc
: /* Like `quote', but preferred for objects which are functions.
485 In byte compilation, `function' causes its argument to be compiled.
486 `quote' cannot do that.
487 usage: (function ARG) */)
490 Lisp_Object quoted
= XCAR (args
);
492 if (!NILP (Fcdr (args
)))
493 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
495 if (!NILP (Vinternal_interpreter_environment
)
497 && EQ (XCAR (quoted
), Qlambda
))
498 /* This is a lambda expression within a lexical environment;
499 return an interpreted closure instead of a simple lambda. */
500 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
503 /* Simply quote the argument. */
508 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
509 doc
: /* Return t if the containing function was run directly by user input.
510 This means that the function was called with `call-interactively'
511 \(which includes being called as the binding of a key)
512 and input is currently coming from the keyboard (not a keyboard macro),
513 and Emacs is not running in batch mode (`noninteractive' is nil).
515 The only known proper use of `interactive-p' is in deciding whether to
516 display a helpful message, or how to display it. If you're thinking
517 of using it for any other purpose, it is quite likely that you're
518 making a mistake. Think: what do you want to do when the command is
519 called from a keyboard macro?
521 To test whether your function was called with `call-interactively',
522 either (i) add an extra optional argument and give it an `interactive'
523 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
524 use `called-interactively-p'. */)
527 return interactive_p () ? Qt
: Qnil
;
531 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
532 doc
: /* Return t if the containing function was called by `call-interactively'.
533 If KIND is `interactive', then only return t if the call was made
534 interactively by the user, i.e. not in `noninteractive' mode nor
535 when `executing-kbd-macro'.
536 If KIND is `any', on the other hand, it will return t for any kind of
537 interactive call, including being called as the binding of a key, or
538 from a keyboard macro, or in `noninteractive' mode.
540 The only known proper use of `interactive' for KIND is in deciding
541 whether to display a helpful message, or how to display it. If you're
542 thinking of using it for any other purpose, it is quite likely that
543 you're making a mistake. Think: what do you want to do when the
544 command is called from a keyboard macro?
546 Instead of using this function, it is sometimes cleaner to give your
547 function an extra optional argument whose `interactive' spec specifies
548 non-nil unconditionally (\"p\" is a good way to do this), or via
549 \(not (or executing-kbd-macro noninteractive)). */)
552 return (((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
558 /* Return true if function in which this appears was called using
559 call-interactively and is not a built-in. */
564 struct backtrace
*btp
;
567 btp
= backtrace_list
;
569 /* If this isn't a byte-compiled function, there may be a frame at
570 the top for Finteractive_p. If so, skip it. */
571 fun
= Findirect_function (*btp
->function
, Qnil
);
572 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
573 || XSUBR (fun
) == &Scalled_interactively_p
))
576 /* If we're running an Emacs 18-style byte-compiled function, there
577 may be a frame for Fbytecode at the top level. In any version of
578 Emacs there can be Fbytecode frames for subexpressions evaluated
579 inside catch and condition-case. Skip past them.
581 If this isn't a byte-compiled function, then we may now be
582 looking at several frames for special forms. Skip past them. */
584 && (EQ (*btp
->function
, Qbytecode
)
585 || btp
->nargs
== UNEVALLED
))
588 /* `btp' now points at the frame of the innermost function that isn't
589 a special form, ignoring frames for Finteractive_p and/or
590 Fbytecode at the top. If this frame is for a built-in function
591 (such as load or eval-region) return false. */
592 fun
= Findirect_function (*btp
->function
, Qnil
);
596 /* `btp' points to the frame of a Lisp function that called interactive-p.
597 Return t if that function was called interactively. */
598 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
604 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
605 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
606 Aliased variables always have the same value; setting one sets the other.
607 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
608 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
609 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
610 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
611 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
612 The return value is BASE-VARIABLE. */)
613 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
615 struct Lisp_Symbol
*sym
;
617 CHECK_SYMBOL (new_alias
);
618 CHECK_SYMBOL (base_variable
);
620 sym
= XSYMBOL (new_alias
);
623 /* Not sure why, but why not? */
624 error ("Cannot make a constant an alias");
626 switch (sym
->redirect
)
628 case SYMBOL_FORWARDED
:
629 error ("Cannot make an internal variable an alias");
630 case SYMBOL_LOCALIZED
:
631 error ("Don't know how to make a localized variable an alias");
634 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
635 If n_a is bound, but b_v is not, set the value of b_v to n_a,
636 so that old-code that affects n_a before the aliasing is setup
638 if (NILP (Fboundp (base_variable
)))
639 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
642 struct specbinding
*p
;
644 for (p
= specpdl_ptr
; p
> specpdl
; )
645 if ((--p
)->func
== NULL
647 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
648 error ("Don't know how to make a let-bound variable an alias");
651 sym
->declared_special
= 1;
652 XSYMBOL (base_variable
)->declared_special
= 1;
653 sym
->redirect
= SYMBOL_VARALIAS
;
654 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
655 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
656 LOADHIST_ATTACH (new_alias
);
657 /* Even if docstring is nil: remove old docstring. */
658 Fput (new_alias
, Qvariable_documentation
, docstring
);
660 return base_variable
;
664 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
665 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
666 You are not required to define a variable in order to use it, but
667 defining it lets you supply an initial value and documentation, which
668 can be referred to by the Emacs help facilities and other programming
669 tools. The `defvar' form also declares the variable as \"special\",
670 so that it is always dynamically bound even if `lexical-binding' is t.
672 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
673 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
674 default value is what is set; buffer-local values are not affected.
675 If INITVALUE is missing, SYMBOL's value is not set.
677 If SYMBOL has a local binding, then this form affects the local
678 binding. This is usually not what you want. Thus, if you need to
679 load a file defining variables, with this form or with `defconst' or
680 `defcustom', you should always load that file _outside_ any bindings
681 for these variables. \(`defconst' and `defcustom' behave similarly in
684 The optional argument DOCSTRING is a documentation string for the
687 To define a user option, use `defcustom' instead of `defvar'.
688 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
691 register Lisp_Object sym
, tem
, tail
;
695 if (!NILP (Fcdr (Fcdr (tail
))))
696 error ("Too many arguments");
698 tem
= Fdefault_boundp (sym
);
701 /* Do it before evaluating the initial value, for self-references. */
702 XSYMBOL (sym
)->declared_special
= 1;
705 Fset_default (sym
, eval_sub (Fcar (tail
)));
707 { /* Check if there is really a global binding rather than just a let
708 binding that shadows the global unboundness of the var. */
709 struct specbinding
*pdl
= specpdl_ptr
;
710 while (pdl
> specpdl
)
712 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
713 && EQ (pdl
->old_value
, Qunbound
))
715 message_with_string ("Warning: defvar ignored because %s is let-bound",
716 SYMBOL_NAME (sym
), 1);
725 if (!NILP (Vpurify_flag
))
726 tem
= Fpurecopy (tem
);
727 Fput (sym
, Qvariable_documentation
, tem
);
729 LOADHIST_ATTACH (sym
);
731 else if (!NILP (Vinternal_interpreter_environment
)
732 && !XSYMBOL (sym
)->declared_special
)
733 /* A simple (defvar foo) with lexical scoping does "nothing" except
734 declare that var to be dynamically scoped *locally* (i.e. within
735 the current file or let-block). */
736 Vinternal_interpreter_environment
=
737 Fcons (sym
, Vinternal_interpreter_environment
);
740 /* Simple (defvar <var>) should not count as a definition at all.
741 It could get in the way of other definitions, and unloading this
742 package could try to make the variable unbound. */
748 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
749 doc
: /* Define SYMBOL as a constant variable.
750 This declares that neither programs nor users should ever change the
751 value. This constancy is not actually enforced by Emacs Lisp, but
752 SYMBOL is marked as a special variable so that it is never lexically
755 The `defconst' form always sets the value of SYMBOL to the result of
756 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
757 what is set; buffer-local values are not affected. If SYMBOL has a
758 local binding, then this form sets the local binding's value.
759 However, you should normally not make local bindings for variables
760 defined with this form.
762 The optional DOCSTRING specifies the variable's documentation string.
763 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
766 register Lisp_Object sym
, tem
;
769 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
770 error ("Too many arguments");
772 tem
= eval_sub (Fcar (Fcdr (args
)));
773 if (!NILP (Vpurify_flag
))
774 tem
= Fpurecopy (tem
);
775 Fset_default (sym
, tem
);
776 XSYMBOL (sym
)->declared_special
= 1;
777 tem
= Fcar (Fcdr (Fcdr (args
)));
780 if (!NILP (Vpurify_flag
))
781 tem
= Fpurecopy (tem
);
782 Fput (sym
, Qvariable_documentation
, tem
);
784 Fput (sym
, Qrisky_local_variable
, Qt
);
785 LOADHIST_ATTACH (sym
);
789 /* Make SYMBOL lexically scoped. */
790 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
791 Smake_var_non_special
, 1, 1, 0,
792 doc
: /* Internal function. */)
795 CHECK_SYMBOL (symbol
);
796 XSYMBOL (symbol
)->declared_special
= 0;
801 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
802 doc
: /* Bind variables according to VARLIST then eval BODY.
803 The value of the last form in BODY is returned.
804 Each element of VARLIST is a symbol (which is bound to nil)
805 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
806 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
807 usage: (let* VARLIST BODY...) */)
810 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
811 ptrdiff_t count
= SPECPDL_INDEX ();
812 struct gcpro gcpro1
, gcpro2
, gcpro3
;
814 GCPRO3 (args
, elt
, varlist
);
816 lexenv
= Vinternal_interpreter_environment
;
818 varlist
= Fcar (args
);
819 while (CONSP (varlist
))
823 elt
= XCAR (varlist
);
829 else if (! NILP (Fcdr (Fcdr (elt
))))
830 signal_error ("`let' bindings can have only one value-form", elt
);
834 val
= eval_sub (Fcar (Fcdr (elt
)));
837 if (!NILP (lexenv
) && SYMBOLP (var
)
838 && !XSYMBOL (var
)->declared_special
839 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
840 /* Lexically bind VAR by adding it to the interpreter's binding
844 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
845 if (EQ (Vinternal_interpreter_environment
, lexenv
))
846 /* Save the old lexical environment on the specpdl stack,
847 but only for the first lexical binding, since we'll never
848 need to revert to one of the intermediate ones. */
849 specbind (Qinternal_interpreter_environment
, newenv
);
851 Vinternal_interpreter_environment
= newenv
;
856 varlist
= XCDR (varlist
);
859 val
= Fprogn (Fcdr (args
));
860 return unbind_to (count
, val
);
863 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
864 doc
: /* Bind variables according to VARLIST then eval BODY.
865 The value of the last form in BODY is returned.
866 Each element of VARLIST is a symbol (which is bound to nil)
867 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
868 All the VALUEFORMs are evalled before any symbols are bound.
869 usage: (let VARLIST BODY...) */)
872 Lisp_Object
*temps
, tem
, lexenv
;
873 register Lisp_Object elt
, varlist
;
874 ptrdiff_t count
= SPECPDL_INDEX ();
876 struct gcpro gcpro1
, gcpro2
;
879 varlist
= Fcar (args
);
881 /* Make space to hold the values to give the bound variables. */
882 elt
= Flength (varlist
);
883 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
885 /* Compute the values and store them in `temps'. */
887 GCPRO2 (args
, *temps
);
890 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
893 elt
= XCAR (varlist
);
895 temps
[argnum
++] = Qnil
;
896 else if (! NILP (Fcdr (Fcdr (elt
))))
897 signal_error ("`let' bindings can have only one value-form", elt
);
899 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
900 gcpro2
.nvars
= argnum
;
904 lexenv
= Vinternal_interpreter_environment
;
906 varlist
= Fcar (args
);
907 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
911 elt
= XCAR (varlist
);
912 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
913 tem
= temps
[argnum
++];
915 if (!NILP (lexenv
) && SYMBOLP (var
)
916 && !XSYMBOL (var
)->declared_special
917 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
918 /* Lexically bind VAR by adding it to the lexenv alist. */
919 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
921 /* Dynamically bind VAR. */
925 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
926 /* Instantiate a new lexical environment. */
927 specbind (Qinternal_interpreter_environment
, lexenv
);
929 elt
= Fprogn (Fcdr (args
));
931 return unbind_to (count
, elt
);
934 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
935 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
936 The order of execution is thus TEST, BODY, TEST, BODY and so on
937 until TEST returns nil.
938 usage: (while TEST BODY...) */)
941 Lisp_Object test
, body
;
942 struct gcpro gcpro1
, gcpro2
;
948 while (!NILP (eval_sub (test
)))
958 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
959 doc
: /* Return result of expanding macros at top level of FORM.
960 If FORM is not a macro call, it is returned unchanged.
961 Otherwise, the macro is expanded and the expansion is considered
962 in place of FORM. When a non-macro-call results, it is returned.
964 The second optional arg ENVIRONMENT specifies an environment of macro
965 definitions to shadow the loaded ones for use in file byte-compilation. */)
966 (Lisp_Object form
, Lisp_Object environment
)
968 /* With cleanups from Hallvard Furuseth. */
969 register Lisp_Object expander
, sym
, def
, tem
;
973 /* Come back here each time we expand a macro call,
974 in case it expands into another macro call. */
977 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
978 def
= sym
= XCAR (form
);
980 /* Trace symbols aliases to other symbols
981 until we get a symbol that is not an alias. */
982 while (SYMBOLP (def
))
986 tem
= Fassq (sym
, environment
);
989 def
= XSYMBOL (sym
)->function
;
990 if (!EQ (def
, Qunbound
))
995 /* Right now TEM is the result from SYM in ENVIRONMENT,
996 and if TEM is nil then DEF is SYM's function definition. */
999 /* SYM is not mentioned in ENVIRONMENT.
1000 Look at its function definition. */
1001 struct gcpro gcpro1
;
1003 def
= Fautoload_do_load (def
, sym
, Qmacro
);
1005 if (EQ (def
, Qunbound
) || !CONSP (def
))
1006 /* Not defined or definition not suitable. */
1008 if (!EQ (XCAR (def
), Qmacro
))
1010 else expander
= XCDR (def
);
1014 expander
= XCDR (tem
);
1015 if (NILP (expander
))
1019 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1020 if (EQ (form
, newform
))
1029 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1030 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1031 TAG is evalled to get the tag to use; it must not be nil.
1033 Then the BODY is executed.
1034 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1035 If no throw happens, `catch' returns the value of the last BODY form.
1036 If a throw happens, it specifies the value to return from `catch'.
1037 usage: (catch TAG BODY...) */)
1040 register Lisp_Object tag
;
1041 struct gcpro gcpro1
;
1044 tag
= eval_sub (Fcar (args
));
1046 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1049 /* Set up a catch, then call C function FUNC on argument ARG.
1050 FUNC should return a Lisp_Object.
1051 This is how catches are done from within C code. */
1054 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1056 /* This structure is made part of the chain `catchlist'. */
1059 /* Fill in the components of c, and put it on the list. */
1063 c
.backlist
= backtrace_list
;
1064 c
.handlerlist
= handlerlist
;
1065 c
.lisp_eval_depth
= lisp_eval_depth
;
1066 c
.pdlcount
= SPECPDL_INDEX ();
1067 c
.poll_suppress_count
= poll_suppress_count
;
1068 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1069 c
.gcpro
= gcprolist
;
1070 c
.byte_stack
= byte_stack_list
;
1074 if (! sys_setjmp (c
.jmp
))
1075 c
.val
= (*func
) (arg
);
1077 /* Throw works by a longjmp that comes right here. */
1082 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1083 jump to that CATCH, returning VALUE as the value of that catch.
1085 This is the guts Fthrow and Fsignal; they differ only in the way
1086 they choose the catch tag to throw to. A catch tag for a
1087 condition-case form has a TAG of Qnil.
1089 Before each catch is discarded, unbind all special bindings and
1090 execute all unwind-protect clauses made above that catch. Unwind
1091 the handler stack as we go, so that the proper handlers are in
1092 effect for each unwind-protect clause we run. At the end, restore
1093 some static info saved in CATCH, and longjmp to the location
1096 This is used for correct unwinding in Fthrow and Fsignal. */
1098 static _Noreturn
void
1099 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1103 /* Save the value in the tag. */
1106 /* Restore certain special C variables. */
1107 set_poll_suppress_count (catch->poll_suppress_count
);
1108 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1109 handling_signal
= 0;
1114 last_time
= catchlist
== catch;
1116 /* Unwind the specpdl stack, and then restore the proper set of
1118 unbind_to (catchlist
->pdlcount
, Qnil
);
1119 handlerlist
= catchlist
->handlerlist
;
1120 catchlist
= catchlist
->next
;
1122 while (! last_time
);
1125 /* If x_catch_errors was done, turn it off now.
1126 (First we give unbind_to a chance to do that.) */
1127 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1128 The catch must remain in effect during that delicate
1129 state. --lorentey */
1130 x_fully_uncatch_errors ();
1134 byte_stack_list
= catch->byte_stack
;
1135 gcprolist
= catch->gcpro
;
1137 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1139 backtrace_list
= catch->backlist
;
1140 lisp_eval_depth
= catch->lisp_eval_depth
;
1142 sys_longjmp (catch->jmp
, 1);
1145 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1146 doc
: /* Throw to the catch for TAG and return VALUE from it.
1147 Both TAG and VALUE are evalled. */)
1148 (register Lisp_Object tag
, Lisp_Object value
)
1150 register struct catchtag
*c
;
1153 for (c
= catchlist
; c
; c
= c
->next
)
1155 if (EQ (c
->tag
, tag
))
1156 unwind_to_catch (c
, value
);
1158 xsignal2 (Qno_catch
, tag
, value
);
1162 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1163 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1164 If BODYFORM completes normally, its value is returned
1165 after executing the UNWINDFORMS.
1166 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1167 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1171 ptrdiff_t count
= SPECPDL_INDEX ();
1173 record_unwind_protect (Fprogn
, Fcdr (args
));
1174 val
= eval_sub (Fcar (args
));
1175 return unbind_to (count
, val
);
1178 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1179 doc
: /* Regain control when an error is signaled.
1180 Executes BODYFORM and returns its value if no error happens.
1181 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1182 where the BODY is made of Lisp expressions.
1184 A handler is applicable to an error
1185 if CONDITION-NAME is one of the error's condition names.
1186 If an error happens, the first applicable handler is run.
1188 The car of a handler may be a list of condition names instead of a
1189 single condition name; then it handles all of them. If the special
1190 condition name `debug' is present in this list, it allows another
1191 condition in the list to run the debugger if `debug-on-error' and the
1192 other usual mechanisms says it should (otherwise, `condition-case'
1193 suppresses the debugger).
1195 When a handler handles an error, control returns to the `condition-case'
1196 and it executes the handler's BODY...
1197 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1198 \(If VAR is nil, the handler can't access that information.)
1199 Then the value of the last BODY form is returned from the `condition-case'
1202 See also the function `signal' for more info.
1203 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1206 Lisp_Object var
= Fcar (args
);
1207 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1208 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1210 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1213 /* Like Fcondition_case, but the args are separate
1214 rather than passed in a list. Used by Fbyte_code. */
1217 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1218 Lisp_Object handlers
)
1226 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1232 && (SYMBOLP (XCAR (tem
))
1233 || CONSP (XCAR (tem
))))))
1234 error ("Invalid condition handler: %s",
1235 SDATA (Fprin1_to_string (tem
, Qt
)));
1240 c
.backlist
= backtrace_list
;
1241 c
.handlerlist
= handlerlist
;
1242 c
.lisp_eval_depth
= lisp_eval_depth
;
1243 c
.pdlcount
= SPECPDL_INDEX ();
1244 c
.poll_suppress_count
= poll_suppress_count
;
1245 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1246 c
.gcpro
= gcprolist
;
1247 c
.byte_stack
= byte_stack_list
;
1248 if (sys_setjmp (c
.jmp
))
1251 specbind (h
.var
, c
.val
);
1252 val
= Fprogn (Fcdr (h
.chosen_clause
));
1254 /* Note that this just undoes the binding of h.var; whoever
1255 longjumped to us unwound the stack to c.pdlcount before
1257 unbind_to (c
.pdlcount
, Qnil
);
1264 h
.handler
= handlers
;
1265 h
.next
= handlerlist
;
1269 val
= eval_sub (bodyform
);
1271 handlerlist
= h
.next
;
1275 /* Call the function BFUN with no arguments, catching errors within it
1276 according to HANDLERS. If there is an error, call HFUN with
1277 one argument which is the data that describes the error:
1280 HANDLERS can be a list of conditions to catch.
1281 If HANDLERS is Qt, catch all errors.
1282 If HANDLERS is Qerror, catch all errors
1283 but allow the debugger to run if that is enabled. */
1286 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1287 Lisp_Object (*hfun
) (Lisp_Object
))
1295 c
.backlist
= backtrace_list
;
1296 c
.handlerlist
= handlerlist
;
1297 c
.lisp_eval_depth
= lisp_eval_depth
;
1298 c
.pdlcount
= SPECPDL_INDEX ();
1299 c
.poll_suppress_count
= poll_suppress_count
;
1300 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1301 c
.gcpro
= gcprolist
;
1302 c
.byte_stack
= byte_stack_list
;
1303 if (sys_setjmp (c
.jmp
))
1305 return (*hfun
) (c
.val
);
1309 h
.handler
= handlers
;
1311 h
.next
= handlerlist
;
1317 handlerlist
= h
.next
;
1321 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1324 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1325 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1333 c
.backlist
= backtrace_list
;
1334 c
.handlerlist
= handlerlist
;
1335 c
.lisp_eval_depth
= lisp_eval_depth
;
1336 c
.pdlcount
= SPECPDL_INDEX ();
1337 c
.poll_suppress_count
= poll_suppress_count
;
1338 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1339 c
.gcpro
= gcprolist
;
1340 c
.byte_stack
= byte_stack_list
;
1341 if (sys_setjmp (c
.jmp
))
1343 return (*hfun
) (c
.val
);
1347 h
.handler
= handlers
;
1349 h
.next
= handlerlist
;
1353 val
= (*bfun
) (arg
);
1355 handlerlist
= h
.next
;
1359 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1363 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1366 Lisp_Object handlers
,
1367 Lisp_Object (*hfun
) (Lisp_Object
))
1375 c
.backlist
= backtrace_list
;
1376 c
.handlerlist
= handlerlist
;
1377 c
.lisp_eval_depth
= lisp_eval_depth
;
1378 c
.pdlcount
= SPECPDL_INDEX ();
1379 c
.poll_suppress_count
= poll_suppress_count
;
1380 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1381 c
.gcpro
= gcprolist
;
1382 c
.byte_stack
= byte_stack_list
;
1383 if (sys_setjmp (c
.jmp
))
1385 return (*hfun
) (c
.val
);
1389 h
.handler
= handlers
;
1391 h
.next
= handlerlist
;
1395 val
= (*bfun
) (arg1
, arg2
);
1397 handlerlist
= h
.next
;
1401 /* Like internal_condition_case but call BFUN with NARGS as first,
1402 and ARGS as second argument. */
1405 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1408 Lisp_Object handlers
,
1409 Lisp_Object (*hfun
) (Lisp_Object err
,
1419 c
.backlist
= backtrace_list
;
1420 c
.handlerlist
= handlerlist
;
1421 c
.lisp_eval_depth
= lisp_eval_depth
;
1422 c
.pdlcount
= SPECPDL_INDEX ();
1423 c
.poll_suppress_count
= poll_suppress_count
;
1424 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1425 c
.gcpro
= gcprolist
;
1426 c
.byte_stack
= byte_stack_list
;
1427 if (sys_setjmp (c
.jmp
))
1429 return (*hfun
) (c
.val
, nargs
, args
);
1433 h
.handler
= handlers
;
1435 h
.next
= handlerlist
;
1439 val
= (*bfun
) (nargs
, args
);
1441 handlerlist
= h
.next
;
1446 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1447 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1451 process_quit_flag (void)
1453 Lisp_Object flag
= Vquit_flag
;
1455 if (EQ (flag
, Qkill_emacs
))
1457 if (EQ (Vthrow_on_input
, flag
))
1458 Fthrow (Vthrow_on_input
, Qt
);
1459 Fsignal (Qquit
, Qnil
);
1462 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1463 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1464 This function does not return.
1466 An error symbol is a symbol with an `error-conditions' property
1467 that is a list of condition names.
1468 A handler for any of those names will get to handle this signal.
1469 The symbol `error' should normally be one of them.
1471 DATA should be a list. Its elements are printed as part of the error message.
1472 See Info anchor `(elisp)Definition of signal' for some details on how this
1473 error message is constructed.
1474 If the signal is handled, DATA is made available to the handler.
1475 See also the function `condition-case'. */)
1476 (Lisp_Object error_symbol
, Lisp_Object data
)
1478 /* When memory is full, ERROR-SYMBOL is nil,
1479 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1480 That is a special case--don't do this in other situations. */
1481 Lisp_Object conditions
;
1483 Lisp_Object real_error_symbol
1484 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1485 register Lisp_Object clause
= Qnil
;
1487 struct backtrace
*bp
;
1489 immediate_quit
= handling_signal
= 0;
1491 if (gc_in_progress
|| waiting_for_input
)
1494 #if 0 /* rms: I don't know why this was here,
1495 but it is surely wrong for an error that is handled. */
1496 #ifdef HAVE_WINDOW_SYSTEM
1497 if (display_hourglass_p
)
1498 cancel_hourglass ();
1502 /* This hook is used by edebug. */
1503 if (! NILP (Vsignal_hook_function
)
1504 && ! NILP (error_symbol
))
1506 /* Edebug takes care of restoring these variables when it exits. */
1507 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1508 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1510 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1511 max_specpdl_size
= SPECPDL_INDEX () + 40;
1513 call2 (Vsignal_hook_function
, error_symbol
, data
);
1516 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1518 /* Remember from where signal was called. Skip over the frame for
1519 `signal' itself. If a frame for `error' follows, skip that,
1520 too. Don't do this when ERROR_SYMBOL is nil, because that
1521 is a memory-full error. */
1522 Vsignaling_function
= Qnil
;
1523 if (backtrace_list
&& !NILP (error_symbol
))
1525 bp
= backtrace_list
->next
;
1526 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1528 if (bp
&& bp
->function
)
1529 Vsignaling_function
= *bp
->function
;
1532 for (h
= handlerlist
; h
; h
= h
->next
)
1534 clause
= find_handler_clause (h
->handler
, conditions
);
1539 if (/* Don't run the debugger for a memory-full error.
1540 (There is no room in memory to do that!) */
1541 !NILP (error_symbol
)
1542 && (!NILP (Vdebug_on_signal
)
1543 /* If no handler is present now, try to run the debugger. */
1545 /* A `debug' symbol in the handler list disables the normal
1546 suppression of the debugger. */
1547 || (CONSP (clause
) && CONSP (XCAR (clause
))
1548 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1549 /* Special handler that means "print a message and run debugger
1551 || EQ (h
->handler
, Qerror
)))
1553 bool debugger_called
1554 = maybe_call_debugger (conditions
, error_symbol
, data
);
1555 /* We can't return values to code which signaled an error, but we
1556 can continue code which has signaled a quit. */
1557 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1563 Lisp_Object unwind_data
1564 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1566 h
->chosen_clause
= clause
;
1567 unwind_to_catch (h
->tag
, unwind_data
);
1572 Fthrow (Qtop_level
, Qt
);
1575 if (! NILP (error_symbol
))
1576 data
= Fcons (error_symbol
, data
);
1578 string
= Ferror_message_string (data
);
1579 fatal ("%s", SDATA (string
));
1582 /* Internal version of Fsignal that never returns.
1583 Used for anything but Qquit (which can return from Fsignal). */
1586 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1588 Fsignal (error_symbol
, data
);
1592 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1595 xsignal0 (Lisp_Object error_symbol
)
1597 xsignal (error_symbol
, Qnil
);
1601 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1603 xsignal (error_symbol
, list1 (arg
));
1607 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1609 xsignal (error_symbol
, list2 (arg1
, arg2
));
1613 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1615 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1618 /* Signal `error' with message S, and additional arg ARG.
1619 If ARG is not a genuine list, make it a one-element list. */
1622 signal_error (const char *s
, Lisp_Object arg
)
1624 Lisp_Object tortoise
, hare
;
1626 hare
= tortoise
= arg
;
1627 while (CONSP (hare
))
1634 tortoise
= XCDR (tortoise
);
1636 if (EQ (hare
, tortoise
))
1641 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1643 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1647 /* Return true if LIST is a non-nil atom or
1648 a list containing one of CONDITIONS. */
1651 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1658 while (CONSP (conditions
))
1660 Lisp_Object
this, tail
;
1661 this = XCAR (conditions
);
1662 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1663 if (EQ (XCAR (tail
), this))
1665 conditions
= XCDR (conditions
);
1670 /* Return true if an error with condition-symbols CONDITIONS,
1671 and described by SIGNAL-DATA, should skip the debugger
1672 according to debugger-ignored-errors. */
1675 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1678 bool first_string
= 1;
1679 Lisp_Object error_message
;
1681 error_message
= Qnil
;
1682 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1684 if (STRINGP (XCAR (tail
)))
1688 error_message
= Ferror_message_string (data
);
1692 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1697 Lisp_Object contail
;
1699 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1700 if (EQ (XCAR (tail
), XCAR (contail
)))
1708 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1709 SIG and DATA describe the signal. There are two ways to pass them:
1710 = SIG is the error symbol, and DATA is the rest of the data.
1711 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1712 This is for memory-full errors only. */
1714 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1716 Lisp_Object combined_data
;
1718 combined_data
= Fcons (sig
, data
);
1721 /* Don't try to run the debugger with interrupts blocked.
1722 The editing loop would return anyway. */
1724 && NILP (Vinhibit_debugger
)
1725 /* Does user want to enter debugger for this kind of error? */
1728 : wants_debugger (Vdebug_on_error
, conditions
))
1729 && ! skip_debugger (conditions
, combined_data
)
1730 /* RMS: What's this for? */
1731 && when_entered_debugger
< num_nonmacro_input_events
)
1733 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1741 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1743 register Lisp_Object h
;
1745 /* t is used by handlers for all conditions, set up by C code. */
1746 if (EQ (handlers
, Qt
))
1749 /* error is used similarly, but means print an error message
1750 and run the debugger if that is enabled. */
1751 if (EQ (handlers
, Qerror
))
1754 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1756 Lisp_Object handler
= XCAR (h
);
1757 Lisp_Object condit
, tem
;
1759 if (!CONSP (handler
))
1761 condit
= XCAR (handler
);
1762 /* Handle a single condition name in handler HANDLER. */
1763 if (SYMBOLP (condit
))
1765 tem
= Fmemq (Fcar (handler
), conditions
);
1769 /* Handle a list of condition names in handler HANDLER. */
1770 else if (CONSP (condit
))
1773 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1775 tem
= Fmemq (XCAR (tail
), conditions
);
1786 /* Dump an error message; called like vprintf. */
1788 verror (const char *m
, va_list ap
)
1791 ptrdiff_t size
= sizeof buf
;
1792 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1797 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1798 string
= make_string (buffer
, used
);
1802 xsignal1 (Qerror
, string
);
1806 /* Dump an error message; called like printf. */
1810 error (const char *m
, ...)
1818 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1819 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1820 This means it contains a description for how to read arguments to give it.
1821 The value is nil for an invalid function or a symbol with no function
1824 Interactively callable functions include strings and vectors (treated
1825 as keyboard macros), lambda-expressions that contain a top-level call
1826 to `interactive', autoload definitions made by `autoload' with non-nil
1827 fourth argument, and some of the built-in functions of Lisp.
1829 Also, a symbol satisfies `commandp' if its function definition does so.
1831 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1832 then strings and vectors are not accepted. */)
1833 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1835 register Lisp_Object fun
;
1836 register Lisp_Object funcar
;
1837 Lisp_Object if_prop
= Qnil
;
1841 fun
= indirect_function (fun
); /* Check cycles. */
1842 if (NILP (fun
) || EQ (fun
, Qunbound
))
1845 /* Check an `interactive-form' property if present, analogous to the
1846 function-documentation property. */
1848 while (SYMBOLP (fun
))
1850 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1853 fun
= Fsymbol_function (fun
);
1856 /* Emacs primitives are interactive if their DEFUN specifies an
1857 interactive spec. */
1859 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1861 /* Bytecode objects are interactive if they are long enough to
1862 have an element whose index is COMPILED_INTERACTIVE, which is
1863 where the interactive spec is stored. */
1864 else if (COMPILEDP (fun
))
1865 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1868 /* Strings and vectors are keyboard macros. */
1869 if (STRINGP (fun
) || VECTORP (fun
))
1870 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1872 /* Lists may represent commands. */
1875 funcar
= XCAR (fun
);
1876 if (EQ (funcar
, Qclosure
))
1877 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1879 else if (EQ (funcar
, Qlambda
))
1880 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1881 else if (EQ (funcar
, Qautoload
))
1882 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1887 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1888 doc
: /* Define FUNCTION to autoload from FILE.
1889 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1890 Third arg DOCSTRING is documentation for the function.
1891 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1892 Fifth arg TYPE indicates the type of the object:
1893 nil or omitted says FUNCTION is a function,
1894 `keymap' says FUNCTION is really a keymap, and
1895 `macro' or t says FUNCTION is really a macro.
1896 Third through fifth args give info about the real definition.
1897 They default to nil.
1898 If FUNCTION is already defined other than as an autoload,
1899 this does nothing and returns nil. */)
1900 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1902 CHECK_SYMBOL (function
);
1903 CHECK_STRING (file
);
1905 /* If function is defined and not as an autoload, don't override. */
1906 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1907 && !(CONSP (XSYMBOL (function
)->function
)
1908 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1911 if (NILP (Vpurify_flag
))
1912 /* Only add entries after dumping, because the ones before are
1913 not useful and else we get loads of them from the loaddefs.el. */
1914 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1915 else if (EQ (docstring
, make_number (0)))
1916 /* `read1' in lread.c has found the docstring starting with "\
1917 and assumed the docstring will be provided by Snarf-documentation, so it
1918 passed us 0 instead. But that leads to accidental sharing in purecopy's
1919 hash-consing, so we use a (hopefully) unique integer instead. */
1920 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1921 return Ffset (function
,
1922 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1923 interactive
, type
)));
1927 un_autoload (Lisp_Object oldqueue
)
1929 register Lisp_Object queue
, first
, second
;
1931 /* Queue to unwind is current value of Vautoload_queue.
1932 oldqueue is the shadowed value to leave in Vautoload_queue. */
1933 queue
= Vautoload_queue
;
1934 Vautoload_queue
= oldqueue
;
1935 while (CONSP (queue
))
1937 first
= XCAR (queue
);
1938 second
= Fcdr (first
);
1939 first
= Fcar (first
);
1940 if (EQ (first
, make_number (0)))
1943 Ffset (first
, second
);
1944 queue
= XCDR (queue
);
1949 /* Load an autoloaded function.
1950 FUNNAME is the symbol which is the function's name.
1951 FUNDEF is the autoload definition (a list). */
1953 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1954 doc
: /* Load FUNDEF which should be an autoload.
1955 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1956 in which case the function returns the new autoloaded function value.
1957 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1958 it is defines a macro. */)
1959 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1961 ptrdiff_t count
= SPECPDL_INDEX ();
1962 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1964 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1967 if (EQ (macro_only
, Qmacro
))
1969 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1970 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1974 /* This is to make sure that loadup.el gives a clear picture
1975 of what files are preloaded and when. */
1976 if (! NILP (Vpurify_flag
))
1977 error ("Attempt to autoload %s while preparing to dump",
1978 SDATA (SYMBOL_NAME (funname
)));
1980 CHECK_SYMBOL (funname
);
1981 GCPRO3 (funname
, fundef
, macro_only
);
1983 /* Preserve the match data. */
1984 record_unwind_save_match_data ();
1986 /* If autoloading gets an error (which includes the error of failing
1987 to define the function being called), we use Vautoload_queue
1988 to undo function definitions and `provide' calls made by
1989 the function. We do this in the specific case of autoloading
1990 because autoloading is not an explicit request "load this file",
1991 but rather a request to "call this function".
1993 The value saved here is to be restored into Vautoload_queue. */
1994 record_unwind_protect (un_autoload
, Vautoload_queue
);
1995 Vautoload_queue
= Qt
;
1996 /* If `macro_only', assume this autoload to be a "best-effort",
1997 so don't signal an error if autoloading fails. */
1998 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
2000 /* Once loading finishes, don't undo it. */
2001 Vautoload_queue
= Qt
;
2002 unbind_to (count
, Qnil
);
2010 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2012 if (!NILP (Fequal (fun
, fundef
)))
2013 error ("Autoloading failed to define function %s",
2014 SDATA (SYMBOL_NAME (funname
)));
2021 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2022 doc
: /* Evaluate FORM and return its value.
2023 If LEXICAL is t, evaluate using lexical scoping. */)
2024 (Lisp_Object form
, Lisp_Object lexical
)
2026 ptrdiff_t count
= SPECPDL_INDEX ();
2027 specbind (Qinternal_interpreter_environment
,
2028 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2029 return unbind_to (count
, eval_sub (form
));
2032 /* Eval a sub-expression of the current expression (i.e. in the same
2035 eval_sub (Lisp_Object form
)
2037 Lisp_Object fun
, val
, original_fun
, original_args
;
2039 struct backtrace backtrace
;
2040 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2042 if (handling_signal
)
2047 /* Look up its binding in the lexical environment.
2048 We do not pay attention to the declared_special flag here, since we
2049 already did that when let-binding the variable. */
2050 Lisp_Object lex_binding
2051 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2052 ? Fassq (form
, Vinternal_interpreter_environment
)
2054 if (CONSP (lex_binding
))
2055 return XCDR (lex_binding
);
2057 return Fsymbol_value (form
);
2066 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2068 if (max_lisp_eval_depth
< 100)
2069 max_lisp_eval_depth
= 100;
2070 if (lisp_eval_depth
> max_lisp_eval_depth
)
2071 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2074 original_fun
= XCAR (form
);
2075 original_args
= XCDR (form
);
2077 backtrace
.next
= backtrace_list
;
2078 backtrace_list
= &backtrace
;
2079 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2080 backtrace
.args
= &original_args
;
2081 backtrace
.nargs
= UNEVALLED
;
2082 backtrace
.debug_on_exit
= 0;
2084 if (debug_on_next_call
)
2085 do_debug_on_call (Qt
);
2087 /* At this point, only original_fun and original_args
2088 have values that will be used below. */
2091 /* Optimize for no indirection. */
2093 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2094 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2095 fun
= indirect_function (fun
);
2099 Lisp_Object numargs
;
2100 Lisp_Object argvals
[8];
2101 Lisp_Object args_left
;
2102 register int i
, maxargs
;
2104 args_left
= original_args
;
2105 numargs
= Flength (args_left
);
2109 if (XINT (numargs
) < XSUBR (fun
)->min_args
2110 || (XSUBR (fun
)->max_args
>= 0
2111 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2112 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2114 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2115 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2116 else if (XSUBR (fun
)->max_args
== MANY
)
2118 /* Pass a vector of evaluated arguments. */
2120 ptrdiff_t argnum
= 0;
2123 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2125 GCPRO3 (args_left
, fun
, fun
);
2129 while (!NILP (args_left
))
2131 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2132 args_left
= Fcdr (args_left
);
2133 gcpro3
.nvars
= argnum
;
2136 backtrace
.args
= vals
;
2137 backtrace
.nargs
= XINT (numargs
);
2139 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2145 GCPRO3 (args_left
, fun
, fun
);
2146 gcpro3
.var
= argvals
;
2149 maxargs
= XSUBR (fun
)->max_args
;
2150 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2152 argvals
[i
] = eval_sub (Fcar (args_left
));
2158 backtrace
.args
= argvals
;
2159 backtrace
.nargs
= XINT (numargs
);
2164 val
= (XSUBR (fun
)->function
.a0 ());
2167 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2170 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2173 val
= (XSUBR (fun
)->function
.a3
2174 (argvals
[0], argvals
[1], argvals
[2]));
2177 val
= (XSUBR (fun
)->function
.a4
2178 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2181 val
= (XSUBR (fun
)->function
.a5
2182 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2186 val
= (XSUBR (fun
)->function
.a6
2187 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2188 argvals
[4], argvals
[5]));
2191 val
= (XSUBR (fun
)->function
.a7
2192 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2193 argvals
[4], argvals
[5], argvals
[6]));
2197 val
= (XSUBR (fun
)->function
.a8
2198 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2199 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2203 /* Someone has created a subr that takes more arguments than
2204 is supported by this code. We need to either rewrite the
2205 subr to use a different argument protocol, or add more
2206 cases to this switch. */
2211 else if (COMPILEDP (fun
))
2212 val
= apply_lambda (fun
, original_args
);
2215 if (EQ (fun
, Qunbound
))
2216 xsignal1 (Qvoid_function
, original_fun
);
2218 xsignal1 (Qinvalid_function
, original_fun
);
2219 funcar
= XCAR (fun
);
2220 if (!SYMBOLP (funcar
))
2221 xsignal1 (Qinvalid_function
, original_fun
);
2222 if (EQ (funcar
, Qautoload
))
2224 Fautoload_do_load (fun
, original_fun
, Qnil
);
2227 if (EQ (funcar
, Qmacro
))
2229 ptrdiff_t count
= SPECPDL_INDEX ();
2231 /* Bind lexical-binding during expansion of the macro, so the
2232 macro can know reliably if the code it outputs will be
2233 interpreted using lexical-binding or not. */
2234 specbind (Qlexical_binding
,
2235 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2236 exp
= apply1 (Fcdr (fun
), original_args
);
2237 unbind_to (count
, Qnil
);
2238 val
= eval_sub (exp
);
2240 else if (EQ (funcar
, Qlambda
)
2241 || EQ (funcar
, Qclosure
))
2242 val
= apply_lambda (fun
, original_args
);
2244 xsignal1 (Qinvalid_function
, original_fun
);
2249 if (backtrace
.debug_on_exit
)
2250 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2251 backtrace_list
= backtrace
.next
;
2256 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2257 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2258 Then return the value FUNCTION returns.
2259 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2260 usage: (apply FUNCTION &rest ARGUMENTS) */)
2261 (ptrdiff_t nargs
, Lisp_Object
*args
)
2265 register Lisp_Object spread_arg
;
2266 register Lisp_Object
*funcall_args
;
2267 Lisp_Object fun
, retval
;
2268 struct gcpro gcpro1
;
2273 spread_arg
= args
[nargs
- 1];
2274 CHECK_LIST (spread_arg
);
2276 numargs
= XINT (Flength (spread_arg
));
2279 return Ffuncall (nargs
- 1, args
);
2280 else if (numargs
== 1)
2282 args
[nargs
- 1] = XCAR (spread_arg
);
2283 return Ffuncall (nargs
, args
);
2286 numargs
+= nargs
- 2;
2288 /* Optimize for no indirection. */
2289 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2290 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2291 fun
= indirect_function (fun
);
2292 if (EQ (fun
, Qunbound
))
2294 /* Let funcall get the error. */
2301 if (numargs
< XSUBR (fun
)->min_args
2302 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2303 goto funcall
; /* Let funcall get the error. */
2304 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2306 /* Avoid making funcall cons up a yet another new vector of arguments
2307 by explicitly supplying nil's for optional values. */
2308 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2309 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2310 funcall_args
[++i
] = Qnil
;
2311 GCPRO1 (*funcall_args
);
2312 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2316 /* We add 1 to numargs because funcall_args includes the
2317 function itself as well as its arguments. */
2320 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2321 GCPRO1 (*funcall_args
);
2322 gcpro1
.nvars
= 1 + numargs
;
2325 memcpy (funcall_args
, args
, nargs
* word_size
);
2326 /* Spread the last arg we got. Its first element goes in
2327 the slot that it used to occupy, hence this value of I. */
2329 while (!NILP (spread_arg
))
2331 funcall_args
[i
++] = XCAR (spread_arg
);
2332 spread_arg
= XCDR (spread_arg
);
2335 /* By convention, the caller needs to gcpro Ffuncall's args. */
2336 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2343 /* Run hook variables in various ways. */
2346 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2348 Ffuncall (nargs
, args
);
2352 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2353 doc
: /* Run each hook in HOOKS.
2354 Each argument should be a symbol, a hook variable.
2355 These symbols are processed in the order specified.
2356 If a hook symbol has a non-nil value, that value may be a function
2357 or a list of functions to be called to run the hook.
2358 If the value is a function, it is called with no arguments.
2359 If it is a list, the elements are called, in order, with no arguments.
2361 Major modes should not use this function directly to run their mode
2362 hook; they should use `run-mode-hooks' instead.
2364 Do not use `make-local-variable' to make a hook variable buffer-local.
2365 Instead, use `add-hook' and specify t for the LOCAL argument.
2366 usage: (run-hooks &rest HOOKS) */)
2367 (ptrdiff_t nargs
, Lisp_Object
*args
)
2369 Lisp_Object hook
[1];
2372 for (i
= 0; i
< nargs
; i
++)
2375 run_hook_with_args (1, hook
, funcall_nil
);
2381 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2382 Srun_hook_with_args
, 1, MANY
, 0,
2383 doc
: /* Run HOOK with the specified arguments ARGS.
2384 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2385 value, that value may be a function or a list of functions to be
2386 called to run the hook. If the value is a function, it is called with
2387 the given arguments and its return value is returned. If it is a list
2388 of functions, those functions are called, in order,
2389 with the given arguments ARGS.
2390 It is best not to depend on the value returned by `run-hook-with-args',
2393 Do not use `make-local-variable' to make a hook variable buffer-local.
2394 Instead, use `add-hook' and specify t for the LOCAL argument.
2395 usage: (run-hook-with-args HOOK &rest ARGS) */)
2396 (ptrdiff_t nargs
, Lisp_Object
*args
)
2398 return run_hook_with_args (nargs
, args
, funcall_nil
);
2401 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2402 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2403 doc
: /* Run HOOK with the specified arguments ARGS.
2404 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2405 value, that value may be a function or a list of functions to be
2406 called to run the hook. If the value is a function, it is called with
2407 the given arguments and its return value is returned.
2408 If it is a list of functions, those functions are called, in order,
2409 with the given arguments ARGS, until one of them
2410 returns a non-nil value. Then we return that value.
2411 However, if they all return nil, we return nil.
2413 Do not use `make-local-variable' to make a hook variable buffer-local.
2414 Instead, use `add-hook' and specify t for the LOCAL argument.
2415 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2416 (ptrdiff_t nargs
, Lisp_Object
*args
)
2418 return run_hook_with_args (nargs
, args
, Ffuncall
);
2422 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2424 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2427 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2428 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2429 doc
: /* Run HOOK with the specified arguments ARGS.
2430 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2431 value, that value may be a function or a list of functions to be
2432 called to run the hook. If the value is a function, it is called with
2433 the given arguments and its return value is returned.
2434 If it is a list of functions, those functions are called, in order,
2435 with the given arguments ARGS, until one of them returns nil.
2436 Then we return nil. However, if they all return non-nil, we return non-nil.
2438 Do not use `make-local-variable' to make a hook variable buffer-local.
2439 Instead, use `add-hook' and specify t for the LOCAL argument.
2440 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2441 (ptrdiff_t nargs
, Lisp_Object
*args
)
2443 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2447 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2449 Lisp_Object tmp
= args
[0], ret
;
2452 ret
= Ffuncall (nargs
, args
);
2458 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2459 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2460 I.e. instead of calling each function FUN directly with arguments ARGS,
2461 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2462 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2463 aborts and returns that value.
2464 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2465 (ptrdiff_t nargs
, Lisp_Object
*args
)
2467 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2470 /* ARGS[0] should be a hook symbol.
2471 Call each of the functions in the hook value, passing each of them
2472 as arguments all the rest of ARGS (all NARGS - 1 elements).
2473 FUNCALL specifies how to call each function on the hook.
2474 The caller (or its caller, etc) must gcpro all of ARGS,
2475 except that it isn't necessary to gcpro ARGS[0]. */
2478 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2479 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2481 Lisp_Object sym
, val
, ret
= Qnil
;
2482 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2484 /* If we are dying or still initializing,
2485 don't do anything--it would probably crash if we tried. */
2486 if (NILP (Vrun_hooks
))
2490 val
= find_symbol_value (sym
);
2492 if (EQ (val
, Qunbound
) || NILP (val
))
2494 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2497 return funcall (nargs
, args
);
2501 Lisp_Object global_vals
= Qnil
;
2502 GCPRO3 (sym
, val
, global_vals
);
2505 CONSP (val
) && NILP (ret
);
2508 if (EQ (XCAR (val
), Qt
))
2510 /* t indicates this hook has a local binding;
2511 it means to run the global binding too. */
2512 global_vals
= Fdefault_value (sym
);
2513 if (NILP (global_vals
)) continue;
2515 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2517 args
[0] = global_vals
;
2518 ret
= funcall (nargs
, args
);
2523 CONSP (global_vals
) && NILP (ret
);
2524 global_vals
= XCDR (global_vals
))
2526 args
[0] = XCAR (global_vals
);
2527 /* In a global value, t should not occur. If it does, we
2528 must ignore it to avoid an endless loop. */
2529 if (!EQ (args
[0], Qt
))
2530 ret
= funcall (nargs
, args
);
2536 args
[0] = XCAR (val
);
2537 ret
= funcall (nargs
, args
);
2546 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2549 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2551 Lisp_Object temp
[3];
2556 Frun_hook_with_args (3, temp
);
2559 /* Apply fn to arg. */
2561 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2563 struct gcpro gcpro1
;
2567 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2570 Lisp_Object args
[2];
2574 RETURN_UNGCPRO (Fapply (2, args
));
2578 /* Call function fn on no arguments. */
2580 call0 (Lisp_Object fn
)
2582 struct gcpro gcpro1
;
2585 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2588 /* Call function fn with 1 argument arg1. */
2591 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2593 struct gcpro gcpro1
;
2594 Lisp_Object args
[2];
2600 RETURN_UNGCPRO (Ffuncall (2, args
));
2603 /* Call function fn with 2 arguments arg1, arg2. */
2606 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2608 struct gcpro gcpro1
;
2609 Lisp_Object args
[3];
2615 RETURN_UNGCPRO (Ffuncall (3, args
));
2618 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2621 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2623 struct gcpro gcpro1
;
2624 Lisp_Object args
[4];
2631 RETURN_UNGCPRO (Ffuncall (4, args
));
2634 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2637 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2640 struct gcpro gcpro1
;
2641 Lisp_Object args
[5];
2649 RETURN_UNGCPRO (Ffuncall (5, args
));
2652 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2655 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2656 Lisp_Object arg4
, Lisp_Object arg5
)
2658 struct gcpro gcpro1
;
2659 Lisp_Object args
[6];
2668 RETURN_UNGCPRO (Ffuncall (6, args
));
2671 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2674 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2675 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2677 struct gcpro gcpro1
;
2678 Lisp_Object args
[7];
2688 RETURN_UNGCPRO (Ffuncall (7, args
));
2691 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2694 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2695 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2697 struct gcpro gcpro1
;
2698 Lisp_Object args
[8];
2709 RETURN_UNGCPRO (Ffuncall (8, args
));
2712 /* The caller should GCPRO all the elements of ARGS. */
2714 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2715 doc
: /* Non-nil if OBJECT is a function. */)
2716 (Lisp_Object object
)
2718 if (FUNCTIONP (object
))
2723 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2724 doc
: /* Call first argument as a function, passing remaining arguments to it.
2725 Return the value that function returns.
2726 Thus, (funcall 'cons 'x 'y) returns (x . y).
2727 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2728 (ptrdiff_t nargs
, Lisp_Object
*args
)
2730 Lisp_Object fun
, original_fun
;
2732 ptrdiff_t numargs
= nargs
- 1;
2733 Lisp_Object lisp_numargs
;
2735 struct backtrace backtrace
;
2736 register Lisp_Object
*internal_args
;
2741 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2743 if (max_lisp_eval_depth
< 100)
2744 max_lisp_eval_depth
= 100;
2745 if (lisp_eval_depth
> max_lisp_eval_depth
)
2746 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2749 backtrace
.next
= backtrace_list
;
2750 backtrace_list
= &backtrace
;
2751 backtrace
.function
= &args
[0];
2752 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2753 backtrace
.nargs
= nargs
- 1;
2754 backtrace
.debug_on_exit
= 0;
2756 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2759 if (debug_on_next_call
)
2760 do_debug_on_call (Qlambda
);
2764 original_fun
= args
[0];
2768 /* Optimize for no indirection. */
2770 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2771 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2772 fun
= indirect_function (fun
);
2776 if (numargs
< XSUBR (fun
)->min_args
2777 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2779 XSETFASTINT (lisp_numargs
, numargs
);
2780 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2783 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2784 xsignal1 (Qinvalid_function
, original_fun
);
2786 else if (XSUBR (fun
)->max_args
== MANY
)
2787 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2790 if (XSUBR (fun
)->max_args
> numargs
)
2792 internal_args
= alloca (XSUBR (fun
)->max_args
2793 * sizeof *internal_args
);
2794 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2795 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2796 internal_args
[i
] = Qnil
;
2799 internal_args
= args
+ 1;
2800 switch (XSUBR (fun
)->max_args
)
2803 val
= (XSUBR (fun
)->function
.a0 ());
2806 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2809 val
= (XSUBR (fun
)->function
.a2
2810 (internal_args
[0], internal_args
[1]));
2813 val
= (XSUBR (fun
)->function
.a3
2814 (internal_args
[0], internal_args
[1], internal_args
[2]));
2817 val
= (XSUBR (fun
)->function
.a4
2818 (internal_args
[0], internal_args
[1], internal_args
[2],
2822 val
= (XSUBR (fun
)->function
.a5
2823 (internal_args
[0], internal_args
[1], internal_args
[2],
2824 internal_args
[3], internal_args
[4]));
2827 val
= (XSUBR (fun
)->function
.a6
2828 (internal_args
[0], internal_args
[1], internal_args
[2],
2829 internal_args
[3], internal_args
[4], internal_args
[5]));
2832 val
= (XSUBR (fun
)->function
.a7
2833 (internal_args
[0], internal_args
[1], internal_args
[2],
2834 internal_args
[3], internal_args
[4], internal_args
[5],
2839 val
= (XSUBR (fun
)->function
.a8
2840 (internal_args
[0], internal_args
[1], internal_args
[2],
2841 internal_args
[3], internal_args
[4], internal_args
[5],
2842 internal_args
[6], internal_args
[7]));
2847 /* If a subr takes more than 8 arguments without using MANY
2848 or UNEVALLED, we need to extend this function to support it.
2849 Until this is done, there is no way to call the function. */
2854 else if (COMPILEDP (fun
))
2855 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2858 if (EQ (fun
, Qunbound
))
2859 xsignal1 (Qvoid_function
, original_fun
);
2861 xsignal1 (Qinvalid_function
, original_fun
);
2862 funcar
= XCAR (fun
);
2863 if (!SYMBOLP (funcar
))
2864 xsignal1 (Qinvalid_function
, original_fun
);
2865 if (EQ (funcar
, Qlambda
)
2866 || EQ (funcar
, Qclosure
))
2867 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2868 else if (EQ (funcar
, Qautoload
))
2870 Fautoload_do_load (fun
, original_fun
, Qnil
);
2875 xsignal1 (Qinvalid_function
, original_fun
);
2879 if (backtrace
.debug_on_exit
)
2880 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2881 backtrace_list
= backtrace
.next
;
2886 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2888 Lisp_Object args_left
;
2891 register Lisp_Object
*arg_vector
;
2892 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2893 register Lisp_Object tem
;
2896 numargs
= XFASTINT (Flength (args
));
2897 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2900 GCPRO3 (*arg_vector
, args_left
, fun
);
2903 for (i
= 0; i
< numargs
; )
2905 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2906 tem
= eval_sub (tem
);
2907 arg_vector
[i
++] = tem
;
2913 backtrace_list
->args
= arg_vector
;
2914 backtrace_list
->nargs
= i
;
2915 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2917 /* Do the debug-on-exit now, while arg_vector still exists. */
2918 if (backtrace_list
->debug_on_exit
)
2919 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2920 /* Don't do it again when we return to eval. */
2921 backtrace_list
->debug_on_exit
= 0;
2926 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2927 and return the result of evaluation.
2928 FUN must be either a lambda-expression or a compiled-code object. */
2931 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2932 register Lisp_Object
*arg_vector
)
2934 Lisp_Object val
, syms_left
, next
, lexenv
;
2935 ptrdiff_t count
= SPECPDL_INDEX ();
2937 bool optional
, rest
;
2941 if (EQ (XCAR (fun
), Qclosure
))
2943 fun
= XCDR (fun
); /* Drop `closure'. */
2944 lexenv
= XCAR (fun
);
2945 CHECK_LIST_CONS (fun
, fun
);
2949 syms_left
= XCDR (fun
);
2950 if (CONSP (syms_left
))
2951 syms_left
= XCAR (syms_left
);
2953 xsignal1 (Qinvalid_function
, fun
);
2955 else if (COMPILEDP (fun
))
2957 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2958 if (INTEGERP (syms_left
))
2959 /* A byte-code object with a non-nil `push args' slot means we
2960 shouldn't bind any arguments, instead just call the byte-code
2961 interpreter directly; it will push arguments as necessary.
2963 Byte-code objects with either a non-existent, or a nil value for
2964 the `push args' slot (the default), have dynamically-bound
2965 arguments, and use the argument-binding code below instead (as do
2966 all interpreted functions, even lexically bound ones). */
2968 /* If we have not actually read the bytecode string
2969 and constants vector yet, fetch them from the file. */
2970 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2971 Ffetch_bytecode (fun
);
2972 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2973 AREF (fun
, COMPILED_CONSTANTS
),
2974 AREF (fun
, COMPILED_STACK_DEPTH
),
2983 i
= optional
= rest
= 0;
2984 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2988 next
= XCAR (syms_left
);
2989 if (!SYMBOLP (next
))
2990 xsignal1 (Qinvalid_function
, fun
);
2992 if (EQ (next
, Qand_rest
))
2994 else if (EQ (next
, Qand_optional
))
3001 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3005 arg
= arg_vector
[i
++];
3007 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3011 /* Bind the argument. */
3012 if (!NILP (lexenv
) && SYMBOLP (next
))
3013 /* Lexically bind NEXT by adding it to the lexenv alist. */
3014 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3016 /* Dynamically bind NEXT. */
3017 specbind (next
, arg
);
3021 if (!NILP (syms_left
))
3022 xsignal1 (Qinvalid_function
, fun
);
3024 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3026 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3027 /* Instantiate a new lexical environment. */
3028 specbind (Qinternal_interpreter_environment
, lexenv
);
3031 val
= Fprogn (XCDR (XCDR (fun
)));
3034 /* If we have not actually read the bytecode string
3035 and constants vector yet, fetch them from the file. */
3036 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3037 Ffetch_bytecode (fun
);
3038 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3039 AREF (fun
, COMPILED_CONSTANTS
),
3040 AREF (fun
, COMPILED_STACK_DEPTH
),
3044 return unbind_to (count
, val
);
3047 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3049 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3050 (Lisp_Object object
)
3054 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3056 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3059 tem
= AREF (object
, COMPILED_BYTECODE
);
3060 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3061 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3063 error ("Invalid byte code");
3065 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3066 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3074 register ptrdiff_t count
= SPECPDL_INDEX ();
3075 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3076 if (max_size
<= specpdl_size
)
3078 if (max_specpdl_size
< 400)
3079 max_size
= max_specpdl_size
= 400;
3080 if (max_size
<= specpdl_size
)
3081 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3083 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3084 specpdl_ptr
= specpdl
+ count
;
3087 /* `specpdl_ptr->symbol' is a field which describes which variable is
3088 let-bound, so it can be properly undone when we unbind_to.
3089 It can have the following two shapes:
3090 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3091 a symbol that is not buffer-local (at least at the time
3092 the let binding started). Note also that it should not be
3093 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3095 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3096 variable SYMBOL which can be buffer-local. WHERE tells us
3097 which buffer is affected (or nil if the let-binding affects the
3098 global value of the variable) and BUFFER tells us which buffer was
3099 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3100 BUFFER did not yet have a buffer-local value). */
3103 specbind (Lisp_Object symbol
, Lisp_Object value
)
3105 struct Lisp_Symbol
*sym
;
3107 eassert (!handling_signal
);
3109 CHECK_SYMBOL (symbol
);
3110 sym
= XSYMBOL (symbol
);
3111 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3115 switch (sym
->redirect
)
3117 case SYMBOL_VARALIAS
:
3118 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3119 case SYMBOL_PLAINVAL
:
3120 /* The most common case is that of a non-constant symbol with a
3121 trivial value. Make that as fast as we can. */
3122 set_specpdl_symbol (symbol
);
3123 set_specpdl_old_value (SYMBOL_VAL (sym
));
3124 specpdl_ptr
->func
= NULL
;
3127 SET_SYMBOL_VAL (sym
, value
);
3129 set_internal (symbol
, value
, Qnil
, 1);
3131 case SYMBOL_LOCALIZED
:
3132 if (SYMBOL_BLV (sym
)->frame_local
)
3133 error ("Frame-local vars cannot be let-bound");
3134 case SYMBOL_FORWARDED
:
3136 Lisp_Object ovalue
= find_symbol_value (symbol
);
3137 specpdl_ptr
->func
= 0;
3138 set_specpdl_old_value (ovalue
);
3140 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3141 || (EQ (SYMBOL_BLV (sym
)->where
,
3142 SYMBOL_BLV (sym
)->frame_local
?
3143 Fselected_frame () : Fcurrent_buffer ())));
3145 if (sym
->redirect
== SYMBOL_LOCALIZED
3146 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3148 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3150 /* For a local variable, record both the symbol and which
3151 buffer's or frame's value we are saving. */
3152 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3154 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3155 || (blv_found (SYMBOL_BLV (sym
))
3156 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3159 else if (sym
->redirect
== SYMBOL_LOCALIZED
3160 && blv_found (SYMBOL_BLV (sym
)))
3161 where
= SYMBOL_BLV (sym
)->where
;
3165 /* We're not using the `unused' slot in the specbinding
3166 structure because this would mean we have to do more
3167 work for simple variables. */
3168 /* FIXME: The third value `current_buffer' is only used in
3169 let_shadows_buffer_binding_p which is itself only used
3170 in set_internal for local_if_set. */
3171 eassert (NILP (where
) || EQ (where
, cur_buf
));
3172 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3174 /* If SYMBOL is a per-buffer variable which doesn't have a
3175 buffer-local value here, make the `let' change the global
3176 value by changing the value of SYMBOL in all buffers not
3177 having their own value. This is consistent with what
3178 happens with other buffer-local variables. */
3180 && sym
->redirect
== SYMBOL_FORWARDED
)
3182 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3184 Fset_default (symbol
, value
);
3189 set_specpdl_symbol (symbol
);
3192 set_internal (symbol
, value
, Qnil
, 1);
3195 default: emacs_abort ();
3200 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3202 eassert (!handling_signal
);
3204 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3206 specpdl_ptr
->func
= function
;
3207 set_specpdl_symbol (Qnil
);
3208 set_specpdl_old_value (arg
);
3213 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3215 Lisp_Object quitf
= Vquit_flag
;
3216 struct gcpro gcpro1
, gcpro2
;
3218 GCPRO2 (value
, quitf
);
3221 while (specpdl_ptr
!= specpdl
+ count
)
3223 /* Copy the binding, and decrement specpdl_ptr, before we do
3224 the work to unbind it. We decrement first
3225 so that an error in unbinding won't try to unbind
3226 the same entry again, and we copy the binding first
3227 in case more bindings are made during some of the code we run. */
3229 struct specbinding this_binding
;
3230 this_binding
= *--specpdl_ptr
;
3232 if (this_binding
.func
!= 0)
3233 (*this_binding
.func
) (this_binding
.old_value
);
3234 /* If the symbol is a list, it is really (SYMBOL WHERE
3235 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3236 frame. If WHERE is a buffer or frame, this indicates we
3237 bound a variable that had a buffer-local or frame-local
3238 binding. WHERE nil means that the variable had the default
3239 value when it was bound. CURRENT-BUFFER is the buffer that
3240 was current when the variable was bound. */
3241 else if (CONSP (this_binding
.symbol
))
3243 Lisp_Object symbol
, where
;
3245 symbol
= XCAR (this_binding
.symbol
);
3246 where
= XCAR (XCDR (this_binding
.symbol
));
3249 Fset_default (symbol
, this_binding
.old_value
);
3250 /* If `where' is non-nil, reset the value in the appropriate
3251 local binding, but only if that binding still exists. */
3252 else if (BUFFERP (where
)
3253 ? !NILP (Flocal_variable_p (symbol
, where
))
3254 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3255 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3257 /* If variable has a trivial value (no forwarding), we can
3258 just set it. No need to check for constant symbols here,
3259 since that was already done by specbind. */
3260 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3261 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3262 this_binding
.old_value
);
3264 /* NOTE: we only ever come here if make_local_foo was used for
3265 the first time on this var within this let. */
3266 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3269 if (NILP (Vquit_flag
) && !NILP (quitf
))
3276 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3277 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3278 A special variable is one that will be bound dynamically, even in a
3279 context where binding is lexical by default. */)
3280 (Lisp_Object symbol
)
3282 CHECK_SYMBOL (symbol
);
3283 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3287 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3288 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3289 The debugger is entered when that frame exits, if the flag is non-nil. */)
3290 (Lisp_Object level
, Lisp_Object flag
)
3292 register struct backtrace
*backlist
= backtrace_list
;
3293 register EMACS_INT i
;
3295 CHECK_NUMBER (level
);
3297 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3299 backlist
= backlist
->next
;
3303 backlist
->debug_on_exit
= !NILP (flag
);
3308 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3309 doc
: /* Print a trace of Lisp function calls currently active.
3310 Output stream used is value of `standard-output'. */)
3313 register struct backtrace
*backlist
= backtrace_list
;
3316 struct gcpro gcpro1
;
3317 Lisp_Object old_print_level
= Vprint_level
;
3319 if (NILP (Vprint_level
))
3320 XSETFASTINT (Vprint_level
, 8);
3327 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3328 if (backlist
->nargs
== UNEVALLED
)
3330 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3331 write_string ("\n", -1);
3335 tem
= *backlist
->function
;
3336 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3337 write_string ("(", -1);
3338 if (backlist
->nargs
== MANY
)
3339 { /* FIXME: Can this happen? */
3341 for (tail
= *backlist
->args
; !NILP (tail
); tail
= Fcdr (tail
))
3344 write_string (" ", -1);
3345 Fprin1 (Fcar (tail
), Qnil
);
3352 for (i
= 0; i
< backlist
->nargs
; i
++)
3354 if (i
) write_string (" ", -1);
3355 Fprin1 (backlist
->args
[i
], Qnil
);
3358 write_string (")\n", -1);
3360 backlist
= backlist
->next
;
3363 Vprint_level
= old_print_level
;
3368 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3369 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3370 If that frame has not evaluated the arguments yet (or is a special form),
3371 the value is (nil FUNCTION ARG-FORMS...).
3372 If that frame has evaluated its arguments and called its function already,
3373 the value is (t FUNCTION ARG-VALUES...).
3374 A &rest arg is represented as the tail of the list ARG-VALUES.
3375 FUNCTION is whatever was supplied as car of evaluated list,
3376 or a lambda expression for macro calls.
3377 If NFRAMES is more than the number of frames, the value is nil. */)
3378 (Lisp_Object nframes
)
3380 register struct backtrace
*backlist
= backtrace_list
;
3381 register EMACS_INT i
;
3384 CHECK_NATNUM (nframes
);
3386 /* Find the frame requested. */
3387 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3388 backlist
= backlist
->next
;
3392 if (backlist
->nargs
== UNEVALLED
)
3393 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3396 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3397 tem
= *backlist
->args
;
3399 tem
= Flist (backlist
->nargs
, backlist
->args
);
3401 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3408 mark_backtrace (void)
3410 register struct backtrace
*backlist
;
3413 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3415 mark_object (*backlist
->function
);
3417 if (backlist
->nargs
== UNEVALLED
3418 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3421 i
= backlist
->nargs
;
3423 mark_object (backlist
->args
[i
]);
3431 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3432 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3433 If Lisp code tries to increase the total number past this amount,
3434 an error is signaled.
3435 You can safely use a value considerably larger than the default value,
3436 if that proves inconveniently small. However, if you increase it too far,
3437 Emacs could run out of memory trying to make the stack bigger. */);
3439 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3440 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3442 This limit serves to catch infinite recursions for you before they cause
3443 actual stack overflow in C, which would be fatal for Emacs.
3444 You can safely make it considerably larger than its default value,
3445 if that proves inconveniently small. However, if you increase it too far,
3446 Emacs could overflow the real C stack, and crash. */);
3448 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3449 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3450 If the value is t, that means do an ordinary quit.
3451 If the value equals `throw-on-input', that means quit by throwing
3452 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3453 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3454 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3457 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3458 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3459 Note that `quit-flag' will still be set by typing C-g,
3460 so a quit will be signaled as soon as `inhibit-quit' is nil.
3461 To prevent this happening, set `quit-flag' to nil
3462 before making `inhibit-quit' nil. */);
3463 Vinhibit_quit
= Qnil
;
3465 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3466 DEFSYM (Qautoload
, "autoload");
3467 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3468 DEFSYM (Qmacro
, "macro");
3469 DEFSYM (Qdeclare
, "declare");
3471 /* Note that the process handling also uses Qexit, but we don't want
3472 to staticpro it twice, so we just do it here. */
3473 DEFSYM (Qexit
, "exit");
3475 DEFSYM (Qinteractive
, "interactive");
3476 DEFSYM (Qcommandp
, "commandp");
3477 DEFSYM (Qand_rest
, "&rest");
3478 DEFSYM (Qand_optional
, "&optional");
3479 DEFSYM (Qclosure
, "closure");
3480 DEFSYM (Qdebug
, "debug");
3482 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3483 doc
: /* Non-nil means never enter the debugger.
3484 Normally set while the debugger is already active, to avoid recursive
3486 Vinhibit_debugger
= Qnil
;
3488 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3489 doc
: /* Non-nil means enter debugger if an error is signaled.
3490 Does not apply to errors handled by `condition-case' or those
3491 matched by `debug-ignored-errors'.
3492 If the value is a list, an error only means to enter the debugger
3493 if one of its condition symbols appears in the list.
3494 When you evaluate an expression interactively, this variable
3495 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3496 The command `toggle-debug-on-error' toggles this.
3497 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3498 Vdebug_on_error
= Qnil
;
3500 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3501 doc
: /* List of errors for which the debugger should not be called.
3502 Each element may be a condition-name or a regexp that matches error messages.
3503 If any element applies to a given error, that error skips the debugger
3504 and just returns to top level.
3505 This overrides the variable `debug-on-error'.
3506 It does not apply to errors handled by `condition-case'. */);
3507 Vdebug_ignored_errors
= Qnil
;
3509 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3510 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3511 Does not apply if quit is handled by a `condition-case'. */);
3514 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3515 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3517 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3518 doc
: /* Non-nil means debugger may continue execution.
3519 This is nil when the debugger is called under circumstances where it
3520 might not be safe to continue. */);
3521 debugger_may_continue
= 1;
3523 DEFVAR_LISP ("debugger", Vdebugger
,
3524 doc
: /* Function to call to invoke debugger.
3525 If due to frame exit, args are `exit' and the value being returned;
3526 this function's value will be returned instead of that.
3527 If due to error, args are `error' and a list of the args to `signal'.
3528 If due to `apply' or `funcall' entry, one arg, `lambda'.
3529 If due to `eval' entry, one arg, t. */);
3532 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3533 doc
: /* If non-nil, this is a function for `signal' to call.
3534 It receives the same arguments that `signal' was given.
3535 The Edebug package uses this to regain control. */);
3536 Vsignal_hook_function
= Qnil
;
3538 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3539 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3540 Note that `debug-on-error', `debug-on-quit' and friends
3541 still determine whether to handle the particular condition. */);
3542 Vdebug_on_signal
= Qnil
;
3544 /* When lexical binding is being used,
3545 Vinternal_interpreter_environment is non-nil, and contains an alist
3546 of lexically-bound variable, or (t), indicating an empty
3547 environment. The lisp name of this variable would be
3548 `internal-interpreter-environment' if it weren't hidden.
3549 Every element of this list can be either a cons (VAR . VAL)
3550 specifying a lexical binding, or a single symbol VAR indicating
3551 that this variable should use dynamic scoping. */
3552 DEFSYM (Qinternal_interpreter_environment
,
3553 "internal-interpreter-environment");
3554 DEFVAR_LISP ("internal-interpreter-environment",
3555 Vinternal_interpreter_environment
,
3556 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3557 When lexical binding is not being used, this variable is nil.
3558 A value of `(t)' indicates an empty environment, otherwise it is an
3559 alist of active lexical bindings. */);
3560 Vinternal_interpreter_environment
= Qnil
;
3561 /* Don't export this variable to Elisp, so no one can mess with it
3562 (Just imagine if someone makes it buffer-local). */
3563 Funintern (Qinternal_interpreter_environment
, Qnil
);
3565 DEFSYM (Vrun_hooks
, "run-hooks");
3567 staticpro (&Vautoload_queue
);
3568 Vautoload_queue
= Qnil
;
3569 staticpro (&Vsignaling_function
);
3570 Vsignaling_function
= Qnil
;
3572 inhibit_lisp_code
= Qnil
;
3583 defsubr (&Sfunction
);
3585 defsubr (&Sdefvaralias
);
3586 defsubr (&Sdefconst
);
3587 defsubr (&Smake_var_non_special
);
3591 defsubr (&Smacroexpand
);
3594 defsubr (&Sunwind_protect
);
3595 defsubr (&Scondition_case
);
3597 defsubr (&Sinteractive_p
);
3598 defsubr (&Scalled_interactively_p
);
3599 defsubr (&Scommandp
);
3600 defsubr (&Sautoload
);
3601 defsubr (&Sautoload_do_load
);
3604 defsubr (&Sfuncall
);
3605 defsubr (&Srun_hooks
);
3606 defsubr (&Srun_hook_with_args
);
3607 defsubr (&Srun_hook_with_args_until_success
);
3608 defsubr (&Srun_hook_with_args_until_failure
);
3609 defsubr (&Srun_hook_wrapped
);
3610 defsubr (&Sfetch_bytecode
);
3611 defsubr (&Sbacktrace_debug
);
3612 defsubr (&Sbacktrace
);
3613 defsubr (&Sbacktrace_frame
);
3614 defsubr (&Sspecial_variable_p
);
3615 defsubr (&Sfunctionp
);