(quail-keyboard-layout-alist): Add
[emacs.git] / src / eval.c
blob671123e398333beb9c16049e8e50288c8ecfe6e2
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include <setjmp.h>
31 /* This definition is duplicated in alloc.c and keyboard.c */
32 /* Putting it in lisp.h makes cc bomb out! */
34 struct backtrace
36 struct backtrace *next;
37 Lisp_Object *function;
38 Lisp_Object *args; /* Points to vector of args. */
39 int nargs; /* Length of vector.
40 If nargs is UNEVALLED, args points to slot holding
41 list of unevalled args */
42 char evalargs;
43 /* Nonzero means call value of debugger when done with this operation. */
44 char debug_on_exit;
47 struct backtrace *backtrace_list;
49 /* This structure helps implement the `catch' and `throw' control
50 structure. A struct catchtag contains all the information needed
51 to restore the state of the interpreter after a non-local jump.
53 Handlers for error conditions (represented by `struct handler'
54 structures) just point to a catch tag to do the cleanup required
55 for their jumps.
57 catchtag structures are chained together in the C calling stack;
58 the `next' member points to the next outer catchtag.
60 A call like (throw TAG VAL) searches for a catchtag whose `tag'
61 member is TAG, and then unbinds to it. The `val' member is used to
62 hold VAL while the stack is unwound; `val' is returned as the value
63 of the catch form.
65 All the other members are concerned with restoring the interpreter
66 state. */
68 struct catchtag
70 Lisp_Object tag;
71 Lisp_Object val;
72 struct catchtag *next;
73 struct gcpro *gcpro;
74 jmp_buf jmp;
75 struct backtrace *backlist;
76 struct handler *handlerlist;
77 int lisp_eval_depth;
78 int pdlcount;
79 int poll_suppress_count;
80 struct byte_stack *byte_stack;
83 struct catchtag *catchlist;
85 #ifdef DEBUG_GCPRO
86 /* Count levels of GCPRO to detect failure to UNGCPRO. */
87 int gcpro_level;
88 #endif
90 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
91 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
92 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
93 Lisp_Object Qand_rest, Qand_optional;
94 Lisp_Object Qdebug_on_error;
96 /* This holds either the symbol `run-hooks' or nil.
97 It is nil at an early stage of startup, and when Emacs
98 is shutting down. */
100 Lisp_Object Vrun_hooks;
102 /* Non-nil means record all fset's and provide's, to be undone
103 if the file being autoloaded is not fully loaded.
104 They are recorded by being consed onto the front of Vautoload_queue:
105 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
107 Lisp_Object Vautoload_queue;
109 /* Current number of specbindings allocated in specpdl. */
111 int specpdl_size;
113 /* Pointer to beginning of specpdl. */
115 struct specbinding *specpdl;
117 /* Pointer to first unused element in specpdl. */
119 struct specbinding *specpdl_ptr;
121 /* Maximum size allowed for specpdl allocation */
123 EMACS_INT max_specpdl_size;
125 /* Depth in Lisp evaluations and function calls. */
127 int lisp_eval_depth;
129 /* Maximum allowed depth in Lisp evaluations and function calls. */
131 EMACS_INT max_lisp_eval_depth;
133 /* Nonzero means enter debugger before next function call */
135 int debug_on_next_call;
137 /* Non-zero means debuffer may continue. This is zero when the
138 debugger is called during redisplay, where it might not be safe to
139 continue the interrupted redisplay. */
141 int debugger_may_continue;
143 /* List of conditions (non-nil atom means all) which cause a backtrace
144 if an error is handled by the command loop's error handler. */
146 Lisp_Object Vstack_trace_on_error;
148 /* List of conditions (non-nil atom means all) which enter the debugger
149 if an error is handled by the command loop's error handler. */
151 Lisp_Object Vdebug_on_error;
153 /* List of conditions and regexps specifying error messages which
154 do not enter the debugger even if Vdebug_on_errors says they should. */
156 Lisp_Object Vdebug_ignored_errors;
158 /* Non-nil means call the debugger even if the error will be handled. */
160 Lisp_Object Vdebug_on_signal;
162 /* Hook for edebug to use. */
164 Lisp_Object Vsignal_hook_function;
166 /* Nonzero means enter debugger if a quit signal
167 is handled by the command loop's error handler. */
169 int debug_on_quit;
171 /* The value of num_nonmacro_input_events as of the last time we
172 started to enter the debugger. If we decide to enter the debugger
173 again when this is still equal to num_nonmacro_input_events, then we
174 know that the debugger itself has an error, and we should just
175 signal the error instead of entering an infinite loop of debugger
176 invocations. */
178 int when_entered_debugger;
180 Lisp_Object Vdebugger;
182 /* The function from which the last `signal' was called. Set in
183 Fsignal. */
185 Lisp_Object Vsignaling_function;
187 /* Set to non-zero while processing X events. Checked in Feval to
188 make sure the Lisp interpreter isn't called from a signal handler,
189 which is unsafe because the interpreter isn't reentrant. */
191 int handling_signal;
193 void specbind P_ ((Lisp_Object, Lisp_Object));
194 void record_unwind_protect P_ ((Lisp_Object (*) (Lisp_Object),
195 Lisp_Object));
198 enum run_hooks_condition {to_completion, until_success, until_failure};
200 Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
201 enum run_hooks_condition));
203 Lisp_Object funcall_lambda P_ ((Lisp_Object, int, Lisp_Object *));
204 extern Lisp_Object ml_apply P_ ((Lisp_Object, Lisp_Object)); /* Apply a mocklisp function to unevaluated argument list */
206 void
207 init_eval_once ()
209 specpdl_size = 50;
210 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
211 specpdl_ptr = specpdl;
212 max_specpdl_size = 600;
213 max_lisp_eval_depth = 300;
215 Vrun_hooks = Qnil;
218 void
219 init_eval ()
221 specpdl_ptr = specpdl;
222 catchlist = 0;
223 handlerlist = 0;
224 backtrace_list = 0;
225 Vquit_flag = Qnil;
226 debug_on_next_call = 0;
227 lisp_eval_depth = 0;
228 #ifdef DEBUG_GCPRO
229 gcpro_level = 0;
230 #endif
231 /* This is less than the initial value of num_nonmacro_input_events. */
232 when_entered_debugger = -1;
235 Lisp_Object
236 call_debugger (arg)
237 Lisp_Object arg;
239 int debug_while_redisplaying;
240 int count = specpdl_ptr - specpdl;
241 Lisp_Object val;
243 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
244 max_lisp_eval_depth = lisp_eval_depth + 20;
246 if (specpdl_size + 40 > max_specpdl_size)
247 max_specpdl_size = specpdl_size + 40;
249 #ifdef HAVE_X_WINDOWS
250 if (display_hourglass_p)
251 cancel_hourglass ();
252 #endif
254 debug_on_next_call = 0;
255 when_entered_debugger = num_nonmacro_input_events;
257 /* Resetting redisplaying_p to 0 makes sure that debug output is
258 displayed if the debugger is invoked during redisplay. */
259 debug_while_redisplaying = redisplaying_p;
260 redisplaying_p = 0;
261 specbind (intern ("debugger-may-continue"),
262 debug_while_redisplaying ? Qnil : Qt);
263 specbind (Qinhibit_redisplay, Qnil);
265 #if 0 /* Binding this prevents execution of Lisp code during
266 redisplay, which necessarily leads to display problems. */
267 specbind (Qinhibit_eval_during_redisplay, Qt);
268 #endif
270 val = apply1 (Vdebugger, arg);
272 /* Interrupting redisplay and resuming it later is not safe under
273 all circumstances. So, when the debugger returns, abort the
274 interupted redisplay by going back to the top-level. */
275 if (debug_while_redisplaying)
276 Ftop_level ();
278 return unbind_to (count, val);
281 void
282 do_debug_on_call (code)
283 Lisp_Object code;
285 debug_on_next_call = 0;
286 backtrace_list->debug_on_exit = 1;
287 call_debugger (Fcons (code, Qnil));
290 /* NOTE!!! Every function that can call EVAL must protect its args
291 and temporaries from garbage collection while it needs them.
292 The definition of `For' shows what you have to do. */
294 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
295 "Eval args until one of them yields non-nil, then return that value.\n\
296 The remaining args are not evalled at all.\n\
297 If all args return nil, return nil.")
298 (args)
299 Lisp_Object args;
301 register Lisp_Object val;
302 Lisp_Object args_left;
303 struct gcpro gcpro1;
305 if (NILP(args))
306 return Qnil;
308 args_left = args;
309 GCPRO1 (args_left);
313 val = Feval (Fcar (args_left));
314 if (!NILP (val))
315 break;
316 args_left = Fcdr (args_left);
318 while (!NILP(args_left));
320 UNGCPRO;
321 return val;
324 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
325 "Eval args until one of them yields nil, then return nil.\n\
326 The remaining args are not evalled at all.\n\
327 If no arg yields nil, return the last arg's value.")
328 (args)
329 Lisp_Object args;
331 register Lisp_Object val;
332 Lisp_Object args_left;
333 struct gcpro gcpro1;
335 if (NILP(args))
336 return Qt;
338 args_left = args;
339 GCPRO1 (args_left);
343 val = Feval (Fcar (args_left));
344 if (NILP (val))
345 break;
346 args_left = Fcdr (args_left);
348 while (!NILP(args_left));
350 UNGCPRO;
351 return val;
354 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
355 "If COND yields non-nil, do THEN, else do ELSE...\n\
356 Returns the value of THEN or the value of the last of the ELSE's.\n\
357 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
358 If COND yields nil, and there are no ELSE's, the value is nil.")
359 (args)
360 Lisp_Object args;
362 register Lisp_Object cond;
363 struct gcpro gcpro1;
365 GCPRO1 (args);
366 cond = Feval (Fcar (args));
367 UNGCPRO;
369 if (!NILP (cond))
370 return Feval (Fcar (Fcdr (args)));
371 return Fprogn (Fcdr (Fcdr (args)));
374 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
375 "Try each clause until one succeeds.\n\
376 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
377 and, if the value is non-nil, this clause succeeds:\n\
378 then the expressions in BODY are evaluated and the last one's\n\
379 value is the value of the cond-form.\n\
380 If no clause succeeds, cond returns nil.\n\
381 If a clause has one element, as in (CONDITION),\n\
382 CONDITION's value if non-nil is returned from the cond-form.")
383 (args)
384 Lisp_Object args;
386 register Lisp_Object clause, val;
387 struct gcpro gcpro1;
389 val = Qnil;
390 GCPRO1 (args);
391 while (!NILP (args))
393 clause = Fcar (args);
394 val = Feval (Fcar (clause));
395 if (!NILP (val))
397 if (!EQ (XCDR (clause), Qnil))
398 val = Fprogn (XCDR (clause));
399 break;
401 args = XCDR (args);
403 UNGCPRO;
405 return val;
408 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
409 "Eval BODY forms sequentially and return value of last one.")
410 (args)
411 Lisp_Object args;
413 register Lisp_Object val, tem;
414 Lisp_Object args_left;
415 struct gcpro gcpro1;
417 /* In Mocklisp code, symbols at the front of the progn arglist
418 are to be bound to zero. */
419 if (!EQ (Vmocklisp_arguments, Qt))
421 val = make_number (0);
422 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
424 QUIT;
425 specbind (tem, val), args = Fcdr (args);
429 if (NILP(args))
430 return Qnil;
432 args_left = args;
433 GCPRO1 (args_left);
437 val = Feval (Fcar (args_left));
438 args_left = Fcdr (args_left);
440 while (!NILP(args_left));
442 UNGCPRO;
443 return val;
446 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
447 "Eval FIRST and BODY sequentially; value from FIRST.\n\
448 The value of FIRST is saved during the evaluation of the remaining args,\n\
449 whose values are discarded.")
450 (args)
451 Lisp_Object args;
453 Lisp_Object val;
454 register Lisp_Object args_left;
455 struct gcpro gcpro1, gcpro2;
456 register int argnum = 0;
458 if (NILP(args))
459 return Qnil;
461 args_left = args;
462 val = Qnil;
463 GCPRO2 (args, val);
467 if (!(argnum++))
468 val = Feval (Fcar (args_left));
469 else
470 Feval (Fcar (args_left));
471 args_left = Fcdr (args_left);
473 while (!NILP(args_left));
475 UNGCPRO;
476 return val;
479 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
480 "Eval X, Y and BODY sequentially; value from Y.\n\
481 The value of Y is saved during the evaluation of the remaining args,\n\
482 whose values are discarded.")
483 (args)
484 Lisp_Object args;
486 Lisp_Object val;
487 register Lisp_Object args_left;
488 struct gcpro gcpro1, gcpro2;
489 register int argnum = -1;
491 val = Qnil;
493 if (NILP (args))
494 return Qnil;
496 args_left = args;
497 val = Qnil;
498 GCPRO2 (args, val);
502 if (!(argnum++))
503 val = Feval (Fcar (args_left));
504 else
505 Feval (Fcar (args_left));
506 args_left = Fcdr (args_left);
508 while (!NILP (args_left));
510 UNGCPRO;
511 return val;
514 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
515 "Set each SYM to the value of its VAL.\n\
516 The symbols SYM are variables; they are literal (not evaluated).\n\
517 The values VAL are expressions; they are evaluated.\n\
518 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
519 The second VAL is not computed until after the first SYM is set, and so on;\n\
520 each VAL can use the new value of variables set earlier in the `setq'.\n\
521 The return value of the `setq' form is the value of the last VAL.")
522 (args)
523 Lisp_Object args;
525 register Lisp_Object args_left;
526 register Lisp_Object val, sym;
527 struct gcpro gcpro1;
529 if (NILP(args))
530 return Qnil;
532 args_left = args;
533 GCPRO1 (args);
537 val = Feval (Fcar (Fcdr (args_left)));
538 sym = Fcar (args_left);
539 Fset (sym, val);
540 args_left = Fcdr (Fcdr (args_left));
542 while (!NILP(args_left));
544 UNGCPRO;
545 return val;
548 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
549 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
550 (args)
551 Lisp_Object args;
553 return Fcar (args);
556 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
557 "Like `quote', but preferred for objects which are functions.\n\
558 In byte compilation, `function' causes its argument to be compiled.\n\
559 `quote' cannot do that.")
560 (args)
561 Lisp_Object args;
563 return Fcar (args);
567 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
568 "Return t if function in which this appears was called interactively.\n\
569 This means that the function was called with call-interactively (which\n\
570 includes being called as the binding of a key)\n\
571 and input is currently coming from the keyboard (not in keyboard macro).")
574 return interactive_p (1) ? Qt : Qnil;
578 /* Return 1 if function in which this appears was called
579 interactively. This means that the function was called with
580 call-interactively (which includes being called as the binding of
581 a key) and input is currently coming from the keyboard (not in
582 keyboard macro).
584 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
585 called is a built-in. */
588 interactive_p (exclude_subrs_p)
589 int exclude_subrs_p;
591 struct backtrace *btp;
592 Lisp_Object fun;
594 if (!INTERACTIVE)
595 return 0;
597 btp = backtrace_list;
599 /* If this isn't a byte-compiled function, there may be a frame at
600 the top for Finteractive_p. If so, skip it. */
601 fun = Findirect_function (*btp->function);
602 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
603 btp = btp->next;
605 /* If we're running an Emacs 18-style byte-compiled function, there
606 may be a frame for Fbytecode. Now, given the strictest
607 definition, this function isn't really being called
608 interactively, but because that's the way Emacs 18 always builds
609 byte-compiled functions, we'll accept it for now. */
610 if (EQ (*btp->function, Qbytecode))
611 btp = btp->next;
613 /* If this isn't a byte-compiled function, then we may now be
614 looking at several frames for special forms. Skip past them. */
615 while (btp &&
616 btp->nargs == UNEVALLED)
617 btp = btp->next;
619 /* btp now points at the frame of the innermost function that isn't
620 a special form, ignoring frames for Finteractive_p and/or
621 Fbytecode at the top. If this frame is for a built-in function
622 (such as load or eval-region) return nil. */
623 fun = Findirect_function (*btp->function);
624 if (exclude_subrs_p && SUBRP (fun))
625 return 0;
627 /* btp points to the frame of a Lisp function that called interactive-p.
628 Return t if that function was called interactively. */
629 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
630 return 1;
631 return 0;
635 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
636 "Define NAME as a function.\n\
637 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
638 See also the function `interactive'.")
639 (args)
640 Lisp_Object args;
642 register Lisp_Object fn_name;
643 register Lisp_Object defn;
645 fn_name = Fcar (args);
646 defn = Fcons (Qlambda, Fcdr (args));
647 if (!NILP (Vpurify_flag))
648 defn = Fpurecopy (defn);
649 Ffset (fn_name, defn);
650 LOADHIST_ATTACH (fn_name);
651 return fn_name;
654 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
655 "Define NAME as a macro.\n\
656 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
657 When the macro is called, as in (NAME ARGS...),\n\
658 the function (lambda ARGLIST BODY...) is applied to\n\
659 the list ARGS... as it appears in the expression,\n\
660 and the result should be a form to be evaluated instead of the original.")
661 (args)
662 Lisp_Object args;
664 register Lisp_Object fn_name;
665 register Lisp_Object defn;
667 fn_name = Fcar (args);
668 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
669 if (!NILP (Vpurify_flag))
670 defn = Fpurecopy (defn);
671 Ffset (fn_name, defn);
672 LOADHIST_ATTACH (fn_name);
673 return fn_name;
676 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
677 "Define SYMBOL as a variable.\n\
678 You are not required to define a variable in order to use it,\n\
679 but the definition can supply documentation and an initial value\n\
680 in a way that tags can recognize.\n\n\
681 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
682 If SYMBOL is buffer-local, its default value is what is set;\n\
683 buffer-local values are not affected.\n\
684 INITVALUE and DOCSTRING are optional.\n\
685 If DOCSTRING starts with *, this variable is identified as a user option.\n\
686 This means that M-x set-variable recognizes it.\n\
687 See also `user-variable-p'.\n\
688 If INITVALUE is missing, SYMBOL's value is not set.")
689 (args)
690 Lisp_Object args;
692 register Lisp_Object sym, tem, tail;
694 sym = Fcar (args);
695 tail = Fcdr (args);
696 if (!NILP (Fcdr (Fcdr (tail))))
697 error ("too many arguments");
699 tem = Fdefault_boundp (sym);
700 if (!NILP (tail))
702 if (NILP (tem))
703 Fset_default (sym, Feval (Fcar (tail)));
704 tail = Fcdr (tail);
705 if (!NILP (Fcar (tail)))
707 tem = Fcar (tail);
708 if (!NILP (Vpurify_flag))
709 tem = Fpurecopy (tem);
710 Fput (sym, Qvariable_documentation, tem);
712 LOADHIST_ATTACH (sym);
714 else
715 /* A (defvar <var>) should not take precedence in the load-history over
716 an earlier (defvar <var> <val>), so only add to history if the default
717 value is still unbound. */
718 if (NILP (tem))
719 LOADHIST_ATTACH (sym);
721 return sym;
724 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
725 "Define SYMBOL as a constant variable.\n\
726 The intent is that neither programs nor users should ever change this value.\n\
727 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
728 If SYMBOL is buffer-local, its default value is what is set;\n\
729 buffer-local values are not affected.\n\
730 DOCSTRING is optional.")
731 (args)
732 Lisp_Object args;
734 register Lisp_Object sym, tem;
736 sym = Fcar (args);
737 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
738 error ("too many arguments");
740 tem = Feval (Fcar (Fcdr (args)));
741 if (!NILP (Vpurify_flag))
742 tem = Fpurecopy (tem);
743 Fset_default (sym, tem);
744 tem = Fcar (Fcdr (Fcdr (args)));
745 if (!NILP (tem))
747 if (!NILP (Vpurify_flag))
748 tem = Fpurecopy (tem);
749 Fput (sym, Qvariable_documentation, tem);
751 LOADHIST_ATTACH (sym);
752 return sym;
755 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
756 "Returns t if VARIABLE is intended to be set and modified by users.\n\
757 \(The alternative is a variable used internally in a Lisp program.)\n\
758 Determined by whether the first character of the documentation\n\
759 for the variable is `*' or if the variable is customizable (has a non-nil\n\
760 value of any of `custom-type', `custom-loads' or `standard-value'\n\
761 on its property list).")
762 (variable)
763 Lisp_Object variable;
765 Lisp_Object documentation;
767 if (!SYMBOLP (variable))
768 return Qnil;
770 documentation = Fget (variable, Qvariable_documentation);
771 if (INTEGERP (documentation) && XINT (documentation) < 0)
772 return Qt;
773 if (STRINGP (documentation)
774 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
775 return Qt;
776 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
777 if (CONSP (documentation)
778 && STRINGP (XCAR (documentation))
779 && INTEGERP (XCDR (documentation))
780 && XINT (XCDR (documentation)) < 0)
781 return Qt;
782 /* Customizable? */
783 if ((!NILP (Fget (variable, intern ("custom-type"))))
784 || (!NILP (Fget (variable, intern ("custom-loads"))))
785 || (!NILP (Fget (variable, intern ("standard-value")))))
786 return Qt;
787 return Qnil;
790 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
791 "Bind variables according to VARLIST then eval BODY.\n\
792 The value of the last form in BODY is returned.\n\
793 Each element of VARLIST is a symbol (which is bound to nil)\n\
794 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
795 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
796 (args)
797 Lisp_Object args;
799 Lisp_Object varlist, val, elt;
800 int count = specpdl_ptr - specpdl;
801 struct gcpro gcpro1, gcpro2, gcpro3;
803 GCPRO3 (args, elt, varlist);
805 varlist = Fcar (args);
806 while (!NILP (varlist))
808 QUIT;
809 elt = Fcar (varlist);
810 if (SYMBOLP (elt))
811 specbind (elt, Qnil);
812 else if (! NILP (Fcdr (Fcdr (elt))))
813 Fsignal (Qerror,
814 Fcons (build_string ("`let' bindings can have only one value-form"),
815 elt));
816 else
818 val = Feval (Fcar (Fcdr (elt)));
819 specbind (Fcar (elt), val);
821 varlist = Fcdr (varlist);
823 UNGCPRO;
824 val = Fprogn (Fcdr (args));
825 return unbind_to (count, val);
828 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
829 "Bind variables according to VARLIST then eval BODY.\n\
830 The value of the last form in BODY is returned.\n\
831 Each element of VARLIST is a symbol (which is bound to nil)\n\
832 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
833 All the VALUEFORMs are evalled before any symbols are bound.")
834 (args)
835 Lisp_Object args;
837 Lisp_Object *temps, tem;
838 register Lisp_Object elt, varlist;
839 int count = specpdl_ptr - specpdl;
840 register int argnum;
841 struct gcpro gcpro1, gcpro2;
843 varlist = Fcar (args);
845 /* Make space to hold the values to give the bound variables */
846 elt = Flength (varlist);
847 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
849 /* Compute the values and store them in `temps' */
851 GCPRO2 (args, *temps);
852 gcpro2.nvars = 0;
854 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
856 QUIT;
857 elt = Fcar (varlist);
858 if (SYMBOLP (elt))
859 temps [argnum++] = Qnil;
860 else if (! NILP (Fcdr (Fcdr (elt))))
861 Fsignal (Qerror,
862 Fcons (build_string ("`let' bindings can have only one value-form"),
863 elt));
864 else
865 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
866 gcpro2.nvars = argnum;
868 UNGCPRO;
870 varlist = Fcar (args);
871 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
873 elt = Fcar (varlist);
874 tem = temps[argnum++];
875 if (SYMBOLP (elt))
876 specbind (elt, tem);
877 else
878 specbind (Fcar (elt), tem);
881 elt = Fprogn (Fcdr (args));
882 return unbind_to (count, elt);
885 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
886 "If TEST yields non-nil, eval BODY... and repeat.\n\
887 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
888 until TEST returns nil.")
889 (args)
890 Lisp_Object args;
892 Lisp_Object test, body, tem;
893 struct gcpro gcpro1, gcpro2;
895 GCPRO2 (test, body);
897 test = Fcar (args);
898 body = Fcdr (args);
899 while (tem = Feval (test),
900 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
902 QUIT;
903 Fprogn (body);
906 UNGCPRO;
907 return Qnil;
910 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
911 "Return result of expanding macros at top level of FORM.\n\
912 If FORM is not a macro call, it is returned unchanged.\n\
913 Otherwise, the macro is expanded and the expansion is considered\n\
914 in place of FORM. When a non-macro-call results, it is returned.\n\n\
915 The second optional arg ENVIRONMENT specifies an environment of macro\n\
916 definitions to shadow the loaded ones for use in file byte-compilation.")
917 (form, environment)
918 Lisp_Object form;
919 Lisp_Object environment;
921 /* With cleanups from Hallvard Furuseth. */
922 register Lisp_Object expander, sym, def, tem;
924 while (1)
926 /* Come back here each time we expand a macro call,
927 in case it expands into another macro call. */
928 if (!CONSP (form))
929 break;
930 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
931 def = sym = XCAR (form);
932 tem = Qnil;
933 /* Trace symbols aliases to other symbols
934 until we get a symbol that is not an alias. */
935 while (SYMBOLP (def))
937 QUIT;
938 sym = def;
939 tem = Fassq (sym, environment);
940 if (NILP (tem))
942 def = XSYMBOL (sym)->function;
943 if (!EQ (def, Qunbound))
944 continue;
946 break;
948 /* Right now TEM is the result from SYM in ENVIRONMENT,
949 and if TEM is nil then DEF is SYM's function definition. */
950 if (NILP (tem))
952 /* SYM is not mentioned in ENVIRONMENT.
953 Look at its function definition. */
954 if (EQ (def, Qunbound) || !CONSP (def))
955 /* Not defined or definition not suitable */
956 break;
957 if (EQ (XCAR (def), Qautoload))
959 /* Autoloading function: will it be a macro when loaded? */
960 tem = Fnth (make_number (4), def);
961 if (EQ (tem, Qt) || EQ (tem, Qmacro))
962 /* Yes, load it and try again. */
964 struct gcpro gcpro1;
965 GCPRO1 (form);
966 do_autoload (def, sym);
967 UNGCPRO;
968 continue;
970 else
971 break;
973 else if (!EQ (XCAR (def), Qmacro))
974 break;
975 else expander = XCDR (def);
977 else
979 expander = XCDR (tem);
980 if (NILP (expander))
981 break;
983 form = apply1 (expander, XCDR (form));
985 return form;
988 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
989 "Eval BODY allowing nonlocal exits using `throw'.\n\
990 TAG is evalled to get the tag to use; it must not be nil.\n\
992 Then the BODY is executed.\n\
993 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
994 If no throw happens, `catch' returns the value of the last BODY form.\n\
995 If a throw happens, it specifies the value to return from `catch'.")
996 (args)
997 Lisp_Object args;
999 register Lisp_Object tag;
1000 struct gcpro gcpro1;
1002 GCPRO1 (args);
1003 tag = Feval (Fcar (args));
1004 UNGCPRO;
1005 return internal_catch (tag, Fprogn, Fcdr (args));
1008 /* Set up a catch, then call C function FUNC on argument ARG.
1009 FUNC should return a Lisp_Object.
1010 This is how catches are done from within C code. */
1012 Lisp_Object
1013 internal_catch (tag, func, arg)
1014 Lisp_Object tag;
1015 Lisp_Object (*func) ();
1016 Lisp_Object arg;
1018 /* This structure is made part of the chain `catchlist'. */
1019 struct catchtag c;
1021 /* Fill in the components of c, and put it on the list. */
1022 c.next = catchlist;
1023 c.tag = tag;
1024 c.val = Qnil;
1025 c.backlist = backtrace_list;
1026 c.handlerlist = handlerlist;
1027 c.lisp_eval_depth = lisp_eval_depth;
1028 c.pdlcount = specpdl_ptr - specpdl;
1029 c.poll_suppress_count = poll_suppress_count;
1030 c.gcpro = gcprolist;
1031 c.byte_stack = byte_stack_list;
1032 catchlist = &c;
1034 /* Call FUNC. */
1035 if (! _setjmp (c.jmp))
1036 c.val = (*func) (arg);
1038 /* Throw works by a longjmp that comes right here. */
1039 catchlist = c.next;
1040 return c.val;
1043 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1044 jump to that CATCH, returning VALUE as the value of that catch.
1046 This is the guts Fthrow and Fsignal; they differ only in the way
1047 they choose the catch tag to throw to. A catch tag for a
1048 condition-case form has a TAG of Qnil.
1050 Before each catch is discarded, unbind all special bindings and
1051 execute all unwind-protect clauses made above that catch. Unwind
1052 the handler stack as we go, so that the proper handlers are in
1053 effect for each unwind-protect clause we run. At the end, restore
1054 some static info saved in CATCH, and longjmp to the location
1055 specified in the
1057 This is used for correct unwinding in Fthrow and Fsignal. */
1059 static void
1060 unwind_to_catch (catch, value)
1061 struct catchtag *catch;
1062 Lisp_Object value;
1064 register int last_time;
1066 /* Save the value in the tag. */
1067 catch->val = value;
1069 /* Restore the polling-suppression count. */
1070 set_poll_suppress_count (catch->poll_suppress_count);
1074 last_time = catchlist == catch;
1076 /* Unwind the specpdl stack, and then restore the proper set of
1077 handlers. */
1078 unbind_to (catchlist->pdlcount, Qnil);
1079 handlerlist = catchlist->handlerlist;
1080 catchlist = catchlist->next;
1082 while (! last_time);
1084 byte_stack_list = catch->byte_stack;
1085 gcprolist = catch->gcpro;
1086 #ifdef DEBUG_GCPRO
1087 if (gcprolist != 0)
1088 gcpro_level = gcprolist->level + 1;
1089 else
1090 gcpro_level = 0;
1091 #endif
1092 backtrace_list = catch->backlist;
1093 lisp_eval_depth = catch->lisp_eval_depth;
1095 _longjmp (catch->jmp, 1);
1098 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1099 "Throw to the catch for TAG and return VALUE from it.\n\
1100 Both TAG and VALUE are evalled.")
1101 (tag, value)
1102 register Lisp_Object tag, value;
1104 register struct catchtag *c;
1106 while (1)
1108 if (!NILP (tag))
1109 for (c = catchlist; c; c = c->next)
1111 if (EQ (c->tag, tag))
1112 unwind_to_catch (c, value);
1114 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1119 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1120 "Do BODYFORM, protecting with UNWINDFORMS.\n\
1121 If BODYFORM completes normally, its value is returned\n\
1122 after executing the UNWINDFORMS.\n\
1123 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1124 (args)
1125 Lisp_Object args;
1127 Lisp_Object val;
1128 int count = specpdl_ptr - specpdl;
1130 record_unwind_protect (0, Fcdr (args));
1131 val = Feval (Fcar (args));
1132 return unbind_to (count, val);
1135 /* Chain of condition handlers currently in effect.
1136 The elements of this chain are contained in the stack frames
1137 of Fcondition_case and internal_condition_case.
1138 When an error is signaled (by calling Fsignal, below),
1139 this chain is searched for an element that applies. */
1141 struct handler *handlerlist;
1143 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1144 "Regain control when an error is signaled.\n\
1145 executes BODYFORM and returns its value if no error happens.\n\
1146 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1147 where the BODY is made of Lisp expressions.\n\n\
1148 A handler is applicable to an error\n\
1149 if CONDITION-NAME is one of the error's condition names.\n\
1150 If an error happens, the first applicable handler is run.\n\
1152 The car of a handler may be a list of condition names\n\
1153 instead of a single condition name.\n\
1155 When a handler handles an error,\n\
1156 control returns to the condition-case and the handler BODY... is executed\n\
1157 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1158 VAR may be nil; then you do not get access to the signal information.\n\
1160 The value of the last BODY form is returned from the condition-case.\n\
1161 See also the function `signal' for more info.")
1162 (args)
1163 Lisp_Object args;
1165 Lisp_Object val;
1166 struct catchtag c;
1167 struct handler h;
1168 register Lisp_Object bodyform, handlers;
1169 volatile Lisp_Object var;
1171 var = Fcar (args);
1172 bodyform = Fcar (Fcdr (args));
1173 handlers = Fcdr (Fcdr (args));
1174 CHECK_SYMBOL (var, 0);
1176 for (val = handlers; ! NILP (val); val = Fcdr (val))
1178 Lisp_Object tem;
1179 tem = Fcar (val);
1180 if (! (NILP (tem)
1181 || (CONSP (tem)
1182 && (SYMBOLP (XCAR (tem))
1183 || CONSP (XCAR (tem))))))
1184 error ("Invalid condition handler", tem);
1187 c.tag = Qnil;
1188 c.val = Qnil;
1189 c.backlist = backtrace_list;
1190 c.handlerlist = handlerlist;
1191 c.lisp_eval_depth = lisp_eval_depth;
1192 c.pdlcount = specpdl_ptr - specpdl;
1193 c.poll_suppress_count = poll_suppress_count;
1194 c.gcpro = gcprolist;
1195 c.byte_stack = byte_stack_list;
1196 if (_setjmp (c.jmp))
1198 if (!NILP (h.var))
1199 specbind (h.var, c.val);
1200 val = Fprogn (Fcdr (h.chosen_clause));
1202 /* Note that this just undoes the binding of h.var; whoever
1203 longjumped to us unwound the stack to c.pdlcount before
1204 throwing. */
1205 unbind_to (c.pdlcount, Qnil);
1206 return val;
1208 c.next = catchlist;
1209 catchlist = &c;
1211 h.var = var;
1212 h.handler = handlers;
1213 h.next = handlerlist;
1214 h.tag = &c;
1215 handlerlist = &h;
1217 val = Feval (bodyform);
1218 catchlist = c.next;
1219 handlerlist = h.next;
1220 return val;
1223 /* Call the function BFUN with no arguments, catching errors within it
1224 according to HANDLERS. If there is an error, call HFUN with
1225 one argument which is the data that describes the error:
1226 (SIGNALNAME . DATA)
1228 HANDLERS can be a list of conditions to catch.
1229 If HANDLERS is Qt, catch all errors.
1230 If HANDLERS is Qerror, catch all errors
1231 but allow the debugger to run if that is enabled. */
1233 Lisp_Object
1234 internal_condition_case (bfun, handlers, hfun)
1235 Lisp_Object (*bfun) ();
1236 Lisp_Object handlers;
1237 Lisp_Object (*hfun) ();
1239 Lisp_Object val;
1240 struct catchtag c;
1241 struct handler h;
1243 #if 0 /* Can't do this check anymore because realize_basic_faces has
1244 to BLOCK_INPUT, and can call Lisp. What's really needed is a
1245 flag indicating that we're currently handling a signal. */
1246 /* Since Fsignal resets this to 0, it had better be 0 now
1247 or else we have a potential bug. */
1248 if (interrupt_input_blocked != 0)
1249 abort ();
1250 #endif
1252 c.tag = Qnil;
1253 c.val = Qnil;
1254 c.backlist = backtrace_list;
1255 c.handlerlist = handlerlist;
1256 c.lisp_eval_depth = lisp_eval_depth;
1257 c.pdlcount = specpdl_ptr - specpdl;
1258 c.poll_suppress_count = poll_suppress_count;
1259 c.gcpro = gcprolist;
1260 c.byte_stack = byte_stack_list;
1261 if (_setjmp (c.jmp))
1263 return (*hfun) (c.val);
1265 c.next = catchlist;
1266 catchlist = &c;
1267 h.handler = handlers;
1268 h.var = Qnil;
1269 h.next = handlerlist;
1270 h.tag = &c;
1271 handlerlist = &h;
1273 val = (*bfun) ();
1274 catchlist = c.next;
1275 handlerlist = h.next;
1276 return val;
1279 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1281 Lisp_Object
1282 internal_condition_case_1 (bfun, arg, handlers, hfun)
1283 Lisp_Object (*bfun) ();
1284 Lisp_Object arg;
1285 Lisp_Object handlers;
1286 Lisp_Object (*hfun) ();
1288 Lisp_Object val;
1289 struct catchtag c;
1290 struct handler h;
1292 c.tag = Qnil;
1293 c.val = Qnil;
1294 c.backlist = backtrace_list;
1295 c.handlerlist = handlerlist;
1296 c.lisp_eval_depth = lisp_eval_depth;
1297 c.pdlcount = specpdl_ptr - specpdl;
1298 c.poll_suppress_count = poll_suppress_count;
1299 c.gcpro = gcprolist;
1300 c.byte_stack = byte_stack_list;
1301 if (_setjmp (c.jmp))
1303 return (*hfun) (c.val);
1305 c.next = catchlist;
1306 catchlist = &c;
1307 h.handler = handlers;
1308 h.var = Qnil;
1309 h.next = handlerlist;
1310 h.tag = &c;
1311 handlerlist = &h;
1313 val = (*bfun) (arg);
1314 catchlist = c.next;
1315 handlerlist = h.next;
1316 return val;
1320 /* Like internal_condition_case but call HFUN with NARGS as first,
1321 and ARGS as second argument. */
1323 Lisp_Object
1324 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1325 Lisp_Object (*bfun) ();
1326 int nargs;
1327 Lisp_Object *args;
1328 Lisp_Object handlers;
1329 Lisp_Object (*hfun) ();
1331 Lisp_Object val;
1332 struct catchtag c;
1333 struct handler h;
1335 c.tag = Qnil;
1336 c.val = Qnil;
1337 c.backlist = backtrace_list;
1338 c.handlerlist = handlerlist;
1339 c.lisp_eval_depth = lisp_eval_depth;
1340 c.pdlcount = specpdl_ptr - specpdl;
1341 c.poll_suppress_count = poll_suppress_count;
1342 c.gcpro = gcprolist;
1343 c.byte_stack = byte_stack_list;
1344 if (_setjmp (c.jmp))
1346 return (*hfun) (c.val);
1348 c.next = catchlist;
1349 catchlist = &c;
1350 h.handler = handlers;
1351 h.var = Qnil;
1352 h.next = handlerlist;
1353 h.tag = &c;
1354 handlerlist = &h;
1356 val = (*bfun) (nargs, args);
1357 catchlist = c.next;
1358 handlerlist = h.next;
1359 return val;
1363 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1364 Lisp_Object, Lisp_Object,
1365 Lisp_Object *));
1367 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1368 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1369 This function does not return.\n\n\
1370 An error symbol is a symbol with an `error-conditions' property\n\
1371 that is a list of condition names.\n\
1372 A handler for any of those names will get to handle this signal.\n\
1373 The symbol `error' should normally be one of them.\n\
1375 DATA should be a list. Its elements are printed as part of the error message.\n\
1376 If the signal is handled, DATA is made available to the handler.\n\
1377 See also the function `condition-case'.")
1378 (error_symbol, data)
1379 Lisp_Object error_symbol, data;
1381 /* When memory is full, ERROR-SYMBOL is nil,
1382 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1383 register struct handler *allhandlers = handlerlist;
1384 Lisp_Object conditions;
1385 extern int gc_in_progress;
1386 extern int waiting_for_input;
1387 Lisp_Object debugger_value;
1388 Lisp_Object string;
1389 Lisp_Object real_error_symbol;
1390 extern int display_hourglass_p;
1391 struct backtrace *bp;
1393 immediate_quit = handling_signal = 0;
1394 if (gc_in_progress || waiting_for_input)
1395 abort ();
1397 TOTALLY_UNBLOCK_INPUT;
1399 if (NILP (error_symbol))
1400 real_error_symbol = Fcar (data);
1401 else
1402 real_error_symbol = error_symbol;
1404 #ifdef HAVE_X_WINDOWS
1405 if (display_hourglass_p)
1406 cancel_hourglass ();
1407 #endif
1409 /* This hook is used by edebug. */
1410 if (! NILP (Vsignal_hook_function))
1411 call2 (Vsignal_hook_function, error_symbol, data);
1413 conditions = Fget (real_error_symbol, Qerror_conditions);
1415 /* Remember from where signal was called. Skip over the frame for
1416 `signal' itself. If a frame for `error' follows, skip that,
1417 too. */
1418 Vsignaling_function = Qnil;
1419 if (backtrace_list)
1421 bp = backtrace_list->next;
1422 if (bp && bp->function && EQ (*bp->function, Qerror))
1423 bp = bp->next;
1424 if (bp && bp->function)
1425 Vsignaling_function = *bp->function;
1428 for (; handlerlist; handlerlist = handlerlist->next)
1430 register Lisp_Object clause;
1432 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1433 max_lisp_eval_depth = lisp_eval_depth + 20;
1435 if (specpdl_size + 40 > max_specpdl_size)
1436 max_specpdl_size = specpdl_size + 40;
1438 clause = find_handler_clause (handlerlist->handler, conditions,
1439 error_symbol, data, &debugger_value);
1441 #if 0 /* Most callers are not prepared to handle gc if this returns.
1442 So, since this feature is not very useful, take it out. */
1443 /* If have called debugger and user wants to continue,
1444 just return nil. */
1445 if (EQ (clause, Qlambda))
1446 return debugger_value;
1447 #else
1448 if (EQ (clause, Qlambda))
1450 /* We can't return values to code which signaled an error, but we
1451 can continue code which has signaled a quit. */
1452 if (EQ (real_error_symbol, Qquit))
1453 return Qnil;
1454 else
1455 error ("Cannot return from the debugger in an error");
1457 #endif
1459 if (!NILP (clause))
1461 Lisp_Object unwind_data;
1462 struct handler *h = handlerlist;
1464 handlerlist = allhandlers;
1466 if (NILP (error_symbol))
1467 unwind_data = data;
1468 else
1469 unwind_data = Fcons (error_symbol, data);
1470 h->chosen_clause = clause;
1471 unwind_to_catch (h->tag, unwind_data);
1475 handlerlist = allhandlers;
1476 /* If no handler is present now, try to run the debugger,
1477 and if that fails, throw to top level. */
1478 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1479 if (catchlist != 0)
1480 Fthrow (Qtop_level, Qt);
1482 if (! NILP (error_symbol))
1483 data = Fcons (error_symbol, data);
1485 string = Ferror_message_string (data);
1486 fatal ("%s", XSTRING (string)->data, 0);
1489 /* Return nonzero iff LIST is a non-nil atom or
1490 a list containing one of CONDITIONS. */
1492 static int
1493 wants_debugger (list, conditions)
1494 Lisp_Object list, conditions;
1496 if (NILP (list))
1497 return 0;
1498 if (! CONSP (list))
1499 return 1;
1501 while (CONSP (conditions))
1503 Lisp_Object this, tail;
1504 this = XCAR (conditions);
1505 for (tail = list; CONSP (tail); tail = XCDR (tail))
1506 if (EQ (XCAR (tail), this))
1507 return 1;
1508 conditions = XCDR (conditions);
1510 return 0;
1513 /* Return 1 if an error with condition-symbols CONDITIONS,
1514 and described by SIGNAL-DATA, should skip the debugger
1515 according to debugger-ignore-errors. */
1517 static int
1518 skip_debugger (conditions, data)
1519 Lisp_Object conditions, data;
1521 Lisp_Object tail;
1522 int first_string = 1;
1523 Lisp_Object error_message;
1525 error_message = Qnil;
1526 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1528 if (STRINGP (XCAR (tail)))
1530 if (first_string)
1532 error_message = Ferror_message_string (data);
1533 first_string = 0;
1536 if (fast_string_match (XCAR (tail), error_message) >= 0)
1537 return 1;
1539 else
1541 Lisp_Object contail;
1543 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1544 if (EQ (XCAR (tail), XCAR (contail)))
1545 return 1;
1549 return 0;
1552 /* Value of Qlambda means we have called debugger and user has continued.
1553 There are two ways to pass SIG and DATA:
1554 = SIG is the error symbol, and DATA is the rest of the data.
1555 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1556 This is for memory-full errors only.
1558 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1560 static Lisp_Object
1561 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1562 Lisp_Object handlers, conditions, sig, data;
1563 Lisp_Object *debugger_value_ptr;
1565 register Lisp_Object h;
1566 register Lisp_Object tem;
1568 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1569 return Qt;
1570 /* error is used similarly, but means print an error message
1571 and run the debugger if that is enabled. */
1572 if (EQ (handlers, Qerror)
1573 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1574 there is a handler. */
1576 int count = specpdl_ptr - specpdl;
1577 int debugger_called = 0;
1578 Lisp_Object sig_symbol, combined_data;
1579 /* This is set to 1 if we are handling a memory-full error,
1580 because these must not run the debugger.
1581 (There is no room in memory to do that!) */
1582 int no_debugger = 0;
1584 if (NILP (sig))
1586 combined_data = data;
1587 sig_symbol = Fcar (data);
1588 no_debugger = 1;
1590 else
1592 combined_data = Fcons (sig, data);
1593 sig_symbol = sig;
1596 if (wants_debugger (Vstack_trace_on_error, conditions))
1598 #ifdef PROTOTYPES
1599 internal_with_output_to_temp_buffer ("*Backtrace*",
1600 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1601 Qnil);
1602 #else
1603 internal_with_output_to_temp_buffer ("*Backtrace*",
1604 Fbacktrace, Qnil);
1605 #endif
1607 if (! no_debugger
1608 && (EQ (sig_symbol, Qquit)
1609 ? debug_on_quit
1610 : wants_debugger (Vdebug_on_error, conditions))
1611 && ! skip_debugger (conditions, combined_data)
1612 && when_entered_debugger < num_nonmacro_input_events)
1614 specbind (Qdebug_on_error, Qnil);
1615 *debugger_value_ptr
1616 = call_debugger (Fcons (Qerror,
1617 Fcons (combined_data, Qnil)));
1618 debugger_called = 1;
1620 /* If there is no handler, return saying whether we ran the debugger. */
1621 if (EQ (handlers, Qerror))
1623 if (debugger_called)
1624 return unbind_to (count, Qlambda);
1625 return Qt;
1628 for (h = handlers; CONSP (h); h = Fcdr (h))
1630 Lisp_Object handler, condit;
1632 handler = Fcar (h);
1633 if (!CONSP (handler))
1634 continue;
1635 condit = Fcar (handler);
1636 /* Handle a single condition name in handler HANDLER. */
1637 if (SYMBOLP (condit))
1639 tem = Fmemq (Fcar (handler), conditions);
1640 if (!NILP (tem))
1641 return handler;
1643 /* Handle a list of condition names in handler HANDLER. */
1644 else if (CONSP (condit))
1646 while (CONSP (condit))
1648 tem = Fmemq (Fcar (condit), conditions);
1649 if (!NILP (tem))
1650 return handler;
1651 condit = XCDR (condit);
1655 return Qnil;
1658 /* dump an error message; called like printf */
1660 /* VARARGS 1 */
1661 void
1662 error (m, a1, a2, a3)
1663 char *m;
1664 char *a1, *a2, *a3;
1666 char buf[200];
1667 int size = 200;
1668 int mlen;
1669 char *buffer = buf;
1670 char *args[3];
1671 int allocated = 0;
1672 Lisp_Object string;
1674 args[0] = a1;
1675 args[1] = a2;
1676 args[2] = a3;
1678 mlen = strlen (m);
1680 while (1)
1682 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1683 if (used < size)
1684 break;
1685 size *= 2;
1686 if (allocated)
1687 buffer = (char *) xrealloc (buffer, size);
1688 else
1690 buffer = (char *) xmalloc (size);
1691 allocated = 1;
1695 string = build_string (buffer);
1696 if (allocated)
1697 xfree (buffer);
1699 Fsignal (Qerror, Fcons (string, Qnil));
1700 abort ();
1703 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1704 "T if FUNCTION makes provisions for interactive calling.\n\
1705 This means it contains a description for how to read arguments to give it.\n\
1706 The value is nil for an invalid function or a symbol with no function\n\
1707 definition.\n\
1709 Interactively callable functions include strings and vectors (treated\n\
1710 as keyboard macros), lambda-expressions that contain a top-level call\n\
1711 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1712 fourth argument, and some of the built-in functions of Lisp.\n\
1714 Also, a symbol satisfies `commandp' if its function definition does so.")
1715 (function)
1716 Lisp_Object function;
1718 register Lisp_Object fun;
1719 register Lisp_Object funcar;
1721 fun = function;
1723 fun = indirect_function (fun);
1724 if (EQ (fun, Qunbound))
1725 return Qnil;
1727 /* Emacs primitives are interactive if their DEFUN specifies an
1728 interactive spec. */
1729 if (SUBRP (fun))
1731 if (XSUBR (fun)->prompt)
1732 return Qt;
1733 else
1734 return Qnil;
1737 /* Bytecode objects are interactive if they are long enough to
1738 have an element whose index is COMPILED_INTERACTIVE, which is
1739 where the interactive spec is stored. */
1740 else if (COMPILEDP (fun))
1741 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1742 ? Qt : Qnil);
1744 /* Strings and vectors are keyboard macros. */
1745 if (STRINGP (fun) || VECTORP (fun))
1746 return Qt;
1748 /* Lists may represent commands. */
1749 if (!CONSP (fun))
1750 return Qnil;
1751 funcar = Fcar (fun);
1752 if (!SYMBOLP (funcar))
1753 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1754 if (EQ (funcar, Qlambda))
1755 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1756 if (EQ (funcar, Qmocklisp))
1757 return Qt; /* All mocklisp functions can be called interactively */
1758 if (EQ (funcar, Qautoload))
1759 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1760 else
1761 return Qnil;
1764 /* ARGSUSED */
1765 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1766 "Define FUNCTION to autoload from FILE.\n\
1767 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1768 Third arg DOCSTRING is documentation for the function.\n\
1769 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1770 Fifth arg TYPE indicates the type of the object:\n\
1771 nil or omitted says FUNCTION is a function,\n\
1772 `keymap' says FUNCTION is really a keymap,\n\
1773 `coding-system' says FUNCTION is really a coding-says, and\n\
1774 `macro' or t says FUNCTION is really a macro.\n\
1775 Third through fifth args give info about the real definition.\n\
1776 They default to nil.\n\
1777 If FUNCTION is already defined other than as an autoload,\n\
1778 this does nothing and returns nil.")
1779 (function, file, docstring, interactive, type)
1780 Lisp_Object function, file, docstring, interactive, type;
1782 #ifdef NO_ARG_ARRAY
1783 Lisp_Object args[4];
1784 #endif
1786 CHECK_SYMBOL (function, 0);
1787 CHECK_STRING (file, 1);
1789 /* If function is defined and not as an autoload, don't override */
1790 if (!EQ (XSYMBOL (function)->function, Qunbound)
1791 && !(CONSP (XSYMBOL (function)->function)
1792 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1793 return Qnil;
1795 if (NILP (Vpurify_flag))
1796 /* Only add entries after dumping, because the ones before are
1797 not useful and else we get loads of them from the loaddefs.el. */
1798 LOADHIST_ATTACH (Fcons (Qautoload, function));
1800 #ifdef NO_ARG_ARRAY
1801 args[0] = file;
1802 args[1] = docstring;
1803 args[2] = interactive;
1804 args[3] = type;
1806 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1807 #else /* NO_ARG_ARRAY */
1808 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1809 #endif /* not NO_ARG_ARRAY */
1812 Lisp_Object
1813 un_autoload (oldqueue)
1814 Lisp_Object oldqueue;
1816 register Lisp_Object queue, first, second;
1818 /* Queue to unwind is current value of Vautoload_queue.
1819 oldqueue is the shadowed value to leave in Vautoload_queue. */
1820 queue = Vautoload_queue;
1821 Vautoload_queue = oldqueue;
1822 while (CONSP (queue))
1824 first = Fcar (queue);
1825 second = Fcdr (first);
1826 first = Fcar (first);
1827 if (EQ (second, Qnil))
1828 Vfeatures = first;
1829 else
1830 Ffset (first, second);
1831 queue = Fcdr (queue);
1833 return Qnil;
1836 /* Load an autoloaded function.
1837 FUNNAME is the symbol which is the function's name.
1838 FUNDEF is the autoload definition (a list). */
1840 void
1841 do_autoload (fundef, funname)
1842 Lisp_Object fundef, funname;
1844 int count = specpdl_ptr - specpdl;
1845 Lisp_Object fun, queue, first, second;
1846 struct gcpro gcpro1, gcpro2, gcpro3;
1848 fun = funname;
1849 CHECK_SYMBOL (funname, 0);
1850 GCPRO3 (fun, funname, fundef);
1852 /* Preserve the match data. */
1853 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1855 /* Value saved here is to be restored into Vautoload_queue. */
1856 record_unwind_protect (un_autoload, Vautoload_queue);
1857 Vautoload_queue = Qt;
1858 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1860 /* Save the old autoloads, in case we ever do an unload. */
1861 queue = Vautoload_queue;
1862 while (CONSP (queue))
1864 first = Fcar (queue);
1865 second = Fcdr (first);
1866 first = Fcar (first);
1868 /* Note: This test is subtle. The cdr of an autoload-queue entry
1869 may be an atom if the autoload entry was generated by a defalias
1870 or fset. */
1871 if (CONSP (second))
1872 Fput (first, Qautoload, (Fcdr (second)));
1874 queue = Fcdr (queue);
1877 /* Once loading finishes, don't undo it. */
1878 Vautoload_queue = Qt;
1879 unbind_to (count, Qnil);
1881 fun = Findirect_function (fun);
1883 if (!NILP (Fequal (fun, fundef)))
1884 error ("Autoloading failed to define function %s",
1885 XSYMBOL (funname)->name->data);
1886 UNGCPRO;
1890 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1891 "Evaluate FORM and return its value.")
1892 (form)
1893 Lisp_Object form;
1895 Lisp_Object fun, val, original_fun, original_args;
1896 Lisp_Object funcar;
1897 struct backtrace backtrace;
1898 struct gcpro gcpro1, gcpro2, gcpro3;
1900 if (handling_signal)
1901 abort ();
1903 if (SYMBOLP (form))
1905 if (EQ (Vmocklisp_arguments, Qt))
1906 return Fsymbol_value (form);
1907 val = Fsymbol_value (form);
1908 if (NILP (val))
1909 XSETFASTINT (val, 0);
1910 else if (EQ (val, Qt))
1911 XSETFASTINT (val, 1);
1912 return val;
1914 if (!CONSP (form))
1915 return form;
1917 QUIT;
1918 if (consing_since_gc > gc_cons_threshold)
1920 GCPRO1 (form);
1921 Fgarbage_collect ();
1922 UNGCPRO;
1925 if (++lisp_eval_depth > max_lisp_eval_depth)
1927 if (max_lisp_eval_depth < 100)
1928 max_lisp_eval_depth = 100;
1929 if (lisp_eval_depth > max_lisp_eval_depth)
1930 error ("Lisp nesting exceeds max-lisp-eval-depth");
1933 original_fun = Fcar (form);
1934 original_args = Fcdr (form);
1936 backtrace.next = backtrace_list;
1937 backtrace_list = &backtrace;
1938 backtrace.function = &original_fun; /* This also protects them from gc */
1939 backtrace.args = &original_args;
1940 backtrace.nargs = UNEVALLED;
1941 backtrace.evalargs = 1;
1942 backtrace.debug_on_exit = 0;
1944 if (debug_on_next_call)
1945 do_debug_on_call (Qt);
1947 /* At this point, only original_fun and original_args
1948 have values that will be used below */
1949 retry:
1950 fun = Findirect_function (original_fun);
1952 if (SUBRP (fun))
1954 Lisp_Object numargs;
1955 Lisp_Object argvals[8];
1956 Lisp_Object args_left;
1957 register int i, maxargs;
1959 args_left = original_args;
1960 numargs = Flength (args_left);
1962 if (XINT (numargs) < XSUBR (fun)->min_args ||
1963 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1964 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1966 if (XSUBR (fun)->max_args == UNEVALLED)
1968 backtrace.evalargs = 0;
1969 val = (*XSUBR (fun)->function) (args_left);
1970 goto done;
1973 if (XSUBR (fun)->max_args == MANY)
1975 /* Pass a vector of evaluated arguments */
1976 Lisp_Object *vals;
1977 register int argnum = 0;
1979 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1981 GCPRO3 (args_left, fun, fun);
1982 gcpro3.var = vals;
1983 gcpro3.nvars = 0;
1985 while (!NILP (args_left))
1987 vals[argnum++] = Feval (Fcar (args_left));
1988 args_left = Fcdr (args_left);
1989 gcpro3.nvars = argnum;
1992 backtrace.args = vals;
1993 backtrace.nargs = XINT (numargs);
1995 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1996 UNGCPRO;
1997 goto done;
2000 GCPRO3 (args_left, fun, fun);
2001 gcpro3.var = argvals;
2002 gcpro3.nvars = 0;
2004 maxargs = XSUBR (fun)->max_args;
2005 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2007 argvals[i] = Feval (Fcar (args_left));
2008 gcpro3.nvars = ++i;
2011 UNGCPRO;
2013 backtrace.args = argvals;
2014 backtrace.nargs = XINT (numargs);
2016 switch (i)
2018 case 0:
2019 val = (*XSUBR (fun)->function) ();
2020 goto done;
2021 case 1:
2022 val = (*XSUBR (fun)->function) (argvals[0]);
2023 goto done;
2024 case 2:
2025 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2026 goto done;
2027 case 3:
2028 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2029 argvals[2]);
2030 goto done;
2031 case 4:
2032 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2033 argvals[2], argvals[3]);
2034 goto done;
2035 case 5:
2036 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2037 argvals[3], argvals[4]);
2038 goto done;
2039 case 6:
2040 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2041 argvals[3], argvals[4], argvals[5]);
2042 goto done;
2043 case 7:
2044 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2045 argvals[3], argvals[4], argvals[5],
2046 argvals[6]);
2047 goto done;
2049 case 8:
2050 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2051 argvals[3], argvals[4], argvals[5],
2052 argvals[6], argvals[7]);
2053 goto done;
2055 default:
2056 /* Someone has created a subr that takes more arguments than
2057 is supported by this code. We need to either rewrite the
2058 subr to use a different argument protocol, or add more
2059 cases to this switch. */
2060 abort ();
2063 if (COMPILEDP (fun))
2064 val = apply_lambda (fun, original_args, 1);
2065 else
2067 if (!CONSP (fun))
2068 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2069 funcar = Fcar (fun);
2070 if (!SYMBOLP (funcar))
2071 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2072 if (EQ (funcar, Qautoload))
2074 do_autoload (fun, original_fun);
2075 goto retry;
2077 if (EQ (funcar, Qmacro))
2078 val = Feval (apply1 (Fcdr (fun), original_args));
2079 else if (EQ (funcar, Qlambda))
2080 val = apply_lambda (fun, original_args, 1);
2081 else if (EQ (funcar, Qmocklisp))
2082 val = ml_apply (fun, original_args);
2083 else
2084 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2086 done:
2087 if (!EQ (Vmocklisp_arguments, Qt))
2089 if (NILP (val))
2090 XSETFASTINT (val, 0);
2091 else if (EQ (val, Qt))
2092 XSETFASTINT (val, 1);
2094 lisp_eval_depth--;
2095 if (backtrace.debug_on_exit)
2096 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2097 backtrace_list = backtrace.next;
2098 return val;
2101 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2102 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
2103 Then return the value FUNCTION returns.\n\
2104 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
2105 (nargs, args)
2106 int nargs;
2107 Lisp_Object *args;
2109 register int i, numargs;
2110 register Lisp_Object spread_arg;
2111 register Lisp_Object *funcall_args;
2112 Lisp_Object fun;
2113 struct gcpro gcpro1;
2115 fun = args [0];
2116 funcall_args = 0;
2117 spread_arg = args [nargs - 1];
2118 CHECK_LIST (spread_arg, nargs);
2120 numargs = XINT (Flength (spread_arg));
2122 if (numargs == 0)
2123 return Ffuncall (nargs - 1, args);
2124 else if (numargs == 1)
2126 args [nargs - 1] = XCAR (spread_arg);
2127 return Ffuncall (nargs, args);
2130 numargs += nargs - 2;
2132 fun = indirect_function (fun);
2133 if (EQ (fun, Qunbound))
2135 /* Let funcall get the error */
2136 fun = args[0];
2137 goto funcall;
2140 if (SUBRP (fun))
2142 if (numargs < XSUBR (fun)->min_args
2143 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2144 goto funcall; /* Let funcall get the error */
2145 else if (XSUBR (fun)->max_args > numargs)
2147 /* Avoid making funcall cons up a yet another new vector of arguments
2148 by explicitly supplying nil's for optional values */
2149 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2150 * sizeof (Lisp_Object));
2151 for (i = numargs; i < XSUBR (fun)->max_args;)
2152 funcall_args[++i] = Qnil;
2153 GCPRO1 (*funcall_args);
2154 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2157 funcall:
2158 /* We add 1 to numargs because funcall_args includes the
2159 function itself as well as its arguments. */
2160 if (!funcall_args)
2162 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2163 * sizeof (Lisp_Object));
2164 GCPRO1 (*funcall_args);
2165 gcpro1.nvars = 1 + numargs;
2168 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2169 /* Spread the last arg we got. Its first element goes in
2170 the slot that it used to occupy, hence this value of I. */
2171 i = nargs - 1;
2172 while (!NILP (spread_arg))
2174 funcall_args [i++] = XCAR (spread_arg);
2175 spread_arg = XCDR (spread_arg);
2178 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2181 /* Run hook variables in various ways. */
2183 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2184 "Run each hook in HOOKS. Major mode functions use this.\n\
2185 Each argument should be a symbol, a hook variable.\n\
2186 These symbols are processed in the order specified.\n\
2187 If a hook symbol has a non-nil value, that value may be a function\n\
2188 or a list of functions to be called to run the hook.\n\
2189 If the value is a function, it is called with no arguments.\n\
2190 If it is a list, the elements are called, in order, with no arguments.\n\
2192 To make a hook variable buffer-local, use `make-local-hook',\n\
2193 not `make-local-variable'.")
2194 (nargs, args)
2195 int nargs;
2196 Lisp_Object *args;
2198 Lisp_Object hook[1];
2199 register int i;
2201 for (i = 0; i < nargs; i++)
2203 hook[0] = args[i];
2204 run_hook_with_args (1, hook, to_completion);
2207 return Qnil;
2210 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2211 Srun_hook_with_args, 1, MANY, 0,
2212 "Run HOOK with the specified arguments ARGS.\n\
2213 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2214 value, that value may be a function or a list of functions to be\n\
2215 called to run the hook. If the value is a function, it is called with\n\
2216 the given arguments and its return value is returned. If it is a list\n\
2217 of functions, those functions are called, in order,\n\
2218 with the given arguments ARGS.\n\
2219 It is best not to depend on the value return by `run-hook-with-args',\n\
2220 as that may change.\n\
2222 To make a hook variable buffer-local, use `make-local-hook',\n\
2223 not `make-local-variable'.")
2224 (nargs, args)
2225 int nargs;
2226 Lisp_Object *args;
2228 return run_hook_with_args (nargs, args, to_completion);
2231 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2232 Srun_hook_with_args_until_success, 1, MANY, 0,
2233 "Run HOOK with the specified arguments ARGS.\n\
2234 HOOK should be a symbol, a hook variable. Its value should\n\
2235 be a list of functions. We call those functions, one by one,\n\
2236 passing arguments ARGS to each of them, until one of them\n\
2237 returns a non-nil value. Then we return that value.\n\
2238 If all the functions return nil, we return nil.\n\
2240 To make a hook variable buffer-local, use `make-local-hook',\n\
2241 not `make-local-variable'.")
2242 (nargs, args)
2243 int nargs;
2244 Lisp_Object *args;
2246 return run_hook_with_args (nargs, args, until_success);
2249 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2250 Srun_hook_with_args_until_failure, 1, MANY, 0,
2251 "Run HOOK with the specified arguments ARGS.\n\
2252 HOOK should be a symbol, a hook variable. Its value should\n\
2253 be a list of functions. We call those functions, one by one,\n\
2254 passing arguments ARGS to each of them, until one of them\n\
2255 returns nil. Then we return nil.\n\
2256 If all the functions return non-nil, we return non-nil.\n\
2258 To make a hook variable buffer-local, use `make-local-hook',\n\
2259 not `make-local-variable'.")
2260 (nargs, args)
2261 int nargs;
2262 Lisp_Object *args;
2264 return run_hook_with_args (nargs, args, until_failure);
2267 /* ARGS[0] should be a hook symbol.
2268 Call each of the functions in the hook value, passing each of them
2269 as arguments all the rest of ARGS (all NARGS - 1 elements).
2270 COND specifies a condition to test after each call
2271 to decide whether to stop.
2272 The caller (or its caller, etc) must gcpro all of ARGS,
2273 except that it isn't necessary to gcpro ARGS[0]. */
2275 Lisp_Object
2276 run_hook_with_args (nargs, args, cond)
2277 int nargs;
2278 Lisp_Object *args;
2279 enum run_hooks_condition cond;
2281 Lisp_Object sym, val, ret;
2282 Lisp_Object globals;
2283 struct gcpro gcpro1, gcpro2, gcpro3;
2285 /* If we are dying or still initializing,
2286 don't do anything--it would probably crash if we tried. */
2287 if (NILP (Vrun_hooks))
2288 return Qnil;
2290 sym = args[0];
2291 val = find_symbol_value (sym);
2292 ret = (cond == until_failure ? Qt : Qnil);
2294 if (EQ (val, Qunbound) || NILP (val))
2295 return ret;
2296 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2298 args[0] = val;
2299 return Ffuncall (nargs, args);
2301 else
2303 globals = Qnil;
2304 GCPRO3 (sym, val, globals);
2306 for (;
2307 CONSP (val) && ((cond == to_completion)
2308 || (cond == until_success ? NILP (ret)
2309 : !NILP (ret)));
2310 val = XCDR (val))
2312 if (EQ (XCAR (val), Qt))
2314 /* t indicates this hook has a local binding;
2315 it means to run the global binding too. */
2317 for (globals = Fdefault_value (sym);
2318 CONSP (globals) && ((cond == to_completion)
2319 || (cond == until_success ? NILP (ret)
2320 : !NILP (ret)));
2321 globals = XCDR (globals))
2323 args[0] = XCAR (globals);
2324 /* In a global value, t should not occur. If it does, we
2325 must ignore it to avoid an endless loop. */
2326 if (!EQ (args[0], Qt))
2327 ret = Ffuncall (nargs, args);
2330 else
2332 args[0] = XCAR (val);
2333 ret = Ffuncall (nargs, args);
2337 UNGCPRO;
2338 return ret;
2342 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2343 present value of that symbol.
2344 Call each element of FUNLIST,
2345 passing each of them the rest of ARGS.
2346 The caller (or its caller, etc) must gcpro all of ARGS,
2347 except that it isn't necessary to gcpro ARGS[0]. */
2349 Lisp_Object
2350 run_hook_list_with_args (funlist, nargs, args)
2351 Lisp_Object funlist;
2352 int nargs;
2353 Lisp_Object *args;
2355 Lisp_Object sym;
2356 Lisp_Object val;
2357 Lisp_Object globals;
2358 struct gcpro gcpro1, gcpro2, gcpro3;
2360 sym = args[0];
2361 globals = Qnil;
2362 GCPRO3 (sym, val, globals);
2364 for (val = funlist; CONSP (val); val = XCDR (val))
2366 if (EQ (XCAR (val), Qt))
2368 /* t indicates this hook has a local binding;
2369 it means to run the global binding too. */
2371 for (globals = Fdefault_value (sym);
2372 CONSP (globals);
2373 globals = XCDR (globals))
2375 args[0] = XCAR (globals);
2376 /* In a global value, t should not occur. If it does, we
2377 must ignore it to avoid an endless loop. */
2378 if (!EQ (args[0], Qt))
2379 Ffuncall (nargs, args);
2382 else
2384 args[0] = XCAR (val);
2385 Ffuncall (nargs, args);
2388 UNGCPRO;
2389 return Qnil;
2392 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2394 void
2395 run_hook_with_args_2 (hook, arg1, arg2)
2396 Lisp_Object hook, arg1, arg2;
2398 Lisp_Object temp[3];
2399 temp[0] = hook;
2400 temp[1] = arg1;
2401 temp[2] = arg2;
2403 Frun_hook_with_args (3, temp);
2406 /* Apply fn to arg */
2407 Lisp_Object
2408 apply1 (fn, arg)
2409 Lisp_Object fn, arg;
2411 struct gcpro gcpro1;
2413 GCPRO1 (fn);
2414 if (NILP (arg))
2415 RETURN_UNGCPRO (Ffuncall (1, &fn));
2416 gcpro1.nvars = 2;
2417 #ifdef NO_ARG_ARRAY
2419 Lisp_Object args[2];
2420 args[0] = fn;
2421 args[1] = arg;
2422 gcpro1.var = args;
2423 RETURN_UNGCPRO (Fapply (2, args));
2425 #else /* not NO_ARG_ARRAY */
2426 RETURN_UNGCPRO (Fapply (2, &fn));
2427 #endif /* not NO_ARG_ARRAY */
2430 /* Call function fn on no arguments */
2431 Lisp_Object
2432 call0 (fn)
2433 Lisp_Object fn;
2435 struct gcpro gcpro1;
2437 GCPRO1 (fn);
2438 RETURN_UNGCPRO (Ffuncall (1, &fn));
2441 /* Call function fn with 1 argument arg1 */
2442 /* ARGSUSED */
2443 Lisp_Object
2444 call1 (fn, arg1)
2445 Lisp_Object fn, arg1;
2447 struct gcpro gcpro1;
2448 #ifdef NO_ARG_ARRAY
2449 Lisp_Object args[2];
2451 args[0] = fn;
2452 args[1] = arg1;
2453 GCPRO1 (args[0]);
2454 gcpro1.nvars = 2;
2455 RETURN_UNGCPRO (Ffuncall (2, args));
2456 #else /* not NO_ARG_ARRAY */
2457 GCPRO1 (fn);
2458 gcpro1.nvars = 2;
2459 RETURN_UNGCPRO (Ffuncall (2, &fn));
2460 #endif /* not NO_ARG_ARRAY */
2463 /* Call function fn with 2 arguments arg1, arg2 */
2464 /* ARGSUSED */
2465 Lisp_Object
2466 call2 (fn, arg1, arg2)
2467 Lisp_Object fn, arg1, arg2;
2469 struct gcpro gcpro1;
2470 #ifdef NO_ARG_ARRAY
2471 Lisp_Object args[3];
2472 args[0] = fn;
2473 args[1] = arg1;
2474 args[2] = arg2;
2475 GCPRO1 (args[0]);
2476 gcpro1.nvars = 3;
2477 RETURN_UNGCPRO (Ffuncall (3, args));
2478 #else /* not NO_ARG_ARRAY */
2479 GCPRO1 (fn);
2480 gcpro1.nvars = 3;
2481 RETURN_UNGCPRO (Ffuncall (3, &fn));
2482 #endif /* not NO_ARG_ARRAY */
2485 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2486 /* ARGSUSED */
2487 Lisp_Object
2488 call3 (fn, arg1, arg2, arg3)
2489 Lisp_Object fn, arg1, arg2, arg3;
2491 struct gcpro gcpro1;
2492 #ifdef NO_ARG_ARRAY
2493 Lisp_Object args[4];
2494 args[0] = fn;
2495 args[1] = arg1;
2496 args[2] = arg2;
2497 args[3] = arg3;
2498 GCPRO1 (args[0]);
2499 gcpro1.nvars = 4;
2500 RETURN_UNGCPRO (Ffuncall (4, args));
2501 #else /* not NO_ARG_ARRAY */
2502 GCPRO1 (fn);
2503 gcpro1.nvars = 4;
2504 RETURN_UNGCPRO (Ffuncall (4, &fn));
2505 #endif /* not NO_ARG_ARRAY */
2508 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2509 /* ARGSUSED */
2510 Lisp_Object
2511 call4 (fn, arg1, arg2, arg3, arg4)
2512 Lisp_Object fn, arg1, arg2, arg3, arg4;
2514 struct gcpro gcpro1;
2515 #ifdef NO_ARG_ARRAY
2516 Lisp_Object args[5];
2517 args[0] = fn;
2518 args[1] = arg1;
2519 args[2] = arg2;
2520 args[3] = arg3;
2521 args[4] = arg4;
2522 GCPRO1 (args[0]);
2523 gcpro1.nvars = 5;
2524 RETURN_UNGCPRO (Ffuncall (5, args));
2525 #else /* not NO_ARG_ARRAY */
2526 GCPRO1 (fn);
2527 gcpro1.nvars = 5;
2528 RETURN_UNGCPRO (Ffuncall (5, &fn));
2529 #endif /* not NO_ARG_ARRAY */
2532 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2533 /* ARGSUSED */
2534 Lisp_Object
2535 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2536 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2538 struct gcpro gcpro1;
2539 #ifdef NO_ARG_ARRAY
2540 Lisp_Object args[6];
2541 args[0] = fn;
2542 args[1] = arg1;
2543 args[2] = arg2;
2544 args[3] = arg3;
2545 args[4] = arg4;
2546 args[5] = arg5;
2547 GCPRO1 (args[0]);
2548 gcpro1.nvars = 6;
2549 RETURN_UNGCPRO (Ffuncall (6, args));
2550 #else /* not NO_ARG_ARRAY */
2551 GCPRO1 (fn);
2552 gcpro1.nvars = 6;
2553 RETURN_UNGCPRO (Ffuncall (6, &fn));
2554 #endif /* not NO_ARG_ARRAY */
2557 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2558 /* ARGSUSED */
2559 Lisp_Object
2560 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2561 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2563 struct gcpro gcpro1;
2564 #ifdef NO_ARG_ARRAY
2565 Lisp_Object args[7];
2566 args[0] = fn;
2567 args[1] = arg1;
2568 args[2] = arg2;
2569 args[3] = arg3;
2570 args[4] = arg4;
2571 args[5] = arg5;
2572 args[6] = arg6;
2573 GCPRO1 (args[0]);
2574 gcpro1.nvars = 7;
2575 RETURN_UNGCPRO (Ffuncall (7, args));
2576 #else /* not NO_ARG_ARRAY */
2577 GCPRO1 (fn);
2578 gcpro1.nvars = 7;
2579 RETURN_UNGCPRO (Ffuncall (7, &fn));
2580 #endif /* not NO_ARG_ARRAY */
2583 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2584 "Call first argument as a function, passing remaining arguments to it.\n\
2585 Return the value that function returns.\n\
2586 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2587 (nargs, args)
2588 int nargs;
2589 Lisp_Object *args;
2591 Lisp_Object fun;
2592 Lisp_Object funcar;
2593 int numargs = nargs - 1;
2594 Lisp_Object lisp_numargs;
2595 Lisp_Object val;
2596 struct backtrace backtrace;
2597 register Lisp_Object *internal_args;
2598 register int i;
2600 QUIT;
2601 if (consing_since_gc > gc_cons_threshold)
2602 Fgarbage_collect ();
2604 if (++lisp_eval_depth > max_lisp_eval_depth)
2606 if (max_lisp_eval_depth < 100)
2607 max_lisp_eval_depth = 100;
2608 if (lisp_eval_depth > max_lisp_eval_depth)
2609 error ("Lisp nesting exceeds max-lisp-eval-depth");
2612 backtrace.next = backtrace_list;
2613 backtrace_list = &backtrace;
2614 backtrace.function = &args[0];
2615 backtrace.args = &args[1];
2616 backtrace.nargs = nargs - 1;
2617 backtrace.evalargs = 0;
2618 backtrace.debug_on_exit = 0;
2620 if (debug_on_next_call)
2621 do_debug_on_call (Qlambda);
2623 retry:
2625 fun = args[0];
2627 fun = Findirect_function (fun);
2629 if (SUBRP (fun))
2631 if (numargs < XSUBR (fun)->min_args
2632 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2634 XSETFASTINT (lisp_numargs, numargs);
2635 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2638 if (XSUBR (fun)->max_args == UNEVALLED)
2639 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2641 if (XSUBR (fun)->max_args == MANY)
2643 val = (*XSUBR (fun)->function) (numargs, args + 1);
2644 goto done;
2647 if (XSUBR (fun)->max_args > numargs)
2649 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2650 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2651 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2652 internal_args[i] = Qnil;
2654 else
2655 internal_args = args + 1;
2656 switch (XSUBR (fun)->max_args)
2658 case 0:
2659 val = (*XSUBR (fun)->function) ();
2660 goto done;
2661 case 1:
2662 val = (*XSUBR (fun)->function) (internal_args[0]);
2663 goto done;
2664 case 2:
2665 val = (*XSUBR (fun)->function) (internal_args[0],
2666 internal_args[1]);
2667 goto done;
2668 case 3:
2669 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2670 internal_args[2]);
2671 goto done;
2672 case 4:
2673 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2674 internal_args[2],
2675 internal_args[3]);
2676 goto done;
2677 case 5:
2678 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2679 internal_args[2], internal_args[3],
2680 internal_args[4]);
2681 goto done;
2682 case 6:
2683 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2684 internal_args[2], internal_args[3],
2685 internal_args[4], internal_args[5]);
2686 goto done;
2687 case 7:
2688 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2689 internal_args[2], internal_args[3],
2690 internal_args[4], internal_args[5],
2691 internal_args[6]);
2692 goto done;
2694 case 8:
2695 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2696 internal_args[2], internal_args[3],
2697 internal_args[4], internal_args[5],
2698 internal_args[6], internal_args[7]);
2699 goto done;
2701 default:
2703 /* If a subr takes more than 8 arguments without using MANY
2704 or UNEVALLED, we need to extend this function to support it.
2705 Until this is done, there is no way to call the function. */
2706 abort ();
2709 if (COMPILEDP (fun))
2710 val = funcall_lambda (fun, numargs, args + 1);
2711 else
2713 if (!CONSP (fun))
2714 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2715 funcar = Fcar (fun);
2716 if (!SYMBOLP (funcar))
2717 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2718 if (EQ (funcar, Qlambda))
2719 val = funcall_lambda (fun, numargs, args + 1);
2720 else if (EQ (funcar, Qmocklisp))
2721 val = ml_apply (fun, Flist (numargs, args + 1));
2722 else if (EQ (funcar, Qautoload))
2724 do_autoload (fun, args[0]);
2725 goto retry;
2727 else
2728 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2730 done:
2731 lisp_eval_depth--;
2732 if (backtrace.debug_on_exit)
2733 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2734 backtrace_list = backtrace.next;
2735 return val;
2738 Lisp_Object
2739 apply_lambda (fun, args, eval_flag)
2740 Lisp_Object fun, args;
2741 int eval_flag;
2743 Lisp_Object args_left;
2744 Lisp_Object numargs;
2745 register Lisp_Object *arg_vector;
2746 struct gcpro gcpro1, gcpro2, gcpro3;
2747 register int i;
2748 register Lisp_Object tem;
2750 numargs = Flength (args);
2751 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2752 args_left = args;
2754 GCPRO3 (*arg_vector, args_left, fun);
2755 gcpro1.nvars = 0;
2757 for (i = 0; i < XINT (numargs);)
2759 tem = Fcar (args_left), args_left = Fcdr (args_left);
2760 if (eval_flag) tem = Feval (tem);
2761 arg_vector[i++] = tem;
2762 gcpro1.nvars = i;
2765 UNGCPRO;
2767 if (eval_flag)
2769 backtrace_list->args = arg_vector;
2770 backtrace_list->nargs = i;
2772 backtrace_list->evalargs = 0;
2773 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2775 /* Do the debug-on-exit now, while arg_vector still exists. */
2776 if (backtrace_list->debug_on_exit)
2777 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2778 /* Don't do it again when we return to eval. */
2779 backtrace_list->debug_on_exit = 0;
2780 return tem;
2783 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2784 and return the result of evaluation.
2785 FUN must be either a lambda-expression or a compiled-code object. */
2787 Lisp_Object
2788 funcall_lambda (fun, nargs, arg_vector)
2789 Lisp_Object fun;
2790 int nargs;
2791 register Lisp_Object *arg_vector;
2793 Lisp_Object val, syms_left, next;
2794 int count = specpdl_ptr - specpdl;
2795 int i, optional, rest;
2797 if (NILP (Vmocklisp_arguments))
2798 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2800 if (CONSP (fun))
2802 syms_left = XCDR (fun);
2803 if (CONSP (syms_left))
2804 syms_left = XCAR (syms_left);
2805 else
2806 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2808 else if (COMPILEDP (fun))
2809 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2810 else
2811 abort ();
2813 i = optional = rest = 0;
2814 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2816 QUIT;
2818 next = XCAR (syms_left);
2819 while (!SYMBOLP (next))
2820 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2822 if (EQ (next, Qand_rest))
2823 rest = 1;
2824 else if (EQ (next, Qand_optional))
2825 optional = 1;
2826 else if (rest)
2828 specbind (next, Flist (nargs - i, &arg_vector[i]));
2829 i = nargs;
2831 else if (i < nargs)
2832 specbind (next, arg_vector[i++]);
2833 else if (!optional)
2834 return Fsignal (Qwrong_number_of_arguments,
2835 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2836 else
2837 specbind (next, Qnil);
2840 if (!NILP (syms_left))
2841 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2842 else if (i < nargs)
2843 return Fsignal (Qwrong_number_of_arguments,
2844 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2846 if (CONSP (fun))
2847 val = Fprogn (XCDR (XCDR (fun)));
2848 else
2850 /* If we have not actually read the bytecode string
2851 and constants vector yet, fetch them from the file. */
2852 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2853 Ffetch_bytecode (fun);
2854 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2855 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2856 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2859 return unbind_to (count, val);
2862 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2863 1, 1, 0,
2864 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2865 (object)
2866 Lisp_Object object;
2868 Lisp_Object tem;
2870 if (COMPILEDP (object)
2871 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2873 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2874 if (!CONSP (tem))
2875 error ("invalid byte code");
2876 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2877 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
2879 return object;
2882 void
2883 grow_specpdl ()
2885 register int count = specpdl_ptr - specpdl;
2886 if (specpdl_size >= max_specpdl_size)
2888 if (max_specpdl_size < 400)
2889 max_specpdl_size = 400;
2890 if (specpdl_size >= max_specpdl_size)
2892 if (!NILP (Vdebug_on_error))
2893 /* Leave room for some specpdl in the debugger. */
2894 max_specpdl_size = specpdl_size + 100;
2895 Fsignal (Qerror,
2896 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2899 specpdl_size *= 2;
2900 if (specpdl_size > max_specpdl_size)
2901 specpdl_size = max_specpdl_size;
2902 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2903 specpdl_ptr = specpdl + count;
2906 void
2907 specbind (symbol, value)
2908 Lisp_Object symbol, value;
2910 Lisp_Object ovalue;
2912 CHECK_SYMBOL (symbol, 0);
2913 if (specpdl_ptr == specpdl + specpdl_size)
2914 grow_specpdl ();
2916 /* The most common case is that a non-constant symbol with a trivial
2917 value. Make that as fast as we can. */
2918 if (!MISCP (XSYMBOL (symbol)->value)
2919 && !EQ (symbol, Qnil)
2920 && !EQ (symbol, Qt)
2921 && !(XSYMBOL (symbol)->name->data[0] == ':'
2922 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
2923 && !EQ (value, symbol)))
2925 specpdl_ptr->symbol = symbol;
2926 specpdl_ptr->old_value = XSYMBOL (symbol)->value;
2927 specpdl_ptr->func = NULL;
2928 ++specpdl_ptr;
2929 XSYMBOL (symbol)->value = value;
2931 else
2933 ovalue = find_symbol_value (symbol);
2934 specpdl_ptr->func = 0;
2935 specpdl_ptr->old_value = ovalue;
2937 if (BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2938 || SOME_BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2939 || BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2941 Lisp_Object current_buffer, binding_buffer;
2943 /* For a local variable, record both the symbol and which
2944 buffer's value we are saving. */
2945 current_buffer = Fcurrent_buffer ();
2946 binding_buffer = current_buffer;
2948 /* If the variable is not local in this buffer,
2949 we are saving the global value, so restore that. */
2950 if (NILP (Flocal_variable_p (symbol, binding_buffer)))
2951 binding_buffer = Qnil;
2952 specpdl_ptr->symbol
2953 = Fcons (symbol, Fcons (binding_buffer, current_buffer));
2955 /* If SYMBOL is a per-buffer variable which doesn't have a
2956 buffer-local value here, make the `let' change the global
2957 value by changing the value of SYMBOL in all buffers not
2958 having their own value. This is consistent with what
2959 happens with other buffer-local variables. */
2960 if (NILP (binding_buffer)
2961 && BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2963 ++specpdl_ptr;
2964 Fset_default (symbol, value);
2965 return;
2968 else
2969 specpdl_ptr->symbol = symbol;
2971 specpdl_ptr++;
2972 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2973 store_symval_forwarding (symbol, ovalue, value, NULL);
2974 else
2975 set_internal (symbol, value, 0, 1);
2979 void
2980 record_unwind_protect (function, arg)
2981 Lisp_Object (*function) P_ ((Lisp_Object));
2982 Lisp_Object arg;
2984 if (specpdl_ptr == specpdl + specpdl_size)
2985 grow_specpdl ();
2986 specpdl_ptr->func = function;
2987 specpdl_ptr->symbol = Qnil;
2988 specpdl_ptr->old_value = arg;
2989 specpdl_ptr++;
2992 Lisp_Object
2993 unbind_to (count, value)
2994 int count;
2995 Lisp_Object value;
2997 int quitf = !NILP (Vquit_flag);
2998 struct gcpro gcpro1;
3000 GCPRO1 (value);
3001 Vquit_flag = Qnil;
3003 while (specpdl_ptr != specpdl + count)
3005 --specpdl_ptr;
3007 if (specpdl_ptr->func != 0)
3008 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3009 /* Note that a "binding" of nil is really an unwind protect,
3010 so in that case the "old value" is a list of forms to evaluate. */
3011 else if (NILP (specpdl_ptr->symbol))
3012 Fprogn (specpdl_ptr->old_value);
3013 /* If the symbol is a list, it is really (SYMBOL BINDING_BUFFER
3014 . CURRENT_BUFFER) and it indicates we bound a variable that
3015 has buffer-local bindings. BINDING_BUFFER nil means that the
3016 variable had the default value when it was bound. */
3017 else if (CONSP (specpdl_ptr->symbol))
3019 Lisp_Object symbol, buffer;
3021 symbol = XCAR (specpdl_ptr->symbol);
3022 buffer = XCAR (XCDR (specpdl_ptr->symbol));
3024 /* Handle restoring a default value. */
3025 if (NILP (buffer))
3026 Fset_default (symbol, specpdl_ptr->old_value);
3027 /* Handle restoring a value saved from a live buffer. */
3028 else
3029 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (buffer), 1);
3031 else
3033 /* If variable has a trivial value (no forwarding), we can
3034 just set it. No need to check for constant symbols here,
3035 since that was already done by specbind. */
3036 if (!MISCP (XSYMBOL (specpdl_ptr->symbol)->value))
3037 XSYMBOL (specpdl_ptr->symbol)->value = specpdl_ptr->old_value;
3038 else
3039 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3043 if (NILP (Vquit_flag) && quitf)
3044 Vquit_flag = Qt;
3046 UNGCPRO;
3047 return value;
3050 #if 0
3052 /* Get the value of symbol's global binding, even if that binding
3053 is not now dynamically visible. */
3055 Lisp_Object
3056 top_level_value (symbol)
3057 Lisp_Object symbol;
3059 register struct specbinding *ptr = specpdl;
3061 CHECK_SYMBOL (symbol, 0);
3062 for (; ptr != specpdl_ptr; ptr++)
3064 if (EQ (ptr->symbol, symbol))
3065 return ptr->old_value;
3067 return Fsymbol_value (symbol);
3070 Lisp_Object
3071 top_level_set (symbol, newval)
3072 Lisp_Object symbol, newval;
3074 register struct specbinding *ptr = specpdl;
3076 CHECK_SYMBOL (symbol, 0);
3077 for (; ptr != specpdl_ptr; ptr++)
3079 if (EQ (ptr->symbol, symbol))
3081 ptr->old_value = newval;
3082 return newval;
3085 return Fset (symbol, newval);
3088 #endif /* 0 */
3090 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3091 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
3092 The debugger is entered when that frame exits, if the flag is non-nil.")
3093 (level, flag)
3094 Lisp_Object level, flag;
3096 register struct backtrace *backlist = backtrace_list;
3097 register int i;
3099 CHECK_NUMBER (level, 0);
3101 for (i = 0; backlist && i < XINT (level); i++)
3103 backlist = backlist->next;
3106 if (backlist)
3107 backlist->debug_on_exit = !NILP (flag);
3109 return flag;
3112 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3113 "Print a trace of Lisp function calls currently active.\n\
3114 Output stream used is value of `standard-output'.")
3117 register struct backtrace *backlist = backtrace_list;
3118 register int i;
3119 Lisp_Object tail;
3120 Lisp_Object tem;
3121 extern Lisp_Object Vprint_level;
3122 struct gcpro gcpro1;
3124 XSETFASTINT (Vprint_level, 3);
3126 tail = Qnil;
3127 GCPRO1 (tail);
3129 while (backlist)
3131 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3132 if (backlist->nargs == UNEVALLED)
3134 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3135 write_string ("\n", -1);
3137 else
3139 tem = *backlist->function;
3140 Fprin1 (tem, Qnil); /* This can QUIT */
3141 write_string ("(", -1);
3142 if (backlist->nargs == MANY)
3144 for (tail = *backlist->args, i = 0;
3145 !NILP (tail);
3146 tail = Fcdr (tail), i++)
3148 if (i) write_string (" ", -1);
3149 Fprin1 (Fcar (tail), Qnil);
3152 else
3154 for (i = 0; i < backlist->nargs; i++)
3156 if (i) write_string (" ", -1);
3157 Fprin1 (backlist->args[i], Qnil);
3160 write_string (")\n", -1);
3162 backlist = backlist->next;
3165 Vprint_level = Qnil;
3166 UNGCPRO;
3167 return Qnil;
3170 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3171 "Return the function and arguments NFRAMES up from current execution point.\n\
3172 If that frame has not evaluated the arguments yet (or is a special form),\n\
3173 the value is (nil FUNCTION ARG-FORMS...).\n\
3174 If that frame has evaluated its arguments and called its function already,\n\
3175 the value is (t FUNCTION ARG-VALUES...).\n\
3176 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3177 FUNCTION is whatever was supplied as car of evaluated list,\n\
3178 or a lambda expression for macro calls.\n\
3179 If NFRAMES is more than the number of frames, the value is nil.")
3180 (nframes)
3181 Lisp_Object nframes;
3183 register struct backtrace *backlist = backtrace_list;
3184 register int i;
3185 Lisp_Object tem;
3187 CHECK_NATNUM (nframes, 0);
3189 /* Find the frame requested. */
3190 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3191 backlist = backlist->next;
3193 if (!backlist)
3194 return Qnil;
3195 if (backlist->nargs == UNEVALLED)
3196 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3197 else
3199 if (backlist->nargs == MANY)
3200 tem = *backlist->args;
3201 else
3202 tem = Flist (backlist->nargs, backlist->args);
3204 return Fcons (Qt, Fcons (*backlist->function, tem));
3209 void
3210 syms_of_eval ()
3212 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3213 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3214 If Lisp code tries to make more than this many at once,\n\
3215 an error is signaled.");
3217 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3218 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
3219 This limit is to catch infinite recursions for you before they cause\n\
3220 actual stack overflow in C, which would be fatal for Emacs.\n\
3221 You can safely make it considerably larger than its default value,\n\
3222 if that proves inconveniently small.");
3224 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3225 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
3226 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
3227 Vquit_flag = Qnil;
3229 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3230 "Non-nil inhibits C-g quitting from happening immediately.\n\
3231 Note that `quit-flag' will still be set by typing C-g,\n\
3232 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
3233 To prevent this happening, set `quit-flag' to nil\n\
3234 before making `inhibit-quit' nil.");
3235 Vinhibit_quit = Qnil;
3237 Qinhibit_quit = intern ("inhibit-quit");
3238 staticpro (&Qinhibit_quit);
3240 Qautoload = intern ("autoload");
3241 staticpro (&Qautoload);
3243 Qdebug_on_error = intern ("debug-on-error");
3244 staticpro (&Qdebug_on_error);
3246 Qmacro = intern ("macro");
3247 staticpro (&Qmacro);
3249 /* Note that the process handling also uses Qexit, but we don't want
3250 to staticpro it twice, so we just do it here. */
3251 Qexit = intern ("exit");
3252 staticpro (&Qexit);
3254 Qinteractive = intern ("interactive");
3255 staticpro (&Qinteractive);
3257 Qcommandp = intern ("commandp");
3258 staticpro (&Qcommandp);
3260 Qdefun = intern ("defun");
3261 staticpro (&Qdefun);
3263 Qand_rest = intern ("&rest");
3264 staticpro (&Qand_rest);
3266 Qand_optional = intern ("&optional");
3267 staticpro (&Qand_optional);
3269 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3270 "*Non-nil means automatically display a backtrace buffer\n\
3271 after any error that is handled by the editor command loop.\n\
3272 If the value is a list, an error only means to display a backtrace\n\
3273 if one of its condition symbols appears in the list.");
3274 Vstack_trace_on_error = Qnil;
3276 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3277 "*Non-nil means enter debugger if an error is signaled.\n\
3278 Does not apply to errors handled by `condition-case' or those\n\
3279 matched by `debug-ignored-errors'.\n\
3280 If the value is a list, an error only means to enter the debugger\n\
3281 if one of its condition symbols appears in the list.\n\
3282 See also variable `debug-on-quit'.");
3283 Vdebug_on_error = Qnil;
3285 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3286 "*List of errors for which the debugger should not be called.\n\
3287 Each element may be a condition-name or a regexp that matches error messages.\n\
3288 If any element applies to a given error, that error skips the debugger\n\
3289 and just returns to top level.\n\
3290 This overrides the variable `debug-on-error'.\n\
3291 It does not apply to errors handled by `condition-case'.");
3292 Vdebug_ignored_errors = Qnil;
3294 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3295 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
3296 Does not apply if quit is handled by a `condition-case'.");
3297 debug_on_quit = 0;
3299 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3300 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3302 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3303 "Non-nil means debugger may continue execution.\n\
3304 This is nil when the debugger is called under circumstances where it\n\
3305 might not be safe to continue.");
3306 debugger_may_continue = 1;
3308 DEFVAR_LISP ("debugger", &Vdebugger,
3309 "Function to call to invoke debugger.\n\
3310 If due to frame exit, args are `exit' and the value being returned;\n\
3311 this function's value will be returned instead of that.\n\
3312 If due to error, args are `error' and a list of the args to `signal'.\n\
3313 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3314 If due to `eval' entry, one arg, t.");
3315 Vdebugger = Qnil;
3317 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3318 "If non-nil, this is a function for `signal' to call.\n\
3319 It receives the same arguments that `signal' was given.\n\
3320 The Edebug package uses this to regain control.");
3321 Vsignal_hook_function = Qnil;
3323 Qmocklisp_arguments = intern ("mocklisp-arguments");
3324 staticpro (&Qmocklisp_arguments);
3325 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3326 "While in a mocklisp function, the list of its unevaluated args.");
3327 Vmocklisp_arguments = Qt;
3329 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3330 "*Non-nil means call the debugger regardless of condition handlers.\n\
3331 Note that `debug-on-error', `debug-on-quit' and friends\n\
3332 still determine whether to handle the particular condition.");
3333 Vdebug_on_signal = Qnil;
3335 Vrun_hooks = intern ("run-hooks");
3336 staticpro (&Vrun_hooks);
3338 staticpro (&Vautoload_queue);
3339 Vautoload_queue = Qnil;
3340 staticpro (&Vsignaling_function);
3341 Vsignaling_function = Qnil;
3343 defsubr (&Sor);
3344 defsubr (&Sand);
3345 defsubr (&Sif);
3346 defsubr (&Scond);
3347 defsubr (&Sprogn);
3348 defsubr (&Sprog1);
3349 defsubr (&Sprog2);
3350 defsubr (&Ssetq);
3351 defsubr (&Squote);
3352 defsubr (&Sfunction);
3353 defsubr (&Sdefun);
3354 defsubr (&Sdefmacro);
3355 defsubr (&Sdefvar);
3356 defsubr (&Sdefconst);
3357 defsubr (&Suser_variable_p);
3358 defsubr (&Slet);
3359 defsubr (&SletX);
3360 defsubr (&Swhile);
3361 defsubr (&Smacroexpand);
3362 defsubr (&Scatch);
3363 defsubr (&Sthrow);
3364 defsubr (&Sunwind_protect);
3365 defsubr (&Scondition_case);
3366 defsubr (&Ssignal);
3367 defsubr (&Sinteractive_p);
3368 defsubr (&Scommandp);
3369 defsubr (&Sautoload);
3370 defsubr (&Seval);
3371 defsubr (&Sapply);
3372 defsubr (&Sfuncall);
3373 defsubr (&Srun_hooks);
3374 defsubr (&Srun_hook_with_args);
3375 defsubr (&Srun_hook_with_args_until_success);
3376 defsubr (&Srun_hook_with_args_until_failure);
3377 defsubr (&Sfetch_bytecode);
3378 defsubr (&Sbacktrace_debug);
3379 defsubr (&Sbacktrace);
3380 defsubr (&Sbacktrace_frame);