(mouse-buffer-menu-mode-groups): Added "Version Control" group.
[emacs.git] / src / eval.c
blob2f87d5fc22cd9c0381c140e54d491de5c35743e0
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.\n\
292 usage: (or CONDITIONS ...)")
293 (args)
294 Lisp_Object args;
296 register Lisp_Object val;
297 Lisp_Object args_left;
298 struct gcpro gcpro1;
300 if (NILP(args))
301 return Qnil;
303 args_left = args;
304 GCPRO1 (args_left);
308 val = Feval (Fcar (args_left));
309 if (!NILP (val))
310 break;
311 args_left = Fcdr (args_left);
313 while (!NILP(args_left));
315 UNGCPRO;
316 return val;
319 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
320 "Eval args until one of them yields nil, then return nil.\n\
321 The remaining args are not evalled at all.\n\
322 If no arg yields nil, return the last arg's value.\n\
323 usage: (and CONDITIONS ...)")
324 (args)
325 Lisp_Object args;
327 register Lisp_Object val;
328 Lisp_Object args_left;
329 struct gcpro gcpro1;
331 if (NILP(args))
332 return Qt;
334 args_left = args;
335 GCPRO1 (args_left);
339 val = Feval (Fcar (args_left));
340 if (NILP (val))
341 break;
342 args_left = Fcdr (args_left);
344 while (!NILP(args_left));
346 UNGCPRO;
347 return val;
350 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
351 "If COND yields non-nil, do THEN, else do ELSE...\n\
352 Returns the value of THEN or the value of the last of the ELSE's.\n\
353 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
354 If COND yields nil, and there are no ELSE's, the value is nil.\n\
355 usage: (if COND THEN ELSE...)")
356 (args)
357 Lisp_Object args;
359 register Lisp_Object cond;
360 struct gcpro gcpro1;
362 GCPRO1 (args);
363 cond = Feval (Fcar (args));
364 UNGCPRO;
366 if (!NILP (cond))
367 return Feval (Fcar (Fcdr (args)));
368 return Fprogn (Fcdr (Fcdr (args)));
371 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
372 "Try each clause until one succeeds.\n\
373 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
374 and, if the value is non-nil, this clause succeeds:\n\
375 then the expressions in BODY are evaluated and the last one's\n\
376 value is the value of the cond-form.\n\
377 If no clause succeeds, cond returns nil.\n\
378 If a clause has one element, as in (CONDITION),\n\
379 CONDITION's value if non-nil is returned from the cond-form.\n\
380 usage: (cond CLAUSES...)")
381 (args)
382 Lisp_Object args;
384 register Lisp_Object clause, val;
385 struct gcpro gcpro1;
387 val = Qnil;
388 GCPRO1 (args);
389 while (!NILP (args))
391 clause = Fcar (args);
392 val = Feval (Fcar (clause));
393 if (!NILP (val))
395 if (!EQ (XCDR (clause), Qnil))
396 val = Fprogn (XCDR (clause));
397 break;
399 args = XCDR (args);
401 UNGCPRO;
403 return val;
406 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
407 "Eval BODY forms sequentially and return value of last one.\n\
408 usage: (progn BODY ...)")
409 (args)
410 Lisp_Object args;
412 register Lisp_Object val, tem;
413 Lisp_Object args_left;
414 struct gcpro gcpro1;
416 /* In Mocklisp code, symbols at the front of the progn arglist
417 are to be bound to zero. */
418 if (!EQ (Vmocklisp_arguments, Qt))
420 val = make_number (0);
421 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
423 QUIT;
424 specbind (tem, val), args = Fcdr (args);
428 if (NILP(args))
429 return Qnil;
431 args_left = args;
432 GCPRO1 (args_left);
436 val = Feval (Fcar (args_left));
437 args_left = Fcdr (args_left);
439 while (!NILP(args_left));
441 UNGCPRO;
442 return val;
445 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
446 "Eval FIRST and BODY sequentially; value from FIRST.\n\
447 The value of FIRST is saved during the evaluation of the remaining args,\n\
448 whose values are discarded.\n\
449 usage: (prog1 FIRST BODY...)")
450 (args)
451 Lisp_Object args;
453 Lisp_Object val;
454 register Lisp_Object args_left;
455 struct gcpro gcpro1, gcpro2;
456 register int argnum = 0;
458 if (NILP(args))
459 return Qnil;
461 args_left = args;
462 val = Qnil;
463 GCPRO2 (args, val);
467 if (!(argnum++))
468 val = Feval (Fcar (args_left));
469 else
470 Feval (Fcar (args_left));
471 args_left = Fcdr (args_left);
473 while (!NILP(args_left));
475 UNGCPRO;
476 return val;
479 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
480 "Eval X, Y and BODY sequentially; value from Y.\n\
481 The value of Y is saved during the evaluation of the remaining args,\n\
482 whose values are discarded.\n\
483 usage: (prog2 X Y BODY...)")
484 (args)
485 Lisp_Object args;
487 Lisp_Object val;
488 register Lisp_Object args_left;
489 struct gcpro gcpro1, gcpro2;
490 register int argnum = -1;
492 val = Qnil;
494 if (NILP (args))
495 return Qnil;
497 args_left = args;
498 val = Qnil;
499 GCPRO2 (args, val);
503 if (!(argnum++))
504 val = Feval (Fcar (args_left));
505 else
506 Feval (Fcar (args_left));
507 args_left = Fcdr (args_left);
509 while (!NILP (args_left));
511 UNGCPRO;
512 return val;
515 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
516 "Set each SYM to the value of its VAL.\n\
517 The symbols SYM are variables; they are literal (not evaluated).\n\
518 The values VAL are expressions; they are evaluated.\n\
519 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
520 The second VAL is not computed until after the first SYM is set, and so on;\n\
521 each VAL can use the new value of variables set earlier in the `setq'.\n\
522 The return value of the `setq' form is the value of the last VAL.\n\
523 usage: (setq SYM VAL SYM VAL ...)")
524 (args)
525 Lisp_Object args;
527 register Lisp_Object args_left;
528 register Lisp_Object val, sym;
529 struct gcpro gcpro1;
531 if (NILP(args))
532 return Qnil;
534 args_left = args;
535 GCPRO1 (args);
539 val = Feval (Fcar (Fcdr (args_left)));
540 sym = Fcar (args_left);
541 Fset (sym, val);
542 args_left = Fcdr (Fcdr (args_left));
544 while (!NILP(args_left));
546 UNGCPRO;
547 return val;
550 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
551 "Return the argument, without evaluating it. `(quote x)' yields `x'.\n\
552 usage: (quote ARG)")
553 (args)
554 Lisp_Object args;
556 return Fcar (args);
559 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
560 "Like `quote', but preferred for objects which are functions.\n\
561 In byte compilation, `function' causes its argument to be compiled.\n\
562 `quote' cannot do that.\n\
563 usage: (function ARG)")
564 (args)
565 Lisp_Object args;
567 return Fcar (args);
571 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
572 "Return t if function in which this appears was called interactively.\n\
573 This means that the function was called with call-interactively (which\n\
574 includes being called as the binding of a key)\n\
575 and input is currently coming from the keyboard (not in keyboard macro).")
578 return interactive_p (1) ? Qt : Qnil;
582 /* Return 1 if function in which this appears was called
583 interactively. This means that the function was called with
584 call-interactively (which includes being called as the binding of
585 a key) and input is currently coming from the keyboard (not in
586 keyboard macro).
588 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
589 called is a built-in. */
592 interactive_p (exclude_subrs_p)
593 int exclude_subrs_p;
595 struct backtrace *btp;
596 Lisp_Object fun;
598 if (!INTERACTIVE)
599 return 0;
601 btp = backtrace_list;
603 /* If this isn't a byte-compiled function, there may be a frame at
604 the top for Finteractive_p. If so, skip it. */
605 fun = Findirect_function (*btp->function);
606 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
607 btp = btp->next;
609 /* If we're running an Emacs 18-style byte-compiled function, there
610 may be a frame for Fbytecode. Now, given the strictest
611 definition, this function isn't really being called
612 interactively, but because that's the way Emacs 18 always builds
613 byte-compiled functions, we'll accept it for now. */
614 if (EQ (*btp->function, Qbytecode))
615 btp = btp->next;
617 /* If this isn't a byte-compiled function, then we may now be
618 looking at several frames for special forms. Skip past them. */
619 while (btp &&
620 btp->nargs == UNEVALLED)
621 btp = btp->next;
623 /* btp now points at the frame of the innermost function that isn't
624 a special form, ignoring frames for Finteractive_p and/or
625 Fbytecode at the top. If this frame is for a built-in function
626 (such as load or eval-region) return nil. */
627 fun = Findirect_function (*btp->function);
628 if (exclude_subrs_p && SUBRP (fun))
629 return 0;
631 /* btp points to the frame of a Lisp function that called interactive-p.
632 Return t if that function was called interactively. */
633 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
634 return 1;
635 return 0;
639 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
640 "Define NAME as a function.\n\
641 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
642 See also the function `interactive'.\n\
643 usage: (defun NAME ARGLIST [DOCSTRING] BODY...)")
644 (args)
645 Lisp_Object args;
647 register Lisp_Object fn_name;
648 register Lisp_Object defn;
650 fn_name = Fcar (args);
651 defn = Fcons (Qlambda, Fcdr (args));
652 if (!NILP (Vpurify_flag))
653 defn = Fpurecopy (defn);
654 Ffset (fn_name, defn);
655 LOADHIST_ATTACH (fn_name);
656 return fn_name;
659 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
660 "Define NAME as a macro.\n\
661 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
662 When the macro is called, as in (NAME ARGS...),\n\
663 the function (lambda ARGLIST BODY...) is applied to\n\
664 the list ARGS... as it appears in the expression,\n\
665 and the result should be a form to be evaluated instead of the original.\n\
666 usage: (defmacro NAME ARGLIST [DOCSTRING] BODY...)")
667 (args)
668 Lisp_Object args;
670 register Lisp_Object fn_name;
671 register Lisp_Object defn;
673 fn_name = Fcar (args);
674 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
675 if (!NILP (Vpurify_flag))
676 defn = Fpurecopy (defn);
677 Ffset (fn_name, defn);
678 LOADHIST_ATTACH (fn_name);
679 return fn_name;
683 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 2, 0,
684 "Make SYMBOL a variable alias for symbol ALIASED.\n\
685 Setting the value of SYMBOL will subsequently set the value of ALIASED,\n\
686 and getting the value of SYMBOL will return the value ALIASED has.\n\
687 ALIASED nil means remove the alias; SYMBOL is unbound after that.")
688 (symbol, aliased)
689 Lisp_Object symbol, aliased;
691 struct Lisp_Symbol *sym;
693 CHECK_SYMBOL (symbol, 0);
694 CHECK_SYMBOL (aliased, 1);
696 if (SYMBOL_CONSTANT_P (symbol))
697 error ("Cannot make a constant an alias");
699 sym = XSYMBOL (symbol);
700 sym->indirect_variable = 1;
701 sym->value = aliased;
702 sym->constant = SYMBOL_CONSTANT_P (aliased);
703 LOADHIST_ATTACH (symbol);
705 return aliased;
709 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
710 "Define SYMBOL as a variable.\n\
711 You are not required to define a variable in order to use it,\n\
712 but the definition can supply documentation and an initial value\n\
713 in a way that tags can recognize.\n\n\
714 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
715 If SYMBOL is buffer-local, its default value is what is set;\n\
716 buffer-local values are not affected.\n\
717 INITVALUE and DOCSTRING are optional.\n\
718 If DOCSTRING starts with *, this variable is identified as a user option.\n\
719 This means that M-x set-variable recognizes it.\n\
720 See also `user-variable-p'.\n\
721 If INITVALUE is missing, SYMBOL's value is not set.\n\
722 usage: (defvar SYMBOL [INITVALUE DOCSTRING])")
723 (args)
724 Lisp_Object args;
726 register Lisp_Object sym, tem, tail;
728 sym = Fcar (args);
729 tail = Fcdr (args);
730 if (!NILP (Fcdr (Fcdr (tail))))
731 error ("too many arguments");
733 tem = Fdefault_boundp (sym);
734 if (!NILP (tail))
736 if (NILP (tem))
737 Fset_default (sym, Feval (Fcar (tail)));
738 tail = Fcdr (tail);
739 if (!NILP (Fcar (tail)))
741 tem = Fcar (tail);
742 if (!NILP (Vpurify_flag))
743 tem = Fpurecopy (tem);
744 Fput (sym, Qvariable_documentation, tem);
746 LOADHIST_ATTACH (sym);
748 else
749 /* A (defvar <var>) should not take precedence in the load-history over
750 an earlier (defvar <var> <val>), so only add to history if the default
751 value is still unbound. */
752 if (NILP (tem))
753 LOADHIST_ATTACH (sym);
755 return sym;
758 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
759 "Define SYMBOL as a constant variable.\n\
760 The intent is that neither programs nor users should ever change this value.\n\
761 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
762 If SYMBOL is buffer-local, its default value is what is set;\n\
763 buffer-local values are not affected.\n\
764 DOCSTRING is optional.\n\
765 usage: (defconst SYMBOL INITVALUE [DOCSTRING])")
766 (args)
767 Lisp_Object args;
769 register Lisp_Object sym, tem;
771 sym = Fcar (args);
772 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
773 error ("too many arguments");
775 tem = Feval (Fcar (Fcdr (args)));
776 if (!NILP (Vpurify_flag))
777 tem = Fpurecopy (tem);
778 Fset_default (sym, tem);
779 tem = Fcar (Fcdr (Fcdr (args)));
780 if (!NILP (tem))
782 if (!NILP (Vpurify_flag))
783 tem = Fpurecopy (tem);
784 Fput (sym, Qvariable_documentation, tem);
786 LOADHIST_ATTACH (sym);
787 return sym;
790 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
791 "Returns t if VARIABLE is intended to be set and modified by users.\n\
792 \(The alternative is a variable used internally in a Lisp program.)\n\
793 Determined by whether the first character of the documentation\n\
794 for the variable is `*' or if the variable is customizable (has a non-nil\n\
795 value of any of `custom-type', `custom-loads' or `standard-value'\n\
796 on its property list).")
797 (variable)
798 Lisp_Object variable;
800 Lisp_Object documentation;
802 if (!SYMBOLP (variable))
803 return Qnil;
805 documentation = Fget (variable, Qvariable_documentation);
806 if (INTEGERP (documentation) && XINT (documentation) < 0)
807 return Qt;
808 if (STRINGP (documentation)
809 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
810 return Qt;
811 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
812 if (CONSP (documentation)
813 && STRINGP (XCAR (documentation))
814 && INTEGERP (XCDR (documentation))
815 && XINT (XCDR (documentation)) < 0)
816 return Qt;
817 /* Customizable? */
818 if ((!NILP (Fget (variable, intern ("custom-type"))))
819 || (!NILP (Fget (variable, intern ("custom-loads"))))
820 || (!NILP (Fget (variable, intern ("standard-value")))))
821 return Qt;
822 return Qnil;
825 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
826 "Bind variables according to VARLIST then eval BODY.\n\
827 The value of the last form in BODY is returned.\n\
828 Each element of VARLIST is a symbol (which is bound to nil)\n\
829 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
830 Each VALUEFORM can refer to the symbols already bound by this VARLIST.\n\
831 usage: (let* VARLIST BODY...)")
832 (args)
833 Lisp_Object args;
835 Lisp_Object varlist, val, elt;
836 int count = specpdl_ptr - specpdl;
837 struct gcpro gcpro1, gcpro2, gcpro3;
839 GCPRO3 (args, elt, varlist);
841 varlist = Fcar (args);
842 while (!NILP (varlist))
844 QUIT;
845 elt = Fcar (varlist);
846 if (SYMBOLP (elt))
847 specbind (elt, Qnil);
848 else if (! NILP (Fcdr (Fcdr (elt))))
849 Fsignal (Qerror,
850 Fcons (build_string ("`let' bindings can have only one value-form"),
851 elt));
852 else
854 val = Feval (Fcar (Fcdr (elt)));
855 specbind (Fcar (elt), val);
857 varlist = Fcdr (varlist);
859 UNGCPRO;
860 val = Fprogn (Fcdr (args));
861 return unbind_to (count, val);
864 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
865 "Bind variables according to VARLIST then eval BODY.\n\
866 The value of the last form in BODY is returned.\n\
867 Each element of VARLIST is a symbol (which is bound to nil)\n\
868 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
869 All the VALUEFORMs are evalled before any symbols are bound.\n\
870 usage: (let VARLIST BODY...)")
871 (args)
872 Lisp_Object args;
874 Lisp_Object *temps, tem;
875 register Lisp_Object elt, varlist;
876 int count = specpdl_ptr - specpdl;
877 register int argnum;
878 struct gcpro gcpro1, gcpro2;
880 varlist = Fcar (args);
882 /* Make space to hold the values to give the bound variables */
883 elt = Flength (varlist);
884 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
886 /* Compute the values and store them in `temps' */
888 GCPRO2 (args, *temps);
889 gcpro2.nvars = 0;
891 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
893 QUIT;
894 elt = Fcar (varlist);
895 if (SYMBOLP (elt))
896 temps [argnum++] = Qnil;
897 else if (! NILP (Fcdr (Fcdr (elt))))
898 Fsignal (Qerror,
899 Fcons (build_string ("`let' bindings can have only one value-form"),
900 elt));
901 else
902 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
903 gcpro2.nvars = argnum;
905 UNGCPRO;
907 varlist = Fcar (args);
908 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
910 elt = Fcar (varlist);
911 tem = temps[argnum++];
912 if (SYMBOLP (elt))
913 specbind (elt, tem);
914 else
915 specbind (Fcar (elt), tem);
918 elt = Fprogn (Fcdr (args));
919 return unbind_to (count, elt);
922 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
923 "If TEST yields non-nil, eval BODY... and repeat.\n\
924 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
925 until TEST returns nil.\n\
926 usage: (while TEST BODY...)")
927 (args)
928 Lisp_Object args;
930 Lisp_Object test, body, tem;
931 struct gcpro gcpro1, gcpro2;
933 GCPRO2 (test, body);
935 test = Fcar (args);
936 body = Fcdr (args);
937 while (tem = Feval (test),
938 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
940 QUIT;
941 Fprogn (body);
944 UNGCPRO;
945 return Qnil;
948 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
949 "Return result of expanding macros at top level of FORM.\n\
950 If FORM is not a macro call, it is returned unchanged.\n\
951 Otherwise, the macro is expanded and the expansion is considered\n\
952 in place of FORM. When a non-macro-call results, it is returned.\n\n\
953 The second optional arg ENVIRONMENT specifies an environment of macro\n\
954 definitions to shadow the loaded ones for use in file byte-compilation.")
955 (form, environment)
956 Lisp_Object form;
957 Lisp_Object environment;
959 /* With cleanups from Hallvard Furuseth. */
960 register Lisp_Object expander, sym, def, tem;
962 while (1)
964 /* Come back here each time we expand a macro call,
965 in case it expands into another macro call. */
966 if (!CONSP (form))
967 break;
968 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
969 def = sym = XCAR (form);
970 tem = Qnil;
971 /* Trace symbols aliases to other symbols
972 until we get a symbol that is not an alias. */
973 while (SYMBOLP (def))
975 QUIT;
976 sym = def;
977 tem = Fassq (sym, environment);
978 if (NILP (tem))
980 def = XSYMBOL (sym)->function;
981 if (!EQ (def, Qunbound))
982 continue;
984 break;
986 /* Right now TEM is the result from SYM in ENVIRONMENT,
987 and if TEM is nil then DEF is SYM's function definition. */
988 if (NILP (tem))
990 /* SYM is not mentioned in ENVIRONMENT.
991 Look at its function definition. */
992 if (EQ (def, Qunbound) || !CONSP (def))
993 /* Not defined or definition not suitable */
994 break;
995 if (EQ (XCAR (def), Qautoload))
997 /* Autoloading function: will it be a macro when loaded? */
998 tem = Fnth (make_number (4), def);
999 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1000 /* Yes, load it and try again. */
1002 struct gcpro gcpro1;
1003 GCPRO1 (form);
1004 do_autoload (def, sym);
1005 UNGCPRO;
1006 continue;
1008 else
1009 break;
1011 else if (!EQ (XCAR (def), Qmacro))
1012 break;
1013 else expander = XCDR (def);
1015 else
1017 expander = XCDR (tem);
1018 if (NILP (expander))
1019 break;
1021 form = apply1 (expander, XCDR (form));
1023 return form;
1026 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1027 "Eval BODY allowing nonlocal exits using `throw'.\n\
1028 TAG is evalled to get the tag to use; it must not be nil.\n\
1030 Then the BODY is executed.\n\
1031 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
1032 If no throw happens, `catch' returns the value of the last BODY form.\n\
1033 If a throw happens, it specifies the value to return from `catch'.\n\
1034 usage: (catch TAG BODY...)")
1035 (args)
1036 Lisp_Object args;
1038 register Lisp_Object tag;
1039 struct gcpro gcpro1;
1041 GCPRO1 (args);
1042 tag = Feval (Fcar (args));
1043 UNGCPRO;
1044 return internal_catch (tag, Fprogn, Fcdr (args));
1047 /* Set up a catch, then call C function FUNC on argument ARG.
1048 FUNC should return a Lisp_Object.
1049 This is how catches are done from within C code. */
1051 Lisp_Object
1052 internal_catch (tag, func, arg)
1053 Lisp_Object tag;
1054 Lisp_Object (*func) ();
1055 Lisp_Object arg;
1057 /* This structure is made part of the chain `catchlist'. */
1058 struct catchtag c;
1060 /* Fill in the components of c, and put it on the list. */
1061 c.next = catchlist;
1062 c.tag = tag;
1063 c.val = Qnil;
1064 c.backlist = backtrace_list;
1065 c.handlerlist = handlerlist;
1066 c.lisp_eval_depth = lisp_eval_depth;
1067 c.pdlcount = specpdl_ptr - specpdl;
1068 c.poll_suppress_count = poll_suppress_count;
1069 c.gcpro = gcprolist;
1070 c.byte_stack = byte_stack_list;
1071 catchlist = &c;
1073 /* Call FUNC. */
1074 if (! _setjmp (c.jmp))
1075 c.val = (*func) (arg);
1077 /* Throw works by a longjmp that comes right here. */
1078 catchlist = c.next;
1079 return c.val;
1082 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1083 jump to that CATCH, returning VALUE as the value of that catch.
1085 This is the guts Fthrow and Fsignal; they differ only in the way
1086 they choose the catch tag to throw to. A catch tag for a
1087 condition-case form has a TAG of Qnil.
1089 Before each catch is discarded, unbind all special bindings and
1090 execute all unwind-protect clauses made above that catch. Unwind
1091 the handler stack as we go, so that the proper handlers are in
1092 effect for each unwind-protect clause we run. At the end, restore
1093 some static info saved in CATCH, and longjmp to the location
1094 specified in the
1096 This is used for correct unwinding in Fthrow and Fsignal. */
1098 static void
1099 unwind_to_catch (catch, value)
1100 struct catchtag *catch;
1101 Lisp_Object value;
1103 register int last_time;
1105 /* Save the value in the tag. */
1106 catch->val = value;
1108 /* Restore the polling-suppression count. */
1109 set_poll_suppress_count (catch->poll_suppress_count);
1113 last_time = catchlist == catch;
1115 /* Unwind the specpdl stack, and then restore the proper set of
1116 handlers. */
1117 unbind_to (catchlist->pdlcount, Qnil);
1118 handlerlist = catchlist->handlerlist;
1119 catchlist = catchlist->next;
1121 while (! last_time);
1123 byte_stack_list = catch->byte_stack;
1124 gcprolist = catch->gcpro;
1125 #ifdef DEBUG_GCPRO
1126 if (gcprolist != 0)
1127 gcpro_level = gcprolist->level + 1;
1128 else
1129 gcpro_level = 0;
1130 #endif
1131 backtrace_list = catch->backlist;
1132 lisp_eval_depth = catch->lisp_eval_depth;
1134 _longjmp (catch->jmp, 1);
1137 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1138 "Throw to the catch for TAG and return VALUE from it.\n\
1139 Both TAG and VALUE are evalled.")
1140 (tag, value)
1141 register Lisp_Object tag, value;
1143 register struct catchtag *c;
1145 while (1)
1147 if (!NILP (tag))
1148 for (c = catchlist; c; c = c->next)
1150 if (EQ (c->tag, tag))
1151 unwind_to_catch (c, value);
1153 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1158 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1159 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1160 If BODYFORM completes normally, its value is returned\n\
1161 after executing the UNWINDFORMS.\n\
1162 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.\n\
1163 usage: (unwind-protect BODYFORM UNWINDFORMS...)")
1164 (args)
1165 Lisp_Object args;
1167 Lisp_Object val;
1168 int count = specpdl_ptr - specpdl;
1170 record_unwind_protect (0, Fcdr (args));
1171 val = Feval (Fcar (args));
1172 return unbind_to (count, val);
1175 /* Chain of condition handlers currently in effect.
1176 The elements of this chain are contained in the stack frames
1177 of Fcondition_case and internal_condition_case.
1178 When an error is signaled (by calling Fsignal, below),
1179 this chain is searched for an element that applies. */
1181 struct handler *handlerlist;
1183 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1184 "Regain control when an error is signaled.\n\
1185 executes BODYFORM and returns its value if no error happens.\n\
1186 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1187 where the BODY is made of Lisp expressions.\n\n\
1188 A handler is applicable to an error\n\
1189 if CONDITION-NAME is one of the error's condition names.\n\
1190 If an error happens, the first applicable handler is run.\n\
1192 The car of a handler may be a list of condition names\n\
1193 instead of a single condition name.\n\
1195 When a handler handles an error,\n\
1196 control returns to the condition-case and the handler BODY... is executed\n\
1197 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1198 VAR may be nil; then you do not get access to the signal information.\n\
1200 The value of the last BODY form is returned from the condition-case.\n\
1201 See also the function `signal' for more info.\n\
1202 usage: (condition-case VAR BODYFORM HANDLERS...)")
1203 (args)
1204 Lisp_Object args;
1206 Lisp_Object val;
1207 struct catchtag c;
1208 struct handler h;
1209 register Lisp_Object bodyform, handlers;
1210 volatile Lisp_Object var;
1212 var = Fcar (args);
1213 bodyform = Fcar (Fcdr (args));
1214 handlers = Fcdr (Fcdr (args));
1215 CHECK_SYMBOL (var, 0);
1217 for (val = handlers; ! NILP (val); val = Fcdr (val))
1219 Lisp_Object tem;
1220 tem = Fcar (val);
1221 if (! (NILP (tem)
1222 || (CONSP (tem)
1223 && (SYMBOLP (XCAR (tem))
1224 || CONSP (XCAR (tem))))))
1225 error ("Invalid condition handler", tem);
1228 c.tag = Qnil;
1229 c.val = Qnil;
1230 c.backlist = backtrace_list;
1231 c.handlerlist = handlerlist;
1232 c.lisp_eval_depth = lisp_eval_depth;
1233 c.pdlcount = specpdl_ptr - specpdl;
1234 c.poll_suppress_count = poll_suppress_count;
1235 c.gcpro = gcprolist;
1236 c.byte_stack = byte_stack_list;
1237 if (_setjmp (c.jmp))
1239 if (!NILP (h.var))
1240 specbind (h.var, c.val);
1241 val = Fprogn (Fcdr (h.chosen_clause));
1243 /* Note that this just undoes the binding of h.var; whoever
1244 longjumped to us unwound the stack to c.pdlcount before
1245 throwing. */
1246 unbind_to (c.pdlcount, Qnil);
1247 return val;
1249 c.next = catchlist;
1250 catchlist = &c;
1252 h.var = var;
1253 h.handler = handlers;
1254 h.next = handlerlist;
1255 h.tag = &c;
1256 handlerlist = &h;
1258 val = Feval (bodyform);
1259 catchlist = c.next;
1260 handlerlist = h.next;
1261 return val;
1264 /* Call the function BFUN with no arguments, catching errors within it
1265 according to HANDLERS. If there is an error, call HFUN with
1266 one argument which is the data that describes the error:
1267 (SIGNALNAME . DATA)
1269 HANDLERS can be a list of conditions to catch.
1270 If HANDLERS is Qt, catch all errors.
1271 If HANDLERS is Qerror, catch all errors
1272 but allow the debugger to run if that is enabled. */
1274 Lisp_Object
1275 internal_condition_case (bfun, handlers, hfun)
1276 Lisp_Object (*bfun) ();
1277 Lisp_Object handlers;
1278 Lisp_Object (*hfun) ();
1280 Lisp_Object val;
1281 struct catchtag c;
1282 struct handler h;
1284 #if 0 /* Can't do this check anymore because realize_basic_faces has
1285 to BLOCK_INPUT, and can call Lisp. What's really needed is a
1286 flag indicating that we're currently handling a signal. */
1287 /* Since Fsignal resets this to 0, it had better be 0 now
1288 or else we have a potential bug. */
1289 if (interrupt_input_blocked != 0)
1290 abort ();
1291 #endif
1293 c.tag = Qnil;
1294 c.val = Qnil;
1295 c.backlist = backtrace_list;
1296 c.handlerlist = handlerlist;
1297 c.lisp_eval_depth = lisp_eval_depth;
1298 c.pdlcount = specpdl_ptr - specpdl;
1299 c.poll_suppress_count = poll_suppress_count;
1300 c.gcpro = gcprolist;
1301 c.byte_stack = byte_stack_list;
1302 if (_setjmp (c.jmp))
1304 return (*hfun) (c.val);
1306 c.next = catchlist;
1307 catchlist = &c;
1308 h.handler = handlers;
1309 h.var = Qnil;
1310 h.next = handlerlist;
1311 h.tag = &c;
1312 handlerlist = &h;
1314 val = (*bfun) ();
1315 catchlist = c.next;
1316 handlerlist = h.next;
1317 return val;
1320 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1322 Lisp_Object
1323 internal_condition_case_1 (bfun, arg, handlers, hfun)
1324 Lisp_Object (*bfun) ();
1325 Lisp_Object arg;
1326 Lisp_Object handlers;
1327 Lisp_Object (*hfun) ();
1329 Lisp_Object val;
1330 struct catchtag c;
1331 struct handler h;
1333 c.tag = Qnil;
1334 c.val = Qnil;
1335 c.backlist = backtrace_list;
1336 c.handlerlist = handlerlist;
1337 c.lisp_eval_depth = lisp_eval_depth;
1338 c.pdlcount = specpdl_ptr - specpdl;
1339 c.poll_suppress_count = poll_suppress_count;
1340 c.gcpro = gcprolist;
1341 c.byte_stack = byte_stack_list;
1342 if (_setjmp (c.jmp))
1344 return (*hfun) (c.val);
1346 c.next = catchlist;
1347 catchlist = &c;
1348 h.handler = handlers;
1349 h.var = Qnil;
1350 h.next = handlerlist;
1351 h.tag = &c;
1352 handlerlist = &h;
1354 val = (*bfun) (arg);
1355 catchlist = c.next;
1356 handlerlist = h.next;
1357 return val;
1361 /* Like internal_condition_case but call HFUN with NARGS as first,
1362 and ARGS as second argument. */
1364 Lisp_Object
1365 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1366 Lisp_Object (*bfun) ();
1367 int nargs;
1368 Lisp_Object *args;
1369 Lisp_Object handlers;
1370 Lisp_Object (*hfun) ();
1372 Lisp_Object val;
1373 struct catchtag c;
1374 struct handler h;
1376 c.tag = Qnil;
1377 c.val = Qnil;
1378 c.backlist = backtrace_list;
1379 c.handlerlist = handlerlist;
1380 c.lisp_eval_depth = lisp_eval_depth;
1381 c.pdlcount = specpdl_ptr - specpdl;
1382 c.poll_suppress_count = poll_suppress_count;
1383 c.gcpro = gcprolist;
1384 c.byte_stack = byte_stack_list;
1385 if (_setjmp (c.jmp))
1387 return (*hfun) (c.val);
1389 c.next = catchlist;
1390 catchlist = &c;
1391 h.handler = handlers;
1392 h.var = Qnil;
1393 h.next = handlerlist;
1394 h.tag = &c;
1395 handlerlist = &h;
1397 val = (*bfun) (nargs, args);
1398 catchlist = c.next;
1399 handlerlist = h.next;
1400 return val;
1404 static Lisp_Object find_handler_clause ();
1406 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1407 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1408 This function does not return.\n\n\
1409 An error symbol is a symbol with an `error-conditions' property\n\
1410 that is a list of condition names.\n\
1411 A handler for any of those names will get to handle this signal.\n\
1412 The symbol `error' should normally be one of them.\n\
1414 DATA should be a list. Its elements are printed as part of the error message.\n\
1415 If the signal is handled, DATA is made available to the handler.\n\
1416 See also the function `condition-case'.")
1417 (error_symbol, data)
1418 Lisp_Object error_symbol, data;
1420 /* When memory is full, ERROR-SYMBOL is nil,
1421 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1422 register struct handler *allhandlers = handlerlist;
1423 Lisp_Object conditions;
1424 extern int gc_in_progress;
1425 extern int waiting_for_input;
1426 Lisp_Object debugger_value;
1427 Lisp_Object string;
1428 Lisp_Object real_error_symbol;
1429 extern int display_hourglass_p;
1430 struct backtrace *bp;
1432 immediate_quit = handling_signal = 0;
1433 if (gc_in_progress || waiting_for_input)
1434 abort ();
1436 TOTALLY_UNBLOCK_INPUT;
1438 if (NILP (error_symbol))
1439 real_error_symbol = Fcar (data);
1440 else
1441 real_error_symbol = error_symbol;
1443 #ifdef HAVE_X_WINDOWS
1444 if (display_hourglass_p)
1445 cancel_hourglass ();
1446 #endif
1448 /* This hook is used by edebug. */
1449 if (! NILP (Vsignal_hook_function))
1450 call2 (Vsignal_hook_function, error_symbol, data);
1452 conditions = Fget (real_error_symbol, Qerror_conditions);
1454 /* Remember from where signal was called. Skip over the frame for
1455 `signal' itself. If a frame for `error' follows, skip that,
1456 too. */
1457 Vsignaling_function = Qnil;
1458 if (backtrace_list)
1460 bp = backtrace_list->next;
1461 if (bp && bp->function && EQ (*bp->function, Qerror))
1462 bp = bp->next;
1463 if (bp && bp->function)
1464 Vsignaling_function = *bp->function;
1467 for (; handlerlist; handlerlist = handlerlist->next)
1469 register Lisp_Object clause;
1471 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1472 max_lisp_eval_depth = lisp_eval_depth + 20;
1474 if (specpdl_size + 40 > max_specpdl_size)
1475 max_specpdl_size = specpdl_size + 40;
1477 clause = find_handler_clause (handlerlist->handler, conditions,
1478 error_symbol, data, &debugger_value);
1480 #if 0 /* Most callers are not prepared to handle gc if this returns.
1481 So, since this feature is not very useful, take it out. */
1482 /* If have called debugger and user wants to continue,
1483 just return nil. */
1484 if (EQ (clause, Qlambda))
1485 return debugger_value;
1486 #else
1487 if (EQ (clause, Qlambda))
1489 /* We can't return values to code which signaled an error, but we
1490 can continue code which has signaled a quit. */
1491 if (EQ (real_error_symbol, Qquit))
1492 return Qnil;
1493 else
1494 error ("Cannot return from the debugger in an error");
1496 #endif
1498 if (!NILP (clause))
1500 Lisp_Object unwind_data;
1501 struct handler *h = handlerlist;
1503 handlerlist = allhandlers;
1505 if (NILP (error_symbol))
1506 unwind_data = data;
1507 else
1508 unwind_data = Fcons (error_symbol, data);
1509 h->chosen_clause = clause;
1510 unwind_to_catch (h->tag, unwind_data);
1514 handlerlist = allhandlers;
1515 /* If no handler is present now, try to run the debugger,
1516 and if that fails, throw to top level. */
1517 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1518 if (catchlist != 0)
1519 Fthrow (Qtop_level, Qt);
1521 if (! NILP (error_symbol))
1522 data = Fcons (error_symbol, data);
1524 string = Ferror_message_string (data);
1525 fatal ("%s", XSTRING (string)->data, 0);
1528 /* Return nonzero iff LIST is a non-nil atom or
1529 a list containing one of CONDITIONS. */
1531 static int
1532 wants_debugger (list, conditions)
1533 Lisp_Object list, conditions;
1535 if (NILP (list))
1536 return 0;
1537 if (! CONSP (list))
1538 return 1;
1540 while (CONSP (conditions))
1542 Lisp_Object this, tail;
1543 this = XCAR (conditions);
1544 for (tail = list; CONSP (tail); tail = XCDR (tail))
1545 if (EQ (XCAR (tail), this))
1546 return 1;
1547 conditions = XCDR (conditions);
1549 return 0;
1552 /* Return 1 if an error with condition-symbols CONDITIONS,
1553 and described by SIGNAL-DATA, should skip the debugger
1554 according to debugger-ignore-errors. */
1556 static int
1557 skip_debugger (conditions, data)
1558 Lisp_Object conditions, data;
1560 Lisp_Object tail;
1561 int first_string = 1;
1562 Lisp_Object error_message;
1564 error_message = Qnil;
1565 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1567 if (STRINGP (XCAR (tail)))
1569 if (first_string)
1571 error_message = Ferror_message_string (data);
1572 first_string = 0;
1575 if (fast_string_match (XCAR (tail), error_message) >= 0)
1576 return 1;
1578 else
1580 Lisp_Object contail;
1582 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1583 if (EQ (XCAR (tail), XCAR (contail)))
1584 return 1;
1588 return 0;
1591 /* Value of Qlambda means we have called debugger and user has continued.
1592 There are two ways to pass SIG and DATA:
1593 = SIG is the error symbol, and DATA is the rest of the data.
1594 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1595 This is for memory-full errors only.
1597 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1599 static Lisp_Object
1600 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1601 Lisp_Object handlers, conditions, sig, data;
1602 Lisp_Object *debugger_value_ptr;
1604 register Lisp_Object h;
1605 register Lisp_Object tem;
1607 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1608 return Qt;
1609 /* error is used similarly, but means print an error message
1610 and run the debugger if that is enabled. */
1611 if (EQ (handlers, Qerror)
1612 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1613 there is a handler. */
1615 int count = specpdl_ptr - specpdl;
1616 int debugger_called = 0;
1617 Lisp_Object sig_symbol, combined_data;
1618 /* This is set to 1 if we are handling a memory-full error,
1619 because these must not run the debugger.
1620 (There is no room in memory to do that!) */
1621 int no_debugger = 0;
1623 if (NILP (sig))
1625 combined_data = data;
1626 sig_symbol = Fcar (data);
1627 no_debugger = 1;
1629 else
1631 combined_data = Fcons (sig, data);
1632 sig_symbol = sig;
1635 if (wants_debugger (Vstack_trace_on_error, conditions))
1637 #ifdef PROTOTYPES
1638 internal_with_output_to_temp_buffer ("*Backtrace*",
1639 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1640 Qnil);
1641 #else
1642 internal_with_output_to_temp_buffer ("*Backtrace*",
1643 Fbacktrace, Qnil);
1644 #endif
1646 if (! no_debugger
1647 && (EQ (sig_symbol, Qquit)
1648 ? debug_on_quit
1649 : wants_debugger (Vdebug_on_error, conditions))
1650 && ! skip_debugger (conditions, combined_data)
1651 && when_entered_debugger < num_nonmacro_input_events)
1653 specbind (Qdebug_on_error, Qnil);
1654 *debugger_value_ptr
1655 = call_debugger (Fcons (Qerror,
1656 Fcons (combined_data, Qnil)));
1657 debugger_called = 1;
1659 /* If there is no handler, return saying whether we ran the debugger. */
1660 if (EQ (handlers, Qerror))
1662 if (debugger_called)
1663 return unbind_to (count, Qlambda);
1664 return Qt;
1667 for (h = handlers; CONSP (h); h = Fcdr (h))
1669 Lisp_Object handler, condit;
1671 handler = Fcar (h);
1672 if (!CONSP (handler))
1673 continue;
1674 condit = Fcar (handler);
1675 /* Handle a single condition name in handler HANDLER. */
1676 if (SYMBOLP (condit))
1678 tem = Fmemq (Fcar (handler), conditions);
1679 if (!NILP (tem))
1680 return handler;
1682 /* Handle a list of condition names in handler HANDLER. */
1683 else if (CONSP (condit))
1685 while (CONSP (condit))
1687 tem = Fmemq (Fcar (condit), conditions);
1688 if (!NILP (tem))
1689 return handler;
1690 condit = XCDR (condit);
1694 return Qnil;
1697 /* dump an error message; called like printf */
1699 /* VARARGS 1 */
1700 void
1701 error (m, a1, a2, a3)
1702 char *m;
1703 char *a1, *a2, *a3;
1705 char buf[200];
1706 int size = 200;
1707 int mlen;
1708 char *buffer = buf;
1709 char *args[3];
1710 int allocated = 0;
1711 Lisp_Object string;
1713 args[0] = a1;
1714 args[1] = a2;
1715 args[2] = a3;
1717 mlen = strlen (m);
1719 while (1)
1721 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1722 if (used < size)
1723 break;
1724 size *= 2;
1725 if (allocated)
1726 buffer = (char *) xrealloc (buffer, size);
1727 else
1729 buffer = (char *) xmalloc (size);
1730 allocated = 1;
1734 string = build_string (buffer);
1735 if (allocated)
1736 xfree (buffer);
1738 Fsignal (Qerror, Fcons (string, Qnil));
1739 abort ();
1742 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1743 "T if FUNCTION makes provisions for interactive calling.\n\
1744 This means it contains a description for how to read arguments to give it.\n\
1745 The value is nil for an invalid function or a symbol with no function\n\
1746 definition.\n\
1748 Interactively callable functions include strings and vectors (treated\n\
1749 as keyboard macros), lambda-expressions that contain a top-level call\n\
1750 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1751 fourth argument, and some of the built-in functions of Lisp.\n\
1753 Also, a symbol satisfies `commandp' if its function definition does so.")
1754 (function)
1755 Lisp_Object function;
1757 register Lisp_Object fun;
1758 register Lisp_Object funcar;
1760 fun = function;
1762 fun = indirect_function (fun);
1763 if (EQ (fun, Qunbound))
1764 return Qnil;
1766 /* Emacs primitives are interactive if their DEFUN specifies an
1767 interactive spec. */
1768 if (SUBRP (fun))
1770 if (XSUBR (fun)->prompt)
1771 return Qt;
1772 else
1773 return Qnil;
1776 /* Bytecode objects are interactive if they are long enough to
1777 have an element whose index is COMPILED_INTERACTIVE, which is
1778 where the interactive spec is stored. */
1779 else if (COMPILEDP (fun))
1780 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1781 ? Qt : Qnil);
1783 /* Strings and vectors are keyboard macros. */
1784 if (STRINGP (fun) || VECTORP (fun))
1785 return Qt;
1787 /* Lists may represent commands. */
1788 if (!CONSP (fun))
1789 return Qnil;
1790 funcar = Fcar (fun);
1791 if (!SYMBOLP (funcar))
1792 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1793 if (EQ (funcar, Qlambda))
1794 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1795 if (EQ (funcar, Qmocklisp))
1796 return Qt; /* All mocklisp functions can be called interactively */
1797 if (EQ (funcar, Qautoload))
1798 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1799 else
1800 return Qnil;
1803 /* ARGSUSED */
1804 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1805 "Define FUNCTION to autoload from FILE.\n\
1806 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1807 Third arg DOCSTRING is documentation for the function.\n\
1808 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1809 Fifth arg TYPE indicates the type of the object:\n\
1810 nil or omitted says FUNCTION is a function,\n\
1811 `keymap' says FUNCTION is really a keymap, and\n\
1812 `macro' or t says FUNCTION is really a macro.\n\
1813 Third through fifth args give info about the real definition.\n\
1814 They default to nil.\n\
1815 If FUNCTION is already defined other than as an autoload,\n\
1816 this does nothing and returns nil.")
1817 (function, file, docstring, interactive, type)
1818 Lisp_Object function, file, docstring, interactive, type;
1820 #ifdef NO_ARG_ARRAY
1821 Lisp_Object args[4];
1822 #endif
1824 CHECK_SYMBOL (function, 0);
1825 CHECK_STRING (file, 1);
1827 /* If function is defined and not as an autoload, don't override */
1828 if (!EQ (XSYMBOL (function)->function, Qunbound)
1829 && !(CONSP (XSYMBOL (function)->function)
1830 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1831 return Qnil;
1833 if (NILP (Vpurify_flag))
1834 /* Only add entries after dumping, because the ones before are
1835 not useful and else we get loads of them from the loaddefs.el. */
1836 LOADHIST_ATTACH (Fcons (Qautoload, function));
1838 #ifdef NO_ARG_ARRAY
1839 args[0] = file;
1840 args[1] = docstring;
1841 args[2] = interactive;
1842 args[3] = type;
1844 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1845 #else /* NO_ARG_ARRAY */
1846 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1847 #endif /* not NO_ARG_ARRAY */
1850 Lisp_Object
1851 un_autoload (oldqueue)
1852 Lisp_Object oldqueue;
1854 register Lisp_Object queue, first, second;
1856 /* Queue to unwind is current value of Vautoload_queue.
1857 oldqueue is the shadowed value to leave in Vautoload_queue. */
1858 queue = Vautoload_queue;
1859 Vautoload_queue = oldqueue;
1860 while (CONSP (queue))
1862 first = Fcar (queue);
1863 second = Fcdr (first);
1864 first = Fcar (first);
1865 if (EQ (second, Qnil))
1866 Vfeatures = first;
1867 else
1868 Ffset (first, second);
1869 queue = Fcdr (queue);
1871 return Qnil;
1874 /* Load an autoloaded function.
1875 FUNNAME is the symbol which is the function's name.
1876 FUNDEF is the autoload definition (a list). */
1878 void
1879 do_autoload (fundef, funname)
1880 Lisp_Object fundef, funname;
1882 int count = specpdl_ptr - specpdl;
1883 Lisp_Object fun, queue, first, second;
1884 struct gcpro gcpro1, gcpro2, gcpro3;
1886 fun = funname;
1887 CHECK_SYMBOL (funname, 0);
1888 GCPRO3 (fun, funname, fundef);
1890 /* Preserve the match data. */
1891 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1893 /* Value saved here is to be restored into Vautoload_queue. */
1894 record_unwind_protect (un_autoload, Vautoload_queue);
1895 Vautoload_queue = Qt;
1896 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1898 /* Save the old autoloads, in case we ever do an unload. */
1899 queue = Vautoload_queue;
1900 while (CONSP (queue))
1902 first = Fcar (queue);
1903 second = Fcdr (first);
1904 first = Fcar (first);
1906 /* Note: This test is subtle. The cdr of an autoload-queue entry
1907 may be an atom if the autoload entry was generated by a defalias
1908 or fset. */
1909 if (CONSP (second))
1910 Fput (first, Qautoload, (Fcdr (second)));
1912 queue = Fcdr (queue);
1915 /* Once loading finishes, don't undo it. */
1916 Vautoload_queue = Qt;
1917 unbind_to (count, Qnil);
1919 fun = Findirect_function (fun);
1921 if (!NILP (Fequal (fun, fundef)))
1922 error ("Autoloading failed to define function %s",
1923 XSYMBOL (funname)->name->data);
1924 UNGCPRO;
1928 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1929 "Evaluate FORM and return its value.")
1930 (form)
1931 Lisp_Object form;
1933 Lisp_Object fun, val, original_fun, original_args;
1934 Lisp_Object funcar;
1935 struct backtrace backtrace;
1936 struct gcpro gcpro1, gcpro2, gcpro3;
1938 if (handling_signal)
1939 abort ();
1941 if (SYMBOLP (form))
1943 if (EQ (Vmocklisp_arguments, Qt))
1944 return Fsymbol_value (form);
1945 val = Fsymbol_value (form);
1946 if (NILP (val))
1947 XSETFASTINT (val, 0);
1948 else if (EQ (val, Qt))
1949 XSETFASTINT (val, 1);
1950 return val;
1952 if (!CONSP (form))
1953 return form;
1955 QUIT;
1956 if (consing_since_gc > gc_cons_threshold)
1958 GCPRO1 (form);
1959 Fgarbage_collect ();
1960 UNGCPRO;
1963 if (++lisp_eval_depth > max_lisp_eval_depth)
1965 if (max_lisp_eval_depth < 100)
1966 max_lisp_eval_depth = 100;
1967 if (lisp_eval_depth > max_lisp_eval_depth)
1968 error ("Lisp nesting exceeds max-lisp-eval-depth");
1971 original_fun = Fcar (form);
1972 original_args = Fcdr (form);
1974 backtrace.next = backtrace_list;
1975 backtrace_list = &backtrace;
1976 backtrace.function = &original_fun; /* This also protects them from gc */
1977 backtrace.args = &original_args;
1978 backtrace.nargs = UNEVALLED;
1979 backtrace.evalargs = 1;
1980 backtrace.debug_on_exit = 0;
1982 if (debug_on_next_call)
1983 do_debug_on_call (Qt);
1985 /* At this point, only original_fun and original_args
1986 have values that will be used below */
1987 retry:
1988 fun = Findirect_function (original_fun);
1990 if (SUBRP (fun))
1992 Lisp_Object numargs;
1993 Lisp_Object argvals[8];
1994 Lisp_Object args_left;
1995 register int i, maxargs;
1997 args_left = original_args;
1998 numargs = Flength (args_left);
2000 if (XINT (numargs) < XSUBR (fun)->min_args ||
2001 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2002 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2004 if (XSUBR (fun)->max_args == UNEVALLED)
2006 backtrace.evalargs = 0;
2007 val = (*XSUBR (fun)->function) (args_left);
2008 goto done;
2011 if (XSUBR (fun)->max_args == MANY)
2013 /* Pass a vector of evaluated arguments */
2014 Lisp_Object *vals;
2015 register int argnum = 0;
2017 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2019 GCPRO3 (args_left, fun, fun);
2020 gcpro3.var = vals;
2021 gcpro3.nvars = 0;
2023 while (!NILP (args_left))
2025 vals[argnum++] = Feval (Fcar (args_left));
2026 args_left = Fcdr (args_left);
2027 gcpro3.nvars = argnum;
2030 backtrace.args = vals;
2031 backtrace.nargs = XINT (numargs);
2033 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2034 UNGCPRO;
2035 goto done;
2038 GCPRO3 (args_left, fun, fun);
2039 gcpro3.var = argvals;
2040 gcpro3.nvars = 0;
2042 maxargs = XSUBR (fun)->max_args;
2043 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2045 argvals[i] = Feval (Fcar (args_left));
2046 gcpro3.nvars = ++i;
2049 UNGCPRO;
2051 backtrace.args = argvals;
2052 backtrace.nargs = XINT (numargs);
2054 switch (i)
2056 case 0:
2057 val = (*XSUBR (fun)->function) ();
2058 goto done;
2059 case 1:
2060 val = (*XSUBR (fun)->function) (argvals[0]);
2061 goto done;
2062 case 2:
2063 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2064 goto done;
2065 case 3:
2066 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2067 argvals[2]);
2068 goto done;
2069 case 4:
2070 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2071 argvals[2], argvals[3]);
2072 goto done;
2073 case 5:
2074 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2075 argvals[3], argvals[4]);
2076 goto done;
2077 case 6:
2078 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2079 argvals[3], argvals[4], argvals[5]);
2080 goto done;
2081 case 7:
2082 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2083 argvals[3], argvals[4], argvals[5],
2084 argvals[6]);
2085 goto done;
2087 case 8:
2088 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2089 argvals[3], argvals[4], argvals[5],
2090 argvals[6], argvals[7]);
2091 goto done;
2093 default:
2094 /* Someone has created a subr that takes more arguments than
2095 is supported by this code. We need to either rewrite the
2096 subr to use a different argument protocol, or add more
2097 cases to this switch. */
2098 abort ();
2101 if (COMPILEDP (fun))
2102 val = apply_lambda (fun, original_args, 1);
2103 else
2105 if (!CONSP (fun))
2106 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2107 funcar = Fcar (fun);
2108 if (!SYMBOLP (funcar))
2109 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2110 if (EQ (funcar, Qautoload))
2112 do_autoload (fun, original_fun);
2113 goto retry;
2115 if (EQ (funcar, Qmacro))
2116 val = Feval (apply1 (Fcdr (fun), original_args));
2117 else if (EQ (funcar, Qlambda))
2118 val = apply_lambda (fun, original_args, 1);
2119 else if (EQ (funcar, Qmocklisp))
2120 val = ml_apply (fun, original_args);
2121 else
2122 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2124 done:
2125 if (!EQ (Vmocklisp_arguments, Qt))
2127 if (NILP (val))
2128 XSETFASTINT (val, 0);
2129 else if (EQ (val, Qt))
2130 XSETFASTINT (val, 1);
2132 lisp_eval_depth--;
2133 if (backtrace.debug_on_exit)
2134 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2135 backtrace_list = backtrace.next;
2136 return val;
2139 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2140 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
2141 Then return the value FUNCTION returns.\n\
2142 Thus, (apply '+ 1 2 '(3 4)) returns 10.\n\
2143 usage: (apply FUNCTION &rest ARGUMENTS)")
2144 (nargs, args)
2145 int nargs;
2146 Lisp_Object *args;
2148 register int i, numargs;
2149 register Lisp_Object spread_arg;
2150 register Lisp_Object *funcall_args;
2151 Lisp_Object fun;
2152 struct gcpro gcpro1;
2154 fun = args [0];
2155 funcall_args = 0;
2156 spread_arg = args [nargs - 1];
2157 CHECK_LIST (spread_arg, nargs);
2159 numargs = XINT (Flength (spread_arg));
2161 if (numargs == 0)
2162 return Ffuncall (nargs - 1, args);
2163 else if (numargs == 1)
2165 args [nargs - 1] = XCAR (spread_arg);
2166 return Ffuncall (nargs, args);
2169 numargs += nargs - 2;
2171 fun = indirect_function (fun);
2172 if (EQ (fun, Qunbound))
2174 /* Let funcall get the error */
2175 fun = args[0];
2176 goto funcall;
2179 if (SUBRP (fun))
2181 if (numargs < XSUBR (fun)->min_args
2182 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2183 goto funcall; /* Let funcall get the error */
2184 else if (XSUBR (fun)->max_args > numargs)
2186 /* Avoid making funcall cons up a yet another new vector of arguments
2187 by explicitly supplying nil's for optional values */
2188 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2189 * sizeof (Lisp_Object));
2190 for (i = numargs; i < XSUBR (fun)->max_args;)
2191 funcall_args[++i] = Qnil;
2192 GCPRO1 (*funcall_args);
2193 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2196 funcall:
2197 /* We add 1 to numargs because funcall_args includes the
2198 function itself as well as its arguments. */
2199 if (!funcall_args)
2201 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2202 * sizeof (Lisp_Object));
2203 GCPRO1 (*funcall_args);
2204 gcpro1.nvars = 1 + numargs;
2207 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2208 /* Spread the last arg we got. Its first element goes in
2209 the slot that it used to occupy, hence this value of I. */
2210 i = nargs - 1;
2211 while (!NILP (spread_arg))
2213 funcall_args [i++] = XCAR (spread_arg);
2214 spread_arg = XCDR (spread_arg);
2217 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2220 /* Run hook variables in various ways. */
2222 enum run_hooks_condition {to_completion, until_success, until_failure};
2224 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2225 "Run each hook in HOOKS. Major mode functions use this.\n\
2226 Each argument should be a symbol, a hook variable.\n\
2227 These symbols are processed in the order specified.\n\
2228 If a hook symbol has a non-nil value, that value may be a function\n\
2229 or a list of functions to be called to run the hook.\n\
2230 If the value is a function, it is called with no arguments.\n\
2231 If it is a list, the elements are called, in order, with no arguments.\n\
2233 To make a hook variable buffer-local, use `make-local-hook',\n\
2234 not `make-local-variable'.\n\
2235 usage: (run-hooks &rest HOOKS)")
2236 (nargs, args)
2237 int nargs;
2238 Lisp_Object *args;
2240 Lisp_Object hook[1];
2241 register int i;
2243 for (i = 0; i < nargs; i++)
2245 hook[0] = args[i];
2246 run_hook_with_args (1, hook, to_completion);
2249 return Qnil;
2252 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2253 Srun_hook_with_args, 1, MANY, 0,
2254 "Run HOOK with the specified arguments ARGS.\n\
2255 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2256 value, that value may be a function or a list of functions to be\n\
2257 called to run the hook. If the value is a function, it is called with\n\
2258 the given arguments and its return value is returned. If it is a list\n\
2259 of functions, those functions are called, in order,\n\
2260 with the given arguments ARGS.\n\
2261 It is best not to depend on the value return by `run-hook-with-args',\n\
2262 as that may change.\n\
2264 To make a hook variable buffer-local, use `make-local-hook',\n\
2265 not `make-local-variable'.\n\
2266 usage: (run-hook-with-args HOOK &rest ARGS)")
2267 (nargs, args)
2268 int nargs;
2269 Lisp_Object *args;
2271 return run_hook_with_args (nargs, args, to_completion);
2274 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2275 Srun_hook_with_args_until_success, 1, MANY, 0,
2276 "Run HOOK with the specified arguments ARGS.\n\
2277 HOOK should be a symbol, a hook variable. Its value should\n\
2278 be a list of functions. We call those functions, one by one,\n\
2279 passing arguments ARGS to each of them, until one of them\n\
2280 returns a non-nil value. Then we return that value.\n\
2281 If all the functions return nil, we return nil.\n\
2283 To make a hook variable buffer-local, use `make-local-hook',\n\
2284 not `make-local-variable'.\n\
2285 usage: (run-hook-with-args-until-success HOOK &rest ARGS)")
2286 (nargs, args)
2287 int nargs;
2288 Lisp_Object *args;
2290 return run_hook_with_args (nargs, args, until_success);
2293 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2294 Srun_hook_with_args_until_failure, 1, MANY, 0,
2295 "Run HOOK with the specified arguments ARGS.\n\
2296 HOOK should be a symbol, a hook variable. Its value should\n\
2297 be a list of functions. We call those functions, one by one,\n\
2298 passing arguments ARGS to each of them, until one of them\n\
2299 returns nil. Then we return nil.\n\
2300 If all the functions return non-nil, we return non-nil.\n\
2302 To make a hook variable buffer-local, use `make-local-hook',\n\
2303 not `make-local-variable'.\n\
2304 usage: (run-hook-with-args-until-failure HOOK &rest ARGS)")
2305 (nargs, args)
2306 int nargs;
2307 Lisp_Object *args;
2309 return run_hook_with_args (nargs, args, until_failure);
2312 /* ARGS[0] should be a hook symbol.
2313 Call each of the functions in the hook value, passing each of them
2314 as arguments all the rest of ARGS (all NARGS - 1 elements).
2315 COND specifies a condition to test after each call
2316 to decide whether to stop.
2317 The caller (or its caller, etc) must gcpro all of ARGS,
2318 except that it isn't necessary to gcpro ARGS[0]. */
2320 Lisp_Object
2321 run_hook_with_args (nargs, args, cond)
2322 int nargs;
2323 Lisp_Object *args;
2324 enum run_hooks_condition cond;
2326 Lisp_Object sym, val, ret;
2327 Lisp_Object globals;
2328 struct gcpro gcpro1, gcpro2, gcpro3;
2330 /* If we are dying or still initializing,
2331 don't do anything--it would probably crash if we tried. */
2332 if (NILP (Vrun_hooks))
2333 return Qnil;
2335 sym = args[0];
2336 val = find_symbol_value (sym);
2337 ret = (cond == until_failure ? Qt : Qnil);
2339 if (EQ (val, Qunbound) || NILP (val))
2340 return ret;
2341 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2343 args[0] = val;
2344 return Ffuncall (nargs, args);
2346 else
2348 globals = Qnil;
2349 GCPRO3 (sym, val, globals);
2351 for (;
2352 CONSP (val) && ((cond == to_completion)
2353 || (cond == until_success ? NILP (ret)
2354 : !NILP (ret)));
2355 val = XCDR (val))
2357 if (EQ (XCAR (val), Qt))
2359 /* t indicates this hook has a local binding;
2360 it means to run the global binding too. */
2362 for (globals = Fdefault_value (sym);
2363 CONSP (globals) && ((cond == to_completion)
2364 || (cond == until_success ? NILP (ret)
2365 : !NILP (ret)));
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 ret = Ffuncall (nargs, args);
2375 else
2377 args[0] = XCAR (val);
2378 ret = Ffuncall (nargs, args);
2382 UNGCPRO;
2383 return ret;
2387 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2388 present value of that symbol.
2389 Call each element of FUNLIST,
2390 passing each of them the rest of ARGS.
2391 The caller (or its caller, etc) must gcpro all of ARGS,
2392 except that it isn't necessary to gcpro ARGS[0]. */
2394 Lisp_Object
2395 run_hook_list_with_args (funlist, nargs, args)
2396 Lisp_Object funlist;
2397 int nargs;
2398 Lisp_Object *args;
2400 Lisp_Object sym;
2401 Lisp_Object val;
2402 Lisp_Object globals;
2403 struct gcpro gcpro1, gcpro2, gcpro3;
2405 sym = args[0];
2406 globals = Qnil;
2407 GCPRO3 (sym, val, globals);
2409 for (val = funlist; CONSP (val); val = XCDR (val))
2411 if (EQ (XCAR (val), Qt))
2413 /* t indicates this hook has a local binding;
2414 it means to run the global binding too. */
2416 for (globals = Fdefault_value (sym);
2417 CONSP (globals);
2418 globals = XCDR (globals))
2420 args[0] = XCAR (globals);
2421 /* In a global value, t should not occur. If it does, we
2422 must ignore it to avoid an endless loop. */
2423 if (!EQ (args[0], Qt))
2424 Ffuncall (nargs, args);
2427 else
2429 args[0] = XCAR (val);
2430 Ffuncall (nargs, args);
2433 UNGCPRO;
2434 return Qnil;
2437 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2439 void
2440 run_hook_with_args_2 (hook, arg1, arg2)
2441 Lisp_Object hook, arg1, arg2;
2443 Lisp_Object temp[3];
2444 temp[0] = hook;
2445 temp[1] = arg1;
2446 temp[2] = arg2;
2448 Frun_hook_with_args (3, temp);
2451 /* Apply fn to arg */
2452 Lisp_Object
2453 apply1 (fn, arg)
2454 Lisp_Object fn, arg;
2456 struct gcpro gcpro1;
2458 GCPRO1 (fn);
2459 if (NILP (arg))
2460 RETURN_UNGCPRO (Ffuncall (1, &fn));
2461 gcpro1.nvars = 2;
2462 #ifdef NO_ARG_ARRAY
2464 Lisp_Object args[2];
2465 args[0] = fn;
2466 args[1] = arg;
2467 gcpro1.var = args;
2468 RETURN_UNGCPRO (Fapply (2, args));
2470 #else /* not NO_ARG_ARRAY */
2471 RETURN_UNGCPRO (Fapply (2, &fn));
2472 #endif /* not NO_ARG_ARRAY */
2475 /* Call function fn on no arguments */
2476 Lisp_Object
2477 call0 (fn)
2478 Lisp_Object fn;
2480 struct gcpro gcpro1;
2482 GCPRO1 (fn);
2483 RETURN_UNGCPRO (Ffuncall (1, &fn));
2486 /* Call function fn with 1 argument arg1 */
2487 /* ARGSUSED */
2488 Lisp_Object
2489 call1 (fn, arg1)
2490 Lisp_Object fn, arg1;
2492 struct gcpro gcpro1;
2493 #ifdef NO_ARG_ARRAY
2494 Lisp_Object args[2];
2496 args[0] = fn;
2497 args[1] = arg1;
2498 GCPRO1 (args[0]);
2499 gcpro1.nvars = 2;
2500 RETURN_UNGCPRO (Ffuncall (2, args));
2501 #else /* not NO_ARG_ARRAY */
2502 GCPRO1 (fn);
2503 gcpro1.nvars = 2;
2504 RETURN_UNGCPRO (Ffuncall (2, &fn));
2505 #endif /* not NO_ARG_ARRAY */
2508 /* Call function fn with 2 arguments arg1, arg2 */
2509 /* ARGSUSED */
2510 Lisp_Object
2511 call2 (fn, arg1, arg2)
2512 Lisp_Object fn, arg1, arg2;
2514 struct gcpro gcpro1;
2515 #ifdef NO_ARG_ARRAY
2516 Lisp_Object args[3];
2517 args[0] = fn;
2518 args[1] = arg1;
2519 args[2] = arg2;
2520 GCPRO1 (args[0]);
2521 gcpro1.nvars = 3;
2522 RETURN_UNGCPRO (Ffuncall (3, args));
2523 #else /* not NO_ARG_ARRAY */
2524 GCPRO1 (fn);
2525 gcpro1.nvars = 3;
2526 RETURN_UNGCPRO (Ffuncall (3, &fn));
2527 #endif /* not NO_ARG_ARRAY */
2530 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2531 /* ARGSUSED */
2532 Lisp_Object
2533 call3 (fn, arg1, arg2, arg3)
2534 Lisp_Object fn, arg1, arg2, arg3;
2536 struct gcpro gcpro1;
2537 #ifdef NO_ARG_ARRAY
2538 Lisp_Object args[4];
2539 args[0] = fn;
2540 args[1] = arg1;
2541 args[2] = arg2;
2542 args[3] = arg3;
2543 GCPRO1 (args[0]);
2544 gcpro1.nvars = 4;
2545 RETURN_UNGCPRO (Ffuncall (4, args));
2546 #else /* not NO_ARG_ARRAY */
2547 GCPRO1 (fn);
2548 gcpro1.nvars = 4;
2549 RETURN_UNGCPRO (Ffuncall (4, &fn));
2550 #endif /* not NO_ARG_ARRAY */
2553 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2554 /* ARGSUSED */
2555 Lisp_Object
2556 call4 (fn, arg1, arg2, arg3, arg4)
2557 Lisp_Object fn, arg1, arg2, arg3, arg4;
2559 struct gcpro gcpro1;
2560 #ifdef NO_ARG_ARRAY
2561 Lisp_Object args[5];
2562 args[0] = fn;
2563 args[1] = arg1;
2564 args[2] = arg2;
2565 args[3] = arg3;
2566 args[4] = arg4;
2567 GCPRO1 (args[0]);
2568 gcpro1.nvars = 5;
2569 RETURN_UNGCPRO (Ffuncall (5, args));
2570 #else /* not NO_ARG_ARRAY */
2571 GCPRO1 (fn);
2572 gcpro1.nvars = 5;
2573 RETURN_UNGCPRO (Ffuncall (5, &fn));
2574 #endif /* not NO_ARG_ARRAY */
2577 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2578 /* ARGSUSED */
2579 Lisp_Object
2580 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2581 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2583 struct gcpro gcpro1;
2584 #ifdef NO_ARG_ARRAY
2585 Lisp_Object args[6];
2586 args[0] = fn;
2587 args[1] = arg1;
2588 args[2] = arg2;
2589 args[3] = arg3;
2590 args[4] = arg4;
2591 args[5] = arg5;
2592 GCPRO1 (args[0]);
2593 gcpro1.nvars = 6;
2594 RETURN_UNGCPRO (Ffuncall (6, args));
2595 #else /* not NO_ARG_ARRAY */
2596 GCPRO1 (fn);
2597 gcpro1.nvars = 6;
2598 RETURN_UNGCPRO (Ffuncall (6, &fn));
2599 #endif /* not NO_ARG_ARRAY */
2602 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2603 /* ARGSUSED */
2604 Lisp_Object
2605 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2606 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2608 struct gcpro gcpro1;
2609 #ifdef NO_ARG_ARRAY
2610 Lisp_Object args[7];
2611 args[0] = fn;
2612 args[1] = arg1;
2613 args[2] = arg2;
2614 args[3] = arg3;
2615 args[4] = arg4;
2616 args[5] = arg5;
2617 args[6] = arg6;
2618 GCPRO1 (args[0]);
2619 gcpro1.nvars = 7;
2620 RETURN_UNGCPRO (Ffuncall (7, args));
2621 #else /* not NO_ARG_ARRAY */
2622 GCPRO1 (fn);
2623 gcpro1.nvars = 7;
2624 RETURN_UNGCPRO (Ffuncall (7, &fn));
2625 #endif /* not NO_ARG_ARRAY */
2628 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2629 "Call first argument as a function, passing remaining arguments to it.\n\
2630 Return the value that function returns.\n\
2631 Thus, (funcall 'cons 'x 'y) returns (x . y).\n\
2632 usage: (funcall FUNCTION &rest ARGUMENTS)")
2633 (nargs, args)
2634 int nargs;
2635 Lisp_Object *args;
2637 Lisp_Object fun;
2638 Lisp_Object funcar;
2639 int numargs = nargs - 1;
2640 Lisp_Object lisp_numargs;
2641 Lisp_Object val;
2642 struct backtrace backtrace;
2643 register Lisp_Object *internal_args;
2644 register int i;
2646 QUIT;
2647 if (consing_since_gc > gc_cons_threshold)
2648 Fgarbage_collect ();
2650 if (++lisp_eval_depth > max_lisp_eval_depth)
2652 if (max_lisp_eval_depth < 100)
2653 max_lisp_eval_depth = 100;
2654 if (lisp_eval_depth > max_lisp_eval_depth)
2655 error ("Lisp nesting exceeds max-lisp-eval-depth");
2658 backtrace.next = backtrace_list;
2659 backtrace_list = &backtrace;
2660 backtrace.function = &args[0];
2661 backtrace.args = &args[1];
2662 backtrace.nargs = nargs - 1;
2663 backtrace.evalargs = 0;
2664 backtrace.debug_on_exit = 0;
2666 if (debug_on_next_call)
2667 do_debug_on_call (Qlambda);
2669 retry:
2671 fun = args[0];
2673 fun = Findirect_function (fun);
2675 if (SUBRP (fun))
2677 if (numargs < XSUBR (fun)->min_args
2678 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2680 XSETFASTINT (lisp_numargs, numargs);
2681 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2684 if (XSUBR (fun)->max_args == UNEVALLED)
2685 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2687 if (XSUBR (fun)->max_args == MANY)
2689 val = (*XSUBR (fun)->function) (numargs, args + 1);
2690 goto done;
2693 if (XSUBR (fun)->max_args > numargs)
2695 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2696 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2697 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2698 internal_args[i] = Qnil;
2700 else
2701 internal_args = args + 1;
2702 switch (XSUBR (fun)->max_args)
2704 case 0:
2705 val = (*XSUBR (fun)->function) ();
2706 goto done;
2707 case 1:
2708 val = (*XSUBR (fun)->function) (internal_args[0]);
2709 goto done;
2710 case 2:
2711 val = (*XSUBR (fun)->function) (internal_args[0],
2712 internal_args[1]);
2713 goto done;
2714 case 3:
2715 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2716 internal_args[2]);
2717 goto done;
2718 case 4:
2719 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2720 internal_args[2],
2721 internal_args[3]);
2722 goto done;
2723 case 5:
2724 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2725 internal_args[2], internal_args[3],
2726 internal_args[4]);
2727 goto done;
2728 case 6:
2729 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2730 internal_args[2], internal_args[3],
2731 internal_args[4], internal_args[5]);
2732 goto done;
2733 case 7:
2734 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2735 internal_args[2], internal_args[3],
2736 internal_args[4], internal_args[5],
2737 internal_args[6]);
2738 goto done;
2740 case 8:
2741 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2742 internal_args[2], internal_args[3],
2743 internal_args[4], internal_args[5],
2744 internal_args[6], internal_args[7]);
2745 goto done;
2747 default:
2749 /* If a subr takes more than 8 arguments without using MANY
2750 or UNEVALLED, we need to extend this function to support it.
2751 Until this is done, there is no way to call the function. */
2752 abort ();
2755 if (COMPILEDP (fun))
2756 val = funcall_lambda (fun, numargs, args + 1);
2757 else
2759 if (!CONSP (fun))
2760 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2761 funcar = Fcar (fun);
2762 if (!SYMBOLP (funcar))
2763 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2764 if (EQ (funcar, Qlambda))
2765 val = funcall_lambda (fun, numargs, args + 1);
2766 else if (EQ (funcar, Qmocklisp))
2767 val = ml_apply (fun, Flist (numargs, args + 1));
2768 else if (EQ (funcar, Qautoload))
2770 do_autoload (fun, args[0]);
2771 goto retry;
2773 else
2774 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2776 done:
2777 lisp_eval_depth--;
2778 if (backtrace.debug_on_exit)
2779 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2780 backtrace_list = backtrace.next;
2781 return val;
2784 Lisp_Object
2785 apply_lambda (fun, args, eval_flag)
2786 Lisp_Object fun, args;
2787 int eval_flag;
2789 Lisp_Object args_left;
2790 Lisp_Object numargs;
2791 register Lisp_Object *arg_vector;
2792 struct gcpro gcpro1, gcpro2, gcpro3;
2793 register int i;
2794 register Lisp_Object tem;
2796 numargs = Flength (args);
2797 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2798 args_left = args;
2800 GCPRO3 (*arg_vector, args_left, fun);
2801 gcpro1.nvars = 0;
2803 for (i = 0; i < XINT (numargs);)
2805 tem = Fcar (args_left), args_left = Fcdr (args_left);
2806 if (eval_flag) tem = Feval (tem);
2807 arg_vector[i++] = tem;
2808 gcpro1.nvars = i;
2811 UNGCPRO;
2813 if (eval_flag)
2815 backtrace_list->args = arg_vector;
2816 backtrace_list->nargs = i;
2818 backtrace_list->evalargs = 0;
2819 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2821 /* Do the debug-on-exit now, while arg_vector still exists. */
2822 if (backtrace_list->debug_on_exit)
2823 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2824 /* Don't do it again when we return to eval. */
2825 backtrace_list->debug_on_exit = 0;
2826 return tem;
2829 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2830 and return the result of evaluation.
2831 FUN must be either a lambda-expression or a compiled-code object. */
2833 Lisp_Object
2834 funcall_lambda (fun, nargs, arg_vector)
2835 Lisp_Object fun;
2836 int nargs;
2837 register Lisp_Object *arg_vector;
2839 Lisp_Object val, syms_left, next;
2840 int count = specpdl_ptr - specpdl;
2841 int i, optional, rest;
2843 if (NILP (Vmocklisp_arguments))
2844 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2846 if (CONSP (fun))
2848 syms_left = XCDR (fun);
2849 if (CONSP (syms_left))
2850 syms_left = XCAR (syms_left);
2851 else
2852 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2854 else if (COMPILEDP (fun))
2855 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2856 else
2857 abort ();
2859 i = optional = rest = 0;
2860 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2862 QUIT;
2864 next = XCAR (syms_left);
2865 while (!SYMBOLP (next))
2866 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2868 if (EQ (next, Qand_rest))
2869 rest = 1;
2870 else if (EQ (next, Qand_optional))
2871 optional = 1;
2872 else if (rest)
2874 specbind (next, Flist (nargs - i, &arg_vector[i]));
2875 i = nargs;
2877 else if (i < nargs)
2878 specbind (next, arg_vector[i++]);
2879 else if (!optional)
2880 return Fsignal (Qwrong_number_of_arguments,
2881 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2882 else
2883 specbind (next, Qnil);
2886 if (!NILP (syms_left))
2887 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2888 else if (i < nargs)
2889 return Fsignal (Qwrong_number_of_arguments,
2890 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2892 if (CONSP (fun))
2893 val = Fprogn (XCDR (XCDR (fun)));
2894 else
2896 /* If we have not actually read the bytecode string
2897 and constants vector yet, fetch them from the file. */
2898 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2899 Ffetch_bytecode (fun);
2900 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2901 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2902 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2905 return unbind_to (count, val);
2908 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2909 1, 1, 0,
2910 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2911 (object)
2912 Lisp_Object object;
2914 Lisp_Object tem;
2916 if (COMPILEDP (object)
2917 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2919 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2920 if (!CONSP (tem))
2921 error ("invalid byte code");
2922 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2923 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2925 return object;
2928 void
2929 grow_specpdl ()
2931 register int count = specpdl_ptr - specpdl;
2932 if (specpdl_size >= max_specpdl_size)
2934 if (max_specpdl_size < 400)
2935 max_specpdl_size = 400;
2936 if (specpdl_size >= max_specpdl_size)
2938 if (!NILP (Vdebug_on_error))
2939 /* Leave room for some specpdl in the debugger. */
2940 max_specpdl_size = specpdl_size + 100;
2941 Fsignal (Qerror,
2942 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2945 specpdl_size *= 2;
2946 if (specpdl_size > max_specpdl_size)
2947 specpdl_size = max_specpdl_size;
2948 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2949 specpdl_ptr = specpdl + count;
2952 void
2953 specbind (symbol, value)
2954 Lisp_Object symbol, value;
2956 Lisp_Object ovalue;
2957 Lisp_Object valcontents;
2959 CHECK_SYMBOL (symbol, 0);
2960 if (specpdl_ptr == specpdl + specpdl_size)
2961 grow_specpdl ();
2963 /* The most common case is that of a non-constant symbol with a
2964 trivial value. Make that as fast as we can. */
2965 valcontents = SYMBOL_VALUE (symbol);
2966 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
2968 specpdl_ptr->symbol = symbol;
2969 specpdl_ptr->old_value = valcontents;
2970 specpdl_ptr->func = NULL;
2971 ++specpdl_ptr;
2972 SET_SYMBOL_VALUE (symbol, value);
2974 else
2976 Lisp_Object valcontents;
2978 ovalue = find_symbol_value (symbol);
2979 specpdl_ptr->func = 0;
2980 specpdl_ptr->old_value = ovalue;
2982 valcontents = XSYMBOL (symbol)->value;
2984 if (BUFFER_LOCAL_VALUEP (valcontents)
2985 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
2986 || BUFFER_OBJFWDP (valcontents))
2988 Lisp_Object where, current_buffer;
2990 current_buffer = Fcurrent_buffer ();
2992 /* For a local variable, record both the symbol and which
2993 buffer's or frame's value we are saving. */
2994 if (!NILP (Flocal_variable_p (symbol, Qnil)))
2995 where = current_buffer;
2996 else if (!BUFFER_OBJFWDP (valcontents)
2997 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
2998 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
2999 else
3000 where = Qnil;
3002 /* We're not using the `unused' slot in the specbinding
3003 structure because this would mean we have to do more
3004 work for simple variables. */
3005 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3007 /* If SYMBOL is a per-buffer variable which doesn't have a
3008 buffer-local value here, make the `let' change the global
3009 value by changing the value of SYMBOL in all buffers not
3010 having their own value. This is consistent with what
3011 happens with other buffer-local variables. */
3012 if (NILP (where)
3013 && BUFFER_OBJFWDP (valcontents))
3015 ++specpdl_ptr;
3016 Fset_default (symbol, value);
3017 return;
3020 else
3021 specpdl_ptr->symbol = symbol;
3023 specpdl_ptr++;
3024 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3025 store_symval_forwarding (symbol, ovalue, value, NULL);
3026 else
3027 set_internal (symbol, value, 0, 1);
3031 void
3032 record_unwind_protect (function, arg)
3033 Lisp_Object (*function) P_ ((Lisp_Object));
3034 Lisp_Object arg;
3036 if (specpdl_ptr == specpdl + specpdl_size)
3037 grow_specpdl ();
3038 specpdl_ptr->func = function;
3039 specpdl_ptr->symbol = Qnil;
3040 specpdl_ptr->old_value = arg;
3041 specpdl_ptr++;
3044 Lisp_Object
3045 unbind_to (count, value)
3046 int count;
3047 Lisp_Object value;
3049 int quitf = !NILP (Vquit_flag);
3050 struct gcpro gcpro1;
3052 GCPRO1 (value);
3053 Vquit_flag = Qnil;
3055 while (specpdl_ptr != specpdl + count)
3057 --specpdl_ptr;
3059 if (specpdl_ptr->func != 0)
3060 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3061 /* Note that a "binding" of nil is really an unwind protect,
3062 so in that case the "old value" is a list of forms to evaluate. */
3063 else if (NILP (specpdl_ptr->symbol))
3064 Fprogn (specpdl_ptr->old_value);
3065 /* If the symbol is a list, it is really (SYMBOL WHERE
3066 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3067 frame. If WHERE is a buffer or frame, this indicates we
3068 bound a variable that had a buffer-local or frmae-local
3069 binding.. WHERE nil means that the variable had the default
3070 value when it was bound. CURRENT-BUFFER is the buffer that
3071 was current when the variable was bound. */
3072 else if (CONSP (specpdl_ptr->symbol))
3074 Lisp_Object symbol, where;
3076 symbol = XCAR (specpdl_ptr->symbol);
3077 where = XCAR (XCDR (specpdl_ptr->symbol));
3079 if (NILP (where))
3080 Fset_default (symbol, specpdl_ptr->old_value);
3081 else if (BUFFERP (where))
3082 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (where), 1);
3083 else
3084 set_internal (symbol, specpdl_ptr->old_value, NULL, 1);
3086 else
3088 /* If variable has a trivial value (no forwarding), we can
3089 just set it. No need to check for constant symbols here,
3090 since that was already done by specbind. */
3091 if (!MISCP (SYMBOL_VALUE (specpdl_ptr->symbol)))
3092 SET_SYMBOL_VALUE (specpdl_ptr->symbol, specpdl_ptr->old_value);
3093 else
3094 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3098 if (NILP (Vquit_flag) && quitf)
3099 Vquit_flag = Qt;
3101 UNGCPRO;
3102 return value;
3105 #if 0
3107 /* Get the value of symbol's global binding, even if that binding
3108 is not now dynamically visible. */
3110 Lisp_Object
3111 top_level_value (symbol)
3112 Lisp_Object symbol;
3114 register struct specbinding *ptr = specpdl;
3116 CHECK_SYMBOL (symbol, 0);
3117 for (; ptr != specpdl_ptr; ptr++)
3119 if (EQ (ptr->symbol, symbol))
3120 return ptr->old_value;
3122 return Fsymbol_value (symbol);
3125 Lisp_Object
3126 top_level_set (symbol, newval)
3127 Lisp_Object symbol, newval;
3129 register struct specbinding *ptr = specpdl;
3131 CHECK_SYMBOL (symbol, 0);
3132 for (; ptr != specpdl_ptr; ptr++)
3134 if (EQ (ptr->symbol, symbol))
3136 ptr->old_value = newval;
3137 return newval;
3140 return Fset (symbol, newval);
3143 #endif /* 0 */
3145 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3146 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
3147 The debugger is entered when that frame exits, if the flag is non-nil.")
3148 (level, flag)
3149 Lisp_Object level, flag;
3151 register struct backtrace *backlist = backtrace_list;
3152 register int i;
3154 CHECK_NUMBER (level, 0);
3156 for (i = 0; backlist && i < XINT (level); i++)
3158 backlist = backlist->next;
3161 if (backlist)
3162 backlist->debug_on_exit = !NILP (flag);
3164 return flag;
3167 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3168 "Print a trace of Lisp function calls currently active.\n\
3169 Output stream used is value of `standard-output'.")
3172 register struct backtrace *backlist = backtrace_list;
3173 register int i;
3174 Lisp_Object tail;
3175 Lisp_Object tem;
3176 extern Lisp_Object Vprint_level;
3177 struct gcpro gcpro1;
3179 XSETFASTINT (Vprint_level, 3);
3181 tail = Qnil;
3182 GCPRO1 (tail);
3184 while (backlist)
3186 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3187 if (backlist->nargs == UNEVALLED)
3189 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3190 write_string ("\n", -1);
3192 else
3194 tem = *backlist->function;
3195 Fprin1 (tem, Qnil); /* This can QUIT */
3196 write_string ("(", -1);
3197 if (backlist->nargs == MANY)
3199 for (tail = *backlist->args, i = 0;
3200 !NILP (tail);
3201 tail = Fcdr (tail), i++)
3203 if (i) write_string (" ", -1);
3204 Fprin1 (Fcar (tail), Qnil);
3207 else
3209 for (i = 0; i < backlist->nargs; i++)
3211 if (i) write_string (" ", -1);
3212 Fprin1 (backlist->args[i], Qnil);
3215 write_string (")\n", -1);
3217 backlist = backlist->next;
3220 Vprint_level = Qnil;
3221 UNGCPRO;
3222 return Qnil;
3225 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3226 "Return the function and arguments NFRAMES up from current execution point.\n\
3227 If that frame has not evaluated the arguments yet (or is a special form),\n\
3228 the value is (nil FUNCTION ARG-FORMS...).\n\
3229 If that frame has evaluated its arguments and called its function already,\n\
3230 the value is (t FUNCTION ARG-VALUES...).\n\
3231 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3232 FUNCTION is whatever was supplied as car of evaluated list,\n\
3233 or a lambda expression for macro calls.\n\
3234 If NFRAMES is more than the number of frames, the value is nil.")
3235 (nframes)
3236 Lisp_Object nframes;
3238 register struct backtrace *backlist = backtrace_list;
3239 register int i;
3240 Lisp_Object tem;
3242 CHECK_NATNUM (nframes, 0);
3244 /* Find the frame requested. */
3245 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3246 backlist = backlist->next;
3248 if (!backlist)
3249 return Qnil;
3250 if (backlist->nargs == UNEVALLED)
3251 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3252 else
3254 if (backlist->nargs == MANY)
3255 tem = *backlist->args;
3256 else
3257 tem = Flist (backlist->nargs, backlist->args);
3259 return Fcons (Qt, Fcons (*backlist->function, tem));
3264 void
3265 syms_of_eval ()
3267 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3268 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3269 If Lisp code tries to make more than this many at once,\n\
3270 an error is signaled.");
3272 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3273 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
3274 This limit is to catch infinite recursions for you before they cause\n\
3275 actual stack overflow in C, which would be fatal for Emacs.\n\
3276 You can safely make it considerably larger than its default value,\n\
3277 if that proves inconveniently small.");
3279 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3280 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
3281 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
3282 Vquit_flag = Qnil;
3284 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3285 "Non-nil inhibits C-g quitting from happening immediately.\n\
3286 Note that `quit-flag' will still be set by typing C-g,\n\
3287 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
3288 To prevent this happening, set `quit-flag' to nil\n\
3289 before making `inhibit-quit' nil.");
3290 Vinhibit_quit = Qnil;
3292 Qinhibit_quit = intern ("inhibit-quit");
3293 staticpro (&Qinhibit_quit);
3295 Qautoload = intern ("autoload");
3296 staticpro (&Qautoload);
3298 Qdebug_on_error = intern ("debug-on-error");
3299 staticpro (&Qdebug_on_error);
3301 Qmacro = intern ("macro");
3302 staticpro (&Qmacro);
3304 /* Note that the process handling also uses Qexit, but we don't want
3305 to staticpro it twice, so we just do it here. */
3306 Qexit = intern ("exit");
3307 staticpro (&Qexit);
3309 Qinteractive = intern ("interactive");
3310 staticpro (&Qinteractive);
3312 Qcommandp = intern ("commandp");
3313 staticpro (&Qcommandp);
3315 Qdefun = intern ("defun");
3316 staticpro (&Qdefun);
3318 Qand_rest = intern ("&rest");
3319 staticpro (&Qand_rest);
3321 Qand_optional = intern ("&optional");
3322 staticpro (&Qand_optional);
3324 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3325 "*Non-nil means automatically display a backtrace buffer\n\
3326 after any error that is handled by the editor command loop.\n\
3327 If the value is a list, an error only means to display a backtrace\n\
3328 if one of its condition symbols appears in the list.");
3329 Vstack_trace_on_error = Qnil;
3331 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3332 "*Non-nil means enter debugger if an error is signaled.\n\
3333 Does not apply to errors handled by `condition-case' or those\n\
3334 matched by `debug-ignored-errors'.\n\
3335 If the value is a list, an error only means to enter the debugger\n\
3336 if one of its condition symbols appears in the list.\n\
3337 When you evaluate an expression interactively, this variable\n\
3338 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.\n\
3339 See also variable `debug-on-quit'.");
3340 Vdebug_on_error = Qnil;
3342 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3343 "*List of errors for which the debugger should not be called.\n\
3344 Each element may be a condition-name or a regexp that matches error messages.\n\
3345 If any element applies to a given error, that error skips the debugger\n\
3346 and just returns to top level.\n\
3347 This overrides the variable `debug-on-error'.\n\
3348 It does not apply to errors handled by `condition-case'.");
3349 Vdebug_ignored_errors = Qnil;
3351 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3352 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3353 Does not apply if quit is handled by a `condition-case'.\n\
3354 When you evaluate an expression interactively, this variable\n\
3355 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil.");
3356 debug_on_quit = 0;
3358 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3359 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3361 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3362 "Non-nil means debugger may continue execution.\n\
3363 This is nil when the debugger is called under circumstances where it\n\
3364 might not be safe to continue.");
3365 debugger_may_continue = 1;
3367 DEFVAR_LISP ("debugger", &Vdebugger,
3368 "Function to call to invoke debugger.\n\
3369 If due to frame exit, args are `exit' and the value being returned;\n\
3370 this function's value will be returned instead of that.\n\
3371 If due to error, args are `error' and a list of the args to `signal'.\n\
3372 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3373 If due to `eval' entry, one arg, t.");
3374 Vdebugger = Qnil;
3376 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3377 "If non-nil, this is a function for `signal' to call.\n\
3378 It receives the same arguments that `signal' was given.\n\
3379 The Edebug package uses this to regain control.");
3380 Vsignal_hook_function = Qnil;
3382 Qmocklisp_arguments = intern ("mocklisp-arguments");
3383 staticpro (&Qmocklisp_arguments);
3384 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3385 "While in a mocklisp function, the list of its unevaluated args.");
3386 Vmocklisp_arguments = Qt;
3388 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3389 "*Non-nil means call the debugger regardless of condition handlers.\n\
3390 Note that `debug-on-error', `debug-on-quit' and friends\n\
3391 still determine whether to handle the particular condition.");
3392 Vdebug_on_signal = Qnil;
3394 Vrun_hooks = intern ("run-hooks");
3395 staticpro (&Vrun_hooks);
3397 staticpro (&Vautoload_queue);
3398 Vautoload_queue = Qnil;
3399 staticpro (&Vsignaling_function);
3400 Vsignaling_function = Qnil;
3402 defsubr (&Sor);
3403 defsubr (&Sand);
3404 defsubr (&Sif);
3405 defsubr (&Scond);
3406 defsubr (&Sprogn);
3407 defsubr (&Sprog1);
3408 defsubr (&Sprog2);
3409 defsubr (&Ssetq);
3410 defsubr (&Squote);
3411 defsubr (&Sfunction);
3412 defsubr (&Sdefun);
3413 defsubr (&Sdefmacro);
3414 defsubr (&Sdefvar);
3415 defsubr (&Sdefvaralias);
3416 defsubr (&Sdefconst);
3417 defsubr (&Suser_variable_p);
3418 defsubr (&Slet);
3419 defsubr (&SletX);
3420 defsubr (&Swhile);
3421 defsubr (&Smacroexpand);
3422 defsubr (&Scatch);
3423 defsubr (&Sthrow);
3424 defsubr (&Sunwind_protect);
3425 defsubr (&Scondition_case);
3426 defsubr (&Ssignal);
3427 defsubr (&Sinteractive_p);
3428 defsubr (&Scommandp);
3429 defsubr (&Sautoload);
3430 defsubr (&Seval);
3431 defsubr (&Sapply);
3432 defsubr (&Sfuncall);
3433 defsubr (&Srun_hooks);
3434 defsubr (&Srun_hook_with_args);
3435 defsubr (&Srun_hook_with_args_until_success);
3436 defsubr (&Srun_hook_with_args_until_failure);
3437 defsubr (&Sfetch_bytecode);
3438 defsubr (&Sbacktrace_debug);
3439 defsubr (&Sbacktrace);
3440 defsubr (&Sbacktrace_frame);