Fix.
[emacs.git] / src / eval.c
blob373a9ad432b5d8624438ed4122fa03a9b50ef8b2
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000, 2001, 2002
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 int interrupt_input_blocked;
81 struct byte_stack *byte_stack;
84 struct catchtag *catchlist;
86 #ifdef DEBUG_GCPRO
87 /* Count levels of GCPRO to detect failure to UNGCPRO. */
88 int gcpro_level;
89 #endif
91 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun, Qdefvar;
92 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
93 Lisp_Object Qand_rest, Qand_optional;
94 Lisp_Object Qdebug_on_error;
95 Lisp_Object Qdeclare;
97 /* This holds either the symbol `run-hooks' or nil.
98 It is nil at an early stage of startup, and when Emacs
99 is shutting down. */
101 Lisp_Object Vrun_hooks;
103 /* Non-nil means record all fset's and provide's, to be undone
104 if the file being autoloaded is not fully loaded.
105 They are recorded by being consed onto the front of Vautoload_queue:
106 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
108 Lisp_Object Vautoload_queue;
110 /* Current number of specbindings allocated in specpdl. */
112 int specpdl_size;
114 /* Pointer to beginning of specpdl. */
116 struct specbinding *specpdl;
118 /* Pointer to first unused element in specpdl. */
120 struct specbinding *specpdl_ptr;
122 /* Maximum size allowed for specpdl allocation */
124 EMACS_INT max_specpdl_size;
126 /* Depth in Lisp evaluations and function calls. */
128 int lisp_eval_depth;
130 /* Maximum allowed depth in Lisp evaluations and function calls. */
132 EMACS_INT max_lisp_eval_depth;
134 /* Nonzero means enter debugger before next function call */
136 int debug_on_next_call;
138 /* Non-zero means debugger may continue. This is zero when the
139 debugger is called during redisplay, where it might not be safe to
140 continue the interrupted redisplay. */
142 int debugger_may_continue;
144 /* List of conditions (non-nil atom means all) which cause a backtrace
145 if an error is handled by the command loop's error handler. */
147 Lisp_Object Vstack_trace_on_error;
149 /* List of conditions (non-nil atom means all) which enter the debugger
150 if an error is handled by the command loop's error handler. */
152 Lisp_Object Vdebug_on_error;
154 /* List of conditions and regexps specifying error messages which
155 do not enter the debugger even if Vdebug_on_error says they should. */
157 Lisp_Object Vdebug_ignored_errors;
159 /* Non-nil means call the debugger even if the error will be handled. */
161 Lisp_Object Vdebug_on_signal;
163 /* Hook for edebug to use. */
165 Lisp_Object Vsignal_hook_function;
167 /* Nonzero means enter debugger if a quit signal
168 is handled by the command loop's error handler. */
170 int debug_on_quit;
172 /* The value of num_nonmacro_input_events as of the last time we
173 started to enter the debugger. If we decide to enter the debugger
174 again when this is still equal to num_nonmacro_input_events, then we
175 know that the debugger itself has an error, and we should just
176 signal the error instead of entering an infinite loop of debugger
177 invocations. */
179 int when_entered_debugger;
181 Lisp_Object Vdebugger;
183 /* The function from which the last `signal' was called. Set in
184 Fsignal. */
186 Lisp_Object Vsignaling_function;
188 /* Set to non-zero while processing X events. Checked in Feval to
189 make sure the Lisp interpreter isn't called from a signal handler,
190 which is unsafe because the interpreter isn't reentrant. */
192 int handling_signal;
194 /* Function to process declarations in defmacro forms. */
196 Lisp_Object Vmacro_declaration_function;
199 static Lisp_Object funcall_lambda P_ ((Lisp_Object, int, Lisp_Object*));
201 void
202 init_eval_once ()
204 specpdl_size = 50;
205 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
206 specpdl_ptr = specpdl;
207 max_specpdl_size = 600;
208 max_lisp_eval_depth = 300;
210 Vrun_hooks = Qnil;
213 void
214 init_eval ()
216 specpdl_ptr = specpdl;
217 catchlist = 0;
218 handlerlist = 0;
219 backtrace_list = 0;
220 Vquit_flag = Qnil;
221 debug_on_next_call = 0;
222 lisp_eval_depth = 0;
223 #ifdef DEBUG_GCPRO
224 gcpro_level = 0;
225 #endif
226 /* This is less than the initial value of num_nonmacro_input_events. */
227 when_entered_debugger = -1;
230 Lisp_Object
231 call_debugger (arg)
232 Lisp_Object arg;
234 int debug_while_redisplaying;
235 int count = SPECPDL_INDEX ();
236 Lisp_Object val;
238 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
239 max_lisp_eval_depth = lisp_eval_depth + 20;
241 if (specpdl_size + 40 > max_specpdl_size)
242 max_specpdl_size = specpdl_size + 40;
244 #ifdef HAVE_X_WINDOWS
245 if (display_hourglass_p)
246 cancel_hourglass ();
247 #endif
249 debug_on_next_call = 0;
250 when_entered_debugger = num_nonmacro_input_events;
252 /* Resetting redisplaying_p to 0 makes sure that debug output is
253 displayed if the debugger is invoked during redisplay. */
254 debug_while_redisplaying = redisplaying_p;
255 redisplaying_p = 0;
256 specbind (intern ("debugger-may-continue"),
257 debug_while_redisplaying ? Qnil : Qt);
258 specbind (Qinhibit_redisplay, Qnil);
260 #if 0 /* Binding this prevents execution of Lisp code during
261 redisplay, which necessarily leads to display problems. */
262 specbind (Qinhibit_eval_during_redisplay, Qt);
263 #endif
265 val = apply1 (Vdebugger, arg);
267 /* Interrupting redisplay and resuming it later is not safe under
268 all circumstances. So, when the debugger returns, abort the
269 interrupted redisplay by going back to the top-level. */
270 if (debug_while_redisplaying)
271 Ftop_level ();
273 return unbind_to (count, val);
276 void
277 do_debug_on_call (code)
278 Lisp_Object code;
280 debug_on_next_call = 0;
281 backtrace_list->debug_on_exit = 1;
282 call_debugger (Fcons (code, Qnil));
285 /* NOTE!!! Every function that can call EVAL must protect its args
286 and temporaries from garbage collection while it needs them.
287 The definition of `For' shows what you have to do. */
289 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
290 doc: /* Eval args until one of them yields non-nil, then return that value.
291 The remaining args are not evalled at all.
292 If all args return nil, return nil.
293 usage: (or CONDITIONS ...) */)
294 (args)
295 Lisp_Object args;
297 register Lisp_Object val;
298 Lisp_Object args_left;
299 struct gcpro gcpro1;
301 if (NILP(args))
302 return Qnil;
304 args_left = args;
305 GCPRO1 (args_left);
309 val = Feval (Fcar (args_left));
310 if (!NILP (val))
311 break;
312 args_left = Fcdr (args_left);
314 while (!NILP(args_left));
316 UNGCPRO;
317 return val;
320 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
321 doc: /* Eval args until one of them yields nil, then return nil.
322 The remaining args are not evalled at all.
323 If no arg yields nil, return the last arg's value.
324 usage: (and CONDITIONS ...) */)
325 (args)
326 Lisp_Object args;
328 register Lisp_Object val;
329 Lisp_Object args_left;
330 struct gcpro gcpro1;
332 if (NILP(args))
333 return Qt;
335 args_left = args;
336 GCPRO1 (args_left);
340 val = Feval (Fcar (args_left));
341 if (NILP (val))
342 break;
343 args_left = Fcdr (args_left);
345 while (!NILP(args_left));
347 UNGCPRO;
348 return val;
351 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
352 doc: /* If COND yields non-nil, do THEN, else do ELSE...
353 Returns the value of THEN or the value of the last of the ELSE's.
354 THEN must be one expression, but ELSE... can be zero or more expressions.
355 If COND yields nil, and there are no ELSE's, the value is nil.
356 usage: (if COND THEN ELSE...) */)
357 (args)
358 Lisp_Object args;
360 register Lisp_Object cond;
361 struct gcpro gcpro1;
363 GCPRO1 (args);
364 cond = Feval (Fcar (args));
365 UNGCPRO;
367 if (!NILP (cond))
368 return Feval (Fcar (Fcdr (args)));
369 return Fprogn (Fcdr (Fcdr (args)));
372 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
373 doc: /* Try each clause until one succeeds.
374 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
375 and, if the value is non-nil, this clause succeeds:
376 then the expressions in BODY are evaluated and the last one's
377 value is the value of the cond-form.
378 If no clause succeeds, cond returns nil.
379 If a clause has one element, as in (CONDITION),
380 CONDITION's value if non-nil is returned from the cond-form.
381 usage: (cond CLAUSES...) */)
382 (args)
383 Lisp_Object args;
385 register Lisp_Object clause, val;
386 struct gcpro gcpro1;
388 val = Qnil;
389 GCPRO1 (args);
390 while (!NILP (args))
392 clause = Fcar (args);
393 val = Feval (Fcar (clause));
394 if (!NILP (val))
396 if (!EQ (XCDR (clause), Qnil))
397 val = Fprogn (XCDR (clause));
398 break;
400 args = XCDR (args);
402 UNGCPRO;
404 return val;
407 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
408 doc: /* Eval BODY forms sequentially and return value of last one.
409 usage: (progn BODY ...) */)
410 (args)
411 Lisp_Object args;
413 register Lisp_Object val;
414 Lisp_Object args_left;
415 struct gcpro gcpro1;
417 if (NILP(args))
418 return Qnil;
420 args_left = args;
421 GCPRO1 (args_left);
425 val = Feval (Fcar (args_left));
426 args_left = Fcdr (args_left);
428 while (!NILP(args_left));
430 UNGCPRO;
431 return val;
434 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
435 doc: /* Eval FIRST and BODY sequentially; value from FIRST.
436 The value of FIRST is saved during the evaluation of the remaining args,
437 whose values are discarded.
438 usage: (prog1 FIRST BODY...) */)
439 (args)
440 Lisp_Object args;
442 Lisp_Object val;
443 register Lisp_Object args_left;
444 struct gcpro gcpro1, gcpro2;
445 register int argnum = 0;
447 if (NILP(args))
448 return Qnil;
450 args_left = args;
451 val = Qnil;
452 GCPRO2 (args, val);
456 if (!(argnum++))
457 val = Feval (Fcar (args_left));
458 else
459 Feval (Fcar (args_left));
460 args_left = Fcdr (args_left);
462 while (!NILP(args_left));
464 UNGCPRO;
465 return val;
468 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
469 doc: /* Eval X, Y and BODY sequentially; value from Y.
470 The value of Y is saved during the evaluation of the remaining args,
471 whose values are discarded.
472 usage: (prog2 X Y BODY...) */)
473 (args)
474 Lisp_Object args;
476 Lisp_Object val;
477 register Lisp_Object args_left;
478 struct gcpro gcpro1, gcpro2;
479 register int argnum = -1;
481 val = Qnil;
483 if (NILP (args))
484 return Qnil;
486 args_left = args;
487 val = Qnil;
488 GCPRO2 (args, val);
492 if (!(argnum++))
493 val = Feval (Fcar (args_left));
494 else
495 Feval (Fcar (args_left));
496 args_left = Fcdr (args_left);
498 while (!NILP (args_left));
500 UNGCPRO;
501 return val;
504 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
505 doc: /* Set each SYM to the value of its VAL.
506 The symbols SYM are variables; they are literal (not evaluated).
507 The values VAL are expressions; they are evaluated.
508 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
509 The second VAL is not computed until after the first SYM is set, and so on;
510 each VAL can use the new value of variables set earlier in the `setq'.
511 The return value of the `setq' form is the value of the last VAL.
512 usage: (setq SYM VAL SYM VAL ...) */)
513 (args)
514 Lisp_Object args;
516 register Lisp_Object args_left;
517 register Lisp_Object val, sym;
518 struct gcpro gcpro1;
520 if (NILP(args))
521 return Qnil;
523 args_left = args;
524 GCPRO1 (args);
528 val = Feval (Fcar (Fcdr (args_left)));
529 sym = Fcar (args_left);
530 Fset (sym, val);
531 args_left = Fcdr (Fcdr (args_left));
533 while (!NILP(args_left));
535 UNGCPRO;
536 return val;
539 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
540 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
541 usage: (quote ARG) */)
542 (args)
543 Lisp_Object args;
545 return Fcar (args);
548 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
549 doc: /* Like `quote', but preferred for objects which are functions.
550 In byte compilation, `function' causes its argument to be compiled.
551 `quote' cannot do that.
552 usage: (function ARG) */)
553 (args)
554 Lisp_Object args;
556 return Fcar (args);
560 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
561 doc: /* Return t if function in which this appears was called interactively.
562 This means that the function was called with call-interactively (which
563 includes being called as the binding of a key)
564 and input is currently coming from the keyboard (not in keyboard macro). */)
567 return interactive_p (1) ? Qt : Qnil;
571 /* Return 1 if function in which this appears was called
572 interactively. This means that the function was called with
573 call-interactively (which includes being called as the binding of
574 a key) and input is currently coming from the keyboard (not in
575 keyboard macro).
577 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
578 called is a built-in. */
581 interactive_p (exclude_subrs_p)
582 int exclude_subrs_p;
584 struct backtrace *btp;
585 Lisp_Object fun;
587 if (!INTERACTIVE)
588 return 0;
590 btp = backtrace_list;
592 /* If this isn't a byte-compiled function, there may be a frame at
593 the top for Finteractive_p. If so, skip it. */
594 fun = Findirect_function (*btp->function);
595 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
596 btp = btp->next;
598 /* If we're running an Emacs 18-style byte-compiled function, there
599 may be a frame for Fbytecode at the top level. In any version of
600 Emacs there can be Fbytecode frames for subexpressions evaluated
601 inside catch and condition-case. Skip past them.
603 If this isn't a byte-compiled function, then we may now be
604 looking at several frames for special forms. Skip past them. */
605 while (btp
606 && (EQ (*btp->function, Qbytecode)
607 || btp->nargs == UNEVALLED))
608 btp = btp->next;
610 /* btp now points at the frame of the innermost function that isn't
611 a special form, ignoring frames for Finteractive_p and/or
612 Fbytecode at the top. If this frame is for a built-in function
613 (such as load or eval-region) return nil. */
614 fun = Findirect_function (*btp->function);
615 if (exclude_subrs_p && SUBRP (fun))
616 return 0;
618 /* btp points to the frame of a Lisp function that called interactive-p.
619 Return t if that function was called interactively. */
620 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
621 return 1;
622 return 0;
626 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
627 doc: /* Define NAME as a function.
628 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
629 See also the function `interactive'.
630 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
631 (args)
632 Lisp_Object args;
634 register Lisp_Object fn_name;
635 register Lisp_Object defn;
637 fn_name = Fcar (args);
638 defn = Fcons (Qlambda, Fcdr (args));
639 if (!NILP (Vpurify_flag))
640 defn = Fpurecopy (defn);
641 if (CONSP (XSYMBOL (fn_name)->function)
642 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
643 LOADHIST_ATTACH (Fcons (Qt, fn_name));
644 Ffset (fn_name, defn);
645 LOADHIST_ATTACH (fn_name);
646 return fn_name;
649 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
650 doc: /* Define NAME as a macro.
651 The actual definition looks like
652 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
653 When the macro is called, as in (NAME ARGS...),
654 the function (lambda ARGLIST BODY...) is applied to
655 the list ARGS... as it appears in the expression,
656 and the result should be a form to be evaluated instead of the original.
658 DECL is a declaration, optional, which can specify how to indent
659 calls to this macro and how Edebug should handle it. It looks like this:
660 (declare SPECS...)
661 The elements can look like this:
662 (indent INDENT)
663 Set NAME's `lisp-indent-function' property to INDENT.
665 (edebug DEBUG)
666 Set NAME's `edebug-form-spec' property to DEBUG. (This is
667 equivalent to writing a `def-edebug-spec' for the macro.)
668 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
669 (args)
670 Lisp_Object args;
672 register Lisp_Object fn_name;
673 register Lisp_Object defn;
674 Lisp_Object lambda_list, doc, tail;
676 fn_name = Fcar (args);
677 lambda_list = Fcar (Fcdr (args));
678 tail = Fcdr (Fcdr (args));
680 doc = Qnil;
681 if (STRINGP (Fcar (tail)))
683 doc = Fcar (tail);
684 tail = Fcdr (tail);
687 while (CONSP (Fcar (tail))
688 && EQ (Fcar (Fcar (tail)), Qdeclare))
690 if (!NILP (Vmacro_declaration_function))
692 struct gcpro gcpro1;
693 GCPRO1 (args);
694 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
695 UNGCPRO;
698 tail = Fcdr (tail);
701 if (NILP (doc))
702 tail = Fcons (lambda_list, tail);
703 else
704 tail = Fcons (lambda_list, Fcons (doc, tail));
705 defn = Fcons (Qmacro, Fcons (Qlambda, tail));
707 if (!NILP (Vpurify_flag))
708 defn = Fpurecopy (defn);
709 if (CONSP (XSYMBOL (fn_name)->function)
710 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
711 LOADHIST_ATTACH (Fcons (Qt, fn_name));
712 Ffset (fn_name, defn);
713 LOADHIST_ATTACH (fn_name);
714 return fn_name;
718 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
719 doc: /* Make SYMBOL a variable alias for symbol ALIASED.
720 Setting the value of SYMBOL will subsequently set the value of ALIASED,
721 and getting the value of SYMBOL will return the value ALIASED has.
722 ALIASED nil means remove the alias; SYMBOL is unbound after that.
723 Third arg DOCSTRING, if non-nil, is documentation for SYMBOL. */)
724 (symbol, aliased, docstring)
725 Lisp_Object symbol, aliased, docstring;
727 struct Lisp_Symbol *sym;
729 CHECK_SYMBOL (symbol);
730 CHECK_SYMBOL (aliased);
732 if (SYMBOL_CONSTANT_P (symbol))
733 error ("Cannot make a constant an alias");
735 sym = XSYMBOL (symbol);
736 sym->indirect_variable = 1;
737 sym->value = aliased;
738 sym->constant = SYMBOL_CONSTANT_P (aliased);
739 LOADHIST_ATTACH (Fcons (Qdefvar, symbol));
740 if (!NILP (docstring))
741 Fput (symbol, Qvariable_documentation, docstring);
743 return aliased;
747 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
748 doc: /* Define SYMBOL as a variable.
749 You are not required to define a variable in order to use it,
750 but the definition can supply documentation and an initial value
751 in a way that tags can recognize.
753 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
754 If SYMBOL is buffer-local, its default value is what is set;
755 buffer-local values are not affected.
756 INITVALUE and DOCSTRING are optional.
757 If DOCSTRING starts with *, this variable is identified as a user option.
758 This means that M-x set-variable recognizes it.
759 See also `user-variable-p'.
760 If INITVALUE is missing, SYMBOL's value is not set.
761 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
762 (args)
763 Lisp_Object args;
765 register Lisp_Object sym, tem, tail;
767 sym = Fcar (args);
768 tail = Fcdr (args);
769 if (!NILP (Fcdr (Fcdr (tail))))
770 error ("too many arguments");
772 tem = Fdefault_boundp (sym);
773 if (!NILP (tail))
775 if (NILP (tem))
776 Fset_default (sym, Feval (Fcar (tail)));
777 tail = Fcdr (tail);
778 if (!NILP (Fcar (tail)))
780 tem = Fcar (tail);
781 if (!NILP (Vpurify_flag))
782 tem = Fpurecopy (tem);
783 Fput (sym, Qvariable_documentation, tem);
785 LOADHIST_ATTACH (Fcons (Qdefvar, sym));
787 else
788 /* Simple (defvar <var>) should not count as a definition at all.
789 It could get in the way of other definitions, and unloading this
790 package could try to make the variable unbound. */
793 return sym;
796 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
797 doc: /* Define SYMBOL as a constant variable.
798 The intent is that neither programs nor users should ever change this value.
799 Always sets the value of SYMBOL to the result of evalling INITVALUE.
800 If SYMBOL is buffer-local, its default value is what is set;
801 buffer-local values are not affected.
802 DOCSTRING is optional.
803 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
804 (args)
805 Lisp_Object args;
807 register Lisp_Object sym, tem;
809 sym = Fcar (args);
810 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
811 error ("too many arguments");
813 tem = Feval (Fcar (Fcdr (args)));
814 if (!NILP (Vpurify_flag))
815 tem = Fpurecopy (tem);
816 Fset_default (sym, tem);
817 tem = Fcar (Fcdr (Fcdr (args)));
818 if (!NILP (tem))
820 if (!NILP (Vpurify_flag))
821 tem = Fpurecopy (tem);
822 Fput (sym, Qvariable_documentation, tem);
824 LOADHIST_ATTACH (Fcons (Qdefvar, sym));
825 return sym;
828 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
829 doc: /* Returns t if VARIABLE is intended to be set and modified by users.
830 \(The alternative is a variable used internally in a Lisp program.)
831 Determined by whether the first character of the documentation
832 for the variable is `*' or if the variable is customizable (has a non-nil
833 value of `standard-value' or of `custom-autoload' on its property list). */)
834 (variable)
835 Lisp_Object variable;
837 Lisp_Object documentation;
839 if (!SYMBOLP (variable))
840 return Qnil;
842 documentation = Fget (variable, Qvariable_documentation);
843 if (INTEGERP (documentation) && XINT (documentation) < 0)
844 return Qt;
845 if (STRINGP (documentation)
846 && ((unsigned char) SREF (documentation, 0) == '*'))
847 return Qt;
848 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
849 if (CONSP (documentation)
850 && STRINGP (XCAR (documentation))
851 && INTEGERP (XCDR (documentation))
852 && XINT (XCDR (documentation)) < 0)
853 return Qt;
854 /* Customizable? See `custom-variable-p'. */
855 if ((!NILP (Fget (variable, intern ("standard-value"))))
856 || (!NILP (Fget (variable, intern ("custom-autoload")))))
857 return Qt;
858 return Qnil;
861 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
862 doc: /* Bind variables according to VARLIST then eval BODY.
863 The value of the last form in BODY is returned.
864 Each element of VARLIST is a symbol (which is bound to nil)
865 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
866 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
867 usage: (let* VARLIST BODY...) */)
868 (args)
869 Lisp_Object args;
871 Lisp_Object varlist, val, elt;
872 int count = SPECPDL_INDEX ();
873 struct gcpro gcpro1, gcpro2, gcpro3;
875 GCPRO3 (args, elt, varlist);
877 varlist = Fcar (args);
878 while (!NILP (varlist))
880 QUIT;
881 elt = Fcar (varlist);
882 if (SYMBOLP (elt))
883 specbind (elt, Qnil);
884 else if (! NILP (Fcdr (Fcdr (elt))))
885 Fsignal (Qerror,
886 Fcons (build_string ("`let' bindings can have only one value-form"),
887 elt));
888 else
890 val = Feval (Fcar (Fcdr (elt)));
891 specbind (Fcar (elt), val);
893 varlist = Fcdr (varlist);
895 UNGCPRO;
896 val = Fprogn (Fcdr (args));
897 return unbind_to (count, val);
900 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
901 doc: /* Bind variables according to VARLIST then eval BODY.
902 The value of the last form in BODY is returned.
903 Each element of VARLIST is a symbol (which is bound to nil)
904 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
905 All the VALUEFORMs are evalled before any symbols are bound.
906 usage: (let VARLIST BODY...) */)
907 (args)
908 Lisp_Object args;
910 Lisp_Object *temps, tem;
911 register Lisp_Object elt, varlist;
912 int count = SPECPDL_INDEX ();
913 register int argnum;
914 struct gcpro gcpro1, gcpro2;
916 varlist = Fcar (args);
918 /* Make space to hold the values to give the bound variables */
919 elt = Flength (varlist);
920 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
922 /* Compute the values and store them in `temps' */
924 GCPRO2 (args, *temps);
925 gcpro2.nvars = 0;
927 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
929 QUIT;
930 elt = Fcar (varlist);
931 if (SYMBOLP (elt))
932 temps [argnum++] = Qnil;
933 else if (! NILP (Fcdr (Fcdr (elt))))
934 Fsignal (Qerror,
935 Fcons (build_string ("`let' bindings can have only one value-form"),
936 elt));
937 else
938 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
939 gcpro2.nvars = argnum;
941 UNGCPRO;
943 varlist = Fcar (args);
944 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
946 elt = Fcar (varlist);
947 tem = temps[argnum++];
948 if (SYMBOLP (elt))
949 specbind (elt, tem);
950 else
951 specbind (Fcar (elt), tem);
954 elt = Fprogn (Fcdr (args));
955 return unbind_to (count, elt);
958 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
959 doc: /* If TEST yields non-nil, eval BODY... and repeat.
960 The order of execution is thus TEST, BODY, TEST, BODY and so on
961 until TEST returns nil.
962 usage: (while TEST BODY...) */)
963 (args)
964 Lisp_Object args;
966 Lisp_Object test, body;
967 struct gcpro gcpro1, gcpro2;
969 GCPRO2 (test, body);
971 test = Fcar (args);
972 body = Fcdr (args);
973 while (!NILP (Feval (test)))
975 QUIT;
976 Fprogn (body);
979 UNGCPRO;
980 return Qnil;
983 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
984 doc: /* Return result of expanding macros at top level of FORM.
985 If FORM is not a macro call, it is returned unchanged.
986 Otherwise, the macro is expanded and the expansion is considered
987 in place of FORM. When a non-macro-call results, it is returned.
989 The second optional arg ENVIRONMENT specifies an environment of macro
990 definitions to shadow the loaded ones for use in file byte-compilation. */)
991 (form, environment)
992 Lisp_Object form;
993 Lisp_Object environment;
995 /* With cleanups from Hallvard Furuseth. */
996 register Lisp_Object expander, sym, def, tem;
998 while (1)
1000 /* Come back here each time we expand a macro call,
1001 in case it expands into another macro call. */
1002 if (!CONSP (form))
1003 break;
1004 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1005 def = sym = XCAR (form);
1006 tem = Qnil;
1007 /* Trace symbols aliases to other symbols
1008 until we get a symbol that is not an alias. */
1009 while (SYMBOLP (def))
1011 QUIT;
1012 sym = def;
1013 tem = Fassq (sym, environment);
1014 if (NILP (tem))
1016 def = XSYMBOL (sym)->function;
1017 if (!EQ (def, Qunbound))
1018 continue;
1020 break;
1022 /* Right now TEM is the result from SYM in ENVIRONMENT,
1023 and if TEM is nil then DEF is SYM's function definition. */
1024 if (NILP (tem))
1026 /* SYM is not mentioned in ENVIRONMENT.
1027 Look at its function definition. */
1028 if (EQ (def, Qunbound) || !CONSP (def))
1029 /* Not defined or definition not suitable */
1030 break;
1031 if (EQ (XCAR (def), Qautoload))
1033 /* Autoloading function: will it be a macro when loaded? */
1034 tem = Fnth (make_number (4), def);
1035 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1036 /* Yes, load it and try again. */
1038 struct gcpro gcpro1;
1039 GCPRO1 (form);
1040 do_autoload (def, sym);
1041 UNGCPRO;
1042 continue;
1044 else
1045 break;
1047 else if (!EQ (XCAR (def), Qmacro))
1048 break;
1049 else expander = XCDR (def);
1051 else
1053 expander = XCDR (tem);
1054 if (NILP (expander))
1055 break;
1057 form = apply1 (expander, XCDR (form));
1059 return form;
1062 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1063 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1064 TAG is evalled to get the tag to use; it must not be nil.
1066 Then the BODY is executed.
1067 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1068 If no throw happens, `catch' returns the value of the last BODY form.
1069 If a throw happens, it specifies the value to return from `catch'.
1070 usage: (catch TAG BODY...) */)
1071 (args)
1072 Lisp_Object args;
1074 register Lisp_Object tag;
1075 struct gcpro gcpro1;
1077 GCPRO1 (args);
1078 tag = Feval (Fcar (args));
1079 UNGCPRO;
1080 return internal_catch (tag, Fprogn, Fcdr (args));
1083 /* Set up a catch, then call C function FUNC on argument ARG.
1084 FUNC should return a Lisp_Object.
1085 This is how catches are done from within C code. */
1087 Lisp_Object
1088 internal_catch (tag, func, arg)
1089 Lisp_Object tag;
1090 Lisp_Object (*func) ();
1091 Lisp_Object arg;
1093 /* This structure is made part of the chain `catchlist'. */
1094 struct catchtag c;
1096 /* Fill in the components of c, and put it on the list. */
1097 c.next = catchlist;
1098 c.tag = tag;
1099 c.val = Qnil;
1100 c.backlist = backtrace_list;
1101 c.handlerlist = handlerlist;
1102 c.lisp_eval_depth = lisp_eval_depth;
1103 c.pdlcount = SPECPDL_INDEX ();
1104 c.poll_suppress_count = poll_suppress_count;
1105 c.interrupt_input_blocked = interrupt_input_blocked;
1106 c.gcpro = gcprolist;
1107 c.byte_stack = byte_stack_list;
1108 catchlist = &c;
1110 /* Call FUNC. */
1111 if (! _setjmp (c.jmp))
1112 c.val = (*func) (arg);
1114 /* Throw works by a longjmp that comes right here. */
1115 catchlist = c.next;
1116 return c.val;
1119 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1120 jump to that CATCH, returning VALUE as the value of that catch.
1122 This is the guts Fthrow and Fsignal; they differ only in the way
1123 they choose the catch tag to throw to. A catch tag for a
1124 condition-case form has a TAG of Qnil.
1126 Before each catch is discarded, unbind all special bindings and
1127 execute all unwind-protect clauses made above that catch. Unwind
1128 the handler stack as we go, so that the proper handlers are in
1129 effect for each unwind-protect clause we run. At the end, restore
1130 some static info saved in CATCH, and longjmp to the location
1131 specified in the
1133 This is used for correct unwinding in Fthrow and Fsignal. */
1135 static void
1136 unwind_to_catch (catch, value)
1137 struct catchtag *catch;
1138 Lisp_Object value;
1140 register int last_time;
1142 /* Save the value in the tag. */
1143 catch->val = value;
1145 /* Restore the polling-suppression count. */
1146 set_poll_suppress_count (catch->poll_suppress_count);
1147 interrupt_input_blocked = catch->interrupt_input_blocked;
1151 last_time = catchlist == catch;
1153 /* Unwind the specpdl stack, and then restore the proper set of
1154 handlers. */
1155 unbind_to (catchlist->pdlcount, Qnil);
1156 handlerlist = catchlist->handlerlist;
1157 catchlist = catchlist->next;
1159 while (! last_time);
1161 byte_stack_list = catch->byte_stack;
1162 gcprolist = catch->gcpro;
1163 #ifdef DEBUG_GCPRO
1164 if (gcprolist != 0)
1165 gcpro_level = gcprolist->level + 1;
1166 else
1167 gcpro_level = 0;
1168 #endif
1169 backtrace_list = catch->backlist;
1170 lisp_eval_depth = catch->lisp_eval_depth;
1172 _longjmp (catch->jmp, 1);
1175 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1176 doc: /* Throw to the catch for TAG and return VALUE from it.
1177 Both TAG and VALUE are evalled. */)
1178 (tag, value)
1179 register Lisp_Object tag, value;
1181 register struct catchtag *c;
1183 while (1)
1185 if (!NILP (tag))
1186 for (c = catchlist; c; c = c->next)
1188 if (EQ (c->tag, tag))
1189 unwind_to_catch (c, value);
1191 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1196 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1197 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1198 If BODYFORM completes normally, its value is returned
1199 after executing the UNWINDFORMS.
1200 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1201 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1202 (args)
1203 Lisp_Object args;
1205 Lisp_Object val;
1206 int count = SPECPDL_INDEX ();
1208 record_unwind_protect (0, Fcdr (args));
1209 val = Feval (Fcar (args));
1210 return unbind_to (count, val);
1213 /* Chain of condition handlers currently in effect.
1214 The elements of this chain are contained in the stack frames
1215 of Fcondition_case and internal_condition_case.
1216 When an error is signaled (by calling Fsignal, below),
1217 this chain is searched for an element that applies. */
1219 struct handler *handlerlist;
1221 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1222 doc: /* Regain control when an error is signaled.
1223 Executes BODYFORM and returns its value if no error happens.
1224 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1225 where the BODY is made of Lisp expressions.
1227 A handler is applicable to an error
1228 if CONDITION-NAME is one of the error's condition names.
1229 If an error happens, the first applicable handler is run.
1231 The car of a handler may be a list of condition names
1232 instead of a single condition name.
1234 When a handler handles an error,
1235 control returns to the condition-case and the handler BODY... is executed
1236 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1237 VAR may be nil; then you do not get access to the signal information.
1239 The value of the last BODY form is returned from the condition-case.
1240 See also the function `signal' for more info.
1241 usage: (condition-case VAR BODYFORM HANDLERS...) */)
1242 (args)
1243 Lisp_Object args;
1245 Lisp_Object val;
1246 struct catchtag c;
1247 struct handler h;
1248 register Lisp_Object bodyform, handlers;
1249 volatile Lisp_Object var;
1251 var = Fcar (args);
1252 bodyform = Fcar (Fcdr (args));
1253 handlers = Fcdr (Fcdr (args));
1254 CHECK_SYMBOL (var);
1256 for (val = handlers; ! NILP (val); val = Fcdr (val))
1258 Lisp_Object tem;
1259 tem = Fcar (val);
1260 if (! (NILP (tem)
1261 || (CONSP (tem)
1262 && (SYMBOLP (XCAR (tem))
1263 || CONSP (XCAR (tem))))))
1264 error ("Invalid condition handler", tem);
1267 c.tag = Qnil;
1268 c.val = Qnil;
1269 c.backlist = backtrace_list;
1270 c.handlerlist = handlerlist;
1271 c.lisp_eval_depth = lisp_eval_depth;
1272 c.pdlcount = SPECPDL_INDEX ();
1273 c.poll_suppress_count = poll_suppress_count;
1274 c.interrupt_input_blocked = interrupt_input_blocked;
1275 c.gcpro = gcprolist;
1276 c.byte_stack = byte_stack_list;
1277 if (_setjmp (c.jmp))
1279 if (!NILP (h.var))
1280 specbind (h.var, c.val);
1281 val = Fprogn (Fcdr (h.chosen_clause));
1283 /* Note that this just undoes the binding of h.var; whoever
1284 longjumped to us unwound the stack to c.pdlcount before
1285 throwing. */
1286 unbind_to (c.pdlcount, Qnil);
1287 return val;
1289 c.next = catchlist;
1290 catchlist = &c;
1292 h.var = var;
1293 h.handler = handlers;
1294 h.next = handlerlist;
1295 h.tag = &c;
1296 handlerlist = &h;
1298 val = Feval (bodyform);
1299 catchlist = c.next;
1300 handlerlist = h.next;
1301 return val;
1304 /* Call the function BFUN with no arguments, catching errors within it
1305 according to HANDLERS. If there is an error, call HFUN with
1306 one argument which is the data that describes the error:
1307 (SIGNALNAME . DATA)
1309 HANDLERS can be a list of conditions to catch.
1310 If HANDLERS is Qt, catch all errors.
1311 If HANDLERS is Qerror, catch all errors
1312 but allow the debugger to run if that is enabled. */
1314 Lisp_Object
1315 internal_condition_case (bfun, handlers, hfun)
1316 Lisp_Object (*bfun) ();
1317 Lisp_Object handlers;
1318 Lisp_Object (*hfun) ();
1320 Lisp_Object val;
1321 struct catchtag c;
1322 struct handler h;
1324 #if 0 /* We now handle interrupt_input_blocked properly.
1325 What we still do not handle is exiting a signal handler. */
1326 abort ();
1327 #endif
1329 c.tag = Qnil;
1330 c.val = Qnil;
1331 c.backlist = backtrace_list;
1332 c.handlerlist = handlerlist;
1333 c.lisp_eval_depth = lisp_eval_depth;
1334 c.pdlcount = SPECPDL_INDEX ();
1335 c.poll_suppress_count = poll_suppress_count;
1336 c.interrupt_input_blocked = interrupt_input_blocked;
1337 c.gcpro = gcprolist;
1338 c.byte_stack = byte_stack_list;
1339 if (_setjmp (c.jmp))
1341 return (*hfun) (c.val);
1343 c.next = catchlist;
1344 catchlist = &c;
1345 h.handler = handlers;
1346 h.var = Qnil;
1347 h.next = handlerlist;
1348 h.tag = &c;
1349 handlerlist = &h;
1351 val = (*bfun) ();
1352 catchlist = c.next;
1353 handlerlist = h.next;
1354 return val;
1357 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1359 Lisp_Object
1360 internal_condition_case_1 (bfun, arg, handlers, hfun)
1361 Lisp_Object (*bfun) ();
1362 Lisp_Object arg;
1363 Lisp_Object handlers;
1364 Lisp_Object (*hfun) ();
1366 Lisp_Object val;
1367 struct catchtag c;
1368 struct handler h;
1370 c.tag = Qnil;
1371 c.val = Qnil;
1372 c.backlist = backtrace_list;
1373 c.handlerlist = handlerlist;
1374 c.lisp_eval_depth = lisp_eval_depth;
1375 c.pdlcount = SPECPDL_INDEX ();
1376 c.poll_suppress_count = poll_suppress_count;
1377 c.interrupt_input_blocked = interrupt_input_blocked;
1378 c.gcpro = gcprolist;
1379 c.byte_stack = byte_stack_list;
1380 if (_setjmp (c.jmp))
1382 return (*hfun) (c.val);
1384 c.next = catchlist;
1385 catchlist = &c;
1386 h.handler = handlers;
1387 h.var = Qnil;
1388 h.next = handlerlist;
1389 h.tag = &c;
1390 handlerlist = &h;
1392 val = (*bfun) (arg);
1393 catchlist = c.next;
1394 handlerlist = h.next;
1395 return val;
1399 /* Like internal_condition_case but call BFUN with NARGS as first,
1400 and ARGS as second argument. */
1402 Lisp_Object
1403 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1404 Lisp_Object (*bfun) ();
1405 int nargs;
1406 Lisp_Object *args;
1407 Lisp_Object handlers;
1408 Lisp_Object (*hfun) ();
1410 Lisp_Object val;
1411 struct catchtag c;
1412 struct handler h;
1414 c.tag = Qnil;
1415 c.val = Qnil;
1416 c.backlist = backtrace_list;
1417 c.handlerlist = handlerlist;
1418 c.lisp_eval_depth = lisp_eval_depth;
1419 c.pdlcount = SPECPDL_INDEX ();
1420 c.poll_suppress_count = poll_suppress_count;
1421 c.interrupt_input_blocked = interrupt_input_blocked;
1422 c.gcpro = gcprolist;
1423 c.byte_stack = byte_stack_list;
1424 if (_setjmp (c.jmp))
1426 return (*hfun) (c.val);
1428 c.next = catchlist;
1429 catchlist = &c;
1430 h.handler = handlers;
1431 h.var = Qnil;
1432 h.next = handlerlist;
1433 h.tag = &c;
1434 handlerlist = &h;
1436 val = (*bfun) (nargs, args);
1437 catchlist = c.next;
1438 handlerlist = h.next;
1439 return val;
1443 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1444 Lisp_Object, Lisp_Object,
1445 Lisp_Object *));
1447 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1448 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1449 This function does not return.
1451 An error symbol is a symbol with an `error-conditions' property
1452 that is a list of condition names.
1453 A handler for any of those names will get to handle this signal.
1454 The symbol `error' should normally be one of them.
1456 DATA should be a list. Its elements are printed as part of the error message.
1457 If the signal is handled, DATA is made available to the handler.
1458 See also the function `condition-case'. */)
1459 (error_symbol, data)
1460 Lisp_Object error_symbol, data;
1462 /* When memory is full, ERROR-SYMBOL is nil,
1463 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1464 That is a special case--don't do this in other situations. */
1465 register struct handler *allhandlers = handlerlist;
1466 Lisp_Object conditions;
1467 extern int gc_in_progress;
1468 extern int waiting_for_input;
1469 Lisp_Object debugger_value;
1470 Lisp_Object string;
1471 Lisp_Object real_error_symbol;
1472 struct backtrace *bp;
1474 immediate_quit = handling_signal = 0;
1475 if (gc_in_progress || waiting_for_input)
1476 abort ();
1478 if (NILP (error_symbol))
1479 real_error_symbol = Fcar (data);
1480 else
1481 real_error_symbol = error_symbol;
1483 #if 0 /* rms: I don't know why this was here,
1484 but it is surely wrong for an error that is handled. */
1485 #ifdef HAVE_X_WINDOWS
1486 if (display_hourglass_p)
1487 cancel_hourglass ();
1488 #endif
1489 #endif
1491 /* This hook is used by edebug. */
1492 if (! NILP (Vsignal_hook_function)
1493 && ! NILP (error_symbol))
1494 call2 (Vsignal_hook_function, error_symbol, data);
1496 conditions = Fget (real_error_symbol, Qerror_conditions);
1498 /* Remember from where signal was called. Skip over the frame for
1499 `signal' itself. If a frame for `error' follows, skip that,
1500 too. Don't do this when ERROR_SYMBOL is nil, because that
1501 is a memory-full error. */
1502 Vsignaling_function = Qnil;
1503 if (backtrace_list && !NILP (error_symbol))
1505 bp = backtrace_list->next;
1506 if (bp && bp->function && EQ (*bp->function, Qerror))
1507 bp = bp->next;
1508 if (bp && bp->function)
1509 Vsignaling_function = *bp->function;
1512 for (; handlerlist; handlerlist = handlerlist->next)
1514 register Lisp_Object clause;
1516 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1517 max_lisp_eval_depth = lisp_eval_depth + 20;
1519 if (specpdl_size + 40 > max_specpdl_size)
1520 max_specpdl_size = specpdl_size + 40;
1522 clause = find_handler_clause (handlerlist->handler, conditions,
1523 error_symbol, data, &debugger_value);
1525 if (EQ (clause, Qlambda))
1527 /* We can't return values to code which signaled an error, but we
1528 can continue code which has signaled a quit. */
1529 if (EQ (real_error_symbol, Qquit))
1530 return Qnil;
1531 else
1532 error ("Cannot return from the debugger in an error");
1535 if (!NILP (clause))
1537 Lisp_Object unwind_data;
1538 struct handler *h = handlerlist;
1540 handlerlist = allhandlers;
1542 if (NILP (error_symbol))
1543 unwind_data = data;
1544 else
1545 unwind_data = Fcons (error_symbol, data);
1546 h->chosen_clause = clause;
1547 unwind_to_catch (h->tag, unwind_data);
1551 handlerlist = allhandlers;
1552 /* If no handler is present now, try to run the debugger,
1553 and if that fails, throw to top level. */
1554 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1555 if (catchlist != 0)
1556 Fthrow (Qtop_level, Qt);
1558 if (! NILP (error_symbol))
1559 data = Fcons (error_symbol, data);
1561 string = Ferror_message_string (data);
1562 fatal ("%s", SDATA (string), 0);
1565 /* Return nonzero iff LIST is a non-nil atom or
1566 a list containing one of CONDITIONS. */
1568 static int
1569 wants_debugger (list, conditions)
1570 Lisp_Object list, conditions;
1572 if (NILP (list))
1573 return 0;
1574 if (! CONSP (list))
1575 return 1;
1577 while (CONSP (conditions))
1579 Lisp_Object this, tail;
1580 this = XCAR (conditions);
1581 for (tail = list; CONSP (tail); tail = XCDR (tail))
1582 if (EQ (XCAR (tail), this))
1583 return 1;
1584 conditions = XCDR (conditions);
1586 return 0;
1589 /* Return 1 if an error with condition-symbols CONDITIONS,
1590 and described by SIGNAL-DATA, should skip the debugger
1591 according to debugger-ignored-errors. */
1593 static int
1594 skip_debugger (conditions, data)
1595 Lisp_Object conditions, data;
1597 Lisp_Object tail;
1598 int first_string = 1;
1599 Lisp_Object error_message;
1601 error_message = Qnil;
1602 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1604 if (STRINGP (XCAR (tail)))
1606 if (first_string)
1608 error_message = Ferror_message_string (data);
1609 first_string = 0;
1612 if (fast_string_match (XCAR (tail), error_message) >= 0)
1613 return 1;
1615 else
1617 Lisp_Object contail;
1619 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1620 if (EQ (XCAR (tail), XCAR (contail)))
1621 return 1;
1625 return 0;
1628 /* Value of Qlambda means we have called debugger and user has continued.
1629 There are two ways to pass SIG and DATA:
1630 = SIG is the error symbol, and DATA is the rest of the data.
1631 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1632 This is for memory-full errors only.
1634 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1636 static Lisp_Object
1637 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1638 Lisp_Object handlers, conditions, sig, data;
1639 Lisp_Object *debugger_value_ptr;
1641 register Lisp_Object h;
1642 register Lisp_Object tem;
1644 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1645 return Qt;
1646 /* error is used similarly, but means print an error message
1647 and run the debugger if that is enabled. */
1648 if (EQ (handlers, Qerror)
1649 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1650 there is a handler. */
1652 int count = SPECPDL_INDEX ();
1653 int debugger_called = 0;
1654 Lisp_Object sig_symbol, combined_data;
1655 /* This is set to 1 if we are handling a memory-full error,
1656 because these must not run the debugger.
1657 (There is no room in memory to do that!) */
1658 int no_debugger = 0;
1660 if (NILP (sig))
1662 combined_data = data;
1663 sig_symbol = Fcar (data);
1664 no_debugger = 1;
1666 else
1668 combined_data = Fcons (sig, data);
1669 sig_symbol = sig;
1672 if (wants_debugger (Vstack_trace_on_error, conditions))
1674 #ifdef PROTOTYPES
1675 internal_with_output_to_temp_buffer ("*Backtrace*",
1676 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1677 Qnil);
1678 #else
1679 internal_with_output_to_temp_buffer ("*Backtrace*",
1680 Fbacktrace, Qnil);
1681 #endif
1683 if (! no_debugger
1684 && (EQ (sig_symbol, Qquit)
1685 ? debug_on_quit
1686 : wants_debugger (Vdebug_on_error, conditions))
1687 && ! skip_debugger (conditions, combined_data)
1688 && when_entered_debugger < num_nonmacro_input_events)
1690 specbind (Qdebug_on_error, Qnil);
1691 *debugger_value_ptr
1692 = call_debugger (Fcons (Qerror,
1693 Fcons (combined_data, Qnil)));
1694 debugger_called = 1;
1696 /* If there is no handler, return saying whether we ran the debugger. */
1697 if (EQ (handlers, Qerror))
1699 if (debugger_called)
1700 return unbind_to (count, Qlambda);
1701 return Qt;
1704 for (h = handlers; CONSP (h); h = Fcdr (h))
1706 Lisp_Object handler, condit;
1708 handler = Fcar (h);
1709 if (!CONSP (handler))
1710 continue;
1711 condit = Fcar (handler);
1712 /* Handle a single condition name in handler HANDLER. */
1713 if (SYMBOLP (condit))
1715 tem = Fmemq (Fcar (handler), conditions);
1716 if (!NILP (tem))
1717 return handler;
1719 /* Handle a list of condition names in handler HANDLER. */
1720 else if (CONSP (condit))
1722 while (CONSP (condit))
1724 tem = Fmemq (Fcar (condit), conditions);
1725 if (!NILP (tem))
1726 return handler;
1727 condit = XCDR (condit);
1731 return Qnil;
1734 /* dump an error message; called like printf */
1736 /* VARARGS 1 */
1737 void
1738 error (m, a1, a2, a3)
1739 char *m;
1740 char *a1, *a2, *a3;
1742 char buf[200];
1743 int size = 200;
1744 int mlen;
1745 char *buffer = buf;
1746 char *args[3];
1747 int allocated = 0;
1748 Lisp_Object string;
1750 args[0] = a1;
1751 args[1] = a2;
1752 args[2] = a3;
1754 mlen = strlen (m);
1756 while (1)
1758 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1759 if (used < size)
1760 break;
1761 size *= 2;
1762 if (allocated)
1763 buffer = (char *) xrealloc (buffer, size);
1764 else
1766 buffer = (char *) xmalloc (size);
1767 allocated = 1;
1771 string = build_string (buffer);
1772 if (allocated)
1773 xfree (buffer);
1775 Fsignal (Qerror, Fcons (string, Qnil));
1776 abort ();
1779 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1780 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1781 This means it contains a description for how to read arguments to give it.
1782 The value is nil for an invalid function or a symbol with no function
1783 definition.
1785 Interactively callable functions include strings and vectors (treated
1786 as keyboard macros), lambda-expressions that contain a top-level call
1787 to `interactive', autoload definitions made by `autoload' with non-nil
1788 fourth argument, and some of the built-in functions of Lisp.
1790 Also, a symbol satisfies `commandp' if its function definition does so.
1792 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1793 then strings and vectors are not accepted. */)
1794 (function, for_call_interactively)
1795 Lisp_Object function, for_call_interactively;
1797 register Lisp_Object fun;
1798 register Lisp_Object funcar;
1800 fun = function;
1802 fun = indirect_function (fun);
1803 if (EQ (fun, Qunbound))
1804 return Qnil;
1806 /* Emacs primitives are interactive if their DEFUN specifies an
1807 interactive spec. */
1808 if (SUBRP (fun))
1810 if (XSUBR (fun)->prompt)
1811 return Qt;
1812 else
1813 return Qnil;
1816 /* Bytecode objects are interactive if they are long enough to
1817 have an element whose index is COMPILED_INTERACTIVE, which is
1818 where the interactive spec is stored. */
1819 else if (COMPILEDP (fun))
1820 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1821 ? Qt : Qnil);
1823 /* Strings and vectors are keyboard macros. */
1824 if (NILP (for_call_interactively) && (STRINGP (fun) || VECTORP (fun)))
1825 return Qt;
1827 /* Lists may represent commands. */
1828 if (!CONSP (fun))
1829 return Qnil;
1830 funcar = Fcar (fun);
1831 if (!SYMBOLP (funcar))
1832 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1833 if (EQ (funcar, Qlambda))
1834 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1835 if (EQ (funcar, Qautoload))
1836 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1837 else
1838 return Qnil;
1841 /* ARGSUSED */
1842 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1843 doc: /* Define FUNCTION to autoload from FILE.
1844 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1845 Third arg DOCSTRING is documentation for the function.
1846 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1847 Fifth arg TYPE indicates the type of the object:
1848 nil or omitted says FUNCTION is a function,
1849 `keymap' says FUNCTION is really a keymap, and
1850 `macro' or t says FUNCTION is really a macro.
1851 Third through fifth args give info about the real definition.
1852 They default to nil.
1853 If FUNCTION is already defined other than as an autoload,
1854 this does nothing and returns nil. */)
1855 (function, file, docstring, interactive, type)
1856 Lisp_Object function, file, docstring, interactive, type;
1858 #ifdef NO_ARG_ARRAY
1859 Lisp_Object args[4];
1860 #endif
1862 CHECK_SYMBOL (function);
1863 CHECK_STRING (file);
1865 /* If function is defined and not as an autoload, don't override */
1866 if (!EQ (XSYMBOL (function)->function, Qunbound)
1867 && !(CONSP (XSYMBOL (function)->function)
1868 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1869 return Qnil;
1871 if (NILP (Vpurify_flag))
1872 /* Only add entries after dumping, because the ones before are
1873 not useful and else we get loads of them from the loaddefs.el. */
1874 LOADHIST_ATTACH (Fcons (Qautoload, function));
1876 #ifdef NO_ARG_ARRAY
1877 args[0] = file;
1878 args[1] = docstring;
1879 args[2] = interactive;
1880 args[3] = type;
1882 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1883 #else /* NO_ARG_ARRAY */
1884 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1885 #endif /* not NO_ARG_ARRAY */
1888 Lisp_Object
1889 un_autoload (oldqueue)
1890 Lisp_Object oldqueue;
1892 register Lisp_Object queue, first, second;
1894 /* Queue to unwind is current value of Vautoload_queue.
1895 oldqueue is the shadowed value to leave in Vautoload_queue. */
1896 queue = Vautoload_queue;
1897 Vautoload_queue = oldqueue;
1898 while (CONSP (queue))
1900 first = Fcar (queue);
1901 second = Fcdr (first);
1902 first = Fcar (first);
1903 if (EQ (second, Qnil))
1904 Vfeatures = first;
1905 else
1906 Ffset (first, second);
1907 queue = Fcdr (queue);
1909 return Qnil;
1912 /* Load an autoloaded function.
1913 FUNNAME is the symbol which is the function's name.
1914 FUNDEF is the autoload definition (a list). */
1916 void
1917 do_autoload (fundef, funname)
1918 Lisp_Object fundef, funname;
1920 int count = SPECPDL_INDEX ();
1921 Lisp_Object fun, queue, first, second;
1922 struct gcpro gcpro1, gcpro2, gcpro3;
1924 /* This is to make sure that loadup.el gives a clear picture
1925 of what files are preloaded and when. */
1926 if (! NILP (Vpurify_flag))
1927 error ("Attempt to autoload %s while preparing to dump",
1928 SDATA (SYMBOL_NAME (funname)));
1930 fun = funname;
1931 CHECK_SYMBOL (funname);
1932 GCPRO3 (fun, funname, fundef);
1934 /* Preserve the match data. */
1935 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1937 /* Value saved here is to be restored into Vautoload_queue. */
1938 record_unwind_protect (un_autoload, Vautoload_queue);
1939 Vautoload_queue = Qt;
1940 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1942 /* Save the old autoloads, in case we ever do an unload. */
1943 queue = Vautoload_queue;
1944 while (CONSP (queue))
1946 first = Fcar (queue);
1947 second = Fcdr (first);
1948 first = Fcar (first);
1950 /* Note: This test is subtle. The cdr of an autoload-queue entry
1951 may be an atom if the autoload entry was generated by a defalias
1952 or fset. */
1953 if (CONSP (second))
1954 Fput (first, Qautoload, (Fcdr (second)));
1956 queue = Fcdr (queue);
1959 /* Once loading finishes, don't undo it. */
1960 Vautoload_queue = Qt;
1961 unbind_to (count, Qnil);
1963 fun = Findirect_function (fun);
1965 if (!NILP (Fequal (fun, fundef)))
1966 error ("Autoloading failed to define function %s",
1967 SDATA (SYMBOL_NAME (funname)));
1968 UNGCPRO;
1972 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1973 doc: /* Evaluate FORM and return its value. */)
1974 (form)
1975 Lisp_Object form;
1977 Lisp_Object fun, val, original_fun, original_args;
1978 Lisp_Object funcar;
1979 struct backtrace backtrace;
1980 struct gcpro gcpro1, gcpro2, gcpro3;
1982 if (handling_signal)
1983 abort ();
1985 if (SYMBOLP (form))
1986 return Fsymbol_value (form);
1987 if (!CONSP (form))
1988 return form;
1990 QUIT;
1991 if (consing_since_gc > gc_cons_threshold)
1993 GCPRO1 (form);
1994 Fgarbage_collect ();
1995 UNGCPRO;
1998 if (++lisp_eval_depth > max_lisp_eval_depth)
2000 if (max_lisp_eval_depth < 100)
2001 max_lisp_eval_depth = 100;
2002 if (lisp_eval_depth > max_lisp_eval_depth)
2003 error ("Lisp nesting exceeds max-lisp-eval-depth");
2006 original_fun = Fcar (form);
2007 original_args = Fcdr (form);
2009 backtrace.next = backtrace_list;
2010 backtrace_list = &backtrace;
2011 backtrace.function = &original_fun; /* This also protects them from gc */
2012 backtrace.args = &original_args;
2013 backtrace.nargs = UNEVALLED;
2014 backtrace.evalargs = 1;
2015 backtrace.debug_on_exit = 0;
2017 if (debug_on_next_call)
2018 do_debug_on_call (Qt);
2020 /* At this point, only original_fun and original_args
2021 have values that will be used below */
2022 retry:
2023 fun = Findirect_function (original_fun);
2025 if (SUBRP (fun))
2027 Lisp_Object numargs;
2028 Lisp_Object argvals[8];
2029 Lisp_Object args_left;
2030 register int i, maxargs;
2032 args_left = original_args;
2033 numargs = Flength (args_left);
2035 if (XINT (numargs) < XSUBR (fun)->min_args ||
2036 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2037 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2039 if (XSUBR (fun)->max_args == UNEVALLED)
2041 backtrace.evalargs = 0;
2042 val = (*XSUBR (fun)->function) (args_left);
2043 goto done;
2046 if (XSUBR (fun)->max_args == MANY)
2048 /* Pass a vector of evaluated arguments */
2049 Lisp_Object *vals;
2050 register int argnum = 0;
2052 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2054 GCPRO3 (args_left, fun, fun);
2055 gcpro3.var = vals;
2056 gcpro3.nvars = 0;
2058 while (!NILP (args_left))
2060 vals[argnum++] = Feval (Fcar (args_left));
2061 args_left = Fcdr (args_left);
2062 gcpro3.nvars = argnum;
2065 backtrace.args = vals;
2066 backtrace.nargs = XINT (numargs);
2068 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2069 UNGCPRO;
2070 goto done;
2073 GCPRO3 (args_left, fun, fun);
2074 gcpro3.var = argvals;
2075 gcpro3.nvars = 0;
2077 maxargs = XSUBR (fun)->max_args;
2078 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2080 argvals[i] = Feval (Fcar (args_left));
2081 gcpro3.nvars = ++i;
2084 UNGCPRO;
2086 backtrace.args = argvals;
2087 backtrace.nargs = XINT (numargs);
2089 switch (i)
2091 case 0:
2092 val = (*XSUBR (fun)->function) ();
2093 goto done;
2094 case 1:
2095 val = (*XSUBR (fun)->function) (argvals[0]);
2096 goto done;
2097 case 2:
2098 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2099 goto done;
2100 case 3:
2101 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2102 argvals[2]);
2103 goto done;
2104 case 4:
2105 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2106 argvals[2], argvals[3]);
2107 goto done;
2108 case 5:
2109 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2110 argvals[3], argvals[4]);
2111 goto done;
2112 case 6:
2113 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2114 argvals[3], argvals[4], argvals[5]);
2115 goto done;
2116 case 7:
2117 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2118 argvals[3], argvals[4], argvals[5],
2119 argvals[6]);
2120 goto done;
2122 case 8:
2123 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2124 argvals[3], argvals[4], argvals[5],
2125 argvals[6], argvals[7]);
2126 goto done;
2128 default:
2129 /* Someone has created a subr that takes more arguments than
2130 is supported by this code. We need to either rewrite the
2131 subr to use a different argument protocol, or add more
2132 cases to this switch. */
2133 abort ();
2136 if (COMPILEDP (fun))
2137 val = apply_lambda (fun, original_args, 1);
2138 else
2140 if (!CONSP (fun))
2141 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2142 funcar = Fcar (fun);
2143 if (!SYMBOLP (funcar))
2144 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2145 if (EQ (funcar, Qautoload))
2147 do_autoload (fun, original_fun);
2148 goto retry;
2150 if (EQ (funcar, Qmacro))
2151 val = Feval (apply1 (Fcdr (fun), original_args));
2152 else if (EQ (funcar, Qlambda))
2153 val = apply_lambda (fun, original_args, 1);
2154 else
2155 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2157 done:
2158 lisp_eval_depth--;
2159 if (backtrace.debug_on_exit)
2160 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2161 backtrace_list = backtrace.next;
2163 #ifdef HAVE_CARBON
2164 mac_check_for_quit_char();
2165 #endif
2166 return val;
2169 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2170 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2171 Then return the value FUNCTION returns.
2172 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2173 usage: (apply FUNCTION &rest ARGUMENTS) */)
2174 (nargs, args)
2175 int nargs;
2176 Lisp_Object *args;
2178 register int i, numargs;
2179 register Lisp_Object spread_arg;
2180 register Lisp_Object *funcall_args;
2181 Lisp_Object fun;
2182 struct gcpro gcpro1;
2184 fun = args [0];
2185 funcall_args = 0;
2186 spread_arg = args [nargs - 1];
2187 CHECK_LIST (spread_arg);
2189 numargs = XINT (Flength (spread_arg));
2191 if (numargs == 0)
2192 return Ffuncall (nargs - 1, args);
2193 else if (numargs == 1)
2195 args [nargs - 1] = XCAR (spread_arg);
2196 return Ffuncall (nargs, args);
2199 numargs += nargs - 2;
2201 fun = indirect_function (fun);
2202 if (EQ (fun, Qunbound))
2204 /* Let funcall get the error */
2205 fun = args[0];
2206 goto funcall;
2209 if (SUBRP (fun))
2211 if (numargs < XSUBR (fun)->min_args
2212 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2213 goto funcall; /* Let funcall get the error */
2214 else if (XSUBR (fun)->max_args > numargs)
2216 /* Avoid making funcall cons up a yet another new vector of arguments
2217 by explicitly supplying nil's for optional values */
2218 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2219 * sizeof (Lisp_Object));
2220 for (i = numargs; i < XSUBR (fun)->max_args;)
2221 funcall_args[++i] = Qnil;
2222 GCPRO1 (*funcall_args);
2223 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2226 funcall:
2227 /* We add 1 to numargs because funcall_args includes the
2228 function itself as well as its arguments. */
2229 if (!funcall_args)
2231 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2232 * sizeof (Lisp_Object));
2233 GCPRO1 (*funcall_args);
2234 gcpro1.nvars = 1 + numargs;
2237 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2238 /* Spread the last arg we got. Its first element goes in
2239 the slot that it used to occupy, hence this value of I. */
2240 i = nargs - 1;
2241 while (!NILP (spread_arg))
2243 funcall_args [i++] = XCAR (spread_arg);
2244 spread_arg = XCDR (spread_arg);
2247 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2250 /* Run hook variables in various ways. */
2252 enum run_hooks_condition {to_completion, until_success, until_failure};
2253 static Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
2254 enum run_hooks_condition));
2256 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2257 doc: /* Run each hook in HOOKS. Major mode functions use this.
2258 Each argument should be a symbol, a hook variable.
2259 These symbols are processed in the order specified.
2260 If a hook symbol has a non-nil value, that value may be a function
2261 or a list of functions to be called to run the hook.
2262 If the value is a function, it is called with no arguments.
2263 If it is a list, the elements are called, in order, with no arguments.
2265 Do not use `make-local-variable' to make a hook variable buffer-local.
2266 Instead, use `add-hook' and specify t for the LOCAL argument.
2267 usage: (run-hooks &rest HOOKS) */)
2268 (nargs, args)
2269 int nargs;
2270 Lisp_Object *args;
2272 Lisp_Object hook[1];
2273 register int i;
2275 for (i = 0; i < nargs; i++)
2277 hook[0] = args[i];
2278 run_hook_with_args (1, hook, to_completion);
2281 return Qnil;
2284 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2285 Srun_hook_with_args, 1, MANY, 0,
2286 doc: /* Run HOOK with the specified arguments ARGS.
2287 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2288 value, that value may be a function or a list of functions to be
2289 called to run the hook. If the value is a function, it is called with
2290 the given arguments and its return value is returned. If it is a list
2291 of functions, those functions are called, in order,
2292 with the given arguments ARGS.
2293 It is best not to depend on the value return by `run-hook-with-args',
2294 as that may change.
2296 Do not use `make-local-variable' to make a hook variable buffer-local.
2297 Instead, use `add-hook' and specify t for the LOCAL argument.
2298 usage: (run-hook-with-args HOOK &rest ARGS) */)
2299 (nargs, args)
2300 int nargs;
2301 Lisp_Object *args;
2303 return run_hook_with_args (nargs, args, to_completion);
2306 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2307 Srun_hook_with_args_until_success, 1, MANY, 0,
2308 doc: /* Run HOOK with the specified arguments ARGS.
2309 HOOK should be a symbol, a hook variable. Its value should
2310 be a list of functions. We call those functions, one by one,
2311 passing arguments ARGS to each of them, until one of them
2312 returns a non-nil value. Then we return that value.
2313 If all the functions return nil, we return nil.
2315 Do not use `make-local-variable' to make a hook variable buffer-local.
2316 Instead, use `add-hook' and specify t for the LOCAL argument.
2317 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2318 (nargs, args)
2319 int nargs;
2320 Lisp_Object *args;
2322 return run_hook_with_args (nargs, args, until_success);
2325 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2326 Srun_hook_with_args_until_failure, 1, MANY, 0,
2327 doc: /* Run HOOK with the specified arguments ARGS.
2328 HOOK should be a symbol, a hook variable. Its value should
2329 be a list of functions. We call those functions, one by one,
2330 passing arguments ARGS to each of them, until one of them
2331 returns nil. Then we return nil.
2332 If all the functions return non-nil, we return non-nil.
2334 Do not use `make-local-variable' to make a hook variable buffer-local.
2335 Instead, use `add-hook' and specify t for the LOCAL argument.
2336 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2337 (nargs, args)
2338 int nargs;
2339 Lisp_Object *args;
2341 return run_hook_with_args (nargs, args, until_failure);
2344 /* ARGS[0] should be a hook symbol.
2345 Call each of the functions in the hook value, passing each of them
2346 as arguments all the rest of ARGS (all NARGS - 1 elements).
2347 COND specifies a condition to test after each call
2348 to decide whether to stop.
2349 The caller (or its caller, etc) must gcpro all of ARGS,
2350 except that it isn't necessary to gcpro ARGS[0]. */
2352 static Lisp_Object
2353 run_hook_with_args (nargs, args, cond)
2354 int nargs;
2355 Lisp_Object *args;
2356 enum run_hooks_condition cond;
2358 Lisp_Object sym, val, ret;
2359 Lisp_Object globals;
2360 struct gcpro gcpro1, gcpro2, gcpro3;
2362 /* If we are dying or still initializing,
2363 don't do anything--it would probably crash if we tried. */
2364 if (NILP (Vrun_hooks))
2365 return Qnil;
2367 sym = args[0];
2368 val = find_symbol_value (sym);
2369 ret = (cond == until_failure ? Qt : Qnil);
2371 if (EQ (val, Qunbound) || NILP (val))
2372 return ret;
2373 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2375 args[0] = val;
2376 return Ffuncall (nargs, args);
2378 else
2380 globals = Qnil;
2381 GCPRO3 (sym, val, globals);
2383 for (;
2384 CONSP (val) && ((cond == to_completion)
2385 || (cond == until_success ? NILP (ret)
2386 : !NILP (ret)));
2387 val = XCDR (val))
2389 if (EQ (XCAR (val), Qt))
2391 /* t indicates this hook has a local binding;
2392 it means to run the global binding too. */
2394 for (globals = Fdefault_value (sym);
2395 CONSP (globals) && ((cond == to_completion)
2396 || (cond == until_success ? NILP (ret)
2397 : !NILP (ret)));
2398 globals = XCDR (globals))
2400 args[0] = XCAR (globals);
2401 /* In a global value, t should not occur. If it does, we
2402 must ignore it to avoid an endless loop. */
2403 if (!EQ (args[0], Qt))
2404 ret = Ffuncall (nargs, args);
2407 else
2409 args[0] = XCAR (val);
2410 ret = Ffuncall (nargs, args);
2414 UNGCPRO;
2415 return ret;
2419 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2420 present value of that symbol.
2421 Call each element of FUNLIST,
2422 passing each of them the rest of ARGS.
2423 The caller (or its caller, etc) must gcpro all of ARGS,
2424 except that it isn't necessary to gcpro ARGS[0]. */
2426 Lisp_Object
2427 run_hook_list_with_args (funlist, nargs, args)
2428 Lisp_Object funlist;
2429 int nargs;
2430 Lisp_Object *args;
2432 Lisp_Object sym;
2433 Lisp_Object val;
2434 Lisp_Object globals;
2435 struct gcpro gcpro1, gcpro2, gcpro3;
2437 sym = args[0];
2438 globals = Qnil;
2439 GCPRO3 (sym, val, globals);
2441 for (val = funlist; CONSP (val); val = XCDR (val))
2443 if (EQ (XCAR (val), Qt))
2445 /* t indicates this hook has a local binding;
2446 it means to run the global binding too. */
2448 for (globals = Fdefault_value (sym);
2449 CONSP (globals);
2450 globals = XCDR (globals))
2452 args[0] = XCAR (globals);
2453 /* In a global value, t should not occur. If it does, we
2454 must ignore it to avoid an endless loop. */
2455 if (!EQ (args[0], Qt))
2456 Ffuncall (nargs, args);
2459 else
2461 args[0] = XCAR (val);
2462 Ffuncall (nargs, args);
2465 UNGCPRO;
2466 return Qnil;
2469 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2471 void
2472 run_hook_with_args_2 (hook, arg1, arg2)
2473 Lisp_Object hook, arg1, arg2;
2475 Lisp_Object temp[3];
2476 temp[0] = hook;
2477 temp[1] = arg1;
2478 temp[2] = arg2;
2480 Frun_hook_with_args (3, temp);
2483 /* Apply fn to arg */
2484 Lisp_Object
2485 apply1 (fn, arg)
2486 Lisp_Object fn, arg;
2488 struct gcpro gcpro1;
2490 GCPRO1 (fn);
2491 if (NILP (arg))
2492 RETURN_UNGCPRO (Ffuncall (1, &fn));
2493 gcpro1.nvars = 2;
2494 #ifdef NO_ARG_ARRAY
2496 Lisp_Object args[2];
2497 args[0] = fn;
2498 args[1] = arg;
2499 gcpro1.var = args;
2500 RETURN_UNGCPRO (Fapply (2, args));
2502 #else /* not NO_ARG_ARRAY */
2503 RETURN_UNGCPRO (Fapply (2, &fn));
2504 #endif /* not NO_ARG_ARRAY */
2507 /* Call function fn on no arguments */
2508 Lisp_Object
2509 call0 (fn)
2510 Lisp_Object fn;
2512 struct gcpro gcpro1;
2514 GCPRO1 (fn);
2515 RETURN_UNGCPRO (Ffuncall (1, &fn));
2518 /* Call function fn with 1 argument arg1 */
2519 /* ARGSUSED */
2520 Lisp_Object
2521 call1 (fn, arg1)
2522 Lisp_Object fn, arg1;
2524 struct gcpro gcpro1;
2525 #ifdef NO_ARG_ARRAY
2526 Lisp_Object args[2];
2528 args[0] = fn;
2529 args[1] = arg1;
2530 GCPRO1 (args[0]);
2531 gcpro1.nvars = 2;
2532 RETURN_UNGCPRO (Ffuncall (2, args));
2533 #else /* not NO_ARG_ARRAY */
2534 GCPRO1 (fn);
2535 gcpro1.nvars = 2;
2536 RETURN_UNGCPRO (Ffuncall (2, &fn));
2537 #endif /* not NO_ARG_ARRAY */
2540 /* Call function fn with 2 arguments arg1, arg2 */
2541 /* ARGSUSED */
2542 Lisp_Object
2543 call2 (fn, arg1, arg2)
2544 Lisp_Object fn, arg1, arg2;
2546 struct gcpro gcpro1;
2547 #ifdef NO_ARG_ARRAY
2548 Lisp_Object args[3];
2549 args[0] = fn;
2550 args[1] = arg1;
2551 args[2] = arg2;
2552 GCPRO1 (args[0]);
2553 gcpro1.nvars = 3;
2554 RETURN_UNGCPRO (Ffuncall (3, args));
2555 #else /* not NO_ARG_ARRAY */
2556 GCPRO1 (fn);
2557 gcpro1.nvars = 3;
2558 RETURN_UNGCPRO (Ffuncall (3, &fn));
2559 #endif /* not NO_ARG_ARRAY */
2562 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2563 /* ARGSUSED */
2564 Lisp_Object
2565 call3 (fn, arg1, arg2, arg3)
2566 Lisp_Object fn, arg1, arg2, arg3;
2568 struct gcpro gcpro1;
2569 #ifdef NO_ARG_ARRAY
2570 Lisp_Object args[4];
2571 args[0] = fn;
2572 args[1] = arg1;
2573 args[2] = arg2;
2574 args[3] = arg3;
2575 GCPRO1 (args[0]);
2576 gcpro1.nvars = 4;
2577 RETURN_UNGCPRO (Ffuncall (4, args));
2578 #else /* not NO_ARG_ARRAY */
2579 GCPRO1 (fn);
2580 gcpro1.nvars = 4;
2581 RETURN_UNGCPRO (Ffuncall (4, &fn));
2582 #endif /* not NO_ARG_ARRAY */
2585 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2586 /* ARGSUSED */
2587 Lisp_Object
2588 call4 (fn, arg1, arg2, arg3, arg4)
2589 Lisp_Object fn, arg1, arg2, arg3, arg4;
2591 struct gcpro gcpro1;
2592 #ifdef NO_ARG_ARRAY
2593 Lisp_Object args[5];
2594 args[0] = fn;
2595 args[1] = arg1;
2596 args[2] = arg2;
2597 args[3] = arg3;
2598 args[4] = arg4;
2599 GCPRO1 (args[0]);
2600 gcpro1.nvars = 5;
2601 RETURN_UNGCPRO (Ffuncall (5, args));
2602 #else /* not NO_ARG_ARRAY */
2603 GCPRO1 (fn);
2604 gcpro1.nvars = 5;
2605 RETURN_UNGCPRO (Ffuncall (5, &fn));
2606 #endif /* not NO_ARG_ARRAY */
2609 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2610 /* ARGSUSED */
2611 Lisp_Object
2612 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2613 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2615 struct gcpro gcpro1;
2616 #ifdef NO_ARG_ARRAY
2617 Lisp_Object args[6];
2618 args[0] = fn;
2619 args[1] = arg1;
2620 args[2] = arg2;
2621 args[3] = arg3;
2622 args[4] = arg4;
2623 args[5] = arg5;
2624 GCPRO1 (args[0]);
2625 gcpro1.nvars = 6;
2626 RETURN_UNGCPRO (Ffuncall (6, args));
2627 #else /* not NO_ARG_ARRAY */
2628 GCPRO1 (fn);
2629 gcpro1.nvars = 6;
2630 RETURN_UNGCPRO (Ffuncall (6, &fn));
2631 #endif /* not NO_ARG_ARRAY */
2634 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2635 /* ARGSUSED */
2636 Lisp_Object
2637 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2638 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2640 struct gcpro gcpro1;
2641 #ifdef NO_ARG_ARRAY
2642 Lisp_Object args[7];
2643 args[0] = fn;
2644 args[1] = arg1;
2645 args[2] = arg2;
2646 args[3] = arg3;
2647 args[4] = arg4;
2648 args[5] = arg5;
2649 args[6] = arg6;
2650 GCPRO1 (args[0]);
2651 gcpro1.nvars = 7;
2652 RETURN_UNGCPRO (Ffuncall (7, args));
2653 #else /* not NO_ARG_ARRAY */
2654 GCPRO1 (fn);
2655 gcpro1.nvars = 7;
2656 RETURN_UNGCPRO (Ffuncall (7, &fn));
2657 #endif /* not NO_ARG_ARRAY */
2660 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2661 doc: /* Call first argument as a function, passing remaining arguments to it.
2662 Return the value that function returns.
2663 Thus, (funcall 'cons 'x 'y) returns (x . y).
2664 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2665 (nargs, args)
2666 int nargs;
2667 Lisp_Object *args;
2669 Lisp_Object fun;
2670 Lisp_Object funcar;
2671 int numargs = nargs - 1;
2672 Lisp_Object lisp_numargs;
2673 Lisp_Object val;
2674 struct backtrace backtrace;
2675 register Lisp_Object *internal_args;
2676 register int i;
2678 QUIT;
2679 if (consing_since_gc > gc_cons_threshold)
2680 Fgarbage_collect ();
2682 if (++lisp_eval_depth > max_lisp_eval_depth)
2684 if (max_lisp_eval_depth < 100)
2685 max_lisp_eval_depth = 100;
2686 if (lisp_eval_depth > max_lisp_eval_depth)
2687 error ("Lisp nesting exceeds max-lisp-eval-depth");
2690 backtrace.next = backtrace_list;
2691 backtrace_list = &backtrace;
2692 backtrace.function = &args[0];
2693 backtrace.args = &args[1];
2694 backtrace.nargs = nargs - 1;
2695 backtrace.evalargs = 0;
2696 backtrace.debug_on_exit = 0;
2698 if (debug_on_next_call)
2699 do_debug_on_call (Qlambda);
2701 retry:
2703 fun = args[0];
2705 fun = Findirect_function (fun);
2707 if (SUBRP (fun))
2709 if (numargs < XSUBR (fun)->min_args
2710 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2712 XSETFASTINT (lisp_numargs, numargs);
2713 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2716 if (XSUBR (fun)->max_args == UNEVALLED)
2717 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2719 if (XSUBR (fun)->max_args == MANY)
2721 val = (*XSUBR (fun)->function) (numargs, args + 1);
2722 goto done;
2725 if (XSUBR (fun)->max_args > numargs)
2727 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2728 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2729 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2730 internal_args[i] = Qnil;
2732 else
2733 internal_args = args + 1;
2734 switch (XSUBR (fun)->max_args)
2736 case 0:
2737 val = (*XSUBR (fun)->function) ();
2738 goto done;
2739 case 1:
2740 val = (*XSUBR (fun)->function) (internal_args[0]);
2741 goto done;
2742 case 2:
2743 val = (*XSUBR (fun)->function) (internal_args[0],
2744 internal_args[1]);
2745 goto done;
2746 case 3:
2747 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2748 internal_args[2]);
2749 goto done;
2750 case 4:
2751 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2752 internal_args[2],
2753 internal_args[3]);
2754 goto done;
2755 case 5:
2756 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2757 internal_args[2], internal_args[3],
2758 internal_args[4]);
2759 goto done;
2760 case 6:
2761 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2762 internal_args[2], internal_args[3],
2763 internal_args[4], internal_args[5]);
2764 goto done;
2765 case 7:
2766 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2767 internal_args[2], internal_args[3],
2768 internal_args[4], internal_args[5],
2769 internal_args[6]);
2770 goto done;
2772 case 8:
2773 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2774 internal_args[2], internal_args[3],
2775 internal_args[4], internal_args[5],
2776 internal_args[6], internal_args[7]);
2777 goto done;
2779 default:
2781 /* If a subr takes more than 8 arguments without using MANY
2782 or UNEVALLED, we need to extend this function to support it.
2783 Until this is done, there is no way to call the function. */
2784 abort ();
2787 if (COMPILEDP (fun))
2788 val = funcall_lambda (fun, numargs, args + 1);
2789 else
2791 if (!CONSP (fun))
2792 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2793 funcar = Fcar (fun);
2794 if (!SYMBOLP (funcar))
2795 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2796 if (EQ (funcar, Qlambda))
2797 val = funcall_lambda (fun, numargs, args + 1);
2798 else if (EQ (funcar, Qautoload))
2800 do_autoload (fun, args[0]);
2801 goto retry;
2803 else
2804 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2806 done:
2807 lisp_eval_depth--;
2808 if (backtrace.debug_on_exit)
2809 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2810 backtrace_list = backtrace.next;
2811 return val;
2814 Lisp_Object
2815 apply_lambda (fun, args, eval_flag)
2816 Lisp_Object fun, args;
2817 int eval_flag;
2819 Lisp_Object args_left;
2820 Lisp_Object numargs;
2821 register Lisp_Object *arg_vector;
2822 struct gcpro gcpro1, gcpro2, gcpro3;
2823 register int i;
2824 register Lisp_Object tem;
2826 numargs = Flength (args);
2827 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2828 args_left = args;
2830 GCPRO3 (*arg_vector, args_left, fun);
2831 gcpro1.nvars = 0;
2833 for (i = 0; i < XINT (numargs);)
2835 tem = Fcar (args_left), args_left = Fcdr (args_left);
2836 if (eval_flag) tem = Feval (tem);
2837 arg_vector[i++] = tem;
2838 gcpro1.nvars = i;
2841 UNGCPRO;
2843 if (eval_flag)
2845 backtrace_list->args = arg_vector;
2846 backtrace_list->nargs = i;
2848 backtrace_list->evalargs = 0;
2849 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2851 /* Do the debug-on-exit now, while arg_vector still exists. */
2852 if (backtrace_list->debug_on_exit)
2853 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2854 /* Don't do it again when we return to eval. */
2855 backtrace_list->debug_on_exit = 0;
2856 return tem;
2859 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2860 and return the result of evaluation.
2861 FUN must be either a lambda-expression or a compiled-code object. */
2863 static Lisp_Object
2864 funcall_lambda (fun, nargs, arg_vector)
2865 Lisp_Object fun;
2866 int nargs;
2867 register Lisp_Object *arg_vector;
2869 Lisp_Object val, syms_left, next;
2870 int count = SPECPDL_INDEX ();
2871 int i, optional, rest;
2873 if (CONSP (fun))
2875 syms_left = XCDR (fun);
2876 if (CONSP (syms_left))
2877 syms_left = XCAR (syms_left);
2878 else
2879 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2881 else if (COMPILEDP (fun))
2882 syms_left = AREF (fun, COMPILED_ARGLIST);
2883 else
2884 abort ();
2886 i = optional = rest = 0;
2887 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2889 QUIT;
2891 next = XCAR (syms_left);
2892 while (!SYMBOLP (next))
2893 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2895 if (EQ (next, Qand_rest))
2896 rest = 1;
2897 else if (EQ (next, Qand_optional))
2898 optional = 1;
2899 else if (rest)
2901 specbind (next, Flist (nargs - i, &arg_vector[i]));
2902 i = nargs;
2904 else if (i < nargs)
2905 specbind (next, arg_vector[i++]);
2906 else if (!optional)
2907 return Fsignal (Qwrong_number_of_arguments,
2908 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2909 else
2910 specbind (next, Qnil);
2913 if (!NILP (syms_left))
2914 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2915 else if (i < nargs)
2916 return Fsignal (Qwrong_number_of_arguments,
2917 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2919 if (CONSP (fun))
2920 val = Fprogn (XCDR (XCDR (fun)));
2921 else
2923 /* If we have not actually read the bytecode string
2924 and constants vector yet, fetch them from the file. */
2925 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2926 Ffetch_bytecode (fun);
2927 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2928 AREF (fun, COMPILED_CONSTANTS),
2929 AREF (fun, COMPILED_STACK_DEPTH));
2932 return unbind_to (count, val);
2935 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2936 1, 1, 0,
2937 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2938 (object)
2939 Lisp_Object object;
2941 Lisp_Object tem;
2943 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2945 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2946 if (!CONSP (tem))
2948 tem = AREF (object, COMPILED_BYTECODE);
2949 if (CONSP (tem) && STRINGP (XCAR (tem)))
2950 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2951 else
2952 error ("Invalid byte code");
2954 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
2955 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
2957 return object;
2960 void
2961 grow_specpdl ()
2963 register int count = SPECPDL_INDEX ();
2964 if (specpdl_size >= max_specpdl_size)
2966 if (max_specpdl_size < 400)
2967 max_specpdl_size = 400;
2968 if (specpdl_size >= max_specpdl_size)
2970 if (!NILP (Vdebug_on_error))
2971 /* Leave room for some specpdl in the debugger. */
2972 max_specpdl_size = specpdl_size + 100;
2973 Fsignal (Qerror,
2974 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2977 specpdl_size *= 2;
2978 if (specpdl_size > max_specpdl_size)
2979 specpdl_size = max_specpdl_size;
2980 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2981 specpdl_ptr = specpdl + count;
2984 void
2985 specbind (symbol, value)
2986 Lisp_Object symbol, value;
2988 Lisp_Object ovalue;
2989 Lisp_Object valcontents;
2991 CHECK_SYMBOL (symbol);
2992 if (specpdl_ptr == specpdl + specpdl_size)
2993 grow_specpdl ();
2995 /* The most common case is that of a non-constant symbol with a
2996 trivial value. Make that as fast as we can. */
2997 valcontents = SYMBOL_VALUE (symbol);
2998 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
3000 specpdl_ptr->symbol = symbol;
3001 specpdl_ptr->old_value = valcontents;
3002 specpdl_ptr->func = NULL;
3003 ++specpdl_ptr;
3004 SET_SYMBOL_VALUE (symbol, value);
3006 else
3008 Lisp_Object valcontents;
3010 ovalue = find_symbol_value (symbol);
3011 specpdl_ptr->func = 0;
3012 specpdl_ptr->old_value = ovalue;
3014 valcontents = XSYMBOL (symbol)->value;
3016 if (BUFFER_LOCAL_VALUEP (valcontents)
3017 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
3018 || BUFFER_OBJFWDP (valcontents))
3020 Lisp_Object where, current_buffer;
3022 current_buffer = Fcurrent_buffer ();
3024 /* For a local variable, record both the symbol and which
3025 buffer's or frame's value we are saving. */
3026 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3027 where = current_buffer;
3028 else if (!BUFFER_OBJFWDP (valcontents)
3029 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3030 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3031 else
3032 where = Qnil;
3034 /* We're not using the `unused' slot in the specbinding
3035 structure because this would mean we have to do more
3036 work for simple variables. */
3037 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3039 /* If SYMBOL is a per-buffer variable which doesn't have a
3040 buffer-local value here, make the `let' change the global
3041 value by changing the value of SYMBOL in all buffers not
3042 having their own value. This is consistent with what
3043 happens with other buffer-local variables. */
3044 if (NILP (where)
3045 && BUFFER_OBJFWDP (valcontents))
3047 ++specpdl_ptr;
3048 Fset_default (symbol, value);
3049 return;
3052 else
3053 specpdl_ptr->symbol = symbol;
3055 specpdl_ptr++;
3056 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3057 store_symval_forwarding (symbol, ovalue, value, NULL);
3058 else
3059 set_internal (symbol, value, 0, 1);
3063 void
3064 record_unwind_protect (function, arg)
3065 Lisp_Object (*function) P_ ((Lisp_Object));
3066 Lisp_Object arg;
3068 if (specpdl_ptr == specpdl + specpdl_size)
3069 grow_specpdl ();
3070 specpdl_ptr->func = function;
3071 specpdl_ptr->symbol = Qnil;
3072 specpdl_ptr->old_value = arg;
3073 specpdl_ptr++;
3076 Lisp_Object
3077 unbind_to (count, value)
3078 int count;
3079 Lisp_Object value;
3081 int quitf = !NILP (Vquit_flag);
3082 struct gcpro gcpro1;
3084 GCPRO1 (value);
3085 Vquit_flag = Qnil;
3087 while (specpdl_ptr != specpdl + count)
3089 --specpdl_ptr;
3091 if (specpdl_ptr->func != 0)
3092 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3093 /* Note that a "binding" of nil is really an unwind protect,
3094 so in that case the "old value" is a list of forms to evaluate. */
3095 else if (NILP (specpdl_ptr->symbol))
3096 Fprogn (specpdl_ptr->old_value);
3097 /* If the symbol is a list, it is really (SYMBOL WHERE
3098 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3099 frame. If WHERE is a buffer or frame, this indicates we
3100 bound a variable that had a buffer-local or frame-local
3101 binding. WHERE nil means that the variable had the default
3102 value when it was bound. CURRENT-BUFFER is the buffer that
3103 was current when the variable was bound. */
3104 else if (CONSP (specpdl_ptr->symbol))
3106 Lisp_Object symbol, where;
3108 symbol = XCAR (specpdl_ptr->symbol);
3109 where = XCAR (XCDR (specpdl_ptr->symbol));
3111 if (NILP (where))
3112 Fset_default (symbol, specpdl_ptr->old_value);
3113 else if (BUFFERP (where))
3114 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (where), 1);
3115 else
3116 set_internal (symbol, specpdl_ptr->old_value, NULL, 1);
3118 else
3120 /* If variable has a trivial value (no forwarding), we can
3121 just set it. No need to check for constant symbols here,
3122 since that was already done by specbind. */
3123 if (!MISCP (SYMBOL_VALUE (specpdl_ptr->symbol)))
3124 SET_SYMBOL_VALUE (specpdl_ptr->symbol, specpdl_ptr->old_value);
3125 else
3126 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3130 if (NILP (Vquit_flag) && quitf)
3131 Vquit_flag = Qt;
3133 UNGCPRO;
3134 return value;
3137 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3138 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3139 The debugger is entered when that frame exits, if the flag is non-nil. */)
3140 (level, flag)
3141 Lisp_Object level, flag;
3143 register struct backtrace *backlist = backtrace_list;
3144 register int i;
3146 CHECK_NUMBER (level);
3148 for (i = 0; backlist && i < XINT (level); i++)
3150 backlist = backlist->next;
3153 if (backlist)
3154 backlist->debug_on_exit = !NILP (flag);
3156 return flag;
3159 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3160 doc: /* Print a trace of Lisp function calls currently active.
3161 Output stream used is value of `standard-output'. */)
3164 register struct backtrace *backlist = backtrace_list;
3165 register int i;
3166 Lisp_Object tail;
3167 Lisp_Object tem;
3168 extern Lisp_Object Vprint_level;
3169 struct gcpro gcpro1;
3171 XSETFASTINT (Vprint_level, 3);
3173 tail = Qnil;
3174 GCPRO1 (tail);
3176 while (backlist)
3178 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3179 if (backlist->nargs == UNEVALLED)
3181 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3182 write_string ("\n", -1);
3184 else
3186 tem = *backlist->function;
3187 Fprin1 (tem, Qnil); /* This can QUIT */
3188 write_string ("(", -1);
3189 if (backlist->nargs == MANY)
3191 for (tail = *backlist->args, i = 0;
3192 !NILP (tail);
3193 tail = Fcdr (tail), i++)
3195 if (i) write_string (" ", -1);
3196 Fprin1 (Fcar (tail), Qnil);
3199 else
3201 for (i = 0; i < backlist->nargs; i++)
3203 if (i) write_string (" ", -1);
3204 Fprin1 (backlist->args[i], Qnil);
3207 write_string (")\n", -1);
3209 backlist = backlist->next;
3212 Vprint_level = Qnil;
3213 UNGCPRO;
3214 return Qnil;
3217 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3218 doc: /* Return the function and arguments NFRAMES up from current execution point.
3219 If that frame has not evaluated the arguments yet (or is a special form),
3220 the value is (nil FUNCTION ARG-FORMS...).
3221 If that frame has evaluated its arguments and called its function already,
3222 the value is (t FUNCTION ARG-VALUES...).
3223 A &rest arg is represented as the tail of the list ARG-VALUES.
3224 FUNCTION is whatever was supplied as car of evaluated list,
3225 or a lambda expression for macro calls.
3226 If NFRAMES is more than the number of frames, the value is nil. */)
3227 (nframes)
3228 Lisp_Object nframes;
3230 register struct backtrace *backlist = backtrace_list;
3231 register int i;
3232 Lisp_Object tem;
3234 CHECK_NATNUM (nframes);
3236 /* Find the frame requested. */
3237 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3238 backlist = backlist->next;
3240 if (!backlist)
3241 return Qnil;
3242 if (backlist->nargs == UNEVALLED)
3243 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3244 else
3246 if (backlist->nargs == MANY)
3247 tem = *backlist->args;
3248 else
3249 tem = Flist (backlist->nargs, backlist->args);
3251 return Fcons (Qt, Fcons (*backlist->function, tem));
3256 void
3257 syms_of_eval ()
3259 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3260 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3261 If Lisp code tries to make more than this many at once,
3262 an error is signaled.
3263 You can safely use a value considerably larger than the default value,
3264 if that proves inconveniently small. However, if you increase it too far,
3265 Emacs could run out of memory trying to make the stack bigger. */);
3267 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3268 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3270 This limit serves to catch infinite recursions for you before they cause
3271 actual stack overflow in C, which would be fatal for Emacs.
3272 You can safely make it considerably larger than its default value,
3273 if that proves inconveniently small. However, if you increase it too far,
3274 Emacs could overflow the real C stack, and crash. */);
3276 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3277 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3278 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'. */);
3279 Vquit_flag = Qnil;
3281 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3282 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3283 Note that `quit-flag' will still be set by typing C-g,
3284 so a quit will be signaled as soon as `inhibit-quit' is nil.
3285 To prevent this happening, set `quit-flag' to nil
3286 before making `inhibit-quit' nil. */);
3287 Vinhibit_quit = Qnil;
3289 Qinhibit_quit = intern ("inhibit-quit");
3290 staticpro (&Qinhibit_quit);
3292 Qautoload = intern ("autoload");
3293 staticpro (&Qautoload);
3295 Qdebug_on_error = intern ("debug-on-error");
3296 staticpro (&Qdebug_on_error);
3298 Qmacro = intern ("macro");
3299 staticpro (&Qmacro);
3301 Qdeclare = intern ("declare");
3302 staticpro (&Qdeclare);
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 Qdefvar = intern ("defvar");
3319 staticpro (&Qdefvar);
3321 Qand_rest = intern ("&rest");
3322 staticpro (&Qand_rest);
3324 Qand_optional = intern ("&optional");
3325 staticpro (&Qand_optional);
3327 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3328 doc: /* *Non-nil means errors display a backtrace buffer.
3329 More precisely, this happens for any error that is handled
3330 by the editor command loop.
3331 If the value is a list, an error only means to display a backtrace
3332 if one of its condition symbols appears in the list. */);
3333 Vstack_trace_on_error = Qnil;
3335 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3336 doc: /* *Non-nil means enter debugger if an error is signaled.
3337 Does not apply to errors handled by `condition-case' or those
3338 matched by `debug-ignored-errors'.
3339 If the value is a list, an error only means to enter the debugger
3340 if one of its condition symbols appears in the list.
3341 When you evaluate an expression interactively, this variable
3342 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3343 See also variable `debug-on-quit'. */);
3344 Vdebug_on_error = Qnil;
3346 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3347 doc: /* *List of errors for which the debugger should not be called.
3348 Each element may be a condition-name or a regexp that matches error messages.
3349 If any element applies to a given error, that error skips the debugger
3350 and just returns to top level.
3351 This overrides the variable `debug-on-error'.
3352 It does not apply to errors handled by `condition-case'. */);
3353 Vdebug_ignored_errors = Qnil;
3355 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3356 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3357 Does not apply if quit is handled by a `condition-case'.
3358 When you evaluate an expression interactively, this variable
3359 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3360 debug_on_quit = 0;
3362 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3363 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3365 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3366 doc: /* Non-nil means debugger may continue execution.
3367 This is nil when the debugger is called under circumstances where it
3368 might not be safe to continue. */);
3369 debugger_may_continue = 1;
3371 DEFVAR_LISP ("debugger", &Vdebugger,
3372 doc: /* Function to call to invoke debugger.
3373 If due to frame exit, args are `exit' and the value being returned;
3374 this function's value will be returned instead of that.
3375 If due to error, args are `error' and a list of the args to `signal'.
3376 If due to `apply' or `funcall' entry, one arg, `lambda'.
3377 If due to `eval' entry, one arg, t. */);
3378 Vdebugger = Qnil;
3380 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3381 doc: /* If non-nil, this is a function for `signal' to call.
3382 It receives the same arguments that `signal' was given.
3383 The Edebug package uses this to regain control. */);
3384 Vsignal_hook_function = Qnil;
3386 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3387 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3388 Note that `debug-on-error', `debug-on-quit' and friends
3389 still determine whether to handle the particular condition. */);
3390 Vdebug_on_signal = Qnil;
3392 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3393 doc: /* Function to process declarations in a macro definition.
3394 The function will be called with two args MACRO and DECL.
3395 MACRO is the name of the macro being defined.
3396 DECL is a list `(declare ...)' containing the declarations.
3397 The value the function returns is not used. */);
3398 Vmacro_declaration_function = Qnil;
3400 Vrun_hooks = intern ("run-hooks");
3401 staticpro (&Vrun_hooks);
3403 staticpro (&Vautoload_queue);
3404 Vautoload_queue = Qnil;
3405 staticpro (&Vsignaling_function);
3406 Vsignaling_function = Qnil;
3408 defsubr (&Sor);
3409 defsubr (&Sand);
3410 defsubr (&Sif);
3411 defsubr (&Scond);
3412 defsubr (&Sprogn);
3413 defsubr (&Sprog1);
3414 defsubr (&Sprog2);
3415 defsubr (&Ssetq);
3416 defsubr (&Squote);
3417 defsubr (&Sfunction);
3418 defsubr (&Sdefun);
3419 defsubr (&Sdefmacro);
3420 defsubr (&Sdefvar);
3421 defsubr (&Sdefvaralias);
3422 defsubr (&Sdefconst);
3423 defsubr (&Suser_variable_p);
3424 defsubr (&Slet);
3425 defsubr (&SletX);
3426 defsubr (&Swhile);
3427 defsubr (&Smacroexpand);
3428 defsubr (&Scatch);
3429 defsubr (&Sthrow);
3430 defsubr (&Sunwind_protect);
3431 defsubr (&Scondition_case);
3432 defsubr (&Ssignal);
3433 defsubr (&Sinteractive_p);
3434 defsubr (&Scommandp);
3435 defsubr (&Sautoload);
3436 defsubr (&Seval);
3437 defsubr (&Sapply);
3438 defsubr (&Sfuncall);
3439 defsubr (&Srun_hooks);
3440 defsubr (&Srun_hook_with_args);
3441 defsubr (&Srun_hook_with_args_until_success);
3442 defsubr (&Srun_hook_with_args_until_failure);
3443 defsubr (&Sfetch_bytecode);
3444 defsubr (&Sbacktrace_debug);
3445 defsubr (&Sbacktrace);
3446 defsubr (&Sbacktrace_frame);