(byte-compile-warning-types): Add interactive-only.
[emacs.git] / src / eval.c
blob6d37d43c79c1ec2a0dec0df79ff0d8904b713b70
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000, 2001, 02, 2004
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 volatile 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 = Qnil;
298 struct gcpro gcpro1;
300 GCPRO1 (args);
302 while (CONSP (args))
304 val = Feval (XCAR (args));
305 if (!NILP (val))
306 break;
307 args = XCDR (args);
310 UNGCPRO;
311 return val;
314 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
315 doc: /* Eval args until one of them yields nil, then return nil.
316 The remaining args are not evalled at all.
317 If no arg yields nil, return the last arg's value.
318 usage: (and CONDITIONS ...) */)
319 (args)
320 Lisp_Object args;
322 register Lisp_Object val = Qt;
323 struct gcpro gcpro1;
325 GCPRO1 (args);
327 while (CONSP (args))
329 val = Feval (XCAR (args));
330 if (NILP (val))
331 break;
332 args = XCDR (args);
335 UNGCPRO;
336 return val;
339 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
340 doc: /* If COND yields non-nil, do THEN, else do ELSE...
341 Returns the value of THEN or the value of the last of the ELSE's.
342 THEN must be one expression, but ELSE... can be zero or more expressions.
343 If COND yields nil, and there are no ELSE's, the value is nil.
344 usage: (if COND THEN ELSE...) */)
345 (args)
346 Lisp_Object args;
348 register Lisp_Object cond;
349 struct gcpro gcpro1;
351 GCPRO1 (args);
352 cond = Feval (Fcar (args));
353 UNGCPRO;
355 if (!NILP (cond))
356 return Feval (Fcar (Fcdr (args)));
357 return Fprogn (Fcdr (Fcdr (args)));
360 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
361 doc: /* Try each clause until one succeeds.
362 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
363 and, if the value is non-nil, this clause succeeds:
364 then the expressions in BODY are evaluated and the last one's
365 value is the value of the cond-form.
366 If no clause succeeds, cond returns nil.
367 If a clause has one element, as in (CONDITION),
368 CONDITION's value if non-nil is returned from the cond-form.
369 usage: (cond CLAUSES...) */)
370 (args)
371 Lisp_Object args;
373 register Lisp_Object clause, val;
374 struct gcpro gcpro1;
376 val = Qnil;
377 GCPRO1 (args);
378 while (!NILP (args))
380 clause = Fcar (args);
381 val = Feval (Fcar (clause));
382 if (!NILP (val))
384 if (!EQ (XCDR (clause), Qnil))
385 val = Fprogn (XCDR (clause));
386 break;
388 args = XCDR (args);
390 UNGCPRO;
392 return val;
395 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
396 doc: /* Eval BODY forms sequentially and return value of last one.
397 usage: (progn BODY ...) */)
398 (args)
399 Lisp_Object args;
401 register Lisp_Object val = Qnil;
402 struct gcpro gcpro1;
404 GCPRO1 (args);
406 while (CONSP (args))
408 val = Feval (XCAR (args));
409 args = XCDR (args);
412 UNGCPRO;
413 return val;
416 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
417 doc: /* Eval FIRST and BODY sequentially; value from FIRST.
418 The value of FIRST is saved during the evaluation of the remaining args,
419 whose values are discarded.
420 usage: (prog1 FIRST BODY...) */)
421 (args)
422 Lisp_Object args;
424 Lisp_Object val;
425 register Lisp_Object args_left;
426 struct gcpro gcpro1, gcpro2;
427 register int argnum = 0;
429 if (NILP(args))
430 return Qnil;
432 args_left = args;
433 val = Qnil;
434 GCPRO2 (args, val);
438 if (!(argnum++))
439 val = Feval (Fcar (args_left));
440 else
441 Feval (Fcar (args_left));
442 args_left = Fcdr (args_left);
444 while (!NILP(args_left));
446 UNGCPRO;
447 return val;
450 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
451 doc: /* Eval X, Y and BODY sequentially; value from Y.
452 The value of Y is saved during the evaluation of the remaining args,
453 whose values are discarded.
454 usage: (prog2 X Y BODY...) */)
455 (args)
456 Lisp_Object args;
458 Lisp_Object val;
459 register Lisp_Object args_left;
460 struct gcpro gcpro1, gcpro2;
461 register int argnum = -1;
463 val = Qnil;
465 if (NILP (args))
466 return Qnil;
468 args_left = args;
469 val = Qnil;
470 GCPRO2 (args, val);
474 if (!(argnum++))
475 val = Feval (Fcar (args_left));
476 else
477 Feval (Fcar (args_left));
478 args_left = Fcdr (args_left);
480 while (!NILP (args_left));
482 UNGCPRO;
483 return val;
486 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
487 doc: /* Set each SYM to the value of its VAL.
488 The symbols SYM are variables; they are literal (not evaluated).
489 The values VAL are expressions; they are evaluated.
490 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
491 The second VAL is not computed until after the first SYM is set, and so on;
492 each VAL can use the new value of variables set earlier in the `setq'.
493 The return value of the `setq' form is the value of the last VAL.
494 usage: (setq SYM VAL SYM VAL ...) */)
495 (args)
496 Lisp_Object args;
498 register Lisp_Object args_left;
499 register Lisp_Object val, sym;
500 struct gcpro gcpro1;
502 if (NILP(args))
503 return Qnil;
505 args_left = args;
506 GCPRO1 (args);
510 val = Feval (Fcar (Fcdr (args_left)));
511 sym = Fcar (args_left);
512 Fset (sym, val);
513 args_left = Fcdr (Fcdr (args_left));
515 while (!NILP(args_left));
517 UNGCPRO;
518 return val;
521 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
522 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
523 usage: (quote ARG) */)
524 (args)
525 Lisp_Object args;
527 return Fcar (args);
530 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
531 doc: /* Like `quote', but preferred for objects which are functions.
532 In byte compilation, `function' causes its argument to be compiled.
533 `quote' cannot do that.
534 usage: (function ARG) */)
535 (args)
536 Lisp_Object args;
538 return Fcar (args);
542 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
543 doc: /* Return t if the function was run directly by user input.
544 This means that the function was called with call-interactively (which
545 includes being called as the binding of a key)
546 and input is currently coming from the keyboard (not in keyboard macro),
547 and Emacs is not running in batch mode (`noninteractive' is nil).
549 The only known proper use of `interactive-p' is in deciding whether to
550 display a helpful message, or how to display it. If you're thinking
551 of using it for any other purpose, it is quite likely that you're
552 making a mistake. Think: what do you want to do when the command is
553 called from a keyboard macro?
555 If you want to test whether your function was called with
556 `call-interactively', the way to do that is by adding an extra
557 optional argument, and making the `interactive' spec specify non-nil
558 unconditionally for that argument. (`p' is a good way to do this.) */)
561 return (INTERACTIVE && interactive_p (1)) ? Qt : Qnil;
565 DEFUN ("called-interactively-p", Fcall_interactive_p, Scall_interactive_p, 0, 0, 0,
566 doc: /* Return t if the function using this was called with call-interactively.
567 This is used for implementing advice and other function-modifying
568 features of Emacs.
570 The cleanest way to test whether your function was called with
571 `call-interactively', the way to do that is by adding an extra
572 optional argument, and making the `interactive' spec specify non-nil
573 unconditionally for that argument. (`p' is a good way to do this.) */)
576 return (INTERACTIVE && interactive_p (1)) ? Qt : Qnil;
580 /* Return 1 if function in which this appears was called using
581 call-interactively.
583 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
584 called is a built-in. */
587 interactive_p (exclude_subrs_p)
588 int exclude_subrs_p;
590 struct backtrace *btp;
591 Lisp_Object fun;
593 btp = backtrace_list;
595 /* If this isn't a byte-compiled function, there may be a frame at
596 the top for Finteractive_p. If so, skip it. */
597 fun = Findirect_function (*btp->function);
598 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
599 btp = btp->next;
601 /* If we're running an Emacs 18-style byte-compiled function, there
602 may be a frame for Fbytecode at the top level. In any version of
603 Emacs there can be Fbytecode frames for subexpressions evaluated
604 inside catch and condition-case. Skip past them.
606 If this isn't a byte-compiled function, then we may now be
607 looking at several frames for special forms. Skip past them. */
608 while (btp
609 && (EQ (*btp->function, Qbytecode)
610 || btp->nargs == UNEVALLED))
611 btp = btp->next;
613 /* btp now points at the frame of the innermost function that isn't
614 a special form, ignoring frames for Finteractive_p and/or
615 Fbytecode at the top. If this frame is for a built-in function
616 (such as load or eval-region) return nil. */
617 fun = Findirect_function (*btp->function);
618 if (exclude_subrs_p && SUBRP (fun))
619 return 0;
621 /* btp points to the frame of a Lisp function that called interactive-p.
622 Return t if that function was called interactively. */
623 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
624 return 1;
625 return 0;
629 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
630 doc: /* Define NAME as a function.
631 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
632 See also the function `interactive'.
633 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
634 (args)
635 Lisp_Object args;
637 register Lisp_Object fn_name;
638 register Lisp_Object defn;
640 fn_name = Fcar (args);
641 CHECK_SYMBOL (fn_name);
642 defn = Fcons (Qlambda, Fcdr (args));
643 if (!NILP (Vpurify_flag))
644 defn = Fpurecopy (defn);
645 if (CONSP (XSYMBOL (fn_name)->function)
646 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
647 LOADHIST_ATTACH (Fcons (Qt, fn_name));
648 Ffset (fn_name, defn);
649 LOADHIST_ATTACH (fn_name);
650 return fn_name;
653 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
654 doc: /* Define NAME as a macro.
655 The actual definition looks like
656 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
657 When the macro is called, as in (NAME ARGS...),
658 the function (lambda ARGLIST BODY...) is applied to
659 the list ARGS... as it appears in the expression,
660 and the result should be a form to be evaluated instead of the original.
662 DECL is a declaration, optional, which can specify how to indent
663 calls to this macro and how Edebug should handle it. It looks like this:
664 (declare SPECS...)
665 The elements can look like this:
666 (indent INDENT)
667 Set NAME's `lisp-indent-function' property to INDENT.
669 (debug DEBUG)
670 Set NAME's `edebug-form-spec' property to DEBUG. (This is
671 equivalent to writing a `def-edebug-spec' for the macro.)
672 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
673 (args)
674 Lisp_Object args;
676 register Lisp_Object fn_name;
677 register Lisp_Object defn;
678 Lisp_Object lambda_list, doc, tail;
680 fn_name = Fcar (args);
681 CHECK_SYMBOL (fn_name);
682 lambda_list = Fcar (Fcdr (args));
683 tail = Fcdr (Fcdr (args));
685 doc = Qnil;
686 if (STRINGP (Fcar (tail)))
688 doc = XCAR (tail);
689 tail = XCDR (tail);
692 while (CONSP (Fcar (tail))
693 && EQ (Fcar (Fcar (tail)), Qdeclare))
695 if (!NILP (Vmacro_declaration_function))
697 struct gcpro gcpro1;
698 GCPRO1 (args);
699 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
700 UNGCPRO;
703 tail = Fcdr (tail);
706 if (NILP (doc))
707 tail = Fcons (lambda_list, tail);
708 else
709 tail = Fcons (lambda_list, Fcons (doc, tail));
710 defn = Fcons (Qmacro, Fcons (Qlambda, tail));
712 if (!NILP (Vpurify_flag))
713 defn = Fpurecopy (defn);
714 if (CONSP (XSYMBOL (fn_name)->function)
715 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
716 LOADHIST_ATTACH (Fcons (Qt, fn_name));
717 Ffset (fn_name, defn);
718 LOADHIST_ATTACH (fn_name);
719 return fn_name;
723 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
724 doc: /* Make SYMBOL a variable alias for symbol ALIASED.
725 Setting the value of SYMBOL will subsequently set the value of ALIASED,
726 and getting the value of SYMBOL will return the value ALIASED has.
727 Third arg DOCSTRING, if non-nil, is documentation for SYMBOL.
728 The return value is ALIASED. */)
729 (symbol, aliased, docstring)
730 Lisp_Object symbol, aliased, docstring;
732 struct Lisp_Symbol *sym;
734 CHECK_SYMBOL (symbol);
735 CHECK_SYMBOL (aliased);
737 if (SYMBOL_CONSTANT_P (symbol))
738 error ("Cannot make a constant an alias");
740 sym = XSYMBOL (symbol);
741 sym->indirect_variable = 1;
742 sym->value = aliased;
743 sym->constant = SYMBOL_CONSTANT_P (aliased);
744 LOADHIST_ATTACH (Fcons (Qdefvar, symbol));
745 if (!NILP (docstring))
746 Fput (symbol, Qvariable_documentation, docstring);
748 return aliased;
752 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
753 doc: /* Define SYMBOL as a variable.
754 You are not required to define a variable in order to use it,
755 but the definition can supply documentation and an initial value
756 in a way that tags can recognize.
758 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
759 If SYMBOL is buffer-local, its default value is what is set;
760 buffer-local values are not affected.
761 INITVALUE and DOCSTRING are optional.
762 If DOCSTRING starts with *, this variable is identified as a user option.
763 This means that M-x set-variable recognizes it.
764 See also `user-variable-p'.
765 If INITVALUE is missing, SYMBOL's value is not set.
767 If SYMBOL has a local binding, then this form affects the local
768 binding. This is usually not what you want. Thus, if you need to
769 load a file defining variables, with this form or with `defconst' or
770 `defcustom', you should always load that file _outside_ any bindings
771 for these variables. \(`defconst' and `defcustom' behave similarly in
772 this respect.)
773 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
774 (args)
775 Lisp_Object args;
777 register Lisp_Object sym, tem, tail;
779 sym = Fcar (args);
780 tail = Fcdr (args);
781 if (!NILP (Fcdr (Fcdr (tail))))
782 error ("too many arguments");
784 tem = Fdefault_boundp (sym);
785 if (!NILP (tail))
787 if (NILP (tem))
788 Fset_default (sym, Feval (Fcar (tail)));
789 tail = Fcdr (tail);
790 tem = Fcar (tail);
791 if (!NILP (tem))
793 if (!NILP (Vpurify_flag))
794 tem = Fpurecopy (tem);
795 Fput (sym, Qvariable_documentation, tem);
797 LOADHIST_ATTACH (Fcons (Qdefvar, sym));
799 else
800 /* Simple (defvar <var>) should not count as a definition at all.
801 It could get in the way of other definitions, and unloading this
802 package could try to make the variable unbound. */
805 return sym;
808 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
809 doc: /* Define SYMBOL as a constant variable.
810 The intent is that neither programs nor users should ever change this value.
811 Always sets the value of SYMBOL to the result of evalling INITVALUE.
812 If SYMBOL is buffer-local, its default value is what is set;
813 buffer-local values are not affected.
814 DOCSTRING is optional.
816 If SYMBOL has a local binding, then this form sets the local binding's
817 value. However, you should normally not make local bindings for
818 variables defined with this form.
819 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
820 (args)
821 Lisp_Object args;
823 register Lisp_Object sym, tem;
825 sym = Fcar (args);
826 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
827 error ("too many arguments");
829 tem = Feval (Fcar (Fcdr (args)));
830 if (!NILP (Vpurify_flag))
831 tem = Fpurecopy (tem);
832 Fset_default (sym, tem);
833 tem = Fcar (Fcdr (Fcdr (args)));
834 if (!NILP (tem))
836 if (!NILP (Vpurify_flag))
837 tem = Fpurecopy (tem);
838 Fput (sym, Qvariable_documentation, tem);
840 LOADHIST_ATTACH (Fcons (Qdefvar, sym));
841 return sym;
844 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
845 doc: /* Returns t if VARIABLE is intended to be set and modified by users.
846 \(The alternative is a variable used internally in a Lisp program.)
847 Determined by whether the first character of the documentation
848 for the variable is `*' or if the variable is customizable (has a non-nil
849 value of `standard-value' or of `custom-autoload' on its property list). */)
850 (variable)
851 Lisp_Object variable;
853 Lisp_Object documentation;
855 if (!SYMBOLP (variable))
856 return Qnil;
858 documentation = Fget (variable, Qvariable_documentation);
859 if (INTEGERP (documentation) && XINT (documentation) < 0)
860 return Qt;
861 if (STRINGP (documentation)
862 && ((unsigned char) SREF (documentation, 0) == '*'))
863 return Qt;
864 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
865 if (CONSP (documentation)
866 && STRINGP (XCAR (documentation))
867 && INTEGERP (XCDR (documentation))
868 && XINT (XCDR (documentation)) < 0)
869 return Qt;
870 /* Customizable? See `custom-variable-p'. */
871 if ((!NILP (Fget (variable, intern ("standard-value"))))
872 || (!NILP (Fget (variable, intern ("custom-autoload")))))
873 return Qt;
874 return Qnil;
877 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
878 doc: /* Bind variables according to VARLIST then eval BODY.
879 The value of the last form in BODY is returned.
880 Each element of VARLIST is a symbol (which is bound to nil)
881 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
882 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
883 usage: (let* VARLIST BODY...) */)
884 (args)
885 Lisp_Object args;
887 Lisp_Object varlist, val, elt;
888 int count = SPECPDL_INDEX ();
889 struct gcpro gcpro1, gcpro2, gcpro3;
891 GCPRO3 (args, elt, varlist);
893 varlist = Fcar (args);
894 while (!NILP (varlist))
896 QUIT;
897 elt = Fcar (varlist);
898 if (SYMBOLP (elt))
899 specbind (elt, Qnil);
900 else if (! NILP (Fcdr (Fcdr (elt))))
901 Fsignal (Qerror,
902 Fcons (build_string ("`let' bindings can have only one value-form"),
903 elt));
904 else
906 val = Feval (Fcar (Fcdr (elt)));
907 specbind (Fcar (elt), val);
909 varlist = Fcdr (varlist);
911 UNGCPRO;
912 val = Fprogn (Fcdr (args));
913 return unbind_to (count, val);
916 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
917 doc: /* Bind variables according to VARLIST then eval BODY.
918 The value of the last form in BODY is returned.
919 Each element of VARLIST is a symbol (which is bound to nil)
920 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
921 All the VALUEFORMs are evalled before any symbols are bound.
922 usage: (let VARLIST BODY...) */)
923 (args)
924 Lisp_Object args;
926 Lisp_Object *temps, tem;
927 register Lisp_Object elt, varlist;
928 int count = SPECPDL_INDEX ();
929 register int argnum;
930 struct gcpro gcpro1, gcpro2;
932 varlist = Fcar (args);
934 /* Make space to hold the values to give the bound variables */
935 elt = Flength (varlist);
936 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
938 /* Compute the values and store them in `temps' */
940 GCPRO2 (args, *temps);
941 gcpro2.nvars = 0;
943 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
945 QUIT;
946 elt = Fcar (varlist);
947 if (SYMBOLP (elt))
948 temps [argnum++] = Qnil;
949 else if (! NILP (Fcdr (Fcdr (elt))))
950 Fsignal (Qerror,
951 Fcons (build_string ("`let' bindings can have only one value-form"),
952 elt));
953 else
954 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
955 gcpro2.nvars = argnum;
957 UNGCPRO;
959 varlist = Fcar (args);
960 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
962 elt = Fcar (varlist);
963 tem = temps[argnum++];
964 if (SYMBOLP (elt))
965 specbind (elt, tem);
966 else
967 specbind (Fcar (elt), tem);
970 elt = Fprogn (Fcdr (args));
971 return unbind_to (count, elt);
974 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
975 doc: /* If TEST yields non-nil, eval BODY... and repeat.
976 The order of execution is thus TEST, BODY, TEST, BODY and so on
977 until TEST returns nil.
978 usage: (while TEST BODY...) */)
979 (args)
980 Lisp_Object args;
982 Lisp_Object test, body;
983 struct gcpro gcpro1, gcpro2;
985 GCPRO2 (test, body);
987 test = Fcar (args);
988 body = Fcdr (args);
989 while (!NILP (Feval (test)))
991 QUIT;
992 Fprogn (body);
995 UNGCPRO;
996 return Qnil;
999 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1000 doc: /* Return result of expanding macros at top level of FORM.
1001 If FORM is not a macro call, it is returned unchanged.
1002 Otherwise, the macro is expanded and the expansion is considered
1003 in place of FORM. When a non-macro-call results, it is returned.
1005 The second optional arg ENVIRONMENT specifies an environment of macro
1006 definitions to shadow the loaded ones for use in file byte-compilation. */)
1007 (form, environment)
1008 Lisp_Object form;
1009 Lisp_Object environment;
1011 /* With cleanups from Hallvard Furuseth. */
1012 register Lisp_Object expander, sym, def, tem;
1014 while (1)
1016 /* Come back here each time we expand a macro call,
1017 in case it expands into another macro call. */
1018 if (!CONSP (form))
1019 break;
1020 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1021 def = sym = XCAR (form);
1022 tem = Qnil;
1023 /* Trace symbols aliases to other symbols
1024 until we get a symbol that is not an alias. */
1025 while (SYMBOLP (def))
1027 QUIT;
1028 sym = def;
1029 tem = Fassq (sym, environment);
1030 if (NILP (tem))
1032 def = XSYMBOL (sym)->function;
1033 if (!EQ (def, Qunbound))
1034 continue;
1036 break;
1038 /* Right now TEM is the result from SYM in ENVIRONMENT,
1039 and if TEM is nil then DEF is SYM's function definition. */
1040 if (NILP (tem))
1042 /* SYM is not mentioned in ENVIRONMENT.
1043 Look at its function definition. */
1044 if (EQ (def, Qunbound) || !CONSP (def))
1045 /* Not defined or definition not suitable */
1046 break;
1047 if (EQ (XCAR (def), Qautoload))
1049 /* Autoloading function: will it be a macro when loaded? */
1050 tem = Fnth (make_number (4), def);
1051 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1052 /* Yes, load it and try again. */
1054 struct gcpro gcpro1;
1055 GCPRO1 (form);
1056 do_autoload (def, sym);
1057 UNGCPRO;
1058 continue;
1060 else
1061 break;
1063 else if (!EQ (XCAR (def), Qmacro))
1064 break;
1065 else expander = XCDR (def);
1067 else
1069 expander = XCDR (tem);
1070 if (NILP (expander))
1071 break;
1073 form = apply1 (expander, XCDR (form));
1075 return form;
1078 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1079 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1080 TAG is evalled to get the tag to use; it must not be nil.
1082 Then the BODY is executed.
1083 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1084 If no throw happens, `catch' returns the value of the last BODY form.
1085 If a throw happens, it specifies the value to return from `catch'.
1086 usage: (catch TAG BODY...) */)
1087 (args)
1088 Lisp_Object args;
1090 register Lisp_Object tag;
1091 struct gcpro gcpro1;
1093 GCPRO1 (args);
1094 tag = Feval (Fcar (args));
1095 UNGCPRO;
1096 return internal_catch (tag, Fprogn, Fcdr (args));
1099 /* Set up a catch, then call C function FUNC on argument ARG.
1100 FUNC should return a Lisp_Object.
1101 This is how catches are done from within C code. */
1103 Lisp_Object
1104 internal_catch (tag, func, arg)
1105 Lisp_Object tag;
1106 Lisp_Object (*func) ();
1107 Lisp_Object arg;
1109 /* This structure is made part of the chain `catchlist'. */
1110 struct catchtag c;
1112 /* Fill in the components of c, and put it on the list. */
1113 c.next = catchlist;
1114 c.tag = tag;
1115 c.val = Qnil;
1116 c.backlist = backtrace_list;
1117 c.handlerlist = handlerlist;
1118 c.lisp_eval_depth = lisp_eval_depth;
1119 c.pdlcount = SPECPDL_INDEX ();
1120 c.poll_suppress_count = poll_suppress_count;
1121 c.interrupt_input_blocked = interrupt_input_blocked;
1122 c.gcpro = gcprolist;
1123 c.byte_stack = byte_stack_list;
1124 catchlist = &c;
1126 /* Call FUNC. */
1127 if (! _setjmp (c.jmp))
1128 c.val = (*func) (arg);
1130 /* Throw works by a longjmp that comes right here. */
1131 catchlist = c.next;
1132 return c.val;
1135 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1136 jump to that CATCH, returning VALUE as the value of that catch.
1138 This is the guts Fthrow and Fsignal; they differ only in the way
1139 they choose the catch tag to throw to. A catch tag for a
1140 condition-case form has a TAG of Qnil.
1142 Before each catch is discarded, unbind all special bindings and
1143 execute all unwind-protect clauses made above that catch. Unwind
1144 the handler stack as we go, so that the proper handlers are in
1145 effect for each unwind-protect clause we run. At the end, restore
1146 some static info saved in CATCH, and longjmp to the location
1147 specified in the
1149 This is used for correct unwinding in Fthrow and Fsignal. */
1151 static void
1152 unwind_to_catch (catch, value)
1153 struct catchtag *catch;
1154 Lisp_Object value;
1156 register int last_time;
1158 /* Save the value in the tag. */
1159 catch->val = value;
1161 /* Restore the polling-suppression count. */
1162 set_poll_suppress_count (catch->poll_suppress_count);
1163 interrupt_input_blocked = catch->interrupt_input_blocked;
1167 last_time = catchlist == catch;
1169 /* Unwind the specpdl stack, and then restore the proper set of
1170 handlers. */
1171 unbind_to (catchlist->pdlcount, Qnil);
1172 handlerlist = catchlist->handlerlist;
1173 catchlist = catchlist->next;
1175 while (! last_time);
1177 byte_stack_list = catch->byte_stack;
1178 gcprolist = catch->gcpro;
1179 #ifdef DEBUG_GCPRO
1180 if (gcprolist != 0)
1181 gcpro_level = gcprolist->level + 1;
1182 else
1183 gcpro_level = 0;
1184 #endif
1185 backtrace_list = catch->backlist;
1186 lisp_eval_depth = catch->lisp_eval_depth;
1188 _longjmp (catch->jmp, 1);
1191 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1192 doc: /* Throw to the catch for TAG and return VALUE from it.
1193 Both TAG and VALUE are evalled. */)
1194 (tag, value)
1195 register Lisp_Object tag, value;
1197 register struct catchtag *c;
1199 while (1)
1201 if (!NILP (tag))
1202 for (c = catchlist; c; c = c->next)
1204 if (EQ (c->tag, tag))
1205 unwind_to_catch (c, value);
1207 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1212 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1213 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1214 If BODYFORM completes normally, its value is returned
1215 after executing the UNWINDFORMS.
1216 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1217 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1218 (args)
1219 Lisp_Object args;
1221 Lisp_Object val;
1222 int count = SPECPDL_INDEX ();
1224 record_unwind_protect (Fprogn, Fcdr (args));
1225 val = Feval (Fcar (args));
1226 return unbind_to (count, val);
1229 /* Chain of condition handlers currently in effect.
1230 The elements of this chain are contained in the stack frames
1231 of Fcondition_case and internal_condition_case.
1232 When an error is signaled (by calling Fsignal, below),
1233 this chain is searched for an element that applies. */
1235 struct handler *handlerlist;
1237 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1238 doc: /* Regain control when an error is signaled.
1239 Executes BODYFORM and returns its value if no error happens.
1240 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1241 where the BODY is made of Lisp expressions.
1243 A handler is applicable to an error
1244 if CONDITION-NAME is one of the error's condition names.
1245 If an error happens, the first applicable handler is run.
1247 The car of a handler may be a list of condition names
1248 instead of a single condition name.
1250 When a handler handles an error,
1251 control returns to the condition-case and the handler BODY... is executed
1252 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1253 VAR may be nil; then you do not get access to the signal information.
1255 The value of the last BODY form is returned from the condition-case.
1256 See also the function `signal' for more info.
1257 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1258 (args)
1259 Lisp_Object args;
1261 Lisp_Object val;
1262 struct catchtag c;
1263 struct handler h;
1264 register Lisp_Object bodyform, handlers;
1265 volatile Lisp_Object var;
1267 var = Fcar (args);
1268 bodyform = Fcar (Fcdr (args));
1269 handlers = Fcdr (Fcdr (args));
1270 CHECK_SYMBOL (var);
1272 for (val = handlers; CONSP (val); val = XCDR (val))
1274 Lisp_Object tem;
1275 tem = XCAR (val);
1276 if (! (NILP (tem)
1277 || (CONSP (tem)
1278 && (SYMBOLP (XCAR (tem))
1279 || CONSP (XCAR (tem))))))
1280 error ("Invalid condition handler", tem);
1283 c.tag = Qnil;
1284 c.val = Qnil;
1285 c.backlist = backtrace_list;
1286 c.handlerlist = handlerlist;
1287 c.lisp_eval_depth = lisp_eval_depth;
1288 c.pdlcount = SPECPDL_INDEX ();
1289 c.poll_suppress_count = poll_suppress_count;
1290 c.interrupt_input_blocked = interrupt_input_blocked;
1291 c.gcpro = gcprolist;
1292 c.byte_stack = byte_stack_list;
1293 if (_setjmp (c.jmp))
1295 if (!NILP (h.var))
1296 specbind (h.var, c.val);
1297 val = Fprogn (Fcdr (h.chosen_clause));
1299 /* Note that this just undoes the binding of h.var; whoever
1300 longjumped to us unwound the stack to c.pdlcount before
1301 throwing. */
1302 unbind_to (c.pdlcount, Qnil);
1303 return val;
1305 c.next = catchlist;
1306 catchlist = &c;
1308 h.var = var;
1309 h.handler = handlers;
1310 h.next = handlerlist;
1311 h.tag = &c;
1312 handlerlist = &h;
1314 val = Feval (bodyform);
1315 catchlist = c.next;
1316 handlerlist = h.next;
1317 return val;
1320 /* Call the function BFUN with no arguments, catching errors within it
1321 according to HANDLERS. If there is an error, call HFUN with
1322 one argument which is the data that describes the error:
1323 (SIGNALNAME . DATA)
1325 HANDLERS can be a list of conditions to catch.
1326 If HANDLERS is Qt, catch all errors.
1327 If HANDLERS is Qerror, catch all errors
1328 but allow the debugger to run if that is enabled. */
1330 Lisp_Object
1331 internal_condition_case (bfun, handlers, hfun)
1332 Lisp_Object (*bfun) ();
1333 Lisp_Object handlers;
1334 Lisp_Object (*hfun) ();
1336 Lisp_Object val;
1337 struct catchtag c;
1338 struct handler h;
1340 #if 0 /* We now handle interrupt_input_blocked properly.
1341 What we still do not handle is exiting a signal handler. */
1342 abort ();
1343 #endif
1345 c.tag = Qnil;
1346 c.val = Qnil;
1347 c.backlist = backtrace_list;
1348 c.handlerlist = handlerlist;
1349 c.lisp_eval_depth = lisp_eval_depth;
1350 c.pdlcount = SPECPDL_INDEX ();
1351 c.poll_suppress_count = poll_suppress_count;
1352 c.interrupt_input_blocked = interrupt_input_blocked;
1353 c.gcpro = gcprolist;
1354 c.byte_stack = byte_stack_list;
1355 if (_setjmp (c.jmp))
1357 return (*hfun) (c.val);
1359 c.next = catchlist;
1360 catchlist = &c;
1361 h.handler = handlers;
1362 h.var = Qnil;
1363 h.next = handlerlist;
1364 h.tag = &c;
1365 handlerlist = &h;
1367 val = (*bfun) ();
1368 catchlist = c.next;
1369 handlerlist = h.next;
1370 return val;
1373 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1375 Lisp_Object
1376 internal_condition_case_1 (bfun, arg, handlers, hfun)
1377 Lisp_Object (*bfun) ();
1378 Lisp_Object arg;
1379 Lisp_Object handlers;
1380 Lisp_Object (*hfun) ();
1382 Lisp_Object val;
1383 struct catchtag c;
1384 struct handler h;
1386 c.tag = Qnil;
1387 c.val = Qnil;
1388 c.backlist = backtrace_list;
1389 c.handlerlist = handlerlist;
1390 c.lisp_eval_depth = lisp_eval_depth;
1391 c.pdlcount = SPECPDL_INDEX ();
1392 c.poll_suppress_count = poll_suppress_count;
1393 c.interrupt_input_blocked = interrupt_input_blocked;
1394 c.gcpro = gcprolist;
1395 c.byte_stack = byte_stack_list;
1396 if (_setjmp (c.jmp))
1398 return (*hfun) (c.val);
1400 c.next = catchlist;
1401 catchlist = &c;
1402 h.handler = handlers;
1403 h.var = Qnil;
1404 h.next = handlerlist;
1405 h.tag = &c;
1406 handlerlist = &h;
1408 val = (*bfun) (arg);
1409 catchlist = c.next;
1410 handlerlist = h.next;
1411 return val;
1415 /* Like internal_condition_case but call BFUN with NARGS as first,
1416 and ARGS as second argument. */
1418 Lisp_Object
1419 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1420 Lisp_Object (*bfun) ();
1421 int nargs;
1422 Lisp_Object *args;
1423 Lisp_Object handlers;
1424 Lisp_Object (*hfun) ();
1426 Lisp_Object val;
1427 struct catchtag c;
1428 struct handler h;
1430 c.tag = Qnil;
1431 c.val = Qnil;
1432 c.backlist = backtrace_list;
1433 c.handlerlist = handlerlist;
1434 c.lisp_eval_depth = lisp_eval_depth;
1435 c.pdlcount = SPECPDL_INDEX ();
1436 c.poll_suppress_count = poll_suppress_count;
1437 c.interrupt_input_blocked = interrupt_input_blocked;
1438 c.gcpro = gcprolist;
1439 c.byte_stack = byte_stack_list;
1440 if (_setjmp (c.jmp))
1442 return (*hfun) (c.val);
1444 c.next = catchlist;
1445 catchlist = &c;
1446 h.handler = handlers;
1447 h.var = Qnil;
1448 h.next = handlerlist;
1449 h.tag = &c;
1450 handlerlist = &h;
1452 val = (*bfun) (nargs, args);
1453 catchlist = c.next;
1454 handlerlist = h.next;
1455 return val;
1459 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1460 Lisp_Object, Lisp_Object,
1461 Lisp_Object *));
1463 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1464 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1465 This function does not return.
1467 An error symbol is a symbol with an `error-conditions' property
1468 that is a list of condition names.
1469 A handler for any of those names will get to handle this signal.
1470 The symbol `error' should normally be one of them.
1472 DATA should be a list. Its elements are printed as part of the error message.
1473 See Info anchor `(elisp)Definition of signal' for some details on how this
1474 error message is constructed.
1475 If the signal is handled, DATA is made available to the handler.
1476 See also the function `condition-case'. */)
1477 (error_symbol, data)
1478 Lisp_Object error_symbol, data;
1480 /* When memory is full, ERROR-SYMBOL is nil,
1481 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1482 That is a special case--don't do this in other situations. */
1483 register struct handler *allhandlers = handlerlist;
1484 Lisp_Object conditions;
1485 extern int gc_in_progress;
1486 extern int waiting_for_input;
1487 Lisp_Object debugger_value;
1488 Lisp_Object string;
1489 Lisp_Object real_error_symbol;
1490 struct backtrace *bp;
1492 immediate_quit = handling_signal = 0;
1493 abort_on_gc = 0;
1494 if (gc_in_progress || waiting_for_input)
1495 abort ();
1497 if (NILP (error_symbol))
1498 real_error_symbol = Fcar (data);
1499 else
1500 real_error_symbol = error_symbol;
1502 #if 0 /* rms: I don't know why this was here,
1503 but it is surely wrong for an error that is handled. */
1504 #ifdef HAVE_X_WINDOWS
1505 if (display_hourglass_p)
1506 cancel_hourglass ();
1507 #endif
1508 #endif
1510 /* This hook is used by edebug. */
1511 if (! NILP (Vsignal_hook_function)
1512 && ! NILP (error_symbol))
1513 call2 (Vsignal_hook_function, error_symbol, data);
1515 conditions = Fget (real_error_symbol, Qerror_conditions);
1517 /* Remember from where signal was called. Skip over the frame for
1518 `signal' itself. If a frame for `error' follows, skip that,
1519 too. Don't do this when ERROR_SYMBOL is nil, because that
1520 is a memory-full error. */
1521 Vsignaling_function = Qnil;
1522 if (backtrace_list && !NILP (error_symbol))
1524 bp = backtrace_list->next;
1525 if (bp && bp->function && EQ (*bp->function, Qerror))
1526 bp = bp->next;
1527 if (bp && bp->function)
1528 Vsignaling_function = *bp->function;
1531 for (; handlerlist; handlerlist = handlerlist->next)
1533 register Lisp_Object clause;
1535 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1536 max_lisp_eval_depth = lisp_eval_depth + 20;
1538 if (specpdl_size + 40 > max_specpdl_size)
1539 max_specpdl_size = specpdl_size + 40;
1541 clause = find_handler_clause (handlerlist->handler, conditions,
1542 error_symbol, data, &debugger_value);
1544 if (EQ (clause, Qlambda))
1546 /* We can't return values to code which signaled an error, but we
1547 can continue code which has signaled a quit. */
1548 if (EQ (real_error_symbol, Qquit))
1549 return Qnil;
1550 else
1551 error ("Cannot return from the debugger in an error");
1554 if (!NILP (clause))
1556 Lisp_Object unwind_data;
1557 struct handler *h = handlerlist;
1559 handlerlist = allhandlers;
1561 if (NILP (error_symbol))
1562 unwind_data = data;
1563 else
1564 unwind_data = Fcons (error_symbol, data);
1565 h->chosen_clause = clause;
1566 unwind_to_catch (h->tag, unwind_data);
1570 handlerlist = allhandlers;
1571 /* If no handler is present now, try to run the debugger,
1572 and if that fails, throw to top level. */
1573 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1574 if (catchlist != 0)
1575 Fthrow (Qtop_level, Qt);
1577 if (! NILP (error_symbol))
1578 data = Fcons (error_symbol, data);
1580 string = Ferror_message_string (data);
1581 fatal ("%s", SDATA (string), 0);
1584 /* Return nonzero iff LIST is a non-nil atom or
1585 a list containing one of CONDITIONS. */
1587 static int
1588 wants_debugger (list, conditions)
1589 Lisp_Object list, conditions;
1591 if (NILP (list))
1592 return 0;
1593 if (! CONSP (list))
1594 return 1;
1596 while (CONSP (conditions))
1598 Lisp_Object this, tail;
1599 this = XCAR (conditions);
1600 for (tail = list; CONSP (tail); tail = XCDR (tail))
1601 if (EQ (XCAR (tail), this))
1602 return 1;
1603 conditions = XCDR (conditions);
1605 return 0;
1608 /* Return 1 if an error with condition-symbols CONDITIONS,
1609 and described by SIGNAL-DATA, should skip the debugger
1610 according to debugger-ignored-errors. */
1612 static int
1613 skip_debugger (conditions, data)
1614 Lisp_Object conditions, data;
1616 Lisp_Object tail;
1617 int first_string = 1;
1618 Lisp_Object error_message;
1620 error_message = Qnil;
1621 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1623 if (STRINGP (XCAR (tail)))
1625 if (first_string)
1627 error_message = Ferror_message_string (data);
1628 first_string = 0;
1631 if (fast_string_match (XCAR (tail), error_message) >= 0)
1632 return 1;
1634 else
1636 Lisp_Object contail;
1638 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1639 if (EQ (XCAR (tail), XCAR (contail)))
1640 return 1;
1644 return 0;
1647 /* Value of Qlambda means we have called debugger and user has continued.
1648 There are two ways to pass SIG and DATA:
1649 = SIG is the error symbol, and DATA is the rest of the data.
1650 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1651 This is for memory-full errors only.
1653 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1655 static Lisp_Object
1656 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1657 Lisp_Object handlers, conditions, sig, data;
1658 Lisp_Object *debugger_value_ptr;
1660 register Lisp_Object h;
1661 register Lisp_Object tem;
1663 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1664 return Qt;
1665 /* error is used similarly, but means print an error message
1666 and run the debugger if that is enabled. */
1667 if (EQ (handlers, Qerror)
1668 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1669 there is a handler. */
1671 int count = SPECPDL_INDEX ();
1672 int debugger_called = 0;
1673 Lisp_Object sig_symbol, combined_data;
1674 /* This is set to 1 if we are handling a memory-full error,
1675 because these must not run the debugger.
1676 (There is no room in memory to do that!) */
1677 int no_debugger = 0;
1679 if (NILP (sig))
1681 combined_data = data;
1682 sig_symbol = Fcar (data);
1683 no_debugger = 1;
1685 else
1687 combined_data = Fcons (sig, data);
1688 sig_symbol = sig;
1691 if (wants_debugger (Vstack_trace_on_error, conditions))
1693 #ifdef PROTOTYPES
1694 internal_with_output_to_temp_buffer ("*Backtrace*",
1695 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1696 Qnil);
1697 #else
1698 internal_with_output_to_temp_buffer ("*Backtrace*",
1699 Fbacktrace, Qnil);
1700 #endif
1702 if (! no_debugger
1703 && (EQ (sig_symbol, Qquit)
1704 ? debug_on_quit
1705 : wants_debugger (Vdebug_on_error, conditions))
1706 && ! skip_debugger (conditions, combined_data)
1707 && when_entered_debugger < num_nonmacro_input_events)
1709 specbind (Qdebug_on_error, Qnil);
1710 *debugger_value_ptr
1711 = call_debugger (Fcons (Qerror,
1712 Fcons (combined_data, Qnil)));
1713 debugger_called = 1;
1715 /* If there is no handler, return saying whether we ran the debugger. */
1716 if (EQ (handlers, Qerror))
1718 if (debugger_called)
1719 return unbind_to (count, Qlambda);
1720 return Qt;
1723 for (h = handlers; CONSP (h); h = Fcdr (h))
1725 Lisp_Object handler, condit;
1727 handler = Fcar (h);
1728 if (!CONSP (handler))
1729 continue;
1730 condit = Fcar (handler);
1731 /* Handle a single condition name in handler HANDLER. */
1732 if (SYMBOLP (condit))
1734 tem = Fmemq (Fcar (handler), conditions);
1735 if (!NILP (tem))
1736 return handler;
1738 /* Handle a list of condition names in handler HANDLER. */
1739 else if (CONSP (condit))
1741 while (CONSP (condit))
1743 tem = Fmemq (Fcar (condit), conditions);
1744 if (!NILP (tem))
1745 return handler;
1746 condit = XCDR (condit);
1750 return Qnil;
1753 /* dump an error message; called like printf */
1755 /* VARARGS 1 */
1756 void
1757 error (m, a1, a2, a3)
1758 char *m;
1759 char *a1, *a2, *a3;
1761 char buf[200];
1762 int size = 200;
1763 int mlen;
1764 char *buffer = buf;
1765 char *args[3];
1766 int allocated = 0;
1767 Lisp_Object string;
1769 args[0] = a1;
1770 args[1] = a2;
1771 args[2] = a3;
1773 mlen = strlen (m);
1775 while (1)
1777 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1778 if (used < size)
1779 break;
1780 size *= 2;
1781 if (allocated)
1782 buffer = (char *) xrealloc (buffer, size);
1783 else
1785 buffer = (char *) xmalloc (size);
1786 allocated = 1;
1790 string = build_string (buffer);
1791 if (allocated)
1792 xfree (buffer);
1794 Fsignal (Qerror, Fcons (string, Qnil));
1795 abort ();
1798 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1799 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1800 This means it contains a description for how to read arguments to give it.
1801 The value is nil for an invalid function or a symbol with no function
1802 definition.
1804 Interactively callable functions include strings and vectors (treated
1805 as keyboard macros), lambda-expressions that contain a top-level call
1806 to `interactive', autoload definitions made by `autoload' with non-nil
1807 fourth argument, and some of the built-in functions of Lisp.
1809 Also, a symbol satisfies `commandp' if its function definition does so.
1811 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1812 then strings and vectors are not accepted. */)
1813 (function, for_call_interactively)
1814 Lisp_Object function, for_call_interactively;
1816 register Lisp_Object fun;
1817 register Lisp_Object funcar;
1819 fun = function;
1821 fun = indirect_function (fun);
1822 if (EQ (fun, Qunbound))
1823 return Qnil;
1825 /* Emacs primitives are interactive if their DEFUN specifies an
1826 interactive spec. */
1827 if (SUBRP (fun))
1829 if (XSUBR (fun)->prompt)
1830 return Qt;
1831 else
1832 return Qnil;
1835 /* Bytecode objects are interactive if they are long enough to
1836 have an element whose index is COMPILED_INTERACTIVE, which is
1837 where the interactive spec is stored. */
1838 else if (COMPILEDP (fun))
1839 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1840 ? Qt : Qnil);
1842 /* Strings and vectors are keyboard macros. */
1843 if (NILP (for_call_interactively) && (STRINGP (fun) || VECTORP (fun)))
1844 return Qt;
1846 /* Lists may represent commands. */
1847 if (!CONSP (fun))
1848 return Qnil;
1849 funcar = XCAR (fun);
1850 if (EQ (funcar, Qlambda))
1851 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
1852 if (EQ (funcar, Qautoload))
1853 return Fcar (Fcdr (Fcdr (XCDR (fun))));
1854 else
1855 return Qnil;
1858 /* ARGSUSED */
1859 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1860 doc: /* Define FUNCTION to autoload from FILE.
1861 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1862 Third arg DOCSTRING is documentation for the function.
1863 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1864 Fifth arg TYPE indicates the type of the object:
1865 nil or omitted says FUNCTION is a function,
1866 `keymap' says FUNCTION is really a keymap, and
1867 `macro' or t says FUNCTION is really a macro.
1868 Third through fifth args give info about the real definition.
1869 They default to nil.
1870 If FUNCTION is already defined other than as an autoload,
1871 this does nothing and returns nil. */)
1872 (function, file, docstring, interactive, type)
1873 Lisp_Object function, file, docstring, interactive, type;
1875 #ifdef NO_ARG_ARRAY
1876 Lisp_Object args[4];
1877 #endif
1879 CHECK_SYMBOL (function);
1880 CHECK_STRING (file);
1882 /* If function is defined and not as an autoload, don't override */
1883 if (!EQ (XSYMBOL (function)->function, Qunbound)
1884 && !(CONSP (XSYMBOL (function)->function)
1885 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1886 return Qnil;
1888 if (NILP (Vpurify_flag))
1889 /* Only add entries after dumping, because the ones before are
1890 not useful and else we get loads of them from the loaddefs.el. */
1891 LOADHIST_ATTACH (Fcons (Qautoload, function));
1893 #ifdef NO_ARG_ARRAY
1894 args[0] = file;
1895 args[1] = docstring;
1896 args[2] = interactive;
1897 args[3] = type;
1899 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1900 #else /* NO_ARG_ARRAY */
1901 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1902 #endif /* not NO_ARG_ARRAY */
1905 Lisp_Object
1906 un_autoload (oldqueue)
1907 Lisp_Object oldqueue;
1909 register Lisp_Object queue, first, second;
1911 /* Queue to unwind is current value of Vautoload_queue.
1912 oldqueue is the shadowed value to leave in Vautoload_queue. */
1913 queue = Vautoload_queue;
1914 Vautoload_queue = oldqueue;
1915 while (CONSP (queue))
1917 first = XCAR (queue);
1918 second = Fcdr (first);
1919 first = Fcar (first);
1920 if (EQ (second, Qnil))
1921 Vfeatures = first;
1922 else
1923 Ffset (first, second);
1924 queue = XCDR (queue);
1926 return Qnil;
1929 /* Load an autoloaded function.
1930 FUNNAME is the symbol which is the function's name.
1931 FUNDEF is the autoload definition (a list). */
1933 void
1934 do_autoload (fundef, funname)
1935 Lisp_Object fundef, funname;
1937 int count = SPECPDL_INDEX ();
1938 Lisp_Object fun, queue, first, second;
1939 struct gcpro gcpro1, gcpro2, gcpro3;
1941 /* This is to make sure that loadup.el gives a clear picture
1942 of what files are preloaded and when. */
1943 if (! NILP (Vpurify_flag))
1944 error ("Attempt to autoload %s while preparing to dump",
1945 SDATA (SYMBOL_NAME (funname)));
1947 fun = funname;
1948 CHECK_SYMBOL (funname);
1949 GCPRO3 (fun, funname, fundef);
1951 /* Preserve the match data. */
1952 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1954 /* Value saved here is to be restored into Vautoload_queue. */
1955 record_unwind_protect (un_autoload, Vautoload_queue);
1956 Vautoload_queue = Qt;
1957 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1959 /* Save the old autoloads, in case we ever do an unload. */
1960 queue = Vautoload_queue;
1961 while (CONSP (queue))
1963 first = XCAR (queue);
1964 second = Fcdr (first);
1965 first = Fcar (first);
1967 /* Note: This test is subtle. The cdr of an autoload-queue entry
1968 may be an atom if the autoload entry was generated by a defalias
1969 or fset. */
1970 if (CONSP (second))
1971 Fput (first, Qautoload, (XCDR (second)));
1973 queue = XCDR (queue);
1976 /* Once loading finishes, don't undo it. */
1977 Vautoload_queue = Qt;
1978 unbind_to (count, Qnil);
1980 fun = Findirect_function (fun);
1982 if (!NILP (Fequal (fun, fundef)))
1983 error ("Autoloading failed to define function %s",
1984 SDATA (SYMBOL_NAME (funname)));
1985 UNGCPRO;
1989 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1990 doc: /* Evaluate FORM and return its value. */)
1991 (form)
1992 Lisp_Object form;
1994 Lisp_Object fun, val, original_fun, original_args;
1995 Lisp_Object funcar;
1996 struct backtrace backtrace;
1997 struct gcpro gcpro1, gcpro2, gcpro3;
1999 if (handling_signal || INPUT_BLOCKED_P)
2000 abort ();
2002 if (SYMBOLP (form))
2003 return Fsymbol_value (form);
2004 if (!CONSP (form))
2005 return form;
2007 QUIT;
2008 if (consing_since_gc > gc_cons_threshold)
2010 GCPRO1 (form);
2011 Fgarbage_collect ();
2012 UNGCPRO;
2015 if (++lisp_eval_depth > max_lisp_eval_depth)
2017 if (max_lisp_eval_depth < 100)
2018 max_lisp_eval_depth = 100;
2019 if (lisp_eval_depth > max_lisp_eval_depth)
2020 error ("Lisp nesting exceeds max-lisp-eval-depth");
2023 original_fun = Fcar (form);
2024 original_args = Fcdr (form);
2026 backtrace.next = backtrace_list;
2027 backtrace_list = &backtrace;
2028 backtrace.function = &original_fun; /* This also protects them from gc */
2029 backtrace.args = &original_args;
2030 backtrace.nargs = UNEVALLED;
2031 backtrace.evalargs = 1;
2032 backtrace.debug_on_exit = 0;
2034 if (debug_on_next_call)
2035 do_debug_on_call (Qt);
2037 /* At this point, only original_fun and original_args
2038 have values that will be used below */
2039 retry:
2040 fun = Findirect_function (original_fun);
2042 if (SUBRP (fun))
2044 Lisp_Object numargs;
2045 Lisp_Object argvals[8];
2046 Lisp_Object args_left;
2047 register int i, maxargs;
2049 args_left = original_args;
2050 numargs = Flength (args_left);
2052 if (XINT (numargs) < XSUBR (fun)->min_args ||
2053 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2054 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2056 if (XSUBR (fun)->max_args == UNEVALLED)
2058 backtrace.evalargs = 0;
2059 val = (*XSUBR (fun)->function) (args_left);
2060 goto done;
2063 if (XSUBR (fun)->max_args == MANY)
2065 /* Pass a vector of evaluated arguments */
2066 Lisp_Object *vals;
2067 register int argnum = 0;
2069 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2071 GCPRO3 (args_left, fun, fun);
2072 gcpro3.var = vals;
2073 gcpro3.nvars = 0;
2075 while (!NILP (args_left))
2077 vals[argnum++] = Feval (Fcar (args_left));
2078 args_left = Fcdr (args_left);
2079 gcpro3.nvars = argnum;
2082 backtrace.args = vals;
2083 backtrace.nargs = XINT (numargs);
2085 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2086 UNGCPRO;
2087 goto done;
2090 GCPRO3 (args_left, fun, fun);
2091 gcpro3.var = argvals;
2092 gcpro3.nvars = 0;
2094 maxargs = XSUBR (fun)->max_args;
2095 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2097 argvals[i] = Feval (Fcar (args_left));
2098 gcpro3.nvars = ++i;
2101 UNGCPRO;
2103 backtrace.args = argvals;
2104 backtrace.nargs = XINT (numargs);
2106 switch (i)
2108 case 0:
2109 val = (*XSUBR (fun)->function) ();
2110 goto done;
2111 case 1:
2112 val = (*XSUBR (fun)->function) (argvals[0]);
2113 goto done;
2114 case 2:
2115 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2116 goto done;
2117 case 3:
2118 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2119 argvals[2]);
2120 goto done;
2121 case 4:
2122 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2123 argvals[2], argvals[3]);
2124 goto done;
2125 case 5:
2126 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2127 argvals[3], argvals[4]);
2128 goto done;
2129 case 6:
2130 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2131 argvals[3], argvals[4], argvals[5]);
2132 goto done;
2133 case 7:
2134 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2135 argvals[3], argvals[4], argvals[5],
2136 argvals[6]);
2137 goto done;
2139 case 8:
2140 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2141 argvals[3], argvals[4], argvals[5],
2142 argvals[6], argvals[7]);
2143 goto done;
2145 default:
2146 /* Someone has created a subr that takes more arguments than
2147 is supported by this code. We need to either rewrite the
2148 subr to use a different argument protocol, or add more
2149 cases to this switch. */
2150 abort ();
2153 if (COMPILEDP (fun))
2154 val = apply_lambda (fun, original_args, 1);
2155 else
2157 if (!CONSP (fun))
2158 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2159 funcar = Fcar (fun);
2160 if (!SYMBOLP (funcar))
2161 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2162 if (EQ (funcar, Qautoload))
2164 do_autoload (fun, original_fun);
2165 goto retry;
2167 if (EQ (funcar, Qmacro))
2168 val = Feval (apply1 (Fcdr (fun), original_args));
2169 else if (EQ (funcar, Qlambda))
2170 val = apply_lambda (fun, original_args, 1);
2171 else
2172 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2174 done:
2175 lisp_eval_depth--;
2176 if (backtrace.debug_on_exit)
2177 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2178 backtrace_list = backtrace.next;
2180 return val;
2183 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2184 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2185 Then return the value FUNCTION returns.
2186 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2187 usage: (apply FUNCTION &rest ARGUMENTS) */)
2188 (nargs, args)
2189 int nargs;
2190 Lisp_Object *args;
2192 register int i, numargs;
2193 register Lisp_Object spread_arg;
2194 register Lisp_Object *funcall_args;
2195 Lisp_Object fun;
2196 struct gcpro gcpro1;
2198 fun = args [0];
2199 funcall_args = 0;
2200 spread_arg = args [nargs - 1];
2201 CHECK_LIST (spread_arg);
2203 numargs = XINT (Flength (spread_arg));
2205 if (numargs == 0)
2206 return Ffuncall (nargs - 1, args);
2207 else if (numargs == 1)
2209 args [nargs - 1] = XCAR (spread_arg);
2210 return Ffuncall (nargs, args);
2213 numargs += nargs - 2;
2215 fun = indirect_function (fun);
2216 if (EQ (fun, Qunbound))
2218 /* Let funcall get the error */
2219 fun = args[0];
2220 goto funcall;
2223 if (SUBRP (fun))
2225 if (numargs < XSUBR (fun)->min_args
2226 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2227 goto funcall; /* Let funcall get the error */
2228 else if (XSUBR (fun)->max_args > numargs)
2230 /* Avoid making funcall cons up a yet another new vector of arguments
2231 by explicitly supplying nil's for optional values */
2232 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2233 * sizeof (Lisp_Object));
2234 for (i = numargs; i < XSUBR (fun)->max_args;)
2235 funcall_args[++i] = Qnil;
2236 GCPRO1 (*funcall_args);
2237 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2240 funcall:
2241 /* We add 1 to numargs because funcall_args includes the
2242 function itself as well as its arguments. */
2243 if (!funcall_args)
2245 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2246 * sizeof (Lisp_Object));
2247 GCPRO1 (*funcall_args);
2248 gcpro1.nvars = 1 + numargs;
2251 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2252 /* Spread the last arg we got. Its first element goes in
2253 the slot that it used to occupy, hence this value of I. */
2254 i = nargs - 1;
2255 while (!NILP (spread_arg))
2257 funcall_args [i++] = XCAR (spread_arg);
2258 spread_arg = XCDR (spread_arg);
2261 /* By convention, the caller needs to gcpro Ffuncall's args. */
2262 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2265 /* Run hook variables in various ways. */
2267 enum run_hooks_condition {to_completion, until_success, until_failure};
2268 static Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
2269 enum run_hooks_condition));
2271 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2272 doc: /* Run each hook in HOOKS. Major mode functions use this.
2273 Each argument should be a symbol, a hook variable.
2274 These symbols are processed in the order specified.
2275 If a hook symbol has a non-nil value, that value may be a function
2276 or a list of functions to be called to run the hook.
2277 If the value is a function, it is called with no arguments.
2278 If it is a list, the elements are called, in order, with no arguments.
2280 Do not use `make-local-variable' to make a hook variable buffer-local.
2281 Instead, use `add-hook' and specify t for the LOCAL argument.
2282 usage: (run-hooks &rest HOOKS) */)
2283 (nargs, args)
2284 int nargs;
2285 Lisp_Object *args;
2287 Lisp_Object hook[1];
2288 register int i;
2290 for (i = 0; i < nargs; i++)
2292 hook[0] = args[i];
2293 run_hook_with_args (1, hook, to_completion);
2296 return Qnil;
2299 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2300 Srun_hook_with_args, 1, MANY, 0,
2301 doc: /* Run HOOK with the specified arguments ARGS.
2302 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2303 value, that value may be a function or a list of functions to be
2304 called to run the hook. If the value is a function, it is called with
2305 the given arguments and its return value is returned. If it is a list
2306 of functions, those functions are called, in order,
2307 with the given arguments ARGS.
2308 It is best not to depend on the value return by `run-hook-with-args',
2309 as that may change.
2311 Do not use `make-local-variable' to make a hook variable buffer-local.
2312 Instead, use `add-hook' and specify t for the LOCAL argument.
2313 usage: (run-hook-with-args HOOK &rest ARGS) */)
2314 (nargs, args)
2315 int nargs;
2316 Lisp_Object *args;
2318 return run_hook_with_args (nargs, args, to_completion);
2321 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2322 Srun_hook_with_args_until_success, 1, MANY, 0,
2323 doc: /* Run HOOK with the specified arguments ARGS.
2324 HOOK should be a symbol, a hook variable. Its value should
2325 be a list of functions. We call those functions, one by one,
2326 passing arguments ARGS to each of them, until one of them
2327 returns a non-nil value. Then we return that value.
2328 If all the functions return nil, we return nil.
2330 Do not use `make-local-variable' to make a hook variable buffer-local.
2331 Instead, use `add-hook' and specify t for the LOCAL argument.
2332 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2333 (nargs, args)
2334 int nargs;
2335 Lisp_Object *args;
2337 return run_hook_with_args (nargs, args, until_success);
2340 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2341 Srun_hook_with_args_until_failure, 1, MANY, 0,
2342 doc: /* Run HOOK with the specified arguments ARGS.
2343 HOOK should be a symbol, a hook variable. Its value should
2344 be a list of functions. We call those functions, one by one,
2345 passing arguments ARGS to each of them, until one of them
2346 returns nil. Then we return nil.
2347 If all the functions return non-nil, we return non-nil.
2349 Do not use `make-local-variable' to make a hook variable buffer-local.
2350 Instead, use `add-hook' and specify t for the LOCAL argument.
2351 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2352 (nargs, args)
2353 int nargs;
2354 Lisp_Object *args;
2356 return run_hook_with_args (nargs, args, until_failure);
2359 /* ARGS[0] should be a hook symbol.
2360 Call each of the functions in the hook value, passing each of them
2361 as arguments all the rest of ARGS (all NARGS - 1 elements).
2362 COND specifies a condition to test after each call
2363 to decide whether to stop.
2364 The caller (or its caller, etc) must gcpro all of ARGS,
2365 except that it isn't necessary to gcpro ARGS[0]. */
2367 static Lisp_Object
2368 run_hook_with_args (nargs, args, cond)
2369 int nargs;
2370 Lisp_Object *args;
2371 enum run_hooks_condition cond;
2373 Lisp_Object sym, val, ret;
2374 Lisp_Object globals;
2375 struct gcpro gcpro1, gcpro2, gcpro3;
2377 /* If we are dying or still initializing,
2378 don't do anything--it would probably crash if we tried. */
2379 if (NILP (Vrun_hooks))
2380 return Qnil;
2382 sym = args[0];
2383 val = find_symbol_value (sym);
2384 ret = (cond == until_failure ? Qt : Qnil);
2386 if (EQ (val, Qunbound) || NILP (val))
2387 return ret;
2388 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2390 args[0] = val;
2391 return Ffuncall (nargs, args);
2393 else
2395 globals = Qnil;
2396 GCPRO3 (sym, val, globals);
2398 for (;
2399 CONSP (val) && ((cond == to_completion)
2400 || (cond == until_success ? NILP (ret)
2401 : !NILP (ret)));
2402 val = XCDR (val))
2404 if (EQ (XCAR (val), Qt))
2406 /* t indicates this hook has a local binding;
2407 it means to run the global binding too. */
2409 for (globals = Fdefault_value (sym);
2410 CONSP (globals) && ((cond == to_completion)
2411 || (cond == until_success ? NILP (ret)
2412 : !NILP (ret)));
2413 globals = XCDR (globals))
2415 args[0] = XCAR (globals);
2416 /* In a global value, t should not occur. If it does, we
2417 must ignore it to avoid an endless loop. */
2418 if (!EQ (args[0], Qt))
2419 ret = Ffuncall (nargs, args);
2422 else
2424 args[0] = XCAR (val);
2425 ret = Ffuncall (nargs, args);
2429 UNGCPRO;
2430 return ret;
2434 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2435 present value of that symbol.
2436 Call each element of FUNLIST,
2437 passing each of them the rest of ARGS.
2438 The caller (or its caller, etc) must gcpro all of ARGS,
2439 except that it isn't necessary to gcpro ARGS[0]. */
2441 Lisp_Object
2442 run_hook_list_with_args (funlist, nargs, args)
2443 Lisp_Object funlist;
2444 int nargs;
2445 Lisp_Object *args;
2447 Lisp_Object sym;
2448 Lisp_Object val;
2449 Lisp_Object globals;
2450 struct gcpro gcpro1, gcpro2, gcpro3;
2452 sym = args[0];
2453 globals = Qnil;
2454 GCPRO3 (sym, val, globals);
2456 for (val = funlist; CONSP (val); val = XCDR (val))
2458 if (EQ (XCAR (val), Qt))
2460 /* t indicates this hook has a local binding;
2461 it means to run the global binding too. */
2463 for (globals = Fdefault_value (sym);
2464 CONSP (globals);
2465 globals = XCDR (globals))
2467 args[0] = XCAR (globals);
2468 /* In a global value, t should not occur. If it does, we
2469 must ignore it to avoid an endless loop. */
2470 if (!EQ (args[0], Qt))
2471 Ffuncall (nargs, args);
2474 else
2476 args[0] = XCAR (val);
2477 Ffuncall (nargs, args);
2480 UNGCPRO;
2481 return Qnil;
2484 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2486 void
2487 run_hook_with_args_2 (hook, arg1, arg2)
2488 Lisp_Object hook, arg1, arg2;
2490 Lisp_Object temp[3];
2491 temp[0] = hook;
2492 temp[1] = arg1;
2493 temp[2] = arg2;
2495 Frun_hook_with_args (3, temp);
2498 /* Apply fn to arg */
2499 Lisp_Object
2500 apply1 (fn, arg)
2501 Lisp_Object fn, arg;
2503 struct gcpro gcpro1;
2505 GCPRO1 (fn);
2506 if (NILP (arg))
2507 RETURN_UNGCPRO (Ffuncall (1, &fn));
2508 gcpro1.nvars = 2;
2509 #ifdef NO_ARG_ARRAY
2511 Lisp_Object args[2];
2512 args[0] = fn;
2513 args[1] = arg;
2514 gcpro1.var = args;
2515 RETURN_UNGCPRO (Fapply (2, args));
2517 #else /* not NO_ARG_ARRAY */
2518 RETURN_UNGCPRO (Fapply (2, &fn));
2519 #endif /* not NO_ARG_ARRAY */
2522 /* Call function fn on no arguments */
2523 Lisp_Object
2524 call0 (fn)
2525 Lisp_Object fn;
2527 struct gcpro gcpro1;
2529 GCPRO1 (fn);
2530 RETURN_UNGCPRO (Ffuncall (1, &fn));
2533 /* Call function fn with 1 argument arg1 */
2534 /* ARGSUSED */
2535 Lisp_Object
2536 call1 (fn, arg1)
2537 Lisp_Object fn, arg1;
2539 struct gcpro gcpro1;
2540 #ifdef NO_ARG_ARRAY
2541 Lisp_Object args[2];
2543 args[0] = fn;
2544 args[1] = arg1;
2545 GCPRO1 (args[0]);
2546 gcpro1.nvars = 2;
2547 RETURN_UNGCPRO (Ffuncall (2, args));
2548 #else /* not NO_ARG_ARRAY */
2549 GCPRO1 (fn);
2550 gcpro1.nvars = 2;
2551 RETURN_UNGCPRO (Ffuncall (2, &fn));
2552 #endif /* not NO_ARG_ARRAY */
2555 /* Call function fn with 2 arguments arg1, arg2 */
2556 /* ARGSUSED */
2557 Lisp_Object
2558 call2 (fn, arg1, arg2)
2559 Lisp_Object fn, arg1, arg2;
2561 struct gcpro gcpro1;
2562 #ifdef NO_ARG_ARRAY
2563 Lisp_Object args[3];
2564 args[0] = fn;
2565 args[1] = arg1;
2566 args[2] = arg2;
2567 GCPRO1 (args[0]);
2568 gcpro1.nvars = 3;
2569 RETURN_UNGCPRO (Ffuncall (3, args));
2570 #else /* not NO_ARG_ARRAY */
2571 GCPRO1 (fn);
2572 gcpro1.nvars = 3;
2573 RETURN_UNGCPRO (Ffuncall (3, &fn));
2574 #endif /* not NO_ARG_ARRAY */
2577 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2578 /* ARGSUSED */
2579 Lisp_Object
2580 call3 (fn, arg1, arg2, arg3)
2581 Lisp_Object fn, arg1, arg2, arg3;
2583 struct gcpro gcpro1;
2584 #ifdef NO_ARG_ARRAY
2585 Lisp_Object args[4];
2586 args[0] = fn;
2587 args[1] = arg1;
2588 args[2] = arg2;
2589 args[3] = arg3;
2590 GCPRO1 (args[0]);
2591 gcpro1.nvars = 4;
2592 RETURN_UNGCPRO (Ffuncall (4, args));
2593 #else /* not NO_ARG_ARRAY */
2594 GCPRO1 (fn);
2595 gcpro1.nvars = 4;
2596 RETURN_UNGCPRO (Ffuncall (4, &fn));
2597 #endif /* not NO_ARG_ARRAY */
2600 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2601 /* ARGSUSED */
2602 Lisp_Object
2603 call4 (fn, arg1, arg2, arg3, arg4)
2604 Lisp_Object fn, arg1, arg2, arg3, arg4;
2606 struct gcpro gcpro1;
2607 #ifdef NO_ARG_ARRAY
2608 Lisp_Object args[5];
2609 args[0] = fn;
2610 args[1] = arg1;
2611 args[2] = arg2;
2612 args[3] = arg3;
2613 args[4] = arg4;
2614 GCPRO1 (args[0]);
2615 gcpro1.nvars = 5;
2616 RETURN_UNGCPRO (Ffuncall (5, args));
2617 #else /* not NO_ARG_ARRAY */
2618 GCPRO1 (fn);
2619 gcpro1.nvars = 5;
2620 RETURN_UNGCPRO (Ffuncall (5, &fn));
2621 #endif /* not NO_ARG_ARRAY */
2624 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2625 /* ARGSUSED */
2626 Lisp_Object
2627 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2628 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2630 struct gcpro gcpro1;
2631 #ifdef NO_ARG_ARRAY
2632 Lisp_Object args[6];
2633 args[0] = fn;
2634 args[1] = arg1;
2635 args[2] = arg2;
2636 args[3] = arg3;
2637 args[4] = arg4;
2638 args[5] = arg5;
2639 GCPRO1 (args[0]);
2640 gcpro1.nvars = 6;
2641 RETURN_UNGCPRO (Ffuncall (6, args));
2642 #else /* not NO_ARG_ARRAY */
2643 GCPRO1 (fn);
2644 gcpro1.nvars = 6;
2645 RETURN_UNGCPRO (Ffuncall (6, &fn));
2646 #endif /* not NO_ARG_ARRAY */
2649 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2650 /* ARGSUSED */
2651 Lisp_Object
2652 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2653 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2655 struct gcpro gcpro1;
2656 #ifdef NO_ARG_ARRAY
2657 Lisp_Object args[7];
2658 args[0] = fn;
2659 args[1] = arg1;
2660 args[2] = arg2;
2661 args[3] = arg3;
2662 args[4] = arg4;
2663 args[5] = arg5;
2664 args[6] = arg6;
2665 GCPRO1 (args[0]);
2666 gcpro1.nvars = 7;
2667 RETURN_UNGCPRO (Ffuncall (7, args));
2668 #else /* not NO_ARG_ARRAY */
2669 GCPRO1 (fn);
2670 gcpro1.nvars = 7;
2671 RETURN_UNGCPRO (Ffuncall (7, &fn));
2672 #endif /* not NO_ARG_ARRAY */
2675 /* The caller should GCPRO all the elements of ARGS. */
2677 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2678 doc: /* Call first argument as a function, passing remaining arguments to it.
2679 Return the value that function returns.
2680 Thus, (funcall 'cons 'x 'y) returns (x . y).
2681 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2682 (nargs, args)
2683 int nargs;
2684 Lisp_Object *args;
2686 Lisp_Object fun;
2687 Lisp_Object funcar;
2688 int numargs = nargs - 1;
2689 Lisp_Object lisp_numargs;
2690 Lisp_Object val;
2691 struct backtrace backtrace;
2692 register Lisp_Object *internal_args;
2693 register int i;
2695 QUIT;
2696 if (consing_since_gc > gc_cons_threshold)
2697 Fgarbage_collect ();
2699 if (++lisp_eval_depth > max_lisp_eval_depth)
2701 if (max_lisp_eval_depth < 100)
2702 max_lisp_eval_depth = 100;
2703 if (lisp_eval_depth > max_lisp_eval_depth)
2704 error ("Lisp nesting exceeds max-lisp-eval-depth");
2707 backtrace.next = backtrace_list;
2708 backtrace_list = &backtrace;
2709 backtrace.function = &args[0];
2710 backtrace.args = &args[1];
2711 backtrace.nargs = nargs - 1;
2712 backtrace.evalargs = 0;
2713 backtrace.debug_on_exit = 0;
2715 if (debug_on_next_call)
2716 do_debug_on_call (Qlambda);
2718 retry:
2720 fun = args[0];
2722 fun = Findirect_function (fun);
2724 if (SUBRP (fun))
2726 if (numargs < XSUBR (fun)->min_args
2727 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2729 XSETFASTINT (lisp_numargs, numargs);
2730 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2733 if (XSUBR (fun)->max_args == UNEVALLED)
2734 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2736 if (XSUBR (fun)->max_args == MANY)
2738 val = (*XSUBR (fun)->function) (numargs, args + 1);
2739 goto done;
2742 if (XSUBR (fun)->max_args > numargs)
2744 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2745 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2746 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2747 internal_args[i] = Qnil;
2749 else
2750 internal_args = args + 1;
2751 switch (XSUBR (fun)->max_args)
2753 case 0:
2754 val = (*XSUBR (fun)->function) ();
2755 goto done;
2756 case 1:
2757 val = (*XSUBR (fun)->function) (internal_args[0]);
2758 goto done;
2759 case 2:
2760 val = (*XSUBR (fun)->function) (internal_args[0],
2761 internal_args[1]);
2762 goto done;
2763 case 3:
2764 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2765 internal_args[2]);
2766 goto done;
2767 case 4:
2768 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2769 internal_args[2],
2770 internal_args[3]);
2771 goto done;
2772 case 5:
2773 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2774 internal_args[2], internal_args[3],
2775 internal_args[4]);
2776 goto done;
2777 case 6:
2778 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2779 internal_args[2], internal_args[3],
2780 internal_args[4], internal_args[5]);
2781 goto done;
2782 case 7:
2783 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2784 internal_args[2], internal_args[3],
2785 internal_args[4], internal_args[5],
2786 internal_args[6]);
2787 goto done;
2789 case 8:
2790 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2791 internal_args[2], internal_args[3],
2792 internal_args[4], internal_args[5],
2793 internal_args[6], internal_args[7]);
2794 goto done;
2796 default:
2798 /* If a subr takes more than 8 arguments without using MANY
2799 or UNEVALLED, we need to extend this function to support it.
2800 Until this is done, there is no way to call the function. */
2801 abort ();
2804 if (COMPILEDP (fun))
2805 val = funcall_lambda (fun, numargs, args + 1);
2806 else
2808 if (!CONSP (fun))
2809 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2810 funcar = Fcar (fun);
2811 if (!SYMBOLP (funcar))
2812 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2813 if (EQ (funcar, Qlambda))
2814 val = funcall_lambda (fun, numargs, args + 1);
2815 else if (EQ (funcar, Qautoload))
2817 do_autoload (fun, args[0]);
2818 goto retry;
2820 else
2821 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2823 done:
2824 lisp_eval_depth--;
2825 if (backtrace.debug_on_exit)
2826 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2827 backtrace_list = backtrace.next;
2828 return val;
2831 Lisp_Object
2832 apply_lambda (fun, args, eval_flag)
2833 Lisp_Object fun, args;
2834 int eval_flag;
2836 Lisp_Object args_left;
2837 Lisp_Object numargs;
2838 register Lisp_Object *arg_vector;
2839 struct gcpro gcpro1, gcpro2, gcpro3;
2840 register int i;
2841 register Lisp_Object tem;
2843 numargs = Flength (args);
2844 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2845 args_left = args;
2847 GCPRO3 (*arg_vector, args_left, fun);
2848 gcpro1.nvars = 0;
2850 for (i = 0; i < XINT (numargs);)
2852 tem = Fcar (args_left), args_left = Fcdr (args_left);
2853 if (eval_flag) tem = Feval (tem);
2854 arg_vector[i++] = tem;
2855 gcpro1.nvars = i;
2858 UNGCPRO;
2860 if (eval_flag)
2862 backtrace_list->args = arg_vector;
2863 backtrace_list->nargs = i;
2865 backtrace_list->evalargs = 0;
2866 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2868 /* Do the debug-on-exit now, while arg_vector still exists. */
2869 if (backtrace_list->debug_on_exit)
2870 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2871 /* Don't do it again when we return to eval. */
2872 backtrace_list->debug_on_exit = 0;
2873 return tem;
2876 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2877 and return the result of evaluation.
2878 FUN must be either a lambda-expression or a compiled-code object. */
2880 static Lisp_Object
2881 funcall_lambda (fun, nargs, arg_vector)
2882 Lisp_Object fun;
2883 int nargs;
2884 register Lisp_Object *arg_vector;
2886 Lisp_Object val, syms_left, next;
2887 int count = SPECPDL_INDEX ();
2888 int i, optional, rest;
2890 if (CONSP (fun))
2892 syms_left = XCDR (fun);
2893 if (CONSP (syms_left))
2894 syms_left = XCAR (syms_left);
2895 else
2896 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2898 else if (COMPILEDP (fun))
2899 syms_left = AREF (fun, COMPILED_ARGLIST);
2900 else
2901 abort ();
2903 i = optional = rest = 0;
2904 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2906 QUIT;
2908 next = XCAR (syms_left);
2909 while (!SYMBOLP (next))
2910 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2912 if (EQ (next, Qand_rest))
2913 rest = 1;
2914 else if (EQ (next, Qand_optional))
2915 optional = 1;
2916 else if (rest)
2918 specbind (next, Flist (nargs - i, &arg_vector[i]));
2919 i = nargs;
2921 else if (i < nargs)
2922 specbind (next, arg_vector[i++]);
2923 else if (!optional)
2924 return Fsignal (Qwrong_number_of_arguments,
2925 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2926 else
2927 specbind (next, Qnil);
2930 if (!NILP (syms_left))
2931 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2932 else if (i < nargs)
2933 return Fsignal (Qwrong_number_of_arguments,
2934 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2936 if (CONSP (fun))
2937 val = Fprogn (XCDR (XCDR (fun)));
2938 else
2940 /* If we have not actually read the bytecode string
2941 and constants vector yet, fetch them from the file. */
2942 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2943 Ffetch_bytecode (fun);
2944 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2945 AREF (fun, COMPILED_CONSTANTS),
2946 AREF (fun, COMPILED_STACK_DEPTH));
2949 return unbind_to (count, val);
2952 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2953 1, 1, 0,
2954 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2955 (object)
2956 Lisp_Object object;
2958 Lisp_Object tem;
2960 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2962 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2963 if (!CONSP (tem))
2965 tem = AREF (object, COMPILED_BYTECODE);
2966 if (CONSP (tem) && STRINGP (XCAR (tem)))
2967 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2968 else
2969 error ("Invalid byte code");
2971 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
2972 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
2974 return object;
2977 void
2978 grow_specpdl ()
2980 register int count = SPECPDL_INDEX ();
2981 if (specpdl_size >= max_specpdl_size)
2983 if (max_specpdl_size < 400)
2984 max_specpdl_size = 400;
2985 if (specpdl_size >= max_specpdl_size)
2987 if (!NILP (Vdebug_on_error))
2988 /* Leave room for some specpdl in the debugger. */
2989 max_specpdl_size = specpdl_size + 100;
2990 Fsignal (Qerror,
2991 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2994 specpdl_size *= 2;
2995 if (specpdl_size > max_specpdl_size)
2996 specpdl_size = max_specpdl_size;
2997 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2998 specpdl_ptr = specpdl + count;
3001 void
3002 specbind (symbol, value)
3003 Lisp_Object symbol, value;
3005 Lisp_Object ovalue;
3006 Lisp_Object valcontents;
3008 CHECK_SYMBOL (symbol);
3009 if (specpdl_ptr == specpdl + specpdl_size)
3010 grow_specpdl ();
3012 /* The most common case is that of a non-constant symbol with a
3013 trivial value. Make that as fast as we can. */
3014 valcontents = SYMBOL_VALUE (symbol);
3015 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
3017 specpdl_ptr->symbol = symbol;
3018 specpdl_ptr->old_value = valcontents;
3019 specpdl_ptr->func = NULL;
3020 ++specpdl_ptr;
3021 SET_SYMBOL_VALUE (symbol, value);
3023 else
3025 Lisp_Object valcontents;
3027 ovalue = find_symbol_value (symbol);
3028 specpdl_ptr->func = 0;
3029 specpdl_ptr->old_value = ovalue;
3031 valcontents = XSYMBOL (symbol)->value;
3033 if (BUFFER_LOCAL_VALUEP (valcontents)
3034 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
3035 || BUFFER_OBJFWDP (valcontents))
3037 Lisp_Object where, current_buffer;
3039 current_buffer = Fcurrent_buffer ();
3041 /* For a local variable, record both the symbol and which
3042 buffer's or frame's value we are saving. */
3043 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3044 where = current_buffer;
3045 else if (!BUFFER_OBJFWDP (valcontents)
3046 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3047 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3048 else
3049 where = Qnil;
3051 /* We're not using the `unused' slot in the specbinding
3052 structure because this would mean we have to do more
3053 work for simple variables. */
3054 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3056 /* If SYMBOL is a per-buffer variable which doesn't have a
3057 buffer-local value here, make the `let' change the global
3058 value by changing the value of SYMBOL in all buffers not
3059 having their own value. This is consistent with what
3060 happens with other buffer-local variables. */
3061 if (NILP (where)
3062 && BUFFER_OBJFWDP (valcontents))
3064 ++specpdl_ptr;
3065 Fset_default (symbol, value);
3066 return;
3069 else
3070 specpdl_ptr->symbol = symbol;
3072 specpdl_ptr++;
3073 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3074 store_symval_forwarding (symbol, ovalue, value, NULL);
3075 else
3076 set_internal (symbol, value, 0, 1);
3080 void
3081 record_unwind_protect (function, arg)
3082 Lisp_Object (*function) P_ ((Lisp_Object));
3083 Lisp_Object arg;
3085 if (specpdl_ptr == specpdl + specpdl_size)
3086 grow_specpdl ();
3087 specpdl_ptr->func = function;
3088 specpdl_ptr->symbol = Qnil;
3089 specpdl_ptr->old_value = arg;
3090 specpdl_ptr++;
3093 Lisp_Object
3094 unbind_to (count, value)
3095 int count;
3096 Lisp_Object value;
3098 int quitf = !NILP (Vquit_flag);
3099 struct gcpro gcpro1;
3101 GCPRO1 (value);
3102 Vquit_flag = Qnil;
3104 while (specpdl_ptr != specpdl + count)
3106 /* Copy the binding, and decrement specpdl_ptr, before we do
3107 the work to unbind it. We decrement first
3108 so that an error in unbinding won't try to unbind
3109 the same entry again, and we copy the binding first
3110 in case more bindings are made during some of the code we run. */
3112 struct specbinding this_binding;
3113 this_binding = *--specpdl_ptr;
3115 if (this_binding.func != 0)
3116 (*this_binding.func) (this_binding.old_value);
3117 /* If the symbol is a list, it is really (SYMBOL WHERE
3118 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3119 frame. If WHERE is a buffer or frame, this indicates we
3120 bound a variable that had a buffer-local or frame-local
3121 binding. WHERE nil means that the variable had the default
3122 value when it was bound. CURRENT-BUFFER is the buffer that
3123 was current when the variable was bound. */
3124 else if (CONSP (this_binding.symbol))
3126 Lisp_Object symbol, where;
3128 symbol = XCAR (this_binding.symbol);
3129 where = XCAR (XCDR (this_binding.symbol));
3131 if (NILP (where))
3132 Fset_default (symbol, this_binding.old_value);
3133 else if (BUFFERP (where))
3134 set_internal (symbol, this_binding.old_value, XBUFFER (where), 1);
3135 else
3136 set_internal (symbol, this_binding.old_value, NULL, 1);
3138 else
3140 /* If variable has a trivial value (no forwarding), we can
3141 just set it. No need to check for constant symbols here,
3142 since that was already done by specbind. */
3143 if (!MISCP (SYMBOL_VALUE (this_binding.symbol)))
3144 SET_SYMBOL_VALUE (this_binding.symbol, this_binding.old_value);
3145 else
3146 set_internal (this_binding.symbol, this_binding.old_value, 0, 1);
3150 if (NILP (Vquit_flag) && quitf)
3151 Vquit_flag = Qt;
3153 UNGCPRO;
3154 return value;
3157 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3158 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3159 The debugger is entered when that frame exits, if the flag is non-nil. */)
3160 (level, flag)
3161 Lisp_Object level, flag;
3163 register struct backtrace *backlist = backtrace_list;
3164 register int i;
3166 CHECK_NUMBER (level);
3168 for (i = 0; backlist && i < XINT (level); i++)
3170 backlist = backlist->next;
3173 if (backlist)
3174 backlist->debug_on_exit = !NILP (flag);
3176 return flag;
3179 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3180 doc: /* Print a trace of Lisp function calls currently active.
3181 Output stream used is value of `standard-output'. */)
3184 register struct backtrace *backlist = backtrace_list;
3185 register int i;
3186 Lisp_Object tail;
3187 Lisp_Object tem;
3188 extern Lisp_Object Vprint_level;
3189 struct gcpro gcpro1;
3191 XSETFASTINT (Vprint_level, 3);
3193 tail = Qnil;
3194 GCPRO1 (tail);
3196 while (backlist)
3198 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3199 if (backlist->nargs == UNEVALLED)
3201 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3202 write_string ("\n", -1);
3204 else
3206 tem = *backlist->function;
3207 Fprin1 (tem, Qnil); /* This can QUIT */
3208 write_string ("(", -1);
3209 if (backlist->nargs == MANY)
3211 for (tail = *backlist->args, i = 0;
3212 !NILP (tail);
3213 tail = Fcdr (tail), i++)
3215 if (i) write_string (" ", -1);
3216 Fprin1 (Fcar (tail), Qnil);
3219 else
3221 for (i = 0; i < backlist->nargs; i++)
3223 if (i) write_string (" ", -1);
3224 Fprin1 (backlist->args[i], Qnil);
3227 write_string (")\n", -1);
3229 backlist = backlist->next;
3232 Vprint_level = Qnil;
3233 UNGCPRO;
3234 return Qnil;
3237 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3238 doc: /* Return the function and arguments NFRAMES up from current execution point.
3239 If that frame has not evaluated the arguments yet (or is a special form),
3240 the value is (nil FUNCTION ARG-FORMS...).
3241 If that frame has evaluated its arguments and called its function already,
3242 the value is (t FUNCTION ARG-VALUES...).
3243 A &rest arg is represented as the tail of the list ARG-VALUES.
3244 FUNCTION is whatever was supplied as car of evaluated list,
3245 or a lambda expression for macro calls.
3246 If NFRAMES is more than the number of frames, the value is nil. */)
3247 (nframes)
3248 Lisp_Object nframes;
3250 register struct backtrace *backlist = backtrace_list;
3251 register int i;
3252 Lisp_Object tem;
3254 CHECK_NATNUM (nframes);
3256 /* Find the frame requested. */
3257 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3258 backlist = backlist->next;
3260 if (!backlist)
3261 return Qnil;
3262 if (backlist->nargs == UNEVALLED)
3263 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3264 else
3266 if (backlist->nargs == MANY)
3267 tem = *backlist->args;
3268 else
3269 tem = Flist (backlist->nargs, backlist->args);
3271 return Fcons (Qt, Fcons (*backlist->function, tem));
3276 void
3277 mark_backtrace ()
3279 register struct backtrace *backlist;
3280 register int i;
3282 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3284 mark_object (*backlist->function);
3286 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3287 i = 0;
3288 else
3289 i = backlist->nargs - 1;
3290 for (; i >= 0; i--)
3291 mark_object (backlist->args[i]);
3295 void
3296 syms_of_eval ()
3298 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3299 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3300 If Lisp code tries to make more than this many at once,
3301 an error is signaled.
3302 You can safely use a value considerably larger than the default value,
3303 if that proves inconveniently small. However, if you increase it too far,
3304 Emacs could run out of memory trying to make the stack bigger. */);
3306 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3307 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3309 This limit serves to catch infinite recursions for you before they cause
3310 actual stack overflow in C, which would be fatal for Emacs.
3311 You can safely make it considerably larger than its default value,
3312 if that proves inconveniently small. However, if you increase it too far,
3313 Emacs could overflow the real C stack, and crash. */);
3315 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3316 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3317 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'. */);
3318 Vquit_flag = Qnil;
3320 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3321 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3322 Note that `quit-flag' will still be set by typing C-g,
3323 so a quit will be signaled as soon as `inhibit-quit' is nil.
3324 To prevent this happening, set `quit-flag' to nil
3325 before making `inhibit-quit' nil. */);
3326 Vinhibit_quit = Qnil;
3328 Qinhibit_quit = intern ("inhibit-quit");
3329 staticpro (&Qinhibit_quit);
3331 Qautoload = intern ("autoload");
3332 staticpro (&Qautoload);
3334 Qdebug_on_error = intern ("debug-on-error");
3335 staticpro (&Qdebug_on_error);
3337 Qmacro = intern ("macro");
3338 staticpro (&Qmacro);
3340 Qdeclare = intern ("declare");
3341 staticpro (&Qdeclare);
3343 /* Note that the process handling also uses Qexit, but we don't want
3344 to staticpro it twice, so we just do it here. */
3345 Qexit = intern ("exit");
3346 staticpro (&Qexit);
3348 Qinteractive = intern ("interactive");
3349 staticpro (&Qinteractive);
3351 Qcommandp = intern ("commandp");
3352 staticpro (&Qcommandp);
3354 Qdefun = intern ("defun");
3355 staticpro (&Qdefun);
3357 Qdefvar = intern ("defvar");
3358 staticpro (&Qdefvar);
3360 Qand_rest = intern ("&rest");
3361 staticpro (&Qand_rest);
3363 Qand_optional = intern ("&optional");
3364 staticpro (&Qand_optional);
3366 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3367 doc: /* *Non-nil means errors display a backtrace buffer.
3368 More precisely, this happens for any error that is handled
3369 by the editor command loop.
3370 If the value is a list, an error only means to display a backtrace
3371 if one of its condition symbols appears in the list. */);
3372 Vstack_trace_on_error = Qnil;
3374 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3375 doc: /* *Non-nil means enter debugger if an error is signaled.
3376 Does not apply to errors handled by `condition-case' or those
3377 matched by `debug-ignored-errors'.
3378 If the value is a list, an error only means to enter the debugger
3379 if one of its condition symbols appears in the list.
3380 When you evaluate an expression interactively, this variable
3381 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3382 See also variable `debug-on-quit'. */);
3383 Vdebug_on_error = Qnil;
3385 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3386 doc: /* *List of errors for which the debugger should not be called.
3387 Each element may be a condition-name or a regexp that matches error messages.
3388 If any element applies to a given error, that error skips the debugger
3389 and just returns to top level.
3390 This overrides the variable `debug-on-error'.
3391 It does not apply to errors handled by `condition-case'. */);
3392 Vdebug_ignored_errors = Qnil;
3394 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3395 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3396 Does not apply if quit is handled by a `condition-case'.
3397 When you evaluate an expression interactively, this variable
3398 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3399 debug_on_quit = 0;
3401 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3402 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3404 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3405 doc: /* Non-nil means debugger may continue execution.
3406 This is nil when the debugger is called under circumstances where it
3407 might not be safe to continue. */);
3408 debugger_may_continue = 1;
3410 DEFVAR_LISP ("debugger", &Vdebugger,
3411 doc: /* Function to call to invoke debugger.
3412 If due to frame exit, args are `exit' and the value being returned;
3413 this function's value will be returned instead of that.
3414 If due to error, args are `error' and a list of the args to `signal'.
3415 If due to `apply' or `funcall' entry, one arg, `lambda'.
3416 If due to `eval' entry, one arg, t. */);
3417 Vdebugger = Qnil;
3419 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3420 doc: /* If non-nil, this is a function for `signal' to call.
3421 It receives the same arguments that `signal' was given.
3422 The Edebug package uses this to regain control. */);
3423 Vsignal_hook_function = Qnil;
3425 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3426 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3427 Note that `debug-on-error', `debug-on-quit' and friends
3428 still determine whether to handle the particular condition. */);
3429 Vdebug_on_signal = Qnil;
3431 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3432 doc: /* Function to process declarations in a macro definition.
3433 The function will be called with two args MACRO and DECL.
3434 MACRO is the name of the macro being defined.
3435 DECL is a list `(declare ...)' containing the declarations.
3436 The value the function returns is not used. */);
3437 Vmacro_declaration_function = Qnil;
3439 Vrun_hooks = intern ("run-hooks");
3440 staticpro (&Vrun_hooks);
3442 staticpro (&Vautoload_queue);
3443 Vautoload_queue = Qnil;
3444 staticpro (&Vsignaling_function);
3445 Vsignaling_function = Qnil;
3447 defsubr (&Sor);
3448 defsubr (&Sand);
3449 defsubr (&Sif);
3450 defsubr (&Scond);
3451 defsubr (&Sprogn);
3452 defsubr (&Sprog1);
3453 defsubr (&Sprog2);
3454 defsubr (&Ssetq);
3455 defsubr (&Squote);
3456 defsubr (&Sfunction);
3457 defsubr (&Sdefun);
3458 defsubr (&Sdefmacro);
3459 defsubr (&Sdefvar);
3460 defsubr (&Sdefvaralias);
3461 defsubr (&Sdefconst);
3462 defsubr (&Suser_variable_p);
3463 defsubr (&Slet);
3464 defsubr (&SletX);
3465 defsubr (&Swhile);
3466 defsubr (&Smacroexpand);
3467 defsubr (&Scatch);
3468 defsubr (&Sthrow);
3469 defsubr (&Sunwind_protect);
3470 defsubr (&Scondition_case);
3471 defsubr (&Ssignal);
3472 defsubr (&Sinteractive_p);
3473 defsubr (&Scommandp);
3474 defsubr (&Sautoload);
3475 defsubr (&Seval);
3476 defsubr (&Sapply);
3477 defsubr (&Sfuncall);
3478 defsubr (&Srun_hooks);
3479 defsubr (&Srun_hook_with_args);
3480 defsubr (&Srun_hook_with_args_until_success);
3481 defsubr (&Srun_hook_with_args_until_failure);
3482 defsubr (&Sfetch_bytecode);
3483 defsubr (&Sbacktrace_debug);
3484 defsubr (&Sbacktrace);
3485 defsubr (&Sbacktrace_frame);
3488 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
3489 (do not change this comment) */