1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2011 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/>. */
23 #include "blockinput.h"
26 #include "dispextern.h"
27 #include "frame.h" /* For XFRAME. */
33 /* This definition is duplicated in alloc.c and keyboard.c */
34 /* Putting it in lisp.h makes cc bomb out! */
38 struct backtrace
*next
;
39 Lisp_Object
*function
;
40 Lisp_Object
*args
; /* Points to vector of args. */
41 size_t nargs
; /* Length of vector.
42 If nargs is (size_t) UNEVALLED, args points
43 to slot holding list of unevalled args */
45 /* Nonzero means call value of debugger when done with this operation. */
49 struct backtrace
*backtrace_list
;
51 struct catchtag
*catchlist
;
54 /* Count levels of GCPRO to detect failure to UNGCPRO. */
58 Lisp_Object Qautoload
, Qmacro
, Qexit
, Qinteractive
, Qcommandp
, Qdefun
;
59 Lisp_Object Qinhibit_quit
;
60 Lisp_Object Qand_rest
, Qand_optional
;
61 Lisp_Object Qdebug_on_error
;
65 /* This holds either the symbol `run-hooks' or nil.
66 It is nil at an early stage of startup, and when Emacs
69 Lisp_Object Vrun_hooks
;
71 /* Non-nil means record all fset's and provide's, to be undone
72 if the file being autoloaded is not fully loaded.
73 They are recorded by being consed onto the front of Vautoload_queue:
74 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
76 Lisp_Object Vautoload_queue
;
78 /* Current number of specbindings allocated in specpdl. */
80 EMACS_INT specpdl_size
;
82 /* Pointer to beginning of specpdl. */
84 struct specbinding
*specpdl
;
86 /* Pointer to first unused element in specpdl. */
88 struct specbinding
*specpdl_ptr
;
90 /* Depth in Lisp evaluations and function calls. */
92 EMACS_INT lisp_eval_depth
;
94 /* The value of num_nonmacro_input_events as of the last time we
95 started to enter the debugger. If we decide to enter the debugger
96 again when this is still equal to num_nonmacro_input_events, then we
97 know that the debugger itself has an error, and we should just
98 signal the error instead of entering an infinite loop of debugger
101 int when_entered_debugger
;
103 /* The function from which the last `signal' was called. Set in
106 Lisp_Object Vsignaling_function
;
108 /* Set to non-zero while processing X events. Checked in Feval to
109 make sure the Lisp interpreter isn't called from a signal handler,
110 which is unsafe because the interpreter isn't reentrant. */
114 static Lisp_Object
funcall_lambda (Lisp_Object
, size_t, Lisp_Object
*);
115 static void unwind_to_catch (struct catchtag
*, Lisp_Object
) NO_RETURN
;
116 static int interactive_p (int);
117 static Lisp_Object
apply_lambda (Lisp_Object
, Lisp_Object
, int);
120 init_eval_once (void)
123 specpdl
= (struct specbinding
*) xmalloc (specpdl_size
* sizeof (struct specbinding
));
124 specpdl_ptr
= specpdl
;
125 /* Don't forget to update docs (lispref node "Local Variables"). */
126 max_specpdl_size
= 1000;
127 max_lisp_eval_depth
= 600;
135 specpdl_ptr
= specpdl
;
140 debug_on_next_call
= 0;
145 /* This is less than the initial value of num_nonmacro_input_events. */
146 when_entered_debugger
= -1;
149 /* unwind-protect function used by call_debugger. */
152 restore_stack_limits (Lisp_Object data
)
154 max_specpdl_size
= XINT (XCAR (data
));
155 max_lisp_eval_depth
= XINT (XCDR (data
));
159 /* Call the Lisp debugger, giving it argument ARG. */
162 call_debugger (Lisp_Object arg
)
164 int debug_while_redisplaying
;
165 int count
= SPECPDL_INDEX ();
167 EMACS_INT old_max
= max_specpdl_size
;
169 /* Temporarily bump up the stack limits,
170 so the debugger won't run out of stack. */
172 max_specpdl_size
+= 1;
173 record_unwind_protect (restore_stack_limits
,
174 Fcons (make_number (old_max
),
175 make_number (max_lisp_eval_depth
)));
176 max_specpdl_size
= old_max
;
178 if (lisp_eval_depth
+ 40 > max_lisp_eval_depth
)
179 max_lisp_eval_depth
= lisp_eval_depth
+ 40;
181 if (SPECPDL_INDEX () + 100 > max_specpdl_size
)
182 max_specpdl_size
= SPECPDL_INDEX () + 100;
184 #ifdef HAVE_WINDOW_SYSTEM
185 if (display_hourglass_p
)
189 debug_on_next_call
= 0;
190 when_entered_debugger
= num_nonmacro_input_events
;
192 /* Resetting redisplaying_p to 0 makes sure that debug output is
193 displayed if the debugger is invoked during redisplay. */
194 debug_while_redisplaying
= redisplaying_p
;
196 specbind (intern ("debugger-may-continue"),
197 debug_while_redisplaying
? Qnil
: Qt
);
198 specbind (Qinhibit_redisplay
, Qnil
);
199 specbind (Qdebug_on_error
, Qnil
);
201 #if 0 /* Binding this prevents execution of Lisp code during
202 redisplay, which necessarily leads to display problems. */
203 specbind (Qinhibit_eval_during_redisplay
, Qt
);
206 val
= apply1 (Vdebugger
, arg
);
208 /* Interrupting redisplay and resuming it later is not safe under
209 all circumstances. So, when the debugger returns, abort the
210 interrupted redisplay by going back to the top-level. */
211 if (debug_while_redisplaying
)
214 return unbind_to (count
, val
);
218 do_debug_on_call (Lisp_Object code
)
220 debug_on_next_call
= 0;
221 backtrace_list
->debug_on_exit
= 1;
222 call_debugger (Fcons (code
, Qnil
));
225 /* NOTE!!! Every function that can call EVAL must protect its args
226 and temporaries from garbage collection while it needs them.
227 The definition of `For' shows what you have to do. */
229 DEFUN ("or", For
, Sor
, 0, UNEVALLED
, 0,
230 doc
: /* Eval args until one of them yields non-nil, then return that value.
231 The remaining args are not evalled at all.
232 If all args return nil, return nil.
233 usage: (or CONDITIONS...) */)
236 register Lisp_Object val
= Qnil
;
243 val
= Feval (XCAR (args
));
253 DEFUN ("and", Fand
, Sand
, 0, UNEVALLED
, 0,
254 doc
: /* Eval args until one of them yields nil, then return nil.
255 The remaining args are not evalled at all.
256 If no arg yields nil, return the last arg's value.
257 usage: (and CONDITIONS...) */)
260 register Lisp_Object val
= Qt
;
267 val
= Feval (XCAR (args
));
277 DEFUN ("if", Fif
, Sif
, 2, UNEVALLED
, 0,
278 doc
: /* If COND yields non-nil, do THEN, else do ELSE...
279 Returns the value of THEN or the value of the last of the ELSE's.
280 THEN must be one expression, but ELSE... can be zero or more expressions.
281 If COND yields nil, and there are no ELSE's, the value is nil.
282 usage: (if COND THEN ELSE...) */)
285 register Lisp_Object cond
;
289 cond
= Feval (Fcar (args
));
293 return Feval (Fcar (Fcdr (args
)));
294 return Fprogn (Fcdr (Fcdr (args
)));
297 DEFUN ("cond", Fcond
, Scond
, 0, UNEVALLED
, 0,
298 doc
: /* Try each clause until one succeeds.
299 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
300 and, if the value is non-nil, this clause succeeds:
301 then the expressions in BODY are evaluated and the last one's
302 value is the value of the cond-form.
303 If no clause succeeds, cond returns nil.
304 If a clause has one element, as in (CONDITION),
305 CONDITION's value if non-nil is returned from the cond-form.
306 usage: (cond CLAUSES...) */)
309 register Lisp_Object clause
, val
;
316 clause
= Fcar (args
);
317 val
= Feval (Fcar (clause
));
320 if (!EQ (XCDR (clause
), Qnil
))
321 val
= Fprogn (XCDR (clause
));
331 DEFUN ("progn", Fprogn
, Sprogn
, 0, UNEVALLED
, 0,
332 doc
: /* Eval BODY forms sequentially and return value of last one.
333 usage: (progn BODY...) */)
336 register Lisp_Object val
= Qnil
;
343 val
= Feval (XCAR (args
));
351 DEFUN ("prog1", Fprog1
, Sprog1
, 1, UNEVALLED
, 0,
352 doc
: /* Eval FIRST and BODY sequentially; return value from FIRST.
353 The value of FIRST is saved during the evaluation of the remaining args,
354 whose values are discarded.
355 usage: (prog1 FIRST BODY...) */)
359 register Lisp_Object args_left
;
360 struct gcpro gcpro1
, gcpro2
;
361 register int argnum
= 0;
373 val
= Feval (Fcar (args_left
));
375 Feval (Fcar (args_left
));
376 args_left
= Fcdr (args_left
);
378 while (!NILP(args_left
));
384 DEFUN ("prog2", Fprog2
, Sprog2
, 2, UNEVALLED
, 0,
385 doc
: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
386 The value of FORM2 is saved during the evaluation of the
387 remaining args, whose values are discarded.
388 usage: (prog2 FORM1 FORM2 BODY...) */)
392 register Lisp_Object args_left
;
393 struct gcpro gcpro1
, gcpro2
;
394 register int argnum
= -1;
408 val
= Feval (Fcar (args_left
));
410 Feval (Fcar (args_left
));
411 args_left
= Fcdr (args_left
);
413 while (!NILP (args_left
));
419 DEFUN ("setq", Fsetq
, Ssetq
, 0, UNEVALLED
, 0,
420 doc
: /* Set each SYM to the value of its VAL.
421 The symbols SYM are variables; they are literal (not evaluated).
422 The values VAL are expressions; they are evaluated.
423 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
424 The second VAL is not computed until after the first SYM is set, and so on;
425 each VAL can use the new value of variables set earlier in the `setq'.
426 The return value of the `setq' form is the value of the last VAL.
427 usage: (setq [SYM VAL]...) */)
430 register Lisp_Object args_left
;
431 register Lisp_Object val
, sym
;
442 val
= Feval (Fcar (Fcdr (args_left
)));
443 sym
= Fcar (args_left
);
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 usage: (quote ARG) */)
458 if (!NILP (Fcdr (args
)))
459 xsignal2 (Qwrong_number_of_arguments
, Qquote
, Flength (args
));
463 DEFUN ("function", Ffunction
, Sfunction
, 1, UNEVALLED
, 0,
464 doc
: /* Like `quote', but preferred for objects which are functions.
465 In byte compilation, `function' causes its argument to be compiled.
466 `quote' cannot do that.
467 usage: (function ARG) */)
470 if (!NILP (Fcdr (args
)))
471 xsignal2 (Qwrong_number_of_arguments
, Qfunction
, Flength (args
));
476 DEFUN ("interactive-p", Finteractive_p
, Sinteractive_p
, 0, 0, 0,
477 doc
: /* Return t if the containing function was run directly by user input.
478 This means that the function was called with `call-interactively'
479 \(which includes being called as the binding of a key)
480 and input is currently coming from the keyboard (not a keyboard macro),
481 and Emacs is not running in batch mode (`noninteractive' is nil).
483 The only known proper use of `interactive-p' is in deciding whether to
484 display a helpful message, or how to display it. If you're thinking
485 of using it for any other purpose, it is quite likely that you're
486 making a mistake. Think: what do you want to do when the command is
487 called from a keyboard macro?
489 To test whether your function was called with `call-interactively',
490 either (i) add an extra optional argument and give it an `interactive'
491 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
492 use `called-interactively-p'. */)
495 return (INTERACTIVE
&& interactive_p (1)) ? Qt
: Qnil
;
499 DEFUN ("called-interactively-p", Fcalled_interactively_p
, Scalled_interactively_p
, 0, 1, 0,
500 doc
: /* Return t if the containing function was called by `call-interactively'.
501 If KIND is `interactive', then only return t if the call was made
502 interactively by the user, i.e. not in `noninteractive' mode nor
503 when `executing-kbd-macro'.
504 If KIND is `any', on the other hand, it will return t for any kind of
505 interactive call, including being called as the binding of a key, or
506 from a keyboard macro, or in `noninteractive' mode.
508 The only known proper use of `interactive' for KIND is in deciding
509 whether to display a helpful message, or how to display it. If you're
510 thinking of using it for any other purpose, it is quite likely that
511 you're making a mistake. Think: what do you want to do when the
512 command is called from a keyboard macro?
514 This function is meant for implementing advice and other
515 function-modifying features. Instead of using this, it is sometimes
516 cleaner to give your function an extra optional argument whose
517 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
518 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
521 return ((INTERACTIVE
|| !EQ (kind
, intern ("interactive")))
522 && interactive_p (1)) ? Qt
: Qnil
;
526 /* Return 1 if function in which this appears was called using
529 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
530 called is a built-in. */
533 interactive_p (int exclude_subrs_p
)
535 struct backtrace
*btp
;
538 btp
= backtrace_list
;
540 /* If this isn't a byte-compiled function, there may be a frame at
541 the top for Finteractive_p. If so, skip it. */
542 fun
= Findirect_function (*btp
->function
, Qnil
);
543 if (SUBRP (fun
) && (XSUBR (fun
) == &Sinteractive_p
544 || XSUBR (fun
) == &Scalled_interactively_p
))
547 /* If we're running an Emacs 18-style byte-compiled function, there
548 may be a frame for Fbytecode at the top level. In any version of
549 Emacs there can be Fbytecode frames for subexpressions evaluated
550 inside catch and condition-case. Skip past them.
552 If this isn't a byte-compiled function, then we may now be
553 looking at several frames for special forms. Skip past them. */
555 && (EQ (*btp
->function
, Qbytecode
)
556 || btp
->nargs
== (size_t) UNEVALLED
))
559 /* btp now points at the frame of the innermost function that isn't
560 a special form, ignoring frames for Finteractive_p and/or
561 Fbytecode at the top. If this frame is for a built-in function
562 (such as load or eval-region) return nil. */
563 fun
= Findirect_function (*btp
->function
, Qnil
);
564 if (exclude_subrs_p
&& SUBRP (fun
))
567 /* btp points to the frame of a Lisp function that called interactive-p.
568 Return t if that function was called interactively. */
569 if (btp
&& btp
->next
&& EQ (*btp
->next
->function
, Qcall_interactively
))
575 DEFUN ("defun", Fdefun
, Sdefun
, 2, UNEVALLED
, 0,
576 doc
: /* Define NAME as a function.
577 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
578 See also the function `interactive'.
579 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
582 register Lisp_Object fn_name
;
583 register Lisp_Object defn
;
585 fn_name
= Fcar (args
);
586 CHECK_SYMBOL (fn_name
);
587 defn
= Fcons (Qlambda
, Fcdr (args
));
588 if (!NILP (Vpurify_flag
))
589 defn
= Fpurecopy (defn
);
590 if (CONSP (XSYMBOL (fn_name
)->function
)
591 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
592 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
593 Ffset (fn_name
, defn
);
594 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
598 DEFUN ("defmacro", Fdefmacro
, Sdefmacro
, 2, UNEVALLED
, 0,
599 doc
: /* Define NAME as a macro.
600 The actual definition looks like
601 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
602 When the macro is called, as in (NAME ARGS...),
603 the function (lambda ARGLIST BODY...) is applied to
604 the list ARGS... as it appears in the expression,
605 and the result should be a form to be evaluated instead of the original.
607 DECL is a declaration, optional, which can specify how to indent
608 calls to this macro, how Edebug should handle it, and which argument
609 should be treated as documentation. It looks like this:
611 The elements can look like this:
613 Set NAME's `lisp-indent-function' property to INDENT.
616 Set NAME's `edebug-form-spec' property to DEBUG. (This is
617 equivalent to writing a `def-edebug-spec' for the macro.)
620 Set NAME's `doc-string-elt' property to ELT.
622 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
625 register Lisp_Object fn_name
;
626 register Lisp_Object defn
;
627 Lisp_Object lambda_list
, doc
, tail
;
629 fn_name
= Fcar (args
);
630 CHECK_SYMBOL (fn_name
);
631 lambda_list
= Fcar (Fcdr (args
));
632 tail
= Fcdr (Fcdr (args
));
635 if (STRINGP (Fcar (tail
)))
641 if (CONSP (Fcar (tail
))
642 && EQ (Fcar (Fcar (tail
)), Qdeclare
))
644 if (!NILP (Vmacro_declaration_function
))
648 call2 (Vmacro_declaration_function
, fn_name
, Fcar (tail
));
656 tail
= Fcons (lambda_list
, tail
);
658 tail
= Fcons (lambda_list
, Fcons (doc
, tail
));
659 defn
= Fcons (Qmacro
, Fcons (Qlambda
, tail
));
661 if (!NILP (Vpurify_flag
))
662 defn
= Fpurecopy (defn
);
663 if (CONSP (XSYMBOL (fn_name
)->function
)
664 && EQ (XCAR (XSYMBOL (fn_name
)->function
), Qautoload
))
665 LOADHIST_ATTACH (Fcons (Qt
, fn_name
));
666 Ffset (fn_name
, defn
);
667 LOADHIST_ATTACH (Fcons (Qdefun
, fn_name
));
672 DEFUN ("defvaralias", Fdefvaralias
, Sdefvaralias
, 2, 3, 0,
673 doc
: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
674 Aliased variables always have the same value; setting one sets the other.
675 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
676 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
677 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
678 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
679 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
680 The return value is BASE-VARIABLE. */)
681 (Lisp_Object new_alias
, Lisp_Object base_variable
, Lisp_Object docstring
)
683 struct Lisp_Symbol
*sym
;
685 CHECK_SYMBOL (new_alias
);
686 CHECK_SYMBOL (base_variable
);
688 sym
= XSYMBOL (new_alias
);
691 /* Not sure why, but why not? */
692 error ("Cannot make a constant an alias");
694 switch (sym
->redirect
)
696 case SYMBOL_FORWARDED
:
697 error ("Cannot make an internal variable an alias");
698 case SYMBOL_LOCALIZED
:
699 error ("Don't know how to make a localized variable an alias");
702 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
703 If n_a is bound, but b_v is not, set the value of b_v to n_a,
704 so that old-code that affects n_a before the aliasing is setup
706 if (NILP (Fboundp (base_variable
)))
707 set_internal (base_variable
, find_symbol_value (new_alias
), Qnil
, 1);
710 struct specbinding
*p
;
712 for (p
= specpdl_ptr
- 1; p
>= specpdl
; p
--)
715 CONSP (p
->symbol
) ? XCAR (p
->symbol
) : p
->symbol
)))
716 error ("Don't know how to make a let-bound variable an alias");
719 sym
->redirect
= SYMBOL_VARALIAS
;
720 SET_SYMBOL_ALIAS (sym
, XSYMBOL (base_variable
));
721 sym
->constant
= SYMBOL_CONSTANT_P (base_variable
);
722 LOADHIST_ATTACH (new_alias
);
723 /* Even if docstring is nil: remove old docstring. */
724 Fput (new_alias
, Qvariable_documentation
, docstring
);
726 return base_variable
;
730 DEFUN ("defvar", Fdefvar
, Sdefvar
, 1, UNEVALLED
, 0,
731 doc
: /* Define SYMBOL as a variable, and return SYMBOL.
732 You are not required to define a variable in order to use it,
733 but the definition can supply documentation and an initial value
734 in a way that tags can recognize.
736 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
737 If SYMBOL is buffer-local, its default value is what is set;
738 buffer-local values are not affected.
739 INITVALUE and DOCSTRING are optional.
740 If DOCSTRING starts with *, this variable is identified as a user option.
741 This means that M-x set-variable recognizes it.
742 See also `user-variable-p'.
743 If INITVALUE is missing, SYMBOL's value is not set.
745 If SYMBOL has a local binding, then this form affects the local
746 binding. This is usually not what you want. Thus, if you need to
747 load a file defining variables, with this form or with `defconst' or
748 `defcustom', you should always load that file _outside_ any bindings
749 for these variables. \(`defconst' and `defcustom' behave similarly in
751 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
754 register Lisp_Object sym
, tem
, tail
;
758 if (!NILP (Fcdr (Fcdr (tail
))))
759 error ("Too many arguments");
761 tem
= Fdefault_boundp (sym
);
764 if (SYMBOL_CONSTANT_P (sym
))
766 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
767 Lisp_Object tem1
= Fcar (tail
);
769 && EQ (XCAR (tem1
), Qquote
)
770 && CONSP (XCDR (tem1
))
771 && EQ (XCAR (XCDR (tem1
)), sym
)))
772 error ("Constant symbol `%s' specified in defvar",
773 SDATA (SYMBOL_NAME (sym
)));
777 Fset_default (sym
, Feval (Fcar (tail
)));
779 { /* Check if there is really a global binding rather than just a let
780 binding that shadows the global unboundness of the var. */
781 volatile struct specbinding
*pdl
= specpdl_ptr
;
782 while (--pdl
>= specpdl
)
784 if (EQ (pdl
->symbol
, sym
) && !pdl
->func
785 && EQ (pdl
->old_value
, Qunbound
))
787 message_with_string ("Warning: defvar ignored because %s is let-bound",
788 SYMBOL_NAME (sym
), 1);
797 if (!NILP (Vpurify_flag
))
798 tem
= Fpurecopy (tem
);
799 Fput (sym
, Qvariable_documentation
, tem
);
801 LOADHIST_ATTACH (sym
);
805 /* Simple (defvar <var>) should not count as a definition at all.
806 It could get in the way of other definitions, and unloading this
807 package could try to make the variable unbound. */
813 DEFUN ("defconst", Fdefconst
, Sdefconst
, 2, UNEVALLED
, 0,
814 doc
: /* Define SYMBOL as a constant variable.
815 The intent is that neither programs nor users should ever change this value.
816 Always sets the value of SYMBOL to the result of evalling INITVALUE.
817 If SYMBOL is buffer-local, its default value is what is set;
818 buffer-local values are not affected.
819 DOCSTRING is optional.
821 If SYMBOL has a local binding, then this form sets the local binding's
822 value. However, you should normally not make local bindings for
823 variables defined with this form.
824 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
827 register Lisp_Object sym
, tem
;
830 if (!NILP (Fcdr (Fcdr (Fcdr (args
)))))
831 error ("Too many arguments");
833 tem
= Feval (Fcar (Fcdr (args
)));
834 if (!NILP (Vpurify_flag
))
835 tem
= Fpurecopy (tem
);
836 Fset_default (sym
, tem
);
837 tem
= Fcar (Fcdr (Fcdr (args
)));
840 if (!NILP (Vpurify_flag
))
841 tem
= Fpurecopy (tem
);
842 Fput (sym
, Qvariable_documentation
, tem
);
844 Fput (sym
, Qrisky_local_variable
, Qt
);
845 LOADHIST_ATTACH (sym
);
849 /* Error handler used in Fuser_variable_p. */
851 user_variable_p_eh (Lisp_Object ignore
)
857 lisp_indirect_variable (Lisp_Object sym
)
859 struct Lisp_Symbol
*s
= indirect_variable (XSYMBOL (sym
));
864 DEFUN ("user-variable-p", Fuser_variable_p
, Suser_variable_p
, 1, 1, 0,
865 doc
: /* Return t if VARIABLE is intended to be set and modified by users.
866 \(The alternative is a variable used internally in a Lisp program.)
867 A variable is a user variable if
868 \(1) the first character of its documentation is `*', or
869 \(2) it is customizable (its property list contains a non-nil value
870 of `standard-value' or `custom-autoload'), or
871 \(3) it is an alias for another user variable.
872 Return nil if VARIABLE is an alias and there is a loop in the
873 chain of symbols. */)
874 (Lisp_Object variable
)
876 Lisp_Object documentation
;
878 if (!SYMBOLP (variable
))
881 /* If indirect and there's an alias loop, don't check anything else. */
882 if (XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
883 && NILP (internal_condition_case_1 (lisp_indirect_variable
, variable
,
884 Qt
, user_variable_p_eh
)))
889 documentation
= Fget (variable
, Qvariable_documentation
);
890 if (INTEGERP (documentation
) && XINT (documentation
) < 0)
892 if (STRINGP (documentation
)
893 && ((unsigned char) SREF (documentation
, 0) == '*'))
895 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
896 if (CONSP (documentation
)
897 && STRINGP (XCAR (documentation
))
898 && INTEGERP (XCDR (documentation
))
899 && XINT (XCDR (documentation
)) < 0)
901 /* Customizable? See `custom-variable-p'. */
902 if ((!NILP (Fget (variable
, intern ("standard-value"))))
903 || (!NILP (Fget (variable
, intern ("custom-autoload")))))
906 if (!(XSYMBOL (variable
)->redirect
== SYMBOL_VARALIAS
))
909 /* An indirect variable? Let's follow the chain. */
910 XSETSYMBOL (variable
, SYMBOL_ALIAS (XSYMBOL (variable
)));
914 DEFUN ("let*", FletX
, SletX
, 1, UNEVALLED
, 0,
915 doc
: /* Bind variables according to VARLIST then eval BODY.
916 The value of the last form in BODY is returned.
917 Each element of VARLIST is a symbol (which is bound to nil)
918 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
919 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
920 usage: (let* VARLIST BODY...) */)
923 Lisp_Object varlist
, val
, elt
;
924 int count
= SPECPDL_INDEX ();
925 struct gcpro gcpro1
, gcpro2
, gcpro3
;
927 GCPRO3 (args
, elt
, varlist
);
929 varlist
= Fcar (args
);
930 while (!NILP (varlist
))
933 elt
= Fcar (varlist
);
935 specbind (elt
, Qnil
);
936 else if (! NILP (Fcdr (Fcdr (elt
))))
937 signal_error ("`let' bindings can have only one value-form", elt
);
940 val
= Feval (Fcar (Fcdr (elt
)));
941 specbind (Fcar (elt
), val
);
943 varlist
= Fcdr (varlist
);
946 val
= Fprogn (Fcdr (args
));
947 return unbind_to (count
, val
);
950 DEFUN ("let", Flet
, Slet
, 1, UNEVALLED
, 0,
951 doc
: /* Bind variables according to VARLIST then eval BODY.
952 The value of the last form in BODY is returned.
953 Each element of VARLIST is a symbol (which is bound to nil)
954 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
955 All the VALUEFORMs are evalled before any symbols are bound.
956 usage: (let VARLIST BODY...) */)
959 Lisp_Object
*temps
, tem
;
960 register Lisp_Object elt
, varlist
;
961 int count
= SPECPDL_INDEX ();
962 register size_t argnum
;
963 struct gcpro gcpro1
, gcpro2
;
966 varlist
= Fcar (args
);
968 /* Make space to hold the values to give the bound variables */
969 elt
= Flength (varlist
);
970 SAFE_ALLOCA_LISP (temps
, XFASTINT (elt
));
972 /* Compute the values and store them in `temps' */
974 GCPRO2 (args
, *temps
);
977 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
980 elt
= XCAR (varlist
);
982 temps
[argnum
++] = Qnil
;
983 else if (! NILP (Fcdr (Fcdr (elt
))))
984 signal_error ("`let' bindings can have only one value-form", elt
);
986 temps
[argnum
++] = Feval (Fcar (Fcdr (elt
)));
987 gcpro2
.nvars
= argnum
;
991 varlist
= Fcar (args
);
992 for (argnum
= 0; CONSP (varlist
); varlist
= XCDR (varlist
))
994 elt
= XCAR (varlist
);
995 tem
= temps
[argnum
++];
999 specbind (Fcar (elt
), tem
);
1002 elt
= Fprogn (Fcdr (args
));
1004 return unbind_to (count
, elt
);
1007 DEFUN ("while", Fwhile
, Swhile
, 1, UNEVALLED
, 0,
1008 doc
: /* If TEST yields non-nil, eval BODY... and repeat.
1009 The order of execution is thus TEST, BODY, TEST, BODY and so on
1010 until TEST returns nil.
1011 usage: (while TEST BODY...) */)
1014 Lisp_Object test
, body
;
1015 struct gcpro gcpro1
, gcpro2
;
1017 GCPRO2 (test
, body
);
1021 while (!NILP (Feval (test
)))
1031 DEFUN ("macroexpand", Fmacroexpand
, Smacroexpand
, 1, 2, 0,
1032 doc
: /* Return result of expanding macros at top level of FORM.
1033 If FORM is not a macro call, it is returned unchanged.
1034 Otherwise, the macro is expanded and the expansion is considered
1035 in place of FORM. When a non-macro-call results, it is returned.
1037 The second optional arg ENVIRONMENT specifies an environment of macro
1038 definitions to shadow the loaded ones for use in file byte-compilation. */)
1039 (Lisp_Object form
, Lisp_Object environment
)
1041 /* With cleanups from Hallvard Furuseth. */
1042 register Lisp_Object expander
, sym
, def
, tem
;
1046 /* Come back here each time we expand a macro call,
1047 in case it expands into another macro call. */
1050 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1051 def
= sym
= XCAR (form
);
1053 /* Trace symbols aliases to other symbols
1054 until we get a symbol that is not an alias. */
1055 while (SYMBOLP (def
))
1059 tem
= Fassq (sym
, environment
);
1062 def
= XSYMBOL (sym
)->function
;
1063 if (!EQ (def
, Qunbound
))
1068 /* Right now TEM is the result from SYM in ENVIRONMENT,
1069 and if TEM is nil then DEF is SYM's function definition. */
1072 /* SYM is not mentioned in ENVIRONMENT.
1073 Look at its function definition. */
1074 if (EQ (def
, Qunbound
) || !CONSP (def
))
1075 /* Not defined or definition not suitable */
1077 if (EQ (XCAR (def
), Qautoload
))
1079 /* Autoloading function: will it be a macro when loaded? */
1080 tem
= Fnth (make_number (4), def
);
1081 if (EQ (tem
, Qt
) || EQ (tem
, Qmacro
))
1082 /* Yes, load it and try again. */
1084 struct gcpro gcpro1
;
1086 do_autoload (def
, sym
);
1093 else if (!EQ (XCAR (def
), Qmacro
))
1095 else expander
= XCDR (def
);
1099 expander
= XCDR (tem
);
1100 if (NILP (expander
))
1103 form
= apply1 (expander
, XCDR (form
));
1108 DEFUN ("catch", Fcatch
, Scatch
, 1, UNEVALLED
, 0,
1109 doc
: /* Eval BODY allowing nonlocal exits using `throw'.
1110 TAG is evalled to get the tag to use; it must not be nil.
1112 Then the BODY is executed.
1113 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1114 If no throw happens, `catch' returns the value of the last BODY form.
1115 If a throw happens, it specifies the value to return from `catch'.
1116 usage: (catch TAG BODY...) */)
1119 register Lisp_Object tag
;
1120 struct gcpro gcpro1
;
1123 tag
= Feval (Fcar (args
));
1125 return internal_catch (tag
, Fprogn
, Fcdr (args
));
1128 /* Set up a catch, then call C function FUNC on argument ARG.
1129 FUNC should return a Lisp_Object.
1130 This is how catches are done from within C code. */
1133 internal_catch (Lisp_Object tag
, Lisp_Object (*func
) (Lisp_Object
), Lisp_Object arg
)
1135 /* This structure is made part of the chain `catchlist'. */
1138 /* Fill in the components of c, and put it on the list. */
1142 c
.backlist
= backtrace_list
;
1143 c
.handlerlist
= handlerlist
;
1144 c
.lisp_eval_depth
= lisp_eval_depth
;
1145 c
.pdlcount
= SPECPDL_INDEX ();
1146 c
.poll_suppress_count
= poll_suppress_count
;
1147 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1148 c
.gcpro
= gcprolist
;
1149 c
.byte_stack
= byte_stack_list
;
1153 if (! _setjmp (c
.jmp
))
1154 c
.val
= (*func
) (arg
);
1156 /* Throw works by a longjmp that comes right here. */
1161 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1162 jump to that CATCH, returning VALUE as the value of that catch.
1164 This is the guts Fthrow and Fsignal; they differ only in the way
1165 they choose the catch tag to throw to. A catch tag for a
1166 condition-case form has a TAG of Qnil.
1168 Before each catch is discarded, unbind all special bindings and
1169 execute all unwind-protect clauses made above that catch. Unwind
1170 the handler stack as we go, so that the proper handlers are in
1171 effect for each unwind-protect clause we run. At the end, restore
1172 some static info saved in CATCH, and longjmp to the location
1175 This is used for correct unwinding in Fthrow and Fsignal. */
1178 unwind_to_catch (struct catchtag
*catch, Lisp_Object value
)
1180 register int last_time
;
1182 /* Save the value in the tag. */
1185 /* Restore certain special C variables. */
1186 set_poll_suppress_count (catch->poll_suppress_count
);
1187 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked
);
1188 handling_signal
= 0;
1193 last_time
= catchlist
== catch;
1195 /* Unwind the specpdl stack, and then restore the proper set of
1197 unbind_to (catchlist
->pdlcount
, Qnil
);
1198 handlerlist
= catchlist
->handlerlist
;
1199 catchlist
= catchlist
->next
;
1201 while (! last_time
);
1204 /* If x_catch_errors was done, turn it off now.
1205 (First we give unbind_to a chance to do that.) */
1206 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1207 The catch must remain in effect during that delicate
1208 state. --lorentey */
1209 x_fully_uncatch_errors ();
1213 byte_stack_list
= catch->byte_stack
;
1214 gcprolist
= catch->gcpro
;
1217 gcpro_level
= gcprolist
->level
+ 1;
1221 backtrace_list
= catch->backlist
;
1222 lisp_eval_depth
= catch->lisp_eval_depth
;
1224 _longjmp (catch->jmp
, 1);
1227 DEFUN ("throw", Fthrow
, Sthrow
, 2, 2, 0,
1228 doc
: /* Throw to the catch for TAG and return VALUE from it.
1229 Both TAG and VALUE are evalled. */)
1230 (register Lisp_Object tag
, Lisp_Object value
)
1232 register struct catchtag
*c
;
1235 for (c
= catchlist
; c
; c
= c
->next
)
1237 if (EQ (c
->tag
, tag
))
1238 unwind_to_catch (c
, value
);
1240 xsignal2 (Qno_catch
, tag
, value
);
1244 DEFUN ("unwind-protect", Funwind_protect
, Sunwind_protect
, 1, UNEVALLED
, 0,
1245 doc
: /* Do BODYFORM, protecting with UNWINDFORMS.
1246 If BODYFORM completes normally, its value is returned
1247 after executing the UNWINDFORMS.
1248 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1249 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1253 int count
= SPECPDL_INDEX ();
1255 record_unwind_protect (Fprogn
, Fcdr (args
));
1256 val
= Feval (Fcar (args
));
1257 return unbind_to (count
, val
);
1260 /* Chain of condition handlers currently in effect.
1261 The elements of this chain are contained in the stack frames
1262 of Fcondition_case and internal_condition_case.
1263 When an error is signaled (by calling Fsignal, below),
1264 this chain is searched for an element that applies. */
1266 struct handler
*handlerlist
;
1268 DEFUN ("condition-case", Fcondition_case
, Scondition_case
, 2, UNEVALLED
, 0,
1269 doc
: /* Regain control when an error is signaled.
1270 Executes BODYFORM and returns its value if no error happens.
1271 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1272 where the BODY is made of Lisp expressions.
1274 A handler is applicable to an error
1275 if CONDITION-NAME is one of the error's condition names.
1276 If an error happens, the first applicable handler is run.
1278 The car of a handler may be a list of condition names
1279 instead of a single condition name. Then it handles all of them.
1281 When a handler handles an error, control returns to the `condition-case'
1282 and it executes the handler's BODY...
1283 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1284 \(If VAR is nil, the handler can't access that information.)
1285 Then the value of the last BODY form is returned from the `condition-case'
1288 See also the function `signal' for more info.
1289 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1292 register Lisp_Object bodyform
, handlers
;
1293 volatile Lisp_Object var
;
1296 bodyform
= Fcar (Fcdr (args
));
1297 handlers
= Fcdr (Fcdr (args
));
1299 return internal_lisp_condition_case (var
, bodyform
, handlers
);
1302 /* Like Fcondition_case, but the args are separate
1303 rather than passed in a list. Used by Fbyte_code. */
1306 internal_lisp_condition_case (volatile Lisp_Object var
, Lisp_Object bodyform
,
1307 Lisp_Object handlers
)
1315 for (val
= handlers
; CONSP (val
); val
= XCDR (val
))
1321 && (SYMBOLP (XCAR (tem
))
1322 || CONSP (XCAR (tem
))))))
1323 error ("Invalid condition handler", tem
);
1328 c
.backlist
= backtrace_list
;
1329 c
.handlerlist
= handlerlist
;
1330 c
.lisp_eval_depth
= lisp_eval_depth
;
1331 c
.pdlcount
= SPECPDL_INDEX ();
1332 c
.poll_suppress_count
= poll_suppress_count
;
1333 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1334 c
.gcpro
= gcprolist
;
1335 c
.byte_stack
= byte_stack_list
;
1336 if (_setjmp (c
.jmp
))
1339 specbind (h
.var
, c
.val
);
1340 val
= Fprogn (Fcdr (h
.chosen_clause
));
1342 /* Note that this just undoes the binding of h.var; whoever
1343 longjumped to us unwound the stack to c.pdlcount before
1345 unbind_to (c
.pdlcount
, Qnil
);
1352 h
.handler
= handlers
;
1353 h
.next
= handlerlist
;
1357 val
= Feval (bodyform
);
1359 handlerlist
= h
.next
;
1363 /* Call the function BFUN with no arguments, catching errors within it
1364 according to HANDLERS. If there is an error, call HFUN with
1365 one argument which is the data that describes the error:
1368 HANDLERS can be a list of conditions to catch.
1369 If HANDLERS is Qt, catch all errors.
1370 If HANDLERS is Qerror, catch all errors
1371 but allow the debugger to run if that is enabled. */
1374 internal_condition_case (Lisp_Object (*bfun
) (void), Lisp_Object handlers
,
1375 Lisp_Object (*hfun
) (Lisp_Object
))
1381 /* Since Fsignal will close off all calls to x_catch_errors,
1382 we will get the wrong results if some are not closed now. */
1384 if (x_catching_errors ())
1390 c
.backlist
= backtrace_list
;
1391 c
.handlerlist
= handlerlist
;
1392 c
.lisp_eval_depth
= lisp_eval_depth
;
1393 c
.pdlcount
= SPECPDL_INDEX ();
1394 c
.poll_suppress_count
= poll_suppress_count
;
1395 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1396 c
.gcpro
= gcprolist
;
1397 c
.byte_stack
= byte_stack_list
;
1398 if (_setjmp (c
.jmp
))
1400 return (*hfun
) (c
.val
);
1404 h
.handler
= handlers
;
1406 h
.next
= handlerlist
;
1412 handlerlist
= h
.next
;
1416 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1419 internal_condition_case_1 (Lisp_Object (*bfun
) (Lisp_Object
), Lisp_Object arg
,
1420 Lisp_Object handlers
, Lisp_Object (*hfun
) (Lisp_Object
))
1426 /* Since Fsignal will close off all calls to x_catch_errors,
1427 we will get the wrong results if some are not closed now. */
1429 if (x_catching_errors ())
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
) (arg
);
1457 handlerlist
= h
.next
;
1461 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1465 internal_condition_case_2 (Lisp_Object (*bfun
) (Lisp_Object
, Lisp_Object
),
1468 Lisp_Object handlers
,
1469 Lisp_Object (*hfun
) (Lisp_Object
))
1475 /* Since Fsignal will close off all calls to x_catch_errors,
1476 we will get the wrong results if some are not closed now. */
1478 if (x_catching_errors ())
1484 c
.backlist
= backtrace_list
;
1485 c
.handlerlist
= handlerlist
;
1486 c
.lisp_eval_depth
= lisp_eval_depth
;
1487 c
.pdlcount
= SPECPDL_INDEX ();
1488 c
.poll_suppress_count
= poll_suppress_count
;
1489 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1490 c
.gcpro
= gcprolist
;
1491 c
.byte_stack
= byte_stack_list
;
1492 if (_setjmp (c
.jmp
))
1494 return (*hfun
) (c
.val
);
1498 h
.handler
= handlers
;
1500 h
.next
= handlerlist
;
1504 val
= (*bfun
) (arg1
, arg2
);
1506 handlerlist
= h
.next
;
1510 /* Like internal_condition_case but call BFUN with NARGS as first,
1511 and ARGS as second argument. */
1514 internal_condition_case_n (Lisp_Object (*bfun
) (size_t, Lisp_Object
*),
1517 Lisp_Object handlers
,
1518 Lisp_Object (*hfun
) (Lisp_Object
))
1524 /* Since Fsignal will close off all calls to x_catch_errors,
1525 we will get the wrong results if some are not closed now. */
1527 if (x_catching_errors ())
1533 c
.backlist
= backtrace_list
;
1534 c
.handlerlist
= handlerlist
;
1535 c
.lisp_eval_depth
= lisp_eval_depth
;
1536 c
.pdlcount
= SPECPDL_INDEX ();
1537 c
.poll_suppress_count
= poll_suppress_count
;
1538 c
.interrupt_input_blocked
= interrupt_input_blocked
;
1539 c
.gcpro
= gcprolist
;
1540 c
.byte_stack
= byte_stack_list
;
1541 if (_setjmp (c
.jmp
))
1543 return (*hfun
) (c
.val
);
1547 h
.handler
= handlers
;
1549 h
.next
= handlerlist
;
1553 val
= (*bfun
) (nargs
, args
);
1555 handlerlist
= h
.next
;
1560 static Lisp_Object
find_handler_clause (Lisp_Object
, Lisp_Object
,
1561 Lisp_Object
, Lisp_Object
);
1562 static int maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
,
1565 DEFUN ("signal", Fsignal
, Ssignal
, 2, 2, 0,
1566 doc
: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1567 This function does not return.
1569 An error symbol is a symbol with an `error-conditions' property
1570 that is a list of condition names.
1571 A handler for any of those names will get to handle this signal.
1572 The symbol `error' should normally be one of them.
1574 DATA should be a list. Its elements are printed as part of the error message.
1575 See Info anchor `(elisp)Definition of signal' for some details on how this
1576 error message is constructed.
1577 If the signal is handled, DATA is made available to the handler.
1578 See also the function `condition-case'. */)
1579 (Lisp_Object error_symbol
, Lisp_Object data
)
1581 /* When memory is full, ERROR-SYMBOL is nil,
1582 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1583 That is a special case--don't do this in other situations. */
1584 Lisp_Object conditions
;
1586 Lisp_Object real_error_symbol
1587 = (NILP (error_symbol
) ? Fcar (data
) : error_symbol
);
1588 register Lisp_Object clause
= Qnil
;
1590 struct backtrace
*bp
;
1592 immediate_quit
= handling_signal
= 0;
1594 if (gc_in_progress
|| waiting_for_input
)
1597 #if 0 /* rms: I don't know why this was here,
1598 but it is surely wrong for an error that is handled. */
1599 #ifdef HAVE_WINDOW_SYSTEM
1600 if (display_hourglass_p
)
1601 cancel_hourglass ();
1605 /* This hook is used by edebug. */
1606 if (! NILP (Vsignal_hook_function
)
1607 && ! NILP (error_symbol
))
1609 /* Edebug takes care of restoring these variables when it exits. */
1610 if (lisp_eval_depth
+ 20 > max_lisp_eval_depth
)
1611 max_lisp_eval_depth
= lisp_eval_depth
+ 20;
1613 if (SPECPDL_INDEX () + 40 > max_specpdl_size
)
1614 max_specpdl_size
= SPECPDL_INDEX () + 40;
1616 call2 (Vsignal_hook_function
, error_symbol
, data
);
1619 conditions
= Fget (real_error_symbol
, Qerror_conditions
);
1621 /* Remember from where signal was called. Skip over the frame for
1622 `signal' itself. If a frame for `error' follows, skip that,
1623 too. Don't do this when ERROR_SYMBOL is nil, because that
1624 is a memory-full error. */
1625 Vsignaling_function
= Qnil
;
1626 if (backtrace_list
&& !NILP (error_symbol
))
1628 bp
= backtrace_list
->next
;
1629 if (bp
&& bp
->function
&& EQ (*bp
->function
, Qerror
))
1631 if (bp
&& bp
->function
)
1632 Vsignaling_function
= *bp
->function
;
1635 for (h
= handlerlist
; h
; h
= h
->next
)
1637 clause
= find_handler_clause (h
->handler
, conditions
,
1638 error_symbol
, data
);
1643 if (/* Don't run the debugger for a memory-full error.
1644 (There is no room in memory to do that!) */
1645 !NILP (error_symbol
)
1646 && (!NILP (Vdebug_on_signal
)
1647 /* If no handler is present now, try to run the debugger. */
1649 /* Special handler that means "print a message and run debugger
1651 || EQ (h
->handler
, Qerror
)))
1654 = maybe_call_debugger (conditions
, error_symbol
, data
);
1655 /* We can't return values to code which signaled an error, but we
1656 can continue code which has signaled a quit. */
1657 if (debugger_called
&& EQ (real_error_symbol
, Qquit
))
1663 Lisp_Object unwind_data
1664 = (NILP (error_symbol
) ? data
: Fcons (error_symbol
, data
));
1666 h
->chosen_clause
= clause
;
1667 unwind_to_catch (h
->tag
, unwind_data
);
1672 Fthrow (Qtop_level
, Qt
);
1675 if (! NILP (error_symbol
))
1676 data
= Fcons (error_symbol
, data
);
1678 string
= Ferror_message_string (data
);
1679 fatal ("%s", SDATA (string
), 0);
1682 /* Internal version of Fsignal that never returns.
1683 Used for anything but Qquit (which can return from Fsignal). */
1686 xsignal (Lisp_Object error_symbol
, Lisp_Object data
)
1688 Fsignal (error_symbol
, data
);
1692 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1695 xsignal0 (Lisp_Object error_symbol
)
1697 xsignal (error_symbol
, Qnil
);
1701 xsignal1 (Lisp_Object error_symbol
, Lisp_Object arg
)
1703 xsignal (error_symbol
, list1 (arg
));
1707 xsignal2 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
)
1709 xsignal (error_symbol
, list2 (arg1
, arg2
));
1713 xsignal3 (Lisp_Object error_symbol
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
1715 xsignal (error_symbol
, list3 (arg1
, arg2
, arg3
));
1718 /* Signal `error' with message S, and additional arg ARG.
1719 If ARG is not a genuine list, make it a one-element list. */
1722 signal_error (const char *s
, Lisp_Object arg
)
1724 Lisp_Object tortoise
, hare
;
1726 hare
= tortoise
= arg
;
1727 while (CONSP (hare
))
1734 tortoise
= XCDR (tortoise
);
1736 if (EQ (hare
, tortoise
))
1741 arg
= Fcons (arg
, Qnil
); /* Make it a list. */
1743 xsignal (Qerror
, Fcons (build_string (s
), arg
));
1747 /* Return nonzero if LIST is a non-nil atom or
1748 a list containing one of CONDITIONS. */
1751 wants_debugger (Lisp_Object list
, Lisp_Object conditions
)
1758 while (CONSP (conditions
))
1760 Lisp_Object
this, tail
;
1761 this = XCAR (conditions
);
1762 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
1763 if (EQ (XCAR (tail
), this))
1765 conditions
= XCDR (conditions
);
1770 /* Return 1 if an error with condition-symbols CONDITIONS,
1771 and described by SIGNAL-DATA, should skip the debugger
1772 according to debugger-ignored-errors. */
1775 skip_debugger (Lisp_Object conditions
, Lisp_Object data
)
1778 int first_string
= 1;
1779 Lisp_Object error_message
;
1781 error_message
= Qnil
;
1782 for (tail
= Vdebug_ignored_errors
; CONSP (tail
); tail
= XCDR (tail
))
1784 if (STRINGP (XCAR (tail
)))
1788 error_message
= Ferror_message_string (data
);
1792 if (fast_string_match (XCAR (tail
), error_message
) >= 0)
1797 Lisp_Object contail
;
1799 for (contail
= conditions
; CONSP (contail
); contail
= XCDR (contail
))
1800 if (EQ (XCAR (tail
), XCAR (contail
)))
1808 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1809 SIG and DATA describe the signal, as in find_handler_clause. */
1812 maybe_call_debugger (Lisp_Object conditions
, Lisp_Object sig
, Lisp_Object data
)
1814 Lisp_Object combined_data
;
1816 combined_data
= Fcons (sig
, data
);
1819 /* Don't try to run the debugger with interrupts blocked.
1820 The editing loop would return anyway. */
1822 /* Does user want to enter debugger for this kind of error? */
1825 : wants_debugger (Vdebug_on_error
, conditions
))
1826 && ! skip_debugger (conditions
, combined_data
)
1827 /* rms: what's this for? */
1828 && when_entered_debugger
< num_nonmacro_input_events
)
1830 call_debugger (Fcons (Qerror
, Fcons (combined_data
, Qnil
)));
1837 /* Value of Qlambda means we have called debugger and user has continued.
1838 There are two ways to pass SIG and DATA:
1839 = SIG is the error symbol, and DATA is the rest of the data.
1840 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1841 This is for memory-full errors only.
1843 We need to increase max_specpdl_size temporarily around
1844 anything we do that can push on the specpdl, so as not to get
1845 a second error here in case we're handling specpdl overflow. */
1848 find_handler_clause (Lisp_Object handlers
, Lisp_Object conditions
,
1849 Lisp_Object sig
, Lisp_Object data
)
1851 register Lisp_Object h
;
1853 /* t is used by handlers for all conditions, set up by C code. */
1854 if (EQ (handlers
, Qt
))
1857 /* error is used similarly, but means print an error message
1858 and run the debugger if that is enabled. */
1859 if (EQ (handlers
, Qerror
))
1862 for (h
= handlers
; CONSP (h
); h
= XCDR (h
))
1864 Lisp_Object handler
= XCAR (h
);
1865 Lisp_Object condit
, tem
;
1867 if (!CONSP (handler
))
1869 condit
= XCAR (handler
);
1870 /* Handle a single condition name in handler HANDLER. */
1871 if (SYMBOLP (condit
))
1873 tem
= Fmemq (Fcar (handler
), conditions
);
1877 /* Handle a list of condition names in handler HANDLER. */
1878 else if (CONSP (condit
))
1881 for (tail
= condit
; CONSP (tail
); tail
= XCDR (tail
))
1883 tem
= Fmemq (XCAR (tail
), conditions
);
1894 /* dump an error message; called like vprintf */
1896 verror (const char *m
, va_list ap
)
1899 EMACS_INT size
= 200;
1910 used
= doprnt (buffer
, size
, m
, m
+ mlen
, ap
);
1915 buffer
= (char *) xrealloc (buffer
, size
);
1918 buffer
= (char *) xmalloc (size
);
1923 string
= build_string (buffer
);
1927 xsignal1 (Qerror
, string
);
1931 /* dump an error message; called like printf */
1935 error (const char *m
, ...)
1943 DEFUN ("commandp", Fcommandp
, Scommandp
, 1, 2, 0,
1944 doc
: /* Non-nil if FUNCTION makes provisions for interactive calling.
1945 This means it contains a description for how to read arguments to give it.
1946 The value is nil for an invalid function or a symbol with no function
1949 Interactively callable functions include strings and vectors (treated
1950 as keyboard macros), lambda-expressions that contain a top-level call
1951 to `interactive', autoload definitions made by `autoload' with non-nil
1952 fourth argument, and some of the built-in functions of Lisp.
1954 Also, a symbol satisfies `commandp' if its function definition does so.
1956 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1957 then strings and vectors are not accepted. */)
1958 (Lisp_Object function
, Lisp_Object for_call_interactively
)
1960 register Lisp_Object fun
;
1961 register Lisp_Object funcar
;
1962 Lisp_Object if_prop
= Qnil
;
1966 fun
= indirect_function (fun
); /* Check cycles. */
1967 if (NILP (fun
) || EQ (fun
, Qunbound
))
1970 /* Check an `interactive-form' property if present, analogous to the
1971 function-documentation property. */
1973 while (SYMBOLP (fun
))
1975 Lisp_Object tmp
= Fget (fun
, Qinteractive_form
);
1978 fun
= Fsymbol_function (fun
);
1981 /* Emacs primitives are interactive if their DEFUN specifies an
1982 interactive spec. */
1984 return XSUBR (fun
)->intspec
? Qt
: if_prop
;
1986 /* Bytecode objects are interactive if they are long enough to
1987 have an element whose index is COMPILED_INTERACTIVE, which is
1988 where the interactive spec is stored. */
1989 else if (COMPILEDP (fun
))
1990 return ((ASIZE (fun
) & PSEUDOVECTOR_SIZE_MASK
) > COMPILED_INTERACTIVE
1993 /* Strings and vectors are keyboard macros. */
1994 if (STRINGP (fun
) || VECTORP (fun
))
1995 return (NILP (for_call_interactively
) ? Qt
: Qnil
);
1997 /* Lists may represent commands. */
2000 funcar
= XCAR (fun
);
2001 if (EQ (funcar
, Qlambda
))
2002 return !NILP (Fassq (Qinteractive
, Fcdr (XCDR (fun
)))) ? Qt
: if_prop
;
2003 if (EQ (funcar
, Qautoload
))
2004 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun
))))) ? Qt
: if_prop
;
2009 DEFUN ("autoload", Fautoload
, Sautoload
, 2, 5, 0,
2010 doc
: /* Define FUNCTION to autoload from FILE.
2011 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2012 Third arg DOCSTRING is documentation for the function.
2013 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2014 Fifth arg TYPE indicates the type of the object:
2015 nil or omitted says FUNCTION is a function,
2016 `keymap' says FUNCTION is really a keymap, and
2017 `macro' or t says FUNCTION is really a macro.
2018 Third through fifth args give info about the real definition.
2019 They default to nil.
2020 If FUNCTION is already defined other than as an autoload,
2021 this does nothing and returns nil. */)
2022 (Lisp_Object function
, Lisp_Object file
, Lisp_Object docstring
, Lisp_Object interactive
, Lisp_Object type
)
2024 CHECK_SYMBOL (function
);
2025 CHECK_STRING (file
);
2027 /* If function is defined and not as an autoload, don't override */
2028 if (!EQ (XSYMBOL (function
)->function
, Qunbound
)
2029 && !(CONSP (XSYMBOL (function
)->function
)
2030 && EQ (XCAR (XSYMBOL (function
)->function
), Qautoload
)))
2033 if (NILP (Vpurify_flag
))
2034 /* Only add entries after dumping, because the ones before are
2035 not useful and else we get loads of them from the loaddefs.el. */
2036 LOADHIST_ATTACH (Fcons (Qautoload
, function
));
2038 /* We don't want the docstring in purespace (instead,
2039 Snarf-documentation should (hopefully) overwrite it).
2040 We used to use 0 here, but that leads to accidental sharing in
2041 purecopy's hash-consing, so we use a (hopefully) unique integer
2043 docstring
= make_number (XHASH (function
));
2044 return Ffset (function
,
2045 Fpurecopy (list5 (Qautoload
, file
, docstring
,
2046 interactive
, type
)));
2050 un_autoload (Lisp_Object oldqueue
)
2052 register Lisp_Object queue
, first
, second
;
2054 /* Queue to unwind is current value of Vautoload_queue.
2055 oldqueue is the shadowed value to leave in Vautoload_queue. */
2056 queue
= Vautoload_queue
;
2057 Vautoload_queue
= oldqueue
;
2058 while (CONSP (queue
))
2060 first
= XCAR (queue
);
2061 second
= Fcdr (first
);
2062 first
= Fcar (first
);
2063 if (EQ (first
, make_number (0)))
2066 Ffset (first
, second
);
2067 queue
= XCDR (queue
);
2072 /* Load an autoloaded function.
2073 FUNNAME is the symbol which is the function's name.
2074 FUNDEF is the autoload definition (a list). */
2077 do_autoload (Lisp_Object fundef
, Lisp_Object funname
)
2079 int count
= SPECPDL_INDEX ();
2081 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2083 /* This is to make sure that loadup.el gives a clear picture
2084 of what files are preloaded and when. */
2085 if (! NILP (Vpurify_flag
))
2086 error ("Attempt to autoload %s while preparing to dump",
2087 SDATA (SYMBOL_NAME (funname
)));
2090 CHECK_SYMBOL (funname
);
2091 GCPRO3 (fun
, funname
, fundef
);
2093 /* Preserve the match data. */
2094 record_unwind_save_match_data ();
2096 /* If autoloading gets an error (which includes the error of failing
2097 to define the function being called), we use Vautoload_queue
2098 to undo function definitions and `provide' calls made by
2099 the function. We do this in the specific case of autoloading
2100 because autoloading is not an explicit request "load this file",
2101 but rather a request to "call this function".
2103 The value saved here is to be restored into Vautoload_queue. */
2104 record_unwind_protect (un_autoload
, Vautoload_queue
);
2105 Vautoload_queue
= Qt
;
2106 Fload (Fcar (Fcdr (fundef
)), Qnil
, Qt
, Qnil
, Qt
);
2108 /* Once loading finishes, don't undo it. */
2109 Vautoload_queue
= Qt
;
2110 unbind_to (count
, Qnil
);
2112 fun
= Findirect_function (fun
, Qnil
);
2114 if (!NILP (Fequal (fun
, fundef
)))
2115 error ("Autoloading failed to define function %s",
2116 SDATA (SYMBOL_NAME (funname
)));
2121 DEFUN ("eval", Feval
, Seval
, 1, 1, 0,
2122 doc
: /* Evaluate FORM and return its value. */)
2125 Lisp_Object fun
, val
, original_fun
, original_args
;
2127 struct backtrace backtrace
;
2128 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2130 if (handling_signal
)
2134 return Fsymbol_value (form
);
2139 if ((consing_since_gc
> gc_cons_threshold
2140 && consing_since_gc
> gc_relative_threshold
)
2142 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2145 Fgarbage_collect ();
2149 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2151 if (max_lisp_eval_depth
< 100)
2152 max_lisp_eval_depth
= 100;
2153 if (lisp_eval_depth
> max_lisp_eval_depth
)
2154 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2157 original_fun
= Fcar (form
);
2158 original_args
= Fcdr (form
);
2160 backtrace
.next
= backtrace_list
;
2161 backtrace_list
= &backtrace
;
2162 backtrace
.function
= &original_fun
; /* This also protects them from gc */
2163 backtrace
.args
= &original_args
;
2164 backtrace
.nargs
= UNEVALLED
;
2165 backtrace
.evalargs
= 1;
2166 backtrace
.debug_on_exit
= 0;
2168 if (debug_on_next_call
)
2169 do_debug_on_call (Qt
);
2171 /* At this point, only original_fun and original_args
2172 have values that will be used below */
2175 /* Optimize for no indirection. */
2177 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2178 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2179 fun
= indirect_function (fun
);
2183 Lisp_Object numargs
;
2184 Lisp_Object argvals
[8];
2185 Lisp_Object args_left
;
2186 register int i
, maxargs
;
2188 args_left
= original_args
;
2189 numargs
= Flength (args_left
);
2193 if (XINT (numargs
) < XSUBR (fun
)->min_args
||
2194 (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< XINT (numargs
)))
2195 xsignal2 (Qwrong_number_of_arguments
, original_fun
, numargs
);
2197 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2199 backtrace
.evalargs
= 0;
2200 val
= (XSUBR (fun
)->function
.aUNEVALLED
) (args_left
);
2202 else if (XSUBR (fun
)->max_args
== MANY
)
2204 /* Pass a vector of evaluated arguments */
2206 register size_t argnum
= 0;
2209 SAFE_ALLOCA_LISP (vals
, XINT (numargs
));
2211 GCPRO3 (args_left
, fun
, fun
);
2215 while (!NILP (args_left
))
2217 vals
[argnum
++] = Feval (Fcar (args_left
));
2218 args_left
= Fcdr (args_left
);
2219 gcpro3
.nvars
= argnum
;
2222 backtrace
.args
= vals
;
2223 backtrace
.nargs
= XINT (numargs
);
2225 val
= (XSUBR (fun
)->function
.aMANY
) (XINT (numargs
), vals
);
2231 GCPRO3 (args_left
, fun
, fun
);
2232 gcpro3
.var
= argvals
;
2235 maxargs
= XSUBR (fun
)->max_args
;
2236 for (i
= 0; i
< maxargs
; args_left
= Fcdr (args_left
))
2238 argvals
[i
] = Feval (Fcar (args_left
));
2244 backtrace
.args
= argvals
;
2245 backtrace
.nargs
= XINT (numargs
);
2250 val
= (XSUBR (fun
)->function
.a0 ());
2253 val
= (XSUBR (fun
)->function
.a1 (argvals
[0]));
2256 val
= (XSUBR (fun
)->function
.a2 (argvals
[0], argvals
[1]));
2259 val
= (XSUBR (fun
)->function
.a3
2260 (argvals
[0], argvals
[1], argvals
[2]));
2263 val
= (XSUBR (fun
)->function
.a4
2264 (argvals
[0], argvals
[1], argvals
[2], argvals
[3]));
2267 val
= (XSUBR (fun
)->function
.a5
2268 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2272 val
= (XSUBR (fun
)->function
.a6
2273 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2274 argvals
[4], argvals
[5]));
2277 val
= (XSUBR (fun
)->function
.a7
2278 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2279 argvals
[4], argvals
[5], argvals
[6]));
2283 val
= (XSUBR (fun
)->function
.a8
2284 (argvals
[0], argvals
[1], argvals
[2], argvals
[3],
2285 argvals
[4], argvals
[5], argvals
[6], argvals
[7]));
2289 /* Someone has created a subr that takes more arguments than
2290 is supported by this code. We need to either rewrite the
2291 subr to use a different argument protocol, or add more
2292 cases to this switch. */
2297 else if (COMPILEDP (fun
))
2298 val
= apply_lambda (fun
, original_args
, 1);
2301 if (EQ (fun
, Qunbound
))
2302 xsignal1 (Qvoid_function
, original_fun
);
2304 xsignal1 (Qinvalid_function
, original_fun
);
2305 funcar
= XCAR (fun
);
2306 if (!SYMBOLP (funcar
))
2307 xsignal1 (Qinvalid_function
, original_fun
);
2308 if (EQ (funcar
, Qautoload
))
2310 do_autoload (fun
, original_fun
);
2313 if (EQ (funcar
, Qmacro
))
2314 val
= Feval (apply1 (Fcdr (fun
), original_args
));
2315 else if (EQ (funcar
, Qlambda
))
2316 val
= apply_lambda (fun
, original_args
, 1);
2318 xsignal1 (Qinvalid_function
, original_fun
);
2323 if (backtrace
.debug_on_exit
)
2324 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2325 backtrace_list
= backtrace
.next
;
2330 DEFUN ("apply", Fapply
, Sapply
, 2, MANY
, 0,
2331 doc
: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2332 Then return the value FUNCTION returns.
2333 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2334 usage: (apply FUNCTION &rest ARGUMENTS) */)
2335 (size_t nargs
, Lisp_Object
*args
)
2337 register size_t i
, numargs
;
2338 register Lisp_Object spread_arg
;
2339 register Lisp_Object
*funcall_args
;
2340 Lisp_Object fun
, retval
;
2341 struct gcpro gcpro1
;
2346 spread_arg
= args
[nargs
- 1];
2347 CHECK_LIST (spread_arg
);
2349 numargs
= XINT (Flength (spread_arg
));
2352 return Ffuncall (nargs
- 1, args
);
2353 else if (numargs
== 1)
2355 args
[nargs
- 1] = XCAR (spread_arg
);
2356 return Ffuncall (nargs
, args
);
2359 numargs
+= nargs
- 2;
2361 /* Optimize for no indirection. */
2362 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2363 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2364 fun
= indirect_function (fun
);
2365 if (EQ (fun
, Qunbound
))
2367 /* Let funcall get the error */
2374 if (numargs
< XSUBR (fun
)->min_args
2375 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2376 goto funcall
; /* Let funcall get the error */
2377 else if (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
> numargs
)
2379 /* Avoid making funcall cons up a yet another new vector of arguments
2380 by explicitly supplying nil's for optional values */
2381 SAFE_ALLOCA_LISP (funcall_args
, 1 + XSUBR (fun
)->max_args
);
2382 for (i
= numargs
; i
< XSUBR (fun
)->max_args
;)
2383 funcall_args
[++i
] = Qnil
;
2384 GCPRO1 (*funcall_args
);
2385 gcpro1
.nvars
= 1 + XSUBR (fun
)->max_args
;
2389 /* We add 1 to numargs because funcall_args includes the
2390 function itself as well as its arguments. */
2393 SAFE_ALLOCA_LISP (funcall_args
, 1 + numargs
);
2394 GCPRO1 (*funcall_args
);
2395 gcpro1
.nvars
= 1 + numargs
;
2398 memcpy (funcall_args
, args
, nargs
* sizeof (Lisp_Object
));
2399 /* Spread the last arg we got. Its first element goes in
2400 the slot that it used to occupy, hence this value of I. */
2402 while (!NILP (spread_arg
))
2404 funcall_args
[i
++] = XCAR (spread_arg
);
2405 spread_arg
= XCDR (spread_arg
);
2408 /* By convention, the caller needs to gcpro Ffuncall's args. */
2409 retval
= Ffuncall (gcpro1
.nvars
, funcall_args
);
2416 /* Run hook variables in various ways. */
2418 enum run_hooks_condition
{to_completion
, until_success
, until_failure
};
2419 static Lisp_Object
run_hook_with_args (size_t, Lisp_Object
*,
2420 enum run_hooks_condition
);
2422 DEFUN ("run-hooks", Frun_hooks
, Srun_hooks
, 0, MANY
, 0,
2423 doc
: /* Run each hook in HOOKS.
2424 Each argument should be a symbol, a hook variable.
2425 These symbols are processed in the order specified.
2426 If a hook symbol has a non-nil value, that value may be a function
2427 or a list of functions to be called to run the hook.
2428 If the value is a function, it is called with no arguments.
2429 If it is a list, the elements are called, in order, with no arguments.
2431 Major modes should not use this function directly to run their mode
2432 hook; they should use `run-mode-hooks' instead.
2434 Do not use `make-local-variable' to make a hook variable buffer-local.
2435 Instead, use `add-hook' and specify t for the LOCAL argument.
2436 usage: (run-hooks &rest HOOKS) */)
2437 (size_t nargs
, Lisp_Object
*args
)
2439 Lisp_Object hook
[1];
2442 for (i
= 0; i
< nargs
; i
++)
2445 run_hook_with_args (1, hook
, to_completion
);
2451 DEFUN ("run-hook-with-args", Frun_hook_with_args
,
2452 Srun_hook_with_args
, 1, MANY
, 0,
2453 doc
: /* Run HOOK with the specified arguments ARGS.
2454 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2455 value, that value may be a function or a list of functions to be
2456 called to run the hook. If the value is a function, it is called with
2457 the given arguments and its return value is returned. If it is a list
2458 of functions, those functions are called, in order,
2459 with the given arguments ARGS.
2460 It is best not to depend on the value returned by `run-hook-with-args',
2463 Do not use `make-local-variable' to make a hook variable buffer-local.
2464 Instead, use `add-hook' and specify t for the LOCAL argument.
2465 usage: (run-hook-with-args HOOK &rest ARGS) */)
2466 (size_t nargs
, Lisp_Object
*args
)
2468 return run_hook_with_args (nargs
, args
, to_completion
);
2471 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success
,
2472 Srun_hook_with_args_until_success
, 1, MANY
, 0,
2473 doc
: /* Run HOOK with the specified arguments ARGS.
2474 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2475 value, that value may be a function or a list of functions to be
2476 called to run the hook. If the value is a function, it is called with
2477 the given arguments and its return value is returned.
2478 If it is a list of functions, those functions are called, in order,
2479 with the given arguments ARGS, until one of them
2480 returns a non-nil value. Then we return that value.
2481 However, if they all return nil, we return nil.
2483 Do not use `make-local-variable' to make a hook variable buffer-local.
2484 Instead, use `add-hook' and specify t for the LOCAL argument.
2485 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2486 (size_t nargs
, Lisp_Object
*args
)
2488 return run_hook_with_args (nargs
, args
, until_success
);
2491 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure
,
2492 Srun_hook_with_args_until_failure
, 1, MANY
, 0,
2493 doc
: /* Run HOOK with the specified arguments ARGS.
2494 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2495 value, that value may be a function or a list of functions to be
2496 called to run the hook. If the value is a function, it is called with
2497 the given arguments and its return value is returned.
2498 If it is a list of functions, those functions are called, in order,
2499 with the given arguments ARGS, until one of them returns nil.
2500 Then we return nil. However, if they all return non-nil, we return non-nil.
2502 Do not use `make-local-variable' to make a hook variable buffer-local.
2503 Instead, use `add-hook' and specify t for the LOCAL argument.
2504 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2505 (size_t nargs
, Lisp_Object
*args
)
2507 return run_hook_with_args (nargs
, args
, until_failure
);
2510 /* ARGS[0] should be a hook symbol.
2511 Call each of the functions in the hook value, passing each of them
2512 as arguments all the rest of ARGS (all NARGS - 1 elements).
2513 COND specifies a condition to test after each call
2514 to decide whether to stop.
2515 The caller (or its caller, etc) must gcpro all of ARGS,
2516 except that it isn't necessary to gcpro ARGS[0]. */
2519 run_hook_with_args (size_t nargs
, Lisp_Object
*args
,
2520 enum run_hooks_condition cond
)
2522 Lisp_Object sym
, val
, ret
;
2523 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2525 /* If we are dying or still initializing,
2526 don't do anything--it would probably crash if we tried. */
2527 if (NILP (Vrun_hooks
))
2531 val
= find_symbol_value (sym
);
2532 ret
= (cond
== until_failure
? Qt
: Qnil
);
2534 if (EQ (val
, Qunbound
) || NILP (val
))
2536 else if (!CONSP (val
) || EQ (XCAR (val
), Qlambda
))
2539 return Ffuncall (nargs
, args
);
2543 Lisp_Object global_vals
= Qnil
;
2544 GCPRO3 (sym
, val
, global_vals
);
2547 CONSP (val
) && ((cond
== to_completion
)
2548 || (cond
== until_success
? NILP (ret
)
2552 if (EQ (XCAR (val
), Qt
))
2554 /* t indicates this hook has a local binding;
2555 it means to run the global binding too. */
2556 global_vals
= Fdefault_value (sym
);
2557 if (NILP (global_vals
)) continue;
2559 if (!CONSP (global_vals
) || EQ (XCAR (global_vals
), Qlambda
))
2561 args
[0] = global_vals
;
2562 ret
= Ffuncall (nargs
, args
);
2567 (CONSP (global_vals
)
2568 && (cond
== to_completion
2569 || (cond
== until_success
2572 global_vals
= XCDR (global_vals
))
2574 args
[0] = XCAR (global_vals
);
2575 /* In a global value, t should not occur. If it does, we
2576 must ignore it to avoid an endless loop. */
2577 if (!EQ (args
[0], Qt
))
2578 ret
= Ffuncall (nargs
, args
);
2584 args
[0] = XCAR (val
);
2585 ret
= Ffuncall (nargs
, args
);
2594 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2597 run_hook_with_args_2 (Lisp_Object hook
, Lisp_Object arg1
, Lisp_Object arg2
)
2599 Lisp_Object temp
[3];
2604 Frun_hook_with_args (3, temp
);
2607 /* Apply fn to arg */
2609 apply1 (Lisp_Object fn
, Lisp_Object arg
)
2611 struct gcpro gcpro1
;
2615 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2618 Lisp_Object args
[2];
2622 RETURN_UNGCPRO (Fapply (2, args
));
2626 /* Call function fn on no arguments */
2628 call0 (Lisp_Object fn
)
2630 struct gcpro gcpro1
;
2633 RETURN_UNGCPRO (Ffuncall (1, &fn
));
2636 /* Call function fn with 1 argument arg1 */
2639 call1 (Lisp_Object fn
, Lisp_Object arg1
)
2641 struct gcpro gcpro1
;
2642 Lisp_Object args
[2];
2648 RETURN_UNGCPRO (Ffuncall (2, args
));
2651 /* Call function fn with 2 arguments arg1, arg2 */
2654 call2 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
)
2656 struct gcpro gcpro1
;
2657 Lisp_Object args
[3];
2663 RETURN_UNGCPRO (Ffuncall (3, args
));
2666 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2669 call3 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
)
2671 struct gcpro gcpro1
;
2672 Lisp_Object args
[4];
2679 RETURN_UNGCPRO (Ffuncall (4, args
));
2682 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2685 call4 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2688 struct gcpro gcpro1
;
2689 Lisp_Object args
[5];
2697 RETURN_UNGCPRO (Ffuncall (5, args
));
2700 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2703 call5 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2704 Lisp_Object arg4
, Lisp_Object arg5
)
2706 struct gcpro gcpro1
;
2707 Lisp_Object args
[6];
2716 RETURN_UNGCPRO (Ffuncall (6, args
));
2719 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2722 call6 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2723 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
)
2725 struct gcpro gcpro1
;
2726 Lisp_Object args
[7];
2736 RETURN_UNGCPRO (Ffuncall (7, args
));
2739 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7 */
2742 call7 (Lisp_Object fn
, Lisp_Object arg1
, Lisp_Object arg2
, Lisp_Object arg3
,
2743 Lisp_Object arg4
, Lisp_Object arg5
, Lisp_Object arg6
, Lisp_Object arg7
)
2745 struct gcpro gcpro1
;
2746 Lisp_Object args
[8];
2757 RETURN_UNGCPRO (Ffuncall (8, args
));
2760 /* The caller should GCPRO all the elements of ARGS. */
2762 DEFUN ("funcall", Ffuncall
, Sfuncall
, 1, MANY
, 0,
2763 doc
: /* Call first argument as a function, passing remaining arguments to it.
2764 Return the value that function returns.
2765 Thus, (funcall 'cons 'x 'y) returns (x . y).
2766 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2767 (size_t nargs
, Lisp_Object
*args
)
2769 Lisp_Object fun
, original_fun
;
2771 size_t numargs
= nargs
- 1;
2772 Lisp_Object lisp_numargs
;
2774 struct backtrace backtrace
;
2775 register Lisp_Object
*internal_args
;
2779 if ((consing_since_gc
> gc_cons_threshold
2780 && consing_since_gc
> gc_relative_threshold
)
2782 (!NILP (Vmemory_full
) && consing_since_gc
> memory_full_cons_threshold
))
2783 Fgarbage_collect ();
2785 if (++lisp_eval_depth
> max_lisp_eval_depth
)
2787 if (max_lisp_eval_depth
< 100)
2788 max_lisp_eval_depth
= 100;
2789 if (lisp_eval_depth
> max_lisp_eval_depth
)
2790 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2793 backtrace
.next
= backtrace_list
;
2794 backtrace_list
= &backtrace
;
2795 backtrace
.function
= &args
[0];
2796 backtrace
.args
= &args
[1];
2797 backtrace
.nargs
= nargs
- 1;
2798 backtrace
.evalargs
= 0;
2799 backtrace
.debug_on_exit
= 0;
2801 if (debug_on_next_call
)
2802 do_debug_on_call (Qlambda
);
2806 original_fun
= args
[0];
2810 /* Optimize for no indirection. */
2812 if (SYMBOLP (fun
) && !EQ (fun
, Qunbound
)
2813 && (fun
= XSYMBOL (fun
)->function
, SYMBOLP (fun
)))
2814 fun
= indirect_function (fun
);
2818 if (numargs
< XSUBR (fun
)->min_args
2819 || (XSUBR (fun
)->max_args
>= 0 && XSUBR (fun
)->max_args
< numargs
))
2821 XSETFASTINT (lisp_numargs
, numargs
);
2822 xsignal2 (Qwrong_number_of_arguments
, original_fun
, lisp_numargs
);
2825 else if (XSUBR (fun
)->max_args
== UNEVALLED
)
2826 xsignal1 (Qinvalid_function
, original_fun
);
2828 else if (XSUBR (fun
)->max_args
== MANY
)
2829 val
= (XSUBR (fun
)->function
.aMANY
) (numargs
, args
+ 1);
2832 if (XSUBR (fun
)->max_args
> numargs
)
2834 internal_args
= (Lisp_Object
*) alloca (XSUBR (fun
)->max_args
* sizeof (Lisp_Object
));
2835 memcpy (internal_args
, args
+ 1, numargs
* sizeof (Lisp_Object
));
2836 for (i
= numargs
; i
< XSUBR (fun
)->max_args
; i
++)
2837 internal_args
[i
] = Qnil
;
2840 internal_args
= args
+ 1;
2841 switch (XSUBR (fun
)->max_args
)
2844 val
= (XSUBR (fun
)->function
.a0 ());
2847 val
= (XSUBR (fun
)->function
.a1 (internal_args
[0]));
2850 val
= (XSUBR (fun
)->function
.a2
2851 (internal_args
[0], internal_args
[1]));
2854 val
= (XSUBR (fun
)->function
.a3
2855 (internal_args
[0], internal_args
[1], internal_args
[2]));
2858 val
= (XSUBR (fun
)->function
.a4
2859 (internal_args
[0], internal_args
[1], internal_args
[2],
2863 val
= (XSUBR (fun
)->function
.a5
2864 (internal_args
[0], internal_args
[1], internal_args
[2],
2865 internal_args
[3], internal_args
[4]));
2868 val
= (XSUBR (fun
)->function
.a6
2869 (internal_args
[0], internal_args
[1], internal_args
[2],
2870 internal_args
[3], internal_args
[4], internal_args
[5]));
2873 val
= (XSUBR (fun
)->function
.a7
2874 (internal_args
[0], internal_args
[1], internal_args
[2],
2875 internal_args
[3], internal_args
[4], internal_args
[5],
2880 val
= (XSUBR (fun
)->function
.a8
2881 (internal_args
[0], internal_args
[1], internal_args
[2],
2882 internal_args
[3], internal_args
[4], internal_args
[5],
2883 internal_args
[6], internal_args
[7]));
2888 /* If a subr takes more than 8 arguments without using MANY
2889 or UNEVALLED, we need to extend this function to support it.
2890 Until this is done, there is no way to call the function. */
2895 else if (COMPILEDP (fun
))
2896 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2899 if (EQ (fun
, Qunbound
))
2900 xsignal1 (Qvoid_function
, original_fun
);
2902 xsignal1 (Qinvalid_function
, original_fun
);
2903 funcar
= XCAR (fun
);
2904 if (!SYMBOLP (funcar
))
2905 xsignal1 (Qinvalid_function
, original_fun
);
2906 if (EQ (funcar
, Qlambda
))
2907 val
= funcall_lambda (fun
, numargs
, args
+ 1);
2908 else if (EQ (funcar
, Qautoload
))
2910 do_autoload (fun
, original_fun
);
2915 xsignal1 (Qinvalid_function
, original_fun
);
2919 if (backtrace
.debug_on_exit
)
2920 val
= call_debugger (Fcons (Qexit
, Fcons (val
, Qnil
)));
2921 backtrace_list
= backtrace
.next
;
2926 apply_lambda (Lisp_Object fun
, Lisp_Object args
, int eval_flag
)
2928 Lisp_Object args_left
;
2930 register Lisp_Object
*arg_vector
;
2931 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2933 register Lisp_Object tem
;
2936 numargs
= XINT (Flength (args
));
2937 SAFE_ALLOCA_LISP (arg_vector
, numargs
);
2940 GCPRO3 (*arg_vector
, args_left
, fun
);
2943 for (i
= 0; i
< numargs
; )
2945 tem
= Fcar (args_left
), args_left
= Fcdr (args_left
);
2946 if (eval_flag
) tem
= Feval (tem
);
2947 arg_vector
[i
++] = tem
;
2955 backtrace_list
->args
= arg_vector
;
2956 backtrace_list
->nargs
= i
;
2958 backtrace_list
->evalargs
= 0;
2959 tem
= funcall_lambda (fun
, numargs
, arg_vector
);
2961 /* Do the debug-on-exit now, while arg_vector still exists. */
2962 if (backtrace_list
->debug_on_exit
)
2963 tem
= call_debugger (Fcons (Qexit
, Fcons (tem
, Qnil
)));
2964 /* Don't do it again when we return to eval. */
2965 backtrace_list
->debug_on_exit
= 0;
2970 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2971 and return the result of evaluation.
2972 FUN must be either a lambda-expression or a compiled-code object. */
2975 funcall_lambda (Lisp_Object fun
, size_t nargs
,
2976 register Lisp_Object
*arg_vector
)
2978 Lisp_Object val
, syms_left
, next
;
2979 int count
= SPECPDL_INDEX ();
2985 syms_left
= XCDR (fun
);
2986 if (CONSP (syms_left
))
2987 syms_left
= XCAR (syms_left
);
2989 xsignal1 (Qinvalid_function
, fun
);
2991 else if (COMPILEDP (fun
))
2992 syms_left
= AREF (fun
, COMPILED_ARGLIST
);
2996 i
= optional
= rest
= 0;
2997 for (; CONSP (syms_left
); syms_left
= XCDR (syms_left
))
3001 next
= XCAR (syms_left
);
3002 if (!SYMBOLP (next
))
3003 xsignal1 (Qinvalid_function
, fun
);
3005 if (EQ (next
, Qand_rest
))
3007 else if (EQ (next
, Qand_optional
))
3011 specbind (next
, Flist (nargs
- i
, &arg_vector
[i
]));
3015 specbind (next
, arg_vector
[i
++]);
3017 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3019 specbind (next
, Qnil
);
3022 if (!NILP (syms_left
))
3023 xsignal1 (Qinvalid_function
, fun
);
3025 xsignal2 (Qwrong_number_of_arguments
, fun
, make_number (nargs
));
3028 val
= Fprogn (XCDR (XCDR (fun
)));
3031 /* If we have not actually read the bytecode string
3032 and constants vector yet, fetch them from the file. */
3033 if (CONSP (AREF (fun
, COMPILED_BYTECODE
)))
3034 Ffetch_bytecode (fun
);
3035 val
= Fbyte_code (AREF (fun
, COMPILED_BYTECODE
),
3036 AREF (fun
, COMPILED_CONSTANTS
),
3037 AREF (fun
, COMPILED_STACK_DEPTH
));
3040 return unbind_to (count
, val
);
3043 DEFUN ("fetch-bytecode", Ffetch_bytecode
, Sfetch_bytecode
,
3045 doc
: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3046 (Lisp_Object object
)
3050 if (COMPILEDP (object
) && CONSP (AREF (object
, COMPILED_BYTECODE
)))
3052 tem
= read_doc_string (AREF (object
, COMPILED_BYTECODE
));
3055 tem
= AREF (object
, COMPILED_BYTECODE
);
3056 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
3057 error ("Invalid byte code in %s", SDATA (XCAR (tem
)));
3059 error ("Invalid byte code");
3061 ASET (object
, COMPILED_BYTECODE
, XCAR (tem
));
3062 ASET (object
, COMPILED_CONSTANTS
, XCDR (tem
));
3070 register int count
= SPECPDL_INDEX ();
3071 if (specpdl_size
>= max_specpdl_size
)
3073 if (max_specpdl_size
< 400)
3074 max_specpdl_size
= 400;
3075 if (specpdl_size
>= max_specpdl_size
)
3076 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil
);
3079 if (specpdl_size
> max_specpdl_size
)
3080 specpdl_size
= max_specpdl_size
;
3081 specpdl
= (struct specbinding
*) xrealloc (specpdl
, specpdl_size
* sizeof (struct specbinding
));
3082 specpdl_ptr
= specpdl
+ count
;
3085 /* specpdl_ptr->symbol is a field which describes which variable is
3086 let-bound, so it can be properly undone when we unbind_to.
3087 It can have the following two shapes:
3088 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3089 a symbol that is not buffer-local (at least at the time
3090 the let binding started). Note also that it should not be
3091 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3093 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3094 variable SYMBOL which can be buffer-local. WHERE tells us
3095 which buffer is affected (or nil if the let-binding affects the
3096 global value of the variable) and BUFFER tells us which buffer was
3097 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3098 BUFFER did not yet have a buffer-local value). */
3101 specbind (Lisp_Object symbol
, Lisp_Object value
)
3103 struct Lisp_Symbol
*sym
;
3105 eassert (!handling_signal
);
3107 CHECK_SYMBOL (symbol
);
3108 sym
= XSYMBOL (symbol
);
3109 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3113 switch (sym
->redirect
)
3115 case SYMBOL_VARALIAS
:
3116 sym
= indirect_variable (sym
); XSETSYMBOL (symbol
, sym
); goto start
;
3117 case SYMBOL_PLAINVAL
:
3118 /* The most common case is that of a non-constant symbol with a
3119 trivial value. Make that as fast as we can. */
3120 specpdl_ptr
->symbol
= symbol
;
3121 specpdl_ptr
->old_value
= SYMBOL_VAL (sym
);
3122 specpdl_ptr
->func
= NULL
;
3125 SET_SYMBOL_VAL (sym
, value
);
3127 set_internal (symbol
, value
, Qnil
, 1);
3129 case SYMBOL_LOCALIZED
:
3130 if (SYMBOL_BLV (sym
)->frame_local
)
3131 error ("Frame-local vars cannot be let-bound");
3132 case SYMBOL_FORWARDED
:
3134 Lisp_Object ovalue
= find_symbol_value (symbol
);
3135 specpdl_ptr
->func
= 0;
3136 specpdl_ptr
->old_value
= ovalue
;
3138 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3139 || (EQ (SYMBOL_BLV (sym
)->where
,
3140 SYMBOL_BLV (sym
)->frame_local
?
3141 Fselected_frame () : Fcurrent_buffer ())));
3143 if (sym
->redirect
== SYMBOL_LOCALIZED
3144 || BUFFER_OBJFWDP (SYMBOL_FWD (sym
)))
3146 Lisp_Object where
, cur_buf
= Fcurrent_buffer ();
3148 /* For a local variable, record both the symbol and which
3149 buffer's or frame's value we are saving. */
3150 if (!NILP (Flocal_variable_p (symbol
, Qnil
)))
3152 eassert (sym
->redirect
!= SYMBOL_LOCALIZED
3153 || (BLV_FOUND (SYMBOL_BLV (sym
))
3154 && EQ (cur_buf
, SYMBOL_BLV (sym
)->where
)));
3157 else if (sym
->redirect
== SYMBOL_LOCALIZED
3158 && BLV_FOUND (SYMBOL_BLV (sym
)))
3159 where
= SYMBOL_BLV (sym
)->where
;
3163 /* We're not using the `unused' slot in the specbinding
3164 structure because this would mean we have to do more
3165 work for simple variables. */
3166 /* FIXME: The third value `current_buffer' is only used in
3167 let_shadows_buffer_binding_p which is itself only used
3168 in set_internal for local_if_set. */
3169 eassert (NILP (where
) || EQ (where
, cur_buf
));
3170 specpdl_ptr
->symbol
= Fcons (symbol
, Fcons (where
, cur_buf
));
3172 /* If SYMBOL is a per-buffer variable which doesn't have a
3173 buffer-local value here, make the `let' change the global
3174 value by changing the value of SYMBOL in all buffers not
3175 having their own value. This is consistent with what
3176 happens with other buffer-local variables. */
3178 && sym
->redirect
== SYMBOL_FORWARDED
)
3180 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym
)));
3182 Fset_default (symbol
, value
);
3187 specpdl_ptr
->symbol
= symbol
;
3190 set_internal (symbol
, value
, Qnil
, 1);
3198 record_unwind_protect (Lisp_Object (*function
) (Lisp_Object
), Lisp_Object arg
)
3200 eassert (!handling_signal
);
3202 if (specpdl_ptr
== specpdl
+ specpdl_size
)
3204 specpdl_ptr
->func
= function
;
3205 specpdl_ptr
->symbol
= Qnil
;
3206 specpdl_ptr
->old_value
= arg
;
3211 unbind_to (int count
, Lisp_Object value
)
3213 Lisp_Object quitf
= Vquit_flag
;
3214 struct gcpro gcpro1
, gcpro2
;
3216 GCPRO2 (value
, quitf
);
3219 while (specpdl_ptr
!= specpdl
+ count
)
3221 /* Copy the binding, and decrement specpdl_ptr, before we do
3222 the work to unbind it. We decrement first
3223 so that an error in unbinding won't try to unbind
3224 the same entry again, and we copy the binding first
3225 in case more bindings are made during some of the code we run. */
3227 struct specbinding this_binding
;
3228 this_binding
= *--specpdl_ptr
;
3230 if (this_binding
.func
!= 0)
3231 (*this_binding
.func
) (this_binding
.old_value
);
3232 /* If the symbol is a list, it is really (SYMBOL WHERE
3233 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3234 frame. If WHERE is a buffer or frame, this indicates we
3235 bound a variable that had a buffer-local or frame-local
3236 binding. WHERE nil means that the variable had the default
3237 value when it was bound. CURRENT-BUFFER is the buffer that
3238 was current when the variable was bound. */
3239 else if (CONSP (this_binding
.symbol
))
3241 Lisp_Object symbol
, where
;
3243 symbol
= XCAR (this_binding
.symbol
);
3244 where
= XCAR (XCDR (this_binding
.symbol
));
3247 Fset_default (symbol
, this_binding
.old_value
);
3248 /* If `where' is non-nil, reset the value in the appropriate
3249 local binding, but only if that binding still exists. */
3250 else if (BUFFERP (where
)
3251 ? !NILP (Flocal_variable_p (symbol
, where
))
3252 : !NILP (Fassq (symbol
, XFRAME (where
)->param_alist
)))
3253 set_internal (symbol
, this_binding
.old_value
, where
, 1);
3255 /* If variable has a trivial value (no forwarding), we can
3256 just set it. No need to check for constant symbols here,
3257 since that was already done by specbind. */
3258 else if (XSYMBOL (this_binding
.symbol
)->redirect
== SYMBOL_PLAINVAL
)
3259 SET_SYMBOL_VAL (XSYMBOL (this_binding
.symbol
),
3260 this_binding
.old_value
);
3262 /* NOTE: we only ever come here if make_local_foo was used for
3263 the first time on this var within this let. */
3264 Fset_default (this_binding
.symbol
, this_binding
.old_value
);
3267 if (NILP (Vquit_flag
) && !NILP (quitf
))
3274 DEFUN ("backtrace-debug", Fbacktrace_debug
, Sbacktrace_debug
, 2, 2, 0,
3275 doc
: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3276 The debugger is entered when that frame exits, if the flag is non-nil. */)
3277 (Lisp_Object level
, Lisp_Object flag
)
3279 register struct backtrace
*backlist
= backtrace_list
;
3282 CHECK_NUMBER (level
);
3284 for (i
= 0; backlist
&& i
< XINT (level
); i
++)
3286 backlist
= backlist
->next
;
3290 backlist
->debug_on_exit
= !NILP (flag
);
3295 DEFUN ("backtrace", Fbacktrace
, Sbacktrace
, 0, 0, "",
3296 doc
: /* Print a trace of Lisp function calls currently active.
3297 Output stream used is value of `standard-output'. */)
3300 register struct backtrace
*backlist
= backtrace_list
;
3304 struct gcpro gcpro1
;
3305 Lisp_Object old_print_level
= Vprint_level
;
3307 if (NILP (Vprint_level
))
3308 XSETFASTINT (Vprint_level
, 8);
3315 write_string (backlist
->debug_on_exit
? "* " : " ", 2);
3316 if (backlist
->nargs
== (size_t) UNEVALLED
)
3318 Fprin1 (Fcons (*backlist
->function
, *backlist
->args
), Qnil
);
3319 write_string ("\n", -1);
3323 tem
= *backlist
->function
;
3324 Fprin1 (tem
, Qnil
); /* This can QUIT */
3325 write_string ("(", -1);
3326 if (backlist
->nargs
== (size_t) MANY
)
3328 for (tail
= *backlist
->args
, i
= 0;
3330 tail
= Fcdr (tail
), i
++)
3332 if (i
) write_string (" ", -1);
3333 Fprin1 (Fcar (tail
), Qnil
);
3338 for (i
= 0; i
< backlist
->nargs
; i
++)
3340 if (i
) write_string (" ", -1);
3341 Fprin1 (backlist
->args
[i
], Qnil
);
3344 write_string (")\n", -1);
3346 backlist
= backlist
->next
;
3349 Vprint_level
= old_print_level
;
3354 DEFUN ("backtrace-frame", Fbacktrace_frame
, Sbacktrace_frame
, 1, 1, NULL
,
3355 doc
: /* Return the function and arguments NFRAMES up from current execution point.
3356 If that frame has not evaluated the arguments yet (or is a special form),
3357 the value is (nil FUNCTION ARG-FORMS...).
3358 If that frame has evaluated its arguments and called its function already,
3359 the value is (t FUNCTION ARG-VALUES...).
3360 A &rest arg is represented as the tail of the list ARG-VALUES.
3361 FUNCTION is whatever was supplied as car of evaluated list,
3362 or a lambda expression for macro calls.
3363 If NFRAMES is more than the number of frames, the value is nil. */)
3364 (Lisp_Object nframes
)
3366 register struct backtrace
*backlist
= backtrace_list
;
3370 CHECK_NATNUM (nframes
);
3372 /* Find the frame requested. */
3373 for (i
= 0; backlist
&& i
< XFASTINT (nframes
); i
++)
3374 backlist
= backlist
->next
;
3378 if (backlist
->nargs
== (size_t) UNEVALLED
)
3379 return Fcons (Qnil
, Fcons (*backlist
->function
, *backlist
->args
));
3382 if (backlist
->nargs
== (size_t) MANY
)
3383 tem
= *backlist
->args
;
3385 tem
= Flist (backlist
->nargs
, backlist
->args
);
3387 return Fcons (Qt
, Fcons (*backlist
->function
, tem
));
3393 mark_backtrace (void)
3395 register struct backtrace
*backlist
;
3398 for (backlist
= backtrace_list
; backlist
; backlist
= backlist
->next
)
3400 mark_object (*backlist
->function
);
3402 if (backlist
->nargs
== (size_t) UNEVALLED
3403 || backlist
->nargs
== (size_t) MANY
)
3406 i
= backlist
->nargs
;
3408 mark_object (backlist
->args
[i
]);
3415 DEFVAR_INT ("max-specpdl-size", max_specpdl_size
,
3416 doc
: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3417 If Lisp code tries to increase the total number past this amount,
3418 an error is signaled.
3419 You can safely use a value considerably larger than the default value,
3420 if that proves inconveniently small. However, if you increase it too far,
3421 Emacs could run out of memory trying to make the stack bigger. */);
3423 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth
,
3424 doc
: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3426 This limit serves to catch infinite recursions for you before they cause
3427 actual stack overflow in C, which would be fatal for Emacs.
3428 You can safely make it considerably larger than its default value,
3429 if that proves inconveniently small. However, if you increase it too far,
3430 Emacs could overflow the real C stack, and crash. */);
3432 DEFVAR_LISP ("quit-flag", Vquit_flag
,
3433 doc
: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3434 If the value is t, that means do an ordinary quit.
3435 If the value equals `throw-on-input', that means quit by throwing
3436 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3437 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3438 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3441 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit
,
3442 doc
: /* Non-nil inhibits C-g quitting from happening immediately.
3443 Note that `quit-flag' will still be set by typing C-g,
3444 so a quit will be signaled as soon as `inhibit-quit' is nil.
3445 To prevent this happening, set `quit-flag' to nil
3446 before making `inhibit-quit' nil. */);
3447 Vinhibit_quit
= Qnil
;
3449 Qinhibit_quit
= intern_c_string ("inhibit-quit");
3450 staticpro (&Qinhibit_quit
);
3452 Qautoload
= intern_c_string ("autoload");
3453 staticpro (&Qautoload
);
3455 Qdebug_on_error
= intern_c_string ("debug-on-error");
3456 staticpro (&Qdebug_on_error
);
3458 Qmacro
= intern_c_string ("macro");
3459 staticpro (&Qmacro
);
3461 Qdeclare
= intern_c_string ("declare");
3462 staticpro (&Qdeclare
);
3464 /* Note that the process handling also uses Qexit, but we don't want
3465 to staticpro it twice, so we just do it here. */
3466 Qexit
= intern_c_string ("exit");
3469 Qinteractive
= intern_c_string ("interactive");
3470 staticpro (&Qinteractive
);
3472 Qcommandp
= intern_c_string ("commandp");
3473 staticpro (&Qcommandp
);
3475 Qdefun
= intern_c_string ("defun");
3476 staticpro (&Qdefun
);
3478 Qand_rest
= intern_c_string ("&rest");
3479 staticpro (&Qand_rest
);
3481 Qand_optional
= intern_c_string ("&optional");
3482 staticpro (&Qand_optional
);
3484 Qdebug
= intern_c_string ("debug");
3485 staticpro (&Qdebug
);
3487 DEFVAR_LISP ("debug-on-error", Vdebug_on_error
,
3488 doc
: /* *Non-nil means enter debugger if an error is signaled.
3489 Does not apply to errors handled by `condition-case' or those
3490 matched by `debug-ignored-errors'.
3491 If the value is a list, an error only means to enter the debugger
3492 if one of its condition symbols appears in the list.
3493 When you evaluate an expression interactively, this variable
3494 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3495 The command `toggle-debug-on-error' toggles this.
3496 See also the variable `debug-on-quit'. */);
3497 Vdebug_on_error
= Qnil
;
3499 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors
,
3500 doc
: /* *List of errors for which the debugger should not be called.
3501 Each element may be a condition-name or a regexp that matches error messages.
3502 If any element applies to a given error, that error skips the debugger
3503 and just returns to top level.
3504 This overrides the variable `debug-on-error'.
3505 It does not apply to errors handled by `condition-case'. */);
3506 Vdebug_ignored_errors
= Qnil
;
3508 DEFVAR_BOOL ("debug-on-quit", debug_on_quit
,
3509 doc
: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3510 Does not apply if quit is handled by a `condition-case'. */);
3513 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call
,
3514 doc
: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3516 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue
,
3517 doc
: /* Non-nil means debugger may continue execution.
3518 This is nil when the debugger is called under circumstances where it
3519 might not be safe to continue. */);
3520 debugger_may_continue
= 1;
3522 DEFVAR_LISP ("debugger", Vdebugger
,
3523 doc
: /* Function to call to invoke debugger.
3524 If due to frame exit, args are `exit' and the value being returned;
3525 this function's value will be returned instead of that.
3526 If due to error, args are `error' and a list of the args to `signal'.
3527 If due to `apply' or `funcall' entry, one arg, `lambda'.
3528 If due to `eval' entry, one arg, t. */);
3531 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function
,
3532 doc
: /* If non-nil, this is a function for `signal' to call.
3533 It receives the same arguments that `signal' was given.
3534 The Edebug package uses this to regain control. */);
3535 Vsignal_hook_function
= Qnil
;
3537 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal
,
3538 doc
: /* *Non-nil means call the debugger regardless of condition handlers.
3539 Note that `debug-on-error', `debug-on-quit' and friends
3540 still determine whether to handle the particular condition. */);
3541 Vdebug_on_signal
= Qnil
;
3543 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function
,
3544 doc
: /* Function to process declarations in a macro definition.
3545 The function will be called with two args MACRO and DECL.
3546 MACRO is the name of the macro being defined.
3547 DECL is a list `(declare ...)' containing the declarations.
3548 The value the function returns is not used. */);
3549 Vmacro_declaration_function
= Qnil
;
3551 Vrun_hooks
= intern_c_string ("run-hooks");
3552 staticpro (&Vrun_hooks
);
3554 staticpro (&Vautoload_queue
);
3555 Vautoload_queue
= Qnil
;
3556 staticpro (&Vsignaling_function
);
3557 Vsignaling_function
= Qnil
;
3568 defsubr (&Sfunction
);
3570 defsubr (&Sdefmacro
);
3572 defsubr (&Sdefvaralias
);
3573 defsubr (&Sdefconst
);
3574 defsubr (&Suser_variable_p
);
3578 defsubr (&Smacroexpand
);
3581 defsubr (&Sunwind_protect
);
3582 defsubr (&Scondition_case
);
3584 defsubr (&Sinteractive_p
);
3585 defsubr (&Scalled_interactively_p
);
3586 defsubr (&Scommandp
);
3587 defsubr (&Sautoload
);
3590 defsubr (&Sfuncall
);
3591 defsubr (&Srun_hooks
);
3592 defsubr (&Srun_hook_with_args
);
3593 defsubr (&Srun_hook_with_args_until_success
);
3594 defsubr (&Srun_hook_with_args_until_failure
);
3595 defsubr (&Sfetch_bytecode
);
3596 defsubr (&Sbacktrace_debug
);
3597 defsubr (&Sbacktrace
);
3598 defsubr (&Sbacktrace_frame
);