(function*, case, ecase, typecase, etypecase, progv, lexical-let, lexical-let*,
[emacs.git] / src / eval.c
blob7f043daa5551150b7ee37b7a069ccc3862adf466
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2004, 2005 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;
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 = 1000;
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", Fcalled_interactively_p, Scalled_interactively_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_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 || XSUBR (fun) == &Scalled_interactively_p))
600 btp = btp->next;
602 /* If we're running an Emacs 18-style byte-compiled function, there
603 may be a frame for Fbytecode at the top level. In any version of
604 Emacs there can be Fbytecode frames for subexpressions evaluated
605 inside catch and condition-case. Skip past them.
607 If this isn't a byte-compiled function, then we may now be
608 looking at several frames for special forms. Skip past them. */
609 while (btp
610 && (EQ (*btp->function, Qbytecode)
611 || btp->nargs == UNEVALLED))
612 btp = btp->next;
614 /* btp now points at the frame of the innermost function that isn't
615 a special form, ignoring frames for Finteractive_p and/or
616 Fbytecode at the top. If this frame is for a built-in function
617 (such as load or eval-region) return nil. */
618 fun = Findirect_function (*btp->function);
619 if (exclude_subrs_p && SUBRP (fun))
620 return 0;
622 /* btp points to the frame of a Lisp function that called interactive-p.
623 Return t if that function was called interactively. */
624 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
625 return 1;
626 return 0;
630 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
631 doc: /* Define NAME as a function.
632 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
633 See also the function `interactive'.
634 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
635 (args)
636 Lisp_Object args;
638 register Lisp_Object fn_name;
639 register Lisp_Object defn;
641 fn_name = Fcar (args);
642 CHECK_SYMBOL (fn_name);
643 defn = Fcons (Qlambda, Fcdr (args));
644 if (!NILP (Vpurify_flag))
645 defn = Fpurecopy (defn);
646 if (CONSP (XSYMBOL (fn_name)->function)
647 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
648 LOADHIST_ATTACH (Fcons (Qt, fn_name));
649 Ffset (fn_name, defn);
650 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
651 return fn_name;
654 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
655 doc: /* Define NAME as a macro.
656 The actual definition looks like
657 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
658 When the macro is called, as in (NAME ARGS...),
659 the function (lambda ARGLIST BODY...) is applied to
660 the list ARGS... as it appears in the expression,
661 and the result should be a form to be evaluated instead of the original.
663 DECL is a declaration, optional, which can specify how to indent
664 calls to this macro and how Edebug should handle it. It looks like this:
665 (declare SPECS...)
666 The elements can look like this:
667 (indent INDENT)
668 Set NAME's `lisp-indent-function' property to INDENT.
670 (debug DEBUG)
671 Set NAME's `edebug-form-spec' property to DEBUG. (This is
672 equivalent to writing a `def-edebug-spec' for the macro.)
673 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
674 (args)
675 Lisp_Object args;
677 register Lisp_Object fn_name;
678 register Lisp_Object defn;
679 Lisp_Object lambda_list, doc, tail;
681 fn_name = Fcar (args);
682 CHECK_SYMBOL (fn_name);
683 lambda_list = Fcar (Fcdr (args));
684 tail = Fcdr (Fcdr (args));
686 doc = Qnil;
687 if (STRINGP (Fcar (tail)))
689 doc = XCAR (tail);
690 tail = XCDR (tail);
693 while (CONSP (Fcar (tail))
694 && EQ (Fcar (Fcar (tail)), Qdeclare))
696 if (!NILP (Vmacro_declaration_function))
698 struct gcpro gcpro1;
699 GCPRO1 (args);
700 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
701 UNGCPRO;
704 tail = Fcdr (tail);
707 if (NILP (doc))
708 tail = Fcons (lambda_list, tail);
709 else
710 tail = Fcons (lambda_list, Fcons (doc, tail));
711 defn = Fcons (Qmacro, Fcons (Qlambda, tail));
713 if (!NILP (Vpurify_flag))
714 defn = Fpurecopy (defn);
715 if (CONSP (XSYMBOL (fn_name)->function)
716 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
717 LOADHIST_ATTACH (Fcons (Qt, fn_name));
718 Ffset (fn_name, defn);
719 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
720 return fn_name;
724 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
725 doc: /* Make SYMBOL a variable alias for symbol ALIASED.
726 Setting the value of SYMBOL will subsequently set the value of ALIASED,
727 and getting the value of SYMBOL will return the value ALIASED has.
728 Third arg DOCSTRING, if non-nil, is documentation for SYMBOL. If it is
729 omitted or nil, SYMBOL gets the documentation string of ALIASED, or of the
730 variable at the end of the chain of aliases, if ALIASED is itself an alias.
731 The return value is ALIASED. */)
732 (symbol, aliased, docstring)
733 Lisp_Object symbol, aliased, docstring;
735 struct Lisp_Symbol *sym;
737 CHECK_SYMBOL (symbol);
738 CHECK_SYMBOL (aliased);
740 if (SYMBOL_CONSTANT_P (symbol))
741 error ("Cannot make a constant an alias");
743 sym = XSYMBOL (symbol);
744 sym->indirect_variable = 1;
745 sym->value = aliased;
746 sym->constant = SYMBOL_CONSTANT_P (aliased);
747 LOADHIST_ATTACH (symbol);
748 if (!NILP (docstring))
749 Fput (symbol, Qvariable_documentation, docstring);
750 else
751 Fput (symbol, Qvariable_documentation, Qnil);
753 return aliased;
757 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
758 doc: /* Define SYMBOL as a variable.
759 You are not required to define a variable in order to use it,
760 but the definition can supply documentation and an initial value
761 in a way that tags can recognize.
763 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
764 If SYMBOL is buffer-local, its default value is what is set;
765 buffer-local values are not affected.
766 INITVALUE and DOCSTRING are optional.
767 If DOCSTRING starts with *, this variable is identified as a user option.
768 This means that M-x set-variable recognizes it.
769 See also `user-variable-p'.
770 If INITVALUE is missing, SYMBOL's value is not set.
772 If SYMBOL has a local binding, then this form affects the local
773 binding. This is usually not what you want. Thus, if you need to
774 load a file defining variables, with this form or with `defconst' or
775 `defcustom', you should always load that file _outside_ any bindings
776 for these variables. \(`defconst' and `defcustom' behave similarly in
777 this respect.)
778 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
779 (args)
780 Lisp_Object args;
782 register Lisp_Object sym, tem, tail;
784 sym = Fcar (args);
785 tail = Fcdr (args);
786 if (!NILP (Fcdr (Fcdr (tail))))
787 error ("too many arguments");
789 tem = Fdefault_boundp (sym);
790 if (!NILP (tail))
792 if (NILP (tem))
793 Fset_default (sym, Feval (Fcar (tail)));
794 else
795 { /* Check if there is really a global binding rather than just a let
796 binding that shadows the global unboundness of the var. */
797 volatile struct specbinding *pdl = specpdl_ptr;
798 while (--pdl >= specpdl)
800 if (EQ (pdl->symbol, sym) && !pdl->func
801 && EQ (pdl->old_value, Qunbound))
803 message_with_string ("Warning: defvar ignored because %s is let-bound",
804 SYMBOL_NAME (sym), 1);
805 break;
809 tail = Fcdr (tail);
810 tem = Fcar (tail);
811 if (!NILP (tem))
813 if (!NILP (Vpurify_flag))
814 tem = Fpurecopy (tem);
815 Fput (sym, Qvariable_documentation, tem);
817 LOADHIST_ATTACH (sym);
819 else
820 /* Simple (defvar <var>) should not count as a definition at all.
821 It could get in the way of other definitions, and unloading this
822 package could try to make the variable unbound. */
825 return sym;
828 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
829 doc: /* Define SYMBOL as a constant variable.
830 The intent is that neither programs nor users should ever change this value.
831 Always sets the value of SYMBOL to the result of evalling INITVALUE.
832 If SYMBOL is buffer-local, its default value is what is set;
833 buffer-local values are not affected.
834 DOCSTRING is optional.
836 If SYMBOL has a local binding, then this form sets the local binding's
837 value. However, you should normally not make local bindings for
838 variables defined with this form.
839 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
840 (args)
841 Lisp_Object args;
843 register Lisp_Object sym, tem;
845 sym = Fcar (args);
846 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
847 error ("too many arguments");
849 tem = Feval (Fcar (Fcdr (args)));
850 if (!NILP (Vpurify_flag))
851 tem = Fpurecopy (tem);
852 Fset_default (sym, tem);
853 tem = Fcar (Fcdr (Fcdr (args)));
854 if (!NILP (tem))
856 if (!NILP (Vpurify_flag))
857 tem = Fpurecopy (tem);
858 Fput (sym, Qvariable_documentation, tem);
860 LOADHIST_ATTACH (sym);
861 return sym;
864 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
865 doc: /* Returns t if VARIABLE is intended to be set and modified by users.
866 \(The alternative is a variable used internally in a Lisp program.)
867 Determined by whether the first character of the documentation
868 for the variable is `*' or if the variable is customizable (has a non-nil
869 value of `standard-value' or of `custom-autoload' on its property list). */)
870 (variable)
871 Lisp_Object variable;
873 Lisp_Object documentation;
875 if (!SYMBOLP (variable))
876 return Qnil;
878 documentation = Fget (variable, Qvariable_documentation);
879 if (INTEGERP (documentation) && XINT (documentation) < 0)
880 return Qt;
881 if (STRINGP (documentation)
882 && ((unsigned char) SREF (documentation, 0) == '*'))
883 return Qt;
884 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
885 if (CONSP (documentation)
886 && STRINGP (XCAR (documentation))
887 && INTEGERP (XCDR (documentation))
888 && XINT (XCDR (documentation)) < 0)
889 return Qt;
890 /* Customizable? See `custom-variable-p'. */
891 if ((!NILP (Fget (variable, intern ("standard-value"))))
892 || (!NILP (Fget (variable, intern ("custom-autoload")))))
893 return Qt;
894 return Qnil;
897 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
898 doc: /* Bind variables according to VARLIST then eval BODY.
899 The value of the last form in BODY is returned.
900 Each element of VARLIST is a symbol (which is bound to nil)
901 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
902 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
903 usage: (let* VARLIST BODY...) */)
904 (args)
905 Lisp_Object args;
907 Lisp_Object varlist, val, elt;
908 int count = SPECPDL_INDEX ();
909 struct gcpro gcpro1, gcpro2, gcpro3;
911 GCPRO3 (args, elt, varlist);
913 varlist = Fcar (args);
914 while (!NILP (varlist))
916 QUIT;
917 elt = Fcar (varlist);
918 if (SYMBOLP (elt))
919 specbind (elt, Qnil);
920 else if (! NILP (Fcdr (Fcdr (elt))))
921 Fsignal (Qerror,
922 Fcons (build_string ("`let' bindings can have only one value-form"),
923 elt));
924 else
926 val = Feval (Fcar (Fcdr (elt)));
927 specbind (Fcar (elt), val);
929 varlist = Fcdr (varlist);
931 UNGCPRO;
932 val = Fprogn (Fcdr (args));
933 return unbind_to (count, val);
936 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
937 doc: /* Bind variables according to VARLIST then eval BODY.
938 The value of the last form in BODY is returned.
939 Each element of VARLIST is a symbol (which is bound to nil)
940 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
941 All the VALUEFORMs are evalled before any symbols are bound.
942 usage: (let VARLIST BODY...) */)
943 (args)
944 Lisp_Object args;
946 Lisp_Object *temps, tem;
947 register Lisp_Object elt, varlist;
948 int count = SPECPDL_INDEX ();
949 register int argnum;
950 struct gcpro gcpro1, gcpro2;
952 varlist = Fcar (args);
954 /* Make space to hold the values to give the bound variables */
955 elt = Flength (varlist);
956 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
958 /* Compute the values and store them in `temps' */
960 GCPRO2 (args, *temps);
961 gcpro2.nvars = 0;
963 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
965 QUIT;
966 elt = Fcar (varlist);
967 if (SYMBOLP (elt))
968 temps [argnum++] = Qnil;
969 else if (! NILP (Fcdr (Fcdr (elt))))
970 Fsignal (Qerror,
971 Fcons (build_string ("`let' bindings can have only one value-form"),
972 elt));
973 else
974 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
975 gcpro2.nvars = argnum;
977 UNGCPRO;
979 varlist = Fcar (args);
980 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
982 elt = Fcar (varlist);
983 tem = temps[argnum++];
984 if (SYMBOLP (elt))
985 specbind (elt, tem);
986 else
987 specbind (Fcar (elt), tem);
990 elt = Fprogn (Fcdr (args));
991 return unbind_to (count, elt);
994 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
995 doc: /* If TEST yields non-nil, eval BODY... and repeat.
996 The order of execution is thus TEST, BODY, TEST, BODY and so on
997 until TEST returns nil.
998 usage: (while TEST BODY...) */)
999 (args)
1000 Lisp_Object args;
1002 Lisp_Object test, body;
1003 struct gcpro gcpro1, gcpro2;
1005 GCPRO2 (test, body);
1007 test = Fcar (args);
1008 body = Fcdr (args);
1009 while (!NILP (Feval (test)))
1011 QUIT;
1012 Fprogn (body);
1015 UNGCPRO;
1016 return Qnil;
1019 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1020 doc: /* Return result of expanding macros at top level of FORM.
1021 If FORM is not a macro call, it is returned unchanged.
1022 Otherwise, the macro is expanded and the expansion is considered
1023 in place of FORM. When a non-macro-call results, it is returned.
1025 The second optional arg ENVIRONMENT specifies an environment of macro
1026 definitions to shadow the loaded ones for use in file byte-compilation. */)
1027 (form, environment)
1028 Lisp_Object form;
1029 Lisp_Object environment;
1031 /* With cleanups from Hallvard Furuseth. */
1032 register Lisp_Object expander, sym, def, tem;
1034 while (1)
1036 /* Come back here each time we expand a macro call,
1037 in case it expands into another macro call. */
1038 if (!CONSP (form))
1039 break;
1040 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1041 def = sym = XCAR (form);
1042 tem = Qnil;
1043 /* Trace symbols aliases to other symbols
1044 until we get a symbol that is not an alias. */
1045 while (SYMBOLP (def))
1047 QUIT;
1048 sym = def;
1049 tem = Fassq (sym, environment);
1050 if (NILP (tem))
1052 def = XSYMBOL (sym)->function;
1053 if (!EQ (def, Qunbound))
1054 continue;
1056 break;
1058 /* Right now TEM is the result from SYM in ENVIRONMENT,
1059 and if TEM is nil then DEF is SYM's function definition. */
1060 if (NILP (tem))
1062 /* SYM is not mentioned in ENVIRONMENT.
1063 Look at its function definition. */
1064 if (EQ (def, Qunbound) || !CONSP (def))
1065 /* Not defined or definition not suitable */
1066 break;
1067 if (EQ (XCAR (def), Qautoload))
1069 /* Autoloading function: will it be a macro when loaded? */
1070 tem = Fnth (make_number (4), def);
1071 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1072 /* Yes, load it and try again. */
1074 struct gcpro gcpro1;
1075 GCPRO1 (form);
1076 do_autoload (def, sym);
1077 UNGCPRO;
1078 continue;
1080 else
1081 break;
1083 else if (!EQ (XCAR (def), Qmacro))
1084 break;
1085 else expander = XCDR (def);
1087 else
1089 expander = XCDR (tem);
1090 if (NILP (expander))
1091 break;
1093 form = apply1 (expander, XCDR (form));
1095 return form;
1098 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1099 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1100 TAG is evalled to get the tag to use; it must not be nil.
1102 Then the BODY is executed.
1103 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1104 If no throw happens, `catch' returns the value of the last BODY form.
1105 If a throw happens, it specifies the value to return from `catch'.
1106 usage: (catch TAG BODY...) */)
1107 (args)
1108 Lisp_Object args;
1110 register Lisp_Object tag;
1111 struct gcpro gcpro1;
1113 GCPRO1 (args);
1114 tag = Feval (Fcar (args));
1115 UNGCPRO;
1116 return internal_catch (tag, Fprogn, Fcdr (args));
1119 /* Set up a catch, then call C function FUNC on argument ARG.
1120 FUNC should return a Lisp_Object.
1121 This is how catches are done from within C code. */
1123 Lisp_Object
1124 internal_catch (tag, func, arg)
1125 Lisp_Object tag;
1126 Lisp_Object (*func) ();
1127 Lisp_Object arg;
1129 /* This structure is made part of the chain `catchlist'. */
1130 struct catchtag c;
1132 /* Fill in the components of c, and put it on the list. */
1133 c.next = catchlist;
1134 c.tag = tag;
1135 c.val = Qnil;
1136 c.backlist = backtrace_list;
1137 c.handlerlist = handlerlist;
1138 c.lisp_eval_depth = lisp_eval_depth;
1139 c.pdlcount = SPECPDL_INDEX ();
1140 c.poll_suppress_count = poll_suppress_count;
1141 c.interrupt_input_blocked = interrupt_input_blocked;
1142 c.gcpro = gcprolist;
1143 c.byte_stack = byte_stack_list;
1144 catchlist = &c;
1146 /* Call FUNC. */
1147 if (! _setjmp (c.jmp))
1148 c.val = (*func) (arg);
1150 /* Throw works by a longjmp that comes right here. */
1151 catchlist = c.next;
1152 return c.val;
1155 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1156 jump to that CATCH, returning VALUE as the value of that catch.
1158 This is the guts Fthrow and Fsignal; they differ only in the way
1159 they choose the catch tag to throw to. A catch tag for a
1160 condition-case form has a TAG of Qnil.
1162 Before each catch is discarded, unbind all special bindings and
1163 execute all unwind-protect clauses made above that catch. Unwind
1164 the handler stack as we go, so that the proper handlers are in
1165 effect for each unwind-protect clause we run. At the end, restore
1166 some static info saved in CATCH, and longjmp to the location
1167 specified in the
1169 This is used for correct unwinding in Fthrow and Fsignal. */
1171 static void
1172 unwind_to_catch (catch, value)
1173 struct catchtag *catch;
1174 Lisp_Object value;
1176 register int last_time;
1178 /* Save the value in the tag. */
1179 catch->val = value;
1181 /* Restore certain special C variables. */
1182 set_poll_suppress_count (catch->poll_suppress_count);
1183 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1184 handling_signal = 0;
1185 immediate_quit = 0;
1189 last_time = catchlist == catch;
1191 /* Unwind the specpdl stack, and then restore the proper set of
1192 handlers. */
1193 unbind_to (catchlist->pdlcount, Qnil);
1194 handlerlist = catchlist->handlerlist;
1195 catchlist = catchlist->next;
1197 while (! last_time);
1199 byte_stack_list = catch->byte_stack;
1200 gcprolist = catch->gcpro;
1201 #ifdef DEBUG_GCPRO
1202 if (gcprolist != 0)
1203 gcpro_level = gcprolist->level + 1;
1204 else
1205 gcpro_level = 0;
1206 #endif
1207 backtrace_list = catch->backlist;
1208 lisp_eval_depth = catch->lisp_eval_depth;
1210 _longjmp (catch->jmp, 1);
1213 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1214 doc: /* Throw to the catch for TAG and return VALUE from it.
1215 Both TAG and VALUE are evalled. */)
1216 (tag, value)
1217 register Lisp_Object tag, value;
1219 register struct catchtag *c;
1221 while (1)
1223 if (!NILP (tag))
1224 for (c = catchlist; c; c = c->next)
1226 if (EQ (c->tag, tag))
1227 unwind_to_catch (c, value);
1229 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1234 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1235 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1236 If BODYFORM completes normally, its value is returned
1237 after executing the UNWINDFORMS.
1238 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1239 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1240 (args)
1241 Lisp_Object args;
1243 Lisp_Object val;
1244 int count = SPECPDL_INDEX ();
1246 record_unwind_protect (Fprogn, Fcdr (args));
1247 val = Feval (Fcar (args));
1248 return unbind_to (count, val);
1251 /* Chain of condition handlers currently in effect.
1252 The elements of this chain are contained in the stack frames
1253 of Fcondition_case and internal_condition_case.
1254 When an error is signaled (by calling Fsignal, below),
1255 this chain is searched for an element that applies. */
1257 struct handler *handlerlist;
1259 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1260 doc: /* Regain control when an error is signaled.
1261 Executes BODYFORM and returns its value if no error happens.
1262 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1263 where the BODY is made of Lisp expressions.
1265 A handler is applicable to an error
1266 if CONDITION-NAME is one of the error's condition names.
1267 If an error happens, the first applicable handler is run.
1269 The car of a handler may be a list of condition names
1270 instead of a single condition name.
1272 When a handler handles an error,
1273 control returns to the condition-case and the handler BODY... is executed
1274 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1275 VAR may be nil; then you do not get access to the signal information.
1277 The value of the last BODY form is returned from the condition-case.
1278 See also the function `signal' for more info.
1279 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1280 (args)
1281 Lisp_Object args;
1283 Lisp_Object val;
1284 struct catchtag c;
1285 struct handler h;
1286 register Lisp_Object bodyform, handlers;
1287 volatile Lisp_Object var;
1289 var = Fcar (args);
1290 bodyform = Fcar (Fcdr (args));
1291 handlers = Fcdr (Fcdr (args));
1292 CHECK_SYMBOL (var);
1294 for (val = handlers; CONSP (val); val = XCDR (val))
1296 Lisp_Object tem;
1297 tem = XCAR (val);
1298 if (! (NILP (tem)
1299 || (CONSP (tem)
1300 && (SYMBOLP (XCAR (tem))
1301 || CONSP (XCAR (tem))))))
1302 error ("Invalid condition handler", tem);
1305 c.tag = Qnil;
1306 c.val = Qnil;
1307 c.backlist = backtrace_list;
1308 c.handlerlist = handlerlist;
1309 c.lisp_eval_depth = lisp_eval_depth;
1310 c.pdlcount = SPECPDL_INDEX ();
1311 c.poll_suppress_count = poll_suppress_count;
1312 c.interrupt_input_blocked = interrupt_input_blocked;
1313 c.gcpro = gcprolist;
1314 c.byte_stack = byte_stack_list;
1315 if (_setjmp (c.jmp))
1317 if (!NILP (h.var))
1318 specbind (h.var, c.val);
1319 val = Fprogn (Fcdr (h.chosen_clause));
1321 /* Note that this just undoes the binding of h.var; whoever
1322 longjumped to us unwound the stack to c.pdlcount before
1323 throwing. */
1324 unbind_to (c.pdlcount, Qnil);
1325 return val;
1327 c.next = catchlist;
1328 catchlist = &c;
1330 h.var = var;
1331 h.handler = handlers;
1332 h.next = handlerlist;
1333 h.tag = &c;
1334 handlerlist = &h;
1336 val = Feval (bodyform);
1337 catchlist = c.next;
1338 handlerlist = h.next;
1339 return val;
1342 /* Call the function BFUN with no arguments, catching errors within it
1343 according to HANDLERS. If there is an error, call HFUN with
1344 one argument which is the data that describes the error:
1345 (SIGNALNAME . DATA)
1347 HANDLERS can be a list of conditions to catch.
1348 If HANDLERS is Qt, catch all errors.
1349 If HANDLERS is Qerror, catch all errors
1350 but allow the debugger to run if that is enabled. */
1352 Lisp_Object
1353 internal_condition_case (bfun, handlers, hfun)
1354 Lisp_Object (*bfun) ();
1355 Lisp_Object handlers;
1356 Lisp_Object (*hfun) ();
1358 Lisp_Object val;
1359 struct catchtag c;
1360 struct handler h;
1362 #if 0 /* We now handle interrupt_input_blocked properly.
1363 What we still do not handle is exiting a signal handler. */
1364 abort ();
1365 #endif
1367 c.tag = Qnil;
1368 c.val = Qnil;
1369 c.backlist = backtrace_list;
1370 c.handlerlist = handlerlist;
1371 c.lisp_eval_depth = lisp_eval_depth;
1372 c.pdlcount = SPECPDL_INDEX ();
1373 c.poll_suppress_count = poll_suppress_count;
1374 c.interrupt_input_blocked = interrupt_input_blocked;
1375 c.gcpro = gcprolist;
1376 c.byte_stack = byte_stack_list;
1377 if (_setjmp (c.jmp))
1379 return (*hfun) (c.val);
1381 c.next = catchlist;
1382 catchlist = &c;
1383 h.handler = handlers;
1384 h.var = Qnil;
1385 h.next = handlerlist;
1386 h.tag = &c;
1387 handlerlist = &h;
1389 val = (*bfun) ();
1390 catchlist = c.next;
1391 handlerlist = h.next;
1392 return val;
1395 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1397 Lisp_Object
1398 internal_condition_case_1 (bfun, arg, handlers, hfun)
1399 Lisp_Object (*bfun) ();
1400 Lisp_Object arg;
1401 Lisp_Object handlers;
1402 Lisp_Object (*hfun) ();
1404 Lisp_Object val;
1405 struct catchtag c;
1406 struct handler h;
1408 c.tag = Qnil;
1409 c.val = Qnil;
1410 c.backlist = backtrace_list;
1411 c.handlerlist = handlerlist;
1412 c.lisp_eval_depth = lisp_eval_depth;
1413 c.pdlcount = SPECPDL_INDEX ();
1414 c.poll_suppress_count = poll_suppress_count;
1415 c.interrupt_input_blocked = interrupt_input_blocked;
1416 c.gcpro = gcprolist;
1417 c.byte_stack = byte_stack_list;
1418 if (_setjmp (c.jmp))
1420 return (*hfun) (c.val);
1422 c.next = catchlist;
1423 catchlist = &c;
1424 h.handler = handlers;
1425 h.var = Qnil;
1426 h.next = handlerlist;
1427 h.tag = &c;
1428 handlerlist = &h;
1430 val = (*bfun) (arg);
1431 catchlist = c.next;
1432 handlerlist = h.next;
1433 return val;
1437 /* Like internal_condition_case but call BFUN with NARGS as first,
1438 and ARGS as second argument. */
1440 Lisp_Object
1441 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1442 Lisp_Object (*bfun) ();
1443 int nargs;
1444 Lisp_Object *args;
1445 Lisp_Object handlers;
1446 Lisp_Object (*hfun) ();
1448 Lisp_Object val;
1449 struct catchtag c;
1450 struct handler h;
1452 c.tag = Qnil;
1453 c.val = Qnil;
1454 c.backlist = backtrace_list;
1455 c.handlerlist = handlerlist;
1456 c.lisp_eval_depth = lisp_eval_depth;
1457 c.pdlcount = SPECPDL_INDEX ();
1458 c.poll_suppress_count = poll_suppress_count;
1459 c.interrupt_input_blocked = interrupt_input_blocked;
1460 c.gcpro = gcprolist;
1461 c.byte_stack = byte_stack_list;
1462 if (_setjmp (c.jmp))
1464 return (*hfun) (c.val);
1466 c.next = catchlist;
1467 catchlist = &c;
1468 h.handler = handlers;
1469 h.var = Qnil;
1470 h.next = handlerlist;
1471 h.tag = &c;
1472 handlerlist = &h;
1474 val = (*bfun) (nargs, args);
1475 catchlist = c.next;
1476 handlerlist = h.next;
1477 return val;
1481 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1482 Lisp_Object, Lisp_Object,
1483 Lisp_Object *));
1485 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1486 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1487 This function does not return.
1489 An error symbol is a symbol with an `error-conditions' property
1490 that is a list of condition names.
1491 A handler for any of those names will get to handle this signal.
1492 The symbol `error' should normally be one of them.
1494 DATA should be a list. Its elements are printed as part of the error message.
1495 See Info anchor `(elisp)Definition of signal' for some details on how this
1496 error message is constructed.
1497 If the signal is handled, DATA is made available to the handler.
1498 See also the function `condition-case'. */)
1499 (error_symbol, data)
1500 Lisp_Object error_symbol, data;
1502 /* When memory is full, ERROR-SYMBOL is nil,
1503 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1504 That is a special case--don't do this in other situations. */
1505 register struct handler *allhandlers = handlerlist;
1506 Lisp_Object conditions;
1507 extern int gc_in_progress;
1508 extern int waiting_for_input;
1509 Lisp_Object debugger_value;
1510 Lisp_Object string;
1511 Lisp_Object real_error_symbol;
1512 struct backtrace *bp;
1514 immediate_quit = handling_signal = 0;
1515 abort_on_gc = 0;
1516 if (gc_in_progress || waiting_for_input)
1517 abort ();
1519 if (NILP (error_symbol))
1520 real_error_symbol = Fcar (data);
1521 else
1522 real_error_symbol = error_symbol;
1524 #if 0 /* rms: I don't know why this was here,
1525 but it is surely wrong for an error that is handled. */
1526 #ifdef HAVE_X_WINDOWS
1527 if (display_hourglass_p)
1528 cancel_hourglass ();
1529 #endif
1530 #endif
1532 /* This hook is used by edebug. */
1533 if (! NILP (Vsignal_hook_function)
1534 && ! NILP (error_symbol))
1535 call2 (Vsignal_hook_function, error_symbol, data);
1537 conditions = Fget (real_error_symbol, Qerror_conditions);
1539 /* Remember from where signal was called. Skip over the frame for
1540 `signal' itself. If a frame for `error' follows, skip that,
1541 too. Don't do this when ERROR_SYMBOL is nil, because that
1542 is a memory-full error. */
1543 Vsignaling_function = Qnil;
1544 if (backtrace_list && !NILP (error_symbol))
1546 bp = backtrace_list->next;
1547 if (bp && bp->function && EQ (*bp->function, Qerror))
1548 bp = bp->next;
1549 if (bp && bp->function)
1550 Vsignaling_function = *bp->function;
1553 for (; handlerlist; handlerlist = handlerlist->next)
1555 register Lisp_Object clause;
1557 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1558 max_lisp_eval_depth = lisp_eval_depth + 20;
1560 if (specpdl_size + 40 > max_specpdl_size)
1561 max_specpdl_size = specpdl_size + 40;
1563 clause = find_handler_clause (handlerlist->handler, conditions,
1564 error_symbol, data, &debugger_value);
1566 if (EQ (clause, Qlambda))
1568 /* We can't return values to code which signaled an error, but we
1569 can continue code which has signaled a quit. */
1570 if (EQ (real_error_symbol, Qquit))
1571 return Qnil;
1572 else
1573 error ("Cannot return from the debugger in an error");
1576 if (!NILP (clause))
1578 Lisp_Object unwind_data;
1579 struct handler *h = handlerlist;
1581 handlerlist = allhandlers;
1583 if (NILP (error_symbol))
1584 unwind_data = data;
1585 else
1586 unwind_data = Fcons (error_symbol, data);
1587 h->chosen_clause = clause;
1588 unwind_to_catch (h->tag, unwind_data);
1592 handlerlist = allhandlers;
1593 /* If no handler is present now, try to run the debugger,
1594 and if that fails, throw to top level. */
1595 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1596 if (catchlist != 0)
1597 Fthrow (Qtop_level, Qt);
1599 if (! NILP (error_symbol))
1600 data = Fcons (error_symbol, data);
1602 string = Ferror_message_string (data);
1603 fatal ("%s", SDATA (string), 0);
1606 /* Return nonzero iff LIST is a non-nil atom or
1607 a list containing one of CONDITIONS. */
1609 static int
1610 wants_debugger (list, conditions)
1611 Lisp_Object list, conditions;
1613 if (NILP (list))
1614 return 0;
1615 if (! CONSP (list))
1616 return 1;
1618 while (CONSP (conditions))
1620 Lisp_Object this, tail;
1621 this = XCAR (conditions);
1622 for (tail = list; CONSP (tail); tail = XCDR (tail))
1623 if (EQ (XCAR (tail), this))
1624 return 1;
1625 conditions = XCDR (conditions);
1627 return 0;
1630 /* Return 1 if an error with condition-symbols CONDITIONS,
1631 and described by SIGNAL-DATA, should skip the debugger
1632 according to debugger-ignored-errors. */
1634 static int
1635 skip_debugger (conditions, data)
1636 Lisp_Object conditions, data;
1638 Lisp_Object tail;
1639 int first_string = 1;
1640 Lisp_Object error_message;
1642 error_message = Qnil;
1643 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1645 if (STRINGP (XCAR (tail)))
1647 if (first_string)
1649 error_message = Ferror_message_string (data);
1650 first_string = 0;
1653 if (fast_string_match (XCAR (tail), error_message) >= 0)
1654 return 1;
1656 else
1658 Lisp_Object contail;
1660 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1661 if (EQ (XCAR (tail), XCAR (contail)))
1662 return 1;
1666 return 0;
1669 /* Value of Qlambda means we have called debugger and user has continued.
1670 There are two ways to pass SIG and DATA:
1671 = SIG is the error symbol, and DATA is the rest of the data.
1672 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1673 This is for memory-full errors only.
1675 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1677 static Lisp_Object
1678 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1679 Lisp_Object handlers, conditions, sig, data;
1680 Lisp_Object *debugger_value_ptr;
1682 register Lisp_Object h;
1683 register Lisp_Object tem;
1685 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1686 return Qt;
1687 /* error is used similarly, but means print an error message
1688 and run the debugger if that is enabled. */
1689 if (EQ (handlers, Qerror)
1690 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1691 there is a handler. */
1693 int count = SPECPDL_INDEX ();
1694 int debugger_called = 0;
1695 Lisp_Object sig_symbol, combined_data;
1696 /* This is set to 1 if we are handling a memory-full error,
1697 because these must not run the debugger.
1698 (There is no room in memory to do that!) */
1699 int no_debugger = 0;
1701 if (NILP (sig))
1703 combined_data = data;
1704 sig_symbol = Fcar (data);
1705 no_debugger = 1;
1707 else
1709 combined_data = Fcons (sig, data);
1710 sig_symbol = sig;
1713 if (wants_debugger (Vstack_trace_on_error, conditions))
1715 #ifdef PROTOTYPES
1716 internal_with_output_to_temp_buffer ("*Backtrace*",
1717 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1718 Qnil);
1719 #else
1720 internal_with_output_to_temp_buffer ("*Backtrace*",
1721 Fbacktrace, Qnil);
1722 #endif
1724 if (! no_debugger
1725 && (EQ (sig_symbol, Qquit)
1726 ? debug_on_quit
1727 : wants_debugger (Vdebug_on_error, conditions))
1728 && ! skip_debugger (conditions, combined_data)
1729 && when_entered_debugger < num_nonmacro_input_events)
1731 specbind (Qdebug_on_error, Qnil);
1732 *debugger_value_ptr
1733 = call_debugger (Fcons (Qerror,
1734 Fcons (combined_data, Qnil)));
1735 debugger_called = 1;
1737 /* If there is no handler, return saying whether we ran the debugger. */
1738 if (EQ (handlers, Qerror))
1740 if (debugger_called)
1741 return unbind_to (count, Qlambda);
1742 return Qt;
1745 for (h = handlers; CONSP (h); h = Fcdr (h))
1747 Lisp_Object handler, condit;
1749 handler = Fcar (h);
1750 if (!CONSP (handler))
1751 continue;
1752 condit = Fcar (handler);
1753 /* Handle a single condition name in handler HANDLER. */
1754 if (SYMBOLP (condit))
1756 tem = Fmemq (Fcar (handler), conditions);
1757 if (!NILP (tem))
1758 return handler;
1760 /* Handle a list of condition names in handler HANDLER. */
1761 else if (CONSP (condit))
1763 while (CONSP (condit))
1765 tem = Fmemq (Fcar (condit), conditions);
1766 if (!NILP (tem))
1767 return handler;
1768 condit = XCDR (condit);
1772 return Qnil;
1775 /* dump an error message; called like printf */
1777 /* VARARGS 1 */
1778 void
1779 error (m, a1, a2, a3)
1780 char *m;
1781 char *a1, *a2, *a3;
1783 char buf[200];
1784 int size = 200;
1785 int mlen;
1786 char *buffer = buf;
1787 char *args[3];
1788 int allocated = 0;
1789 Lisp_Object string;
1791 args[0] = a1;
1792 args[1] = a2;
1793 args[2] = a3;
1795 mlen = strlen (m);
1797 while (1)
1799 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1800 if (used < size)
1801 break;
1802 size *= 2;
1803 if (allocated)
1804 buffer = (char *) xrealloc (buffer, size);
1805 else
1807 buffer = (char *) xmalloc (size);
1808 allocated = 1;
1812 string = build_string (buffer);
1813 if (allocated)
1814 xfree (buffer);
1816 Fsignal (Qerror, Fcons (string, Qnil));
1817 abort ();
1820 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1821 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1822 This means it contains a description for how to read arguments to give it.
1823 The value is nil for an invalid function or a symbol with no function
1824 definition.
1826 Interactively callable functions include strings and vectors (treated
1827 as keyboard macros), lambda-expressions that contain a top-level call
1828 to `interactive', autoload definitions made by `autoload' with non-nil
1829 fourth argument, and some of the built-in functions of Lisp.
1831 Also, a symbol satisfies `commandp' if its function definition does so.
1833 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1834 then strings and vectors are not accepted. */)
1835 (function, for_call_interactively)
1836 Lisp_Object function, for_call_interactively;
1838 register Lisp_Object fun;
1839 register Lisp_Object funcar;
1841 fun = function;
1843 fun = indirect_function (fun);
1844 if (EQ (fun, Qunbound))
1845 return Qnil;
1847 /* Emacs primitives are interactive if their DEFUN specifies an
1848 interactive spec. */
1849 if (SUBRP (fun))
1851 if (XSUBR (fun)->prompt)
1852 return Qt;
1853 else
1854 return Qnil;
1857 /* Bytecode objects are interactive if they are long enough to
1858 have an element whose index is COMPILED_INTERACTIVE, which is
1859 where the interactive spec is stored. */
1860 else if (COMPILEDP (fun))
1861 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1862 ? Qt : Qnil);
1864 /* Strings and vectors are keyboard macros. */
1865 if (NILP (for_call_interactively) && (STRINGP (fun) || VECTORP (fun)))
1866 return Qt;
1868 /* Lists may represent commands. */
1869 if (!CONSP (fun))
1870 return Qnil;
1871 funcar = XCAR (fun);
1872 if (EQ (funcar, Qlambda))
1873 return Fassq (Qinteractive, Fcdr (XCDR (fun)));
1874 if (EQ (funcar, Qautoload))
1875 return Fcar (Fcdr (Fcdr (XCDR (fun))));
1876 else
1877 return Qnil;
1880 /* ARGSUSED */
1881 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1882 doc: /* Define FUNCTION to autoload from FILE.
1883 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1884 Third arg DOCSTRING is documentation for the function.
1885 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1886 Fifth arg TYPE indicates the type of the object:
1887 nil or omitted says FUNCTION is a function,
1888 `keymap' says FUNCTION is really a keymap, and
1889 `macro' or t says FUNCTION is really a macro.
1890 Third through fifth args give info about the real definition.
1891 They default to nil.
1892 If FUNCTION is already defined other than as an autoload,
1893 this does nothing and returns nil. */)
1894 (function, file, docstring, interactive, type)
1895 Lisp_Object function, file, docstring, interactive, type;
1897 #ifdef NO_ARG_ARRAY
1898 Lisp_Object args[4];
1899 #endif
1901 CHECK_SYMBOL (function);
1902 CHECK_STRING (file);
1904 /* If function is defined and not as an autoload, don't override */
1905 if (!EQ (XSYMBOL (function)->function, Qunbound)
1906 && !(CONSP (XSYMBOL (function)->function)
1907 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1908 return Qnil;
1910 if (NILP (Vpurify_flag))
1911 /* Only add entries after dumping, because the ones before are
1912 not useful and else we get loads of them from the loaddefs.el. */
1913 LOADHIST_ATTACH (Fcons (Qautoload, function));
1915 #ifdef NO_ARG_ARRAY
1916 args[0] = file;
1917 args[1] = docstring;
1918 args[2] = interactive;
1919 args[3] = type;
1921 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1922 #else /* NO_ARG_ARRAY */
1923 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1924 #endif /* not NO_ARG_ARRAY */
1927 Lisp_Object
1928 un_autoload (oldqueue)
1929 Lisp_Object oldqueue;
1931 register Lisp_Object queue, first, second;
1933 /* Queue to unwind is current value of Vautoload_queue.
1934 oldqueue is the shadowed value to leave in Vautoload_queue. */
1935 queue = Vautoload_queue;
1936 Vautoload_queue = oldqueue;
1937 while (CONSP (queue))
1939 first = XCAR (queue);
1940 second = Fcdr (first);
1941 first = Fcar (first);
1942 if (EQ (second, Qnil))
1943 Vfeatures = first;
1944 else
1945 Ffset (first, second);
1946 queue = XCDR (queue);
1948 return Qnil;
1951 /* Load an autoloaded function.
1952 FUNNAME is the symbol which is the function's name.
1953 FUNDEF is the autoload definition (a list). */
1955 void
1956 do_autoload (fundef, funname)
1957 Lisp_Object fundef, funname;
1959 int count = SPECPDL_INDEX ();
1960 Lisp_Object fun, queue, first, second;
1961 struct gcpro gcpro1, gcpro2, gcpro3;
1963 /* This is to make sure that loadup.el gives a clear picture
1964 of what files are preloaded and when. */
1965 if (! NILP (Vpurify_flag))
1966 error ("Attempt to autoload %s while preparing to dump",
1967 SDATA (SYMBOL_NAME (funname)));
1969 fun = funname;
1970 CHECK_SYMBOL (funname);
1971 GCPRO3 (fun, funname, fundef);
1973 /* Preserve the match data. */
1974 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1976 /* Value saved here is to be restored into Vautoload_queue. */
1977 record_unwind_protect (un_autoload, Vautoload_queue);
1978 Vautoload_queue = Qt;
1979 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1981 /* Save the old autoloads, in case we ever do an unload. */
1982 queue = Vautoload_queue;
1983 while (CONSP (queue))
1985 first = XCAR (queue);
1986 second = Fcdr (first);
1987 first = Fcar (first);
1989 if (CONSP (second) && EQ (XCAR (second), Qautoload))
1990 Fput (first, Qautoload, (XCDR (second)));
1992 queue = XCDR (queue);
1995 /* Once loading finishes, don't undo it. */
1996 Vautoload_queue = Qt;
1997 unbind_to (count, Qnil);
1999 fun = Findirect_function (fun);
2001 if (!NILP (Fequal (fun, fundef)))
2002 error ("Autoloading failed to define function %s",
2003 SDATA (SYMBOL_NAME (funname)));
2004 UNGCPRO;
2008 DEFUN ("eval", Feval, Seval, 1, 1, 0,
2009 doc: /* Evaluate FORM and return its value. */)
2010 (form)
2011 Lisp_Object form;
2013 Lisp_Object fun, val, original_fun, original_args;
2014 Lisp_Object funcar;
2015 struct backtrace backtrace;
2016 struct gcpro gcpro1, gcpro2, gcpro3;
2018 if (handling_signal)
2019 abort ();
2021 if (SYMBOLP (form))
2022 return Fsymbol_value (form);
2023 if (!CONSP (form))
2024 return form;
2026 QUIT;
2027 if (consing_since_gc > gc_cons_threshold)
2029 GCPRO1 (form);
2030 Fgarbage_collect ();
2031 UNGCPRO;
2034 if (++lisp_eval_depth > max_lisp_eval_depth)
2036 if (max_lisp_eval_depth < 100)
2037 max_lisp_eval_depth = 100;
2038 if (lisp_eval_depth > max_lisp_eval_depth)
2039 error ("Lisp nesting exceeds max-lisp-eval-depth");
2042 original_fun = Fcar (form);
2043 original_args = Fcdr (form);
2045 backtrace.next = backtrace_list;
2046 backtrace_list = &backtrace;
2047 backtrace.function = &original_fun; /* This also protects them from gc */
2048 backtrace.args = &original_args;
2049 backtrace.nargs = UNEVALLED;
2050 backtrace.evalargs = 1;
2051 backtrace.debug_on_exit = 0;
2053 if (debug_on_next_call)
2054 do_debug_on_call (Qt);
2056 /* At this point, only original_fun and original_args
2057 have values that will be used below */
2058 retry:
2059 fun = Findirect_function (original_fun);
2061 if (SUBRP (fun))
2063 Lisp_Object numargs;
2064 Lisp_Object argvals[8];
2065 Lisp_Object args_left;
2066 register int i, maxargs;
2068 args_left = original_args;
2069 numargs = Flength (args_left);
2071 CHECK_CONS_LIST ();
2073 if (XINT (numargs) < XSUBR (fun)->min_args ||
2074 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2075 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2077 if (XSUBR (fun)->max_args == UNEVALLED)
2079 backtrace.evalargs = 0;
2080 val = (*XSUBR (fun)->function) (args_left);
2081 goto done;
2084 if (XSUBR (fun)->max_args == MANY)
2086 /* Pass a vector of evaluated arguments */
2087 Lisp_Object *vals;
2088 register int argnum = 0;
2090 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2092 GCPRO3 (args_left, fun, fun);
2093 gcpro3.var = vals;
2094 gcpro3.nvars = 0;
2096 while (!NILP (args_left))
2098 vals[argnum++] = Feval (Fcar (args_left));
2099 args_left = Fcdr (args_left);
2100 gcpro3.nvars = argnum;
2103 backtrace.args = vals;
2104 backtrace.nargs = XINT (numargs);
2106 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2107 UNGCPRO;
2108 goto done;
2111 GCPRO3 (args_left, fun, fun);
2112 gcpro3.var = argvals;
2113 gcpro3.nvars = 0;
2115 maxargs = XSUBR (fun)->max_args;
2116 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2118 argvals[i] = Feval (Fcar (args_left));
2119 gcpro3.nvars = ++i;
2122 UNGCPRO;
2124 backtrace.args = argvals;
2125 backtrace.nargs = XINT (numargs);
2127 switch (i)
2129 case 0:
2130 val = (*XSUBR (fun)->function) ();
2131 goto done;
2132 case 1:
2133 val = (*XSUBR (fun)->function) (argvals[0]);
2134 goto done;
2135 case 2:
2136 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2137 goto done;
2138 case 3:
2139 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2140 argvals[2]);
2141 goto done;
2142 case 4:
2143 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2144 argvals[2], argvals[3]);
2145 goto done;
2146 case 5:
2147 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2148 argvals[3], argvals[4]);
2149 goto done;
2150 case 6:
2151 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2152 argvals[3], argvals[4], argvals[5]);
2153 goto done;
2154 case 7:
2155 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2156 argvals[3], argvals[4], argvals[5],
2157 argvals[6]);
2158 goto done;
2160 case 8:
2161 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2162 argvals[3], argvals[4], argvals[5],
2163 argvals[6], argvals[7]);
2164 goto done;
2166 default:
2167 /* Someone has created a subr that takes more arguments than
2168 is supported by this code. We need to either rewrite the
2169 subr to use a different argument protocol, or add more
2170 cases to this switch. */
2171 abort ();
2174 if (COMPILEDP (fun))
2175 val = apply_lambda (fun, original_args, 1);
2176 else
2178 if (!CONSP (fun))
2179 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2180 funcar = Fcar (fun);
2181 if (!SYMBOLP (funcar))
2182 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2183 if (EQ (funcar, Qautoload))
2185 do_autoload (fun, original_fun);
2186 goto retry;
2188 if (EQ (funcar, Qmacro))
2189 val = Feval (apply1 (Fcdr (fun), original_args));
2190 else if (EQ (funcar, Qlambda))
2191 val = apply_lambda (fun, original_args, 1);
2192 else
2193 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2195 done:
2196 CHECK_CONS_LIST ();
2198 lisp_eval_depth--;
2199 if (backtrace.debug_on_exit)
2200 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2201 backtrace_list = backtrace.next;
2203 return val;
2206 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2207 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2208 Then return the value FUNCTION returns.
2209 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2210 usage: (apply FUNCTION &rest ARGUMENTS) */)
2211 (nargs, args)
2212 int nargs;
2213 Lisp_Object *args;
2215 register int i, numargs;
2216 register Lisp_Object spread_arg;
2217 register Lisp_Object *funcall_args;
2218 Lisp_Object fun;
2219 struct gcpro gcpro1;
2221 fun = args [0];
2222 funcall_args = 0;
2223 spread_arg = args [nargs - 1];
2224 CHECK_LIST (spread_arg);
2226 numargs = XINT (Flength (spread_arg));
2228 if (numargs == 0)
2229 return Ffuncall (nargs - 1, args);
2230 else if (numargs == 1)
2232 args [nargs - 1] = XCAR (spread_arg);
2233 return Ffuncall (nargs, args);
2236 numargs += nargs - 2;
2238 fun = indirect_function (fun);
2239 if (EQ (fun, Qunbound))
2241 /* Let funcall get the error */
2242 fun = args[0];
2243 goto funcall;
2246 if (SUBRP (fun))
2248 if (numargs < XSUBR (fun)->min_args
2249 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2250 goto funcall; /* Let funcall get the error */
2251 else if (XSUBR (fun)->max_args > numargs)
2253 /* Avoid making funcall cons up a yet another new vector of arguments
2254 by explicitly supplying nil's for optional values */
2255 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2256 * sizeof (Lisp_Object));
2257 for (i = numargs; i < XSUBR (fun)->max_args;)
2258 funcall_args[++i] = Qnil;
2259 GCPRO1 (*funcall_args);
2260 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2263 funcall:
2264 /* We add 1 to numargs because funcall_args includes the
2265 function itself as well as its arguments. */
2266 if (!funcall_args)
2268 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2269 * sizeof (Lisp_Object));
2270 GCPRO1 (*funcall_args);
2271 gcpro1.nvars = 1 + numargs;
2274 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2275 /* Spread the last arg we got. Its first element goes in
2276 the slot that it used to occupy, hence this value of I. */
2277 i = nargs - 1;
2278 while (!NILP (spread_arg))
2280 funcall_args [i++] = XCAR (spread_arg);
2281 spread_arg = XCDR (spread_arg);
2284 /* By convention, the caller needs to gcpro Ffuncall's args. */
2285 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2288 /* Run hook variables in various ways. */
2290 enum run_hooks_condition {to_completion, until_success, until_failure};
2291 static Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
2292 enum run_hooks_condition));
2294 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2295 doc: /* Run each hook in HOOKS. Major mode functions use this.
2296 Each argument should be a symbol, a hook variable.
2297 These symbols are processed in the order specified.
2298 If a hook symbol has a non-nil value, that value may be a function
2299 or a list of functions to be called to run the hook.
2300 If the value is a function, it is called with no arguments.
2301 If it is a list, the elements are called, in order, with no arguments.
2303 Do not use `make-local-variable' to make a hook variable buffer-local.
2304 Instead, use `add-hook' and specify t for the LOCAL argument.
2305 usage: (run-hooks &rest HOOKS) */)
2306 (nargs, args)
2307 int nargs;
2308 Lisp_Object *args;
2310 Lisp_Object hook[1];
2311 register int i;
2313 for (i = 0; i < nargs; i++)
2315 hook[0] = args[i];
2316 run_hook_with_args (1, hook, to_completion);
2319 return Qnil;
2322 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2323 Srun_hook_with_args, 1, MANY, 0,
2324 doc: /* Run HOOK with the specified arguments ARGS.
2325 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2326 value, that value may be a function or a list of functions to be
2327 called to run the hook. If the value is a function, it is called with
2328 the given arguments and its return value is returned. If it is a list
2329 of functions, those functions are called, in order,
2330 with the given arguments ARGS.
2331 It is best not to depend on the value returned by `run-hook-with-args',
2332 as that may change.
2334 Do not use `make-local-variable' to make a hook variable buffer-local.
2335 Instead, use `add-hook' and specify t for the LOCAL argument.
2336 usage: (run-hook-with-args HOOK &rest ARGS) */)
2337 (nargs, args)
2338 int nargs;
2339 Lisp_Object *args;
2341 return run_hook_with_args (nargs, args, to_completion);
2344 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2345 Srun_hook_with_args_until_success, 1, MANY, 0,
2346 doc: /* Run HOOK with the specified arguments ARGS.
2347 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2348 value, that value may be a function or a list of functions to be
2349 called to run the hook. If the value is a function, it is called with
2350 the given arguments and its return value is returned.
2351 If it is a list of functions, those functions are called, in order,
2352 with the given arguments ARGS, until one of them
2353 returns a non-nil value. Then we return that value.
2354 However, if they all return nil, we return nil.
2356 Do not use `make-local-variable' to make a hook variable buffer-local.
2357 Instead, use `add-hook' and specify t for the LOCAL argument.
2358 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2359 (nargs, args)
2360 int nargs;
2361 Lisp_Object *args;
2363 return run_hook_with_args (nargs, args, until_success);
2366 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2367 Srun_hook_with_args_until_failure, 1, MANY, 0,
2368 doc: /* Run HOOK with the specified arguments ARGS.
2369 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2370 value, that value may be a function or a list of functions to be
2371 called to run the hook. If the value is a function, it is called with
2372 the given arguments and its return value is returned.
2373 If it is a list of functions, those functions are called, in order,
2374 with the given arguments ARGS, until one of them returns nil.
2375 Then we return nil. However, if they all return non-nil, we return non-nil.
2377 Do not use `make-local-variable' to make a hook variable buffer-local.
2378 Instead, use `add-hook' and specify t for the LOCAL argument.
2379 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2380 (nargs, args)
2381 int nargs;
2382 Lisp_Object *args;
2384 return run_hook_with_args (nargs, args, until_failure);
2387 /* ARGS[0] should be a hook symbol.
2388 Call each of the functions in the hook value, passing each of them
2389 as arguments all the rest of ARGS (all NARGS - 1 elements).
2390 COND specifies a condition to test after each call
2391 to decide whether to stop.
2392 The caller (or its caller, etc) must gcpro all of ARGS,
2393 except that it isn't necessary to gcpro ARGS[0]. */
2395 static Lisp_Object
2396 run_hook_with_args (nargs, args, cond)
2397 int nargs;
2398 Lisp_Object *args;
2399 enum run_hooks_condition cond;
2401 Lisp_Object sym, val, ret;
2402 Lisp_Object globals;
2403 struct gcpro gcpro1, gcpro2, gcpro3;
2405 /* If we are dying or still initializing,
2406 don't do anything--it would probably crash if we tried. */
2407 if (NILP (Vrun_hooks))
2408 return Qnil;
2410 sym = args[0];
2411 val = find_symbol_value (sym);
2412 ret = (cond == until_failure ? Qt : Qnil);
2414 if (EQ (val, Qunbound) || NILP (val))
2415 return ret;
2416 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2418 args[0] = val;
2419 return Ffuncall (nargs, args);
2421 else
2423 globals = Qnil;
2424 GCPRO3 (sym, val, globals);
2426 for (;
2427 CONSP (val) && ((cond == to_completion)
2428 || (cond == until_success ? NILP (ret)
2429 : !NILP (ret)));
2430 val = XCDR (val))
2432 if (EQ (XCAR (val), Qt))
2434 /* t indicates this hook has a local binding;
2435 it means to run the global binding too. */
2437 for (globals = Fdefault_value (sym);
2438 CONSP (globals) && ((cond == to_completion)
2439 || (cond == until_success ? NILP (ret)
2440 : !NILP (ret)));
2441 globals = XCDR (globals))
2443 args[0] = XCAR (globals);
2444 /* In a global value, t should not occur. If it does, we
2445 must ignore it to avoid an endless loop. */
2446 if (!EQ (args[0], Qt))
2447 ret = Ffuncall (nargs, args);
2450 else
2452 args[0] = XCAR (val);
2453 ret = Ffuncall (nargs, args);
2457 UNGCPRO;
2458 return ret;
2462 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2463 present value of that symbol.
2464 Call each element of FUNLIST,
2465 passing each of them the rest of ARGS.
2466 The caller (or its caller, etc) must gcpro all of ARGS,
2467 except that it isn't necessary to gcpro ARGS[0]. */
2469 Lisp_Object
2470 run_hook_list_with_args (funlist, nargs, args)
2471 Lisp_Object funlist;
2472 int nargs;
2473 Lisp_Object *args;
2475 Lisp_Object sym;
2476 Lisp_Object val;
2477 Lisp_Object globals;
2478 struct gcpro gcpro1, gcpro2, gcpro3;
2480 sym = args[0];
2481 globals = Qnil;
2482 GCPRO3 (sym, val, globals);
2484 for (val = funlist; CONSP (val); val = XCDR (val))
2486 if (EQ (XCAR (val), Qt))
2488 /* t indicates this hook has a local binding;
2489 it means to run the global binding too. */
2491 for (globals = Fdefault_value (sym);
2492 CONSP (globals);
2493 globals = XCDR (globals))
2495 args[0] = XCAR (globals);
2496 /* In a global value, t should not occur. If it does, we
2497 must ignore it to avoid an endless loop. */
2498 if (!EQ (args[0], Qt))
2499 Ffuncall (nargs, args);
2502 else
2504 args[0] = XCAR (val);
2505 Ffuncall (nargs, args);
2508 UNGCPRO;
2509 return Qnil;
2512 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2514 void
2515 run_hook_with_args_2 (hook, arg1, arg2)
2516 Lisp_Object hook, arg1, arg2;
2518 Lisp_Object temp[3];
2519 temp[0] = hook;
2520 temp[1] = arg1;
2521 temp[2] = arg2;
2523 Frun_hook_with_args (3, temp);
2526 /* Apply fn to arg */
2527 Lisp_Object
2528 apply1 (fn, arg)
2529 Lisp_Object fn, arg;
2531 struct gcpro gcpro1;
2533 GCPRO1 (fn);
2534 if (NILP (arg))
2535 RETURN_UNGCPRO (Ffuncall (1, &fn));
2536 gcpro1.nvars = 2;
2537 #ifdef NO_ARG_ARRAY
2539 Lisp_Object args[2];
2540 args[0] = fn;
2541 args[1] = arg;
2542 gcpro1.var = args;
2543 RETURN_UNGCPRO (Fapply (2, args));
2545 #else /* not NO_ARG_ARRAY */
2546 RETURN_UNGCPRO (Fapply (2, &fn));
2547 #endif /* not NO_ARG_ARRAY */
2550 /* Call function fn on no arguments */
2551 Lisp_Object
2552 call0 (fn)
2553 Lisp_Object fn;
2555 struct gcpro gcpro1;
2557 GCPRO1 (fn);
2558 RETURN_UNGCPRO (Ffuncall (1, &fn));
2561 /* Call function fn with 1 argument arg1 */
2562 /* ARGSUSED */
2563 Lisp_Object
2564 call1 (fn, arg1)
2565 Lisp_Object fn, arg1;
2567 struct gcpro gcpro1;
2568 #ifdef NO_ARG_ARRAY
2569 Lisp_Object args[2];
2571 args[0] = fn;
2572 args[1] = arg1;
2573 GCPRO1 (args[0]);
2574 gcpro1.nvars = 2;
2575 RETURN_UNGCPRO (Ffuncall (2, args));
2576 #else /* not NO_ARG_ARRAY */
2577 GCPRO1 (fn);
2578 gcpro1.nvars = 2;
2579 RETURN_UNGCPRO (Ffuncall (2, &fn));
2580 #endif /* not NO_ARG_ARRAY */
2583 /* Call function fn with 2 arguments arg1, arg2 */
2584 /* ARGSUSED */
2585 Lisp_Object
2586 call2 (fn, arg1, arg2)
2587 Lisp_Object fn, arg1, arg2;
2589 struct gcpro gcpro1;
2590 #ifdef NO_ARG_ARRAY
2591 Lisp_Object args[3];
2592 args[0] = fn;
2593 args[1] = arg1;
2594 args[2] = arg2;
2595 GCPRO1 (args[0]);
2596 gcpro1.nvars = 3;
2597 RETURN_UNGCPRO (Ffuncall (3, args));
2598 #else /* not NO_ARG_ARRAY */
2599 GCPRO1 (fn);
2600 gcpro1.nvars = 3;
2601 RETURN_UNGCPRO (Ffuncall (3, &fn));
2602 #endif /* not NO_ARG_ARRAY */
2605 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2606 /* ARGSUSED */
2607 Lisp_Object
2608 call3 (fn, arg1, arg2, arg3)
2609 Lisp_Object fn, arg1, arg2, arg3;
2611 struct gcpro gcpro1;
2612 #ifdef NO_ARG_ARRAY
2613 Lisp_Object args[4];
2614 args[0] = fn;
2615 args[1] = arg1;
2616 args[2] = arg2;
2617 args[3] = arg3;
2618 GCPRO1 (args[0]);
2619 gcpro1.nvars = 4;
2620 RETURN_UNGCPRO (Ffuncall (4, args));
2621 #else /* not NO_ARG_ARRAY */
2622 GCPRO1 (fn);
2623 gcpro1.nvars = 4;
2624 RETURN_UNGCPRO (Ffuncall (4, &fn));
2625 #endif /* not NO_ARG_ARRAY */
2628 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2629 /* ARGSUSED */
2630 Lisp_Object
2631 call4 (fn, arg1, arg2, arg3, arg4)
2632 Lisp_Object fn, arg1, arg2, arg3, arg4;
2634 struct gcpro gcpro1;
2635 #ifdef NO_ARG_ARRAY
2636 Lisp_Object args[5];
2637 args[0] = fn;
2638 args[1] = arg1;
2639 args[2] = arg2;
2640 args[3] = arg3;
2641 args[4] = arg4;
2642 GCPRO1 (args[0]);
2643 gcpro1.nvars = 5;
2644 RETURN_UNGCPRO (Ffuncall (5, args));
2645 #else /* not NO_ARG_ARRAY */
2646 GCPRO1 (fn);
2647 gcpro1.nvars = 5;
2648 RETURN_UNGCPRO (Ffuncall (5, &fn));
2649 #endif /* not NO_ARG_ARRAY */
2652 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2653 /* ARGSUSED */
2654 Lisp_Object
2655 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2656 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2658 struct gcpro gcpro1;
2659 #ifdef NO_ARG_ARRAY
2660 Lisp_Object args[6];
2661 args[0] = fn;
2662 args[1] = arg1;
2663 args[2] = arg2;
2664 args[3] = arg3;
2665 args[4] = arg4;
2666 args[5] = arg5;
2667 GCPRO1 (args[0]);
2668 gcpro1.nvars = 6;
2669 RETURN_UNGCPRO (Ffuncall (6, args));
2670 #else /* not NO_ARG_ARRAY */
2671 GCPRO1 (fn);
2672 gcpro1.nvars = 6;
2673 RETURN_UNGCPRO (Ffuncall (6, &fn));
2674 #endif /* not NO_ARG_ARRAY */
2677 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2678 /* ARGSUSED */
2679 Lisp_Object
2680 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2681 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2683 struct gcpro gcpro1;
2684 #ifdef NO_ARG_ARRAY
2685 Lisp_Object args[7];
2686 args[0] = fn;
2687 args[1] = arg1;
2688 args[2] = arg2;
2689 args[3] = arg3;
2690 args[4] = arg4;
2691 args[5] = arg5;
2692 args[6] = arg6;
2693 GCPRO1 (args[0]);
2694 gcpro1.nvars = 7;
2695 RETURN_UNGCPRO (Ffuncall (7, args));
2696 #else /* not NO_ARG_ARRAY */
2697 GCPRO1 (fn);
2698 gcpro1.nvars = 7;
2699 RETURN_UNGCPRO (Ffuncall (7, &fn));
2700 #endif /* not NO_ARG_ARRAY */
2703 /* The caller should GCPRO all the elements of ARGS. */
2705 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2706 doc: /* Call first argument as a function, passing remaining arguments to it.
2707 Return the value that function returns.
2708 Thus, (funcall 'cons 'x 'y) returns (x . y).
2709 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2710 (nargs, args)
2711 int nargs;
2712 Lisp_Object *args;
2714 Lisp_Object fun;
2715 Lisp_Object funcar;
2716 int numargs = nargs - 1;
2717 Lisp_Object lisp_numargs;
2718 Lisp_Object val;
2719 struct backtrace backtrace;
2720 register Lisp_Object *internal_args;
2721 register int i;
2723 QUIT;
2724 if (consing_since_gc > gc_cons_threshold)
2725 Fgarbage_collect ();
2727 if (++lisp_eval_depth > max_lisp_eval_depth)
2729 if (max_lisp_eval_depth < 100)
2730 max_lisp_eval_depth = 100;
2731 if (lisp_eval_depth > max_lisp_eval_depth)
2732 error ("Lisp nesting exceeds max-lisp-eval-depth");
2735 backtrace.next = backtrace_list;
2736 backtrace_list = &backtrace;
2737 backtrace.function = &args[0];
2738 backtrace.args = &args[1];
2739 backtrace.nargs = nargs - 1;
2740 backtrace.evalargs = 0;
2741 backtrace.debug_on_exit = 0;
2743 if (debug_on_next_call)
2744 do_debug_on_call (Qlambda);
2746 CHECK_CONS_LIST ();
2748 retry:
2750 fun = args[0];
2752 fun = Findirect_function (fun);
2754 if (SUBRP (fun))
2756 if (numargs < XSUBR (fun)->min_args
2757 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2759 XSETFASTINT (lisp_numargs, numargs);
2760 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2763 if (XSUBR (fun)->max_args == UNEVALLED)
2764 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2766 if (XSUBR (fun)->max_args == MANY)
2768 val = (*XSUBR (fun)->function) (numargs, args + 1);
2769 goto done;
2772 if (XSUBR (fun)->max_args > numargs)
2774 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2775 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2776 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2777 internal_args[i] = Qnil;
2779 else
2780 internal_args = args + 1;
2781 switch (XSUBR (fun)->max_args)
2783 case 0:
2784 val = (*XSUBR (fun)->function) ();
2785 goto done;
2786 case 1:
2787 val = (*XSUBR (fun)->function) (internal_args[0]);
2788 goto done;
2789 case 2:
2790 val = (*XSUBR (fun)->function) (internal_args[0],
2791 internal_args[1]);
2792 goto done;
2793 case 3:
2794 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2795 internal_args[2]);
2796 goto done;
2797 case 4:
2798 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2799 internal_args[2],
2800 internal_args[3]);
2801 goto done;
2802 case 5:
2803 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2804 internal_args[2], internal_args[3],
2805 internal_args[4]);
2806 goto done;
2807 case 6:
2808 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2809 internal_args[2], internal_args[3],
2810 internal_args[4], internal_args[5]);
2811 goto done;
2812 case 7:
2813 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2814 internal_args[2], internal_args[3],
2815 internal_args[4], internal_args[5],
2816 internal_args[6]);
2817 goto done;
2819 case 8:
2820 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2821 internal_args[2], internal_args[3],
2822 internal_args[4], internal_args[5],
2823 internal_args[6], internal_args[7]);
2824 goto done;
2826 default:
2828 /* If a subr takes more than 8 arguments without using MANY
2829 or UNEVALLED, we need to extend this function to support it.
2830 Until this is done, there is no way to call the function. */
2831 abort ();
2834 if (COMPILEDP (fun))
2835 val = funcall_lambda (fun, numargs, args + 1);
2836 else
2838 if (!CONSP (fun))
2839 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2840 funcar = Fcar (fun);
2841 if (!SYMBOLP (funcar))
2842 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2843 if (EQ (funcar, Qlambda))
2844 val = funcall_lambda (fun, numargs, args + 1);
2845 else if (EQ (funcar, Qautoload))
2847 do_autoload (fun, args[0]);
2848 CHECK_CONS_LIST ();
2849 goto retry;
2851 else
2852 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2854 done:
2855 CHECK_CONS_LIST ();
2856 lisp_eval_depth--;
2857 if (backtrace.debug_on_exit)
2858 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2859 backtrace_list = backtrace.next;
2860 return val;
2863 Lisp_Object
2864 apply_lambda (fun, args, eval_flag)
2865 Lisp_Object fun, args;
2866 int eval_flag;
2868 Lisp_Object args_left;
2869 Lisp_Object numargs;
2870 register Lisp_Object *arg_vector;
2871 struct gcpro gcpro1, gcpro2, gcpro3;
2872 register int i;
2873 register Lisp_Object tem;
2875 numargs = Flength (args);
2876 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2877 args_left = args;
2879 GCPRO3 (*arg_vector, args_left, fun);
2880 gcpro1.nvars = 0;
2882 for (i = 0; i < XINT (numargs);)
2884 tem = Fcar (args_left), args_left = Fcdr (args_left);
2885 if (eval_flag) tem = Feval (tem);
2886 arg_vector[i++] = tem;
2887 gcpro1.nvars = i;
2890 UNGCPRO;
2892 if (eval_flag)
2894 backtrace_list->args = arg_vector;
2895 backtrace_list->nargs = i;
2897 backtrace_list->evalargs = 0;
2898 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2900 /* Do the debug-on-exit now, while arg_vector still exists. */
2901 if (backtrace_list->debug_on_exit)
2902 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2903 /* Don't do it again when we return to eval. */
2904 backtrace_list->debug_on_exit = 0;
2905 return tem;
2908 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2909 and return the result of evaluation.
2910 FUN must be either a lambda-expression or a compiled-code object. */
2912 static Lisp_Object
2913 funcall_lambda (fun, nargs, arg_vector)
2914 Lisp_Object fun;
2915 int nargs;
2916 register Lisp_Object *arg_vector;
2918 Lisp_Object val, syms_left, next;
2919 int count = SPECPDL_INDEX ();
2920 int i, optional, rest;
2922 if (CONSP (fun))
2924 syms_left = XCDR (fun);
2925 if (CONSP (syms_left))
2926 syms_left = XCAR (syms_left);
2927 else
2928 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2930 else if (COMPILEDP (fun))
2931 syms_left = AREF (fun, COMPILED_ARGLIST);
2932 else
2933 abort ();
2935 i = optional = rest = 0;
2936 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2938 QUIT;
2940 next = XCAR (syms_left);
2941 while (!SYMBOLP (next))
2942 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2944 if (EQ (next, Qand_rest))
2945 rest = 1;
2946 else if (EQ (next, Qand_optional))
2947 optional = 1;
2948 else if (rest)
2950 specbind (next, Flist (nargs - i, &arg_vector[i]));
2951 i = nargs;
2953 else if (i < nargs)
2954 specbind (next, arg_vector[i++]);
2955 else if (!optional)
2956 return Fsignal (Qwrong_number_of_arguments,
2957 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2958 else
2959 specbind (next, Qnil);
2962 if (!NILP (syms_left))
2963 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2964 else if (i < nargs)
2965 return Fsignal (Qwrong_number_of_arguments,
2966 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2968 if (CONSP (fun))
2969 val = Fprogn (XCDR (XCDR (fun)));
2970 else
2972 /* If we have not actually read the bytecode string
2973 and constants vector yet, fetch them from the file. */
2974 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2975 Ffetch_bytecode (fun);
2976 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2977 AREF (fun, COMPILED_CONSTANTS),
2978 AREF (fun, COMPILED_STACK_DEPTH));
2981 return unbind_to (count, val);
2984 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2985 1, 1, 0,
2986 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2987 (object)
2988 Lisp_Object object;
2990 Lisp_Object tem;
2992 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2994 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2995 if (!CONSP (tem))
2997 tem = AREF (object, COMPILED_BYTECODE);
2998 if (CONSP (tem) && STRINGP (XCAR (tem)))
2999 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3000 else
3001 error ("Invalid byte code");
3003 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
3004 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
3006 return object;
3009 void
3010 grow_specpdl ()
3012 register int count = SPECPDL_INDEX ();
3013 if (specpdl_size >= max_specpdl_size)
3015 if (max_specpdl_size < 400)
3016 max_specpdl_size = 400;
3017 if (specpdl_size >= max_specpdl_size)
3019 if (!NILP (Vdebug_on_error))
3020 /* Leave room for some specpdl in the debugger. */
3021 max_specpdl_size = specpdl_size + 100;
3022 Fsignal (Qerror,
3023 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
3026 specpdl_size *= 2;
3027 if (specpdl_size > max_specpdl_size)
3028 specpdl_size = max_specpdl_size;
3029 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
3030 specpdl_ptr = specpdl + count;
3033 void
3034 specbind (symbol, value)
3035 Lisp_Object symbol, value;
3037 Lisp_Object ovalue;
3038 Lisp_Object valcontents;
3040 CHECK_SYMBOL (symbol);
3041 if (specpdl_ptr == specpdl + specpdl_size)
3042 grow_specpdl ();
3044 /* The most common case is that of a non-constant symbol with a
3045 trivial value. Make that as fast as we can. */
3046 valcontents = SYMBOL_VALUE (symbol);
3047 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
3049 specpdl_ptr->symbol = symbol;
3050 specpdl_ptr->old_value = valcontents;
3051 specpdl_ptr->func = NULL;
3052 ++specpdl_ptr;
3053 SET_SYMBOL_VALUE (symbol, value);
3055 else
3057 Lisp_Object valcontents;
3059 ovalue = find_symbol_value (symbol);
3060 specpdl_ptr->func = 0;
3061 specpdl_ptr->old_value = ovalue;
3063 valcontents = XSYMBOL (symbol)->value;
3065 if (BUFFER_LOCAL_VALUEP (valcontents)
3066 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
3067 || BUFFER_OBJFWDP (valcontents))
3069 Lisp_Object where, current_buffer;
3071 current_buffer = Fcurrent_buffer ();
3073 /* For a local variable, record both the symbol and which
3074 buffer's or frame's value we are saving. */
3075 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3076 where = current_buffer;
3077 else if (!BUFFER_OBJFWDP (valcontents)
3078 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3079 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3080 else
3081 where = Qnil;
3083 /* We're not using the `unused' slot in the specbinding
3084 structure because this would mean we have to do more
3085 work for simple variables. */
3086 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3088 /* If SYMBOL is a per-buffer variable which doesn't have a
3089 buffer-local value here, make the `let' change the global
3090 value by changing the value of SYMBOL in all buffers not
3091 having their own value. This is consistent with what
3092 happens with other buffer-local variables. */
3093 if (NILP (where)
3094 && BUFFER_OBJFWDP (valcontents))
3096 ++specpdl_ptr;
3097 Fset_default (symbol, value);
3098 return;
3101 else
3102 specpdl_ptr->symbol = symbol;
3104 specpdl_ptr++;
3105 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3106 store_symval_forwarding (symbol, ovalue, value, NULL);
3107 else
3108 set_internal (symbol, value, 0, 1);
3112 void
3113 record_unwind_protect (function, arg)
3114 Lisp_Object (*function) P_ ((Lisp_Object));
3115 Lisp_Object arg;
3117 if (specpdl_ptr == specpdl + specpdl_size)
3118 grow_specpdl ();
3119 specpdl_ptr->func = function;
3120 specpdl_ptr->symbol = Qnil;
3121 specpdl_ptr->old_value = arg;
3122 specpdl_ptr++;
3125 Lisp_Object
3126 unbind_to (count, value)
3127 int count;
3128 Lisp_Object value;
3130 int quitf = !NILP (Vquit_flag);
3131 struct gcpro gcpro1;
3133 GCPRO1 (value);
3134 Vquit_flag = Qnil;
3136 while (specpdl_ptr != specpdl + count)
3138 /* Copy the binding, and decrement specpdl_ptr, before we do
3139 the work to unbind it. We decrement first
3140 so that an error in unbinding won't try to unbind
3141 the same entry again, and we copy the binding first
3142 in case more bindings are made during some of the code we run. */
3144 struct specbinding this_binding;
3145 this_binding = *--specpdl_ptr;
3147 if (this_binding.func != 0)
3148 (*this_binding.func) (this_binding.old_value);
3149 /* If the symbol is a list, it is really (SYMBOL WHERE
3150 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3151 frame. If WHERE is a buffer or frame, this indicates we
3152 bound a variable that had a buffer-local or frame-local
3153 binding. WHERE nil means that the variable had the default
3154 value when it was bound. CURRENT-BUFFER is the buffer that
3155 was current when the variable was bound. */
3156 else if (CONSP (this_binding.symbol))
3158 Lisp_Object symbol, where;
3160 symbol = XCAR (this_binding.symbol);
3161 where = XCAR (XCDR (this_binding.symbol));
3163 if (NILP (where))
3164 Fset_default (symbol, this_binding.old_value);
3165 else if (BUFFERP (where))
3166 set_internal (symbol, this_binding.old_value, XBUFFER (where), 1);
3167 else
3168 set_internal (symbol, this_binding.old_value, NULL, 1);
3170 else
3172 /* If variable has a trivial value (no forwarding), we can
3173 just set it. No need to check for constant symbols here,
3174 since that was already done by specbind. */
3175 if (!MISCP (SYMBOL_VALUE (this_binding.symbol)))
3176 SET_SYMBOL_VALUE (this_binding.symbol, this_binding.old_value);
3177 else
3178 set_internal (this_binding.symbol, this_binding.old_value, 0, 1);
3182 if (NILP (Vquit_flag) && quitf)
3183 Vquit_flag = Qt;
3185 UNGCPRO;
3186 return value;
3189 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3190 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3191 The debugger is entered when that frame exits, if the flag is non-nil. */)
3192 (level, flag)
3193 Lisp_Object level, flag;
3195 register struct backtrace *backlist = backtrace_list;
3196 register int i;
3198 CHECK_NUMBER (level);
3200 for (i = 0; backlist && i < XINT (level); i++)
3202 backlist = backlist->next;
3205 if (backlist)
3206 backlist->debug_on_exit = !NILP (flag);
3208 return flag;
3211 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3212 doc: /* Print a trace of Lisp function calls currently active.
3213 Output stream used is value of `standard-output'. */)
3216 register struct backtrace *backlist = backtrace_list;
3217 register int i;
3218 Lisp_Object tail;
3219 Lisp_Object tem;
3220 extern Lisp_Object Vprint_level;
3221 struct gcpro gcpro1;
3223 XSETFASTINT (Vprint_level, 3);
3225 tail = Qnil;
3226 GCPRO1 (tail);
3228 while (backlist)
3230 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3231 if (backlist->nargs == UNEVALLED)
3233 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3234 write_string ("\n", -1);
3236 else
3238 tem = *backlist->function;
3239 Fprin1 (tem, Qnil); /* This can QUIT */
3240 write_string ("(", -1);
3241 if (backlist->nargs == MANY)
3243 for (tail = *backlist->args, i = 0;
3244 !NILP (tail);
3245 tail = Fcdr (tail), i++)
3247 if (i) write_string (" ", -1);
3248 Fprin1 (Fcar (tail), Qnil);
3251 else
3253 for (i = 0; i < backlist->nargs; i++)
3255 if (i) write_string (" ", -1);
3256 Fprin1 (backlist->args[i], Qnil);
3259 write_string (")\n", -1);
3261 backlist = backlist->next;
3264 Vprint_level = Qnil;
3265 UNGCPRO;
3266 return Qnil;
3269 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3270 doc: /* Return the function and arguments NFRAMES up from current execution point.
3271 If that frame has not evaluated the arguments yet (or is a special form),
3272 the value is (nil FUNCTION ARG-FORMS...).
3273 If that frame has evaluated its arguments and called its function already,
3274 the value is (t FUNCTION ARG-VALUES...).
3275 A &rest arg is represented as the tail of the list ARG-VALUES.
3276 FUNCTION is whatever was supplied as car of evaluated list,
3277 or a lambda expression for macro calls.
3278 If NFRAMES is more than the number of frames, the value is nil. */)
3279 (nframes)
3280 Lisp_Object nframes;
3282 register struct backtrace *backlist = backtrace_list;
3283 register int i;
3284 Lisp_Object tem;
3286 CHECK_NATNUM (nframes);
3288 /* Find the frame requested. */
3289 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3290 backlist = backlist->next;
3292 if (!backlist)
3293 return Qnil;
3294 if (backlist->nargs == UNEVALLED)
3295 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3296 else
3298 if (backlist->nargs == MANY)
3299 tem = *backlist->args;
3300 else
3301 tem = Flist (backlist->nargs, backlist->args);
3303 return Fcons (Qt, Fcons (*backlist->function, tem));
3308 void
3309 mark_backtrace ()
3311 register struct backtrace *backlist;
3312 register int i;
3314 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3316 mark_object (*backlist->function);
3318 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3319 i = 0;
3320 else
3321 i = backlist->nargs - 1;
3322 for (; i >= 0; i--)
3323 mark_object (backlist->args[i]);
3327 void
3328 syms_of_eval ()
3330 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3331 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3332 If Lisp code tries to make more than this many at once,
3333 an error is signaled.
3334 You can safely use a value considerably larger than the default value,
3335 if that proves inconveniently small. However, if you increase it too far,
3336 Emacs could run out of memory trying to make the stack bigger. */);
3338 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3339 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3341 This limit serves to catch infinite recursions for you before they cause
3342 actual stack overflow in C, which would be fatal for Emacs.
3343 You can safely make it considerably larger than its default value,
3344 if that proves inconveniently small. However, if you increase it too far,
3345 Emacs could overflow the real C stack, and crash. */);
3347 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3348 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3349 If the value is t, that means do an ordinary quit.
3350 If the value equals `throw-on-input', that means quit by throwing
3351 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3352 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3353 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3354 Vquit_flag = Qnil;
3356 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3357 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3358 Note that `quit-flag' will still be set by typing C-g,
3359 so a quit will be signaled as soon as `inhibit-quit' is nil.
3360 To prevent this happening, set `quit-flag' to nil
3361 before making `inhibit-quit' nil. */);
3362 Vinhibit_quit = Qnil;
3364 Qinhibit_quit = intern ("inhibit-quit");
3365 staticpro (&Qinhibit_quit);
3367 Qautoload = intern ("autoload");
3368 staticpro (&Qautoload);
3370 Qdebug_on_error = intern ("debug-on-error");
3371 staticpro (&Qdebug_on_error);
3373 Qmacro = intern ("macro");
3374 staticpro (&Qmacro);
3376 Qdeclare = intern ("declare");
3377 staticpro (&Qdeclare);
3379 /* Note that the process handling also uses Qexit, but we don't want
3380 to staticpro it twice, so we just do it here. */
3381 Qexit = intern ("exit");
3382 staticpro (&Qexit);
3384 Qinteractive = intern ("interactive");
3385 staticpro (&Qinteractive);
3387 Qcommandp = intern ("commandp");
3388 staticpro (&Qcommandp);
3390 Qdefun = intern ("defun");
3391 staticpro (&Qdefun);
3393 Qand_rest = intern ("&rest");
3394 staticpro (&Qand_rest);
3396 Qand_optional = intern ("&optional");
3397 staticpro (&Qand_optional);
3399 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3400 doc: /* *Non-nil means errors display a backtrace buffer.
3401 More precisely, this happens for any error that is handled
3402 by the editor command loop.
3403 If the value is a list, an error only means to display a backtrace
3404 if one of its condition symbols appears in the list. */);
3405 Vstack_trace_on_error = Qnil;
3407 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3408 doc: /* *Non-nil means enter debugger if an error is signaled.
3409 Does not apply to errors handled by `condition-case' or those
3410 matched by `debug-ignored-errors'.
3411 If the value is a list, an error only means to enter the debugger
3412 if one of its condition symbols appears in the list.
3413 When you evaluate an expression interactively, this variable
3414 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3415 See also variable `debug-on-quit'. */);
3416 Vdebug_on_error = Qnil;
3418 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3419 doc: /* *List of errors for which the debugger should not be called.
3420 Each element may be a condition-name or a regexp that matches error messages.
3421 If any element applies to a given error, that error skips the debugger
3422 and just returns to top level.
3423 This overrides the variable `debug-on-error'.
3424 It does not apply to errors handled by `condition-case'. */);
3425 Vdebug_ignored_errors = Qnil;
3427 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3428 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3429 Does not apply if quit is handled by a `condition-case'.
3430 When you evaluate an expression interactively, this variable
3431 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3432 debug_on_quit = 0;
3434 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3435 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3437 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3438 doc: /* Non-nil means debugger may continue execution.
3439 This is nil when the debugger is called under circumstances where it
3440 might not be safe to continue. */);
3441 debugger_may_continue = 1;
3443 DEFVAR_LISP ("debugger", &Vdebugger,
3444 doc: /* Function to call to invoke debugger.
3445 If due to frame exit, args are `exit' and the value being returned;
3446 this function's value will be returned instead of that.
3447 If due to error, args are `error' and a list of the args to `signal'.
3448 If due to `apply' or `funcall' entry, one arg, `lambda'.
3449 If due to `eval' entry, one arg, t. */);
3450 Vdebugger = Qnil;
3452 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3453 doc: /* If non-nil, this is a function for `signal' to call.
3454 It receives the same arguments that `signal' was given.
3455 The Edebug package uses this to regain control. */);
3456 Vsignal_hook_function = Qnil;
3458 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3459 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3460 Note that `debug-on-error', `debug-on-quit' and friends
3461 still determine whether to handle the particular condition. */);
3462 Vdebug_on_signal = Qnil;
3464 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3465 doc: /* Function to process declarations in a macro definition.
3466 The function will be called with two args MACRO and DECL.
3467 MACRO is the name of the macro being defined.
3468 DECL is a list `(declare ...)' containing the declarations.
3469 The value the function returns is not used. */);
3470 Vmacro_declaration_function = Qnil;
3472 Vrun_hooks = intern ("run-hooks");
3473 staticpro (&Vrun_hooks);
3475 staticpro (&Vautoload_queue);
3476 Vautoload_queue = Qnil;
3477 staticpro (&Vsignaling_function);
3478 Vsignaling_function = Qnil;
3480 defsubr (&Sor);
3481 defsubr (&Sand);
3482 defsubr (&Sif);
3483 defsubr (&Scond);
3484 defsubr (&Sprogn);
3485 defsubr (&Sprog1);
3486 defsubr (&Sprog2);
3487 defsubr (&Ssetq);
3488 defsubr (&Squote);
3489 defsubr (&Sfunction);
3490 defsubr (&Sdefun);
3491 defsubr (&Sdefmacro);
3492 defsubr (&Sdefvar);
3493 defsubr (&Sdefvaralias);
3494 defsubr (&Sdefconst);
3495 defsubr (&Suser_variable_p);
3496 defsubr (&Slet);
3497 defsubr (&SletX);
3498 defsubr (&Swhile);
3499 defsubr (&Smacroexpand);
3500 defsubr (&Scatch);
3501 defsubr (&Sthrow);
3502 defsubr (&Sunwind_protect);
3503 defsubr (&Scondition_case);
3504 defsubr (&Ssignal);
3505 defsubr (&Sinteractive_p);
3506 defsubr (&Scalled_interactively_p);
3507 defsubr (&Scommandp);
3508 defsubr (&Sautoload);
3509 defsubr (&Seval);
3510 defsubr (&Sapply);
3511 defsubr (&Sfuncall);
3512 defsubr (&Srun_hooks);
3513 defsubr (&Srun_hook_with_args);
3514 defsubr (&Srun_hook_with_args_until_success);
3515 defsubr (&Srun_hook_with_args_until_failure);
3516 defsubr (&Sfetch_bytecode);
3517 defsubr (&Sbacktrace_debug);
3518 defsubr (&Sbacktrace);
3519 defsubr (&Sbacktrace_frame);
3522 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
3523 (do not change this comment) */