Fix ChangeLog typo.
[emacs.git] / src / eval.c
blobe6ccf0bdcb50bb591cbdda7e1b018d4b806753ba
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 #if !BYTE_MARK_STACK
36 static
37 #endif
38 struct catchtag *catchlist;
40 /* Chain of condition handlers currently in effect.
41 The elements of this chain are contained in the stack frames
42 of Fcondition_case and internal_condition_case.
43 When an error is signaled (by calling Fsignal, below),
44 this chain is searched for an element that applies. */
46 #if !BYTE_MARK_STACK
47 static
48 #endif
49 struct handler *handlerlist;
51 #ifdef DEBUG_GCPRO
52 /* Count levels of GCPRO to detect failure to UNGCPRO. */
53 int gcpro_level;
54 #endif
56 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
57 Lisp_Object Qinhibit_quit;
58 Lisp_Object Qand_rest;
59 static Lisp_Object Qand_optional;
60 static Lisp_Object Qinhibit_debugger;
61 static Lisp_Object Qdeclare;
62 Lisp_Object Qinternal_interpreter_environment, Qclosure;
64 static Lisp_Object Qdebug;
66 /* This holds either the symbol `run-hooks' or nil.
67 It is nil at an early stage of startup, and when Emacs
68 is shutting down. */
70 Lisp_Object Vrun_hooks;
72 /* Non-nil means record all fset's and provide's, to be undone
73 if the file being autoloaded is not fully loaded.
74 They are recorded by being consed onto the front of Vautoload_queue:
75 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
77 Lisp_Object Vautoload_queue;
79 /* Current number of specbindings allocated in specpdl, not counting
80 the dummy entry specpdl[-1]. */
82 ptrdiff_t specpdl_size;
84 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
87 union specbinding *specpdl;
89 /* Pointer to first unused element in specpdl. */
91 union specbinding *specpdl_ptr;
93 /* Depth in Lisp evaluations and function calls. */
95 static EMACS_INT lisp_eval_depth;
97 /* The value of num_nonmacro_input_events as of the last time we
98 started to enter the debugger. If we decide to enter the debugger
99 again when this is still equal to num_nonmacro_input_events, then we
100 know that the debugger itself has an error, and we should just
101 signal the error instead of entering an infinite loop of debugger
102 invocations. */
104 static EMACS_INT when_entered_debugger;
106 /* The function from which the last `signal' was called. Set in
107 Fsignal. */
108 /* FIXME: We should probably get rid of this! */
109 Lisp_Object Vsignaling_function;
111 /* If non-nil, Lisp code must not be run since some part of Emacs is
112 in an inconsistent state. Currently, x-create-frame uses this to
113 avoid triggering window-configuration-change-hook while the new
114 frame is half-initialized. */
115 Lisp_Object inhibit_lisp_code;
117 /* These would ordinarily be static, but they need to be visible to GDB. */
118 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
119 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
120 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
121 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
122 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
124 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
125 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
127 static Lisp_Object
128 specpdl_symbol (union specbinding *pdl)
130 eassert (pdl->kind >= SPECPDL_LET);
131 return pdl->let.symbol;
134 static Lisp_Object
135 specpdl_old_value (union specbinding *pdl)
137 eassert (pdl->kind >= SPECPDL_LET);
138 return pdl->let.old_value;
141 static Lisp_Object
142 specpdl_where (union specbinding *pdl)
144 eassert (pdl->kind > SPECPDL_LET);
145 return pdl->let.where;
148 static Lisp_Object
149 specpdl_arg (union specbinding *pdl)
151 eassert (pdl->kind == SPECPDL_UNWIND);
152 return pdl->unwind.arg;
155 Lisp_Object
156 backtrace_function (union specbinding *pdl)
158 eassert (pdl->kind == SPECPDL_BACKTRACE);
159 return pdl->bt.function;
162 static ptrdiff_t
163 backtrace_nargs (union specbinding *pdl)
165 eassert (pdl->kind == SPECPDL_BACKTRACE);
166 return pdl->bt.nargs;
169 Lisp_Object *
170 backtrace_args (union specbinding *pdl)
172 eassert (pdl->kind == SPECPDL_BACKTRACE);
173 return pdl->bt.args;
176 static bool
177 backtrace_debug_on_exit (union specbinding *pdl)
179 eassert (pdl->kind == SPECPDL_BACKTRACE);
180 return pdl->bt.debug_on_exit;
183 /* Functions to modify slots of backtrace records. */
185 static void
186 set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
188 eassert (pdl->kind == SPECPDL_BACKTRACE);
189 pdl->bt.args = args;
192 static void
193 set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
195 eassert (pdl->kind == SPECPDL_BACKTRACE);
196 pdl->bt.nargs = n;
199 static void
200 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
202 eassert (pdl->kind == SPECPDL_BACKTRACE);
203 pdl->bt.debug_on_exit = doe;
206 /* Helper functions to scan the backtrace. */
208 bool
209 backtrace_p (union specbinding *pdl)
210 { return pdl >= specpdl; }
212 union specbinding *
213 backtrace_top (void)
215 union specbinding *pdl = specpdl_ptr - 1;
216 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
217 pdl--;
218 return pdl;
221 union specbinding *
222 backtrace_next (union specbinding *pdl)
224 pdl--;
225 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
226 pdl--;
227 return pdl;
231 void
232 init_eval_once (void)
234 enum { size = 50 };
235 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
236 specpdl_size = size;
237 specpdl = specpdl_ptr = pdlvec + 1;
238 /* Don't forget to update docs (lispref node "Local Variables"). */
239 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
240 max_lisp_eval_depth = 600;
242 Vrun_hooks = Qnil;
245 void
246 init_eval (void)
248 specpdl_ptr = specpdl;
249 catchlist = 0;
250 handlerlist = 0;
251 Vquit_flag = Qnil;
252 debug_on_next_call = 0;
253 lisp_eval_depth = 0;
254 #ifdef DEBUG_GCPRO
255 gcpro_level = 0;
256 #endif
257 /* This is less than the initial value of num_nonmacro_input_events. */
258 when_entered_debugger = -1;
261 /* Unwind-protect function used by call_debugger. */
263 static void
264 restore_stack_limits (Lisp_Object data)
266 max_specpdl_size = XINT (XCAR (data));
267 max_lisp_eval_depth = XINT (XCDR (data));
270 /* Call the Lisp debugger, giving it argument ARG. */
272 Lisp_Object
273 call_debugger (Lisp_Object arg)
275 bool debug_while_redisplaying;
276 ptrdiff_t count = SPECPDL_INDEX ();
277 Lisp_Object val;
278 EMACS_INT old_max = max_specpdl_size;
280 /* Temporarily bump up the stack limits,
281 so the debugger won't run out of stack. */
283 max_specpdl_size += 1;
284 record_unwind_protect (restore_stack_limits,
285 Fcons (make_number (old_max),
286 make_number (max_lisp_eval_depth)));
287 max_specpdl_size = old_max;
289 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
290 max_lisp_eval_depth = lisp_eval_depth + 40;
292 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
293 max_specpdl_size = SPECPDL_INDEX () + 100;
295 #ifdef HAVE_WINDOW_SYSTEM
296 if (display_hourglass_p)
297 cancel_hourglass ();
298 #endif
300 debug_on_next_call = 0;
301 when_entered_debugger = num_nonmacro_input_events;
303 /* Resetting redisplaying_p to 0 makes sure that debug output is
304 displayed if the debugger is invoked during redisplay. */
305 debug_while_redisplaying = redisplaying_p;
306 redisplaying_p = 0;
307 specbind (intern ("debugger-may-continue"),
308 debug_while_redisplaying ? Qnil : Qt);
309 specbind (Qinhibit_redisplay, Qnil);
310 specbind (Qinhibit_debugger, Qt);
312 #if 0 /* Binding this prevents execution of Lisp code during
313 redisplay, which necessarily leads to display problems. */
314 specbind (Qinhibit_eval_during_redisplay, Qt);
315 #endif
317 val = apply1 (Vdebugger, arg);
319 /* Interrupting redisplay and resuming it later is not safe under
320 all circumstances. So, when the debugger returns, abort the
321 interrupted redisplay by going back to the top-level. */
322 if (debug_while_redisplaying)
323 Ftop_level ();
325 return unbind_to (count, val);
328 static void
329 do_debug_on_call (Lisp_Object code)
331 debug_on_next_call = 0;
332 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
333 call_debugger (list1 (code));
336 /* NOTE!!! Every function that can call EVAL must protect its args
337 and temporaries from garbage collection while it needs them.
338 The definition of `For' shows what you have to do. */
340 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
341 doc: /* Eval args until one of them yields non-nil, then return that value.
342 The remaining args are not evalled at all.
343 If all args return nil, return nil.
344 usage: (or CONDITIONS...) */)
345 (Lisp_Object args)
347 register Lisp_Object val = Qnil;
348 struct gcpro gcpro1;
350 GCPRO1 (args);
352 while (CONSP (args))
354 val = eval_sub (XCAR (args));
355 if (!NILP (val))
356 break;
357 args = XCDR (args);
360 UNGCPRO;
361 return val;
364 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
365 doc: /* Eval args until one of them yields nil, then return nil.
366 The remaining args are not evalled at all.
367 If no arg yields nil, return the last arg's value.
368 usage: (and CONDITIONS...) */)
369 (Lisp_Object args)
371 register Lisp_Object val = Qt;
372 struct gcpro gcpro1;
374 GCPRO1 (args);
376 while (CONSP (args))
378 val = eval_sub (XCAR (args));
379 if (NILP (val))
380 break;
381 args = XCDR (args);
384 UNGCPRO;
385 return val;
388 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
389 doc: /* If COND yields non-nil, do THEN, else do ELSE...
390 Returns the value of THEN or the value of the last of the ELSE's.
391 THEN must be one expression, but ELSE... can be zero or more expressions.
392 If COND yields nil, and there are no ELSE's, the value is nil.
393 usage: (if COND THEN ELSE...) */)
394 (Lisp_Object args)
396 Lisp_Object cond;
397 struct gcpro gcpro1;
399 GCPRO1 (args);
400 cond = eval_sub (XCAR (args));
401 UNGCPRO;
403 if (!NILP (cond))
404 return eval_sub (Fcar (XCDR (args)));
405 return Fprogn (XCDR (XCDR (args)));
408 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
409 doc: /* Try each clause until one succeeds.
410 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
411 and, if the value is non-nil, this clause succeeds:
412 then the expressions in BODY are evaluated and the last one's
413 value is the value of the cond-form.
414 If no clause succeeds, cond returns nil.
415 If a clause has one element, as in (CONDITION),
416 CONDITION's value if non-nil is returned from the cond-form.
417 usage: (cond CLAUSES...) */)
418 (Lisp_Object args)
420 Lisp_Object val = args;
421 struct gcpro gcpro1;
423 GCPRO1 (args);
424 while (CONSP (args))
426 Lisp_Object clause = XCAR (args);
427 val = eval_sub (Fcar (clause));
428 if (!NILP (val))
430 if (!NILP (XCDR (clause)))
431 val = Fprogn (XCDR (clause));
432 break;
434 args = XCDR (args);
436 UNGCPRO;
438 return val;
441 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
442 doc: /* Eval BODY forms sequentially and return value of last one.
443 usage: (progn BODY...) */)
444 (Lisp_Object body)
446 Lisp_Object val = Qnil;
447 struct gcpro gcpro1;
449 GCPRO1 (body);
451 while (CONSP (body))
453 val = eval_sub (XCAR (body));
454 body = XCDR (body);
457 if (!NILP (body))
459 /* This can happen if functions like Fcond are the caller. */
460 wrong_type_argument (Qlistp, body);
463 UNGCPRO;
464 return val;
467 /* Evaluate BODY sequentially, discarding its value. Suitable for
468 record_unwind_protect. */
470 void
471 unwind_body (Lisp_Object body)
473 Fprogn (body);
476 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
477 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
478 The value of FIRST is saved during the evaluation of the remaining args,
479 whose values are discarded.
480 usage: (prog1 FIRST BODY...) */)
481 (Lisp_Object args)
483 Lisp_Object val;
484 Lisp_Object args_left;
485 struct gcpro gcpro1, gcpro2;
487 args_left = args;
488 val = args;
489 GCPRO2 (args, val);
491 val = eval_sub (XCAR (args_left));
492 while (CONSP (args_left = XCDR (args_left)))
493 eval_sub (XCAR (args_left));
495 UNGCPRO;
496 return val;
499 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
500 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
501 The value of FORM2 is saved during the evaluation of the
502 remaining args, whose values are discarded.
503 usage: (prog2 FORM1 FORM2 BODY...) */)
504 (Lisp_Object args)
506 struct gcpro gcpro1;
508 GCPRO1 (args);
509 eval_sub (XCAR (args));
510 UNGCPRO;
511 return Fprog1 (XCDR (args));
514 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
515 doc: /* Set each SYM to the value of its VAL.
516 The symbols SYM are variables; they are literal (not evaluated).
517 The values VAL are expressions; they are evaluated.
518 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
519 The second VAL is not computed until after the first SYM is set, and so on;
520 each VAL can use the new value of variables set earlier in the `setq'.
521 The return value of the `setq' form is the value of the last VAL.
522 usage: (setq [SYM VAL]...) */)
523 (Lisp_Object args)
525 Lisp_Object val, sym, lex_binding;
527 val = args;
528 if (CONSP (args))
530 Lisp_Object args_left = args;
531 struct gcpro gcpro1;
532 GCPRO1 (args);
536 val = eval_sub (Fcar (XCDR (args_left)));
537 sym = XCAR (args_left);
539 /* Like for eval_sub, we do not check declared_special here since
540 it's been done when let-binding. */
541 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
542 && SYMBOLP (sym)
543 && !NILP (lex_binding
544 = Fassq (sym, Vinternal_interpreter_environment)))
545 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
546 else
547 Fset (sym, val); /* SYM is dynamically bound. */
549 args_left = Fcdr (XCDR (args_left));
551 while (CONSP (args_left));
553 UNGCPRO;
556 return val;
559 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
560 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
561 Warning: `quote' does not construct its return value, but just returns
562 the value that was pre-constructed by the Lisp reader (see info node
563 `(elisp)Printed Representation').
564 This means that '(a . b) is not identical to (cons 'a 'b): the former
565 does not cons. Quoting should be reserved for constants that will
566 never be modified by side-effects, unless you like self-modifying code.
567 See the common pitfall in info node `(elisp)Rearrangement' for an example
568 of unexpected results when a quoted object is modified.
569 usage: (quote ARG) */)
570 (Lisp_Object args)
572 if (CONSP (XCDR (args)))
573 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
574 return XCAR (args);
577 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
578 doc: /* Like `quote', but preferred for objects which are functions.
579 In byte compilation, `function' causes its argument to be compiled.
580 `quote' cannot do that.
581 usage: (function ARG) */)
582 (Lisp_Object args)
584 Lisp_Object quoted = XCAR (args);
586 if (CONSP (XCDR (args)))
587 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
589 if (!NILP (Vinternal_interpreter_environment)
590 && CONSP (quoted)
591 && EQ (XCAR (quoted), Qlambda))
592 /* This is a lambda expression within a lexical environment;
593 return an interpreted closure instead of a simple lambda. */
594 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
595 XCDR (quoted)));
596 else
597 /* Simply quote the argument. */
598 return quoted;
602 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
603 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
604 Aliased variables always have the same value; setting one sets the other.
605 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
606 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
607 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
608 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
609 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
610 The return value is BASE-VARIABLE. */)
611 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
613 struct Lisp_Symbol *sym;
615 CHECK_SYMBOL (new_alias);
616 CHECK_SYMBOL (base_variable);
618 sym = XSYMBOL (new_alias);
620 if (sym->constant)
621 /* Not sure why, but why not? */
622 error ("Cannot make a constant an alias");
624 switch (sym->redirect)
626 case SYMBOL_FORWARDED:
627 error ("Cannot make an internal variable an alias");
628 case SYMBOL_LOCALIZED:
629 error ("Don't know how to make a localized variable an alias");
632 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
633 If n_a is bound, but b_v is not, set the value of b_v to n_a,
634 so that old-code that affects n_a before the aliasing is setup
635 still works. */
636 if (NILP (Fboundp (base_variable)))
637 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
640 union specbinding *p;
642 for (p = specpdl_ptr; p > specpdl; )
643 if ((--p)->kind >= SPECPDL_LET
644 && (EQ (new_alias, specpdl_symbol (p))))
645 error ("Don't know how to make a let-bound variable an alias");
648 sym->declared_special = 1;
649 XSYMBOL (base_variable)->declared_special = 1;
650 sym->redirect = SYMBOL_VARALIAS;
651 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
652 sym->constant = SYMBOL_CONSTANT_P (base_variable);
653 LOADHIST_ATTACH (new_alias);
654 /* Even if docstring is nil: remove old docstring. */
655 Fput (new_alias, Qvariable_documentation, docstring);
657 return base_variable;
661 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
662 doc: /* Define SYMBOL as a variable, and return SYMBOL.
663 You are not required to define a variable in order to use it, but
664 defining it lets you supply an initial value and documentation, which
665 can be referred to by the Emacs help facilities and other programming
666 tools. The `defvar' form also declares the variable as \"special\",
667 so that it is always dynamically bound even if `lexical-binding' is t.
669 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
670 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
671 default value is what is set; buffer-local values are not affected.
672 If INITVALUE is missing, SYMBOL's value is not set.
674 If SYMBOL has a local binding, then this form affects the local
675 binding. This is usually not what you want. Thus, if you need to
676 load a file defining variables, with this form or with `defconst' or
677 `defcustom', you should always load that file _outside_ any bindings
678 for these variables. \(`defconst' and `defcustom' behave similarly in
679 this respect.)
681 The optional argument DOCSTRING is a documentation string for the
682 variable.
684 To define a user option, use `defcustom' instead of `defvar'.
685 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
686 (Lisp_Object args)
688 Lisp_Object sym, tem, tail;
690 sym = XCAR (args);
691 tail = XCDR (args);
693 if (CONSP (tail))
695 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
696 error ("Too many arguments");
698 tem = Fdefault_boundp (sym);
700 /* Do it before evaluating the initial value, for self-references. */
701 XSYMBOL (sym)->declared_special = 1;
703 if (NILP (tem))
704 Fset_default (sym, eval_sub (XCAR (tail)));
705 else
706 { /* Check if there is really a global binding rather than just a let
707 binding that shadows the global unboundness of the var. */
708 union specbinding *pdl = specpdl_ptr;
709 while (pdl > specpdl)
711 if ((--pdl)->kind >= SPECPDL_LET
712 && EQ (specpdl_symbol (pdl), sym)
713 && EQ (specpdl_old_value (pdl), Qunbound))
715 message_with_string
716 ("Warning: defvar ignored because %s is let-bound",
717 SYMBOL_NAME (sym), 1);
718 break;
722 tail = XCDR (tail);
723 tem = Fcar (tail);
724 if (!NILP (tem))
726 if (!NILP (Vpurify_flag))
727 tem = Fpurecopy (tem);
728 Fput (sym, Qvariable_documentation, tem);
730 LOADHIST_ATTACH (sym);
732 else if (!NILP (Vinternal_interpreter_environment)
733 && !XSYMBOL (sym)->declared_special)
734 /* A simple (defvar foo) with lexical scoping does "nothing" except
735 declare that var to be dynamically scoped *locally* (i.e. within
736 the current file or let-block). */
737 Vinternal_interpreter_environment
738 = Fcons (sym, Vinternal_interpreter_environment);
739 else
741 /* Simple (defvar <var>) should not count as a definition at all.
742 It could get in the way of other definitions, and unloading this
743 package could try to make the variable unbound. */
746 return sym;
749 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
750 doc: /* Define SYMBOL as a constant variable.
751 This declares that neither programs nor users should ever change the
752 value. This constancy is not actually enforced by Emacs Lisp, but
753 SYMBOL is marked as a special variable so that it is never lexically
754 bound.
756 The `defconst' form always sets the value of SYMBOL to the result of
757 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
758 what is set; buffer-local values are not affected. If SYMBOL has a
759 local binding, then this form sets the local binding's value.
760 However, you should normally not make local bindings for variables
761 defined with this form.
763 The optional DOCSTRING specifies the variable's documentation string.
764 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
765 (Lisp_Object args)
767 Lisp_Object sym, tem;
769 sym = XCAR (args);
770 if (CONSP (Fcdr (XCDR (XCDR (args)))))
771 error ("Too many arguments");
773 tem = eval_sub (Fcar (XCDR (args)));
774 if (!NILP (Vpurify_flag))
775 tem = Fpurecopy (tem);
776 Fset_default (sym, tem);
777 XSYMBOL (sym)->declared_special = 1;
778 tem = Fcar (XCDR (XCDR (args)));
779 if (!NILP (tem))
781 if (!NILP (Vpurify_flag))
782 tem = Fpurecopy (tem);
783 Fput (sym, Qvariable_documentation, tem);
785 Fput (sym, Qrisky_local_variable, Qt);
786 LOADHIST_ATTACH (sym);
787 return sym;
790 /* Make SYMBOL lexically scoped. */
791 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
792 Smake_var_non_special, 1, 1, 0,
793 doc: /* Internal function. */)
794 (Lisp_Object symbol)
796 CHECK_SYMBOL (symbol);
797 XSYMBOL (symbol)->declared_special = 0;
798 return Qnil;
802 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
803 doc: /* Bind variables according to VARLIST then eval BODY.
804 The value of the last form in BODY is returned.
805 Each element of VARLIST is a symbol (which is bound to nil)
806 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
807 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
808 usage: (let* VARLIST BODY...) */)
809 (Lisp_Object args)
811 Lisp_Object varlist, var, val, elt, lexenv;
812 ptrdiff_t count = SPECPDL_INDEX ();
813 struct gcpro gcpro1, gcpro2, gcpro3;
815 GCPRO3 (args, elt, varlist);
817 lexenv = Vinternal_interpreter_environment;
819 varlist = XCAR (args);
820 while (CONSP (varlist))
822 QUIT;
824 elt = XCAR (varlist);
825 if (SYMBOLP (elt))
827 var = elt;
828 val = Qnil;
830 else if (! NILP (Fcdr (Fcdr (elt))))
831 signal_error ("`let' bindings can have only one value-form", elt);
832 else
834 var = Fcar (elt);
835 val = eval_sub (Fcar (Fcdr (elt)));
838 if (!NILP (lexenv) && SYMBOLP (var)
839 && !XSYMBOL (var)->declared_special
840 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
841 /* Lexically bind VAR by adding it to the interpreter's binding
842 alist. */
844 Lisp_Object newenv
845 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
846 if (EQ (Vinternal_interpreter_environment, lexenv))
847 /* Save the old lexical environment on the specpdl stack,
848 but only for the first lexical binding, since we'll never
849 need to revert to one of the intermediate ones. */
850 specbind (Qinternal_interpreter_environment, newenv);
851 else
852 Vinternal_interpreter_environment = newenv;
854 else
855 specbind (var, val);
857 varlist = XCDR (varlist);
859 UNGCPRO;
860 val = Fprogn (XCDR (args));
861 return unbind_to (count, val);
864 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
865 doc: /* Bind variables according to VARLIST then eval BODY.
866 The value of the last form in BODY is returned.
867 Each element of VARLIST is a symbol (which is bound to nil)
868 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
869 All the VALUEFORMs are evalled before any symbols are bound.
870 usage: (let VARLIST BODY...) */)
871 (Lisp_Object args)
873 Lisp_Object *temps, tem, lexenv;
874 register Lisp_Object elt, varlist;
875 ptrdiff_t count = SPECPDL_INDEX ();
876 ptrdiff_t argnum;
877 struct gcpro gcpro1, gcpro2;
878 USE_SAFE_ALLOCA;
880 varlist = XCAR (args);
882 /* Make space to hold the values to give the bound variables. */
883 elt = Flength (varlist);
884 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
886 /* Compute the values and store them in `temps'. */
888 GCPRO2 (args, *temps);
889 gcpro2.nvars = 0;
891 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
893 QUIT;
894 elt = XCAR (varlist);
895 if (SYMBOLP (elt))
896 temps [argnum++] = Qnil;
897 else if (! NILP (Fcdr (Fcdr (elt))))
898 signal_error ("`let' bindings can have only one value-form", elt);
899 else
900 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
901 gcpro2.nvars = argnum;
903 UNGCPRO;
905 lexenv = Vinternal_interpreter_environment;
907 varlist = XCAR (args);
908 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
910 Lisp_Object var;
912 elt = XCAR (varlist);
913 var = SYMBOLP (elt) ? elt : Fcar (elt);
914 tem = temps[argnum++];
916 if (!NILP (lexenv) && SYMBOLP (var)
917 && !XSYMBOL (var)->declared_special
918 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
919 /* Lexically bind VAR by adding it to the lexenv alist. */
920 lexenv = Fcons (Fcons (var, tem), lexenv);
921 else
922 /* Dynamically bind VAR. */
923 specbind (var, tem);
926 if (!EQ (lexenv, Vinternal_interpreter_environment))
927 /* Instantiate a new lexical environment. */
928 specbind (Qinternal_interpreter_environment, lexenv);
930 elt = Fprogn (XCDR (args));
931 SAFE_FREE ();
932 return unbind_to (count, elt);
935 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
936 doc: /* If TEST yields non-nil, eval BODY... and repeat.
937 The order of execution is thus TEST, BODY, TEST, BODY and so on
938 until TEST returns nil.
939 usage: (while TEST BODY...) */)
940 (Lisp_Object args)
942 Lisp_Object test, body;
943 struct gcpro gcpro1, gcpro2;
945 GCPRO2 (test, body);
947 test = XCAR (args);
948 body = XCDR (args);
949 while (!NILP (eval_sub (test)))
951 QUIT;
952 Fprogn (body);
955 UNGCPRO;
956 return Qnil;
959 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
960 doc: /* Return result of expanding macros at top level of FORM.
961 If FORM is not a macro call, it is returned unchanged.
962 Otherwise, the macro is expanded and the expansion is considered
963 in place of FORM. When a non-macro-call results, it is returned.
965 The second optional arg ENVIRONMENT specifies an environment of macro
966 definitions to shadow the loaded ones for use in file byte-compilation. */)
967 (Lisp_Object form, Lisp_Object environment)
969 /* With cleanups from Hallvard Furuseth. */
970 register Lisp_Object expander, sym, def, tem;
972 while (1)
974 /* Come back here each time we expand a macro call,
975 in case it expands into another macro call. */
976 if (!CONSP (form))
977 break;
978 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
979 def = sym = XCAR (form);
980 tem = Qnil;
981 /* Trace symbols aliases to other symbols
982 until we get a symbol that is not an alias. */
983 while (SYMBOLP (def))
985 QUIT;
986 sym = def;
987 tem = Fassq (sym, environment);
988 if (NILP (tem))
990 def = XSYMBOL (sym)->function;
991 if (!NILP (def))
992 continue;
994 break;
996 /* Right now TEM is the result from SYM in ENVIRONMENT,
997 and if TEM is nil then DEF is SYM's function definition. */
998 if (NILP (tem))
1000 /* SYM is not mentioned in ENVIRONMENT.
1001 Look at its function definition. */
1002 struct gcpro gcpro1;
1003 GCPRO1 (form);
1004 def = Fautoload_do_load (def, sym, Qmacro);
1005 UNGCPRO;
1006 if (!CONSP (def))
1007 /* Not defined or definition not suitable. */
1008 break;
1009 if (!EQ (XCAR (def), Qmacro))
1010 break;
1011 else expander = XCDR (def);
1013 else
1015 expander = XCDR (tem);
1016 if (NILP (expander))
1017 break;
1020 Lisp_Object newform = apply1 (expander, XCDR (form));
1021 if (EQ (form, newform))
1022 break;
1023 else
1024 form = newform;
1027 return form;
1030 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1031 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1032 TAG is evalled to get the tag to use; it must not be nil.
1034 Then the BODY is executed.
1035 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1036 If no throw happens, `catch' returns the value of the last BODY form.
1037 If a throw happens, it specifies the value to return from `catch'.
1038 usage: (catch TAG BODY...) */)
1039 (Lisp_Object args)
1041 register Lisp_Object tag;
1042 struct gcpro gcpro1;
1044 GCPRO1 (args);
1045 tag = eval_sub (XCAR (args));
1046 UNGCPRO;
1047 return internal_catch (tag, Fprogn, XCDR (args));
1050 /* Set up a catch, then call C function FUNC on argument ARG.
1051 FUNC should return a Lisp_Object.
1052 This is how catches are done from within C code. */
1054 Lisp_Object
1055 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1057 /* This structure is made part of the chain `catchlist'. */
1058 struct catchtag c;
1060 /* Fill in the components of c, and put it on the list. */
1061 c.next = catchlist;
1062 c.tag = tag;
1063 c.val = Qnil;
1064 c.handlerlist = handlerlist;
1065 c.lisp_eval_depth = lisp_eval_depth;
1066 c.pdlcount = SPECPDL_INDEX ();
1067 c.poll_suppress_count = poll_suppress_count;
1068 c.interrupt_input_blocked = interrupt_input_blocked;
1069 c.gcpro = gcprolist;
1070 c.byte_stack = byte_stack_list;
1071 catchlist = &c;
1073 /* Call FUNC. */
1074 if (! sys_setjmp (c.jmp))
1075 c.val = (*func) (arg);
1077 /* Throw works by a longjmp that comes right here. */
1078 catchlist = c.next;
1079 return c.val;
1082 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1083 jump to that CATCH, returning VALUE as the value of that catch.
1085 This is the guts of Fthrow and Fsignal; they differ only in the way
1086 they choose the catch tag to throw to. A catch tag for a
1087 condition-case form has a TAG of Qnil.
1089 Before each catch is discarded, unbind all special bindings and
1090 execute all unwind-protect clauses made above that catch. Unwind
1091 the handler stack as we go, so that the proper handlers are in
1092 effect for each unwind-protect clause we run. At the end, restore
1093 some static info saved in CATCH, and longjmp to the location
1094 specified there.
1096 This is used for correct unwinding in Fthrow and Fsignal. */
1098 static _Noreturn void
1099 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1101 bool last_time;
1103 /* Save the value in the tag. */
1104 catch->val = value;
1106 /* Restore certain special C variables. */
1107 set_poll_suppress_count (catch->poll_suppress_count);
1108 unblock_input_to (catch->interrupt_input_blocked);
1109 immediate_quit = 0;
1113 last_time = catchlist == catch;
1115 /* Unwind the specpdl stack, and then restore the proper set of
1116 handlers. */
1117 unbind_to (catchlist->pdlcount, Qnil);
1118 handlerlist = catchlist->handlerlist;
1119 catchlist = catchlist->next;
1121 while (! last_time);
1123 byte_stack_list = catch->byte_stack;
1124 gcprolist = catch->gcpro;
1125 #ifdef DEBUG_GCPRO
1126 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1127 #endif
1128 lisp_eval_depth = catch->lisp_eval_depth;
1130 sys_longjmp (catch->jmp, 1);
1133 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1134 doc: /* Throw to the catch for TAG and return VALUE from it.
1135 Both TAG and VALUE are evalled. */)
1136 (register Lisp_Object tag, Lisp_Object value)
1138 register struct catchtag *c;
1140 if (!NILP (tag))
1141 for (c = catchlist; c; c = c->next)
1143 if (EQ (c->tag, tag))
1144 unwind_to_catch (c, value);
1146 xsignal2 (Qno_catch, tag, value);
1150 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1151 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1152 If BODYFORM completes normally, its value is returned
1153 after executing the UNWINDFORMS.
1154 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1155 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1156 (Lisp_Object args)
1158 Lisp_Object val;
1159 ptrdiff_t count = SPECPDL_INDEX ();
1161 record_unwind_protect (unwind_body, XCDR (args));
1162 val = eval_sub (XCAR (args));
1163 return unbind_to (count, val);
1166 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1167 doc: /* Regain control when an error is signaled.
1168 Executes BODYFORM and returns its value if no error happens.
1169 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1170 where the BODY is made of Lisp expressions.
1172 A handler is applicable to an error
1173 if CONDITION-NAME is one of the error's condition names.
1174 If an error happens, the first applicable handler is run.
1176 The car of a handler may be a list of condition names instead of a
1177 single condition name; then it handles all of them. If the special
1178 condition name `debug' is present in this list, it allows another
1179 condition in the list to run the debugger if `debug-on-error' and the
1180 other usual mechanisms says it should (otherwise, `condition-case'
1181 suppresses the debugger).
1183 When a handler handles an error, control returns to the `condition-case'
1184 and it executes the handler's BODY...
1185 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1186 \(If VAR is nil, the handler can't access that information.)
1187 Then the value of the last BODY form is returned from the `condition-case'
1188 expression.
1190 See also the function `signal' for more info.
1191 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1192 (Lisp_Object args)
1194 Lisp_Object var = XCAR (args);
1195 Lisp_Object bodyform = XCAR (XCDR (args));
1196 Lisp_Object handlers = XCDR (XCDR (args));
1198 return internal_lisp_condition_case (var, bodyform, handlers);
1201 /* Like Fcondition_case, but the args are separate
1202 rather than passed in a list. Used by Fbyte_code. */
1204 Lisp_Object
1205 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1206 Lisp_Object handlers)
1208 Lisp_Object val;
1209 struct catchtag c;
1210 struct handler h;
1212 CHECK_SYMBOL (var);
1214 for (val = handlers; CONSP (val); val = XCDR (val))
1216 Lisp_Object tem;
1217 tem = XCAR (val);
1218 if (! (NILP (tem)
1219 || (CONSP (tem)
1220 && (SYMBOLP (XCAR (tem))
1221 || CONSP (XCAR (tem))))))
1222 error ("Invalid condition handler: %s",
1223 SDATA (Fprin1_to_string (tem, Qt)));
1226 c.tag = Qnil;
1227 c.val = Qnil;
1228 c.handlerlist = handlerlist;
1229 c.lisp_eval_depth = lisp_eval_depth;
1230 c.pdlcount = SPECPDL_INDEX ();
1231 c.poll_suppress_count = poll_suppress_count;
1232 c.interrupt_input_blocked = interrupt_input_blocked;
1233 c.gcpro = gcprolist;
1234 c.byte_stack = byte_stack_list;
1235 if (sys_setjmp (c.jmp))
1237 if (!NILP (h.var))
1238 specbind (h.var, c.val);
1239 val = Fprogn (Fcdr (h.chosen_clause));
1241 /* Note that this just undoes the binding of h.var; whoever
1242 longjumped to us unwound the stack to c.pdlcount before
1243 throwing. */
1244 unbind_to (c.pdlcount, Qnil);
1245 return val;
1247 c.next = catchlist;
1248 catchlist = &c;
1250 h.var = var;
1251 h.handler = handlers;
1252 h.next = handlerlist;
1253 h.tag = &c;
1254 handlerlist = &h;
1256 val = eval_sub (bodyform);
1257 catchlist = c.next;
1258 handlerlist = h.next;
1259 return val;
1262 /* Call the function BFUN with no arguments, catching errors within it
1263 according to HANDLERS. If there is an error, call HFUN with
1264 one argument which is the data that describes the error:
1265 (SIGNALNAME . DATA)
1267 HANDLERS can be a list of conditions to catch.
1268 If HANDLERS is Qt, catch all errors.
1269 If HANDLERS is Qerror, catch all errors
1270 but allow the debugger to run if that is enabled. */
1272 Lisp_Object
1273 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1274 Lisp_Object (*hfun) (Lisp_Object))
1276 Lisp_Object val;
1277 struct catchtag c;
1278 struct handler h;
1280 c.tag = Qnil;
1281 c.val = Qnil;
1282 c.handlerlist = handlerlist;
1283 c.lisp_eval_depth = lisp_eval_depth;
1284 c.pdlcount = SPECPDL_INDEX ();
1285 c.poll_suppress_count = poll_suppress_count;
1286 c.interrupt_input_blocked = interrupt_input_blocked;
1287 c.gcpro = gcprolist;
1288 c.byte_stack = byte_stack_list;
1289 if (sys_setjmp (c.jmp))
1291 return (*hfun) (c.val);
1293 c.next = catchlist;
1294 catchlist = &c;
1295 h.handler = handlers;
1296 h.var = Qnil;
1297 h.next = handlerlist;
1298 h.tag = &c;
1299 handlerlist = &h;
1301 val = (*bfun) ();
1302 catchlist = c.next;
1303 handlerlist = h.next;
1304 return val;
1307 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1309 Lisp_Object
1310 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1311 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1313 Lisp_Object val;
1314 struct catchtag c;
1315 struct handler h;
1317 c.tag = Qnil;
1318 c.val = Qnil;
1319 c.handlerlist = handlerlist;
1320 c.lisp_eval_depth = lisp_eval_depth;
1321 c.pdlcount = SPECPDL_INDEX ();
1322 c.poll_suppress_count = poll_suppress_count;
1323 c.interrupt_input_blocked = interrupt_input_blocked;
1324 c.gcpro = gcprolist;
1325 c.byte_stack = byte_stack_list;
1326 if (sys_setjmp (c.jmp))
1328 return (*hfun) (c.val);
1330 c.next = catchlist;
1331 catchlist = &c;
1332 h.handler = handlers;
1333 h.var = Qnil;
1334 h.next = handlerlist;
1335 h.tag = &c;
1336 handlerlist = &h;
1338 val = (*bfun) (arg);
1339 catchlist = c.next;
1340 handlerlist = h.next;
1341 return val;
1344 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1345 its arguments. */
1347 Lisp_Object
1348 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1349 Lisp_Object arg1,
1350 Lisp_Object arg2,
1351 Lisp_Object handlers,
1352 Lisp_Object (*hfun) (Lisp_Object))
1354 Lisp_Object val;
1355 struct catchtag c;
1356 struct handler h;
1358 c.tag = Qnil;
1359 c.val = Qnil;
1360 c.handlerlist = handlerlist;
1361 c.lisp_eval_depth = lisp_eval_depth;
1362 c.pdlcount = SPECPDL_INDEX ();
1363 c.poll_suppress_count = poll_suppress_count;
1364 c.interrupt_input_blocked = interrupt_input_blocked;
1365 c.gcpro = gcprolist;
1366 c.byte_stack = byte_stack_list;
1367 if (sys_setjmp (c.jmp))
1369 return (*hfun) (c.val);
1371 c.next = catchlist;
1372 catchlist = &c;
1373 h.handler = handlers;
1374 h.var = Qnil;
1375 h.next = handlerlist;
1376 h.tag = &c;
1377 handlerlist = &h;
1379 val = (*bfun) (arg1, arg2);
1380 catchlist = c.next;
1381 handlerlist = h.next;
1382 return val;
1385 /* Like internal_condition_case but call BFUN with NARGS as first,
1386 and ARGS as second argument. */
1388 Lisp_Object
1389 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1390 ptrdiff_t nargs,
1391 Lisp_Object *args,
1392 Lisp_Object handlers,
1393 Lisp_Object (*hfun) (Lisp_Object err,
1394 ptrdiff_t nargs,
1395 Lisp_Object *args))
1397 Lisp_Object val;
1398 struct catchtag c;
1399 struct handler h;
1401 c.tag = Qnil;
1402 c.val = Qnil;
1403 c.handlerlist = handlerlist;
1404 c.lisp_eval_depth = lisp_eval_depth;
1405 c.pdlcount = SPECPDL_INDEX ();
1406 c.poll_suppress_count = poll_suppress_count;
1407 c.interrupt_input_blocked = interrupt_input_blocked;
1408 c.gcpro = gcprolist;
1409 c.byte_stack = byte_stack_list;
1410 if (sys_setjmp (c.jmp))
1412 return (*hfun) (c.val, nargs, args);
1414 c.next = catchlist;
1415 catchlist = &c;
1416 h.handler = handlers;
1417 h.var = Qnil;
1418 h.next = handlerlist;
1419 h.tag = &c;
1420 handlerlist = &h;
1422 val = (*bfun) (nargs, args);
1423 catchlist = c.next;
1424 handlerlist = h.next;
1425 return val;
1429 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1430 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1431 Lisp_Object data);
1433 void
1434 process_quit_flag (void)
1436 Lisp_Object flag = Vquit_flag;
1437 Vquit_flag = Qnil;
1438 if (EQ (flag, Qkill_emacs))
1439 Fkill_emacs (Qnil);
1440 if (EQ (Vthrow_on_input, flag))
1441 Fthrow (Vthrow_on_input, Qt);
1442 Fsignal (Qquit, Qnil);
1445 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1446 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1447 This function does not return.
1449 An error symbol is a symbol with an `error-conditions' property
1450 that is a list of condition names.
1451 A handler for any of those names will get to handle this signal.
1452 The symbol `error' should normally be one of them.
1454 DATA should be a list. Its elements are printed as part of the error message.
1455 See Info anchor `(elisp)Definition of signal' for some details on how this
1456 error message is constructed.
1457 If the signal is handled, DATA is made available to the handler.
1458 See also the function `condition-case'. */)
1459 (Lisp_Object error_symbol, Lisp_Object data)
1461 /* When memory is full, ERROR-SYMBOL is nil,
1462 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1463 That is a special case--don't do this in other situations. */
1464 Lisp_Object conditions;
1465 Lisp_Object string;
1466 Lisp_Object real_error_symbol
1467 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1468 register Lisp_Object clause = Qnil;
1469 struct handler *h;
1471 immediate_quit = 0;
1472 abort_on_gc = 0;
1473 if (gc_in_progress || waiting_for_input)
1474 emacs_abort ();
1476 #if 0 /* rms: I don't know why this was here,
1477 but it is surely wrong for an error that is handled. */
1478 #ifdef HAVE_WINDOW_SYSTEM
1479 if (display_hourglass_p)
1480 cancel_hourglass ();
1481 #endif
1482 #endif
1484 /* This hook is used by edebug. */
1485 if (! NILP (Vsignal_hook_function)
1486 && ! NILP (error_symbol))
1488 /* Edebug takes care of restoring these variables when it exits. */
1489 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1490 max_lisp_eval_depth = lisp_eval_depth + 20;
1492 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1493 max_specpdl_size = SPECPDL_INDEX () + 40;
1495 call2 (Vsignal_hook_function, error_symbol, data);
1498 conditions = Fget (real_error_symbol, Qerror_conditions);
1500 /* Remember from where signal was called. Skip over the frame for
1501 `signal' itself. If a frame for `error' follows, skip that,
1502 too. Don't do this when ERROR_SYMBOL is nil, because that
1503 is a memory-full error. */
1504 Vsignaling_function = Qnil;
1505 if (!NILP (error_symbol))
1507 union specbinding *pdl = backtrace_next (backtrace_top ());
1508 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1509 pdl = backtrace_next (pdl);
1510 if (backtrace_p (pdl))
1511 Vsignaling_function = backtrace_function (pdl);
1514 for (h = handlerlist; h; h = h->next)
1516 clause = find_handler_clause (h->handler, conditions);
1517 if (!NILP (clause))
1518 break;
1521 if (/* Don't run the debugger for a memory-full error.
1522 (There is no room in memory to do that!) */
1523 !NILP (error_symbol)
1524 && (!NILP (Vdebug_on_signal)
1525 /* If no handler is present now, try to run the debugger. */
1526 || NILP (clause)
1527 /* A `debug' symbol in the handler list disables the normal
1528 suppression of the debugger. */
1529 || (CONSP (clause) && CONSP (XCAR (clause))
1530 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1531 /* Special handler that means "print a message and run debugger
1532 if requested". */
1533 || EQ (h->handler, Qerror)))
1535 bool debugger_called
1536 = maybe_call_debugger (conditions, error_symbol, data);
1537 /* We can't return values to code which signaled an error, but we
1538 can continue code which has signaled a quit. */
1539 if (debugger_called && EQ (real_error_symbol, Qquit))
1540 return Qnil;
1543 if (!NILP (clause))
1545 Lisp_Object unwind_data
1546 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1548 h->chosen_clause = clause;
1549 unwind_to_catch (h->tag, unwind_data);
1551 else
1553 if (catchlist != 0)
1554 Fthrow (Qtop_level, Qt);
1557 if (! NILP (error_symbol))
1558 data = Fcons (error_symbol, data);
1560 string = Ferror_message_string (data);
1561 fatal ("%s", SDATA (string));
1564 /* Internal version of Fsignal that never returns.
1565 Used for anything but Qquit (which can return from Fsignal). */
1567 void
1568 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1570 Fsignal (error_symbol, data);
1571 emacs_abort ();
1574 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1576 void
1577 xsignal0 (Lisp_Object error_symbol)
1579 xsignal (error_symbol, Qnil);
1582 void
1583 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1585 xsignal (error_symbol, list1 (arg));
1588 void
1589 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1591 xsignal (error_symbol, list2 (arg1, arg2));
1594 void
1595 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1597 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1600 /* Signal `error' with message S, and additional arg ARG.
1601 If ARG is not a genuine list, make it a one-element list. */
1603 void
1604 signal_error (const char *s, Lisp_Object arg)
1606 Lisp_Object tortoise, hare;
1608 hare = tortoise = arg;
1609 while (CONSP (hare))
1611 hare = XCDR (hare);
1612 if (!CONSP (hare))
1613 break;
1615 hare = XCDR (hare);
1616 tortoise = XCDR (tortoise);
1618 if (EQ (hare, tortoise))
1619 break;
1622 if (!NILP (hare))
1623 arg = list1 (arg);
1625 xsignal (Qerror, Fcons (build_string (s), arg));
1629 /* Return true if LIST is a non-nil atom or
1630 a list containing one of CONDITIONS. */
1632 static bool
1633 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1635 if (NILP (list))
1636 return 0;
1637 if (! CONSP (list))
1638 return 1;
1640 while (CONSP (conditions))
1642 Lisp_Object this, tail;
1643 this = XCAR (conditions);
1644 for (tail = list; CONSP (tail); tail = XCDR (tail))
1645 if (EQ (XCAR (tail), this))
1646 return 1;
1647 conditions = XCDR (conditions);
1649 return 0;
1652 /* Return true if an error with condition-symbols CONDITIONS,
1653 and described by SIGNAL-DATA, should skip the debugger
1654 according to debugger-ignored-errors. */
1656 static bool
1657 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1659 Lisp_Object tail;
1660 bool first_string = 1;
1661 Lisp_Object error_message;
1663 error_message = Qnil;
1664 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1666 if (STRINGP (XCAR (tail)))
1668 if (first_string)
1670 error_message = Ferror_message_string (data);
1671 first_string = 0;
1674 if (fast_string_match (XCAR (tail), error_message) >= 0)
1675 return 1;
1677 else
1679 Lisp_Object contail;
1681 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1682 if (EQ (XCAR (tail), XCAR (contail)))
1683 return 1;
1687 return 0;
1690 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1691 SIG and DATA describe the signal. There are two ways to pass them:
1692 = SIG is the error symbol, and DATA is the rest of the data.
1693 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1694 This is for memory-full errors only. */
1695 static bool
1696 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1698 Lisp_Object combined_data;
1700 combined_data = Fcons (sig, data);
1702 if (
1703 /* Don't try to run the debugger with interrupts blocked.
1704 The editing loop would return anyway. */
1705 ! input_blocked_p ()
1706 && NILP (Vinhibit_debugger)
1707 /* Does user want to enter debugger for this kind of error? */
1708 && (EQ (sig, Qquit)
1709 ? debug_on_quit
1710 : wants_debugger (Vdebug_on_error, conditions))
1711 && ! skip_debugger (conditions, combined_data)
1712 /* RMS: What's this for? */
1713 && when_entered_debugger < num_nonmacro_input_events)
1715 call_debugger (list2 (Qerror, combined_data));
1716 return 1;
1719 return 0;
1722 static Lisp_Object
1723 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1725 register Lisp_Object h;
1727 /* t is used by handlers for all conditions, set up by C code. */
1728 if (EQ (handlers, Qt))
1729 return Qt;
1731 /* error is used similarly, but means print an error message
1732 and run the debugger if that is enabled. */
1733 if (EQ (handlers, Qerror))
1734 return Qt;
1736 for (h = handlers; CONSP (h); h = XCDR (h))
1738 Lisp_Object handler = XCAR (h);
1739 Lisp_Object condit, tem;
1741 if (!CONSP (handler))
1742 continue;
1743 condit = XCAR (handler);
1744 /* Handle a single condition name in handler HANDLER. */
1745 if (SYMBOLP (condit))
1747 tem = Fmemq (Fcar (handler), conditions);
1748 if (!NILP (tem))
1749 return handler;
1751 /* Handle a list of condition names in handler HANDLER. */
1752 else if (CONSP (condit))
1754 Lisp_Object tail;
1755 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1757 tem = Fmemq (XCAR (tail), conditions);
1758 if (!NILP (tem))
1759 return handler;
1764 return Qnil;
1768 /* Dump an error message; called like vprintf. */
1769 void
1770 verror (const char *m, va_list ap)
1772 char buf[4000];
1773 ptrdiff_t size = sizeof buf;
1774 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1775 char *buffer = buf;
1776 ptrdiff_t used;
1777 Lisp_Object string;
1779 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1780 string = make_string (buffer, used);
1781 if (buffer != buf)
1782 xfree (buffer);
1784 xsignal1 (Qerror, string);
1788 /* Dump an error message; called like printf. */
1790 /* VARARGS 1 */
1791 void
1792 error (const char *m, ...)
1794 va_list ap;
1795 va_start (ap, m);
1796 verror (m, ap);
1799 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1800 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1801 This means it contains a description for how to read arguments to give it.
1802 The value is nil for an invalid function or a symbol with no function
1803 definition.
1805 Interactively callable functions include strings and vectors (treated
1806 as keyboard macros), lambda-expressions that contain a top-level call
1807 to `interactive', autoload definitions made by `autoload' with non-nil
1808 fourth argument, and some of the built-in functions of Lisp.
1810 Also, a symbol satisfies `commandp' if its function definition does so.
1812 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1813 then strings and vectors are not accepted. */)
1814 (Lisp_Object function, Lisp_Object for_call_interactively)
1816 register Lisp_Object fun;
1817 register Lisp_Object funcar;
1818 Lisp_Object if_prop = Qnil;
1820 fun = function;
1822 fun = indirect_function (fun); /* Check cycles. */
1823 if (NILP (fun))
1824 return Qnil;
1826 /* Check an `interactive-form' property if present, analogous to the
1827 function-documentation property. */
1828 fun = function;
1829 while (SYMBOLP (fun))
1831 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1832 if (!NILP (tmp))
1833 if_prop = Qt;
1834 fun = Fsymbol_function (fun);
1837 /* Emacs primitives are interactive if their DEFUN specifies an
1838 interactive spec. */
1839 if (SUBRP (fun))
1840 return XSUBR (fun)->intspec ? Qt : if_prop;
1842 /* Bytecode objects are interactive if they are long enough to
1843 have an element whose index is COMPILED_INTERACTIVE, which is
1844 where the interactive spec is stored. */
1845 else if (COMPILEDP (fun))
1846 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1847 ? Qt : if_prop);
1849 /* Strings and vectors are keyboard macros. */
1850 if (STRINGP (fun) || VECTORP (fun))
1851 return (NILP (for_call_interactively) ? Qt : Qnil);
1853 /* Lists may represent commands. */
1854 if (!CONSP (fun))
1855 return Qnil;
1856 funcar = XCAR (fun);
1857 if (EQ (funcar, Qclosure))
1858 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1859 ? Qt : if_prop);
1860 else if (EQ (funcar, Qlambda))
1861 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1862 else if (EQ (funcar, Qautoload))
1863 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1864 else
1865 return Qnil;
1868 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1869 doc: /* Define FUNCTION to autoload from FILE.
1870 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1871 Third arg DOCSTRING is documentation for the function.
1872 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1873 Fifth arg TYPE indicates the type of the object:
1874 nil or omitted says FUNCTION is a function,
1875 `keymap' says FUNCTION is really a keymap, and
1876 `macro' or t says FUNCTION is really a macro.
1877 Third through fifth args give info about the real definition.
1878 They default to nil.
1879 If FUNCTION is already defined other than as an autoload,
1880 this does nothing and returns nil. */)
1881 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1883 CHECK_SYMBOL (function);
1884 CHECK_STRING (file);
1886 /* If function is defined and not as an autoload, don't override. */
1887 if (!NILP (XSYMBOL (function)->function)
1888 && !AUTOLOADP (XSYMBOL (function)->function))
1889 return Qnil;
1891 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1892 /* `read1' in lread.c has found the docstring starting with "\
1893 and assumed the docstring will be provided by Snarf-documentation, so it
1894 passed us 0 instead. But that leads to accidental sharing in purecopy's
1895 hash-consing, so we use a (hopefully) unique integer instead. */
1896 docstring = make_number (XHASH (function));
1897 return Fdefalias (function,
1898 list5 (Qautoload, file, docstring, interactive, type),
1899 Qnil);
1902 void
1903 un_autoload (Lisp_Object oldqueue)
1905 Lisp_Object queue, first, second;
1907 /* Queue to unwind is current value of Vautoload_queue.
1908 oldqueue is the shadowed value to leave in Vautoload_queue. */
1909 queue = Vautoload_queue;
1910 Vautoload_queue = oldqueue;
1911 while (CONSP (queue))
1913 first = XCAR (queue);
1914 second = Fcdr (first);
1915 first = Fcar (first);
1916 if (EQ (first, make_number (0)))
1917 Vfeatures = second;
1918 else
1919 Ffset (first, second);
1920 queue = XCDR (queue);
1924 /* Load an autoloaded function.
1925 FUNNAME is the symbol which is the function's name.
1926 FUNDEF is the autoload definition (a list). */
1928 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1929 doc: /* Load FUNDEF which should be an autoload.
1930 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1931 in which case the function returns the new autoloaded function value.
1932 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1933 it is defines a macro. */)
1934 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1936 ptrdiff_t count = SPECPDL_INDEX ();
1937 struct gcpro gcpro1, gcpro2, gcpro3;
1939 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1940 return fundef;
1942 if (EQ (macro_only, Qmacro))
1944 Lisp_Object kind = Fnth (make_number (4), fundef);
1945 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1946 return fundef;
1949 /* This is to make sure that loadup.el gives a clear picture
1950 of what files are preloaded and when. */
1951 if (! NILP (Vpurify_flag))
1952 error ("Attempt to autoload %s while preparing to dump",
1953 SDATA (SYMBOL_NAME (funname)));
1955 CHECK_SYMBOL (funname);
1956 GCPRO3 (funname, fundef, macro_only);
1958 /* Preserve the match data. */
1959 record_unwind_save_match_data ();
1961 /* If autoloading gets an error (which includes the error of failing
1962 to define the function being called), we use Vautoload_queue
1963 to undo function definitions and `provide' calls made by
1964 the function. We do this in the specific case of autoloading
1965 because autoloading is not an explicit request "load this file",
1966 but rather a request to "call this function".
1968 The value saved here is to be restored into Vautoload_queue. */
1969 record_unwind_protect (un_autoload, Vautoload_queue);
1970 Vautoload_queue = Qt;
1971 /* If `macro_only', assume this autoload to be a "best-effort",
1972 so don't signal an error if autoloading fails. */
1973 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1975 /* Once loading finishes, don't undo it. */
1976 Vautoload_queue = Qt;
1977 unbind_to (count, Qnil);
1979 UNGCPRO;
1981 if (NILP (funname))
1982 return Qnil;
1983 else
1985 Lisp_Object fun = Findirect_function (funname, Qnil);
1987 if (!NILP (Fequal (fun, fundef)))
1988 error ("Autoloading failed to define function %s",
1989 SDATA (SYMBOL_NAME (funname)));
1990 else
1991 return fun;
1996 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1997 doc: /* Evaluate FORM and return its value.
1998 If LEXICAL is t, evaluate using lexical scoping. */)
1999 (Lisp_Object form, Lisp_Object lexical)
2001 ptrdiff_t count = SPECPDL_INDEX ();
2002 specbind (Qinternal_interpreter_environment,
2003 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2004 return unbind_to (count, eval_sub (form));
2007 /* Grow the specpdl stack by one entry.
2008 The caller should have already initialized the entry.
2009 Signal an error on stack overflow.
2011 Make sure that there is always one unused entry past the top of the
2012 stack, so that the just-initialized entry is safely unwound if
2013 memory exhausted and an error is signaled here. Also, allocate a
2014 never-used entry just before the bottom of the stack; sometimes its
2015 address is taken. */
2017 static void
2018 grow_specpdl (void)
2020 specpdl_ptr++;
2022 if (specpdl_ptr == specpdl + specpdl_size)
2024 ptrdiff_t count = SPECPDL_INDEX ();
2025 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2026 union specbinding *pdlvec = specpdl - 1;
2027 ptrdiff_t pdlvecsize = specpdl_size + 1;
2028 if (max_size <= specpdl_size)
2030 if (max_specpdl_size < 400)
2031 max_size = max_specpdl_size = 400;
2032 if (max_size <= specpdl_size)
2033 signal_error ("Variable binding depth exceeds max-specpdl-size",
2034 Qnil);
2036 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2037 specpdl = pdlvec + 1;
2038 specpdl_size = pdlvecsize - 1;
2039 specpdl_ptr = specpdl + count;
2043 void
2044 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2046 eassert (nargs >= UNEVALLED);
2047 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2048 specpdl_ptr->bt.debug_on_exit = false;
2049 specpdl_ptr->bt.function = function;
2050 specpdl_ptr->bt.args = args;
2051 specpdl_ptr->bt.nargs = nargs;
2052 grow_specpdl ();
2055 /* Eval a sub-expression of the current expression (i.e. in the same
2056 lexical scope). */
2057 Lisp_Object
2058 eval_sub (Lisp_Object form)
2060 Lisp_Object fun, val, original_fun, original_args;
2061 Lisp_Object funcar;
2062 struct gcpro gcpro1, gcpro2, gcpro3;
2064 if (SYMBOLP (form))
2066 /* Look up its binding in the lexical environment.
2067 We do not pay attention to the declared_special flag here, since we
2068 already did that when let-binding the variable. */
2069 Lisp_Object lex_binding
2070 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2071 ? Fassq (form, Vinternal_interpreter_environment)
2072 : Qnil;
2073 if (CONSP (lex_binding))
2074 return XCDR (lex_binding);
2075 else
2076 return Fsymbol_value (form);
2079 if (!CONSP (form))
2080 return form;
2082 QUIT;
2084 GCPRO1 (form);
2085 maybe_gc ();
2086 UNGCPRO;
2088 if (++lisp_eval_depth > max_lisp_eval_depth)
2090 if (max_lisp_eval_depth < 100)
2091 max_lisp_eval_depth = 100;
2092 if (lisp_eval_depth > max_lisp_eval_depth)
2093 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2096 original_fun = XCAR (form);
2097 original_args = XCDR (form);
2099 /* This also protects them from gc. */
2100 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2102 if (debug_on_next_call)
2103 do_debug_on_call (Qt);
2105 /* At this point, only original_fun and original_args
2106 have values that will be used below. */
2107 retry:
2109 /* Optimize for no indirection. */
2110 fun = original_fun;
2111 if (SYMBOLP (fun) && !NILP (fun)
2112 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2113 fun = indirect_function (fun);
2115 if (SUBRP (fun))
2117 Lisp_Object numargs;
2118 Lisp_Object argvals[8];
2119 Lisp_Object args_left;
2120 register int i, maxargs;
2122 args_left = original_args;
2123 numargs = Flength (args_left);
2125 check_cons_list ();
2127 if (XINT (numargs) < XSUBR (fun)->min_args
2128 || (XSUBR (fun)->max_args >= 0
2129 && XSUBR (fun)->max_args < XINT (numargs)))
2130 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2132 else if (XSUBR (fun)->max_args == UNEVALLED)
2133 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2134 else if (XSUBR (fun)->max_args == MANY)
2136 /* Pass a vector of evaluated arguments. */
2137 Lisp_Object *vals;
2138 ptrdiff_t argnum = 0;
2139 USE_SAFE_ALLOCA;
2141 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2143 GCPRO3 (args_left, fun, fun);
2144 gcpro3.var = vals;
2145 gcpro3.nvars = 0;
2147 while (!NILP (args_left))
2149 vals[argnum++] = eval_sub (Fcar (args_left));
2150 args_left = Fcdr (args_left);
2151 gcpro3.nvars = argnum;
2154 set_backtrace_args (specpdl_ptr - 1, vals);
2155 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2157 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2158 UNGCPRO;
2159 SAFE_FREE ();
2161 else
2163 GCPRO3 (args_left, fun, fun);
2164 gcpro3.var = argvals;
2165 gcpro3.nvars = 0;
2167 maxargs = XSUBR (fun)->max_args;
2168 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2170 argvals[i] = eval_sub (Fcar (args_left));
2171 gcpro3.nvars = ++i;
2174 UNGCPRO;
2176 set_backtrace_args (specpdl_ptr - 1, argvals);
2177 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2179 switch (i)
2181 case 0:
2182 val = (XSUBR (fun)->function.a0 ());
2183 break;
2184 case 1:
2185 val = (XSUBR (fun)->function.a1 (argvals[0]));
2186 break;
2187 case 2:
2188 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2189 break;
2190 case 3:
2191 val = (XSUBR (fun)->function.a3
2192 (argvals[0], argvals[1], argvals[2]));
2193 break;
2194 case 4:
2195 val = (XSUBR (fun)->function.a4
2196 (argvals[0], argvals[1], argvals[2], argvals[3]));
2197 break;
2198 case 5:
2199 val = (XSUBR (fun)->function.a5
2200 (argvals[0], argvals[1], argvals[2], argvals[3],
2201 argvals[4]));
2202 break;
2203 case 6:
2204 val = (XSUBR (fun)->function.a6
2205 (argvals[0], argvals[1], argvals[2], argvals[3],
2206 argvals[4], argvals[5]));
2207 break;
2208 case 7:
2209 val = (XSUBR (fun)->function.a7
2210 (argvals[0], argvals[1], argvals[2], argvals[3],
2211 argvals[4], argvals[5], argvals[6]));
2212 break;
2214 case 8:
2215 val = (XSUBR (fun)->function.a8
2216 (argvals[0], argvals[1], argvals[2], argvals[3],
2217 argvals[4], argvals[5], argvals[6], argvals[7]));
2218 break;
2220 default:
2221 /* Someone has created a subr that takes more arguments than
2222 is supported by this code. We need to either rewrite the
2223 subr to use a different argument protocol, or add more
2224 cases to this switch. */
2225 emacs_abort ();
2229 else if (COMPILEDP (fun))
2230 val = apply_lambda (fun, original_args);
2231 else
2233 if (NILP (fun))
2234 xsignal1 (Qvoid_function, original_fun);
2235 if (!CONSP (fun))
2236 xsignal1 (Qinvalid_function, original_fun);
2237 funcar = XCAR (fun);
2238 if (!SYMBOLP (funcar))
2239 xsignal1 (Qinvalid_function, original_fun);
2240 if (EQ (funcar, Qautoload))
2242 Fautoload_do_load (fun, original_fun, Qnil);
2243 goto retry;
2245 if (EQ (funcar, Qmacro))
2247 ptrdiff_t count = SPECPDL_INDEX ();
2248 Lisp_Object exp;
2249 /* Bind lexical-binding during expansion of the macro, so the
2250 macro can know reliably if the code it outputs will be
2251 interpreted using lexical-binding or not. */
2252 specbind (Qlexical_binding,
2253 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2254 exp = apply1 (Fcdr (fun), original_args);
2255 unbind_to (count, Qnil);
2256 val = eval_sub (exp);
2258 else if (EQ (funcar, Qlambda)
2259 || EQ (funcar, Qclosure))
2260 val = apply_lambda (fun, original_args);
2261 else
2262 xsignal1 (Qinvalid_function, original_fun);
2264 check_cons_list ();
2266 lisp_eval_depth--;
2267 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2268 val = call_debugger (list2 (Qexit, val));
2269 specpdl_ptr--;
2271 return val;
2274 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2275 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2276 Then return the value FUNCTION returns.
2277 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2278 usage: (apply FUNCTION &rest ARGUMENTS) */)
2279 (ptrdiff_t nargs, Lisp_Object *args)
2281 ptrdiff_t i;
2282 EMACS_INT numargs;
2283 register Lisp_Object spread_arg;
2284 register Lisp_Object *funcall_args;
2285 Lisp_Object fun, retval;
2286 struct gcpro gcpro1;
2287 USE_SAFE_ALLOCA;
2289 fun = args [0];
2290 funcall_args = 0;
2291 spread_arg = args [nargs - 1];
2292 CHECK_LIST (spread_arg);
2294 numargs = XINT (Flength (spread_arg));
2296 if (numargs == 0)
2297 return Ffuncall (nargs - 1, args);
2298 else if (numargs == 1)
2300 args [nargs - 1] = XCAR (spread_arg);
2301 return Ffuncall (nargs, args);
2304 numargs += nargs - 2;
2306 /* Optimize for no indirection. */
2307 if (SYMBOLP (fun) && !NILP (fun)
2308 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2309 fun = indirect_function (fun);
2310 if (NILP (fun))
2312 /* Let funcall get the error. */
2313 fun = args[0];
2314 goto funcall;
2317 if (SUBRP (fun))
2319 if (numargs < XSUBR (fun)->min_args
2320 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2321 goto funcall; /* Let funcall get the error. */
2322 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2324 /* Avoid making funcall cons up a yet another new vector of arguments
2325 by explicitly supplying nil's for optional values. */
2326 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2327 for (i = numargs; i < XSUBR (fun)->max_args;)
2328 funcall_args[++i] = Qnil;
2329 GCPRO1 (*funcall_args);
2330 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2333 funcall:
2334 /* We add 1 to numargs because funcall_args includes the
2335 function itself as well as its arguments. */
2336 if (!funcall_args)
2338 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2339 GCPRO1 (*funcall_args);
2340 gcpro1.nvars = 1 + numargs;
2343 memcpy (funcall_args, args, nargs * word_size);
2344 /* Spread the last arg we got. Its first element goes in
2345 the slot that it used to occupy, hence this value of I. */
2346 i = nargs - 1;
2347 while (!NILP (spread_arg))
2349 funcall_args [i++] = XCAR (spread_arg);
2350 spread_arg = XCDR (spread_arg);
2353 /* By convention, the caller needs to gcpro Ffuncall's args. */
2354 retval = Ffuncall (gcpro1.nvars, funcall_args);
2355 UNGCPRO;
2356 SAFE_FREE ();
2358 return retval;
2361 /* Run hook variables in various ways. */
2363 static Lisp_Object
2364 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2366 Ffuncall (nargs, args);
2367 return Qnil;
2370 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2371 doc: /* Run each hook in HOOKS.
2372 Each argument should be a symbol, a hook variable.
2373 These symbols are processed in the order specified.
2374 If a hook symbol has a non-nil value, that value may be a function
2375 or a list of functions to be called to run the hook.
2376 If the value is a function, it is called with no arguments.
2377 If it is a list, the elements are called, in order, with no arguments.
2379 Major modes should not use this function directly to run their mode
2380 hook; they should use `run-mode-hooks' instead.
2382 Do not use `make-local-variable' to make a hook variable buffer-local.
2383 Instead, use `add-hook' and specify t for the LOCAL argument.
2384 usage: (run-hooks &rest HOOKS) */)
2385 (ptrdiff_t nargs, Lisp_Object *args)
2387 Lisp_Object hook[1];
2388 ptrdiff_t i;
2390 for (i = 0; i < nargs; i++)
2392 hook[0] = args[i];
2393 run_hook_with_args (1, hook, funcall_nil);
2396 return Qnil;
2399 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2400 Srun_hook_with_args, 1, MANY, 0,
2401 doc: /* Run HOOK with the specified arguments ARGS.
2402 HOOK should be a symbol, a hook variable. The value of HOOK
2403 may be nil, a function, or a list of functions. Call each
2404 function in order with arguments ARGS. The final return value
2405 is unspecified.
2407 Do not use `make-local-variable' to make a hook variable buffer-local.
2408 Instead, use `add-hook' and specify t for the LOCAL argument.
2409 usage: (run-hook-with-args HOOK &rest ARGS) */)
2410 (ptrdiff_t nargs, Lisp_Object *args)
2412 return run_hook_with_args (nargs, args, funcall_nil);
2415 /* NB this one still documents a specific non-nil return value.
2416 (As did run-hook-with-args and run-hook-with-args-until-failure
2417 until they were changed in 24.1.) */
2418 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2419 Srun_hook_with_args_until_success, 1, MANY, 0,
2420 doc: /* Run HOOK with the specified arguments ARGS.
2421 HOOK should be a symbol, a hook variable. The value of HOOK
2422 may be nil, a function, or a list of functions. Call each
2423 function in order with arguments ARGS, stopping at the first
2424 one that returns non-nil, and return that value. Otherwise (if
2425 all functions return nil, or if there are no functions to call),
2426 return nil.
2428 Do not use `make-local-variable' to make a hook variable buffer-local.
2429 Instead, use `add-hook' and specify t for the LOCAL argument.
2430 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2431 (ptrdiff_t nargs, Lisp_Object *args)
2433 return run_hook_with_args (nargs, args, Ffuncall);
2436 static Lisp_Object
2437 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2439 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2442 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2443 Srun_hook_with_args_until_failure, 1, MANY, 0,
2444 doc: /* Run HOOK with the specified arguments ARGS.
2445 HOOK should be a symbol, a hook variable. The value of HOOK
2446 may be nil, a function, or a list of functions. Call each
2447 function in order with arguments ARGS, stopping at the first
2448 one that returns nil, and return nil. Otherwise (if all functions
2449 return non-nil, or if there are no functions to call), return non-nil
2450 \(do not rely on the precise return value in this case).
2452 Do not use `make-local-variable' to make a hook variable buffer-local.
2453 Instead, use `add-hook' and specify t for the LOCAL argument.
2454 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2455 (ptrdiff_t nargs, Lisp_Object *args)
2457 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2460 static Lisp_Object
2461 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2463 Lisp_Object tmp = args[0], ret;
2464 args[0] = args[1];
2465 args[1] = tmp;
2466 ret = Ffuncall (nargs, args);
2467 args[1] = args[0];
2468 args[0] = tmp;
2469 return ret;
2472 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2473 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2474 I.e. instead of calling each function FUN directly with arguments ARGS,
2475 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2476 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2477 aborts and returns that value.
2478 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2479 (ptrdiff_t nargs, Lisp_Object *args)
2481 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2484 /* ARGS[0] should be a hook symbol.
2485 Call each of the functions in the hook value, passing each of them
2486 as arguments all the rest of ARGS (all NARGS - 1 elements).
2487 FUNCALL specifies how to call each function on the hook.
2488 The caller (or its caller, etc) must gcpro all of ARGS,
2489 except that it isn't necessary to gcpro ARGS[0]. */
2491 Lisp_Object
2492 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2493 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2495 Lisp_Object sym, val, ret = Qnil;
2496 struct gcpro gcpro1, gcpro2, gcpro3;
2498 /* If we are dying or still initializing,
2499 don't do anything--it would probably crash if we tried. */
2500 if (NILP (Vrun_hooks))
2501 return Qnil;
2503 sym = args[0];
2504 val = find_symbol_value (sym);
2506 if (EQ (val, Qunbound) || NILP (val))
2507 return ret;
2508 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2510 args[0] = val;
2511 return funcall (nargs, args);
2513 else
2515 Lisp_Object global_vals = Qnil;
2516 GCPRO3 (sym, val, global_vals);
2518 for (;
2519 CONSP (val) && NILP (ret);
2520 val = XCDR (val))
2522 if (EQ (XCAR (val), Qt))
2524 /* t indicates this hook has a local binding;
2525 it means to run the global binding too. */
2526 global_vals = Fdefault_value (sym);
2527 if (NILP (global_vals)) continue;
2529 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2531 args[0] = global_vals;
2532 ret = funcall (nargs, args);
2534 else
2536 for (;
2537 CONSP (global_vals) && NILP (ret);
2538 global_vals = XCDR (global_vals))
2540 args[0] = XCAR (global_vals);
2541 /* In a global value, t should not occur. If it does, we
2542 must ignore it to avoid an endless loop. */
2543 if (!EQ (args[0], Qt))
2544 ret = funcall (nargs, args);
2548 else
2550 args[0] = XCAR (val);
2551 ret = funcall (nargs, args);
2555 UNGCPRO;
2556 return ret;
2560 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2562 void
2563 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2565 Lisp_Object temp[3];
2566 temp[0] = hook;
2567 temp[1] = arg1;
2568 temp[2] = arg2;
2570 Frun_hook_with_args (3, temp);
2573 /* Apply fn to arg. */
2574 Lisp_Object
2575 apply1 (Lisp_Object fn, Lisp_Object arg)
2577 struct gcpro gcpro1;
2579 GCPRO1 (fn);
2580 if (NILP (arg))
2581 RETURN_UNGCPRO (Ffuncall (1, &fn));
2582 gcpro1.nvars = 2;
2584 Lisp_Object args[2];
2585 args[0] = fn;
2586 args[1] = arg;
2587 gcpro1.var = args;
2588 RETURN_UNGCPRO (Fapply (2, args));
2592 /* Call function fn on no arguments. */
2593 Lisp_Object
2594 call0 (Lisp_Object fn)
2596 struct gcpro gcpro1;
2598 GCPRO1 (fn);
2599 RETURN_UNGCPRO (Ffuncall (1, &fn));
2602 /* Call function fn with 1 argument arg1. */
2603 /* ARGSUSED */
2604 Lisp_Object
2605 call1 (Lisp_Object fn, Lisp_Object arg1)
2607 struct gcpro gcpro1;
2608 Lisp_Object args[2];
2610 args[0] = fn;
2611 args[1] = arg1;
2612 GCPRO1 (args[0]);
2613 gcpro1.nvars = 2;
2614 RETURN_UNGCPRO (Ffuncall (2, args));
2617 /* Call function fn with 2 arguments arg1, arg2. */
2618 /* ARGSUSED */
2619 Lisp_Object
2620 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2622 struct gcpro gcpro1;
2623 Lisp_Object args[3];
2624 args[0] = fn;
2625 args[1] = arg1;
2626 args[2] = arg2;
2627 GCPRO1 (args[0]);
2628 gcpro1.nvars = 3;
2629 RETURN_UNGCPRO (Ffuncall (3, args));
2632 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2633 /* ARGSUSED */
2634 Lisp_Object
2635 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2637 struct gcpro gcpro1;
2638 Lisp_Object args[4];
2639 args[0] = fn;
2640 args[1] = arg1;
2641 args[2] = arg2;
2642 args[3] = arg3;
2643 GCPRO1 (args[0]);
2644 gcpro1.nvars = 4;
2645 RETURN_UNGCPRO (Ffuncall (4, args));
2648 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2649 /* ARGSUSED */
2650 Lisp_Object
2651 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2652 Lisp_Object arg4)
2654 struct gcpro gcpro1;
2655 Lisp_Object args[5];
2656 args[0] = fn;
2657 args[1] = arg1;
2658 args[2] = arg2;
2659 args[3] = arg3;
2660 args[4] = arg4;
2661 GCPRO1 (args[0]);
2662 gcpro1.nvars = 5;
2663 RETURN_UNGCPRO (Ffuncall (5, args));
2666 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2667 /* ARGSUSED */
2668 Lisp_Object
2669 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2670 Lisp_Object arg4, Lisp_Object arg5)
2672 struct gcpro gcpro1;
2673 Lisp_Object args[6];
2674 args[0] = fn;
2675 args[1] = arg1;
2676 args[2] = arg2;
2677 args[3] = arg3;
2678 args[4] = arg4;
2679 args[5] = arg5;
2680 GCPRO1 (args[0]);
2681 gcpro1.nvars = 6;
2682 RETURN_UNGCPRO (Ffuncall (6, args));
2685 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2686 /* ARGSUSED */
2687 Lisp_Object
2688 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2689 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2691 struct gcpro gcpro1;
2692 Lisp_Object args[7];
2693 args[0] = fn;
2694 args[1] = arg1;
2695 args[2] = arg2;
2696 args[3] = arg3;
2697 args[4] = arg4;
2698 args[5] = arg5;
2699 args[6] = arg6;
2700 GCPRO1 (args[0]);
2701 gcpro1.nvars = 7;
2702 RETURN_UNGCPRO (Ffuncall (7, args));
2705 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2706 /* ARGSUSED */
2707 Lisp_Object
2708 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2709 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2711 struct gcpro gcpro1;
2712 Lisp_Object args[8];
2713 args[0] = fn;
2714 args[1] = arg1;
2715 args[2] = arg2;
2716 args[3] = arg3;
2717 args[4] = arg4;
2718 args[5] = arg5;
2719 args[6] = arg6;
2720 args[7] = arg7;
2721 GCPRO1 (args[0]);
2722 gcpro1.nvars = 8;
2723 RETURN_UNGCPRO (Ffuncall (8, args));
2726 /* The caller should GCPRO all the elements of ARGS. */
2728 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2729 doc: /* Non-nil if OBJECT is a function. */)
2730 (Lisp_Object object)
2732 if (FUNCTIONP (object))
2733 return Qt;
2734 return Qnil;
2737 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2738 doc: /* Call first argument as a function, passing remaining arguments to it.
2739 Return the value that function returns.
2740 Thus, (funcall 'cons 'x 'y) returns (x . y).
2741 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2742 (ptrdiff_t nargs, Lisp_Object *args)
2744 Lisp_Object fun, original_fun;
2745 Lisp_Object funcar;
2746 ptrdiff_t numargs = nargs - 1;
2747 Lisp_Object lisp_numargs;
2748 Lisp_Object val;
2749 register Lisp_Object *internal_args;
2750 ptrdiff_t i;
2752 QUIT;
2754 if (++lisp_eval_depth > max_lisp_eval_depth)
2756 if (max_lisp_eval_depth < 100)
2757 max_lisp_eval_depth = 100;
2758 if (lisp_eval_depth > max_lisp_eval_depth)
2759 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2762 /* This also GCPROs them. */
2763 record_in_backtrace (args[0], &args[1], nargs - 1);
2765 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2766 maybe_gc ();
2768 if (debug_on_next_call)
2769 do_debug_on_call (Qlambda);
2771 check_cons_list ();
2773 original_fun = args[0];
2775 retry:
2777 /* Optimize for no indirection. */
2778 fun = original_fun;
2779 if (SYMBOLP (fun) && !NILP (fun)
2780 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2781 fun = indirect_function (fun);
2783 if (SUBRP (fun))
2785 if (numargs < XSUBR (fun)->min_args
2786 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2788 XSETFASTINT (lisp_numargs, numargs);
2789 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2792 else if (XSUBR (fun)->max_args == UNEVALLED)
2793 xsignal1 (Qinvalid_function, original_fun);
2795 else if (XSUBR (fun)->max_args == MANY)
2796 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2797 else
2799 if (XSUBR (fun)->max_args > numargs)
2801 internal_args = alloca (XSUBR (fun)->max_args
2802 * sizeof *internal_args);
2803 memcpy (internal_args, args + 1, numargs * word_size);
2804 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2805 internal_args[i] = Qnil;
2807 else
2808 internal_args = args + 1;
2809 switch (XSUBR (fun)->max_args)
2811 case 0:
2812 val = (XSUBR (fun)->function.a0 ());
2813 break;
2814 case 1:
2815 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2816 break;
2817 case 2:
2818 val = (XSUBR (fun)->function.a2
2819 (internal_args[0], internal_args[1]));
2820 break;
2821 case 3:
2822 val = (XSUBR (fun)->function.a3
2823 (internal_args[0], internal_args[1], internal_args[2]));
2824 break;
2825 case 4:
2826 val = (XSUBR (fun)->function.a4
2827 (internal_args[0], internal_args[1], internal_args[2],
2828 internal_args[3]));
2829 break;
2830 case 5:
2831 val = (XSUBR (fun)->function.a5
2832 (internal_args[0], internal_args[1], internal_args[2],
2833 internal_args[3], internal_args[4]));
2834 break;
2835 case 6:
2836 val = (XSUBR (fun)->function.a6
2837 (internal_args[0], internal_args[1], internal_args[2],
2838 internal_args[3], internal_args[4], internal_args[5]));
2839 break;
2840 case 7:
2841 val = (XSUBR (fun)->function.a7
2842 (internal_args[0], internal_args[1], internal_args[2],
2843 internal_args[3], internal_args[4], internal_args[5],
2844 internal_args[6]));
2845 break;
2847 case 8:
2848 val = (XSUBR (fun)->function.a8
2849 (internal_args[0], internal_args[1], internal_args[2],
2850 internal_args[3], internal_args[4], internal_args[5],
2851 internal_args[6], internal_args[7]));
2852 break;
2854 default:
2856 /* If a subr takes more than 8 arguments without using MANY
2857 or UNEVALLED, we need to extend this function to support it.
2858 Until this is done, there is no way to call the function. */
2859 emacs_abort ();
2863 else if (COMPILEDP (fun))
2864 val = funcall_lambda (fun, numargs, args + 1);
2865 else
2867 if (NILP (fun))
2868 xsignal1 (Qvoid_function, original_fun);
2869 if (!CONSP (fun))
2870 xsignal1 (Qinvalid_function, original_fun);
2871 funcar = XCAR (fun);
2872 if (!SYMBOLP (funcar))
2873 xsignal1 (Qinvalid_function, original_fun);
2874 if (EQ (funcar, Qlambda)
2875 || EQ (funcar, Qclosure))
2876 val = funcall_lambda (fun, numargs, args + 1);
2877 else if (EQ (funcar, Qautoload))
2879 Fautoload_do_load (fun, original_fun, Qnil);
2880 check_cons_list ();
2881 goto retry;
2883 else
2884 xsignal1 (Qinvalid_function, original_fun);
2886 check_cons_list ();
2887 lisp_eval_depth--;
2888 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2889 val = call_debugger (list2 (Qexit, val));
2890 specpdl_ptr--;
2891 return val;
2894 static Lisp_Object
2895 apply_lambda (Lisp_Object fun, Lisp_Object args)
2897 Lisp_Object args_left;
2898 ptrdiff_t i;
2899 EMACS_INT numargs;
2900 register Lisp_Object *arg_vector;
2901 struct gcpro gcpro1, gcpro2, gcpro3;
2902 register Lisp_Object tem;
2903 USE_SAFE_ALLOCA;
2905 numargs = XFASTINT (Flength (args));
2906 SAFE_ALLOCA_LISP (arg_vector, numargs);
2907 args_left = args;
2909 GCPRO3 (*arg_vector, args_left, fun);
2910 gcpro1.nvars = 0;
2912 for (i = 0; i < numargs; )
2914 tem = Fcar (args_left), args_left = Fcdr (args_left);
2915 tem = eval_sub (tem);
2916 arg_vector[i++] = tem;
2917 gcpro1.nvars = i;
2920 UNGCPRO;
2922 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2923 set_backtrace_nargs (specpdl_ptr - 1, i);
2924 tem = funcall_lambda (fun, numargs, arg_vector);
2926 /* Do the debug-on-exit now, while arg_vector still exists. */
2927 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2929 /* Don't do it again when we return to eval. */
2930 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2931 tem = call_debugger (list2 (Qexit, tem));
2933 SAFE_FREE ();
2934 return tem;
2937 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2938 and return the result of evaluation.
2939 FUN must be either a lambda-expression or a compiled-code object. */
2941 static Lisp_Object
2942 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2943 register Lisp_Object *arg_vector)
2945 Lisp_Object val, syms_left, next, lexenv;
2946 ptrdiff_t count = SPECPDL_INDEX ();
2947 ptrdiff_t i;
2948 bool optional, rest;
2950 if (CONSP (fun))
2952 if (EQ (XCAR (fun), Qclosure))
2954 fun = XCDR (fun); /* Drop `closure'. */
2955 lexenv = XCAR (fun);
2956 CHECK_LIST_CONS (fun, fun);
2958 else
2959 lexenv = Qnil;
2960 syms_left = XCDR (fun);
2961 if (CONSP (syms_left))
2962 syms_left = XCAR (syms_left);
2963 else
2964 xsignal1 (Qinvalid_function, fun);
2966 else if (COMPILEDP (fun))
2968 syms_left = AREF (fun, COMPILED_ARGLIST);
2969 if (INTEGERP (syms_left))
2970 /* A byte-code object with a non-nil `push args' slot means we
2971 shouldn't bind any arguments, instead just call the byte-code
2972 interpreter directly; it will push arguments as necessary.
2974 Byte-code objects with either a non-existent, or a nil value for
2975 the `push args' slot (the default), have dynamically-bound
2976 arguments, and use the argument-binding code below instead (as do
2977 all interpreted functions, even lexically bound ones). */
2979 /* If we have not actually read the bytecode string
2980 and constants vector yet, fetch them from the file. */
2981 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2982 Ffetch_bytecode (fun);
2983 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2984 AREF (fun, COMPILED_CONSTANTS),
2985 AREF (fun, COMPILED_STACK_DEPTH),
2986 syms_left,
2987 nargs, arg_vector);
2989 lexenv = Qnil;
2991 else
2992 emacs_abort ();
2994 i = optional = rest = 0;
2995 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2997 QUIT;
2999 next = XCAR (syms_left);
3000 if (!SYMBOLP (next))
3001 xsignal1 (Qinvalid_function, fun);
3003 if (EQ (next, Qand_rest))
3004 rest = 1;
3005 else if (EQ (next, Qand_optional))
3006 optional = 1;
3007 else
3009 Lisp_Object arg;
3010 if (rest)
3012 arg = Flist (nargs - i, &arg_vector[i]);
3013 i = nargs;
3015 else if (i < nargs)
3016 arg = arg_vector[i++];
3017 else if (!optional)
3018 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3019 else
3020 arg = Qnil;
3022 /* Bind the argument. */
3023 if (!NILP (lexenv) && SYMBOLP (next))
3024 /* Lexically bind NEXT by adding it to the lexenv alist. */
3025 lexenv = Fcons (Fcons (next, arg), lexenv);
3026 else
3027 /* Dynamically bind NEXT. */
3028 specbind (next, arg);
3032 if (!NILP (syms_left))
3033 xsignal1 (Qinvalid_function, fun);
3034 else if (i < nargs)
3035 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3037 if (!EQ (lexenv, Vinternal_interpreter_environment))
3038 /* Instantiate a new lexical environment. */
3039 specbind (Qinternal_interpreter_environment, lexenv);
3041 if (CONSP (fun))
3042 val = Fprogn (XCDR (XCDR (fun)));
3043 else
3045 /* If we have not actually read the bytecode string
3046 and constants vector yet, fetch them from the file. */
3047 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3048 Ffetch_bytecode (fun);
3049 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3050 AREF (fun, COMPILED_CONSTANTS),
3051 AREF (fun, COMPILED_STACK_DEPTH),
3052 Qnil, 0, 0);
3055 return unbind_to (count, val);
3058 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3059 1, 1, 0,
3060 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3061 (Lisp_Object object)
3063 Lisp_Object tem;
3065 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3067 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3068 if (!CONSP (tem))
3070 tem = AREF (object, COMPILED_BYTECODE);
3071 if (CONSP (tem) && STRINGP (XCAR (tem)))
3072 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3073 else
3074 error ("Invalid byte code");
3076 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3077 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3079 return object;
3082 /* Return true if SYMBOL currently has a let-binding
3083 which was made in the buffer that is now current. */
3085 bool
3086 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3088 union specbinding *p;
3089 Lisp_Object buf = Fcurrent_buffer ();
3091 for (p = specpdl_ptr; p > specpdl; )
3092 if ((--p)->kind > SPECPDL_LET)
3094 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3095 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3096 if (symbol == let_bound_symbol
3097 && EQ (specpdl_where (p), buf))
3098 return 1;
3101 return 0;
3104 bool
3105 let_shadows_global_binding_p (Lisp_Object symbol)
3107 union specbinding *p;
3109 for (p = specpdl_ptr; p > specpdl; )
3110 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3111 return 1;
3113 return 0;
3116 /* `specpdl_ptr->symbol' is a field which describes which variable is
3117 let-bound, so it can be properly undone when we unbind_to.
3118 It can have the following two shapes:
3119 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3120 a symbol that is not buffer-local (at least at the time
3121 the let binding started). Note also that it should not be
3122 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3123 to record V2 here).
3124 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3125 variable SYMBOL which can be buffer-local. WHERE tells us
3126 which buffer is affected (or nil if the let-binding affects the
3127 global value of the variable) and BUFFER tells us which buffer was
3128 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3129 BUFFER did not yet have a buffer-local value). */
3131 void
3132 specbind (Lisp_Object symbol, Lisp_Object value)
3134 struct Lisp_Symbol *sym;
3136 CHECK_SYMBOL (symbol);
3137 sym = XSYMBOL (symbol);
3139 start:
3140 switch (sym->redirect)
3142 case SYMBOL_VARALIAS:
3143 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3144 case SYMBOL_PLAINVAL:
3145 /* The most common case is that of a non-constant symbol with a
3146 trivial value. Make that as fast as we can. */
3147 specpdl_ptr->let.kind = SPECPDL_LET;
3148 specpdl_ptr->let.symbol = symbol;
3149 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3150 grow_specpdl ();
3151 if (!sym->constant)
3152 SET_SYMBOL_VAL (sym, value);
3153 else
3154 set_internal (symbol, value, Qnil, 1);
3155 break;
3156 case SYMBOL_LOCALIZED:
3157 if (SYMBOL_BLV (sym)->frame_local)
3158 error ("Frame-local vars cannot be let-bound");
3159 case SYMBOL_FORWARDED:
3161 Lisp_Object ovalue = find_symbol_value (symbol);
3162 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3163 specpdl_ptr->let.symbol = symbol;
3164 specpdl_ptr->let.old_value = ovalue;
3165 specpdl_ptr->let.where = Fcurrent_buffer ();
3167 eassert (sym->redirect != SYMBOL_LOCALIZED
3168 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3170 if (sym->redirect == SYMBOL_LOCALIZED)
3172 if (!blv_found (SYMBOL_BLV (sym)))
3173 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3175 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3177 /* If SYMBOL is a per-buffer variable which doesn't have a
3178 buffer-local value here, make the `let' change the global
3179 value by changing the value of SYMBOL in all buffers not
3180 having their own value. This is consistent with what
3181 happens with other buffer-local variables. */
3182 if (NILP (Flocal_variable_p (symbol, Qnil)))
3184 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3185 grow_specpdl ();
3186 Fset_default (symbol, value);
3187 return;
3190 else
3191 specpdl_ptr->let.kind = SPECPDL_LET;
3193 grow_specpdl ();
3194 set_internal (symbol, value, Qnil, 1);
3195 break;
3197 default: emacs_abort ();
3201 /* Push unwind-protect entries of various types. */
3203 void
3204 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3206 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3207 specpdl_ptr->unwind.func = function;
3208 specpdl_ptr->unwind.arg = arg;
3209 grow_specpdl ();
3212 void
3213 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3215 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3216 specpdl_ptr->unwind_ptr.func = function;
3217 specpdl_ptr->unwind_ptr.arg = arg;
3218 grow_specpdl ();
3221 void
3222 record_unwind_protect_int (void (*function) (int), int arg)
3224 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3225 specpdl_ptr->unwind_int.func = function;
3226 specpdl_ptr->unwind_int.arg = arg;
3227 grow_specpdl ();
3230 void
3231 record_unwind_protect_void (void (*function) (void))
3233 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3234 specpdl_ptr->unwind_void.func = function;
3235 grow_specpdl ();
3238 static void
3239 do_nothing (void)
3242 /* Push an unwind-protect entry that does nothing, so that
3243 set_unwind_protect_ptr can overwrite it later. */
3245 void
3246 record_unwind_protect_nothing (void)
3248 record_unwind_protect_void (do_nothing);
3251 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3252 It need not be at the top of the stack. */
3254 void
3255 clear_unwind_protect (ptrdiff_t count)
3257 union specbinding *p = specpdl + count;
3258 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3259 p->unwind_void.func = do_nothing;
3262 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3263 It need not be at the top of the stack. Discard the entry's
3264 previous value without invoking it. */
3266 void
3267 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3269 union specbinding *p = specpdl + count;
3270 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3271 p->unwind_ptr.func = func;
3272 p->unwind_ptr.arg = arg;
3275 /* Pop and execute entries from the unwind-protect stack until the
3276 depth COUNT is reached. Return VALUE. */
3278 Lisp_Object
3279 unbind_to (ptrdiff_t count, Lisp_Object value)
3281 Lisp_Object quitf = Vquit_flag;
3282 struct gcpro gcpro1, gcpro2;
3284 GCPRO2 (value, quitf);
3285 Vquit_flag = Qnil;
3287 while (specpdl_ptr != specpdl + count)
3289 /* Decrement specpdl_ptr before we do the work to unbind it, so
3290 that an error in unbinding won't try to unbind the same entry
3291 again. Take care to copy any parts of the binding needed
3292 before invoking any code that can make more bindings. */
3294 specpdl_ptr--;
3296 switch (specpdl_ptr->kind)
3298 case SPECPDL_UNWIND:
3299 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3300 break;
3301 case SPECPDL_UNWIND_PTR:
3302 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3303 break;
3304 case SPECPDL_UNWIND_INT:
3305 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3306 break;
3307 case SPECPDL_UNWIND_VOID:
3308 specpdl_ptr->unwind_void.func ();
3309 break;
3310 case SPECPDL_LET:
3311 /* If variable has a trivial value (no forwarding), we can
3312 just set it. No need to check for constant symbols here,
3313 since that was already done by specbind. */
3314 if (XSYMBOL (specpdl_symbol (specpdl_ptr))->redirect
3315 == SYMBOL_PLAINVAL)
3316 SET_SYMBOL_VAL (XSYMBOL (specpdl_symbol (specpdl_ptr)),
3317 specpdl_old_value (specpdl_ptr));
3318 else
3319 /* NOTE: we only ever come here if make_local_foo was used for
3320 the first time on this var within this let. */
3321 Fset_default (specpdl_symbol (specpdl_ptr),
3322 specpdl_old_value (specpdl_ptr));
3323 break;
3324 case SPECPDL_BACKTRACE:
3325 break;
3326 case SPECPDL_LET_LOCAL:
3327 case SPECPDL_LET_DEFAULT:
3328 { /* If the symbol is a list, it is really (SYMBOL WHERE
3329 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3330 frame. If WHERE is a buffer or frame, this indicates we
3331 bound a variable that had a buffer-local or frame-local
3332 binding. WHERE nil means that the variable had the default
3333 value when it was bound. CURRENT-BUFFER is the buffer that
3334 was current when the variable was bound. */
3335 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3336 Lisp_Object where = specpdl_where (specpdl_ptr);
3337 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3338 eassert (BUFFERP (where));
3340 if (specpdl_ptr->kind == SPECPDL_LET_DEFAULT)
3341 Fset_default (symbol, old_value);
3342 /* If this was a local binding, reset the value in the appropriate
3343 buffer, but only if that buffer's binding still exists. */
3344 else if (!NILP (Flocal_variable_p (symbol, where)))
3345 set_internal (symbol, old_value, where, 1);
3347 break;
3351 if (NILP (Vquit_flag) && !NILP (quitf))
3352 Vquit_flag = quitf;
3354 UNGCPRO;
3355 return value;
3358 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3359 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3360 A special variable is one that will be bound dynamically, even in a
3361 context where binding is lexical by default. */)
3362 (Lisp_Object symbol)
3364 CHECK_SYMBOL (symbol);
3365 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3369 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3370 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3371 The debugger is entered when that frame exits, if the flag is non-nil. */)
3372 (Lisp_Object level, Lisp_Object flag)
3374 union specbinding *pdl = backtrace_top ();
3375 register EMACS_INT i;
3377 CHECK_NUMBER (level);
3379 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3380 pdl = backtrace_next (pdl);
3382 if (backtrace_p (pdl))
3383 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3385 return flag;
3388 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3389 doc: /* Print a trace of Lisp function calls currently active.
3390 Output stream used is value of `standard-output'. */)
3391 (void)
3393 union specbinding *pdl = backtrace_top ();
3394 Lisp_Object tem;
3395 Lisp_Object old_print_level = Vprint_level;
3397 if (NILP (Vprint_level))
3398 XSETFASTINT (Vprint_level, 8);
3400 while (backtrace_p (pdl))
3402 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3403 if (backtrace_nargs (pdl) == UNEVALLED)
3405 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3406 Qnil);
3407 write_string ("\n", -1);
3409 else
3411 tem = backtrace_function (pdl);
3412 Fprin1 (tem, Qnil); /* This can QUIT. */
3413 write_string ("(", -1);
3415 ptrdiff_t i;
3416 for (i = 0; i < backtrace_nargs (pdl); i++)
3418 if (i) write_string (" ", -1);
3419 Fprin1 (backtrace_args (pdl)[i], Qnil);
3422 write_string (")\n", -1);
3424 pdl = backtrace_next (pdl);
3427 Vprint_level = old_print_level;
3428 return Qnil;
3431 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3432 doc: /* Return the function and arguments NFRAMES up from current execution point.
3433 If that frame has not evaluated the arguments yet (or is a special form),
3434 the value is (nil FUNCTION ARG-FORMS...).
3435 If that frame has evaluated its arguments and called its function already,
3436 the value is (t FUNCTION ARG-VALUES...).
3437 A &rest arg is represented as the tail of the list ARG-VALUES.
3438 FUNCTION is whatever was supplied as car of evaluated list,
3439 or a lambda expression for macro calls.
3440 If NFRAMES is more than the number of frames, the value is nil. */)
3441 (Lisp_Object nframes)
3443 union specbinding *pdl = backtrace_top ();
3444 register EMACS_INT i;
3446 CHECK_NATNUM (nframes);
3448 /* Find the frame requested. */
3449 for (i = 0; backtrace_p (pdl) && i < XFASTINT (nframes); i++)
3450 pdl = backtrace_next (pdl);
3452 if (!backtrace_p (pdl))
3453 return Qnil;
3454 if (backtrace_nargs (pdl) == UNEVALLED)
3455 return Fcons (Qnil,
3456 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3457 else
3459 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3461 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3466 void
3467 mark_specpdl (void)
3469 union specbinding *pdl;
3470 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3472 switch (pdl->kind)
3474 case SPECPDL_UNWIND:
3475 mark_object (specpdl_arg (pdl));
3476 break;
3478 case SPECPDL_BACKTRACE:
3480 ptrdiff_t nargs = backtrace_nargs (pdl);
3481 mark_object (backtrace_function (pdl));
3482 if (nargs == UNEVALLED)
3483 nargs = 1;
3484 while (nargs--)
3485 mark_object (backtrace_args (pdl)[nargs]);
3487 break;
3489 case SPECPDL_LET_DEFAULT:
3490 case SPECPDL_LET_LOCAL:
3491 mark_object (specpdl_where (pdl));
3492 /* Fall through. */
3493 case SPECPDL_LET:
3494 mark_object (specpdl_symbol (pdl));
3495 mark_object (specpdl_old_value (pdl));
3496 break;
3501 void
3502 get_backtrace (Lisp_Object array)
3504 union specbinding *pdl = backtrace_next (backtrace_top ());
3505 ptrdiff_t i = 0, asize = ASIZE (array);
3507 /* Copy the backtrace contents into working memory. */
3508 for (; i < asize; i++)
3510 if (backtrace_p (pdl))
3512 ASET (array, i, backtrace_function (pdl));
3513 pdl = backtrace_next (pdl);
3515 else
3516 ASET (array, i, Qnil);
3520 Lisp_Object backtrace_top_function (void)
3522 union specbinding *pdl = backtrace_top ();
3523 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3526 void
3527 syms_of_eval (void)
3529 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3530 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3531 If Lisp code tries to increase the total number past this amount,
3532 an error is signaled.
3533 You can safely use a value considerably larger than the default value,
3534 if that proves inconveniently small. However, if you increase it too far,
3535 Emacs could run out of memory trying to make the stack bigger. */);
3537 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3538 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3540 This limit serves to catch infinite recursions for you before they cause
3541 actual stack overflow in C, which would be fatal for Emacs.
3542 You can safely make it considerably larger than its default value,
3543 if that proves inconveniently small. However, if you increase it too far,
3544 Emacs could overflow the real C stack, and crash. */);
3546 DEFVAR_LISP ("quit-flag", Vquit_flag,
3547 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3548 If the value is t, that means do an ordinary quit.
3549 If the value equals `throw-on-input', that means quit by throwing
3550 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3551 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3552 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3553 Vquit_flag = Qnil;
3555 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3556 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3557 Note that `quit-flag' will still be set by typing C-g,
3558 so a quit will be signaled as soon as `inhibit-quit' is nil.
3559 To prevent this happening, set `quit-flag' to nil
3560 before making `inhibit-quit' nil. */);
3561 Vinhibit_quit = Qnil;
3563 DEFSYM (Qinhibit_quit, "inhibit-quit");
3564 DEFSYM (Qautoload, "autoload");
3565 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3566 DEFSYM (Qmacro, "macro");
3567 DEFSYM (Qdeclare, "declare");
3569 /* Note that the process handling also uses Qexit, but we don't want
3570 to staticpro it twice, so we just do it here. */
3571 DEFSYM (Qexit, "exit");
3573 DEFSYM (Qinteractive, "interactive");
3574 DEFSYM (Qcommandp, "commandp");
3575 DEFSYM (Qand_rest, "&rest");
3576 DEFSYM (Qand_optional, "&optional");
3577 DEFSYM (Qclosure, "closure");
3578 DEFSYM (Qdebug, "debug");
3580 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3581 doc: /* Non-nil means never enter the debugger.
3582 Normally set while the debugger is already active, to avoid recursive
3583 invocations. */);
3584 Vinhibit_debugger = Qnil;
3586 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3587 doc: /* Non-nil means enter debugger if an error is signaled.
3588 Does not apply to errors handled by `condition-case' or those
3589 matched by `debug-ignored-errors'.
3590 If the value is a list, an error only means to enter the debugger
3591 if one of its condition symbols appears in the list.
3592 When you evaluate an expression interactively, this variable
3593 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3594 The command `toggle-debug-on-error' toggles this.
3595 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3596 Vdebug_on_error = Qnil;
3598 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3599 doc: /* List of errors for which the debugger should not be called.
3600 Each element may be a condition-name or a regexp that matches error messages.
3601 If any element applies to a given error, that error skips the debugger
3602 and just returns to top level.
3603 This overrides the variable `debug-on-error'.
3604 It does not apply to errors handled by `condition-case'. */);
3605 Vdebug_ignored_errors = Qnil;
3607 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3608 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3609 Does not apply if quit is handled by a `condition-case'. */);
3610 debug_on_quit = 0;
3612 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3613 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3615 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3616 doc: /* Non-nil means debugger may continue execution.
3617 This is nil when the debugger is called under circumstances where it
3618 might not be safe to continue. */);
3619 debugger_may_continue = 1;
3621 DEFVAR_LISP ("debugger", Vdebugger,
3622 doc: /* Function to call to invoke debugger.
3623 If due to frame exit, args are `exit' and the value being returned;
3624 this function's value will be returned instead of that.
3625 If due to error, args are `error' and a list of the args to `signal'.
3626 If due to `apply' or `funcall' entry, one arg, `lambda'.
3627 If due to `eval' entry, one arg, t. */);
3628 Vdebugger = Qnil;
3630 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3631 doc: /* If non-nil, this is a function for `signal' to call.
3632 It receives the same arguments that `signal' was given.
3633 The Edebug package uses this to regain control. */);
3634 Vsignal_hook_function = Qnil;
3636 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3637 doc: /* Non-nil means call the debugger regardless of condition handlers.
3638 Note that `debug-on-error', `debug-on-quit' and friends
3639 still determine whether to handle the particular condition. */);
3640 Vdebug_on_signal = Qnil;
3642 /* When lexical binding is being used,
3643 Vinternal_interpreter_environment is non-nil, and contains an alist
3644 of lexically-bound variable, or (t), indicating an empty
3645 environment. The lisp name of this variable would be
3646 `internal-interpreter-environment' if it weren't hidden.
3647 Every element of this list can be either a cons (VAR . VAL)
3648 specifying a lexical binding, or a single symbol VAR indicating
3649 that this variable should use dynamic scoping. */
3650 DEFSYM (Qinternal_interpreter_environment,
3651 "internal-interpreter-environment");
3652 DEFVAR_LISP ("internal-interpreter-environment",
3653 Vinternal_interpreter_environment,
3654 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3655 When lexical binding is not being used, this variable is nil.
3656 A value of `(t)' indicates an empty environment, otherwise it is an
3657 alist of active lexical bindings. */);
3658 Vinternal_interpreter_environment = Qnil;
3659 /* Don't export this variable to Elisp, so no one can mess with it
3660 (Just imagine if someone makes it buffer-local). */
3661 Funintern (Qinternal_interpreter_environment, Qnil);
3663 DEFSYM (Vrun_hooks, "run-hooks");
3665 staticpro (&Vautoload_queue);
3666 Vautoload_queue = Qnil;
3667 staticpro (&Vsignaling_function);
3668 Vsignaling_function = Qnil;
3670 inhibit_lisp_code = Qnil;
3672 defsubr (&Sor);
3673 defsubr (&Sand);
3674 defsubr (&Sif);
3675 defsubr (&Scond);
3676 defsubr (&Sprogn);
3677 defsubr (&Sprog1);
3678 defsubr (&Sprog2);
3679 defsubr (&Ssetq);
3680 defsubr (&Squote);
3681 defsubr (&Sfunction);
3682 defsubr (&Sdefvar);
3683 defsubr (&Sdefvaralias);
3684 defsubr (&Sdefconst);
3685 defsubr (&Smake_var_non_special);
3686 defsubr (&Slet);
3687 defsubr (&SletX);
3688 defsubr (&Swhile);
3689 defsubr (&Smacroexpand);
3690 defsubr (&Scatch);
3691 defsubr (&Sthrow);
3692 defsubr (&Sunwind_protect);
3693 defsubr (&Scondition_case);
3694 defsubr (&Ssignal);
3695 defsubr (&Scommandp);
3696 defsubr (&Sautoload);
3697 defsubr (&Sautoload_do_load);
3698 defsubr (&Seval);
3699 defsubr (&Sapply);
3700 defsubr (&Sfuncall);
3701 defsubr (&Srun_hooks);
3702 defsubr (&Srun_hook_with_args);
3703 defsubr (&Srun_hook_with_args_until_success);
3704 defsubr (&Srun_hook_with_args_until_failure);
3705 defsubr (&Srun_hook_wrapped);
3706 defsubr (&Sfetch_bytecode);
3707 defsubr (&Sbacktrace_debug);
3708 defsubr (&Sbacktrace);
3709 defsubr (&Sbacktrace_frame);
3710 defsubr (&Sspecial_variable_p);
3711 defsubr (&Sfunctionp);