Merge from trunk
[emacs.git] / src / eval.c
blob9f90e6df4b5044110c2ebfb45bfbad00c52a7e12
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <setjmp.h>
22 #include "lisp.h"
23 #include "blockinput.h"
24 #include "commands.h"
25 #include "keyboard.h"
26 #include "dispextern.h"
27 #include "frame.h" /* For XFRAME. */
29 #if HAVE_X_WINDOWS
30 #include "xterm.h"
31 #endif
33 /* This definition is duplicated in alloc.c and keyboard.c. */
34 /* Putting it in lisp.h makes cc bomb out! */
36 struct backtrace
38 struct backtrace *next;
39 Lisp_Object *function;
40 Lisp_Object *args; /* Points to vector of args. */
41 #define NARGS_BITS (BITS_PER_INT - 2)
42 /* Let's not use size_t because we want to allow negative values (for
43 UNEVALLED). Also let's steal 2 bits so we save a word (or more for
44 alignment). In any case I doubt Emacs would survive a function call with
45 more than 500M arguments. */
46 int nargs : NARGS_BITS; /* Length of vector.
47 If nargs is UNEVALLED, args points
48 to slot holding list of unevalled args. */
49 char evalargs : 1;
50 /* Nonzero means call value of debugger when done with this operation. */
51 char debug_on_exit : 1;
54 struct backtrace *backtrace_list;
55 struct catchtag *catchlist;
57 #ifdef DEBUG_GCPRO
58 /* Count levels of GCPRO to detect failure to UNGCPRO. */
59 int gcpro_level;
60 #endif
62 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
63 Lisp_Object Qinhibit_quit;
64 Lisp_Object Qand_rest, Qand_optional;
65 Lisp_Object Qdebug_on_error;
66 Lisp_Object Qdeclare;
67 Lisp_Object Qinternal_interpreter_environment, Qclosure;
69 Lisp_Object Qdebug;
71 /* This holds either the symbol `run-hooks' or nil.
72 It is nil at an early stage of startup, and when Emacs
73 is shutting down. */
75 Lisp_Object Vrun_hooks;
77 /* Non-nil means record all fset's and provide's, to be undone
78 if the file being autoloaded is not fully loaded.
79 They are recorded by being consed onto the front of Vautoload_queue:
80 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
82 Lisp_Object Vautoload_queue;
84 /* Current number of specbindings allocated in specpdl. */
86 EMACS_INT specpdl_size;
88 /* Pointer to beginning of specpdl. */
90 struct specbinding *specpdl;
92 /* Pointer to first unused element in specpdl. */
94 struct specbinding *specpdl_ptr;
96 /* Depth in Lisp evaluations and function calls. */
98 EMACS_INT lisp_eval_depth;
100 /* The value of num_nonmacro_input_events as of the last time we
101 started to enter the debugger. If we decide to enter the debugger
102 again when this is still equal to num_nonmacro_input_events, then we
103 know that the debugger itself has an error, and we should just
104 signal the error instead of entering an infinite loop of debugger
105 invocations. */
107 int when_entered_debugger;
109 /* The function from which the last `signal' was called. Set in
110 Fsignal. */
112 Lisp_Object Vsignaling_function;
114 /* Set to non-zero while processing X events. Checked in Feval to
115 make sure the Lisp interpreter isn't called from a signal handler,
116 which is unsafe because the interpreter isn't reentrant. */
118 int handling_signal;
120 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
121 static Lisp_Object funcall_lambda (Lisp_Object, size_t, Lisp_Object *);
122 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
123 static int interactive_p (int);
125 void
126 init_eval_once (void)
128 specpdl_size = 50;
129 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
130 specpdl_ptr = specpdl;
131 /* Don't forget to update docs (lispref node "Local Variables"). */
132 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
133 max_lisp_eval_depth = 600;
135 Vrun_hooks = Qnil;
138 void
139 init_eval (void)
141 specpdl_ptr = specpdl;
142 catchlist = 0;
143 handlerlist = 0;
144 backtrace_list = 0;
145 Vquit_flag = Qnil;
146 debug_on_next_call = 0;
147 lisp_eval_depth = 0;
148 #ifdef DEBUG_GCPRO
149 gcpro_level = 0;
150 #endif
151 /* This is less than the initial value of num_nonmacro_input_events. */
152 when_entered_debugger = -1;
155 /* Unwind-protect function used by call_debugger. */
157 static Lisp_Object
158 restore_stack_limits (Lisp_Object data)
160 max_specpdl_size = XINT (XCAR (data));
161 max_lisp_eval_depth = XINT (XCDR (data));
162 return Qnil;
165 /* Call the Lisp debugger, giving it argument ARG. */
167 static Lisp_Object
168 call_debugger (Lisp_Object arg)
170 int debug_while_redisplaying;
171 int count = SPECPDL_INDEX ();
172 Lisp_Object val;
173 EMACS_INT old_max = max_specpdl_size;
175 /* Temporarily bump up the stack limits,
176 so the debugger won't run out of stack. */
178 max_specpdl_size += 1;
179 record_unwind_protect (restore_stack_limits,
180 Fcons (make_number (old_max),
181 make_number (max_lisp_eval_depth)));
182 max_specpdl_size = old_max;
184 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
185 max_lisp_eval_depth = lisp_eval_depth + 40;
187 if (SPECPDL_INDEX () + 100 > max_specpdl_size)
188 max_specpdl_size = SPECPDL_INDEX () + 100;
190 #ifdef HAVE_WINDOW_SYSTEM
191 if (display_hourglass_p)
192 cancel_hourglass ();
193 #endif
195 debug_on_next_call = 0;
196 when_entered_debugger = num_nonmacro_input_events;
198 /* Resetting redisplaying_p to 0 makes sure that debug output is
199 displayed if the debugger is invoked during redisplay. */
200 debug_while_redisplaying = redisplaying_p;
201 redisplaying_p = 0;
202 specbind (intern ("debugger-may-continue"),
203 debug_while_redisplaying ? Qnil : Qt);
204 specbind (Qinhibit_redisplay, Qnil);
205 specbind (Qdebug_on_error, Qnil);
207 #if 0 /* Binding this prevents execution of Lisp code during
208 redisplay, which necessarily leads to display problems. */
209 specbind (Qinhibit_eval_during_redisplay, Qt);
210 #endif
212 val = apply1 (Vdebugger, arg);
214 /* Interrupting redisplay and resuming it later is not safe under
215 all circumstances. So, when the debugger returns, abort the
216 interrupted redisplay by going back to the top-level. */
217 if (debug_while_redisplaying)
218 Ftop_level ();
220 return unbind_to (count, val);
223 static void
224 do_debug_on_call (Lisp_Object code)
226 debug_on_next_call = 0;
227 backtrace_list->debug_on_exit = 1;
228 call_debugger (Fcons (code, Qnil));
231 /* NOTE!!! Every function that can call EVAL must protect its args
232 and temporaries from garbage collection while it needs them.
233 The definition of `For' shows what you have to do. */
235 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
236 doc: /* Eval args until one of them yields non-nil, then return that value.
237 The remaining args are not evalled at all.
238 If all args return nil, return nil.
239 usage: (or CONDITIONS...) */)
240 (Lisp_Object args)
242 register Lisp_Object val = Qnil;
243 struct gcpro gcpro1;
245 GCPRO1 (args);
247 while (CONSP (args))
249 val = eval_sub (XCAR (args));
250 if (!NILP (val))
251 break;
252 args = XCDR (args);
255 UNGCPRO;
256 return val;
259 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
260 doc: /* Eval args until one of them yields nil, then return nil.
261 The remaining args are not evalled at all.
262 If no arg yields nil, return the last arg's value.
263 usage: (and CONDITIONS...) */)
264 (Lisp_Object args)
266 register Lisp_Object val = Qt;
267 struct gcpro gcpro1;
269 GCPRO1 (args);
271 while (CONSP (args))
273 val = eval_sub (XCAR (args));
274 if (NILP (val))
275 break;
276 args = XCDR (args);
279 UNGCPRO;
280 return val;
283 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
284 doc: /* If COND yields non-nil, do THEN, else do ELSE...
285 Returns the value of THEN or the value of the last of the ELSE's.
286 THEN must be one expression, but ELSE... can be zero or more expressions.
287 If COND yields nil, and there are no ELSE's, the value is nil.
288 usage: (if COND THEN ELSE...) */)
289 (Lisp_Object args)
291 register Lisp_Object cond;
292 struct gcpro gcpro1;
294 GCPRO1 (args);
295 cond = eval_sub (Fcar (args));
296 UNGCPRO;
298 if (!NILP (cond))
299 return eval_sub (Fcar (Fcdr (args)));
300 return Fprogn (Fcdr (Fcdr (args)));
303 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
304 doc: /* Try each clause until one succeeds.
305 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
306 and, if the value is non-nil, this clause succeeds:
307 then the expressions in BODY are evaluated and the last one's
308 value is the value of the cond-form.
309 If no clause succeeds, cond returns nil.
310 If a clause has one element, as in (CONDITION),
311 CONDITION's value if non-nil is returned from the cond-form.
312 usage: (cond CLAUSES...) */)
313 (Lisp_Object args)
315 register Lisp_Object clause, val;
316 struct gcpro gcpro1;
318 val = Qnil;
319 GCPRO1 (args);
320 while (!NILP (args))
322 clause = Fcar (args);
323 val = eval_sub (Fcar (clause));
324 if (!NILP (val))
326 if (!EQ (XCDR (clause), Qnil))
327 val = Fprogn (XCDR (clause));
328 break;
330 args = XCDR (args);
332 UNGCPRO;
334 return val;
337 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
338 doc: /* Eval BODY forms sequentially and return value of last one.
339 usage: (progn BODY...) */)
340 (Lisp_Object args)
342 register Lisp_Object val = Qnil;
343 struct gcpro gcpro1;
345 GCPRO1 (args);
347 while (CONSP (args))
349 val = eval_sub (XCAR (args));
350 args = XCDR (args);
353 UNGCPRO;
354 return val;
357 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
358 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
359 The value of FIRST is saved during the evaluation of the remaining args,
360 whose values are discarded.
361 usage: (prog1 FIRST BODY...) */)
362 (Lisp_Object args)
364 Lisp_Object val;
365 register Lisp_Object args_left;
366 struct gcpro gcpro1, gcpro2;
367 register int argnum = 0;
369 if (NILP (args))
370 return Qnil;
372 args_left = args;
373 val = Qnil;
374 GCPRO2 (args, val);
378 Lisp_Object tem = eval_sub (XCAR (args_left));
379 if (!(argnum++))
380 val = tem;
381 args_left = XCDR (args_left);
383 while (CONSP (args_left));
385 UNGCPRO;
386 return val;
389 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
390 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
391 The value of FORM2 is saved during the evaluation of the
392 remaining args, whose values are discarded.
393 usage: (prog2 FORM1 FORM2 BODY...) */)
394 (Lisp_Object args)
396 Lisp_Object val;
397 register Lisp_Object args_left;
398 struct gcpro gcpro1, gcpro2;
399 register int argnum = -1;
401 val = Qnil;
403 if (NILP (args))
404 return Qnil;
406 args_left = args;
407 val = Qnil;
408 GCPRO2 (args, val);
412 Lisp_Object tem = eval_sub (XCAR (args_left));
413 if (!(argnum++))
414 val = tem;
415 args_left = XCDR (args_left);
417 while (CONSP (args_left));
419 UNGCPRO;
420 return val;
423 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
424 doc: /* Set each SYM to the value of its VAL.
425 The symbols SYM are variables; they are literal (not evaluated).
426 The values VAL are expressions; they are evaluated.
427 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
428 The second VAL is not computed until after the first SYM is set, and so on;
429 each VAL can use the new value of variables set earlier in the `setq'.
430 The return value of the `setq' form is the value of the last VAL.
431 usage: (setq [SYM VAL]...) */)
432 (Lisp_Object args)
434 register Lisp_Object args_left;
435 register Lisp_Object val, sym, lex_binding;
436 struct gcpro gcpro1;
438 if (NILP (args))
439 return Qnil;
441 args_left = args;
442 GCPRO1 (args);
446 val = eval_sub (Fcar (Fcdr (args_left)));
447 sym = Fcar (args_left);
449 /* Like for eval_sub, we do not check declared_special here since
450 it's been done when let-binding. */
451 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
452 && SYMBOLP (sym)
453 && !NILP (lex_binding
454 = Fassq (sym, Vinternal_interpreter_environment)))
455 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
456 else
457 Fset (sym, val); /* SYM is dynamically bound. */
459 args_left = Fcdr (Fcdr (args_left));
461 while (!NILP(args_left));
463 UNGCPRO;
464 return val;
467 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
468 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
469 usage: (quote ARG) */)
470 (Lisp_Object args)
472 if (!NILP (Fcdr (args)))
473 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
474 return Fcar (args);
477 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
478 doc: /* Like `quote', but preferred for objects which are functions.
479 In byte compilation, `function' causes its argument to be compiled.
480 `quote' cannot do that.
481 usage: (function ARG) */)
482 (Lisp_Object args)
484 Lisp_Object quoted = XCAR (args);
486 if (!NILP (Fcdr (args)))
487 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
489 if (!NILP (Vinternal_interpreter_environment)
490 && CONSP (quoted)
491 && EQ (XCAR (quoted), Qlambda))
492 /* This is a lambda expression within a lexical environment;
493 return an interpreted closure instead of a simple lambda. */
494 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
495 XCDR (quoted)));
496 else
497 /* Simply quote the argument. */
498 return quoted;
502 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
503 doc: /* Return t if the containing function was run directly by user input.
504 This means that the function was called with `call-interactively'
505 \(which includes being called as the binding of a key)
506 and input is currently coming from the keyboard (not a keyboard macro),
507 and Emacs is not running in batch mode (`noninteractive' is nil).
509 The only known proper use of `interactive-p' is in deciding whether to
510 display a helpful message, or how to display it. If you're thinking
511 of using it for any other purpose, it is quite likely that you're
512 making a mistake. Think: what do you want to do when the command is
513 called from a keyboard macro?
515 To test whether your function was called with `call-interactively',
516 either (i) add an extra optional argument and give it an `interactive'
517 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
518 use `called-interactively-p'. */)
519 (void)
521 return interactive_p (1) ? Qt : Qnil;
525 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
526 doc: /* Return t if the containing function was called by `call-interactively'.
527 If KIND is `interactive', then only return t if the call was made
528 interactively by the user, i.e. not in `noninteractive' mode nor
529 when `executing-kbd-macro'.
530 If KIND is `any', on the other hand, it will return t for any kind of
531 interactive call, including being called as the binding of a key, or
532 from a keyboard macro, or in `noninteractive' mode.
534 The only known proper use of `interactive' for KIND is in deciding
535 whether to display a helpful message, or how to display it. If you're
536 thinking of using it for any other purpose, it is quite likely that
537 you're making a mistake. Think: what do you want to do when the
538 command is called from a keyboard macro?
540 This function is meant for implementing advice and other
541 function-modifying features. Instead of using this, it is sometimes
542 cleaner to give your function an extra optional argument whose
543 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
544 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
545 (Lisp_Object kind)
547 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
548 && interactive_p (1)) ? Qt : Qnil;
552 /* Return 1 if function in which this appears was called using
553 call-interactively.
555 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
556 called is a built-in. */
558 static int
559 interactive_p (int exclude_subrs_p)
561 struct backtrace *btp;
562 Lisp_Object fun;
564 btp = backtrace_list;
566 /* If this isn't a byte-compiled function, there may be a frame at
567 the top for Finteractive_p. If so, skip it. */
568 fun = Findirect_function (*btp->function, Qnil);
569 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
570 || XSUBR (fun) == &Scalled_interactively_p))
571 btp = btp->next;
573 /* If we're running an Emacs 18-style byte-compiled function, there
574 may be a frame for Fbytecode at the top level. In any version of
575 Emacs there can be Fbytecode frames for subexpressions evaluated
576 inside catch and condition-case. Skip past them.
578 If this isn't a byte-compiled function, then we may now be
579 looking at several frames for special forms. Skip past them. */
580 while (btp
581 && (EQ (*btp->function, Qbytecode)
582 || btp->nargs == UNEVALLED))
583 btp = btp->next;
585 /* `btp' now points at the frame of the innermost function that isn't
586 a special form, ignoring frames for Finteractive_p and/or
587 Fbytecode at the top. If this frame is for a built-in function
588 (such as load or eval-region) return nil. */
589 fun = Findirect_function (*btp->function, Qnil);
590 if (exclude_subrs_p && SUBRP (fun))
591 return 0;
593 /* `btp' points to the frame of a Lisp function that called interactive-p.
594 Return t if that function was called interactively. */
595 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
596 return 1;
597 return 0;
601 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
602 doc: /* Define NAME as a function.
603 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
604 See also the function `interactive'.
605 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
606 (Lisp_Object args)
608 register Lisp_Object fn_name;
609 register Lisp_Object defn;
611 fn_name = Fcar (args);
612 CHECK_SYMBOL (fn_name);
613 defn = Fcons (Qlambda, Fcdr (args));
614 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
615 defn = Ffunction (Fcons (defn, Qnil));
616 if (!NILP (Vpurify_flag))
617 defn = Fpurecopy (defn);
618 if (CONSP (XSYMBOL (fn_name)->function)
619 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
620 LOADHIST_ATTACH (Fcons (Qt, fn_name));
621 Ffset (fn_name, defn);
622 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
623 return fn_name;
626 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
627 doc: /* Define NAME as a macro.
628 The actual definition looks like
629 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
630 When the macro is called, as in (NAME ARGS...),
631 the function (lambda ARGLIST BODY...) is applied to
632 the list ARGS... as it appears in the expression,
633 and the result should be a form to be evaluated instead of the original.
635 DECL is a declaration, optional, which can specify how to indent
636 calls to this macro, how Edebug should handle it, and which argument
637 should be treated as documentation. It looks like this:
638 (declare SPECS...)
639 The elements can look like this:
640 (indent INDENT)
641 Set NAME's `lisp-indent-function' property to INDENT.
643 (debug DEBUG)
644 Set NAME's `edebug-form-spec' property to DEBUG. (This is
645 equivalent to writing a `def-edebug-spec' for the macro.)
647 (doc-string ELT)
648 Set NAME's `doc-string-elt' property to ELT.
650 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
651 (Lisp_Object args)
653 register Lisp_Object fn_name;
654 register Lisp_Object defn;
655 Lisp_Object lambda_list, doc, tail;
657 fn_name = Fcar (args);
658 CHECK_SYMBOL (fn_name);
659 lambda_list = Fcar (Fcdr (args));
660 tail = Fcdr (Fcdr (args));
662 doc = Qnil;
663 if (STRINGP (Fcar (tail)))
665 doc = XCAR (tail);
666 tail = XCDR (tail);
669 if (CONSP (Fcar (tail))
670 && EQ (Fcar (Fcar (tail)), Qdeclare))
672 if (!NILP (Vmacro_declaration_function))
674 struct gcpro gcpro1;
675 GCPRO1 (args);
676 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
677 UNGCPRO;
680 tail = Fcdr (tail);
683 if (NILP (doc))
684 tail = Fcons (lambda_list, tail);
685 else
686 tail = Fcons (lambda_list, Fcons (doc, tail));
688 defn = Fcons (Qlambda, tail);
689 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
690 defn = Ffunction (Fcons (defn, Qnil));
691 defn = Fcons (Qmacro, defn);
693 if (!NILP (Vpurify_flag))
694 defn = Fpurecopy (defn);
695 if (CONSP (XSYMBOL (fn_name)->function)
696 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
697 LOADHIST_ATTACH (Fcons (Qt, fn_name));
698 Ffset (fn_name, defn);
699 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
700 return fn_name;
704 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
705 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
706 Aliased variables always have the same value; setting one sets the other.
707 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
708 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
709 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
710 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
711 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
712 The return value is BASE-VARIABLE. */)
713 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
715 struct Lisp_Symbol *sym;
717 CHECK_SYMBOL (new_alias);
718 CHECK_SYMBOL (base_variable);
720 sym = XSYMBOL (new_alias);
722 if (sym->constant)
723 /* Not sure why, but why not? */
724 error ("Cannot make a constant an alias");
726 switch (sym->redirect)
728 case SYMBOL_FORWARDED:
729 error ("Cannot make an internal variable an alias");
730 case SYMBOL_LOCALIZED:
731 error ("Don't know how to make a localized variable an alias");
734 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
735 If n_a is bound, but b_v is not, set the value of b_v to n_a,
736 so that old-code that affects n_a before the aliasing is setup
737 still works. */
738 if (NILP (Fboundp (base_variable)))
739 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
742 struct specbinding *p;
744 for (p = specpdl_ptr - 1; p >= specpdl; p--)
745 if (p->func == NULL
746 && (EQ (new_alias,
747 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
748 error ("Don't know how to make a let-bound variable an alias");
751 sym->declared_special = 1;
752 sym->redirect = SYMBOL_VARALIAS;
753 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
754 sym->constant = SYMBOL_CONSTANT_P (base_variable);
755 LOADHIST_ATTACH (new_alias);
756 /* Even if docstring is nil: remove old docstring. */
757 Fput (new_alias, Qvariable_documentation, docstring);
759 return base_variable;
763 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
764 doc: /* Define SYMBOL as a variable, and return SYMBOL.
765 You are not required to define a variable in order to use it,
766 but the definition can supply documentation and an initial value
767 in a way that tags can recognize.
769 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
770 If SYMBOL is buffer-local, its default value is what is set;
771 buffer-local values are not affected.
772 INITVALUE and DOCSTRING are optional.
773 If DOCSTRING starts with *, this variable is identified as a user option.
774 This means that M-x set-variable recognizes it.
775 See also `user-variable-p'.
776 If INITVALUE is missing, SYMBOL's value is not set.
778 If SYMBOL has a local binding, then this form affects the local
779 binding. This is usually not what you want. Thus, if you need to
780 load a file defining variables, with this form or with `defconst' or
781 `defcustom', you should always load that file _outside_ any bindings
782 for these variables. \(`defconst' and `defcustom' behave similarly in
783 this respect.)
784 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
785 (Lisp_Object args)
787 register Lisp_Object sym, tem, tail;
789 sym = Fcar (args);
790 tail = Fcdr (args);
791 if (!NILP (Fcdr (Fcdr (tail))))
792 error ("Too many arguments");
794 tem = Fdefault_boundp (sym);
795 if (!NILP (tail))
797 /* Do it before evaluating the initial value, for self-references. */
798 XSYMBOL (sym)->declared_special = 1;
800 if (SYMBOL_CONSTANT_P (sym))
802 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
803 Lisp_Object tem1 = Fcar (tail);
804 if (! (CONSP (tem1)
805 && EQ (XCAR (tem1), Qquote)
806 && CONSP (XCDR (tem1))
807 && EQ (XCAR (XCDR (tem1)), sym)))
808 error ("Constant symbol `%s' specified in defvar",
809 SDATA (SYMBOL_NAME (sym)));
812 if (NILP (tem))
813 Fset_default (sym, eval_sub (Fcar (tail)));
814 else
815 { /* Check if there is really a global binding rather than just a let
816 binding that shadows the global unboundness of the var. */
817 volatile struct specbinding *pdl = specpdl_ptr;
818 while (--pdl >= specpdl)
820 if (EQ (pdl->symbol, sym) && !pdl->func
821 && EQ (pdl->old_value, Qunbound))
823 message_with_string ("Warning: defvar ignored because %s is let-bound",
824 SYMBOL_NAME (sym), 1);
825 break;
829 tail = Fcdr (tail);
830 tem = Fcar (tail);
831 if (!NILP (tem))
833 if (!NILP (Vpurify_flag))
834 tem = Fpurecopy (tem);
835 Fput (sym, Qvariable_documentation, tem);
837 LOADHIST_ATTACH (sym);
839 else if (!NILP (Vinternal_interpreter_environment)
840 && !XSYMBOL (sym)->declared_special)
841 /* A simple (defvar foo) with lexical scoping does "nothing" except
842 declare that var to be dynamically scoped *locally* (i.e. within
843 the current file or let-block). */
844 Vinternal_interpreter_environment =
845 Fcons (sym, Vinternal_interpreter_environment);
846 else
848 /* Simple (defvar <var>) should not count as a definition at all.
849 It could get in the way of other definitions, and unloading this
850 package could try to make the variable unbound. */
853 return sym;
856 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
857 doc: /* Define SYMBOL as a constant variable.
858 The intent is that neither programs nor users should ever change this value.
859 Always sets the value of SYMBOL to the result of evalling INITVALUE.
860 If SYMBOL is buffer-local, its default value is what is set;
861 buffer-local values are not affected.
862 DOCSTRING is optional.
864 If SYMBOL has a local binding, then this form sets the local binding's
865 value. However, you should normally not make local bindings for
866 variables defined with this form.
867 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
868 (Lisp_Object args)
870 register Lisp_Object sym, tem;
872 sym = Fcar (args);
873 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
874 error ("Too many arguments");
876 tem = eval_sub (Fcar (Fcdr (args)));
877 if (!NILP (Vpurify_flag))
878 tem = Fpurecopy (tem);
879 Fset_default (sym, tem);
880 XSYMBOL (sym)->declared_special = 1;
881 tem = Fcar (Fcdr (Fcdr (args)));
882 if (!NILP (tem))
884 if (!NILP (Vpurify_flag))
885 tem = Fpurecopy (tem);
886 Fput (sym, Qvariable_documentation, tem);
888 Fput (sym, Qrisky_local_variable, Qt);
889 LOADHIST_ATTACH (sym);
890 return sym;
893 /* Error handler used in Fuser_variable_p. */
894 static Lisp_Object
895 user_variable_p_eh (Lisp_Object ignore)
897 return Qnil;
900 static Lisp_Object
901 lisp_indirect_variable (Lisp_Object sym)
903 struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
904 XSETSYMBOL (sym, s);
905 return sym;
908 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
909 doc: /* Return t if VARIABLE is intended to be set and modified by users.
910 \(The alternative is a variable used internally in a Lisp program.)
911 A variable is a user variable if
912 \(1) the first character of its documentation is `*', or
913 \(2) it is customizable (its property list contains a non-nil value
914 of `standard-value' or `custom-autoload'), or
915 \(3) it is an alias for another user variable.
916 Return nil if VARIABLE is an alias and there is a loop in the
917 chain of symbols. */)
918 (Lisp_Object variable)
920 Lisp_Object documentation;
922 if (!SYMBOLP (variable))
923 return Qnil;
925 /* If indirect and there's an alias loop, don't check anything else. */
926 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
927 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
928 Qt, user_variable_p_eh)))
929 return Qnil;
931 while (1)
933 documentation = Fget (variable, Qvariable_documentation);
934 if (INTEGERP (documentation) && XINT (documentation) < 0)
935 return Qt;
936 if (STRINGP (documentation)
937 && ((unsigned char) SREF (documentation, 0) == '*'))
938 return Qt;
939 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
940 if (CONSP (documentation)
941 && STRINGP (XCAR (documentation))
942 && INTEGERP (XCDR (documentation))
943 && XINT (XCDR (documentation)) < 0)
944 return Qt;
945 /* Customizable? See `custom-variable-p'. */
946 if ((!NILP (Fget (variable, intern ("standard-value"))))
947 || (!NILP (Fget (variable, intern ("custom-autoload")))))
948 return Qt;
950 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
951 return Qnil;
953 /* An indirect variable? Let's follow the chain. */
954 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
958 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
959 doc: /* Bind variables according to VARLIST then eval BODY.
960 The value of the last form in BODY is returned.
961 Each element of VARLIST is a symbol (which is bound to nil)
962 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
963 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
964 usage: (let* VARLIST BODY...) */)
965 (Lisp_Object args)
967 Lisp_Object varlist, var, val, elt, lexenv;
968 int count = SPECPDL_INDEX ();
969 struct gcpro gcpro1, gcpro2, gcpro3;
971 GCPRO3 (args, elt, varlist);
973 lexenv = Vinternal_interpreter_environment;
975 varlist = Fcar (args);
976 while (CONSP (varlist))
978 QUIT;
980 elt = XCAR (varlist);
981 if (SYMBOLP (elt))
983 var = elt;
984 val = Qnil;
986 else if (! NILP (Fcdr (Fcdr (elt))))
987 signal_error ("`let' bindings can have only one value-form", elt);
988 else
990 var = Fcar (elt);
991 val = eval_sub (Fcar (Fcdr (elt)));
994 if (!NILP (lexenv) && SYMBOLP (var)
995 && !XSYMBOL (var)->declared_special
996 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
997 /* Lexically bind VAR by adding it to the interpreter's binding
998 alist. */
1000 Lisp_Object newenv
1001 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1002 if (EQ (Vinternal_interpreter_environment, lexenv))
1003 /* Save the old lexical environment on the specpdl stack,
1004 but only for the first lexical binding, since we'll never
1005 need to revert to one of the intermediate ones. */
1006 specbind (Qinternal_interpreter_environment, newenv);
1007 else
1008 Vinternal_interpreter_environment = newenv;
1010 else
1011 specbind (var, val);
1013 varlist = XCDR (varlist);
1016 UNGCPRO;
1018 val = Fprogn (Fcdr (args));
1020 return unbind_to (count, val);
1023 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1024 doc: /* Bind variables according to VARLIST then eval BODY.
1025 The value of the last form in BODY is returned.
1026 Each element of VARLIST is a symbol (which is bound to nil)
1027 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1028 All the VALUEFORMs are evalled before any symbols are bound.
1029 usage: (let VARLIST BODY...) */)
1030 (Lisp_Object args)
1032 Lisp_Object *temps, tem, lexenv;
1033 register Lisp_Object elt, varlist;
1034 int count = SPECPDL_INDEX ();
1035 register size_t argnum;
1036 struct gcpro gcpro1, gcpro2;
1037 USE_SAFE_ALLOCA;
1039 varlist = Fcar (args);
1041 /* Make space to hold the values to give the bound variables. */
1042 elt = Flength (varlist);
1043 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1045 /* Compute the values and store them in `temps'. */
1047 GCPRO2 (args, *temps);
1048 gcpro2.nvars = 0;
1050 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1052 QUIT;
1053 elt = XCAR (varlist);
1054 if (SYMBOLP (elt))
1055 temps [argnum++] = Qnil;
1056 else if (! NILP (Fcdr (Fcdr (elt))))
1057 signal_error ("`let' bindings can have only one value-form", elt);
1058 else
1059 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1060 gcpro2.nvars = argnum;
1062 UNGCPRO;
1064 lexenv = Vinternal_interpreter_environment;
1066 varlist = Fcar (args);
1067 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1069 Lisp_Object var;
1071 elt = XCAR (varlist);
1072 var = SYMBOLP (elt) ? elt : Fcar (elt);
1073 tem = temps[argnum++];
1075 if (!NILP (lexenv) && SYMBOLP (var)
1076 && !XSYMBOL (var)->declared_special
1077 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1078 /* Lexically bind VAR by adding it to the lexenv alist. */
1079 lexenv = Fcons (Fcons (var, tem), lexenv);
1080 else
1081 /* Dynamically bind VAR. */
1082 specbind (var, tem);
1085 if (!EQ (lexenv, Vinternal_interpreter_environment))
1086 /* Instantiate a new lexical environment. */
1087 specbind (Qinternal_interpreter_environment, lexenv);
1089 elt = Fprogn (Fcdr (args));
1090 SAFE_FREE ();
1091 return unbind_to (count, elt);
1094 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1095 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1096 The order of execution is thus TEST, BODY, TEST, BODY and so on
1097 until TEST returns nil.
1098 usage: (while TEST BODY...) */)
1099 (Lisp_Object args)
1101 Lisp_Object test, body;
1102 struct gcpro gcpro1, gcpro2;
1104 GCPRO2 (test, body);
1106 test = Fcar (args);
1107 body = Fcdr (args);
1108 while (!NILP (eval_sub (test)))
1110 QUIT;
1111 Fprogn (body);
1114 UNGCPRO;
1115 return Qnil;
1118 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1119 doc: /* Return result of expanding macros at top level of FORM.
1120 If FORM is not a macro call, it is returned unchanged.
1121 Otherwise, the macro is expanded and the expansion is considered
1122 in place of FORM. When a non-macro-call results, it is returned.
1124 The second optional arg ENVIRONMENT specifies an environment of macro
1125 definitions to shadow the loaded ones for use in file byte-compilation. */)
1126 (Lisp_Object form, Lisp_Object environment)
1128 /* With cleanups from Hallvard Furuseth. */
1129 register Lisp_Object expander, sym, def, tem;
1131 while (1)
1133 /* Come back here each time we expand a macro call,
1134 in case it expands into another macro call. */
1135 if (!CONSP (form))
1136 break;
1137 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1138 def = sym = XCAR (form);
1139 tem = Qnil;
1140 /* Trace symbols aliases to other symbols
1141 until we get a symbol that is not an alias. */
1142 while (SYMBOLP (def))
1144 QUIT;
1145 sym = def;
1146 tem = Fassq (sym, environment);
1147 if (NILP (tem))
1149 def = XSYMBOL (sym)->function;
1150 if (!EQ (def, Qunbound))
1151 continue;
1153 break;
1155 /* Right now TEM is the result from SYM in ENVIRONMENT,
1156 and if TEM is nil then DEF is SYM's function definition. */
1157 if (NILP (tem))
1159 /* SYM is not mentioned in ENVIRONMENT.
1160 Look at its function definition. */
1161 if (EQ (def, Qunbound) || !CONSP (def))
1162 /* Not defined or definition not suitable. */
1163 break;
1164 if (EQ (XCAR (def), Qautoload))
1166 /* Autoloading function: will it be a macro when loaded? */
1167 tem = Fnth (make_number (4), def);
1168 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1169 /* Yes, load it and try again. */
1171 struct gcpro gcpro1;
1172 GCPRO1 (form);
1173 do_autoload (def, sym);
1174 UNGCPRO;
1175 continue;
1177 else
1178 break;
1180 else if (!EQ (XCAR (def), Qmacro))
1181 break;
1182 else expander = XCDR (def);
1184 else
1186 expander = XCDR (tem);
1187 if (NILP (expander))
1188 break;
1190 form = apply1 (expander, XCDR (form));
1192 return form;
1195 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1196 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1197 TAG is evalled to get the tag to use; it must not be nil.
1199 Then the BODY is executed.
1200 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1201 If no throw happens, `catch' returns the value of the last BODY form.
1202 If a throw happens, it specifies the value to return from `catch'.
1203 usage: (catch TAG BODY...) */)
1204 (Lisp_Object args)
1206 register Lisp_Object tag;
1207 struct gcpro gcpro1;
1209 GCPRO1 (args);
1210 tag = eval_sub (Fcar (args));
1211 UNGCPRO;
1212 return internal_catch (tag, Fprogn, Fcdr (args));
1215 /* Set up a catch, then call C function FUNC on argument ARG.
1216 FUNC should return a Lisp_Object.
1217 This is how catches are done from within C code. */
1219 Lisp_Object
1220 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1222 /* This structure is made part of the chain `catchlist'. */
1223 struct catchtag c;
1225 /* Fill in the components of c, and put it on the list. */
1226 c.next = catchlist;
1227 c.tag = tag;
1228 c.val = Qnil;
1229 c.backlist = backtrace_list;
1230 c.handlerlist = handlerlist;
1231 c.lisp_eval_depth = lisp_eval_depth;
1232 c.pdlcount = SPECPDL_INDEX ();
1233 c.poll_suppress_count = poll_suppress_count;
1234 c.interrupt_input_blocked = interrupt_input_blocked;
1235 c.gcpro = gcprolist;
1236 c.byte_stack = byte_stack_list;
1237 catchlist = &c;
1239 /* Call FUNC. */
1240 if (! _setjmp (c.jmp))
1241 c.val = (*func) (arg);
1243 /* Throw works by a longjmp that comes right here. */
1244 catchlist = c.next;
1245 return c.val;
1248 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1249 jump to that CATCH, returning VALUE as the value of that catch.
1251 This is the guts Fthrow and Fsignal; they differ only in the way
1252 they choose the catch tag to throw to. A catch tag for a
1253 condition-case form has a TAG of Qnil.
1255 Before each catch is discarded, unbind all special bindings and
1256 execute all unwind-protect clauses made above that catch. Unwind
1257 the handler stack as we go, so that the proper handlers are in
1258 effect for each unwind-protect clause we run. At the end, restore
1259 some static info saved in CATCH, and longjmp to the location
1260 specified in the
1262 This is used for correct unwinding in Fthrow and Fsignal. */
1264 static void
1265 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1267 register int last_time;
1269 /* Save the value in the tag. */
1270 catch->val = value;
1272 /* Restore certain special C variables. */
1273 set_poll_suppress_count (catch->poll_suppress_count);
1274 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1275 handling_signal = 0;
1276 immediate_quit = 0;
1280 last_time = catchlist == catch;
1282 /* Unwind the specpdl stack, and then restore the proper set of
1283 handlers. */
1284 unbind_to (catchlist->pdlcount, Qnil);
1285 handlerlist = catchlist->handlerlist;
1286 catchlist = catchlist->next;
1288 while (! last_time);
1290 #if HAVE_X_WINDOWS
1291 /* If x_catch_errors was done, turn it off now.
1292 (First we give unbind_to a chance to do that.) */
1293 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1294 The catch must remain in effect during that delicate
1295 state. --lorentey */
1296 x_fully_uncatch_errors ();
1297 #endif
1298 #endif
1300 byte_stack_list = catch->byte_stack;
1301 gcprolist = catch->gcpro;
1302 #ifdef DEBUG_GCPRO
1303 gcpro_level = gcprolist ? gcprolist->level + 1 : gcpro_level = 0;
1304 #endif
1305 backtrace_list = catch->backlist;
1306 lisp_eval_depth = catch->lisp_eval_depth;
1308 _longjmp (catch->jmp, 1);
1311 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1312 doc: /* Throw to the catch for TAG and return VALUE from it.
1313 Both TAG and VALUE are evalled. */)
1314 (register Lisp_Object tag, Lisp_Object value)
1316 register struct catchtag *c;
1318 if (!NILP (tag))
1319 for (c = catchlist; c; c = c->next)
1321 if (EQ (c->tag, tag))
1322 unwind_to_catch (c, value);
1324 xsignal2 (Qno_catch, tag, value);
1328 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1329 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1330 If BODYFORM completes normally, its value is returned
1331 after executing the UNWINDFORMS.
1332 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1333 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1334 (Lisp_Object args)
1336 Lisp_Object val;
1337 int count = SPECPDL_INDEX ();
1339 record_unwind_protect (Fprogn, Fcdr (args));
1340 val = eval_sub (Fcar (args));
1341 return unbind_to (count, val);
1344 /* Chain of condition handlers currently in effect.
1345 The elements of this chain are contained in the stack frames
1346 of Fcondition_case and internal_condition_case.
1347 When an error is signaled (by calling Fsignal, below),
1348 this chain is searched for an element that applies. */
1350 struct handler *handlerlist;
1352 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1353 doc: /* Regain control when an error is signaled.
1354 Executes BODYFORM and returns its value if no error happens.
1355 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1356 where the BODY is made of Lisp expressions.
1358 A handler is applicable to an error
1359 if CONDITION-NAME is one of the error's condition names.
1360 If an error happens, the first applicable handler is run.
1362 The car of a handler may be a list of condition names
1363 instead of a single condition name. Then it handles all of them.
1365 When a handler handles an error, control returns to the `condition-case'
1366 and it executes the handler's BODY...
1367 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1368 \(If VAR is nil, the handler can't access that information.)
1369 Then the value of the last BODY form is returned from the `condition-case'
1370 expression.
1372 See also the function `signal' for more info.
1373 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1374 (Lisp_Object args)
1376 register Lisp_Object bodyform, handlers;
1377 volatile Lisp_Object var;
1379 var = Fcar (args);
1380 bodyform = Fcar (Fcdr (args));
1381 handlers = Fcdr (Fcdr (args));
1383 return internal_lisp_condition_case (var, bodyform, handlers);
1386 /* Like Fcondition_case, but the args are separate
1387 rather than passed in a list. Used by Fbyte_code. */
1389 Lisp_Object
1390 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1391 Lisp_Object handlers)
1393 Lisp_Object val;
1394 struct catchtag c;
1395 struct handler h;
1397 CHECK_SYMBOL (var);
1399 for (val = handlers; CONSP (val); val = XCDR (val))
1401 Lisp_Object tem;
1402 tem = XCAR (val);
1403 if (! (NILP (tem)
1404 || (CONSP (tem)
1405 && (SYMBOLP (XCAR (tem))
1406 || CONSP (XCAR (tem))))))
1407 error ("Invalid condition handler", tem);
1410 c.tag = Qnil;
1411 c.val = Qnil;
1412 c.backlist = backtrace_list;
1413 c.handlerlist = handlerlist;
1414 c.lisp_eval_depth = lisp_eval_depth;
1415 c.pdlcount = SPECPDL_INDEX ();
1416 c.poll_suppress_count = poll_suppress_count;
1417 c.interrupt_input_blocked = interrupt_input_blocked;
1418 c.gcpro = gcprolist;
1419 c.byte_stack = byte_stack_list;
1420 if (_setjmp (c.jmp))
1422 if (!NILP (h.var))
1423 specbind (h.var, c.val);
1424 val = Fprogn (Fcdr (h.chosen_clause));
1426 /* Note that this just undoes the binding of h.var; whoever
1427 longjumped to us unwound the stack to c.pdlcount before
1428 throwing. */
1429 unbind_to (c.pdlcount, Qnil);
1430 return val;
1432 c.next = catchlist;
1433 catchlist = &c;
1435 h.var = var;
1436 h.handler = handlers;
1437 h.next = handlerlist;
1438 h.tag = &c;
1439 handlerlist = &h;
1441 val = eval_sub (bodyform);
1442 catchlist = c.next;
1443 handlerlist = h.next;
1444 return val;
1447 /* Call the function BFUN with no arguments, catching errors within it
1448 according to HANDLERS. If there is an error, call HFUN with
1449 one argument which is the data that describes the error:
1450 (SIGNALNAME . DATA)
1452 HANDLERS can be a list of conditions to catch.
1453 If HANDLERS is Qt, catch all errors.
1454 If HANDLERS is Qerror, catch all errors
1455 but allow the debugger to run if that is enabled. */
1457 Lisp_Object
1458 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1459 Lisp_Object (*hfun) (Lisp_Object))
1461 Lisp_Object val;
1462 struct catchtag c;
1463 struct handler h;
1465 /* Since Fsignal will close off all calls to x_catch_errors,
1466 we will get the wrong results if some are not closed now. */
1467 #if HAVE_X_WINDOWS
1468 if (x_catching_errors ())
1469 abort ();
1470 #endif
1472 c.tag = Qnil;
1473 c.val = Qnil;
1474 c.backlist = backtrace_list;
1475 c.handlerlist = handlerlist;
1476 c.lisp_eval_depth = lisp_eval_depth;
1477 c.pdlcount = SPECPDL_INDEX ();
1478 c.poll_suppress_count = poll_suppress_count;
1479 c.interrupt_input_blocked = interrupt_input_blocked;
1480 c.gcpro = gcprolist;
1481 c.byte_stack = byte_stack_list;
1482 if (_setjmp (c.jmp))
1484 return (*hfun) (c.val);
1486 c.next = catchlist;
1487 catchlist = &c;
1488 h.handler = handlers;
1489 h.var = Qnil;
1490 h.next = handlerlist;
1491 h.tag = &c;
1492 handlerlist = &h;
1494 val = (*bfun) ();
1495 catchlist = c.next;
1496 handlerlist = h.next;
1497 return val;
1500 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1502 Lisp_Object
1503 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1504 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1506 Lisp_Object val;
1507 struct catchtag c;
1508 struct handler h;
1510 /* Since Fsignal will close off all calls to x_catch_errors,
1511 we will get the wrong results if some are not closed now. */
1512 #if HAVE_X_WINDOWS
1513 if (x_catching_errors ())
1514 abort ();
1515 #endif
1517 c.tag = Qnil;
1518 c.val = Qnil;
1519 c.backlist = backtrace_list;
1520 c.handlerlist = handlerlist;
1521 c.lisp_eval_depth = lisp_eval_depth;
1522 c.pdlcount = SPECPDL_INDEX ();
1523 c.poll_suppress_count = poll_suppress_count;
1524 c.interrupt_input_blocked = interrupt_input_blocked;
1525 c.gcpro = gcprolist;
1526 c.byte_stack = byte_stack_list;
1527 if (_setjmp (c.jmp))
1529 return (*hfun) (c.val);
1531 c.next = catchlist;
1532 catchlist = &c;
1533 h.handler = handlers;
1534 h.var = Qnil;
1535 h.next = handlerlist;
1536 h.tag = &c;
1537 handlerlist = &h;
1539 val = (*bfun) (arg);
1540 catchlist = c.next;
1541 handlerlist = h.next;
1542 return val;
1545 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1546 its arguments. */
1548 Lisp_Object
1549 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1550 Lisp_Object arg1,
1551 Lisp_Object arg2,
1552 Lisp_Object handlers,
1553 Lisp_Object (*hfun) (Lisp_Object))
1555 Lisp_Object val;
1556 struct catchtag c;
1557 struct handler h;
1559 /* Since Fsignal will close off all calls to x_catch_errors,
1560 we will get the wrong results if some are not closed now. */
1561 #if HAVE_X_WINDOWS
1562 if (x_catching_errors ())
1563 abort ();
1564 #endif
1566 c.tag = Qnil;
1567 c.val = Qnil;
1568 c.backlist = backtrace_list;
1569 c.handlerlist = handlerlist;
1570 c.lisp_eval_depth = lisp_eval_depth;
1571 c.pdlcount = SPECPDL_INDEX ();
1572 c.poll_suppress_count = poll_suppress_count;
1573 c.interrupt_input_blocked = interrupt_input_blocked;
1574 c.gcpro = gcprolist;
1575 c.byte_stack = byte_stack_list;
1576 if (_setjmp (c.jmp))
1578 return (*hfun) (c.val);
1580 c.next = catchlist;
1581 catchlist = &c;
1582 h.handler = handlers;
1583 h.var = Qnil;
1584 h.next = handlerlist;
1585 h.tag = &c;
1586 handlerlist = &h;
1588 val = (*bfun) (arg1, arg2);
1589 catchlist = c.next;
1590 handlerlist = h.next;
1591 return val;
1594 /* Like internal_condition_case but call BFUN with NARGS as first,
1595 and ARGS as second argument. */
1597 Lisp_Object
1598 internal_condition_case_n (Lisp_Object (*bfun) (size_t, Lisp_Object *),
1599 size_t nargs,
1600 Lisp_Object *args,
1601 Lisp_Object handlers,
1602 Lisp_Object (*hfun) (Lisp_Object))
1604 Lisp_Object val;
1605 struct catchtag c;
1606 struct handler h;
1608 /* Since Fsignal will close off all calls to x_catch_errors,
1609 we will get the wrong results if some are not closed now. */
1610 #if HAVE_X_WINDOWS
1611 if (x_catching_errors ())
1612 abort ();
1613 #endif
1615 c.tag = Qnil;
1616 c.val = Qnil;
1617 c.backlist = backtrace_list;
1618 c.handlerlist = handlerlist;
1619 c.lisp_eval_depth = lisp_eval_depth;
1620 c.pdlcount = SPECPDL_INDEX ();
1621 c.poll_suppress_count = poll_suppress_count;
1622 c.interrupt_input_blocked = interrupt_input_blocked;
1623 c.gcpro = gcprolist;
1624 c.byte_stack = byte_stack_list;
1625 if (_setjmp (c.jmp))
1627 return (*hfun) (c.val);
1629 c.next = catchlist;
1630 catchlist = &c;
1631 h.handler = handlers;
1632 h.var = Qnil;
1633 h.next = handlerlist;
1634 h.tag = &c;
1635 handlerlist = &h;
1637 val = (*bfun) (nargs, args);
1638 catchlist = c.next;
1639 handlerlist = h.next;
1640 return val;
1644 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object,
1645 Lisp_Object, Lisp_Object);
1646 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1647 Lisp_Object data);
1649 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1650 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1651 This function does not return.
1653 An error symbol is a symbol with an `error-conditions' property
1654 that is a list of condition names.
1655 A handler for any of those names will get to handle this signal.
1656 The symbol `error' should normally be one of them.
1658 DATA should be a list. Its elements are printed as part of the error message.
1659 See Info anchor `(elisp)Definition of signal' for some details on how this
1660 error message is constructed.
1661 If the signal is handled, DATA is made available to the handler.
1662 See also the function `condition-case'. */)
1663 (Lisp_Object error_symbol, Lisp_Object data)
1665 /* When memory is full, ERROR-SYMBOL is nil,
1666 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1667 That is a special case--don't do this in other situations. */
1668 Lisp_Object conditions;
1669 Lisp_Object string;
1670 Lisp_Object real_error_symbol
1671 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1672 register Lisp_Object clause = Qnil;
1673 struct handler *h;
1674 struct backtrace *bp;
1676 immediate_quit = handling_signal = 0;
1677 abort_on_gc = 0;
1678 if (gc_in_progress || waiting_for_input)
1679 abort ();
1681 #if 0 /* rms: I don't know why this was here,
1682 but it is surely wrong for an error that is handled. */
1683 #ifdef HAVE_WINDOW_SYSTEM
1684 if (display_hourglass_p)
1685 cancel_hourglass ();
1686 #endif
1687 #endif
1689 /* This hook is used by edebug. */
1690 if (! NILP (Vsignal_hook_function)
1691 && ! NILP (error_symbol))
1693 /* Edebug takes care of restoring these variables when it exits. */
1694 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1695 max_lisp_eval_depth = lisp_eval_depth + 20;
1697 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1698 max_specpdl_size = SPECPDL_INDEX () + 40;
1700 call2 (Vsignal_hook_function, error_symbol, data);
1703 conditions = Fget (real_error_symbol, Qerror_conditions);
1705 /* Remember from where signal was called. Skip over the frame for
1706 `signal' itself. If a frame for `error' follows, skip that,
1707 too. Don't do this when ERROR_SYMBOL is nil, because that
1708 is a memory-full error. */
1709 Vsignaling_function = Qnil;
1710 if (backtrace_list && !NILP (error_symbol))
1712 bp = backtrace_list->next;
1713 if (bp && bp->function && EQ (*bp->function, Qerror))
1714 bp = bp->next;
1715 if (bp && bp->function)
1716 Vsignaling_function = *bp->function;
1719 for (h = handlerlist; h; h = h->next)
1721 clause = find_handler_clause (h->handler, conditions,
1722 error_symbol, data);
1723 if (!NILP (clause))
1724 break;
1727 if (/* Don't run the debugger for a memory-full error.
1728 (There is no room in memory to do that!) */
1729 !NILP (error_symbol)
1730 && (!NILP (Vdebug_on_signal)
1731 /* If no handler is present now, try to run the debugger. */
1732 || NILP (clause)
1733 /* Special handler that means "print a message and run debugger
1734 if requested". */
1735 || EQ (h->handler, Qerror)))
1737 int debugger_called
1738 = maybe_call_debugger (conditions, error_symbol, data);
1739 /* We can't return values to code which signaled an error, but we
1740 can continue code which has signaled a quit. */
1741 if (debugger_called && EQ (real_error_symbol, Qquit))
1742 return Qnil;
1745 if (!NILP (clause))
1747 Lisp_Object unwind_data
1748 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1750 h->chosen_clause = clause;
1751 unwind_to_catch (h->tag, unwind_data);
1753 else
1755 if (catchlist != 0)
1756 Fthrow (Qtop_level, Qt);
1759 if (! NILP (error_symbol))
1760 data = Fcons (error_symbol, data);
1762 string = Ferror_message_string (data);
1763 fatal ("%s", SDATA (string), 0);
1766 /* Internal version of Fsignal that never returns.
1767 Used for anything but Qquit (which can return from Fsignal). */
1769 void
1770 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1772 Fsignal (error_symbol, data);
1773 abort ();
1776 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1778 void
1779 xsignal0 (Lisp_Object error_symbol)
1781 xsignal (error_symbol, Qnil);
1784 void
1785 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1787 xsignal (error_symbol, list1 (arg));
1790 void
1791 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1793 xsignal (error_symbol, list2 (arg1, arg2));
1796 void
1797 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1799 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1802 /* Signal `error' with message S, and additional arg ARG.
1803 If ARG is not a genuine list, make it a one-element list. */
1805 void
1806 signal_error (const char *s, Lisp_Object arg)
1808 Lisp_Object tortoise, hare;
1810 hare = tortoise = arg;
1811 while (CONSP (hare))
1813 hare = XCDR (hare);
1814 if (!CONSP (hare))
1815 break;
1817 hare = XCDR (hare);
1818 tortoise = XCDR (tortoise);
1820 if (EQ (hare, tortoise))
1821 break;
1824 if (!NILP (hare))
1825 arg = Fcons (arg, Qnil); /* Make it a list. */
1827 xsignal (Qerror, Fcons (build_string (s), arg));
1831 /* Return nonzero if LIST is a non-nil atom or
1832 a list containing one of CONDITIONS. */
1834 static int
1835 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1837 if (NILP (list))
1838 return 0;
1839 if (! CONSP (list))
1840 return 1;
1842 while (CONSP (conditions))
1844 Lisp_Object this, tail;
1845 this = XCAR (conditions);
1846 for (tail = list; CONSP (tail); tail = XCDR (tail))
1847 if (EQ (XCAR (tail), this))
1848 return 1;
1849 conditions = XCDR (conditions);
1851 return 0;
1854 /* Return 1 if an error with condition-symbols CONDITIONS,
1855 and described by SIGNAL-DATA, should skip the debugger
1856 according to debugger-ignored-errors. */
1858 static int
1859 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1861 Lisp_Object tail;
1862 int first_string = 1;
1863 Lisp_Object error_message;
1865 error_message = Qnil;
1866 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1868 if (STRINGP (XCAR (tail)))
1870 if (first_string)
1872 error_message = Ferror_message_string (data);
1873 first_string = 0;
1876 if (fast_string_match (XCAR (tail), error_message) >= 0)
1877 return 1;
1879 else
1881 Lisp_Object contail;
1883 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1884 if (EQ (XCAR (tail), XCAR (contail)))
1885 return 1;
1889 return 0;
1892 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1893 SIG and DATA describe the signal, as in find_handler_clause. */
1895 static int
1896 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1898 Lisp_Object combined_data;
1900 combined_data = Fcons (sig, data);
1902 if (
1903 /* Don't try to run the debugger with interrupts blocked.
1904 The editing loop would return anyway. */
1905 ! INPUT_BLOCKED_P
1906 /* Does user want to enter debugger for this kind of error? */
1907 && (EQ (sig, Qquit)
1908 ? debug_on_quit
1909 : wants_debugger (Vdebug_on_error, conditions))
1910 && ! skip_debugger (conditions, combined_data)
1911 /* RMS: What's this for? */
1912 && when_entered_debugger < num_nonmacro_input_events)
1914 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1915 return 1;
1918 return 0;
1921 /* Value of Qlambda means we have called debugger and user has continued.
1922 There are two ways to pass SIG and DATA:
1923 = SIG is the error symbol, and DATA is the rest of the data.
1924 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1925 This is for memory-full errors only.
1927 We need to increase max_specpdl_size temporarily around
1928 anything we do that can push on the specpdl, so as not to get
1929 a second error here in case we're handling specpdl overflow. */
1931 static Lisp_Object
1932 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions,
1933 Lisp_Object sig, Lisp_Object data)
1935 register Lisp_Object h;
1937 /* t is used by handlers for all conditions, set up by C code. */
1938 if (EQ (handlers, Qt))
1939 return Qt;
1941 /* error is used similarly, but means print an error message
1942 and run the debugger if that is enabled. */
1943 if (EQ (handlers, Qerror))
1944 return Qt;
1946 for (h = handlers; CONSP (h); h = XCDR (h))
1948 Lisp_Object handler = XCAR (h);
1949 Lisp_Object condit, tem;
1951 if (!CONSP (handler))
1952 continue;
1953 condit = XCAR (handler);
1954 /* Handle a single condition name in handler HANDLER. */
1955 if (SYMBOLP (condit))
1957 tem = Fmemq (Fcar (handler), conditions);
1958 if (!NILP (tem))
1959 return handler;
1961 /* Handle a list of condition names in handler HANDLER. */
1962 else if (CONSP (condit))
1964 Lisp_Object tail;
1965 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1967 tem = Fmemq (XCAR (tail), conditions);
1968 if (!NILP (tem))
1969 return handler;
1974 return Qnil;
1978 /* Dump an error message; called like vprintf. */
1979 void
1980 verror (const char *m, va_list ap)
1982 char buf[200];
1983 EMACS_INT size = 200;
1984 int mlen;
1985 char *buffer = buf;
1986 int allocated = 0;
1987 Lisp_Object string;
1989 mlen = strlen (m);
1991 while (1)
1993 EMACS_INT used;
1994 used = doprnt (buffer, size, m, m + mlen, ap);
1995 if (used < size)
1996 break;
1997 size *= 2;
1998 if (allocated)
1999 buffer = (char *) xrealloc (buffer, size);
2000 else
2002 buffer = (char *) xmalloc (size);
2003 allocated = 1;
2007 string = build_string (buffer);
2008 if (allocated)
2009 xfree (buffer);
2011 xsignal1 (Qerror, string);
2015 /* Dump an error message; called like printf. */
2017 /* VARARGS 1 */
2018 void
2019 error (const char *m, ...)
2021 va_list ap;
2022 va_start (ap, m);
2023 verror (m, ap);
2024 va_end (ap);
2027 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2028 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2029 This means it contains a description for how to read arguments to give it.
2030 The value is nil for an invalid function or a symbol with no function
2031 definition.
2033 Interactively callable functions include strings and vectors (treated
2034 as keyboard macros), lambda-expressions that contain a top-level call
2035 to `interactive', autoload definitions made by `autoload' with non-nil
2036 fourth argument, and some of the built-in functions of Lisp.
2038 Also, a symbol satisfies `commandp' if its function definition does so.
2040 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2041 then strings and vectors are not accepted. */)
2042 (Lisp_Object function, Lisp_Object for_call_interactively)
2044 register Lisp_Object fun;
2045 register Lisp_Object funcar;
2046 Lisp_Object if_prop = Qnil;
2048 fun = function;
2050 fun = indirect_function (fun); /* Check cycles. */
2051 if (NILP (fun) || EQ (fun, Qunbound))
2052 return Qnil;
2054 /* Check an `interactive-form' property if present, analogous to the
2055 function-documentation property. */
2056 fun = function;
2057 while (SYMBOLP (fun))
2059 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2060 if (!NILP (tmp))
2061 if_prop = Qt;
2062 fun = Fsymbol_function (fun);
2065 /* Emacs primitives are interactive if their DEFUN specifies an
2066 interactive spec. */
2067 if (SUBRP (fun))
2068 return XSUBR (fun)->intspec ? Qt : if_prop;
2070 /* Bytecode objects are interactive if they are long enough to
2071 have an element whose index is COMPILED_INTERACTIVE, which is
2072 where the interactive spec is stored. */
2073 else if (COMPILEDP (fun))
2074 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2075 ? Qt : if_prop);
2077 /* Strings and vectors are keyboard macros. */
2078 if (STRINGP (fun) || VECTORP (fun))
2079 return (NILP (for_call_interactively) ? Qt : Qnil);
2081 /* Lists may represent commands. */
2082 if (!CONSP (fun))
2083 return Qnil;
2084 funcar = XCAR (fun);
2085 if (EQ (funcar, Qclosure))
2086 return !NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2087 else if (EQ (funcar, Qlambda))
2088 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2089 else if (EQ (funcar, Qautoload))
2090 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2091 else
2092 return Qnil;
2095 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2096 doc: /* Define FUNCTION to autoload from FILE.
2097 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2098 Third arg DOCSTRING is documentation for the function.
2099 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2100 Fifth arg TYPE indicates the type of the object:
2101 nil or omitted says FUNCTION is a function,
2102 `keymap' says FUNCTION is really a keymap, and
2103 `macro' or t says FUNCTION is really a macro.
2104 Third through fifth args give info about the real definition.
2105 They default to nil.
2106 If FUNCTION is already defined other than as an autoload,
2107 this does nothing and returns nil. */)
2108 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2110 CHECK_SYMBOL (function);
2111 CHECK_STRING (file);
2113 /* If function is defined and not as an autoload, don't override. */
2114 if (!EQ (XSYMBOL (function)->function, Qunbound)
2115 && !(CONSP (XSYMBOL (function)->function)
2116 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2117 return Qnil;
2119 if (NILP (Vpurify_flag))
2120 /* Only add entries after dumping, because the ones before are
2121 not useful and else we get loads of them from the loaddefs.el. */
2122 LOADHIST_ATTACH (Fcons (Qautoload, function));
2123 else
2124 /* We don't want the docstring in purespace (instead,
2125 Snarf-documentation should (hopefully) overwrite it).
2126 We used to use 0 here, but that leads to accidental sharing in
2127 purecopy's hash-consing, so we use a (hopefully) unique integer
2128 instead. */
2129 docstring = make_number (XHASH (function));
2130 return Ffset (function,
2131 Fpurecopy (list5 (Qautoload, file, docstring,
2132 interactive, type)));
2135 Lisp_Object
2136 un_autoload (Lisp_Object oldqueue)
2138 register Lisp_Object queue, first, second;
2140 /* Queue to unwind is current value of Vautoload_queue.
2141 oldqueue is the shadowed value to leave in Vautoload_queue. */
2142 queue = Vautoload_queue;
2143 Vautoload_queue = oldqueue;
2144 while (CONSP (queue))
2146 first = XCAR (queue);
2147 second = Fcdr (first);
2148 first = Fcar (first);
2149 if (EQ (first, make_number (0)))
2150 Vfeatures = second;
2151 else
2152 Ffset (first, second);
2153 queue = XCDR (queue);
2155 return Qnil;
2158 /* Load an autoloaded function.
2159 FUNNAME is the symbol which is the function's name.
2160 FUNDEF is the autoload definition (a list). */
2162 void
2163 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2165 int count = SPECPDL_INDEX ();
2166 Lisp_Object fun;
2167 struct gcpro gcpro1, gcpro2, gcpro3;
2169 /* This is to make sure that loadup.el gives a clear picture
2170 of what files are preloaded and when. */
2171 if (! NILP (Vpurify_flag))
2172 error ("Attempt to autoload %s while preparing to dump",
2173 SDATA (SYMBOL_NAME (funname)));
2175 fun = funname;
2176 CHECK_SYMBOL (funname);
2177 GCPRO3 (fun, funname, fundef);
2179 /* Preserve the match data. */
2180 record_unwind_save_match_data ();
2182 /* If autoloading gets an error (which includes the error of failing
2183 to define the function being called), we use Vautoload_queue
2184 to undo function definitions and `provide' calls made by
2185 the function. We do this in the specific case of autoloading
2186 because autoloading is not an explicit request "load this file",
2187 but rather a request to "call this function".
2189 The value saved here is to be restored into Vautoload_queue. */
2190 record_unwind_protect (un_autoload, Vautoload_queue);
2191 Vautoload_queue = Qt;
2192 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2194 /* Once loading finishes, don't undo it. */
2195 Vautoload_queue = Qt;
2196 unbind_to (count, Qnil);
2198 fun = Findirect_function (fun, Qnil);
2200 if (!NILP (Fequal (fun, fundef)))
2201 error ("Autoloading failed to define function %s",
2202 SDATA (SYMBOL_NAME (funname)));
2203 UNGCPRO;
2207 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2208 doc: /* Evaluate FORM and return its value.
2209 If LEXICAL is t, evaluate using lexical scoping. */)
2210 (Lisp_Object form, Lisp_Object lexical)
2212 int count = SPECPDL_INDEX ();
2213 specbind (Qinternal_interpreter_environment,
2214 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2215 return unbind_to (count, eval_sub (form));
2218 /* Eval a sub-expression of the current expression (i.e. in the same
2219 lexical scope). */
2220 Lisp_Object
2221 eval_sub (Lisp_Object form)
2223 Lisp_Object fun, val, original_fun, original_args;
2224 Lisp_Object funcar;
2225 struct backtrace backtrace;
2226 struct gcpro gcpro1, gcpro2, gcpro3;
2228 if (handling_signal)
2229 abort ();
2231 if (SYMBOLP (form))
2233 /* Look up its binding in the lexical environment.
2234 We do not pay attention to the declared_special flag here, since we
2235 already did that when let-binding the variable. */
2236 Lisp_Object lex_binding
2237 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2238 ? Fassq (form, Vinternal_interpreter_environment)
2239 : Qnil;
2240 if (CONSP (lex_binding))
2241 return XCDR (lex_binding);
2242 else
2243 return Fsymbol_value (form);
2246 if (!CONSP (form))
2247 return form;
2249 QUIT;
2250 if ((consing_since_gc > gc_cons_threshold
2251 && consing_since_gc > gc_relative_threshold)
2253 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2255 GCPRO1 (form);
2256 Fgarbage_collect ();
2257 UNGCPRO;
2260 if (++lisp_eval_depth > max_lisp_eval_depth)
2262 if (max_lisp_eval_depth < 100)
2263 max_lisp_eval_depth = 100;
2264 if (lisp_eval_depth > max_lisp_eval_depth)
2265 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2268 original_fun = Fcar (form);
2269 original_args = Fcdr (form);
2271 backtrace.next = backtrace_list;
2272 backtrace_list = &backtrace;
2273 backtrace.function = &original_fun; /* This also protects them from gc. */
2274 backtrace.args = &original_args;
2275 backtrace.nargs = UNEVALLED;
2276 backtrace.evalargs = 1;
2277 backtrace.debug_on_exit = 0;
2279 if (debug_on_next_call)
2280 do_debug_on_call (Qt);
2282 /* At this point, only original_fun and original_args
2283 have values that will be used below. */
2284 retry:
2286 /* Optimize for no indirection. */
2287 fun = original_fun;
2288 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2289 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2290 fun = indirect_function (fun);
2292 if (SUBRP (fun))
2294 Lisp_Object numargs;
2295 Lisp_Object argvals[8];
2296 Lisp_Object args_left;
2297 register int i, maxargs;
2299 args_left = original_args;
2300 numargs = Flength (args_left);
2302 CHECK_CONS_LIST ();
2304 if (XINT (numargs) < XSUBR (fun)->min_args
2305 || (XSUBR (fun)->max_args >= 0
2306 && XSUBR (fun)->max_args < XINT (numargs)))
2307 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2309 else if (XSUBR (fun)->max_args == UNEVALLED)
2311 backtrace.evalargs = 0;
2312 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2314 else if (XSUBR (fun)->max_args == MANY)
2316 /* Pass a vector of evaluated arguments. */
2317 Lisp_Object *vals;
2318 register size_t argnum = 0;
2319 USE_SAFE_ALLOCA;
2321 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2323 GCPRO3 (args_left, fun, fun);
2324 gcpro3.var = vals;
2325 gcpro3.nvars = 0;
2327 while (!NILP (args_left))
2329 vals[argnum++] = eval_sub (Fcar (args_left));
2330 args_left = Fcdr (args_left);
2331 gcpro3.nvars = argnum;
2334 backtrace.args = vals;
2335 backtrace.nargs = XINT (numargs);
2337 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2338 UNGCPRO;
2339 SAFE_FREE ();
2341 else
2343 GCPRO3 (args_left, fun, fun);
2344 gcpro3.var = argvals;
2345 gcpro3.nvars = 0;
2347 maxargs = XSUBR (fun)->max_args;
2348 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2350 argvals[i] = eval_sub (Fcar (args_left));
2351 gcpro3.nvars = ++i;
2354 UNGCPRO;
2356 backtrace.args = argvals;
2357 backtrace.nargs = XINT (numargs);
2359 switch (i)
2361 case 0:
2362 val = (XSUBR (fun)->function.a0 ());
2363 break;
2364 case 1:
2365 val = (XSUBR (fun)->function.a1 (argvals[0]));
2366 break;
2367 case 2:
2368 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2369 break;
2370 case 3:
2371 val = (XSUBR (fun)->function.a3
2372 (argvals[0], argvals[1], argvals[2]));
2373 break;
2374 case 4:
2375 val = (XSUBR (fun)->function.a4
2376 (argvals[0], argvals[1], argvals[2], argvals[3]));
2377 break;
2378 case 5:
2379 val = (XSUBR (fun)->function.a5
2380 (argvals[0], argvals[1], argvals[2], argvals[3],
2381 argvals[4]));
2382 break;
2383 case 6:
2384 val = (XSUBR (fun)->function.a6
2385 (argvals[0], argvals[1], argvals[2], argvals[3],
2386 argvals[4], argvals[5]));
2387 break;
2388 case 7:
2389 val = (XSUBR (fun)->function.a7
2390 (argvals[0], argvals[1], argvals[2], argvals[3],
2391 argvals[4], argvals[5], argvals[6]));
2392 break;
2394 case 8:
2395 val = (XSUBR (fun)->function.a8
2396 (argvals[0], argvals[1], argvals[2], argvals[3],
2397 argvals[4], argvals[5], argvals[6], argvals[7]));
2398 break;
2400 default:
2401 /* Someone has created a subr that takes more arguments than
2402 is supported by this code. We need to either rewrite the
2403 subr to use a different argument protocol, or add more
2404 cases to this switch. */
2405 abort ();
2409 else if (COMPILEDP (fun))
2410 val = apply_lambda (fun, original_args);
2411 else
2413 if (EQ (fun, Qunbound))
2414 xsignal1 (Qvoid_function, original_fun);
2415 if (!CONSP (fun))
2416 xsignal1 (Qinvalid_function, original_fun);
2417 funcar = XCAR (fun);
2418 if (!SYMBOLP (funcar))
2419 xsignal1 (Qinvalid_function, original_fun);
2420 if (EQ (funcar, Qautoload))
2422 do_autoload (fun, original_fun);
2423 goto retry;
2425 if (EQ (funcar, Qmacro))
2426 val = eval_sub (apply1 (Fcdr (fun), original_args));
2427 else if (EQ (funcar, Qlambda)
2428 || EQ (funcar, Qclosure))
2429 val = apply_lambda (fun, original_args);
2430 else
2431 xsignal1 (Qinvalid_function, original_fun);
2433 CHECK_CONS_LIST ();
2435 lisp_eval_depth--;
2436 if (backtrace.debug_on_exit)
2437 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2438 backtrace_list = backtrace.next;
2440 return val;
2443 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2444 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2445 Then return the value FUNCTION returns.
2446 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2447 usage: (apply FUNCTION &rest ARGUMENTS) */)
2448 (size_t nargs, Lisp_Object *args)
2450 register size_t i, numargs;
2451 register Lisp_Object spread_arg;
2452 register Lisp_Object *funcall_args;
2453 Lisp_Object fun, retval;
2454 struct gcpro gcpro1;
2455 USE_SAFE_ALLOCA;
2457 fun = args [0];
2458 funcall_args = 0;
2459 spread_arg = args [nargs - 1];
2460 CHECK_LIST (spread_arg);
2462 numargs = XINT (Flength (spread_arg));
2464 if (numargs == 0)
2465 return Ffuncall (nargs - 1, args);
2466 else if (numargs == 1)
2468 args [nargs - 1] = XCAR (spread_arg);
2469 return Ffuncall (nargs, args);
2472 numargs += nargs - 2;
2474 /* Optimize for no indirection. */
2475 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2476 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2477 fun = indirect_function (fun);
2478 if (EQ (fun, Qunbound))
2480 /* Let funcall get the error. */
2481 fun = args[0];
2482 goto funcall;
2485 if (SUBRP (fun))
2487 if (numargs < XSUBR (fun)->min_args
2488 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2489 goto funcall; /* Let funcall get the error. */
2490 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2492 /* Avoid making funcall cons up a yet another new vector of arguments
2493 by explicitly supplying nil's for optional values. */
2494 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2495 for (i = numargs; i < XSUBR (fun)->max_args;)
2496 funcall_args[++i] = Qnil;
2497 GCPRO1 (*funcall_args);
2498 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2501 funcall:
2502 /* We add 1 to numargs because funcall_args includes the
2503 function itself as well as its arguments. */
2504 if (!funcall_args)
2506 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2507 GCPRO1 (*funcall_args);
2508 gcpro1.nvars = 1 + numargs;
2511 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2512 /* Spread the last arg we got. Its first element goes in
2513 the slot that it used to occupy, hence this value of I. */
2514 i = nargs - 1;
2515 while (!NILP (spread_arg))
2517 funcall_args [i++] = XCAR (spread_arg);
2518 spread_arg = XCDR (spread_arg);
2521 /* By convention, the caller needs to gcpro Ffuncall's args. */
2522 retval = Ffuncall (gcpro1.nvars, funcall_args);
2523 UNGCPRO;
2524 SAFE_FREE ();
2526 return retval;
2529 /* Run hook variables in various ways. */
2531 static Lisp_Object
2532 funcall_nil (size_t nargs, Lisp_Object *args)
2534 Ffuncall (nargs, args);
2535 return Qnil;
2538 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2539 doc: /* Run each hook in HOOKS.
2540 Each argument should be a symbol, a hook variable.
2541 These symbols are processed in the order specified.
2542 If a hook symbol has a non-nil value, that value may be a function
2543 or a list of functions to be called to run the hook.
2544 If the value is a function, it is called with no arguments.
2545 If it is a list, the elements are called, in order, with no arguments.
2547 Major modes should not use this function directly to run their mode
2548 hook; they should use `run-mode-hooks' instead.
2550 Do not use `make-local-variable' to make a hook variable buffer-local.
2551 Instead, use `add-hook' and specify t for the LOCAL argument.
2552 usage: (run-hooks &rest HOOKS) */)
2553 (size_t nargs, Lisp_Object *args)
2555 Lisp_Object hook[1];
2556 register size_t i;
2558 for (i = 0; i < nargs; i++)
2560 hook[0] = args[i];
2561 run_hook_with_args (1, hook, funcall_nil);
2564 return Qnil;
2567 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2568 Srun_hook_with_args, 1, MANY, 0,
2569 doc: /* Run HOOK with the specified arguments ARGS.
2570 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2571 value, that value may be a function or a list of functions to be
2572 called to run the hook. If the value is a function, it is called with
2573 the given arguments and its return value is returned. If it is a list
2574 of functions, those functions are called, in order,
2575 with the given arguments ARGS.
2576 It is best not to depend on the value returned by `run-hook-with-args',
2577 as that may change.
2579 Do not use `make-local-variable' to make a hook variable buffer-local.
2580 Instead, use `add-hook' and specify t for the LOCAL argument.
2581 usage: (run-hook-with-args HOOK &rest ARGS) */)
2582 (size_t nargs, Lisp_Object *args)
2584 return run_hook_with_args (nargs, args, funcall_nil);
2587 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2588 Srun_hook_with_args_until_success, 1, MANY, 0,
2589 doc: /* Run HOOK with the specified arguments ARGS.
2590 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2591 value, that value may be a function or a list of functions to be
2592 called to run the hook. If the value is a function, it is called with
2593 the given arguments and its return value is returned.
2594 If it is a list of functions, those functions are called, in order,
2595 with the given arguments ARGS, until one of them
2596 returns a non-nil value. Then we return that value.
2597 However, if they all return nil, we return nil.
2599 Do not use `make-local-variable' to make a hook variable buffer-local.
2600 Instead, use `add-hook' and specify t for the LOCAL argument.
2601 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2602 (size_t nargs, Lisp_Object *args)
2604 return run_hook_with_args (nargs, args, Ffuncall);
2607 static Lisp_Object
2608 funcall_not (size_t nargs, Lisp_Object *args)
2610 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2613 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2614 Srun_hook_with_args_until_failure, 1, MANY, 0,
2615 doc: /* Run HOOK with the specified arguments ARGS.
2616 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2617 value, that value may be a function or a list of functions to be
2618 called to run the hook. If the value is a function, it is called with
2619 the given arguments and its return value is returned.
2620 If it is a list of functions, those functions are called, in order,
2621 with the given arguments ARGS, until one of them returns nil.
2622 Then we return nil. However, if they all return non-nil, we return non-nil.
2624 Do not use `make-local-variable' to make a hook variable buffer-local.
2625 Instead, use `add-hook' and specify t for the LOCAL argument.
2626 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2627 (size_t nargs, Lisp_Object *args)
2629 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2632 static Lisp_Object
2633 run_hook_wrapped_funcall (size_t nargs, Lisp_Object *args)
2635 Lisp_Object tmp = args[0], ret;
2636 args[0] = args[1];
2637 args[1] = tmp;
2638 ret = Ffuncall (nargs, args);
2639 args[1] = args[0];
2640 args[0] = tmp;
2641 return ret;
2644 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2645 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2646 I.e. instead of calling each function FUN directly with arguments ARGS,
2647 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2648 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2649 aborts and returns that value.
2650 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2651 (size_t nargs, Lisp_Object *args)
2653 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2656 /* ARGS[0] should be a hook symbol.
2657 Call each of the functions in the hook value, passing each of them
2658 as arguments all the rest of ARGS (all NARGS - 1 elements).
2659 FUNCALL specifies how to call each function on the hook.
2660 The caller (or its caller, etc) must gcpro all of ARGS,
2661 except that it isn't necessary to gcpro ARGS[0]. */
2663 Lisp_Object
2664 run_hook_with_args (size_t nargs, Lisp_Object *args,
2665 Lisp_Object (*funcall) (size_t nargs, Lisp_Object *args))
2667 Lisp_Object sym, val, ret = Qnil;
2668 struct gcpro gcpro1, gcpro2, gcpro3;
2670 /* If we are dying or still initializing,
2671 don't do anything--it would probably crash if we tried. */
2672 if (NILP (Vrun_hooks))
2673 return Qnil;
2675 sym = args[0];
2676 val = find_symbol_value (sym);
2678 if (EQ (val, Qunbound) || NILP (val))
2679 return ret;
2680 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2682 args[0] = val;
2683 return funcall (nargs, args);
2685 else
2687 Lisp_Object global_vals = Qnil;
2688 GCPRO3 (sym, val, global_vals);
2690 for (;
2691 CONSP (val) && NILP (ret);
2692 val = XCDR (val))
2694 if (EQ (XCAR (val), Qt))
2696 /* t indicates this hook has a local binding;
2697 it means to run the global binding too. */
2698 global_vals = Fdefault_value (sym);
2699 if (NILP (global_vals)) continue;
2701 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2703 args[0] = global_vals;
2704 ret = funcall (nargs, args);
2706 else
2708 for (;
2709 CONSP (global_vals) && NILP (ret);
2710 global_vals = XCDR (global_vals))
2712 args[0] = XCAR (global_vals);
2713 /* In a global value, t should not occur. If it does, we
2714 must ignore it to avoid an endless loop. */
2715 if (!EQ (args[0], Qt))
2716 ret = funcall (nargs, args);
2720 else
2722 args[0] = XCAR (val);
2723 ret = funcall (nargs, args);
2727 UNGCPRO;
2728 return ret;
2732 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2734 void
2735 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2737 Lisp_Object temp[3];
2738 temp[0] = hook;
2739 temp[1] = arg1;
2740 temp[2] = arg2;
2742 Frun_hook_with_args (3, temp);
2745 /* Apply fn to arg. */
2746 Lisp_Object
2747 apply1 (Lisp_Object fn, Lisp_Object arg)
2749 struct gcpro gcpro1;
2751 GCPRO1 (fn);
2752 if (NILP (arg))
2753 RETURN_UNGCPRO (Ffuncall (1, &fn));
2754 gcpro1.nvars = 2;
2756 Lisp_Object args[2];
2757 args[0] = fn;
2758 args[1] = arg;
2759 gcpro1.var = args;
2760 RETURN_UNGCPRO (Fapply (2, args));
2764 /* Call function fn on no arguments. */
2765 Lisp_Object
2766 call0 (Lisp_Object fn)
2768 struct gcpro gcpro1;
2770 GCPRO1 (fn);
2771 RETURN_UNGCPRO (Ffuncall (1, &fn));
2774 /* Call function fn with 1 argument arg1. */
2775 /* ARGSUSED */
2776 Lisp_Object
2777 call1 (Lisp_Object fn, Lisp_Object arg1)
2779 struct gcpro gcpro1;
2780 Lisp_Object args[2];
2782 args[0] = fn;
2783 args[1] = arg1;
2784 GCPRO1 (args[0]);
2785 gcpro1.nvars = 2;
2786 RETURN_UNGCPRO (Ffuncall (2, args));
2789 /* Call function fn with 2 arguments arg1, arg2. */
2790 /* ARGSUSED */
2791 Lisp_Object
2792 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2794 struct gcpro gcpro1;
2795 Lisp_Object args[3];
2796 args[0] = fn;
2797 args[1] = arg1;
2798 args[2] = arg2;
2799 GCPRO1 (args[0]);
2800 gcpro1.nvars = 3;
2801 RETURN_UNGCPRO (Ffuncall (3, args));
2804 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2805 /* ARGSUSED */
2806 Lisp_Object
2807 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2809 struct gcpro gcpro1;
2810 Lisp_Object args[4];
2811 args[0] = fn;
2812 args[1] = arg1;
2813 args[2] = arg2;
2814 args[3] = arg3;
2815 GCPRO1 (args[0]);
2816 gcpro1.nvars = 4;
2817 RETURN_UNGCPRO (Ffuncall (4, args));
2820 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2821 /* ARGSUSED */
2822 Lisp_Object
2823 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2824 Lisp_Object arg4)
2826 struct gcpro gcpro1;
2827 Lisp_Object args[5];
2828 args[0] = fn;
2829 args[1] = arg1;
2830 args[2] = arg2;
2831 args[3] = arg3;
2832 args[4] = arg4;
2833 GCPRO1 (args[0]);
2834 gcpro1.nvars = 5;
2835 RETURN_UNGCPRO (Ffuncall (5, args));
2838 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2839 /* ARGSUSED */
2840 Lisp_Object
2841 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2842 Lisp_Object arg4, Lisp_Object arg5)
2844 struct gcpro gcpro1;
2845 Lisp_Object args[6];
2846 args[0] = fn;
2847 args[1] = arg1;
2848 args[2] = arg2;
2849 args[3] = arg3;
2850 args[4] = arg4;
2851 args[5] = arg5;
2852 GCPRO1 (args[0]);
2853 gcpro1.nvars = 6;
2854 RETURN_UNGCPRO (Ffuncall (6, args));
2857 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2858 /* ARGSUSED */
2859 Lisp_Object
2860 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2861 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2863 struct gcpro gcpro1;
2864 Lisp_Object args[7];
2865 args[0] = fn;
2866 args[1] = arg1;
2867 args[2] = arg2;
2868 args[3] = arg3;
2869 args[4] = arg4;
2870 args[5] = arg5;
2871 args[6] = arg6;
2872 GCPRO1 (args[0]);
2873 gcpro1.nvars = 7;
2874 RETURN_UNGCPRO (Ffuncall (7, args));
2877 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2878 /* ARGSUSED */
2879 Lisp_Object
2880 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2881 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2883 struct gcpro gcpro1;
2884 Lisp_Object args[8];
2885 args[0] = fn;
2886 args[1] = arg1;
2887 args[2] = arg2;
2888 args[3] = arg3;
2889 args[4] = arg4;
2890 args[5] = arg5;
2891 args[6] = arg6;
2892 args[7] = arg7;
2893 GCPRO1 (args[0]);
2894 gcpro1.nvars = 8;
2895 RETURN_UNGCPRO (Ffuncall (8, args));
2898 /* The caller should GCPRO all the elements of ARGS. */
2900 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2901 doc: /* Return non-nil if OBJECT is a type of object that can be called as a function. */)
2902 (Lisp_Object object)
2904 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2906 object = Findirect_function (object, Qt);
2908 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2910 /* Autoloaded symbols are functions, except if they load
2911 macros or keymaps. */
2912 int i;
2913 for (i = 0; i < 4 && CONSP (object); i++)
2914 object = XCDR (object);
2916 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2920 if (SUBRP (object))
2921 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2922 else if (COMPILEDP (object))
2923 return Qt;
2924 else if (CONSP (object))
2926 Lisp_Object car = XCAR (object);
2927 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2929 else
2930 return Qnil;
2933 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2934 doc: /* Call first argument as a function, passing remaining arguments to it.
2935 Return the value that function returns.
2936 Thus, (funcall 'cons 'x 'y) returns (x . y).
2937 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2938 (size_t nargs, Lisp_Object *args)
2940 Lisp_Object fun, original_fun;
2941 Lisp_Object funcar;
2942 size_t numargs = nargs - 1;
2943 Lisp_Object lisp_numargs;
2944 Lisp_Object val;
2945 struct backtrace backtrace;
2946 register Lisp_Object *internal_args;
2947 register size_t i;
2949 QUIT;
2950 if ((consing_since_gc > gc_cons_threshold
2951 && consing_since_gc > gc_relative_threshold)
2953 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2954 Fgarbage_collect ();
2956 if (++lisp_eval_depth > max_lisp_eval_depth)
2958 if (max_lisp_eval_depth < 100)
2959 max_lisp_eval_depth = 100;
2960 if (lisp_eval_depth > max_lisp_eval_depth)
2961 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2964 backtrace.next = backtrace_list;
2965 backtrace_list = &backtrace;
2966 backtrace.function = &args[0];
2967 backtrace.args = &args[1];
2968 backtrace.nargs = nargs - 1;
2969 backtrace.evalargs = 0;
2970 backtrace.debug_on_exit = 0;
2972 if (debug_on_next_call)
2973 do_debug_on_call (Qlambda);
2975 CHECK_CONS_LIST ();
2977 original_fun = args[0];
2979 retry:
2981 /* Optimize for no indirection. */
2982 fun = original_fun;
2983 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2984 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2985 fun = indirect_function (fun);
2987 if (SUBRP (fun))
2989 if (numargs < XSUBR (fun)->min_args
2990 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2992 XSETFASTINT (lisp_numargs, numargs);
2993 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2996 else if (XSUBR (fun)->max_args == UNEVALLED)
2997 xsignal1 (Qinvalid_function, original_fun);
2999 else if (XSUBR (fun)->max_args == MANY)
3000 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
3001 else
3003 if (XSUBR (fun)->max_args > numargs)
3005 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
3006 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
3007 for (i = numargs; i < XSUBR (fun)->max_args; i++)
3008 internal_args[i] = Qnil;
3010 else
3011 internal_args = args + 1;
3012 switch (XSUBR (fun)->max_args)
3014 case 0:
3015 val = (XSUBR (fun)->function.a0 ());
3016 break;
3017 case 1:
3018 val = (XSUBR (fun)->function.a1 (internal_args[0]));
3019 break;
3020 case 2:
3021 val = (XSUBR (fun)->function.a2
3022 (internal_args[0], internal_args[1]));
3023 break;
3024 case 3:
3025 val = (XSUBR (fun)->function.a3
3026 (internal_args[0], internal_args[1], internal_args[2]));
3027 break;
3028 case 4:
3029 val = (XSUBR (fun)->function.a4
3030 (internal_args[0], internal_args[1], internal_args[2],
3031 internal_args[3]));
3032 break;
3033 case 5:
3034 val = (XSUBR (fun)->function.a5
3035 (internal_args[0], internal_args[1], internal_args[2],
3036 internal_args[3], internal_args[4]));
3037 break;
3038 case 6:
3039 val = (XSUBR (fun)->function.a6
3040 (internal_args[0], internal_args[1], internal_args[2],
3041 internal_args[3], internal_args[4], internal_args[5]));
3042 break;
3043 case 7:
3044 val = (XSUBR (fun)->function.a7
3045 (internal_args[0], internal_args[1], internal_args[2],
3046 internal_args[3], internal_args[4], internal_args[5],
3047 internal_args[6]));
3048 break;
3050 case 8:
3051 val = (XSUBR (fun)->function.a8
3052 (internal_args[0], internal_args[1], internal_args[2],
3053 internal_args[3], internal_args[4], internal_args[5],
3054 internal_args[6], internal_args[7]));
3055 break;
3057 default:
3059 /* If a subr takes more than 8 arguments without using MANY
3060 or UNEVALLED, we need to extend this function to support it.
3061 Until this is done, there is no way to call the function. */
3062 abort ();
3066 else if (COMPILEDP (fun))
3067 val = funcall_lambda (fun, numargs, args + 1);
3068 else
3070 if (EQ (fun, Qunbound))
3071 xsignal1 (Qvoid_function, original_fun);
3072 if (!CONSP (fun))
3073 xsignal1 (Qinvalid_function, original_fun);
3074 funcar = XCAR (fun);
3075 if (!SYMBOLP (funcar))
3076 xsignal1 (Qinvalid_function, original_fun);
3077 if (EQ (funcar, Qlambda)
3078 || EQ (funcar, Qclosure))
3079 val = funcall_lambda (fun, numargs, args + 1);
3080 else if (EQ (funcar, Qautoload))
3082 do_autoload (fun, original_fun);
3083 CHECK_CONS_LIST ();
3084 goto retry;
3086 else
3087 xsignal1 (Qinvalid_function, original_fun);
3089 CHECK_CONS_LIST ();
3090 lisp_eval_depth--;
3091 if (backtrace.debug_on_exit)
3092 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3093 backtrace_list = backtrace.next;
3094 return val;
3097 static Lisp_Object
3098 apply_lambda (Lisp_Object fun, Lisp_Object args)
3100 Lisp_Object args_left;
3101 size_t numargs;
3102 register Lisp_Object *arg_vector;
3103 struct gcpro gcpro1, gcpro2, gcpro3;
3104 register size_t i;
3105 register Lisp_Object tem;
3106 USE_SAFE_ALLOCA;
3108 numargs = XINT (Flength (args));
3109 SAFE_ALLOCA_LISP (arg_vector, numargs);
3110 args_left = args;
3112 GCPRO3 (*arg_vector, args_left, fun);
3113 gcpro1.nvars = 0;
3115 for (i = 0; i < numargs; )
3117 tem = Fcar (args_left), args_left = Fcdr (args_left);
3118 tem = eval_sub (tem);
3119 arg_vector[i++] = tem;
3120 gcpro1.nvars = i;
3123 UNGCPRO;
3125 backtrace_list->args = arg_vector;
3126 backtrace_list->nargs = i;
3127 backtrace_list->evalargs = 0;
3128 tem = funcall_lambda (fun, numargs, arg_vector);
3130 /* Do the debug-on-exit now, while arg_vector still exists. */
3131 if (backtrace_list->debug_on_exit)
3132 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3133 /* Don't do it again when we return to eval. */
3134 backtrace_list->debug_on_exit = 0;
3135 SAFE_FREE ();
3136 return tem;
3139 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3140 and return the result of evaluation.
3141 FUN must be either a lambda-expression or a compiled-code object. */
3143 static Lisp_Object
3144 funcall_lambda (Lisp_Object fun, size_t nargs,
3145 register Lisp_Object *arg_vector)
3147 Lisp_Object val, syms_left, next, lexenv;
3148 int count = SPECPDL_INDEX ();
3149 size_t i;
3150 int optional, rest;
3152 if (CONSP (fun))
3154 if (EQ (XCAR (fun), Qclosure))
3156 fun = XCDR (fun); /* Drop `closure'. */
3157 lexenv = XCAR (fun);
3158 CHECK_LIST_CONS (fun, fun);
3160 else
3161 lexenv = Qnil;
3162 syms_left = XCDR (fun);
3163 if (CONSP (syms_left))
3164 syms_left = XCAR (syms_left);
3165 else
3166 xsignal1 (Qinvalid_function, fun);
3168 else if (COMPILEDP (fun))
3170 syms_left = AREF (fun, COMPILED_ARGLIST);
3171 if (INTEGERP (syms_left))
3172 /* A byte-code object with a non-nil `push args' slot means we
3173 shouldn't bind any arguments, instead just call the byte-code
3174 interpreter directly; it will push arguments as necessary.
3176 Byte-code objects with either a non-existant, or a nil value for
3177 the `push args' slot (the default), have dynamically-bound
3178 arguments, and use the argument-binding code below instead (as do
3179 all interpreted functions, even lexically bound ones). */
3181 /* If we have not actually read the bytecode string
3182 and constants vector yet, fetch them from the file. */
3183 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3184 Ffetch_bytecode (fun);
3185 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3186 AREF (fun, COMPILED_CONSTANTS),
3187 AREF (fun, COMPILED_STACK_DEPTH),
3188 syms_left,
3189 nargs, arg_vector);
3191 lexenv = Qnil;
3193 else
3194 abort ();
3196 i = optional = rest = 0;
3197 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3199 QUIT;
3201 next = XCAR (syms_left);
3202 if (!SYMBOLP (next))
3203 xsignal1 (Qinvalid_function, fun);
3205 if (EQ (next, Qand_rest))
3206 rest = 1;
3207 else if (EQ (next, Qand_optional))
3208 optional = 1;
3209 else
3211 Lisp_Object val;
3212 if (rest)
3214 val = Flist (nargs - i, &arg_vector[i]);
3215 i = nargs;
3217 else if (i < nargs)
3218 val = arg_vector[i++];
3219 else if (!optional)
3220 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3221 else
3222 val = Qnil;
3224 /* Bind the argument. */
3225 if (!NILP (lexenv) && SYMBOLP (next))
3226 /* Lexically bind NEXT by adding it to the lexenv alist. */
3227 lexenv = Fcons (Fcons (next, val), lexenv);
3228 else
3229 /* Dynamically bind NEXT. */
3230 specbind (next, val);
3234 if (!NILP (syms_left))
3235 xsignal1 (Qinvalid_function, fun);
3236 else if (i < nargs)
3237 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3239 if (!EQ (lexenv, Vinternal_interpreter_environment))
3240 /* Instantiate a new lexical environment. */
3241 specbind (Qinternal_interpreter_environment, lexenv);
3243 if (CONSP (fun))
3244 val = Fprogn (XCDR (XCDR (fun)));
3245 else
3247 /* If we have not actually read the bytecode string
3248 and constants vector yet, fetch them from the file. */
3249 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3250 Ffetch_bytecode (fun);
3251 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3252 AREF (fun, COMPILED_CONSTANTS),
3253 AREF (fun, COMPILED_STACK_DEPTH),
3254 Qnil, 0, 0);
3257 return unbind_to (count, val);
3260 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3261 1, 1, 0,
3262 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3263 (Lisp_Object object)
3265 Lisp_Object tem;
3267 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3269 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3270 if (!CONSP (tem))
3272 tem = AREF (object, COMPILED_BYTECODE);
3273 if (CONSP (tem) && STRINGP (XCAR (tem)))
3274 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3275 else
3276 error ("Invalid byte code");
3278 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3279 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3281 return object;
3284 static void
3285 grow_specpdl (void)
3287 register int count = SPECPDL_INDEX ();
3288 if (specpdl_size >= max_specpdl_size)
3290 if (max_specpdl_size < 400)
3291 max_specpdl_size = 400;
3292 if (specpdl_size >= max_specpdl_size)
3293 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3295 specpdl_size *= 2;
3296 if (specpdl_size > max_specpdl_size)
3297 specpdl_size = max_specpdl_size;
3298 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
3299 specpdl_ptr = specpdl + count;
3302 /* `specpdl_ptr->symbol' is a field which describes which variable is
3303 let-bound, so it can be properly undone when we unbind_to.
3304 It can have the following two shapes:
3305 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3306 a symbol that is not buffer-local (at least at the time
3307 the let binding started). Note also that it should not be
3308 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3309 to record V2 here).
3310 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3311 variable SYMBOL which can be buffer-local. WHERE tells us
3312 which buffer is affected (or nil if the let-binding affects the
3313 global value of the variable) and BUFFER tells us which buffer was
3314 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3315 BUFFER did not yet have a buffer-local value). */
3317 void
3318 specbind (Lisp_Object symbol, Lisp_Object value)
3320 struct Lisp_Symbol *sym;
3322 eassert (!handling_signal);
3324 CHECK_SYMBOL (symbol);
3325 sym = XSYMBOL (symbol);
3326 if (specpdl_ptr == specpdl + specpdl_size)
3327 grow_specpdl ();
3329 start:
3330 switch (sym->redirect)
3332 case SYMBOL_VARALIAS:
3333 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3334 case SYMBOL_PLAINVAL:
3335 /* The most common case is that of a non-constant symbol with a
3336 trivial value. Make that as fast as we can. */
3337 specpdl_ptr->symbol = symbol;
3338 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3339 specpdl_ptr->func = NULL;
3340 ++specpdl_ptr;
3341 if (!sym->constant)
3342 SET_SYMBOL_VAL (sym, value);
3343 else
3344 set_internal (symbol, value, Qnil, 1);
3345 break;
3346 case SYMBOL_LOCALIZED:
3347 if (SYMBOL_BLV (sym)->frame_local)
3348 error ("Frame-local vars cannot be let-bound");
3349 case SYMBOL_FORWARDED:
3351 Lisp_Object ovalue = find_symbol_value (symbol);
3352 specpdl_ptr->func = 0;
3353 specpdl_ptr->old_value = ovalue;
3355 eassert (sym->redirect != SYMBOL_LOCALIZED
3356 || (EQ (SYMBOL_BLV (sym)->where,
3357 SYMBOL_BLV (sym)->frame_local ?
3358 Fselected_frame () : Fcurrent_buffer ())));
3360 if (sym->redirect == SYMBOL_LOCALIZED
3361 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3363 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3365 /* For a local variable, record both the symbol and which
3366 buffer's or frame's value we are saving. */
3367 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3369 eassert (sym->redirect != SYMBOL_LOCALIZED
3370 || (BLV_FOUND (SYMBOL_BLV (sym))
3371 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3372 where = cur_buf;
3374 else if (sym->redirect == SYMBOL_LOCALIZED
3375 && BLV_FOUND (SYMBOL_BLV (sym)))
3376 where = SYMBOL_BLV (sym)->where;
3377 else
3378 where = Qnil;
3380 /* We're not using the `unused' slot in the specbinding
3381 structure because this would mean we have to do more
3382 work for simple variables. */
3383 /* FIXME: The third value `current_buffer' is only used in
3384 let_shadows_buffer_binding_p which is itself only used
3385 in set_internal for local_if_set. */
3386 eassert (NILP (where) || EQ (where, cur_buf));
3387 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3389 /* If SYMBOL is a per-buffer variable which doesn't have a
3390 buffer-local value here, make the `let' change the global
3391 value by changing the value of SYMBOL in all buffers not
3392 having their own value. This is consistent with what
3393 happens with other buffer-local variables. */
3394 if (NILP (where)
3395 && sym->redirect == SYMBOL_FORWARDED)
3397 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3398 ++specpdl_ptr;
3399 Fset_default (symbol, value);
3400 return;
3403 else
3404 specpdl_ptr->symbol = symbol;
3406 specpdl_ptr++;
3407 set_internal (symbol, value, Qnil, 1);
3408 break;
3410 default: abort ();
3414 void
3415 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3417 eassert (!handling_signal);
3419 if (specpdl_ptr == specpdl + specpdl_size)
3420 grow_specpdl ();
3421 specpdl_ptr->func = function;
3422 specpdl_ptr->symbol = Qnil;
3423 specpdl_ptr->old_value = arg;
3424 specpdl_ptr++;
3427 Lisp_Object
3428 unbind_to (int count, Lisp_Object value)
3430 Lisp_Object quitf = Vquit_flag;
3431 struct gcpro gcpro1, gcpro2;
3433 GCPRO2 (value, quitf);
3434 Vquit_flag = Qnil;
3436 while (specpdl_ptr != specpdl + count)
3438 /* Copy the binding, and decrement specpdl_ptr, before we do
3439 the work to unbind it. We decrement first
3440 so that an error in unbinding won't try to unbind
3441 the same entry again, and we copy the binding first
3442 in case more bindings are made during some of the code we run. */
3444 struct specbinding this_binding;
3445 this_binding = *--specpdl_ptr;
3447 if (this_binding.func != 0)
3448 (*this_binding.func) (this_binding.old_value);
3449 /* If the symbol is a list, it is really (SYMBOL WHERE
3450 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3451 frame. If WHERE is a buffer or frame, this indicates we
3452 bound a variable that had a buffer-local or frame-local
3453 binding. WHERE nil means that the variable had the default
3454 value when it was bound. CURRENT-BUFFER is the buffer that
3455 was current when the variable was bound. */
3456 else if (CONSP (this_binding.symbol))
3458 Lisp_Object symbol, where;
3460 symbol = XCAR (this_binding.symbol);
3461 where = XCAR (XCDR (this_binding.symbol));
3463 if (NILP (where))
3464 Fset_default (symbol, this_binding.old_value);
3465 /* If `where' is non-nil, reset the value in the appropriate
3466 local binding, but only if that binding still exists. */
3467 else if (BUFFERP (where)
3468 ? !NILP (Flocal_variable_p (symbol, where))
3469 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3470 set_internal (symbol, this_binding.old_value, where, 1);
3472 /* If variable has a trivial value (no forwarding), we can
3473 just set it. No need to check for constant symbols here,
3474 since that was already done by specbind. */
3475 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3476 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3477 this_binding.old_value);
3478 else
3479 /* NOTE: we only ever come here if make_local_foo was used for
3480 the first time on this var within this let. */
3481 Fset_default (this_binding.symbol, this_binding.old_value);
3484 if (NILP (Vquit_flag) && !NILP (quitf))
3485 Vquit_flag = quitf;
3487 UNGCPRO;
3488 return value;
3493 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3494 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3495 A special variable is one that will be bound dynamically, even in a
3496 context where binding is lexical by default. */)
3497 (Lisp_Object symbol)
3499 CHECK_SYMBOL (symbol);
3500 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3505 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3506 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3507 The debugger is entered when that frame exits, if the flag is non-nil. */)
3508 (Lisp_Object level, Lisp_Object flag)
3510 register struct backtrace *backlist = backtrace_list;
3511 register int i;
3513 CHECK_NUMBER (level);
3515 for (i = 0; backlist && i < XINT (level); i++)
3517 backlist = backlist->next;
3520 if (backlist)
3521 backlist->debug_on_exit = !NILP (flag);
3523 return flag;
3526 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3527 doc: /* Print a trace of Lisp function calls currently active.
3528 Output stream used is value of `standard-output'. */)
3529 (void)
3531 register struct backtrace *backlist = backtrace_list;
3532 Lisp_Object tail;
3533 Lisp_Object tem;
3534 struct gcpro gcpro1;
3535 Lisp_Object old_print_level = Vprint_level;
3537 if (NILP (Vprint_level))
3538 XSETFASTINT (Vprint_level, 8);
3540 tail = Qnil;
3541 GCPRO1 (tail);
3543 while (backlist)
3545 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3546 if (backlist->nargs == UNEVALLED)
3548 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3549 write_string ("\n", -1);
3551 else
3553 tem = *backlist->function;
3554 Fprin1 (tem, Qnil); /* This can QUIT. */
3555 write_string ("(", -1);
3556 if (backlist->nargs == MANY)
3557 { /* FIXME: Can this happen? */
3558 int i;
3559 for (tail = *backlist->args, i = 0;
3560 !NILP (tail);
3561 tail = Fcdr (tail), i = 1)
3563 if (i) write_string (" ", -1);
3564 Fprin1 (Fcar (tail), Qnil);
3567 else
3569 size_t i;
3570 for (i = 0; i < backlist->nargs; i++)
3572 if (i) write_string (" ", -1);
3573 Fprin1 (backlist->args[i], Qnil);
3576 write_string (")\n", -1);
3578 backlist = backlist->next;
3581 Vprint_level = old_print_level;
3582 UNGCPRO;
3583 return Qnil;
3586 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3587 doc: /* Return the function and arguments NFRAMES up from current execution point.
3588 If that frame has not evaluated the arguments yet (or is a special form),
3589 the value is (nil FUNCTION ARG-FORMS...).
3590 If that frame has evaluated its arguments and called its function already,
3591 the value is (t FUNCTION ARG-VALUES...).
3592 A &rest arg is represented as the tail of the list ARG-VALUES.
3593 FUNCTION is whatever was supplied as car of evaluated list,
3594 or a lambda expression for macro calls.
3595 If NFRAMES is more than the number of frames, the value is nil. */)
3596 (Lisp_Object nframes)
3598 register struct backtrace *backlist = backtrace_list;
3599 register EMACS_INT i;
3600 Lisp_Object tem;
3602 CHECK_NATNUM (nframes);
3604 /* Find the frame requested. */
3605 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3606 backlist = backlist->next;
3608 if (!backlist)
3609 return Qnil;
3610 if (backlist->nargs == UNEVALLED)
3611 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3612 else
3614 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3615 tem = *backlist->args;
3616 else
3617 tem = Flist (backlist->nargs, backlist->args);
3619 return Fcons (Qt, Fcons (*backlist->function, tem));
3624 void
3625 mark_backtrace (void)
3627 register struct backtrace *backlist;
3628 register size_t i;
3630 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3632 mark_object (*backlist->function);
3634 if (backlist->nargs == UNEVALLED
3635 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3636 i = 1;
3637 else
3638 i = backlist->nargs;
3639 while (i--)
3640 mark_object (backlist->args[i]);
3644 EXFUN (Funintern, 2);
3646 void
3647 syms_of_eval (void)
3649 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3650 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3651 If Lisp code tries to increase the total number past this amount,
3652 an error is signaled.
3653 You can safely use a value considerably larger than the default value,
3654 if that proves inconveniently small. However, if you increase it too far,
3655 Emacs could run out of memory trying to make the stack bigger. */);
3657 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3658 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3660 This limit serves to catch infinite recursions for you before they cause
3661 actual stack overflow in C, which would be fatal for Emacs.
3662 You can safely make it considerably larger than its default value,
3663 if that proves inconveniently small. However, if you increase it too far,
3664 Emacs could overflow the real C stack, and crash. */);
3666 DEFVAR_LISP ("quit-flag", Vquit_flag,
3667 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3668 If the value is t, that means do an ordinary quit.
3669 If the value equals `throw-on-input', that means quit by throwing
3670 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3671 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3672 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3673 Vquit_flag = Qnil;
3675 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3676 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3677 Note that `quit-flag' will still be set by typing C-g,
3678 so a quit will be signaled as soon as `inhibit-quit' is nil.
3679 To prevent this happening, set `quit-flag' to nil
3680 before making `inhibit-quit' nil. */);
3681 Vinhibit_quit = Qnil;
3683 Qinhibit_quit = intern_c_string ("inhibit-quit");
3684 staticpro (&Qinhibit_quit);
3686 Qautoload = intern_c_string ("autoload");
3687 staticpro (&Qautoload);
3689 Qdebug_on_error = intern_c_string ("debug-on-error");
3690 staticpro (&Qdebug_on_error);
3692 Qmacro = intern_c_string ("macro");
3693 staticpro (&Qmacro);
3695 Qdeclare = intern_c_string ("declare");
3696 staticpro (&Qdeclare);
3698 /* Note that the process handling also uses Qexit, but we don't want
3699 to staticpro it twice, so we just do it here. */
3700 Qexit = intern_c_string ("exit");
3701 staticpro (&Qexit);
3703 Qinteractive = intern_c_string ("interactive");
3704 staticpro (&Qinteractive);
3706 Qcommandp = intern_c_string ("commandp");
3707 staticpro (&Qcommandp);
3709 Qdefun = intern_c_string ("defun");
3710 staticpro (&Qdefun);
3712 Qand_rest = intern_c_string ("&rest");
3713 staticpro (&Qand_rest);
3715 Qand_optional = intern_c_string ("&optional");
3716 staticpro (&Qand_optional);
3718 Qclosure = intern_c_string ("closure");
3719 staticpro (&Qclosure);
3721 Qdebug = intern_c_string ("debug");
3722 staticpro (&Qdebug);
3724 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3725 doc: /* *Non-nil means enter debugger if an error is signaled.
3726 Does not apply to errors handled by `condition-case' or those
3727 matched by `debug-ignored-errors'.
3728 If the value is a list, an error only means to enter the debugger
3729 if one of its condition symbols appears in the list.
3730 When you evaluate an expression interactively, this variable
3731 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3732 The command `toggle-debug-on-error' toggles this.
3733 See also the variable `debug-on-quit'. */);
3734 Vdebug_on_error = Qnil;
3736 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3737 doc: /* *List of errors for which the debugger should not be called.
3738 Each element may be a condition-name or a regexp that matches error messages.
3739 If any element applies to a given error, that error skips the debugger
3740 and just returns to top level.
3741 This overrides the variable `debug-on-error'.
3742 It does not apply to errors handled by `condition-case'. */);
3743 Vdebug_ignored_errors = Qnil;
3745 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3746 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3747 Does not apply if quit is handled by a `condition-case'. */);
3748 debug_on_quit = 0;
3750 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3751 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3753 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3754 doc: /* Non-nil means debugger may continue execution.
3755 This is nil when the debugger is called under circumstances where it
3756 might not be safe to continue. */);
3757 debugger_may_continue = 1;
3759 DEFVAR_LISP ("debugger", Vdebugger,
3760 doc: /* Function to call to invoke debugger.
3761 If due to frame exit, args are `exit' and the value being returned;
3762 this function's value will be returned instead of that.
3763 If due to error, args are `error' and a list of the args to `signal'.
3764 If due to `apply' or `funcall' entry, one arg, `lambda'.
3765 If due to `eval' entry, one arg, t. */);
3766 Vdebugger = Qnil;
3768 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3769 doc: /* If non-nil, this is a function for `signal' to call.
3770 It receives the same arguments that `signal' was given.
3771 The Edebug package uses this to regain control. */);
3772 Vsignal_hook_function = Qnil;
3774 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3775 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3776 Note that `debug-on-error', `debug-on-quit' and friends
3777 still determine whether to handle the particular condition. */);
3778 Vdebug_on_signal = Qnil;
3780 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3781 doc: /* Function to process declarations in a macro definition.
3782 The function will be called with two args MACRO and DECL.
3783 MACRO is the name of the macro being defined.
3784 DECL is a list `(declare ...)' containing the declarations.
3785 The value the function returns is not used. */);
3786 Vmacro_declaration_function = Qnil;
3788 /* When lexical binding is being used,
3789 vinternal_interpreter_environment is non-nil, and contains an alist
3790 of lexically-bound variable, or (t), indicating an empty
3791 environment. The lisp name of this variable would be
3792 `internal-interpreter-environment' if it weren't hidden.
3793 Every element of this list can be either a cons (VAR . VAL)
3794 specifying a lexical binding, or a single symbol VAR indicating
3795 that this variable should use dynamic scoping. */
3796 Qinternal_interpreter_environment
3797 = intern_c_string ("internal-interpreter-environment");
3798 staticpro (&Qinternal_interpreter_environment);
3799 DEFVAR_LISP ("internal-interpreter-environment",
3800 Vinternal_interpreter_environment,
3801 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3802 When lexical binding is not being used, this variable is nil.
3803 A value of `(t)' indicates an empty environment, otherwise it is an
3804 alist of active lexical bindings. */);
3805 Vinternal_interpreter_environment = Qnil;
3806 /* Don't export this variable to Elisp, so noone can mess with it
3807 (Just imagine if someone makes it buffer-local). */
3808 Funintern (Qinternal_interpreter_environment, Qnil);
3810 Vrun_hooks = intern_c_string ("run-hooks");
3811 staticpro (&Vrun_hooks);
3813 staticpro (&Vautoload_queue);
3814 Vautoload_queue = Qnil;
3815 staticpro (&Vsignaling_function);
3816 Vsignaling_function = Qnil;
3818 defsubr (&Sor);
3819 defsubr (&Sand);
3820 defsubr (&Sif);
3821 defsubr (&Scond);
3822 defsubr (&Sprogn);
3823 defsubr (&Sprog1);
3824 defsubr (&Sprog2);
3825 defsubr (&Ssetq);
3826 defsubr (&Squote);
3827 defsubr (&Sfunction);
3828 defsubr (&Sdefun);
3829 defsubr (&Sdefmacro);
3830 defsubr (&Sdefvar);
3831 defsubr (&Sdefvaralias);
3832 defsubr (&Sdefconst);
3833 defsubr (&Suser_variable_p);
3834 defsubr (&Slet);
3835 defsubr (&SletX);
3836 defsubr (&Swhile);
3837 defsubr (&Smacroexpand);
3838 defsubr (&Scatch);
3839 defsubr (&Sthrow);
3840 defsubr (&Sunwind_protect);
3841 defsubr (&Scondition_case);
3842 defsubr (&Ssignal);
3843 defsubr (&Sinteractive_p);
3844 defsubr (&Scalled_interactively_p);
3845 defsubr (&Scommandp);
3846 defsubr (&Sautoload);
3847 defsubr (&Seval);
3848 defsubr (&Sapply);
3849 defsubr (&Sfuncall);
3850 defsubr (&Srun_hooks);
3851 defsubr (&Srun_hook_with_args);
3852 defsubr (&Srun_hook_with_args_until_success);
3853 defsubr (&Srun_hook_with_args_until_failure);
3854 defsubr (&Srun_hook_wrapped);
3855 defsubr (&Sfetch_bytecode);
3856 defsubr (&Sbacktrace_debug);
3857 defsubr (&Sbacktrace);
3858 defsubr (&Sbacktrace_frame);
3859 defsubr (&Sspecial_variable_p);
3860 defsubr (&Sfunctionp);