*** empty log message ***
[emacs.git] / src / eval.c
blob1bb7cb809f909079332207f731ef6b697c5c14b4
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 any of `custom-type', `custom-loads' or `standard-value'
834 on its property list). */)
835 (variable)
836 Lisp_Object variable;
838 Lisp_Object documentation;
840 if (!SYMBOLP (variable))
841 return Qnil;
843 documentation = Fget (variable, Qvariable_documentation);
844 if (INTEGERP (documentation) && XINT (documentation) < 0)
845 return Qt;
846 if (STRINGP (documentation)
847 && ((unsigned char) SREF (documentation, 0) == '*'))
848 return Qt;
849 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
850 if (CONSP (documentation)
851 && STRINGP (XCAR (documentation))
852 && INTEGERP (XCDR (documentation))
853 && XINT (XCDR (documentation)) < 0)
854 return Qt;
855 /* Customizable? */
856 if ((!NILP (Fget (variable, intern ("custom-type"))))
857 || (!NILP (Fget (variable, intern ("custom-loads"))))
858 || (!NILP (Fget (variable, intern ("standard-value")))))
859 return Qt;
860 return Qnil;
863 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
864 doc: /* Bind variables according to VARLIST then eval BODY.
865 The value of the last form in BODY is returned.
866 Each element of VARLIST is a symbol (which is bound to nil)
867 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
868 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
869 usage: (let* VARLIST BODY...) */)
870 (args)
871 Lisp_Object args;
873 Lisp_Object varlist, val, elt;
874 int count = SPECPDL_INDEX ();
875 struct gcpro gcpro1, gcpro2, gcpro3;
877 GCPRO3 (args, elt, varlist);
879 varlist = Fcar (args);
880 while (!NILP (varlist))
882 QUIT;
883 elt = Fcar (varlist);
884 if (SYMBOLP (elt))
885 specbind (elt, Qnil);
886 else if (! NILP (Fcdr (Fcdr (elt))))
887 Fsignal (Qerror,
888 Fcons (build_string ("`let' bindings can have only one value-form"),
889 elt));
890 else
892 val = Feval (Fcar (Fcdr (elt)));
893 specbind (Fcar (elt), val);
895 varlist = Fcdr (varlist);
897 UNGCPRO;
898 val = Fprogn (Fcdr (args));
899 return unbind_to (count, val);
902 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
903 doc: /* Bind variables according to VARLIST then eval BODY.
904 The value of the last form in BODY is returned.
905 Each element of VARLIST is a symbol (which is bound to nil)
906 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
907 All the VALUEFORMs are evalled before any symbols are bound.
908 usage: (let VARLIST BODY...) */)
909 (args)
910 Lisp_Object args;
912 Lisp_Object *temps, tem;
913 register Lisp_Object elt, varlist;
914 int count = SPECPDL_INDEX ();
915 register int argnum;
916 struct gcpro gcpro1, gcpro2;
918 varlist = Fcar (args);
920 /* Make space to hold the values to give the bound variables */
921 elt = Flength (varlist);
922 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
924 /* Compute the values and store them in `temps' */
926 GCPRO2 (args, *temps);
927 gcpro2.nvars = 0;
929 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
931 QUIT;
932 elt = Fcar (varlist);
933 if (SYMBOLP (elt))
934 temps [argnum++] = Qnil;
935 else if (! NILP (Fcdr (Fcdr (elt))))
936 Fsignal (Qerror,
937 Fcons (build_string ("`let' bindings can have only one value-form"),
938 elt));
939 else
940 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
941 gcpro2.nvars = argnum;
943 UNGCPRO;
945 varlist = Fcar (args);
946 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
948 elt = Fcar (varlist);
949 tem = temps[argnum++];
950 if (SYMBOLP (elt))
951 specbind (elt, tem);
952 else
953 specbind (Fcar (elt), tem);
956 elt = Fprogn (Fcdr (args));
957 return unbind_to (count, elt);
960 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
961 doc: /* If TEST yields non-nil, eval BODY... and repeat.
962 The order of execution is thus TEST, BODY, TEST, BODY and so on
963 until TEST returns nil.
964 usage: (while TEST BODY...) */)
965 (args)
966 Lisp_Object args;
968 Lisp_Object test, body;
969 struct gcpro gcpro1, gcpro2;
971 GCPRO2 (test, body);
973 test = Fcar (args);
974 body = Fcdr (args);
975 while (!NILP (Feval (test)))
977 QUIT;
978 Fprogn (body);
981 UNGCPRO;
982 return Qnil;
985 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
986 doc: /* Return result of expanding macros at top level of FORM.
987 If FORM is not a macro call, it is returned unchanged.
988 Otherwise, the macro is expanded and the expansion is considered
989 in place of FORM. When a non-macro-call results, it is returned.
991 The second optional arg ENVIRONMENT specifies an environment of macro
992 definitions to shadow the loaded ones for use in file byte-compilation. */)
993 (form, environment)
994 Lisp_Object form;
995 Lisp_Object environment;
997 /* With cleanups from Hallvard Furuseth. */
998 register Lisp_Object expander, sym, def, tem;
1000 while (1)
1002 /* Come back here each time we expand a macro call,
1003 in case it expands into another macro call. */
1004 if (!CONSP (form))
1005 break;
1006 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1007 def = sym = XCAR (form);
1008 tem = Qnil;
1009 /* Trace symbols aliases to other symbols
1010 until we get a symbol that is not an alias. */
1011 while (SYMBOLP (def))
1013 QUIT;
1014 sym = def;
1015 tem = Fassq (sym, environment);
1016 if (NILP (tem))
1018 def = XSYMBOL (sym)->function;
1019 if (!EQ (def, Qunbound))
1020 continue;
1022 break;
1024 /* Right now TEM is the result from SYM in ENVIRONMENT,
1025 and if TEM is nil then DEF is SYM's function definition. */
1026 if (NILP (tem))
1028 /* SYM is not mentioned in ENVIRONMENT.
1029 Look at its function definition. */
1030 if (EQ (def, Qunbound) || !CONSP (def))
1031 /* Not defined or definition not suitable */
1032 break;
1033 if (EQ (XCAR (def), Qautoload))
1035 /* Autoloading function: will it be a macro when loaded? */
1036 tem = Fnth (make_number (4), def);
1037 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1038 /* Yes, load it and try again. */
1040 struct gcpro gcpro1;
1041 GCPRO1 (form);
1042 do_autoload (def, sym);
1043 UNGCPRO;
1044 continue;
1046 else
1047 break;
1049 else if (!EQ (XCAR (def), Qmacro))
1050 break;
1051 else expander = XCDR (def);
1053 else
1055 expander = XCDR (tem);
1056 if (NILP (expander))
1057 break;
1059 form = apply1 (expander, XCDR (form));
1061 return form;
1064 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1065 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1066 TAG is evalled to get the tag to use; it must not be nil.
1068 Then the BODY is executed.
1069 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1070 If no throw happens, `catch' returns the value of the last BODY form.
1071 If a throw happens, it specifies the value to return from `catch'.
1072 usage: (catch TAG BODY...) */)
1073 (args)
1074 Lisp_Object args;
1076 register Lisp_Object tag;
1077 struct gcpro gcpro1;
1079 GCPRO1 (args);
1080 tag = Feval (Fcar (args));
1081 UNGCPRO;
1082 return internal_catch (tag, Fprogn, Fcdr (args));
1085 /* Set up a catch, then call C function FUNC on argument ARG.
1086 FUNC should return a Lisp_Object.
1087 This is how catches are done from within C code. */
1089 Lisp_Object
1090 internal_catch (tag, func, arg)
1091 Lisp_Object tag;
1092 Lisp_Object (*func) ();
1093 Lisp_Object arg;
1095 /* This structure is made part of the chain `catchlist'. */
1096 struct catchtag c;
1098 /* Fill in the components of c, and put it on the list. */
1099 c.next = catchlist;
1100 c.tag = tag;
1101 c.val = Qnil;
1102 c.backlist = backtrace_list;
1103 c.handlerlist = handlerlist;
1104 c.lisp_eval_depth = lisp_eval_depth;
1105 c.pdlcount = SPECPDL_INDEX ();
1106 c.poll_suppress_count = poll_suppress_count;
1107 c.interrupt_input_blocked = interrupt_input_blocked;
1108 c.gcpro = gcprolist;
1109 c.byte_stack = byte_stack_list;
1110 catchlist = &c;
1112 /* Call FUNC. */
1113 if (! _setjmp (c.jmp))
1114 c.val = (*func) (arg);
1116 /* Throw works by a longjmp that comes right here. */
1117 catchlist = c.next;
1118 return c.val;
1121 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1122 jump to that CATCH, returning VALUE as the value of that catch.
1124 This is the guts Fthrow and Fsignal; they differ only in the way
1125 they choose the catch tag to throw to. A catch tag for a
1126 condition-case form has a TAG of Qnil.
1128 Before each catch is discarded, unbind all special bindings and
1129 execute all unwind-protect clauses made above that catch. Unwind
1130 the handler stack as we go, so that the proper handlers are in
1131 effect for each unwind-protect clause we run. At the end, restore
1132 some static info saved in CATCH, and longjmp to the location
1133 specified in the
1135 This is used for correct unwinding in Fthrow and Fsignal. */
1137 static void
1138 unwind_to_catch (catch, value)
1139 struct catchtag *catch;
1140 Lisp_Object value;
1142 register int last_time;
1144 /* Save the value in the tag. */
1145 catch->val = value;
1147 /* Restore the polling-suppression count. */
1148 set_poll_suppress_count (catch->poll_suppress_count);
1149 interrupt_input_blocked = catch->interrupt_input_blocked;
1153 last_time = catchlist == catch;
1155 /* Unwind the specpdl stack, and then restore the proper set of
1156 handlers. */
1157 unbind_to (catchlist->pdlcount, Qnil);
1158 handlerlist = catchlist->handlerlist;
1159 catchlist = catchlist->next;
1161 while (! last_time);
1163 byte_stack_list = catch->byte_stack;
1164 gcprolist = catch->gcpro;
1165 #ifdef DEBUG_GCPRO
1166 if (gcprolist != 0)
1167 gcpro_level = gcprolist->level + 1;
1168 else
1169 gcpro_level = 0;
1170 #endif
1171 backtrace_list = catch->backlist;
1172 lisp_eval_depth = catch->lisp_eval_depth;
1174 _longjmp (catch->jmp, 1);
1177 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1178 doc: /* Throw to the catch for TAG and return VALUE from it.
1179 Both TAG and VALUE are evalled. */)
1180 (tag, value)
1181 register Lisp_Object tag, value;
1183 register struct catchtag *c;
1185 while (1)
1187 if (!NILP (tag))
1188 for (c = catchlist; c; c = c->next)
1190 if (EQ (c->tag, tag))
1191 unwind_to_catch (c, value);
1193 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1198 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1199 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1200 If BODYFORM completes normally, its value is returned
1201 after executing the UNWINDFORMS.
1202 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1203 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1204 (args)
1205 Lisp_Object args;
1207 Lisp_Object val;
1208 int count = SPECPDL_INDEX ();
1210 record_unwind_protect (0, Fcdr (args));
1211 val = Feval (Fcar (args));
1212 return unbind_to (count, val);
1215 /* Chain of condition handlers currently in effect.
1216 The elements of this chain are contained in the stack frames
1217 of Fcondition_case and internal_condition_case.
1218 When an error is signaled (by calling Fsignal, below),
1219 this chain is searched for an element that applies. */
1221 struct handler *handlerlist;
1223 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1224 doc: /* Regain control when an error is signaled.
1225 Executes BODYFORM and returns its value if no error happens.
1226 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1227 where the BODY is made of Lisp expressions.
1229 A handler is applicable to an error
1230 if CONDITION-NAME is one of the error's condition names.
1231 If an error happens, the first applicable handler is run.
1233 The car of a handler may be a list of condition names
1234 instead of a single condition name.
1236 When a handler handles an error,
1237 control returns to the condition-case and the handler BODY... is executed
1238 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1239 VAR may be nil; then you do not get access to the signal information.
1241 The value of the last BODY form is returned from the condition-case.
1242 See also the function `signal' for more info.
1243 usage: (condition-case VAR BODYFORM HANDLERS...) */)
1244 (args)
1245 Lisp_Object args;
1247 Lisp_Object val;
1248 struct catchtag c;
1249 struct handler h;
1250 register Lisp_Object bodyform, handlers;
1251 volatile Lisp_Object var;
1253 var = Fcar (args);
1254 bodyform = Fcar (Fcdr (args));
1255 handlers = Fcdr (Fcdr (args));
1256 CHECK_SYMBOL (var);
1258 for (val = handlers; ! NILP (val); val = Fcdr (val))
1260 Lisp_Object tem;
1261 tem = Fcar (val);
1262 if (! (NILP (tem)
1263 || (CONSP (tem)
1264 && (SYMBOLP (XCAR (tem))
1265 || CONSP (XCAR (tem))))))
1266 error ("Invalid condition handler", tem);
1269 c.tag = Qnil;
1270 c.val = Qnil;
1271 c.backlist = backtrace_list;
1272 c.handlerlist = handlerlist;
1273 c.lisp_eval_depth = lisp_eval_depth;
1274 c.pdlcount = SPECPDL_INDEX ();
1275 c.poll_suppress_count = poll_suppress_count;
1276 c.interrupt_input_blocked = interrupt_input_blocked;
1277 c.gcpro = gcprolist;
1278 c.byte_stack = byte_stack_list;
1279 if (_setjmp (c.jmp))
1281 if (!NILP (h.var))
1282 specbind (h.var, c.val);
1283 val = Fprogn (Fcdr (h.chosen_clause));
1285 /* Note that this just undoes the binding of h.var; whoever
1286 longjumped to us unwound the stack to c.pdlcount before
1287 throwing. */
1288 unbind_to (c.pdlcount, Qnil);
1289 return val;
1291 c.next = catchlist;
1292 catchlist = &c;
1294 h.var = var;
1295 h.handler = handlers;
1296 h.next = handlerlist;
1297 h.tag = &c;
1298 handlerlist = &h;
1300 val = Feval (bodyform);
1301 catchlist = c.next;
1302 handlerlist = h.next;
1303 return val;
1306 /* Call the function BFUN with no arguments, catching errors within it
1307 according to HANDLERS. If there is an error, call HFUN with
1308 one argument which is the data that describes the error:
1309 (SIGNALNAME . DATA)
1311 HANDLERS can be a list of conditions to catch.
1312 If HANDLERS is Qt, catch all errors.
1313 If HANDLERS is Qerror, catch all errors
1314 but allow the debugger to run if that is enabled. */
1316 Lisp_Object
1317 internal_condition_case (bfun, handlers, hfun)
1318 Lisp_Object (*bfun) ();
1319 Lisp_Object handlers;
1320 Lisp_Object (*hfun) ();
1322 Lisp_Object val;
1323 struct catchtag c;
1324 struct handler h;
1326 #if 0 /* We now handle interrupt_input_blocked properly.
1327 What we still do not handle is exiting a signal handler. */
1328 abort ();
1329 #endif
1331 c.tag = Qnil;
1332 c.val = Qnil;
1333 c.backlist = backtrace_list;
1334 c.handlerlist = handlerlist;
1335 c.lisp_eval_depth = lisp_eval_depth;
1336 c.pdlcount = SPECPDL_INDEX ();
1337 c.poll_suppress_count = poll_suppress_count;
1338 c.interrupt_input_blocked = interrupt_input_blocked;
1339 c.gcpro = gcprolist;
1340 c.byte_stack = byte_stack_list;
1341 if (_setjmp (c.jmp))
1343 return (*hfun) (c.val);
1345 c.next = catchlist;
1346 catchlist = &c;
1347 h.handler = handlers;
1348 h.var = Qnil;
1349 h.next = handlerlist;
1350 h.tag = &c;
1351 handlerlist = &h;
1353 val = (*bfun) ();
1354 catchlist = c.next;
1355 handlerlist = h.next;
1356 return val;
1359 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1361 Lisp_Object
1362 internal_condition_case_1 (bfun, arg, handlers, hfun)
1363 Lisp_Object (*bfun) ();
1364 Lisp_Object arg;
1365 Lisp_Object handlers;
1366 Lisp_Object (*hfun) ();
1368 Lisp_Object val;
1369 struct catchtag c;
1370 struct handler h;
1372 c.tag = Qnil;
1373 c.val = Qnil;
1374 c.backlist = backtrace_list;
1375 c.handlerlist = handlerlist;
1376 c.lisp_eval_depth = lisp_eval_depth;
1377 c.pdlcount = SPECPDL_INDEX ();
1378 c.poll_suppress_count = poll_suppress_count;
1379 c.interrupt_input_blocked = interrupt_input_blocked;
1380 c.gcpro = gcprolist;
1381 c.byte_stack = byte_stack_list;
1382 if (_setjmp (c.jmp))
1384 return (*hfun) (c.val);
1386 c.next = catchlist;
1387 catchlist = &c;
1388 h.handler = handlers;
1389 h.var = Qnil;
1390 h.next = handlerlist;
1391 h.tag = &c;
1392 handlerlist = &h;
1394 val = (*bfun) (arg);
1395 catchlist = c.next;
1396 handlerlist = h.next;
1397 return val;
1401 /* Like internal_condition_case but call BFUN with NARGS as first,
1402 and ARGS as second argument. */
1404 Lisp_Object
1405 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1406 Lisp_Object (*bfun) ();
1407 int nargs;
1408 Lisp_Object *args;
1409 Lisp_Object handlers;
1410 Lisp_Object (*hfun) ();
1412 Lisp_Object val;
1413 struct catchtag c;
1414 struct handler h;
1416 c.tag = Qnil;
1417 c.val = Qnil;
1418 c.backlist = backtrace_list;
1419 c.handlerlist = handlerlist;
1420 c.lisp_eval_depth = lisp_eval_depth;
1421 c.pdlcount = SPECPDL_INDEX ();
1422 c.poll_suppress_count = poll_suppress_count;
1423 c.interrupt_input_blocked = interrupt_input_blocked;
1424 c.gcpro = gcprolist;
1425 c.byte_stack = byte_stack_list;
1426 if (_setjmp (c.jmp))
1428 return (*hfun) (c.val);
1430 c.next = catchlist;
1431 catchlist = &c;
1432 h.handler = handlers;
1433 h.var = Qnil;
1434 h.next = handlerlist;
1435 h.tag = &c;
1436 handlerlist = &h;
1438 val = (*bfun) (nargs, args);
1439 catchlist = c.next;
1440 handlerlist = h.next;
1441 return val;
1445 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1446 Lisp_Object, Lisp_Object,
1447 Lisp_Object *));
1449 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1450 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1451 This function does not return.
1453 An error symbol is a symbol with an `error-conditions' property
1454 that is a list of condition names.
1455 A handler for any of those names will get to handle this signal.
1456 The symbol `error' should normally be one of them.
1458 DATA should be a list. Its elements are printed as part of the error message.
1459 If the signal is handled, DATA is made available to the handler.
1460 See also the function `condition-case'. */)
1461 (error_symbol, data)
1462 Lisp_Object error_symbol, data;
1464 /* When memory is full, ERROR-SYMBOL is nil,
1465 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1466 That is a special case--don't do this in other situations. */
1467 register struct handler *allhandlers = handlerlist;
1468 Lisp_Object conditions;
1469 extern int gc_in_progress;
1470 extern int waiting_for_input;
1471 Lisp_Object debugger_value;
1472 Lisp_Object string;
1473 Lisp_Object real_error_symbol;
1474 struct backtrace *bp;
1476 immediate_quit = handling_signal = 0;
1477 if (gc_in_progress || waiting_for_input)
1478 abort ();
1480 if (NILP (error_symbol))
1481 real_error_symbol = Fcar (data);
1482 else
1483 real_error_symbol = error_symbol;
1485 #if 0 /* rms: I don't know why this was here,
1486 but it is surely wrong for an error that is handled. */
1487 #ifdef HAVE_X_WINDOWS
1488 if (display_hourglass_p)
1489 cancel_hourglass ();
1490 #endif
1491 #endif
1493 /* This hook is used by edebug. */
1494 if (! NILP (Vsignal_hook_function)
1495 && ! NILP (error_symbol))
1496 call2 (Vsignal_hook_function, error_symbol, data);
1498 conditions = Fget (real_error_symbol, Qerror_conditions);
1500 /* Remember from where signal was called. Skip over the frame for
1501 `signal' itself. If a frame for `error' follows, skip that,
1502 too. Don't do this when ERROR_SYMBOL is nil, because that
1503 is a memory-full error. */
1504 Vsignaling_function = Qnil;
1505 if (backtrace_list && !NILP (error_symbol))
1507 bp = backtrace_list->next;
1508 if (bp && bp->function && EQ (*bp->function, Qerror))
1509 bp = bp->next;
1510 if (bp && bp->function)
1511 Vsignaling_function = *bp->function;
1514 for (; handlerlist; handlerlist = handlerlist->next)
1516 register Lisp_Object clause;
1518 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1519 max_lisp_eval_depth = lisp_eval_depth + 20;
1521 if (specpdl_size + 40 > max_specpdl_size)
1522 max_specpdl_size = specpdl_size + 40;
1524 clause = find_handler_clause (handlerlist->handler, conditions,
1525 error_symbol, data, &debugger_value);
1527 if (EQ (clause, Qlambda))
1529 /* We can't return values to code which signaled an error, but we
1530 can continue code which has signaled a quit. */
1531 if (EQ (real_error_symbol, Qquit))
1532 return Qnil;
1533 else
1534 error ("Cannot return from the debugger in an error");
1537 if (!NILP (clause))
1539 Lisp_Object unwind_data;
1540 struct handler *h = handlerlist;
1542 handlerlist = allhandlers;
1544 if (NILP (error_symbol))
1545 unwind_data = data;
1546 else
1547 unwind_data = Fcons (error_symbol, data);
1548 h->chosen_clause = clause;
1549 unwind_to_catch (h->tag, unwind_data);
1553 handlerlist = allhandlers;
1554 /* If no handler is present now, try to run the debugger,
1555 and if that fails, throw to top level. */
1556 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1557 if (catchlist != 0)
1558 Fthrow (Qtop_level, Qt);
1560 if (! NILP (error_symbol))
1561 data = Fcons (error_symbol, data);
1563 string = Ferror_message_string (data);
1564 fatal ("%s", SDATA (string), 0);
1567 /* Return nonzero iff LIST is a non-nil atom or
1568 a list containing one of CONDITIONS. */
1570 static int
1571 wants_debugger (list, conditions)
1572 Lisp_Object list, conditions;
1574 if (NILP (list))
1575 return 0;
1576 if (! CONSP (list))
1577 return 1;
1579 while (CONSP (conditions))
1581 Lisp_Object this, tail;
1582 this = XCAR (conditions);
1583 for (tail = list; CONSP (tail); tail = XCDR (tail))
1584 if (EQ (XCAR (tail), this))
1585 return 1;
1586 conditions = XCDR (conditions);
1588 return 0;
1591 /* Return 1 if an error with condition-symbols CONDITIONS,
1592 and described by SIGNAL-DATA, should skip the debugger
1593 according to debugger-ignored-errors. */
1595 static int
1596 skip_debugger (conditions, data)
1597 Lisp_Object conditions, data;
1599 Lisp_Object tail;
1600 int first_string = 1;
1601 Lisp_Object error_message;
1603 error_message = Qnil;
1604 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1606 if (STRINGP (XCAR (tail)))
1608 if (first_string)
1610 error_message = Ferror_message_string (data);
1611 first_string = 0;
1614 if (fast_string_match (XCAR (tail), error_message) >= 0)
1615 return 1;
1617 else
1619 Lisp_Object contail;
1621 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1622 if (EQ (XCAR (tail), XCAR (contail)))
1623 return 1;
1627 return 0;
1630 /* Value of Qlambda means we have called debugger and user has continued.
1631 There are two ways to pass SIG and DATA:
1632 = SIG is the error symbol, and DATA is the rest of the data.
1633 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1634 This is for memory-full errors only.
1636 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1638 static Lisp_Object
1639 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1640 Lisp_Object handlers, conditions, sig, data;
1641 Lisp_Object *debugger_value_ptr;
1643 register Lisp_Object h;
1644 register Lisp_Object tem;
1646 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1647 return Qt;
1648 /* error is used similarly, but means print an error message
1649 and run the debugger if that is enabled. */
1650 if (EQ (handlers, Qerror)
1651 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1652 there is a handler. */
1654 int count = SPECPDL_INDEX ();
1655 int debugger_called = 0;
1656 Lisp_Object sig_symbol, combined_data;
1657 /* This is set to 1 if we are handling a memory-full error,
1658 because these must not run the debugger.
1659 (There is no room in memory to do that!) */
1660 int no_debugger = 0;
1662 if (NILP (sig))
1664 combined_data = data;
1665 sig_symbol = Fcar (data);
1666 no_debugger = 1;
1668 else
1670 combined_data = Fcons (sig, data);
1671 sig_symbol = sig;
1674 if (wants_debugger (Vstack_trace_on_error, conditions))
1676 #ifdef PROTOTYPES
1677 internal_with_output_to_temp_buffer ("*Backtrace*",
1678 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1679 Qnil);
1680 #else
1681 internal_with_output_to_temp_buffer ("*Backtrace*",
1682 Fbacktrace, Qnil);
1683 #endif
1685 if (! no_debugger
1686 && (EQ (sig_symbol, Qquit)
1687 ? debug_on_quit
1688 : wants_debugger (Vdebug_on_error, conditions))
1689 && ! skip_debugger (conditions, combined_data)
1690 && when_entered_debugger < num_nonmacro_input_events)
1692 specbind (Qdebug_on_error, Qnil);
1693 *debugger_value_ptr
1694 = call_debugger (Fcons (Qerror,
1695 Fcons (combined_data, Qnil)));
1696 debugger_called = 1;
1698 /* If there is no handler, return saying whether we ran the debugger. */
1699 if (EQ (handlers, Qerror))
1701 if (debugger_called)
1702 return unbind_to (count, Qlambda);
1703 return Qt;
1706 for (h = handlers; CONSP (h); h = Fcdr (h))
1708 Lisp_Object handler, condit;
1710 handler = Fcar (h);
1711 if (!CONSP (handler))
1712 continue;
1713 condit = Fcar (handler);
1714 /* Handle a single condition name in handler HANDLER. */
1715 if (SYMBOLP (condit))
1717 tem = Fmemq (Fcar (handler), conditions);
1718 if (!NILP (tem))
1719 return handler;
1721 /* Handle a list of condition names in handler HANDLER. */
1722 else if (CONSP (condit))
1724 while (CONSP (condit))
1726 tem = Fmemq (Fcar (condit), conditions);
1727 if (!NILP (tem))
1728 return handler;
1729 condit = XCDR (condit);
1733 return Qnil;
1736 /* dump an error message; called like printf */
1738 /* VARARGS 1 */
1739 void
1740 error (m, a1, a2, a3)
1741 char *m;
1742 char *a1, *a2, *a3;
1744 char buf[200];
1745 int size = 200;
1746 int mlen;
1747 char *buffer = buf;
1748 char *args[3];
1749 int allocated = 0;
1750 Lisp_Object string;
1752 args[0] = a1;
1753 args[1] = a2;
1754 args[2] = a3;
1756 mlen = strlen (m);
1758 while (1)
1760 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1761 if (used < size)
1762 break;
1763 size *= 2;
1764 if (allocated)
1765 buffer = (char *) xrealloc (buffer, size);
1766 else
1768 buffer = (char *) xmalloc (size);
1769 allocated = 1;
1773 string = build_string (buffer);
1774 if (allocated)
1775 xfree (buffer);
1777 Fsignal (Qerror, Fcons (string, Qnil));
1778 abort ();
1781 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1782 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1783 This means it contains a description for how to read arguments to give it.
1784 The value is nil for an invalid function or a symbol with no function
1785 definition.
1787 Interactively callable functions include strings and vectors (treated
1788 as keyboard macros), lambda-expressions that contain a top-level call
1789 to `interactive', autoload definitions made by `autoload' with non-nil
1790 fourth argument, and some of the built-in functions of Lisp.
1792 Also, a symbol satisfies `commandp' if its function definition does so.
1794 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1795 then strings and vectors are not accepted. */)
1796 (function, for_call_interactively)
1797 Lisp_Object function, for_call_interactively;
1799 register Lisp_Object fun;
1800 register Lisp_Object funcar;
1802 fun = function;
1804 fun = indirect_function (fun);
1805 if (EQ (fun, Qunbound))
1806 return Qnil;
1808 /* Emacs primitives are interactive if their DEFUN specifies an
1809 interactive spec. */
1810 if (SUBRP (fun))
1812 if (XSUBR (fun)->prompt)
1813 return Qt;
1814 else
1815 return Qnil;
1818 /* Bytecode objects are interactive if they are long enough to
1819 have an element whose index is COMPILED_INTERACTIVE, which is
1820 where the interactive spec is stored. */
1821 else if (COMPILEDP (fun))
1822 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1823 ? Qt : Qnil);
1825 /* Strings and vectors are keyboard macros. */
1826 if (NILP (for_call_interactively) && (STRINGP (fun) || VECTORP (fun)))
1827 return Qt;
1829 /* Lists may represent commands. */
1830 if (!CONSP (fun))
1831 return Qnil;
1832 funcar = Fcar (fun);
1833 if (!SYMBOLP (funcar))
1834 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1835 if (EQ (funcar, Qlambda))
1836 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1837 if (EQ (funcar, Qautoload))
1838 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1839 else
1840 return Qnil;
1843 /* ARGSUSED */
1844 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1845 doc: /* Define FUNCTION to autoload from FILE.
1846 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1847 Third arg DOCSTRING is documentation for the function.
1848 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1849 Fifth arg TYPE indicates the type of the object:
1850 nil or omitted says FUNCTION is a function,
1851 `keymap' says FUNCTION is really a keymap, and
1852 `macro' or t says FUNCTION is really a macro.
1853 Third through fifth args give info about the real definition.
1854 They default to nil.
1855 If FUNCTION is already defined other than as an autoload,
1856 this does nothing and returns nil. */)
1857 (function, file, docstring, interactive, type)
1858 Lisp_Object function, file, docstring, interactive, type;
1860 #ifdef NO_ARG_ARRAY
1861 Lisp_Object args[4];
1862 #endif
1864 CHECK_SYMBOL (function);
1865 CHECK_STRING (file);
1867 /* If function is defined and not as an autoload, don't override */
1868 if (!EQ (XSYMBOL (function)->function, Qunbound)
1869 && !(CONSP (XSYMBOL (function)->function)
1870 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1871 return Qnil;
1873 if (NILP (Vpurify_flag))
1874 /* Only add entries after dumping, because the ones before are
1875 not useful and else we get loads of them from the loaddefs.el. */
1876 LOADHIST_ATTACH (Fcons (Qautoload, function));
1878 #ifdef NO_ARG_ARRAY
1879 args[0] = file;
1880 args[1] = docstring;
1881 args[2] = interactive;
1882 args[3] = type;
1884 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1885 #else /* NO_ARG_ARRAY */
1886 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1887 #endif /* not NO_ARG_ARRAY */
1890 Lisp_Object
1891 un_autoload (oldqueue)
1892 Lisp_Object oldqueue;
1894 register Lisp_Object queue, first, second;
1896 /* Queue to unwind is current value of Vautoload_queue.
1897 oldqueue is the shadowed value to leave in Vautoload_queue. */
1898 queue = Vautoload_queue;
1899 Vautoload_queue = oldqueue;
1900 while (CONSP (queue))
1902 first = Fcar (queue);
1903 second = Fcdr (first);
1904 first = Fcar (first);
1905 if (EQ (second, Qnil))
1906 Vfeatures = first;
1907 else
1908 Ffset (first, second);
1909 queue = Fcdr (queue);
1911 return Qnil;
1914 /* Load an autoloaded function.
1915 FUNNAME is the symbol which is the function's name.
1916 FUNDEF is the autoload definition (a list). */
1918 void
1919 do_autoload (fundef, funname)
1920 Lisp_Object fundef, funname;
1922 int count = SPECPDL_INDEX ();
1923 Lisp_Object fun, queue, first, second;
1924 struct gcpro gcpro1, gcpro2, gcpro3;
1926 /* This is to make sure that loadup.el gives a clear picture
1927 of what files are preloaded and when. */
1928 if (! NILP (Vpurify_flag))
1929 error ("Attempt to autoload %s while preparing to dump",
1930 SDATA (SYMBOL_NAME (funname)));
1932 fun = funname;
1933 CHECK_SYMBOL (funname);
1934 GCPRO3 (fun, funname, fundef);
1936 /* Preserve the match data. */
1937 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1939 /* Value saved here is to be restored into Vautoload_queue. */
1940 record_unwind_protect (un_autoload, Vautoload_queue);
1941 Vautoload_queue = Qt;
1942 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1944 /* Save the old autoloads, in case we ever do an unload. */
1945 queue = Vautoload_queue;
1946 while (CONSP (queue))
1948 first = Fcar (queue);
1949 second = Fcdr (first);
1950 first = Fcar (first);
1952 /* Note: This test is subtle. The cdr of an autoload-queue entry
1953 may be an atom if the autoload entry was generated by a defalias
1954 or fset. */
1955 if (CONSP (second))
1956 Fput (first, Qautoload, (Fcdr (second)));
1958 queue = Fcdr (queue);
1961 /* Once loading finishes, don't undo it. */
1962 Vautoload_queue = Qt;
1963 unbind_to (count, Qnil);
1965 fun = Findirect_function (fun);
1967 if (!NILP (Fequal (fun, fundef)))
1968 error ("Autoloading failed to define function %s",
1969 SDATA (SYMBOL_NAME (funname)));
1970 UNGCPRO;
1974 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1975 doc: /* Evaluate FORM and return its value. */)
1976 (form)
1977 Lisp_Object form;
1979 Lisp_Object fun, val, original_fun, original_args;
1980 Lisp_Object funcar;
1981 struct backtrace backtrace;
1982 struct gcpro gcpro1, gcpro2, gcpro3;
1984 if (handling_signal)
1985 abort ();
1987 if (SYMBOLP (form))
1988 return Fsymbol_value (form);
1989 if (!CONSP (form))
1990 return form;
1992 QUIT;
1993 if (consing_since_gc > gc_cons_threshold)
1995 GCPRO1 (form);
1996 Fgarbage_collect ();
1997 UNGCPRO;
2000 if (++lisp_eval_depth > max_lisp_eval_depth)
2002 if (max_lisp_eval_depth < 100)
2003 max_lisp_eval_depth = 100;
2004 if (lisp_eval_depth > max_lisp_eval_depth)
2005 error ("Lisp nesting exceeds max-lisp-eval-depth");
2008 original_fun = Fcar (form);
2009 original_args = Fcdr (form);
2011 backtrace.next = backtrace_list;
2012 backtrace_list = &backtrace;
2013 backtrace.function = &original_fun; /* This also protects them from gc */
2014 backtrace.args = &original_args;
2015 backtrace.nargs = UNEVALLED;
2016 backtrace.evalargs = 1;
2017 backtrace.debug_on_exit = 0;
2019 if (debug_on_next_call)
2020 do_debug_on_call (Qt);
2022 /* At this point, only original_fun and original_args
2023 have values that will be used below */
2024 retry:
2025 fun = Findirect_function (original_fun);
2027 if (SUBRP (fun))
2029 Lisp_Object numargs;
2030 Lisp_Object argvals[8];
2031 Lisp_Object args_left;
2032 register int i, maxargs;
2034 args_left = original_args;
2035 numargs = Flength (args_left);
2037 if (XINT (numargs) < XSUBR (fun)->min_args ||
2038 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2039 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2041 if (XSUBR (fun)->max_args == UNEVALLED)
2043 backtrace.evalargs = 0;
2044 val = (*XSUBR (fun)->function) (args_left);
2045 goto done;
2048 if (XSUBR (fun)->max_args == MANY)
2050 /* Pass a vector of evaluated arguments */
2051 Lisp_Object *vals;
2052 register int argnum = 0;
2054 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2056 GCPRO3 (args_left, fun, fun);
2057 gcpro3.var = vals;
2058 gcpro3.nvars = 0;
2060 while (!NILP (args_left))
2062 vals[argnum++] = Feval (Fcar (args_left));
2063 args_left = Fcdr (args_left);
2064 gcpro3.nvars = argnum;
2067 backtrace.args = vals;
2068 backtrace.nargs = XINT (numargs);
2070 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2071 UNGCPRO;
2072 goto done;
2075 GCPRO3 (args_left, fun, fun);
2076 gcpro3.var = argvals;
2077 gcpro3.nvars = 0;
2079 maxargs = XSUBR (fun)->max_args;
2080 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2082 argvals[i] = Feval (Fcar (args_left));
2083 gcpro3.nvars = ++i;
2086 UNGCPRO;
2088 backtrace.args = argvals;
2089 backtrace.nargs = XINT (numargs);
2091 switch (i)
2093 case 0:
2094 val = (*XSUBR (fun)->function) ();
2095 goto done;
2096 case 1:
2097 val = (*XSUBR (fun)->function) (argvals[0]);
2098 goto done;
2099 case 2:
2100 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2101 goto done;
2102 case 3:
2103 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2104 argvals[2]);
2105 goto done;
2106 case 4:
2107 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2108 argvals[2], argvals[3]);
2109 goto done;
2110 case 5:
2111 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2112 argvals[3], argvals[4]);
2113 goto done;
2114 case 6:
2115 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2116 argvals[3], argvals[4], argvals[5]);
2117 goto done;
2118 case 7:
2119 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2120 argvals[3], argvals[4], argvals[5],
2121 argvals[6]);
2122 goto done;
2124 case 8:
2125 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2126 argvals[3], argvals[4], argvals[5],
2127 argvals[6], argvals[7]);
2128 goto done;
2130 default:
2131 /* Someone has created a subr that takes more arguments than
2132 is supported by this code. We need to either rewrite the
2133 subr to use a different argument protocol, or add more
2134 cases to this switch. */
2135 abort ();
2138 if (COMPILEDP (fun))
2139 val = apply_lambda (fun, original_args, 1);
2140 else
2142 if (!CONSP (fun))
2143 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2144 funcar = Fcar (fun);
2145 if (!SYMBOLP (funcar))
2146 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2147 if (EQ (funcar, Qautoload))
2149 do_autoload (fun, original_fun);
2150 goto retry;
2152 if (EQ (funcar, Qmacro))
2153 val = Feval (apply1 (Fcdr (fun), original_args));
2154 else if (EQ (funcar, Qlambda))
2155 val = apply_lambda (fun, original_args, 1);
2156 else
2157 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2159 done:
2160 lisp_eval_depth--;
2161 if (backtrace.debug_on_exit)
2162 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2163 backtrace_list = backtrace.next;
2165 #ifdef HAVE_CARBON
2166 mac_check_for_quit_char();
2167 #endif
2168 return val;
2171 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2172 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2173 Then return the value FUNCTION returns.
2174 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2175 usage: (apply FUNCTION &rest ARGUMENTS) */)
2176 (nargs, args)
2177 int nargs;
2178 Lisp_Object *args;
2180 register int i, numargs;
2181 register Lisp_Object spread_arg;
2182 register Lisp_Object *funcall_args;
2183 Lisp_Object fun;
2184 struct gcpro gcpro1;
2186 fun = args [0];
2187 funcall_args = 0;
2188 spread_arg = args [nargs - 1];
2189 CHECK_LIST (spread_arg);
2191 numargs = XINT (Flength (spread_arg));
2193 if (numargs == 0)
2194 return Ffuncall (nargs - 1, args);
2195 else if (numargs == 1)
2197 args [nargs - 1] = XCAR (spread_arg);
2198 return Ffuncall (nargs, args);
2201 numargs += nargs - 2;
2203 fun = indirect_function (fun);
2204 if (EQ (fun, Qunbound))
2206 /* Let funcall get the error */
2207 fun = args[0];
2208 goto funcall;
2211 if (SUBRP (fun))
2213 if (numargs < XSUBR (fun)->min_args
2214 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2215 goto funcall; /* Let funcall get the error */
2216 else if (XSUBR (fun)->max_args > numargs)
2218 /* Avoid making funcall cons up a yet another new vector of arguments
2219 by explicitly supplying nil's for optional values */
2220 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2221 * sizeof (Lisp_Object));
2222 for (i = numargs; i < XSUBR (fun)->max_args;)
2223 funcall_args[++i] = Qnil;
2224 GCPRO1 (*funcall_args);
2225 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2228 funcall:
2229 /* We add 1 to numargs because funcall_args includes the
2230 function itself as well as its arguments. */
2231 if (!funcall_args)
2233 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2234 * sizeof (Lisp_Object));
2235 GCPRO1 (*funcall_args);
2236 gcpro1.nvars = 1 + numargs;
2239 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2240 /* Spread the last arg we got. Its first element goes in
2241 the slot that it used to occupy, hence this value of I. */
2242 i = nargs - 1;
2243 while (!NILP (spread_arg))
2245 funcall_args [i++] = XCAR (spread_arg);
2246 spread_arg = XCDR (spread_arg);
2249 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2252 /* Run hook variables in various ways. */
2254 enum run_hooks_condition {to_completion, until_success, until_failure};
2255 static Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
2256 enum run_hooks_condition));
2258 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2259 doc: /* Run each hook in HOOKS. Major mode functions use this.
2260 Each argument should be a symbol, a hook variable.
2261 These symbols are processed in the order specified.
2262 If a hook symbol has a non-nil value, that value may be a function
2263 or a list of functions to be called to run the hook.
2264 If the value is a function, it is called with no arguments.
2265 If it is a list, the elements are called, in order, with no arguments.
2267 Do not use `make-local-variable' to make a hook variable buffer-local.
2268 Instead, use `add-hook' and specify t for the LOCAL argument.
2269 usage: (run-hooks &rest HOOKS) */)
2270 (nargs, args)
2271 int nargs;
2272 Lisp_Object *args;
2274 Lisp_Object hook[1];
2275 register int i;
2277 for (i = 0; i < nargs; i++)
2279 hook[0] = args[i];
2280 run_hook_with_args (1, hook, to_completion);
2283 return Qnil;
2286 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2287 Srun_hook_with_args, 1, MANY, 0,
2288 doc: /* Run HOOK with the specified arguments ARGS.
2289 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2290 value, that value may be a function or a list of functions to be
2291 called to run the hook. If the value is a function, it is called with
2292 the given arguments and its return value is returned. If it is a list
2293 of functions, those functions are called, in order,
2294 with the given arguments ARGS.
2295 It is best not to depend on the value return by `run-hook-with-args',
2296 as that may change.
2298 Do not use `make-local-variable' to make a hook variable buffer-local.
2299 Instead, use `add-hook' and specify t for the LOCAL argument.
2300 usage: (run-hook-with-args HOOK &rest ARGS) */)
2301 (nargs, args)
2302 int nargs;
2303 Lisp_Object *args;
2305 return run_hook_with_args (nargs, args, to_completion);
2308 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2309 Srun_hook_with_args_until_success, 1, MANY, 0,
2310 doc: /* Run HOOK with the specified arguments ARGS.
2311 HOOK should be a symbol, a hook variable. Its value should
2312 be a list of functions. We call those functions, one by one,
2313 passing arguments ARGS to each of them, until one of them
2314 returns a non-nil value. Then we return that value.
2315 If all the functions return nil, we return nil.
2317 Do not use `make-local-variable' to make a hook variable buffer-local.
2318 Instead, use `add-hook' and specify t for the LOCAL argument.
2319 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2320 (nargs, args)
2321 int nargs;
2322 Lisp_Object *args;
2324 return run_hook_with_args (nargs, args, until_success);
2327 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2328 Srun_hook_with_args_until_failure, 1, MANY, 0,
2329 doc: /* Run HOOK with the specified arguments ARGS.
2330 HOOK should be a symbol, a hook variable. Its value should
2331 be a list of functions. We call those functions, one by one,
2332 passing arguments ARGS to each of them, until one of them
2333 returns nil. Then we return nil.
2334 If all the functions return non-nil, we return non-nil.
2336 Do not use `make-local-variable' to make a hook variable buffer-local.
2337 Instead, use `add-hook' and specify t for the LOCAL argument.
2338 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2339 (nargs, args)
2340 int nargs;
2341 Lisp_Object *args;
2343 return run_hook_with_args (nargs, args, until_failure);
2346 /* ARGS[0] should be a hook symbol.
2347 Call each of the functions in the hook value, passing each of them
2348 as arguments all the rest of ARGS (all NARGS - 1 elements).
2349 COND specifies a condition to test after each call
2350 to decide whether to stop.
2351 The caller (or its caller, etc) must gcpro all of ARGS,
2352 except that it isn't necessary to gcpro ARGS[0]. */
2354 static Lisp_Object
2355 run_hook_with_args (nargs, args, cond)
2356 int nargs;
2357 Lisp_Object *args;
2358 enum run_hooks_condition cond;
2360 Lisp_Object sym, val, ret;
2361 Lisp_Object globals;
2362 struct gcpro gcpro1, gcpro2, gcpro3;
2364 /* If we are dying or still initializing,
2365 don't do anything--it would probably crash if we tried. */
2366 if (NILP (Vrun_hooks))
2367 return Qnil;
2369 sym = args[0];
2370 val = find_symbol_value (sym);
2371 ret = (cond == until_failure ? Qt : Qnil);
2373 if (EQ (val, Qunbound) || NILP (val))
2374 return ret;
2375 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2377 args[0] = val;
2378 return Ffuncall (nargs, args);
2380 else
2382 globals = Qnil;
2383 GCPRO3 (sym, val, globals);
2385 for (;
2386 CONSP (val) && ((cond == to_completion)
2387 || (cond == until_success ? NILP (ret)
2388 : !NILP (ret)));
2389 val = XCDR (val))
2391 if (EQ (XCAR (val), Qt))
2393 /* t indicates this hook has a local binding;
2394 it means to run the global binding too. */
2396 for (globals = Fdefault_value (sym);
2397 CONSP (globals) && ((cond == to_completion)
2398 || (cond == until_success ? NILP (ret)
2399 : !NILP (ret)));
2400 globals = XCDR (globals))
2402 args[0] = XCAR (globals);
2403 /* In a global value, t should not occur. If it does, we
2404 must ignore it to avoid an endless loop. */
2405 if (!EQ (args[0], Qt))
2406 ret = Ffuncall (nargs, args);
2409 else
2411 args[0] = XCAR (val);
2412 ret = Ffuncall (nargs, args);
2416 UNGCPRO;
2417 return ret;
2421 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2422 present value of that symbol.
2423 Call each element of FUNLIST,
2424 passing each of them the rest of ARGS.
2425 The caller (or its caller, etc) must gcpro all of ARGS,
2426 except that it isn't necessary to gcpro ARGS[0]. */
2428 Lisp_Object
2429 run_hook_list_with_args (funlist, nargs, args)
2430 Lisp_Object funlist;
2431 int nargs;
2432 Lisp_Object *args;
2434 Lisp_Object sym;
2435 Lisp_Object val;
2436 Lisp_Object globals;
2437 struct gcpro gcpro1, gcpro2, gcpro3;
2439 sym = args[0];
2440 globals = Qnil;
2441 GCPRO3 (sym, val, globals);
2443 for (val = funlist; CONSP (val); val = XCDR (val))
2445 if (EQ (XCAR (val), Qt))
2447 /* t indicates this hook has a local binding;
2448 it means to run the global binding too. */
2450 for (globals = Fdefault_value (sym);
2451 CONSP (globals);
2452 globals = XCDR (globals))
2454 args[0] = XCAR (globals);
2455 /* In a global value, t should not occur. If it does, we
2456 must ignore it to avoid an endless loop. */
2457 if (!EQ (args[0], Qt))
2458 Ffuncall (nargs, args);
2461 else
2463 args[0] = XCAR (val);
2464 Ffuncall (nargs, args);
2467 UNGCPRO;
2468 return Qnil;
2471 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2473 void
2474 run_hook_with_args_2 (hook, arg1, arg2)
2475 Lisp_Object hook, arg1, arg2;
2477 Lisp_Object temp[3];
2478 temp[0] = hook;
2479 temp[1] = arg1;
2480 temp[2] = arg2;
2482 Frun_hook_with_args (3, temp);
2485 /* Apply fn to arg */
2486 Lisp_Object
2487 apply1 (fn, arg)
2488 Lisp_Object fn, arg;
2490 struct gcpro gcpro1;
2492 GCPRO1 (fn);
2493 if (NILP (arg))
2494 RETURN_UNGCPRO (Ffuncall (1, &fn));
2495 gcpro1.nvars = 2;
2496 #ifdef NO_ARG_ARRAY
2498 Lisp_Object args[2];
2499 args[0] = fn;
2500 args[1] = arg;
2501 gcpro1.var = args;
2502 RETURN_UNGCPRO (Fapply (2, args));
2504 #else /* not NO_ARG_ARRAY */
2505 RETURN_UNGCPRO (Fapply (2, &fn));
2506 #endif /* not NO_ARG_ARRAY */
2509 /* Call function fn on no arguments */
2510 Lisp_Object
2511 call0 (fn)
2512 Lisp_Object fn;
2514 struct gcpro gcpro1;
2516 GCPRO1 (fn);
2517 RETURN_UNGCPRO (Ffuncall (1, &fn));
2520 /* Call function fn with 1 argument arg1 */
2521 /* ARGSUSED */
2522 Lisp_Object
2523 call1 (fn, arg1)
2524 Lisp_Object fn, arg1;
2526 struct gcpro gcpro1;
2527 #ifdef NO_ARG_ARRAY
2528 Lisp_Object args[2];
2530 args[0] = fn;
2531 args[1] = arg1;
2532 GCPRO1 (args[0]);
2533 gcpro1.nvars = 2;
2534 RETURN_UNGCPRO (Ffuncall (2, args));
2535 #else /* not NO_ARG_ARRAY */
2536 GCPRO1 (fn);
2537 gcpro1.nvars = 2;
2538 RETURN_UNGCPRO (Ffuncall (2, &fn));
2539 #endif /* not NO_ARG_ARRAY */
2542 /* Call function fn with 2 arguments arg1, arg2 */
2543 /* ARGSUSED */
2544 Lisp_Object
2545 call2 (fn, arg1, arg2)
2546 Lisp_Object fn, arg1, arg2;
2548 struct gcpro gcpro1;
2549 #ifdef NO_ARG_ARRAY
2550 Lisp_Object args[3];
2551 args[0] = fn;
2552 args[1] = arg1;
2553 args[2] = arg2;
2554 GCPRO1 (args[0]);
2555 gcpro1.nvars = 3;
2556 RETURN_UNGCPRO (Ffuncall (3, args));
2557 #else /* not NO_ARG_ARRAY */
2558 GCPRO1 (fn);
2559 gcpro1.nvars = 3;
2560 RETURN_UNGCPRO (Ffuncall (3, &fn));
2561 #endif /* not NO_ARG_ARRAY */
2564 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2565 /* ARGSUSED */
2566 Lisp_Object
2567 call3 (fn, arg1, arg2, arg3)
2568 Lisp_Object fn, arg1, arg2, arg3;
2570 struct gcpro gcpro1;
2571 #ifdef NO_ARG_ARRAY
2572 Lisp_Object args[4];
2573 args[0] = fn;
2574 args[1] = arg1;
2575 args[2] = arg2;
2576 args[3] = arg3;
2577 GCPRO1 (args[0]);
2578 gcpro1.nvars = 4;
2579 RETURN_UNGCPRO (Ffuncall (4, args));
2580 #else /* not NO_ARG_ARRAY */
2581 GCPRO1 (fn);
2582 gcpro1.nvars = 4;
2583 RETURN_UNGCPRO (Ffuncall (4, &fn));
2584 #endif /* not NO_ARG_ARRAY */
2587 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2588 /* ARGSUSED */
2589 Lisp_Object
2590 call4 (fn, arg1, arg2, arg3, arg4)
2591 Lisp_Object fn, arg1, arg2, arg3, arg4;
2593 struct gcpro gcpro1;
2594 #ifdef NO_ARG_ARRAY
2595 Lisp_Object args[5];
2596 args[0] = fn;
2597 args[1] = arg1;
2598 args[2] = arg2;
2599 args[3] = arg3;
2600 args[4] = arg4;
2601 GCPRO1 (args[0]);
2602 gcpro1.nvars = 5;
2603 RETURN_UNGCPRO (Ffuncall (5, args));
2604 #else /* not NO_ARG_ARRAY */
2605 GCPRO1 (fn);
2606 gcpro1.nvars = 5;
2607 RETURN_UNGCPRO (Ffuncall (5, &fn));
2608 #endif /* not NO_ARG_ARRAY */
2611 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2612 /* ARGSUSED */
2613 Lisp_Object
2614 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2615 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2617 struct gcpro gcpro1;
2618 #ifdef NO_ARG_ARRAY
2619 Lisp_Object args[6];
2620 args[0] = fn;
2621 args[1] = arg1;
2622 args[2] = arg2;
2623 args[3] = arg3;
2624 args[4] = arg4;
2625 args[5] = arg5;
2626 GCPRO1 (args[0]);
2627 gcpro1.nvars = 6;
2628 RETURN_UNGCPRO (Ffuncall (6, args));
2629 #else /* not NO_ARG_ARRAY */
2630 GCPRO1 (fn);
2631 gcpro1.nvars = 6;
2632 RETURN_UNGCPRO (Ffuncall (6, &fn));
2633 #endif /* not NO_ARG_ARRAY */
2636 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2637 /* ARGSUSED */
2638 Lisp_Object
2639 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2640 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2642 struct gcpro gcpro1;
2643 #ifdef NO_ARG_ARRAY
2644 Lisp_Object args[7];
2645 args[0] = fn;
2646 args[1] = arg1;
2647 args[2] = arg2;
2648 args[3] = arg3;
2649 args[4] = arg4;
2650 args[5] = arg5;
2651 args[6] = arg6;
2652 GCPRO1 (args[0]);
2653 gcpro1.nvars = 7;
2654 RETURN_UNGCPRO (Ffuncall (7, args));
2655 #else /* not NO_ARG_ARRAY */
2656 GCPRO1 (fn);
2657 gcpro1.nvars = 7;
2658 RETURN_UNGCPRO (Ffuncall (7, &fn));
2659 #endif /* not NO_ARG_ARRAY */
2662 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2663 doc: /* Call first argument as a function, passing remaining arguments to it.
2664 Return the value that function returns.
2665 Thus, (funcall 'cons 'x 'y) returns (x . y).
2666 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2667 (nargs, args)
2668 int nargs;
2669 Lisp_Object *args;
2671 Lisp_Object fun;
2672 Lisp_Object funcar;
2673 int numargs = nargs - 1;
2674 Lisp_Object lisp_numargs;
2675 Lisp_Object val;
2676 struct backtrace backtrace;
2677 register Lisp_Object *internal_args;
2678 register int i;
2680 QUIT;
2681 if (consing_since_gc > gc_cons_threshold)
2682 Fgarbage_collect ();
2684 if (++lisp_eval_depth > max_lisp_eval_depth)
2686 if (max_lisp_eval_depth < 100)
2687 max_lisp_eval_depth = 100;
2688 if (lisp_eval_depth > max_lisp_eval_depth)
2689 error ("Lisp nesting exceeds max-lisp-eval-depth");
2692 backtrace.next = backtrace_list;
2693 backtrace_list = &backtrace;
2694 backtrace.function = &args[0];
2695 backtrace.args = &args[1];
2696 backtrace.nargs = nargs - 1;
2697 backtrace.evalargs = 0;
2698 backtrace.debug_on_exit = 0;
2700 if (debug_on_next_call)
2701 do_debug_on_call (Qlambda);
2703 retry:
2705 fun = args[0];
2707 fun = Findirect_function (fun);
2709 if (SUBRP (fun))
2711 if (numargs < XSUBR (fun)->min_args
2712 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2714 XSETFASTINT (lisp_numargs, numargs);
2715 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2718 if (XSUBR (fun)->max_args == UNEVALLED)
2719 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2721 if (XSUBR (fun)->max_args == MANY)
2723 val = (*XSUBR (fun)->function) (numargs, args + 1);
2724 goto done;
2727 if (XSUBR (fun)->max_args > numargs)
2729 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2730 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2731 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2732 internal_args[i] = Qnil;
2734 else
2735 internal_args = args + 1;
2736 switch (XSUBR (fun)->max_args)
2738 case 0:
2739 val = (*XSUBR (fun)->function) ();
2740 goto done;
2741 case 1:
2742 val = (*XSUBR (fun)->function) (internal_args[0]);
2743 goto done;
2744 case 2:
2745 val = (*XSUBR (fun)->function) (internal_args[0],
2746 internal_args[1]);
2747 goto done;
2748 case 3:
2749 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2750 internal_args[2]);
2751 goto done;
2752 case 4:
2753 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2754 internal_args[2],
2755 internal_args[3]);
2756 goto done;
2757 case 5:
2758 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2759 internal_args[2], internal_args[3],
2760 internal_args[4]);
2761 goto done;
2762 case 6:
2763 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2764 internal_args[2], internal_args[3],
2765 internal_args[4], internal_args[5]);
2766 goto done;
2767 case 7:
2768 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2769 internal_args[2], internal_args[3],
2770 internal_args[4], internal_args[5],
2771 internal_args[6]);
2772 goto done;
2774 case 8:
2775 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2776 internal_args[2], internal_args[3],
2777 internal_args[4], internal_args[5],
2778 internal_args[6], internal_args[7]);
2779 goto done;
2781 default:
2783 /* If a subr takes more than 8 arguments without using MANY
2784 or UNEVALLED, we need to extend this function to support it.
2785 Until this is done, there is no way to call the function. */
2786 abort ();
2789 if (COMPILEDP (fun))
2790 val = funcall_lambda (fun, numargs, args + 1);
2791 else
2793 if (!CONSP (fun))
2794 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2795 funcar = Fcar (fun);
2796 if (!SYMBOLP (funcar))
2797 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2798 if (EQ (funcar, Qlambda))
2799 val = funcall_lambda (fun, numargs, args + 1);
2800 else if (EQ (funcar, Qautoload))
2802 do_autoload (fun, args[0]);
2803 goto retry;
2805 else
2806 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2808 done:
2809 lisp_eval_depth--;
2810 if (backtrace.debug_on_exit)
2811 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2812 backtrace_list = backtrace.next;
2813 return val;
2816 Lisp_Object
2817 apply_lambda (fun, args, eval_flag)
2818 Lisp_Object fun, args;
2819 int eval_flag;
2821 Lisp_Object args_left;
2822 Lisp_Object numargs;
2823 register Lisp_Object *arg_vector;
2824 struct gcpro gcpro1, gcpro2, gcpro3;
2825 register int i;
2826 register Lisp_Object tem;
2828 numargs = Flength (args);
2829 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2830 args_left = args;
2832 GCPRO3 (*arg_vector, args_left, fun);
2833 gcpro1.nvars = 0;
2835 for (i = 0; i < XINT (numargs);)
2837 tem = Fcar (args_left), args_left = Fcdr (args_left);
2838 if (eval_flag) tem = Feval (tem);
2839 arg_vector[i++] = tem;
2840 gcpro1.nvars = i;
2843 UNGCPRO;
2845 if (eval_flag)
2847 backtrace_list->args = arg_vector;
2848 backtrace_list->nargs = i;
2850 backtrace_list->evalargs = 0;
2851 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2853 /* Do the debug-on-exit now, while arg_vector still exists. */
2854 if (backtrace_list->debug_on_exit)
2855 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2856 /* Don't do it again when we return to eval. */
2857 backtrace_list->debug_on_exit = 0;
2858 return tem;
2861 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2862 and return the result of evaluation.
2863 FUN must be either a lambda-expression or a compiled-code object. */
2865 static Lisp_Object
2866 funcall_lambda (fun, nargs, arg_vector)
2867 Lisp_Object fun;
2868 int nargs;
2869 register Lisp_Object *arg_vector;
2871 Lisp_Object val, syms_left, next;
2872 int count = SPECPDL_INDEX ();
2873 int i, optional, rest;
2875 if (CONSP (fun))
2877 syms_left = XCDR (fun);
2878 if (CONSP (syms_left))
2879 syms_left = XCAR (syms_left);
2880 else
2881 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2883 else if (COMPILEDP (fun))
2884 syms_left = AREF (fun, COMPILED_ARGLIST);
2885 else
2886 abort ();
2888 i = optional = rest = 0;
2889 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2891 QUIT;
2893 next = XCAR (syms_left);
2894 while (!SYMBOLP (next))
2895 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2897 if (EQ (next, Qand_rest))
2898 rest = 1;
2899 else if (EQ (next, Qand_optional))
2900 optional = 1;
2901 else if (rest)
2903 specbind (next, Flist (nargs - i, &arg_vector[i]));
2904 i = nargs;
2906 else if (i < nargs)
2907 specbind (next, arg_vector[i++]);
2908 else if (!optional)
2909 return Fsignal (Qwrong_number_of_arguments,
2910 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2911 else
2912 specbind (next, Qnil);
2915 if (!NILP (syms_left))
2916 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2917 else if (i < nargs)
2918 return Fsignal (Qwrong_number_of_arguments,
2919 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2921 if (CONSP (fun))
2922 val = Fprogn (XCDR (XCDR (fun)));
2923 else
2925 /* If we have not actually read the bytecode string
2926 and constants vector yet, fetch them from the file. */
2927 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2928 Ffetch_bytecode (fun);
2929 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2930 AREF (fun, COMPILED_CONSTANTS),
2931 AREF (fun, COMPILED_STACK_DEPTH));
2934 return unbind_to (count, val);
2937 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2938 1, 1, 0,
2939 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2940 (object)
2941 Lisp_Object object;
2943 Lisp_Object tem;
2945 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2947 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2948 if (!CONSP (tem))
2950 tem = AREF (object, COMPILED_BYTECODE);
2951 if (CONSP (tem) && STRINGP (XCAR (tem)))
2952 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2953 else
2954 error ("Invalid byte code");
2956 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
2957 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
2959 return object;
2962 void
2963 grow_specpdl ()
2965 register int count = SPECPDL_INDEX ();
2966 if (specpdl_size >= max_specpdl_size)
2968 if (max_specpdl_size < 400)
2969 max_specpdl_size = 400;
2970 if (specpdl_size >= max_specpdl_size)
2972 if (!NILP (Vdebug_on_error))
2973 /* Leave room for some specpdl in the debugger. */
2974 max_specpdl_size = specpdl_size + 100;
2975 Fsignal (Qerror,
2976 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2979 specpdl_size *= 2;
2980 if (specpdl_size > max_specpdl_size)
2981 specpdl_size = max_specpdl_size;
2982 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2983 specpdl_ptr = specpdl + count;
2986 void
2987 specbind (symbol, value)
2988 Lisp_Object symbol, value;
2990 Lisp_Object ovalue;
2991 Lisp_Object valcontents;
2993 CHECK_SYMBOL (symbol);
2994 if (specpdl_ptr == specpdl + specpdl_size)
2995 grow_specpdl ();
2997 /* The most common case is that of a non-constant symbol with a
2998 trivial value. Make that as fast as we can. */
2999 valcontents = SYMBOL_VALUE (symbol);
3000 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
3002 specpdl_ptr->symbol = symbol;
3003 specpdl_ptr->old_value = valcontents;
3004 specpdl_ptr->func = NULL;
3005 ++specpdl_ptr;
3006 SET_SYMBOL_VALUE (symbol, value);
3008 else
3010 Lisp_Object valcontents;
3012 ovalue = find_symbol_value (symbol);
3013 specpdl_ptr->func = 0;
3014 specpdl_ptr->old_value = ovalue;
3016 valcontents = XSYMBOL (symbol)->value;
3018 if (BUFFER_LOCAL_VALUEP (valcontents)
3019 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
3020 || BUFFER_OBJFWDP (valcontents))
3022 Lisp_Object where, current_buffer;
3024 current_buffer = Fcurrent_buffer ();
3026 /* For a local variable, record both the symbol and which
3027 buffer's or frame's value we are saving. */
3028 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3029 where = current_buffer;
3030 else if (!BUFFER_OBJFWDP (valcontents)
3031 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3032 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3033 else
3034 where = Qnil;
3036 /* We're not using the `unused' slot in the specbinding
3037 structure because this would mean we have to do more
3038 work for simple variables. */
3039 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3041 /* If SYMBOL is a per-buffer variable which doesn't have a
3042 buffer-local value here, make the `let' change the global
3043 value by changing the value of SYMBOL in all buffers not
3044 having their own value. This is consistent with what
3045 happens with other buffer-local variables. */
3046 if (NILP (where)
3047 && BUFFER_OBJFWDP (valcontents))
3049 ++specpdl_ptr;
3050 Fset_default (symbol, value);
3051 return;
3054 else
3055 specpdl_ptr->symbol = symbol;
3057 specpdl_ptr++;
3058 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3059 store_symval_forwarding (symbol, ovalue, value, NULL);
3060 else
3061 set_internal (symbol, value, 0, 1);
3065 void
3066 record_unwind_protect (function, arg)
3067 Lisp_Object (*function) P_ ((Lisp_Object));
3068 Lisp_Object arg;
3070 if (specpdl_ptr == specpdl + specpdl_size)
3071 grow_specpdl ();
3072 specpdl_ptr->func = function;
3073 specpdl_ptr->symbol = Qnil;
3074 specpdl_ptr->old_value = arg;
3075 specpdl_ptr++;
3078 Lisp_Object
3079 unbind_to (count, value)
3080 int count;
3081 Lisp_Object value;
3083 int quitf = !NILP (Vquit_flag);
3084 struct gcpro gcpro1;
3086 GCPRO1 (value);
3087 Vquit_flag = Qnil;
3089 while (specpdl_ptr != specpdl + count)
3091 --specpdl_ptr;
3093 if (specpdl_ptr->func != 0)
3094 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3095 /* Note that a "binding" of nil is really an unwind protect,
3096 so in that case the "old value" is a list of forms to evaluate. */
3097 else if (NILP (specpdl_ptr->symbol))
3098 Fprogn (specpdl_ptr->old_value);
3099 /* If the symbol is a list, it is really (SYMBOL WHERE
3100 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3101 frame. If WHERE is a buffer or frame, this indicates we
3102 bound a variable that had a buffer-local or frame-local
3103 binding. WHERE nil means that the variable had the default
3104 value when it was bound. CURRENT-BUFFER is the buffer that
3105 was current when the variable was bound. */
3106 else if (CONSP (specpdl_ptr->symbol))
3108 Lisp_Object symbol, where;
3110 symbol = XCAR (specpdl_ptr->symbol);
3111 where = XCAR (XCDR (specpdl_ptr->symbol));
3113 if (NILP (where))
3114 Fset_default (symbol, specpdl_ptr->old_value);
3115 else if (BUFFERP (where))
3116 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (where), 1);
3117 else
3118 set_internal (symbol, specpdl_ptr->old_value, NULL, 1);
3120 else
3122 /* If variable has a trivial value (no forwarding), we can
3123 just set it. No need to check for constant symbols here,
3124 since that was already done by specbind. */
3125 if (!MISCP (SYMBOL_VALUE (specpdl_ptr->symbol)))
3126 SET_SYMBOL_VALUE (specpdl_ptr->symbol, specpdl_ptr->old_value);
3127 else
3128 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3132 if (NILP (Vquit_flag) && quitf)
3133 Vquit_flag = Qt;
3135 UNGCPRO;
3136 return value;
3139 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3140 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3141 The debugger is entered when that frame exits, if the flag is non-nil. */)
3142 (level, flag)
3143 Lisp_Object level, flag;
3145 register struct backtrace *backlist = backtrace_list;
3146 register int i;
3148 CHECK_NUMBER (level);
3150 for (i = 0; backlist && i < XINT (level); i++)
3152 backlist = backlist->next;
3155 if (backlist)
3156 backlist->debug_on_exit = !NILP (flag);
3158 return flag;
3161 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3162 doc: /* Print a trace of Lisp function calls currently active.
3163 Output stream used is value of `standard-output'. */)
3166 register struct backtrace *backlist = backtrace_list;
3167 register int i;
3168 Lisp_Object tail;
3169 Lisp_Object tem;
3170 extern Lisp_Object Vprint_level;
3171 struct gcpro gcpro1;
3173 XSETFASTINT (Vprint_level, 3);
3175 tail = Qnil;
3176 GCPRO1 (tail);
3178 while (backlist)
3180 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3181 if (backlist->nargs == UNEVALLED)
3183 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3184 write_string ("\n", -1);
3186 else
3188 tem = *backlist->function;
3189 Fprin1 (tem, Qnil); /* This can QUIT */
3190 write_string ("(", -1);
3191 if (backlist->nargs == MANY)
3193 for (tail = *backlist->args, i = 0;
3194 !NILP (tail);
3195 tail = Fcdr (tail), i++)
3197 if (i) write_string (" ", -1);
3198 Fprin1 (Fcar (tail), Qnil);
3201 else
3203 for (i = 0; i < backlist->nargs; i++)
3205 if (i) write_string (" ", -1);
3206 Fprin1 (backlist->args[i], Qnil);
3209 write_string (")\n", -1);
3211 backlist = backlist->next;
3214 Vprint_level = Qnil;
3215 UNGCPRO;
3216 return Qnil;
3219 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3220 doc: /* Return the function and arguments NFRAMES up from current execution point.
3221 If that frame has not evaluated the arguments yet (or is a special form),
3222 the value is (nil FUNCTION ARG-FORMS...).
3223 If that frame has evaluated its arguments and called its function already,
3224 the value is (t FUNCTION ARG-VALUES...).
3225 A &rest arg is represented as the tail of the list ARG-VALUES.
3226 FUNCTION is whatever was supplied as car of evaluated list,
3227 or a lambda expression for macro calls.
3228 If NFRAMES is more than the number of frames, the value is nil. */)
3229 (nframes)
3230 Lisp_Object nframes;
3232 register struct backtrace *backlist = backtrace_list;
3233 register int i;
3234 Lisp_Object tem;
3236 CHECK_NATNUM (nframes);
3238 /* Find the frame requested. */
3239 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3240 backlist = backlist->next;
3242 if (!backlist)
3243 return Qnil;
3244 if (backlist->nargs == UNEVALLED)
3245 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3246 else
3248 if (backlist->nargs == MANY)
3249 tem = *backlist->args;
3250 else
3251 tem = Flist (backlist->nargs, backlist->args);
3253 return Fcons (Qt, Fcons (*backlist->function, tem));
3258 void
3259 syms_of_eval ()
3261 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3262 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3263 If Lisp code tries to make more than this many at once,
3264 an error is signaled.
3265 You can safely use a value considerably larger than the default value,
3266 if that proves inconveniently small. However, if you increase it too far,
3267 Emacs could run out of memory trying to make the stack bigger. */);
3269 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3270 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3272 This limit serves to catch infinite recursions for you before they cause
3273 actual stack overflow in C, which would be fatal for Emacs.
3274 You can safely make it considerably larger than its default value,
3275 if that proves inconveniently small. However, if you increase it too far,
3276 Emacs could overflow the real C stack, and crash. */);
3278 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3279 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3280 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'. */);
3281 Vquit_flag = Qnil;
3283 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3284 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3285 Note that `quit-flag' will still be set by typing C-g,
3286 so a quit will be signaled as soon as `inhibit-quit' is nil.
3287 To prevent this happening, set `quit-flag' to nil
3288 before making `inhibit-quit' nil. */);
3289 Vinhibit_quit = Qnil;
3291 Qinhibit_quit = intern ("inhibit-quit");
3292 staticpro (&Qinhibit_quit);
3294 Qautoload = intern ("autoload");
3295 staticpro (&Qautoload);
3297 Qdebug_on_error = intern ("debug-on-error");
3298 staticpro (&Qdebug_on_error);
3300 Qmacro = intern ("macro");
3301 staticpro (&Qmacro);
3303 Qdeclare = intern ("declare");
3304 staticpro (&Qdeclare);
3306 /* Note that the process handling also uses Qexit, but we don't want
3307 to staticpro it twice, so we just do it here. */
3308 Qexit = intern ("exit");
3309 staticpro (&Qexit);
3311 Qinteractive = intern ("interactive");
3312 staticpro (&Qinteractive);
3314 Qcommandp = intern ("commandp");
3315 staticpro (&Qcommandp);
3317 Qdefun = intern ("defun");
3318 staticpro (&Qdefun);
3320 Qdefvar = intern ("defvar");
3321 staticpro (&Qdefvar);
3323 Qand_rest = intern ("&rest");
3324 staticpro (&Qand_rest);
3326 Qand_optional = intern ("&optional");
3327 staticpro (&Qand_optional);
3329 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3330 doc: /* *Non-nil means errors display a backtrace buffer.
3331 More precisely, this happens for any error that is handled
3332 by the editor command loop.
3333 If the value is a list, an error only means to display a backtrace
3334 if one of its condition symbols appears in the list. */);
3335 Vstack_trace_on_error = Qnil;
3337 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3338 doc: /* *Non-nil means enter debugger if an error is signaled.
3339 Does not apply to errors handled by `condition-case' or those
3340 matched by `debug-ignored-errors'.
3341 If the value is a list, an error only means to enter the debugger
3342 if one of its condition symbols appears in the list.
3343 When you evaluate an expression interactively, this variable
3344 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3345 See also variable `debug-on-quit'. */);
3346 Vdebug_on_error = Qnil;
3348 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3349 doc: /* *List of errors for which the debugger should not be called.
3350 Each element may be a condition-name or a regexp that matches error messages.
3351 If any element applies to a given error, that error skips the debugger
3352 and just returns to top level.
3353 This overrides the variable `debug-on-error'.
3354 It does not apply to errors handled by `condition-case'. */);
3355 Vdebug_ignored_errors = Qnil;
3357 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3358 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3359 Does not apply if quit is handled by a `condition-case'.
3360 When you evaluate an expression interactively, this variable
3361 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3362 debug_on_quit = 0;
3364 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3365 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3367 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3368 doc: /* Non-nil means debugger may continue execution.
3369 This is nil when the debugger is called under circumstances where it
3370 might not be safe to continue. */);
3371 debugger_may_continue = 1;
3373 DEFVAR_LISP ("debugger", &Vdebugger,
3374 doc: /* Function to call to invoke debugger.
3375 If due to frame exit, args are `exit' and the value being returned;
3376 this function's value will be returned instead of that.
3377 If due to error, args are `error' and a list of the args to `signal'.
3378 If due to `apply' or `funcall' entry, one arg, `lambda'.
3379 If due to `eval' entry, one arg, t. */);
3380 Vdebugger = Qnil;
3382 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3383 doc: /* If non-nil, this is a function for `signal' to call.
3384 It receives the same arguments that `signal' was given.
3385 The Edebug package uses this to regain control. */);
3386 Vsignal_hook_function = Qnil;
3388 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3389 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3390 Note that `debug-on-error', `debug-on-quit' and friends
3391 still determine whether to handle the particular condition. */);
3392 Vdebug_on_signal = Qnil;
3394 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3395 doc: /* Function to process declarations in a macro definition.
3396 The function will be called with two args MACRO and DECL.
3397 MACRO is the name of the macro being defined.
3398 DECL is a list `(declare ...)' containing the declarations.
3399 The value the function returns is not used. */);
3400 Vmacro_declaration_function = Qnil;
3402 Vrun_hooks = intern ("run-hooks");
3403 staticpro (&Vrun_hooks);
3405 staticpro (&Vautoload_queue);
3406 Vautoload_queue = Qnil;
3407 staticpro (&Vsignaling_function);
3408 Vsignaling_function = Qnil;
3410 defsubr (&Sor);
3411 defsubr (&Sand);
3412 defsubr (&Sif);
3413 defsubr (&Scond);
3414 defsubr (&Sprogn);
3415 defsubr (&Sprog1);
3416 defsubr (&Sprog2);
3417 defsubr (&Ssetq);
3418 defsubr (&Squote);
3419 defsubr (&Sfunction);
3420 defsubr (&Sdefun);
3421 defsubr (&Sdefmacro);
3422 defsubr (&Sdefvar);
3423 defsubr (&Sdefvaralias);
3424 defsubr (&Sdefconst);
3425 defsubr (&Suser_variable_p);
3426 defsubr (&Slet);
3427 defsubr (&SletX);
3428 defsubr (&Swhile);
3429 defsubr (&Smacroexpand);
3430 defsubr (&Scatch);
3431 defsubr (&Sthrow);
3432 defsubr (&Sunwind_protect);
3433 defsubr (&Scondition_case);
3434 defsubr (&Ssignal);
3435 defsubr (&Sinteractive_p);
3436 defsubr (&Scommandp);
3437 defsubr (&Sautoload);
3438 defsubr (&Seval);
3439 defsubr (&Sapply);
3440 defsubr (&Sfuncall);
3441 defsubr (&Srun_hooks);
3442 defsubr (&Srun_hook_with_args);
3443 defsubr (&Srun_hook_with_args_until_success);
3444 defsubr (&Srun_hook_with_args_until_failure);
3445 defsubr (&Sfetch_bytecode);
3446 defsubr (&Sbacktrace_debug);
3447 defsubr (&Sbacktrace);
3448 defsubr (&Sbacktrace_frame);