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 void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
135 static int interactive_p (int);
136 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
137 static Lisp_Object
Ffetch_bytecode (Lisp_Object
);
140 init_eval_once (void)
143 specpdl
= (struct specbinding
*) xmalloc (size
* sizeof (struct specbinding
));
145 specpdl_ptr
= specpdl
;
146 /* Don't forget to update docs (lispref node "Local Variables"). */
147 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
148 max_lisp_eval_depth
= 600;
156 specpdl_ptr
= specpdl
;
161 debug_on_next_call
= 0;
166 /* This is less than the initial value of num_nonmacro_input_events. */
167 when_entered_debugger
= -1;
170 /* Unwind-protect function used by call_debugger. */
173 restore_stack_limits (Lisp_Object data
)
175 max_specpdl_size
= XINT (XCAR (data
));
176 max_lisp_eval_depth
= XINT (XCDR (data
));
180 /* Call the Lisp debugger, giving it argument ARG. */
183 call_debugger (Lisp_Object arg
)
185 int debug_while_redisplaying
;
186 ptrdiff_t count
= SPECPDL_INDEX ();
188 EMACS_INT old_max
= max_specpdl_size
;
190 /* Temporarily bump up the stack limits,
191 so the debugger won't run out of stack. */
193 max_specpdl_size
+= 1;
194 record_unwind_protect (restore_stack_limits
,
195 Fcons (make_number (old_max
),
196 make_number (max_lisp_eval_depth
)));
197 max_specpdl_size
= old_max
;
199 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
200 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
202 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
203 max_specpdl_size
= SPECPDL_INDEX () + 100;
205 #ifdef HAVE_WINDOW_SYSTEM
206 if (display_hourglass_p
)
210 debug_on_next_call
= 0;
211 when_entered_debugger
= num_nonmacro_input_events
;
213 /* Resetting redisplaying_p to 0 makes sure that debug output is
214 displayed if the debugger is invoked during redisplay. */
215 debug_while_redisplaying
= redisplaying_p
;
217 specbind (intern ("debugger-may-continue"),
218 debug_while_redisplaying
? Qnil
: Qt
);
219 specbind (Qinhibit_redisplay
, Qnil
);
220 specbind (Qdebug_on_error
, Qnil
);
222 #if 0 /* Binding this prevents execution of Lisp code during
223 redisplay, which necessarily leads to display problems. */
224 specbind (Qinhibit_eval_during_redisplay
, Qt
);
227 val
= apply1 (Vdebugger
, arg
);
229 /* Interrupting redisplay and resuming it later is not safe under
230 all circumstances. So, when the debugger returns, abort the
231 interrupted redisplay by going back to the top-level. */
232 if (debug_while_redisplaying
)
235 return unbind_to (count
, val
);
239 do_debug_on_call (Lisp_Object code
)
241 debug_on_next_call
= 0;
242 backtrace_list
->debug_on_exit
= 1;
243 call_debugger (Fcons (code
, Qnil
));
246 /* NOTE!!! Every function that can call EVAL must protect its args
247 and temporaries from garbage collection while it needs them.
248 The definition of `For' shows what you have to do. */
250 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
251 doc
: /* Eval args until one of them yields non-nil, then return that value.
252 The remaining args are not evalled at all.
253 If all args return nil, return nil.
254 usage: (or CONDITIONS...) */)
257 register Lisp_Object val
= Qnil
;
264 val
= eval_sub (XCAR (args
));
274 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
275 doc
: /* Eval args until one of them yields nil, then return nil.
276 The remaining args are not evalled at all.
277 If no arg yields nil, return the last arg's value.
278 usage: (and CONDITIONS...) */)
281 register Lisp_Object val
= Qt
;
288 val
= eval_sub (XCAR (args
));
298 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
299 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
300 Returns the value of THEN or the value of the last of the ELSE's.
301 THEN must be one expression, but ELSE... can be zero or more expressions.
302 If COND yields nil, and there are no ELSE's, the value is nil.
303 usage: (if COND THEN ELSE...) */)
306 register Lisp_Object cond
;
310 cond
= eval_sub (Fcar (args
));
314 return eval_sub (Fcar (Fcdr (args
)));
315 return Fprogn (Fcdr (Fcdr (args
)));
318 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
319 doc
: /* Try each clause until one succeeds.
320 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
321 and, if the value is non-nil, this clause succeeds:
322 then the expressions in BODY are evaluated and the last one's
323 value is the value of the cond-form.
324 If no clause succeeds, cond returns nil.
325 If a clause has one element, as in (CONDITION),
326 CONDITION's value if non-nil is returned from the cond-form.
327 usage: (cond CLAUSES...) */)
330 register Lisp_Object clause
, val
;
337 clause
= Fcar (args
);
338 val
= eval_sub (Fcar (clause
));
341 if (!EQ (XCDR (clause
), Qnil
))
342 val
= Fprogn (XCDR (clause
));
352 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
353 doc
: /* Eval BODY forms sequentially and return value of last one.
354 usage: (progn BODY...) */)
357 register Lisp_Object val
= Qnil
;
364 val
= eval_sub (XCAR (args
));
372 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
373 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
374 The value of FIRST is saved during the evaluation of the remaining args,
375 whose values are discarded.
376 usage: (prog1 FIRST BODY...) */)
380 register Lisp_Object args_left
;
381 struct gcpro gcpro1
, gcpro2
;
387 val
= eval_sub (XCAR (args_left
));
388 while (CONSP (args_left
= XCDR (args_left
)))
389 eval_sub (XCAR (args_left
));
395 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
396 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
397 The value of FORM2 is saved during the evaluation of the
398 remaining args, whose values are discarded.
399 usage: (prog2 FORM1 FORM2 BODY...) */)
405 eval_sub (XCAR (args
));
407 return Fprog1 (XCDR (args
));
410 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
411 doc
: /* Set each SYM to the value of its VAL.
412 The symbols SYM are variables; they are literal (not evaluated).
413 The values VAL are expressions; they are evaluated.
414 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
415 The second VAL is not computed until after the first SYM is set, and so on;
416 each VAL can use the new value of variables set earlier in the `setq'.
417 The return value of the `setq' form is the value of the last VAL.
418 usage: (setq [SYM VAL]...) */)
421 register Lisp_Object args_left
;
422 register Lisp_Object val
, sym
, lex_binding
;
433 val
= eval_sub (Fcar (Fcdr (args_left
)));
434 sym
= Fcar (args_left
);
436 /* Like for eval_sub, we do not check declared_special here since
437 it's been done when let-binding. */
438 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
440 && !NILP (lex_binding
441 = Fassq (sym
, Vinternal_interpreter_environment
)))
442 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
444 Fset (sym
, val
); /* SYM is dynamically bound. */
446 args_left
= Fcdr (Fcdr (args_left
));
448 while (!NILP (args_left
));
454 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
455 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
456 Warning: `quote' does not construct its return value, but just returns
457 the value that was pre-constructed by the Lisp reader (see info node
458 `(elisp)Printed Representation').
459 This means that '(a . b) is not identical to (cons 'a 'b): the former
460 does not cons. Quoting should be reserved for constants that will
461 never be modified by side-effects, unless you like self-modifying code.
462 See the common pitfall in info node `(elisp)Rearrangement' for an example
463 of unexpected results when a quoted object is modified.
464 usage: (quote ARG) */)
467 if (!NILP (Fcdr (args
)))
468 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
472 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
473 doc
: /* Like `quote', but preferred for objects which are functions.
474 In byte compilation, `function' causes its argument to be compiled.
475 `quote' cannot do that.
476 usage: (function ARG) */)
479 Lisp_Object quoted
= XCAR (args
);
481 if (!NILP (Fcdr (args
)))
482 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
484 if (!NILP (Vinternal_interpreter_environment
)
486 && EQ (XCAR (quoted
), Qlambda
))
487 /* This is a lambda expression within a lexical environment;
488 return an interpreted closure instead of a simple lambda. */
489 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
492 /* Simply quote the argument. */
497 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
498 doc
: /* Return t if the containing function was run directly by user input.
499 This means that the function was called with `call-interactively'
500 \(which includes being called as the binding of a key)
501 and input is currently coming from the keyboard (not a keyboard macro),
502 and Emacs is not running in batch mode (`noninteractive' is nil).
504 The only known proper use of `interactive-p' is in deciding whether to
505 display a helpful message, or how to display it. If you're thinking
506 of using it for any other purpose, it is quite likely that you're
507 making a mistake. Think: what do you want to do when the command is
508 called from a keyboard macro?
510 To test whether your function was called with `call-interactively',
511 either (i) add an extra optional argument and give it an `interactive'
512 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
513 use `called-interactively-p'. */)
516 return interactive_p (1) ? Qt
: Qnil
;
520 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
521 doc
: /* Return t if the containing function was called by `call-interactively'.
522 If KIND is `interactive', then only return t if the call was made
523 interactively by the user, i.e. not in `noninteractive' mode nor
524 when `executing-kbd-macro'.
525 If KIND is `any', on the other hand, it will return t for any kind of
526 interactive call, including being called as the binding of a key, or
527 from a keyboard macro, or in `noninteractive' mode.
529 The only known proper use of `interactive' for KIND is in deciding
530 whether to display a helpful message, or how to display it. If you're
531 thinking of using it for any other purpose, it is quite likely that
532 you're making a mistake. Think: what do you want to do when the
533 command is called from a keyboard macro?
535 This function is meant for implementing advice and other
536 function-modifying features. Instead of using this, it is sometimes
537 cleaner to give your function an extra optional argument whose
538 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
539 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
542 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
543 && interactive_p (1)) ? Qt
: Qnil
;
547 /* Return 1 if function in which this appears was called using
550 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
551 called is a built-in. */
554 interactive_p (int exclude_subrs_p
)
556 struct backtrace
*btp
;
559 btp
= backtrace_list
;
561 /* If this isn't a byte-compiled function, there may be a frame at
562 the top for Finteractive_p. If so, skip it. */
563 fun
= Findirect_function (*btp
->function
, Qnil
);
564 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
565 || XSUBR (fun
) == &Scalled_interactively_p
))
568 /* If we're running an Emacs 18-style byte-compiled function, there
569 may be a frame for Fbytecode at the top level. In any version of
570 Emacs there can be Fbytecode frames for subexpressions evaluated
571 inside catch and condition-case. Skip past them.
573 If this isn't a byte-compiled function, then we may now be
574 looking at several frames for special forms. Skip past them. */
576 && (EQ (*btp
->function
, Qbytecode
)
577 || btp
->nargs
== UNEVALLED
))
580 /* `btp' now points at the frame of the innermost function that isn't
581 a special form, ignoring frames for Finteractive_p and/or
582 Fbytecode at the top. If this frame is for a built-in function
583 (such as load or eval-region) return nil. */
584 fun
= Findirect_function (*btp
->function
, Qnil
);
585 if (exclude_subrs_p
&& SUBRP (fun
))
588 /* `btp' points to the frame of a Lisp function that called interactive-p.
589 Return t if that function was called interactively. */
590 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
596 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
597 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
598 Aliased variables always have the same value; setting one sets the other.
599 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
600 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
601 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
602 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
603 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
604 The return value is BASE-VARIABLE. */)
605 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
607 struct Lisp_Symbol
*sym
;
609 CHECK_SYMBOL (new_alias
);
610 CHECK_SYMBOL (base_variable
);
612 sym
= XSYMBOL (new_alias
);
615 /* Not sure why, but why not? */
616 error ("Cannot make a constant an alias");
618 switch (sym
->redirect
)
620 case SYMBOL_FORWARDED
:
621 error ("Cannot make an internal variable an alias");
622 case SYMBOL_LOCALIZED
:
623 error ("Don't know how to make a localized variable an alias");
626 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
627 If n_a is bound, but b_v is not, set the value of b_v to n_a,
628 so that old-code that affects n_a before the aliasing is setup
630 if (NILP (Fboundp (base_variable
)))
631 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
634 struct specbinding
*p
;
636 for (p
= specpdl_ptr
; p
> specpdl
; )
637 if ((--p
)->func
== NULL
639 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
640 error ("Don't know how to make a let-bound variable an alias");
643 sym
->declared_special
= 1;
644 XSYMBOL (base_variable
)->declared_special
= 1;
645 sym
->redirect
= SYMBOL_VARALIAS
;
646 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
647 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
648 LOADHIST_ATTACH (new_alias
);
649 /* Even if docstring is nil: remove old docstring. */
650 Fput (new_alias
, Qvariable_documentation
, docstring
);
652 return base_variable
;
656 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
657 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
658 You are not required to define a variable in order to use it, but
659 defining it lets you supply an initial value and documentation, which
660 can be referred to by the Emacs help facilities and other programming
661 tools. The `defvar' form also declares the variable as \"special\",
662 so that it is always dynamically bound even if `lexical-binding' is t.
664 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
665 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
666 default value is what is set; buffer-local values are not affected.
667 If INITVALUE is missing, SYMBOL's value is not set.
669 If SYMBOL has a local binding, then this form affects the local
670 binding. This is usually not what you want. Thus, if you need to
671 load a file defining variables, with this form or with `defconst' or
672 `defcustom', you should always load that file _outside_ any bindings
673 for these variables. \(`defconst' and `defcustom' behave similarly in
676 The optional argument DOCSTRING is a documentation string for the
679 To define a user option, use `defcustom' instead of `defvar'.
680 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
683 register Lisp_Object sym
, tem
, tail
;
687 if (!NILP (Fcdr (Fcdr (tail
))))
688 error ("Too many arguments");
690 tem
= Fdefault_boundp (sym
);
693 /* Do it before evaluating the initial value, for self-references. */
694 XSYMBOL (sym
)->declared_special
= 1;
696 if (SYMBOL_CONSTANT_P (sym
))
698 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
699 Lisp_Object tem1
= Fcar (tail
);
701 && EQ (XCAR (tem1
), Qquote
)
702 && CONSP (XCDR (tem1
))
703 && EQ (XCAR (XCDR (tem1
)), sym
)))
704 error ("Constant symbol `%s' specified in defvar",
705 SDATA (SYMBOL_NAME (sym
)));
709 Fset_default (sym
, eval_sub (Fcar (tail
)));
711 { /* Check if there is really a global binding rather than just a let
712 binding that shadows the global unboundness of the var. */
713 volatile struct specbinding
*pdl
= specpdl_ptr
;
714 while (pdl
> specpdl
)
716 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
717 && EQ (pdl
->old_value
, Qunbound
))
719 message_with_string ("Warning: defvar ignored because %s is let-bound",
720 SYMBOL_NAME (sym
), 1);
729 if (!NILP (Vpurify_flag
))
730 tem
= Fpurecopy (tem
);
731 Fput (sym
, Qvariable_documentation
, tem
);
733 LOADHIST_ATTACH (sym
);
735 else if (!NILP (Vinternal_interpreter_environment
)
736 && !XSYMBOL (sym
)->declared_special
)
737 /* A simple (defvar foo) with lexical scoping does "nothing" except
738 declare that var to be dynamically scoped *locally* (i.e. within
739 the current file or let-block). */
740 Vinternal_interpreter_environment
=
741 Fcons (sym
, Vinternal_interpreter_environment
);
744 /* Simple (defvar <var>) should not count as a definition at all.
745 It could get in the way of other definitions, and unloading this
746 package could try to make the variable unbound. */
752 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
753 doc
: /* Define SYMBOL as a constant variable.
754 This declares that neither programs nor users should ever change the
755 value. This constancy is not actually enforced by Emacs Lisp, but
756 SYMBOL is marked as a special variable so that it is never lexically
759 The `defconst' form always sets the value of SYMBOL to the result of
760 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
761 what is set; buffer-local values are not affected. If SYMBOL has a
762 local binding, then this form sets the local binding's value.
763 However, you should normally not make local bindings for variables
764 defined with this form.
766 The optional DOCSTRING specifies the variable's documentation string.
767 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
770 register Lisp_Object sym
, tem
;
773 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
774 error ("Too many arguments");
776 tem
= eval_sub (Fcar (Fcdr (args
)));
777 if (!NILP (Vpurify_flag
))
778 tem
= Fpurecopy (tem
);
779 Fset_default (sym
, tem
);
780 XSYMBOL (sym
)->declared_special
= 1;
781 tem
= Fcar (Fcdr (Fcdr (args
)));
784 if (!NILP (Vpurify_flag
))
785 tem
= Fpurecopy (tem
);
786 Fput (sym
, Qvariable_documentation
, tem
);
788 Fput (sym
, Qrisky_local_variable
, Qt
);
789 LOADHIST_ATTACH (sym
);
793 /* Make SYMBOL lexically scoped. */
794 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
795 Smake_var_non_special
, 1, 1, 0,
796 doc
: /* Internal function. */)
799 CHECK_SYMBOL (symbol
);
800 XSYMBOL (symbol
)->declared_special
= 0;
805 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
806 doc
: /* Bind variables according to VARLIST then eval BODY.
807 The value of the last form in BODY is returned.
808 Each element of VARLIST is a symbol (which is bound to nil)
809 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
810 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
811 usage: (let* VARLIST BODY...) */)
814 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
815 ptrdiff_t count
= SPECPDL_INDEX ();
816 struct gcpro gcpro1
, gcpro2
, gcpro3
;
818 GCPRO3 (args
, elt
, varlist
);
820 lexenv
= Vinternal_interpreter_environment
;
822 varlist
= Fcar (args
);
823 while (CONSP (varlist
))
827 elt
= XCAR (varlist
);
833 else if (! NILP (Fcdr (Fcdr (elt
))))
834 signal_error ("`let' bindings can have only one value-form", elt
);
838 val
= eval_sub (Fcar (Fcdr (elt
)));
841 if (!NILP (lexenv
) && SYMBOLP (var
)
842 && !XSYMBOL (var
)->declared_special
843 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
844 /* Lexically bind VAR by adding it to the interpreter's binding
848 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
849 if (EQ (Vinternal_interpreter_environment
, lexenv
))
850 /* Save the old lexical environment on the specpdl stack,
851 but only for the first lexical binding, since we'll never
852 need to revert to one of the intermediate ones. */
853 specbind (Qinternal_interpreter_environment
, newenv
);
855 Vinternal_interpreter_environment
= newenv
;
860 varlist
= XCDR (varlist
);
863 val
= Fprogn (Fcdr (args
));
864 return unbind_to (count
, val
);
867 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
868 doc
: /* Bind variables according to VARLIST then eval BODY.
869 The value of the last form in BODY is returned.
870 Each element of VARLIST is a symbol (which is bound to nil)
871 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
872 All the VALUEFORMs are evalled before any symbols are bound.
873 usage: (let VARLIST BODY...) */)
876 Lisp_Object
*temps
, tem
, lexenv
;
877 register Lisp_Object elt
, varlist
;
878 ptrdiff_t count
= SPECPDL_INDEX ();
880 struct gcpro gcpro1
, gcpro2
;
883 varlist
= Fcar (args
);
885 /* Make space to hold the values to give the bound variables. */
886 elt
= Flength (varlist
);
887 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
889 /* Compute the values and store them in `temps'. */
891 GCPRO2 (args
, *temps
);
894 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
897 elt
= XCAR (varlist
);
899 temps
[argnum
++] = Qnil
;
900 else if (! NILP (Fcdr (Fcdr (elt
))))
901 signal_error ("`let' bindings can have only one value-form", elt
);
903 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
904 gcpro2
.nvars
= argnum
;
908 lexenv
= Vinternal_interpreter_environment
;
910 varlist
= Fcar (args
);
911 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
915 elt
= XCAR (varlist
);
916 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
917 tem
= temps
[argnum
++];
919 if (!NILP (lexenv
) && SYMBOLP (var
)
920 && !XSYMBOL (var
)->declared_special
921 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
922 /* Lexically bind VAR by adding it to the lexenv alist. */
923 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
925 /* Dynamically bind VAR. */
929 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
930 /* Instantiate a new lexical environment. */
931 specbind (Qinternal_interpreter_environment
, lexenv
);
933 elt
= Fprogn (Fcdr (args
));
935 return unbind_to (count
, elt
);
938 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
939 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
940 The order of execution is thus TEST, BODY, TEST, BODY and so on
941 until TEST returns nil.
942 usage: (while TEST BODY...) */)
945 Lisp_Object test
, body
;
946 struct gcpro gcpro1
, gcpro2
;
952 while (!NILP (eval_sub (test
)))
962 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
963 doc
: /* Return result of expanding macros at top level of FORM.
964 If FORM is not a macro call, it is returned unchanged.
965 Otherwise, the macro is expanded and the expansion is considered
966 in place of FORM. When a non-macro-call results, it is returned.
968 The second optional arg ENVIRONMENT specifies an environment of macro
969 definitions to shadow the loaded ones for use in file byte-compilation. */)
970 (Lisp_Object form
, Lisp_Object environment
)
972 /* With cleanups from Hallvard Furuseth. */
973 register Lisp_Object expander
, sym
, def
, tem
;
977 /* Come back here each time we expand a macro call,
978 in case it expands into another macro call. */
981 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
982 def
= sym
= XCAR (form
);
984 /* Trace symbols aliases to other symbols
985 until we get a symbol that is not an alias. */
986 while (SYMBOLP (def
))
990 tem
= Fassq (sym
, environment
);
993 def
= XSYMBOL (sym
)->function
;
994 if (!EQ (def
, Qunbound
))
999 /* Right now TEM is the result from SYM in ENVIRONMENT,
1000 and if TEM is nil then DEF is SYM's function definition. */
1003 /* SYM is not mentioned in ENVIRONMENT.
1004 Look at its function definition. */
1005 if (EQ (def
, Qunbound
) || !CONSP (def
))
1006 /* Not defined or definition not suitable. */
1008 if (EQ (XCAR (def
), Qautoload
))
1010 /* Autoloading function: will it be a macro when loaded? */
1011 tem
= Fnth (make_number (4), def
);
1012 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1013 /* Yes, load it and try again. */
1015 struct gcpro gcpro1
;
1017 do_autoload (def
, sym
);
1024 else if (!EQ (XCAR (def
), Qmacro
))
1026 else expander
= XCDR (def
);
1030 expander
= XCDR (tem
);
1031 if (NILP (expander
))
1035 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1036 if (EQ (form
, newform
))
1045 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1046 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1047 TAG is evalled to get the tag to use; it must not be nil.
1049 Then the BODY is executed.
1050 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1051 If no throw happens, `catch' returns the value of the last BODY form.
1052 If a throw happens, it specifies the value to return from `catch'.
1053 usage: (catch TAG BODY...) */)
1056 register Lisp_Object tag
;
1057 struct gcpro gcpro1
;
1060 tag
= eval_sub (Fcar (args
));
1062 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1065 /* Set up a catch, then call C function FUNC on argument ARG.
1066 FUNC should return a Lisp_Object.
1067 This is how catches are done from within C code. */
1070 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1072 /* This structure is made part of the chain `catchlist'. */
1075 /* Fill in the components of c, and put it on the list. */
1079 c
.backlist
= backtrace_list
;
1080 c
.handlerlist
= handlerlist
;
1081 c
.lisp_eval_depth
= lisp_eval_depth
;
1082 c
.pdlcount
= SPECPDL_INDEX ();
1083 c
.poll_suppress_count
= poll_suppress_count
;
1084 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1085 c
.gcpro
= gcprolist
;
1086 c
.byte_stack
= byte_stack_list
;
1090 if (! _setjmp (c
.jmp
))
1091 c
.val
= (*func
) (arg
);
1093 /* Throw works by a longjmp that comes right here. */
1098 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1099 jump to that CATCH, returning VALUE as the value of that catch.
1101 This is the guts Fthrow and Fsignal; they differ only in the way
1102 they choose the catch tag to throw to. A catch tag for a
1103 condition-case form has a TAG of Qnil.
1105 Before each catch is discarded, unbind all special bindings and
1106 execute all unwind-protect clauses made above that catch. Unwind
1107 the handler stack as we go, so that the proper handlers are in
1108 effect for each unwind-protect clause we run. At the end, restore
1109 some static info saved in CATCH, and longjmp to the location
1112 This is used for correct unwinding in Fthrow and Fsignal. */
1115 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1117 register int last_time
;
1119 /* Save the value in the tag. */
1122 /* Restore certain special C variables. */
1123 set_poll_suppress_count (catch->poll_suppress_count
);
1124 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1125 handling_signal
= 0;
1130 last_time
= catchlist
== catch;
1132 /* Unwind the specpdl stack, and then restore the proper set of
1134 unbind_to (catchlist
->pdlcount
, Qnil
);
1135 handlerlist
= catchlist
->handlerlist
;
1136 catchlist
= catchlist
->next
;
1138 while (! last_time
);
1141 /* If x_catch_errors was done, turn it off now.
1142 (First we give unbind_to a chance to do that.) */
1143 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1144 The catch must remain in effect during that delicate
1145 state. --lorentey */
1146 x_fully_uncatch_errors ();
1150 byte_stack_list
= catch->byte_stack
;
1151 gcprolist
= catch->gcpro
;
1153 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1155 backtrace_list
= catch->backlist
;
1156 lisp_eval_depth
= catch->lisp_eval_depth
;
1158 _longjmp (catch->jmp
, 1);
1161 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1162 doc
: /* Throw to the catch for TAG and return VALUE from it.
1163 Both TAG and VALUE are evalled. */)
1164 (register Lisp_Object tag
, Lisp_Object value
)
1166 register struct catchtag
*c
;
1169 for (c
= catchlist
; c
; c
= c
->next
)
1171 if (EQ (c
->tag
, tag
))
1172 unwind_to_catch (c
, value
);
1174 xsignal2 (Qno_catch
, tag
, value
);
1178 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1179 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1180 If BODYFORM completes normally, its value is returned
1181 after executing the UNWINDFORMS.
1182 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1183 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1187 ptrdiff_t count
= SPECPDL_INDEX ();
1189 record_unwind_protect (Fprogn
, Fcdr (args
));
1190 val
= eval_sub (Fcar (args
));
1191 return unbind_to (count
, val
);
1194 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1195 doc
: /* Regain control when an error is signaled.
1196 Executes BODYFORM and returns its value if no error happens.
1197 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1198 where the BODY is made of Lisp expressions.
1200 A handler is applicable to an error
1201 if CONDITION-NAME is one of the error's condition names.
1202 If an error happens, the first applicable handler is run.
1204 The car of a handler may be a list of condition names instead of a
1205 single condition name; then it handles all of them. If the special
1206 condition name `debug' is present in this list, it allows another
1207 condition in the list to run the debugger if `debug-on-error' and the
1208 other usual mechanisms says it should (otherwise, `condition-case'
1209 suppresses the debugger).
1211 When a handler handles an error, control returns to the `condition-case'
1212 and it executes the handler's BODY...
1213 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1214 \(If VAR is nil, the handler can't access that information.)
1215 Then the value of the last BODY form is returned from the `condition-case'
1218 See also the function `signal' for more info.
1219 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1222 register Lisp_Object bodyform
, handlers
;
1223 volatile Lisp_Object var
;
1226 bodyform
= Fcar (Fcdr (args
));
1227 handlers
= Fcdr (Fcdr (args
));
1229 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1232 /* Like Fcondition_case, but the args are separate
1233 rather than passed in a list. Used by Fbyte_code. */
1236 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1237 Lisp_Object handlers
)
1245 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1251 && (SYMBOLP (XCAR (tem
))
1252 || CONSP (XCAR (tem
))))))
1253 error ("Invalid condition handler: %s",
1254 SDATA (Fprin1_to_string (tem
, Qt
)));
1259 c
.backlist
= backtrace_list
;
1260 c
.handlerlist
= handlerlist
;
1261 c
.lisp_eval_depth
= lisp_eval_depth
;
1262 c
.pdlcount
= SPECPDL_INDEX ();
1263 c
.poll_suppress_count
= poll_suppress_count
;
1264 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1265 c
.gcpro
= gcprolist
;
1266 c
.byte_stack
= byte_stack_list
;
1267 if (_setjmp (c
.jmp
))
1270 specbind (h
.var
, c
.val
);
1271 val
= Fprogn (Fcdr (h
.chosen_clause
));
1273 /* Note that this just undoes the binding of h.var; whoever
1274 longjumped to us unwound the stack to c.pdlcount before
1276 unbind_to (c
.pdlcount
, Qnil
);
1283 h
.handler
= handlers
;
1284 h
.next
= handlerlist
;
1288 val
= eval_sub (bodyform
);
1290 handlerlist
= h
.next
;
1294 /* Call the function BFUN with no arguments, catching errors within it
1295 according to HANDLERS. If there is an error, call HFUN with
1296 one argument which is the data that describes the error:
1299 HANDLERS can be a list of conditions to catch.
1300 If HANDLERS is Qt, catch all errors.
1301 If HANDLERS is Qerror, catch all errors
1302 but allow the debugger to run if that is enabled. */
1305 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1306 Lisp_Object (*hfun
) (Lisp_Object
))
1314 c
.backlist
= backtrace_list
;
1315 c
.handlerlist
= handlerlist
;
1316 c
.lisp_eval_depth
= lisp_eval_depth
;
1317 c
.pdlcount
= SPECPDL_INDEX ();
1318 c
.poll_suppress_count
= poll_suppress_count
;
1319 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1320 c
.gcpro
= gcprolist
;
1321 c
.byte_stack
= byte_stack_list
;
1322 if (_setjmp (c
.jmp
))
1324 return (*hfun
) (c
.val
);
1328 h
.handler
= handlers
;
1330 h
.next
= handlerlist
;
1336 handlerlist
= h
.next
;
1340 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1343 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1344 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1352 c
.backlist
= backtrace_list
;
1353 c
.handlerlist
= handlerlist
;
1354 c
.lisp_eval_depth
= lisp_eval_depth
;
1355 c
.pdlcount
= SPECPDL_INDEX ();
1356 c
.poll_suppress_count
= poll_suppress_count
;
1357 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1358 c
.gcpro
= gcprolist
;
1359 c
.byte_stack
= byte_stack_list
;
1360 if (_setjmp (c
.jmp
))
1362 return (*hfun
) (c
.val
);
1366 h
.handler
= handlers
;
1368 h
.next
= handlerlist
;
1372 val
= (*bfun
) (arg
);
1374 handlerlist
= h
.next
;
1378 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1382 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1385 Lisp_Object handlers
,
1386 Lisp_Object (*hfun
) (Lisp_Object
))
1394 c
.backlist
= backtrace_list
;
1395 c
.handlerlist
= handlerlist
;
1396 c
.lisp_eval_depth
= lisp_eval_depth
;
1397 c
.pdlcount
= SPECPDL_INDEX ();
1398 c
.poll_suppress_count
= poll_suppress_count
;
1399 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1400 c
.gcpro
= gcprolist
;
1401 c
.byte_stack
= byte_stack_list
;
1402 if (_setjmp (c
.jmp
))
1404 return (*hfun
) (c
.val
);
1408 h
.handler
= handlers
;
1410 h
.next
= handlerlist
;
1414 val
= (*bfun
) (arg1
, arg2
);
1416 handlerlist
= h
.next
;
1420 /* Like internal_condition_case but call BFUN with NARGS as first,
1421 and ARGS as second argument. */
1424 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1427 Lisp_Object handlers
,
1428 Lisp_Object (*hfun
) (Lisp_Object
))
1436 c
.backlist
= backtrace_list
;
1437 c
.handlerlist
= handlerlist
;
1438 c
.lisp_eval_depth
= lisp_eval_depth
;
1439 c
.pdlcount
= SPECPDL_INDEX ();
1440 c
.poll_suppress_count
= poll_suppress_count
;
1441 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1442 c
.gcpro
= gcprolist
;
1443 c
.byte_stack
= byte_stack_list
;
1444 if (_setjmp (c
.jmp
))
1446 return (*hfun
) (c
.val
);
1450 h
.handler
= handlers
;
1452 h
.next
= handlerlist
;
1456 val
= (*bfun
) (nargs
, args
);
1458 handlerlist
= h
.next
;
1463 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1464 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1468 process_quit_flag (void)
1470 Lisp_Object flag
= Vquit_flag
;
1472 if (EQ (flag
, Qkill_emacs
))
1474 if (EQ (Vthrow_on_input
, flag
))
1475 Fthrow (Vthrow_on_input
, Qt
);
1476 Fsignal (Qquit
, Qnil
);
1479 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1480 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1481 This function does not return.
1483 An error symbol is a symbol with an `error-conditions' property
1484 that is a list of condition names.
1485 A handler for any of those names will get to handle this signal.
1486 The symbol `error' should normally be one of them.
1488 DATA should be a list. Its elements are printed as part of the error message.
1489 See Info anchor `(elisp)Definition of signal' for some details on how this
1490 error message is constructed.
1491 If the signal is handled, DATA is made available to the handler.
1492 See also the function `condition-case'. */)
1493 (Lisp_Object error_symbol
, Lisp_Object data
)
1495 /* When memory is full, ERROR-SYMBOL is nil,
1496 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1497 That is a special case--don't do this in other situations. */
1498 Lisp_Object conditions
;
1500 Lisp_Object real_error_symbol
1501 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1502 register Lisp_Object clause
= Qnil
;
1504 struct backtrace
*bp
;
1506 immediate_quit
= handling_signal
= 0;
1508 if (gc_in_progress
|| waiting_for_input
)
1511 #if 0 /* rms: I don't know why this was here,
1512 but it is surely wrong for an error that is handled. */
1513 #ifdef HAVE_WINDOW_SYSTEM
1514 if (display_hourglass_p
)
1515 cancel_hourglass ();
1519 /* This hook is used by edebug. */
1520 if (! NILP (Vsignal_hook_function
)
1521 && ! NILP (error_symbol
))
1523 /* Edebug takes care of restoring these variables when it exits. */
1524 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1525 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1527 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1528 max_specpdl_size
= SPECPDL_INDEX () + 40;
1530 call2 (Vsignal_hook_function
, error_symbol
, data
);
1533 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1535 /* Remember from where signal was called. Skip over the frame for
1536 `signal' itself. If a frame for `error' follows, skip that,
1537 too. Don't do this when ERROR_SYMBOL is nil, because that
1538 is a memory-full error. */
1539 Vsignaling_function
= Qnil
;
1540 if (backtrace_list
&& !NILP (error_symbol
))
1542 bp
= backtrace_list
->next
;
1543 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1545 if (bp
&& bp
->function
)
1546 Vsignaling_function
= *bp
->function
;
1549 for (h
= handlerlist
; h
; h
= h
->next
)
1551 clause
= find_handler_clause (h
->handler
, conditions
);
1556 if (/* Don't run the debugger for a memory-full error.
1557 (There is no room in memory to do that!) */
1558 !NILP (error_symbol
)
1559 && (!NILP (Vdebug_on_signal
)
1560 /* If no handler is present now, try to run the debugger. */
1562 /* A `debug' symbol in the handler list disables the normal
1563 suppression of the debugger. */
1564 || (CONSP (clause
) && CONSP (XCAR (clause
))
1565 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1566 /* Special handler that means "print a message and run debugger
1568 || EQ (h
->handler
, Qerror
)))
1571 = maybe_call_debugger (conditions
, error_symbol
, data
);
1572 /* We can't return values to code which signaled an error, but we
1573 can continue code which has signaled a quit. */
1574 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1580 Lisp_Object unwind_data
1581 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1583 h
->chosen_clause
= clause
;
1584 unwind_to_catch (h
->tag
, unwind_data
);
1589 Fthrow (Qtop_level
, Qt
);
1592 if (! NILP (error_symbol
))
1593 data
= Fcons (error_symbol
, data
);
1595 string
= Ferror_message_string (data
);
1596 fatal ("%s", SDATA (string
));
1599 /* Internal version of Fsignal that never returns.
1600 Used for anything but Qquit (which can return from Fsignal). */
1603 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1605 Fsignal (error_symbol
, data
);
1609 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1612 xsignal0 (Lisp_Object error_symbol
)
1614 xsignal (error_symbol
, Qnil
);
1618 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1620 xsignal (error_symbol
, list1 (arg
));
1624 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1626 xsignal (error_symbol
, list2 (arg1
, arg2
));
1630 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1632 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1635 /* Signal `error' with message S, and additional arg ARG.
1636 If ARG is not a genuine list, make it a one-element list. */
1639 signal_error (const char *s
, Lisp_Object arg
)
1641 Lisp_Object tortoise
, hare
;
1643 hare
= tortoise
= arg
;
1644 while (CONSP (hare
))
1651 tortoise
= XCDR (tortoise
);
1653 if (EQ (hare
, tortoise
))
1658 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1660 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1664 /* Return nonzero if LIST is a non-nil atom or
1665 a list containing one of CONDITIONS. */
1668 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1675 while (CONSP (conditions
))
1677 Lisp_Object
this, tail
;
1678 this = XCAR (conditions
);
1679 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1680 if (EQ (XCAR (tail
), this))
1682 conditions
= XCDR (conditions
);
1687 /* Return 1 if an error with condition-symbols CONDITIONS,
1688 and described by SIGNAL-DATA, should skip the debugger
1689 according to debugger-ignored-errors. */
1692 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1695 int first_string
= 1;
1696 Lisp_Object error_message
;
1698 error_message
= Qnil
;
1699 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1701 if (STRINGP (XCAR (tail
)))
1705 error_message
= Ferror_message_string (data
);
1709 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1714 Lisp_Object contail
;
1716 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1717 if (EQ (XCAR (tail
), XCAR (contail
)))
1725 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1726 SIG and DATA describe the signal. There are two ways to pass them:
1727 = SIG is the error symbol, and DATA is the rest of the data.
1728 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1729 This is for memory-full errors only. */
1731 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1733 Lisp_Object combined_data
;
1735 combined_data
= Fcons (sig
, data
);
1738 /* Don't try to run the debugger with interrupts blocked.
1739 The editing loop would return anyway. */
1741 /* Does user want to enter debugger for this kind of error? */
1744 : wants_debugger (Vdebug_on_error
, conditions
))
1745 && ! skip_debugger (conditions
, combined_data
)
1746 /* RMS: What's this for? */
1747 && when_entered_debugger
< num_nonmacro_input_events
)
1749 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1757 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1759 register Lisp_Object h
;
1761 /* t is used by handlers for all conditions, set up by C code. */
1762 if (EQ (handlers
, Qt
))
1765 /* error is used similarly, but means print an error message
1766 and run the debugger if that is enabled. */
1767 if (EQ (handlers
, Qerror
))
1770 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1772 Lisp_Object handler
= XCAR (h
);
1773 Lisp_Object condit
, tem
;
1775 if (!CONSP (handler
))
1777 condit
= XCAR (handler
);
1778 /* Handle a single condition name in handler HANDLER. */
1779 if (SYMBOLP (condit
))
1781 tem
= Fmemq (Fcar (handler
), conditions
);
1785 /* Handle a list of condition names in handler HANDLER. */
1786 else if (CONSP (condit
))
1789 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1791 tem
= Fmemq (XCAR (tail
), conditions
);
1802 /* Dump an error message; called like vprintf. */
1804 verror (const char *m
, va_list ap
)
1807 ptrdiff_t size
= sizeof buf
;
1808 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1813 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1814 string
= make_string (buffer
, used
);
1818 xsignal1 (Qerror
, string
);
1822 /* Dump an error message; called like printf. */
1826 error (const char *m
, ...)
1834 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1835 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1836 This means it contains a description for how to read arguments to give it.
1837 The value is nil for an invalid function or a symbol with no function
1840 Interactively callable functions include strings and vectors (treated
1841 as keyboard macros), lambda-expressions that contain a top-level call
1842 to `interactive', autoload definitions made by `autoload' with non-nil
1843 fourth argument, and some of the built-in functions of Lisp.
1845 Also, a symbol satisfies `commandp' if its function definition does so.
1847 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1848 then strings and vectors are not accepted. */)
1849 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1851 register Lisp_Object fun
;
1852 register Lisp_Object funcar
;
1853 Lisp_Object if_prop
= Qnil
;
1857 fun
= indirect_function (fun
); /* Check cycles. */
1858 if (NILP (fun
) || EQ (fun
, Qunbound
))
1861 /* Check an `interactive-form' property if present, analogous to the
1862 function-documentation property. */
1864 while (SYMBOLP (fun
))
1866 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1869 fun
= Fsymbol_function (fun
);
1872 /* Emacs primitives are interactive if their DEFUN specifies an
1873 interactive spec. */
1875 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1877 /* Bytecode objects are interactive if they are long enough to
1878 have an element whose index is COMPILED_INTERACTIVE, which is
1879 where the interactive spec is stored. */
1880 else if (COMPILEDP (fun
))
1881 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1884 /* Strings and vectors are keyboard macros. */
1885 if (STRINGP (fun
) || VECTORP (fun
))
1886 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1888 /* Lists may represent commands. */
1891 funcar
= XCAR (fun
);
1892 if (EQ (funcar
, Qclosure
))
1893 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1895 else if (EQ (funcar
, Qlambda
))
1896 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1897 else if (EQ (funcar
, Qautoload
))
1898 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1903 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1904 doc
: /* Define FUNCTION to autoload from FILE.
1905 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1906 Third arg DOCSTRING is documentation for the function.
1907 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1908 Fifth arg TYPE indicates the type of the object:
1909 nil or omitted says FUNCTION is a function,
1910 `keymap' says FUNCTION is really a keymap, and
1911 `macro' or t says FUNCTION is really a macro.
1912 Third through fifth args give info about the real definition.
1913 They default to nil.
1914 If FUNCTION is already defined other than as an autoload,
1915 this does nothing and returns nil. */)
1916 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1918 CHECK_SYMBOL (function
);
1919 CHECK_STRING (file
);
1921 /* If function is defined and not as an autoload, don't override. */
1922 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1923 && !(CONSP (XSYMBOL (function
)->function
)
1924 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1927 if (NILP (Vpurify_flag
))
1928 /* Only add entries after dumping, because the ones before are
1929 not useful and else we get loads of them from the loaddefs.el. */
1930 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1931 else if (EQ (docstring
, make_number (0)))
1932 /* `read1' in lread.c has found the docstring starting with "\
1933 and assumed the docstring will be provided by Snarf-documentation, so it
1934 passed us 0 instead. But that leads to accidental sharing in purecopy's
1935 hash-consing, so we use a (hopefully) unique integer instead. */
1936 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1937 return Ffset (function
,
1938 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1939 interactive
, type
)));
1943 un_autoload (Lisp_Object oldqueue
)
1945 register Lisp_Object queue
, first
, second
;
1947 /* Queue to unwind is current value of Vautoload_queue.
1948 oldqueue is the shadowed value to leave in Vautoload_queue. */
1949 queue
= Vautoload_queue
;
1950 Vautoload_queue
= oldqueue
;
1951 while (CONSP (queue
))
1953 first
= XCAR (queue
);
1954 second
= Fcdr (first
);
1955 first
= Fcar (first
);
1956 if (EQ (first
, make_number (0)))
1959 Ffset (first
, second
);
1960 queue
= XCDR (queue
);
1965 /* Load an autoloaded function.
1966 FUNNAME is the symbol which is the function's name.
1967 FUNDEF is the autoload definition (a list). */
1970 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
1972 ptrdiff_t count
= SPECPDL_INDEX ();
1974 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1976 /* This is to make sure that loadup.el gives a clear picture
1977 of what files are preloaded and when. */
1978 if (! NILP (Vpurify_flag
))
1979 error ("Attempt to autoload %s while preparing to dump",
1980 SDATA (SYMBOL_NAME (funname
)));
1983 CHECK_SYMBOL (funname
);
1984 GCPRO3 (fun
, funname
, fundef
);
1986 /* Preserve the match data. */
1987 record_unwind_save_match_data ();
1989 /* If autoloading gets an error (which includes the error of failing
1990 to define the function being called), we use Vautoload_queue
1991 to undo function definitions and `provide' calls made by
1992 the function. We do this in the specific case of autoloading
1993 because autoloading is not an explicit request "load this file",
1994 but rather a request to "call this function".
1996 The value saved here is to be restored into Vautoload_queue. */
1997 record_unwind_protect (un_autoload
, Vautoload_queue
);
1998 Vautoload_queue
= Qt
;
1999 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2001 /* Once loading finishes, don't undo it. */
2002 Vautoload_queue
= Qt
;
2003 unbind_to (count
, Qnil
);
2005 fun
= Findirect_function (fun
, Qnil
);
2007 if (!NILP (Fequal (fun
, fundef
)))
2008 error ("Autoloading failed to define function %s",
2009 SDATA (SYMBOL_NAME (funname
)));
2014 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2015 doc
: /* Evaluate FORM and return its value.
2016 If LEXICAL is t, evaluate using lexical scoping. */)
2017 (Lisp_Object form
, Lisp_Object lexical
)
2019 ptrdiff_t count
= SPECPDL_INDEX ();
2020 specbind (Qinternal_interpreter_environment
,
2021 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2022 return unbind_to (count
, eval_sub (form
));
2025 /* Eval a sub-expression of the current expression (i.e. in the same
2028 eval_sub (Lisp_Object form
)
2030 Lisp_Object fun
, val
, original_fun
, original_args
;
2032 struct backtrace backtrace
;
2033 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2035 if (handling_signal
)
2040 /* Look up its binding in the lexical environment.
2041 We do not pay attention to the declared_special flag here, since we
2042 already did that when let-binding the variable. */
2043 Lisp_Object lex_binding
2044 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2045 ? Fassq (form
, Vinternal_interpreter_environment
)
2047 if (CONSP (lex_binding
))
2048 return XCDR (lex_binding
);
2050 return Fsymbol_value (form
);
2057 if ((consing_since_gc
> gc_cons_threshold
2058 && consing_since_gc
> gc_relative_threshold
)
2060 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2063 Fgarbage_collect ();
2067 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2069 if (max_lisp_eval_depth
< 100)
2070 max_lisp_eval_depth
= 100;
2071 if (lisp_eval_depth
> max_lisp_eval_depth
)
2072 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2075 original_fun
= Fcar (form
);
2076 original_args
= Fcdr (form
);
2078 backtrace
.next
= backtrace_list
;
2079 backtrace_list
= &backtrace
;
2080 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2081 backtrace
.args
= &original_args
;
2082 backtrace
.nargs
= UNEVALLED
;
2083 backtrace
.debug_on_exit
= 0;
2085 if (debug_on_next_call
)
2086 do_debug_on_call (Qt
);
2088 /* At this point, only original_fun and original_args
2089 have values that will be used below. */
2092 /* Optimize for no indirection. */
2094 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2095 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2096 fun
= indirect_function (fun
);
2100 Lisp_Object numargs
;
2101 Lisp_Object argvals
[8];
2102 Lisp_Object args_left
;
2103 register int i
, maxargs
;
2105 args_left
= original_args
;
2106 numargs
= Flength (args_left
);
2110 if (XINT (numargs
) < XSUBR (fun
)->min_args
2111 || (XSUBR (fun
)->max_args
>= 0
2112 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2113 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2115 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2116 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2117 else if (XSUBR (fun
)->max_args
== MANY
)
2119 /* Pass a vector of evaluated arguments. */
2121 ptrdiff_t argnum
= 0;
2124 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2126 GCPRO3 (args_left
, fun
, fun
);
2130 while (!NILP (args_left
))
2132 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2133 args_left
= Fcdr (args_left
);
2134 gcpro3
.nvars
= argnum
;
2137 backtrace
.args
= vals
;
2138 backtrace
.nargs
= XINT (numargs
);
2140 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2146 GCPRO3 (args_left
, fun
, fun
);
2147 gcpro3
.var
= argvals
;
2150 maxargs
= XSUBR (fun
)->max_args
;
2151 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2153 argvals
[i
] = eval_sub (Fcar (args_left
));
2159 backtrace
.args
= argvals
;
2160 backtrace
.nargs
= XINT (numargs
);
2165 val
= (XSUBR (fun
)->function
.a0 ());
2168 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2171 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2174 val
= (XSUBR (fun
)->function
.a3
2175 (argvals
[0], argvals
[1], argvals
[2]));
2178 val
= (XSUBR (fun
)->function
.a4
2179 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2182 val
= (XSUBR (fun
)->function
.a5
2183 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2187 val
= (XSUBR (fun
)->function
.a6
2188 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2189 argvals
[4], argvals
[5]));
2192 val
= (XSUBR (fun
)->function
.a7
2193 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2194 argvals
[4], argvals
[5], argvals
[6]));
2198 val
= (XSUBR (fun
)->function
.a8
2199 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2200 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2204 /* Someone has created a subr that takes more arguments than
2205 is supported by this code. We need to either rewrite the
2206 subr to use a different argument protocol, or add more
2207 cases to this switch. */
2212 else if (COMPILEDP (fun
))
2213 val
= apply_lambda (fun
, original_args
);
2216 if (EQ (fun
, Qunbound
))
2217 xsignal1 (Qvoid_function
, original_fun
);
2219 xsignal1 (Qinvalid_function
, original_fun
);
2220 funcar
= XCAR (fun
);
2221 if (!SYMBOLP (funcar
))
2222 xsignal1 (Qinvalid_function
, original_fun
);
2223 if (EQ (funcar
, Qautoload
))
2225 do_autoload (fun
, original_fun
);
2228 if (EQ (funcar
, Qmacro
))
2229 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2230 else if (EQ (funcar
, Qlambda
)
2231 || EQ (funcar
, Qclosure
))
2232 val
= apply_lambda (fun
, original_args
);
2234 xsignal1 (Qinvalid_function
, original_fun
);
2239 if (backtrace
.debug_on_exit
)
2240 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2241 backtrace_list
= backtrace
.next
;
2246 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2247 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2248 Then return the value FUNCTION returns.
2249 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2250 usage: (apply FUNCTION &rest ARGUMENTS) */)
2251 (ptrdiff_t nargs
, Lisp_Object
*args
)
2255 register Lisp_Object spread_arg
;
2256 register Lisp_Object
*funcall_args
;
2257 Lisp_Object fun
, retval
;
2258 struct gcpro gcpro1
;
2263 spread_arg
= args
[nargs
- 1];
2264 CHECK_LIST (spread_arg
);
2266 numargs
= XINT (Flength (spread_arg
));
2269 return Ffuncall (nargs
- 1, args
);
2270 else if (numargs
== 1)
2272 args
[nargs
- 1] = XCAR (spread_arg
);
2273 return Ffuncall (nargs
, args
);
2276 numargs
+= nargs
- 2;
2278 /* Optimize for no indirection. */
2279 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2280 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2281 fun
= indirect_function (fun
);
2282 if (EQ (fun
, Qunbound
))
2284 /* Let funcall get the error. */
2291 if (numargs
< XSUBR (fun
)->min_args
2292 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2293 goto funcall
; /* Let funcall get the error. */
2294 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2296 /* Avoid making funcall cons up a yet another new vector of arguments
2297 by explicitly supplying nil's for optional values. */
2298 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2299 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2300 funcall_args
[++i
] = Qnil
;
2301 GCPRO1 (*funcall_args
);
2302 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2306 /* We add 1 to numargs because funcall_args includes the
2307 function itself as well as its arguments. */
2310 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2311 GCPRO1 (*funcall_args
);
2312 gcpro1
.nvars
= 1 + numargs
;
2315 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2316 /* Spread the last arg we got. Its first element goes in
2317 the slot that it used to occupy, hence this value of I. */
2319 while (!NILP (spread_arg
))
2321 funcall_args
[i
++] = XCAR (spread_arg
);
2322 spread_arg
= XCDR (spread_arg
);
2325 /* By convention, the caller needs to gcpro Ffuncall's args. */
2326 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2333 /* Run hook variables in various ways. */
2336 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2338 Ffuncall (nargs
, args
);
2342 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2343 doc
: /* Run each hook in HOOKS.
2344 Each argument should be a symbol, a hook variable.
2345 These symbols are processed in the order specified.
2346 If a hook symbol has a non-nil value, that value may be a function
2347 or a list of functions to be called to run the hook.
2348 If the value is a function, it is called with no arguments.
2349 If it is a list, the elements are called, in order, with no arguments.
2351 Major modes should not use this function directly to run their mode
2352 hook; they should use `run-mode-hooks' instead.
2354 Do not use `make-local-variable' to make a hook variable buffer-local.
2355 Instead, use `add-hook' and specify t for the LOCAL argument.
2356 usage: (run-hooks &rest HOOKS) */)
2357 (ptrdiff_t nargs
, Lisp_Object
*args
)
2359 Lisp_Object hook
[1];
2362 for (i
= 0; i
< nargs
; i
++)
2365 run_hook_with_args (1, hook
, funcall_nil
);
2371 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2372 Srun_hook_with_args
, 1, MANY
, 0,
2373 doc
: /* Run HOOK with the specified arguments ARGS.
2374 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2375 value, that value may be a function or a list of functions to be
2376 called to run the hook. If the value is a function, it is called with
2377 the given arguments and its return value is returned. If it is a list
2378 of functions, those functions are called, in order,
2379 with the given arguments ARGS.
2380 It is best not to depend on the value returned by `run-hook-with-args',
2383 Do not use `make-local-variable' to make a hook variable buffer-local.
2384 Instead, use `add-hook' and specify t for the LOCAL argument.
2385 usage: (run-hook-with-args HOOK &rest ARGS) */)
2386 (ptrdiff_t nargs
, Lisp_Object
*args
)
2388 return run_hook_with_args (nargs
, args
, funcall_nil
);
2391 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2392 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2393 doc
: /* Run HOOK with the specified arguments ARGS.
2394 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2395 value, that value may be a function or a list of functions to be
2396 called to run the hook. If the value is a function, it is called with
2397 the given arguments and its return value is returned.
2398 If it is a list of functions, those functions are called, in order,
2399 with the given arguments ARGS, until one of them
2400 returns a non-nil value. Then we return that value.
2401 However, if they all return nil, we return nil.
2403 Do not use `make-local-variable' to make a hook variable buffer-local.
2404 Instead, use `add-hook' and specify t for the LOCAL argument.
2405 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2406 (ptrdiff_t nargs
, Lisp_Object
*args
)
2408 return run_hook_with_args (nargs
, args
, Ffuncall
);
2412 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2414 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2417 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2418 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2419 doc
: /* Run HOOK with the specified arguments ARGS.
2420 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2421 value, that value may be a function or a list of functions to be
2422 called to run the hook. If the value is a function, it is called with
2423 the given arguments and its return value is returned.
2424 If it is a list of functions, those functions are called, in order,
2425 with the given arguments ARGS, until one of them returns nil.
2426 Then we return nil. However, if they all return non-nil, we return non-nil.
2428 Do not use `make-local-variable' to make a hook variable buffer-local.
2429 Instead, use `add-hook' and specify t for the LOCAL argument.
2430 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2431 (ptrdiff_t nargs
, Lisp_Object
*args
)
2433 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2437 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2439 Lisp_Object tmp
= args
[0], ret
;
2442 ret
= Ffuncall (nargs
, args
);
2448 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2449 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2450 I.e. instead of calling each function FUN directly with arguments ARGS,
2451 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2452 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2453 aborts and returns that value.
2454 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2455 (ptrdiff_t nargs
, Lisp_Object
*args
)
2457 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2460 /* ARGS[0] should be a hook symbol.
2461 Call each of the functions in the hook value, passing each of them
2462 as arguments all the rest of ARGS (all NARGS - 1 elements).
2463 FUNCALL specifies how to call each function on the hook.
2464 The caller (or its caller, etc) must gcpro all of ARGS,
2465 except that it isn't necessary to gcpro ARGS[0]. */
2468 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2469 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2471 Lisp_Object sym
, val
, ret
= Qnil
;
2472 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2474 /* If we are dying or still initializing,
2475 don't do anything--it would probably crash if we tried. */
2476 if (NILP (Vrun_hooks
))
2480 val
= find_symbol_value (sym
);
2482 if (EQ (val
, Qunbound
) || NILP (val
))
2484 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2487 return funcall (nargs
, args
);
2491 Lisp_Object global_vals
= Qnil
;
2492 GCPRO3 (sym
, val
, global_vals
);
2495 CONSP (val
) && NILP (ret
);
2498 if (EQ (XCAR (val
), Qt
))
2500 /* t indicates this hook has a local binding;
2501 it means to run the global binding too. */
2502 global_vals
= Fdefault_value (sym
);
2503 if (NILP (global_vals
)) continue;
2505 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2507 args
[0] = global_vals
;
2508 ret
= funcall (nargs
, args
);
2513 CONSP (global_vals
) && NILP (ret
);
2514 global_vals
= XCDR (global_vals
))
2516 args
[0] = XCAR (global_vals
);
2517 /* In a global value, t should not occur. If it does, we
2518 must ignore it to avoid an endless loop. */
2519 if (!EQ (args
[0], Qt
))
2520 ret
= funcall (nargs
, args
);
2526 args
[0] = XCAR (val
);
2527 ret
= funcall (nargs
, args
);
2536 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2539 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2541 Lisp_Object temp
[3];
2546 Frun_hook_with_args (3, temp
);
2549 /* Apply fn to arg. */
2551 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2553 struct gcpro gcpro1
;
2557 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2560 Lisp_Object args
[2];
2564 RETURN_UNGCPRO (Fapply (2, args
));
2568 /* Call function fn on no arguments. */
2570 call0 (Lisp_Object fn
)
2572 struct gcpro gcpro1
;
2575 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2578 /* Call function fn with 1 argument arg1. */
2581 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2583 struct gcpro gcpro1
;
2584 Lisp_Object args
[2];
2590 RETURN_UNGCPRO (Ffuncall (2, args
));
2593 /* Call function fn with 2 arguments arg1, arg2. */
2596 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2598 struct gcpro gcpro1
;
2599 Lisp_Object args
[3];
2605 RETURN_UNGCPRO (Ffuncall (3, args
));
2608 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2611 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2613 struct gcpro gcpro1
;
2614 Lisp_Object args
[4];
2621 RETURN_UNGCPRO (Ffuncall (4, args
));
2624 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2627 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2630 struct gcpro gcpro1
;
2631 Lisp_Object args
[5];
2639 RETURN_UNGCPRO (Ffuncall (5, args
));
2642 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2645 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2646 Lisp_Object arg4
, Lisp_Object arg5
)
2648 struct gcpro gcpro1
;
2649 Lisp_Object args
[6];
2658 RETURN_UNGCPRO (Ffuncall (6, args
));
2661 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2664 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2665 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2667 struct gcpro gcpro1
;
2668 Lisp_Object args
[7];
2678 RETURN_UNGCPRO (Ffuncall (7, args
));
2681 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2684 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2685 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2687 struct gcpro gcpro1
;
2688 Lisp_Object args
[8];
2699 RETURN_UNGCPRO (Ffuncall (8, args
));
2702 /* The caller should GCPRO all the elements of ARGS. */
2704 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2705 doc
: /* Non-nil if OBJECT is a function. */)
2706 (Lisp_Object object
)
2708 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2710 object
= Findirect_function (object
, Qt
);
2712 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2714 /* Autoloaded symbols are functions, except if they load
2715 macros or keymaps. */
2717 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2718 object
= XCDR (object
);
2720 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2725 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2726 else if (COMPILEDP (object
))
2728 else if (CONSP (object
))
2730 Lisp_Object car
= XCAR (object
);
2731 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2737 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2738 doc
: /* Call first argument as a function, passing remaining arguments to it.
2739 Return the value that function returns.
2740 Thus, (funcall 'cons 'x 'y) returns (x . y).
2741 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2742 (ptrdiff_t nargs
, Lisp_Object
*args
)
2744 Lisp_Object fun
, original_fun
;
2746 ptrdiff_t numargs
= nargs
- 1;
2747 Lisp_Object lisp_numargs
;
2749 struct backtrace backtrace
;
2750 register Lisp_Object
*internal_args
;
2754 if ((consing_since_gc
> gc_cons_threshold
2755 && consing_since_gc
> gc_relative_threshold
)
2757 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2758 Fgarbage_collect ();
2760 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2762 if (max_lisp_eval_depth
< 100)
2763 max_lisp_eval_depth
= 100;
2764 if (lisp_eval_depth
> max_lisp_eval_depth
)
2765 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2768 backtrace
.next
= backtrace_list
;
2769 backtrace_list
= &backtrace
;
2770 backtrace
.function
= &args
[0];
2771 backtrace
.args
= &args
[1];
2772 backtrace
.nargs
= nargs
- 1;
2773 backtrace
.debug_on_exit
= 0;
2775 if (debug_on_next_call
)
2776 do_debug_on_call (Qlambda
);
2780 original_fun
= args
[0];
2784 /* Optimize for no indirection. */
2786 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2787 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2788 fun
= indirect_function (fun
);
2792 if (numargs
< XSUBR (fun
)->min_args
2793 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2795 XSETFASTINT (lisp_numargs
, numargs
);
2796 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2799 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2800 xsignal1 (Qinvalid_function
, original_fun
);
2802 else if (XSUBR (fun
)->max_args
== MANY
)
2803 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2806 if (XSUBR (fun
)->max_args
> numargs
)
2808 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2809 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2810 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2811 internal_args
[i
] = Qnil
;
2814 internal_args
= args
+ 1;
2815 switch (XSUBR (fun
)->max_args
)
2818 val
= (XSUBR (fun
)->function
.a0 ());
2821 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2824 val
= (XSUBR (fun
)->function
.a2
2825 (internal_args
[0], internal_args
[1]));
2828 val
= (XSUBR (fun
)->function
.a3
2829 (internal_args
[0], internal_args
[1], internal_args
[2]));
2832 val
= (XSUBR (fun
)->function
.a4
2833 (internal_args
[0], internal_args
[1], internal_args
[2],
2837 val
= (XSUBR (fun
)->function
.a5
2838 (internal_args
[0], internal_args
[1], internal_args
[2],
2839 internal_args
[3], internal_args
[4]));
2842 val
= (XSUBR (fun
)->function
.a6
2843 (internal_args
[0], internal_args
[1], internal_args
[2],
2844 internal_args
[3], internal_args
[4], internal_args
[5]));
2847 val
= (XSUBR (fun
)->function
.a7
2848 (internal_args
[0], internal_args
[1], internal_args
[2],
2849 internal_args
[3], internal_args
[4], internal_args
[5],
2854 val
= (XSUBR (fun
)->function
.a8
2855 (internal_args
[0], internal_args
[1], internal_args
[2],
2856 internal_args
[3], internal_args
[4], internal_args
[5],
2857 internal_args
[6], internal_args
[7]));
2862 /* If a subr takes more than 8 arguments without using MANY
2863 or UNEVALLED, we need to extend this function to support it.
2864 Until this is done, there is no way to call the function. */
2869 else if (COMPILEDP (fun
))
2870 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2873 if (EQ (fun
, Qunbound
))
2874 xsignal1 (Qvoid_function
, original_fun
);
2876 xsignal1 (Qinvalid_function
, original_fun
);
2877 funcar
= XCAR (fun
);
2878 if (!SYMBOLP (funcar
))
2879 xsignal1 (Qinvalid_function
, original_fun
);
2880 if (EQ (funcar
, Qlambda
)
2881 || EQ (funcar
, Qclosure
))
2882 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2883 else if (EQ (funcar
, Qautoload
))
2885 do_autoload (fun
, original_fun
);
2890 xsignal1 (Qinvalid_function
, original_fun
);
2894 if (backtrace
.debug_on_exit
)
2895 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2896 backtrace_list
= backtrace
.next
;
2901 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2903 Lisp_Object args_left
;
2906 register Lisp_Object
*arg_vector
;
2907 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2908 register Lisp_Object tem
;
2911 numargs
= XFASTINT (Flength (args
));
2912 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2915 GCPRO3 (*arg_vector
, args_left
, fun
);
2918 for (i
= 0; i
< numargs
; )
2920 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2921 tem
= eval_sub (tem
);
2922 arg_vector
[i
++] = tem
;
2928 backtrace_list
->args
= arg_vector
;
2929 backtrace_list
->nargs
= i
;
2930 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2932 /* Do the debug-on-exit now, while arg_vector still exists. */
2933 if (backtrace_list
->debug_on_exit
)
2934 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2935 /* Don't do it again when we return to eval. */
2936 backtrace_list
->debug_on_exit
= 0;
2941 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2942 and return the result of evaluation.
2943 FUN must be either a lambda-expression or a compiled-code object. */
2946 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2947 register Lisp_Object
*arg_vector
)
2949 Lisp_Object val
, syms_left
, next
, lexenv
;
2950 ptrdiff_t count
= SPECPDL_INDEX ();
2956 if (EQ (XCAR (fun
), Qclosure
))
2958 fun
= XCDR (fun
); /* Drop `closure'. */
2959 lexenv
= XCAR (fun
);
2960 CHECK_LIST_CONS (fun
, fun
);
2964 syms_left
= XCDR (fun
);
2965 if (CONSP (syms_left
))
2966 syms_left
= XCAR (syms_left
);
2968 xsignal1 (Qinvalid_function
, fun
);
2970 else if (COMPILEDP (fun
))
2972 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2973 if (INTEGERP (syms_left
))
2974 /* A byte-code object with a non-nil `push args' slot means we
2975 shouldn't bind any arguments, instead just call the byte-code
2976 interpreter directly; it will push arguments as necessary.
2978 Byte-code objects with either a non-existent, or a nil value for
2979 the `push args' slot (the default), have dynamically-bound
2980 arguments, and use the argument-binding code below instead (as do
2981 all interpreted functions, even lexically bound ones). */
2983 /* If we have not actually read the bytecode string
2984 and constants vector yet, fetch them from the file. */
2985 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2986 Ffetch_bytecode (fun
);
2987 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2988 AREF (fun
, COMPILED_CONSTANTS
),
2989 AREF (fun
, COMPILED_STACK_DEPTH
),
2998 i
= optional
= rest
= 0;
2999 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3003 next
= XCAR (syms_left
);
3004 if (!SYMBOLP (next
))
3005 xsignal1 (Qinvalid_function
, fun
);
3007 if (EQ (next
, Qand_rest
))
3009 else if (EQ (next
, Qand_optional
))
3016 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3020 arg
= arg_vector
[i
++];
3022 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3026 /* Bind the argument. */
3027 if (!NILP (lexenv
) && SYMBOLP (next
))
3028 /* Lexically bind NEXT by adding it to the lexenv alist. */
3029 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3031 /* Dynamically bind NEXT. */
3032 specbind (next
, arg
);
3036 if (!NILP (syms_left
))
3037 xsignal1 (Qinvalid_function
, fun
);
3039 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3041 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3042 /* Instantiate a new lexical environment. */
3043 specbind (Qinternal_interpreter_environment
, lexenv
);
3046 val
= Fprogn (XCDR (XCDR (fun
)));
3049 /* If we have not actually read the bytecode string
3050 and constants vector yet, fetch them from the file. */
3051 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3052 Ffetch_bytecode (fun
);
3053 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3054 AREF (fun
, COMPILED_CONSTANTS
),
3055 AREF (fun
, COMPILED_STACK_DEPTH
),
3059 return unbind_to (count
, val
);
3062 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3064 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3065 (Lisp_Object object
)
3069 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3071 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3074 tem
= AREF (object
, COMPILED_BYTECODE
);
3075 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3076 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3078 error ("Invalid byte code");
3080 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3081 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3089 register ptrdiff_t count
= SPECPDL_INDEX ();
3090 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3091 if (max_size
<= specpdl_size
)
3093 if (max_specpdl_size
< 400)
3094 max_size
= max_specpdl_size
= 400;
3095 if (max_size
<= specpdl_size
)
3096 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3098 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3099 specpdl_ptr
= specpdl
+ count
;
3102 /* `specpdl_ptr->symbol' is a field which describes which variable is
3103 let-bound, so it can be properly undone when we unbind_to.
3104 It can have the following two shapes:
3105 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3106 a symbol that is not buffer-local (at least at the time
3107 the let binding started). Note also that it should not be
3108 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3110 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3111 variable SYMBOL which can be buffer-local. WHERE tells us
3112 which buffer is affected (or nil if the let-binding affects the
3113 global value of the variable) and BUFFER tells us which buffer was
3114 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3115 BUFFER did not yet have a buffer-local value). */
3118 specbind (Lisp_Object symbol
, Lisp_Object value
)
3120 struct Lisp_Symbol
*sym
;
3122 eassert (!handling_signal
);
3124 CHECK_SYMBOL (symbol
);
3125 sym
= XSYMBOL (symbol
);
3126 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3130 switch (sym
->redirect
)
3132 case SYMBOL_VARALIAS
:
3133 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3134 case SYMBOL_PLAINVAL
:
3135 /* The most common case is that of a non-constant symbol with a
3136 trivial value. Make that as fast as we can. */
3137 specpdl_ptr
->symbol
= symbol
;
3138 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3139 specpdl_ptr
->func
= NULL
;
3142 SET_SYMBOL_VAL (sym
, value
);
3144 set_internal (symbol
, value
, Qnil
, 1);
3146 case SYMBOL_LOCALIZED
:
3147 if (SYMBOL_BLV (sym
)->frame_local
)
3148 error ("Frame-local vars cannot be let-bound");
3149 case SYMBOL_FORWARDED
:
3151 Lisp_Object ovalue
= find_symbol_value (symbol
);
3152 specpdl_ptr
->func
= 0;
3153 specpdl_ptr
->old_value
= ovalue
;
3155 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3156 || (EQ (SYMBOL_BLV (sym
)->where
,
3157 SYMBOL_BLV (sym
)->frame_local
?
3158 Fselected_frame () : Fcurrent_buffer ())));
3160 if (sym
->redirect
== SYMBOL_LOCALIZED
3161 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3163 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3165 /* For a local variable, record both the symbol and which
3166 buffer's or frame's value we are saving. */
3167 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3169 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3170 || (BLV_FOUND (SYMBOL_BLV (sym
))
3171 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3174 else if (sym
->redirect
== SYMBOL_LOCALIZED
3175 && BLV_FOUND (SYMBOL_BLV (sym
)))
3176 where
= SYMBOL_BLV (sym
)->where
;
3180 /* We're not using the `unused' slot in the specbinding
3181 structure because this would mean we have to do more
3182 work for simple variables. */
3183 /* FIXME: The third value `current_buffer' is only used in
3184 let_shadows_buffer_binding_p which is itself only used
3185 in set_internal for local_if_set. */
3186 eassert (NILP (where
) || EQ (where
, cur_buf
));
3187 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3189 /* If SYMBOL is a per-buffer variable which doesn't have a
3190 buffer-local value here, make the `let' change the global
3191 value by changing the value of SYMBOL in all buffers not
3192 having their own value. This is consistent with what
3193 happens with other buffer-local variables. */
3195 && sym
->redirect
== SYMBOL_FORWARDED
)
3197 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3199 Fset_default (symbol
, value
);
3204 specpdl_ptr
->symbol
= symbol
;
3207 set_internal (symbol
, value
, Qnil
, 1);
3215 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3217 eassert (!handling_signal
);
3219 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3221 specpdl_ptr
->func
= function
;
3222 specpdl_ptr
->symbol
= Qnil
;
3223 specpdl_ptr
->old_value
= arg
;
3228 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3230 Lisp_Object quitf
= Vquit_flag
;
3231 struct gcpro gcpro1
, gcpro2
;
3233 GCPRO2 (value
, quitf
);
3236 while (specpdl_ptr
!= specpdl
+ count
)
3238 /* Copy the binding, and decrement specpdl_ptr, before we do
3239 the work to unbind it. We decrement first
3240 so that an error in unbinding won't try to unbind
3241 the same entry again, and we copy the binding first
3242 in case more bindings are made during some of the code we run. */
3244 struct specbinding this_binding
;
3245 this_binding
= *--specpdl_ptr
;
3247 if (this_binding
.func
!= 0)
3248 (*this_binding
.func
) (this_binding
.old_value
);
3249 /* If the symbol is a list, it is really (SYMBOL WHERE
3250 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3251 frame. If WHERE is a buffer or frame, this indicates we
3252 bound a variable that had a buffer-local or frame-local
3253 binding. WHERE nil means that the variable had the default
3254 value when it was bound. CURRENT-BUFFER is the buffer that
3255 was current when the variable was bound. */
3256 else if (CONSP (this_binding
.symbol
))
3258 Lisp_Object symbol
, where
;
3260 symbol
= XCAR (this_binding
.symbol
);
3261 where
= XCAR (XCDR (this_binding
.symbol
));
3264 Fset_default (symbol
, this_binding
.old_value
);
3265 /* If `where' is non-nil, reset the value in the appropriate
3266 local binding, but only if that binding still exists. */
3267 else if (BUFFERP (where
)
3268 ? !NILP (Flocal_variable_p (symbol
, where
))
3269 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3270 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3272 /* If variable has a trivial value (no forwarding), we can
3273 just set it. No need to check for constant symbols here,
3274 since that was already done by specbind. */
3275 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3276 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3277 this_binding
.old_value
);
3279 /* NOTE: we only ever come here if make_local_foo was used for
3280 the first time on this var within this let. */
3281 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3284 if (NILP (Vquit_flag
) && !NILP (quitf
))
3291 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3292 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3293 A special variable is one that will be bound dynamically, even in a
3294 context where binding is lexical by default. */)
3295 (Lisp_Object symbol
)
3297 CHECK_SYMBOL (symbol
);
3298 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3302 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3303 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3304 The debugger is entered when that frame exits, if the flag is non-nil. */)
3305 (Lisp_Object level
, Lisp_Object flag
)
3307 register struct backtrace
*backlist
= backtrace_list
;
3308 register EMACS_INT i
;
3310 CHECK_NUMBER (level
);
3312 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3314 backlist
= backlist
->next
;
3318 backlist
->debug_on_exit
= !NILP (flag
);
3323 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3324 doc
: /* Print a trace of Lisp function calls currently active.
3325 Output stream used is value of `standard-output'. */)
3328 register struct backtrace
*backlist
= backtrace_list
;
3331 struct gcpro gcpro1
;
3332 Lisp_Object old_print_level
= Vprint_level
;
3334 if (NILP (Vprint_level
))
3335 XSETFASTINT (Vprint_level
, 8);
3342 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3343 if (backlist
->nargs
== UNEVALLED
)
3345 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3346 write_string ("\n", -1);
3350 tem
= *backlist
->function
;
3351 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3352 write_string ("(", -1);
3353 if (backlist
->nargs
== MANY
)
3354 { /* FIXME: Can this happen? */
3356 for (tail
= *backlist
->args
, i
= 0;
3358 tail
= Fcdr (tail
), i
= 1)
3360 if (i
) write_string (" ", -1);
3361 Fprin1 (Fcar (tail
), Qnil
);
3367 for (i
= 0; i
< backlist
->nargs
; i
++)
3369 if (i
) write_string (" ", -1);
3370 Fprin1 (backlist
->args
[i
], Qnil
);
3373 write_string (")\n", -1);
3375 backlist
= backlist
->next
;
3378 Vprint_level
= old_print_level
;
3383 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3384 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3385 If that frame has not evaluated the arguments yet (or is a special form),
3386 the value is (nil FUNCTION ARG-FORMS...).
3387 If that frame has evaluated its arguments and called its function already,
3388 the value is (t FUNCTION ARG-VALUES...).
3389 A &rest arg is represented as the tail of the list ARG-VALUES.
3390 FUNCTION is whatever was supplied as car of evaluated list,
3391 or a lambda expression for macro calls.
3392 If NFRAMES is more than the number of frames, the value is nil. */)
3393 (Lisp_Object nframes
)
3395 register struct backtrace
*backlist
= backtrace_list
;
3396 register EMACS_INT i
;
3399 CHECK_NATNUM (nframes
);
3401 /* Find the frame requested. */
3402 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3403 backlist
= backlist
->next
;
3407 if (backlist
->nargs
== UNEVALLED
)
3408 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3411 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3412 tem
= *backlist
->args
;
3414 tem
= Flist (backlist
->nargs
, backlist
->args
);
3416 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3423 mark_backtrace (void)
3425 register struct backtrace
*backlist
;
3428 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3430 mark_object (*backlist
->function
);
3432 if (backlist
->nargs
== UNEVALLED
3433 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3436 i
= backlist
->nargs
;
3438 mark_object (backlist
->args
[i
]);
3446 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3447 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3448 If Lisp code tries to increase the total number past this amount,
3449 an error is signaled.
3450 You can safely use a value considerably larger than the default value,
3451 if that proves inconveniently small. However, if you increase it too far,
3452 Emacs could run out of memory trying to make the stack bigger. */);
3454 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3455 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3457 This limit serves to catch infinite recursions for you before they cause
3458 actual stack overflow in C, which would be fatal for Emacs.
3459 You can safely make it considerably larger than its default value,
3460 if that proves inconveniently small. However, if you increase it too far,
3461 Emacs could overflow the real C stack, and crash. */);
3463 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3464 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3465 If the value is t, that means do an ordinary quit.
3466 If the value equals `throw-on-input', that means quit by throwing
3467 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3468 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3469 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3472 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3473 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3474 Note that `quit-flag' will still be set by typing C-g,
3475 so a quit will be signaled as soon as `inhibit-quit' is nil.
3476 To prevent this happening, set `quit-flag' to nil
3477 before making `inhibit-quit' nil. */);
3478 Vinhibit_quit
= Qnil
;
3480 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3481 DEFSYM (Qautoload
, "autoload");
3482 DEFSYM (Qdebug_on_error
, "debug-on-error");
3483 DEFSYM (Qmacro
, "macro");
3484 DEFSYM (Qdeclare
, "declare");
3486 /* Note that the process handling also uses Qexit, but we don't want
3487 to staticpro it twice, so we just do it here. */
3488 DEFSYM (Qexit
, "exit");
3490 DEFSYM (Qinteractive
, "interactive");
3491 DEFSYM (Qcommandp
, "commandp");
3492 DEFSYM (Qand_rest
, "&rest");
3493 DEFSYM (Qand_optional
, "&optional");
3494 DEFSYM (Qclosure
, "closure");
3495 DEFSYM (Qdebug
, "debug");
3497 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3498 doc
: /* Non-nil means enter debugger if an error is signaled.
3499 Does not apply to errors handled by `condition-case' or those
3500 matched by `debug-ignored-errors'.
3501 If the value is a list, an error only means to enter the debugger
3502 if one of its condition symbols appears in the list.
3503 When you evaluate an expression interactively, this variable
3504 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3505 The command `toggle-debug-on-error' toggles this.
3506 See also the variable `debug-on-quit'. */);
3507 Vdebug_on_error
= Qnil
;
3509 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3510 doc
: /* List of errors for which the debugger should not be called.
3511 Each element may be a condition-name or a regexp that matches error messages.
3512 If any element applies to a given error, that error skips the debugger
3513 and just returns to top level.
3514 This overrides the variable `debug-on-error'.
3515 It does not apply to errors handled by `condition-case'. */);
3516 Vdebug_ignored_errors
= Qnil
;
3518 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3519 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3520 Does not apply if quit is handled by a `condition-case'. */);
3523 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3524 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3526 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3527 doc
: /* Non-nil means debugger may continue execution.
3528 This is nil when the debugger is called under circumstances where it
3529 might not be safe to continue. */);
3530 debugger_may_continue
= 1;
3532 DEFVAR_LISP ("debugger", Vdebugger
,
3533 doc
: /* Function to call to invoke debugger.
3534 If due to frame exit, args are `exit' and the value being returned;
3535 this function's value will be returned instead of that.
3536 If due to error, args are `error' and a list of the args to `signal'.
3537 If due to `apply' or `funcall' entry, one arg, `lambda'.
3538 If due to `eval' entry, one arg, t. */);
3541 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3542 doc
: /* If non-nil, this is a function for `signal' to call.
3543 It receives the same arguments that `signal' was given.
3544 The Edebug package uses this to regain control. */);
3545 Vsignal_hook_function
= Qnil
;
3547 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3548 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3549 Note that `debug-on-error', `debug-on-quit' and friends
3550 still determine whether to handle the particular condition. */);
3551 Vdebug_on_signal
= Qnil
;
3553 /* When lexical binding is being used,
3554 Vinternal_interpreter_environment is non-nil, and contains an alist
3555 of lexically-bound variable, or (t), indicating an empty
3556 environment. The lisp name of this variable would be
3557 `internal-interpreter-environment' if it weren't hidden.
3558 Every element of this list can be either a cons (VAR . VAL)
3559 specifying a lexical binding, or a single symbol VAR indicating
3560 that this variable should use dynamic scoping. */
3561 DEFSYM (Qinternal_interpreter_environment
,
3562 "internal-interpreter-environment");
3563 DEFVAR_LISP ("internal-interpreter-environment",
3564 Vinternal_interpreter_environment
,
3565 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3566 When lexical binding is not being used, this variable is nil.
3567 A value of `(t)' indicates an empty environment, otherwise it is an
3568 alist of active lexical bindings. */);
3569 Vinternal_interpreter_environment
= Qnil
;
3570 /* Don't export this variable to Elisp, so no one can mess with it
3571 (Just imagine if someone makes it buffer-local). */
3572 Funintern (Qinternal_interpreter_environment
, Qnil
);
3574 DEFSYM (Vrun_hooks
, "run-hooks");
3576 staticpro (&Vautoload_queue
);
3577 Vautoload_queue
= Qnil
;
3578 staticpro (&Vsignaling_function
);
3579 Vsignaling_function
= Qnil
;
3581 inhibit_lisp_code
= Qnil
;
3592 defsubr (&Sfunction
);
3594 defsubr (&Sdefvaralias
);
3595 defsubr (&Sdefconst
);
3596 defsubr (&Smake_var_non_special
);
3600 defsubr (&Smacroexpand
);
3603 defsubr (&Sunwind_protect
);
3604 defsubr (&Scondition_case
);
3606 defsubr (&Sinteractive_p
);
3607 defsubr (&Scalled_interactively_p
);
3608 defsubr (&Scommandp
);
3609 defsubr (&Sautoload
);
3612 defsubr (&Sfuncall
);
3613 defsubr (&Srun_hooks
);
3614 defsubr (&Srun_hook_with_args
);
3615 defsubr (&Srun_hook_with_args_until_success
);
3616 defsubr (&Srun_hook_with_args_until_failure
);
3617 defsubr (&Srun_hook_wrapped
);
3618 defsubr (&Sfetch_bytecode
);
3619 defsubr (&Sbacktrace_debug
);
3620 defsubr (&Sbacktrace
);
3621 defsubr (&Sbacktrace_frame
);
3622 defsubr (&Sspecial_variable_p
);
3623 defsubr (&Sfunctionp
);