(Finternal_lisp_face_equal_p): Really report on faces in a frame, if the
[emacs.git] / src / eval.c
blob8bb201c5df5aebb7b392d412541be3273a7e65ed
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.
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 Major modes should not use this function directly to run their mode
2304 hook; they should use `run-mode-hooks' instead.
2306 Do not use `make-local-variable' to make a hook variable buffer-local.
2307 Instead, use `add-hook' and specify t for the LOCAL argument.
2308 usage: (run-hooks &rest HOOKS) */)
2309 (nargs, args)
2310 int nargs;
2311 Lisp_Object *args;
2313 Lisp_Object hook[1];
2314 register int i;
2316 for (i = 0; i < nargs; i++)
2318 hook[0] = args[i];
2319 run_hook_with_args (1, hook, to_completion);
2322 return Qnil;
2325 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2326 Srun_hook_with_args, 1, MANY, 0,
2327 doc: /* Run HOOK with the specified arguments ARGS.
2328 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2329 value, that value may be a function or a list of functions to be
2330 called to run the hook. If the value is a function, it is called with
2331 the given arguments and its return value is returned. If it is a list
2332 of functions, those functions are called, in order,
2333 with the given arguments ARGS.
2334 It is best not to depend on the value returned by `run-hook-with-args',
2335 as that may change.
2337 Do not use `make-local-variable' to make a hook variable buffer-local.
2338 Instead, use `add-hook' and specify t for the LOCAL argument.
2339 usage: (run-hook-with-args HOOK &rest ARGS) */)
2340 (nargs, args)
2341 int nargs;
2342 Lisp_Object *args;
2344 return run_hook_with_args (nargs, args, to_completion);
2347 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2348 Srun_hook_with_args_until_success, 1, MANY, 0,
2349 doc: /* Run HOOK with the specified arguments ARGS.
2350 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2351 value, that value may be a function or a list of functions to be
2352 called to run the hook. If the value is a function, it is called with
2353 the given arguments and its return value is returned.
2354 If it is a list of functions, those functions are called, in order,
2355 with the given arguments ARGS, until one of them
2356 returns a non-nil value. Then we return that value.
2357 However, if they all return nil, we return nil.
2359 Do not use `make-local-variable' to make a hook variable buffer-local.
2360 Instead, use `add-hook' and specify t for the LOCAL argument.
2361 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2362 (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2366 return run_hook_with_args (nargs, args, until_success);
2369 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2370 Srun_hook_with_args_until_failure, 1, MANY, 0,
2371 doc: /* Run HOOK with the specified arguments ARGS.
2372 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2373 value, that value may be a function or a list of functions to be
2374 called to run the hook. If the value is a function, it is called with
2375 the given arguments and its return value is returned.
2376 If it is a list of functions, those functions are called, in order,
2377 with the given arguments ARGS, until one of them returns nil.
2378 Then we return nil. However, if they all return non-nil, we return non-nil.
2380 Do not use `make-local-variable' to make a hook variable buffer-local.
2381 Instead, use `add-hook' and specify t for the LOCAL argument.
2382 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2383 (nargs, args)
2384 int nargs;
2385 Lisp_Object *args;
2387 return run_hook_with_args (nargs, args, until_failure);
2390 /* ARGS[0] should be a hook symbol.
2391 Call each of the functions in the hook value, passing each of them
2392 as arguments all the rest of ARGS (all NARGS - 1 elements).
2393 COND specifies a condition to test after each call
2394 to decide whether to stop.
2395 The caller (or its caller, etc) must gcpro all of ARGS,
2396 except that it isn't necessary to gcpro ARGS[0]. */
2398 static Lisp_Object
2399 run_hook_with_args (nargs, args, cond)
2400 int nargs;
2401 Lisp_Object *args;
2402 enum run_hooks_condition cond;
2404 Lisp_Object sym, val, ret;
2405 Lisp_Object globals;
2406 struct gcpro gcpro1, gcpro2, gcpro3;
2408 /* If we are dying or still initializing,
2409 don't do anything--it would probably crash if we tried. */
2410 if (NILP (Vrun_hooks))
2411 return Qnil;
2413 sym = args[0];
2414 val = find_symbol_value (sym);
2415 ret = (cond == until_failure ? Qt : Qnil);
2417 if (EQ (val, Qunbound) || NILP (val))
2418 return ret;
2419 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2421 args[0] = val;
2422 return Ffuncall (nargs, args);
2424 else
2426 globals = Qnil;
2427 GCPRO3 (sym, val, globals);
2429 for (;
2430 CONSP (val) && ((cond == to_completion)
2431 || (cond == until_success ? NILP (ret)
2432 : !NILP (ret)));
2433 val = XCDR (val))
2435 if (EQ (XCAR (val), Qt))
2437 /* t indicates this hook has a local binding;
2438 it means to run the global binding too. */
2440 for (globals = Fdefault_value (sym);
2441 CONSP (globals) && ((cond == to_completion)
2442 || (cond == until_success ? NILP (ret)
2443 : !NILP (ret)));
2444 globals = XCDR (globals))
2446 args[0] = XCAR (globals);
2447 /* In a global value, t should not occur. If it does, we
2448 must ignore it to avoid an endless loop. */
2449 if (!EQ (args[0], Qt))
2450 ret = Ffuncall (nargs, args);
2453 else
2455 args[0] = XCAR (val);
2456 ret = Ffuncall (nargs, args);
2460 UNGCPRO;
2461 return ret;
2465 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2466 present value of that symbol.
2467 Call each element of FUNLIST,
2468 passing each of them the rest of ARGS.
2469 The caller (or its caller, etc) must gcpro all of ARGS,
2470 except that it isn't necessary to gcpro ARGS[0]. */
2472 Lisp_Object
2473 run_hook_list_with_args (funlist, nargs, args)
2474 Lisp_Object funlist;
2475 int nargs;
2476 Lisp_Object *args;
2478 Lisp_Object sym;
2479 Lisp_Object val;
2480 Lisp_Object globals;
2481 struct gcpro gcpro1, gcpro2, gcpro3;
2483 sym = args[0];
2484 globals = Qnil;
2485 GCPRO3 (sym, val, globals);
2487 for (val = funlist; CONSP (val); val = XCDR (val))
2489 if (EQ (XCAR (val), Qt))
2491 /* t indicates this hook has a local binding;
2492 it means to run the global binding too. */
2494 for (globals = Fdefault_value (sym);
2495 CONSP (globals);
2496 globals = XCDR (globals))
2498 args[0] = XCAR (globals);
2499 /* In a global value, t should not occur. If it does, we
2500 must ignore it to avoid an endless loop. */
2501 if (!EQ (args[0], Qt))
2502 Ffuncall (nargs, args);
2505 else
2507 args[0] = XCAR (val);
2508 Ffuncall (nargs, args);
2511 UNGCPRO;
2512 return Qnil;
2515 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2517 void
2518 run_hook_with_args_2 (hook, arg1, arg2)
2519 Lisp_Object hook, arg1, arg2;
2521 Lisp_Object temp[3];
2522 temp[0] = hook;
2523 temp[1] = arg1;
2524 temp[2] = arg2;
2526 Frun_hook_with_args (3, temp);
2529 /* Apply fn to arg */
2530 Lisp_Object
2531 apply1 (fn, arg)
2532 Lisp_Object fn, arg;
2534 struct gcpro gcpro1;
2536 GCPRO1 (fn);
2537 if (NILP (arg))
2538 RETURN_UNGCPRO (Ffuncall (1, &fn));
2539 gcpro1.nvars = 2;
2540 #ifdef NO_ARG_ARRAY
2542 Lisp_Object args[2];
2543 args[0] = fn;
2544 args[1] = arg;
2545 gcpro1.var = args;
2546 RETURN_UNGCPRO (Fapply (2, args));
2548 #else /* not NO_ARG_ARRAY */
2549 RETURN_UNGCPRO (Fapply (2, &fn));
2550 #endif /* not NO_ARG_ARRAY */
2553 /* Call function fn on no arguments */
2554 Lisp_Object
2555 call0 (fn)
2556 Lisp_Object fn;
2558 struct gcpro gcpro1;
2560 GCPRO1 (fn);
2561 RETURN_UNGCPRO (Ffuncall (1, &fn));
2564 /* Call function fn with 1 argument arg1 */
2565 /* ARGSUSED */
2566 Lisp_Object
2567 call1 (fn, arg1)
2568 Lisp_Object fn, arg1;
2570 struct gcpro gcpro1;
2571 #ifdef NO_ARG_ARRAY
2572 Lisp_Object args[2];
2574 args[0] = fn;
2575 args[1] = arg1;
2576 GCPRO1 (args[0]);
2577 gcpro1.nvars = 2;
2578 RETURN_UNGCPRO (Ffuncall (2, args));
2579 #else /* not NO_ARG_ARRAY */
2580 GCPRO1 (fn);
2581 gcpro1.nvars = 2;
2582 RETURN_UNGCPRO (Ffuncall (2, &fn));
2583 #endif /* not NO_ARG_ARRAY */
2586 /* Call function fn with 2 arguments arg1, arg2 */
2587 /* ARGSUSED */
2588 Lisp_Object
2589 call2 (fn, arg1, arg2)
2590 Lisp_Object fn, arg1, arg2;
2592 struct gcpro gcpro1;
2593 #ifdef NO_ARG_ARRAY
2594 Lisp_Object args[3];
2595 args[0] = fn;
2596 args[1] = arg1;
2597 args[2] = arg2;
2598 GCPRO1 (args[0]);
2599 gcpro1.nvars = 3;
2600 RETURN_UNGCPRO (Ffuncall (3, args));
2601 #else /* not NO_ARG_ARRAY */
2602 GCPRO1 (fn);
2603 gcpro1.nvars = 3;
2604 RETURN_UNGCPRO (Ffuncall (3, &fn));
2605 #endif /* not NO_ARG_ARRAY */
2608 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2609 /* ARGSUSED */
2610 Lisp_Object
2611 call3 (fn, arg1, arg2, arg3)
2612 Lisp_Object fn, arg1, arg2, arg3;
2614 struct gcpro gcpro1;
2615 #ifdef NO_ARG_ARRAY
2616 Lisp_Object args[4];
2617 args[0] = fn;
2618 args[1] = arg1;
2619 args[2] = arg2;
2620 args[3] = arg3;
2621 GCPRO1 (args[0]);
2622 gcpro1.nvars = 4;
2623 RETURN_UNGCPRO (Ffuncall (4, args));
2624 #else /* not NO_ARG_ARRAY */
2625 GCPRO1 (fn);
2626 gcpro1.nvars = 4;
2627 RETURN_UNGCPRO (Ffuncall (4, &fn));
2628 #endif /* not NO_ARG_ARRAY */
2631 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2632 /* ARGSUSED */
2633 Lisp_Object
2634 call4 (fn, arg1, arg2, arg3, arg4)
2635 Lisp_Object fn, arg1, arg2, arg3, arg4;
2637 struct gcpro gcpro1;
2638 #ifdef NO_ARG_ARRAY
2639 Lisp_Object args[5];
2640 args[0] = fn;
2641 args[1] = arg1;
2642 args[2] = arg2;
2643 args[3] = arg3;
2644 args[4] = arg4;
2645 GCPRO1 (args[0]);
2646 gcpro1.nvars = 5;
2647 RETURN_UNGCPRO (Ffuncall (5, args));
2648 #else /* not NO_ARG_ARRAY */
2649 GCPRO1 (fn);
2650 gcpro1.nvars = 5;
2651 RETURN_UNGCPRO (Ffuncall (5, &fn));
2652 #endif /* not NO_ARG_ARRAY */
2655 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2656 /* ARGSUSED */
2657 Lisp_Object
2658 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2659 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2661 struct gcpro gcpro1;
2662 #ifdef NO_ARG_ARRAY
2663 Lisp_Object args[6];
2664 args[0] = fn;
2665 args[1] = arg1;
2666 args[2] = arg2;
2667 args[3] = arg3;
2668 args[4] = arg4;
2669 args[5] = arg5;
2670 GCPRO1 (args[0]);
2671 gcpro1.nvars = 6;
2672 RETURN_UNGCPRO (Ffuncall (6, args));
2673 #else /* not NO_ARG_ARRAY */
2674 GCPRO1 (fn);
2675 gcpro1.nvars = 6;
2676 RETURN_UNGCPRO (Ffuncall (6, &fn));
2677 #endif /* not NO_ARG_ARRAY */
2680 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2681 /* ARGSUSED */
2682 Lisp_Object
2683 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2684 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2686 struct gcpro gcpro1;
2687 #ifdef NO_ARG_ARRAY
2688 Lisp_Object args[7];
2689 args[0] = fn;
2690 args[1] = arg1;
2691 args[2] = arg2;
2692 args[3] = arg3;
2693 args[4] = arg4;
2694 args[5] = arg5;
2695 args[6] = arg6;
2696 GCPRO1 (args[0]);
2697 gcpro1.nvars = 7;
2698 RETURN_UNGCPRO (Ffuncall (7, args));
2699 #else /* not NO_ARG_ARRAY */
2700 GCPRO1 (fn);
2701 gcpro1.nvars = 7;
2702 RETURN_UNGCPRO (Ffuncall (7, &fn));
2703 #endif /* not NO_ARG_ARRAY */
2706 /* The caller should GCPRO all the elements of ARGS. */
2708 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2709 doc: /* Call first argument as a function, passing remaining arguments to it.
2710 Return the value that function returns.
2711 Thus, (funcall 'cons 'x 'y) returns (x . y).
2712 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2713 (nargs, args)
2714 int nargs;
2715 Lisp_Object *args;
2717 Lisp_Object fun;
2718 Lisp_Object funcar;
2719 int numargs = nargs - 1;
2720 Lisp_Object lisp_numargs;
2721 Lisp_Object val;
2722 struct backtrace backtrace;
2723 register Lisp_Object *internal_args;
2724 register int i;
2726 QUIT;
2727 if (consing_since_gc > gc_cons_threshold)
2728 Fgarbage_collect ();
2730 if (++lisp_eval_depth > max_lisp_eval_depth)
2732 if (max_lisp_eval_depth < 100)
2733 max_lisp_eval_depth = 100;
2734 if (lisp_eval_depth > max_lisp_eval_depth)
2735 error ("Lisp nesting exceeds max-lisp-eval-depth");
2738 backtrace.next = backtrace_list;
2739 backtrace_list = &backtrace;
2740 backtrace.function = &args[0];
2741 backtrace.args = &args[1];
2742 backtrace.nargs = nargs - 1;
2743 backtrace.evalargs = 0;
2744 backtrace.debug_on_exit = 0;
2746 if (debug_on_next_call)
2747 do_debug_on_call (Qlambda);
2749 CHECK_CONS_LIST ();
2751 retry:
2753 fun = args[0];
2755 fun = Findirect_function (fun);
2757 if (SUBRP (fun))
2759 if (numargs < XSUBR (fun)->min_args
2760 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2762 XSETFASTINT (lisp_numargs, numargs);
2763 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2766 if (XSUBR (fun)->max_args == UNEVALLED)
2767 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2769 if (XSUBR (fun)->max_args == MANY)
2771 val = (*XSUBR (fun)->function) (numargs, args + 1);
2772 goto done;
2775 if (XSUBR (fun)->max_args > numargs)
2777 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2778 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2779 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2780 internal_args[i] = Qnil;
2782 else
2783 internal_args = args + 1;
2784 switch (XSUBR (fun)->max_args)
2786 case 0:
2787 val = (*XSUBR (fun)->function) ();
2788 goto done;
2789 case 1:
2790 val = (*XSUBR (fun)->function) (internal_args[0]);
2791 goto done;
2792 case 2:
2793 val = (*XSUBR (fun)->function) (internal_args[0],
2794 internal_args[1]);
2795 goto done;
2796 case 3:
2797 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2798 internal_args[2]);
2799 goto done;
2800 case 4:
2801 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2802 internal_args[2],
2803 internal_args[3]);
2804 goto done;
2805 case 5:
2806 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2807 internal_args[2], internal_args[3],
2808 internal_args[4]);
2809 goto done;
2810 case 6:
2811 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2812 internal_args[2], internal_args[3],
2813 internal_args[4], internal_args[5]);
2814 goto done;
2815 case 7:
2816 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2817 internal_args[2], internal_args[3],
2818 internal_args[4], internal_args[5],
2819 internal_args[6]);
2820 goto done;
2822 case 8:
2823 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2824 internal_args[2], internal_args[3],
2825 internal_args[4], internal_args[5],
2826 internal_args[6], internal_args[7]);
2827 goto done;
2829 default:
2831 /* If a subr takes more than 8 arguments without using MANY
2832 or UNEVALLED, we need to extend this function to support it.
2833 Until this is done, there is no way to call the function. */
2834 abort ();
2837 if (COMPILEDP (fun))
2838 val = funcall_lambda (fun, numargs, args + 1);
2839 else
2841 if (!CONSP (fun))
2842 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2843 funcar = Fcar (fun);
2844 if (!SYMBOLP (funcar))
2845 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2846 if (EQ (funcar, Qlambda))
2847 val = funcall_lambda (fun, numargs, args + 1);
2848 else if (EQ (funcar, Qautoload))
2850 do_autoload (fun, args[0]);
2851 CHECK_CONS_LIST ();
2852 goto retry;
2854 else
2855 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2857 done:
2858 CHECK_CONS_LIST ();
2859 lisp_eval_depth--;
2860 if (backtrace.debug_on_exit)
2861 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2862 backtrace_list = backtrace.next;
2863 return val;
2866 Lisp_Object
2867 apply_lambda (fun, args, eval_flag)
2868 Lisp_Object fun, args;
2869 int eval_flag;
2871 Lisp_Object args_left;
2872 Lisp_Object numargs;
2873 register Lisp_Object *arg_vector;
2874 struct gcpro gcpro1, gcpro2, gcpro3;
2875 register int i;
2876 register Lisp_Object tem;
2878 numargs = Flength (args);
2879 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2880 args_left = args;
2882 GCPRO3 (*arg_vector, args_left, fun);
2883 gcpro1.nvars = 0;
2885 for (i = 0; i < XINT (numargs);)
2887 tem = Fcar (args_left), args_left = Fcdr (args_left);
2888 if (eval_flag) tem = Feval (tem);
2889 arg_vector[i++] = tem;
2890 gcpro1.nvars = i;
2893 UNGCPRO;
2895 if (eval_flag)
2897 backtrace_list->args = arg_vector;
2898 backtrace_list->nargs = i;
2900 backtrace_list->evalargs = 0;
2901 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2903 /* Do the debug-on-exit now, while arg_vector still exists. */
2904 if (backtrace_list->debug_on_exit)
2905 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2906 /* Don't do it again when we return to eval. */
2907 backtrace_list->debug_on_exit = 0;
2908 return tem;
2911 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2912 and return the result of evaluation.
2913 FUN must be either a lambda-expression or a compiled-code object. */
2915 static Lisp_Object
2916 funcall_lambda (fun, nargs, arg_vector)
2917 Lisp_Object fun;
2918 int nargs;
2919 register Lisp_Object *arg_vector;
2921 Lisp_Object val, syms_left, next;
2922 int count = SPECPDL_INDEX ();
2923 int i, optional, rest;
2925 if (CONSP (fun))
2927 syms_left = XCDR (fun);
2928 if (CONSP (syms_left))
2929 syms_left = XCAR (syms_left);
2930 else
2931 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2933 else if (COMPILEDP (fun))
2934 syms_left = AREF (fun, COMPILED_ARGLIST);
2935 else
2936 abort ();
2938 i = optional = rest = 0;
2939 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2941 QUIT;
2943 next = XCAR (syms_left);
2944 while (!SYMBOLP (next))
2945 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2947 if (EQ (next, Qand_rest))
2948 rest = 1;
2949 else if (EQ (next, Qand_optional))
2950 optional = 1;
2951 else if (rest)
2953 specbind (next, Flist (nargs - i, &arg_vector[i]));
2954 i = nargs;
2956 else if (i < nargs)
2957 specbind (next, arg_vector[i++]);
2958 else if (!optional)
2959 return Fsignal (Qwrong_number_of_arguments,
2960 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2961 else
2962 specbind (next, Qnil);
2965 if (!NILP (syms_left))
2966 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2967 else if (i < nargs)
2968 return Fsignal (Qwrong_number_of_arguments,
2969 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2971 if (CONSP (fun))
2972 val = Fprogn (XCDR (XCDR (fun)));
2973 else
2975 /* If we have not actually read the bytecode string
2976 and constants vector yet, fetch them from the file. */
2977 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2978 Ffetch_bytecode (fun);
2979 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2980 AREF (fun, COMPILED_CONSTANTS),
2981 AREF (fun, COMPILED_STACK_DEPTH));
2984 return unbind_to (count, val);
2987 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2988 1, 1, 0,
2989 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2990 (object)
2991 Lisp_Object object;
2993 Lisp_Object tem;
2995 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2997 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2998 if (!CONSP (tem))
3000 tem = AREF (object, COMPILED_BYTECODE);
3001 if (CONSP (tem) && STRINGP (XCAR (tem)))
3002 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3003 else
3004 error ("Invalid byte code");
3006 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
3007 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
3009 return object;
3012 void
3013 grow_specpdl ()
3015 register int count = SPECPDL_INDEX ();
3016 if (specpdl_size >= max_specpdl_size)
3018 if (max_specpdl_size < 400)
3019 max_specpdl_size = 400;
3020 if (specpdl_size >= max_specpdl_size)
3022 if (!NILP (Vdebug_on_error))
3023 /* Leave room for some specpdl in the debugger. */
3024 max_specpdl_size = specpdl_size + 100;
3025 Fsignal (Qerror,
3026 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
3029 specpdl_size *= 2;
3030 if (specpdl_size > max_specpdl_size)
3031 specpdl_size = max_specpdl_size;
3032 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
3033 specpdl_ptr = specpdl + count;
3036 void
3037 specbind (symbol, value)
3038 Lisp_Object symbol, value;
3040 Lisp_Object ovalue;
3041 Lisp_Object valcontents;
3043 CHECK_SYMBOL (symbol);
3044 if (specpdl_ptr == specpdl + specpdl_size)
3045 grow_specpdl ();
3047 /* The most common case is that of a non-constant symbol with a
3048 trivial value. Make that as fast as we can. */
3049 valcontents = SYMBOL_VALUE (symbol);
3050 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
3052 specpdl_ptr->symbol = symbol;
3053 specpdl_ptr->old_value = valcontents;
3054 specpdl_ptr->func = NULL;
3055 ++specpdl_ptr;
3056 SET_SYMBOL_VALUE (symbol, value);
3058 else
3060 Lisp_Object valcontents;
3062 ovalue = find_symbol_value (symbol);
3063 specpdl_ptr->func = 0;
3064 specpdl_ptr->old_value = ovalue;
3066 valcontents = XSYMBOL (symbol)->value;
3068 if (BUFFER_LOCAL_VALUEP (valcontents)
3069 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
3070 || BUFFER_OBJFWDP (valcontents))
3072 Lisp_Object where, current_buffer;
3074 current_buffer = Fcurrent_buffer ();
3076 /* For a local variable, record both the symbol and which
3077 buffer's or frame's value we are saving. */
3078 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3079 where = current_buffer;
3080 else if (!BUFFER_OBJFWDP (valcontents)
3081 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3082 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3083 else
3084 where = Qnil;
3086 /* We're not using the `unused' slot in the specbinding
3087 structure because this would mean we have to do more
3088 work for simple variables. */
3089 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3091 /* If SYMBOL is a per-buffer variable which doesn't have a
3092 buffer-local value here, make the `let' change the global
3093 value by changing the value of SYMBOL in all buffers not
3094 having their own value. This is consistent with what
3095 happens with other buffer-local variables. */
3096 if (NILP (where)
3097 && BUFFER_OBJFWDP (valcontents))
3099 ++specpdl_ptr;
3100 Fset_default (symbol, value);
3101 return;
3104 else
3105 specpdl_ptr->symbol = symbol;
3107 specpdl_ptr++;
3108 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3109 store_symval_forwarding (symbol, ovalue, value, NULL);
3110 else
3111 set_internal (symbol, value, 0, 1);
3115 void
3116 record_unwind_protect (function, arg)
3117 Lisp_Object (*function) P_ ((Lisp_Object));
3118 Lisp_Object arg;
3120 if (specpdl_ptr == specpdl + specpdl_size)
3121 grow_specpdl ();
3122 specpdl_ptr->func = function;
3123 specpdl_ptr->symbol = Qnil;
3124 specpdl_ptr->old_value = arg;
3125 specpdl_ptr++;
3128 Lisp_Object
3129 unbind_to (count, value)
3130 int count;
3131 Lisp_Object value;
3133 int quitf = !NILP (Vquit_flag);
3134 struct gcpro gcpro1;
3136 GCPRO1 (value);
3137 Vquit_flag = Qnil;
3139 while (specpdl_ptr != specpdl + count)
3141 /* Copy the binding, and decrement specpdl_ptr, before we do
3142 the work to unbind it. We decrement first
3143 so that an error in unbinding won't try to unbind
3144 the same entry again, and we copy the binding first
3145 in case more bindings are made during some of the code we run. */
3147 struct specbinding this_binding;
3148 this_binding = *--specpdl_ptr;
3150 if (this_binding.func != 0)
3151 (*this_binding.func) (this_binding.old_value);
3152 /* If the symbol is a list, it is really (SYMBOL WHERE
3153 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3154 frame. If WHERE is a buffer or frame, this indicates we
3155 bound a variable that had a buffer-local or frame-local
3156 binding. WHERE nil means that the variable had the default
3157 value when it was bound. CURRENT-BUFFER is the buffer that
3158 was current when the variable was bound. */
3159 else if (CONSP (this_binding.symbol))
3161 Lisp_Object symbol, where;
3163 symbol = XCAR (this_binding.symbol);
3164 where = XCAR (XCDR (this_binding.symbol));
3166 if (NILP (where))
3167 Fset_default (symbol, this_binding.old_value);
3168 else if (BUFFERP (where))
3169 set_internal (symbol, this_binding.old_value, XBUFFER (where), 1);
3170 else
3171 set_internal (symbol, this_binding.old_value, NULL, 1);
3173 else
3175 /* If variable has a trivial value (no forwarding), we can
3176 just set it. No need to check for constant symbols here,
3177 since that was already done by specbind. */
3178 if (!MISCP (SYMBOL_VALUE (this_binding.symbol)))
3179 SET_SYMBOL_VALUE (this_binding.symbol, this_binding.old_value);
3180 else
3181 set_internal (this_binding.symbol, this_binding.old_value, 0, 1);
3185 if (NILP (Vquit_flag) && quitf)
3186 Vquit_flag = Qt;
3188 UNGCPRO;
3189 return value;
3192 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3193 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3194 The debugger is entered when that frame exits, if the flag is non-nil. */)
3195 (level, flag)
3196 Lisp_Object level, flag;
3198 register struct backtrace *backlist = backtrace_list;
3199 register int i;
3201 CHECK_NUMBER (level);
3203 for (i = 0; backlist && i < XINT (level); i++)
3205 backlist = backlist->next;
3208 if (backlist)
3209 backlist->debug_on_exit = !NILP (flag);
3211 return flag;
3214 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3215 doc: /* Print a trace of Lisp function calls currently active.
3216 Output stream used is value of `standard-output'. */)
3219 register struct backtrace *backlist = backtrace_list;
3220 register int i;
3221 Lisp_Object tail;
3222 Lisp_Object tem;
3223 extern Lisp_Object Vprint_level;
3224 struct gcpro gcpro1;
3226 XSETFASTINT (Vprint_level, 3);
3228 tail = Qnil;
3229 GCPRO1 (tail);
3231 while (backlist)
3233 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3234 if (backlist->nargs == UNEVALLED)
3236 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3237 write_string ("\n", -1);
3239 else
3241 tem = *backlist->function;
3242 Fprin1 (tem, Qnil); /* This can QUIT */
3243 write_string ("(", -1);
3244 if (backlist->nargs == MANY)
3246 for (tail = *backlist->args, i = 0;
3247 !NILP (tail);
3248 tail = Fcdr (tail), i++)
3250 if (i) write_string (" ", -1);
3251 Fprin1 (Fcar (tail), Qnil);
3254 else
3256 for (i = 0; i < backlist->nargs; i++)
3258 if (i) write_string (" ", -1);
3259 Fprin1 (backlist->args[i], Qnil);
3262 write_string (")\n", -1);
3264 backlist = backlist->next;
3267 Vprint_level = Qnil;
3268 UNGCPRO;
3269 return Qnil;
3272 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3273 doc: /* Return the function and arguments NFRAMES up from current execution point.
3274 If that frame has not evaluated the arguments yet (or is a special form),
3275 the value is (nil FUNCTION ARG-FORMS...).
3276 If that frame has evaluated its arguments and called its function already,
3277 the value is (t FUNCTION ARG-VALUES...).
3278 A &rest arg is represented as the tail of the list ARG-VALUES.
3279 FUNCTION is whatever was supplied as car of evaluated list,
3280 or a lambda expression for macro calls.
3281 If NFRAMES is more than the number of frames, the value is nil. */)
3282 (nframes)
3283 Lisp_Object nframes;
3285 register struct backtrace *backlist = backtrace_list;
3286 register int i;
3287 Lisp_Object tem;
3289 CHECK_NATNUM (nframes);
3291 /* Find the frame requested. */
3292 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3293 backlist = backlist->next;
3295 if (!backlist)
3296 return Qnil;
3297 if (backlist->nargs == UNEVALLED)
3298 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3299 else
3301 if (backlist->nargs == MANY)
3302 tem = *backlist->args;
3303 else
3304 tem = Flist (backlist->nargs, backlist->args);
3306 return Fcons (Qt, Fcons (*backlist->function, tem));
3311 void
3312 mark_backtrace ()
3314 register struct backtrace *backlist;
3315 register int i;
3317 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3319 mark_object (*backlist->function);
3321 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3322 i = 0;
3323 else
3324 i = backlist->nargs - 1;
3325 for (; i >= 0; i--)
3326 mark_object (backlist->args[i]);
3330 void
3331 syms_of_eval ()
3333 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3334 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3335 If Lisp code tries to make more than this many at once,
3336 an error is signaled.
3337 You can safely use a value considerably larger than the default value,
3338 if that proves inconveniently small. However, if you increase it too far,
3339 Emacs could run out of memory trying to make the stack bigger. */);
3341 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3342 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3344 This limit serves to catch infinite recursions for you before they cause
3345 actual stack overflow in C, which would be fatal for Emacs.
3346 You can safely make it considerably larger than its default value,
3347 if that proves inconveniently small. However, if you increase it too far,
3348 Emacs could overflow the real C stack, and crash. */);
3350 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3351 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3352 If the value is t, that means do an ordinary quit.
3353 If the value equals `throw-on-input', that means quit by throwing
3354 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3355 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3356 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3357 Vquit_flag = Qnil;
3359 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3360 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3361 Note that `quit-flag' will still be set by typing C-g,
3362 so a quit will be signaled as soon as `inhibit-quit' is nil.
3363 To prevent this happening, set `quit-flag' to nil
3364 before making `inhibit-quit' nil. */);
3365 Vinhibit_quit = Qnil;
3367 Qinhibit_quit = intern ("inhibit-quit");
3368 staticpro (&Qinhibit_quit);
3370 Qautoload = intern ("autoload");
3371 staticpro (&Qautoload);
3373 Qdebug_on_error = intern ("debug-on-error");
3374 staticpro (&Qdebug_on_error);
3376 Qmacro = intern ("macro");
3377 staticpro (&Qmacro);
3379 Qdeclare = intern ("declare");
3380 staticpro (&Qdeclare);
3382 /* Note that the process handling also uses Qexit, but we don't want
3383 to staticpro it twice, so we just do it here. */
3384 Qexit = intern ("exit");
3385 staticpro (&Qexit);
3387 Qinteractive = intern ("interactive");
3388 staticpro (&Qinteractive);
3390 Qcommandp = intern ("commandp");
3391 staticpro (&Qcommandp);
3393 Qdefun = intern ("defun");
3394 staticpro (&Qdefun);
3396 Qand_rest = intern ("&rest");
3397 staticpro (&Qand_rest);
3399 Qand_optional = intern ("&optional");
3400 staticpro (&Qand_optional);
3402 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3403 doc: /* *Non-nil means errors display a backtrace buffer.
3404 More precisely, this happens for any error that is handled
3405 by the editor command loop.
3406 If the value is a list, an error only means to display a backtrace
3407 if one of its condition symbols appears in the list. */);
3408 Vstack_trace_on_error = Qnil;
3410 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3411 doc: /* *Non-nil means enter debugger if an error is signaled.
3412 Does not apply to errors handled by `condition-case' or those
3413 matched by `debug-ignored-errors'.
3414 If the value is a list, an error only means to enter the debugger
3415 if one of its condition symbols appears in the list.
3416 When you evaluate an expression interactively, this variable
3417 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3418 See also variable `debug-on-quit'. */);
3419 Vdebug_on_error = Qnil;
3421 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3422 doc: /* *List of errors for which the debugger should not be called.
3423 Each element may be a condition-name or a regexp that matches error messages.
3424 If any element applies to a given error, that error skips the debugger
3425 and just returns to top level.
3426 This overrides the variable `debug-on-error'.
3427 It does not apply to errors handled by `condition-case'. */);
3428 Vdebug_ignored_errors = Qnil;
3430 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3431 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3432 Does not apply if quit is handled by a `condition-case'.
3433 When you evaluate an expression interactively, this variable
3434 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3435 debug_on_quit = 0;
3437 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3438 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3440 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3441 doc: /* Non-nil means debugger may continue execution.
3442 This is nil when the debugger is called under circumstances where it
3443 might not be safe to continue. */);
3444 debugger_may_continue = 1;
3446 DEFVAR_LISP ("debugger", &Vdebugger,
3447 doc: /* Function to call to invoke debugger.
3448 If due to frame exit, args are `exit' and the value being returned;
3449 this function's value will be returned instead of that.
3450 If due to error, args are `error' and a list of the args to `signal'.
3451 If due to `apply' or `funcall' entry, one arg, `lambda'.
3452 If due to `eval' entry, one arg, t. */);
3453 Vdebugger = Qnil;
3455 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3456 doc: /* If non-nil, this is a function for `signal' to call.
3457 It receives the same arguments that `signal' was given.
3458 The Edebug package uses this to regain control. */);
3459 Vsignal_hook_function = Qnil;
3461 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3462 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3463 Note that `debug-on-error', `debug-on-quit' and friends
3464 still determine whether to handle the particular condition. */);
3465 Vdebug_on_signal = Qnil;
3467 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3468 doc: /* Function to process declarations in a macro definition.
3469 The function will be called with two args MACRO and DECL.
3470 MACRO is the name of the macro being defined.
3471 DECL is a list `(declare ...)' containing the declarations.
3472 The value the function returns is not used. */);
3473 Vmacro_declaration_function = Qnil;
3475 Vrun_hooks = intern ("run-hooks");
3476 staticpro (&Vrun_hooks);
3478 staticpro (&Vautoload_queue);
3479 Vautoload_queue = Qnil;
3480 staticpro (&Vsignaling_function);
3481 Vsignaling_function = Qnil;
3483 defsubr (&Sor);
3484 defsubr (&Sand);
3485 defsubr (&Sif);
3486 defsubr (&Scond);
3487 defsubr (&Sprogn);
3488 defsubr (&Sprog1);
3489 defsubr (&Sprog2);
3490 defsubr (&Ssetq);
3491 defsubr (&Squote);
3492 defsubr (&Sfunction);
3493 defsubr (&Sdefun);
3494 defsubr (&Sdefmacro);
3495 defsubr (&Sdefvar);
3496 defsubr (&Sdefvaralias);
3497 defsubr (&Sdefconst);
3498 defsubr (&Suser_variable_p);
3499 defsubr (&Slet);
3500 defsubr (&SletX);
3501 defsubr (&Swhile);
3502 defsubr (&Smacroexpand);
3503 defsubr (&Scatch);
3504 defsubr (&Sthrow);
3505 defsubr (&Sunwind_protect);
3506 defsubr (&Scondition_case);
3507 defsubr (&Ssignal);
3508 defsubr (&Sinteractive_p);
3509 defsubr (&Scalled_interactively_p);
3510 defsubr (&Scommandp);
3511 defsubr (&Sautoload);
3512 defsubr (&Seval);
3513 defsubr (&Sapply);
3514 defsubr (&Sfuncall);
3515 defsubr (&Srun_hooks);
3516 defsubr (&Srun_hook_with_args);
3517 defsubr (&Srun_hook_with_args_until_success);
3518 defsubr (&Srun_hook_with_args_until_failure);
3519 defsubr (&Sfetch_bytecode);
3520 defsubr (&Sbacktrace_debug);
3521 defsubr (&Sbacktrace);
3522 defsubr (&Sbacktrace_frame);
3525 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
3526 (do not change this comment) */