(cl-do-arglist): Revert change of
[emacs.git] / src / eval.c
blobe000ea622c55e859d89e73f869323b123bbe3cf5
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000, 2001
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. */
68 struct catchtag
70 Lisp_Object tag;
71 Lisp_Object val;
72 struct catchtag *next;
73 struct gcpro *gcpro;
74 jmp_buf jmp;
75 struct backtrace *backlist;
76 struct handler *handlerlist;
77 int lisp_eval_depth;
78 int pdlcount;
79 int poll_suppress_count;
80 struct byte_stack *byte_stack;
83 struct catchtag *catchlist;
85 #ifdef DEBUG_GCPRO
86 /* Count levels of GCPRO to detect failure to UNGCPRO. */
87 int gcpro_level;
88 #endif
90 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
91 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
92 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
93 Lisp_Object Qand_rest, Qand_optional;
94 Lisp_Object Qdebug_on_error;
96 /* This holds either the symbol `run-hooks' or nil.
97 It is nil at an early stage of startup, and when Emacs
98 is shutting down. */
100 Lisp_Object Vrun_hooks;
102 /* Non-nil means record all fset's and provide's, to be undone
103 if the file being autoloaded is not fully loaded.
104 They are recorded by being consed onto the front of Vautoload_queue:
105 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
107 Lisp_Object Vautoload_queue;
109 /* Current number of specbindings allocated in specpdl. */
111 int specpdl_size;
113 /* Pointer to beginning of specpdl. */
115 struct specbinding *specpdl;
117 /* Pointer to first unused element in specpdl. */
119 struct specbinding *specpdl_ptr;
121 /* Maximum size allowed for specpdl allocation */
123 int max_specpdl_size;
125 /* Depth in Lisp evaluations and function calls. */
127 int lisp_eval_depth;
129 /* Maximum allowed depth in Lisp evaluations and function calls. */
131 int max_lisp_eval_depth;
133 /* Nonzero means enter debugger before next function call */
135 int debug_on_next_call;
137 /* Non-zero means debuffer may continue. This is zero when the
138 debugger is called during redisplay, where it might not be safe to
139 continue the interrupted redisplay. */
141 int debugger_may_continue;
143 /* List of conditions (non-nil atom means all) which cause a backtrace
144 if an error is handled by the command loop's error handler. */
146 Lisp_Object Vstack_trace_on_error;
148 /* List of conditions (non-nil atom means all) which enter the debugger
149 if an error is handled by the command loop's error handler. */
151 Lisp_Object Vdebug_on_error;
153 /* List of conditions and regexps specifying error messages which
154 do not enter the debugger even if Vdebug_on_errors says they should. */
156 Lisp_Object Vdebug_ignored_errors;
158 /* Non-nil means call the debugger even if the error will be handled. */
160 Lisp_Object Vdebug_on_signal;
162 /* Hook for edebug to use. */
164 Lisp_Object Vsignal_hook_function;
166 /* Nonzero means enter debugger if a quit signal
167 is handled by the command loop's error handler. */
169 int debug_on_quit;
171 /* The value of num_nonmacro_input_events as of the last time we
172 started to enter the debugger. If we decide to enter the debugger
173 again when this is still equal to num_nonmacro_input_events, then we
174 know that the debugger itself has an error, and we should just
175 signal the error instead of entering an infinite loop of debugger
176 invocations. */
178 int when_entered_debugger;
180 Lisp_Object Vdebugger;
182 /* The function from which the last `signal' was called. Set in
183 Fsignal. */
185 Lisp_Object Vsignaling_function;
187 /* Set to non-zero while processing X events. Checked in Feval to
188 make sure the Lisp interpreter isn't called from a signal handler,
189 which is unsafe because the interpreter isn't reentrant. */
191 int handling_signal;
193 void specbind (), record_unwind_protect ();
195 Lisp_Object run_hook_with_args ();
197 Lisp_Object funcall_lambda ();
198 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
200 void
201 init_eval_once ()
203 specpdl_size = 50;
204 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
205 specpdl_ptr = specpdl;
206 max_specpdl_size = 600;
207 max_lisp_eval_depth = 300;
209 Vrun_hooks = Qnil;
212 void
213 init_eval ()
215 specpdl_ptr = specpdl;
216 catchlist = 0;
217 handlerlist = 0;
218 backtrace_list = 0;
219 Vquit_flag = Qnil;
220 debug_on_next_call = 0;
221 lisp_eval_depth = 0;
222 #ifdef DEBUG_GCPRO
223 gcpro_level = 0;
224 #endif
225 /* This is less than the initial value of num_nonmacro_input_events. */
226 when_entered_debugger = -1;
229 Lisp_Object
230 call_debugger (arg)
231 Lisp_Object arg;
233 int debug_while_redisplaying;
234 int count = specpdl_ptr - specpdl;
235 Lisp_Object val;
237 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
238 max_lisp_eval_depth = lisp_eval_depth + 20;
240 if (specpdl_size + 40 > max_specpdl_size)
241 max_specpdl_size = specpdl_size + 40;
243 #ifdef HAVE_X_WINDOWS
244 if (display_hourglass_p)
245 cancel_hourglass ();
246 #endif
248 debug_on_next_call = 0;
249 when_entered_debugger = num_nonmacro_input_events;
251 /* Resetting redisplaying_p to 0 makes sure that debug output is
252 displayed if the debugger is invoked during redisplay. */
253 debug_while_redisplaying = redisplaying_p;
254 redisplaying_p = 0;
255 specbind (intern ("debugger-may-continue"),
256 debug_while_redisplaying ? Qnil : Qt);
257 specbind (Qinhibit_redisplay, Qnil);
259 #if 0 /* Binding this prevents execution of Lisp code during
260 redisplay, which necessarily leads to display problems. */
261 specbind (Qinhibit_eval_during_redisplay, Qt);
262 #endif
264 val = apply1 (Vdebugger, arg);
266 /* Interrupting redisplay and resuming it later is not safe under
267 all circumstances. So, when the debugger returns, abort the
268 interupted redisplay by going back to the top-level. */
269 if (debug_while_redisplaying)
270 Ftop_level ();
272 return unbind_to (count, val);
275 void
276 do_debug_on_call (code)
277 Lisp_Object code;
279 debug_on_next_call = 0;
280 backtrace_list->debug_on_exit = 1;
281 call_debugger (Fcons (code, Qnil));
284 /* NOTE!!! Every function that can call EVAL must protect its args
285 and temporaries from garbage collection while it needs them.
286 The definition of `For' shows what you have to do. */
288 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
289 "Eval args until one of them yields non-nil, then return that value.\n\
290 The remaining args are not evalled at all.\n\
291 If all args return nil, return nil.")
292 (args)
293 Lisp_Object args;
295 register Lisp_Object val;
296 Lisp_Object args_left;
297 struct gcpro gcpro1;
299 if (NILP(args))
300 return Qnil;
302 args_left = args;
303 GCPRO1 (args_left);
307 val = Feval (Fcar (args_left));
308 if (!NILP (val))
309 break;
310 args_left = Fcdr (args_left);
312 while (!NILP(args_left));
314 UNGCPRO;
315 return val;
318 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
319 "Eval args until one of them yields nil, then return nil.\n\
320 The remaining args are not evalled at all.\n\
321 If no arg yields nil, return the last arg's value.")
322 (args)
323 Lisp_Object args;
325 register Lisp_Object val;
326 Lisp_Object args_left;
327 struct gcpro gcpro1;
329 if (NILP(args))
330 return Qt;
332 args_left = args;
333 GCPRO1 (args_left);
337 val = Feval (Fcar (args_left));
338 if (NILP (val))
339 break;
340 args_left = Fcdr (args_left);
342 while (!NILP(args_left));
344 UNGCPRO;
345 return val;
348 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
349 "If COND yields non-nil, do THEN, else do ELSE...\n\
350 Returns the value of THEN or the value of the last of the ELSE's.\n\
351 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
352 If COND yields nil, and there are no ELSE's, the value is nil.")
353 (args)
354 Lisp_Object args;
356 register Lisp_Object cond;
357 struct gcpro gcpro1;
359 GCPRO1 (args);
360 cond = Feval (Fcar (args));
361 UNGCPRO;
363 if (!NILP (cond))
364 return Feval (Fcar (Fcdr (args)));
365 return Fprogn (Fcdr (Fcdr (args)));
368 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
369 "Try each clause until one succeeds.\n\
370 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
371 and, if the value is non-nil, this clause succeeds:\n\
372 then the expressions in BODY are evaluated and the last one's\n\
373 value is the value of the cond-form.\n\
374 If no clause succeeds, cond returns nil.\n\
375 If a clause has one element, as in (CONDITION),\n\
376 CONDITION's value if non-nil is returned from the cond-form.")
377 (args)
378 Lisp_Object args;
380 register Lisp_Object clause, val;
381 struct gcpro gcpro1;
383 val = Qnil;
384 GCPRO1 (args);
385 while (!NILP (args))
387 clause = Fcar (args);
388 val = Feval (Fcar (clause));
389 if (!NILP (val))
391 if (!EQ (XCDR (clause), Qnil))
392 val = Fprogn (XCDR (clause));
393 break;
395 args = XCDR (args);
397 UNGCPRO;
399 return val;
402 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
403 "Eval BODY forms sequentially and return value of last one.")
404 (args)
405 Lisp_Object args;
407 register Lisp_Object val, tem;
408 Lisp_Object args_left;
409 struct gcpro gcpro1;
411 /* In Mocklisp code, symbols at the front of the progn arglist
412 are to be bound to zero. */
413 if (!EQ (Vmocklisp_arguments, Qt))
415 val = make_number (0);
416 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
418 QUIT;
419 specbind (tem, val), args = Fcdr (args);
423 if (NILP(args))
424 return Qnil;
426 args_left = args;
427 GCPRO1 (args_left);
431 val = Feval (Fcar (args_left));
432 args_left = Fcdr (args_left);
434 while (!NILP(args_left));
436 UNGCPRO;
437 return val;
440 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
441 "Eval FIRST and BODY sequentially; value from FIRST.\n\
442 The value of FIRST 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 = 0;
452 if (NILP(args))
453 return Qnil;
455 args_left = args;
456 val = Qnil;
457 GCPRO2 (args, val);
461 if (!(argnum++))
462 val = Feval (Fcar (args_left));
463 else
464 Feval (Fcar (args_left));
465 args_left = Fcdr (args_left);
467 while (!NILP(args_left));
469 UNGCPRO;
470 return val;
473 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
474 "Eval X, Y and BODY sequentially; value from Y.\n\
475 The value of Y is saved during the evaluation of the remaining args,\n\
476 whose values are discarded.")
477 (args)
478 Lisp_Object args;
480 Lisp_Object val;
481 register Lisp_Object args_left;
482 struct gcpro gcpro1, gcpro2;
483 register int argnum = -1;
485 val = Qnil;
487 if (NILP (args))
488 return Qnil;
490 args_left = args;
491 val = Qnil;
492 GCPRO2 (args, val);
496 if (!(argnum++))
497 val = Feval (Fcar (args_left));
498 else
499 Feval (Fcar (args_left));
500 args_left = Fcdr (args_left);
502 while (!NILP (args_left));
504 UNGCPRO;
505 return val;
508 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
509 "Set each SYM to the value of its VAL.\n\
510 The symbols SYM are variables; they are literal (not evaluated).\n\
511 The values VAL are expressions; they are evaluated.\n\
512 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
513 The second VAL is not computed until after the first SYM is set, and so on;\n\
514 each VAL can use the new value of variables set earlier in the `setq'.\n\
515 The return value of the `setq' form is the value of the last VAL.")
516 (args)
517 Lisp_Object args;
519 register Lisp_Object args_left;
520 register Lisp_Object val, sym;
521 struct gcpro gcpro1;
523 if (NILP(args))
524 return Qnil;
526 args_left = args;
527 GCPRO1 (args);
531 val = Feval (Fcar (Fcdr (args_left)));
532 sym = Fcar (args_left);
533 Fset (sym, val);
534 args_left = Fcdr (Fcdr (args_left));
536 while (!NILP(args_left));
538 UNGCPRO;
539 return val;
542 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
543 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
544 (args)
545 Lisp_Object args;
547 return Fcar (args);
550 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
551 "Like `quote', but preferred for objects which are functions.\n\
552 In byte compilation, `function' causes its argument to be compiled.\n\
553 `quote' cannot do that.")
554 (args)
555 Lisp_Object args;
557 return Fcar (args);
561 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
562 "Return t if function in which this appears was called interactively.\n\
563 This means that the function was called with call-interactively (which\n\
564 includes being called as the binding of a key)\n\
565 and input is currently coming from the keyboard (not in keyboard macro).")
568 return interactive_p (1) ? Qt : Qnil;
572 /* Return 1 if function in which this appears was called
573 interactively. This means that the function was called with
574 call-interactively (which includes being called as the binding of
575 a key) and input is currently coming from the keyboard (not in
576 keyboard macro).
578 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
579 called is a built-in. */
582 interactive_p (exclude_subrs_p)
583 int exclude_subrs_p;
585 struct backtrace *btp;
586 Lisp_Object fun;
588 if (!INTERACTIVE)
589 return 0;
591 btp = backtrace_list;
593 /* If this isn't a byte-compiled function, there may be a frame at
594 the top for Finteractive_p. If so, skip it. */
595 fun = Findirect_function (*btp->function);
596 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
597 btp = btp->next;
599 /* If we're running an Emacs 18-style byte-compiled function, there
600 may be a frame for Fbytecode. Now, given the strictest
601 definition, this function isn't really being called
602 interactively, but because that's the way Emacs 18 always builds
603 byte-compiled functions, we'll accept it for now. */
604 if (EQ (*btp->function, Qbytecode))
605 btp = btp->next;
607 /* If this isn't a byte-compiled function, then we may now be
608 looking at several frames for special forms. Skip past them. */
609 while (btp &&
610 btp->nargs == UNEVALLED)
611 btp = btp->next;
613 /* btp now points at the frame of the innermost function that isn't
614 a special form, ignoring frames for Finteractive_p and/or
615 Fbytecode at the top. If this frame is for a built-in function
616 (such as load or eval-region) return nil. */
617 fun = Findirect_function (*btp->function);
618 if (exclude_subrs_p && SUBRP (fun))
619 return 0;
621 /* btp points to the frame of a Lisp function that called interactive-p.
622 Return t if that function was called interactively. */
623 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
624 return 1;
625 return 0;
629 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
630 "Define NAME as a function.\n\
631 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
632 See also the function `interactive'.")
633 (args)
634 Lisp_Object args;
636 register Lisp_Object fn_name;
637 register Lisp_Object defn;
639 fn_name = Fcar (args);
640 defn = Fcons (Qlambda, Fcdr (args));
641 if (!NILP (Vpurify_flag))
642 defn = Fpurecopy (defn);
643 Ffset (fn_name, defn);
644 LOADHIST_ATTACH (fn_name);
645 return fn_name;
648 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
649 "Define NAME as a macro.\n\
650 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
651 When the macro is called, as in (NAME ARGS...),\n\
652 the function (lambda ARGLIST BODY...) is applied to\n\
653 the list ARGS... as it appears in the expression,\n\
654 and the result should be a form to be evaluated instead of the original.")
655 (args)
656 Lisp_Object args;
658 register Lisp_Object fn_name;
659 register Lisp_Object defn;
661 fn_name = Fcar (args);
662 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
663 if (!NILP (Vpurify_flag))
664 defn = Fpurecopy (defn);
665 Ffset (fn_name, defn);
666 LOADHIST_ATTACH (fn_name);
667 return fn_name;
670 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
671 "Define SYMBOL as a variable.\n\
672 You are not required to define a variable in order to use it,\n\
673 but the definition can supply documentation and an initial value\n\
674 in a way that tags can recognize.\n\n\
675 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
676 If SYMBOL is buffer-local, its default value is what is set;\n\
677 buffer-local values are not affected.\n\
678 INITVALUE and DOCSTRING are optional.\n\
679 If DOCSTRING starts with *, this variable is identified as a user option.\n\
680 This means that M-x set-variable recognizes it.\n\
681 See also `user-variable-p'.\n\
682 If INITVALUE is missing, SYMBOL's value is not set.")
683 (args)
684 Lisp_Object args;
686 register Lisp_Object sym, tem, tail;
688 sym = Fcar (args);
689 tail = Fcdr (args);
690 if (!NILP (Fcdr (Fcdr (tail))))
691 error ("too many arguments");
693 tem = Fdefault_boundp (sym);
694 if (!NILP (tail))
696 if (NILP (tem))
697 Fset_default (sym, Feval (Fcar (tail)));
698 tail = Fcdr (tail);
699 if (!NILP (Fcar (tail)))
701 tem = Fcar (tail);
702 if (!NILP (Vpurify_flag))
703 tem = Fpurecopy (tem);
704 Fput (sym, Qvariable_documentation, tem);
706 LOADHIST_ATTACH (sym);
708 else
709 /* A (defvar <var>) should not take precedence in the load-history over
710 an earlier (defvar <var> <val>), so only add to history if the default
711 value is still unbound. */
712 if (NILP (tem))
713 LOADHIST_ATTACH (sym);
715 return sym;
718 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
719 "Define SYMBOL as a constant variable.\n\
720 The intent is that neither programs nor users should ever change this value.\n\
721 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
722 If SYMBOL is buffer-local, its default value is what is set;\n\
723 buffer-local values are not affected.\n\
724 DOCSTRING is optional.")
725 (args)
726 Lisp_Object args;
728 register Lisp_Object sym, tem;
730 sym = Fcar (args);
731 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
732 error ("too many arguments");
734 tem = Feval (Fcar (Fcdr (args)));
735 if (!NILP (Vpurify_flag))
736 tem = Fpurecopy (tem);
737 Fset_default (sym, tem);
738 tem = Fcar (Fcdr (Fcdr (args)));
739 if (!NILP (tem))
741 if (!NILP (Vpurify_flag))
742 tem = Fpurecopy (tem);
743 Fput (sym, Qvariable_documentation, tem);
745 LOADHIST_ATTACH (sym);
746 return sym;
749 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
750 "Returns t if VARIABLE is intended to be set and modified by users.\n\
751 \(The alternative is a variable used internally in a Lisp program.)\n\
752 Determined by whether the first character of the documentation\n\
753 for the variable is `*' or if the variable is customizable (has a non-nil\n\
754 value of any of `custom-type', `custom-loads' or `standard-value'\n\
755 on its property list).")
756 (variable)
757 Lisp_Object variable;
759 Lisp_Object documentation;
761 if (!SYMBOLP (variable))
762 return Qnil;
764 documentation = Fget (variable, Qvariable_documentation);
765 if (INTEGERP (documentation) && XINT (documentation) < 0)
766 return Qt;
767 if (STRINGP (documentation)
768 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
769 return Qt;
770 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
771 if (CONSP (documentation)
772 && STRINGP (XCAR (documentation))
773 && INTEGERP (XCDR (documentation))
774 && XINT (XCDR (documentation)) < 0)
775 return Qt;
776 /* Customizable? */
777 if ((!NILP (Fget (variable, intern ("custom-type"))))
778 || (!NILP (Fget (variable, intern ("custom-loads"))))
779 || (!NILP (Fget (variable, intern ("standard-value")))))
780 return Qt;
781 return Qnil;
784 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
785 "Bind variables according to VARLIST then eval BODY.\n\
786 The value of the last form in BODY is returned.\n\
787 Each element of VARLIST is a symbol (which is bound to nil)\n\
788 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
789 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
790 (args)
791 Lisp_Object args;
793 Lisp_Object varlist, val, elt;
794 int count = specpdl_ptr - specpdl;
795 struct gcpro gcpro1, gcpro2, gcpro3;
797 GCPRO3 (args, elt, varlist);
799 varlist = Fcar (args);
800 while (!NILP (varlist))
802 QUIT;
803 elt = Fcar (varlist);
804 if (SYMBOLP (elt))
805 specbind (elt, Qnil);
806 else if (! NILP (Fcdr (Fcdr (elt))))
807 Fsignal (Qerror,
808 Fcons (build_string ("`let' bindings can have only one value-form"),
809 elt));
810 else
812 val = Feval (Fcar (Fcdr (elt)));
813 specbind (Fcar (elt), val);
815 varlist = Fcdr (varlist);
817 UNGCPRO;
818 val = Fprogn (Fcdr (args));
819 return unbind_to (count, val);
822 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
823 "Bind variables according to VARLIST then eval BODY.\n\
824 The value of the last form in BODY is returned.\n\
825 Each element of VARLIST is a symbol (which is bound to nil)\n\
826 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
827 All the VALUEFORMs are evalled before any symbols are bound.")
828 (args)
829 Lisp_Object args;
831 Lisp_Object *temps, tem;
832 register Lisp_Object elt, varlist;
833 int count = specpdl_ptr - specpdl;
834 register int argnum;
835 struct gcpro gcpro1, gcpro2;
837 varlist = Fcar (args);
839 /* Make space to hold the values to give the bound variables */
840 elt = Flength (varlist);
841 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
843 /* Compute the values and store them in `temps' */
845 GCPRO2 (args, *temps);
846 gcpro2.nvars = 0;
848 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
850 QUIT;
851 elt = Fcar (varlist);
852 if (SYMBOLP (elt))
853 temps [argnum++] = Qnil;
854 else if (! NILP (Fcdr (Fcdr (elt))))
855 Fsignal (Qerror,
856 Fcons (build_string ("`let' bindings can have only one value-form"),
857 elt));
858 else
859 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
860 gcpro2.nvars = argnum;
862 UNGCPRO;
864 varlist = Fcar (args);
865 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
867 elt = Fcar (varlist);
868 tem = temps[argnum++];
869 if (SYMBOLP (elt))
870 specbind (elt, tem);
871 else
872 specbind (Fcar (elt), tem);
875 elt = Fprogn (Fcdr (args));
876 return unbind_to (count, elt);
879 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
880 "If TEST yields non-nil, eval BODY... and repeat.\n\
881 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
882 until TEST returns nil.")
883 (args)
884 Lisp_Object args;
886 Lisp_Object test, body, tem;
887 struct gcpro gcpro1, gcpro2;
889 GCPRO2 (test, body);
891 test = Fcar (args);
892 body = Fcdr (args);
893 while (tem = Feval (test),
894 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
896 QUIT;
897 Fprogn (body);
900 UNGCPRO;
901 return Qnil;
904 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
905 "Return result of expanding macros at top level of FORM.\n\
906 If FORM is not a macro call, it is returned unchanged.\n\
907 Otherwise, the macro is expanded and the expansion is considered\n\
908 in place of FORM. When a non-macro-call results, it is returned.\n\n\
909 The second optional arg ENVIRONMENT specifies an environment of macro\n\
910 definitions to shadow the loaded ones for use in file byte-compilation.")
911 (form, environment)
912 Lisp_Object form;
913 Lisp_Object environment;
915 /* With cleanups from Hallvard Furuseth. */
916 register Lisp_Object expander, sym, def, tem;
918 while (1)
920 /* Come back here each time we expand a macro call,
921 in case it expands into another macro call. */
922 if (!CONSP (form))
923 break;
924 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
925 def = sym = XCAR (form);
926 tem = Qnil;
927 /* Trace symbols aliases to other symbols
928 until we get a symbol that is not an alias. */
929 while (SYMBOLP (def))
931 QUIT;
932 sym = def;
933 tem = Fassq (sym, environment);
934 if (NILP (tem))
936 def = XSYMBOL (sym)->function;
937 if (!EQ (def, Qunbound))
938 continue;
940 break;
942 /* Right now TEM is the result from SYM in ENVIRONMENT,
943 and if TEM is nil then DEF is SYM's function definition. */
944 if (NILP (tem))
946 /* SYM is not mentioned in ENVIRONMENT.
947 Look at its function definition. */
948 if (EQ (def, Qunbound) || !CONSP (def))
949 /* Not defined or definition not suitable */
950 break;
951 if (EQ (XCAR (def), Qautoload))
953 /* Autoloading function: will it be a macro when loaded? */
954 tem = Fnth (make_number (4), def);
955 if (EQ (tem, Qt) || EQ (tem, Qmacro))
956 /* Yes, load it and try again. */
958 struct gcpro gcpro1;
959 GCPRO1 (form);
960 do_autoload (def, sym);
961 UNGCPRO;
962 continue;
964 else
965 break;
967 else if (!EQ (XCAR (def), Qmacro))
968 break;
969 else expander = XCDR (def);
971 else
973 expander = XCDR (tem);
974 if (NILP (expander))
975 break;
977 form = apply1 (expander, XCDR (form));
979 return form;
982 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
983 "Eval BODY allowing nonlocal exits using `throw'.\n\
984 TAG is evalled to get the tag to use; it must not be nil.\n\
986 Then the BODY is executed.\n\
987 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
988 If no throw happens, `catch' returns the value of the last BODY form.\n\
989 If a throw happens, it specifies the value to return from `catch'.")
990 (args)
991 Lisp_Object args;
993 register Lisp_Object tag;
994 struct gcpro gcpro1;
996 GCPRO1 (args);
997 tag = Feval (Fcar (args));
998 UNGCPRO;
999 return internal_catch (tag, Fprogn, Fcdr (args));
1002 /* Set up a catch, then call C function FUNC on argument ARG.
1003 FUNC should return a Lisp_Object.
1004 This is how catches are done from within C code. */
1006 Lisp_Object
1007 internal_catch (tag, func, arg)
1008 Lisp_Object tag;
1009 Lisp_Object (*func) ();
1010 Lisp_Object arg;
1012 /* This structure is made part of the chain `catchlist'. */
1013 struct catchtag c;
1015 /* Fill in the components of c, and put it on the list. */
1016 c.next = catchlist;
1017 c.tag = tag;
1018 c.val = Qnil;
1019 c.backlist = backtrace_list;
1020 c.handlerlist = handlerlist;
1021 c.lisp_eval_depth = lisp_eval_depth;
1022 c.pdlcount = specpdl_ptr - specpdl;
1023 c.poll_suppress_count = poll_suppress_count;
1024 c.gcpro = gcprolist;
1025 c.byte_stack = byte_stack_list;
1026 catchlist = &c;
1028 /* Call FUNC. */
1029 if (! _setjmp (c.jmp))
1030 c.val = (*func) (arg);
1032 /* Throw works by a longjmp that comes right here. */
1033 catchlist = c.next;
1034 return c.val;
1037 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1038 jump to that CATCH, returning VALUE as the value of that catch.
1040 This is the guts Fthrow and Fsignal; they differ only in the way
1041 they choose the catch tag to throw to. A catch tag for a
1042 condition-case form has a TAG of Qnil.
1044 Before each catch is discarded, unbind all special bindings and
1045 execute all unwind-protect clauses made above that catch. Unwind
1046 the handler stack as we go, so that the proper handlers are in
1047 effect for each unwind-protect clause we run. At the end, restore
1048 some static info saved in CATCH, and longjmp to the location
1049 specified in the
1051 This is used for correct unwinding in Fthrow and Fsignal. */
1053 static void
1054 unwind_to_catch (catch, value)
1055 struct catchtag *catch;
1056 Lisp_Object value;
1058 register int last_time;
1060 /* Save the value in the tag. */
1061 catch->val = value;
1063 /* Restore the polling-suppression count. */
1064 set_poll_suppress_count (catch->poll_suppress_count);
1068 last_time = catchlist == catch;
1070 /* Unwind the specpdl stack, and then restore the proper set of
1071 handlers. */
1072 unbind_to (catchlist->pdlcount, Qnil);
1073 handlerlist = catchlist->handlerlist;
1074 catchlist = catchlist->next;
1076 while (! last_time);
1078 byte_stack_list = catch->byte_stack;
1079 gcprolist = catch->gcpro;
1080 #ifdef DEBUG_GCPRO
1081 if (gcprolist != 0)
1082 gcpro_level = gcprolist->level + 1;
1083 else
1084 gcpro_level = 0;
1085 #endif
1086 backtrace_list = catch->backlist;
1087 lisp_eval_depth = catch->lisp_eval_depth;
1089 _longjmp (catch->jmp, 1);
1092 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1093 "Throw to the catch for TAG and return VALUE from it.\n\
1094 Both TAG and VALUE are evalled.")
1095 (tag, value)
1096 register Lisp_Object tag, value;
1098 register struct catchtag *c;
1100 while (1)
1102 if (!NILP (tag))
1103 for (c = catchlist; c; c = c->next)
1105 if (EQ (c->tag, tag))
1106 unwind_to_catch (c, value);
1108 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1113 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1114 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1115 If BODYFORM completes normally, its value is returned\n\
1116 after executing the UNWINDFORMS.\n\
1117 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1118 (args)
1119 Lisp_Object args;
1121 Lisp_Object val;
1122 int count = specpdl_ptr - specpdl;
1124 record_unwind_protect (0, Fcdr (args));
1125 val = Feval (Fcar (args));
1126 return unbind_to (count, val);
1129 /* Chain of condition handlers currently in effect.
1130 The elements of this chain are contained in the stack frames
1131 of Fcondition_case and internal_condition_case.
1132 When an error is signaled (by calling Fsignal, below),
1133 this chain is searched for an element that applies. */
1135 struct handler *handlerlist;
1137 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1138 "Regain control when an error is signaled.\n\
1139 executes BODYFORM and returns its value if no error happens.\n\
1140 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1141 where the BODY is made of Lisp expressions.\n\n\
1142 A handler is applicable to an error\n\
1143 if CONDITION-NAME is one of the error's condition names.\n\
1144 If an error happens, the first applicable handler is run.\n\
1146 The car of a handler may be a list of condition names\n\
1147 instead of a single condition name.\n\
1149 When a handler handles an error,\n\
1150 control returns to the condition-case and the handler BODY... is executed\n\
1151 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1152 VAR may be nil; then you do not get access to the signal information.\n\
1154 The value of the last BODY form is returned from the condition-case.\n\
1155 See also the function `signal' for more info.")
1156 (args)
1157 Lisp_Object args;
1159 Lisp_Object val;
1160 struct catchtag c;
1161 struct handler h;
1162 register Lisp_Object bodyform, handlers;
1163 volatile Lisp_Object var;
1165 var = Fcar (args);
1166 bodyform = Fcar (Fcdr (args));
1167 handlers = Fcdr (Fcdr (args));
1168 CHECK_SYMBOL (var, 0);
1170 for (val = handlers; ! NILP (val); val = Fcdr (val))
1172 Lisp_Object tem;
1173 tem = Fcar (val);
1174 if (! (NILP (tem)
1175 || (CONSP (tem)
1176 && (SYMBOLP (XCAR (tem))
1177 || CONSP (XCAR (tem))))))
1178 error ("Invalid condition handler", tem);
1181 c.tag = Qnil;
1182 c.val = Qnil;
1183 c.backlist = backtrace_list;
1184 c.handlerlist = handlerlist;
1185 c.lisp_eval_depth = lisp_eval_depth;
1186 c.pdlcount = specpdl_ptr - specpdl;
1187 c.poll_suppress_count = poll_suppress_count;
1188 c.gcpro = gcprolist;
1189 c.byte_stack = byte_stack_list;
1190 if (_setjmp (c.jmp))
1192 if (!NILP (h.var))
1193 specbind (h.var, c.val);
1194 val = Fprogn (Fcdr (h.chosen_clause));
1196 /* Note that this just undoes the binding of h.var; whoever
1197 longjumped to us unwound the stack to c.pdlcount before
1198 throwing. */
1199 unbind_to (c.pdlcount, Qnil);
1200 return val;
1202 c.next = catchlist;
1203 catchlist = &c;
1205 h.var = var;
1206 h.handler = handlers;
1207 h.next = handlerlist;
1208 h.tag = &c;
1209 handlerlist = &h;
1211 val = Feval (bodyform);
1212 catchlist = c.next;
1213 handlerlist = h.next;
1214 return val;
1217 /* Call the function BFUN with no arguments, catching errors within it
1218 according to HANDLERS. If there is an error, call HFUN with
1219 one argument which is the data that describes the error:
1220 (SIGNALNAME . DATA)
1222 HANDLERS can be a list of conditions to catch.
1223 If HANDLERS is Qt, catch all errors.
1224 If HANDLERS is Qerror, catch all errors
1225 but allow the debugger to run if that is enabled. */
1227 Lisp_Object
1228 internal_condition_case (bfun, handlers, hfun)
1229 Lisp_Object (*bfun) ();
1230 Lisp_Object handlers;
1231 Lisp_Object (*hfun) ();
1233 Lisp_Object val;
1234 struct catchtag c;
1235 struct handler h;
1237 #if 0 /* Can't do this check anymore because realize_basic_faces has
1238 to BLOCK_INPUT, and can call Lisp. What's really needed is a
1239 flag indicating that we're currently handling a signal. */
1240 /* Since Fsignal resets this to 0, it had better be 0 now
1241 or else we have a potential bug. */
1242 if (interrupt_input_blocked != 0)
1243 abort ();
1244 #endif
1246 c.tag = Qnil;
1247 c.val = Qnil;
1248 c.backlist = backtrace_list;
1249 c.handlerlist = handlerlist;
1250 c.lisp_eval_depth = lisp_eval_depth;
1251 c.pdlcount = specpdl_ptr - specpdl;
1252 c.poll_suppress_count = poll_suppress_count;
1253 c.gcpro = gcprolist;
1254 c.byte_stack = byte_stack_list;
1255 if (_setjmp (c.jmp))
1257 return (*hfun) (c.val);
1259 c.next = catchlist;
1260 catchlist = &c;
1261 h.handler = handlers;
1262 h.var = Qnil;
1263 h.next = handlerlist;
1264 h.tag = &c;
1265 handlerlist = &h;
1267 val = (*bfun) ();
1268 catchlist = c.next;
1269 handlerlist = h.next;
1270 return val;
1273 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1275 Lisp_Object
1276 internal_condition_case_1 (bfun, arg, handlers, hfun)
1277 Lisp_Object (*bfun) ();
1278 Lisp_Object arg;
1279 Lisp_Object handlers;
1280 Lisp_Object (*hfun) ();
1282 Lisp_Object val;
1283 struct catchtag c;
1284 struct handler h;
1286 c.tag = Qnil;
1287 c.val = Qnil;
1288 c.backlist = backtrace_list;
1289 c.handlerlist = handlerlist;
1290 c.lisp_eval_depth = lisp_eval_depth;
1291 c.pdlcount = specpdl_ptr - specpdl;
1292 c.poll_suppress_count = poll_suppress_count;
1293 c.gcpro = gcprolist;
1294 c.byte_stack = byte_stack_list;
1295 if (_setjmp (c.jmp))
1297 return (*hfun) (c.val);
1299 c.next = catchlist;
1300 catchlist = &c;
1301 h.handler = handlers;
1302 h.var = Qnil;
1303 h.next = handlerlist;
1304 h.tag = &c;
1305 handlerlist = &h;
1307 val = (*bfun) (arg);
1308 catchlist = c.next;
1309 handlerlist = h.next;
1310 return val;
1314 /* Like internal_condition_case but call HFUN with NARGS as first,
1315 and ARGS as second argument. */
1317 Lisp_Object
1318 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1319 Lisp_Object (*bfun) ();
1320 int nargs;
1321 Lisp_Object *args;
1322 Lisp_Object handlers;
1323 Lisp_Object (*hfun) ();
1325 Lisp_Object val;
1326 struct catchtag c;
1327 struct handler h;
1329 c.tag = Qnil;
1330 c.val = Qnil;
1331 c.backlist = backtrace_list;
1332 c.handlerlist = handlerlist;
1333 c.lisp_eval_depth = lisp_eval_depth;
1334 c.pdlcount = specpdl_ptr - specpdl;
1335 c.poll_suppress_count = poll_suppress_count;
1336 c.gcpro = gcprolist;
1337 c.byte_stack = byte_stack_list;
1338 if (_setjmp (c.jmp))
1340 return (*hfun) (c.val);
1342 c.next = catchlist;
1343 catchlist = &c;
1344 h.handler = handlers;
1345 h.var = Qnil;
1346 h.next = handlerlist;
1347 h.tag = &c;
1348 handlerlist = &h;
1350 val = (*bfun) (nargs, args);
1351 catchlist = c.next;
1352 handlerlist = h.next;
1353 return val;
1357 static Lisp_Object find_handler_clause ();
1359 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1360 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1361 This function does not return.\n\n\
1362 An error symbol is a symbol with an `error-conditions' property\n\
1363 that is a list of condition names.\n\
1364 A handler for any of those names will get to handle this signal.\n\
1365 The symbol `error' should normally be one of them.\n\
1367 DATA should be a list. Its elements are printed as part of the error message.\n\
1368 If the signal is handled, DATA is made available to the handler.\n\
1369 See also the function `condition-case'.")
1370 (error_symbol, data)
1371 Lisp_Object error_symbol, data;
1373 /* When memory is full, ERROR-SYMBOL is nil,
1374 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1375 register struct handler *allhandlers = handlerlist;
1376 Lisp_Object conditions;
1377 extern int gc_in_progress;
1378 extern int waiting_for_input;
1379 Lisp_Object debugger_value;
1380 Lisp_Object string;
1381 Lisp_Object real_error_symbol;
1382 extern int display_hourglass_p;
1383 struct backtrace *bp;
1385 immediate_quit = handling_signal = 0;
1386 if (gc_in_progress || waiting_for_input)
1387 abort ();
1389 TOTALLY_UNBLOCK_INPUT;
1391 if (NILP (error_symbol))
1392 real_error_symbol = Fcar (data);
1393 else
1394 real_error_symbol = error_symbol;
1396 #ifdef HAVE_X_WINDOWS
1397 if (display_hourglass_p)
1398 cancel_hourglass ();
1399 #endif
1401 /* This hook is used by edebug. */
1402 if (! NILP (Vsignal_hook_function))
1403 call2 (Vsignal_hook_function, error_symbol, data);
1405 conditions = Fget (real_error_symbol, Qerror_conditions);
1407 /* Remember from where signal was called. Skip over the frame for
1408 `signal' itself. If a frame for `error' follows, skip that,
1409 too. */
1410 Vsignaling_function = Qnil;
1411 if (backtrace_list)
1413 bp = backtrace_list->next;
1414 if (bp && bp->function && EQ (*bp->function, Qerror))
1415 bp = bp->next;
1416 if (bp && bp->function)
1417 Vsignaling_function = *bp->function;
1420 for (; handlerlist; handlerlist = handlerlist->next)
1422 register Lisp_Object clause;
1424 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1425 max_lisp_eval_depth = lisp_eval_depth + 20;
1427 if (specpdl_size + 40 > max_specpdl_size)
1428 max_specpdl_size = specpdl_size + 40;
1430 clause = find_handler_clause (handlerlist->handler, conditions,
1431 error_symbol, data, &debugger_value);
1433 #if 0 /* Most callers are not prepared to handle gc if this returns.
1434 So, since this feature is not very useful, take it out. */
1435 /* If have called debugger and user wants to continue,
1436 just return nil. */
1437 if (EQ (clause, Qlambda))
1438 return debugger_value;
1439 #else
1440 if (EQ (clause, Qlambda))
1442 /* We can't return values to code which signaled an error, but we
1443 can continue code which has signaled a quit. */
1444 if (EQ (real_error_symbol, Qquit))
1445 return Qnil;
1446 else
1447 error ("Cannot return from the debugger in an error");
1449 #endif
1451 if (!NILP (clause))
1453 Lisp_Object unwind_data;
1454 struct handler *h = handlerlist;
1456 handlerlist = allhandlers;
1458 if (NILP (error_symbol))
1459 unwind_data = data;
1460 else
1461 unwind_data = Fcons (error_symbol, data);
1462 h->chosen_clause = clause;
1463 unwind_to_catch (h->tag, unwind_data);
1467 handlerlist = allhandlers;
1468 /* If no handler is present now, try to run the debugger,
1469 and if that fails, throw to top level. */
1470 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1471 if (catchlist != 0)
1472 Fthrow (Qtop_level, Qt);
1474 if (! NILP (error_symbol))
1475 data = Fcons (error_symbol, data);
1477 string = Ferror_message_string (data);
1478 fatal ("%s", XSTRING (string)->data, 0);
1481 /* Return nonzero iff LIST is a non-nil atom or
1482 a list containing one of CONDITIONS. */
1484 static int
1485 wants_debugger (list, conditions)
1486 Lisp_Object list, conditions;
1488 if (NILP (list))
1489 return 0;
1490 if (! CONSP (list))
1491 return 1;
1493 while (CONSP (conditions))
1495 Lisp_Object this, tail;
1496 this = XCAR (conditions);
1497 for (tail = list; CONSP (tail); tail = XCDR (tail))
1498 if (EQ (XCAR (tail), this))
1499 return 1;
1500 conditions = XCDR (conditions);
1502 return 0;
1505 /* Return 1 if an error with condition-symbols CONDITIONS,
1506 and described by SIGNAL-DATA, should skip the debugger
1507 according to debugger-ignore-errors. */
1509 static int
1510 skip_debugger (conditions, data)
1511 Lisp_Object conditions, data;
1513 Lisp_Object tail;
1514 int first_string = 1;
1515 Lisp_Object error_message;
1517 error_message = Qnil;
1518 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1520 if (STRINGP (XCAR (tail)))
1522 if (first_string)
1524 error_message = Ferror_message_string (data);
1525 first_string = 0;
1528 if (fast_string_match (XCAR (tail), error_message) >= 0)
1529 return 1;
1531 else
1533 Lisp_Object contail;
1535 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1536 if (EQ (XCAR (tail), XCAR (contail)))
1537 return 1;
1541 return 0;
1544 /* Value of Qlambda means we have called debugger and user has continued.
1545 There are two ways to pass SIG and DATA:
1546 = SIG is the error symbol, and DATA is the rest of the data.
1547 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1548 This is for memory-full errors only.
1550 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1552 static Lisp_Object
1553 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1554 Lisp_Object handlers, conditions, sig, data;
1555 Lisp_Object *debugger_value_ptr;
1557 register Lisp_Object h;
1558 register Lisp_Object tem;
1560 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1561 return Qt;
1562 /* error is used similarly, but means print an error message
1563 and run the debugger if that is enabled. */
1564 if (EQ (handlers, Qerror)
1565 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1566 there is a handler. */
1568 int count = specpdl_ptr - specpdl;
1569 int debugger_called = 0;
1570 Lisp_Object sig_symbol, combined_data;
1571 /* This is set to 1 if we are handling a memory-full error,
1572 because these must not run the debugger.
1573 (There is no room in memory to do that!) */
1574 int no_debugger = 0;
1576 if (NILP (sig))
1578 combined_data = data;
1579 sig_symbol = Fcar (data);
1580 no_debugger = 1;
1582 else
1584 combined_data = Fcons (sig, data);
1585 sig_symbol = sig;
1588 if (wants_debugger (Vstack_trace_on_error, conditions))
1590 #ifdef PROTOTYPES
1591 internal_with_output_to_temp_buffer ("*Backtrace*",
1592 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1593 Qnil);
1594 #else
1595 internal_with_output_to_temp_buffer ("*Backtrace*",
1596 Fbacktrace, Qnil);
1597 #endif
1599 if (! no_debugger
1600 && (EQ (sig_symbol, Qquit)
1601 ? debug_on_quit
1602 : wants_debugger (Vdebug_on_error, conditions))
1603 && ! skip_debugger (conditions, combined_data)
1604 && when_entered_debugger < num_nonmacro_input_events)
1606 specbind (Qdebug_on_error, Qnil);
1607 *debugger_value_ptr
1608 = call_debugger (Fcons (Qerror,
1609 Fcons (combined_data, Qnil)));
1610 debugger_called = 1;
1612 /* If there is no handler, return saying whether we ran the debugger. */
1613 if (EQ (handlers, Qerror))
1615 if (debugger_called)
1616 return unbind_to (count, Qlambda);
1617 return Qt;
1620 for (h = handlers; CONSP (h); h = Fcdr (h))
1622 Lisp_Object handler, condit;
1624 handler = Fcar (h);
1625 if (!CONSP (handler))
1626 continue;
1627 condit = Fcar (handler);
1628 /* Handle a single condition name in handler HANDLER. */
1629 if (SYMBOLP (condit))
1631 tem = Fmemq (Fcar (handler), conditions);
1632 if (!NILP (tem))
1633 return handler;
1635 /* Handle a list of condition names in handler HANDLER. */
1636 else if (CONSP (condit))
1638 while (CONSP (condit))
1640 tem = Fmemq (Fcar (condit), conditions);
1641 if (!NILP (tem))
1642 return handler;
1643 condit = XCDR (condit);
1647 return Qnil;
1650 /* dump an error message; called like printf */
1652 /* VARARGS 1 */
1653 void
1654 error (m, a1, a2, a3)
1655 char *m;
1656 char *a1, *a2, *a3;
1658 char buf[200];
1659 int size = 200;
1660 int mlen;
1661 char *buffer = buf;
1662 char *args[3];
1663 int allocated = 0;
1664 Lisp_Object string;
1666 args[0] = a1;
1667 args[1] = a2;
1668 args[2] = a3;
1670 mlen = strlen (m);
1672 while (1)
1674 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1675 if (used < size)
1676 break;
1677 size *= 2;
1678 if (allocated)
1679 buffer = (char *) xrealloc (buffer, size);
1680 else
1682 buffer = (char *) xmalloc (size);
1683 allocated = 1;
1687 string = build_string (buffer);
1688 if (allocated)
1689 xfree (buffer);
1691 Fsignal (Qerror, Fcons (string, Qnil));
1692 abort ();
1695 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1696 "T if FUNCTION makes provisions for interactive calling.\n\
1697 This means it contains a description for how to read arguments to give it.\n\
1698 The value is nil for an invalid function or a symbol with no function\n\
1699 definition.\n\
1701 Interactively callable functions include strings and vectors (treated\n\
1702 as keyboard macros), lambda-expressions that contain a top-level call\n\
1703 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1704 fourth argument, and some of the built-in functions of Lisp.\n\
1706 Also, a symbol satisfies `commandp' if its function definition does so.")
1707 (function)
1708 Lisp_Object function;
1710 register Lisp_Object fun;
1711 register Lisp_Object funcar;
1713 fun = function;
1715 fun = indirect_function (fun);
1716 if (EQ (fun, Qunbound))
1717 return Qnil;
1719 /* Emacs primitives are interactive if their DEFUN specifies an
1720 interactive spec. */
1721 if (SUBRP (fun))
1723 if (XSUBR (fun)->prompt)
1724 return Qt;
1725 else
1726 return Qnil;
1729 /* Bytecode objects are interactive if they are long enough to
1730 have an element whose index is COMPILED_INTERACTIVE, which is
1731 where the interactive spec is stored. */
1732 else if (COMPILEDP (fun))
1733 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1734 ? Qt : Qnil);
1736 /* Strings and vectors are keyboard macros. */
1737 if (STRINGP (fun) || VECTORP (fun))
1738 return Qt;
1740 /* Lists may represent commands. */
1741 if (!CONSP (fun))
1742 return Qnil;
1743 funcar = Fcar (fun);
1744 if (!SYMBOLP (funcar))
1745 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1746 if (EQ (funcar, Qlambda))
1747 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1748 if (EQ (funcar, Qmocklisp))
1749 return Qt; /* All mocklisp functions can be called interactively */
1750 if (EQ (funcar, Qautoload))
1751 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1752 else
1753 return Qnil;
1756 /* ARGSUSED */
1757 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1758 "Define FUNCTION to autoload from FILE.\n\
1759 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1760 Third arg DOCSTRING is documentation for the function.\n\
1761 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1762 Fifth arg TYPE indicates the type of the object:\n\
1763 nil or omitted says FUNCTION is a function,\n\
1764 `keymap' says FUNCTION is really a keymap, and\n\
1765 `macro' or t says FUNCTION is really a macro.\n\
1766 Third through fifth args give info about the real definition.\n\
1767 They default to nil.\n\
1768 If FUNCTION is already defined other than as an autoload,\n\
1769 this does nothing and returns nil.")
1770 (function, file, docstring, interactive, type)
1771 Lisp_Object function, file, docstring, interactive, type;
1773 #ifdef NO_ARG_ARRAY
1774 Lisp_Object args[4];
1775 #endif
1777 CHECK_SYMBOL (function, 0);
1778 CHECK_STRING (file, 1);
1780 /* If function is defined and not as an autoload, don't override */
1781 if (!EQ (XSYMBOL (function)->function, Qunbound)
1782 && !(CONSP (XSYMBOL (function)->function)
1783 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1784 return Qnil;
1786 if (NILP (Vpurify_flag))
1787 /* Only add entries after dumping, because the ones before are
1788 not useful and else we get loads of them from the loaddefs.el. */
1789 LOADHIST_ATTACH (Fcons (Qautoload, function));
1791 #ifdef NO_ARG_ARRAY
1792 args[0] = file;
1793 args[1] = docstring;
1794 args[2] = interactive;
1795 args[3] = type;
1797 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1798 #else /* NO_ARG_ARRAY */
1799 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1800 #endif /* not NO_ARG_ARRAY */
1803 Lisp_Object
1804 un_autoload (oldqueue)
1805 Lisp_Object oldqueue;
1807 register Lisp_Object queue, first, second;
1809 /* Queue to unwind is current value of Vautoload_queue.
1810 oldqueue is the shadowed value to leave in Vautoload_queue. */
1811 queue = Vautoload_queue;
1812 Vautoload_queue = oldqueue;
1813 while (CONSP (queue))
1815 first = Fcar (queue);
1816 second = Fcdr (first);
1817 first = Fcar (first);
1818 if (EQ (second, Qnil))
1819 Vfeatures = first;
1820 else
1821 Ffset (first, second);
1822 queue = Fcdr (queue);
1824 return Qnil;
1827 /* Load an autoloaded function.
1828 FUNNAME is the symbol which is the function's name.
1829 FUNDEF is the autoload definition (a list). */
1831 void
1832 do_autoload (fundef, funname)
1833 Lisp_Object fundef, funname;
1835 int count = specpdl_ptr - specpdl;
1836 Lisp_Object fun, queue, first, second;
1837 struct gcpro gcpro1, gcpro2, gcpro3;
1839 fun = funname;
1840 CHECK_SYMBOL (funname, 0);
1841 GCPRO3 (fun, funname, fundef);
1843 /* Preserve the match data. */
1844 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1846 /* Value saved here is to be restored into Vautoload_queue. */
1847 record_unwind_protect (un_autoload, Vautoload_queue);
1848 Vautoload_queue = Qt;
1849 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1851 /* Save the old autoloads, in case we ever do an unload. */
1852 queue = Vautoload_queue;
1853 while (CONSP (queue))
1855 first = Fcar (queue);
1856 second = Fcdr (first);
1857 first = Fcar (first);
1859 /* Note: This test is subtle. The cdr of an autoload-queue entry
1860 may be an atom if the autoload entry was generated by a defalias
1861 or fset. */
1862 if (CONSP (second))
1863 Fput (first, Qautoload, (Fcdr (second)));
1865 queue = Fcdr (queue);
1868 /* Once loading finishes, don't undo it. */
1869 Vautoload_queue = Qt;
1870 unbind_to (count, Qnil);
1872 fun = Findirect_function (fun);
1874 if (!NILP (Fequal (fun, fundef)))
1875 error ("Autoloading failed to define function %s",
1876 XSYMBOL (funname)->name->data);
1877 UNGCPRO;
1881 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1882 "Evaluate FORM and return its value.")
1883 (form)
1884 Lisp_Object form;
1886 Lisp_Object fun, val, original_fun, original_args;
1887 Lisp_Object funcar;
1888 struct backtrace backtrace;
1889 struct gcpro gcpro1, gcpro2, gcpro3;
1891 if (handling_signal)
1892 abort ();
1894 if (SYMBOLP (form))
1896 if (EQ (Vmocklisp_arguments, Qt))
1897 return Fsymbol_value (form);
1898 val = Fsymbol_value (form);
1899 if (NILP (val))
1900 XSETFASTINT (val, 0);
1901 else if (EQ (val, Qt))
1902 XSETFASTINT (val, 1);
1903 return val;
1905 if (!CONSP (form))
1906 return form;
1908 QUIT;
1909 if (consing_since_gc > gc_cons_threshold)
1911 GCPRO1 (form);
1912 Fgarbage_collect ();
1913 UNGCPRO;
1916 if (++lisp_eval_depth > max_lisp_eval_depth)
1918 if (max_lisp_eval_depth < 100)
1919 max_lisp_eval_depth = 100;
1920 if (lisp_eval_depth > max_lisp_eval_depth)
1921 error ("Lisp nesting exceeds max-lisp-eval-depth");
1924 original_fun = Fcar (form);
1925 original_args = Fcdr (form);
1927 backtrace.next = backtrace_list;
1928 backtrace_list = &backtrace;
1929 backtrace.function = &original_fun; /* This also protects them from gc */
1930 backtrace.args = &original_args;
1931 backtrace.nargs = UNEVALLED;
1932 backtrace.evalargs = 1;
1933 backtrace.debug_on_exit = 0;
1935 if (debug_on_next_call)
1936 do_debug_on_call (Qt);
1938 /* At this point, only original_fun and original_args
1939 have values that will be used below */
1940 retry:
1941 fun = Findirect_function (original_fun);
1943 if (SUBRP (fun))
1945 Lisp_Object numargs;
1946 Lisp_Object argvals[8];
1947 Lisp_Object args_left;
1948 register int i, maxargs;
1950 args_left = original_args;
1951 numargs = Flength (args_left);
1953 if (XINT (numargs) < XSUBR (fun)->min_args ||
1954 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1955 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1957 if (XSUBR (fun)->max_args == UNEVALLED)
1959 backtrace.evalargs = 0;
1960 val = (*XSUBR (fun)->function) (args_left);
1961 goto done;
1964 if (XSUBR (fun)->max_args == MANY)
1966 /* Pass a vector of evaluated arguments */
1967 Lisp_Object *vals;
1968 register int argnum = 0;
1970 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1972 GCPRO3 (args_left, fun, fun);
1973 gcpro3.var = vals;
1974 gcpro3.nvars = 0;
1976 while (!NILP (args_left))
1978 vals[argnum++] = Feval (Fcar (args_left));
1979 args_left = Fcdr (args_left);
1980 gcpro3.nvars = argnum;
1983 backtrace.args = vals;
1984 backtrace.nargs = XINT (numargs);
1986 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1987 UNGCPRO;
1988 goto done;
1991 GCPRO3 (args_left, fun, fun);
1992 gcpro3.var = argvals;
1993 gcpro3.nvars = 0;
1995 maxargs = XSUBR (fun)->max_args;
1996 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1998 argvals[i] = Feval (Fcar (args_left));
1999 gcpro3.nvars = ++i;
2002 UNGCPRO;
2004 backtrace.args = argvals;
2005 backtrace.nargs = XINT (numargs);
2007 switch (i)
2009 case 0:
2010 val = (*XSUBR (fun)->function) ();
2011 goto done;
2012 case 1:
2013 val = (*XSUBR (fun)->function) (argvals[0]);
2014 goto done;
2015 case 2:
2016 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2017 goto done;
2018 case 3:
2019 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2020 argvals[2]);
2021 goto done;
2022 case 4:
2023 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2024 argvals[2], argvals[3]);
2025 goto done;
2026 case 5:
2027 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2028 argvals[3], argvals[4]);
2029 goto done;
2030 case 6:
2031 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2032 argvals[3], argvals[4], argvals[5]);
2033 goto done;
2034 case 7:
2035 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2036 argvals[3], argvals[4], argvals[5],
2037 argvals[6]);
2038 goto done;
2040 case 8:
2041 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2042 argvals[3], argvals[4], argvals[5],
2043 argvals[6], argvals[7]);
2044 goto done;
2046 default:
2047 /* Someone has created a subr that takes more arguments than
2048 is supported by this code. We need to either rewrite the
2049 subr to use a different argument protocol, or add more
2050 cases to this switch. */
2051 abort ();
2054 if (COMPILEDP (fun))
2055 val = apply_lambda (fun, original_args, 1);
2056 else
2058 if (!CONSP (fun))
2059 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2060 funcar = Fcar (fun);
2061 if (!SYMBOLP (funcar))
2062 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2063 if (EQ (funcar, Qautoload))
2065 do_autoload (fun, original_fun);
2066 goto retry;
2068 if (EQ (funcar, Qmacro))
2069 val = Feval (apply1 (Fcdr (fun), original_args));
2070 else if (EQ (funcar, Qlambda))
2071 val = apply_lambda (fun, original_args, 1);
2072 else if (EQ (funcar, Qmocklisp))
2073 val = ml_apply (fun, original_args);
2074 else
2075 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2077 done:
2078 if (!EQ (Vmocklisp_arguments, Qt))
2080 if (NILP (val))
2081 XSETFASTINT (val, 0);
2082 else if (EQ (val, Qt))
2083 XSETFASTINT (val, 1);
2085 lisp_eval_depth--;
2086 if (backtrace.debug_on_exit)
2087 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2088 backtrace_list = backtrace.next;
2089 return val;
2092 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2093 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
2094 Then return the value FUNCTION returns.\n\
2095 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
2096 (nargs, args)
2097 int nargs;
2098 Lisp_Object *args;
2100 register int i, numargs;
2101 register Lisp_Object spread_arg;
2102 register Lisp_Object *funcall_args;
2103 Lisp_Object fun;
2104 struct gcpro gcpro1;
2106 fun = args [0];
2107 funcall_args = 0;
2108 spread_arg = args [nargs - 1];
2109 CHECK_LIST (spread_arg, nargs);
2111 numargs = XINT (Flength (spread_arg));
2113 if (numargs == 0)
2114 return Ffuncall (nargs - 1, args);
2115 else if (numargs == 1)
2117 args [nargs - 1] = XCAR (spread_arg);
2118 return Ffuncall (nargs, args);
2121 numargs += nargs - 2;
2123 fun = indirect_function (fun);
2124 if (EQ (fun, Qunbound))
2126 /* Let funcall get the error */
2127 fun = args[0];
2128 goto funcall;
2131 if (SUBRP (fun))
2133 if (numargs < XSUBR (fun)->min_args
2134 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2135 goto funcall; /* Let funcall get the error */
2136 else if (XSUBR (fun)->max_args > numargs)
2138 /* Avoid making funcall cons up a yet another new vector of arguments
2139 by explicitly supplying nil's for optional values */
2140 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2141 * sizeof (Lisp_Object));
2142 for (i = numargs; i < XSUBR (fun)->max_args;)
2143 funcall_args[++i] = Qnil;
2144 GCPRO1 (*funcall_args);
2145 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2148 funcall:
2149 /* We add 1 to numargs because funcall_args includes the
2150 function itself as well as its arguments. */
2151 if (!funcall_args)
2153 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2154 * sizeof (Lisp_Object));
2155 GCPRO1 (*funcall_args);
2156 gcpro1.nvars = 1 + numargs;
2159 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2160 /* Spread the last arg we got. Its first element goes in
2161 the slot that it used to occupy, hence this value of I. */
2162 i = nargs - 1;
2163 while (!NILP (spread_arg))
2165 funcall_args [i++] = XCAR (spread_arg);
2166 spread_arg = XCDR (spread_arg);
2169 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2172 /* Run hook variables in various ways. */
2174 enum run_hooks_condition {to_completion, until_success, until_failure};
2176 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2177 "Run each hook in HOOKS. Major mode functions use this.\n\
2178 Each argument should be a symbol, a hook variable.\n\
2179 These symbols are processed in the order specified.\n\
2180 If a hook symbol has a non-nil value, that value may be a function\n\
2181 or a list of functions to be called to run the hook.\n\
2182 If the value is a function, it is called with no arguments.\n\
2183 If it is a list, the elements are called, in order, with no arguments.\n\
2185 To make a hook variable buffer-local, use `make-local-hook',\n\
2186 not `make-local-variable'.")
2187 (nargs, args)
2188 int nargs;
2189 Lisp_Object *args;
2191 Lisp_Object hook[1];
2192 register int i;
2194 for (i = 0; i < nargs; i++)
2196 hook[0] = args[i];
2197 run_hook_with_args (1, hook, to_completion);
2200 return Qnil;
2203 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2204 Srun_hook_with_args, 1, MANY, 0,
2205 "Run HOOK with the specified arguments ARGS.\n\
2206 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2207 value, that value may be a function or a list of functions to be\n\
2208 called to run the hook. If the value is a function, it is called with\n\
2209 the given arguments and its return value is returned. If it is a list\n\
2210 of functions, those functions are called, in order,\n\
2211 with the given arguments ARGS.\n\
2212 It is best not to depend on the value return by `run-hook-with-args',\n\
2213 as that may change.\n\
2215 To make a hook variable buffer-local, use `make-local-hook',\n\
2216 not `make-local-variable'.")
2217 (nargs, args)
2218 int nargs;
2219 Lisp_Object *args;
2221 return run_hook_with_args (nargs, args, to_completion);
2224 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2225 Srun_hook_with_args_until_success, 1, MANY, 0,
2226 "Run HOOK with the specified arguments ARGS.\n\
2227 HOOK should be a symbol, a hook variable. Its value should\n\
2228 be a list of functions. We call those functions, one by one,\n\
2229 passing arguments ARGS to each of them, until one of them\n\
2230 returns a non-nil value. Then we return that value.\n\
2231 If all the functions return nil, we return nil.\n\
2233 To make a hook variable buffer-local, use `make-local-hook',\n\
2234 not `make-local-variable'.")
2235 (nargs, args)
2236 int nargs;
2237 Lisp_Object *args;
2239 return run_hook_with_args (nargs, args, until_success);
2242 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2243 Srun_hook_with_args_until_failure, 1, MANY, 0,
2244 "Run HOOK with the specified arguments ARGS.\n\
2245 HOOK should be a symbol, a hook variable. Its value should\n\
2246 be a list of functions. We call those functions, one by one,\n\
2247 passing arguments ARGS to each of them, until one of them\n\
2248 returns nil. Then we return nil.\n\
2249 If all the functions return non-nil, we return non-nil.\n\
2251 To make a hook variable buffer-local, use `make-local-hook',\n\
2252 not `make-local-variable'.")
2253 (nargs, args)
2254 int nargs;
2255 Lisp_Object *args;
2257 return run_hook_with_args (nargs, args, until_failure);
2260 /* ARGS[0] should be a hook symbol.
2261 Call each of the functions in the hook value, passing each of them
2262 as arguments all the rest of ARGS (all NARGS - 1 elements).
2263 COND specifies a condition to test after each call
2264 to decide whether to stop.
2265 The caller (or its caller, etc) must gcpro all of ARGS,
2266 except that it isn't necessary to gcpro ARGS[0]. */
2268 Lisp_Object
2269 run_hook_with_args (nargs, args, cond)
2270 int nargs;
2271 Lisp_Object *args;
2272 enum run_hooks_condition cond;
2274 Lisp_Object sym, val, ret;
2275 Lisp_Object globals;
2276 struct gcpro gcpro1, gcpro2, gcpro3;
2278 /* If we are dying or still initializing,
2279 don't do anything--it would probably crash if we tried. */
2280 if (NILP (Vrun_hooks))
2281 return Qnil;
2283 sym = args[0];
2284 val = find_symbol_value (sym);
2285 ret = (cond == until_failure ? Qt : Qnil);
2287 if (EQ (val, Qunbound) || NILP (val))
2288 return ret;
2289 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2291 args[0] = val;
2292 return Ffuncall (nargs, args);
2294 else
2296 globals = Qnil;
2297 GCPRO3 (sym, val, globals);
2299 for (;
2300 CONSP (val) && ((cond == to_completion)
2301 || (cond == until_success ? NILP (ret)
2302 : !NILP (ret)));
2303 val = XCDR (val))
2305 if (EQ (XCAR (val), Qt))
2307 /* t indicates this hook has a local binding;
2308 it means to run the global binding too. */
2310 for (globals = Fdefault_value (sym);
2311 CONSP (globals) && ((cond == to_completion)
2312 || (cond == until_success ? NILP (ret)
2313 : !NILP (ret)));
2314 globals = XCDR (globals))
2316 args[0] = XCAR (globals);
2317 /* In a global value, t should not occur. If it does, we
2318 must ignore it to avoid an endless loop. */
2319 if (!EQ (args[0], Qt))
2320 ret = Ffuncall (nargs, args);
2323 else
2325 args[0] = XCAR (val);
2326 ret = Ffuncall (nargs, args);
2330 UNGCPRO;
2331 return ret;
2335 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2336 present value of that symbol.
2337 Call each element of FUNLIST,
2338 passing each of them the rest of ARGS.
2339 The caller (or its caller, etc) must gcpro all of ARGS,
2340 except that it isn't necessary to gcpro ARGS[0]. */
2342 Lisp_Object
2343 run_hook_list_with_args (funlist, nargs, args)
2344 Lisp_Object funlist;
2345 int nargs;
2346 Lisp_Object *args;
2348 Lisp_Object sym;
2349 Lisp_Object val;
2350 Lisp_Object globals;
2351 struct gcpro gcpro1, gcpro2, gcpro3;
2353 sym = args[0];
2354 globals = Qnil;
2355 GCPRO3 (sym, val, globals);
2357 for (val = funlist; CONSP (val); val = XCDR (val))
2359 if (EQ (XCAR (val), Qt))
2361 /* t indicates this hook has a local binding;
2362 it means to run the global binding too. */
2364 for (globals = Fdefault_value (sym);
2365 CONSP (globals);
2366 globals = XCDR (globals))
2368 args[0] = XCAR (globals);
2369 /* In a global value, t should not occur. If it does, we
2370 must ignore it to avoid an endless loop. */
2371 if (!EQ (args[0], Qt))
2372 Ffuncall (nargs, args);
2375 else
2377 args[0] = XCAR (val);
2378 Ffuncall (nargs, args);
2381 UNGCPRO;
2382 return Qnil;
2385 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2387 void
2388 run_hook_with_args_2 (hook, arg1, arg2)
2389 Lisp_Object hook, arg1, arg2;
2391 Lisp_Object temp[3];
2392 temp[0] = hook;
2393 temp[1] = arg1;
2394 temp[2] = arg2;
2396 Frun_hook_with_args (3, temp);
2399 /* Apply fn to arg */
2400 Lisp_Object
2401 apply1 (fn, arg)
2402 Lisp_Object fn, arg;
2404 struct gcpro gcpro1;
2406 GCPRO1 (fn);
2407 if (NILP (arg))
2408 RETURN_UNGCPRO (Ffuncall (1, &fn));
2409 gcpro1.nvars = 2;
2410 #ifdef NO_ARG_ARRAY
2412 Lisp_Object args[2];
2413 args[0] = fn;
2414 args[1] = arg;
2415 gcpro1.var = args;
2416 RETURN_UNGCPRO (Fapply (2, args));
2418 #else /* not NO_ARG_ARRAY */
2419 RETURN_UNGCPRO (Fapply (2, &fn));
2420 #endif /* not NO_ARG_ARRAY */
2423 /* Call function fn on no arguments */
2424 Lisp_Object
2425 call0 (fn)
2426 Lisp_Object fn;
2428 struct gcpro gcpro1;
2430 GCPRO1 (fn);
2431 RETURN_UNGCPRO (Ffuncall (1, &fn));
2434 /* Call function fn with 1 argument arg1 */
2435 /* ARGSUSED */
2436 Lisp_Object
2437 call1 (fn, arg1)
2438 Lisp_Object fn, arg1;
2440 struct gcpro gcpro1;
2441 #ifdef NO_ARG_ARRAY
2442 Lisp_Object args[2];
2444 args[0] = fn;
2445 args[1] = arg1;
2446 GCPRO1 (args[0]);
2447 gcpro1.nvars = 2;
2448 RETURN_UNGCPRO (Ffuncall (2, args));
2449 #else /* not NO_ARG_ARRAY */
2450 GCPRO1 (fn);
2451 gcpro1.nvars = 2;
2452 RETURN_UNGCPRO (Ffuncall (2, &fn));
2453 #endif /* not NO_ARG_ARRAY */
2456 /* Call function fn with 2 arguments arg1, arg2 */
2457 /* ARGSUSED */
2458 Lisp_Object
2459 call2 (fn, arg1, arg2)
2460 Lisp_Object fn, arg1, arg2;
2462 struct gcpro gcpro1;
2463 #ifdef NO_ARG_ARRAY
2464 Lisp_Object args[3];
2465 args[0] = fn;
2466 args[1] = arg1;
2467 args[2] = arg2;
2468 GCPRO1 (args[0]);
2469 gcpro1.nvars = 3;
2470 RETURN_UNGCPRO (Ffuncall (3, args));
2471 #else /* not NO_ARG_ARRAY */
2472 GCPRO1 (fn);
2473 gcpro1.nvars = 3;
2474 RETURN_UNGCPRO (Ffuncall (3, &fn));
2475 #endif /* not NO_ARG_ARRAY */
2478 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2479 /* ARGSUSED */
2480 Lisp_Object
2481 call3 (fn, arg1, arg2, arg3)
2482 Lisp_Object fn, arg1, arg2, arg3;
2484 struct gcpro gcpro1;
2485 #ifdef NO_ARG_ARRAY
2486 Lisp_Object args[4];
2487 args[0] = fn;
2488 args[1] = arg1;
2489 args[2] = arg2;
2490 args[3] = arg3;
2491 GCPRO1 (args[0]);
2492 gcpro1.nvars = 4;
2493 RETURN_UNGCPRO (Ffuncall (4, args));
2494 #else /* not NO_ARG_ARRAY */
2495 GCPRO1 (fn);
2496 gcpro1.nvars = 4;
2497 RETURN_UNGCPRO (Ffuncall (4, &fn));
2498 #endif /* not NO_ARG_ARRAY */
2501 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2502 /* ARGSUSED */
2503 Lisp_Object
2504 call4 (fn, arg1, arg2, arg3, arg4)
2505 Lisp_Object fn, arg1, arg2, arg3, arg4;
2507 struct gcpro gcpro1;
2508 #ifdef NO_ARG_ARRAY
2509 Lisp_Object args[5];
2510 args[0] = fn;
2511 args[1] = arg1;
2512 args[2] = arg2;
2513 args[3] = arg3;
2514 args[4] = arg4;
2515 GCPRO1 (args[0]);
2516 gcpro1.nvars = 5;
2517 RETURN_UNGCPRO (Ffuncall (5, args));
2518 #else /* not NO_ARG_ARRAY */
2519 GCPRO1 (fn);
2520 gcpro1.nvars = 5;
2521 RETURN_UNGCPRO (Ffuncall (5, &fn));
2522 #endif /* not NO_ARG_ARRAY */
2525 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2526 /* ARGSUSED */
2527 Lisp_Object
2528 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2529 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2531 struct gcpro gcpro1;
2532 #ifdef NO_ARG_ARRAY
2533 Lisp_Object args[6];
2534 args[0] = fn;
2535 args[1] = arg1;
2536 args[2] = arg2;
2537 args[3] = arg3;
2538 args[4] = arg4;
2539 args[5] = arg5;
2540 GCPRO1 (args[0]);
2541 gcpro1.nvars = 6;
2542 RETURN_UNGCPRO (Ffuncall (6, args));
2543 #else /* not NO_ARG_ARRAY */
2544 GCPRO1 (fn);
2545 gcpro1.nvars = 6;
2546 RETURN_UNGCPRO (Ffuncall (6, &fn));
2547 #endif /* not NO_ARG_ARRAY */
2550 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2551 /* ARGSUSED */
2552 Lisp_Object
2553 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2554 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2556 struct gcpro gcpro1;
2557 #ifdef NO_ARG_ARRAY
2558 Lisp_Object args[7];
2559 args[0] = fn;
2560 args[1] = arg1;
2561 args[2] = arg2;
2562 args[3] = arg3;
2563 args[4] = arg4;
2564 args[5] = arg5;
2565 args[6] = arg6;
2566 GCPRO1 (args[0]);
2567 gcpro1.nvars = 7;
2568 RETURN_UNGCPRO (Ffuncall (7, args));
2569 #else /* not NO_ARG_ARRAY */
2570 GCPRO1 (fn);
2571 gcpro1.nvars = 7;
2572 RETURN_UNGCPRO (Ffuncall (7, &fn));
2573 #endif /* not NO_ARG_ARRAY */
2576 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2577 "Call first argument as a function, passing remaining arguments to it.\n\
2578 Return the value that function returns.\n\
2579 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2580 (nargs, args)
2581 int nargs;
2582 Lisp_Object *args;
2584 Lisp_Object fun;
2585 Lisp_Object funcar;
2586 int numargs = nargs - 1;
2587 Lisp_Object lisp_numargs;
2588 Lisp_Object val;
2589 struct backtrace backtrace;
2590 register Lisp_Object *internal_args;
2591 register int i;
2593 QUIT;
2594 if (consing_since_gc > gc_cons_threshold)
2595 Fgarbage_collect ();
2597 if (++lisp_eval_depth > max_lisp_eval_depth)
2599 if (max_lisp_eval_depth < 100)
2600 max_lisp_eval_depth = 100;
2601 if (lisp_eval_depth > max_lisp_eval_depth)
2602 error ("Lisp nesting exceeds max-lisp-eval-depth");
2605 backtrace.next = backtrace_list;
2606 backtrace_list = &backtrace;
2607 backtrace.function = &args[0];
2608 backtrace.args = &args[1];
2609 backtrace.nargs = nargs - 1;
2610 backtrace.evalargs = 0;
2611 backtrace.debug_on_exit = 0;
2613 if (debug_on_next_call)
2614 do_debug_on_call (Qlambda);
2616 retry:
2618 fun = args[0];
2620 fun = Findirect_function (fun);
2622 if (SUBRP (fun))
2624 if (numargs < XSUBR (fun)->min_args
2625 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2627 XSETFASTINT (lisp_numargs, numargs);
2628 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2631 if (XSUBR (fun)->max_args == UNEVALLED)
2632 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2634 if (XSUBR (fun)->max_args == MANY)
2636 val = (*XSUBR (fun)->function) (numargs, args + 1);
2637 goto done;
2640 if (XSUBR (fun)->max_args > numargs)
2642 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2643 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2644 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2645 internal_args[i] = Qnil;
2647 else
2648 internal_args = args + 1;
2649 switch (XSUBR (fun)->max_args)
2651 case 0:
2652 val = (*XSUBR (fun)->function) ();
2653 goto done;
2654 case 1:
2655 val = (*XSUBR (fun)->function) (internal_args[0]);
2656 goto done;
2657 case 2:
2658 val = (*XSUBR (fun)->function) (internal_args[0],
2659 internal_args[1]);
2660 goto done;
2661 case 3:
2662 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2663 internal_args[2]);
2664 goto done;
2665 case 4:
2666 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2667 internal_args[2],
2668 internal_args[3]);
2669 goto done;
2670 case 5:
2671 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2672 internal_args[2], internal_args[3],
2673 internal_args[4]);
2674 goto done;
2675 case 6:
2676 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2677 internal_args[2], internal_args[3],
2678 internal_args[4], internal_args[5]);
2679 goto done;
2680 case 7:
2681 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2682 internal_args[2], internal_args[3],
2683 internal_args[4], internal_args[5],
2684 internal_args[6]);
2685 goto done;
2687 case 8:
2688 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2689 internal_args[2], internal_args[3],
2690 internal_args[4], internal_args[5],
2691 internal_args[6], internal_args[7]);
2692 goto done;
2694 default:
2696 /* If a subr takes more than 8 arguments without using MANY
2697 or UNEVALLED, we need to extend this function to support it.
2698 Until this is done, there is no way to call the function. */
2699 abort ();
2702 if (COMPILEDP (fun))
2703 val = funcall_lambda (fun, numargs, args + 1);
2704 else
2706 if (!CONSP (fun))
2707 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2708 funcar = Fcar (fun);
2709 if (!SYMBOLP (funcar))
2710 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2711 if (EQ (funcar, Qlambda))
2712 val = funcall_lambda (fun, numargs, args + 1);
2713 else if (EQ (funcar, Qmocklisp))
2714 val = ml_apply (fun, Flist (numargs, args + 1));
2715 else if (EQ (funcar, Qautoload))
2717 do_autoload (fun, args[0]);
2718 goto retry;
2720 else
2721 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2723 done:
2724 lisp_eval_depth--;
2725 if (backtrace.debug_on_exit)
2726 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2727 backtrace_list = backtrace.next;
2728 return val;
2731 Lisp_Object
2732 apply_lambda (fun, args, eval_flag)
2733 Lisp_Object fun, args;
2734 int eval_flag;
2736 Lisp_Object args_left;
2737 Lisp_Object numargs;
2738 register Lisp_Object *arg_vector;
2739 struct gcpro gcpro1, gcpro2, gcpro3;
2740 register int i;
2741 register Lisp_Object tem;
2743 numargs = Flength (args);
2744 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2745 args_left = args;
2747 GCPRO3 (*arg_vector, args_left, fun);
2748 gcpro1.nvars = 0;
2750 for (i = 0; i < XINT (numargs);)
2752 tem = Fcar (args_left), args_left = Fcdr (args_left);
2753 if (eval_flag) tem = Feval (tem);
2754 arg_vector[i++] = tem;
2755 gcpro1.nvars = i;
2758 UNGCPRO;
2760 if (eval_flag)
2762 backtrace_list->args = arg_vector;
2763 backtrace_list->nargs = i;
2765 backtrace_list->evalargs = 0;
2766 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2768 /* Do the debug-on-exit now, while arg_vector still exists. */
2769 if (backtrace_list->debug_on_exit)
2770 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2771 /* Don't do it again when we return to eval. */
2772 backtrace_list->debug_on_exit = 0;
2773 return tem;
2776 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2777 and return the result of evaluation.
2778 FUN must be either a lambda-expression or a compiled-code object. */
2780 Lisp_Object
2781 funcall_lambda (fun, nargs, arg_vector)
2782 Lisp_Object fun;
2783 int nargs;
2784 register Lisp_Object *arg_vector;
2786 Lisp_Object val, syms_left, next;
2787 int count = specpdl_ptr - specpdl;
2788 int i, optional, rest;
2790 if (NILP (Vmocklisp_arguments))
2791 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2793 if (CONSP (fun))
2795 syms_left = XCDR (fun);
2796 if (CONSP (syms_left))
2797 syms_left = XCAR (syms_left);
2798 else
2799 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2801 else if (COMPILEDP (fun))
2802 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2803 else
2804 abort ();
2806 i = optional = rest = 0;
2807 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2809 QUIT;
2811 next = XCAR (syms_left);
2812 while (!SYMBOLP (next))
2813 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2815 if (EQ (next, Qand_rest))
2816 rest = 1;
2817 else if (EQ (next, Qand_optional))
2818 optional = 1;
2819 else if (rest)
2821 specbind (next, Flist (nargs - i, &arg_vector[i]));
2822 i = nargs;
2824 else if (i < nargs)
2825 specbind (next, arg_vector[i++]);
2826 else if (!optional)
2827 return Fsignal (Qwrong_number_of_arguments,
2828 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2829 else
2830 specbind (next, Qnil);
2833 if (!NILP (syms_left))
2834 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2835 else if (i < nargs)
2836 return Fsignal (Qwrong_number_of_arguments,
2837 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2839 if (CONSP (fun))
2840 val = Fprogn (XCDR (XCDR (fun)));
2841 else
2843 /* If we have not actually read the bytecode string
2844 and constants vector yet, fetch them from the file. */
2845 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2846 Ffetch_bytecode (fun);
2847 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2848 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2849 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2852 return unbind_to (count, val);
2855 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2856 1, 1, 0,
2857 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2858 (object)
2859 Lisp_Object object;
2861 Lisp_Object tem;
2863 if (COMPILEDP (object)
2864 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2866 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2867 if (!CONSP (tem))
2868 error ("invalid byte code");
2869 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2870 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2872 return object;
2875 void
2876 grow_specpdl ()
2878 register int count = specpdl_ptr - specpdl;
2879 if (specpdl_size >= max_specpdl_size)
2881 if (max_specpdl_size < 400)
2882 max_specpdl_size = 400;
2883 if (specpdl_size >= max_specpdl_size)
2885 if (!NILP (Vdebug_on_error))
2886 /* Leave room for some specpdl in the debugger. */
2887 max_specpdl_size = specpdl_size + 100;
2888 Fsignal (Qerror,
2889 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2892 specpdl_size *= 2;
2893 if (specpdl_size > max_specpdl_size)
2894 specpdl_size = max_specpdl_size;
2895 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2896 specpdl_ptr = specpdl + count;
2899 void
2900 specbind (symbol, value)
2901 Lisp_Object symbol, value;
2903 Lisp_Object ovalue;
2905 CHECK_SYMBOL (symbol, 0);
2906 if (specpdl_ptr == specpdl + specpdl_size)
2907 grow_specpdl ();
2909 /* The most common case is that a non-constant symbol with a trivial
2910 value. Make that as fast as we can. */
2911 if (!MISCP (XSYMBOL (symbol)->value)
2912 && !EQ (symbol, Qnil)
2913 && !EQ (symbol, Qt)
2914 && !(XSYMBOL (symbol)->name->data[0] == ':'
2915 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
2916 && !EQ (value, symbol)))
2918 specpdl_ptr->symbol = symbol;
2919 specpdl_ptr->old_value = XSYMBOL (symbol)->value;
2920 specpdl_ptr->func = NULL;
2921 ++specpdl_ptr;
2922 XSYMBOL (symbol)->value = value;
2924 else
2926 ovalue = find_symbol_value (symbol);
2927 specpdl_ptr->func = 0;
2928 specpdl_ptr->old_value = ovalue;
2930 if (BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2931 || SOME_BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2932 || BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2934 Lisp_Object current_buffer, binding_buffer;
2936 /* For a local variable, record both the symbol and which
2937 buffer's value we are saving. */
2938 current_buffer = Fcurrent_buffer ();
2939 binding_buffer = current_buffer;
2941 /* If the variable is not local in this buffer,
2942 we are saving the global value, so restore that. */
2943 if (NILP (Flocal_variable_p (symbol, binding_buffer)))
2944 binding_buffer = Qnil;
2945 specpdl_ptr->symbol
2946 = Fcons (symbol, Fcons (binding_buffer, current_buffer));
2948 /* If SYMBOL is a per-buffer variable which doesn't have a
2949 buffer-local value here, make the `let' change the global
2950 value by changing the value of SYMBOL in all buffers not
2951 having their own value. This is consistent with what
2952 happens with other buffer-local variables. */
2953 if (NILP (binding_buffer)
2954 && BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2956 ++specpdl_ptr;
2957 Fset_default (symbol, value);
2958 return;
2961 else
2962 specpdl_ptr->symbol = symbol;
2964 specpdl_ptr++;
2965 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2966 store_symval_forwarding (symbol, ovalue, value, NULL);
2967 else
2968 set_internal (symbol, value, 0, 1);
2972 void
2973 record_unwind_protect (function, arg)
2974 Lisp_Object (*function) P_ ((Lisp_Object));
2975 Lisp_Object arg;
2977 if (specpdl_ptr == specpdl + specpdl_size)
2978 grow_specpdl ();
2979 specpdl_ptr->func = function;
2980 specpdl_ptr->symbol = Qnil;
2981 specpdl_ptr->old_value = arg;
2982 specpdl_ptr++;
2985 Lisp_Object
2986 unbind_to (count, value)
2987 int count;
2988 Lisp_Object value;
2990 int quitf = !NILP (Vquit_flag);
2991 struct gcpro gcpro1;
2993 GCPRO1 (value);
2994 Vquit_flag = Qnil;
2996 while (specpdl_ptr != specpdl + count)
2998 --specpdl_ptr;
3000 if (specpdl_ptr->func != 0)
3001 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3002 /* Note that a "binding" of nil is really an unwind protect,
3003 so in that case the "old value" is a list of forms to evaluate. */
3004 else if (NILP (specpdl_ptr->symbol))
3005 Fprogn (specpdl_ptr->old_value);
3006 /* If the symbol is a list, it is really (SYMBOL BINDING_BUFFER
3007 . CURRENT_BUFFER) and it indicates we bound a variable that
3008 has buffer-local bindings. BINDING_BUFFER nil means that the
3009 variable had the default value when it was bound. */
3010 else if (CONSP (specpdl_ptr->symbol))
3012 Lisp_Object symbol, buffer;
3014 symbol = XCAR (specpdl_ptr->symbol);
3015 buffer = XCAR (XCDR (specpdl_ptr->symbol));
3017 /* Handle restoring a default value. */
3018 if (NILP (buffer))
3019 Fset_default (symbol, specpdl_ptr->old_value);
3020 /* Handle restoring a value saved from a live buffer. */
3021 else
3022 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (buffer), 1);
3024 else
3026 /* If variable has a trivial value (no forwarding), we can
3027 just set it. No need to check for constant symbols here,
3028 since that was already done by specbind. */
3029 if (!MISCP (XSYMBOL (specpdl_ptr->symbol)->value))
3030 XSYMBOL (specpdl_ptr->symbol)->value = specpdl_ptr->old_value;
3031 else
3032 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3036 if (NILP (Vquit_flag) && quitf)
3037 Vquit_flag = Qt;
3039 UNGCPRO;
3040 return value;
3043 #if 0
3045 /* Get the value of symbol's global binding, even if that binding
3046 is not now dynamically visible. */
3048 Lisp_Object
3049 top_level_value (symbol)
3050 Lisp_Object symbol;
3052 register struct specbinding *ptr = specpdl;
3054 CHECK_SYMBOL (symbol, 0);
3055 for (; ptr != specpdl_ptr; ptr++)
3057 if (EQ (ptr->symbol, symbol))
3058 return ptr->old_value;
3060 return Fsymbol_value (symbol);
3063 Lisp_Object
3064 top_level_set (symbol, newval)
3065 Lisp_Object symbol, newval;
3067 register struct specbinding *ptr = specpdl;
3069 CHECK_SYMBOL (symbol, 0);
3070 for (; ptr != specpdl_ptr; ptr++)
3072 if (EQ (ptr->symbol, symbol))
3074 ptr->old_value = newval;
3075 return newval;
3078 return Fset (symbol, newval);
3081 #endif /* 0 */
3083 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3084 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
3085 The debugger is entered when that frame exits, if the flag is non-nil.")
3086 (level, flag)
3087 Lisp_Object level, flag;
3089 register struct backtrace *backlist = backtrace_list;
3090 register int i;
3092 CHECK_NUMBER (level, 0);
3094 for (i = 0; backlist && i < XINT (level); i++)
3096 backlist = backlist->next;
3099 if (backlist)
3100 backlist->debug_on_exit = !NILP (flag);
3102 return flag;
3105 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3106 "Print a trace of Lisp function calls currently active.\n\
3107 Output stream used is value of `standard-output'.")
3110 register struct backtrace *backlist = backtrace_list;
3111 register int i;
3112 Lisp_Object tail;
3113 Lisp_Object tem;
3114 extern Lisp_Object Vprint_level;
3115 struct gcpro gcpro1;
3117 XSETFASTINT (Vprint_level, 3);
3119 tail = Qnil;
3120 GCPRO1 (tail);
3122 while (backlist)
3124 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3125 if (backlist->nargs == UNEVALLED)
3127 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3128 write_string ("\n", -1);
3130 else
3132 tem = *backlist->function;
3133 Fprin1 (tem, Qnil); /* This can QUIT */
3134 write_string ("(", -1);
3135 if (backlist->nargs == MANY)
3137 for (tail = *backlist->args, i = 0;
3138 !NILP (tail);
3139 tail = Fcdr (tail), i++)
3141 if (i) write_string (" ", -1);
3142 Fprin1 (Fcar (tail), Qnil);
3145 else
3147 for (i = 0; i < backlist->nargs; i++)
3149 if (i) write_string (" ", -1);
3150 Fprin1 (backlist->args[i], Qnil);
3153 write_string (")\n", -1);
3155 backlist = backlist->next;
3158 Vprint_level = Qnil;
3159 UNGCPRO;
3160 return Qnil;
3163 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3164 "Return the function and arguments NFRAMES up from current execution point.\n\
3165 If that frame has not evaluated the arguments yet (or is a special form),\n\
3166 the value is (nil FUNCTION ARG-FORMS...).\n\
3167 If that frame has evaluated its arguments and called its function already,\n\
3168 the value is (t FUNCTION ARG-VALUES...).\n\
3169 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3170 FUNCTION is whatever was supplied as car of evaluated list,\n\
3171 or a lambda expression for macro calls.\n\
3172 If NFRAMES is more than the number of frames, the value is nil.")
3173 (nframes)
3174 Lisp_Object nframes;
3176 register struct backtrace *backlist = backtrace_list;
3177 register int i;
3178 Lisp_Object tem;
3180 CHECK_NATNUM (nframes, 0);
3182 /* Find the frame requested. */
3183 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3184 backlist = backlist->next;
3186 if (!backlist)
3187 return Qnil;
3188 if (backlist->nargs == UNEVALLED)
3189 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3190 else
3192 if (backlist->nargs == MANY)
3193 tem = *backlist->args;
3194 else
3195 tem = Flist (backlist->nargs, backlist->args);
3197 return Fcons (Qt, Fcons (*backlist->function, tem));
3202 void
3203 syms_of_eval ()
3205 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3206 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3207 If Lisp code tries to make more than this many at once,\n\
3208 an error is signaled.");
3210 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3211 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
3212 This limit is to catch infinite recursions for you before they cause\n\
3213 actual stack overflow in C, which would be fatal for Emacs.\n\
3214 You can safely make it considerably larger than its default value,\n\
3215 if that proves inconveniently small.");
3217 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3218 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
3219 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
3220 Vquit_flag = Qnil;
3222 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3223 "Non-nil inhibits C-g quitting from happening immediately.\n\
3224 Note that `quit-flag' will still be set by typing C-g,\n\
3225 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
3226 To prevent this happening, set `quit-flag' to nil\n\
3227 before making `inhibit-quit' nil.");
3228 Vinhibit_quit = Qnil;
3230 Qinhibit_quit = intern ("inhibit-quit");
3231 staticpro (&Qinhibit_quit);
3233 Qautoload = intern ("autoload");
3234 staticpro (&Qautoload);
3236 Qdebug_on_error = intern ("debug-on-error");
3237 staticpro (&Qdebug_on_error);
3239 Qmacro = intern ("macro");
3240 staticpro (&Qmacro);
3242 /* Note that the process handling also uses Qexit, but we don't want
3243 to staticpro it twice, so we just do it here. */
3244 Qexit = intern ("exit");
3245 staticpro (&Qexit);
3247 Qinteractive = intern ("interactive");
3248 staticpro (&Qinteractive);
3250 Qcommandp = intern ("commandp");
3251 staticpro (&Qcommandp);
3253 Qdefun = intern ("defun");
3254 staticpro (&Qdefun);
3256 Qand_rest = intern ("&rest");
3257 staticpro (&Qand_rest);
3259 Qand_optional = intern ("&optional");
3260 staticpro (&Qand_optional);
3262 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3263 "*Non-nil means automatically display a backtrace buffer\n\
3264 after any error that is handled by the editor command loop.\n\
3265 If the value is a list, an error only means to display a backtrace\n\
3266 if one of its condition symbols appears in the list.");
3267 Vstack_trace_on_error = Qnil;
3269 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3270 "*Non-nil means enter debugger if an error is signaled.\n\
3271 Does not apply to errors handled by `condition-case' or those\n\
3272 matched by `debug-ignored-errors'.\n\
3273 If the value is a list, an error only means to enter the debugger\n\
3274 if one of its condition symbols appears in the list.\n\
3275 See also variable `debug-on-quit'.");
3276 Vdebug_on_error = Qnil;
3278 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3279 "*List of errors for which the debugger should not be called.\n\
3280 Each element may be a condition-name or a regexp that matches error messages.\n\
3281 If any element applies to a given error, that error skips the debugger\n\
3282 and just returns to top level.\n\
3283 This overrides the variable `debug-on-error'.\n\
3284 It does not apply to errors handled by `condition-case'.");
3285 Vdebug_ignored_errors = Qnil;
3287 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3288 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3289 Does not apply if quit is handled by a `condition-case'.");
3290 debug_on_quit = 0;
3292 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3293 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3295 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3296 "Non-nil means debugger may continue execution.\n\
3297 This is nil when the debugger is called under circumstances where it\n\
3298 might not be safe to continue.");
3299 debugger_may_continue = 1;
3301 DEFVAR_LISP ("debugger", &Vdebugger,
3302 "Function to call to invoke debugger.\n\
3303 If due to frame exit, args are `exit' and the value being returned;\n\
3304 this function's value will be returned instead of that.\n\
3305 If due to error, args are `error' and a list of the args to `signal'.\n\
3306 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3307 If due to `eval' entry, one arg, t.");
3308 Vdebugger = Qnil;
3310 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3311 "If non-nil, this is a function for `signal' to call.\n\
3312 It receives the same arguments that `signal' was given.\n\
3313 The Edebug package uses this to regain control.");
3314 Vsignal_hook_function = Qnil;
3316 Qmocklisp_arguments = intern ("mocklisp-arguments");
3317 staticpro (&Qmocklisp_arguments);
3318 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3319 "While in a mocklisp function, the list of its unevaluated args.");
3320 Vmocklisp_arguments = Qt;
3322 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3323 "*Non-nil means call the debugger regardless of condition handlers.\n\
3324 Note that `debug-on-error', `debug-on-quit' and friends\n\
3325 still determine whether to handle the particular condition.");
3326 Vdebug_on_signal = Qnil;
3328 Vrun_hooks = intern ("run-hooks");
3329 staticpro (&Vrun_hooks);
3331 staticpro (&Vautoload_queue);
3332 Vautoload_queue = Qnil;
3333 staticpro (&Vsignaling_function);
3334 Vsignaling_function = Qnil;
3336 defsubr (&Sor);
3337 defsubr (&Sand);
3338 defsubr (&Sif);
3339 defsubr (&Scond);
3340 defsubr (&Sprogn);
3341 defsubr (&Sprog1);
3342 defsubr (&Sprog2);
3343 defsubr (&Ssetq);
3344 defsubr (&Squote);
3345 defsubr (&Sfunction);
3346 defsubr (&Sdefun);
3347 defsubr (&Sdefmacro);
3348 defsubr (&Sdefvar);
3349 defsubr (&Sdefconst);
3350 defsubr (&Suser_variable_p);
3351 defsubr (&Slet);
3352 defsubr (&SletX);
3353 defsubr (&Swhile);
3354 defsubr (&Smacroexpand);
3355 defsubr (&Scatch);
3356 defsubr (&Sthrow);
3357 defsubr (&Sunwind_protect);
3358 defsubr (&Scondition_case);
3359 defsubr (&Ssignal);
3360 defsubr (&Sinteractive_p);
3361 defsubr (&Scommandp);
3362 defsubr (&Sautoload);
3363 defsubr (&Seval);
3364 defsubr (&Sapply);
3365 defsubr (&Sfuncall);
3366 defsubr (&Srun_hooks);
3367 defsubr (&Srun_hook_with_args);
3368 defsubr (&Srun_hook_with_args_until_success);
3369 defsubr (&Srun_hook_with_args_until_failure);
3370 defsubr (&Sfetch_bytecode);
3371 defsubr (&Sbacktrace_debug);
3372 defsubr (&Sbacktrace);
3373 defsubr (&Sbacktrace_frame);