Include errno.h, string.h, unistd.h. Don't declare errno, strerror.
[emacs.git] / src / eval.c
blobd616addde4369d43e57e888492a420acccbeef80
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include <setjmp.h>
31 /* This definition is duplicated in alloc.c and keyboard.c */
32 /* Putting it in lisp.h makes cc bomb out! */
34 struct backtrace
36 struct backtrace *next;
37 Lisp_Object *function;
38 Lisp_Object *args; /* Points to vector of args. */
39 int nargs; /* Length of vector.
40 If nargs is UNEVALLED, args points to slot holding
41 list of unevalled args */
42 char evalargs;
43 /* Nonzero means call value of debugger when done with this operation. */
44 char debug_on_exit;
47 struct backtrace *backtrace_list;
49 /* This structure helps implement the `catch' and `throw' control
50 structure. A struct catchtag contains all the information needed
51 to restore the state of the interpreter after a non-local jump.
53 Handlers for error conditions (represented by `struct handler'
54 structures) just point to a catch tag to do the cleanup required
55 for their jumps.
57 catchtag structures are chained together in the C calling stack;
58 the `next' member points to the next outer catchtag.
60 A call like (throw TAG VAL) searches for a catchtag whose `tag'
61 member is TAG, and then unbinds to it. The `val' member is used to
62 hold VAL while the stack is unwound; `val' is returned as the value
63 of the catch form.
65 All the other members are concerned with restoring the interpreter
66 state. */
67 struct catchtag
69 Lisp_Object tag;
70 Lisp_Object val;
71 struct catchtag *next;
72 struct gcpro *gcpro;
73 jmp_buf jmp;
74 struct backtrace *backlist;
75 struct handler *handlerlist;
76 int lisp_eval_depth;
77 int pdlcount;
78 int poll_suppress_count;
79 struct byte_stack *byte_stack;
82 struct catchtag *catchlist;
84 #ifdef DEBUG_GCPRO
85 /* Count levels of GCPRO to detect failure to UNGCPRO. */
86 int gcpro_level;
87 #endif
89 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
90 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
91 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
92 Lisp_Object Qand_rest, Qand_optional;
93 Lisp_Object Qdebug_on_error;
95 /* This holds either the symbol `run-hooks' or nil.
96 It is nil at an early stage of startup, and when Emacs
97 is shutting down. */
98 Lisp_Object Vrun_hooks;
100 /* Non-nil means record all fset's and provide's, to be undone
101 if the file being autoloaded is not fully loaded.
102 They are recorded by being consed onto the front of Vautoload_queue:
103 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
105 Lisp_Object Vautoload_queue;
107 /* Current number of specbindings allocated in specpdl. */
108 int specpdl_size;
110 /* Pointer to beginning of specpdl. */
111 struct specbinding *specpdl;
113 /* Pointer to first unused element in specpdl. */
114 struct specbinding *specpdl_ptr;
116 /* Maximum size allowed for specpdl allocation */
117 int max_specpdl_size;
119 /* Depth in Lisp evaluations and function calls. */
120 int lisp_eval_depth;
122 /* Maximum allowed depth in Lisp evaluations and function calls. */
123 int max_lisp_eval_depth;
125 /* Nonzero means enter debugger before next function call */
126 int debug_on_next_call;
128 /* Non-zero means debuffer may continue. This is zero when the
129 debugger is called during redisplay, where it might not be safe to
130 continue the interrupted redisplay. */
132 int debugger_may_continue;
134 /* List of conditions (non-nil atom means all) which cause a backtrace
135 if an error is handled by the command loop's error handler. */
136 Lisp_Object Vstack_trace_on_error;
138 /* List of conditions (non-nil atom means all) which enter the debugger
139 if an error is handled by the command loop's error handler. */
140 Lisp_Object Vdebug_on_error;
142 /* List of conditions and regexps specifying error messages which
143 do not enter the debugger even if Vdebug_on_errors says they should. */
144 Lisp_Object Vdebug_ignored_errors;
146 /* Non-nil means call the debugger even if the error will be handled. */
147 Lisp_Object Vdebug_on_signal;
149 /* Hook for edebug to use. */
150 Lisp_Object Vsignal_hook_function;
152 /* Nonzero means enter debugger if a quit signal
153 is handled by the command loop's error handler. */
154 int debug_on_quit;
156 /* The value of num_nonmacro_input_events as of the last time we
157 started to enter the debugger. If we decide to enter the debugger
158 again when this is still equal to num_nonmacro_input_events, then we
159 know that the debugger itself has an error, and we should just
160 signal the error instead of entering an infinite loop of debugger
161 invocations. */
162 int when_entered_debugger;
164 Lisp_Object Vdebugger;
166 void specbind (), record_unwind_protect ();
168 Lisp_Object run_hook_with_args ();
170 Lisp_Object funcall_lambda ();
171 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
173 void
174 init_eval_once ()
176 specpdl_size = 50;
177 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
178 specpdl_ptr = specpdl;
179 max_specpdl_size = 600;
180 max_lisp_eval_depth = 300;
182 Vrun_hooks = Qnil;
185 void
186 init_eval ()
188 specpdl_ptr = specpdl;
189 catchlist = 0;
190 handlerlist = 0;
191 backtrace_list = 0;
192 Vquit_flag = Qnil;
193 debug_on_next_call = 0;
194 lisp_eval_depth = 0;
195 #ifdef DEBUG_GCPRO
196 gcpro_level = 0;
197 #endif
198 /* This is less than the initial value of num_nonmacro_input_events. */
199 when_entered_debugger = -1;
202 Lisp_Object
203 call_debugger (arg)
204 Lisp_Object arg;
206 int debug_while_redisplaying;
207 int count = specpdl_ptr - specpdl;
208 Lisp_Object val;
210 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
211 max_lisp_eval_depth = lisp_eval_depth + 20;
213 if (specpdl_size + 40 > max_specpdl_size)
214 max_specpdl_size = specpdl_size + 40;
216 #ifdef HAVE_X_WINDOWS
217 if (display_busy_cursor_p)
218 cancel_busy_cursor ();
219 #endif
221 debug_on_next_call = 0;
222 when_entered_debugger = num_nonmacro_input_events;
224 /* Resetting redisplaying_p to 0 makes sure that debug output is
225 displayed if the debugger is invoked during redisplay. */
226 debug_while_redisplaying = redisplaying_p;
227 redisplaying_p = 0;
228 specbind (intern ("debugger-may-continue"),
229 debug_while_redisplaying ? Qnil : Qt);
231 val = apply1 (Vdebugger, arg);
233 /* Interrupting redisplay and resuming it later is not safe under
234 all circumstances. So, when the debugger returns, abort the
235 interupted redisplay by going back to the top-level. */
236 if (debug_while_redisplaying)
237 Ftop_level ();
239 return unbind_to (count, val);
242 void
243 do_debug_on_call (code)
244 Lisp_Object code;
246 debug_on_next_call = 0;
247 backtrace_list->debug_on_exit = 1;
248 call_debugger (Fcons (code, Qnil));
251 /* NOTE!!! Every function that can call EVAL must protect its args
252 and temporaries from garbage collection while it needs them.
253 The definition of `For' shows what you have to do. */
255 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
256 "Eval args until one of them yields non-nil, then return that value.\n\
257 The remaining args are not evalled at all.\n\
258 If all args return nil, return nil.")
259 (args)
260 Lisp_Object args;
262 register Lisp_Object val;
263 Lisp_Object args_left;
264 struct gcpro gcpro1;
266 if (NILP(args))
267 return Qnil;
269 args_left = args;
270 GCPRO1 (args_left);
274 val = Feval (Fcar (args_left));
275 if (!NILP (val))
276 break;
277 args_left = Fcdr (args_left);
279 while (!NILP(args_left));
281 UNGCPRO;
282 return val;
285 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
286 "Eval args until one of them yields nil, then return nil.\n\
287 The remaining args are not evalled at all.\n\
288 If no arg yields nil, return the last arg's value.")
289 (args)
290 Lisp_Object args;
292 register Lisp_Object val;
293 Lisp_Object args_left;
294 struct gcpro gcpro1;
296 if (NILP(args))
297 return Qt;
299 args_left = args;
300 GCPRO1 (args_left);
304 val = Feval (Fcar (args_left));
305 if (NILP (val))
306 break;
307 args_left = Fcdr (args_left);
309 while (!NILP(args_left));
311 UNGCPRO;
312 return val;
315 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
316 "If COND yields non-nil, do THEN, else do ELSE...\n\
317 Returns the value of THEN or the value of the last of the ELSE's.\n\
318 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
319 If COND yields nil, and there are no ELSE's, the value is nil.")
320 (args)
321 Lisp_Object args;
323 register Lisp_Object cond;
324 struct gcpro gcpro1;
326 GCPRO1 (args);
327 cond = Feval (Fcar (args));
328 UNGCPRO;
330 if (!NILP (cond))
331 return Feval (Fcar (Fcdr (args)));
332 return Fprogn (Fcdr (Fcdr (args)));
335 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
336 "Try each clause until one succeeds.\n\
337 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
338 and, if the value is non-nil, this clause succeeds:\n\
339 then the expressions in BODY are evaluated and the last one's\n\
340 value is the value of the cond-form.\n\
341 If no clause succeeds, cond returns nil.\n\
342 If a clause has one element, as in (CONDITION),\n\
343 CONDITION's value if non-nil is returned from the cond-form.")
344 (args)
345 Lisp_Object args;
347 register Lisp_Object clause, val;
348 struct gcpro gcpro1;
350 val = Qnil;
351 GCPRO1 (args);
352 while (!NILP (args))
354 clause = Fcar (args);
355 val = Feval (Fcar (clause));
356 if (!NILP (val))
358 if (!EQ (XCDR (clause), Qnil))
359 val = Fprogn (XCDR (clause));
360 break;
362 args = XCDR (args);
364 UNGCPRO;
366 return val;
369 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
370 "Eval BODY forms sequentially and return value of last one.")
371 (args)
372 Lisp_Object args;
374 register Lisp_Object val, tem;
375 Lisp_Object args_left;
376 struct gcpro gcpro1;
378 /* In Mocklisp code, symbols at the front of the progn arglist
379 are to be bound to zero. */
380 if (!EQ (Vmocklisp_arguments, Qt))
382 val = make_number (0);
383 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
385 QUIT;
386 specbind (tem, val), args = Fcdr (args);
390 if (NILP(args))
391 return Qnil;
393 args_left = args;
394 GCPRO1 (args_left);
398 val = Feval (Fcar (args_left));
399 args_left = Fcdr (args_left);
401 while (!NILP(args_left));
403 UNGCPRO;
404 return val;
407 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
408 "Eval FIRST and BODY sequentially; value from FIRST.\n\
409 The value of FIRST is saved during the evaluation of the remaining args,\n\
410 whose values are discarded.")
411 (args)
412 Lisp_Object args;
414 Lisp_Object val;
415 register Lisp_Object args_left;
416 struct gcpro gcpro1, gcpro2;
417 register int argnum = 0;
419 if (NILP(args))
420 return Qnil;
422 args_left = args;
423 val = Qnil;
424 GCPRO2 (args, val);
428 if (!(argnum++))
429 val = Feval (Fcar (args_left));
430 else
431 Feval (Fcar (args_left));
432 args_left = Fcdr (args_left);
434 while (!NILP(args_left));
436 UNGCPRO;
437 return val;
440 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
441 "Eval X, Y and BODY sequentially; value from Y.\n\
442 The value of Y is saved during the evaluation of the remaining args,\n\
443 whose values are discarded.")
444 (args)
445 Lisp_Object args;
447 Lisp_Object val;
448 register Lisp_Object args_left;
449 struct gcpro gcpro1, gcpro2;
450 register int argnum = -1;
452 val = Qnil;
454 if (NILP (args))
455 return Qnil;
457 args_left = args;
458 val = Qnil;
459 GCPRO2 (args, val);
463 if (!(argnum++))
464 val = Feval (Fcar (args_left));
465 else
466 Feval (Fcar (args_left));
467 args_left = Fcdr (args_left);
469 while (!NILP (args_left));
471 UNGCPRO;
472 return val;
475 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
476 "Set each SYM to the value of its VAL.\n\
477 The symbols SYM are variables; they are literal (not evaluated).\n\
478 The values VAL are expressions; they are evaluated.\n\
479 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
480 The second VAL is not computed until after the first SYM is set, and so on;\n\
481 each VAL can use the new value of variables set earlier in the `setq'.\n\
482 The return value of the `setq' form is the value of the last VAL.")
483 (args)
484 Lisp_Object args;
486 register Lisp_Object args_left;
487 register Lisp_Object val, sym;
488 struct gcpro gcpro1;
490 if (NILP(args))
491 return Qnil;
493 args_left = args;
494 GCPRO1 (args);
498 val = Feval (Fcar (Fcdr (args_left)));
499 sym = Fcar (args_left);
500 Fset (sym, val);
501 args_left = Fcdr (Fcdr (args_left));
503 while (!NILP(args_left));
505 UNGCPRO;
506 return val;
509 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
510 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
511 (args)
512 Lisp_Object args;
514 return Fcar (args);
517 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
518 "Like `quote', but preferred for objects which are functions.\n\
519 In byte compilation, `function' causes its argument to be compiled.\n\
520 `quote' cannot do that.")
521 (args)
522 Lisp_Object args;
524 return Fcar (args);
527 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
528 "Return t if function in which this appears was called interactively.\n\
529 This means that the function was called with call-interactively (which\n\
530 includes being called as the binding of a key)\n\
531 and input is currently coming from the keyboard (not in keyboard macro).")
534 register struct backtrace *btp;
535 register Lisp_Object fun;
537 if (!INTERACTIVE)
538 return Qnil;
540 btp = backtrace_list;
542 /* If this isn't a byte-compiled function, there may be a frame at
543 the top for Finteractive_p itself. If so, skip it. */
544 fun = Findirect_function (*btp->function);
545 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
546 btp = btp->next;
548 /* If we're running an Emacs 18-style byte-compiled function, there
549 may be a frame for Fbytecode. Now, given the strictest
550 definition, this function isn't really being called
551 interactively, but because that's the way Emacs 18 always builds
552 byte-compiled functions, we'll accept it for now. */
553 if (EQ (*btp->function, Qbytecode))
554 btp = btp->next;
556 /* If this isn't a byte-compiled function, then we may now be
557 looking at several frames for special forms. Skip past them. */
558 while (btp &&
559 btp->nargs == UNEVALLED)
560 btp = btp->next;
562 /* btp now points at the frame of the innermost function that isn't
563 a special form, ignoring frames for Finteractive_p and/or
564 Fbytecode at the top. If this frame is for a built-in function
565 (such as load or eval-region) return nil. */
566 fun = Findirect_function (*btp->function);
567 if (SUBRP (fun))
568 return Qnil;
569 /* btp points to the frame of a Lisp function that called interactive-p.
570 Return t if that function was called interactively. */
571 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
572 return Qt;
573 return Qnil;
576 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
577 "Define NAME as a function.\n\
578 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
579 See also the function `interactive'.")
580 (args)
581 Lisp_Object args;
583 register Lisp_Object fn_name;
584 register Lisp_Object defn;
586 fn_name = Fcar (args);
587 defn = Fcons (Qlambda, Fcdr (args));
588 if (!NILP (Vpurify_flag))
589 defn = Fpurecopy (defn);
590 Ffset (fn_name, defn);
591 LOADHIST_ATTACH (fn_name);
592 return fn_name;
595 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
596 "Define NAME as a macro.\n\
597 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
598 When the macro is called, as in (NAME ARGS...),\n\
599 the function (lambda ARGLIST BODY...) is applied to\n\
600 the list ARGS... as it appears in the expression,\n\
601 and the result should be a form to be evaluated instead of the original.")
602 (args)
603 Lisp_Object args;
605 register Lisp_Object fn_name;
606 register Lisp_Object defn;
608 fn_name = Fcar (args);
609 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
610 if (!NILP (Vpurify_flag))
611 defn = Fpurecopy (defn);
612 Ffset (fn_name, defn);
613 LOADHIST_ATTACH (fn_name);
614 return fn_name;
617 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
618 "Define SYMBOL as a variable.\n\
619 You are not required to define a variable in order to use it,\n\
620 but the definition can supply documentation and an initial value\n\
621 in a way that tags can recognize.\n\n\
622 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
623 If SYMBOL is buffer-local, its default value is what is set;\n\
624 buffer-local values are not affected.\n\
625 INITVALUE and DOCSTRING are optional.\n\
626 If DOCSTRING starts with *, this variable is identified as a user option.\n\
627 This means that M-x set-variable and M-x edit-options recognize it.\n\
628 If INITVALUE is missing, SYMBOL's value is not set.")
629 (args)
630 Lisp_Object args;
632 register Lisp_Object sym, tem, tail;
634 sym = Fcar (args);
635 tail = Fcdr (args);
636 if (!NILP (Fcdr (Fcdr (tail))))
637 error ("too many arguments");
639 if (!NILP (tail))
641 tem = Fdefault_boundp (sym);
642 if (NILP (tem))
643 Fset_default (sym, Feval (Fcar (Fcdr (args))));
645 tail = Fcdr (Fcdr (args));
646 if (!NILP (Fcar (tail)))
648 tem = Fcar (tail);
649 if (!NILP (Vpurify_flag))
650 tem = Fpurecopy (tem);
651 Fput (sym, Qvariable_documentation, tem);
653 LOADHIST_ATTACH (sym);
654 return sym;
657 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
658 "Define SYMBOL as a constant variable.\n\
659 The intent is that neither programs nor users should ever change this value.\n\
660 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
661 If SYMBOL is buffer-local, its default value is what is set;\n\
662 buffer-local values are not affected.\n\
663 DOCSTRING is optional.")
664 (args)
665 Lisp_Object args;
667 register Lisp_Object sym, tem;
669 sym = Fcar (args);
670 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
671 error ("too many arguments");
673 tem = Feval (Fcar (Fcdr (args)));
674 if (!NILP (Vpurify_flag))
675 tem = Fpurecopy (tem);
676 Fset_default (sym, tem);
677 tem = Fcar (Fcdr (Fcdr (args)));
678 if (!NILP (tem))
680 if (!NILP (Vpurify_flag))
681 tem = Fpurecopy (tem);
682 Fput (sym, Qvariable_documentation, tem);
684 LOADHIST_ATTACH (sym);
685 return sym;
688 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
689 "Returns t if VARIABLE is intended to be set and modified by users.\n\
690 \(The alternative is a variable used internally in a Lisp program.)\n\
691 Determined by whether the first character of the documentation\n\
692 for the variable is `*' or if the variable is customizable (has a non-nil\n\
693 value of any of `custom-type', `custom-loads' or `standard-value'\n\
694 on its property list).")
695 (variable)
696 Lisp_Object variable;
698 Lisp_Object documentation;
700 if (!SYMBOLP (variable))
701 return Qnil;
703 documentation = Fget (variable, Qvariable_documentation);
704 if (INTEGERP (documentation) && XINT (documentation) < 0)
705 return Qt;
706 if (STRINGP (documentation)
707 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
708 return Qt;
709 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
710 if (CONSP (documentation)
711 && STRINGP (XCAR (documentation))
712 && INTEGERP (XCDR (documentation))
713 && XINT (XCDR (documentation)) < 0)
714 return Qt;
715 /* Customizable? */
716 if ((!NILP (Fget (variable, intern ("custom-type"))))
717 || (!NILP (Fget (variable, intern ("custom-loads"))))
718 || (!NILP (Fget (variable, intern ("standard-value")))))
719 return Qt;
720 return Qnil;
723 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
724 "Bind variables according to VARLIST then eval BODY.\n\
725 The value of the last form in BODY is returned.\n\
726 Each element of VARLIST is a symbol (which is bound to nil)\n\
727 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
728 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
729 (args)
730 Lisp_Object args;
732 Lisp_Object varlist, val, elt;
733 int count = specpdl_ptr - specpdl;
734 struct gcpro gcpro1, gcpro2, gcpro3;
736 GCPRO3 (args, elt, varlist);
738 varlist = Fcar (args);
739 while (!NILP (varlist))
741 QUIT;
742 elt = Fcar (varlist);
743 if (SYMBOLP (elt))
744 specbind (elt, Qnil);
745 else if (! NILP (Fcdr (Fcdr (elt))))
746 Fsignal (Qerror,
747 Fcons (build_string ("`let' bindings can have only one value-form"),
748 elt));
749 else
751 val = Feval (Fcar (Fcdr (elt)));
752 specbind (Fcar (elt), val);
754 varlist = Fcdr (varlist);
756 UNGCPRO;
757 val = Fprogn (Fcdr (args));
758 return unbind_to (count, val);
761 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
762 "Bind variables according to VARLIST then eval BODY.\n\
763 The value of the last form in BODY is returned.\n\
764 Each element of VARLIST is a symbol (which is bound to nil)\n\
765 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
766 All the VALUEFORMs are evalled before any symbols are bound.")
767 (args)
768 Lisp_Object args;
770 Lisp_Object *temps, tem;
771 register Lisp_Object elt, varlist;
772 int count = specpdl_ptr - specpdl;
773 register int argnum;
774 struct gcpro gcpro1, gcpro2;
776 varlist = Fcar (args);
778 /* Make space to hold the values to give the bound variables */
779 elt = Flength (varlist);
780 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
782 /* Compute the values and store them in `temps' */
784 GCPRO2 (args, *temps);
785 gcpro2.nvars = 0;
787 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
789 QUIT;
790 elt = Fcar (varlist);
791 if (SYMBOLP (elt))
792 temps [argnum++] = Qnil;
793 else if (! NILP (Fcdr (Fcdr (elt))))
794 Fsignal (Qerror,
795 Fcons (build_string ("`let' bindings can have only one value-form"),
796 elt));
797 else
798 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
799 gcpro2.nvars = argnum;
801 UNGCPRO;
803 varlist = Fcar (args);
804 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
806 elt = Fcar (varlist);
807 tem = temps[argnum++];
808 if (SYMBOLP (elt))
809 specbind (elt, tem);
810 else
811 specbind (Fcar (elt), tem);
814 elt = Fprogn (Fcdr (args));
815 return unbind_to (count, elt);
818 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
819 "If TEST yields non-nil, eval BODY... and repeat.\n\
820 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
821 until TEST returns nil.")
822 (args)
823 Lisp_Object args;
825 Lisp_Object test, body, tem;
826 struct gcpro gcpro1, gcpro2;
828 GCPRO2 (test, body);
830 test = Fcar (args);
831 body = Fcdr (args);
832 while (tem = Feval (test),
833 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
835 QUIT;
836 Fprogn (body);
839 UNGCPRO;
840 return Qnil;
843 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
844 "Return result of expanding macros at top level of FORM.\n\
845 If FORM is not a macro call, it is returned unchanged.\n\
846 Otherwise, the macro is expanded and the expansion is considered\n\
847 in place of FORM. When a non-macro-call results, it is returned.\n\n\
848 The second optional arg ENVIRONMENT species an environment of macro\n\
849 definitions to shadow the loaded ones for use in file byte-compilation.")
850 (form, environment)
851 Lisp_Object form;
852 Lisp_Object environment;
854 /* With cleanups from Hallvard Furuseth. */
855 register Lisp_Object expander, sym, def, tem;
857 while (1)
859 /* Come back here each time we expand a macro call,
860 in case it expands into another macro call. */
861 if (!CONSP (form))
862 break;
863 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
864 def = sym = XCAR (form);
865 tem = Qnil;
866 /* Trace symbols aliases to other symbols
867 until we get a symbol that is not an alias. */
868 while (SYMBOLP (def))
870 QUIT;
871 sym = def;
872 tem = Fassq (sym, environment);
873 if (NILP (tem))
875 def = XSYMBOL (sym)->function;
876 if (!EQ (def, Qunbound))
877 continue;
879 break;
881 /* Right now TEM is the result from SYM in ENVIRONMENT,
882 and if TEM is nil then DEF is SYM's function definition. */
883 if (NILP (tem))
885 /* SYM is not mentioned in ENVIRONMENT.
886 Look at its function definition. */
887 if (EQ (def, Qunbound) || !CONSP (def))
888 /* Not defined or definition not suitable */
889 break;
890 if (EQ (XCAR (def), Qautoload))
892 /* Autoloading function: will it be a macro when loaded? */
893 tem = Fnth (make_number (4), def);
894 if (EQ (tem, Qt) || EQ (tem, Qmacro))
895 /* Yes, load it and try again. */
897 struct gcpro gcpro1;
898 GCPRO1 (form);
899 do_autoload (def, sym);
900 UNGCPRO;
901 continue;
903 else
904 break;
906 else if (!EQ (XCAR (def), Qmacro))
907 break;
908 else expander = XCDR (def);
910 else
912 expander = XCDR (tem);
913 if (NILP (expander))
914 break;
916 form = apply1 (expander, XCDR (form));
918 return form;
921 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
922 "Eval BODY allowing nonlocal exits using `throw'.\n\
923 TAG is evalled to get the tag to use; it must not be nil.\n\
925 Then the BODY is executed.\n\
926 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
927 If no throw happens, `catch' returns the value of the last BODY form.\n\
928 If a throw happens, it specifies the value to return from `catch'.")
929 (args)
930 Lisp_Object args;
932 register Lisp_Object tag;
933 struct gcpro gcpro1;
935 GCPRO1 (args);
936 tag = Feval (Fcar (args));
937 UNGCPRO;
938 return internal_catch (tag, Fprogn, Fcdr (args));
941 /* Set up a catch, then call C function FUNC on argument ARG.
942 FUNC should return a Lisp_Object.
943 This is how catches are done from within C code. */
945 Lisp_Object
946 internal_catch (tag, func, arg)
947 Lisp_Object tag;
948 Lisp_Object (*func) ();
949 Lisp_Object arg;
951 /* This structure is made part of the chain `catchlist'. */
952 struct catchtag c;
954 /* Fill in the components of c, and put it on the list. */
955 c.next = catchlist;
956 c.tag = tag;
957 c.val = Qnil;
958 c.backlist = backtrace_list;
959 c.handlerlist = handlerlist;
960 c.lisp_eval_depth = lisp_eval_depth;
961 c.pdlcount = specpdl_ptr - specpdl;
962 c.poll_suppress_count = poll_suppress_count;
963 c.gcpro = gcprolist;
964 c.byte_stack = byte_stack_list;
965 catchlist = &c;
967 /* Call FUNC. */
968 if (! _setjmp (c.jmp))
969 c.val = (*func) (arg);
971 /* Throw works by a longjmp that comes right here. */
972 catchlist = c.next;
973 return c.val;
976 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
977 jump to that CATCH, returning VALUE as the value of that catch.
979 This is the guts Fthrow and Fsignal; they differ only in the way
980 they choose the catch tag to throw to. A catch tag for a
981 condition-case form has a TAG of Qnil.
983 Before each catch is discarded, unbind all special bindings and
984 execute all unwind-protect clauses made above that catch. Unwind
985 the handler stack as we go, so that the proper handlers are in
986 effect for each unwind-protect clause we run. At the end, restore
987 some static info saved in CATCH, and longjmp to the location
988 specified in the
990 This is used for correct unwinding in Fthrow and Fsignal. */
992 static void
993 unwind_to_catch (catch, value)
994 struct catchtag *catch;
995 Lisp_Object value;
997 register int last_time;
999 /* Save the value in the tag. */
1000 catch->val = value;
1002 /* Restore the polling-suppression count. */
1003 set_poll_suppress_count (catch->poll_suppress_count);
1007 last_time = catchlist == catch;
1009 /* Unwind the specpdl stack, and then restore the proper set of
1010 handlers. */
1011 unbind_to (catchlist->pdlcount, Qnil);
1012 handlerlist = catchlist->handlerlist;
1013 catchlist = catchlist->next;
1015 while (! last_time);
1017 byte_stack_list = catch->byte_stack;
1018 gcprolist = catch->gcpro;
1019 #ifdef DEBUG_GCPRO
1020 if (gcprolist != 0)
1021 gcpro_level = gcprolist->level + 1;
1022 else
1023 gcpro_level = 0;
1024 #endif
1025 backtrace_list = catch->backlist;
1026 lisp_eval_depth = catch->lisp_eval_depth;
1028 _longjmp (catch->jmp, 1);
1031 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1032 "Throw to the catch for TAG and return VALUE from it.\n\
1033 Both TAG and VALUE are evalled.")
1034 (tag, value)
1035 register Lisp_Object tag, value;
1037 register struct catchtag *c;
1039 while (1)
1041 if (!NILP (tag))
1042 for (c = catchlist; c; c = c->next)
1044 if (EQ (c->tag, tag))
1045 unwind_to_catch (c, value);
1047 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1052 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1053 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1054 If BODYFORM completes normally, its value is returned\n\
1055 after executing the UNWINDFORMS.\n\
1056 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1057 (args)
1058 Lisp_Object args;
1060 Lisp_Object val;
1061 int count = specpdl_ptr - specpdl;
1063 record_unwind_protect (0, Fcdr (args));
1064 val = Feval (Fcar (args));
1065 return unbind_to (count, val);
1068 /* Chain of condition handlers currently in effect.
1069 The elements of this chain are contained in the stack frames
1070 of Fcondition_case and internal_condition_case.
1071 When an error is signaled (by calling Fsignal, below),
1072 this chain is searched for an element that applies. */
1074 struct handler *handlerlist;
1076 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1077 "Regain control when an error is signaled.\n\
1078 executes BODYFORM and returns its value if no error happens.\n\
1079 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1080 where the BODY is made of Lisp expressions.\n\n\
1081 A handler is applicable to an error\n\
1082 if CONDITION-NAME is one of the error's condition names.\n\
1083 If an error happens, the first applicable handler is run.\n\
1085 The car of a handler may be a list of condition names\n\
1086 instead of a single condition name.\n\
1088 When a handler handles an error,\n\
1089 control returns to the condition-case and the handler BODY... is executed\n\
1090 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1091 VAR may be nil; then you do not get access to the signal information.\n\
1093 The value of the last BODY form is returned from the condition-case.\n\
1094 See also the function `signal' for more info.")
1095 (args)
1096 Lisp_Object args;
1098 Lisp_Object val;
1099 struct catchtag c;
1100 struct handler h;
1101 register Lisp_Object var, bodyform, handlers;
1103 var = Fcar (args);
1104 bodyform = Fcar (Fcdr (args));
1105 handlers = Fcdr (Fcdr (args));
1106 CHECK_SYMBOL (var, 0);
1108 for (val = handlers; ! NILP (val); val = Fcdr (val))
1110 Lisp_Object tem;
1111 tem = Fcar (val);
1112 if (! (NILP (tem)
1113 || (CONSP (tem)
1114 && (SYMBOLP (XCAR (tem))
1115 || CONSP (XCAR (tem))))))
1116 error ("Invalid condition handler", tem);
1119 c.tag = Qnil;
1120 c.val = Qnil;
1121 c.backlist = backtrace_list;
1122 c.handlerlist = handlerlist;
1123 c.lisp_eval_depth = lisp_eval_depth;
1124 c.pdlcount = specpdl_ptr - specpdl;
1125 c.poll_suppress_count = poll_suppress_count;
1126 c.gcpro = gcprolist;
1127 c.byte_stack = byte_stack_list;
1128 if (_setjmp (c.jmp))
1130 if (!NILP (h.var))
1131 specbind (h.var, c.val);
1132 val = Fprogn (Fcdr (h.chosen_clause));
1134 /* Note that this just undoes the binding of h.var; whoever
1135 longjumped to us unwound the stack to c.pdlcount before
1136 throwing. */
1137 unbind_to (c.pdlcount, Qnil);
1138 return val;
1140 c.next = catchlist;
1141 catchlist = &c;
1143 h.var = var;
1144 h.handler = handlers;
1145 h.next = handlerlist;
1146 h.tag = &c;
1147 handlerlist = &h;
1149 val = Feval (bodyform);
1150 catchlist = c.next;
1151 handlerlist = h.next;
1152 return val;
1155 /* Call the function BFUN with no arguments, catching errors within it
1156 according to HANDLERS. If there is an error, call HFUN with
1157 one argument which is the data that describes the error:
1158 (SIGNALNAME . DATA)
1160 HANDLERS can be a list of conditions to catch.
1161 If HANDLERS is Qt, catch all errors.
1162 If HANDLERS is Qerror, catch all errors
1163 but allow the debugger to run if that is enabled. */
1165 Lisp_Object
1166 internal_condition_case (bfun, handlers, hfun)
1167 Lisp_Object (*bfun) ();
1168 Lisp_Object handlers;
1169 Lisp_Object (*hfun) ();
1171 Lisp_Object val;
1172 struct catchtag c;
1173 struct handler h;
1175 /* Since Fsignal resets this to 0, it had better be 0 now
1176 or else we have a potential bug. */
1177 if (interrupt_input_blocked != 0)
1178 abort ();
1180 c.tag = Qnil;
1181 c.val = Qnil;
1182 c.backlist = backtrace_list;
1183 c.handlerlist = handlerlist;
1184 c.lisp_eval_depth = lisp_eval_depth;
1185 c.pdlcount = specpdl_ptr - specpdl;
1186 c.poll_suppress_count = poll_suppress_count;
1187 c.gcpro = gcprolist;
1188 c.byte_stack = byte_stack_list;
1189 if (_setjmp (c.jmp))
1191 return (*hfun) (c.val);
1193 c.next = catchlist;
1194 catchlist = &c;
1195 h.handler = handlers;
1196 h.var = Qnil;
1197 h.next = handlerlist;
1198 h.tag = &c;
1199 handlerlist = &h;
1201 val = (*bfun) ();
1202 catchlist = c.next;
1203 handlerlist = h.next;
1204 return val;
1207 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1209 Lisp_Object
1210 internal_condition_case_1 (bfun, arg, handlers, hfun)
1211 Lisp_Object (*bfun) ();
1212 Lisp_Object arg;
1213 Lisp_Object handlers;
1214 Lisp_Object (*hfun) ();
1216 Lisp_Object val;
1217 struct catchtag c;
1218 struct handler h;
1220 c.tag = Qnil;
1221 c.val = Qnil;
1222 c.backlist = backtrace_list;
1223 c.handlerlist = handlerlist;
1224 c.lisp_eval_depth = lisp_eval_depth;
1225 c.pdlcount = specpdl_ptr - specpdl;
1226 c.poll_suppress_count = poll_suppress_count;
1227 c.gcpro = gcprolist;
1228 c.byte_stack = byte_stack_list;
1229 if (_setjmp (c.jmp))
1231 return (*hfun) (c.val);
1233 c.next = catchlist;
1234 catchlist = &c;
1235 h.handler = handlers;
1236 h.var = Qnil;
1237 h.next = handlerlist;
1238 h.tag = &c;
1239 handlerlist = &h;
1241 val = (*bfun) (arg);
1242 catchlist = c.next;
1243 handlerlist = h.next;
1244 return val;
1247 static Lisp_Object find_handler_clause ();
1249 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1250 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1251 This function does not return.\n\n\
1252 An error symbol is a symbol with an `error-conditions' property\n\
1253 that is a list of condition names.\n\
1254 A handler for any of those names will get to handle this signal.\n\
1255 The symbol `error' should normally be one of them.\n\
1257 DATA should be a list. Its elements are printed as part of the error message.\n\
1258 If the signal is handled, DATA is made available to the handler.\n\
1259 See also the function `condition-case'.")
1260 (error_symbol, data)
1261 Lisp_Object error_symbol, data;
1263 /* When memory is full, ERROR-SYMBOL is nil,
1264 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1265 register struct handler *allhandlers = handlerlist;
1266 Lisp_Object conditions;
1267 extern int gc_in_progress;
1268 extern int waiting_for_input;
1269 Lisp_Object debugger_value;
1270 Lisp_Object string;
1271 Lisp_Object real_error_symbol;
1272 extern int display_busy_cursor_p;
1274 immediate_quit = 0;
1275 if (gc_in_progress || waiting_for_input)
1276 abort ();
1278 TOTALLY_UNBLOCK_INPUT;
1280 if (NILP (error_symbol))
1281 real_error_symbol = Fcar (data);
1282 else
1283 real_error_symbol = error_symbol;
1285 #ifdef HAVE_X_WINDOWS
1286 if (display_busy_cursor_p)
1287 cancel_busy_cursor ();
1288 #endif
1290 /* This hook is used by edebug. */
1291 if (! NILP (Vsignal_hook_function))
1292 call2 (Vsignal_hook_function, error_symbol, data);
1294 conditions = Fget (real_error_symbol, Qerror_conditions);
1296 for (; handlerlist; handlerlist = handlerlist->next)
1298 register Lisp_Object clause;
1300 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1301 max_lisp_eval_depth = lisp_eval_depth + 20;
1303 if (specpdl_size + 40 > max_specpdl_size)
1304 max_specpdl_size = specpdl_size + 40;
1306 clause = find_handler_clause (handlerlist->handler, conditions,
1307 error_symbol, data, &debugger_value);
1309 #if 0 /* Most callers are not prepared to handle gc if this returns.
1310 So, since this feature is not very useful, take it out. */
1311 /* If have called debugger and user wants to continue,
1312 just return nil. */
1313 if (EQ (clause, Qlambda))
1314 return debugger_value;
1315 #else
1316 if (EQ (clause, Qlambda))
1318 /* We can't return values to code which signaled an error, but we
1319 can continue code which has signaled a quit. */
1320 if (EQ (real_error_symbol, Qquit))
1321 return Qnil;
1322 else
1323 error ("Cannot return from the debugger in an error");
1325 #endif
1327 if (!NILP (clause))
1329 Lisp_Object unwind_data;
1330 struct handler *h = handlerlist;
1332 handlerlist = allhandlers;
1334 if (NILP (error_symbol))
1335 unwind_data = data;
1336 else
1337 unwind_data = Fcons (error_symbol, data);
1338 h->chosen_clause = clause;
1339 unwind_to_catch (h->tag, unwind_data);
1343 handlerlist = allhandlers;
1344 /* If no handler is present now, try to run the debugger,
1345 and if that fails, throw to top level. */
1346 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1347 if (catchlist != 0)
1348 Fthrow (Qtop_level, Qt);
1350 if (! NILP (error_symbol))
1351 data = Fcons (error_symbol, data);
1353 string = Ferror_message_string (data);
1354 fatal ("%s", XSTRING (string)->data, 0);
1357 /* Return nonzero iff LIST is a non-nil atom or
1358 a list containing one of CONDITIONS. */
1360 static int
1361 wants_debugger (list, conditions)
1362 Lisp_Object list, conditions;
1364 if (NILP (list))
1365 return 0;
1366 if (! CONSP (list))
1367 return 1;
1369 while (CONSP (conditions))
1371 Lisp_Object this, tail;
1372 this = XCAR (conditions);
1373 for (tail = list; CONSP (tail); tail = XCDR (tail))
1374 if (EQ (XCAR (tail), this))
1375 return 1;
1376 conditions = XCDR (conditions);
1378 return 0;
1381 /* Return 1 if an error with condition-symbols CONDITIONS,
1382 and described by SIGNAL-DATA, should skip the debugger
1383 according to debugger-ignore-errors. */
1385 static int
1386 skip_debugger (conditions, data)
1387 Lisp_Object conditions, data;
1389 Lisp_Object tail;
1390 int first_string = 1;
1391 Lisp_Object error_message;
1393 for (tail = Vdebug_ignored_errors; CONSP (tail);
1394 tail = XCDR (tail))
1396 if (STRINGP (XCAR (tail)))
1398 if (first_string)
1400 error_message = Ferror_message_string (data);
1401 first_string = 0;
1403 if (fast_string_match (XCAR (tail), error_message) >= 0)
1404 return 1;
1406 else
1408 Lisp_Object contail;
1410 for (contail = conditions; CONSP (contail);
1411 contail = XCDR (contail))
1412 if (EQ (XCAR (tail), XCAR (contail)))
1413 return 1;
1417 return 0;
1420 /* Value of Qlambda means we have called debugger and user has continued.
1421 There are two ways to pass SIG and DATA:
1422 = SIG is the error symbol, and DATA is the rest of the data.
1423 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1424 This is for memory-full errors only.
1426 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1428 static Lisp_Object
1429 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1430 Lisp_Object handlers, conditions, sig, data;
1431 Lisp_Object *debugger_value_ptr;
1433 register Lisp_Object h;
1434 register Lisp_Object tem;
1436 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1437 return Qt;
1438 /* error is used similarly, but means print an error message
1439 and run the debugger if that is enabled. */
1440 if (EQ (handlers, Qerror)
1441 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1442 there is a handler. */
1444 int count = specpdl_ptr - specpdl;
1445 int debugger_called = 0;
1446 Lisp_Object sig_symbol, combined_data;
1447 /* This is set to 1 if we are handling a memory-full error,
1448 because these must not run the debugger.
1449 (There is no room in memory to do that!) */
1450 int no_debugger = 0;
1452 if (NILP (sig))
1454 combined_data = data;
1455 sig_symbol = Fcar (data);
1456 no_debugger = 1;
1458 else
1460 combined_data = Fcons (sig, data);
1461 sig_symbol = sig;
1464 if (wants_debugger (Vstack_trace_on_error, conditions))
1466 #ifdef PROTOTYPES
1467 internal_with_output_to_temp_buffer ("*Backtrace*",
1468 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1469 Qnil);
1470 #else
1471 internal_with_output_to_temp_buffer ("*Backtrace*",
1472 Fbacktrace, Qnil);
1473 #endif
1475 if (! no_debugger
1476 && (EQ (sig_symbol, Qquit)
1477 ? debug_on_quit
1478 : wants_debugger (Vdebug_on_error, conditions))
1479 && ! skip_debugger (conditions, combined_data)
1480 && when_entered_debugger < num_nonmacro_input_events)
1482 specbind (Qdebug_on_error, Qnil);
1483 *debugger_value_ptr
1484 = call_debugger (Fcons (Qerror,
1485 Fcons (combined_data, Qnil)));
1486 debugger_called = 1;
1488 /* If there is no handler, return saying whether we ran the debugger. */
1489 if (EQ (handlers, Qerror))
1491 if (debugger_called)
1492 return unbind_to (count, Qlambda);
1493 return Qt;
1496 for (h = handlers; CONSP (h); h = Fcdr (h))
1498 Lisp_Object handler, condit;
1500 handler = Fcar (h);
1501 if (!CONSP (handler))
1502 continue;
1503 condit = Fcar (handler);
1504 /* Handle a single condition name in handler HANDLER. */
1505 if (SYMBOLP (condit))
1507 tem = Fmemq (Fcar (handler), conditions);
1508 if (!NILP (tem))
1509 return handler;
1511 /* Handle a list of condition names in handler HANDLER. */
1512 else if (CONSP (condit))
1514 while (CONSP (condit))
1516 tem = Fmemq (Fcar (condit), conditions);
1517 if (!NILP (tem))
1518 return handler;
1519 condit = XCDR (condit);
1523 return Qnil;
1526 /* dump an error message; called like printf */
1528 /* VARARGS 1 */
1529 void
1530 error (m, a1, a2, a3)
1531 char *m;
1532 char *a1, *a2, *a3;
1534 char buf[200];
1535 int size = 200;
1536 int mlen;
1537 char *buffer = buf;
1538 char *args[3];
1539 int allocated = 0;
1540 Lisp_Object string;
1542 args[0] = a1;
1543 args[1] = a2;
1544 args[2] = a3;
1546 mlen = strlen (m);
1548 while (1)
1550 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1551 if (used < size)
1552 break;
1553 size *= 2;
1554 if (allocated)
1555 buffer = (char *) xrealloc (buffer, size);
1556 else
1558 buffer = (char *) xmalloc (size);
1559 allocated = 1;
1563 string = build_string (buffer);
1564 if (allocated)
1565 free (buffer);
1567 Fsignal (Qerror, Fcons (string, Qnil));
1570 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1571 "T if FUNCTION makes provisions for interactive calling.\n\
1572 This means it contains a description for how to read arguments to give it.\n\
1573 The value is nil for an invalid function or a symbol with no function\n\
1574 definition.\n\
1576 Interactively callable functions include strings and vectors (treated\n\
1577 as keyboard macros), lambda-expressions that contain a top-level call\n\
1578 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1579 fourth argument, and some of the built-in functions of Lisp.\n\
1581 Also, a symbol satisfies `commandp' if its function definition does so.")
1582 (function)
1583 Lisp_Object function;
1585 register Lisp_Object fun;
1586 register Lisp_Object funcar;
1588 fun = function;
1590 fun = indirect_function (fun);
1591 if (EQ (fun, Qunbound))
1592 return Qnil;
1594 /* Emacs primitives are interactive if their DEFUN specifies an
1595 interactive spec. */
1596 if (SUBRP (fun))
1598 if (XSUBR (fun)->prompt)
1599 return Qt;
1600 else
1601 return Qnil;
1604 /* Bytecode objects are interactive if they are long enough to
1605 have an element whose index is COMPILED_INTERACTIVE, which is
1606 where the interactive spec is stored. */
1607 else if (COMPILEDP (fun))
1608 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1609 ? Qt : Qnil);
1611 /* Strings and vectors are keyboard macros. */
1612 if (STRINGP (fun) || VECTORP (fun))
1613 return Qt;
1615 /* Lists may represent commands. */
1616 if (!CONSP (fun))
1617 return Qnil;
1618 funcar = Fcar (fun);
1619 if (!SYMBOLP (funcar))
1620 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1621 if (EQ (funcar, Qlambda))
1622 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1623 if (EQ (funcar, Qmocklisp))
1624 return Qt; /* All mocklisp functions can be called interactively */
1625 if (EQ (funcar, Qautoload))
1626 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1627 else
1628 return Qnil;
1631 /* ARGSUSED */
1632 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1633 "Define FUNCTION to autoload from FILE.\n\
1634 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1635 Third arg DOCSTRING is documentation for the function.\n\
1636 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1637 Fifth arg TYPE indicates the type of the object:\n\
1638 nil or omitted says FUNCTION is a function,\n\
1639 `keymap' says FUNCTION is really a keymap, and\n\
1640 `macro' or t says FUNCTION is really a macro.\n\
1641 Third through fifth args give info about the real definition.\n\
1642 They default to nil.\n\
1643 If FUNCTION is already defined other than as an autoload,\n\
1644 this does nothing and returns nil.")
1645 (function, file, docstring, interactive, type)
1646 Lisp_Object function, file, docstring, interactive, type;
1648 #ifdef NO_ARG_ARRAY
1649 Lisp_Object args[4];
1650 #endif
1652 CHECK_SYMBOL (function, 0);
1653 CHECK_STRING (file, 1);
1655 /* If function is defined and not as an autoload, don't override */
1656 if (!EQ (XSYMBOL (function)->function, Qunbound)
1657 && !(CONSP (XSYMBOL (function)->function)
1658 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1659 return Qnil;
1661 if (NILP (Vpurify_flag))
1662 /* Only add entries after dumping, because the ones before are
1663 not useful and else we get loads of them from the loaddefs.el. */
1664 LOADHIST_ATTACH (Fcons (Qautoload, function));
1666 #ifdef NO_ARG_ARRAY
1667 args[0] = file;
1668 args[1] = docstring;
1669 args[2] = interactive;
1670 args[3] = type;
1672 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1673 #else /* NO_ARG_ARRAY */
1674 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1675 #endif /* not NO_ARG_ARRAY */
1678 Lisp_Object
1679 un_autoload (oldqueue)
1680 Lisp_Object oldqueue;
1682 register Lisp_Object queue, first, second;
1684 /* Queue to unwind is current value of Vautoload_queue.
1685 oldqueue is the shadowed value to leave in Vautoload_queue. */
1686 queue = Vautoload_queue;
1687 Vautoload_queue = oldqueue;
1688 while (CONSP (queue))
1690 first = Fcar (queue);
1691 second = Fcdr (first);
1692 first = Fcar (first);
1693 if (EQ (second, Qnil))
1694 Vfeatures = first;
1695 else
1696 Ffset (first, second);
1697 queue = Fcdr (queue);
1699 return Qnil;
1702 /* Load an autoloaded function.
1703 FUNNAME is the symbol which is the function's name.
1704 FUNDEF is the autoload definition (a list). */
1706 void
1707 do_autoload (fundef, funname)
1708 Lisp_Object fundef, funname;
1710 int count = specpdl_ptr - specpdl;
1711 Lisp_Object fun, queue, first, second;
1712 struct gcpro gcpro1, gcpro2, gcpro3;
1714 fun = funname;
1715 CHECK_SYMBOL (funname, 0);
1716 GCPRO3 (fun, funname, fundef);
1718 /* Preserve the match data. */
1719 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1721 /* Value saved here is to be restored into Vautoload_queue. */
1722 record_unwind_protect (un_autoload, Vautoload_queue);
1723 Vautoload_queue = Qt;
1724 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1726 /* Save the old autoloads, in case we ever do an unload. */
1727 queue = Vautoload_queue;
1728 while (CONSP (queue))
1730 first = Fcar (queue);
1731 second = Fcdr (first);
1732 first = Fcar (first);
1734 /* Note: This test is subtle. The cdr of an autoload-queue entry
1735 may be an atom if the autoload entry was generated by a defalias
1736 or fset. */
1737 if (CONSP (second))
1738 Fput (first, Qautoload, (Fcdr (second)));
1740 queue = Fcdr (queue);
1743 /* Once loading finishes, don't undo it. */
1744 Vautoload_queue = Qt;
1745 unbind_to (count, Qnil);
1747 fun = Findirect_function (fun);
1749 if (!NILP (Fequal (fun, fundef)))
1750 error ("Autoloading failed to define function %s",
1751 XSYMBOL (funname)->name->data);
1752 UNGCPRO;
1755 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1756 "Evaluate FORM and return its value.")
1757 (form)
1758 Lisp_Object form;
1760 Lisp_Object fun, val, original_fun, original_args;
1761 Lisp_Object funcar;
1762 struct backtrace backtrace;
1763 struct gcpro gcpro1, gcpro2, gcpro3;
1765 /* Since Fsignal resets this to 0, it had better be 0 now
1766 or else we have a potential bug. */
1767 if (interrupt_input_blocked != 0)
1768 abort ();
1770 if (SYMBOLP (form))
1772 if (EQ (Vmocklisp_arguments, Qt))
1773 return Fsymbol_value (form);
1774 val = Fsymbol_value (form);
1775 if (NILP (val))
1776 XSETFASTINT (val, 0);
1777 else if (EQ (val, Qt))
1778 XSETFASTINT (val, 1);
1779 return val;
1781 if (!CONSP (form))
1782 return form;
1784 QUIT;
1785 if (consing_since_gc > gc_cons_threshold)
1787 GCPRO1 (form);
1788 Fgarbage_collect ();
1789 UNGCPRO;
1792 if (++lisp_eval_depth > max_lisp_eval_depth)
1794 if (max_lisp_eval_depth < 100)
1795 max_lisp_eval_depth = 100;
1796 if (lisp_eval_depth > max_lisp_eval_depth)
1797 error ("Lisp nesting exceeds max-lisp-eval-depth");
1800 original_fun = Fcar (form);
1801 original_args = Fcdr (form);
1803 backtrace.next = backtrace_list;
1804 backtrace_list = &backtrace;
1805 backtrace.function = &original_fun; /* This also protects them from gc */
1806 backtrace.args = &original_args;
1807 backtrace.nargs = UNEVALLED;
1808 backtrace.evalargs = 1;
1809 backtrace.debug_on_exit = 0;
1811 if (debug_on_next_call)
1812 do_debug_on_call (Qt);
1814 /* At this point, only original_fun and original_args
1815 have values that will be used below */
1816 retry:
1817 fun = Findirect_function (original_fun);
1819 if (SUBRP (fun))
1821 Lisp_Object numargs;
1822 Lisp_Object argvals[8];
1823 Lisp_Object args_left;
1824 register int i, maxargs;
1826 args_left = original_args;
1827 numargs = Flength (args_left);
1829 if (XINT (numargs) < XSUBR (fun)->min_args ||
1830 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1831 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1833 if (XSUBR (fun)->max_args == UNEVALLED)
1835 backtrace.evalargs = 0;
1836 val = (*XSUBR (fun)->function) (args_left);
1837 goto done;
1840 if (XSUBR (fun)->max_args == MANY)
1842 /* Pass a vector of evaluated arguments */
1843 Lisp_Object *vals;
1844 register int argnum = 0;
1846 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1848 GCPRO3 (args_left, fun, fun);
1849 gcpro3.var = vals;
1850 gcpro3.nvars = 0;
1852 while (!NILP (args_left))
1854 vals[argnum++] = Feval (Fcar (args_left));
1855 args_left = Fcdr (args_left);
1856 gcpro3.nvars = argnum;
1859 backtrace.args = vals;
1860 backtrace.nargs = XINT (numargs);
1862 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1863 UNGCPRO;
1864 goto done;
1867 GCPRO3 (args_left, fun, fun);
1868 gcpro3.var = argvals;
1869 gcpro3.nvars = 0;
1871 maxargs = XSUBR (fun)->max_args;
1872 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1874 argvals[i] = Feval (Fcar (args_left));
1875 gcpro3.nvars = ++i;
1878 UNGCPRO;
1880 backtrace.args = argvals;
1881 backtrace.nargs = XINT (numargs);
1883 switch (i)
1885 case 0:
1886 val = (*XSUBR (fun)->function) ();
1887 goto done;
1888 case 1:
1889 val = (*XSUBR (fun)->function) (argvals[0]);
1890 goto done;
1891 case 2:
1892 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1893 goto done;
1894 case 3:
1895 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1896 argvals[2]);
1897 goto done;
1898 case 4:
1899 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1900 argvals[2], argvals[3]);
1901 goto done;
1902 case 5:
1903 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1904 argvals[3], argvals[4]);
1905 goto done;
1906 case 6:
1907 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1908 argvals[3], argvals[4], argvals[5]);
1909 goto done;
1910 case 7:
1911 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1912 argvals[3], argvals[4], argvals[5],
1913 argvals[6]);
1914 goto done;
1916 case 8:
1917 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1918 argvals[3], argvals[4], argvals[5],
1919 argvals[6], argvals[7]);
1920 goto done;
1922 default:
1923 /* Someone has created a subr that takes more arguments than
1924 is supported by this code. We need to either rewrite the
1925 subr to use a different argument protocol, or add more
1926 cases to this switch. */
1927 abort ();
1930 if (COMPILEDP (fun))
1931 val = apply_lambda (fun, original_args, 1);
1932 else
1934 if (!CONSP (fun))
1935 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1936 funcar = Fcar (fun);
1937 if (!SYMBOLP (funcar))
1938 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1939 if (EQ (funcar, Qautoload))
1941 do_autoload (fun, original_fun);
1942 goto retry;
1944 if (EQ (funcar, Qmacro))
1945 val = Feval (apply1 (Fcdr (fun), original_args));
1946 else if (EQ (funcar, Qlambda))
1947 val = apply_lambda (fun, original_args, 1);
1948 else if (EQ (funcar, Qmocklisp))
1949 val = ml_apply (fun, original_args);
1950 else
1951 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1953 done:
1954 if (!EQ (Vmocklisp_arguments, Qt))
1956 if (NILP (val))
1957 XSETFASTINT (val, 0);
1958 else if (EQ (val, Qt))
1959 XSETFASTINT (val, 1);
1961 lisp_eval_depth--;
1962 if (backtrace.debug_on_exit)
1963 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1964 backtrace_list = backtrace.next;
1965 return val;
1968 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1969 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1970 Then return the value FUNCTION returns.\n\
1971 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1972 (nargs, args)
1973 int nargs;
1974 Lisp_Object *args;
1976 register int i, numargs;
1977 register Lisp_Object spread_arg;
1978 register Lisp_Object *funcall_args;
1979 Lisp_Object fun;
1980 struct gcpro gcpro1;
1982 fun = args [0];
1983 funcall_args = 0;
1984 spread_arg = args [nargs - 1];
1985 CHECK_LIST (spread_arg, nargs);
1987 numargs = XINT (Flength (spread_arg));
1989 if (numargs == 0)
1990 return Ffuncall (nargs - 1, args);
1991 else if (numargs == 1)
1993 args [nargs - 1] = XCAR (spread_arg);
1994 return Ffuncall (nargs, args);
1997 numargs += nargs - 2;
1999 fun = indirect_function (fun);
2000 if (EQ (fun, Qunbound))
2002 /* Let funcall get the error */
2003 fun = args[0];
2004 goto funcall;
2007 if (SUBRP (fun))
2009 if (numargs < XSUBR (fun)->min_args
2010 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2011 goto funcall; /* Let funcall get the error */
2012 else if (XSUBR (fun)->max_args > numargs)
2014 /* Avoid making funcall cons up a yet another new vector of arguments
2015 by explicitly supplying nil's for optional values */
2016 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2017 * sizeof (Lisp_Object));
2018 for (i = numargs; i < XSUBR (fun)->max_args;)
2019 funcall_args[++i] = Qnil;
2020 GCPRO1 (*funcall_args);
2021 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2024 funcall:
2025 /* We add 1 to numargs because funcall_args includes the
2026 function itself as well as its arguments. */
2027 if (!funcall_args)
2029 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2030 * sizeof (Lisp_Object));
2031 GCPRO1 (*funcall_args);
2032 gcpro1.nvars = 1 + numargs;
2035 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2036 /* Spread the last arg we got. Its first element goes in
2037 the slot that it used to occupy, hence this value of I. */
2038 i = nargs - 1;
2039 while (!NILP (spread_arg))
2041 funcall_args [i++] = XCAR (spread_arg);
2042 spread_arg = XCDR (spread_arg);
2045 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2048 /* Run hook variables in various ways. */
2050 enum run_hooks_condition {to_completion, until_success, until_failure};
2052 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
2053 "Run each hook in HOOKS. Major mode functions use this.\n\
2054 Each argument should be a symbol, a hook variable.\n\
2055 These symbols are processed in the order specified.\n\
2056 If a hook symbol has a non-nil value, that value may be a function\n\
2057 or a list of functions to be called to run the hook.\n\
2058 If the value is a function, it is called with no arguments.\n\
2059 If it is a list, the elements are called, in order, with no arguments.\n\
2061 To make a hook variable buffer-local, use `make-local-hook',\n\
2062 not `make-local-variable'.")
2063 (nargs, args)
2064 int nargs;
2065 Lisp_Object *args;
2067 Lisp_Object hook[1];
2068 register int i;
2070 for (i = 0; i < nargs; i++)
2072 hook[0] = args[i];
2073 run_hook_with_args (1, hook, to_completion);
2076 return Qnil;
2079 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2080 Srun_hook_with_args, 1, MANY, 0,
2081 "Run HOOK with the specified arguments ARGS.\n\
2082 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2083 value, that value may be a function or a list of functions to be\n\
2084 called to run the hook. If the value is a function, it is called with\n\
2085 the given arguments and its return value is returned. If it is a list\n\
2086 of functions, those functions are called, in order,\n\
2087 with the given arguments ARGS.\n\
2088 It is best not to depend on the value return by `run-hook-with-args',\n\
2089 as that may change.\n\
2091 To make a hook variable buffer-local, use `make-local-hook',\n\
2092 not `make-local-variable'.")
2093 (nargs, args)
2094 int nargs;
2095 Lisp_Object *args;
2097 return run_hook_with_args (nargs, args, to_completion);
2100 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2101 Srun_hook_with_args_until_success, 1, MANY, 0,
2102 "Run HOOK with the specified arguments ARGS.\n\
2103 HOOK should be a symbol, a hook variable. Its value should\n\
2104 be a list of functions. We call those functions, one by one,\n\
2105 passing arguments ARGS to each of them, until one of them\n\
2106 returns a non-nil value. Then we return that value.\n\
2107 If all the functions return nil, we return nil.\n\
2109 To make a hook variable buffer-local, use `make-local-hook',\n\
2110 not `make-local-variable'.")
2111 (nargs, args)
2112 int nargs;
2113 Lisp_Object *args;
2115 return run_hook_with_args (nargs, args, until_success);
2118 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2119 Srun_hook_with_args_until_failure, 1, MANY, 0,
2120 "Run HOOK with the specified arguments ARGS.\n\
2121 HOOK should be a symbol, a hook variable. Its value should\n\
2122 be a list of functions. We call those functions, one by one,\n\
2123 passing arguments ARGS to each of them, until one of them\n\
2124 returns nil. Then we return nil.\n\
2125 If all the functions return non-nil, we return non-nil.\n\
2127 To make a hook variable buffer-local, use `make-local-hook',\n\
2128 not `make-local-variable'.")
2129 (nargs, args)
2130 int nargs;
2131 Lisp_Object *args;
2133 return run_hook_with_args (nargs, args, until_failure);
2136 /* ARGS[0] should be a hook symbol.
2137 Call each of the functions in the hook value, passing each of them
2138 as arguments all the rest of ARGS (all NARGS - 1 elements).
2139 COND specifies a condition to test after each call
2140 to decide whether to stop.
2141 The caller (or its caller, etc) must gcpro all of ARGS,
2142 except that it isn't necessary to gcpro ARGS[0]. */
2144 Lisp_Object
2145 run_hook_with_args (nargs, args, cond)
2146 int nargs;
2147 Lisp_Object *args;
2148 enum run_hooks_condition cond;
2150 Lisp_Object sym, val, ret;
2151 Lisp_Object globals;
2152 struct gcpro gcpro1, gcpro2, gcpro3;
2154 /* If we are dying or still initializing,
2155 don't do anything--it would probably crash if we tried. */
2156 if (NILP (Vrun_hooks))
2157 return Qnil;
2159 sym = args[0];
2160 val = find_symbol_value (sym);
2161 ret = (cond == until_failure ? Qt : Qnil);
2163 if (EQ (val, Qunbound) || NILP (val))
2164 return ret;
2165 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2167 args[0] = val;
2168 return Ffuncall (nargs, args);
2170 else
2172 globals = Qnil;
2173 GCPRO3 (sym, val, globals);
2175 for (;
2176 CONSP (val) && ((cond == to_completion)
2177 || (cond == until_success ? NILP (ret)
2178 : !NILP (ret)));
2179 val = XCDR (val))
2181 if (EQ (XCAR (val), Qt))
2183 /* t indicates this hook has a local binding;
2184 it means to run the global binding too. */
2186 for (globals = Fdefault_value (sym);
2187 CONSP (globals) && ((cond == to_completion)
2188 || (cond == until_success ? NILP (ret)
2189 : !NILP (ret)));
2190 globals = XCDR (globals))
2192 args[0] = XCAR (globals);
2193 /* In a global value, t should not occur. If it does, we
2194 must ignore it to avoid an endless loop. */
2195 if (!EQ (args[0], Qt))
2196 ret = Ffuncall (nargs, args);
2199 else
2201 args[0] = XCAR (val);
2202 ret = Ffuncall (nargs, args);
2206 UNGCPRO;
2207 return ret;
2211 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2212 present value of that symbol.
2213 Call each element of FUNLIST,
2214 passing each of them the rest of ARGS.
2215 The caller (or its caller, etc) must gcpro all of ARGS,
2216 except that it isn't necessary to gcpro ARGS[0]. */
2218 Lisp_Object
2219 run_hook_list_with_args (funlist, nargs, args)
2220 Lisp_Object funlist;
2221 int nargs;
2222 Lisp_Object *args;
2224 Lisp_Object sym;
2225 Lisp_Object val;
2226 Lisp_Object globals;
2227 struct gcpro gcpro1, gcpro2, gcpro3;
2229 sym = args[0];
2230 globals = Qnil;
2231 GCPRO3 (sym, val, globals);
2233 for (val = funlist; CONSP (val); val = XCDR (val))
2235 if (EQ (XCAR (val), Qt))
2237 /* t indicates this hook has a local binding;
2238 it means to run the global binding too. */
2240 for (globals = Fdefault_value (sym);
2241 CONSP (globals);
2242 globals = XCDR (globals))
2244 args[0] = XCAR (globals);
2245 /* In a global value, t should not occur. If it does, we
2246 must ignore it to avoid an endless loop. */
2247 if (!EQ (args[0], Qt))
2248 Ffuncall (nargs, args);
2251 else
2253 args[0] = XCAR (val);
2254 Ffuncall (nargs, args);
2257 UNGCPRO;
2258 return Qnil;
2261 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2263 void
2264 run_hook_with_args_2 (hook, arg1, arg2)
2265 Lisp_Object hook, arg1, arg2;
2267 Lisp_Object temp[3];
2268 temp[0] = hook;
2269 temp[1] = arg1;
2270 temp[2] = arg2;
2272 Frun_hook_with_args (3, temp);
2275 /* Apply fn to arg */
2276 Lisp_Object
2277 apply1 (fn, arg)
2278 Lisp_Object fn, arg;
2280 struct gcpro gcpro1;
2282 GCPRO1 (fn);
2283 if (NILP (arg))
2284 RETURN_UNGCPRO (Ffuncall (1, &fn));
2285 gcpro1.nvars = 2;
2286 #ifdef NO_ARG_ARRAY
2288 Lisp_Object args[2];
2289 args[0] = fn;
2290 args[1] = arg;
2291 gcpro1.var = args;
2292 RETURN_UNGCPRO (Fapply (2, args));
2294 #else /* not NO_ARG_ARRAY */
2295 RETURN_UNGCPRO (Fapply (2, &fn));
2296 #endif /* not NO_ARG_ARRAY */
2299 /* Call function fn on no arguments */
2300 Lisp_Object
2301 call0 (fn)
2302 Lisp_Object fn;
2304 struct gcpro gcpro1;
2306 GCPRO1 (fn);
2307 RETURN_UNGCPRO (Ffuncall (1, &fn));
2310 /* Call function fn with 1 argument arg1 */
2311 /* ARGSUSED */
2312 Lisp_Object
2313 call1 (fn, arg1)
2314 Lisp_Object fn, arg1;
2316 struct gcpro gcpro1;
2317 #ifdef NO_ARG_ARRAY
2318 Lisp_Object args[2];
2320 args[0] = fn;
2321 args[1] = arg1;
2322 GCPRO1 (args[0]);
2323 gcpro1.nvars = 2;
2324 RETURN_UNGCPRO (Ffuncall (2, args));
2325 #else /* not NO_ARG_ARRAY */
2326 GCPRO1 (fn);
2327 gcpro1.nvars = 2;
2328 RETURN_UNGCPRO (Ffuncall (2, &fn));
2329 #endif /* not NO_ARG_ARRAY */
2332 /* Call function fn with 2 arguments arg1, arg2 */
2333 /* ARGSUSED */
2334 Lisp_Object
2335 call2 (fn, arg1, arg2)
2336 Lisp_Object fn, arg1, arg2;
2338 struct gcpro gcpro1;
2339 #ifdef NO_ARG_ARRAY
2340 Lisp_Object args[3];
2341 args[0] = fn;
2342 args[1] = arg1;
2343 args[2] = arg2;
2344 GCPRO1 (args[0]);
2345 gcpro1.nvars = 3;
2346 RETURN_UNGCPRO (Ffuncall (3, args));
2347 #else /* not NO_ARG_ARRAY */
2348 GCPRO1 (fn);
2349 gcpro1.nvars = 3;
2350 RETURN_UNGCPRO (Ffuncall (3, &fn));
2351 #endif /* not NO_ARG_ARRAY */
2354 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2355 /* ARGSUSED */
2356 Lisp_Object
2357 call3 (fn, arg1, arg2, arg3)
2358 Lisp_Object fn, arg1, arg2, arg3;
2360 struct gcpro gcpro1;
2361 #ifdef NO_ARG_ARRAY
2362 Lisp_Object args[4];
2363 args[0] = fn;
2364 args[1] = arg1;
2365 args[2] = arg2;
2366 args[3] = arg3;
2367 GCPRO1 (args[0]);
2368 gcpro1.nvars = 4;
2369 RETURN_UNGCPRO (Ffuncall (4, args));
2370 #else /* not NO_ARG_ARRAY */
2371 GCPRO1 (fn);
2372 gcpro1.nvars = 4;
2373 RETURN_UNGCPRO (Ffuncall (4, &fn));
2374 #endif /* not NO_ARG_ARRAY */
2377 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2378 /* ARGSUSED */
2379 Lisp_Object
2380 call4 (fn, arg1, arg2, arg3, arg4)
2381 Lisp_Object fn, arg1, arg2, arg3, arg4;
2383 struct gcpro gcpro1;
2384 #ifdef NO_ARG_ARRAY
2385 Lisp_Object args[5];
2386 args[0] = fn;
2387 args[1] = arg1;
2388 args[2] = arg2;
2389 args[3] = arg3;
2390 args[4] = arg4;
2391 GCPRO1 (args[0]);
2392 gcpro1.nvars = 5;
2393 RETURN_UNGCPRO (Ffuncall (5, args));
2394 #else /* not NO_ARG_ARRAY */
2395 GCPRO1 (fn);
2396 gcpro1.nvars = 5;
2397 RETURN_UNGCPRO (Ffuncall (5, &fn));
2398 #endif /* not NO_ARG_ARRAY */
2401 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2402 /* ARGSUSED */
2403 Lisp_Object
2404 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2405 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2407 struct gcpro gcpro1;
2408 #ifdef NO_ARG_ARRAY
2409 Lisp_Object args[6];
2410 args[0] = fn;
2411 args[1] = arg1;
2412 args[2] = arg2;
2413 args[3] = arg3;
2414 args[4] = arg4;
2415 args[5] = arg5;
2416 GCPRO1 (args[0]);
2417 gcpro1.nvars = 6;
2418 RETURN_UNGCPRO (Ffuncall (6, args));
2419 #else /* not NO_ARG_ARRAY */
2420 GCPRO1 (fn);
2421 gcpro1.nvars = 6;
2422 RETURN_UNGCPRO (Ffuncall (6, &fn));
2423 #endif /* not NO_ARG_ARRAY */
2426 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2427 /* ARGSUSED */
2428 Lisp_Object
2429 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2430 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2432 struct gcpro gcpro1;
2433 #ifdef NO_ARG_ARRAY
2434 Lisp_Object args[7];
2435 args[0] = fn;
2436 args[1] = arg1;
2437 args[2] = arg2;
2438 args[3] = arg3;
2439 args[4] = arg4;
2440 args[5] = arg5;
2441 args[6] = arg6;
2442 GCPRO1 (args[0]);
2443 gcpro1.nvars = 7;
2444 RETURN_UNGCPRO (Ffuncall (7, args));
2445 #else /* not NO_ARG_ARRAY */
2446 GCPRO1 (fn);
2447 gcpro1.nvars = 7;
2448 RETURN_UNGCPRO (Ffuncall (7, &fn));
2449 #endif /* not NO_ARG_ARRAY */
2452 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2453 "Call first argument as a function, passing remaining arguments to it.\n\
2454 Return the value that function returns.\n\
2455 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2456 (nargs, args)
2457 int nargs;
2458 Lisp_Object *args;
2460 Lisp_Object fun;
2461 Lisp_Object funcar;
2462 int numargs = nargs - 1;
2463 Lisp_Object lisp_numargs;
2464 Lisp_Object val;
2465 struct backtrace backtrace;
2466 register Lisp_Object *internal_args;
2467 register int i;
2469 QUIT;
2470 if (consing_since_gc > gc_cons_threshold)
2471 Fgarbage_collect ();
2473 if (++lisp_eval_depth > max_lisp_eval_depth)
2475 if (max_lisp_eval_depth < 100)
2476 max_lisp_eval_depth = 100;
2477 if (lisp_eval_depth > max_lisp_eval_depth)
2478 error ("Lisp nesting exceeds max-lisp-eval-depth");
2481 backtrace.next = backtrace_list;
2482 backtrace_list = &backtrace;
2483 backtrace.function = &args[0];
2484 backtrace.args = &args[1];
2485 backtrace.nargs = nargs - 1;
2486 backtrace.evalargs = 0;
2487 backtrace.debug_on_exit = 0;
2489 if (debug_on_next_call)
2490 do_debug_on_call (Qlambda);
2492 retry:
2494 fun = args[0];
2496 fun = Findirect_function (fun);
2498 if (SUBRP (fun))
2500 if (numargs < XSUBR (fun)->min_args
2501 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2503 XSETFASTINT (lisp_numargs, numargs);
2504 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2507 if (XSUBR (fun)->max_args == UNEVALLED)
2508 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2510 if (XSUBR (fun)->max_args == MANY)
2512 val = (*XSUBR (fun)->function) (numargs, args + 1);
2513 goto done;
2516 if (XSUBR (fun)->max_args > numargs)
2518 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2519 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2520 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2521 internal_args[i] = Qnil;
2523 else
2524 internal_args = args + 1;
2525 switch (XSUBR (fun)->max_args)
2527 case 0:
2528 val = (*XSUBR (fun)->function) ();
2529 goto done;
2530 case 1:
2531 val = (*XSUBR (fun)->function) (internal_args[0]);
2532 goto done;
2533 case 2:
2534 val = (*XSUBR (fun)->function) (internal_args[0],
2535 internal_args[1]);
2536 goto done;
2537 case 3:
2538 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2539 internal_args[2]);
2540 goto done;
2541 case 4:
2542 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2543 internal_args[2],
2544 internal_args[3]);
2545 goto done;
2546 case 5:
2547 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2548 internal_args[2], internal_args[3],
2549 internal_args[4]);
2550 goto done;
2551 case 6:
2552 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2553 internal_args[2], internal_args[3],
2554 internal_args[4], internal_args[5]);
2555 goto done;
2556 case 7:
2557 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2558 internal_args[2], internal_args[3],
2559 internal_args[4], internal_args[5],
2560 internal_args[6]);
2561 goto done;
2563 case 8:
2564 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2565 internal_args[2], internal_args[3],
2566 internal_args[4], internal_args[5],
2567 internal_args[6], internal_args[7]);
2568 goto done;
2570 default:
2572 /* If a subr takes more than 8 arguments without using MANY
2573 or UNEVALLED, we need to extend this function to support it.
2574 Until this is done, there is no way to call the function. */
2575 abort ();
2578 if (COMPILEDP (fun))
2579 val = funcall_lambda (fun, numargs, args + 1);
2580 else
2582 if (!CONSP (fun))
2583 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2584 funcar = Fcar (fun);
2585 if (!SYMBOLP (funcar))
2586 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2587 if (EQ (funcar, Qlambda))
2588 val = funcall_lambda (fun, numargs, args + 1);
2589 else if (EQ (funcar, Qmocklisp))
2590 val = ml_apply (fun, Flist (numargs, args + 1));
2591 else if (EQ (funcar, Qautoload))
2593 do_autoload (fun, args[0]);
2594 goto retry;
2596 else
2597 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2599 done:
2600 lisp_eval_depth--;
2601 if (backtrace.debug_on_exit)
2602 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2603 backtrace_list = backtrace.next;
2604 return val;
2607 Lisp_Object
2608 apply_lambda (fun, args, eval_flag)
2609 Lisp_Object fun, args;
2610 int eval_flag;
2612 Lisp_Object args_left;
2613 Lisp_Object numargs;
2614 register Lisp_Object *arg_vector;
2615 struct gcpro gcpro1, gcpro2, gcpro3;
2616 register int i;
2617 register Lisp_Object tem;
2619 numargs = Flength (args);
2620 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2621 args_left = args;
2623 GCPRO3 (*arg_vector, args_left, fun);
2624 gcpro1.nvars = 0;
2626 for (i = 0; i < XINT (numargs);)
2628 tem = Fcar (args_left), args_left = Fcdr (args_left);
2629 if (eval_flag) tem = Feval (tem);
2630 arg_vector[i++] = tem;
2631 gcpro1.nvars = i;
2634 UNGCPRO;
2636 if (eval_flag)
2638 backtrace_list->args = arg_vector;
2639 backtrace_list->nargs = i;
2641 backtrace_list->evalargs = 0;
2642 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2644 /* Do the debug-on-exit now, while arg_vector still exists. */
2645 if (backtrace_list->debug_on_exit)
2646 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2647 /* Don't do it again when we return to eval. */
2648 backtrace_list->debug_on_exit = 0;
2649 return tem;
2652 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2653 and return the result of evaluation.
2654 FUN must be either a lambda-expression or a compiled-code object. */
2656 Lisp_Object
2657 funcall_lambda (fun, nargs, arg_vector)
2658 Lisp_Object fun;
2659 int nargs;
2660 register Lisp_Object *arg_vector;
2662 Lisp_Object val, syms_left, next;
2663 int count = specpdl_ptr - specpdl;
2664 int i, optional, rest;
2666 if (NILP (Vmocklisp_arguments))
2667 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2669 if (CONSP (fun))
2671 syms_left = XCDR (fun);
2672 if (CONSP (syms_left))
2673 syms_left = XCAR (syms_left);
2674 else
2675 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2677 else if (COMPILEDP (fun))
2678 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2679 else
2680 abort ();
2682 i = optional = rest = 0;
2683 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2685 QUIT;
2687 next = XCAR (syms_left);
2688 while (!SYMBOLP (next))
2689 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2691 if (EQ (next, Qand_rest))
2692 rest = 1;
2693 else if (EQ (next, Qand_optional))
2694 optional = 1;
2695 else if (rest)
2697 specbind (next, Flist (nargs - i, &arg_vector[i]));
2698 i = nargs;
2700 else if (i < nargs)
2701 specbind (next, arg_vector[i++]);
2702 else if (!optional)
2703 return Fsignal (Qwrong_number_of_arguments,
2704 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2705 else
2706 specbind (next, Qnil);
2709 if (!NILP (syms_left))
2710 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2711 else if (i < nargs)
2712 return Fsignal (Qwrong_number_of_arguments,
2713 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2715 if (CONSP (fun))
2716 val = Fprogn (XCDR (XCDR (fun)));
2717 else
2719 /* If we have not actually read the bytecode string
2720 and constants vector yet, fetch them from the file. */
2721 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2722 Ffetch_bytecode (fun);
2723 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2724 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2725 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2728 return unbind_to (count, val);
2731 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2732 1, 1, 0,
2733 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2734 (object)
2735 Lisp_Object object;
2737 Lisp_Object tem;
2739 if (COMPILEDP (object)
2740 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2742 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2743 if (!CONSP (tem))
2744 error ("invalid byte code");
2745 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2746 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2748 return object;
2751 void
2752 grow_specpdl ()
2754 register int count = specpdl_ptr - specpdl;
2755 if (specpdl_size >= max_specpdl_size)
2757 if (max_specpdl_size < 400)
2758 max_specpdl_size = 400;
2759 if (specpdl_size >= max_specpdl_size)
2761 if (!NILP (Vdebug_on_error))
2762 /* Leave room for some specpdl in the debugger. */
2763 max_specpdl_size = specpdl_size + 100;
2764 Fsignal (Qerror,
2765 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2768 specpdl_size *= 2;
2769 if (specpdl_size > max_specpdl_size)
2770 specpdl_size = max_specpdl_size;
2771 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2772 specpdl_ptr = specpdl + count;
2775 void
2776 specbind (symbol, value)
2777 Lisp_Object symbol, value;
2779 Lisp_Object ovalue;
2781 CHECK_SYMBOL (symbol, 0);
2782 if (specpdl_ptr == specpdl + specpdl_size)
2783 grow_specpdl ();
2785 /* The most common case is that a non-constant symbol with a trivial
2786 value. Make that as fast as we can. */
2787 if (!MISCP (XSYMBOL (symbol)->value)
2788 && !EQ (symbol, Qnil)
2789 && !EQ (symbol, Qt)
2790 && !(XSYMBOL (symbol)->name->data[0] == ':'
2791 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
2792 && !EQ (value, symbol)))
2794 specpdl_ptr->symbol = symbol;
2795 specpdl_ptr->old_value = XSYMBOL (symbol)->value;
2796 specpdl_ptr->func = NULL;
2797 ++specpdl_ptr;
2798 XSYMBOL (symbol)->value = value;
2800 else
2802 ovalue = find_symbol_value (symbol);
2803 specpdl_ptr->func = 0;
2804 specpdl_ptr->old_value = ovalue;
2806 if (BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2807 || SOME_BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2808 || BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2810 Lisp_Object current_buffer, binding_buffer;
2811 /* For a local variable, record both the symbol and which
2812 buffer's value we are saving. */
2813 current_buffer = Fcurrent_buffer ();
2814 binding_buffer = current_buffer;
2815 /* If the variable is not local in this buffer,
2816 we are saving the global value, so restore that. */
2817 if (NILP (Flocal_variable_p (symbol, binding_buffer)))
2818 binding_buffer = Qnil;
2819 specpdl_ptr->symbol
2820 = Fcons (symbol, Fcons (binding_buffer, current_buffer));
2822 else
2823 specpdl_ptr->symbol = symbol;
2825 specpdl_ptr++;
2826 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2827 store_symval_forwarding (symbol, ovalue, value);
2828 else
2829 set_internal (symbol, value, 0, 1);
2833 void
2834 record_unwind_protect (function, arg)
2835 Lisp_Object (*function) P_ ((Lisp_Object));
2836 Lisp_Object arg;
2838 if (specpdl_ptr == specpdl + specpdl_size)
2839 grow_specpdl ();
2840 specpdl_ptr->func = function;
2841 specpdl_ptr->symbol = Qnil;
2842 specpdl_ptr->old_value = arg;
2843 specpdl_ptr++;
2846 Lisp_Object
2847 unbind_to (count, value)
2848 int count;
2849 Lisp_Object value;
2851 int quitf = !NILP (Vquit_flag);
2852 struct gcpro gcpro1;
2854 GCPRO1 (value);
2855 Vquit_flag = Qnil;
2857 while (specpdl_ptr != specpdl + count)
2859 --specpdl_ptr;
2861 if (specpdl_ptr->func != 0)
2862 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2863 /* Note that a "binding" of nil is really an unwind protect,
2864 so in that case the "old value" is a list of forms to evaluate. */
2865 else if (NILP (specpdl_ptr->symbol))
2866 Fprogn (specpdl_ptr->old_value);
2867 /* If the symbol is a list, it is really
2868 (SYMBOL BINDING_BUFFER . CURRENT_BUFFER)
2869 and it indicates we bound a variable that has
2870 buffer-local bindings. */
2871 else if (CONSP (specpdl_ptr->symbol))
2873 Lisp_Object symbol, buffer;
2875 symbol = XCAR (specpdl_ptr->symbol);
2876 buffer = XCAR (XCDR (specpdl_ptr->symbol));
2878 /* Handle restoring a default value. */
2879 if (NILP (buffer))
2880 Fset_default (symbol, specpdl_ptr->old_value);
2881 /* Handle restoring a value saved from a live buffer. */
2882 else
2883 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (buffer), 1);
2885 else
2887 /* If variable has a trivial value (no forwarding), we can
2888 just set it. No need to check for constant symbols here,
2889 since that was already done by specbind. */
2890 if (!MISCP (XSYMBOL (specpdl_ptr->symbol)->value))
2891 XSYMBOL (specpdl_ptr->symbol)->value = specpdl_ptr->old_value;
2892 else
2893 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
2897 if (NILP (Vquit_flag) && quitf)
2898 Vquit_flag = Qt;
2900 UNGCPRO;
2901 return value;
2904 #if 0
2906 /* Get the value of symbol's global binding, even if that binding
2907 is not now dynamically visible. */
2909 Lisp_Object
2910 top_level_value (symbol)
2911 Lisp_Object symbol;
2913 register struct specbinding *ptr = specpdl;
2915 CHECK_SYMBOL (symbol, 0);
2916 for (; ptr != specpdl_ptr; ptr++)
2918 if (EQ (ptr->symbol, symbol))
2919 return ptr->old_value;
2921 return Fsymbol_value (symbol);
2924 Lisp_Object
2925 top_level_set (symbol, newval)
2926 Lisp_Object symbol, newval;
2928 register struct specbinding *ptr = specpdl;
2930 CHECK_SYMBOL (symbol, 0);
2931 for (; ptr != specpdl_ptr; ptr++)
2933 if (EQ (ptr->symbol, symbol))
2935 ptr->old_value = newval;
2936 return newval;
2939 return Fset (symbol, newval);
2942 #endif /* 0 */
2944 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2945 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2946 The debugger is entered when that frame exits, if the flag is non-nil.")
2947 (level, flag)
2948 Lisp_Object level, flag;
2950 register struct backtrace *backlist = backtrace_list;
2951 register int i;
2953 CHECK_NUMBER (level, 0);
2955 for (i = 0; backlist && i < XINT (level); i++)
2957 backlist = backlist->next;
2960 if (backlist)
2961 backlist->debug_on_exit = !NILP (flag);
2963 return flag;
2966 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2967 "Print a trace of Lisp function calls currently active.\n\
2968 Output stream used is value of `standard-output'.")
2971 register struct backtrace *backlist = backtrace_list;
2972 register int i;
2973 Lisp_Object tail;
2974 Lisp_Object tem;
2975 extern Lisp_Object Vprint_level;
2976 struct gcpro gcpro1;
2978 XSETFASTINT (Vprint_level, 3);
2980 tail = Qnil;
2981 GCPRO1 (tail);
2983 while (backlist)
2985 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2986 if (backlist->nargs == UNEVALLED)
2988 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2989 write_string ("\n", -1);
2991 else
2993 tem = *backlist->function;
2994 Fprin1 (tem, Qnil); /* This can QUIT */
2995 write_string ("(", -1);
2996 if (backlist->nargs == MANY)
2998 for (tail = *backlist->args, i = 0;
2999 !NILP (tail);
3000 tail = Fcdr (tail), i++)
3002 if (i) write_string (" ", -1);
3003 Fprin1 (Fcar (tail), Qnil);
3006 else
3008 for (i = 0; i < backlist->nargs; i++)
3010 if (i) write_string (" ", -1);
3011 Fprin1 (backlist->args[i], Qnil);
3014 write_string (")\n", -1);
3016 backlist = backlist->next;
3019 Vprint_level = Qnil;
3020 UNGCPRO;
3021 return Qnil;
3024 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
3025 "Return the function and arguments NFRAMES up from current execution point.\n\
3026 If that frame has not evaluated the arguments yet (or is a special form),\n\
3027 the value is (nil FUNCTION ARG-FORMS...).\n\
3028 If that frame has evaluated its arguments and called its function already,\n\
3029 the value is (t FUNCTION ARG-VALUES...).\n\
3030 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3031 FUNCTION is whatever was supplied as car of evaluated list,\n\
3032 or a lambda expression for macro calls.\n\
3033 If NFRAMES is more than the number of frames, the value is nil.")
3034 (nframes)
3035 Lisp_Object nframes;
3037 register struct backtrace *backlist = backtrace_list;
3038 register int i;
3039 Lisp_Object tem;
3041 CHECK_NATNUM (nframes, 0);
3043 /* Find the frame requested. */
3044 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3045 backlist = backlist->next;
3047 if (!backlist)
3048 return Qnil;
3049 if (backlist->nargs == UNEVALLED)
3050 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3051 else
3053 if (backlist->nargs == MANY)
3054 tem = *backlist->args;
3055 else
3056 tem = Flist (backlist->nargs, backlist->args);
3058 return Fcons (Qt, Fcons (*backlist->function, tem));
3062 void
3063 syms_of_eval ()
3065 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3066 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3067 If Lisp code tries to make more than this many at once,\n\
3068 an error is signaled.");
3070 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3071 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
3072 This limit is to catch infinite recursions for you before they cause\n\
3073 actual stack overflow in C, which would be fatal for Emacs.\n\
3074 You can safely make it considerably larger than its default value,\n\
3075 if that proves inconveniently small.");
3077 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3078 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
3079 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
3080 Vquit_flag = Qnil;
3082 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3083 "Non-nil inhibits C-g quitting from happening immediately.\n\
3084 Note that `quit-flag' will still be set by typing C-g,\n\
3085 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
3086 To prevent this happening, set `quit-flag' to nil\n\
3087 before making `inhibit-quit' nil.");
3088 Vinhibit_quit = Qnil;
3090 Qinhibit_quit = intern ("inhibit-quit");
3091 staticpro (&Qinhibit_quit);
3093 Qautoload = intern ("autoload");
3094 staticpro (&Qautoload);
3096 Qdebug_on_error = intern ("debug-on-error");
3097 staticpro (&Qdebug_on_error);
3099 Qmacro = intern ("macro");
3100 staticpro (&Qmacro);
3102 /* Note that the process handling also uses Qexit, but we don't want
3103 to staticpro it twice, so we just do it here. */
3104 Qexit = intern ("exit");
3105 staticpro (&Qexit);
3107 Qinteractive = intern ("interactive");
3108 staticpro (&Qinteractive);
3110 Qcommandp = intern ("commandp");
3111 staticpro (&Qcommandp);
3113 Qdefun = intern ("defun");
3114 staticpro (&Qdefun);
3116 Qand_rest = intern ("&rest");
3117 staticpro (&Qand_rest);
3119 Qand_optional = intern ("&optional");
3120 staticpro (&Qand_optional);
3122 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3123 "*Non-nil means automatically display a backtrace buffer\n\
3124 after any error that is handled by the editor command loop.\n\
3125 If the value is a list, an error only means to display a backtrace\n\
3126 if one of its condition symbols appears in the list.");
3127 Vstack_trace_on_error = Qnil;
3129 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3130 "*Non-nil means enter debugger if an error is signaled.\n\
3131 Does not apply to errors handled by `condition-case'.\n\
3132 If the value is a list, an error only means to enter the debugger\n\
3133 if one of its condition symbols appears in the list.\n\
3134 See also variable `debug-on-quit'.");
3135 Vdebug_on_error = Qnil;
3137 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3138 "*List of errors for which the debugger should not be called.\n\
3139 Each element may be a condition-name or a regexp that matches error messages.\n\
3140 If any element applies to a given error, that error skips the debugger\n\
3141 and just returns to top level.\n\
3142 This overrides the variable `debug-on-error'.\n\
3143 It does not apply to errors handled by `condition-case'.");
3144 Vdebug_ignored_errors = Qnil;
3146 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3147 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3148 Does not apply if quit is handled by a `condition-case'.");
3149 debug_on_quit = 0;
3151 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3152 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3154 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3155 "Non-nil means debugger may continue execution.\n\
3156 This is nil when the debugger is called under circumstances where it\n\
3157 might not be safe to continue.");
3158 debugger_may_continue = 1;
3160 DEFVAR_LISP ("debugger", &Vdebugger,
3161 "Function to call to invoke debugger.\n\
3162 If due to frame exit, args are `exit' and the value being returned;\n\
3163 this function's value will be returned instead of that.\n\
3164 If due to error, args are `error' and a list of the args to `signal'.\n\
3165 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3166 If due to `eval' entry, one arg, t.");
3167 Vdebugger = Qnil;
3169 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3170 "If non-nil, this is a function for `signal' to call.\n\
3171 It receives the same arguments that `signal' was given.\n\
3172 The Edebug package uses this to regain control.");
3173 Vsignal_hook_function = Qnil;
3175 Qmocklisp_arguments = intern ("mocklisp-arguments");
3176 staticpro (&Qmocklisp_arguments);
3177 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3178 "While in a mocklisp function, the list of its unevaluated args.");
3179 Vmocklisp_arguments = Qt;
3181 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3182 "*Non-nil means call the debugger regardless of condition handlers.\n\
3183 Note that `debug-on-error', `debug-on-quit' and friends\n\
3184 still determine whether to handle the particular condition.");
3185 Vdebug_on_signal = Qnil;
3187 Vrun_hooks = intern ("run-hooks");
3188 staticpro (&Vrun_hooks);
3190 staticpro (&Vautoload_queue);
3191 Vautoload_queue = Qnil;
3193 defsubr (&Sor);
3194 defsubr (&Sand);
3195 defsubr (&Sif);
3196 defsubr (&Scond);
3197 defsubr (&Sprogn);
3198 defsubr (&Sprog1);
3199 defsubr (&Sprog2);
3200 defsubr (&Ssetq);
3201 defsubr (&Squote);
3202 defsubr (&Sfunction);
3203 defsubr (&Sdefun);
3204 defsubr (&Sdefmacro);
3205 defsubr (&Sdefvar);
3206 defsubr (&Sdefconst);
3207 defsubr (&Suser_variable_p);
3208 defsubr (&Slet);
3209 defsubr (&SletX);
3210 defsubr (&Swhile);
3211 defsubr (&Smacroexpand);
3212 defsubr (&Scatch);
3213 defsubr (&Sthrow);
3214 defsubr (&Sunwind_protect);
3215 defsubr (&Scondition_case);
3216 defsubr (&Ssignal);
3217 defsubr (&Sinteractive_p);
3218 defsubr (&Scommandp);
3219 defsubr (&Sautoload);
3220 defsubr (&Seval);
3221 defsubr (&Sapply);
3222 defsubr (&Sfuncall);
3223 defsubr (&Srun_hooks);
3224 defsubr (&Srun_hook_with_args);
3225 defsubr (&Srun_hook_with_args_until_success);
3226 defsubr (&Srun_hook_with_args_until_failure);
3227 defsubr (&Sfetch_bytecode);
3228 defsubr (&Sbacktrace_debug);
3229 defsubr (&Sbacktrace);
3230 defsubr (&Sbacktrace_frame);