1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "blockinput.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
37 struct backtrace
*next
;
38 Lisp_Object
*function
;
39 Lisp_Object
*args
; /* Points to vector of args. */
40 ptrdiff_t nargs
; /* Length of vector. */
41 /* Nonzero means call value of debugger when done with this operation. */
42 unsigned int debug_on_exit
: 1;
45 static struct backtrace
*backtrace_list
;
50 struct catchtag
*catchlist
;
52 /* Chain of condition handlers currently in effect.
53 The elements of this chain are contained in the stack frames
54 of Fcondition_case and internal_condition_case.
55 When an error is signaled (by calling Fsignal, below),
56 this chain is searched for an element that applies. */
61 struct handler
*handlerlist
;
64 /* Count levels of GCPRO to detect failure to UNGCPRO. */
68 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
;
69 Lisp_Object Qinhibit_quit
;
70 Lisp_Object Qand_rest
;
71 static Lisp_Object Qand_optional
;
72 static Lisp_Object Qdebug_on_error
;
73 static Lisp_Object Qdeclare
;
74 Lisp_Object Qinternal_interpreter_environment
, Qclosure
;
76 static Lisp_Object Qdebug
;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
82 Lisp_Object Vrun_hooks
;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue
;
91 /* Current number of specbindings allocated in specpdl. */
93 ptrdiff_t specpdl_size
;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding
*specpdl
;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding
*specpdl_ptr
;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth
;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
114 static EMACS_INT when_entered_debugger
;
116 /* The function from which the last `signal' was called. Set in
119 Lisp_Object Vsignaling_function
;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
127 /* If non-nil, Lisp code must not be run since some part of Emacs is
128 in an inconsistent state. Currently, x-create-frame uses this to
129 avoid triggering window-configuration-change-hook while the new
130 frame is half-initialized. */
131 Lisp_Object inhibit_lisp_code
;
133 static Lisp_Object
funcall_lambda (Lisp_Object
, ptrdiff_t, Lisp_Object
*);
134 static int interactive_p (int);
135 static Lisp_Object
apply_lambda (Lisp_Object fun
, Lisp_Object args
);
136 static Lisp_Object
Ffetch_bytecode (Lisp_Object
);
139 init_eval_once (void)
142 specpdl
= (struct specbinding
*) xmalloc (size
* sizeof (struct specbinding
));
144 specpdl_ptr
= specpdl
;
145 /* Don't forget to update docs (lispref node "Local Variables"). */
146 max_specpdl_size
= 1300; /* 1000 is not enough for CEDET's c-by.el. */
147 max_lisp_eval_depth
= 600;
155 specpdl_ptr
= specpdl
;
160 debug_on_next_call
= 0;
165 /* This is less than the initial value of num_nonmacro_input_events. */
166 when_entered_debugger
= -1;
169 /* Unwind-protect function used by call_debugger. */
172 restore_stack_limits (Lisp_Object data
)
174 max_specpdl_size
= XINT (XCAR (data
));
175 max_lisp_eval_depth
= XINT (XCDR (data
));
179 /* Call the Lisp debugger, giving it argument ARG. */
182 call_debugger (Lisp_Object arg
)
184 int debug_while_redisplaying
;
185 ptrdiff_t count
= SPECPDL_INDEX ();
187 EMACS_INT old_max
= max_specpdl_size
;
189 /* Temporarily bump up the stack limits,
190 so the debugger won't run out of stack. */
192 max_specpdl_size
+= 1;
193 record_unwind_protect (restore_stack_limits
,
194 Fcons (make_number (old_max
),
195 make_number (max_lisp_eval_depth
)));
196 max_specpdl_size
= old_max
;
198 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
199 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
201 if (max_specpdl_size
- 100 < SPECPDL_INDEX ())
202 max_specpdl_size
= SPECPDL_INDEX () + 100;
204 #ifdef HAVE_WINDOW_SYSTEM
205 if (display_hourglass_p
)
209 debug_on_next_call
= 0;
210 when_entered_debugger
= num_nonmacro_input_events
;
212 /* Resetting redisplaying_p to 0 makes sure that debug output is
213 displayed if the debugger is invoked during redisplay. */
214 debug_while_redisplaying
= redisplaying_p
;
216 specbind (intern ("debugger-may-continue"),
217 debug_while_redisplaying
? Qnil
: Qt
);
218 specbind (Qinhibit_redisplay
, Qnil
);
219 specbind (Qdebug_on_error
, Qnil
);
221 #if 0 /* Binding this prevents execution of Lisp code during
222 redisplay, which necessarily leads to display problems. */
223 specbind (Qinhibit_eval_during_redisplay
, Qt
);
226 val
= apply1 (Vdebugger
, arg
);
228 /* Interrupting redisplay and resuming it later is not safe under
229 all circumstances. So, when the debugger returns, abort the
230 interrupted redisplay by going back to the top-level. */
231 if (debug_while_redisplaying
)
234 return unbind_to (count
, val
);
238 do_debug_on_call (Lisp_Object code
)
240 debug_on_next_call
= 0;
241 backtrace_list
->debug_on_exit
= 1;
242 call_debugger (Fcons (code
, Qnil
));
245 /* NOTE!!! Every function that can call EVAL must protect its args
246 and temporaries from garbage collection while it needs them.
247 The definition of `For' shows what you have to do. */
249 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
250 doc
: /* Eval args until one of them yields non-nil, then return that value.
251 The remaining args are not evalled at all.
252 If all args return nil, return nil.
253 usage: (or CONDITIONS...) */)
256 register Lisp_Object val
= Qnil
;
263 val
= eval_sub (XCAR (args
));
273 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
274 doc
: /* Eval args until one of them yields nil, then return nil.
275 The remaining args are not evalled at all.
276 If no arg yields nil, return the last arg's value.
277 usage: (and CONDITIONS...) */)
280 register Lisp_Object val
= Qt
;
287 val
= eval_sub (XCAR (args
));
297 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
298 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
299 Returns the value of THEN or the value of the last of the ELSE's.
300 THEN must be one expression, but ELSE... can be zero or more expressions.
301 If COND yields nil, and there are no ELSE's, the value is nil.
302 usage: (if COND THEN ELSE...) */)
305 register Lisp_Object cond
;
309 cond
= eval_sub (Fcar (args
));
313 return eval_sub (Fcar (Fcdr (args
)));
314 return Fprogn (Fcdr (Fcdr (args
)));
317 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
318 doc
: /* Try each clause until one succeeds.
319 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
320 and, if the value is non-nil, this clause succeeds:
321 then the expressions in BODY are evaluated and the last one's
322 value is the value of the cond-form.
323 If no clause succeeds, cond returns nil.
324 If a clause has one element, as in (CONDITION),
325 CONDITION's value if non-nil is returned from the cond-form.
326 usage: (cond CLAUSES...) */)
329 register Lisp_Object clause
, val
;
336 clause
= Fcar (args
);
337 val
= eval_sub (Fcar (clause
));
340 if (!EQ (XCDR (clause
), Qnil
))
341 val
= Fprogn (XCDR (clause
));
351 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
352 doc
: /* Eval BODY forms sequentially and return value of last one.
353 usage: (progn BODY...) */)
356 register Lisp_Object val
= Qnil
;
363 val
= eval_sub (XCAR (args
));
371 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
372 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
373 The value of FIRST is saved during the evaluation of the remaining args,
374 whose values are discarded.
375 usage: (prog1 FIRST BODY...) */)
379 register Lisp_Object args_left
;
380 struct gcpro gcpro1
, gcpro2
;
386 val
= eval_sub (XCAR (args_left
));
387 while (CONSP (args_left
= XCDR (args_left
)))
388 eval_sub (XCAR (args_left
));
394 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
395 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
396 The value of FORM2 is saved during the evaluation of the
397 remaining args, whose values are discarded.
398 usage: (prog2 FORM1 FORM2 BODY...) */)
404 eval_sub (XCAR (args
));
406 return Fprog1 (XCDR (args
));
409 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
410 doc
: /* Set each SYM to the value of its VAL.
411 The symbols SYM are variables; they are literal (not evaluated).
412 The values VAL are expressions; they are evaluated.
413 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
414 The second VAL is not computed until after the first SYM is set, and so on;
415 each VAL can use the new value of variables set earlier in the `setq'.
416 The return value of the `setq' form is the value of the last VAL.
417 usage: (setq [SYM VAL]...) */)
420 register Lisp_Object args_left
;
421 register Lisp_Object val
, sym
, lex_binding
;
432 val
= eval_sub (Fcar (Fcdr (args_left
)));
433 sym
= Fcar (args_left
);
435 /* Like for eval_sub, we do not check declared_special here since
436 it's been done when let-binding. */
437 if (!NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
439 && !NILP (lex_binding
440 = Fassq (sym
, Vinternal_interpreter_environment
)))
441 XSETCDR (lex_binding
, val
); /* SYM is lexically bound. */
443 Fset (sym
, val
); /* SYM is dynamically bound. */
445 args_left
= Fcdr (Fcdr (args_left
));
447 while (!NILP (args_left
));
453 DEFUN ("quote", Fquote
, Squote
, 1, UNEVALLED
, 0,
454 doc
: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
455 Warning: `quote' does not construct its return value, but just returns
456 the value that was pre-constructed by the Lisp reader (see info node
457 `(elisp)Printed Representation').
458 This means that '(a . b) is not identical to (cons 'a 'b): the former
459 does not cons. Quoting should be reserved for constants that will
460 never be modified by side-effects, unless you like self-modifying code.
461 See the common pitfall in info node `(elisp)Rearrangement' for an example
462 of unexpected results when a quoted object is modified.
463 usage: (quote ARG) */)
466 if (!NILP (Fcdr (args
)))
467 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
471 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
472 doc
: /* Like `quote', but preferred for objects which are functions.
473 In byte compilation, `function' causes its argument to be compiled.
474 `quote' cannot do that.
475 usage: (function ARG) */)
478 Lisp_Object quoted
= XCAR (args
);
480 if (!NILP (Fcdr (args
)))
481 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
483 if (!NILP (Vinternal_interpreter_environment
)
485 && EQ (XCAR (quoted
), Qlambda
))
486 /* This is a lambda expression within a lexical environment;
487 return an interpreted closure instead of a simple lambda. */
488 return Fcons (Qclosure
, Fcons (Vinternal_interpreter_environment
,
491 /* Simply quote the argument. */
496 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
497 doc
: /* Return t if the containing function was run directly by user input.
498 This means that the function was called with `call-interactively'
499 \(which includes being called as the binding of a key)
500 and input is currently coming from the keyboard (not a keyboard macro),
501 and Emacs is not running in batch mode (`noninteractive' is nil).
503 The only known proper use of `interactive-p' is in deciding whether to
504 display a helpful message, or how to display it. If you're thinking
505 of using it for any other purpose, it is quite likely that you're
506 making a mistake. Think: what do you want to do when the command is
507 called from a keyboard macro?
509 To test whether your function was called with `call-interactively',
510 either (i) add an extra optional argument and give it an `interactive'
511 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
512 use `called-interactively-p'. */)
515 return interactive_p (1) ? Qt
: Qnil
;
519 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
520 doc
: /* Return t if the containing function was called by `call-interactively'.
521 If KIND is `interactive', then only return t if the call was made
522 interactively by the user, i.e. not in `noninteractive' mode nor
523 when `executing-kbd-macro'.
524 If KIND is `any', on the other hand, it will return t for any kind of
525 interactive call, including being called as the binding of a key, or
526 from a keyboard macro, or in `noninteractive' mode.
528 The only known proper use of `interactive' for KIND is in deciding
529 whether to display a helpful message, or how to display it. If you're
530 thinking of using it for any other purpose, it is quite likely that
531 you're making a mistake. Think: what do you want to do when the
532 command is called from a keyboard macro?
534 This function is meant for implementing advice and other
535 function-modifying features. Instead of using this, it is sometimes
536 cleaner to give your function an extra optional argument whose
537 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
538 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
541 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
542 && interactive_p (1)) ? Qt
: Qnil
;
546 /* Return 1 if function in which this appears was called using
549 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
550 called is a built-in. */
553 interactive_p (int exclude_subrs_p
)
555 struct backtrace
*btp
;
558 btp
= backtrace_list
;
560 /* If this isn't a byte-compiled function, there may be a frame at
561 the top for Finteractive_p. If so, skip it. */
562 fun
= Findirect_function (*btp
->function
, Qnil
);
563 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
564 || XSUBR (fun
) == &Scalled_interactively_p
))
567 /* If we're running an Emacs 18-style byte-compiled function, there
568 may be a frame for Fbytecode at the top level. In any version of
569 Emacs there can be Fbytecode frames for subexpressions evaluated
570 inside catch and condition-case. Skip past them.
572 If this isn't a byte-compiled function, then we may now be
573 looking at several frames for special forms. Skip past them. */
575 && (EQ (*btp
->function
, Qbytecode
)
576 || btp
->nargs
== UNEVALLED
))
579 /* `btp' now points at the frame of the innermost function that isn't
580 a special form, ignoring frames for Finteractive_p and/or
581 Fbytecode at the top. If this frame is for a built-in function
582 (such as load or eval-region) return nil. */
583 fun
= Findirect_function (*btp
->function
, Qnil
);
584 if (exclude_subrs_p
&& SUBRP (fun
))
587 /* `btp' points to the frame of a Lisp function that called interactive-p.
588 Return t if that function was called interactively. */
589 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
595 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
596 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
597 Aliased variables always have the same value; setting one sets the other.
598 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
599 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
600 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
601 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
602 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
603 The return value is BASE-VARIABLE. */)
604 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
606 struct Lisp_Symbol
*sym
;
608 CHECK_SYMBOL (new_alias
);
609 CHECK_SYMBOL (base_variable
);
611 sym
= XSYMBOL (new_alias
);
614 /* Not sure why, but why not? */
615 error ("Cannot make a constant an alias");
617 switch (sym
->redirect
)
619 case SYMBOL_FORWARDED
:
620 error ("Cannot make an internal variable an alias");
621 case SYMBOL_LOCALIZED
:
622 error ("Don't know how to make a localized variable an alias");
625 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
626 If n_a is bound, but b_v is not, set the value of b_v to n_a,
627 so that old-code that affects n_a before the aliasing is setup
629 if (NILP (Fboundp (base_variable
)))
630 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
633 struct specbinding
*p
;
635 for (p
= specpdl_ptr
; p
> specpdl
; )
636 if ((--p
)->func
== NULL
638 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
639 error ("Don't know how to make a let-bound variable an alias");
642 sym
->declared_special
= 1;
643 XSYMBOL (base_variable
)->declared_special
= 1;
644 sym
->redirect
= SYMBOL_VARALIAS
;
645 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
646 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
647 LOADHIST_ATTACH (new_alias
);
648 /* Even if docstring is nil: remove old docstring. */
649 Fput (new_alias
, Qvariable_documentation
, docstring
);
651 return base_variable
;
655 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
656 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
657 You are not required to define a variable in order to use it, but
658 defining it lets you supply an initial value and documentation, which
659 can be referred to by the Emacs help facilities and other programming
660 tools. The `defvar' form also declares the variable as \"special\",
661 so that it is always dynamically bound even if `lexical-binding' is t.
663 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
664 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
665 default value is what is set; buffer-local values are not affected.
666 If INITVALUE is missing, SYMBOL's value is not set.
668 If SYMBOL has a local binding, then this form affects the local
669 binding. This is usually not what you want. Thus, if you need to
670 load a file defining variables, with this form or with `defconst' or
671 `defcustom', you should always load that file _outside_ any bindings
672 for these variables. \(`defconst' and `defcustom' behave similarly in
675 The optional argument DOCSTRING is a documentation string for the
678 To define a user option, use `defcustom' instead of `defvar'.
679 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
682 register Lisp_Object sym
, tem
, tail
;
686 if (!NILP (Fcdr (Fcdr (tail
))))
687 error ("Too many arguments");
689 tem
= Fdefault_boundp (sym
);
692 /* Do it before evaluating the initial value, for self-references. */
693 XSYMBOL (sym
)->declared_special
= 1;
695 if (SYMBOL_CONSTANT_P (sym
))
697 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
698 Lisp_Object tem1
= Fcar (tail
);
700 && EQ (XCAR (tem1
), Qquote
)
701 && CONSP (XCDR (tem1
))
702 && EQ (XCAR (XCDR (tem1
)), sym
)))
703 error ("Constant symbol `%s' specified in defvar",
704 SDATA (SYMBOL_NAME (sym
)));
708 Fset_default (sym
, eval_sub (Fcar (tail
)));
710 { /* Check if there is really a global binding rather than just a let
711 binding that shadows the global unboundness of the var. */
712 volatile struct specbinding
*pdl
= specpdl_ptr
;
713 while (pdl
> specpdl
)
715 if (EQ ((--pdl
)->symbol
, sym
) && !pdl
->func
716 && EQ (pdl
->old_value
, Qunbound
))
718 message_with_string ("Warning: defvar ignored because %s is let-bound",
719 SYMBOL_NAME (sym
), 1);
728 if (!NILP (Vpurify_flag
))
729 tem
= Fpurecopy (tem
);
730 Fput (sym
, Qvariable_documentation
, tem
);
732 LOADHIST_ATTACH (sym
);
734 else if (!NILP (Vinternal_interpreter_environment
)
735 && !XSYMBOL (sym
)->declared_special
)
736 /* A simple (defvar foo) with lexical scoping does "nothing" except
737 declare that var to be dynamically scoped *locally* (i.e. within
738 the current file or let-block). */
739 Vinternal_interpreter_environment
=
740 Fcons (sym
, Vinternal_interpreter_environment
);
743 /* Simple (defvar <var>) should not count as a definition at all.
744 It could get in the way of other definitions, and unloading this
745 package could try to make the variable unbound. */
751 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
752 doc
: /* Define SYMBOL as a constant variable.
753 This declares that neither programs nor users should ever change the
754 value. This constancy is not actually enforced by Emacs Lisp, but
755 SYMBOL is marked as a special variable so that it is never lexically
758 The `defconst' form always sets the value of SYMBOL to the result of
759 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
760 what is set; buffer-local values are not affected. If SYMBOL has a
761 local binding, then this form sets the local binding's value.
762 However, you should normally not make local bindings for variables
763 defined with this form.
765 The optional DOCSTRING specifies the variable's documentation string.
766 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
769 register Lisp_Object sym
, tem
;
772 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
773 error ("Too many arguments");
775 tem
= eval_sub (Fcar (Fcdr (args
)));
776 if (!NILP (Vpurify_flag
))
777 tem
= Fpurecopy (tem
);
778 Fset_default (sym
, tem
);
779 XSYMBOL (sym
)->declared_special
= 1;
780 tem
= Fcar (Fcdr (Fcdr (args
)));
783 if (!NILP (Vpurify_flag
))
784 tem
= Fpurecopy (tem
);
785 Fput (sym
, Qvariable_documentation
, tem
);
787 Fput (sym
, Qrisky_local_variable
, Qt
);
788 LOADHIST_ATTACH (sym
);
792 /* Make SYMBOL lexically scoped. */
793 DEFUN ("internal-make-var-non-special", Fmake_var_non_special
,
794 Smake_var_non_special
, 1, 1, 0,
795 doc
: /* Internal function. */)
798 CHECK_SYMBOL (symbol
);
799 XSYMBOL (symbol
)->declared_special
= 0;
804 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
805 doc
: /* Bind variables according to VARLIST then eval BODY.
806 The value of the last form in BODY is returned.
807 Each element of VARLIST is a symbol (which is bound to nil)
808 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
809 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
810 usage: (let* VARLIST BODY...) */)
813 Lisp_Object varlist
, var
, val
, elt
, lexenv
;
814 ptrdiff_t count
= SPECPDL_INDEX ();
815 struct gcpro gcpro1
, gcpro2
, gcpro3
;
817 GCPRO3 (args
, elt
, varlist
);
819 lexenv
= Vinternal_interpreter_environment
;
821 varlist
= Fcar (args
);
822 while (CONSP (varlist
))
826 elt
= XCAR (varlist
);
832 else if (! NILP (Fcdr (Fcdr (elt
))))
833 signal_error ("`let' bindings can have only one value-form", elt
);
837 val
= eval_sub (Fcar (Fcdr (elt
)));
840 if (!NILP (lexenv
) && SYMBOLP (var
)
841 && !XSYMBOL (var
)->declared_special
842 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
843 /* Lexically bind VAR by adding it to the interpreter's binding
847 = Fcons (Fcons (var
, val
), Vinternal_interpreter_environment
);
848 if (EQ (Vinternal_interpreter_environment
, lexenv
))
849 /* Save the old lexical environment on the specpdl stack,
850 but only for the first lexical binding, since we'll never
851 need to revert to one of the intermediate ones. */
852 specbind (Qinternal_interpreter_environment
, newenv
);
854 Vinternal_interpreter_environment
= newenv
;
859 varlist
= XCDR (varlist
);
862 val
= Fprogn (Fcdr (args
));
863 return unbind_to (count
, val
);
866 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
867 doc
: /* Bind variables according to VARLIST then eval BODY.
868 The value of the last form in BODY is returned.
869 Each element of VARLIST is a symbol (which is bound to nil)
870 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
871 All the VALUEFORMs are evalled before any symbols are bound.
872 usage: (let VARLIST BODY...) */)
875 Lisp_Object
*temps
, tem
, lexenv
;
876 register Lisp_Object elt
, varlist
;
877 ptrdiff_t count
= SPECPDL_INDEX ();
879 struct gcpro gcpro1
, gcpro2
;
882 varlist
= Fcar (args
);
884 /* Make space to hold the values to give the bound variables. */
885 elt
= Flength (varlist
);
886 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
888 /* Compute the values and store them in `temps'. */
890 GCPRO2 (args
, *temps
);
893 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
896 elt
= XCAR (varlist
);
898 temps
[argnum
++] = Qnil
;
899 else if (! NILP (Fcdr (Fcdr (elt
))))
900 signal_error ("`let' bindings can have only one value-form", elt
);
902 temps
[argnum
++] = eval_sub (Fcar (Fcdr (elt
)));
903 gcpro2
.nvars
= argnum
;
907 lexenv
= Vinternal_interpreter_environment
;
909 varlist
= Fcar (args
);
910 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
914 elt
= XCAR (varlist
);
915 var
= SYMBOLP (elt
) ? elt
: Fcar (elt
);
916 tem
= temps
[argnum
++];
918 if (!NILP (lexenv
) && SYMBOLP (var
)
919 && !XSYMBOL (var
)->declared_special
920 && NILP (Fmemq (var
, Vinternal_interpreter_environment
)))
921 /* Lexically bind VAR by adding it to the lexenv alist. */
922 lexenv
= Fcons (Fcons (var
, tem
), lexenv
);
924 /* Dynamically bind VAR. */
928 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
929 /* Instantiate a new lexical environment. */
930 specbind (Qinternal_interpreter_environment
, lexenv
);
932 elt
= Fprogn (Fcdr (args
));
934 return unbind_to (count
, elt
);
937 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
938 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
939 The order of execution is thus TEST, BODY, TEST, BODY and so on
940 until TEST returns nil.
941 usage: (while TEST BODY...) */)
944 Lisp_Object test
, body
;
945 struct gcpro gcpro1
, gcpro2
;
951 while (!NILP (eval_sub (test
)))
961 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
962 doc
: /* Return result of expanding macros at top level of FORM.
963 If FORM is not a macro call, it is returned unchanged.
964 Otherwise, the macro is expanded and the expansion is considered
965 in place of FORM. When a non-macro-call results, it is returned.
967 The second optional arg ENVIRONMENT specifies an environment of macro
968 definitions to shadow the loaded ones for use in file byte-compilation. */)
969 (Lisp_Object form
, Lisp_Object environment
)
971 /* With cleanups from Hallvard Furuseth. */
972 register Lisp_Object expander
, sym
, def
, tem
;
976 /* Come back here each time we expand a macro call,
977 in case it expands into another macro call. */
980 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
981 def
= sym
= XCAR (form
);
983 /* Trace symbols aliases to other symbols
984 until we get a symbol that is not an alias. */
985 while (SYMBOLP (def
))
989 tem
= Fassq (sym
, environment
);
992 def
= XSYMBOL (sym
)->function
;
993 if (!EQ (def
, Qunbound
))
998 /* Right now TEM is the result from SYM in ENVIRONMENT,
999 and if TEM is nil then DEF is SYM's function definition. */
1002 /* SYM is not mentioned in ENVIRONMENT.
1003 Look at its function definition. */
1004 if (EQ (def
, Qunbound
) || !CONSP (def
))
1005 /* Not defined or definition not suitable. */
1007 if (EQ (XCAR (def
), Qautoload
))
1009 /* Autoloading function: will it be a macro when loaded? */
1010 tem
= Fnth (make_number (4), def
);
1011 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1012 /* Yes, load it and try again. */
1014 struct gcpro gcpro1
;
1016 do_autoload (def
, sym
);
1023 else if (!EQ (XCAR (def
), Qmacro
))
1025 else expander
= XCDR (def
);
1029 expander
= XCDR (tem
);
1030 if (NILP (expander
))
1034 Lisp_Object newform
= apply1 (expander
, XCDR (form
));
1035 if (EQ (form
, newform
))
1044 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1045 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1046 TAG is evalled to get the tag to use; it must not be nil.
1048 Then the BODY is executed.
1049 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1050 If no throw happens, `catch' returns the value of the last BODY form.
1051 If a throw happens, it specifies the value to return from `catch'.
1052 usage: (catch TAG BODY...) */)
1055 register Lisp_Object tag
;
1056 struct gcpro gcpro1
;
1059 tag
= eval_sub (Fcar (args
));
1061 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1064 /* Set up a catch, then call C function FUNC on argument ARG.
1065 FUNC should return a Lisp_Object.
1066 This is how catches are done from within C code. */
1069 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1071 /* This structure is made part of the chain `catchlist'. */
1074 /* Fill in the components of c, and put it on the list. */
1078 c
.backlist
= backtrace_list
;
1079 c
.handlerlist
= handlerlist
;
1080 c
.lisp_eval_depth
= lisp_eval_depth
;
1081 c
.pdlcount
= SPECPDL_INDEX ();
1082 c
.poll_suppress_count
= poll_suppress_count
;
1083 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1084 c
.gcpro
= gcprolist
;
1085 c
.byte_stack
= byte_stack_list
;
1089 if (! _setjmp (c
.jmp
))
1090 c
.val
= (*func
) (arg
);
1092 /* Throw works by a longjmp that comes right here. */
1097 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1098 jump to that CATCH, returning VALUE as the value of that catch.
1100 This is the guts Fthrow and Fsignal; they differ only in the way
1101 they choose the catch tag to throw to. A catch tag for a
1102 condition-case form has a TAG of Qnil.
1104 Before each catch is discarded, unbind all special bindings and
1105 execute all unwind-protect clauses made above that catch. Unwind
1106 the handler stack as we go, so that the proper handlers are in
1107 effect for each unwind-protect clause we run. At the end, restore
1108 some static info saved in CATCH, and longjmp to the location
1111 This is used for correct unwinding in Fthrow and Fsignal. */
1113 static _Noreturn
void
1114 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1118 /* Save the value in the tag. */
1121 /* Restore certain special C variables. */
1122 set_poll_suppress_count (catch->poll_suppress_count
);
1123 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1124 handling_signal
= 0;
1129 last_time
= catchlist
== catch;
1131 /* Unwind the specpdl stack, and then restore the proper set of
1133 unbind_to (catchlist
->pdlcount
, Qnil
);
1134 handlerlist
= catchlist
->handlerlist
;
1135 catchlist
= catchlist
->next
;
1137 while (! last_time
);
1140 /* If x_catch_errors was done, turn it off now.
1141 (First we give unbind_to a chance to do that.) */
1142 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1143 The catch must remain in effect during that delicate
1144 state. --lorentey */
1145 x_fully_uncatch_errors ();
1149 byte_stack_list
= catch->byte_stack
;
1150 gcprolist
= catch->gcpro
;
1152 gcpro_level
= gcprolist
? gcprolist
->level
+ 1 : 0;
1154 backtrace_list
= catch->backlist
;
1155 lisp_eval_depth
= catch->lisp_eval_depth
;
1157 _longjmp (catch->jmp
, 1);
1160 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1161 doc
: /* Throw to the catch for TAG and return VALUE from it.
1162 Both TAG and VALUE are evalled. */)
1163 (register Lisp_Object tag
, Lisp_Object value
)
1165 register struct catchtag
*c
;
1168 for (c
= catchlist
; c
; c
= c
->next
)
1170 if (EQ (c
->tag
, tag
))
1171 unwind_to_catch (c
, value
);
1173 xsignal2 (Qno_catch
, tag
, value
);
1177 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1178 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1179 If BODYFORM completes normally, its value is returned
1180 after executing the UNWINDFORMS.
1181 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1182 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1186 ptrdiff_t count
= SPECPDL_INDEX ();
1188 record_unwind_protect (Fprogn
, Fcdr (args
));
1189 val
= eval_sub (Fcar (args
));
1190 return unbind_to (count
, val
);
1193 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1194 doc
: /* Regain control when an error is signaled.
1195 Executes BODYFORM and returns its value if no error happens.
1196 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1197 where the BODY is made of Lisp expressions.
1199 A handler is applicable to an error
1200 if CONDITION-NAME is one of the error's condition names.
1201 If an error happens, the first applicable handler is run.
1203 The car of a handler may be a list of condition names instead of a
1204 single condition name; then it handles all of them. If the special
1205 condition name `debug' is present in this list, it allows another
1206 condition in the list to run the debugger if `debug-on-error' and the
1207 other usual mechanisms says it should (otherwise, `condition-case'
1208 suppresses the debugger).
1210 When a handler handles an error, control returns to the `condition-case'
1211 and it executes the handler's BODY...
1212 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1213 \(If VAR is nil, the handler can't access that information.)
1214 Then the value of the last BODY form is returned from the `condition-case'
1217 See also the function `signal' for more info.
1218 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1221 register Lisp_Object bodyform
, handlers
;
1222 volatile Lisp_Object var
;
1225 bodyform
= Fcar (Fcdr (args
));
1226 handlers
= Fcdr (Fcdr (args
));
1228 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1231 /* Like Fcondition_case, but the args are separate
1232 rather than passed in a list. Used by Fbyte_code. */
1235 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1236 Lisp_Object handlers
)
1244 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1250 && (SYMBOLP (XCAR (tem
))
1251 || CONSP (XCAR (tem
))))))
1252 error ("Invalid condition handler: %s",
1253 SDATA (Fprin1_to_string (tem
, Qt
)));
1258 c
.backlist
= backtrace_list
;
1259 c
.handlerlist
= handlerlist
;
1260 c
.lisp_eval_depth
= lisp_eval_depth
;
1261 c
.pdlcount
= SPECPDL_INDEX ();
1262 c
.poll_suppress_count
= poll_suppress_count
;
1263 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1264 c
.gcpro
= gcprolist
;
1265 c
.byte_stack
= byte_stack_list
;
1266 if (_setjmp (c
.jmp
))
1269 specbind (h
.var
, c
.val
);
1270 val
= Fprogn (Fcdr (h
.chosen_clause
));
1272 /* Note that this just undoes the binding of h.var; whoever
1273 longjumped to us unwound the stack to c.pdlcount before
1275 unbind_to (c
.pdlcount
, Qnil
);
1282 h
.handler
= handlers
;
1283 h
.next
= handlerlist
;
1287 val
= eval_sub (bodyform
);
1289 handlerlist
= h
.next
;
1293 /* Call the function BFUN with no arguments, catching errors within it
1294 according to HANDLERS. If there is an error, call HFUN with
1295 one argument which is the data that describes the error:
1298 HANDLERS can be a list of conditions to catch.
1299 If HANDLERS is Qt, catch all errors.
1300 If HANDLERS is Qerror, catch all errors
1301 but allow the debugger to run if that is enabled. */
1304 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1305 Lisp_Object (*hfun
) (Lisp_Object
))
1313 c
.backlist
= backtrace_list
;
1314 c
.handlerlist
= handlerlist
;
1315 c
.lisp_eval_depth
= lisp_eval_depth
;
1316 c
.pdlcount
= SPECPDL_INDEX ();
1317 c
.poll_suppress_count
= poll_suppress_count
;
1318 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1319 c
.gcpro
= gcprolist
;
1320 c
.byte_stack
= byte_stack_list
;
1321 if (_setjmp (c
.jmp
))
1323 return (*hfun
) (c
.val
);
1327 h
.handler
= handlers
;
1329 h
.next
= handlerlist
;
1335 handlerlist
= h
.next
;
1339 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1342 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1343 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1351 c
.backlist
= backtrace_list
;
1352 c
.handlerlist
= handlerlist
;
1353 c
.lisp_eval_depth
= lisp_eval_depth
;
1354 c
.pdlcount
= SPECPDL_INDEX ();
1355 c
.poll_suppress_count
= poll_suppress_count
;
1356 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1357 c
.gcpro
= gcprolist
;
1358 c
.byte_stack
= byte_stack_list
;
1359 if (_setjmp (c
.jmp
))
1361 return (*hfun
) (c
.val
);
1365 h
.handler
= handlers
;
1367 h
.next
= handlerlist
;
1371 val
= (*bfun
) (arg
);
1373 handlerlist
= h
.next
;
1377 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1381 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1384 Lisp_Object handlers
,
1385 Lisp_Object (*hfun
) (Lisp_Object
))
1393 c
.backlist
= backtrace_list
;
1394 c
.handlerlist
= handlerlist
;
1395 c
.lisp_eval_depth
= lisp_eval_depth
;
1396 c
.pdlcount
= SPECPDL_INDEX ();
1397 c
.poll_suppress_count
= poll_suppress_count
;
1398 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1399 c
.gcpro
= gcprolist
;
1400 c
.byte_stack
= byte_stack_list
;
1401 if (_setjmp (c
.jmp
))
1403 return (*hfun
) (c
.val
);
1407 h
.handler
= handlers
;
1409 h
.next
= handlerlist
;
1413 val
= (*bfun
) (arg1
, arg2
);
1415 handlerlist
= h
.next
;
1419 /* Like internal_condition_case but call BFUN with NARGS as first,
1420 and ARGS as second argument. */
1423 internal_condition_case_n (Lisp_Object (*bfun
) (ptrdiff_t, Lisp_Object
*),
1426 Lisp_Object handlers
,
1427 Lisp_Object (*hfun
) (Lisp_Object
))
1435 c
.backlist
= backtrace_list
;
1436 c
.handlerlist
= handlerlist
;
1437 c
.lisp_eval_depth
= lisp_eval_depth
;
1438 c
.pdlcount
= SPECPDL_INDEX ();
1439 c
.poll_suppress_count
= poll_suppress_count
;
1440 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1441 c
.gcpro
= gcprolist
;
1442 c
.byte_stack
= byte_stack_list
;
1443 if (_setjmp (c
.jmp
))
1445 return (*hfun
) (c
.val
);
1449 h
.handler
= handlers
;
1451 h
.next
= handlerlist
;
1455 val
= (*bfun
) (nargs
, args
);
1457 handlerlist
= h
.next
;
1462 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
);
1463 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1467 process_quit_flag (void)
1469 Lisp_Object flag
= Vquit_flag
;
1471 if (EQ (flag
, Qkill_emacs
))
1473 if (EQ (Vthrow_on_input
, flag
))
1474 Fthrow (Vthrow_on_input
, Qt
);
1475 Fsignal (Qquit
, Qnil
);
1478 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1479 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1480 This function does not return.
1482 An error symbol is a symbol with an `error-conditions' property
1483 that is a list of condition names.
1484 A handler for any of those names will get to handle this signal.
1485 The symbol `error' should normally be one of them.
1487 DATA should be a list. Its elements are printed as part of the error message.
1488 See Info anchor `(elisp)Definition of signal' for some details on how this
1489 error message is constructed.
1490 If the signal is handled, DATA is made available to the handler.
1491 See also the function `condition-case'. */)
1492 (Lisp_Object error_symbol
, Lisp_Object data
)
1494 /* When memory is full, ERROR-SYMBOL is nil,
1495 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1496 That is a special case--don't do this in other situations. */
1497 Lisp_Object conditions
;
1499 Lisp_Object real_error_symbol
1500 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1501 register Lisp_Object clause
= Qnil
;
1503 struct backtrace
*bp
;
1505 immediate_quit
= handling_signal
= 0;
1507 if (gc_in_progress
|| waiting_for_input
)
1510 #if 0 /* rms: I don't know why this was here,
1511 but it is surely wrong for an error that is handled. */
1512 #ifdef HAVE_WINDOW_SYSTEM
1513 if (display_hourglass_p
)
1514 cancel_hourglass ();
1518 /* This hook is used by edebug. */
1519 if (! NILP (Vsignal_hook_function
)
1520 && ! NILP (error_symbol
))
1522 /* Edebug takes care of restoring these variables when it exits. */
1523 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1524 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1526 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1527 max_specpdl_size
= SPECPDL_INDEX () + 40;
1529 call2 (Vsignal_hook_function
, error_symbol
, data
);
1532 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1534 /* Remember from where signal was called. Skip over the frame for
1535 `signal' itself. If a frame for `error' follows, skip that,
1536 too. Don't do this when ERROR_SYMBOL is nil, because that
1537 is a memory-full error. */
1538 Vsignaling_function
= Qnil
;
1539 if (backtrace_list
&& !NILP (error_symbol
))
1541 bp
= backtrace_list
->next
;
1542 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1544 if (bp
&& bp
->function
)
1545 Vsignaling_function
= *bp
->function
;
1548 for (h
= handlerlist
; h
; h
= h
->next
)
1550 clause
= find_handler_clause (h
->handler
, conditions
);
1555 if (/* Don't run the debugger for a memory-full error.
1556 (There is no room in memory to do that!) */
1557 !NILP (error_symbol
)
1558 && (!NILP (Vdebug_on_signal
)
1559 /* If no handler is present now, try to run the debugger. */
1561 /* A `debug' symbol in the handler list disables the normal
1562 suppression of the debugger. */
1563 || (CONSP (clause
) && CONSP (XCAR (clause
))
1564 && !NILP (Fmemq (Qdebug
, XCAR (clause
))))
1565 /* Special handler that means "print a message and run debugger
1567 || EQ (h
->handler
, Qerror
)))
1570 = maybe_call_debugger (conditions
, error_symbol
, data
);
1571 /* We can't return values to code which signaled an error, but we
1572 can continue code which has signaled a quit. */
1573 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1579 Lisp_Object unwind_data
1580 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1582 h
->chosen_clause
= clause
;
1583 unwind_to_catch (h
->tag
, unwind_data
);
1588 Fthrow (Qtop_level
, Qt
);
1591 if (! NILP (error_symbol
))
1592 data
= Fcons (error_symbol
, data
);
1594 string
= Ferror_message_string (data
);
1595 fatal ("%s", SDATA (string
));
1598 /* Internal version of Fsignal that never returns.
1599 Used for anything but Qquit (which can return from Fsignal). */
1602 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1604 Fsignal (error_symbol
, data
);
1608 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1611 xsignal0 (Lisp_Object error_symbol
)
1613 xsignal (error_symbol
, Qnil
);
1617 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1619 xsignal (error_symbol
, list1 (arg
));
1623 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1625 xsignal (error_symbol
, list2 (arg1
, arg2
));
1629 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1631 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1634 /* Signal `error' with message S, and additional arg ARG.
1635 If ARG is not a genuine list, make it a one-element list. */
1638 signal_error (const char *s
, Lisp_Object arg
)
1640 Lisp_Object tortoise
, hare
;
1642 hare
= tortoise
= arg
;
1643 while (CONSP (hare
))
1650 tortoise
= XCDR (tortoise
);
1652 if (EQ (hare
, tortoise
))
1657 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1659 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1663 /* Return nonzero if LIST is a non-nil atom or
1664 a list containing one of CONDITIONS. */
1667 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1674 while (CONSP (conditions
))
1676 Lisp_Object
this, tail
;
1677 this = XCAR (conditions
);
1678 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1679 if (EQ (XCAR (tail
), this))
1681 conditions
= XCDR (conditions
);
1686 /* Return 1 if an error with condition-symbols CONDITIONS,
1687 and described by SIGNAL-DATA, should skip the debugger
1688 according to debugger-ignored-errors. */
1691 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1694 int first_string
= 1;
1695 Lisp_Object error_message
;
1697 error_message
= Qnil
;
1698 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1700 if (STRINGP (XCAR (tail
)))
1704 error_message
= Ferror_message_string (data
);
1708 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1713 Lisp_Object contail
;
1715 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1716 if (EQ (XCAR (tail
), XCAR (contail
)))
1724 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1725 SIG and DATA describe the signal. There are two ways to pass them:
1726 = SIG is the error symbol, and DATA is the rest of the data.
1727 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1728 This is for memory-full errors only. */
1730 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1732 Lisp_Object combined_data
;
1734 combined_data
= Fcons (sig
, data
);
1737 /* Don't try to run the debugger with interrupts blocked.
1738 The editing loop would return anyway. */
1740 /* Does user want to enter debugger for this kind of error? */
1743 : wants_debugger (Vdebug_on_error
, conditions
))
1744 && ! skip_debugger (conditions
, combined_data
)
1745 /* RMS: What's this for? */
1746 && when_entered_debugger
< num_nonmacro_input_events
)
1748 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1756 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
)
1758 register Lisp_Object h
;
1760 /* t is used by handlers for all conditions, set up by C code. */
1761 if (EQ (handlers
, Qt
))
1764 /* error is used similarly, but means print an error message
1765 and run the debugger if that is enabled. */
1766 if (EQ (handlers
, Qerror
))
1769 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1771 Lisp_Object handler
= XCAR (h
);
1772 Lisp_Object condit
, tem
;
1774 if (!CONSP (handler
))
1776 condit
= XCAR (handler
);
1777 /* Handle a single condition name in handler HANDLER. */
1778 if (SYMBOLP (condit
))
1780 tem
= Fmemq (Fcar (handler
), conditions
);
1784 /* Handle a list of condition names in handler HANDLER. */
1785 else if (CONSP (condit
))
1788 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1790 tem
= Fmemq (XCAR (tail
), conditions
);
1801 /* Dump an error message; called like vprintf. */
1803 verror (const char *m
, va_list ap
)
1806 ptrdiff_t size
= sizeof buf
;
1807 ptrdiff_t size_max
= STRING_BYTES_BOUND
+ 1;
1812 used
= evxprintf (&buffer
, &size
, buf
, size_max
, m
, ap
);
1813 string
= make_string (buffer
, used
);
1817 xsignal1 (Qerror
, string
);
1821 /* Dump an error message; called like printf. */
1825 error (const char *m
, ...)
1833 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1834 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1835 This means it contains a description for how to read arguments to give it.
1836 The value is nil for an invalid function or a symbol with no function
1839 Interactively callable functions include strings and vectors (treated
1840 as keyboard macros), lambda-expressions that contain a top-level call
1841 to `interactive', autoload definitions made by `autoload' with non-nil
1842 fourth argument, and some of the built-in functions of Lisp.
1844 Also, a symbol satisfies `commandp' if its function definition does so.
1846 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1847 then strings and vectors are not accepted. */)
1848 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1850 register Lisp_Object fun
;
1851 register Lisp_Object funcar
;
1852 Lisp_Object if_prop
= Qnil
;
1856 fun
= indirect_function (fun
); /* Check cycles. */
1857 if (NILP (fun
) || EQ (fun
, Qunbound
))
1860 /* Check an `interactive-form' property if present, analogous to the
1861 function-documentation property. */
1863 while (SYMBOLP (fun
))
1865 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1868 fun
= Fsymbol_function (fun
);
1871 /* Emacs primitives are interactive if their DEFUN specifies an
1872 interactive spec. */
1874 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1876 /* Bytecode objects are interactive if they are long enough to
1877 have an element whose index is COMPILED_INTERACTIVE, which is
1878 where the interactive spec is stored. */
1879 else if (COMPILEDP (fun
))
1880 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1883 /* Strings and vectors are keyboard macros. */
1884 if (STRINGP (fun
) || VECTORP (fun
))
1885 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1887 /* Lists may represent commands. */
1890 funcar
= XCAR (fun
);
1891 if (EQ (funcar
, Qclosure
))
1892 return (!NILP (Fassq (Qinteractive
, Fcdr (Fcdr (XCDR (fun
)))))
1894 else if (EQ (funcar
, Qlambda
))
1895 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
1896 else if (EQ (funcar
, Qautoload
))
1897 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
1902 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
1903 doc
: /* Define FUNCTION to autoload from FILE.
1904 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1905 Third arg DOCSTRING is documentation for the function.
1906 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1907 Fifth arg TYPE indicates the type of the object:
1908 nil or omitted says FUNCTION is a function,
1909 `keymap' says FUNCTION is really a keymap, and
1910 `macro' or t says FUNCTION is really a macro.
1911 Third through fifth args give info about the real definition.
1912 They default to nil.
1913 If FUNCTION is already defined other than as an autoload,
1914 this does nothing and returns nil. */)
1915 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
1917 CHECK_SYMBOL (function
);
1918 CHECK_STRING (file
);
1920 /* If function is defined and not as an autoload, don't override. */
1921 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
1922 && !(CONSP (XSYMBOL (function
)->function
)
1923 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
1926 if (NILP (Vpurify_flag
))
1927 /* Only add entries after dumping, because the ones before are
1928 not useful and else we get loads of them from the loaddefs.el. */
1929 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
1930 else if (EQ (docstring
, make_number (0)))
1931 /* `read1' in lread.c has found the docstring starting with "\
1932 and assumed the docstring will be provided by Snarf-documentation, so it
1933 passed us 0 instead. But that leads to accidental sharing in purecopy's
1934 hash-consing, so we use a (hopefully) unique integer instead. */
1935 docstring
= make_number (XUNTAG (function
, Lisp_Symbol
));
1936 return Ffset (function
,
1937 Fpurecopy (list5 (Qautoload
, file
, docstring
,
1938 interactive
, type
)));
1942 un_autoload (Lisp_Object oldqueue
)
1944 register Lisp_Object queue
, first
, second
;
1946 /* Queue to unwind is current value of Vautoload_queue.
1947 oldqueue is the shadowed value to leave in Vautoload_queue. */
1948 queue
= Vautoload_queue
;
1949 Vautoload_queue
= oldqueue
;
1950 while (CONSP (queue
))
1952 first
= XCAR (queue
);
1953 second
= Fcdr (first
);
1954 first
= Fcar (first
);
1955 if (EQ (first
, make_number (0)))
1958 Ffset (first
, second
);
1959 queue
= XCDR (queue
);
1964 /* Load an autoloaded function.
1965 FUNNAME is the symbol which is the function's name.
1966 FUNDEF is the autoload definition (a list). */
1969 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
1971 ptrdiff_t count
= SPECPDL_INDEX ();
1973 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1975 /* This is to make sure that loadup.el gives a clear picture
1976 of what files are preloaded and when. */
1977 if (! NILP (Vpurify_flag
))
1978 error ("Attempt to autoload %s while preparing to dump",
1979 SDATA (SYMBOL_NAME (funname
)));
1982 CHECK_SYMBOL (funname
);
1983 GCPRO3 (fun
, funname
, fundef
);
1985 /* Preserve the match data. */
1986 record_unwind_save_match_data ();
1988 /* If autoloading gets an error (which includes the error of failing
1989 to define the function being called), we use Vautoload_queue
1990 to undo function definitions and `provide' calls made by
1991 the function. We do this in the specific case of autoloading
1992 because autoloading is not an explicit request "load this file",
1993 but rather a request to "call this function".
1995 The value saved here is to be restored into Vautoload_queue. */
1996 record_unwind_protect (un_autoload
, Vautoload_queue
);
1997 Vautoload_queue
= Qt
;
1998 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2000 /* Once loading finishes, don't undo it. */
2001 Vautoload_queue
= Qt
;
2002 unbind_to (count
, Qnil
);
2004 fun
= Findirect_function (fun
, Qnil
);
2006 if (!NILP (Fequal (fun
, fundef
)))
2007 error ("Autoloading failed to define function %s",
2008 SDATA (SYMBOL_NAME (funname
)));
2013 DEFUN ("eval", Feval
, Seval
, 1, 2, 0,
2014 doc
: /* Evaluate FORM and return its value.
2015 If LEXICAL is t, evaluate using lexical scoping. */)
2016 (Lisp_Object form
, Lisp_Object lexical
)
2018 ptrdiff_t count
= SPECPDL_INDEX ();
2019 specbind (Qinternal_interpreter_environment
,
2020 NILP (lexical
) ? Qnil
: Fcons (Qt
, Qnil
));
2021 return unbind_to (count
, eval_sub (form
));
2024 /* Eval a sub-expression of the current expression (i.e. in the same
2027 eval_sub (Lisp_Object form
)
2029 Lisp_Object fun
, val
, original_fun
, original_args
;
2031 struct backtrace backtrace
;
2032 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2034 if (handling_signal
)
2039 /* Look up its binding in the lexical environment.
2040 We do not pay attention to the declared_special flag here, since we
2041 already did that when let-binding the variable. */
2042 Lisp_Object lex_binding
2043 = !NILP (Vinternal_interpreter_environment
) /* Mere optimization! */
2044 ? Fassq (form
, Vinternal_interpreter_environment
)
2046 if (CONSP (lex_binding
))
2047 return XCDR (lex_binding
);
2049 return Fsymbol_value (form
);
2056 if ((consing_since_gc
> gc_cons_threshold
2057 && consing_since_gc
> gc_relative_threshold
)
2059 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2062 Fgarbage_collect ();
2066 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2068 if (max_lisp_eval_depth
< 100)
2069 max_lisp_eval_depth
= 100;
2070 if (lisp_eval_depth
> max_lisp_eval_depth
)
2071 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2074 original_fun
= Fcar (form
);
2075 original_args
= Fcdr (form
);
2077 backtrace
.next
= backtrace_list
;
2078 backtrace_list
= &backtrace
;
2079 backtrace
.function
= &original_fun
; /* This also protects them from gc. */
2080 backtrace
.args
= &original_args
;
2081 backtrace
.nargs
= UNEVALLED
;
2082 backtrace
.debug_on_exit
= 0;
2084 if (debug_on_next_call
)
2085 do_debug_on_call (Qt
);
2087 /* At this point, only original_fun and original_args
2088 have values that will be used below. */
2091 /* Optimize for no indirection. */
2093 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2094 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2095 fun
= indirect_function (fun
);
2099 Lisp_Object numargs
;
2100 Lisp_Object argvals
[8];
2101 Lisp_Object args_left
;
2102 register int i
, maxargs
;
2104 args_left
= original_args
;
2105 numargs
= Flength (args_left
);
2109 if (XINT (numargs
) < XSUBR (fun
)->min_args
2110 || (XSUBR (fun
)->max_args
>= 0
2111 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2112 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2114 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2115 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2116 else if (XSUBR (fun
)->max_args
== MANY
)
2118 /* Pass a vector of evaluated arguments. */
2120 ptrdiff_t argnum
= 0;
2123 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2125 GCPRO3 (args_left
, fun
, fun
);
2129 while (!NILP (args_left
))
2131 vals
[argnum
++] = eval_sub (Fcar (args_left
));
2132 args_left
= Fcdr (args_left
);
2133 gcpro3
.nvars
= argnum
;
2136 backtrace
.args
= vals
;
2137 backtrace
.nargs
= XINT (numargs
);
2139 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2145 GCPRO3 (args_left
, fun
, fun
);
2146 gcpro3
.var
= argvals
;
2149 maxargs
= XSUBR (fun
)->max_args
;
2150 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2152 argvals
[i
] = eval_sub (Fcar (args_left
));
2158 backtrace
.args
= argvals
;
2159 backtrace
.nargs
= XINT (numargs
);
2164 val
= (XSUBR (fun
)->function
.a0 ());
2167 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2170 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2173 val
= (XSUBR (fun
)->function
.a3
2174 (argvals
[0], argvals
[1], argvals
[2]));
2177 val
= (XSUBR (fun
)->function
.a4
2178 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2181 val
= (XSUBR (fun
)->function
.a5
2182 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2186 val
= (XSUBR (fun
)->function
.a6
2187 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2188 argvals
[4], argvals
[5]));
2191 val
= (XSUBR (fun
)->function
.a7
2192 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2193 argvals
[4], argvals
[5], argvals
[6]));
2197 val
= (XSUBR (fun
)->function
.a8
2198 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2199 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2203 /* Someone has created a subr that takes more arguments than
2204 is supported by this code. We need to either rewrite the
2205 subr to use a different argument protocol, or add more
2206 cases to this switch. */
2211 else if (COMPILEDP (fun
))
2212 val
= apply_lambda (fun
, original_args
);
2215 if (EQ (fun
, Qunbound
))
2216 xsignal1 (Qvoid_function
, original_fun
);
2218 xsignal1 (Qinvalid_function
, original_fun
);
2219 funcar
= XCAR (fun
);
2220 if (!SYMBOLP (funcar
))
2221 xsignal1 (Qinvalid_function
, original_fun
);
2222 if (EQ (funcar
, Qautoload
))
2224 do_autoload (fun
, original_fun
);
2227 if (EQ (funcar
, Qmacro
))
2228 val
= eval_sub (apply1 (Fcdr (fun
), original_args
));
2229 else if (EQ (funcar
, Qlambda
)
2230 || EQ (funcar
, Qclosure
))
2231 val
= apply_lambda (fun
, original_args
);
2233 xsignal1 (Qinvalid_function
, original_fun
);
2238 if (backtrace
.debug_on_exit
)
2239 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2240 backtrace_list
= backtrace
.next
;
2245 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2246 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2247 Then return the value FUNCTION returns.
2248 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2249 usage: (apply FUNCTION &rest ARGUMENTS) */)
2250 (ptrdiff_t nargs
, Lisp_Object
*args
)
2254 register Lisp_Object spread_arg
;
2255 register Lisp_Object
*funcall_args
;
2256 Lisp_Object fun
, retval
;
2257 struct gcpro gcpro1
;
2262 spread_arg
= args
[nargs
- 1];
2263 CHECK_LIST (spread_arg
);
2265 numargs
= XINT (Flength (spread_arg
));
2268 return Ffuncall (nargs
- 1, args
);
2269 else if (numargs
== 1)
2271 args
[nargs
- 1] = XCAR (spread_arg
);
2272 return Ffuncall (nargs
, args
);
2275 numargs
+= nargs
- 2;
2277 /* Optimize for no indirection. */
2278 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2279 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2280 fun
= indirect_function (fun
);
2281 if (EQ (fun
, Qunbound
))
2283 /* Let funcall get the error. */
2290 if (numargs
< XSUBR (fun
)->min_args
2291 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2292 goto funcall
; /* Let funcall get the error. */
2293 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2295 /* Avoid making funcall cons up a yet another new vector of arguments
2296 by explicitly supplying nil's for optional values. */
2297 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2298 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2299 funcall_args
[++i
] = Qnil
;
2300 GCPRO1 (*funcall_args
);
2301 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2305 /* We add 1 to numargs because funcall_args includes the
2306 function itself as well as its arguments. */
2309 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2310 GCPRO1 (*funcall_args
);
2311 gcpro1
.nvars
= 1 + numargs
;
2314 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2315 /* Spread the last arg we got. Its first element goes in
2316 the slot that it used to occupy, hence this value of I. */
2318 while (!NILP (spread_arg
))
2320 funcall_args
[i
++] = XCAR (spread_arg
);
2321 spread_arg
= XCDR (spread_arg
);
2324 /* By convention, the caller needs to gcpro Ffuncall's args. */
2325 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2332 /* Run hook variables in various ways. */
2335 funcall_nil (ptrdiff_t nargs
, Lisp_Object
*args
)
2337 Ffuncall (nargs
, args
);
2341 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2342 doc
: /* Run each hook in HOOKS.
2343 Each argument should be a symbol, a hook variable.
2344 These symbols are processed in the order specified.
2345 If a hook symbol has a non-nil value, that value may be a function
2346 or a list of functions to be called to run the hook.
2347 If the value is a function, it is called with no arguments.
2348 If it is a list, the elements are called, in order, with no arguments.
2350 Major modes should not use this function directly to run their mode
2351 hook; they should use `run-mode-hooks' instead.
2353 Do not use `make-local-variable' to make a hook variable buffer-local.
2354 Instead, use `add-hook' and specify t for the LOCAL argument.
2355 usage: (run-hooks &rest HOOKS) */)
2356 (ptrdiff_t nargs
, Lisp_Object
*args
)
2358 Lisp_Object hook
[1];
2361 for (i
= 0; i
< nargs
; i
++)
2364 run_hook_with_args (1, hook
, funcall_nil
);
2370 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2371 Srun_hook_with_args
, 1, MANY
, 0,
2372 doc
: /* Run HOOK with the specified arguments ARGS.
2373 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2374 value, that value may be a function or a list of functions to be
2375 called to run the hook. If the value is a function, it is called with
2376 the given arguments and its return value is returned. If it is a list
2377 of functions, those functions are called, in order,
2378 with the given arguments ARGS.
2379 It is best not to depend on the value returned by `run-hook-with-args',
2382 Do not use `make-local-variable' to make a hook variable buffer-local.
2383 Instead, use `add-hook' and specify t for the LOCAL argument.
2384 usage: (run-hook-with-args HOOK &rest ARGS) */)
2385 (ptrdiff_t nargs
, Lisp_Object
*args
)
2387 return run_hook_with_args (nargs
, args
, funcall_nil
);
2390 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2391 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2392 doc
: /* Run HOOK with the specified arguments ARGS.
2393 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2394 value, that value may be a function or a list of functions to be
2395 called to run the hook. If the value is a function, it is called with
2396 the given arguments and its return value is returned.
2397 If it is a list of functions, those functions are called, in order,
2398 with the given arguments ARGS, until one of them
2399 returns a non-nil value. Then we return that value.
2400 However, if they all return nil, we return nil.
2402 Do not use `make-local-variable' to make a hook variable buffer-local.
2403 Instead, use `add-hook' and specify t for the LOCAL argument.
2404 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2405 (ptrdiff_t nargs
, Lisp_Object
*args
)
2407 return run_hook_with_args (nargs
, args
, Ffuncall
);
2411 funcall_not (ptrdiff_t nargs
, Lisp_Object
*args
)
2413 return NILP (Ffuncall (nargs
, args
)) ? Qt
: Qnil
;
2416 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2417 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2418 doc
: /* Run HOOK with the specified arguments ARGS.
2419 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2420 value, that value may be a function or a list of functions to be
2421 called to run the hook. If the value is a function, it is called with
2422 the given arguments and its return value is returned.
2423 If it is a list of functions, those functions are called, in order,
2424 with the given arguments ARGS, until one of them returns nil.
2425 Then we return nil. However, if they all return non-nil, we return non-nil.
2427 Do not use `make-local-variable' to make a hook variable buffer-local.
2428 Instead, use `add-hook' and specify t for the LOCAL argument.
2429 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2430 (ptrdiff_t nargs
, Lisp_Object
*args
)
2432 return NILP (run_hook_with_args (nargs
, args
, funcall_not
)) ? Qt
: Qnil
;
2436 run_hook_wrapped_funcall (ptrdiff_t nargs
, Lisp_Object
*args
)
2438 Lisp_Object tmp
= args
[0], ret
;
2441 ret
= Ffuncall (nargs
, args
);
2447 DEFUN ("run-hook-wrapped", Frun_hook_wrapped
, Srun_hook_wrapped
, 2, MANY
, 0,
2448 doc
: /* Run HOOK, passing each function through WRAP-FUNCTION.
2449 I.e. instead of calling each function FUN directly with arguments ARGS,
2450 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2451 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2452 aborts and returns that value.
2453 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2454 (ptrdiff_t nargs
, Lisp_Object
*args
)
2456 return run_hook_with_args (nargs
, args
, run_hook_wrapped_funcall
);
2459 /* ARGS[0] should be a hook symbol.
2460 Call each of the functions in the hook value, passing each of them
2461 as arguments all the rest of ARGS (all NARGS - 1 elements).
2462 FUNCALL specifies how to call each function on the hook.
2463 The caller (or its caller, etc) must gcpro all of ARGS,
2464 except that it isn't necessary to gcpro ARGS[0]. */
2467 run_hook_with_args (ptrdiff_t nargs
, Lisp_Object
*args
,
2468 Lisp_Object (*funcall
) (ptrdiff_t nargs
, Lisp_Object
*args
))
2470 Lisp_Object sym
, val
, ret
= Qnil
;
2471 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2473 /* If we are dying or still initializing,
2474 don't do anything--it would probably crash if we tried. */
2475 if (NILP (Vrun_hooks
))
2479 val
= find_symbol_value (sym
);
2481 if (EQ (val
, Qunbound
) || NILP (val
))
2483 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2486 return funcall (nargs
, args
);
2490 Lisp_Object global_vals
= Qnil
;
2491 GCPRO3 (sym
, val
, global_vals
);
2494 CONSP (val
) && NILP (ret
);
2497 if (EQ (XCAR (val
), Qt
))
2499 /* t indicates this hook has a local binding;
2500 it means to run the global binding too. */
2501 global_vals
= Fdefault_value (sym
);
2502 if (NILP (global_vals
)) continue;
2504 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2506 args
[0] = global_vals
;
2507 ret
= funcall (nargs
, args
);
2512 CONSP (global_vals
) && NILP (ret
);
2513 global_vals
= XCDR (global_vals
))
2515 args
[0] = XCAR (global_vals
);
2516 /* In a global value, t should not occur. If it does, we
2517 must ignore it to avoid an endless loop. */
2518 if (!EQ (args
[0], Qt
))
2519 ret
= funcall (nargs
, args
);
2525 args
[0] = XCAR (val
);
2526 ret
= funcall (nargs
, args
);
2535 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2538 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2540 Lisp_Object temp
[3];
2545 Frun_hook_with_args (3, temp
);
2548 /* Apply fn to arg. */
2550 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2552 struct gcpro gcpro1
;
2556 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2559 Lisp_Object args
[2];
2563 RETURN_UNGCPRO (Fapply (2, args
));
2567 /* Call function fn on no arguments. */
2569 call0 (Lisp_Object fn
)
2571 struct gcpro gcpro1
;
2574 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2577 /* Call function fn with 1 argument arg1. */
2580 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2582 struct gcpro gcpro1
;
2583 Lisp_Object args
[2];
2589 RETURN_UNGCPRO (Ffuncall (2, args
));
2592 /* Call function fn with 2 arguments arg1, arg2. */
2595 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2597 struct gcpro gcpro1
;
2598 Lisp_Object args
[3];
2604 RETURN_UNGCPRO (Ffuncall (3, args
));
2607 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2610 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2612 struct gcpro gcpro1
;
2613 Lisp_Object args
[4];
2620 RETURN_UNGCPRO (Ffuncall (4, args
));
2623 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2626 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2629 struct gcpro gcpro1
;
2630 Lisp_Object args
[5];
2638 RETURN_UNGCPRO (Ffuncall (5, args
));
2641 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2644 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2645 Lisp_Object arg4
, Lisp_Object arg5
)
2647 struct gcpro gcpro1
;
2648 Lisp_Object args
[6];
2657 RETURN_UNGCPRO (Ffuncall (6, args
));
2660 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2663 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2664 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2666 struct gcpro gcpro1
;
2667 Lisp_Object args
[7];
2677 RETURN_UNGCPRO (Ffuncall (7, args
));
2680 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2683 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2684 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2686 struct gcpro gcpro1
;
2687 Lisp_Object args
[8];
2698 RETURN_UNGCPRO (Ffuncall (8, args
));
2701 /* The caller should GCPRO all the elements of ARGS. */
2703 DEFUN ("functionp", Ffunctionp
, Sfunctionp
, 1, 1, 0,
2704 doc
: /* Non-nil if OBJECT is a function. */)
2705 (Lisp_Object object
)
2707 if (SYMBOLP (object
) && !NILP (Ffboundp (object
)))
2709 object
= Findirect_function (object
, Qt
);
2711 if (CONSP (object
) && EQ (XCAR (object
), Qautoload
))
2713 /* Autoloaded symbols are functions, except if they load
2714 macros or keymaps. */
2716 for (i
= 0; i
< 4 && CONSP (object
); i
++)
2717 object
= XCDR (object
);
2719 return (CONSP (object
) && !NILP (XCAR (object
))) ? Qnil
: Qt
;
2724 return (XSUBR (object
)->max_args
!= UNEVALLED
) ? Qt
: Qnil
;
2725 else if (COMPILEDP (object
))
2727 else if (CONSP (object
))
2729 Lisp_Object car
= XCAR (object
);
2730 return (EQ (car
, Qlambda
) || EQ (car
, Qclosure
)) ? Qt
: Qnil
;
2736 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2737 doc
: /* Call first argument as a function, passing remaining arguments to it.
2738 Return the value that function returns.
2739 Thus, (funcall 'cons 'x 'y) returns (x . y).
2740 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2741 (ptrdiff_t nargs
, Lisp_Object
*args
)
2743 Lisp_Object fun
, original_fun
;
2745 ptrdiff_t numargs
= nargs
- 1;
2746 Lisp_Object lisp_numargs
;
2748 struct backtrace backtrace
;
2749 register Lisp_Object
*internal_args
;
2753 if ((consing_since_gc
> gc_cons_threshold
2754 && consing_since_gc
> gc_relative_threshold
)
2756 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2757 Fgarbage_collect ();
2759 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2761 if (max_lisp_eval_depth
< 100)
2762 max_lisp_eval_depth
= 100;
2763 if (lisp_eval_depth
> max_lisp_eval_depth
)
2764 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2767 backtrace
.next
= backtrace_list
;
2768 backtrace_list
= &backtrace
;
2769 backtrace
.function
= &args
[0];
2770 backtrace
.args
= &args
[1];
2771 backtrace
.nargs
= nargs
- 1;
2772 backtrace
.debug_on_exit
= 0;
2774 if (debug_on_next_call
)
2775 do_debug_on_call (Qlambda
);
2779 original_fun
= args
[0];
2783 /* Optimize for no indirection. */
2785 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2786 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2787 fun
= indirect_function (fun
);
2791 if (numargs
< XSUBR (fun
)->min_args
2792 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2794 XSETFASTINT (lisp_numargs
, numargs
);
2795 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2798 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2799 xsignal1 (Qinvalid_function
, original_fun
);
2801 else if (XSUBR (fun
)->max_args
== MANY
)
2802 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2805 if (XSUBR (fun
)->max_args
> numargs
)
2807 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2808 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2809 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2810 internal_args
[i
] = Qnil
;
2813 internal_args
= args
+ 1;
2814 switch (XSUBR (fun
)->max_args
)
2817 val
= (XSUBR (fun
)->function
.a0 ());
2820 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2823 val
= (XSUBR (fun
)->function
.a2
2824 (internal_args
[0], internal_args
[1]));
2827 val
= (XSUBR (fun
)->function
.a3
2828 (internal_args
[0], internal_args
[1], internal_args
[2]));
2831 val
= (XSUBR (fun
)->function
.a4
2832 (internal_args
[0], internal_args
[1], internal_args
[2],
2836 val
= (XSUBR (fun
)->function
.a5
2837 (internal_args
[0], internal_args
[1], internal_args
[2],
2838 internal_args
[3], internal_args
[4]));
2841 val
= (XSUBR (fun
)->function
.a6
2842 (internal_args
[0], internal_args
[1], internal_args
[2],
2843 internal_args
[3], internal_args
[4], internal_args
[5]));
2846 val
= (XSUBR (fun
)->function
.a7
2847 (internal_args
[0], internal_args
[1], internal_args
[2],
2848 internal_args
[3], internal_args
[4], internal_args
[5],
2853 val
= (XSUBR (fun
)->function
.a8
2854 (internal_args
[0], internal_args
[1], internal_args
[2],
2855 internal_args
[3], internal_args
[4], internal_args
[5],
2856 internal_args
[6], internal_args
[7]));
2861 /* If a subr takes more than 8 arguments without using MANY
2862 or UNEVALLED, we need to extend this function to support it.
2863 Until this is done, there is no way to call the function. */
2868 else if (COMPILEDP (fun
))
2869 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2872 if (EQ (fun
, Qunbound
))
2873 xsignal1 (Qvoid_function
, original_fun
);
2875 xsignal1 (Qinvalid_function
, original_fun
);
2876 funcar
= XCAR (fun
);
2877 if (!SYMBOLP (funcar
))
2878 xsignal1 (Qinvalid_function
, original_fun
);
2879 if (EQ (funcar
, Qlambda
)
2880 || EQ (funcar
, Qclosure
))
2881 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2882 else if (EQ (funcar
, Qautoload
))
2884 do_autoload (fun
, original_fun
);
2889 xsignal1 (Qinvalid_function
, original_fun
);
2893 if (backtrace
.debug_on_exit
)
2894 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2895 backtrace_list
= backtrace
.next
;
2900 apply_lambda (Lisp_Object fun
, Lisp_Object args
)
2902 Lisp_Object args_left
;
2905 register Lisp_Object
*arg_vector
;
2906 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2907 register Lisp_Object tem
;
2910 numargs
= XFASTINT (Flength (args
));
2911 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2914 GCPRO3 (*arg_vector
, args_left
, fun
);
2917 for (i
= 0; i
< numargs
; )
2919 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2920 tem
= eval_sub (tem
);
2921 arg_vector
[i
++] = tem
;
2927 backtrace_list
->args
= arg_vector
;
2928 backtrace_list
->nargs
= i
;
2929 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2931 /* Do the debug-on-exit now, while arg_vector still exists. */
2932 if (backtrace_list
->debug_on_exit
)
2933 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2934 /* Don't do it again when we return to eval. */
2935 backtrace_list
->debug_on_exit
= 0;
2940 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2941 and return the result of evaluation.
2942 FUN must be either a lambda-expression or a compiled-code object. */
2945 funcall_lambda (Lisp_Object fun
, ptrdiff_t nargs
,
2946 register Lisp_Object
*arg_vector
)
2948 Lisp_Object val
, syms_left
, next
, lexenv
;
2949 ptrdiff_t count
= SPECPDL_INDEX ();
2955 if (EQ (XCAR (fun
), Qclosure
))
2957 fun
= XCDR (fun
); /* Drop `closure'. */
2958 lexenv
= XCAR (fun
);
2959 CHECK_LIST_CONS (fun
, fun
);
2963 syms_left
= XCDR (fun
);
2964 if (CONSP (syms_left
))
2965 syms_left
= XCAR (syms_left
);
2967 xsignal1 (Qinvalid_function
, fun
);
2969 else if (COMPILEDP (fun
))
2971 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2972 if (INTEGERP (syms_left
))
2973 /* A byte-code object with a non-nil `push args' slot means we
2974 shouldn't bind any arguments, instead just call the byte-code
2975 interpreter directly; it will push arguments as necessary.
2977 Byte-code objects with either a non-existent, or a nil value for
2978 the `push args' slot (the default), have dynamically-bound
2979 arguments, and use the argument-binding code below instead (as do
2980 all interpreted functions, even lexically bound ones). */
2982 /* If we have not actually read the bytecode string
2983 and constants vector yet, fetch them from the file. */
2984 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
2985 Ffetch_bytecode (fun
);
2986 return exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
2987 AREF (fun
, COMPILED_CONSTANTS
),
2988 AREF (fun
, COMPILED_STACK_DEPTH
),
2997 i
= optional
= rest
= 0;
2998 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3002 next
= XCAR (syms_left
);
3003 if (!SYMBOLP (next
))
3004 xsignal1 (Qinvalid_function
, fun
);
3006 if (EQ (next
, Qand_rest
))
3008 else if (EQ (next
, Qand_optional
))
3015 arg
= Flist (nargs
- i
, &arg_vector
[i
]);
3019 arg
= arg_vector
[i
++];
3021 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3025 /* Bind the argument. */
3026 if (!NILP (lexenv
) && SYMBOLP (next
))
3027 /* Lexically bind NEXT by adding it to the lexenv alist. */
3028 lexenv
= Fcons (Fcons (next
, arg
), lexenv
);
3030 /* Dynamically bind NEXT. */
3031 specbind (next
, arg
);
3035 if (!NILP (syms_left
))
3036 xsignal1 (Qinvalid_function
, fun
);
3038 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3040 if (!EQ (lexenv
, Vinternal_interpreter_environment
))
3041 /* Instantiate a new lexical environment. */
3042 specbind (Qinternal_interpreter_environment
, lexenv
);
3045 val
= Fprogn (XCDR (XCDR (fun
)));
3048 /* If we have not actually read the bytecode string
3049 and constants vector yet, fetch them from the file. */
3050 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3051 Ffetch_bytecode (fun
);
3052 val
= exec_byte_code (AREF (fun
, COMPILED_BYTECODE
),
3053 AREF (fun
, COMPILED_CONSTANTS
),
3054 AREF (fun
, COMPILED_STACK_DEPTH
),
3058 return unbind_to (count
, val
);
3061 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3063 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3064 (Lisp_Object object
)
3068 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3070 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3073 tem
= AREF (object
, COMPILED_BYTECODE
);
3074 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3075 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3077 error ("Invalid byte code");
3079 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3080 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3088 register ptrdiff_t count
= SPECPDL_INDEX ();
3089 ptrdiff_t max_size
= min (max_specpdl_size
, PTRDIFF_MAX
);
3090 if (max_size
<= specpdl_size
)
3092 if (max_specpdl_size
< 400)
3093 max_size
= max_specpdl_size
= 400;
3094 if (max_size
<= specpdl_size
)
3095 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3097 specpdl
= xpalloc (specpdl
, &specpdl_size
, 1, max_size
, sizeof *specpdl
);
3098 specpdl_ptr
= specpdl
+ count
;
3101 /* `specpdl_ptr->symbol' is a field which describes which variable is
3102 let-bound, so it can be properly undone when we unbind_to.
3103 It can have the following two shapes:
3104 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3105 a symbol that is not buffer-local (at least at the time
3106 the let binding started). Note also that it should not be
3107 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3109 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3110 variable SYMBOL which can be buffer-local. WHERE tells us
3111 which buffer is affected (or nil if the let-binding affects the
3112 global value of the variable) and BUFFER tells us which buffer was
3113 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3114 BUFFER did not yet have a buffer-local value). */
3117 specbind (Lisp_Object symbol
, Lisp_Object value
)
3119 struct Lisp_Symbol
*sym
;
3121 eassert (!handling_signal
);
3123 CHECK_SYMBOL (symbol
);
3124 sym
= XSYMBOL (symbol
);
3125 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3129 switch (sym
->redirect
)
3131 case SYMBOL_VARALIAS
:
3132 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3133 case SYMBOL_PLAINVAL
:
3134 /* The most common case is that of a non-constant symbol with a
3135 trivial value. Make that as fast as we can. */
3136 specpdl_ptr
->symbol
= symbol
;
3137 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3138 specpdl_ptr
->func
= NULL
;
3141 SET_SYMBOL_VAL (sym
, value
);
3143 set_internal (symbol
, value
, Qnil
, 1);
3145 case SYMBOL_LOCALIZED
:
3146 if (SYMBOL_BLV (sym
)->frame_local
)
3147 error ("Frame-local vars cannot be let-bound");
3148 case SYMBOL_FORWARDED
:
3150 Lisp_Object ovalue
= find_symbol_value (symbol
);
3151 specpdl_ptr
->func
= 0;
3152 specpdl_ptr
->old_value
= ovalue
;
3154 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3155 || (EQ (SYMBOL_BLV (sym
)->where
,
3156 SYMBOL_BLV (sym
)->frame_local
?
3157 Fselected_frame () : Fcurrent_buffer ())));
3159 if (sym
->redirect
== SYMBOL_LOCALIZED
3160 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3162 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3164 /* For a local variable, record both the symbol and which
3165 buffer's or frame's value we are saving. */
3166 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3168 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3169 || (BLV_FOUND (SYMBOL_BLV (sym
))
3170 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3173 else if (sym
->redirect
== SYMBOL_LOCALIZED
3174 && BLV_FOUND (SYMBOL_BLV (sym
)))
3175 where
= SYMBOL_BLV (sym
)->where
;
3179 /* We're not using the `unused' slot in the specbinding
3180 structure because this would mean we have to do more
3181 work for simple variables. */
3182 /* FIXME: The third value `current_buffer' is only used in
3183 let_shadows_buffer_binding_p which is itself only used
3184 in set_internal for local_if_set. */
3185 eassert (NILP (where
) || EQ (where
, cur_buf
));
3186 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3188 /* If SYMBOL is a per-buffer variable which doesn't have a
3189 buffer-local value here, make the `let' change the global
3190 value by changing the value of SYMBOL in all buffers not
3191 having their own value. This is consistent with what
3192 happens with other buffer-local variables. */
3194 && sym
->redirect
== SYMBOL_FORWARDED
)
3196 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3198 Fset_default (symbol
, value
);
3203 specpdl_ptr
->symbol
= symbol
;
3206 set_internal (symbol
, value
, Qnil
, 1);
3214 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3216 eassert (!handling_signal
);
3218 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3220 specpdl_ptr
->func
= function
;
3221 specpdl_ptr
->symbol
= Qnil
;
3222 specpdl_ptr
->old_value
= arg
;
3227 unbind_to (ptrdiff_t count
, Lisp_Object value
)
3229 Lisp_Object quitf
= Vquit_flag
;
3230 struct gcpro gcpro1
, gcpro2
;
3232 GCPRO2 (value
, quitf
);
3235 while (specpdl_ptr
!= specpdl
+ count
)
3237 /* Copy the binding, and decrement specpdl_ptr, before we do
3238 the work to unbind it. We decrement first
3239 so that an error in unbinding won't try to unbind
3240 the same entry again, and we copy the binding first
3241 in case more bindings are made during some of the code we run. */
3243 struct specbinding this_binding
;
3244 this_binding
= *--specpdl_ptr
;
3246 if (this_binding
.func
!= 0)
3247 (*this_binding
.func
) (this_binding
.old_value
);
3248 /* If the symbol is a list, it is really (SYMBOL WHERE
3249 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3250 frame. If WHERE is a buffer or frame, this indicates we
3251 bound a variable that had a buffer-local or frame-local
3252 binding. WHERE nil means that the variable had the default
3253 value when it was bound. CURRENT-BUFFER is the buffer that
3254 was current when the variable was bound. */
3255 else if (CONSP (this_binding
.symbol
))
3257 Lisp_Object symbol
, where
;
3259 symbol
= XCAR (this_binding
.symbol
);
3260 where
= XCAR (XCDR (this_binding
.symbol
));
3263 Fset_default (symbol
, this_binding
.old_value
);
3264 /* If `where' is non-nil, reset the value in the appropriate
3265 local binding, but only if that binding still exists. */
3266 else if (BUFFERP (where
)
3267 ? !NILP (Flocal_variable_p (symbol
, where
))
3268 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3269 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3271 /* If variable has a trivial value (no forwarding), we can
3272 just set it. No need to check for constant symbols here,
3273 since that was already done by specbind. */
3274 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3275 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3276 this_binding
.old_value
);
3278 /* NOTE: we only ever come here if make_local_foo was used for
3279 the first time on this var within this let. */
3280 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3283 if (NILP (Vquit_flag
) && !NILP (quitf
))
3290 DEFUN ("special-variable-p", Fspecial_variable_p
, Sspecial_variable_p
, 1, 1, 0,
3291 doc
: /* Return non-nil if SYMBOL's global binding has been declared special.
3292 A special variable is one that will be bound dynamically, even in a
3293 context where binding is lexical by default. */)
3294 (Lisp_Object symbol
)
3296 CHECK_SYMBOL (symbol
);
3297 return XSYMBOL (symbol
)->declared_special
? Qt
: Qnil
;
3301 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3302 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3303 The debugger is entered when that frame exits, if the flag is non-nil. */)
3304 (Lisp_Object level
, Lisp_Object flag
)
3306 register struct backtrace
*backlist
= backtrace_list
;
3307 register EMACS_INT i
;
3309 CHECK_NUMBER (level
);
3311 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3313 backlist
= backlist
->next
;
3317 backlist
->debug_on_exit
= !NILP (flag
);
3322 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3323 doc
: /* Print a trace of Lisp function calls currently active.
3324 Output stream used is value of `standard-output'. */)
3327 register struct backtrace
*backlist
= backtrace_list
;
3330 struct gcpro gcpro1
;
3331 Lisp_Object old_print_level
= Vprint_level
;
3333 if (NILP (Vprint_level
))
3334 XSETFASTINT (Vprint_level
, 8);
3341 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3342 if (backlist
->nargs
== UNEVALLED
)
3344 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3345 write_string ("\n", -1);
3349 tem
= *backlist
->function
;
3350 Fprin1 (tem
, Qnil
); /* This can QUIT. */
3351 write_string ("(", -1);
3352 if (backlist
->nargs
== MANY
)
3353 { /* FIXME: Can this happen? */
3355 for (tail
= *backlist
->args
, i
= 0;
3357 tail
= Fcdr (tail
), i
= 1)
3359 if (i
) write_string (" ", -1);
3360 Fprin1 (Fcar (tail
), Qnil
);
3366 for (i
= 0; i
< backlist
->nargs
; i
++)
3368 if (i
) write_string (" ", -1);
3369 Fprin1 (backlist
->args
[i
], Qnil
);
3372 write_string (")\n", -1);
3374 backlist
= backlist
->next
;
3377 Vprint_level
= old_print_level
;
3382 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3383 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3384 If that frame has not evaluated the arguments yet (or is a special form),
3385 the value is (nil FUNCTION ARG-FORMS...).
3386 If that frame has evaluated its arguments and called its function already,
3387 the value is (t FUNCTION ARG-VALUES...).
3388 A &rest arg is represented as the tail of the list ARG-VALUES.
3389 FUNCTION is whatever was supplied as car of evaluated list,
3390 or a lambda expression for macro calls.
3391 If NFRAMES is more than the number of frames, the value is nil. */)
3392 (Lisp_Object nframes
)
3394 register struct backtrace
*backlist
= backtrace_list
;
3395 register EMACS_INT i
;
3398 CHECK_NATNUM (nframes
);
3400 /* Find the frame requested. */
3401 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3402 backlist
= backlist
->next
;
3406 if (backlist
->nargs
== UNEVALLED
)
3407 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3410 if (backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3411 tem
= *backlist
->args
;
3413 tem
= Flist (backlist
->nargs
, backlist
->args
);
3415 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3422 mark_backtrace (void)
3424 register struct backtrace
*backlist
;
3427 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3429 mark_object (*backlist
->function
);
3431 if (backlist
->nargs
== UNEVALLED
3432 || backlist
->nargs
== MANY
) /* FIXME: Can this happen? */
3435 i
= backlist
->nargs
;
3437 mark_object (backlist
->args
[i
]);
3445 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3446 doc
: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3447 If Lisp code tries to increase the total number past this amount,
3448 an error is signaled.
3449 You can safely use a value considerably larger than the default value,
3450 if that proves inconveniently small. However, if you increase it too far,
3451 Emacs could run out of memory trying to make the stack bigger. */);
3453 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3454 doc
: /* Limit on depth in `eval', `apply' and `funcall' before error.
3456 This limit serves to catch infinite recursions for you before they cause
3457 actual stack overflow in C, which would be fatal for Emacs.
3458 You can safely make it considerably larger than its default value,
3459 if that proves inconveniently small. However, if you increase it too far,
3460 Emacs could overflow the real C stack, and crash. */);
3462 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3463 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3464 If the value is t, that means do an ordinary quit.
3465 If the value equals `throw-on-input', that means quit by throwing
3466 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3467 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3468 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3471 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3472 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3473 Note that `quit-flag' will still be set by typing C-g,
3474 so a quit will be signaled as soon as `inhibit-quit' is nil.
3475 To prevent this happening, set `quit-flag' to nil
3476 before making `inhibit-quit' nil. */);
3477 Vinhibit_quit
= Qnil
;
3479 DEFSYM (Qinhibit_quit
, "inhibit-quit");
3480 DEFSYM (Qautoload
, "autoload");
3481 DEFSYM (Qdebug_on_error
, "debug-on-error");
3482 DEFSYM (Qmacro
, "macro");
3483 DEFSYM (Qdeclare
, "declare");
3485 /* Note that the process handling also uses Qexit, but we don't want
3486 to staticpro it twice, so we just do it here. */
3487 DEFSYM (Qexit
, "exit");
3489 DEFSYM (Qinteractive
, "interactive");
3490 DEFSYM (Qcommandp
, "commandp");
3491 DEFSYM (Qand_rest
, "&rest");
3492 DEFSYM (Qand_optional
, "&optional");
3493 DEFSYM (Qclosure
, "closure");
3494 DEFSYM (Qdebug
, "debug");
3496 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3497 doc
: /* Non-nil means enter debugger if an error is signaled.
3498 Does not apply to errors handled by `condition-case' or those
3499 matched by `debug-ignored-errors'.
3500 If the value is a list, an error only means to enter the debugger
3501 if one of its condition symbols appears in the list.
3502 When you evaluate an expression interactively, this variable
3503 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3504 The command `toggle-debug-on-error' toggles this.
3505 See also the variable `debug-on-quit'. */);
3506 Vdebug_on_error
= Qnil
;
3508 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3509 doc
: /* List of errors for which the debugger should not be called.
3510 Each element may be a condition-name or a regexp that matches error messages.
3511 If any element applies to a given error, that error skips the debugger
3512 and just returns to top level.
3513 This overrides the variable `debug-on-error'.
3514 It does not apply to errors handled by `condition-case'. */);
3515 Vdebug_ignored_errors
= Qnil
;
3517 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3518 doc
: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3519 Does not apply if quit is handled by a `condition-case'. */);
3522 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3523 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3525 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3526 doc
: /* Non-nil means debugger may continue execution.
3527 This is nil when the debugger is called under circumstances where it
3528 might not be safe to continue. */);
3529 debugger_may_continue
= 1;
3531 DEFVAR_LISP ("debugger", Vdebugger
,
3532 doc
: /* Function to call to invoke debugger.
3533 If due to frame exit, args are `exit' and the value being returned;
3534 this function's value will be returned instead of that.
3535 If due to error, args are `error' and a list of the args to `signal'.
3536 If due to `apply' or `funcall' entry, one arg, `lambda'.
3537 If due to `eval' entry, one arg, t. */);
3540 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3541 doc
: /* If non-nil, this is a function for `signal' to call.
3542 It receives the same arguments that `signal' was given.
3543 The Edebug package uses this to regain control. */);
3544 Vsignal_hook_function
= Qnil
;
3546 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3547 doc
: /* Non-nil means call the debugger regardless of condition handlers.
3548 Note that `debug-on-error', `debug-on-quit' and friends
3549 still determine whether to handle the particular condition. */);
3550 Vdebug_on_signal
= Qnil
;
3552 /* When lexical binding is being used,
3553 Vinternal_interpreter_environment is non-nil, and contains an alist
3554 of lexically-bound variable, or (t), indicating an empty
3555 environment. The lisp name of this variable would be
3556 `internal-interpreter-environment' if it weren't hidden.
3557 Every element of this list can be either a cons (VAR . VAL)
3558 specifying a lexical binding, or a single symbol VAR indicating
3559 that this variable should use dynamic scoping. */
3560 DEFSYM (Qinternal_interpreter_environment
,
3561 "internal-interpreter-environment");
3562 DEFVAR_LISP ("internal-interpreter-environment",
3563 Vinternal_interpreter_environment
,
3564 doc
: /* If non-nil, the current lexical environment of the lisp interpreter.
3565 When lexical binding is not being used, this variable is nil.
3566 A value of `(t)' indicates an empty environment, otherwise it is an
3567 alist of active lexical bindings. */);
3568 Vinternal_interpreter_environment
= Qnil
;
3569 /* Don't export this variable to Elisp, so no one can mess with it
3570 (Just imagine if someone makes it buffer-local). */
3571 Funintern (Qinternal_interpreter_environment
, Qnil
);
3573 DEFSYM (Vrun_hooks
, "run-hooks");
3575 staticpro (&Vautoload_queue
);
3576 Vautoload_queue
= Qnil
;
3577 staticpro (&Vsignaling_function
);
3578 Vsignaling_function
= Qnil
;
3580 inhibit_lisp_code
= Qnil
;
3591 defsubr (&Sfunction
);
3593 defsubr (&Sdefvaralias
);
3594 defsubr (&Sdefconst
);
3595 defsubr (&Smake_var_non_special
);
3599 defsubr (&Smacroexpand
);
3602 defsubr (&Sunwind_protect
);
3603 defsubr (&Scondition_case
);
3605 defsubr (&Sinteractive_p
);
3606 defsubr (&Scalled_interactively_p
);
3607 defsubr (&Scommandp
);
3608 defsubr (&Sautoload
);
3611 defsubr (&Sfuncall
);
3612 defsubr (&Srun_hooks
);
3613 defsubr (&Srun_hook_with_args
);
3614 defsubr (&Srun_hook_with_args_until_success
);
3615 defsubr (&Srun_hook_with_args_until_failure
);
3616 defsubr (&Srun_hook_wrapped
);
3617 defsubr (&Sfetch_bytecode
);
3618 defsubr (&Sbacktrace_debug
);
3619 defsubr (&Sbacktrace
);
3620 defsubr (&Sbacktrace_frame
);
3621 defsubr (&Sspecial_variable_p
);
3622 defsubr (&Sfunctionp
);