Checked in at last a lot of improvementes and bug fixes. The oldest dating
[emacs/old-mirror.git] / src / eval.c
blob59150df232a09125f0c4f268096da3930dc4d1d0
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 1999 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 2, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
24 #include "lisp.h"
25 #include "blockinput.h"
27 #ifndef standalone
28 #include "commands.h"
29 #include "keyboard.h"
30 #else
31 #define INTERACTIVE 1
32 #endif
34 #include <setjmp.h>
36 /* This definition is duplicated in alloc.c and keyboard.c */
37 /* Putting it in lisp.h makes cc bomb out! */
39 struct backtrace
41 struct backtrace *next;
42 Lisp_Object *function;
43 Lisp_Object *args; /* Points to vector of args. */
44 int nargs; /* Length of vector.
45 If nargs is UNEVALLED, args points to slot holding
46 list of unevalled args */
47 char evalargs;
48 /* Nonzero means call value of debugger when done with this operation. */
49 char debug_on_exit;
52 struct backtrace *backtrace_list;
54 /* This structure helps implement the `catch' and `throw' control
55 structure. A struct catchtag contains all the information needed
56 to restore the state of the interpreter after a non-local jump.
58 Handlers for error conditions (represented by `struct handler'
59 structures) just point to a catch tag to do the cleanup required
60 for their jumps.
62 catchtag structures are chained together in the C calling stack;
63 the `next' member points to the next outer catchtag.
65 A call like (throw TAG VAL) searches for a catchtag whose `tag'
66 member is TAG, and then unbinds to it. The `val' member is used to
67 hold VAL while the stack is unwound; `val' is returned as the value
68 of the catch form.
70 All the other members are concerned with restoring the interpreter
71 state. */
72 struct catchtag
74 Lisp_Object tag;
75 Lisp_Object val;
76 struct catchtag *next;
77 struct gcpro *gcpro;
78 jmp_buf jmp;
79 struct backtrace *backlist;
80 struct handler *handlerlist;
81 int lisp_eval_depth;
82 int pdlcount;
83 int poll_suppress_count;
86 struct catchtag *catchlist;
88 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
89 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
90 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
91 Lisp_Object Qand_rest, Qand_optional;
92 Lisp_Object Qdebug_on_error;
94 /* This holds either the symbol `run-hooks' or nil.
95 It is nil at an early stage of startup, and when Emacs
96 is shutting down. */
97 Lisp_Object Vrun_hooks;
99 /* Non-nil means record all fset's and provide's, to be undone
100 if the file being autoloaded is not fully loaded.
101 They are recorded by being consed onto the front of Vautoload_queue:
102 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
104 Lisp_Object Vautoload_queue;
106 /* Current number of specbindings allocated in specpdl. */
107 int specpdl_size;
109 /* Pointer to beginning of specpdl. */
110 struct specbinding *specpdl;
112 /* Pointer to first unused element in specpdl. */
113 struct specbinding *specpdl_ptr;
115 /* Maximum size allowed for specpdl allocation */
116 int max_specpdl_size;
118 /* Depth in Lisp evaluations and function calls. */
119 int lisp_eval_depth;
121 /* Maximum allowed depth in Lisp evaluations and function calls. */
122 int max_lisp_eval_depth;
124 /* Nonzero means enter debugger before next function call */
125 int debug_on_next_call;
127 /* List of conditions (non-nil atom means all) which cause a backtrace
128 if an error is handled by the command loop's error handler. */
129 Lisp_Object Vstack_trace_on_error;
131 /* List of conditions (non-nil atom means all) which enter the debugger
132 if an error is handled by the command loop's error handler. */
133 Lisp_Object Vdebug_on_error;
135 /* List of conditions and regexps specifying error messages which
136 do not enter the debugger even if Vdebug_on_errors says they should. */
137 Lisp_Object Vdebug_ignored_errors;
139 /* Non-nil means call the debugger even if the error will be handled. */
140 Lisp_Object Vdebug_on_signal;
142 /* Hook for edebug to use. */
143 Lisp_Object Vsignal_hook_function;
145 /* Nonzero means enter debugger if a quit signal
146 is handled by the command loop's error handler. */
147 int debug_on_quit;
149 /* The value of num_nonmacro_input_events as of the last time we
150 started to enter the debugger. If we decide to enter the debugger
151 again when this is still equal to num_nonmacro_input_events, then we
152 know that the debugger itself has an error, and we should just
153 signal the error instead of entering an infinite loop of debugger
154 invocations. */
155 int when_entered_debugger;
157 Lisp_Object Vdebugger;
159 void specbind (), record_unwind_protect ();
161 Lisp_Object run_hook_with_args ();
163 Lisp_Object funcall_lambda ();
164 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
166 void
167 init_eval_once ()
169 specpdl_size = 50;
170 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
171 specpdl_ptr = specpdl;
172 max_specpdl_size = 600;
173 max_lisp_eval_depth = 300;
175 Vrun_hooks = Qnil;
178 void
179 init_eval ()
181 specpdl_ptr = specpdl;
182 catchlist = 0;
183 handlerlist = 0;
184 backtrace_list = 0;
185 Vquit_flag = Qnil;
186 debug_on_next_call = 0;
187 lisp_eval_depth = 0;
188 /* This is less than the initial value of num_nonmacro_input_events. */
189 when_entered_debugger = -1;
192 Lisp_Object
193 call_debugger (arg)
194 Lisp_Object arg;
196 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
197 max_lisp_eval_depth = lisp_eval_depth + 20;
198 if (specpdl_size + 40 > max_specpdl_size)
199 max_specpdl_size = specpdl_size + 40;
200 debug_on_next_call = 0;
201 when_entered_debugger = num_nonmacro_input_events;
202 return apply1 (Vdebugger, arg);
205 void
206 do_debug_on_call (code)
207 Lisp_Object code;
209 debug_on_next_call = 0;
210 backtrace_list->debug_on_exit = 1;
211 call_debugger (Fcons (code, Qnil));
214 /* NOTE!!! Every function that can call EVAL must protect its args
215 and temporaries from garbage collection while it needs them.
216 The definition of `For' shows what you have to do. */
218 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
219 "Eval args until one of them yields non-nil, then return that value.\n\
220 The remaining args are not evalled at all.\n\
221 If all args return nil, return nil.")
222 (args)
223 Lisp_Object args;
225 register Lisp_Object val;
226 Lisp_Object args_left;
227 struct gcpro gcpro1;
229 if (NILP(args))
230 return Qnil;
232 args_left = args;
233 GCPRO1 (args_left);
237 val = Feval (Fcar (args_left));
238 if (!NILP (val))
239 break;
240 args_left = Fcdr (args_left);
242 while (!NILP(args_left));
244 UNGCPRO;
245 return val;
248 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
249 "Eval args until one of them yields nil, then return nil.\n\
250 The remaining args are not evalled at all.\n\
251 If no arg yields nil, return the last arg's value.")
252 (args)
253 Lisp_Object args;
255 register Lisp_Object val;
256 Lisp_Object args_left;
257 struct gcpro gcpro1;
259 if (NILP(args))
260 return Qt;
262 args_left = args;
263 GCPRO1 (args_left);
267 val = Feval (Fcar (args_left));
268 if (NILP (val))
269 break;
270 args_left = Fcdr (args_left);
272 while (!NILP(args_left));
274 UNGCPRO;
275 return val;
278 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
279 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
280 Returns the value of THEN or the value of the last of the ELSE's.\n\
281 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
282 If COND yields nil, and there are no ELSE's, the value is nil.")
283 (args)
284 Lisp_Object args;
286 register Lisp_Object cond;
287 struct gcpro gcpro1;
289 GCPRO1 (args);
290 cond = Feval (Fcar (args));
291 UNGCPRO;
293 if (!NILP (cond))
294 return Feval (Fcar (Fcdr (args)));
295 return Fprogn (Fcdr (Fcdr (args)));
298 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
299 "(cond CLAUSES...): try each clause until one succeeds.\n\
300 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
301 and, if the value is non-nil, this clause succeeds:\n\
302 then the expressions in BODY are evaluated and the last one's\n\
303 value is the value of the cond-form.\n\
304 If no clause succeeds, cond returns nil.\n\
305 If a clause has one element, as in (CONDITION),\n\
306 CONDITION's value if non-nil is returned from the cond-form.")
307 (args)
308 Lisp_Object args;
310 register Lisp_Object clause, val;
311 struct gcpro gcpro1;
313 val = Qnil;
314 GCPRO1 (args);
315 while (!NILP (args))
317 clause = Fcar (args);
318 val = Feval (Fcar (clause));
319 if (!NILP (val))
321 if (!EQ (XCDR (clause), Qnil))
322 val = Fprogn (XCDR (clause));
323 break;
325 args = XCDR (args);
327 UNGCPRO;
329 return val;
332 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
333 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
334 (args)
335 Lisp_Object args;
337 register Lisp_Object val, tem;
338 Lisp_Object args_left;
339 struct gcpro gcpro1;
341 /* In Mocklisp code, symbols at the front of the progn arglist
342 are to be bound to zero. */
343 if (!EQ (Vmocklisp_arguments, Qt))
345 val = make_number (0);
346 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
348 QUIT;
349 specbind (tem, val), args = Fcdr (args);
353 if (NILP(args))
354 return Qnil;
356 args_left = args;
357 GCPRO1 (args_left);
361 val = Feval (Fcar (args_left));
362 args_left = Fcdr (args_left);
364 while (!NILP(args_left));
366 UNGCPRO;
367 return val;
370 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
371 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
372 The value of FIRST is saved during the evaluation of the remaining args,\n\
373 whose values are discarded.")
374 (args)
375 Lisp_Object args;
377 Lisp_Object val;
378 register Lisp_Object args_left;
379 struct gcpro gcpro1, gcpro2;
380 register int argnum = 0;
382 if (NILP(args))
383 return Qnil;
385 args_left = args;
386 val = Qnil;
387 GCPRO2 (args, val);
391 if (!(argnum++))
392 val = Feval (Fcar (args_left));
393 else
394 Feval (Fcar (args_left));
395 args_left = Fcdr (args_left);
397 while (!NILP(args_left));
399 UNGCPRO;
400 return val;
403 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
404 "(prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
405 The value of Y is saved during the evaluation of the remaining args,\n\
406 whose values are discarded.")
407 (args)
408 Lisp_Object args;
410 Lisp_Object val;
411 register Lisp_Object args_left;
412 struct gcpro gcpro1, gcpro2;
413 register int argnum = -1;
415 val = Qnil;
417 if (NILP (args))
418 return Qnil;
420 args_left = args;
421 val = Qnil;
422 GCPRO2 (args, val);
426 if (!(argnum++))
427 val = Feval (Fcar (args_left));
428 else
429 Feval (Fcar (args_left));
430 args_left = Fcdr (args_left);
432 while (!NILP (args_left));
434 UNGCPRO;
435 return val;
438 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
439 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
440 The symbols SYM are variables; they are literal (not evaluated).\n\
441 The values VAL are expressions; they are evaluated.\n\
442 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
443 The second VAL is not computed until after the first SYM is set, and so on;\n\
444 each VAL can use the new value of variables set earlier in the `setq'.\n\
445 The return value of the `setq' form is the value of the last VAL.")
446 (args)
447 Lisp_Object args;
449 register Lisp_Object args_left;
450 register Lisp_Object val, sym;
451 struct gcpro gcpro1;
453 if (NILP(args))
454 return Qnil;
456 args_left = args;
457 GCPRO1 (args);
461 val = Feval (Fcar (Fcdr (args_left)));
462 sym = Fcar (args_left);
463 Fset (sym, val);
464 args_left = Fcdr (Fcdr (args_left));
466 while (!NILP(args_left));
468 UNGCPRO;
469 return val;
472 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
473 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
474 (args)
475 Lisp_Object args;
477 return Fcar (args);
480 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
481 "Like `quote', but preferred for objects which are functions.\n\
482 In byte compilation, `function' causes its argument to be compiled.\n\
483 `quote' cannot do that.")
484 (args)
485 Lisp_Object args;
487 return Fcar (args);
490 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
491 "Return t if function in which this appears was called interactively.\n\
492 This means that the function was called with call-interactively (which\n\
493 includes being called as the binding of a key)\n\
494 and input is currently coming from the keyboard (not in keyboard macro).")
497 register struct backtrace *btp;
498 register Lisp_Object fun;
500 if (!INTERACTIVE)
501 return Qnil;
503 btp = backtrace_list;
505 /* If this isn't a byte-compiled function, there may be a frame at
506 the top for Finteractive_p itself. If so, skip it. */
507 fun = Findirect_function (*btp->function);
508 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
509 btp = btp->next;
511 /* If we're running an Emacs 18-style byte-compiled function, there
512 may be a frame for Fbytecode. Now, given the strictest
513 definition, this function isn't really being called
514 interactively, but because that's the way Emacs 18 always builds
515 byte-compiled functions, we'll accept it for now. */
516 if (EQ (*btp->function, Qbytecode))
517 btp = btp->next;
519 /* If this isn't a byte-compiled function, then we may now be
520 looking at several frames for special forms. Skip past them. */
521 while (btp &&
522 btp->nargs == UNEVALLED)
523 btp = btp->next;
525 /* btp now points at the frame of the innermost function that isn't
526 a special form, ignoring frames for Finteractive_p and/or
527 Fbytecode at the top. If this frame is for a built-in function
528 (such as load or eval-region) return nil. */
529 fun = Findirect_function (*btp->function);
530 if (SUBRP (fun))
531 return Qnil;
532 /* btp points to the frame of a Lisp function that called interactive-p.
533 Return t if that function was called interactively. */
534 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
535 return Qt;
536 return Qnil;
539 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
540 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
541 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
542 See also the function `interactive'.")
543 (args)
544 Lisp_Object args;
546 register Lisp_Object fn_name;
547 register Lisp_Object defn;
549 fn_name = Fcar (args);
550 defn = Fcons (Qlambda, Fcdr (args));
551 if (!NILP (Vpurify_flag))
552 defn = Fpurecopy (defn);
553 Ffset (fn_name, defn);
554 LOADHIST_ATTACH (fn_name);
555 return fn_name;
558 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
559 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
560 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
561 When the macro is called, as in (NAME ARGS...),\n\
562 the function (lambda ARGLIST BODY...) is applied to\n\
563 the list ARGS... as it appears in the expression,\n\
564 and the result should be a form to be evaluated instead of the original.")
565 (args)
566 Lisp_Object args;
568 register Lisp_Object fn_name;
569 register Lisp_Object defn;
571 fn_name = Fcar (args);
572 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
573 if (!NILP (Vpurify_flag))
574 defn = Fpurecopy (defn);
575 Ffset (fn_name, defn);
576 LOADHIST_ATTACH (fn_name);
577 return fn_name;
580 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
581 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
582 You are not required to define a variable in order to use it,\n\
583 but the definition can supply documentation and an initial value\n\
584 in a way that tags can recognize.\n\n\
585 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
586 If SYMBOL is buffer-local, its default value is what is set;\n\
587 buffer-local values are not affected.\n\
588 INITVALUE and DOCSTRING are optional.\n\
589 If DOCSTRING starts with *, this variable is identified as a user option.\n\
590 This means that M-x set-variable and M-x edit-options recognize it.\n\
591 If INITVALUE is missing, SYMBOL's value is not set.")
592 (args)
593 Lisp_Object args;
595 register Lisp_Object sym, tem, tail;
597 sym = Fcar (args);
598 tail = Fcdr (args);
599 if (!NILP (Fcdr (Fcdr (tail))))
600 error ("too many arguments");
602 if (!NILP (tail))
604 tem = Fdefault_boundp (sym);
605 if (NILP (tem))
606 Fset_default (sym, Feval (Fcar (Fcdr (args))));
608 tail = Fcdr (Fcdr (args));
609 if (!NILP (Fcar (tail)))
611 tem = Fcar (tail);
612 if (!NILP (Vpurify_flag))
613 tem = Fpurecopy (tem);
614 Fput (sym, Qvariable_documentation, tem);
616 LOADHIST_ATTACH (sym);
617 return sym;
620 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
621 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
622 The intent is that neither programs nor users should ever change this value.\n\
623 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
624 If SYMBOL is buffer-local, its default value is what is set;\n\
625 buffer-local values are not affected.\n\
626 DOCSTRING is optional.")
627 (args)
628 Lisp_Object args;
630 register Lisp_Object sym, tem;
632 sym = Fcar (args);
633 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
634 error ("too many arguments");
636 Fset_default (sym, Feval (Fcar (Fcdr (args))));
637 tem = Fcar (Fcdr (Fcdr (args)));
638 if (!NILP (tem))
640 if (!NILP (Vpurify_flag))
641 tem = Fpurecopy (tem);
642 Fput (sym, Qvariable_documentation, tem);
644 LOADHIST_ATTACH (sym);
645 return sym;
648 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
649 "Returns t if VARIABLE is intended to be set and modified by users.\n\
650 \(The alternative is a variable used internally in a Lisp program.)\n\
651 Determined by whether the first character of the documentation\n\
652 for the variable is `*'.")
653 (variable)
654 Lisp_Object variable;
656 Lisp_Object documentation;
658 if (!SYMBOLP (variable))
659 return Qnil;
661 documentation = Fget (variable, Qvariable_documentation);
662 if (INTEGERP (documentation) && XINT (documentation) < 0)
663 return Qt;
664 if (STRINGP (documentation)
665 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
666 return Qt;
667 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
668 if (CONSP (documentation)
669 && STRINGP (XCAR (documentation))
670 && INTEGERP (XCDR (documentation))
671 && XINT (XCDR (documentation)) < 0)
672 return Qt;
673 return Qnil;
676 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
677 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
678 The value of the last form in BODY is returned.\n\
679 Each element of VARLIST is a symbol (which is bound to nil)\n\
680 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
681 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
682 (args)
683 Lisp_Object args;
685 Lisp_Object varlist, val, elt;
686 int count = specpdl_ptr - specpdl;
687 struct gcpro gcpro1, gcpro2, gcpro3;
689 GCPRO3 (args, elt, varlist);
691 varlist = Fcar (args);
692 while (!NILP (varlist))
694 QUIT;
695 elt = Fcar (varlist);
696 if (SYMBOLP (elt))
697 specbind (elt, Qnil);
698 else if (! NILP (Fcdr (Fcdr (elt))))
699 Fsignal (Qerror,
700 Fcons (build_string ("`let' bindings can have only one value-form"),
701 elt));
702 else
704 val = Feval (Fcar (Fcdr (elt)));
705 specbind (Fcar (elt), val);
707 varlist = Fcdr (varlist);
709 UNGCPRO;
710 val = Fprogn (Fcdr (args));
711 return unbind_to (count, val);
714 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
715 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
716 The value of the last form in BODY is returned.\n\
717 Each element of VARLIST is a symbol (which is bound to nil)\n\
718 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
719 All the VALUEFORMs are evalled before any symbols are bound.")
720 (args)
721 Lisp_Object args;
723 Lisp_Object *temps, tem;
724 register Lisp_Object elt, varlist;
725 int count = specpdl_ptr - specpdl;
726 register int argnum;
727 struct gcpro gcpro1, gcpro2;
729 varlist = Fcar (args);
731 /* Make space to hold the values to give the bound variables */
732 elt = Flength (varlist);
733 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
735 /* Compute the values and store them in `temps' */
737 GCPRO2 (args, *temps);
738 gcpro2.nvars = 0;
740 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
742 QUIT;
743 elt = Fcar (varlist);
744 if (SYMBOLP (elt))
745 temps [argnum++] = Qnil;
746 else if (! NILP (Fcdr (Fcdr (elt))))
747 Fsignal (Qerror,
748 Fcons (build_string ("`let' bindings can have only one value-form"),
749 elt));
750 else
751 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
752 gcpro2.nvars = argnum;
754 UNGCPRO;
756 varlist = Fcar (args);
757 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
759 elt = Fcar (varlist);
760 tem = temps[argnum++];
761 if (SYMBOLP (elt))
762 specbind (elt, tem);
763 else
764 specbind (Fcar (elt), tem);
767 elt = Fprogn (Fcdr (args));
768 return unbind_to (count, elt);
771 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
772 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
773 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
774 until TEST returns nil.")
775 (args)
776 Lisp_Object args;
778 Lisp_Object test, body, tem;
779 struct gcpro gcpro1, gcpro2;
781 GCPRO2 (test, body);
783 test = Fcar (args);
784 body = Fcdr (args);
785 while (tem = Feval (test),
786 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
788 QUIT;
789 Fprogn (body);
792 UNGCPRO;
793 return Qnil;
796 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
797 "Return result of expanding macros at top level of FORM.\n\
798 If FORM is not a macro call, it is returned unchanged.\n\
799 Otherwise, the macro is expanded and the expansion is considered\n\
800 in place of FORM. When a non-macro-call results, it is returned.\n\n\
801 The second optional arg ENVIRONMENT species an environment of macro\n\
802 definitions to shadow the loaded ones for use in file byte-compilation.")
803 (form, environment)
804 Lisp_Object form;
805 Lisp_Object environment;
807 /* With cleanups from Hallvard Furuseth. */
808 register Lisp_Object expander, sym, def, tem;
810 while (1)
812 /* Come back here each time we expand a macro call,
813 in case it expands into another macro call. */
814 if (!CONSP (form))
815 break;
816 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
817 def = sym = XCAR (form);
818 tem = Qnil;
819 /* Trace symbols aliases to other symbols
820 until we get a symbol that is not an alias. */
821 while (SYMBOLP (def))
823 QUIT;
824 sym = def;
825 tem = Fassq (sym, environment);
826 if (NILP (tem))
828 def = XSYMBOL (sym)->function;
829 if (!EQ (def, Qunbound))
830 continue;
832 break;
834 /* Right now TEM is the result from SYM in ENVIRONMENT,
835 and if TEM is nil then DEF is SYM's function definition. */
836 if (NILP (tem))
838 /* SYM is not mentioned in ENVIRONMENT.
839 Look at its function definition. */
840 if (EQ (def, Qunbound) || !CONSP (def))
841 /* Not defined or definition not suitable */
842 break;
843 if (EQ (XCAR (def), Qautoload))
845 /* Autoloading function: will it be a macro when loaded? */
846 tem = Fnth (make_number (4), def);
847 if (EQ (tem, Qt) || EQ (tem, Qmacro))
848 /* Yes, load it and try again. */
850 struct gcpro gcpro1;
851 GCPRO1 (form);
852 do_autoload (def, sym);
853 UNGCPRO;
854 continue;
856 else
857 break;
859 else if (!EQ (XCAR (def), Qmacro))
860 break;
861 else expander = XCDR (def);
863 else
865 expander = XCDR (tem);
866 if (NILP (expander))
867 break;
869 form = apply1 (expander, XCDR (form));
871 return form;
874 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
875 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
876 TAG is evalled to get the tag to use; it must not be nil.\n\
878 Then the BODY is executed.\n\
879 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
880 If no throw happens, `catch' returns the value of the last BODY form.\n\
881 If a throw happens, it specifies the value to return from `catch'.")
882 (args)
883 Lisp_Object args;
885 register Lisp_Object tag;
886 struct gcpro gcpro1;
888 GCPRO1 (args);
889 tag = Feval (Fcar (args));
890 UNGCPRO;
891 return internal_catch (tag, Fprogn, Fcdr (args));
894 /* Set up a catch, then call C function FUNC on argument ARG.
895 FUNC should return a Lisp_Object.
896 This is how catches are done from within C code. */
898 Lisp_Object
899 internal_catch (tag, func, arg)
900 Lisp_Object tag;
901 Lisp_Object (*func) ();
902 Lisp_Object arg;
904 /* This structure is made part of the chain `catchlist'. */
905 struct catchtag c;
907 /* Fill in the components of c, and put it on the list. */
908 c.next = catchlist;
909 c.tag = tag;
910 c.val = Qnil;
911 c.backlist = backtrace_list;
912 c.handlerlist = handlerlist;
913 c.lisp_eval_depth = lisp_eval_depth;
914 c.pdlcount = specpdl_ptr - specpdl;
915 c.poll_suppress_count = poll_suppress_count;
916 c.gcpro = gcprolist;
917 catchlist = &c;
919 /* Call FUNC. */
920 if (! _setjmp (c.jmp))
921 c.val = (*func) (arg);
923 /* Throw works by a longjmp that comes right here. */
924 catchlist = c.next;
925 return c.val;
928 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
929 jump to that CATCH, returning VALUE as the value of that catch.
931 This is the guts Fthrow and Fsignal; they differ only in the way
932 they choose the catch tag to throw to. A catch tag for a
933 condition-case form has a TAG of Qnil.
935 Before each catch is discarded, unbind all special bindings and
936 execute all unwind-protect clauses made above that catch. Unwind
937 the handler stack as we go, so that the proper handlers are in
938 effect for each unwind-protect clause we run. At the end, restore
939 some static info saved in CATCH, and longjmp to the location
940 specified in the
942 This is used for correct unwinding in Fthrow and Fsignal. */
944 static void
945 unwind_to_catch (catch, value)
946 struct catchtag *catch;
947 Lisp_Object value;
949 register int last_time;
951 /* Save the value in the tag. */
952 catch->val = value;
954 /* Restore the polling-suppression count. */
955 set_poll_suppress_count (catch->poll_suppress_count);
959 last_time = catchlist == catch;
961 /* Unwind the specpdl stack, and then restore the proper set of
962 handlers. */
963 unbind_to (catchlist->pdlcount, Qnil);
964 handlerlist = catchlist->handlerlist;
965 catchlist = catchlist->next;
967 while (! last_time);
969 gcprolist = catch->gcpro;
970 backtrace_list = catch->backlist;
971 lisp_eval_depth = catch->lisp_eval_depth;
973 _longjmp (catch->jmp, 1);
976 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
977 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
978 Both TAG and VALUE are evalled.")
979 (tag, value)
980 register Lisp_Object tag, value;
982 register struct catchtag *c;
984 while (1)
986 if (!NILP (tag))
987 for (c = catchlist; c; c = c->next)
989 if (EQ (c->tag, tag))
990 unwind_to_catch (c, value);
992 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
997 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
998 "Do BODYFORM, protecting with UNWINDFORMS.\n\
999 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
1000 If BODYFORM completes normally, its value is returned\n\
1001 after executing the UNWINDFORMS.\n\
1002 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1003 (args)
1004 Lisp_Object args;
1006 Lisp_Object val;
1007 int count = specpdl_ptr - specpdl;
1009 record_unwind_protect (0, Fcdr (args));
1010 val = Feval (Fcar (args));
1011 return unbind_to (count, val);
1014 /* Chain of condition handlers currently in effect.
1015 The elements of this chain are contained in the stack frames
1016 of Fcondition_case and internal_condition_case.
1017 When an error is signaled (by calling Fsignal, below),
1018 this chain is searched for an element that applies. */
1020 struct handler *handlerlist;
1022 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1023 "Regain control when an error is signaled.\n\
1024 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
1025 executes BODYFORM and returns its value if no error happens.\n\
1026 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1027 where the BODY is made of Lisp expressions.\n\n\
1028 A handler is applicable to an error\n\
1029 if CONDITION-NAME is one of the error's condition names.\n\
1030 If an error happens, the first applicable handler is run.\n\
1032 The car of a handler may be a list of condition names\n\
1033 instead of a single condition name.\n\
1035 When a handler handles an error,\n\
1036 control returns to the condition-case and the handler BODY... is executed\n\
1037 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1038 VAR may be nil; then you do not get access to the signal information.\n\
1040 The value of the last BODY form is returned from the condition-case.\n\
1041 See also the function `signal' for more info.")
1042 (args)
1043 Lisp_Object args;
1045 Lisp_Object val;
1046 struct catchtag c;
1047 struct handler h;
1048 register Lisp_Object var, bodyform, handlers;
1050 var = Fcar (args);
1051 bodyform = Fcar (Fcdr (args));
1052 handlers = Fcdr (Fcdr (args));
1053 CHECK_SYMBOL (var, 0);
1055 for (val = handlers; ! NILP (val); val = Fcdr (val))
1057 Lisp_Object tem;
1058 tem = Fcar (val);
1059 if (! (NILP (tem)
1060 || (CONSP (tem)
1061 && (SYMBOLP (XCAR (tem))
1062 || CONSP (XCAR (tem))))))
1063 error ("Invalid condition handler", tem);
1066 c.tag = Qnil;
1067 c.val = Qnil;
1068 c.backlist = backtrace_list;
1069 c.handlerlist = handlerlist;
1070 c.lisp_eval_depth = lisp_eval_depth;
1071 c.pdlcount = specpdl_ptr - specpdl;
1072 c.poll_suppress_count = poll_suppress_count;
1073 c.gcpro = gcprolist;
1074 if (_setjmp (c.jmp))
1076 if (!NILP (h.var))
1077 specbind (h.var, c.val);
1078 val = Fprogn (Fcdr (h.chosen_clause));
1080 /* Note that this just undoes the binding of h.var; whoever
1081 longjumped to us unwound the stack to c.pdlcount before
1082 throwing. */
1083 unbind_to (c.pdlcount, Qnil);
1084 return val;
1086 c.next = catchlist;
1087 catchlist = &c;
1089 h.var = var;
1090 h.handler = handlers;
1091 h.next = handlerlist;
1092 h.tag = &c;
1093 handlerlist = &h;
1095 val = Feval (bodyform);
1096 catchlist = c.next;
1097 handlerlist = h.next;
1098 return val;
1101 /* Call the function BFUN with no arguments, catching errors within it
1102 according to HANDLERS. If there is an error, call HFUN with
1103 one argument which is the data that describes the error:
1104 (SIGNALNAME . DATA)
1106 HANDLERS can be a list of conditions to catch.
1107 If HANDLERS is Qt, catch all errors.
1108 If HANDLERS is Qerror, catch all errors
1109 but allow the debugger to run if that is enabled. */
1111 Lisp_Object
1112 internal_condition_case (bfun, handlers, hfun)
1113 Lisp_Object (*bfun) ();
1114 Lisp_Object handlers;
1115 Lisp_Object (*hfun) ();
1117 Lisp_Object val;
1118 struct catchtag c;
1119 struct handler h;
1121 /* Since Fsignal resets this to 0, it had better be 0 now
1122 or else we have a potential bug. */
1123 if (interrupt_input_blocked != 0)
1124 abort ();
1126 c.tag = Qnil;
1127 c.val = Qnil;
1128 c.backlist = backtrace_list;
1129 c.handlerlist = handlerlist;
1130 c.lisp_eval_depth = lisp_eval_depth;
1131 c.pdlcount = specpdl_ptr - specpdl;
1132 c.poll_suppress_count = poll_suppress_count;
1133 c.gcpro = gcprolist;
1134 if (_setjmp (c.jmp))
1136 return (*hfun) (c.val);
1138 c.next = catchlist;
1139 catchlist = &c;
1140 h.handler = handlers;
1141 h.var = Qnil;
1142 h.next = handlerlist;
1143 h.tag = &c;
1144 handlerlist = &h;
1146 val = (*bfun) ();
1147 catchlist = c.next;
1148 handlerlist = h.next;
1149 return val;
1152 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1154 Lisp_Object
1155 internal_condition_case_1 (bfun, arg, handlers, hfun)
1156 Lisp_Object (*bfun) ();
1157 Lisp_Object arg;
1158 Lisp_Object handlers;
1159 Lisp_Object (*hfun) ();
1161 Lisp_Object val;
1162 struct catchtag c;
1163 struct handler h;
1165 c.tag = Qnil;
1166 c.val = Qnil;
1167 c.backlist = backtrace_list;
1168 c.handlerlist = handlerlist;
1169 c.lisp_eval_depth = lisp_eval_depth;
1170 c.pdlcount = specpdl_ptr - specpdl;
1171 c.poll_suppress_count = poll_suppress_count;
1172 c.gcpro = gcprolist;
1173 if (_setjmp (c.jmp))
1175 return (*hfun) (c.val);
1177 c.next = catchlist;
1178 catchlist = &c;
1179 h.handler = handlers;
1180 h.var = Qnil;
1181 h.next = handlerlist;
1182 h.tag = &c;
1183 handlerlist = &h;
1185 val = (*bfun) (arg);
1186 catchlist = c.next;
1187 handlerlist = h.next;
1188 return val;
1191 static Lisp_Object find_handler_clause ();
1193 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1194 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1195 This function does not return.\n\n\
1196 An error symbol is a symbol with an `error-conditions' property\n\
1197 that is a list of condition names.\n\
1198 A handler for any of those names will get to handle this signal.\n\
1199 The symbol `error' should normally be one of them.\n\
1201 DATA should be a list. Its elements are printed as part of the error message.\n\
1202 If the signal is handled, DATA is made available to the handler.\n\
1203 See also the function `condition-case'.")
1204 (error_symbol, data)
1205 Lisp_Object error_symbol, data;
1207 /* When memory is full, ERROR-SYMBOL is nil,
1208 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1209 register struct handler *allhandlers = handlerlist;
1210 Lisp_Object conditions;
1211 extern int gc_in_progress;
1212 extern int waiting_for_input;
1213 Lisp_Object debugger_value;
1214 Lisp_Object string;
1215 Lisp_Object real_error_symbol;
1216 extern int display_busy_cursor_p;
1218 immediate_quit = 0;
1219 if (gc_in_progress || waiting_for_input)
1220 abort ();
1222 TOTALLY_UNBLOCK_INPUT;
1224 if (NILP (error_symbol))
1225 real_error_symbol = Fcar (data);
1226 else
1227 real_error_symbol = error_symbol;
1229 #ifdef HAVE_X_WINDOWS
1230 if (display_busy_cursor_p)
1231 Fx_hide_busy_cursor (Qt);
1232 #endif
1234 /* This hook is used by edebug. */
1235 if (! NILP (Vsignal_hook_function))
1236 call2 (Vsignal_hook_function, error_symbol, data);
1238 conditions = Fget (real_error_symbol, Qerror_conditions);
1240 for (; handlerlist; handlerlist = handlerlist->next)
1242 register Lisp_Object clause;
1243 clause = find_handler_clause (handlerlist->handler, conditions,
1244 error_symbol, data, &debugger_value);
1246 #if 0 /* Most callers are not prepared to handle gc if this returns.
1247 So, since this feature is not very useful, take it out. */
1248 /* If have called debugger and user wants to continue,
1249 just return nil. */
1250 if (EQ (clause, Qlambda))
1251 return debugger_value;
1252 #else
1253 if (EQ (clause, Qlambda))
1255 /* We can't return values to code which signaled an error, but we
1256 can continue code which has signaled a quit. */
1257 if (EQ (real_error_symbol, Qquit))
1258 return Qnil;
1259 else
1260 error ("Cannot return from the debugger in an error");
1262 #endif
1264 if (!NILP (clause))
1266 Lisp_Object unwind_data;
1267 struct handler *h = handlerlist;
1269 handlerlist = allhandlers;
1271 if (NILP (error_symbol))
1272 unwind_data = data;
1273 else
1274 unwind_data = Fcons (error_symbol, data);
1275 h->chosen_clause = clause;
1276 unwind_to_catch (h->tag, unwind_data);
1280 handlerlist = allhandlers;
1281 /* If no handler is present now, try to run the debugger,
1282 and if that fails, throw to top level. */
1283 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1284 if (catchlist != 0)
1285 Fthrow (Qtop_level, Qt);
1287 if (! NILP (error_symbol))
1288 data = Fcons (error_symbol, data);
1290 string = Ferror_message_string (data);
1291 fatal ("%s", XSTRING (string)->data, 0);
1294 /* Return nonzero iff LIST is a non-nil atom or
1295 a list containing one of CONDITIONS. */
1297 static int
1298 wants_debugger (list, conditions)
1299 Lisp_Object list, conditions;
1301 if (NILP (list))
1302 return 0;
1303 if (! CONSP (list))
1304 return 1;
1306 while (CONSP (conditions))
1308 Lisp_Object this, tail;
1309 this = XCAR (conditions);
1310 for (tail = list; CONSP (tail); tail = XCDR (tail))
1311 if (EQ (XCAR (tail), this))
1312 return 1;
1313 conditions = XCDR (conditions);
1315 return 0;
1318 /* Return 1 if an error with condition-symbols CONDITIONS,
1319 and described by SIGNAL-DATA, should skip the debugger
1320 according to debugger-ignore-errors. */
1322 static int
1323 skip_debugger (conditions, data)
1324 Lisp_Object conditions, data;
1326 Lisp_Object tail;
1327 int first_string = 1;
1328 Lisp_Object error_message;
1330 for (tail = Vdebug_ignored_errors; CONSP (tail);
1331 tail = XCDR (tail))
1333 if (STRINGP (XCAR (tail)))
1335 if (first_string)
1337 error_message = Ferror_message_string (data);
1338 first_string = 0;
1340 if (fast_string_match (XCAR (tail), error_message) >= 0)
1341 return 1;
1343 else
1345 Lisp_Object contail;
1347 for (contail = conditions; CONSP (contail);
1348 contail = XCDR (contail))
1349 if (EQ (XCAR (tail), XCAR (contail)))
1350 return 1;
1354 return 0;
1357 /* Value of Qlambda means we have called debugger and user has continued.
1358 There are two ways to pass SIG and DATA:
1359 = SIG is the error symbol, and DATA is the rest of the data.
1360 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1361 This is for memory-full errors only.
1363 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1365 static Lisp_Object
1366 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1367 Lisp_Object handlers, conditions, sig, data;
1368 Lisp_Object *debugger_value_ptr;
1370 register Lisp_Object h;
1371 register Lisp_Object tem;
1373 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1374 return Qt;
1375 /* error is used similarly, but means print an error message
1376 and run the debugger if that is enabled. */
1377 if (EQ (handlers, Qerror)
1378 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1379 there is a handler. */
1381 int count = specpdl_ptr - specpdl;
1382 int debugger_called = 0;
1383 Lisp_Object sig_symbol, combined_data;
1384 /* This is set to 1 if we are handling a memory-full error,
1385 because these must not run the debugger.
1386 (There is no room in memory to do that!) */
1387 int no_debugger = 0;
1389 if (NILP (sig))
1391 combined_data = data;
1392 sig_symbol = Fcar (data);
1393 no_debugger = 1;
1395 else
1397 combined_data = Fcons (sig, data);
1398 sig_symbol = sig;
1401 if (wants_debugger (Vstack_trace_on_error, conditions))
1403 #ifdef __STDC__
1404 internal_with_output_to_temp_buffer ("*Backtrace*",
1405 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1406 Qnil);
1407 #else
1408 internal_with_output_to_temp_buffer ("*Backtrace*",
1409 Fbacktrace, Qnil);
1410 #endif
1412 if (! no_debugger
1413 && (EQ (sig_symbol, Qquit)
1414 ? debug_on_quit
1415 : wants_debugger (Vdebug_on_error, conditions))
1416 && ! skip_debugger (conditions, combined_data)
1417 && when_entered_debugger < num_nonmacro_input_events)
1419 specbind (Qdebug_on_error, Qnil);
1420 *debugger_value_ptr
1421 = call_debugger (Fcons (Qerror,
1422 Fcons (combined_data, Qnil)));
1423 debugger_called = 1;
1425 /* If there is no handler, return saying whether we ran the debugger. */
1426 if (EQ (handlers, Qerror))
1428 if (debugger_called)
1429 return unbind_to (count, Qlambda);
1430 return Qt;
1433 for (h = handlers; CONSP (h); h = Fcdr (h))
1435 Lisp_Object handler, condit;
1437 handler = Fcar (h);
1438 if (!CONSP (handler))
1439 continue;
1440 condit = Fcar (handler);
1441 /* Handle a single condition name in handler HANDLER. */
1442 if (SYMBOLP (condit))
1444 tem = Fmemq (Fcar (handler), conditions);
1445 if (!NILP (tem))
1446 return handler;
1448 /* Handle a list of condition names in handler HANDLER. */
1449 else if (CONSP (condit))
1451 while (CONSP (condit))
1453 tem = Fmemq (Fcar (condit), conditions);
1454 if (!NILP (tem))
1455 return handler;
1456 condit = XCDR (condit);
1460 return Qnil;
1463 /* dump an error message; called like printf */
1465 /* VARARGS 1 */
1466 void
1467 error (m, a1, a2, a3)
1468 char *m;
1469 char *a1, *a2, *a3;
1471 char buf[200];
1472 int size = 200;
1473 int mlen;
1474 char *buffer = buf;
1475 char *args[3];
1476 int allocated = 0;
1477 Lisp_Object string;
1479 args[0] = a1;
1480 args[1] = a2;
1481 args[2] = a3;
1483 mlen = strlen (m);
1485 while (1)
1487 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1488 if (used < size)
1489 break;
1490 size *= 2;
1491 if (allocated)
1492 buffer = (char *) xrealloc (buffer, size);
1493 else
1495 buffer = (char *) xmalloc (size);
1496 allocated = 1;
1500 string = build_string (buffer);
1501 if (allocated)
1502 free (buffer);
1504 Fsignal (Qerror, Fcons (string, Qnil));
1507 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1508 "T if FUNCTION makes provisions for interactive calling.\n\
1509 This means it contains a description for how to read arguments to give it.\n\
1510 The value is nil for an invalid function or a symbol with no function\n\
1511 definition.\n\
1513 Interactively callable functions include strings and vectors (treated\n\
1514 as keyboard macros), lambda-expressions that contain a top-level call\n\
1515 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1516 fourth argument, and some of the built-in functions of Lisp.\n\
1518 Also, a symbol satisfies `commandp' if its function definition does so.")
1519 (function)
1520 Lisp_Object function;
1522 register Lisp_Object fun;
1523 register Lisp_Object funcar;
1525 fun = function;
1527 fun = indirect_function (fun);
1528 if (EQ (fun, Qunbound))
1529 return Qnil;
1531 /* Emacs primitives are interactive if their DEFUN specifies an
1532 interactive spec. */
1533 if (SUBRP (fun))
1535 if (XSUBR (fun)->prompt)
1536 return Qt;
1537 else
1538 return Qnil;
1541 /* Bytecode objects are interactive if they are long enough to
1542 have an element whose index is COMPILED_INTERACTIVE, which is
1543 where the interactive spec is stored. */
1544 else if (COMPILEDP (fun))
1545 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1546 ? Qt : Qnil);
1548 /* Strings and vectors are keyboard macros. */
1549 if (STRINGP (fun) || VECTORP (fun))
1550 return Qt;
1552 /* Lists may represent commands. */
1553 if (!CONSP (fun))
1554 return Qnil;
1555 funcar = Fcar (fun);
1556 if (!SYMBOLP (funcar))
1557 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1558 if (EQ (funcar, Qlambda))
1559 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1560 if (EQ (funcar, Qmocklisp))
1561 return Qt; /* All mocklisp functions can be called interactively */
1562 if (EQ (funcar, Qautoload))
1563 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1564 else
1565 return Qnil;
1568 /* ARGSUSED */
1569 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1570 "Define FUNCTION to autoload from FILE.\n\
1571 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1572 Third arg DOCSTRING is documentation for the function.\n\
1573 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1574 Fifth arg TYPE indicates the type of the object:\n\
1575 nil or omitted says FUNCTION is a function,\n\
1576 `keymap' says FUNCTION is really a keymap, and\n\
1577 `macro' or t says FUNCTION is really a macro.\n\
1578 Third through fifth args give info about the real definition.\n\
1579 They default to nil.\n\
1580 If FUNCTION is already defined other than as an autoload,\n\
1581 this does nothing and returns nil.")
1582 (function, file, docstring, interactive, type)
1583 Lisp_Object function, file, docstring, interactive, type;
1585 #ifdef NO_ARG_ARRAY
1586 Lisp_Object args[4];
1587 #endif
1589 CHECK_SYMBOL (function, 0);
1590 CHECK_STRING (file, 1);
1592 /* If function is defined and not as an autoload, don't override */
1593 if (!EQ (XSYMBOL (function)->function, Qunbound)
1594 && !(CONSP (XSYMBOL (function)->function)
1595 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1596 return Qnil;
1598 #ifdef NO_ARG_ARRAY
1599 args[0] = file;
1600 args[1] = docstring;
1601 args[2] = interactive;
1602 args[3] = type;
1604 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1605 #else /* NO_ARG_ARRAY */
1606 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1607 #endif /* not NO_ARG_ARRAY */
1610 Lisp_Object
1611 un_autoload (oldqueue)
1612 Lisp_Object oldqueue;
1614 register Lisp_Object queue, first, second;
1616 /* Queue to unwind is current value of Vautoload_queue.
1617 oldqueue is the shadowed value to leave in Vautoload_queue. */
1618 queue = Vautoload_queue;
1619 Vautoload_queue = oldqueue;
1620 while (CONSP (queue))
1622 first = Fcar (queue);
1623 second = Fcdr (first);
1624 first = Fcar (first);
1625 if (EQ (second, Qnil))
1626 Vfeatures = first;
1627 else
1628 Ffset (first, second);
1629 queue = Fcdr (queue);
1631 return Qnil;
1634 /* Load an autoloaded function.
1635 FUNNAME is the symbol which is the function's name.
1636 FUNDEF is the autoload definition (a list). */
1638 void
1639 do_autoload (fundef, funname)
1640 Lisp_Object fundef, funname;
1642 int count = specpdl_ptr - specpdl;
1643 Lisp_Object fun, queue, first, second;
1644 struct gcpro gcpro1, gcpro2, gcpro3;
1646 fun = funname;
1647 CHECK_SYMBOL (funname, 0);
1648 GCPRO3 (fun, funname, fundef);
1650 /* Preserve the match data. */
1651 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1653 /* Value saved here is to be restored into Vautoload_queue. */
1654 record_unwind_protect (un_autoload, Vautoload_queue);
1655 Vautoload_queue = Qt;
1656 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1658 /* Save the old autoloads, in case we ever do an unload. */
1659 queue = Vautoload_queue;
1660 while (CONSP (queue))
1662 first = Fcar (queue);
1663 second = Fcdr (first);
1664 first = Fcar (first);
1666 /* Note: This test is subtle. The cdr of an autoload-queue entry
1667 may be an atom if the autoload entry was generated by a defalias
1668 or fset. */
1669 if (CONSP (second))
1670 Fput (first, Qautoload, (Fcdr (second)));
1672 queue = Fcdr (queue);
1675 /* Once loading finishes, don't undo it. */
1676 Vautoload_queue = Qt;
1677 unbind_to (count, Qnil);
1679 fun = Findirect_function (fun);
1681 if (!NILP (Fequal (fun, fundef)))
1682 error ("Autoloading failed to define function %s",
1683 XSYMBOL (funname)->name->data);
1684 UNGCPRO;
1687 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1688 "Evaluate FORM and return its value.")
1689 (form)
1690 Lisp_Object form;
1692 Lisp_Object fun, val, original_fun, original_args;
1693 Lisp_Object funcar;
1694 struct backtrace backtrace;
1695 struct gcpro gcpro1, gcpro2, gcpro3;
1697 /* Since Fsignal resets this to 0, it had better be 0 now
1698 or else we have a potential bug. */
1699 if (interrupt_input_blocked != 0)
1700 abort ();
1702 if (SYMBOLP (form))
1704 if (EQ (Vmocklisp_arguments, Qt))
1705 return Fsymbol_value (form);
1706 val = Fsymbol_value (form);
1707 if (NILP (val))
1708 XSETFASTINT (val, 0);
1709 else if (EQ (val, Qt))
1710 XSETFASTINT (val, 1);
1711 return val;
1713 if (!CONSP (form))
1714 return form;
1716 QUIT;
1717 if (consing_since_gc > gc_cons_threshold)
1719 GCPRO1 (form);
1720 Fgarbage_collect ();
1721 UNGCPRO;
1724 if (++lisp_eval_depth > max_lisp_eval_depth)
1726 if (max_lisp_eval_depth < 100)
1727 max_lisp_eval_depth = 100;
1728 if (lisp_eval_depth > max_lisp_eval_depth)
1729 error ("Lisp nesting exceeds max-lisp-eval-depth");
1732 original_fun = Fcar (form);
1733 original_args = Fcdr (form);
1735 backtrace.next = backtrace_list;
1736 backtrace_list = &backtrace;
1737 backtrace.function = &original_fun; /* This also protects them from gc */
1738 backtrace.args = &original_args;
1739 backtrace.nargs = UNEVALLED;
1740 backtrace.evalargs = 1;
1741 backtrace.debug_on_exit = 0;
1743 if (debug_on_next_call)
1744 do_debug_on_call (Qt);
1746 /* At this point, only original_fun and original_args
1747 have values that will be used below */
1748 retry:
1749 fun = Findirect_function (original_fun);
1751 if (SUBRP (fun))
1753 Lisp_Object numargs;
1754 Lisp_Object argvals[8];
1755 Lisp_Object args_left;
1756 register int i, maxargs;
1758 args_left = original_args;
1759 numargs = Flength (args_left);
1761 if (XINT (numargs) < XSUBR (fun)->min_args ||
1762 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1763 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1765 if (XSUBR (fun)->max_args == UNEVALLED)
1767 backtrace.evalargs = 0;
1768 val = (*XSUBR (fun)->function) (args_left);
1769 goto done;
1772 if (XSUBR (fun)->max_args == MANY)
1774 /* Pass a vector of evaluated arguments */
1775 Lisp_Object *vals;
1776 register int argnum = 0;
1778 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1780 GCPRO3 (args_left, fun, fun);
1781 gcpro3.var = vals;
1782 gcpro3.nvars = 0;
1784 while (!NILP (args_left))
1786 vals[argnum++] = Feval (Fcar (args_left));
1787 args_left = Fcdr (args_left);
1788 gcpro3.nvars = argnum;
1791 backtrace.args = vals;
1792 backtrace.nargs = XINT (numargs);
1794 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1795 UNGCPRO;
1796 goto done;
1799 GCPRO3 (args_left, fun, fun);
1800 gcpro3.var = argvals;
1801 gcpro3.nvars = 0;
1803 maxargs = XSUBR (fun)->max_args;
1804 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1806 argvals[i] = Feval (Fcar (args_left));
1807 gcpro3.nvars = ++i;
1810 UNGCPRO;
1812 backtrace.args = argvals;
1813 backtrace.nargs = XINT (numargs);
1815 switch (i)
1817 case 0:
1818 val = (*XSUBR (fun)->function) ();
1819 goto done;
1820 case 1:
1821 val = (*XSUBR (fun)->function) (argvals[0]);
1822 goto done;
1823 case 2:
1824 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1825 goto done;
1826 case 3:
1827 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1828 argvals[2]);
1829 goto done;
1830 case 4:
1831 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1832 argvals[2], argvals[3]);
1833 goto done;
1834 case 5:
1835 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1836 argvals[3], argvals[4]);
1837 goto done;
1838 case 6:
1839 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1840 argvals[3], argvals[4], argvals[5]);
1841 goto done;
1842 case 7:
1843 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1844 argvals[3], argvals[4], argvals[5],
1845 argvals[6]);
1846 goto done;
1848 case 8:
1849 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1850 argvals[3], argvals[4], argvals[5],
1851 argvals[6], argvals[7]);
1852 goto done;
1854 default:
1855 /* Someone has created a subr that takes more arguments than
1856 is supported by this code. We need to either rewrite the
1857 subr to use a different argument protocol, or add more
1858 cases to this switch. */
1859 abort ();
1862 if (COMPILEDP (fun))
1863 val = apply_lambda (fun, original_args, 1);
1864 else
1866 if (!CONSP (fun))
1867 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1868 funcar = Fcar (fun);
1869 if (!SYMBOLP (funcar))
1870 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1871 if (EQ (funcar, Qautoload))
1873 do_autoload (fun, original_fun);
1874 goto retry;
1876 if (EQ (funcar, Qmacro))
1877 val = Feval (apply1 (Fcdr (fun), original_args));
1878 else if (EQ (funcar, Qlambda))
1879 val = apply_lambda (fun, original_args, 1);
1880 else if (EQ (funcar, Qmocklisp))
1881 val = ml_apply (fun, original_args);
1882 else
1883 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1885 done:
1886 if (!EQ (Vmocklisp_arguments, Qt))
1888 if (NILP (val))
1889 XSETFASTINT (val, 0);
1890 else if (EQ (val, Qt))
1891 XSETFASTINT (val, 1);
1893 lisp_eval_depth--;
1894 if (backtrace.debug_on_exit)
1895 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1896 backtrace_list = backtrace.next;
1897 return val;
1900 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1901 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1902 Then return the value FUNCTION returns.\n\
1903 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1904 (nargs, args)
1905 int nargs;
1906 Lisp_Object *args;
1908 register int i, numargs;
1909 register Lisp_Object spread_arg;
1910 register Lisp_Object *funcall_args;
1911 Lisp_Object fun;
1912 struct gcpro gcpro1;
1914 fun = args [0];
1915 funcall_args = 0;
1916 spread_arg = args [nargs - 1];
1917 CHECK_LIST (spread_arg, nargs);
1919 numargs = XINT (Flength (spread_arg));
1921 if (numargs == 0)
1922 return Ffuncall (nargs - 1, args);
1923 else if (numargs == 1)
1925 args [nargs - 1] = XCAR (spread_arg);
1926 return Ffuncall (nargs, args);
1929 numargs += nargs - 2;
1931 fun = indirect_function (fun);
1932 if (EQ (fun, Qunbound))
1934 /* Let funcall get the error */
1935 fun = args[0];
1936 goto funcall;
1939 if (SUBRP (fun))
1941 if (numargs < XSUBR (fun)->min_args
1942 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1943 goto funcall; /* Let funcall get the error */
1944 else if (XSUBR (fun)->max_args > numargs)
1946 /* Avoid making funcall cons up a yet another new vector of arguments
1947 by explicitly supplying nil's for optional values */
1948 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1949 * sizeof (Lisp_Object));
1950 for (i = numargs; i < XSUBR (fun)->max_args;)
1951 funcall_args[++i] = Qnil;
1952 GCPRO1 (*funcall_args);
1953 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
1956 funcall:
1957 /* We add 1 to numargs because funcall_args includes the
1958 function itself as well as its arguments. */
1959 if (!funcall_args)
1961 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1962 * sizeof (Lisp_Object));
1963 GCPRO1 (*funcall_args);
1964 gcpro1.nvars = 1 + numargs;
1967 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1968 /* Spread the last arg we got. Its first element goes in
1969 the slot that it used to occupy, hence this value of I. */
1970 i = nargs - 1;
1971 while (!NILP (spread_arg))
1973 funcall_args [i++] = XCAR (spread_arg);
1974 spread_arg = XCDR (spread_arg);
1977 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1980 /* Run hook variables in various ways. */
1982 enum run_hooks_condition {to_completion, until_success, until_failure};
1984 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
1985 "Run each hook in HOOKS. Major mode functions use this.\n\
1986 Each argument should be a symbol, a hook variable.\n\
1987 These symbols are processed in the order specified.\n\
1988 If a hook symbol has a non-nil value, that value may be a function\n\
1989 or a list of functions to be called to run the hook.\n\
1990 If the value is a function, it is called with no arguments.\n\
1991 If it is a list, the elements are called, in order, with no arguments.\n\
1993 To make a hook variable buffer-local, use `make-local-hook',\n\
1994 not `make-local-variable'.")
1995 (nargs, args)
1996 int nargs;
1997 Lisp_Object *args;
1999 Lisp_Object hook[1];
2000 register int i;
2002 for (i = 0; i < nargs; i++)
2004 hook[0] = args[i];
2005 run_hook_with_args (1, hook, to_completion);
2008 return Qnil;
2011 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2012 Srun_hook_with_args, 1, MANY, 0,
2013 "Run HOOK with the specified arguments ARGS.\n\
2014 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2015 value, that value may be a function or a list of functions to be\n\
2016 called to run the hook. If the value is a function, it is called with\n\
2017 the given arguments and its return value is returned. If it is a list\n\
2018 of functions, those functions are called, in order,\n\
2019 with the given arguments ARGS.\n\
2020 It is best not to depend on the value return by `run-hook-with-args',\n\
2021 as that may change.\n\
2023 To make a hook variable buffer-local, use `make-local-hook',\n\
2024 not `make-local-variable'.")
2025 (nargs, args)
2026 int nargs;
2027 Lisp_Object *args;
2029 return run_hook_with_args (nargs, args, to_completion);
2032 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2033 Srun_hook_with_args_until_success, 1, MANY, 0,
2034 "Run HOOK with the specified arguments ARGS.\n\
2035 HOOK should be a symbol, a hook variable. Its value should\n\
2036 be a list of functions. We call those functions, one by one,\n\
2037 passing arguments ARGS to each of them, until one of them\n\
2038 returns a non-nil value. Then we return that value.\n\
2039 If all the functions return nil, we return nil.\n\
2041 To make a hook variable buffer-local, use `make-local-hook',\n\
2042 not `make-local-variable'.")
2043 (nargs, args)
2044 int nargs;
2045 Lisp_Object *args;
2047 return run_hook_with_args (nargs, args, until_success);
2050 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2051 Srun_hook_with_args_until_failure, 1, MANY, 0,
2052 "Run HOOK with the specified arguments ARGS.\n\
2053 HOOK should be a symbol, a hook variable. Its value should\n\
2054 be a list of functions. We call those functions, one by one,\n\
2055 passing arguments ARGS to each of them, until one of them\n\
2056 returns nil. Then we return nil.\n\
2057 If all the functions return non-nil, we return non-nil.\n\
2059 To make a hook variable buffer-local, use `make-local-hook',\n\
2060 not `make-local-variable'.")
2061 (nargs, args)
2062 int nargs;
2063 Lisp_Object *args;
2065 return run_hook_with_args (nargs, args, until_failure);
2068 /* ARGS[0] should be a hook symbol.
2069 Call each of the functions in the hook value, passing each of them
2070 as arguments all the rest of ARGS (all NARGS - 1 elements).
2071 COND specifies a condition to test after each call
2072 to decide whether to stop.
2073 The caller (or its caller, etc) must gcpro all of ARGS,
2074 except that it isn't necessary to gcpro ARGS[0]. */
2076 Lisp_Object
2077 run_hook_with_args (nargs, args, cond)
2078 int nargs;
2079 Lisp_Object *args;
2080 enum run_hooks_condition cond;
2082 Lisp_Object sym, val, ret;
2083 Lisp_Object globals;
2084 struct gcpro gcpro1, gcpro2, gcpro3;
2086 /* If we are dying or still initializing,
2087 don't do anything--it would probably crash if we tried. */
2088 if (NILP (Vrun_hooks))
2089 return;
2091 sym = args[0];
2092 val = find_symbol_value (sym);
2093 ret = (cond == until_failure ? Qt : Qnil);
2095 if (EQ (val, Qunbound) || NILP (val))
2096 return ret;
2097 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2099 args[0] = val;
2100 return Ffuncall (nargs, args);
2102 else
2104 globals = Qnil;
2105 GCPRO3 (sym, val, globals);
2107 for (;
2108 CONSP (val) && ((cond == to_completion)
2109 || (cond == until_success ? NILP (ret)
2110 : !NILP (ret)));
2111 val = XCDR (val))
2113 if (EQ (XCAR (val), Qt))
2115 /* t indicates this hook has a local binding;
2116 it means to run the global binding too. */
2118 for (globals = Fdefault_value (sym);
2119 CONSP (globals) && ((cond == to_completion)
2120 || (cond == until_success ? NILP (ret)
2121 : !NILP (ret)));
2122 globals = XCDR (globals))
2124 args[0] = XCAR (globals);
2125 /* In a global value, t should not occur. If it does, we
2126 must ignore it to avoid an endless loop. */
2127 if (!EQ (args[0], Qt))
2128 ret = Ffuncall (nargs, args);
2131 else
2133 args[0] = XCAR (val);
2134 ret = Ffuncall (nargs, args);
2138 UNGCPRO;
2139 return ret;
2143 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2144 present value of that symbol.
2145 Call each element of FUNLIST,
2146 passing each of them the rest of ARGS.
2147 The caller (or its caller, etc) must gcpro all of ARGS,
2148 except that it isn't necessary to gcpro ARGS[0]. */
2150 Lisp_Object
2151 run_hook_list_with_args (funlist, nargs, args)
2152 Lisp_Object funlist;
2153 int nargs;
2154 Lisp_Object *args;
2156 Lisp_Object sym;
2157 Lisp_Object val;
2158 Lisp_Object globals;
2159 struct gcpro gcpro1, gcpro2, gcpro3;
2161 sym = args[0];
2162 globals = Qnil;
2163 GCPRO3 (sym, val, globals);
2165 for (val = funlist; CONSP (val); val = XCDR (val))
2167 if (EQ (XCAR (val), Qt))
2169 /* t indicates this hook has a local binding;
2170 it means to run the global binding too. */
2172 for (globals = Fdefault_value (sym);
2173 CONSP (globals);
2174 globals = XCDR (globals))
2176 args[0] = XCAR (globals);
2177 /* In a global value, t should not occur. If it does, we
2178 must ignore it to avoid an endless loop. */
2179 if (!EQ (args[0], Qt))
2180 Ffuncall (nargs, args);
2183 else
2185 args[0] = XCAR (val);
2186 Ffuncall (nargs, args);
2189 UNGCPRO;
2190 return Qnil;
2193 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2195 void
2196 run_hook_with_args_2 (hook, arg1, arg2)
2197 Lisp_Object hook, arg1, arg2;
2199 Lisp_Object temp[3];
2200 temp[0] = hook;
2201 temp[1] = arg1;
2202 temp[2] = arg2;
2204 Frun_hook_with_args (3, temp);
2207 /* Apply fn to arg */
2208 Lisp_Object
2209 apply1 (fn, arg)
2210 Lisp_Object fn, arg;
2212 struct gcpro gcpro1;
2214 GCPRO1 (fn);
2215 if (NILP (arg))
2216 RETURN_UNGCPRO (Ffuncall (1, &fn));
2217 gcpro1.nvars = 2;
2218 #ifdef NO_ARG_ARRAY
2220 Lisp_Object args[2];
2221 args[0] = fn;
2222 args[1] = arg;
2223 gcpro1.var = args;
2224 RETURN_UNGCPRO (Fapply (2, args));
2226 #else /* not NO_ARG_ARRAY */
2227 RETURN_UNGCPRO (Fapply (2, &fn));
2228 #endif /* not NO_ARG_ARRAY */
2231 /* Call function fn on no arguments */
2232 Lisp_Object
2233 call0 (fn)
2234 Lisp_Object fn;
2236 struct gcpro gcpro1;
2238 GCPRO1 (fn);
2239 RETURN_UNGCPRO (Ffuncall (1, &fn));
2242 /* Call function fn with 1 argument arg1 */
2243 /* ARGSUSED */
2244 Lisp_Object
2245 call1 (fn, arg1)
2246 Lisp_Object fn, arg1;
2248 struct gcpro gcpro1;
2249 #ifdef NO_ARG_ARRAY
2250 Lisp_Object args[2];
2252 args[0] = fn;
2253 args[1] = arg1;
2254 GCPRO1 (args[0]);
2255 gcpro1.nvars = 2;
2256 RETURN_UNGCPRO (Ffuncall (2, args));
2257 #else /* not NO_ARG_ARRAY */
2258 GCPRO1 (fn);
2259 gcpro1.nvars = 2;
2260 RETURN_UNGCPRO (Ffuncall (2, &fn));
2261 #endif /* not NO_ARG_ARRAY */
2264 /* Call function fn with 2 arguments arg1, arg2 */
2265 /* ARGSUSED */
2266 Lisp_Object
2267 call2 (fn, arg1, arg2)
2268 Lisp_Object fn, arg1, arg2;
2270 struct gcpro gcpro1;
2271 #ifdef NO_ARG_ARRAY
2272 Lisp_Object args[3];
2273 args[0] = fn;
2274 args[1] = arg1;
2275 args[2] = arg2;
2276 GCPRO1 (args[0]);
2277 gcpro1.nvars = 3;
2278 RETURN_UNGCPRO (Ffuncall (3, args));
2279 #else /* not NO_ARG_ARRAY */
2280 GCPRO1 (fn);
2281 gcpro1.nvars = 3;
2282 RETURN_UNGCPRO (Ffuncall (3, &fn));
2283 #endif /* not NO_ARG_ARRAY */
2286 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2287 /* ARGSUSED */
2288 Lisp_Object
2289 call3 (fn, arg1, arg2, arg3)
2290 Lisp_Object fn, arg1, arg2, arg3;
2292 struct gcpro gcpro1;
2293 #ifdef NO_ARG_ARRAY
2294 Lisp_Object args[4];
2295 args[0] = fn;
2296 args[1] = arg1;
2297 args[2] = arg2;
2298 args[3] = arg3;
2299 GCPRO1 (args[0]);
2300 gcpro1.nvars = 4;
2301 RETURN_UNGCPRO (Ffuncall (4, args));
2302 #else /* not NO_ARG_ARRAY */
2303 GCPRO1 (fn);
2304 gcpro1.nvars = 4;
2305 RETURN_UNGCPRO (Ffuncall (4, &fn));
2306 #endif /* not NO_ARG_ARRAY */
2309 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2310 /* ARGSUSED */
2311 Lisp_Object
2312 call4 (fn, arg1, arg2, arg3, arg4)
2313 Lisp_Object fn, arg1, arg2, arg3, arg4;
2315 struct gcpro gcpro1;
2316 #ifdef NO_ARG_ARRAY
2317 Lisp_Object args[5];
2318 args[0] = fn;
2319 args[1] = arg1;
2320 args[2] = arg2;
2321 args[3] = arg3;
2322 args[4] = arg4;
2323 GCPRO1 (args[0]);
2324 gcpro1.nvars = 5;
2325 RETURN_UNGCPRO (Ffuncall (5, args));
2326 #else /* not NO_ARG_ARRAY */
2327 GCPRO1 (fn);
2328 gcpro1.nvars = 5;
2329 RETURN_UNGCPRO (Ffuncall (5, &fn));
2330 #endif /* not NO_ARG_ARRAY */
2333 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2334 /* ARGSUSED */
2335 Lisp_Object
2336 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2337 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2339 struct gcpro gcpro1;
2340 #ifdef NO_ARG_ARRAY
2341 Lisp_Object args[6];
2342 args[0] = fn;
2343 args[1] = arg1;
2344 args[2] = arg2;
2345 args[3] = arg3;
2346 args[4] = arg4;
2347 args[5] = arg5;
2348 GCPRO1 (args[0]);
2349 gcpro1.nvars = 6;
2350 RETURN_UNGCPRO (Ffuncall (6, args));
2351 #else /* not NO_ARG_ARRAY */
2352 GCPRO1 (fn);
2353 gcpro1.nvars = 6;
2354 RETURN_UNGCPRO (Ffuncall (6, &fn));
2355 #endif /* not NO_ARG_ARRAY */
2358 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2359 /* ARGSUSED */
2360 Lisp_Object
2361 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2362 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2364 struct gcpro gcpro1;
2365 #ifdef NO_ARG_ARRAY
2366 Lisp_Object args[7];
2367 args[0] = fn;
2368 args[1] = arg1;
2369 args[2] = arg2;
2370 args[3] = arg3;
2371 args[4] = arg4;
2372 args[5] = arg5;
2373 args[6] = arg6;
2374 GCPRO1 (args[0]);
2375 gcpro1.nvars = 7;
2376 RETURN_UNGCPRO (Ffuncall (7, args));
2377 #else /* not NO_ARG_ARRAY */
2378 GCPRO1 (fn);
2379 gcpro1.nvars = 7;
2380 RETURN_UNGCPRO (Ffuncall (7, &fn));
2381 #endif /* not NO_ARG_ARRAY */
2384 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2385 "Call first argument as a function, passing remaining arguments to it.\n\
2386 Return the value that function returns.\n\
2387 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2388 (nargs, args)
2389 int nargs;
2390 Lisp_Object *args;
2392 Lisp_Object fun;
2393 Lisp_Object funcar;
2394 int numargs = nargs - 1;
2395 Lisp_Object lisp_numargs;
2396 Lisp_Object val;
2397 struct backtrace backtrace;
2398 register Lisp_Object *internal_args;
2399 register int i;
2401 QUIT;
2402 if (consing_since_gc > gc_cons_threshold)
2403 Fgarbage_collect ();
2405 if (++lisp_eval_depth > max_lisp_eval_depth)
2407 if (max_lisp_eval_depth < 100)
2408 max_lisp_eval_depth = 100;
2409 if (lisp_eval_depth > max_lisp_eval_depth)
2410 error ("Lisp nesting exceeds max-lisp-eval-depth");
2413 backtrace.next = backtrace_list;
2414 backtrace_list = &backtrace;
2415 backtrace.function = &args[0];
2416 backtrace.args = &args[1];
2417 backtrace.nargs = nargs - 1;
2418 backtrace.evalargs = 0;
2419 backtrace.debug_on_exit = 0;
2421 if (debug_on_next_call)
2422 do_debug_on_call (Qlambda);
2424 retry:
2426 fun = args[0];
2428 fun = Findirect_function (fun);
2430 if (SUBRP (fun))
2432 if (numargs < XSUBR (fun)->min_args
2433 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2435 XSETFASTINT (lisp_numargs, numargs);
2436 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2439 if (XSUBR (fun)->max_args == UNEVALLED)
2440 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2442 if (XSUBR (fun)->max_args == MANY)
2444 val = (*XSUBR (fun)->function) (numargs, args + 1);
2445 goto done;
2448 if (XSUBR (fun)->max_args > numargs)
2450 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2451 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2452 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2453 internal_args[i] = Qnil;
2455 else
2456 internal_args = args + 1;
2457 switch (XSUBR (fun)->max_args)
2459 case 0:
2460 val = (*XSUBR (fun)->function) ();
2461 goto done;
2462 case 1:
2463 val = (*XSUBR (fun)->function) (internal_args[0]);
2464 goto done;
2465 case 2:
2466 val = (*XSUBR (fun)->function) (internal_args[0],
2467 internal_args[1]);
2468 goto done;
2469 case 3:
2470 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2471 internal_args[2]);
2472 goto done;
2473 case 4:
2474 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2475 internal_args[2],
2476 internal_args[3]);
2477 goto done;
2478 case 5:
2479 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2480 internal_args[2], internal_args[3],
2481 internal_args[4]);
2482 goto done;
2483 case 6:
2484 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2485 internal_args[2], internal_args[3],
2486 internal_args[4], internal_args[5]);
2487 goto done;
2488 case 7:
2489 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2490 internal_args[2], internal_args[3],
2491 internal_args[4], internal_args[5],
2492 internal_args[6]);
2493 goto done;
2495 case 8:
2496 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2497 internal_args[2], internal_args[3],
2498 internal_args[4], internal_args[5],
2499 internal_args[6], internal_args[7]);
2500 goto done;
2502 default:
2504 /* If a subr takes more than 8 arguments without using MANY
2505 or UNEVALLED, we need to extend this function to support it.
2506 Until this is done, there is no way to call the function. */
2507 abort ();
2510 if (COMPILEDP (fun))
2511 val = funcall_lambda (fun, numargs, args + 1);
2512 else
2514 if (!CONSP (fun))
2515 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2516 funcar = Fcar (fun);
2517 if (!SYMBOLP (funcar))
2518 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2519 if (EQ (funcar, Qlambda))
2520 val = funcall_lambda (fun, numargs, args + 1);
2521 else if (EQ (funcar, Qmocklisp))
2522 val = ml_apply (fun, Flist (numargs, args + 1));
2523 else if (EQ (funcar, Qautoload))
2525 do_autoload (fun, args[0]);
2526 goto retry;
2528 else
2529 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2531 done:
2532 lisp_eval_depth--;
2533 if (backtrace.debug_on_exit)
2534 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2535 backtrace_list = backtrace.next;
2536 return val;
2539 Lisp_Object
2540 apply_lambda (fun, args, eval_flag)
2541 Lisp_Object fun, args;
2542 int eval_flag;
2544 Lisp_Object args_left;
2545 Lisp_Object numargs;
2546 register Lisp_Object *arg_vector;
2547 struct gcpro gcpro1, gcpro2, gcpro3;
2548 register int i;
2549 register Lisp_Object tem;
2551 numargs = Flength (args);
2552 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2553 args_left = args;
2555 GCPRO3 (*arg_vector, args_left, fun);
2556 gcpro1.nvars = 0;
2558 for (i = 0; i < XINT (numargs);)
2560 tem = Fcar (args_left), args_left = Fcdr (args_left);
2561 if (eval_flag) tem = Feval (tem);
2562 arg_vector[i++] = tem;
2563 gcpro1.nvars = i;
2566 UNGCPRO;
2568 if (eval_flag)
2570 backtrace_list->args = arg_vector;
2571 backtrace_list->nargs = i;
2573 backtrace_list->evalargs = 0;
2574 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2576 /* Do the debug-on-exit now, while arg_vector still exists. */
2577 if (backtrace_list->debug_on_exit)
2578 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2579 /* Don't do it again when we return to eval. */
2580 backtrace_list->debug_on_exit = 0;
2581 return tem;
2584 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2585 and return the result of evaluation.
2586 FUN must be either a lambda-expression or a compiled-code object. */
2588 Lisp_Object
2589 funcall_lambda (fun, nargs, arg_vector)
2590 Lisp_Object fun;
2591 int nargs;
2592 register Lisp_Object *arg_vector;
2594 Lisp_Object val, tem;
2595 register Lisp_Object syms_left;
2596 Lisp_Object numargs;
2597 register Lisp_Object next;
2598 int count = specpdl_ptr - specpdl;
2599 register int i;
2600 int optional = 0, rest = 0;
2602 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2604 XSETFASTINT (numargs, nargs);
2606 if (CONSP (fun))
2607 syms_left = Fcar (Fcdr (fun));
2608 else if (COMPILEDP (fun))
2609 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2610 else abort ();
2612 i = 0;
2613 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
2615 QUIT;
2616 next = Fcar (syms_left);
2617 while (!SYMBOLP (next))
2618 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2619 if (EQ (next, Qand_rest))
2620 rest = 1;
2621 else if (EQ (next, Qand_optional))
2622 optional = 1;
2623 else if (rest)
2625 specbind (next, Flist (nargs - i, &arg_vector[i]));
2626 i = nargs;
2628 else if (i < nargs)
2630 tem = arg_vector[i++];
2631 specbind (next, tem);
2633 else if (!optional)
2634 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2635 else
2636 specbind (next, Qnil);
2639 if (i < nargs)
2640 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2642 if (CONSP (fun))
2643 val = Fprogn (Fcdr (Fcdr (fun)));
2644 else
2646 /* If we have not actually read the bytecode string
2647 and constants vector yet, fetch them from the file. */
2648 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2649 Ffetch_bytecode (fun);
2650 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2651 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2652 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2654 return unbind_to (count, val);
2657 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2658 1, 1, 0,
2659 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2660 (object)
2661 Lisp_Object object;
2663 Lisp_Object tem;
2665 if (COMPILEDP (object)
2666 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2668 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2669 if (!CONSP (tem))
2670 error ("invalid byte code");
2671 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2672 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2674 return object;
2677 void
2678 grow_specpdl ()
2680 register int count = specpdl_ptr - specpdl;
2681 if (specpdl_size >= max_specpdl_size)
2683 if (max_specpdl_size < 400)
2684 max_specpdl_size = 400;
2685 if (specpdl_size >= max_specpdl_size)
2687 if (!NILP (Vdebug_on_error))
2688 /* Leave room for some specpdl in the debugger. */
2689 max_specpdl_size = specpdl_size + 100;
2690 Fsignal (Qerror,
2691 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2694 specpdl_size *= 2;
2695 if (specpdl_size > max_specpdl_size)
2696 specpdl_size = max_specpdl_size;
2697 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2698 specpdl_ptr = specpdl + count;
2701 void
2702 specbind (symbol, value)
2703 Lisp_Object symbol, value;
2705 Lisp_Object ovalue;
2707 CHECK_SYMBOL (symbol, 0);
2709 if (specpdl_ptr == specpdl + specpdl_size)
2710 grow_specpdl ();
2711 specpdl_ptr->symbol = symbol;
2712 specpdl_ptr->func = 0;
2713 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
2714 specpdl_ptr++;
2715 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2716 store_symval_forwarding (symbol, ovalue, value);
2717 else
2718 set_internal (symbol, value, 1);
2721 void
2722 record_unwind_protect (function, arg)
2723 Lisp_Object (*function) P_ ((Lisp_Object));
2724 Lisp_Object arg;
2726 if (specpdl_ptr == specpdl + specpdl_size)
2727 grow_specpdl ();
2728 specpdl_ptr->func = function;
2729 specpdl_ptr->symbol = Qnil;
2730 specpdl_ptr->old_value = arg;
2731 specpdl_ptr++;
2734 Lisp_Object
2735 unbind_to (count, value)
2736 int count;
2737 Lisp_Object value;
2739 int quitf = !NILP (Vquit_flag);
2740 struct gcpro gcpro1;
2742 GCPRO1 (value);
2744 Vquit_flag = Qnil;
2746 while (specpdl_ptr != specpdl + count)
2748 --specpdl_ptr;
2749 if (specpdl_ptr->func != 0)
2750 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2751 /* Note that a "binding" of nil is really an unwind protect,
2752 so in that case the "old value" is a list of forms to evaluate. */
2753 else if (NILP (specpdl_ptr->symbol))
2754 Fprogn (specpdl_ptr->old_value);
2755 else
2756 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 1);
2758 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2760 UNGCPRO;
2762 return value;
2765 #if 0
2767 /* Get the value of symbol's global binding, even if that binding
2768 is not now dynamically visible. */
2770 Lisp_Object
2771 top_level_value (symbol)
2772 Lisp_Object symbol;
2774 register struct specbinding *ptr = specpdl;
2776 CHECK_SYMBOL (symbol, 0);
2777 for (; ptr != specpdl_ptr; ptr++)
2779 if (EQ (ptr->symbol, symbol))
2780 return ptr->old_value;
2782 return Fsymbol_value (symbol);
2785 Lisp_Object
2786 top_level_set (symbol, newval)
2787 Lisp_Object symbol, newval;
2789 register struct specbinding *ptr = specpdl;
2791 CHECK_SYMBOL (symbol, 0);
2792 for (; ptr != specpdl_ptr; ptr++)
2794 if (EQ (ptr->symbol, symbol))
2796 ptr->old_value = newval;
2797 return newval;
2800 return Fset (symbol, newval);
2803 #endif /* 0 */
2805 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2806 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2807 The debugger is entered when that frame exits, if the flag is non-nil.")
2808 (level, flag)
2809 Lisp_Object level, flag;
2811 register struct backtrace *backlist = backtrace_list;
2812 register int i;
2814 CHECK_NUMBER (level, 0);
2816 for (i = 0; backlist && i < XINT (level); i++)
2818 backlist = backlist->next;
2821 if (backlist)
2822 backlist->debug_on_exit = !NILP (flag);
2824 return flag;
2827 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2828 "Print a trace of Lisp function calls currently active.\n\
2829 Output stream used is value of `standard-output'.")
2832 register struct backtrace *backlist = backtrace_list;
2833 register int i;
2834 Lisp_Object tail;
2835 Lisp_Object tem;
2836 extern Lisp_Object Vprint_level;
2837 struct gcpro gcpro1;
2839 XSETFASTINT (Vprint_level, 3);
2841 tail = Qnil;
2842 GCPRO1 (tail);
2844 while (backlist)
2846 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2847 if (backlist->nargs == UNEVALLED)
2849 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2850 write_string ("\n", -1);
2852 else
2854 tem = *backlist->function;
2855 Fprin1 (tem, Qnil); /* This can QUIT */
2856 write_string ("(", -1);
2857 if (backlist->nargs == MANY)
2859 for (tail = *backlist->args, i = 0;
2860 !NILP (tail);
2861 tail = Fcdr (tail), i++)
2863 if (i) write_string (" ", -1);
2864 Fprin1 (Fcar (tail), Qnil);
2867 else
2869 for (i = 0; i < backlist->nargs; i++)
2871 if (i) write_string (" ", -1);
2872 Fprin1 (backlist->args[i], Qnil);
2875 write_string (")\n", -1);
2877 backlist = backlist->next;
2880 Vprint_level = Qnil;
2881 UNGCPRO;
2882 return Qnil;
2885 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2886 "Return the function and arguments NFRAMES up from current execution point.\n\
2887 If that frame has not evaluated the arguments yet (or is a special form),\n\
2888 the value is (nil FUNCTION ARG-FORMS...).\n\
2889 If that frame has evaluated its arguments and called its function already,\n\
2890 the value is (t FUNCTION ARG-VALUES...).\n\
2891 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2892 FUNCTION is whatever was supplied as car of evaluated list,\n\
2893 or a lambda expression for macro calls.\n\
2894 If NFRAMES is more than the number of frames, the value is nil.")
2895 (nframes)
2896 Lisp_Object nframes;
2898 register struct backtrace *backlist = backtrace_list;
2899 register int i;
2900 Lisp_Object tem;
2902 CHECK_NATNUM (nframes, 0);
2904 /* Find the frame requested. */
2905 for (i = 0; backlist && i < XFASTINT (nframes); i++)
2906 backlist = backlist->next;
2908 if (!backlist)
2909 return Qnil;
2910 if (backlist->nargs == UNEVALLED)
2911 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2912 else
2914 if (backlist->nargs == MANY)
2915 tem = *backlist->args;
2916 else
2917 tem = Flist (backlist->nargs, backlist->args);
2919 return Fcons (Qt, Fcons (*backlist->function, tem));
2923 void
2924 syms_of_eval ()
2926 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2927 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
2928 If Lisp code tries to make more than this many at once,\n\
2929 an error is signaled.");
2931 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2932 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
2933 This limit is to catch infinite recursions for you before they cause\n\
2934 actual stack overflow in C, which would be fatal for Emacs.\n\
2935 You can safely make it considerably larger than its default value,\n\
2936 if that proves inconveniently small.");
2938 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2939 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2940 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2941 Vquit_flag = Qnil;
2943 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2944 "Non-nil inhibits C-g quitting from happening immediately.\n\
2945 Note that `quit-flag' will still be set by typing C-g,\n\
2946 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
2947 To prevent this happening, set `quit-flag' to nil\n\
2948 before making `inhibit-quit' nil.");
2949 Vinhibit_quit = Qnil;
2951 Qinhibit_quit = intern ("inhibit-quit");
2952 staticpro (&Qinhibit_quit);
2954 Qautoload = intern ("autoload");
2955 staticpro (&Qautoload);
2957 Qdebug_on_error = intern ("debug-on-error");
2958 staticpro (&Qdebug_on_error);
2960 Qmacro = intern ("macro");
2961 staticpro (&Qmacro);
2963 /* Note that the process handling also uses Qexit, but we don't want
2964 to staticpro it twice, so we just do it here. */
2965 Qexit = intern ("exit");
2966 staticpro (&Qexit);
2968 Qinteractive = intern ("interactive");
2969 staticpro (&Qinteractive);
2971 Qcommandp = intern ("commandp");
2972 staticpro (&Qcommandp);
2974 Qdefun = intern ("defun");
2975 staticpro (&Qdefun);
2977 Qand_rest = intern ("&rest");
2978 staticpro (&Qand_rest);
2980 Qand_optional = intern ("&optional");
2981 staticpro (&Qand_optional);
2983 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
2984 "*Non-nil means automatically display a backtrace buffer\n\
2985 after any error that is handled by the editor command loop.\n\
2986 If the value is a list, an error only means to display a backtrace\n\
2987 if one of its condition symbols appears in the list.");
2988 Vstack_trace_on_error = Qnil;
2990 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
2991 "*Non-nil means enter debugger if an error is signaled.\n\
2992 Does not apply to errors handled by `condition-case'.\n\
2993 If the value is a list, an error only means to enter the debugger\n\
2994 if one of its condition symbols appears in the list.\n\
2995 See also variable `debug-on-quit'.");
2996 Vdebug_on_error = Qnil;
2998 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
2999 "*List of errors for which the debugger should not be called.\n\
3000 Each element may be a condition-name or a regexp that matches error messages.\n\
3001 If any element applies to a given error, that error skips the debugger\n\
3002 and just returns to top level.\n\
3003 This overrides the variable `debug-on-error'.\n\
3004 It does not apply to errors handled by `condition-case'.");
3005 Vdebug_ignored_errors = Qnil;
3007 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3008 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3009 Does not apply if quit is handled by a `condition-case'.");
3010 debug_on_quit = 0;
3012 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3013 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3015 DEFVAR_LISP ("debugger", &Vdebugger,
3016 "Function to call to invoke debugger.\n\
3017 If due to frame exit, args are `exit' and the value being returned;\n\
3018 this function's value will be returned instead of that.\n\
3019 If due to error, args are `error' and a list of the args to `signal'.\n\
3020 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3021 If due to `eval' entry, one arg, t.");
3022 Vdebugger = Qnil;
3024 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3025 "If non-nil, this is a function for `signal' to call.\n\
3026 It receives the same arguments that `signal' was given.\n\
3027 The Edebug package uses this to regain control.");
3028 Vsignal_hook_function = Qnil;
3030 Qmocklisp_arguments = intern ("mocklisp-arguments");
3031 staticpro (&Qmocklisp_arguments);
3032 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3033 "While in a mocklisp function, the list of its unevaluated args.");
3034 Vmocklisp_arguments = Qt;
3036 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3037 "*Non-nil means call the debugger regardless of condition handlers.\n\
3038 Note that `debug-on-error', `debug-on-quit' and friends\n\
3039 still determine whether to handle the particular condition.");
3040 Vdebug_on_signal = Qnil;
3042 Vrun_hooks = intern ("run-hooks");
3043 staticpro (&Vrun_hooks);
3045 staticpro (&Vautoload_queue);
3046 Vautoload_queue = Qnil;
3048 defsubr (&Sor);
3049 defsubr (&Sand);
3050 defsubr (&Sif);
3051 defsubr (&Scond);
3052 defsubr (&Sprogn);
3053 defsubr (&Sprog1);
3054 defsubr (&Sprog2);
3055 defsubr (&Ssetq);
3056 defsubr (&Squote);
3057 defsubr (&Sfunction);
3058 defsubr (&Sdefun);
3059 defsubr (&Sdefmacro);
3060 defsubr (&Sdefvar);
3061 defsubr (&Sdefconst);
3062 defsubr (&Suser_variable_p);
3063 defsubr (&Slet);
3064 defsubr (&SletX);
3065 defsubr (&Swhile);
3066 defsubr (&Smacroexpand);
3067 defsubr (&Scatch);
3068 defsubr (&Sthrow);
3069 defsubr (&Sunwind_protect);
3070 defsubr (&Scondition_case);
3071 defsubr (&Ssignal);
3072 defsubr (&Sinteractive_p);
3073 defsubr (&Scommandp);
3074 defsubr (&Sautoload);
3075 defsubr (&Seval);
3076 defsubr (&Sapply);
3077 defsubr (&Sfuncall);
3078 defsubr (&Srun_hooks);
3079 defsubr (&Srun_hook_with_args);
3080 defsubr (&Srun_hook_with_args_until_success);
3081 defsubr (&Srun_hook_with_args_until_failure);
3082 defsubr (&Sfetch_bytecode);
3083 defsubr (&Sbacktrace_debug);
3084 defsubr (&Sbacktrace);
3085 defsubr (&Sbacktrace_frame);