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 Qdebug_on_error
;
73 static Lisp_Object Qdeclare
;
74 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
76 static Lisp_Object Qdebug
;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
82 Lisp_Object Vrun_hooks
;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue
;
91 /* Current number of specbindings allocated in specpdl. */
93 ptrdiff_t specpdl_size
;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding
*specpdl
;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding
*specpdl_ptr
;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth
;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
114 static EMACS_INT when_entered_debugger
;
116 /* The function from which the last `signal' was called. Set in
119 Lisp_Object Vsignaling_function
;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
127 /* 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 int interactive_p (int);
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 int 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 (Qdebug_on_error
, Qnil
);
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 (1) ? 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")))
554 && interactive_p (1)) ? Qt
: Qnil
;
558 /* Return 1 if function in which this appears was called using
561 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
562 called is a built-in. */
565 interactive_p (int exclude_subrs_p
)
567 struct backtrace
*btp
;
570 btp
= backtrace_list
;
572 /* If this isn't a byte-compiled function, there may be a frame at
573 the top for Finteractive_p. If so, skip it. */
574 fun
= Findirect_function (*btp
->function
, Qnil
);
575 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
576 || XSUBR (fun
) == &Scalled_interactively_p
))
579 /* If we're running an Emacs 18-style byte-compiled function, there
580 may be a frame for Fbytecode at the top level. In any version of
581 Emacs there can be Fbytecode frames for subexpressions evaluated
582 inside catch and condition-case. Skip past them.
584 If this isn't a byte-compiled function, then we may now be
585 looking at several frames for special forms. Skip past them. */
587 && (EQ (*btp
->function
, Qbytecode
)
588 || btp
->nargs
== UNEVALLED
))
591 /* `btp' now points at the frame of the innermost function that isn't
592 a special form, ignoring frames for Finteractive_p and/or
593 Fbytecode at the top. If this frame is for a built-in function
594 (such as load or eval-region) return nil. */
595 fun
= Findirect_function (*btp
->function
, Qnil
);
596 if (exclude_subrs_p
&& SUBRP (fun
))
599 /* `btp' points to the frame of a Lisp function that called interactive-p.
600 Return t if that function was called interactively. */
601 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
607 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
608 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
609 Aliased variables always have the same value; setting one sets the other.
610 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
611 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
612 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
613 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
614 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
615 The return value is BASE-VARIABLE. */)
616 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
618 struct Lisp_Symbol
*sym
;
620 CHECK_SYMBOL (new_alias
);
621 CHECK_SYMBOL (base_variable
);
623 sym
= XSYMBOL (new_alias
);
626 /* Not sure why, but why not? */
627 error ("Cannot make a constant an alias");
629 switch (sym
->redirect
)
631 case SYMBOL_FORWARDED
:
632 error ("Cannot make an internal variable an alias");
633 case SYMBOL_LOCALIZED
:
634 error ("Don't know how to make a localized variable an alias");
637 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
638 If n_a is bound, but b_v is not, set the value of b_v to n_a,
639 so that old-code that affects n_a before the aliasing is setup
641 if (NILP (Fboundp (base_variable
)))
642 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
645 struct specbinding
*p
;
647 for (p
= specpdl_ptr
; p
> specpdl
; )
648 if ((--p
)->func
== NULL
650 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
651 error ("Don't know how to make a let-bound variable an alias");
654 sym
->declared_special
= 1;
655 XSYMBOL (base_variable
)->declared_special
= 1;
656 sym
->redirect
= SYMBOL_VARALIAS
;
657 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
658 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
659 LOADHIST_ATTACH (new_alias
);
660 /* Even if docstring is nil: remove old docstring. */
661 Fput (new_alias
, Qvariable_documentation
, docstring
);
663 return base_variable
;
667 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
668 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
669 You are not required to define a variable in order to use it, but
670 defining it lets you supply an initial value and documentation, which
671 can be referred to by the Emacs help facilities and other programming
672 tools. The `defvar' form also declares the variable as \"special\",
673 so that it is always dynamically bound even if `lexical-binding' is t.
675 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
676 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
677 default value is what is set; buffer-local values are not affected.
678 If INITVALUE is missing, SYMBOL's value is not set.
680 If SYMBOL has a local binding, then this form affects the local
681 binding. This is usually not what you want. Thus, if you need to
682 load a file defining variables, with this form or with `defconst' or
683 `defcustom', you should always load that file _outside_ any bindings
684 for these variables. \(`defconst' and `defcustom' behave similarly in
687 The optional argument DOCSTRING is a documentation string for the
690 To define a user option, use `defcustom' instead of `defvar'.
691 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
694 register Lisp_Object sym
, tem
, tail
;
698 if (!NILP (Fcdr (Fcdr (tail
))))
699 error ("Too many arguments");
701 tem
= Fdefault_boundp (sym
);
704 /* Do it before evaluating the initial value, for self-references. */
705 XSYMBOL (sym
)->declared_special
= 1;
708 Fset_default (sym
, eval_sub (Fcar (tail
)));
710 { /* Check if there is really a global binding rather than just a let
711 binding that shadows the global unboundness of the var. */
712 volatile struct specbinding
*pdl
= specpdl_ptr
;
713 while (pdl
> specpdl
)
715 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
716 && EQ (pdl
->old_value
, Qunbound
))
718 message_with_string ("Warning: defvar ignored because %s is let-bound",
719 SYMBOL_NAME (sym
), 1);
728 if (!NILP (Vpurify_flag
))
729 tem
= Fpurecopy (tem
);
730 Fput (sym
, Qvariable_documentation
, tem
);
732 LOADHIST_ATTACH (sym
);
734 else if (!NILP (Vinternal_interpreter_environment
)
735 && !XSYMBOL (sym
)->declared_special
)
736 /* A simple (defvar foo) with lexical scoping does "nothing" except
737 declare that var to be dynamically scoped *locally* (i.e. within
738 the current file or let-block). */
739 Vinternal_interpreter_environment
=
740 Fcons (sym
, Vinternal_interpreter_environment
);
743 /* Simple (defvar <var>) should not count as a definition at all.
744 It could get in the way of other definitions, and unloading this
745 package could try to make the variable unbound. */
751 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
752 doc
: /* Define SYMBOL as a constant variable.
753 This declares that neither programs nor users should ever change the
754 value. This constancy is not actually enforced by Emacs Lisp, but
755 SYMBOL is marked as a special variable so that it is never lexically
758 The `defconst' form always sets the value of SYMBOL to the result of
759 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
760 what is set; buffer-local values are not affected. If SYMBOL has a
761 local binding, then this form sets the local binding's value.
762 However, you should normally not make local bindings for variables
763 defined with this form.
765 The optional DOCSTRING specifies the variable's documentation string.
766 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
769 register Lisp_Object sym
, tem
;
772 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
773 error ("Too many arguments");
775 tem
= eval_sub (Fcar (Fcdr (args
)));
776 if (!NILP (Vpurify_flag
))
777 tem
= Fpurecopy (tem
);
778 Fset_default (sym
, tem
);
779 XSYMBOL (sym
)->declared_special
= 1;
780 tem
= Fcar (Fcdr (Fcdr (args
)));
783 if (!NILP (Vpurify_flag
))
784 tem
= Fpurecopy (tem
);
785 Fput (sym
, Qvariable_documentation
, tem
);
787 Fput (sym
, Qrisky_local_variable
, Qt
);
788 LOADHIST_ATTACH (sym
);
792 /* Make SYMBOL lexically scoped. */
793 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
794 Smake_var_non_special
, 1, 1, 0,
795 doc
: /* Internal function. */)
798 CHECK_SYMBOL (symbol
);
799 XSYMBOL (symbol
)->declared_special
= 0;
804 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
805 doc
: /* Bind variables according to VARLIST then eval BODY.
806 The value of the last form in BODY is returned.
807 Each element of VARLIST is a symbol (which is bound to nil)
808 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
809 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
810 usage: (let* VARLIST BODY...) */)
813 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
814 ptrdiff_t count
= SPECPDL_INDEX ();
815 struct gcpro gcpro1
, gcpro2
, gcpro3
;
817 GCPRO3 (args
, elt
, varlist
);
819 lexenv
= Vinternal_interpreter_environment
;
821 varlist
= Fcar (args
);
822 while (CONSP (varlist
))
826 elt
= XCAR (varlist
);
832 else if (! NILP (Fcdr (Fcdr (elt
))))
833 signal_error ("`let' bindings can have only one value-form", elt
);
837 val
= eval_sub (Fcar (Fcdr (elt
)));
840 if (!NILP (lexenv
) && SYMBOLP (var
)
841 && !XSYMBOL (var
)->declared_special
842 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
843 /* Lexically bind VAR by adding it to the interpreter's binding
847 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
848 if (EQ (Vinternal_interpreter_environment
, lexenv
))
849 /* Save the old lexical environment on the specpdl stack,
850 but only for the first lexical binding, since we'll never
851 need to revert to one of the intermediate ones. */
852 specbind (Qinternal_interpreter_environment
, newenv
);
854 Vinternal_interpreter_environment
= newenv
;
859 varlist
= XCDR (varlist
);
862 val
= Fprogn (Fcdr (args
));
863 return unbind_to (count
, val
);
866 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
867 doc
: /* Bind variables according to VARLIST then eval BODY.
868 The value of the last form in BODY is returned.
869 Each element of VARLIST is a symbol (which is bound to nil)
870 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
871 All the VALUEFORMs are evalled before any symbols are bound.
872 usage: (let VARLIST BODY...) */)
875 Lisp_Object
*temps
, tem
, lexenv
;
876 register Lisp_Object elt
, varlist
;
877 ptrdiff_t count
= SPECPDL_INDEX ();
879 struct gcpro gcpro1
, gcpro2
;
882 varlist
= Fcar (args
);
884 /* Make space to hold the values to give the bound variables. */
885 elt
= Flength (varlist
);
886 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
888 /* Compute the values and store them in `temps'. */
890 GCPRO2 (args
, *temps
);
893 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
896 elt
= XCAR (varlist
);
898 temps
[argnum
++] = Qnil
;
899 else if (! NILP (Fcdr (Fcdr (elt
))))
900 signal_error ("`let' bindings can have only one value-form", elt
);
902 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
903 gcpro2
.nvars
= argnum
;
907 lexenv
= Vinternal_interpreter_environment
;
909 varlist
= Fcar (args
);
910 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
914 elt
= XCAR (varlist
);
915 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
916 tem
= temps
[argnum
++];
918 if (!NILP (lexenv
) && SYMBOLP (var
)
919 && !XSYMBOL (var
)->declared_special
920 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
921 /* Lexically bind VAR by adding it to the lexenv alist. */
922 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
924 /* Dynamically bind VAR. */
928 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
929 /* Instantiate a new lexical environment. */
930 specbind (Qinternal_interpreter_environment
, lexenv
);
932 elt
= Fprogn (Fcdr (args
));
934 return unbind_to (count
, elt
);
937 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
938 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
939 The order of execution is thus TEST, BODY, TEST, BODY and so on
940 until TEST returns nil.
941 usage: (while TEST BODY...) */)
944 Lisp_Object test
, body
;
945 struct gcpro gcpro1
, gcpro2
;
951 while (!NILP (eval_sub (test
)))
961 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
962 doc
: /* Return result of expanding macros at top level of FORM.
963 If FORM is not a macro call, it is returned unchanged.
964 Otherwise, the macro is expanded and the expansion is considered
965 in place of FORM. When a non-macro-call results, it is returned.
967 The second optional arg ENVIRONMENT specifies an environment of macro
968 definitions to shadow the loaded ones for use in file byte-compilation. */)
969 (Lisp_Object form
, Lisp_Object environment
)
971 /* With cleanups from Hallvard Furuseth. */
972 register Lisp_Object expander
, sym
, def
, tem
;
976 /* Come back here each time we expand a macro call,
977 in case it expands into another macro call. */
980 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
981 def
= sym
= XCAR (form
);
983 /* Trace symbols aliases to other symbols
984 until we get a symbol that is not an alias. */
985 while (SYMBOLP (def
))
989 tem
= Fassq (sym
, environment
);
992 def
= XSYMBOL (sym
)->function
;
993 if (!EQ (def
, Qunbound
))
998 /* Right now TEM is the result from SYM in ENVIRONMENT,
999 and if TEM is nil then DEF is SYM's function definition. */
1002 /* SYM is not mentioned in ENVIRONMENT.
1003 Look at its function definition. */
1004 struct gcpro gcpro1
;
1006 def
= Fautoload_do_load (def
, sym
, Qmacro
);
1008 if (EQ (def
, Qunbound
) || !CONSP (def
))
1009 /* Not defined or definition not suitable. */
1011 if (!EQ (XCAR (def
), Qmacro
))
1013 else expander
= XCDR (def
);
1017 expander
= XCDR (tem
);
1018 if (NILP (expander
))
1022 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1023 if (EQ (form
, newform
))
1032 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1033 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1034 TAG is evalled to get the tag to use; it must not be nil.
1036 Then the BODY is executed.
1037 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1038 If no throw happens, `catch' returns the value of the last BODY form.
1039 If a throw happens, it specifies the value to return from `catch'.
1040 usage: (catch TAG BODY...) */)
1043 register Lisp_Object tag
;
1044 struct gcpro gcpro1
;
1047 tag
= eval_sub (Fcar (args
));
1049 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1052 /* Set up a catch, then call C function FUNC on argument ARG.
1053 FUNC should return a Lisp_Object.
1054 This is how catches are done from within C code. */
1057 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1059 /* This structure is made part of the chain `catchlist'. */
1062 /* Fill in the components of c, and put it on the list. */
1066 c
.backlist
= backtrace_list
;
1067 c
.handlerlist
= handlerlist
;
1068 c
.lisp_eval_depth
= lisp_eval_depth
;
1069 c
.pdlcount
= SPECPDL_INDEX ();
1070 c
.poll_suppress_count
= poll_suppress_count
;
1071 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1072 c
.gcpro
= gcprolist
;
1073 c
.byte_stack
= byte_stack_list
;
1077 if (! _setjmp (c
.jmp
))
1078 c
.val
= (*func
) (arg
);
1080 /* Throw works by a longjmp that comes right here. */
1085 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1086 jump to that CATCH, returning VALUE as the value of that catch.
1088 This is the guts Fthrow and Fsignal; they differ only in the way
1089 they choose the catch tag to throw to. A catch tag for a
1090 condition-case form has a TAG of Qnil.
1092 Before each catch is discarded, unbind all special bindings and
1093 execute all unwind-protect clauses made above that catch. Unwind
1094 the handler stack as we go, so that the proper handlers are in
1095 effect for each unwind-protect clause we run. At the end, restore
1096 some static info saved in CATCH, and longjmp to the location
1099 This is used for correct unwinding in Fthrow and Fsignal. */
1101 static _Noreturn
void
1102 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1106 /* Save the value in the tag. */
1109 /* Restore certain special C variables. */
1110 set_poll_suppress_count (catch->poll_suppress_count
);
1111 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1112 handling_signal
= 0;
1117 last_time
= catchlist
== catch;
1119 /* Unwind the specpdl stack, and then restore the proper set of
1121 unbind_to (catchlist
->pdlcount
, Qnil
);
1122 handlerlist
= catchlist
->handlerlist
;
1123 catchlist
= catchlist
->next
;
1125 while (! last_time
);
1128 /* If x_catch_errors was done, turn it off now.
1129 (First we give unbind_to a chance to do that.) */
1130 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1131 The catch must remain in effect during that delicate
1132 state. --lorentey */
1133 x_fully_uncatch_errors ();
1137 byte_stack_list
= catch->byte_stack
;
1138 gcprolist
= catch->gcpro
;
1140 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1142 backtrace_list
= catch->backlist
;
1143 lisp_eval_depth
= catch->lisp_eval_depth
;
1145 _longjmp (catch->jmp
, 1);
1148 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1149 doc
: /* Throw to the catch for TAG and return VALUE from it.
1150 Both TAG and VALUE are evalled. */)
1151 (register Lisp_Object tag
, Lisp_Object value
)
1153 register struct catchtag
*c
;
1156 for (c
= catchlist
; c
; c
= c
->next
)
1158 if (EQ (c
->tag
, tag
))
1159 unwind_to_catch (c
, value
);
1161 xsignal2 (Qno_catch
, tag
, value
);
1165 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1166 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1167 If BODYFORM completes normally, its value is returned
1168 after executing the UNWINDFORMS.
1169 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1170 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1174 ptrdiff_t count
= SPECPDL_INDEX ();
1176 record_unwind_protect (Fprogn
, Fcdr (args
));
1177 val
= eval_sub (Fcar (args
));
1178 return unbind_to (count
, val
);
1181 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1182 doc
: /* Regain control when an error is signaled.
1183 Executes BODYFORM and returns its value if no error happens.
1184 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1185 where the BODY is made of Lisp expressions.
1187 A handler is applicable to an error
1188 if CONDITION-NAME is one of the error's condition names.
1189 If an error happens, the first applicable handler is run.
1191 The car of a handler may be a list of condition names instead of a
1192 single condition name; then it handles all of them. If the special
1193 condition name `debug' is present in this list, it allows another
1194 condition in the list to run the debugger if `debug-on-error' and the
1195 other usual mechanisms says it should (otherwise, `condition-case'
1196 suppresses the debugger).
1198 When a handler handles an error, control returns to the `condition-case'
1199 and it executes the handler's BODY...
1200 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1201 \(If VAR is nil, the handler can't access that information.)
1202 Then the value of the last BODY form is returned from the `condition-case'
1205 See also the function `signal' for more info.
1206 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1209 register Lisp_Object bodyform
, handlers
;
1210 volatile Lisp_Object var
;
1213 bodyform
= Fcar (Fcdr (args
));
1214 handlers
= Fcdr (Fcdr (args
));
1216 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1219 /* Like Fcondition_case, but the args are separate
1220 rather than passed in a list. Used by Fbyte_code. */
1223 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1224 Lisp_Object handlers
)
1232 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1238 && (SYMBOLP (XCAR (tem
))
1239 || CONSP (XCAR (tem
))))))
1240 error ("Invalid condition handler: %s",
1241 SDATA (Fprin1_to_string (tem
, Qt
)));
1246 c
.backlist
= backtrace_list
;
1247 c
.handlerlist
= handlerlist
;
1248 c
.lisp_eval_depth
= lisp_eval_depth
;
1249 c
.pdlcount
= SPECPDL_INDEX ();
1250 c
.poll_suppress_count
= poll_suppress_count
;
1251 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1252 c
.gcpro
= gcprolist
;
1253 c
.byte_stack
= byte_stack_list
;
1254 if (_setjmp (c
.jmp
))
1257 specbind (h
.var
, c
.val
);
1258 val
= Fprogn (Fcdr (h
.chosen_clause
));
1260 /* Note that this just undoes the binding of h.var; whoever
1261 longjumped to us unwound the stack to c.pdlcount before
1263 unbind_to (c
.pdlcount
, Qnil
);
1270 h
.handler
= handlers
;
1271 h
.next
= handlerlist
;
1275 val
= eval_sub (bodyform
);
1277 handlerlist
= h
.next
;
1281 /* Call the function BFUN with no arguments, catching errors within it
1282 according to HANDLERS. If there is an error, call HFUN with
1283 one argument which is the data that describes the error:
1286 HANDLERS can be a list of conditions to catch.
1287 If HANDLERS is Qt, catch all errors.
1288 If HANDLERS is Qerror, catch all errors
1289 but allow the debugger to run if that is enabled. */
1292 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1293 Lisp_Object (*hfun
) (Lisp_Object
))
1301 c
.backlist
= backtrace_list
;
1302 c
.handlerlist
= handlerlist
;
1303 c
.lisp_eval_depth
= lisp_eval_depth
;
1304 c
.pdlcount
= SPECPDL_INDEX ();
1305 c
.poll_suppress_count
= poll_suppress_count
;
1306 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1307 c
.gcpro
= gcprolist
;
1308 c
.byte_stack
= byte_stack_list
;
1309 if (_setjmp (c
.jmp
))
1311 return (*hfun
) (c
.val
);
1315 h
.handler
= handlers
;
1317 h
.next
= handlerlist
;
1323 handlerlist
= h
.next
;
1327 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1330 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1331 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1339 c
.backlist
= backtrace_list
;
1340 c
.handlerlist
= handlerlist
;
1341 c
.lisp_eval_depth
= lisp_eval_depth
;
1342 c
.pdlcount
= SPECPDL_INDEX ();
1343 c
.poll_suppress_count
= poll_suppress_count
;
1344 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1345 c
.gcpro
= gcprolist
;
1346 c
.byte_stack
= byte_stack_list
;
1347 if (_setjmp (c
.jmp
))
1349 return (*hfun
) (c
.val
);
1353 h
.handler
= handlers
;
1355 h
.next
= handlerlist
;
1359 val
= (*bfun
) (arg
);
1361 handlerlist
= h
.next
;
1365 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1369 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1372 Lisp_Object handlers
,
1373 Lisp_Object (*hfun
) (Lisp_Object
))
1381 c
.backlist
= backtrace_list
;
1382 c
.handlerlist
= handlerlist
;
1383 c
.lisp_eval_depth
= lisp_eval_depth
;
1384 c
.pdlcount
= SPECPDL_INDEX ();
1385 c
.poll_suppress_count
= poll_suppress_count
;
1386 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1387 c
.gcpro
= gcprolist
;
1388 c
.byte_stack
= byte_stack_list
;
1389 if (_setjmp (c
.jmp
))
1391 return (*hfun
) (c
.val
);
1395 h
.handler
= handlers
;
1397 h
.next
= handlerlist
;
1401 val
= (*bfun
) (arg1
, arg2
);
1403 handlerlist
= h
.next
;
1407 /* Like internal_condition_case but call BFUN with NARGS as first,
1408 and ARGS as second argument. */
1411 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1414 Lisp_Object handlers
,
1415 Lisp_Object (*hfun
) (Lisp_Object err
,
1425 c
.backlist
= backtrace_list
;
1426 c
.handlerlist
= handlerlist
;
1427 c
.lisp_eval_depth
= lisp_eval_depth
;
1428 c
.pdlcount
= SPECPDL_INDEX ();
1429 c
.poll_suppress_count
= poll_suppress_count
;
1430 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1431 c
.gcpro
= gcprolist
;
1432 c
.byte_stack
= byte_stack_list
;
1433 if (_setjmp (c
.jmp
))
1435 return (*hfun
) (c
.val
, nargs
, args
);
1439 h
.handler
= handlers
;
1441 h
.next
= handlerlist
;
1445 val
= (*bfun
) (nargs
, args
);
1447 handlerlist
= h
.next
;
1452 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1453 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1457 process_quit_flag (void)
1459 Lisp_Object flag
= Vquit_flag
;
1461 if (EQ (flag
, Qkill_emacs
))
1463 if (EQ (Vthrow_on_input
, flag
))
1464 Fthrow (Vthrow_on_input
, Qt
);
1465 Fsignal (Qquit
, Qnil
);
1468 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1469 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1470 This function does not return.
1472 An error symbol is a symbol with an `error-conditions' property
1473 that is a list of condition names.
1474 A handler for any of those names will get to handle this signal.
1475 The symbol `error' should normally be one of them.
1477 DATA should be a list. Its elements are printed as part of the error message.
1478 See Info anchor `(elisp)Definition of signal' for some details on how this
1479 error message is constructed.
1480 If the signal is handled, DATA is made available to the handler.
1481 See also the function `condition-case'. */)
1482 (Lisp_Object error_symbol
, Lisp_Object data
)
1484 /* When memory is full, ERROR-SYMBOL is nil,
1485 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1486 That is a special case--don't do this in other situations. */
1487 Lisp_Object conditions
;
1489 Lisp_Object real_error_symbol
1490 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1491 register Lisp_Object clause
= Qnil
;
1493 struct backtrace
*bp
;
1495 immediate_quit
= handling_signal
= 0;
1497 if (gc_in_progress
|| waiting_for_input
)
1500 #if 0 /* rms: I don't know why this was here,
1501 but it is surely wrong for an error that is handled. */
1502 #ifdef HAVE_WINDOW_SYSTEM
1503 if (display_hourglass_p
)
1504 cancel_hourglass ();
1508 /* This hook is used by edebug. */
1509 if (! NILP (Vsignal_hook_function
)
1510 && ! NILP (error_symbol
))
1512 /* Edebug takes care of restoring these variables when it exits. */
1513 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1514 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1516 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1517 max_specpdl_size
= SPECPDL_INDEX () + 40;
1519 call2 (Vsignal_hook_function
, error_symbol
, data
);
1522 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1524 /* Remember from where signal was called. Skip over the frame for
1525 `signal' itself. If a frame for `error' follows, skip that,
1526 too. Don't do this when ERROR_SYMBOL is nil, because that
1527 is a memory-full error. */
1528 Vsignaling_function
= Qnil
;
1529 if (backtrace_list
&& !NILP (error_symbol
))
1531 bp
= backtrace_list
->next
;
1532 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1534 if (bp
&& bp
->function
)
1535 Vsignaling_function
= *bp
->function
;
1538 for (h
= handlerlist
; h
; h
= h
->next
)
1540 clause
= find_handler_clause (h
->handler
, conditions
);
1545 if (/* Don't run the debugger for a memory-full error.
1546 (There is no room in memory to do that!) */
1547 !NILP (error_symbol
)
1548 && (!NILP (Vdebug_on_signal
)
1549 /* If no handler is present now, try to run the debugger. */
1551 /* A `debug' symbol in the handler list disables the normal
1552 suppression of the debugger. */
1553 || (CONSP (clause
) && CONSP (XCAR (clause
))
1554 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1555 /* Special handler that means "print a message and run debugger
1557 || EQ (h
->handler
, Qerror
)))
1560 = maybe_call_debugger (conditions
, error_symbol
, data
);
1561 /* We can't return values to code which signaled an error, but we
1562 can continue code which has signaled a quit. */
1563 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1569 Lisp_Object unwind_data
1570 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1572 h
->chosen_clause
= clause
;
1573 unwind_to_catch (h
->tag
, unwind_data
);
1578 Fthrow (Qtop_level
, Qt
);
1581 if (! NILP (error_symbol
))
1582 data
= Fcons (error_symbol
, data
);
1584 string
= Ferror_message_string (data
);
1585 fatal ("%s", SDATA (string
));
1588 /* Internal version of Fsignal that never returns.
1589 Used for anything but Qquit (which can return from Fsignal). */
1592 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1594 Fsignal (error_symbol
, data
);
1598 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1601 xsignal0 (Lisp_Object error_symbol
)
1603 xsignal (error_symbol
, Qnil
);
1607 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1609 xsignal (error_symbol
, list1 (arg
));
1613 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1615 xsignal (error_symbol
, list2 (arg1
, arg2
));
1619 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1621 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1624 /* Signal `error' with message S, and additional arg ARG.
1625 If ARG is not a genuine list, make it a one-element list. */
1628 signal_error (const char *s
, Lisp_Object arg
)
1630 Lisp_Object tortoise
, hare
;
1632 hare
= tortoise
= arg
;
1633 while (CONSP (hare
))
1640 tortoise
= XCDR (tortoise
);
1642 if (EQ (hare
, tortoise
))
1647 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1649 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1653 /* Return nonzero if LIST is a non-nil atom or
1654 a list containing one of CONDITIONS. */
1657 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1664 while (CONSP (conditions
))
1666 Lisp_Object
this, tail
;
1667 this = XCAR (conditions
);
1668 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1669 if (EQ (XCAR (tail
), this))
1671 conditions
= XCDR (conditions
);
1676 /* Return 1 if an error with condition-symbols CONDITIONS,
1677 and described by SIGNAL-DATA, should skip the debugger
1678 according to debugger-ignored-errors. */
1681 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1684 int first_string
= 1;
1685 Lisp_Object error_message
;
1687 error_message
= Qnil
;
1688 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1690 if (STRINGP (XCAR (tail
)))
1694 error_message
= Ferror_message_string (data
);
1698 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1703 Lisp_Object contail
;
1705 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1706 if (EQ (XCAR (tail
), XCAR (contail
)))
1714 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1715 SIG and DATA describe the signal. There are two ways to pass them:
1716 = SIG is the error symbol, and DATA is the rest of the data.
1717 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1718 This is for memory-full errors only. */
1720 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1722 Lisp_Object combined_data
;
1724 combined_data
= Fcons (sig
, data
);
1727 /* Don't try to run the debugger with interrupts blocked.
1728 The editing loop would return anyway. */
1730 /* Does user want to enter debugger for this kind of error? */
1733 : wants_debugger (Vdebug_on_error
, conditions
))
1734 && ! skip_debugger (conditions
, combined_data
)
1735 /* RMS: What's this for? */
1736 && when_entered_debugger
< num_nonmacro_input_events
)
1738 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1746 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1748 register Lisp_Object h
;
1750 /* t is used by handlers for all conditions, set up by C code. */
1751 if (EQ (handlers
, Qt
))
1754 /* error is used similarly, but means print an error message
1755 and run the debugger if that is enabled. */
1756 if (EQ (handlers
, Qerror
))
1759 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1761 Lisp_Object handler
= XCAR (h
);
1762 Lisp_Object condit
, tem
;
1764 if (!CONSP (handler
))
1766 condit
= XCAR (handler
);
1767 /* Handle a single condition name in handler HANDLER. */
1768 if (SYMBOLP (condit
))
1770 tem
= Fmemq (Fcar (handler
), conditions
);
1774 /* Handle a list of condition names in handler HANDLER. */
1775 else if (CONSP (condit
))
1778 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1780 tem
= Fmemq (XCAR (tail
), conditions
);
1791 /* Dump an error message; called like vprintf. */
1793 verror (const char *m
, va_list ap
)
1796 ptrdiff_t size
= sizeof buf
;
1797 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1802 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1803 string
= make_string (buffer
, used
);
1807 xsignal1 (Qerror
, string
);
1811 /* Dump an error message; called like printf. */
1815 error (const char *m
, ...)
1823 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1824 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1825 This means it contains a description for how to read arguments to give it.
1826 The value is nil for an invalid function or a symbol with no function
1829 Interactively callable functions include strings and vectors (treated
1830 as keyboard macros), lambda-expressions that contain a top-level call
1831 to `interactive', autoload definitions made by `autoload' with non-nil
1832 fourth argument, and some of the built-in functions of Lisp.
1834 Also, a symbol satisfies `commandp' if its function definition does so.
1836 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1837 then strings and vectors are not accepted. */)
1838 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1840 register Lisp_Object fun
;
1841 register Lisp_Object funcar
;
1842 Lisp_Object if_prop
= Qnil
;
1846 fun
= indirect_function (fun
); /* Check cycles. */
1847 if (NILP (fun
) || EQ (fun
, Qunbound
))
1850 /* Check an `interactive-form' property if present, analogous to the
1851 function-documentation property. */
1853 while (SYMBOLP (fun
))
1855 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1858 fun
= Fsymbol_function (fun
);
1861 /* Emacs primitives are interactive if their DEFUN specifies an
1862 interactive spec. */
1864 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1866 /* Bytecode objects are interactive if they are long enough to
1867 have an element whose index is COMPILED_INTERACTIVE, which is
1868 where the interactive spec is stored. */
1869 else if (COMPILEDP (fun
))
1870 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1873 /* Strings and vectors are keyboard macros. */
1874 if (STRINGP (fun
) || VECTORP (fun
))
1875 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1877 /* Lists may represent commands. */
1880 funcar
= XCAR (fun
);
1881 if (EQ (funcar
, Qclosure
))
1882 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1884 else if (EQ (funcar
, Qlambda
))
1885 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1886 else if (EQ (funcar
, Qautoload
))
1887 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1892 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1893 doc
: /* Define FUNCTION to autoload from FILE.
1894 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1895 Third arg DOCSTRING is documentation for the function.
1896 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1897 Fifth arg TYPE indicates the type of the object:
1898 nil or omitted says FUNCTION is a function,
1899 `keymap' says FUNCTION is really a keymap, and
1900 `macro' or t says FUNCTION is really a macro.
1901 Third through fifth args give info about the real definition.
1902 They default to nil.
1903 If FUNCTION is already defined other than as an autoload,
1904 this does nothing and returns nil. */)
1905 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1907 CHECK_SYMBOL (function
);
1908 CHECK_STRING (file
);
1910 /* If function is defined and not as an autoload, don't override. */
1911 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1912 && !(CONSP (XSYMBOL (function
)->function
)
1913 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1916 if (NILP (Vpurify_flag
))
1917 /* Only add entries after dumping, because the ones before are
1918 not useful and else we get loads of them from the loaddefs.el. */
1919 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1920 else if (EQ (docstring
, make_number (0)))
1921 /* `read1' in lread.c has found the docstring starting with "\
1922 and assumed the docstring will be provided by Snarf-documentation, so it
1923 passed us 0 instead. But that leads to accidental sharing in purecopy's
1924 hash-consing, so we use a (hopefully) unique integer instead. */
1925 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1926 return Ffset (function
,
1927 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1928 interactive
, type
)));
1932 un_autoload (Lisp_Object oldqueue
)
1934 register Lisp_Object queue
, first
, second
;
1936 /* Queue to unwind is current value of Vautoload_queue.
1937 oldqueue is the shadowed value to leave in Vautoload_queue. */
1938 queue
= Vautoload_queue
;
1939 Vautoload_queue
= oldqueue
;
1940 while (CONSP (queue
))
1942 first
= XCAR (queue
);
1943 second
= Fcdr (first
);
1944 first
= Fcar (first
);
1945 if (EQ (first
, make_number (0)))
1948 Ffset (first
, second
);
1949 queue
= XCDR (queue
);
1954 /* Load an autoloaded function.
1955 FUNNAME is the symbol which is the function's name.
1956 FUNDEF is the autoload definition (a list). */
1958 DEFUN ("autoload-do-load", Fautoload_do_load
, Sautoload_do_load
, 1, 3, 0,
1959 doc
: /* Load FUNDEF which should be an autoload.
1960 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1961 in which case the function returns the new autoloaded function value.
1962 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1963 it is defines a macro. */)
1964 (Lisp_Object fundef
, Lisp_Object funname
, Lisp_Object macro_only
)
1966 ptrdiff_t count
= SPECPDL_INDEX ();
1967 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1969 if (!CONSP (fundef
) || !EQ (Qautoload
, XCAR (fundef
)))
1972 if (EQ (macro_only
, Qmacro
))
1974 Lisp_Object kind
= Fnth (make_number (4), fundef
);
1975 if (! (EQ (kind
, Qt
) || EQ (kind
, Qmacro
)))
1979 /* This is to make sure that loadup.el gives a clear picture
1980 of what files are preloaded and when. */
1981 if (! NILP (Vpurify_flag
))
1982 error ("Attempt to autoload %s while preparing to dump",
1983 SDATA (SYMBOL_NAME (funname
)));
1985 CHECK_SYMBOL (funname
);
1986 GCPRO3 (funname
, fundef
, macro_only
);
1988 /* Preserve the match data. */
1989 record_unwind_save_match_data ();
1991 /* If autoloading gets an error (which includes the error of failing
1992 to define the function being called), we use Vautoload_queue
1993 to undo function definitions and `provide' calls made by
1994 the function. We do this in the specific case of autoloading
1995 because autoloading is not an explicit request "load this file",
1996 but rather a request to "call this function".
1998 The value saved here is to be restored into Vautoload_queue. */
1999 record_unwind_protect (un_autoload
, Vautoload_queue
);
2000 Vautoload_queue
= Qt
;
2001 /* If `macro_only', assume this autoload to be a "best-effort",
2002 so don't signal an error if autoloading fails. */
2003 Fload (Fcar (Fcdr (fundef
)), macro_only
, Qt
, Qnil
, Qt
);
2005 /* Once loading finishes, don't undo it. */
2006 Vautoload_queue
= Qt
;
2007 unbind_to (count
, Qnil
);
2015 Lisp_Object fun
= Findirect_function (funname
, Qnil
);
2017 if (!NILP (Fequal (fun
, fundef
)))
2018 error ("Autoloading failed to define function %s",
2019 SDATA (SYMBOL_NAME (funname
)));
2026 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2027 doc
: /* Evaluate FORM and return its value.
2028 If LEXICAL is t, evaluate using lexical scoping. */)
2029 (Lisp_Object form
, Lisp_Object lexical
)
2031 ptrdiff_t count
= SPECPDL_INDEX ();
2032 specbind (Qinternal_interpreter_environment
,
2033 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2034 return unbind_to (count
, eval_sub (form
));
2037 /* Eval a sub-expression of the current expression (i.e. in the same
2040 eval_sub (Lisp_Object form
)
2042 Lisp_Object fun
, val
, original_fun
, original_args
;
2044 struct backtrace backtrace
;
2045 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2047 if (handling_signal
)
2052 /* Look up its binding in the lexical environment.
2053 We do not pay attention to the declared_special flag here, since we
2054 already did that when let-binding the variable. */
2055 Lisp_Object lex_binding
2056 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2057 ? Fassq (form
, Vinternal_interpreter_environment
)
2059 if (CONSP (lex_binding
))
2060 return XCDR (lex_binding
);
2062 return Fsymbol_value (form
);
2071 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2073 if (max_lisp_eval_depth
< 100)
2074 max_lisp_eval_depth
= 100;
2075 if (lisp_eval_depth
> max_lisp_eval_depth
)
2076 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2079 original_fun
= XCAR (form
);
2080 original_args
= XCDR (form
);
2082 backtrace
.next
= backtrace_list
;
2083 backtrace_list
= &backtrace
;
2084 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2085 backtrace
.args
= &original_args
;
2086 backtrace
.nargs
= UNEVALLED
;
2087 backtrace
.debug_on_exit
= 0;
2089 if (debug_on_next_call
)
2090 do_debug_on_call (Qt
);
2092 /* At this point, only original_fun and original_args
2093 have values that will be used below. */
2096 /* Optimize for no indirection. */
2098 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2099 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2100 fun
= indirect_function (fun
);
2104 Lisp_Object numargs
;
2105 Lisp_Object argvals
[8];
2106 Lisp_Object args_left
;
2107 register int i
, maxargs
;
2109 args_left
= original_args
;
2110 numargs
= Flength (args_left
);
2114 if (XINT (numargs
) < XSUBR (fun
)->min_args
2115 || (XSUBR (fun
)->max_args
>= 0
2116 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2117 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2119 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2120 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2121 else if (XSUBR (fun
)->max_args
== MANY
)
2123 /* Pass a vector of evaluated arguments. */
2125 ptrdiff_t argnum
= 0;
2128 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2130 GCPRO3 (args_left
, fun
, fun
);
2134 while (!NILP (args_left
))
2136 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2137 args_left
= Fcdr (args_left
);
2138 gcpro3
.nvars
= argnum
;
2141 backtrace
.args
= vals
;
2142 backtrace
.nargs
= XINT (numargs
);
2144 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2150 GCPRO3 (args_left
, fun
, fun
);
2151 gcpro3
.var
= argvals
;
2154 maxargs
= XSUBR (fun
)->max_args
;
2155 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2157 argvals
[i
] = eval_sub (Fcar (args_left
));
2163 backtrace
.args
= argvals
;
2164 backtrace
.nargs
= XINT (numargs
);
2169 val
= (XSUBR (fun
)->function
.a0 ());
2172 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2175 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2178 val
= (XSUBR (fun
)->function
.a3
2179 (argvals
[0], argvals
[1], argvals
[2]));
2182 val
= (XSUBR (fun
)->function
.a4
2183 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2186 val
= (XSUBR (fun
)->function
.a5
2187 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2191 val
= (XSUBR (fun
)->function
.a6
2192 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2193 argvals
[4], argvals
[5]));
2196 val
= (XSUBR (fun
)->function
.a7
2197 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2198 argvals
[4], argvals
[5], argvals
[6]));
2202 val
= (XSUBR (fun
)->function
.a8
2203 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2204 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2208 /* Someone has created a subr that takes more arguments than
2209 is supported by this code. We need to either rewrite the
2210 subr to use a different argument protocol, or add more
2211 cases to this switch. */
2216 else if (COMPILEDP (fun
))
2217 val
= apply_lambda (fun
, original_args
);
2220 if (EQ (fun
, Qunbound
))
2221 xsignal1 (Qvoid_function
, original_fun
);
2223 xsignal1 (Qinvalid_function
, original_fun
);
2224 funcar
= XCAR (fun
);
2225 if (!SYMBOLP (funcar
))
2226 xsignal1 (Qinvalid_function
, original_fun
);
2227 if (EQ (funcar
, Qautoload
))
2229 Fautoload_do_load (fun
, original_fun
, Qnil
);
2232 if (EQ (funcar
, Qmacro
))
2234 ptrdiff_t count
= SPECPDL_INDEX ();
2235 extern Lisp_Object Qlexical_binding
;
2237 /* Bind lexical-binding during expansion of the macro, so the
2238 macro can know reliably if the code it outputs will be
2239 interpreted using lexical-binding or not. */
2240 specbind (Qlexical_binding
,
2241 NILP (Vinternal_interpreter_environment
) ? Qnil
: Qt
);
2242 exp
= apply1 (Fcdr (fun
), original_args
);
2243 unbind_to (count
, Qnil
);
2244 val
= eval_sub (exp
);
2246 else if (EQ (funcar
, Qlambda
)
2247 || EQ (funcar
, Qclosure
))
2248 val
= apply_lambda (fun
, original_args
);
2250 xsignal1 (Qinvalid_function
, original_fun
);
2255 if (backtrace
.debug_on_exit
)
2256 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2257 backtrace_list
= backtrace
.next
;
2262 DEFUN ("apply", Fapply
, Sapply
, 1, MANY
, 0,
2263 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2264 Then return the value FUNCTION returns.
2265 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2266 usage: (apply FUNCTION &rest ARGUMENTS) */)
2267 (ptrdiff_t nargs
, Lisp_Object
*args
)
2271 register Lisp_Object spread_arg
;
2272 register Lisp_Object
*funcall_args
;
2273 Lisp_Object fun
, retval
;
2274 struct gcpro gcpro1
;
2279 spread_arg
= args
[nargs
- 1];
2280 CHECK_LIST (spread_arg
);
2282 numargs
= XINT (Flength (spread_arg
));
2285 return Ffuncall (nargs
- 1, args
);
2286 else if (numargs
== 1)
2288 args
[nargs
- 1] = XCAR (spread_arg
);
2289 return Ffuncall (nargs
, args
);
2292 numargs
+= nargs
- 2;
2294 /* Optimize for no indirection. */
2295 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2296 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2297 fun
= indirect_function (fun
);
2298 if (EQ (fun
, Qunbound
))
2300 /* Let funcall get the error. */
2307 if (numargs
< XSUBR (fun
)->min_args
2308 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2309 goto funcall
; /* Let funcall get the error. */
2310 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2312 /* Avoid making funcall cons up a yet another new vector of arguments
2313 by explicitly supplying nil's for optional values. */
2314 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2315 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2316 funcall_args
[++i
] = Qnil
;
2317 GCPRO1 (*funcall_args
);
2318 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2322 /* We add 1 to numargs because funcall_args includes the
2323 function itself as well as its arguments. */
2326 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2327 GCPRO1 (*funcall_args
);
2328 gcpro1
.nvars
= 1 + numargs
;
2331 memcpy (funcall_args
, args
, nargs
* word_size
);
2332 /* Spread the last arg we got. Its first element goes in
2333 the slot that it used to occupy, hence this value of I. */
2335 while (!NILP (spread_arg
))
2337 funcall_args
[i
++] = XCAR (spread_arg
);
2338 spread_arg
= XCDR (spread_arg
);
2341 /* By convention, the caller needs to gcpro Ffuncall's args. */
2342 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2349 /* Run hook variables in various ways. */
2352 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2354 Ffuncall (nargs
, args
);
2358 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2359 doc
: /* Run each hook in HOOKS.
2360 Each argument should be a symbol, a hook variable.
2361 These symbols are processed in the order specified.
2362 If a hook symbol has a non-nil value, that value may be a function
2363 or a list of functions to be called to run the hook.
2364 If the value is a function, it is called with no arguments.
2365 If it is a list, the elements are called, in order, with no arguments.
2367 Major modes should not use this function directly to run their mode
2368 hook; they should use `run-mode-hooks' instead.
2370 Do not use `make-local-variable' to make a hook variable buffer-local.
2371 Instead, use `add-hook' and specify t for the LOCAL argument.
2372 usage: (run-hooks &rest HOOKS) */)
2373 (ptrdiff_t nargs
, Lisp_Object
*args
)
2375 Lisp_Object hook
[1];
2378 for (i
= 0; i
< nargs
; i
++)
2381 run_hook_with_args (1, hook
, funcall_nil
);
2387 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2388 Srun_hook_with_args
, 1, MANY
, 0,
2389 doc
: /* Run HOOK with the specified arguments ARGS.
2390 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2391 value, that value may be a function or a list of functions to be
2392 called to run the hook. If the value is a function, it is called with
2393 the given arguments and its return value is returned. If it is a list
2394 of functions, those functions are called, in order,
2395 with the given arguments ARGS.
2396 It is best not to depend on the value returned by `run-hook-with-args',
2399 Do not use `make-local-variable' to make a hook variable buffer-local.
2400 Instead, use `add-hook' and specify t for the LOCAL argument.
2401 usage: (run-hook-with-args HOOK &rest ARGS) */)
2402 (ptrdiff_t nargs
, Lisp_Object
*args
)
2404 return run_hook_with_args (nargs
, args
, funcall_nil
);
2407 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2408 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2409 doc
: /* Run HOOK with the specified arguments ARGS.
2410 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2411 value, that value may be a function or a list of functions to be
2412 called to run the hook. If the value is a function, it is called with
2413 the given arguments and its return value is returned.
2414 If it is a list of functions, those functions are called, in order,
2415 with the given arguments ARGS, until one of them
2416 returns a non-nil value. Then we return that value.
2417 However, if they all return nil, we return nil.
2419 Do not use `make-local-variable' to make a hook variable buffer-local.
2420 Instead, use `add-hook' and specify t for the LOCAL argument.
2421 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2422 (ptrdiff_t nargs
, Lisp_Object
*args
)
2424 return run_hook_with_args (nargs
, args
, Ffuncall
);
2428 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2430 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2433 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2434 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2435 doc
: /* Run HOOK with the specified arguments ARGS.
2436 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2437 value, that value may be a function or a list of functions to be
2438 called to run the hook. If the value is a function, it is called with
2439 the given arguments and its return value is returned.
2440 If it is a list of functions, those functions are called, in order,
2441 with the given arguments ARGS, until one of them returns nil.
2442 Then we return nil. However, if they all return non-nil, we return non-nil.
2444 Do not use `make-local-variable' to make a hook variable buffer-local.
2445 Instead, use `add-hook' and specify t for the LOCAL argument.
2446 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2447 (ptrdiff_t nargs
, Lisp_Object
*args
)
2449 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2453 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2455 Lisp_Object tmp
= args
[0], ret
;
2458 ret
= Ffuncall (nargs
, args
);
2464 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2465 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2466 I.e. instead of calling each function FUN directly with arguments ARGS,
2467 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2468 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2469 aborts and returns that value.
2470 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2471 (ptrdiff_t nargs
, Lisp_Object
*args
)
2473 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2476 /* ARGS[0] should be a hook symbol.
2477 Call each of the functions in the hook value, passing each of them
2478 as arguments all the rest of ARGS (all NARGS - 1 elements).
2479 FUNCALL specifies how to call each function on the hook.
2480 The caller (or its caller, etc) must gcpro all of ARGS,
2481 except that it isn't necessary to gcpro ARGS[0]. */
2484 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2485 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2487 Lisp_Object sym
, val
, ret
= Qnil
;
2488 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2490 /* If we are dying or still initializing,
2491 don't do anything--it would probably crash if we tried. */
2492 if (NILP (Vrun_hooks
))
2496 val
= find_symbol_value (sym
);
2498 if (EQ (val
, Qunbound
) || NILP (val
))
2500 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2503 return funcall (nargs
, args
);
2507 Lisp_Object global_vals
= Qnil
;
2508 GCPRO3 (sym
, val
, global_vals
);
2511 CONSP (val
) && NILP (ret
);
2514 if (EQ (XCAR (val
), Qt
))
2516 /* t indicates this hook has a local binding;
2517 it means to run the global binding too. */
2518 global_vals
= Fdefault_value (sym
);
2519 if (NILP (global_vals
)) continue;
2521 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2523 args
[0] = global_vals
;
2524 ret
= funcall (nargs
, args
);
2529 CONSP (global_vals
) && NILP (ret
);
2530 global_vals
= XCDR (global_vals
))
2532 args
[0] = XCAR (global_vals
);
2533 /* In a global value, t should not occur. If it does, we
2534 must ignore it to avoid an endless loop. */
2535 if (!EQ (args
[0], Qt
))
2536 ret
= funcall (nargs
, args
);
2542 args
[0] = XCAR (val
);
2543 ret
= funcall (nargs
, args
);
2552 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2555 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2557 Lisp_Object temp
[3];
2562 Frun_hook_with_args (3, temp
);
2565 /* Apply fn to arg. */
2567 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2569 struct gcpro gcpro1
;
2573 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2576 Lisp_Object args
[2];
2580 RETURN_UNGCPRO (Fapply (2, args
));
2584 /* Call function fn on no arguments. */
2586 call0 (Lisp_Object fn
)
2588 struct gcpro gcpro1
;
2591 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2594 /* Call function fn with 1 argument arg1. */
2597 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2599 struct gcpro gcpro1
;
2600 Lisp_Object args
[2];
2606 RETURN_UNGCPRO (Ffuncall (2, args
));
2609 /* Call function fn with 2 arguments arg1, arg2. */
2612 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2614 struct gcpro gcpro1
;
2615 Lisp_Object args
[3];
2621 RETURN_UNGCPRO (Ffuncall (3, args
));
2624 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2627 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2629 struct gcpro gcpro1
;
2630 Lisp_Object args
[4];
2637 RETURN_UNGCPRO (Ffuncall (4, args
));
2640 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2643 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2646 struct gcpro gcpro1
;
2647 Lisp_Object args
[5];
2655 RETURN_UNGCPRO (Ffuncall (5, args
));
2658 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2661 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2662 Lisp_Object arg4
, Lisp_Object arg5
)
2664 struct gcpro gcpro1
;
2665 Lisp_Object args
[6];
2674 RETURN_UNGCPRO (Ffuncall (6, args
));
2677 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2680 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2681 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2683 struct gcpro gcpro1
;
2684 Lisp_Object args
[7];
2694 RETURN_UNGCPRO (Ffuncall (7, args
));
2697 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2700 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2701 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2703 struct gcpro gcpro1
;
2704 Lisp_Object args
[8];
2715 RETURN_UNGCPRO (Ffuncall (8, args
));
2718 /* The caller should GCPRO all the elements of ARGS. */
2720 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2721 doc
: /* Non-nil if OBJECT is a function. */)
2722 (Lisp_Object object
)
2724 if (FUNCTIONP (object
))
2729 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2730 doc
: /* Call first argument as a function, passing remaining arguments to it.
2731 Return the value that function returns.
2732 Thus, (funcall 'cons 'x 'y) returns (x . y).
2733 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2734 (ptrdiff_t nargs
, Lisp_Object
*args
)
2736 Lisp_Object fun
, original_fun
;
2738 ptrdiff_t numargs
= nargs
- 1;
2739 Lisp_Object lisp_numargs
;
2741 struct backtrace backtrace
;
2742 register Lisp_Object
*internal_args
;
2747 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2749 if (max_lisp_eval_depth
< 100)
2750 max_lisp_eval_depth
= 100;
2751 if (lisp_eval_depth
> max_lisp_eval_depth
)
2752 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2755 backtrace
.next
= backtrace_list
;
2756 backtrace_list
= &backtrace
;
2757 backtrace
.function
= &args
[0];
2758 backtrace
.args
= &args
[1]; /* This also GCPROs them. */
2759 backtrace
.nargs
= nargs
- 1;
2760 backtrace
.debug_on_exit
= 0;
2762 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2765 if (debug_on_next_call
)
2766 do_debug_on_call (Qlambda
);
2770 original_fun
= args
[0];
2774 /* Optimize for no indirection. */
2776 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2777 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2778 fun
= indirect_function (fun
);
2782 if (numargs
< XSUBR (fun
)->min_args
2783 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2785 XSETFASTINT (lisp_numargs
, numargs
);
2786 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2789 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2790 xsignal1 (Qinvalid_function
, original_fun
);
2792 else if (XSUBR (fun
)->max_args
== MANY
)
2793 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2796 if (XSUBR (fun
)->max_args
> numargs
)
2798 internal_args
= alloca (XSUBR (fun
)->max_args
2799 * sizeof *internal_args
);
2800 memcpy (internal_args
, args
+ 1, numargs
* word_size
);
2801 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2802 internal_args
[i
] = Qnil
;
2805 internal_args
= args
+ 1;
2806 switch (XSUBR (fun
)->max_args
)
2809 val
= (XSUBR (fun
)->function
.a0 ());
2812 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2815 val
= (XSUBR (fun
)->function
.a2
2816 (internal_args
[0], internal_args
[1]));
2819 val
= (XSUBR (fun
)->function
.a3
2820 (internal_args
[0], internal_args
[1], internal_args
[2]));
2823 val
= (XSUBR (fun
)->function
.a4
2824 (internal_args
[0], internal_args
[1], internal_args
[2],
2828 val
= (XSUBR (fun
)->function
.a5
2829 (internal_args
[0], internal_args
[1], internal_args
[2],
2830 internal_args
[3], internal_args
[4]));
2833 val
= (XSUBR (fun
)->function
.a6
2834 (internal_args
[0], internal_args
[1], internal_args
[2],
2835 internal_args
[3], internal_args
[4], internal_args
[5]));
2838 val
= (XSUBR (fun
)->function
.a7
2839 (internal_args
[0], internal_args
[1], internal_args
[2],
2840 internal_args
[3], internal_args
[4], internal_args
[5],
2845 val
= (XSUBR (fun
)->function
.a8
2846 (internal_args
[0], internal_args
[1], internal_args
[2],
2847 internal_args
[3], internal_args
[4], internal_args
[5],
2848 internal_args
[6], internal_args
[7]));
2853 /* If a subr takes more than 8 arguments without using MANY
2854 or UNEVALLED, we need to extend this function to support it.
2855 Until this is done, there is no way to call the function. */
2860 else if (COMPILEDP (fun
))
2861 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2864 if (EQ (fun
, Qunbound
))
2865 xsignal1 (Qvoid_function
, original_fun
);
2867 xsignal1 (Qinvalid_function
, original_fun
);
2868 funcar
= XCAR (fun
);
2869 if (!SYMBOLP (funcar
))
2870 xsignal1 (Qinvalid_function
, original_fun
);
2871 if (EQ (funcar
, Qlambda
)
2872 || EQ (funcar
, Qclosure
))
2873 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2874 else if (EQ (funcar
, Qautoload
))
2876 Fautoload_do_load (fun
, original_fun
, Qnil
);
2881 xsignal1 (Qinvalid_function
, original_fun
);
2885 if (backtrace
.debug_on_exit
)
2886 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2887 backtrace_list
= backtrace
.next
;
2892 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2894 Lisp_Object args_left
;
2897 register Lisp_Object
*arg_vector
;
2898 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2899 register Lisp_Object tem
;
2902 numargs
= XFASTINT (Flength (args
));
2903 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2906 GCPRO3 (*arg_vector
, args_left
, fun
);
2909 for (i
= 0; i
< numargs
; )
2911 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2912 tem
= eval_sub (tem
);
2913 arg_vector
[i
++] = tem
;
2919 backtrace_list
->args
= arg_vector
;
2920 backtrace_list
->nargs
= i
;
2921 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2923 /* Do the debug-on-exit now, while arg_vector still exists. */
2924 if (backtrace_list
->debug_on_exit
)
2925 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2926 /* Don't do it again when we return to eval. */
2927 backtrace_list
->debug_on_exit
= 0;
2932 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2933 and return the result of evaluation.
2934 FUN must be either a lambda-expression or a compiled-code object. */
2937 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2938 register Lisp_Object
*arg_vector
)
2940 Lisp_Object val
, syms_left
, next
, lexenv
;
2941 ptrdiff_t count
= SPECPDL_INDEX ();
2947 if (EQ (XCAR (fun
), Qclosure
))
2949 fun
= XCDR (fun
); /* Drop `closure'. */
2950 lexenv
= XCAR (fun
);
2951 CHECK_LIST_CONS (fun
, fun
);
2955 syms_left
= XCDR (fun
);
2956 if (CONSP (syms_left
))
2957 syms_left
= XCAR (syms_left
);
2959 xsignal1 (Qinvalid_function
, fun
);
2961 else if (COMPILEDP (fun
))
2963 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2964 if (INTEGERP (syms_left
))
2965 /* A byte-code object with a non-nil `push args' slot means we
2966 shouldn't bind any arguments, instead just call the byte-code
2967 interpreter directly; it will push arguments as necessary.
2969 Byte-code objects with either a non-existent, or a nil value for
2970 the `push args' slot (the default), have dynamically-bound
2971 arguments, and use the argument-binding code below instead (as do
2972 all interpreted functions, even lexically bound ones). */
2974 /* If we have not actually read the bytecode string
2975 and constants vector yet, fetch them from the file. */
2976 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2977 Ffetch_bytecode (fun
);
2978 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2979 AREF (fun
, COMPILED_CONSTANTS
),
2980 AREF (fun
, COMPILED_STACK_DEPTH
),
2989 i
= optional
= rest
= 0;
2990 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
2994 next
= XCAR (syms_left
);
2995 if (!SYMBOLP (next
))
2996 xsignal1 (Qinvalid_function
, fun
);
2998 if (EQ (next
, Qand_rest
))
3000 else if (EQ (next
, Qand_optional
))
3007 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3011 arg
= arg_vector
[i
++];
3013 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3017 /* Bind the argument. */
3018 if (!NILP (lexenv
) && SYMBOLP (next
))
3019 /* Lexically bind NEXT by adding it to the lexenv alist. */
3020 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3022 /* Dynamically bind NEXT. */
3023 specbind (next
, arg
);
3027 if (!NILP (syms_left
))
3028 xsignal1 (Qinvalid_function
, fun
);
3030 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3032 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3033 /* Instantiate a new lexical environment. */
3034 specbind (Qinternal_interpreter_environment
, lexenv
);
3037 val
= Fprogn (XCDR (XCDR (fun
)));
3040 /* If we have not actually read the bytecode string
3041 and constants vector yet, fetch them from the file. */
3042 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3043 Ffetch_bytecode (fun
);
3044 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3045 AREF (fun
, COMPILED_CONSTANTS
),
3046 AREF (fun
, COMPILED_STACK_DEPTH
),
3050 return unbind_to (count
, val
);
3053 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3055 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3056 (Lisp_Object object
)
3060 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3062 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3065 tem
= AREF (object
, COMPILED_BYTECODE
);
3066 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3067 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3069 error ("Invalid byte code");
3071 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3072 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3080 register ptrdiff_t count
= SPECPDL_INDEX ();
3081 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3082 if (max_size
<= specpdl_size
)
3084 if (max_specpdl_size
< 400)
3085 max_size
= max_specpdl_size
= 400;
3086 if (max_size
<= specpdl_size
)
3087 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3089 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3090 specpdl_ptr
= specpdl
+ count
;
3093 /* `specpdl_ptr->symbol' is a field which describes which variable is
3094 let-bound, so it can be properly undone when we unbind_to.
3095 It can have the following two shapes:
3096 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3097 a symbol that is not buffer-local (at least at the time
3098 the let binding started). Note also that it should not be
3099 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3101 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3102 variable SYMBOL which can be buffer-local. WHERE tells us
3103 which buffer is affected (or nil if the let-binding affects the
3104 global value of the variable) and BUFFER tells us which buffer was
3105 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3106 BUFFER did not yet have a buffer-local value). */
3109 specbind (Lisp_Object symbol
, Lisp_Object value
)
3111 struct Lisp_Symbol
*sym
;
3113 eassert (!handling_signal
);
3115 CHECK_SYMBOL (symbol
);
3116 sym
= XSYMBOL (symbol
);
3117 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3121 switch (sym
->redirect
)
3123 case SYMBOL_VARALIAS
:
3124 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3125 case SYMBOL_PLAINVAL
:
3126 /* The most common case is that of a non-constant symbol with a
3127 trivial value. Make that as fast as we can. */
3128 set_specpdl_symbol (symbol
);
3129 set_specpdl_old_value (SYMBOL_VAL (sym
));
3130 specpdl_ptr
->func
= NULL
;
3133 SET_SYMBOL_VAL (sym
, value
);
3135 set_internal (symbol
, value
, Qnil
, 1);
3137 case SYMBOL_LOCALIZED
:
3138 if (SYMBOL_BLV (sym
)->frame_local
)
3139 error ("Frame-local vars cannot be let-bound");
3140 case SYMBOL_FORWARDED
:
3142 Lisp_Object ovalue
= find_symbol_value (symbol
);
3143 specpdl_ptr
->func
= 0;
3144 set_specpdl_old_value (ovalue
);
3146 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3147 || (EQ (SYMBOL_BLV (sym
)->where
,
3148 SYMBOL_BLV (sym
)->frame_local
?
3149 Fselected_frame () : Fcurrent_buffer ())));
3151 if (sym
->redirect
== SYMBOL_LOCALIZED
3152 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3154 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3156 /* For a local variable, record both the symbol and which
3157 buffer's or frame's value we are saving. */
3158 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3160 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3161 || (blv_found (SYMBOL_BLV (sym
))
3162 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3165 else if (sym
->redirect
== SYMBOL_LOCALIZED
3166 && blv_found (SYMBOL_BLV (sym
)))
3167 where
= SYMBOL_BLV (sym
)->where
;
3171 /* We're not using the `unused' slot in the specbinding
3172 structure because this would mean we have to do more
3173 work for simple variables. */
3174 /* FIXME: The third value `current_buffer' is only used in
3175 let_shadows_buffer_binding_p which is itself only used
3176 in set_internal for local_if_set. */
3177 eassert (NILP (where
) || EQ (where
, cur_buf
));
3178 set_specpdl_symbol (Fcons (symbol
, Fcons (where
, cur_buf
)));
3180 /* If SYMBOL is a per-buffer variable which doesn't have a
3181 buffer-local value here, make the `let' change the global
3182 value by changing the value of SYMBOL in all buffers not
3183 having their own value. This is consistent with what
3184 happens with other buffer-local variables. */
3186 && sym
->redirect
== SYMBOL_FORWARDED
)
3188 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3190 Fset_default (symbol
, value
);
3195 set_specpdl_symbol (symbol
);
3198 set_internal (symbol
, value
, Qnil
, 1);
3206 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3208 eassert (!handling_signal
);
3210 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3212 specpdl_ptr
->func
= function
;
3213 set_specpdl_symbol (Qnil
);
3214 set_specpdl_old_value (arg
);
3219 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3221 Lisp_Object quitf
= Vquit_flag
;
3222 struct gcpro gcpro1
, gcpro2
;
3224 GCPRO2 (value
, quitf
);
3227 while (specpdl_ptr
!= specpdl
+ count
)
3229 /* Copy the binding, and decrement specpdl_ptr, before we do
3230 the work to unbind it. We decrement first
3231 so that an error in unbinding won't try to unbind
3232 the same entry again, and we copy the binding first
3233 in case more bindings are made during some of the code we run. */
3235 struct specbinding this_binding
;
3236 this_binding
= *--specpdl_ptr
;
3238 if (this_binding
.func
!= 0)
3239 (*this_binding
.func
) (this_binding
.old_value
);
3240 /* If the symbol is a list, it is really (SYMBOL WHERE
3241 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3242 frame. If WHERE is a buffer or frame, this indicates we
3243 bound a variable that had a buffer-local or frame-local
3244 binding. WHERE nil means that the variable had the default
3245 value when it was bound. CURRENT-BUFFER is the buffer that
3246 was current when the variable was bound. */
3247 else if (CONSP (this_binding
.symbol
))
3249 Lisp_Object symbol
, where
;
3251 symbol
= XCAR (this_binding
.symbol
);
3252 where
= XCAR (XCDR (this_binding
.symbol
));
3255 Fset_default (symbol
, this_binding
.old_value
);
3256 /* If `where' is non-nil, reset the value in the appropriate
3257 local binding, but only if that binding still exists. */
3258 else if (BUFFERP (where
)
3259 ? !NILP (Flocal_variable_p (symbol
, where
))
3260 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3261 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3263 /* If variable has a trivial value (no forwarding), we can
3264 just set it. No need to check for constant symbols here,
3265 since that was already done by specbind. */
3266 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3267 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3268 this_binding
.old_value
);
3270 /* NOTE: we only ever come here if make_local_foo was used for
3271 the first time on this var within this let. */
3272 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3275 if (NILP (Vquit_flag
) && !NILP (quitf
))
3282 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3283 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3284 A special variable is one that will be bound dynamically, even in a
3285 context where binding is lexical by default. */)
3286 (Lisp_Object symbol
)
3288 CHECK_SYMBOL (symbol
);
3289 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3293 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3294 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3295 The debugger is entered when that frame exits, if the flag is non-nil. */)
3296 (Lisp_Object level
, Lisp_Object flag
)
3298 register struct backtrace
*backlist
= backtrace_list
;
3299 register EMACS_INT i
;
3301 CHECK_NUMBER (level
);
3303 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3305 backlist
= backlist
->next
;
3309 backlist
->debug_on_exit
= !NILP (flag
);
3314 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3315 doc
: /* Print a trace of Lisp function calls currently active.
3316 Output stream used is value of `standard-output'. */)
3319 register struct backtrace
*backlist
= backtrace_list
;
3322 struct gcpro gcpro1
;
3323 Lisp_Object old_print_level
= Vprint_level
;
3325 if (NILP (Vprint_level
))
3326 XSETFASTINT (Vprint_level
, 8);
3333 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3334 if (backlist
->nargs
== UNEVALLED
)
3336 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3337 write_string ("\n", -1);
3341 tem
= *backlist
->function
;
3342 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3343 write_string ("(", -1);
3344 if (backlist
->nargs
== MANY
)
3345 { /* FIXME: Can this happen? */
3347 for (tail
= *backlist
->args
, i
= 0;
3349 tail
= Fcdr (tail
), i
= 1)
3351 if (i
) write_string (" ", -1);
3352 Fprin1 (Fcar (tail
), Qnil
);
3358 for (i
= 0; i
< backlist
->nargs
; i
++)
3360 if (i
) write_string (" ", -1);
3361 Fprin1 (backlist
->args
[i
], Qnil
);
3364 write_string (")\n", -1);
3366 backlist
= backlist
->next
;
3369 Vprint_level
= old_print_level
;
3374 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3375 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3376 If that frame has not evaluated the arguments yet (or is a special form),
3377 the value is (nil FUNCTION ARG-FORMS...).
3378 If that frame has evaluated its arguments and called its function already,
3379 the value is (t FUNCTION ARG-VALUES...).
3380 A &rest arg is represented as the tail of the list ARG-VALUES.
3381 FUNCTION is whatever was supplied as car of evaluated list,
3382 or a lambda expression for macro calls.
3383 If NFRAMES is more than the number of frames, the value is nil. */)
3384 (Lisp_Object nframes
)
3386 register struct backtrace
*backlist
= backtrace_list
;
3387 register EMACS_INT i
;
3390 CHECK_NATNUM (nframes
);
3392 /* Find the frame requested. */
3393 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3394 backlist
= backlist
->next
;
3398 if (backlist
->nargs
== UNEVALLED
)
3399 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3402 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3403 tem
= *backlist
->args
;
3405 tem
= Flist (backlist
->nargs
, backlist
->args
);
3407 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3414 mark_backtrace (void)
3416 register struct backtrace
*backlist
;
3419 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3421 mark_object (*backlist
->function
);
3423 if (backlist
->nargs
== UNEVALLED
3424 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3427 i
= backlist
->nargs
;
3429 mark_object (backlist
->args
[i
]);
3437 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3438 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3439 If Lisp code tries to increase the total number past this amount,
3440 an error is signaled.
3441 You can safely use a value considerably larger than the default value,
3442 if that proves inconveniently small. However, if you increase it too far,
3443 Emacs could run out of memory trying to make the stack bigger. */);
3445 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3446 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3448 This limit serves to catch infinite recursions for you before they cause
3449 actual stack overflow in C, which would be fatal for Emacs.
3450 You can safely make it considerably larger than its default value,
3451 if that proves inconveniently small. However, if you increase it too far,
3452 Emacs could overflow the real C stack, and crash. */);
3454 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3455 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3456 If the value is t, that means do an ordinary quit.
3457 If the value equals `throw-on-input', that means quit by throwing
3458 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3459 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3460 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3463 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3464 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3465 Note that `quit-flag' will still be set by typing C-g,
3466 so a quit will be signaled as soon as `inhibit-quit' is nil.
3467 To prevent this happening, set `quit-flag' to nil
3468 before making `inhibit-quit' nil. */);
3469 Vinhibit_quit
= Qnil
;
3471 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3472 DEFSYM (Qautoload
, "autoload");
3473 DEFSYM (Qdebug_on_error
, "debug-on-error");
3474 DEFSYM (Qmacro
, "macro");
3475 DEFSYM (Qdeclare
, "declare");
3477 /* Note that the process handling also uses Qexit, but we don't want
3478 to staticpro it twice, so we just do it here. */
3479 DEFSYM (Qexit
, "exit");
3481 DEFSYM (Qinteractive
, "interactive");
3482 DEFSYM (Qcommandp
, "commandp");
3483 DEFSYM (Qand_rest
, "&rest");
3484 DEFSYM (Qand_optional
, "&optional");
3485 DEFSYM (Qclosure
, "closure");
3486 DEFSYM (Qdebug
, "debug");
3488 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3489 doc
: /* Non-nil means enter debugger if an error is signaled.
3490 Does not apply to errors handled by `condition-case' or those
3491 matched by `debug-ignored-errors'.
3492 If the value is a list, an error only means to enter the debugger
3493 if one of its condition symbols appears in the list.
3494 When you evaluate an expression interactively, this variable
3495 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3496 The command `toggle-debug-on-error' toggles this.
3497 See also the variable `debug-on-quit'. */);
3498 Vdebug_on_error
= Qnil
;
3500 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3501 doc
: /* List of errors for which the debugger should not be called.
3502 Each element may be a condition-name or a regexp that matches error messages.
3503 If any element applies to a given error, that error skips the debugger
3504 and just returns to top level.
3505 This overrides the variable `debug-on-error'.
3506 It does not apply to errors handled by `condition-case'. */);
3507 Vdebug_ignored_errors
= Qnil
;
3509 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3510 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3511 Does not apply if quit is handled by a `condition-case'. */);
3514 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3515 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3517 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3518 doc
: /* Non-nil means debugger may continue execution.
3519 This is nil when the debugger is called under circumstances where it
3520 might not be safe to continue. */);
3521 debugger_may_continue
= 1;
3523 DEFVAR_LISP ("debugger", Vdebugger
,
3524 doc
: /* Function to call to invoke debugger.
3525 If due to frame exit, args are `exit' and the value being returned;
3526 this function's value will be returned instead of that.
3527 If due to error, args are `error' and a list of the args to `signal'.
3528 If due to `apply' or `funcall' entry, one arg, `lambda'.
3529 If due to `eval' entry, one arg, t. */);
3532 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3533 doc
: /* If non-nil, this is a function for `signal' to call.
3534 It receives the same arguments that `signal' was given.
3535 The Edebug package uses this to regain control. */);
3536 Vsignal_hook_function
= Qnil
;
3538 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3539 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3540 Note that `debug-on-error', `debug-on-quit' and friends
3541 still determine whether to handle the particular condition. */);
3542 Vdebug_on_signal
= Qnil
;
3544 /* When lexical binding is being used,
3545 Vinternal_interpreter_environment is non-nil, and contains an alist
3546 of lexically-bound variable, or (t), indicating an empty
3547 environment. The lisp name of this variable would be
3548 `internal-interpreter-environment' if it weren't hidden.
3549 Every element of this list can be either a cons (VAR . VAL)
3550 specifying a lexical binding, or a single symbol VAR indicating
3551 that this variable should use dynamic scoping. */
3552 DEFSYM (Qinternal_interpreter_environment
,
3553 "internal-interpreter-environment");
3554 DEFVAR_LISP ("internal-interpreter-environment",
3555 Vinternal_interpreter_environment
,
3556 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3557 When lexical binding is not being used, this variable is nil.
3558 A value of `(t)' indicates an empty environment, otherwise it is an
3559 alist of active lexical bindings. */);
3560 Vinternal_interpreter_environment
= Qnil
;
3561 /* Don't export this variable to Elisp, so no one can mess with it
3562 (Just imagine if someone makes it buffer-local). */
3563 Funintern (Qinternal_interpreter_environment
, Qnil
);
3565 DEFSYM (Vrun_hooks
, "run-hooks");
3567 staticpro (&Vautoload_queue
);
3568 Vautoload_queue
= Qnil
;
3569 staticpro (&Vsignaling_function
);
3570 Vsignaling_function
= Qnil
;
3572 inhibit_lisp_code
= Qnil
;
3583 defsubr (&Sfunction
);
3585 defsubr (&Sdefvaralias
);
3586 defsubr (&Sdefconst
);
3587 defsubr (&Smake_var_non_special
);
3591 defsubr (&Smacroexpand
);
3594 defsubr (&Sunwind_protect
);
3595 defsubr (&Scondition_case
);
3597 defsubr (&Sinteractive_p
);
3598 defsubr (&Scalled_interactively_p
);
3599 defsubr (&Scommandp
);
3600 defsubr (&Sautoload
);
3601 defsubr (&Sautoload_do_load
);
3604 defsubr (&Sfuncall
);
3605 defsubr (&Srun_hooks
);
3606 defsubr (&Srun_hook_with_args
);
3607 defsubr (&Srun_hook_with_args_until_success
);
3608 defsubr (&Srun_hook_with_args_until_failure
);
3609 defsubr (&Srun_hook_wrapped
);
3610 defsubr (&Sfetch_bytecode
);
3611 defsubr (&Sbacktrace_debug
);
3612 defsubr (&Sbacktrace
);
3613 defsubr (&Sbacktrace_frame
);
3614 defsubr (&Sspecial_variable_p
);
3615 defsubr (&Sfunctionp
);