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/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
37 struct backtrace
*next
;
38 Lisp_Object
*function
;
39 Lisp_Object
*args
; /* Points to vector of args. */
40 ptrdiff_t nargs
; /* Length of vector. */
41 /* Nonzero means call value of debugger when done with this operation. */
42 unsigned int debug_on_exit
: 1;
45 static struct backtrace
*backtrace_list
;
50 struct catchtag
*catchlist
;
52 /* Chain of condition handlers currently in effect.
53 The elements of this chain are contained in the stack frames
54 of Fcondition_case and internal_condition_case.
55 When an error is signaled (by calling Fsignal, below),
56 this chain is searched for an element that applies. */
61 struct handler
*handlerlist
;
64 /* Count levels of GCPRO to detect failure to UNGCPRO. */
68 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
69 Lisp_Object Qinhibit_quit
;
70 Lisp_Object Qand_rest
;
71 static Lisp_Object Qand_optional
;
72 static Lisp_Object Qinhibit_debugger
;
73 static Lisp_Object Qdeclare
;
74 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
76 static Lisp_Object Qdebug
;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
82 Lisp_Object Vrun_hooks
;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue
;
91 /* Current number of specbindings allocated in specpdl. */
93 ptrdiff_t specpdl_size
;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding
*specpdl
;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding
*specpdl_ptr
;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth
;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
114 static EMACS_INT when_entered_debugger
;
116 /* The function from which the last `signal' was called. Set in
119 Lisp_Object Vsignaling_function
;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
127 /* If non-nil, Lisp code must not be run since some part of Emacs is
128 in an inconsistent state. Currently, x-create-frame uses this to
129 avoid triggering window-configuration-change-hook while the new
130 frame is half-initialized. */
131 Lisp_Object inhibit_lisp_code
;
133 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
134 static bool interactive_p (void);
135 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
137 /* Functions to set Lisp_Object slots of struct specbinding. */
140 set_specpdl_symbol (Lisp_Object symbol
)
142 specpdl_ptr
->symbol
= symbol
;
146 set_specpdl_old_value (Lisp_Object oldval
)
148 specpdl_ptr
->old_value
= oldval
;
152 init_eval_once (void)
155 specpdl
= xmalloc (size
* sizeof *specpdl
);
157 specpdl_ptr
= specpdl
;
158 /* Don't forget to update docs (lispref node "Local Variables"). */
159 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
160 max_lisp_eval_depth
= 600;
168 specpdl_ptr
= specpdl
;
173 debug_on_next_call
= 0;
178 /* This is less than the initial value of num_nonmacro_input_events. */
179 when_entered_debugger
= -1;
182 /* Unwind-protect function used by call_debugger. */
185 restore_stack_limits (Lisp_Object data
)
187 max_specpdl_size
= XINT (XCAR (data
));
188 max_lisp_eval_depth
= XINT (XCDR (data
));
192 /* Call the Lisp debugger, giving it argument ARG. */
195 call_debugger (Lisp_Object arg
)
197 bool debug_while_redisplaying
;
198 ptrdiff_t count
= SPECPDL_INDEX ();
200 EMACS_INT old_max
= max_specpdl_size
;
202 /* Temporarily bump up the stack limits,
203 so the debugger won't run out of stack. */
205 max_specpdl_size
+= 1;
206 record_unwind_protect (restore_stack_limits
,
207 Fcons (make_number (old_max
),
208 make_number (max_lisp_eval_depth
)));
209 max_specpdl_size
= old_max
;
211 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
212 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
214 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
215 max_specpdl_size
= SPECPDL_INDEX () + 100;
217 #ifdef HAVE_WINDOW_SYSTEM
218 if (display_hourglass_p
)
222 debug_on_next_call
= 0;
223 when_entered_debugger
= num_nonmacro_input_events
;
225 /* Resetting redisplaying_p to 0 makes sure that debug output is
226 displayed if the debugger is invoked during redisplay. */
227 debug_while_redisplaying
= redisplaying_p
;
229 specbind (intern ("debugger-may-continue"),
230 debug_while_redisplaying
? Qnil
: Qt
);
231 specbind (Qinhibit_redisplay
, Qnil
);
232 specbind (Qinhibit_debugger
, Qt
);
234 #if 0 /* Binding this prevents execution of Lisp code during
235 redisplay, which necessarily leads to display problems. */
236 specbind (Qinhibit_eval_during_redisplay
, Qt
);
239 val
= apply1 (Vdebugger
, arg
);
241 /* Interrupting redisplay and resuming it later is not safe under
242 all circumstances. So, when the debugger returns, abort the
243 interrupted redisplay by going back to the top-level. */
244 if (debug_while_redisplaying
)
247 return unbind_to (count
, val
);
251 do_debug_on_call (Lisp_Object code
)
253 debug_on_next_call
= 0;
254 backtrace_list
->debug_on_exit
= 1;
255 call_debugger (Fcons (code
, Qnil
));
258 /* NOTE!!! Every function that can call EVAL must protect its args
259 and temporaries from garbage collection while it needs them.
260 The definition of `For' shows what you have to do. */
262 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
263 doc
: /* Eval args until one of them yields non-nil, then return that value.
264 The remaining args are not evalled at all.
265 If all args return nil, return nil.
266 usage: (or CONDITIONS...) */)
269 register Lisp_Object val
= Qnil
;
276 val
= eval_sub (XCAR (args
));
286 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
287 doc
: /* Eval args until one of them yields nil, then return nil.
288 The remaining args are not evalled at all.
289 If no arg yields nil, return the last arg's value.
290 usage: (and CONDITIONS...) */)
293 register Lisp_Object val
= Qt
;
300 val
= eval_sub (XCAR (args
));
310 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
311 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
312 Returns the value of THEN or the value of the last of the ELSE's.
313 THEN must be one expression, but ELSE... can be zero or more expressions.
314 If COND yields nil, and there are no ELSE's, the value is nil.
315 usage: (if COND THEN ELSE...) */)
318 register Lisp_Object cond
;
322 cond
= eval_sub (Fcar (args
));
326 return eval_sub (Fcar (Fcdr (args
)));
327 return Fprogn (Fcdr (Fcdr (args
)));
330 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
331 doc
: /* Try each clause until one succeeds.
332 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
333 and, if the value is non-nil, this clause succeeds:
334 then the expressions in BODY are evaluated and the last one's
335 value is the value of the cond-form.
336 If no clause succeeds, cond returns nil.
337 If a clause has one element, as in (CONDITION),
338 CONDITION's value if non-nil is returned from the cond-form.
339 usage: (cond CLAUSES...) */)
342 register Lisp_Object clause
, val
;
349 clause
= Fcar (args
);
350 val
= eval_sub (Fcar (clause
));
353 if (!EQ (XCDR (clause
), Qnil
))
354 val
= Fprogn (XCDR (clause
));
364 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
365 doc
: /* Eval BODY forms sequentially and return value of last one.
366 usage: (progn BODY...) */)
369 register Lisp_Object val
= Qnil
;
376 val
= eval_sub (XCAR (args
));
384 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
385 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
386 The value of FIRST is saved during the evaluation of the remaining args,
387 whose values are discarded.
388 usage: (prog1 FIRST BODY...) */)
392 register Lisp_Object args_left
;
393 struct gcpro gcpro1
, gcpro2
;
399 val
= eval_sub (XCAR (args_left
));
400 while (CONSP (args_left
= XCDR (args_left
)))
401 eval_sub (XCAR (args_left
));
407 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
408 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
409 The value of FORM2 is saved during the evaluation of the
410 remaining args, whose values are discarded.
411 usage: (prog2 FORM1 FORM2 BODY...) */)
417 eval_sub (XCAR (args
));
419 return Fprog1 (XCDR (args
));
422 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
423 doc
: /* Set each SYM to the value of its VAL.
424 The symbols SYM are variables; they are literal (not evaluated).
425 The values VAL are expressions; they are evaluated.
426 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
427 The second VAL is not computed until after the first SYM is set, and so on;
428 each VAL can use the new value of variables set earlier in the `setq'.
429 The return value of the `setq' form is the value of the last VAL.
430 usage: (setq [SYM VAL]...) */)
433 register Lisp_Object args_left
;
434 register Lisp_Object val
, sym
, lex_binding
;
445 val
= eval_sub (Fcar (Fcdr (args_left
)));
446 sym
= Fcar (args_left
);
448 /* Like for eval_sub, we do not check declared_special here since
449 it's been done when let-binding. */
450 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
452 && !NILP (lex_binding
453 = Fassq (sym
, Vinternal_interpreter_environment
)))
454 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
456 Fset (sym
, val
); /* SYM is dynamically bound. */
458 args_left
= Fcdr (Fcdr (args_left
));
460 while (!NILP (args_left
));
466 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
467 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
468 Warning: `quote' does not construct its return value, but just returns
469 the value that was pre-constructed by the Lisp reader (see info node
470 `(elisp)Printed Representation').
471 This means that '(a . b) is not identical to (cons 'a 'b): the former
472 does not cons. Quoting should be reserved for constants that will
473 never be modified by side-effects, unless you like self-modifying code.
474 See the common pitfall in info node `(elisp)Rearrangement' for an example
475 of unexpected results when a quoted object is modified.
476 usage: (quote ARG) */)
479 if (!NILP (Fcdr (args
)))
480 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
484 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
485 doc
: /* Like `quote', but preferred for objects which are functions.
486 In byte compilation, `function' causes its argument to be compiled.
487 `quote' cannot do that.
488 usage: (function ARG) */)
491 Lisp_Object quoted
= XCAR (args
);
493 if (!NILP (Fcdr (args
)))
494 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
496 if (!NILP (Vinternal_interpreter_environment
)
498 && EQ (XCAR (quoted
), Qlambda
))
499 /* This is a lambda expression within a lexical environment;
500 return an interpreted closure instead of a simple lambda. */
501 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
504 /* Simply quote the argument. */
509 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
510 doc
: /* Return t if the containing function was run directly by user input.
511 This means that the function was called with `call-interactively'
512 \(which includes being called as the binding of a key)
513 and input is currently coming from the keyboard (not a keyboard macro),
514 and Emacs is not running in batch mode (`noninteractive' is nil).
516 The only known proper use of `interactive-p' is in deciding whether to
517 display a helpful message, or how to display it. If you're thinking
518 of using it for any other purpose, it is quite likely that you're
519 making a mistake. Think: what do you want to do when the command is
520 called from a keyboard macro?
522 To test whether your function was called with `call-interactively',
523 either (i) add an extra optional argument and give it an `interactive'
524 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
525 use `called-interactively-p'. */)
528 return interactive_p () ? Qt
: Qnil
;
532 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
533 doc
: /* Return t if the containing function was called by `call-interactively'.
534 If KIND is `interactive', then only return t if the call was made
535 interactively by the user, i.e. not in `noninteractive' mode nor
536 when `executing-kbd-macro'.
537 If KIND is `any', on the other hand, it will return t for any kind of
538 interactive call, including being called as the binding of a key, or
539 from a keyboard macro, or in `noninteractive' mode.
541 The only known proper use of `interactive' for KIND is in deciding
542 whether to display a helpful message, or how to display it. If you're
543 thinking of using it for any other purpose, it is quite likely that
544 you're making a mistake. Think: what do you want to do when the
545 command is called from a keyboard macro?
547 Instead of using this function, it is sometimes cleaner to give your
548 function an extra optional argument whose `interactive' spec specifies
549 non-nil unconditionally (\"p\" is a good way to do this), or via
550 \(not (or executing-kbd-macro noninteractive)). */)
553 return (((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
559 /* Return true if function in which this appears was called using
560 call-interactively and is not a built-in. */
565 struct backtrace
*btp
;
568 btp
= backtrace_list
;
570 /* If this isn't a byte-compiled function, there may be a frame at
571 the top for Finteractive_p. If so, skip it. */
572 fun
= Findirect_function (*btp
->function
, Qnil
);
573 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
574 || XSUBR (fun
) == &Scalled_interactively_p
))
577 /* If we're running an Emacs 18-style byte-compiled function, there
578 may be a frame for Fbytecode at the top level. In any version of
579 Emacs there can be Fbytecode frames for subexpressions evaluated
580 inside catch and condition-case. Skip past them.
582 If this isn't a byte-compiled function, then we may now be
583 looking at several frames for special forms. Skip past them. */
585 && (EQ (*btp
->function
, Qbytecode
)
586 || btp
->nargs
== UNEVALLED
))
589 /* `btp' now points at the frame of the innermost function that isn't
590 a special form, ignoring frames for Finteractive_p and/or
591 Fbytecode at the top. If this frame is for a built-in function
592 (such as load or eval-region) return false. */
593 fun
= Findirect_function (*btp
->function
, Qnil
);
597 /* `btp' points to the frame of a Lisp function that called interactive-p.
598 Return t if that function was called interactively. */
599 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
605 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
606 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
607 Aliased variables always have the same value; setting one sets the other.
608 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
609 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
610 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
611 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
612 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
613 The return value is BASE-VARIABLE. */)
614 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
616 struct Lisp_Symbol
*sym
;
618 CHECK_SYMBOL (new_alias
);
619 CHECK_SYMBOL (base_variable
);
621 sym
= XSYMBOL (new_alias
);
624 /* Not sure why, but why not? */
625 error ("Cannot make a constant an alias");
627 switch (sym
->redirect
)
629 case SYMBOL_FORWARDED
:
630 error ("Cannot make an internal variable an alias");
631 case SYMBOL_LOCALIZED
:
632 error ("Don't know how to make a localized variable an alias");
635 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
636 If n_a is bound, but b_v is not, set the value of b_v to n_a,
637 so that old-code that affects n_a before the aliasing is setup
639 if (NILP (Fboundp (base_variable
)))
640 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
643 struct specbinding
*p
;
645 for (p
= specpdl_ptr
; p
> specpdl
; )
646 if ((--p
)->func
== NULL
648 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
649 error ("Don't know how to make a let-bound variable an alias");
652 sym
->declared_special
= 1;
653 XSYMBOL (base_variable
)->declared_special
= 1;
654 sym
->redirect
= SYMBOL_VARALIAS
;
655 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
656 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
657 LOADHIST_ATTACH (new_alias
);
658 /* Even if docstring is nil: remove old docstring. */
659 Fput (new_alias
, Qvariable_documentation
, docstring
);
661 return base_variable
;
665 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
666 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
667 You are not required to define a variable in order to use it, but
668 defining it lets you supply an initial value and documentation, which
669 can be referred to by the Emacs help facilities and other programming
670 tools. The `defvar' form also declares the variable as \"special\",
671 so that it is always dynamically bound even if `lexical-binding' is t.
673 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
674 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
675 default value is what is set; buffer-local values are not affected.
676 If INITVALUE is missing, SYMBOL's value is not set.
678 If SYMBOL has a local binding, then this form affects the local
679 binding. This is usually not what you want. Thus, if you need to
680 load a file defining variables, with this form or with `defconst' or
681 `defcustom', you should always load that file _outside_ any bindings
682 for these variables. \(`defconst' and `defcustom' behave similarly in
685 The optional argument DOCSTRING is a documentation string for the
688 To define a user option, use `defcustom' instead of `defvar'.
689 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
692 register Lisp_Object sym
, tem
, tail
;
696 if (!NILP (Fcdr (Fcdr (tail
))))
697 error ("Too many arguments");
699 tem
= Fdefault_boundp (sym
);
702 /* Do it before evaluating the initial value, for self-references. */
703 XSYMBOL (sym
)->declared_special
= 1;
706 Fset_default (sym
, eval_sub (Fcar (tail
)));
708 { /* Check if there is really a global binding rather than just a let
709 binding that shadows the global unboundness of the var. */
710 struct specbinding
*pdl
= specpdl_ptr
;
711 while (pdl
> specpdl
)
713 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
714 && EQ (pdl
->old_value
, Qunbound
))
716 message_with_string ("Warning: defvar ignored because %s is let-bound",
717 SYMBOL_NAME (sym
), 1);
726 if (!NILP (Vpurify_flag
))
727 tem
= Fpurecopy (tem
);
728 Fput (sym
, Qvariable_documentation
, tem
);
730 LOADHIST_ATTACH (sym
);
732 else if (!NILP (Vinternal_interpreter_environment
)
733 && !XSYMBOL (sym
)->declared_special
)
734 /* A simple (defvar foo) with lexical scoping does "nothing" except
735 declare that var to be dynamically scoped *locally* (i.e. within
736 the current file or let-block). */
737 Vinternal_interpreter_environment
=
738 Fcons (sym
, Vinternal_interpreter_environment
);
741 /* Simple (defvar <var>) should not count as a definition at all.
742 It could get in the way of other definitions, and unloading this
743 package could try to make the variable unbound. */
749 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
750 doc
: /* Define SYMBOL as a constant variable.
751 This declares that neither programs nor users should ever change the
752 value. This constancy is not actually enforced by Emacs Lisp, but
753 SYMBOL is marked as a special variable so that it is never lexically
756 The `defconst' form always sets the value of SYMBOL to the result of
757 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
758 what is set; buffer-local values are not affected. If SYMBOL has a
759 local binding, then this form sets the local binding's value.
760 However, you should normally not make local bindings for variables
761 defined with this form.
763 The optional DOCSTRING specifies the variable's documentation string.
764 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
767 register Lisp_Object sym
, tem
;
770 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
771 error ("Too many arguments");
773 tem
= eval_sub (Fcar (Fcdr (args
)));
774 if (!NILP (Vpurify_flag
))
775 tem
= Fpurecopy (tem
);
776 Fset_default (sym
, tem
);
777 XSYMBOL (sym
)->declared_special
= 1;
778 tem
= Fcar (Fcdr (Fcdr (args
)));
781 if (!NILP (Vpurify_flag
))
782 tem
= Fpurecopy (tem
);
783 Fput (sym
, Qvariable_documentation
, tem
);
785 Fput (sym
, Qrisky_local_variable
, Qt
);
786 LOADHIST_ATTACH (sym
);
790 /* Make SYMBOL lexically scoped. */
791 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
792 Smake_var_non_special
, 1, 1, 0,
793 doc
: /* Internal function. */)
796 CHECK_SYMBOL (symbol
);
797 XSYMBOL (symbol
)->declared_special
= 0;
802 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
803 doc
: /* Bind variables according to VARLIST then eval BODY.
804 The value of the last form in BODY is returned.
805 Each element of VARLIST is a symbol (which is bound to nil)
806 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
807 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
808 usage: (let* VARLIST BODY...) */)
811 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
812 ptrdiff_t count
= SPECPDL_INDEX ();
813 struct gcpro gcpro1
, gcpro2
, gcpro3
;
815 GCPRO3 (args
, elt
, varlist
);
817 lexenv
= Vinternal_interpreter_environment
;
819 varlist
= Fcar (args
);
820 while (CONSP (varlist
))
824 elt
= XCAR (varlist
);
830 else if (! NILP (Fcdr (Fcdr (elt
))))
831 signal_error ("`let' bindings can have only one value-form", elt
);
835 val
= eval_sub (Fcar (Fcdr (elt
)));
838 if (!NILP (lexenv
) && SYMBOLP (var
)
839 && !XSYMBOL (var
)->declared_special
840 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
841 /* Lexically bind VAR by adding it to the interpreter's binding
845 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
846 if (EQ (Vinternal_interpreter_environment
, lexenv
))
847 /* Save the old lexical environment on the specpdl stack,
848 but only for the first lexical binding, since we'll never
849 need to revert to one of the intermediate ones. */
850 specbind (Qinternal_interpreter_environment
, newenv
);
852 Vinternal_interpreter_environment
= newenv
;
857 varlist
= XCDR (varlist
);
860 val
= Fprogn (Fcdr (args
));
861 return unbind_to (count
, val
);
864 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
865 doc
: /* Bind variables according to VARLIST then eval BODY.
866 The value of the last form in BODY is returned.
867 Each element of VARLIST is a symbol (which is bound to nil)
868 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
869 All the VALUEFORMs are evalled before any symbols are bound.
870 usage: (let VARLIST BODY...) */)
873 Lisp_Object
*temps
, tem
, lexenv
;
874 register Lisp_Object elt
, varlist
;
875 ptrdiff_t count
= SPECPDL_INDEX ();
877 struct gcpro gcpro1
, gcpro2
;
880 varlist
= Fcar (args
);
882 /* Make space to hold the values to give the bound variables. */
883 elt
= Flength (varlist
);
884 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
886 /* Compute the values and store them in `temps'. */
888 GCPRO2 (args
, *temps
);
891 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
894 elt
= XCAR (varlist
);
896 temps
[argnum
++] = Qnil
;
897 else if (! NILP (Fcdr (Fcdr (elt
))))
898 signal_error ("`let' bindings can have only one value-form", elt
);
900 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
901 gcpro2
.nvars
= argnum
;
905 lexenv
= Vinternal_interpreter_environment
;
907 varlist
= Fcar (args
);
908 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
912 elt
= XCAR (varlist
);
913 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
914 tem
= temps
[argnum
++];
916 if (!NILP (lexenv
) && SYMBOLP (var
)
917 && !XSYMBOL (var
)->declared_special
918 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
919 /* Lexically bind VAR by adding it to the lexenv alist. */
920 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
922 /* Dynamically bind VAR. */
926 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
927 /* Instantiate a new lexical environment. */
928 specbind (Qinternal_interpreter_environment
, lexenv
);
930 elt
= Fprogn (Fcdr (args
));
932 return unbind_to (count
, elt
);
935 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
936 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
937 The order of execution is thus TEST, BODY, TEST, BODY and so on
938 until TEST returns nil.
939 usage: (while TEST BODY...) */)
942 Lisp_Object test
, body
;
943 struct gcpro gcpro1
, gcpro2
;
949 while (!NILP (eval_sub (test
)))
959 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
960 doc
: /* Return result of expanding macros at top level of FORM.
961 If FORM is not a macro call, it is returned unchanged.
962 Otherwise, the macro is expanded and the expansion is considered
963 in place of FORM. When a non-macro-call results, it is returned.
965 The second optional arg ENVIRONMENT specifies an environment of macro
966 definitions to shadow the loaded ones for use in file byte-compilation. */)
967 (Lisp_Object form
, Lisp_Object environment
)
969 /* With cleanups from Hallvard Furuseth. */
970 register Lisp_Object expander
, sym
, def
, tem
;
974 /* Come back here each time we expand a macro call,
975 in case it expands into another macro call. */
978 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
979 def
= sym
= XCAR (form
);
981 /* Trace symbols aliases to other symbols
982 until we get a symbol that is not an alias. */
983 while (SYMBOLP (def
))
987 tem
= Fassq (sym
, environment
);
990 def
= XSYMBOL (sym
)->function
;
991 if (!EQ (def
, Qunbound
))
996 /* Right now TEM is the result from SYM in ENVIRONMENT,
997 and if TEM is nil then DEF is SYM's function definition. */
1000 /* SYM is not mentioned in ENVIRONMENT.
1001 Look at its function definition. */
1002 struct gcpro gcpro1
;
1004 def
= Fautoload_do_load (def
, sym
, Qmacro
);
1006 if (EQ (def
, Qunbound
) || !CONSP (def
))
1007 /* Not defined or definition not suitable. */
1009 if (!EQ (XCAR (def
), Qmacro
))
1011 else expander
= XCDR (def
);
1015 expander
= XCDR (tem
);
1016 if (NILP (expander
))
1020 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1021 if (EQ (form
, newform
))
1030 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1031 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1032 TAG is evalled to get the tag to use; it must not be nil.
1034 Then the BODY is executed.
1035 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1036 If no throw happens, `catch' returns the value of the last BODY form.
1037 If a throw happens, it specifies the value to return from `catch'.
1038 usage: (catch TAG BODY...) */)
1041 register Lisp_Object tag
;
1042 struct gcpro gcpro1
;
1045 tag
= eval_sub (Fcar (args
));
1047 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1050 /* Set up a catch, then call C function FUNC on argument ARG.
1051 FUNC should return a Lisp_Object.
1052 This is how catches are done from within C code. */
1055 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1057 /* This structure is made part of the chain `catchlist'. */
1060 /* Fill in the components of c, and put it on the list. */
1064 c
.backlist
= backtrace_list
;
1065 c
.handlerlist
= handlerlist
;
1066 c
.lisp_eval_depth
= lisp_eval_depth
;
1067 c
.pdlcount
= SPECPDL_INDEX ();
1068 c
.poll_suppress_count
= poll_suppress_count
;
1069 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1070 c
.gcpro
= gcprolist
;
1071 c
.byte_stack
= byte_stack_list
;
1075 if (! _setjmp (c
.jmp
))
1076 c
.val
= (*func
) (arg
);
1078 /* Throw works by a longjmp that comes right here. */
1083 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1084 jump to that CATCH, returning VALUE as the value of that catch.
1086 This is the guts Fthrow and Fsignal; they differ only in the way
1087 they choose the catch tag to throw to. A catch tag for a
1088 condition-case form has a TAG of Qnil.
1090 Before each catch is discarded, unbind all special bindings and
1091 execute all unwind-protect clauses made above that catch. Unwind
1092 the handler stack as we go, so that the proper handlers are in
1093 effect for each unwind-protect clause we run. At the end, restore
1094 some static info saved in CATCH, and longjmp to the location
1097 This is used for correct unwinding in Fthrow and Fsignal. */
1099 static _Noreturn
void
1100 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1104 /* Save the value in the tag. */
1107 /* Restore certain special C variables. */
1108 set_poll_suppress_count (catch->poll_suppress_count
);
1109 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1110 handling_signal
= 0;
1115 last_time
= catchlist
== catch;
1117 /* Unwind the specpdl stack, and then restore the proper set of
1119 unbind_to (catchlist
->pdlcount
, Qnil
);
1120 handlerlist
= catchlist
->handlerlist
;
1121 catchlist
= catchlist
->next
;
1123 while (! last_time
);
1126 /* If x_catch_errors was done, turn it off now.
1127 (First we give unbind_to a chance to do that.) */
1128 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1129 The catch must remain in effect during that delicate
1130 state. --lorentey */
1131 x_fully_uncatch_errors ();
1135 byte_stack_list
= catch->byte_stack
;
1136 gcprolist
= catch->gcpro
;
1138 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1140 backtrace_list
= catch->backlist
;
1141 lisp_eval_depth
= catch->lisp_eval_depth
;
1143 _longjmp (catch->jmp
, 1);
1146 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1147 doc
: /* Throw to the catch for TAG and return VALUE from it.
1148 Both TAG and VALUE are evalled. */)
1149 (register Lisp_Object tag
, Lisp_Object value
)
1151 register struct catchtag
*c
;
1154 for (c
= catchlist
; c
; c
= c
->next
)
1156 if (EQ (c
->tag
, tag
))
1157 unwind_to_catch (c
, value
);
1159 xsignal2 (Qno_catch
, tag
, value
);
1163 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1164 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1165 If BODYFORM completes normally, its value is returned
1166 after executing the UNWINDFORMS.
1167 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1168 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1172 ptrdiff_t count
= SPECPDL_INDEX ();
1174 record_unwind_protect (Fprogn
, Fcdr (args
));
1175 val
= eval_sub (Fcar (args
));
1176 return unbind_to (count
, val
);
1179 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1180 doc
: /* Regain control when an error is signaled.
1181 Executes BODYFORM and returns its value if no error happens.
1182 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1183 where the BODY is made of Lisp expressions.
1185 A handler is applicable to an error
1186 if CONDITION-NAME is one of the error's condition names.
1187 If an error happens, the first applicable handler is run.
1189 The car of a handler may be a list of condition names instead of a
1190 single condition name; then it handles all of them. If the special
1191 condition name `debug' is present in this list, it allows another
1192 condition in the list to run the debugger if `debug-on-error' and the
1193 other usual mechanisms says it should (otherwise, `condition-case'
1194 suppresses the debugger).
1196 When a handler handles an error, control returns to the `condition-case'
1197 and it executes the handler's BODY...
1198 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1199 \(If VAR is nil, the handler can't access that information.)
1200 Then the value of the last BODY form is returned from the `condition-case'
1203 See also the function `signal' for more info.
1204 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1207 Lisp_Object var
= Fcar (args
);
1208 Lisp_Object bodyform
= Fcar (Fcdr (args
));
1209 Lisp_Object handlers
= Fcdr (Fcdr (args
));
1211 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1214 /* Like Fcondition_case, but the args are separate
1215 rather than passed in a list. Used by Fbyte_code. */
1218 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1219 Lisp_Object handlers
)
1227 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1233 && (SYMBOLP (XCAR (tem
))
1234 || CONSP (XCAR (tem
))))))
1235 error ("Invalid condition handler: %s",
1236 SDATA (Fprin1_to_string (tem
, Qt
)));
1241 c
.backlist
= backtrace_list
;
1242 c
.handlerlist
= handlerlist
;
1243 c
.lisp_eval_depth
= lisp_eval_depth
;
1244 c
.pdlcount
= SPECPDL_INDEX ();
1245 c
.poll_suppress_count
= poll_suppress_count
;
1246 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1247 c
.gcpro
= gcprolist
;
1248 c
.byte_stack
= byte_stack_list
;
1249 if (_setjmp (c
.jmp
))
1252 specbind (h
.var
, c
.val
);
1253 val
= Fprogn (Fcdr (h
.chosen_clause
));
1255 /* Note that this just undoes the binding of h.var; whoever
1256 longjumped to us unwound the stack to c.pdlcount before
1258 unbind_to (c
.pdlcount
, Qnil
);
1265 h
.handler
= handlers
;
1266 h
.next
= handlerlist
;
1270 val
= eval_sub (bodyform
);
1272 handlerlist
= h
.next
;
1276 /* Call the function BFUN with no arguments, catching errors within it
1277 according to HANDLERS. If there is an error, call HFUN with
1278 one argument which is the data that describes the error:
1281 HANDLERS can be a list of conditions to catch.
1282 If HANDLERS is Qt, catch all errors.
1283 If HANDLERS is Qerror, catch all errors
1284 but allow the debugger to run if that is enabled. */
1287 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1288 Lisp_Object (*hfun
) (Lisp_Object
))
1296 c
.backlist
= backtrace_list
;
1297 c
.handlerlist
= handlerlist
;
1298 c
.lisp_eval_depth
= lisp_eval_depth
;
1299 c
.pdlcount
= SPECPDL_INDEX ();
1300 c
.poll_suppress_count
= poll_suppress_count
;
1301 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1302 c
.gcpro
= gcprolist
;
1303 c
.byte_stack
= byte_stack_list
;
1304 if (_setjmp (c
.jmp
))
1306 return (*hfun
) (c
.val
);
1310 h
.handler
= handlers
;
1312 h
.next
= handlerlist
;
1318 handlerlist
= h
.next
;
1322 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1325 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1326 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1334 c
.backlist
= backtrace_list
;
1335 c
.handlerlist
= handlerlist
;
1336 c
.lisp_eval_depth
= lisp_eval_depth
;
1337 c
.pdlcount
= SPECPDL_INDEX ();
1338 c
.poll_suppress_count
= poll_suppress_count
;
1339 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1340 c
.gcpro
= gcprolist
;
1341 c
.byte_stack
= byte_stack_list
;
1342 if (_setjmp (c
.jmp
))
1344 return (*hfun
) (c
.val
);
1348 h
.handler
= handlers
;
1350 h
.next
= handlerlist
;
1354 val
= (*bfun
) (arg
);
1356 handlerlist
= h
.next
;
1360 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1364 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1367 Lisp_Object handlers
,
1368 Lisp_Object (*hfun
) (Lisp_Object
))
1376 c
.backlist
= backtrace_list
;
1377 c
.handlerlist
= handlerlist
;
1378 c
.lisp_eval_depth
= lisp_eval_depth
;
1379 c
.pdlcount
= SPECPDL_INDEX ();
1380 c
.poll_suppress_count
= poll_suppress_count
;
1381 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1382 c
.gcpro
= gcprolist
;
1383 c
.byte_stack
= byte_stack_list
;
1384 if (_setjmp (c
.jmp
))
1386 return (*hfun
) (c
.val
);
1390 h
.handler
= handlers
;
1392 h
.next
= handlerlist
;
1396 val
= (*bfun
) (arg1
, arg2
);
1398 handlerlist
= h
.next
;
1402 /* Like internal_condition_case but call BFUN with NARGS as first,
1403 and ARGS as second argument. */
1406 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1409 Lisp_Object handlers
,
1410 Lisp_Object (*hfun
) (Lisp_Object err
,
1420 c
.backlist
= backtrace_list
;
1421 c
.handlerlist
= handlerlist
;
1422 c
.lisp_eval_depth
= lisp_eval_depth
;
1423 c
.pdlcount
= SPECPDL_INDEX ();
1424 c
.poll_suppress_count
= poll_suppress_count
;
1425 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1426 c
.gcpro
= gcprolist
;
1427 c
.byte_stack
= byte_stack_list
;
1428 if (_setjmp (c
.jmp
))
1430 return (*hfun
) (c
.val
, nargs
, args
);
1434 h
.handler
= handlers
;
1436 h
.next
= handlerlist
;
1440 val
= (*bfun
) (nargs
, args
);
1442 handlerlist
= h
.next
;
1447 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1448 static bool maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1452 process_quit_flag (void)
1454 Lisp_Object flag
= Vquit_flag
;
1456 if (EQ (flag
, Qkill_emacs
))
1458 if (EQ (Vthrow_on_input
, flag
))
1459 Fthrow (Vthrow_on_input
, Qt
);
1460 Fsignal (Qquit
, Qnil
);
1463 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1464 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1465 This function does not return.
1467 An error symbol is a symbol with an `error-conditions' property
1468 that is a list of condition names.
1469 A handler for any of those names will get to handle this signal.
1470 The symbol `error' should normally be one of them.
1472 DATA should be a list. Its elements are printed as part of the error message.
1473 See Info anchor `(elisp)Definition of signal' for some details on how this
1474 error message is constructed.
1475 If the signal is handled, DATA is made available to the handler.
1476 See also the function `condition-case'. */)
1477 (Lisp_Object error_symbol
, Lisp_Object data
)
1479 /* When memory is full, ERROR-SYMBOL is nil,
1480 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1481 That is a special case--don't do this in other situations. */
1482 Lisp_Object conditions
;
1484 Lisp_Object real_error_symbol
1485 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1486 register Lisp_Object clause
= Qnil
;
1488 struct backtrace
*bp
;
1490 immediate_quit
= handling_signal
= 0;
1492 if (gc_in_progress
|| waiting_for_input
)
1495 #if 0 /* rms: I don't know why this was here,
1496 but it is surely wrong for an error that is handled. */
1497 #ifdef HAVE_WINDOW_SYSTEM
1498 if (display_hourglass_p
)
1499 cancel_hourglass ();
1503 /* This hook is used by edebug. */
1504 if (! NILP (Vsignal_hook_function
)
1505 && ! NILP (error_symbol
))
1507 /* Edebug takes care of restoring these variables when it exits. */
1508 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1509 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1511 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1512 max_specpdl_size
= SPECPDL_INDEX () + 40;
1514 call2 (Vsignal_hook_function
, error_symbol
, data
);
1517 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1519 /* Remember from where signal was called. Skip over the frame for
1520 `signal' itself. If a frame for `error' follows, skip that,
1521 too. Don't do this when ERROR_SYMBOL is nil, because that
1522 is a memory-full error. */
1523 Vsignaling_function
= Qnil
;
1524 if (backtrace_list
&& !NILP (error_symbol
))
1526 bp
= backtrace_list
->next
;
1527 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1529 if (bp
&& bp
->function
)
1530 Vsignaling_function
= *bp
->function
;
1533 for (h
= handlerlist
; h
; h
= h
->next
)
1535 clause
= find_handler_clause (h
->handler
, conditions
);
1540 if (/* Don't run the debugger for a memory-full error.
1541 (There is no room in memory to do that!) */
1542 !NILP (error_symbol
)
1543 && (!NILP (Vdebug_on_signal
)
1544 /* If no handler is present now, try to run the debugger. */
1546 /* A `debug' symbol in the handler list disables the normal
1547 suppression of the debugger. */
1548 || (CONSP (clause
) && CONSP (XCAR (clause
))
1549 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1550 /* Special handler that means "print a message and run debugger
1552 || EQ (h
->handler
, Qerror
)))
1554 bool debugger_called
1555 = maybe_call_debugger (conditions
, error_symbol
, data
);
1556 /* We can't return values to code which signaled an error, but we
1557 can continue code which has signaled a quit. */
1558 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1564 Lisp_Object unwind_data
1565 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1567 h
->chosen_clause
= clause
;
1568 unwind_to_catch (h
->tag
, unwind_data
);
1573 Fthrow (Qtop_level
, Qt
);
1576 if (! NILP (error_symbol
))
1577 data
= Fcons (error_symbol
, data
);
1579 string
= Ferror_message_string (data
);
1580 fatal ("%s", SDATA (string
));
1583 /* Internal version of Fsignal that never returns.
1584 Used for anything but Qquit (which can return from Fsignal). */
1587 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1589 Fsignal (error_symbol
, data
);
1593 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1596 xsignal0 (Lisp_Object error_symbol
)
1598 xsignal (error_symbol
, Qnil
);
1602 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1604 xsignal (error_symbol
, list1 (arg
));
1608 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1610 xsignal (error_symbol
, list2 (arg1
, arg2
));
1614 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1616 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1619 /* Signal `error' with message S, and additional arg ARG.
1620 If ARG is not a genuine list, make it a one-element list. */
1623 signal_error (const char *s
, Lisp_Object arg
)
1625 Lisp_Object tortoise
, hare
;
1627 hare
= tortoise
= arg
;
1628 while (CONSP (hare
))
1635 tortoise
= XCDR (tortoise
);
1637 if (EQ (hare
, tortoise
))
1642 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1644 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1648 /* Return true if LIST is a non-nil atom or
1649 a list containing one of CONDITIONS. */
1652 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1659 while (CONSP (conditions
))
1661 Lisp_Object
this, tail
;
1662 this = XCAR (conditions
);
1663 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1664 if (EQ (XCAR (tail
), this))
1666 conditions
= XCDR (conditions
);
1671 /* Return true if an error with condition-symbols CONDITIONS,
1672 and described by SIGNAL-DATA, should skip the debugger
1673 according to debugger-ignored-errors. */
1676 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1679 bool first_string
= 1;
1680 Lisp_Object error_message
;
1682 error_message
= Qnil
;
1683 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1685 if (STRINGP (XCAR (tail
)))
1689 error_message
= Ferror_message_string (data
);
1693 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1698 Lisp_Object contail
;
1700 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1701 if (EQ (XCAR (tail
), XCAR (contail
)))
1709 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1710 SIG and DATA describe the signal. There are two ways to pass them:
1711 = SIG is the error symbol, and DATA is the rest of the data.
1712 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1713 This is for memory-full errors only. */
1715 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1717 Lisp_Object combined_data
;
1719 combined_data
= Fcons (sig
, data
);
1722 /* Don't try to run the debugger with interrupts blocked.
1723 The editing loop would return anyway. */
1725 && NILP (Vinhibit_debugger
)
1726 /* Does user want to enter debugger for this kind of error? */
1729 : wants_debugger (Vdebug_on_error
, conditions
))
1730 && ! skip_debugger (conditions
, combined_data
)
1731 /* RMS: What's this for? */
1732 && when_entered_debugger
< num_nonmacro_input_events
)
1734 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1742 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1744 register Lisp_Object h
;
1746 /* t is used by handlers for all conditions, set up by C code. */
1747 if (EQ (handlers
, Qt
))
1750 /* error is used similarly, but means print an error message
1751 and run the debugger if that is enabled. */
1752 if (EQ (handlers
, Qerror
))
1755 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1757 Lisp_Object handler
= XCAR (h
);
1758 Lisp_Object condit
, tem
;
1760 if (!CONSP (handler
))
1762 condit
= XCAR (handler
);
1763 /* Handle a single condition name in handler HANDLER. */
1764 if (SYMBOLP (condit
))
1766 tem
= Fmemq (Fcar (handler
), conditions
);
1770 /* Handle a list of condition names in handler HANDLER. */
1771 else if (CONSP (condit
))
1774 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1776 tem
= Fmemq (XCAR (tail
), conditions
);
1787 /* Dump an error message; called like vprintf. */
1789 verror (const char *m
, va_list ap
)
1792 ptrdiff_t size
= sizeof buf
;
1793 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1798 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1799 string
= make_string (buffer
, used
);
1803 xsignal1 (Qerror
, string
);
1807 /* Dump an error message; called like printf. */
1811 error (const char *m
, ...)
1819 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1820 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1821 This means it contains a description for how to read arguments to give it.
1822 The value is nil for an invalid function or a symbol with no function
1825 Interactively callable functions include strings and vectors (treated
1826 as keyboard macros), lambda-expressions that contain a top-level call
1827 to `interactive', autoload definitions made by `autoload' with non-nil
1828 fourth argument, and some of the built-in functions of Lisp.
1830 Also, a symbol satisfies `commandp' if its function definition does so.
1832 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1833 then strings and vectors are not accepted. */)
1834 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1836 register Lisp_Object fun
;
1837 register Lisp_Object funcar
;
1838 Lisp_Object if_prop
= Qnil
;
1842 fun
= indirect_function (fun
); /* Check cycles. */
1843 if (NILP (fun
) || EQ (fun
, Qunbound
))
1846 /* Check an `interactive-form' property if present, analogous to the
1847 function-documentation property. */
1849 while (SYMBOLP (fun
))
1851 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1854 fun
= Fsymbol_function (fun
);
1857 /* Emacs primitives are interactive if their DEFUN specifies an
1858 interactive spec. */
1860 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1862 /* Bytecode objects are interactive if they are long enough to
1863 have an element whose index is COMPILED_INTERACTIVE, which is
1864 where the interactive spec is stored. */
1865 else if (COMPILEDP (fun
))
1866 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1869 /* Strings and vectors are keyboard macros. */
1870 if (STRINGP (fun
) || VECTORP (fun
))
1871 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1873 /* Lists may represent commands. */
1876 funcar
= XCAR (fun
);
1877 if (EQ (funcar
, Qclosure
))
1878 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1880 else if (EQ (funcar
, Qlambda
))
1881 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1882 else if (EQ (funcar
, Qautoload
))
1883 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1888 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1889 doc
: /* Define FUNCTION to autoload from FILE.
1890 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1891 Third arg DOCSTRING is documentation for the function.
1892 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1893 Fifth arg TYPE indicates the type of the object:
1894 nil or omitted says FUNCTION is a function,
1895 `keymap' says FUNCTION is really a keymap, and
1896 `macro' or t says FUNCTION is really a macro.
1897 Third through fifth args give info about the real definition.
1898 They default to nil.
1899 If FUNCTION is already defined other than as an autoload,
1900 this does nothing and returns nil. */)
1901 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1903 CHECK_SYMBOL (function
);
1904 CHECK_STRING (file
);
1906 /* If function is defined and not as an autoload, don't override. */
1907 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1908 && !(CONSP (XSYMBOL (function
)->function
)
1909 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1912 if (NILP (Vpurify_flag
))
1913 /* Only add entries after dumping, because the ones before are
1914 not useful and else we get loads of them from the loaddefs.el. */
1915 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1916 else if (EQ (docstring
, make_number (0)))
1917 /* `read1' in lread.c has found the docstring starting with "\
1918 and assumed the docstring will be provided by Snarf-documentation, so it
1919 passed us 0 instead. But that leads to accidental sharing in purecopy's
1920 hash-consing, so we use a (hopefully) unique integer instead. */
1921 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1922 return Ffset (function
,
1923 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1924 interactive
, type
)));
1928 un_autoload (Lisp_Object oldqueue
)
1930 register Lisp_Object queue
, first
, second
;
1932 /* Queue to unwind is current value of Vautoload_queue.
1933 oldqueue is the shadowed value to leave in Vautoload_queue. */
1934 queue
= Vautoload_queue
;
1935 Vautoload_queue
= oldqueue
;
1936 while (CONSP (queue
))
1938 first
= XCAR (queue
);
1939 second
= Fcdr (first
);
1940 first
= Fcar (first
);
1941 if (EQ (first
, make_number (0)))
1944 Ffset (first
, second
);
1945 queue
= XCDR (queue
);
1950 /* Load an autoloaded function.
1951 FUNNAME is the symbol which is the function's name.
1952 FUNDEF is the autoload definition (a list). */
1954 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1955 doc
: /* Load FUNDEF which should be an autoload.
1956 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1957 in which case the function returns the new autoloaded function value.
1958 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1959 it is defines a macro. */)
1960 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1962 ptrdiff_t count
= SPECPDL_INDEX ();
1963 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1965 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1968 if (EQ (macro_only
, Qmacro
))
1970 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1971 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1975 /* This is to make sure that loadup.el gives a clear picture
1976 of what files are preloaded and when. */
1977 if (! NILP (Vpurify_flag
))
1978 error ("Attempt to autoload %s while preparing to dump",
1979 SDATA (SYMBOL_NAME (funname
)));
1981 CHECK_SYMBOL (funname
);
1982 GCPRO3 (funname
, fundef
, macro_only
);
1984 /* Preserve the match data. */
1985 record_unwind_save_match_data ();
1987 /* If autoloading gets an error (which includes the error of failing
1988 to define the function being called), we use Vautoload_queue
1989 to undo function definitions and `provide' calls made by
1990 the function. We do this in the specific case of autoloading
1991 because autoloading is not an explicit request "load this file",
1992 but rather a request to "call this function".
1994 The value saved here is to be restored into Vautoload_queue. */
1995 record_unwind_protect (un_autoload
, Vautoload_queue
);
1996 Vautoload_queue
= Qt
;
1997 /* If `macro_only', assume this autoload to be a "best-effort",
1998 so don't signal an error if autoloading fails. */
1999 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
2001 /* Once loading finishes, don't undo it. */
2002 Vautoload_queue
= Qt
;
2003 unbind_to (count
, Qnil
);
2011 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2013 if (!NILP (Fequal (fun
, fundef
)))
2014 error ("Autoloading failed to define function %s",
2015 SDATA (SYMBOL_NAME (funname
)));
2022 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2023 doc
: /* Evaluate FORM and return its value.
2024 If LEXICAL is t, evaluate using lexical scoping. */)
2025 (Lisp_Object form
, Lisp_Object lexical
)
2027 ptrdiff_t count
= SPECPDL_INDEX ();
2028 specbind (Qinternal_interpreter_environment
,
2029 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2030 return unbind_to (count
, eval_sub (form
));
2033 /* Eval a sub-expression of the current expression (i.e. in the same
2036 eval_sub (Lisp_Object form
)
2038 Lisp_Object fun
, val
, original_fun
, original_args
;
2040 struct backtrace backtrace
;
2041 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2043 if (handling_signal
)
2048 /* Look up its binding in the lexical environment.
2049 We do not pay attention to the declared_special flag here, since we
2050 already did that when let-binding the variable. */
2051 Lisp_Object lex_binding
2052 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2053 ? Fassq (form
, Vinternal_interpreter_environment
)
2055 if (CONSP (lex_binding
))
2056 return XCDR (lex_binding
);
2058 return Fsymbol_value (form
);
2067 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2069 if (max_lisp_eval_depth
< 100)
2070 max_lisp_eval_depth
= 100;
2071 if (lisp_eval_depth
> max_lisp_eval_depth
)
2072 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2075 original_fun
= XCAR (form
);
2076 original_args
= XCDR (form
);
2078 backtrace
.next
= backtrace_list
;
2079 backtrace_list
= &backtrace
;
2080 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2081 backtrace
.args
= &original_args
;
2082 backtrace
.nargs
= UNEVALLED
;
2083 backtrace
.debug_on_exit
= 0;
2085 if (debug_on_next_call
)
2086 do_debug_on_call (Qt
);
2088 /* At this point, only original_fun and original_args
2089 have values that will be used below. */
2092 /* Optimize for no indirection. */
2094 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2095 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2096 fun
= indirect_function (fun
);
2100 Lisp_Object numargs
;
2101 Lisp_Object argvals
[8];
2102 Lisp_Object args_left
;
2103 register int i
, maxargs
;
2105 args_left
= original_args
;
2106 numargs
= Flength (args_left
);
2110 if (XINT (numargs
) < XSUBR (fun
)->min_args
2111 || (XSUBR (fun
)->max_args
>= 0
2112 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2113 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2115 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2116 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2117 else if (XSUBR (fun
)->max_args
== MANY
)
2119 /* Pass a vector of evaluated arguments. */
2121 ptrdiff_t argnum
= 0;
2124 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2126 GCPRO3 (args_left
, fun
, fun
);
2130 while (!NILP (args_left
))
2132 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2133 args_left
= Fcdr (args_left
);
2134 gcpro3
.nvars
= argnum
;
2137 backtrace
.args
= vals
;
2138 backtrace
.nargs
= XINT (numargs
);
2140 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2146 GCPRO3 (args_left
, fun
, fun
);
2147 gcpro3
.var
= argvals
;
2150 maxargs
= XSUBR (fun
)->max_args
;
2151 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2153 argvals
[i
] = eval_sub (Fcar (args_left
));
2159 backtrace
.args
= argvals
;
2160 backtrace
.nargs
= XINT (numargs
);
2165 val
= (XSUBR (fun
)->function
.a0 ());
2168 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2171 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2174 val
= (XSUBR (fun
)->function
.a3
2175 (argvals
[0], argvals
[1], argvals
[2]));
2178 val
= (XSUBR (fun
)->function
.a4
2179 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2182 val
= (XSUBR (fun
)->function
.a5
2183 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2187 val
= (XSUBR (fun
)->function
.a6
2188 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2189 argvals
[4], argvals
[5]));
2192 val
= (XSUBR (fun
)->function
.a7
2193 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2194 argvals
[4], argvals
[5], argvals
[6]));
2198 val
= (XSUBR (fun
)->function
.a8
2199 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2200 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2204 /* Someone has created a subr that takes more arguments than
2205 is supported by this code. We need to either rewrite the
2206 subr to use a different argument protocol, or add more
2207 cases to this switch. */
2212 else if (COMPILEDP (fun
))
2213 val
= apply_lambda (fun
, original_args
);
2216 if (EQ (fun
, Qunbound
))
2217 xsignal1 (Qvoid_function
, original_fun
);
2219 xsignal1 (Qinvalid_function
, original_fun
);
2220 funcar
= XCAR (fun
);
2221 if (!SYMBOLP (funcar
))
2222 xsignal1 (Qinvalid_function
, original_fun
);
2223 if (EQ (funcar
, Qautoload
))
2225 Fautoload_do_load (fun
, original_fun
, Qnil
);
2228 if (EQ (funcar
, Qmacro
))
2230 ptrdiff_t count
= SPECPDL_INDEX ();
2232 /* Bind lexical-binding during expansion of the macro, so the
2233 macro can know reliably if the code it outputs will be
2234 interpreted using lexical-binding or not. */
2235 specbind (Qlexical_binding
,
2236 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2237 exp
= apply1 (Fcdr (fun
), original_args
);
2238 unbind_to (count
, Qnil
);
2239 val
= eval_sub (exp
);
2241 else if (EQ (funcar
, Qlambda
)
2242 || EQ (funcar
, Qclosure
))
2243 val
= apply_lambda (fun
, original_args
);
2245 xsignal1 (Qinvalid_function
, original_fun
);
2250 if (backtrace
.debug_on_exit
)
2251 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2252 backtrace_list
= backtrace
.next
;
2257 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2258 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2259 Then return the value FUNCTION returns.
2260 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2261 usage: (apply FUNCTION &rest ARGUMENTS) */)
2262 (ptrdiff_t nargs
, Lisp_Object
*args
)
2266 register Lisp_Object spread_arg
;
2267 register Lisp_Object
*funcall_args
;
2268 Lisp_Object fun
, retval
;
2269 struct gcpro gcpro1
;
2274 spread_arg
= args
[nargs
- 1];
2275 CHECK_LIST (spread_arg
);
2277 numargs
= XINT (Flength (spread_arg
));
2280 return Ffuncall (nargs
- 1, args
);
2281 else if (numargs
== 1)
2283 args
[nargs
- 1] = XCAR (spread_arg
);
2284 return Ffuncall (nargs
, args
);
2287 numargs
+= nargs
- 2;
2289 /* Optimize for no indirection. */
2290 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2291 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2292 fun
= indirect_function (fun
);
2293 if (EQ (fun
, Qunbound
))
2295 /* Let funcall get the error. */
2302 if (numargs
< XSUBR (fun
)->min_args
2303 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2304 goto funcall
; /* Let funcall get the error. */
2305 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2307 /* Avoid making funcall cons up a yet another new vector of arguments
2308 by explicitly supplying nil's for optional values. */
2309 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2310 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2311 funcall_args
[++i
] = Qnil
;
2312 GCPRO1 (*funcall_args
);
2313 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2317 /* We add 1 to numargs because funcall_args includes the
2318 function itself as well as its arguments. */
2321 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2322 GCPRO1 (*funcall_args
);
2323 gcpro1
.nvars
= 1 + numargs
;
2326 memcpy (funcall_args
, args
, nargs
* word_size
);
2327 /* Spread the last arg we got. Its first element goes in
2328 the slot that it used to occupy, hence this value of I. */
2330 while (!NILP (spread_arg
))
2332 funcall_args
[i
++] = XCAR (spread_arg
);
2333 spread_arg
= XCDR (spread_arg
);
2336 /* By convention, the caller needs to gcpro Ffuncall's args. */
2337 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2344 /* Run hook variables in various ways. */
2347 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2349 Ffuncall (nargs
, args
);
2353 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2354 doc
: /* Run each hook in HOOKS.
2355 Each argument should be a symbol, a hook variable.
2356 These symbols are processed in the order specified.
2357 If a hook symbol has a non-nil value, that value may be a function
2358 or a list of functions to be called to run the hook.
2359 If the value is a function, it is called with no arguments.
2360 If it is a list, the elements are called, in order, with no arguments.
2362 Major modes should not use this function directly to run their mode
2363 hook; they should use `run-mode-hooks' instead.
2365 Do not use `make-local-variable' to make a hook variable buffer-local.
2366 Instead, use `add-hook' and specify t for the LOCAL argument.
2367 usage: (run-hooks &rest HOOKS) */)
2368 (ptrdiff_t nargs
, Lisp_Object
*args
)
2370 Lisp_Object hook
[1];
2373 for (i
= 0; i
< nargs
; i
++)
2376 run_hook_with_args (1, hook
, funcall_nil
);
2382 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2383 Srun_hook_with_args
, 1, MANY
, 0,
2384 doc
: /* Run HOOK with the specified arguments ARGS.
2385 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2386 value, that value may be a function or a list of functions to be
2387 called to run the hook. If the value is a function, it is called with
2388 the given arguments and its return value is returned. If it is a list
2389 of functions, those functions are called, in order,
2390 with the given arguments ARGS.
2391 It is best not to depend on the value returned by `run-hook-with-args',
2394 Do not use `make-local-variable' to make a hook variable buffer-local.
2395 Instead, use `add-hook' and specify t for the LOCAL argument.
2396 usage: (run-hook-with-args HOOK &rest ARGS) */)
2397 (ptrdiff_t nargs
, Lisp_Object
*args
)
2399 return run_hook_with_args (nargs
, args
, funcall_nil
);
2402 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2403 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2404 doc
: /* Run HOOK with the specified arguments ARGS.
2405 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2406 value, that value may be a function or a list of functions to be
2407 called to run the hook. If the value is a function, it is called with
2408 the given arguments and its return value is returned.
2409 If it is a list of functions, those functions are called, in order,
2410 with the given arguments ARGS, until one of them
2411 returns a non-nil value. Then we return that value.
2412 However, if they all return nil, we return nil.
2414 Do not use `make-local-variable' to make a hook variable buffer-local.
2415 Instead, use `add-hook' and specify t for the LOCAL argument.
2416 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2417 (ptrdiff_t nargs
, Lisp_Object
*args
)
2419 return run_hook_with_args (nargs
, args
, Ffuncall
);
2423 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2425 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2428 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2429 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2430 doc
: /* Run HOOK with the specified arguments ARGS.
2431 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2432 value, that value may be a function or a list of functions to be
2433 called to run the hook. If the value is a function, it is called with
2434 the given arguments and its return value is returned.
2435 If it is a list of functions, those functions are called, in order,
2436 with the given arguments ARGS, until one of them returns nil.
2437 Then we return nil. However, if they all return non-nil, we return non-nil.
2439 Do not use `make-local-variable' to make a hook variable buffer-local.
2440 Instead, use `add-hook' and specify t for the LOCAL argument.
2441 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2442 (ptrdiff_t nargs
, Lisp_Object
*args
)
2444 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2448 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2450 Lisp_Object tmp
= args
[0], ret
;
2453 ret
= Ffuncall (nargs
, args
);
2459 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2460 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2461 I.e. instead of calling each function FUN directly with arguments ARGS,
2462 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2463 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2464 aborts and returns that value.
2465 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2466 (ptrdiff_t nargs
, Lisp_Object
*args
)
2468 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2471 /* ARGS[0] should be a hook symbol.
2472 Call each of the functions in the hook value, passing each of them
2473 as arguments all the rest of ARGS (all NARGS - 1 elements).
2474 FUNCALL specifies how to call each function on the hook.
2475 The caller (or its caller, etc) must gcpro all of ARGS,
2476 except that it isn't necessary to gcpro ARGS[0]. */
2479 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2480 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2482 Lisp_Object sym
, val
, ret
= Qnil
;
2483 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2485 /* If we are dying or still initializing,
2486 don't do anything--it would probably crash if we tried. */
2487 if (NILP (Vrun_hooks
))
2491 val
= find_symbol_value (sym
);
2493 if (EQ (val
, Qunbound
) || NILP (val
))
2495 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2498 return funcall (nargs
, args
);
2502 Lisp_Object global_vals
= Qnil
;
2503 GCPRO3 (sym
, val
, global_vals
);
2506 CONSP (val
) && NILP (ret
);
2509 if (EQ (XCAR (val
), Qt
))
2511 /* t indicates this hook has a local binding;
2512 it means to run the global binding too. */
2513 global_vals
= Fdefault_value (sym
);
2514 if (NILP (global_vals
)) continue;
2516 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2518 args
[0] = global_vals
;
2519 ret
= funcall (nargs
, args
);
2524 CONSP (global_vals
) && NILP (ret
);
2525 global_vals
= XCDR (global_vals
))
2527 args
[0] = XCAR (global_vals
);
2528 /* In a global value, t should not occur. If it does, we
2529 must ignore it to avoid an endless loop. */
2530 if (!EQ (args
[0], Qt
))
2531 ret
= funcall (nargs
, args
);
2537 args
[0] = XCAR (val
);
2538 ret
= funcall (nargs
, args
);
2547 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2550 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2552 Lisp_Object temp
[3];
2557 Frun_hook_with_args (3, temp
);
2560 /* Apply fn to arg. */
2562 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2564 struct gcpro gcpro1
;
2568 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2571 Lisp_Object args
[2];
2575 RETURN_UNGCPRO (Fapply (2, args
));
2579 /* Call function fn on no arguments. */
2581 call0 (Lisp_Object fn
)
2583 struct gcpro gcpro1
;
2586 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2589 /* Call function fn with 1 argument arg1. */
2592 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2594 struct gcpro gcpro1
;
2595 Lisp_Object args
[2];
2601 RETURN_UNGCPRO (Ffuncall (2, args
));
2604 /* Call function fn with 2 arguments arg1, arg2. */
2607 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2609 struct gcpro gcpro1
;
2610 Lisp_Object args
[3];
2616 RETURN_UNGCPRO (Ffuncall (3, args
));
2619 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2622 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2624 struct gcpro gcpro1
;
2625 Lisp_Object args
[4];
2632 RETURN_UNGCPRO (Ffuncall (4, args
));
2635 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2638 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2641 struct gcpro gcpro1
;
2642 Lisp_Object args
[5];
2650 RETURN_UNGCPRO (Ffuncall (5, args
));
2653 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2656 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2657 Lisp_Object arg4
, Lisp_Object arg5
)
2659 struct gcpro gcpro1
;
2660 Lisp_Object args
[6];
2669 RETURN_UNGCPRO (Ffuncall (6, args
));
2672 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2675 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2676 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2678 struct gcpro gcpro1
;
2679 Lisp_Object args
[7];
2689 RETURN_UNGCPRO (Ffuncall (7, args
));
2692 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2695 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2696 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2698 struct gcpro gcpro1
;
2699 Lisp_Object args
[8];
2710 RETURN_UNGCPRO (Ffuncall (8, args
));
2713 /* The caller should GCPRO all the elements of ARGS. */
2715 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2716 doc
: /* Non-nil if OBJECT is a function. */)
2717 (Lisp_Object object
)
2719 if (FUNCTIONP (object
))
2724 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2725 doc
: /* Call first argument as a function, passing remaining arguments to it.
2726 Return the value that function returns.
2727 Thus, (funcall 'cons 'x 'y) returns (x . y).
2728 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2729 (ptrdiff_t nargs
, Lisp_Object
*args
)
2731 Lisp_Object fun
, original_fun
;
2733 ptrdiff_t numargs
= nargs
- 1;
2734 Lisp_Object lisp_numargs
;
2736 struct backtrace backtrace
;
2737 register Lisp_Object
*internal_args
;
2742 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2744 if (max_lisp_eval_depth
< 100)
2745 max_lisp_eval_depth
= 100;
2746 if (lisp_eval_depth
> max_lisp_eval_depth
)
2747 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2750 backtrace
.next
= backtrace_list
;
2751 backtrace_list
= &backtrace
;
2752 backtrace
.function
= &args
[0];
2753 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2754 backtrace
.nargs
= nargs
- 1;
2755 backtrace
.debug_on_exit
= 0;
2757 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2760 if (debug_on_next_call
)
2761 do_debug_on_call (Qlambda
);
2765 original_fun
= args
[0];
2769 /* Optimize for no indirection. */
2771 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2772 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2773 fun
= indirect_function (fun
);
2777 if (numargs
< XSUBR (fun
)->min_args
2778 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2780 XSETFASTINT (lisp_numargs
, numargs
);
2781 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2784 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2785 xsignal1 (Qinvalid_function
, original_fun
);
2787 else if (XSUBR (fun
)->max_args
== MANY
)
2788 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2791 if (XSUBR (fun
)->max_args
> numargs
)
2793 internal_args
= alloca (XSUBR (fun
)->max_args
2794 * sizeof *internal_args
);
2795 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2796 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2797 internal_args
[i
] = Qnil
;
2800 internal_args
= args
+ 1;
2801 switch (XSUBR (fun
)->max_args
)
2804 val
= (XSUBR (fun
)->function
.a0 ());
2807 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2810 val
= (XSUBR (fun
)->function
.a2
2811 (internal_args
[0], internal_args
[1]));
2814 val
= (XSUBR (fun
)->function
.a3
2815 (internal_args
[0], internal_args
[1], internal_args
[2]));
2818 val
= (XSUBR (fun
)->function
.a4
2819 (internal_args
[0], internal_args
[1], internal_args
[2],
2823 val
= (XSUBR (fun
)->function
.a5
2824 (internal_args
[0], internal_args
[1], internal_args
[2],
2825 internal_args
[3], internal_args
[4]));
2828 val
= (XSUBR (fun
)->function
.a6
2829 (internal_args
[0], internal_args
[1], internal_args
[2],
2830 internal_args
[3], internal_args
[4], internal_args
[5]));
2833 val
= (XSUBR (fun
)->function
.a7
2834 (internal_args
[0], internal_args
[1], internal_args
[2],
2835 internal_args
[3], internal_args
[4], internal_args
[5],
2840 val
= (XSUBR (fun
)->function
.a8
2841 (internal_args
[0], internal_args
[1], internal_args
[2],
2842 internal_args
[3], internal_args
[4], internal_args
[5],
2843 internal_args
[6], internal_args
[7]));
2848 /* If a subr takes more than 8 arguments without using MANY
2849 or UNEVALLED, we need to extend this function to support it.
2850 Until this is done, there is no way to call the function. */
2855 else if (COMPILEDP (fun
))
2856 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2859 if (EQ (fun
, Qunbound
))
2860 xsignal1 (Qvoid_function
, original_fun
);
2862 xsignal1 (Qinvalid_function
, original_fun
);
2863 funcar
= XCAR (fun
);
2864 if (!SYMBOLP (funcar
))
2865 xsignal1 (Qinvalid_function
, original_fun
);
2866 if (EQ (funcar
, Qlambda
)
2867 || EQ (funcar
, Qclosure
))
2868 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2869 else if (EQ (funcar
, Qautoload
))
2871 Fautoload_do_load (fun
, original_fun
, Qnil
);
2876 xsignal1 (Qinvalid_function
, original_fun
);
2880 if (backtrace
.debug_on_exit
)
2881 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2882 backtrace_list
= backtrace
.next
;
2887 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2889 Lisp_Object args_left
;
2892 register Lisp_Object
*arg_vector
;
2893 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2894 register Lisp_Object tem
;
2897 numargs
= XFASTINT (Flength (args
));
2898 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2901 GCPRO3 (*arg_vector
, args_left
, fun
);
2904 for (i
= 0; i
< numargs
; )
2906 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2907 tem
= eval_sub (tem
);
2908 arg_vector
[i
++] = tem
;
2914 backtrace_list
->args
= arg_vector
;
2915 backtrace_list
->nargs
= i
;
2916 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2918 /* Do the debug-on-exit now, while arg_vector still exists. */
2919 if (backtrace_list
->debug_on_exit
)
2920 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2921 /* Don't do it again when we return to eval. */
2922 backtrace_list
->debug_on_exit
= 0;
2927 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2928 and return the result of evaluation.
2929 FUN must be either a lambda-expression or a compiled-code object. */
2932 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2933 register Lisp_Object
*arg_vector
)
2935 Lisp_Object val
, syms_left
, next
, lexenv
;
2936 ptrdiff_t count
= SPECPDL_INDEX ();
2938 bool optional
, rest
;
2942 if (EQ (XCAR (fun
), Qclosure
))
2944 fun
= XCDR (fun
); /* Drop `closure'. */
2945 lexenv
= XCAR (fun
);
2946 CHECK_LIST_CONS (fun
, fun
);
2950 syms_left
= XCDR (fun
);
2951 if (CONSP (syms_left
))
2952 syms_left
= XCAR (syms_left
);
2954 xsignal1 (Qinvalid_function
, fun
);
2956 else if (COMPILEDP (fun
))
2958 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2959 if (INTEGERP (syms_left
))
2960 /* A byte-code object with a non-nil `push args' slot means we
2961 shouldn't bind any arguments, instead just call the byte-code
2962 interpreter directly; it will push arguments as necessary.
2964 Byte-code objects with either a non-existent, or a nil value for
2965 the `push args' slot (the default), have dynamically-bound
2966 arguments, and use the argument-binding code below instead (as do
2967 all interpreted functions, even lexically bound ones). */
2969 /* If we have not actually read the bytecode string
2970 and constants vector yet, fetch them from the file. */
2971 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2972 Ffetch_bytecode (fun
);
2973 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2974 AREF (fun
, COMPILED_CONSTANTS
),
2975 AREF (fun
, COMPILED_STACK_DEPTH
),
2984 i
= optional
= rest
= 0;
2985 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2989 next
= XCAR (syms_left
);
2990 if (!SYMBOLP (next
))
2991 xsignal1 (Qinvalid_function
, fun
);
2993 if (EQ (next
, Qand_rest
))
2995 else if (EQ (next
, Qand_optional
))
3002 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3006 arg
= arg_vector
[i
++];
3008 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3012 /* Bind the argument. */
3013 if (!NILP (lexenv
) && SYMBOLP (next
))
3014 /* Lexically bind NEXT by adding it to the lexenv alist. */
3015 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3017 /* Dynamically bind NEXT. */
3018 specbind (next
, arg
);
3022 if (!NILP (syms_left
))
3023 xsignal1 (Qinvalid_function
, fun
);
3025 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3027 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3028 /* Instantiate a new lexical environment. */
3029 specbind (Qinternal_interpreter_environment
, lexenv
);
3032 val
= Fprogn (XCDR (XCDR (fun
)));
3035 /* If we have not actually read the bytecode string
3036 and constants vector yet, fetch them from the file. */
3037 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3038 Ffetch_bytecode (fun
);
3039 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3040 AREF (fun
, COMPILED_CONSTANTS
),
3041 AREF (fun
, COMPILED_STACK_DEPTH
),
3045 return unbind_to (count
, val
);
3048 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3050 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3051 (Lisp_Object object
)
3055 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3057 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3060 tem
= AREF (object
, COMPILED_BYTECODE
);
3061 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3062 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3064 error ("Invalid byte code");
3066 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3067 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3075 register ptrdiff_t count
= SPECPDL_INDEX ();
3076 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3077 if (max_size
<= specpdl_size
)
3079 if (max_specpdl_size
< 400)
3080 max_size
= max_specpdl_size
= 400;
3081 if (max_size
<= specpdl_size
)
3082 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3084 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3085 specpdl_ptr
= specpdl
+ count
;
3088 /* `specpdl_ptr->symbol' is a field which describes which variable is
3089 let-bound, so it can be properly undone when we unbind_to.
3090 It can have the following two shapes:
3091 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3092 a symbol that is not buffer-local (at least at the time
3093 the let binding started). Note also that it should not be
3094 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3096 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3097 variable SYMBOL which can be buffer-local. WHERE tells us
3098 which buffer is affected (or nil if the let-binding affects the
3099 global value of the variable) and BUFFER tells us which buffer was
3100 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3101 BUFFER did not yet have a buffer-local value). */
3104 specbind (Lisp_Object symbol
, Lisp_Object value
)
3106 struct Lisp_Symbol
*sym
;
3108 eassert (!handling_signal
);
3110 CHECK_SYMBOL (symbol
);
3111 sym
= XSYMBOL (symbol
);
3112 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3116 switch (sym
->redirect
)
3118 case SYMBOL_VARALIAS
:
3119 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3120 case SYMBOL_PLAINVAL
:
3121 /* The most common case is that of a non-constant symbol with a
3122 trivial value. Make that as fast as we can. */
3123 set_specpdl_symbol (symbol
);
3124 set_specpdl_old_value (SYMBOL_VAL (sym
));
3125 specpdl_ptr
->func
= NULL
;
3128 SET_SYMBOL_VAL (sym
, value
);
3130 set_internal (symbol
, value
, Qnil
, 1);
3132 case SYMBOL_LOCALIZED
:
3133 if (SYMBOL_BLV (sym
)->frame_local
)
3134 error ("Frame-local vars cannot be let-bound");
3135 case SYMBOL_FORWARDED
:
3137 Lisp_Object ovalue
= find_symbol_value (symbol
);
3138 specpdl_ptr
->func
= 0;
3139 set_specpdl_old_value (ovalue
);
3141 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3142 || (EQ (SYMBOL_BLV (sym
)->where
,
3143 SYMBOL_BLV (sym
)->frame_local
?
3144 Fselected_frame () : Fcurrent_buffer ())));
3146 if (sym
->redirect
== SYMBOL_LOCALIZED
3147 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3149 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3151 /* For a local variable, record both the symbol and which
3152 buffer's or frame's value we are saving. */
3153 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3155 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3156 || (blv_found (SYMBOL_BLV (sym
))
3157 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3160 else if (sym
->redirect
== SYMBOL_LOCALIZED
3161 && blv_found (SYMBOL_BLV (sym
)))
3162 where
= SYMBOL_BLV (sym
)->where
;
3166 /* We're not using the `unused' slot in the specbinding
3167 structure because this would mean we have to do more
3168 work for simple variables. */
3169 /* FIXME: The third value `current_buffer' is only used in
3170 let_shadows_buffer_binding_p which is itself only used
3171 in set_internal for local_if_set. */
3172 eassert (NILP (where
) || EQ (where
, cur_buf
));
3173 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3175 /* If SYMBOL is a per-buffer variable which doesn't have a
3176 buffer-local value here, make the `let' change the global
3177 value by changing the value of SYMBOL in all buffers not
3178 having their own value. This is consistent with what
3179 happens with other buffer-local variables. */
3181 && sym
->redirect
== SYMBOL_FORWARDED
)
3183 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3185 Fset_default (symbol
, value
);
3190 set_specpdl_symbol (symbol
);
3193 set_internal (symbol
, value
, Qnil
, 1);
3196 default: emacs_abort ();
3201 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3203 eassert (!handling_signal
);
3205 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3207 specpdl_ptr
->func
= function
;
3208 set_specpdl_symbol (Qnil
);
3209 set_specpdl_old_value (arg
);
3214 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3216 Lisp_Object quitf
= Vquit_flag
;
3217 struct gcpro gcpro1
, gcpro2
;
3219 GCPRO2 (value
, quitf
);
3222 while (specpdl_ptr
!= specpdl
+ count
)
3224 /* Copy the binding, and decrement specpdl_ptr, before we do
3225 the work to unbind it. We decrement first
3226 so that an error in unbinding won't try to unbind
3227 the same entry again, and we copy the binding first
3228 in case more bindings are made during some of the code we run. */
3230 struct specbinding this_binding
;
3231 this_binding
= *--specpdl_ptr
;
3233 if (this_binding
.func
!= 0)
3234 (*this_binding
.func
) (this_binding
.old_value
);
3235 /* If the symbol is a list, it is really (SYMBOL WHERE
3236 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3237 frame. If WHERE is a buffer or frame, this indicates we
3238 bound a variable that had a buffer-local or frame-local
3239 binding. WHERE nil means that the variable had the default
3240 value when it was bound. CURRENT-BUFFER is the buffer that
3241 was current when the variable was bound. */
3242 else if (CONSP (this_binding
.symbol
))
3244 Lisp_Object symbol
, where
;
3246 symbol
= XCAR (this_binding
.symbol
);
3247 where
= XCAR (XCDR (this_binding
.symbol
));
3250 Fset_default (symbol
, this_binding
.old_value
);
3251 /* If `where' is non-nil, reset the value in the appropriate
3252 local binding, but only if that binding still exists. */
3253 else if (BUFFERP (where
)
3254 ? !NILP (Flocal_variable_p (symbol
, where
))
3255 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3256 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3258 /* If variable has a trivial value (no forwarding), we can
3259 just set it. No need to check for constant symbols here,
3260 since that was already done by specbind. */
3261 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3262 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3263 this_binding
.old_value
);
3265 /* NOTE: we only ever come here if make_local_foo was used for
3266 the first time on this var within this let. */
3267 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3270 if (NILP (Vquit_flag
) && !NILP (quitf
))
3277 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3278 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3279 A special variable is one that will be bound dynamically, even in a
3280 context where binding is lexical by default. */)
3281 (Lisp_Object symbol
)
3283 CHECK_SYMBOL (symbol
);
3284 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3288 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3289 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3290 The debugger is entered when that frame exits, if the flag is non-nil. */)
3291 (Lisp_Object level
, Lisp_Object flag
)
3293 register struct backtrace
*backlist
= backtrace_list
;
3294 register EMACS_INT i
;
3296 CHECK_NUMBER (level
);
3298 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3300 backlist
= backlist
->next
;
3304 backlist
->debug_on_exit
= !NILP (flag
);
3309 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3310 doc
: /* Print a trace of Lisp function calls currently active.
3311 Output stream used is value of `standard-output'. */)
3314 register struct backtrace
*backlist
= backtrace_list
;
3317 struct gcpro gcpro1
;
3318 Lisp_Object old_print_level
= Vprint_level
;
3320 if (NILP (Vprint_level
))
3321 XSETFASTINT (Vprint_level
, 8);
3328 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3329 if (backlist
->nargs
== UNEVALLED
)
3331 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3332 write_string ("\n", -1);
3336 tem
= *backlist
->function
;
3337 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3338 write_string ("(", -1);
3339 if (backlist
->nargs
== MANY
)
3340 { /* FIXME: Can this happen? */
3342 for (tail
= *backlist
->args
; !NILP (tail
); tail
= Fcdr (tail
))
3345 write_string (" ", -1);
3346 Fprin1 (Fcar (tail
), Qnil
);
3353 for (i
= 0; i
< backlist
->nargs
; i
++)
3355 if (i
) write_string (" ", -1);
3356 Fprin1 (backlist
->args
[i
], Qnil
);
3359 write_string (")\n", -1);
3361 backlist
= backlist
->next
;
3364 Vprint_level
= old_print_level
;
3369 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3370 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3371 If that frame has not evaluated the arguments yet (or is a special form),
3372 the value is (nil FUNCTION ARG-FORMS...).
3373 If that frame has evaluated its arguments and called its function already,
3374 the value is (t FUNCTION ARG-VALUES...).
3375 A &rest arg is represented as the tail of the list ARG-VALUES.
3376 FUNCTION is whatever was supplied as car of evaluated list,
3377 or a lambda expression for macro calls.
3378 If NFRAMES is more than the number of frames, the value is nil. */)
3379 (Lisp_Object nframes
)
3381 register struct backtrace
*backlist
= backtrace_list
;
3382 register EMACS_INT i
;
3385 CHECK_NATNUM (nframes
);
3387 /* Find the frame requested. */
3388 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3389 backlist
= backlist
->next
;
3393 if (backlist
->nargs
== UNEVALLED
)
3394 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3397 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3398 tem
= *backlist
->args
;
3400 tem
= Flist (backlist
->nargs
, backlist
->args
);
3402 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3409 mark_backtrace (void)
3411 register struct backtrace
*backlist
;
3414 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3416 mark_object (*backlist
->function
);
3418 if (backlist
->nargs
== UNEVALLED
3419 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3422 i
= backlist
->nargs
;
3424 mark_object (backlist
->args
[i
]);
3432 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3433 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3434 If Lisp code tries to increase the total number past this amount,
3435 an error is signaled.
3436 You can safely use a value considerably larger than the default value,
3437 if that proves inconveniently small. However, if you increase it too far,
3438 Emacs could run out of memory trying to make the stack bigger. */);
3440 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3441 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3443 This limit serves to catch infinite recursions for you before they cause
3444 actual stack overflow in C, which would be fatal for Emacs.
3445 You can safely make it considerably larger than its default value,
3446 if that proves inconveniently small. However, if you increase it too far,
3447 Emacs could overflow the real C stack, and crash. */);
3449 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3450 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3451 If the value is t, that means do an ordinary quit.
3452 If the value equals `throw-on-input', that means quit by throwing
3453 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3454 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3455 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3458 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3459 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3460 Note that `quit-flag' will still be set by typing C-g,
3461 so a quit will be signaled as soon as `inhibit-quit' is nil.
3462 To prevent this happening, set `quit-flag' to nil
3463 before making `inhibit-quit' nil. */);
3464 Vinhibit_quit
= Qnil
;
3466 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3467 DEFSYM (Qautoload
, "autoload");
3468 DEFSYM (Qinhibit_debugger
, "inhibit-debugger");
3469 DEFSYM (Qmacro
, "macro");
3470 DEFSYM (Qdeclare
, "declare");
3472 /* Note that the process handling also uses Qexit, but we don't want
3473 to staticpro it twice, so we just do it here. */
3474 DEFSYM (Qexit
, "exit");
3476 DEFSYM (Qinteractive
, "interactive");
3477 DEFSYM (Qcommandp
, "commandp");
3478 DEFSYM (Qand_rest
, "&rest");
3479 DEFSYM (Qand_optional
, "&optional");
3480 DEFSYM (Qclosure
, "closure");
3481 DEFSYM (Qdebug
, "debug");
3483 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger
,
3484 doc
: /* Non-nil means never enter the debugger.
3485 Normally set while the debugger is already active, to avoid recursive
3487 Vinhibit_debugger
= Qnil
;
3489 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3490 doc
: /* Non-nil means enter debugger if an error is signaled.
3491 Does not apply to errors handled by `condition-case' or those
3492 matched by `debug-ignored-errors'.
3493 If the value is a list, an error only means to enter the debugger
3494 if one of its condition symbols appears in the list.
3495 When you evaluate an expression interactively, this variable
3496 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3497 The command `toggle-debug-on-error' toggles this.
3498 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3499 Vdebug_on_error
= Qnil
;
3501 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3502 doc
: /* List of errors for which the debugger should not be called.
3503 Each element may be a condition-name or a regexp that matches error messages.
3504 If any element applies to a given error, that error skips the debugger
3505 and just returns to top level.
3506 This overrides the variable `debug-on-error'.
3507 It does not apply to errors handled by `condition-case'. */);
3508 Vdebug_ignored_errors
= Qnil
;
3510 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3511 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3512 Does not apply if quit is handled by a `condition-case'. */);
3515 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3516 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3518 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3519 doc
: /* Non-nil means debugger may continue execution.
3520 This is nil when the debugger is called under circumstances where it
3521 might not be safe to continue. */);
3522 debugger_may_continue
= 1;
3524 DEFVAR_LISP ("debugger", Vdebugger
,
3525 doc
: /* Function to call to invoke debugger.
3526 If due to frame exit, args are `exit' and the value being returned;
3527 this function's value will be returned instead of that.
3528 If due to error, args are `error' and a list of the args to `signal'.
3529 If due to `apply' or `funcall' entry, one arg, `lambda'.
3530 If due to `eval' entry, one arg, t. */);
3533 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3534 doc
: /* If non-nil, this is a function for `signal' to call.
3535 It receives the same arguments that `signal' was given.
3536 The Edebug package uses this to regain control. */);
3537 Vsignal_hook_function
= Qnil
;
3539 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3540 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3541 Note that `debug-on-error', `debug-on-quit' and friends
3542 still determine whether to handle the particular condition. */);
3543 Vdebug_on_signal
= Qnil
;
3545 /* When lexical binding is being used,
3546 Vinternal_interpreter_environment is non-nil, and contains an alist
3547 of lexically-bound variable, or (t), indicating an empty
3548 environment. The lisp name of this variable would be
3549 `internal-interpreter-environment' if it weren't hidden.
3550 Every element of this list can be either a cons (VAR . VAL)
3551 specifying a lexical binding, or a single symbol VAR indicating
3552 that this variable should use dynamic scoping. */
3553 DEFSYM (Qinternal_interpreter_environment
,
3554 "internal-interpreter-environment");
3555 DEFVAR_LISP ("internal-interpreter-environment",
3556 Vinternal_interpreter_environment
,
3557 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3558 When lexical binding is not being used, this variable is nil.
3559 A value of `(t)' indicates an empty environment, otherwise it is an
3560 alist of active lexical bindings. */);
3561 Vinternal_interpreter_environment
= Qnil
;
3562 /* Don't export this variable to Elisp, so no one can mess with it
3563 (Just imagine if someone makes it buffer-local). */
3564 Funintern (Qinternal_interpreter_environment
, Qnil
);
3566 DEFSYM (Vrun_hooks
, "run-hooks");
3568 staticpro (&Vautoload_queue
);
3569 Vautoload_queue
= Qnil
;
3570 staticpro (&Vsignaling_function
);
3571 Vsignaling_function
= Qnil
;
3573 inhibit_lisp_code
= Qnil
;
3584 defsubr (&Sfunction
);
3586 defsubr (&Sdefvaralias
);
3587 defsubr (&Sdefconst
);
3588 defsubr (&Smake_var_non_special
);
3592 defsubr (&Smacroexpand
);
3595 defsubr (&Sunwind_protect
);
3596 defsubr (&Scondition_case
);
3598 defsubr (&Sinteractive_p
);
3599 defsubr (&Scalled_interactively_p
);
3600 defsubr (&Scommandp
);
3601 defsubr (&Sautoload
);
3602 defsubr (&Sautoload_do_load
);
3605 defsubr (&Sfuncall
);
3606 defsubr (&Srun_hooks
);
3607 defsubr (&Srun_hook_with_args
);
3608 defsubr (&Srun_hook_with_args_until_success
);
3609 defsubr (&Srun_hook_with_args_until_failure
);
3610 defsubr (&Srun_hook_wrapped
);
3611 defsubr (&Sfetch_bytecode
);
3612 defsubr (&Sbacktrace_debug
);
3613 defsubr (&Sbacktrace
);
3614 defsubr (&Sbacktrace_frame
);
3615 defsubr (&Sspecial_variable_p
);
3616 defsubr (&Sfunctionp
);