Rename x_autoselect_window_p to autoselect_window_p.
[emacs.git] / src / eval.c
blobfc7a7f426bc900264d3f0262d76c96863c74064e
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 Qand_rest, Qand_optional;
93 Lisp_Object Qdebug_on_error;
94 Lisp_Object Qdeclare;
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 debugger 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_error 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 /* Function to process declarations in defmacro forms. */
195 Lisp_Object Vmacro_declaration_function;
198 static Lisp_Object funcall_lambda P_ ((Lisp_Object, int, Lisp_Object*));
200 void
201 init_eval_once ()
203 specpdl_size = 50;
204 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
205 specpdl_ptr = specpdl;
206 max_specpdl_size = 600;
207 max_lisp_eval_depth = 300;
209 Vrun_hooks = Qnil;
212 void
213 init_eval ()
215 specpdl_ptr = specpdl;
216 catchlist = 0;
217 handlerlist = 0;
218 backtrace_list = 0;
219 Vquit_flag = Qnil;
220 debug_on_next_call = 0;
221 lisp_eval_depth = 0;
222 #ifdef DEBUG_GCPRO
223 gcpro_level = 0;
224 #endif
225 /* This is less than the initial value of num_nonmacro_input_events. */
226 when_entered_debugger = -1;
229 Lisp_Object
230 call_debugger (arg)
231 Lisp_Object arg;
233 int debug_while_redisplaying;
234 int count = specpdl_ptr - specpdl;
235 Lisp_Object val;
237 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
238 max_lisp_eval_depth = lisp_eval_depth + 20;
240 if (specpdl_size + 40 > max_specpdl_size)
241 max_specpdl_size = specpdl_size + 40;
243 #ifdef HAVE_X_WINDOWS
244 if (display_hourglass_p)
245 cancel_hourglass ();
246 #endif
248 debug_on_next_call = 0;
249 when_entered_debugger = num_nonmacro_input_events;
251 /* Resetting redisplaying_p to 0 makes sure that debug output is
252 displayed if the debugger is invoked during redisplay. */
253 debug_while_redisplaying = redisplaying_p;
254 redisplaying_p = 0;
255 specbind (intern ("debugger-may-continue"),
256 debug_while_redisplaying ? Qnil : Qt);
257 specbind (Qinhibit_redisplay, Qnil);
259 #if 0 /* Binding this prevents execution of Lisp code during
260 redisplay, which necessarily leads to display problems. */
261 specbind (Qinhibit_eval_during_redisplay, Qt);
262 #endif
264 val = apply1 (Vdebugger, arg);
266 /* Interrupting redisplay and resuming it later is not safe under
267 all circumstances. So, when the debugger returns, abort the
268 interrupted redisplay by going back to the top-level. */
269 if (debug_while_redisplaying)
270 Ftop_level ();
272 return unbind_to (count, val);
275 void
276 do_debug_on_call (code)
277 Lisp_Object code;
279 debug_on_next_call = 0;
280 backtrace_list->debug_on_exit = 1;
281 call_debugger (Fcons (code, Qnil));
284 /* NOTE!!! Every function that can call EVAL must protect its args
285 and temporaries from garbage collection while it needs them.
286 The definition of `For' shows what you have to do. */
288 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
289 doc: /* Eval args until one of them yields non-nil, then return that value.
290 The remaining args are not evalled at all.
291 If all args return nil, return nil.
292 usage: (or CONDITIONS ...) */)
293 (args)
294 Lisp_Object args;
296 register Lisp_Object val;
297 Lisp_Object args_left;
298 struct gcpro gcpro1;
300 if (NILP(args))
301 return Qnil;
303 args_left = args;
304 GCPRO1 (args_left);
308 val = Feval (Fcar (args_left));
309 if (!NILP (val))
310 break;
311 args_left = Fcdr (args_left);
313 while (!NILP(args_left));
315 UNGCPRO;
316 return val;
319 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
320 doc: /* Eval args until one of them yields nil, then return nil.
321 The remaining args are not evalled at all.
322 If no arg yields nil, return the last arg's value.
323 usage: (and CONDITIONS ...) */)
324 (args)
325 Lisp_Object args;
327 register Lisp_Object val;
328 Lisp_Object args_left;
329 struct gcpro gcpro1;
331 if (NILP(args))
332 return Qt;
334 args_left = args;
335 GCPRO1 (args_left);
339 val = Feval (Fcar (args_left));
340 if (NILP (val))
341 break;
342 args_left = Fcdr (args_left);
344 while (!NILP(args_left));
346 UNGCPRO;
347 return val;
350 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
351 doc: /* If COND yields non-nil, do THEN, else do ELSE...
352 Returns the value of THEN or the value of the last of the ELSE's.
353 THEN must be one expression, but ELSE... can be zero or more expressions.
354 If COND yields nil, and there are no ELSE's, the value is nil.
355 usage: (if COND THEN ELSE...) */)
356 (args)
357 Lisp_Object args;
359 register Lisp_Object cond;
360 struct gcpro gcpro1;
362 GCPRO1 (args);
363 cond = Feval (Fcar (args));
364 UNGCPRO;
366 if (!NILP (cond))
367 return Feval (Fcar (Fcdr (args)));
368 return Fprogn (Fcdr (Fcdr (args)));
371 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
372 doc: /* Try each clause until one succeeds.
373 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
374 and, if the value is non-nil, this clause succeeds:
375 then the expressions in BODY are evaluated and the last one's
376 value is the value of the cond-form.
377 If no clause succeeds, cond returns nil.
378 If a clause has one element, as in (CONDITION),
379 CONDITION's value if non-nil is returned from the cond-form.
380 usage: (cond CLAUSES...) */)
381 (args)
382 Lisp_Object args;
384 register Lisp_Object clause, val;
385 struct gcpro gcpro1;
387 val = Qnil;
388 GCPRO1 (args);
389 while (!NILP (args))
391 clause = Fcar (args);
392 val = Feval (Fcar (clause));
393 if (!NILP (val))
395 if (!EQ (XCDR (clause), Qnil))
396 val = Fprogn (XCDR (clause));
397 break;
399 args = XCDR (args);
401 UNGCPRO;
403 return val;
406 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
407 doc: /* Eval BODY forms sequentially and return value of last one.
408 usage: (progn BODY ...) */)
409 (args)
410 Lisp_Object args;
412 register Lisp_Object val;
413 Lisp_Object args_left;
414 struct gcpro gcpro1;
416 if (NILP(args))
417 return Qnil;
419 args_left = args;
420 GCPRO1 (args_left);
424 val = Feval (Fcar (args_left));
425 args_left = Fcdr (args_left);
427 while (!NILP(args_left));
429 UNGCPRO;
430 return val;
433 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
434 doc: /* Eval FIRST and BODY sequentially; value from FIRST.
435 The value of FIRST is saved during the evaluation of the remaining args,
436 whose values are discarded.
437 usage: (prog1 FIRST BODY...) */)
438 (args)
439 Lisp_Object args;
441 Lisp_Object val;
442 register Lisp_Object args_left;
443 struct gcpro gcpro1, gcpro2;
444 register int argnum = 0;
446 if (NILP(args))
447 return Qnil;
449 args_left = args;
450 val = Qnil;
451 GCPRO2 (args, val);
455 if (!(argnum++))
456 val = Feval (Fcar (args_left));
457 else
458 Feval (Fcar (args_left));
459 args_left = Fcdr (args_left);
461 while (!NILP(args_left));
463 UNGCPRO;
464 return val;
467 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
468 doc: /* Eval X, Y and BODY sequentially; value from Y.
469 The value of Y is saved during the evaluation of the remaining args,
470 whose values are discarded.
471 usage: (prog2 X Y BODY...) */)
472 (args)
473 Lisp_Object args;
475 Lisp_Object val;
476 register Lisp_Object args_left;
477 struct gcpro gcpro1, gcpro2;
478 register int argnum = -1;
480 val = Qnil;
482 if (NILP (args))
483 return Qnil;
485 args_left = args;
486 val = Qnil;
487 GCPRO2 (args, val);
491 if (!(argnum++))
492 val = Feval (Fcar (args_left));
493 else
494 Feval (Fcar (args_left));
495 args_left = Fcdr (args_left);
497 while (!NILP (args_left));
499 UNGCPRO;
500 return val;
503 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
504 doc: /* Set each SYM to the value of its VAL.
505 The symbols SYM are variables; they are literal (not evaluated).
506 The values VAL are expressions; they are evaluated.
507 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
508 The second VAL is not computed until after the first SYM is set, and so on;
509 each VAL can use the new value of variables set earlier in the `setq'.
510 The return value of the `setq' form is the value of the last VAL.
511 usage: (setq SYM VAL SYM VAL ...) */)
512 (args)
513 Lisp_Object args;
515 register Lisp_Object args_left;
516 register Lisp_Object val, sym;
517 struct gcpro gcpro1;
519 if (NILP(args))
520 return Qnil;
522 args_left = args;
523 GCPRO1 (args);
527 val = Feval (Fcar (Fcdr (args_left)));
528 sym = Fcar (args_left);
529 Fset (sym, val);
530 args_left = Fcdr (Fcdr (args_left));
532 while (!NILP(args_left));
534 UNGCPRO;
535 return val;
538 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
539 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
540 usage: (quote ARG) */)
541 (args)
542 Lisp_Object args;
544 return Fcar (args);
547 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
548 doc: /* Like `quote', but preferred for objects which are functions.
549 In byte compilation, `function' causes its argument to be compiled.
550 `quote' cannot do that.
551 usage: (function ARG) */)
552 (args)
553 Lisp_Object args;
555 return Fcar (args);
559 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
560 doc: /* Return t if function in which this appears was called interactively.
561 This means that the function was called with call-interactively (which
562 includes being called as the binding of a key)
563 and input is currently coming from the keyboard (not in keyboard macro). */)
566 return interactive_p (1) ? Qt : Qnil;
570 /* Return 1 if function in which this appears was called
571 interactively. This means that the function was called with
572 call-interactively (which includes being called as the binding of
573 a key) and input is currently coming from the keyboard (not in
574 keyboard macro).
576 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
577 called is a built-in. */
580 interactive_p (exclude_subrs_p)
581 int exclude_subrs_p;
583 struct backtrace *btp;
584 Lisp_Object fun;
586 if (!INTERACTIVE)
587 return 0;
589 btp = backtrace_list;
591 /* If this isn't a byte-compiled function, there may be a frame at
592 the top for Finteractive_p. If so, skip it. */
593 fun = Findirect_function (*btp->function);
594 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
595 btp = btp->next;
597 /* If we're running an Emacs 18-style byte-compiled function, there
598 may be a frame for Fbytecode. Now, given the strictest
599 definition, this function isn't really being called
600 interactively, but because that's the way Emacs 18 always builds
601 byte-compiled functions, we'll accept it for now. */
602 if (EQ (*btp->function, Qbytecode))
603 btp = btp->next;
605 /* If this isn't a byte-compiled function, then we may now be
606 looking at several frames for special forms. Skip past them. */
607 while (btp &&
608 btp->nargs == UNEVALLED)
609 btp = btp->next;
611 /* btp now points at the frame of the innermost function that isn't
612 a special form, ignoring frames for Finteractive_p and/or
613 Fbytecode at the top. If this frame is for a built-in function
614 (such as load or eval-region) return nil. */
615 fun = Findirect_function (*btp->function);
616 if (exclude_subrs_p && SUBRP (fun))
617 return 0;
619 /* btp points to the frame of a Lisp function that called interactive-p.
620 Return t if that function was called interactively. */
621 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
622 return 1;
623 return 0;
627 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
628 doc: /* Define NAME as a function.
629 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
630 See also the function `interactive'.
631 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
632 (args)
633 Lisp_Object args;
635 register Lisp_Object fn_name;
636 register Lisp_Object defn;
638 fn_name = Fcar (args);
639 defn = Fcons (Qlambda, Fcdr (args));
640 if (!NILP (Vpurify_flag))
641 defn = Fpurecopy (defn);
642 Ffset (fn_name, defn);
643 LOADHIST_ATTACH (fn_name);
644 return fn_name;
647 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
648 doc: /* Define NAME as a macro.
649 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).
650 When the macro is called, as in (NAME ARGS...),
651 the function (lambda ARGLIST BODY...) is applied to
652 the list ARGS... as it appears in the expression,
653 and the result should be a form to be evaluated instead of the original.
654 usage: (defmacro NAME ARGLIST [DOCSTRING] BODY...) */)
655 (args)
656 Lisp_Object args;
658 register Lisp_Object fn_name;
659 register Lisp_Object defn;
660 Lisp_Object lambda_list, doc, tail;
662 fn_name = Fcar (args);
663 lambda_list = Fcar (Fcdr (args));
664 tail = Fcdr (Fcdr (args));
666 doc = Qnil;
667 if (STRINGP (Fcar (tail)))
669 doc = Fcar (tail);
670 tail = Fcdr (tail);
673 while (CONSP (Fcar (tail))
674 && EQ (Fcar (Fcar (tail)), Qdeclare))
676 if (!NILP (Vmacro_declaration_function))
678 struct gcpro gcpro1;
679 GCPRO1 (args);
680 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
681 UNGCPRO;
684 tail = Fcdr (tail);
687 if (NILP (doc))
688 tail = Fcons (lambda_list, tail);
689 else
690 tail = Fcons (lambda_list, Fcons (doc, tail));
691 defn = Fcons (Qmacro, Fcons (Qlambda, tail));
693 if (!NILP (Vpurify_flag))
694 defn = Fpurecopy (defn);
695 Ffset (fn_name, defn);
696 LOADHIST_ATTACH (fn_name);
697 return fn_name;
701 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 2, 0,
702 doc: /* Make SYMBOL a variable alias for symbol ALIASED.
703 Setting the value of SYMBOL will subsequently set the value of ALIASED,
704 and getting the value of SYMBOL will return the value ALIASED has.
705 ALIASED nil means remove the alias; SYMBOL is unbound after that. */)
706 (symbol, aliased)
707 Lisp_Object symbol, aliased;
709 struct Lisp_Symbol *sym;
711 CHECK_SYMBOL (symbol);
712 CHECK_SYMBOL (aliased);
714 if (SYMBOL_CONSTANT_P (symbol))
715 error ("Cannot make a constant an alias");
717 sym = XSYMBOL (symbol);
718 sym->indirect_variable = 1;
719 sym->value = aliased;
720 sym->constant = SYMBOL_CONSTANT_P (aliased);
721 LOADHIST_ATTACH (symbol);
723 return aliased;
727 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
728 doc: /* Define SYMBOL as a variable.
729 You are not required to define a variable in order to use it,
730 but the definition can supply documentation and an initial value
731 in a way that tags can recognize.
733 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
734 If SYMBOL is buffer-local, its default value is what is set;
735 buffer-local values are not affected.
736 INITVALUE and DOCSTRING are optional.
737 If DOCSTRING starts with *, this variable is identified as a user option.
738 This means that M-x set-variable recognizes it.
739 See also `user-variable-p'.
740 If INITVALUE is missing, SYMBOL's value is not set.
741 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
742 (args)
743 Lisp_Object args;
745 register Lisp_Object sym, tem, tail;
747 sym = Fcar (args);
748 tail = Fcdr (args);
749 if (!NILP (Fcdr (Fcdr (tail))))
750 error ("too many arguments");
752 tem = Fdefault_boundp (sym);
753 if (!NILP (tail))
755 if (NILP (tem))
756 Fset_default (sym, Feval (Fcar (tail)));
757 tail = Fcdr (tail);
758 if (!NILP (Fcar (tail)))
760 tem = Fcar (tail);
761 if (!NILP (Vpurify_flag))
762 tem = Fpurecopy (tem);
763 Fput (sym, Qvariable_documentation, tem);
765 LOADHIST_ATTACH (sym);
767 else
768 /* A (defvar <var>) should not take precedence in the load-history over
769 an earlier (defvar <var> <val>), so only add to history if the default
770 value is still unbound. */
771 if (NILP (tem))
772 LOADHIST_ATTACH (sym);
774 return sym;
777 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
778 doc: /* Define SYMBOL as a constant variable.
779 The intent is that neither programs nor users should ever change this value.
780 Always sets the value of SYMBOL to the result of evalling INITVALUE.
781 If SYMBOL is buffer-local, its default value is what is set;
782 buffer-local values are not affected.
783 DOCSTRING is optional.
784 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
785 (args)
786 Lisp_Object args;
788 register Lisp_Object sym, tem;
790 sym = Fcar (args);
791 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
792 error ("too many arguments");
794 tem = Feval (Fcar (Fcdr (args)));
795 if (!NILP (Vpurify_flag))
796 tem = Fpurecopy (tem);
797 Fset_default (sym, tem);
798 tem = Fcar (Fcdr (Fcdr (args)));
799 if (!NILP (tem))
801 if (!NILP (Vpurify_flag))
802 tem = Fpurecopy (tem);
803 Fput (sym, Qvariable_documentation, tem);
805 LOADHIST_ATTACH (sym);
806 return sym;
809 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
810 doc: /* Returns t if VARIABLE is intended to be set and modified by users.
811 \(The alternative is a variable used internally in a Lisp program.)
812 Determined by whether the first character of the documentation
813 for the variable is `*' or if the variable is customizable (has a non-nil
814 value of any of `custom-type', `custom-loads' or `standard-value'
815 on its property list). */)
816 (variable)
817 Lisp_Object variable;
819 Lisp_Object documentation;
821 if (!SYMBOLP (variable))
822 return Qnil;
824 documentation = Fget (variable, Qvariable_documentation);
825 if (INTEGERP (documentation) && XINT (documentation) < 0)
826 return Qt;
827 if (STRINGP (documentation)
828 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
829 return Qt;
830 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
831 if (CONSP (documentation)
832 && STRINGP (XCAR (documentation))
833 && INTEGERP (XCDR (documentation))
834 && XINT (XCDR (documentation)) < 0)
835 return Qt;
836 /* Customizable? */
837 if ((!NILP (Fget (variable, intern ("custom-type"))))
838 || (!NILP (Fget (variable, intern ("custom-loads"))))
839 || (!NILP (Fget (variable, intern ("standard-value")))))
840 return Qt;
841 return Qnil;
844 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
845 doc: /* Bind variables according to VARLIST then eval BODY.
846 The value of the last form in BODY is returned.
847 Each element of VARLIST is a symbol (which is bound to nil)
848 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
849 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
850 usage: (let* VARLIST BODY...) */)
851 (args)
852 Lisp_Object args;
854 Lisp_Object varlist, val, elt;
855 int count = specpdl_ptr - specpdl;
856 struct gcpro gcpro1, gcpro2, gcpro3;
858 GCPRO3 (args, elt, varlist);
860 varlist = Fcar (args);
861 while (!NILP (varlist))
863 QUIT;
864 elt = Fcar (varlist);
865 if (SYMBOLP (elt))
866 specbind (elt, Qnil);
867 else if (! NILP (Fcdr (Fcdr (elt))))
868 Fsignal (Qerror,
869 Fcons (build_string ("`let' bindings can have only one value-form"),
870 elt));
871 else
873 val = Feval (Fcar (Fcdr (elt)));
874 specbind (Fcar (elt), val);
876 varlist = Fcdr (varlist);
878 UNGCPRO;
879 val = Fprogn (Fcdr (args));
880 return unbind_to (count, val);
883 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
884 doc: /* Bind variables according to VARLIST then eval BODY.
885 The value of the last form in BODY is returned.
886 Each element of VARLIST is a symbol (which is bound to nil)
887 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
888 All the VALUEFORMs are evalled before any symbols are bound.
889 usage: (let VARLIST BODY...) */)
890 (args)
891 Lisp_Object args;
893 Lisp_Object *temps, tem;
894 register Lisp_Object elt, varlist;
895 int count = specpdl_ptr - specpdl;
896 register int argnum;
897 struct gcpro gcpro1, gcpro2;
899 varlist = Fcar (args);
901 /* Make space to hold the values to give the bound variables */
902 elt = Flength (varlist);
903 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
905 /* Compute the values and store them in `temps' */
907 GCPRO2 (args, *temps);
908 gcpro2.nvars = 0;
910 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
912 QUIT;
913 elt = Fcar (varlist);
914 if (SYMBOLP (elt))
915 temps [argnum++] = Qnil;
916 else if (! NILP (Fcdr (Fcdr (elt))))
917 Fsignal (Qerror,
918 Fcons (build_string ("`let' bindings can have only one value-form"),
919 elt));
920 else
921 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
922 gcpro2.nvars = argnum;
924 UNGCPRO;
926 varlist = Fcar (args);
927 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
929 elt = Fcar (varlist);
930 tem = temps[argnum++];
931 if (SYMBOLP (elt))
932 specbind (elt, tem);
933 else
934 specbind (Fcar (elt), tem);
937 elt = Fprogn (Fcdr (args));
938 return unbind_to (count, elt);
941 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
942 doc: /* If TEST yields non-nil, eval BODY... and repeat.
943 The order of execution is thus TEST, BODY, TEST, BODY and so on
944 until TEST returns nil.
945 usage: (while TEST BODY...) */)
946 (args)
947 Lisp_Object args;
949 Lisp_Object test, body;
950 struct gcpro gcpro1, gcpro2;
952 GCPRO2 (test, body);
954 test = Fcar (args);
955 body = Fcdr (args);
956 while (!NILP (Feval (test)))
958 QUIT;
959 Fprogn (body);
962 UNGCPRO;
963 return Qnil;
966 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
967 doc: /* Return result of expanding macros at top level of FORM.
968 If FORM is not a macro call, it is returned unchanged.
969 Otherwise, the macro is expanded and the expansion is considered
970 in place of FORM. When a non-macro-call results, it is returned.
972 The second optional arg ENVIRONMENT specifies an environment of macro
973 definitions to shadow the loaded ones for use in file byte-compilation. */)
974 (form, environment)
975 Lisp_Object form;
976 Lisp_Object environment;
978 /* With cleanups from Hallvard Furuseth. */
979 register Lisp_Object expander, sym, def, tem;
981 while (1)
983 /* Come back here each time we expand a macro call,
984 in case it expands into another macro call. */
985 if (!CONSP (form))
986 break;
987 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
988 def = sym = XCAR (form);
989 tem = Qnil;
990 /* Trace symbols aliases to other symbols
991 until we get a symbol that is not an alias. */
992 while (SYMBOLP (def))
994 QUIT;
995 sym = def;
996 tem = Fassq (sym, environment);
997 if (NILP (tem))
999 def = XSYMBOL (sym)->function;
1000 if (!EQ (def, Qunbound))
1001 continue;
1003 break;
1005 /* Right now TEM is the result from SYM in ENVIRONMENT,
1006 and if TEM is nil then DEF is SYM's function definition. */
1007 if (NILP (tem))
1009 /* SYM is not mentioned in ENVIRONMENT.
1010 Look at its function definition. */
1011 if (EQ (def, Qunbound) || !CONSP (def))
1012 /* Not defined or definition not suitable */
1013 break;
1014 if (EQ (XCAR (def), Qautoload))
1016 /* Autoloading function: will it be a macro when loaded? */
1017 tem = Fnth (make_number (4), def);
1018 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1019 /* Yes, load it and try again. */
1021 struct gcpro gcpro1;
1022 GCPRO1 (form);
1023 do_autoload (def, sym);
1024 UNGCPRO;
1025 continue;
1027 else
1028 break;
1030 else if (!EQ (XCAR (def), Qmacro))
1031 break;
1032 else expander = XCDR (def);
1034 else
1036 expander = XCDR (tem);
1037 if (NILP (expander))
1038 break;
1040 form = apply1 (expander, XCDR (form));
1042 return form;
1045 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1046 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1047 TAG is evalled to get the tag to use; it must not be nil.
1049 Then the BODY is executed.
1050 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1051 If no throw happens, `catch' returns the value of the last BODY form.
1052 If a throw happens, it specifies the value to return from `catch'.
1053 usage: (catch TAG BODY...) */)
1054 (args)
1055 Lisp_Object args;
1057 register Lisp_Object tag;
1058 struct gcpro gcpro1;
1060 GCPRO1 (args);
1061 tag = Feval (Fcar (args));
1062 UNGCPRO;
1063 return internal_catch (tag, Fprogn, Fcdr (args));
1066 /* Set up a catch, then call C function FUNC on argument ARG.
1067 FUNC should return a Lisp_Object.
1068 This is how catches are done from within C code. */
1070 Lisp_Object
1071 internal_catch (tag, func, arg)
1072 Lisp_Object tag;
1073 Lisp_Object (*func) ();
1074 Lisp_Object arg;
1076 /* This structure is made part of the chain `catchlist'. */
1077 struct catchtag c;
1079 /* Fill in the components of c, and put it on the list. */
1080 c.next = catchlist;
1081 c.tag = tag;
1082 c.val = Qnil;
1083 c.backlist = backtrace_list;
1084 c.handlerlist = handlerlist;
1085 c.lisp_eval_depth = lisp_eval_depth;
1086 c.pdlcount = specpdl_ptr - specpdl;
1087 c.poll_suppress_count = poll_suppress_count;
1088 c.gcpro = gcprolist;
1089 c.byte_stack = byte_stack_list;
1090 catchlist = &c;
1092 /* Call FUNC. */
1093 if (! _setjmp (c.jmp))
1094 c.val = (*func) (arg);
1096 /* Throw works by a longjmp that comes right here. */
1097 catchlist = c.next;
1098 return c.val;
1101 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1102 jump to that CATCH, returning VALUE as the value of that catch.
1104 This is the guts Fthrow and Fsignal; they differ only in the way
1105 they choose the catch tag to throw to. A catch tag for a
1106 condition-case form has a TAG of Qnil.
1108 Before each catch is discarded, unbind all special bindings and
1109 execute all unwind-protect clauses made above that catch. Unwind
1110 the handler stack as we go, so that the proper handlers are in
1111 effect for each unwind-protect clause we run. At the end, restore
1112 some static info saved in CATCH, and longjmp to the location
1113 specified in the
1115 This is used for correct unwinding in Fthrow and Fsignal. */
1117 static void
1118 unwind_to_catch (catch, value)
1119 struct catchtag *catch;
1120 Lisp_Object value;
1122 register int last_time;
1124 /* Save the value in the tag. */
1125 catch->val = value;
1127 /* Restore the polling-suppression count. */
1128 set_poll_suppress_count (catch->poll_suppress_count);
1132 last_time = catchlist == catch;
1134 /* Unwind the specpdl stack, and then restore the proper set of
1135 handlers. */
1136 unbind_to (catchlist->pdlcount, Qnil);
1137 handlerlist = catchlist->handlerlist;
1138 catchlist = catchlist->next;
1140 while (! last_time);
1142 byte_stack_list = catch->byte_stack;
1143 gcprolist = catch->gcpro;
1144 #ifdef DEBUG_GCPRO
1145 if (gcprolist != 0)
1146 gcpro_level = gcprolist->level + 1;
1147 else
1148 gcpro_level = 0;
1149 #endif
1150 backtrace_list = catch->backlist;
1151 lisp_eval_depth = catch->lisp_eval_depth;
1153 _longjmp (catch->jmp, 1);
1156 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1157 doc: /* Throw to the catch for TAG and return VALUE from it.
1158 Both TAG and VALUE are evalled. */)
1159 (tag, value)
1160 register Lisp_Object tag, value;
1162 register struct catchtag *c;
1164 while (1)
1166 if (!NILP (tag))
1167 for (c = catchlist; c; c = c->next)
1169 if (EQ (c->tag, tag))
1170 unwind_to_catch (c, value);
1172 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
1177 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1178 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1179 If BODYFORM completes normally, its value is returned
1180 after executing the UNWINDFORMS.
1181 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1182 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1183 (args)
1184 Lisp_Object args;
1186 Lisp_Object val;
1187 int count = specpdl_ptr - specpdl;
1189 record_unwind_protect (0, Fcdr (args));
1190 val = Feval (Fcar (args));
1191 return unbind_to (count, val);
1194 /* Chain of condition handlers currently in effect.
1195 The elements of this chain are contained in the stack frames
1196 of Fcondition_case and internal_condition_case.
1197 When an error is signaled (by calling Fsignal, below),
1198 this chain is searched for an element that applies. */
1200 struct handler *handlerlist;
1202 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1203 doc: /* Regain control when an error is signaled.
1204 Executes BODYFORM and returns its value if no error happens.
1205 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1206 where the BODY is made of Lisp expressions.
1208 A handler is applicable to an error
1209 if CONDITION-NAME is one of the error's condition names.
1210 If an error happens, the first applicable handler is run.
1212 The car of a handler may be a list of condition names
1213 instead of a single condition name.
1215 When a handler handles an error,
1216 control returns to the condition-case and the handler BODY... is executed
1217 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1218 VAR may be nil; then you do not get access to the signal information.
1220 The value of the last BODY form is returned from the condition-case.
1221 See also the function `signal' for more info.
1222 usage: (condition-case VAR BODYFORM HANDLERS...) */)
1223 (args)
1224 Lisp_Object args;
1226 Lisp_Object val;
1227 struct catchtag c;
1228 struct handler h;
1229 register Lisp_Object bodyform, handlers;
1230 volatile Lisp_Object var;
1232 var = Fcar (args);
1233 bodyform = Fcar (Fcdr (args));
1234 handlers = Fcdr (Fcdr (args));
1235 CHECK_SYMBOL (var);
1237 for (val = handlers; ! NILP (val); val = Fcdr (val))
1239 Lisp_Object tem;
1240 tem = Fcar (val);
1241 if (! (NILP (tem)
1242 || (CONSP (tem)
1243 && (SYMBOLP (XCAR (tem))
1244 || CONSP (XCAR (tem))))))
1245 error ("Invalid condition handler", tem);
1248 c.tag = Qnil;
1249 c.val = Qnil;
1250 c.backlist = backtrace_list;
1251 c.handlerlist = handlerlist;
1252 c.lisp_eval_depth = lisp_eval_depth;
1253 c.pdlcount = specpdl_ptr - specpdl;
1254 c.poll_suppress_count = poll_suppress_count;
1255 c.gcpro = gcprolist;
1256 c.byte_stack = byte_stack_list;
1257 if (_setjmp (c.jmp))
1259 if (!NILP (h.var))
1260 specbind (h.var, c.val);
1261 val = Fprogn (Fcdr (h.chosen_clause));
1263 /* Note that this just undoes the binding of h.var; whoever
1264 longjumped to us unwound the stack to c.pdlcount before
1265 throwing. */
1266 unbind_to (c.pdlcount, Qnil);
1267 return val;
1269 c.next = catchlist;
1270 catchlist = &c;
1272 h.var = var;
1273 h.handler = handlers;
1274 h.next = handlerlist;
1275 h.tag = &c;
1276 handlerlist = &h;
1278 val = Feval (bodyform);
1279 catchlist = c.next;
1280 handlerlist = h.next;
1281 return val;
1284 /* Call the function BFUN with no arguments, catching errors within it
1285 according to HANDLERS. If there is an error, call HFUN with
1286 one argument which is the data that describes the error:
1287 (SIGNALNAME . DATA)
1289 HANDLERS can be a list of conditions to catch.
1290 If HANDLERS is Qt, catch all errors.
1291 If HANDLERS is Qerror, catch all errors
1292 but allow the debugger to run if that is enabled. */
1294 Lisp_Object
1295 internal_condition_case (bfun, handlers, hfun)
1296 Lisp_Object (*bfun) ();
1297 Lisp_Object handlers;
1298 Lisp_Object (*hfun) ();
1300 Lisp_Object val;
1301 struct catchtag c;
1302 struct handler h;
1304 #if 0 /* Can't do this check anymore because realize_basic_faces has
1305 to BLOCK_INPUT, and can call Lisp. What's really needed is a
1306 flag indicating that we're currently handling a signal. */
1307 /* Since Fsignal resets this to 0, it had better be 0 now
1308 or else we have a potential bug. */
1309 if (interrupt_input_blocked != 0)
1310 abort ();
1311 #endif
1313 c.tag = Qnil;
1314 c.val = Qnil;
1315 c.backlist = backtrace_list;
1316 c.handlerlist = handlerlist;
1317 c.lisp_eval_depth = lisp_eval_depth;
1318 c.pdlcount = specpdl_ptr - specpdl;
1319 c.poll_suppress_count = poll_suppress_count;
1320 c.gcpro = gcprolist;
1321 c.byte_stack = byte_stack_list;
1322 if (_setjmp (c.jmp))
1324 return (*hfun) (c.val);
1326 c.next = catchlist;
1327 catchlist = &c;
1328 h.handler = handlers;
1329 h.var = Qnil;
1330 h.next = handlerlist;
1331 h.tag = &c;
1332 handlerlist = &h;
1334 val = (*bfun) ();
1335 catchlist = c.next;
1336 handlerlist = h.next;
1337 return val;
1340 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1342 Lisp_Object
1343 internal_condition_case_1 (bfun, arg, handlers, hfun)
1344 Lisp_Object (*bfun) ();
1345 Lisp_Object arg;
1346 Lisp_Object handlers;
1347 Lisp_Object (*hfun) ();
1349 Lisp_Object val;
1350 struct catchtag c;
1351 struct handler h;
1353 c.tag = Qnil;
1354 c.val = Qnil;
1355 c.backlist = backtrace_list;
1356 c.handlerlist = handlerlist;
1357 c.lisp_eval_depth = lisp_eval_depth;
1358 c.pdlcount = specpdl_ptr - specpdl;
1359 c.poll_suppress_count = poll_suppress_count;
1360 c.gcpro = gcprolist;
1361 c.byte_stack = byte_stack_list;
1362 if (_setjmp (c.jmp))
1364 return (*hfun) (c.val);
1366 c.next = catchlist;
1367 catchlist = &c;
1368 h.handler = handlers;
1369 h.var = Qnil;
1370 h.next = handlerlist;
1371 h.tag = &c;
1372 handlerlist = &h;
1374 val = (*bfun) (arg);
1375 catchlist = c.next;
1376 handlerlist = h.next;
1377 return val;
1381 /* Like internal_condition_case but call HFUN with NARGS as first,
1382 and ARGS as second argument. */
1384 Lisp_Object
1385 internal_condition_case_2 (bfun, nargs, args, handlers, hfun)
1386 Lisp_Object (*bfun) ();
1387 int nargs;
1388 Lisp_Object *args;
1389 Lisp_Object handlers;
1390 Lisp_Object (*hfun) ();
1392 Lisp_Object val;
1393 struct catchtag c;
1394 struct handler h;
1396 c.tag = Qnil;
1397 c.val = Qnil;
1398 c.backlist = backtrace_list;
1399 c.handlerlist = handlerlist;
1400 c.lisp_eval_depth = lisp_eval_depth;
1401 c.pdlcount = specpdl_ptr - specpdl;
1402 c.poll_suppress_count = poll_suppress_count;
1403 c.gcpro = gcprolist;
1404 c.byte_stack = byte_stack_list;
1405 if (_setjmp (c.jmp))
1407 return (*hfun) (c.val);
1409 c.next = catchlist;
1410 catchlist = &c;
1411 h.handler = handlers;
1412 h.var = Qnil;
1413 h.next = handlerlist;
1414 h.tag = &c;
1415 handlerlist = &h;
1417 val = (*bfun) (nargs, args);
1418 catchlist = c.next;
1419 handlerlist = h.next;
1420 return val;
1424 static Lisp_Object find_handler_clause P_ ((Lisp_Object, Lisp_Object,
1425 Lisp_Object, Lisp_Object,
1426 Lisp_Object *));
1428 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1429 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1430 This function does not return.
1432 An error symbol is a symbol with an `error-conditions' property
1433 that is a list of condition names.
1434 A handler for any of those names will get to handle this signal.
1435 The symbol `error' should normally be one of them.
1437 DATA should be a list. Its elements are printed as part of the error message.
1438 If the signal is handled, DATA is made available to the handler.
1439 See also the function `condition-case'. */)
1440 (error_symbol, data)
1441 Lisp_Object error_symbol, data;
1443 /* When memory is full, ERROR-SYMBOL is nil,
1444 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
1445 register struct handler *allhandlers = handlerlist;
1446 Lisp_Object conditions;
1447 extern int gc_in_progress;
1448 extern int waiting_for_input;
1449 Lisp_Object debugger_value;
1450 Lisp_Object string;
1451 Lisp_Object real_error_symbol;
1452 struct backtrace *bp;
1454 immediate_quit = handling_signal = 0;
1455 if (gc_in_progress || waiting_for_input)
1456 abort ();
1458 TOTALLY_UNBLOCK_INPUT;
1460 if (NILP (error_symbol))
1461 real_error_symbol = Fcar (data);
1462 else
1463 real_error_symbol = error_symbol;
1465 #ifdef HAVE_X_WINDOWS
1466 if (display_hourglass_p)
1467 cancel_hourglass ();
1468 #endif
1470 /* This hook is used by edebug. */
1471 if (! NILP (Vsignal_hook_function))
1472 call2 (Vsignal_hook_function, error_symbol, data);
1474 conditions = Fget (real_error_symbol, Qerror_conditions);
1476 /* Remember from where signal was called. Skip over the frame for
1477 `signal' itself. If a frame for `error' follows, skip that,
1478 too. */
1479 Vsignaling_function = Qnil;
1480 if (backtrace_list)
1482 bp = backtrace_list->next;
1483 if (bp && bp->function && EQ (*bp->function, Qerror))
1484 bp = bp->next;
1485 if (bp && bp->function)
1486 Vsignaling_function = *bp->function;
1489 for (; handlerlist; handlerlist = handlerlist->next)
1491 register Lisp_Object clause;
1493 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1494 max_lisp_eval_depth = lisp_eval_depth + 20;
1496 if (specpdl_size + 40 > max_specpdl_size)
1497 max_specpdl_size = specpdl_size + 40;
1499 clause = find_handler_clause (handlerlist->handler, conditions,
1500 error_symbol, data, &debugger_value);
1502 #if 0 /* Most callers are not prepared to handle gc if this returns.
1503 So, since this feature is not very useful, take it out. */
1504 /* If have called debugger and user wants to continue,
1505 just return nil. */
1506 if (EQ (clause, Qlambda))
1507 return debugger_value;
1508 #else
1509 if (EQ (clause, Qlambda))
1511 /* We can't return values to code which signaled an error, but we
1512 can continue code which has signaled a quit. */
1513 if (EQ (real_error_symbol, Qquit))
1514 return Qnil;
1515 else
1516 error ("Cannot return from the debugger in an error");
1518 #endif
1520 if (!NILP (clause))
1522 Lisp_Object unwind_data;
1523 struct handler *h = handlerlist;
1525 handlerlist = allhandlers;
1527 if (NILP (error_symbol))
1528 unwind_data = data;
1529 else
1530 unwind_data = Fcons (error_symbol, data);
1531 h->chosen_clause = clause;
1532 unwind_to_catch (h->tag, unwind_data);
1536 handlerlist = allhandlers;
1537 /* If no handler is present now, try to run the debugger,
1538 and if that fails, throw to top level. */
1539 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1540 if (catchlist != 0)
1541 Fthrow (Qtop_level, Qt);
1543 if (! NILP (error_symbol))
1544 data = Fcons (error_symbol, data);
1546 string = Ferror_message_string (data);
1547 fatal ("%s", XSTRING (string)->data, 0);
1550 /* Return nonzero iff LIST is a non-nil atom or
1551 a list containing one of CONDITIONS. */
1553 static int
1554 wants_debugger (list, conditions)
1555 Lisp_Object list, conditions;
1557 if (NILP (list))
1558 return 0;
1559 if (! CONSP (list))
1560 return 1;
1562 while (CONSP (conditions))
1564 Lisp_Object this, tail;
1565 this = XCAR (conditions);
1566 for (tail = list; CONSP (tail); tail = XCDR (tail))
1567 if (EQ (XCAR (tail), this))
1568 return 1;
1569 conditions = XCDR (conditions);
1571 return 0;
1574 /* Return 1 if an error with condition-symbols CONDITIONS,
1575 and described by SIGNAL-DATA, should skip the debugger
1576 according to debugger-ignored-errors. */
1578 static int
1579 skip_debugger (conditions, data)
1580 Lisp_Object conditions, data;
1582 Lisp_Object tail;
1583 int first_string = 1;
1584 Lisp_Object error_message;
1586 error_message = Qnil;
1587 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1589 if (STRINGP (XCAR (tail)))
1591 if (first_string)
1593 error_message = Ferror_message_string (data);
1594 first_string = 0;
1597 if (fast_string_match (XCAR (tail), error_message) >= 0)
1598 return 1;
1600 else
1602 Lisp_Object contail;
1604 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1605 if (EQ (XCAR (tail), XCAR (contail)))
1606 return 1;
1610 return 0;
1613 /* Value of Qlambda means we have called debugger and user has continued.
1614 There are two ways to pass SIG and DATA:
1615 = SIG is the error symbol, and DATA is the rest of the data.
1616 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1617 This is for memory-full errors only.
1619 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1621 static Lisp_Object
1622 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1623 Lisp_Object handlers, conditions, sig, data;
1624 Lisp_Object *debugger_value_ptr;
1626 register Lisp_Object h;
1627 register Lisp_Object tem;
1629 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1630 return Qt;
1631 /* error is used similarly, but means print an error message
1632 and run the debugger if that is enabled. */
1633 if (EQ (handlers, Qerror)
1634 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1635 there is a handler. */
1637 int count = specpdl_ptr - specpdl;
1638 int debugger_called = 0;
1639 Lisp_Object sig_symbol, combined_data;
1640 /* This is set to 1 if we are handling a memory-full error,
1641 because these must not run the debugger.
1642 (There is no room in memory to do that!) */
1643 int no_debugger = 0;
1645 if (NILP (sig))
1647 combined_data = data;
1648 sig_symbol = Fcar (data);
1649 no_debugger = 1;
1651 else
1653 combined_data = Fcons (sig, data);
1654 sig_symbol = sig;
1657 if (wants_debugger (Vstack_trace_on_error, conditions))
1659 #ifdef PROTOTYPES
1660 internal_with_output_to_temp_buffer ("*Backtrace*",
1661 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1662 Qnil);
1663 #else
1664 internal_with_output_to_temp_buffer ("*Backtrace*",
1665 Fbacktrace, Qnil);
1666 #endif
1668 if (! no_debugger
1669 && (EQ (sig_symbol, Qquit)
1670 ? debug_on_quit
1671 : wants_debugger (Vdebug_on_error, conditions))
1672 && ! skip_debugger (conditions, combined_data)
1673 && when_entered_debugger < num_nonmacro_input_events)
1675 specbind (Qdebug_on_error, Qnil);
1676 *debugger_value_ptr
1677 = call_debugger (Fcons (Qerror,
1678 Fcons (combined_data, Qnil)));
1679 debugger_called = 1;
1681 /* If there is no handler, return saying whether we ran the debugger. */
1682 if (EQ (handlers, Qerror))
1684 if (debugger_called)
1685 return unbind_to (count, Qlambda);
1686 return Qt;
1689 for (h = handlers; CONSP (h); h = Fcdr (h))
1691 Lisp_Object handler, condit;
1693 handler = Fcar (h);
1694 if (!CONSP (handler))
1695 continue;
1696 condit = Fcar (handler);
1697 /* Handle a single condition name in handler HANDLER. */
1698 if (SYMBOLP (condit))
1700 tem = Fmemq (Fcar (handler), conditions);
1701 if (!NILP (tem))
1702 return handler;
1704 /* Handle a list of condition names in handler HANDLER. */
1705 else if (CONSP (condit))
1707 while (CONSP (condit))
1709 tem = Fmemq (Fcar (condit), conditions);
1710 if (!NILP (tem))
1711 return handler;
1712 condit = XCDR (condit);
1716 return Qnil;
1719 /* dump an error message; called like printf */
1721 /* VARARGS 1 */
1722 void
1723 error (m, a1, a2, a3)
1724 char *m;
1725 char *a1, *a2, *a3;
1727 char buf[200];
1728 int size = 200;
1729 int mlen;
1730 char *buffer = buf;
1731 char *args[3];
1732 int allocated = 0;
1733 Lisp_Object string;
1735 args[0] = a1;
1736 args[1] = a2;
1737 args[2] = a3;
1739 mlen = strlen (m);
1741 while (1)
1743 int used = doprnt (buffer, size, m, m + mlen, 3, args);
1744 if (used < size)
1745 break;
1746 size *= 2;
1747 if (allocated)
1748 buffer = (char *) xrealloc (buffer, size);
1749 else
1751 buffer = (char *) xmalloc (size);
1752 allocated = 1;
1756 string = build_string (buffer);
1757 if (allocated)
1758 xfree (buffer);
1760 Fsignal (Qerror, Fcons (string, Qnil));
1761 abort ();
1764 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1765 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1766 This means it contains a description for how to read arguments to give it.
1767 The value is nil for an invalid function or a symbol with no function
1768 definition.
1770 Interactively callable functions include strings and vectors (treated
1771 as keyboard macros), lambda-expressions that contain a top-level call
1772 to `interactive', autoload definitions made by `autoload' with non-nil
1773 fourth argument, and some of the built-in functions of Lisp.
1775 Also, a symbol satisfies `commandp' if its function definition does so. */)
1776 (function)
1777 Lisp_Object function;
1779 register Lisp_Object fun;
1780 register Lisp_Object funcar;
1782 fun = function;
1784 fun = indirect_function (fun);
1785 if (EQ (fun, Qunbound))
1786 return Qnil;
1788 /* Emacs primitives are interactive if their DEFUN specifies an
1789 interactive spec. */
1790 if (SUBRP (fun))
1792 if (XSUBR (fun)->prompt)
1793 return Qt;
1794 else
1795 return Qnil;
1798 /* Bytecode objects are interactive if they are long enough to
1799 have an element whose index is COMPILED_INTERACTIVE, which is
1800 where the interactive spec is stored. */
1801 else if (COMPILEDP (fun))
1802 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1803 ? Qt : Qnil);
1805 /* Strings and vectors are keyboard macros. */
1806 if (STRINGP (fun) || VECTORP (fun))
1807 return Qt;
1809 /* Lists may represent commands. */
1810 if (!CONSP (fun))
1811 return Qnil;
1812 funcar = Fcar (fun);
1813 if (!SYMBOLP (funcar))
1814 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1815 if (EQ (funcar, Qlambda))
1816 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1817 if (EQ (funcar, Qautoload))
1818 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1819 else
1820 return Qnil;
1823 /* ARGSUSED */
1824 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1825 doc: /* Define FUNCTION to autoload from FILE.
1826 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1827 Third arg DOCSTRING is documentation for the function.
1828 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1829 Fifth arg TYPE indicates the type of the object:
1830 nil or omitted says FUNCTION is a function,
1831 `keymap' says FUNCTION is really a keymap, and
1832 `macro' or t says FUNCTION is really a macro.
1833 Third through fifth args give info about the real definition.
1834 They default to nil.
1835 If FUNCTION is already defined other than as an autoload,
1836 this does nothing and returns nil. */)
1837 (function, file, docstring, interactive, type)
1838 Lisp_Object function, file, docstring, interactive, type;
1840 #ifdef NO_ARG_ARRAY
1841 Lisp_Object args[4];
1842 #endif
1844 CHECK_SYMBOL (function);
1845 CHECK_STRING (file);
1847 /* If function is defined and not as an autoload, don't override */
1848 if (!EQ (XSYMBOL (function)->function, Qunbound)
1849 && !(CONSP (XSYMBOL (function)->function)
1850 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1851 return Qnil;
1853 if (NILP (Vpurify_flag))
1854 /* Only add entries after dumping, because the ones before are
1855 not useful and else we get loads of them from the loaddefs.el. */
1856 LOADHIST_ATTACH (Fcons (Qautoload, function));
1858 #ifdef NO_ARG_ARRAY
1859 args[0] = file;
1860 args[1] = docstring;
1861 args[2] = interactive;
1862 args[3] = type;
1864 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1865 #else /* NO_ARG_ARRAY */
1866 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1867 #endif /* not NO_ARG_ARRAY */
1870 Lisp_Object
1871 un_autoload (oldqueue)
1872 Lisp_Object oldqueue;
1874 register Lisp_Object queue, first, second;
1876 /* Queue to unwind is current value of Vautoload_queue.
1877 oldqueue is the shadowed value to leave in Vautoload_queue. */
1878 queue = Vautoload_queue;
1879 Vautoload_queue = oldqueue;
1880 while (CONSP (queue))
1882 first = Fcar (queue);
1883 second = Fcdr (first);
1884 first = Fcar (first);
1885 if (EQ (second, Qnil))
1886 Vfeatures = first;
1887 else
1888 Ffset (first, second);
1889 queue = Fcdr (queue);
1891 return Qnil;
1894 /* Load an autoloaded function.
1895 FUNNAME is the symbol which is the function's name.
1896 FUNDEF is the autoload definition (a list). */
1898 void
1899 do_autoload (fundef, funname)
1900 Lisp_Object fundef, funname;
1902 int count = specpdl_ptr - specpdl;
1903 Lisp_Object fun, queue, first, second;
1904 struct gcpro gcpro1, gcpro2, gcpro3;
1906 fun = funname;
1907 CHECK_SYMBOL (funname);
1908 GCPRO3 (fun, funname, fundef);
1910 /* Preserve the match data. */
1911 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1913 /* Value saved here is to be restored into Vautoload_queue. */
1914 record_unwind_protect (un_autoload, Vautoload_queue);
1915 Vautoload_queue = Qt;
1916 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
1918 /* Save the old autoloads, in case we ever do an unload. */
1919 queue = Vautoload_queue;
1920 while (CONSP (queue))
1922 first = Fcar (queue);
1923 second = Fcdr (first);
1924 first = Fcar (first);
1926 /* Note: This test is subtle. The cdr of an autoload-queue entry
1927 may be an atom if the autoload entry was generated by a defalias
1928 or fset. */
1929 if (CONSP (second))
1930 Fput (first, Qautoload, (Fcdr (second)));
1932 queue = Fcdr (queue);
1935 /* Once loading finishes, don't undo it. */
1936 Vautoload_queue = Qt;
1937 unbind_to (count, Qnil);
1939 fun = Findirect_function (fun);
1941 if (!NILP (Fequal (fun, fundef)))
1942 error ("Autoloading failed to define function %s",
1943 XSYMBOL (funname)->name->data);
1944 UNGCPRO;
1948 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1949 doc: /* Evaluate FORM and return its value. */)
1950 (form)
1951 Lisp_Object form;
1953 Lisp_Object fun, val, original_fun, original_args;
1954 Lisp_Object funcar;
1955 struct backtrace backtrace;
1956 struct gcpro gcpro1, gcpro2, gcpro3;
1958 if (handling_signal)
1959 abort ();
1961 if (SYMBOLP (form))
1962 return Fsymbol_value (form);
1963 if (!CONSP (form))
1964 return form;
1966 QUIT;
1967 if (consing_since_gc > gc_cons_threshold)
1969 GCPRO1 (form);
1970 Fgarbage_collect ();
1971 UNGCPRO;
1974 if (++lisp_eval_depth > max_lisp_eval_depth)
1976 if (max_lisp_eval_depth < 100)
1977 max_lisp_eval_depth = 100;
1978 if (lisp_eval_depth > max_lisp_eval_depth)
1979 error ("Lisp nesting exceeds max-lisp-eval-depth");
1982 original_fun = Fcar (form);
1983 original_args = Fcdr (form);
1985 backtrace.next = backtrace_list;
1986 backtrace_list = &backtrace;
1987 backtrace.function = &original_fun; /* This also protects them from gc */
1988 backtrace.args = &original_args;
1989 backtrace.nargs = UNEVALLED;
1990 backtrace.evalargs = 1;
1991 backtrace.debug_on_exit = 0;
1993 if (debug_on_next_call)
1994 do_debug_on_call (Qt);
1996 /* At this point, only original_fun and original_args
1997 have values that will be used below */
1998 retry:
1999 fun = Findirect_function (original_fun);
2001 if (SUBRP (fun))
2003 Lisp_Object numargs;
2004 Lisp_Object argvals[8];
2005 Lisp_Object args_left;
2006 register int i, maxargs;
2008 args_left = original_args;
2009 numargs = Flength (args_left);
2011 if (XINT (numargs) < XSUBR (fun)->min_args ||
2012 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2013 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2015 if (XSUBR (fun)->max_args == UNEVALLED)
2017 backtrace.evalargs = 0;
2018 val = (*XSUBR (fun)->function) (args_left);
2019 goto done;
2022 if (XSUBR (fun)->max_args == MANY)
2024 /* Pass a vector of evaluated arguments */
2025 Lisp_Object *vals;
2026 register int argnum = 0;
2028 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2030 GCPRO3 (args_left, fun, fun);
2031 gcpro3.var = vals;
2032 gcpro3.nvars = 0;
2034 while (!NILP (args_left))
2036 vals[argnum++] = Feval (Fcar (args_left));
2037 args_left = Fcdr (args_left);
2038 gcpro3.nvars = argnum;
2041 backtrace.args = vals;
2042 backtrace.nargs = XINT (numargs);
2044 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
2045 UNGCPRO;
2046 goto done;
2049 GCPRO3 (args_left, fun, fun);
2050 gcpro3.var = argvals;
2051 gcpro3.nvars = 0;
2053 maxargs = XSUBR (fun)->max_args;
2054 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2056 argvals[i] = Feval (Fcar (args_left));
2057 gcpro3.nvars = ++i;
2060 UNGCPRO;
2062 backtrace.args = argvals;
2063 backtrace.nargs = XINT (numargs);
2065 switch (i)
2067 case 0:
2068 val = (*XSUBR (fun)->function) ();
2069 goto done;
2070 case 1:
2071 val = (*XSUBR (fun)->function) (argvals[0]);
2072 goto done;
2073 case 2:
2074 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
2075 goto done;
2076 case 3:
2077 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2078 argvals[2]);
2079 goto done;
2080 case 4:
2081 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
2082 argvals[2], argvals[3]);
2083 goto done;
2084 case 5:
2085 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2086 argvals[3], argvals[4]);
2087 goto done;
2088 case 6:
2089 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2090 argvals[3], argvals[4], argvals[5]);
2091 goto done;
2092 case 7:
2093 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2094 argvals[3], argvals[4], argvals[5],
2095 argvals[6]);
2096 goto done;
2098 case 8:
2099 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
2100 argvals[3], argvals[4], argvals[5],
2101 argvals[6], argvals[7]);
2102 goto done;
2104 default:
2105 /* Someone has created a subr that takes more arguments than
2106 is supported by this code. We need to either rewrite the
2107 subr to use a different argument protocol, or add more
2108 cases to this switch. */
2109 abort ();
2112 if (COMPILEDP (fun))
2113 val = apply_lambda (fun, original_args, 1);
2114 else
2116 if (!CONSP (fun))
2117 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2118 funcar = Fcar (fun);
2119 if (!SYMBOLP (funcar))
2120 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2121 if (EQ (funcar, Qautoload))
2123 do_autoload (fun, original_fun);
2124 goto retry;
2126 if (EQ (funcar, Qmacro))
2127 val = Feval (apply1 (Fcdr (fun), original_args));
2128 else if (EQ (funcar, Qlambda))
2129 val = apply_lambda (fun, original_args, 1);
2130 else
2131 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2133 done:
2134 lisp_eval_depth--;
2135 if (backtrace.debug_on_exit)
2136 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2137 backtrace_list = backtrace.next;
2138 return val;
2141 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2142 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2143 Then return the value FUNCTION returns.
2144 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2145 usage: (apply FUNCTION &rest ARGUMENTS) */)
2146 (nargs, args)
2147 int nargs;
2148 Lisp_Object *args;
2150 register int i, numargs;
2151 register Lisp_Object spread_arg;
2152 register Lisp_Object *funcall_args;
2153 Lisp_Object fun;
2154 struct gcpro gcpro1;
2156 fun = args [0];
2157 funcall_args = 0;
2158 spread_arg = args [nargs - 1];
2159 CHECK_LIST (spread_arg);
2161 numargs = XINT (Flength (spread_arg));
2163 if (numargs == 0)
2164 return Ffuncall (nargs - 1, args);
2165 else if (numargs == 1)
2167 args [nargs - 1] = XCAR (spread_arg);
2168 return Ffuncall (nargs, args);
2171 numargs += nargs - 2;
2173 fun = indirect_function (fun);
2174 if (EQ (fun, Qunbound))
2176 /* Let funcall get the error */
2177 fun = args[0];
2178 goto funcall;
2181 if (SUBRP (fun))
2183 if (numargs < XSUBR (fun)->min_args
2184 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2185 goto funcall; /* Let funcall get the error */
2186 else if (XSUBR (fun)->max_args > numargs)
2188 /* Avoid making funcall cons up a yet another new vector of arguments
2189 by explicitly supplying nil's for optional values */
2190 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2191 * sizeof (Lisp_Object));
2192 for (i = numargs; i < XSUBR (fun)->max_args;)
2193 funcall_args[++i] = Qnil;
2194 GCPRO1 (*funcall_args);
2195 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2198 funcall:
2199 /* We add 1 to numargs because funcall_args includes the
2200 function itself as well as its arguments. */
2201 if (!funcall_args)
2203 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2204 * sizeof (Lisp_Object));
2205 GCPRO1 (*funcall_args);
2206 gcpro1.nvars = 1 + numargs;
2209 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2210 /* Spread the last arg we got. Its first element goes in
2211 the slot that it used to occupy, hence this value of I. */
2212 i = nargs - 1;
2213 while (!NILP (spread_arg))
2215 funcall_args [i++] = XCAR (spread_arg);
2216 spread_arg = XCDR (spread_arg);
2219 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2222 /* Run hook variables in various ways. */
2224 enum run_hooks_condition {to_completion, until_success, until_failure};
2225 static Lisp_Object run_hook_with_args P_ ((int, Lisp_Object *,
2226 enum run_hooks_condition));
2228 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2229 doc: /* Run each hook in HOOKS. Major mode functions use this.
2230 Each argument should be a symbol, a hook variable.
2231 These symbols are processed in the order specified.
2232 If a hook symbol has a non-nil value, that value may be a function
2233 or a list of functions to be called to run the hook.
2234 If the value is a function, it is called with no arguments.
2235 If it is a list, the elements are called, in order, with no arguments.
2237 Do not use `make-local-variable' to make a hook variable buffer-local.
2238 Instead, use `add-hook' and specify t for the LOCAL argument.
2239 usage: (run-hooks &rest HOOKS) */)
2240 (nargs, args)
2241 int nargs;
2242 Lisp_Object *args;
2244 Lisp_Object hook[1];
2245 register int i;
2247 for (i = 0; i < nargs; i++)
2249 hook[0] = args[i];
2250 run_hook_with_args (1, hook, to_completion);
2253 return Qnil;
2256 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2257 Srun_hook_with_args, 1, MANY, 0,
2258 doc: /* Run HOOK with the specified arguments ARGS.
2259 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2260 value, that value may be a function or a list of functions to be
2261 called to run the hook. If the value is a function, it is called with
2262 the given arguments and its return value is returned. If it is a list
2263 of functions, those functions are called, in order,
2264 with the given arguments ARGS.
2265 It is best not to depend on the value return by `run-hook-with-args',
2266 as that may change.
2268 Do not use `make-local-variable' to make a hook variable buffer-local.
2269 Instead, use `add-hook' and specify t for the LOCAL argument.
2270 usage: (run-hook-with-args HOOK &rest ARGS) */)
2271 (nargs, args)
2272 int nargs;
2273 Lisp_Object *args;
2275 return run_hook_with_args (nargs, args, to_completion);
2278 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2279 Srun_hook_with_args_until_success, 1, MANY, 0,
2280 doc: /* Run HOOK with the specified arguments ARGS.
2281 HOOK should be a symbol, a hook variable. Its value should
2282 be a list of functions. We call those functions, one by one,
2283 passing arguments ARGS to each of them, until one of them
2284 returns a non-nil value. Then we return that value.
2285 If all the functions return nil, we return nil.
2287 Do not use `make-local-variable' to make a hook variable buffer-local.
2288 Instead, use `add-hook' and specify t for the LOCAL argument.
2289 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2290 (nargs, args)
2291 int nargs;
2292 Lisp_Object *args;
2294 return run_hook_with_args (nargs, args, until_success);
2297 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2298 Srun_hook_with_args_until_failure, 1, MANY, 0,
2299 doc: /* Run HOOK with the specified arguments ARGS.
2300 HOOK should be a symbol, a hook variable. Its value should
2301 be a list of functions. We call those functions, one by one,
2302 passing arguments ARGS to each of them, until one of them
2303 returns nil. Then we return nil.
2304 If all the functions return non-nil, we return non-nil.
2306 Do not use `make-local-variable' to make a hook variable buffer-local.
2307 Instead, use `add-hook' and specify t for the LOCAL argument.
2308 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2309 (nargs, args)
2310 int nargs;
2311 Lisp_Object *args;
2313 return run_hook_with_args (nargs, args, until_failure);
2316 /* ARGS[0] should be a hook symbol.
2317 Call each of the functions in the hook value, passing each of them
2318 as arguments all the rest of ARGS (all NARGS - 1 elements).
2319 COND specifies a condition to test after each call
2320 to decide whether to stop.
2321 The caller (or its caller, etc) must gcpro all of ARGS,
2322 except that it isn't necessary to gcpro ARGS[0]. */
2324 static Lisp_Object
2325 run_hook_with_args (nargs, args, cond)
2326 int nargs;
2327 Lisp_Object *args;
2328 enum run_hooks_condition cond;
2330 Lisp_Object sym, val, ret;
2331 Lisp_Object globals;
2332 struct gcpro gcpro1, gcpro2, gcpro3;
2334 /* If we are dying or still initializing,
2335 don't do anything--it would probably crash if we tried. */
2336 if (NILP (Vrun_hooks))
2337 return Qnil;
2339 sym = args[0];
2340 val = find_symbol_value (sym);
2341 ret = (cond == until_failure ? Qt : Qnil);
2343 if (EQ (val, Qunbound) || NILP (val))
2344 return ret;
2345 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2347 args[0] = val;
2348 return Ffuncall (nargs, args);
2350 else
2352 globals = Qnil;
2353 GCPRO3 (sym, val, globals);
2355 for (;
2356 CONSP (val) && ((cond == to_completion)
2357 || (cond == until_success ? NILP (ret)
2358 : !NILP (ret)));
2359 val = XCDR (val))
2361 if (EQ (XCAR (val), Qt))
2363 /* t indicates this hook has a local binding;
2364 it means to run the global binding too. */
2366 for (globals = Fdefault_value (sym);
2367 CONSP (globals) && ((cond == to_completion)
2368 || (cond == until_success ? NILP (ret)
2369 : !NILP (ret)));
2370 globals = XCDR (globals))
2372 args[0] = XCAR (globals);
2373 /* In a global value, t should not occur. If it does, we
2374 must ignore it to avoid an endless loop. */
2375 if (!EQ (args[0], Qt))
2376 ret = Ffuncall (nargs, args);
2379 else
2381 args[0] = XCAR (val);
2382 ret = Ffuncall (nargs, args);
2386 UNGCPRO;
2387 return ret;
2391 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2392 present value of that symbol.
2393 Call each element of FUNLIST,
2394 passing each of them the rest of ARGS.
2395 The caller (or its caller, etc) must gcpro all of ARGS,
2396 except that it isn't necessary to gcpro ARGS[0]. */
2398 Lisp_Object
2399 run_hook_list_with_args (funlist, nargs, args)
2400 Lisp_Object funlist;
2401 int nargs;
2402 Lisp_Object *args;
2404 Lisp_Object sym;
2405 Lisp_Object val;
2406 Lisp_Object globals;
2407 struct gcpro gcpro1, gcpro2, gcpro3;
2409 sym = args[0];
2410 globals = Qnil;
2411 GCPRO3 (sym, val, globals);
2413 for (val = funlist; CONSP (val); val = XCDR (val))
2415 if (EQ (XCAR (val), Qt))
2417 /* t indicates this hook has a local binding;
2418 it means to run the global binding too. */
2420 for (globals = Fdefault_value (sym);
2421 CONSP (globals);
2422 globals = XCDR (globals))
2424 args[0] = XCAR (globals);
2425 /* In a global value, t should not occur. If it does, we
2426 must ignore it to avoid an endless loop. */
2427 if (!EQ (args[0], Qt))
2428 Ffuncall (nargs, args);
2431 else
2433 args[0] = XCAR (val);
2434 Ffuncall (nargs, args);
2437 UNGCPRO;
2438 return Qnil;
2441 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2443 void
2444 run_hook_with_args_2 (hook, arg1, arg2)
2445 Lisp_Object hook, arg1, arg2;
2447 Lisp_Object temp[3];
2448 temp[0] = hook;
2449 temp[1] = arg1;
2450 temp[2] = arg2;
2452 Frun_hook_with_args (3, temp);
2455 /* Apply fn to arg */
2456 Lisp_Object
2457 apply1 (fn, arg)
2458 Lisp_Object fn, arg;
2460 struct gcpro gcpro1;
2462 GCPRO1 (fn);
2463 if (NILP (arg))
2464 RETURN_UNGCPRO (Ffuncall (1, &fn));
2465 gcpro1.nvars = 2;
2466 #ifdef NO_ARG_ARRAY
2468 Lisp_Object args[2];
2469 args[0] = fn;
2470 args[1] = arg;
2471 gcpro1.var = args;
2472 RETURN_UNGCPRO (Fapply (2, args));
2474 #else /* not NO_ARG_ARRAY */
2475 RETURN_UNGCPRO (Fapply (2, &fn));
2476 #endif /* not NO_ARG_ARRAY */
2479 /* Call function fn on no arguments */
2480 Lisp_Object
2481 call0 (fn)
2482 Lisp_Object fn;
2484 struct gcpro gcpro1;
2486 GCPRO1 (fn);
2487 RETURN_UNGCPRO (Ffuncall (1, &fn));
2490 /* Call function fn with 1 argument arg1 */
2491 /* ARGSUSED */
2492 Lisp_Object
2493 call1 (fn, arg1)
2494 Lisp_Object fn, arg1;
2496 struct gcpro gcpro1;
2497 #ifdef NO_ARG_ARRAY
2498 Lisp_Object args[2];
2500 args[0] = fn;
2501 args[1] = arg1;
2502 GCPRO1 (args[0]);
2503 gcpro1.nvars = 2;
2504 RETURN_UNGCPRO (Ffuncall (2, args));
2505 #else /* not NO_ARG_ARRAY */
2506 GCPRO1 (fn);
2507 gcpro1.nvars = 2;
2508 RETURN_UNGCPRO (Ffuncall (2, &fn));
2509 #endif /* not NO_ARG_ARRAY */
2512 /* Call function fn with 2 arguments arg1, arg2 */
2513 /* ARGSUSED */
2514 Lisp_Object
2515 call2 (fn, arg1, arg2)
2516 Lisp_Object fn, arg1, arg2;
2518 struct gcpro gcpro1;
2519 #ifdef NO_ARG_ARRAY
2520 Lisp_Object args[3];
2521 args[0] = fn;
2522 args[1] = arg1;
2523 args[2] = arg2;
2524 GCPRO1 (args[0]);
2525 gcpro1.nvars = 3;
2526 RETURN_UNGCPRO (Ffuncall (3, args));
2527 #else /* not NO_ARG_ARRAY */
2528 GCPRO1 (fn);
2529 gcpro1.nvars = 3;
2530 RETURN_UNGCPRO (Ffuncall (3, &fn));
2531 #endif /* not NO_ARG_ARRAY */
2534 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2535 /* ARGSUSED */
2536 Lisp_Object
2537 call3 (fn, arg1, arg2, arg3)
2538 Lisp_Object fn, arg1, arg2, arg3;
2540 struct gcpro gcpro1;
2541 #ifdef NO_ARG_ARRAY
2542 Lisp_Object args[4];
2543 args[0] = fn;
2544 args[1] = arg1;
2545 args[2] = arg2;
2546 args[3] = arg3;
2547 GCPRO1 (args[0]);
2548 gcpro1.nvars = 4;
2549 RETURN_UNGCPRO (Ffuncall (4, args));
2550 #else /* not NO_ARG_ARRAY */
2551 GCPRO1 (fn);
2552 gcpro1.nvars = 4;
2553 RETURN_UNGCPRO (Ffuncall (4, &fn));
2554 #endif /* not NO_ARG_ARRAY */
2557 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2558 /* ARGSUSED */
2559 Lisp_Object
2560 call4 (fn, arg1, arg2, arg3, arg4)
2561 Lisp_Object fn, arg1, arg2, arg3, arg4;
2563 struct gcpro gcpro1;
2564 #ifdef NO_ARG_ARRAY
2565 Lisp_Object args[5];
2566 args[0] = fn;
2567 args[1] = arg1;
2568 args[2] = arg2;
2569 args[3] = arg3;
2570 args[4] = arg4;
2571 GCPRO1 (args[0]);
2572 gcpro1.nvars = 5;
2573 RETURN_UNGCPRO (Ffuncall (5, args));
2574 #else /* not NO_ARG_ARRAY */
2575 GCPRO1 (fn);
2576 gcpro1.nvars = 5;
2577 RETURN_UNGCPRO (Ffuncall (5, &fn));
2578 #endif /* not NO_ARG_ARRAY */
2581 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2582 /* ARGSUSED */
2583 Lisp_Object
2584 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2585 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2587 struct gcpro gcpro1;
2588 #ifdef NO_ARG_ARRAY
2589 Lisp_Object args[6];
2590 args[0] = fn;
2591 args[1] = arg1;
2592 args[2] = arg2;
2593 args[3] = arg3;
2594 args[4] = arg4;
2595 args[5] = arg5;
2596 GCPRO1 (args[0]);
2597 gcpro1.nvars = 6;
2598 RETURN_UNGCPRO (Ffuncall (6, args));
2599 #else /* not NO_ARG_ARRAY */
2600 GCPRO1 (fn);
2601 gcpro1.nvars = 6;
2602 RETURN_UNGCPRO (Ffuncall (6, &fn));
2603 #endif /* not NO_ARG_ARRAY */
2606 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2607 /* ARGSUSED */
2608 Lisp_Object
2609 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2610 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2612 struct gcpro gcpro1;
2613 #ifdef NO_ARG_ARRAY
2614 Lisp_Object args[7];
2615 args[0] = fn;
2616 args[1] = arg1;
2617 args[2] = arg2;
2618 args[3] = arg3;
2619 args[4] = arg4;
2620 args[5] = arg5;
2621 args[6] = arg6;
2622 GCPRO1 (args[0]);
2623 gcpro1.nvars = 7;
2624 RETURN_UNGCPRO (Ffuncall (7, args));
2625 #else /* not NO_ARG_ARRAY */
2626 GCPRO1 (fn);
2627 gcpro1.nvars = 7;
2628 RETURN_UNGCPRO (Ffuncall (7, &fn));
2629 #endif /* not NO_ARG_ARRAY */
2632 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2633 doc: /* Call first argument as a function, passing remaining arguments to it.
2634 Return the value that function returns.
2635 Thus, (funcall 'cons 'x 'y) returns (x . y).
2636 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2637 (nargs, args)
2638 int nargs;
2639 Lisp_Object *args;
2641 Lisp_Object fun;
2642 Lisp_Object funcar;
2643 int numargs = nargs - 1;
2644 Lisp_Object lisp_numargs;
2645 Lisp_Object val;
2646 struct backtrace backtrace;
2647 register Lisp_Object *internal_args;
2648 register int i;
2650 QUIT;
2651 if (consing_since_gc > gc_cons_threshold)
2652 Fgarbage_collect ();
2654 if (++lisp_eval_depth > max_lisp_eval_depth)
2656 if (max_lisp_eval_depth < 100)
2657 max_lisp_eval_depth = 100;
2658 if (lisp_eval_depth > max_lisp_eval_depth)
2659 error ("Lisp nesting exceeds max-lisp-eval-depth");
2662 backtrace.next = backtrace_list;
2663 backtrace_list = &backtrace;
2664 backtrace.function = &args[0];
2665 backtrace.args = &args[1];
2666 backtrace.nargs = nargs - 1;
2667 backtrace.evalargs = 0;
2668 backtrace.debug_on_exit = 0;
2670 if (debug_on_next_call)
2671 do_debug_on_call (Qlambda);
2673 retry:
2675 fun = args[0];
2677 fun = Findirect_function (fun);
2679 if (SUBRP (fun))
2681 if (numargs < XSUBR (fun)->min_args
2682 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2684 XSETFASTINT (lisp_numargs, numargs);
2685 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2688 if (XSUBR (fun)->max_args == UNEVALLED)
2689 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2691 if (XSUBR (fun)->max_args == MANY)
2693 val = (*XSUBR (fun)->function) (numargs, args + 1);
2694 goto done;
2697 if (XSUBR (fun)->max_args > numargs)
2699 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2700 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2701 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2702 internal_args[i] = Qnil;
2704 else
2705 internal_args = args + 1;
2706 switch (XSUBR (fun)->max_args)
2708 case 0:
2709 val = (*XSUBR (fun)->function) ();
2710 goto done;
2711 case 1:
2712 val = (*XSUBR (fun)->function) (internal_args[0]);
2713 goto done;
2714 case 2:
2715 val = (*XSUBR (fun)->function) (internal_args[0],
2716 internal_args[1]);
2717 goto done;
2718 case 3:
2719 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2720 internal_args[2]);
2721 goto done;
2722 case 4:
2723 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2724 internal_args[2],
2725 internal_args[3]);
2726 goto done;
2727 case 5:
2728 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2729 internal_args[2], internal_args[3],
2730 internal_args[4]);
2731 goto done;
2732 case 6:
2733 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2734 internal_args[2], internal_args[3],
2735 internal_args[4], internal_args[5]);
2736 goto done;
2737 case 7:
2738 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2739 internal_args[2], internal_args[3],
2740 internal_args[4], internal_args[5],
2741 internal_args[6]);
2742 goto done;
2744 case 8:
2745 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2746 internal_args[2], internal_args[3],
2747 internal_args[4], internal_args[5],
2748 internal_args[6], internal_args[7]);
2749 goto done;
2751 default:
2753 /* If a subr takes more than 8 arguments without using MANY
2754 or UNEVALLED, we need to extend this function to support it.
2755 Until this is done, there is no way to call the function. */
2756 abort ();
2759 if (COMPILEDP (fun))
2760 val = funcall_lambda (fun, numargs, args + 1);
2761 else
2763 if (!CONSP (fun))
2764 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2765 funcar = Fcar (fun);
2766 if (!SYMBOLP (funcar))
2767 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2768 if (EQ (funcar, Qlambda))
2769 val = funcall_lambda (fun, numargs, args + 1);
2770 else if (EQ (funcar, Qautoload))
2772 do_autoload (fun, args[0]);
2773 goto retry;
2775 else
2776 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2778 done:
2779 lisp_eval_depth--;
2780 if (backtrace.debug_on_exit)
2781 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2782 backtrace_list = backtrace.next;
2783 return val;
2786 Lisp_Object
2787 apply_lambda (fun, args, eval_flag)
2788 Lisp_Object fun, args;
2789 int eval_flag;
2791 Lisp_Object args_left;
2792 Lisp_Object numargs;
2793 register Lisp_Object *arg_vector;
2794 struct gcpro gcpro1, gcpro2, gcpro3;
2795 register int i;
2796 register Lisp_Object tem;
2798 numargs = Flength (args);
2799 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2800 args_left = args;
2802 GCPRO3 (*arg_vector, args_left, fun);
2803 gcpro1.nvars = 0;
2805 for (i = 0; i < XINT (numargs);)
2807 tem = Fcar (args_left), args_left = Fcdr (args_left);
2808 if (eval_flag) tem = Feval (tem);
2809 arg_vector[i++] = tem;
2810 gcpro1.nvars = i;
2813 UNGCPRO;
2815 if (eval_flag)
2817 backtrace_list->args = arg_vector;
2818 backtrace_list->nargs = i;
2820 backtrace_list->evalargs = 0;
2821 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2823 /* Do the debug-on-exit now, while arg_vector still exists. */
2824 if (backtrace_list->debug_on_exit)
2825 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2826 /* Don't do it again when we return to eval. */
2827 backtrace_list->debug_on_exit = 0;
2828 return tem;
2831 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2832 and return the result of evaluation.
2833 FUN must be either a lambda-expression or a compiled-code object. */
2835 static Lisp_Object
2836 funcall_lambda (fun, nargs, arg_vector)
2837 Lisp_Object fun;
2838 int nargs;
2839 register Lisp_Object *arg_vector;
2841 Lisp_Object val, syms_left, next;
2842 int count = specpdl_ptr - specpdl;
2843 int i, optional, rest;
2845 if (CONSP (fun))
2847 syms_left = XCDR (fun);
2848 if (CONSP (syms_left))
2849 syms_left = XCAR (syms_left);
2850 else
2851 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2853 else if (COMPILEDP (fun))
2854 syms_left = AREF (fun, COMPILED_ARGLIST);
2855 else
2856 abort ();
2858 i = optional = rest = 0;
2859 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2861 QUIT;
2863 next = XCAR (syms_left);
2864 while (!SYMBOLP (next))
2865 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2867 if (EQ (next, Qand_rest))
2868 rest = 1;
2869 else if (EQ (next, Qand_optional))
2870 optional = 1;
2871 else if (rest)
2873 specbind (next, Flist (nargs - i, &arg_vector[i]));
2874 i = nargs;
2876 else if (i < nargs)
2877 specbind (next, arg_vector[i++]);
2878 else if (!optional)
2879 return Fsignal (Qwrong_number_of_arguments,
2880 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2881 else
2882 specbind (next, Qnil);
2885 if (!NILP (syms_left))
2886 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2887 else if (i < nargs)
2888 return Fsignal (Qwrong_number_of_arguments,
2889 Fcons (fun, Fcons (make_number (nargs), Qnil)));
2891 if (CONSP (fun))
2892 val = Fprogn (XCDR (XCDR (fun)));
2893 else
2895 /* If we have not actually read the bytecode string
2896 and constants vector yet, fetch them from the file. */
2897 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2898 Ffetch_bytecode (fun);
2899 val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
2900 AREF (fun, COMPILED_CONSTANTS),
2901 AREF (fun, COMPILED_STACK_DEPTH));
2904 return unbind_to (count, val);
2907 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2908 1, 1, 0,
2909 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2910 (object)
2911 Lisp_Object object;
2913 Lisp_Object tem;
2915 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2917 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2918 if (!CONSP (tem))
2920 tem = AREF (object, COMPILED_BYTECODE);
2921 if (CONSP (tem) && STRINGP (XCAR (tem)))
2922 error ("Invalid byte code in %s", XSTRING (XCAR (tem))->data);
2923 else
2924 error ("Invalid byte code");
2926 AREF (object, COMPILED_BYTECODE) = XCAR (tem);
2927 AREF (object, COMPILED_CONSTANTS) = XCDR (tem);
2929 return object;
2932 void
2933 grow_specpdl ()
2935 register int count = specpdl_ptr - specpdl;
2936 if (specpdl_size >= max_specpdl_size)
2938 if (max_specpdl_size < 400)
2939 max_specpdl_size = 400;
2940 if (specpdl_size >= max_specpdl_size)
2942 if (!NILP (Vdebug_on_error))
2943 /* Leave room for some specpdl in the debugger. */
2944 max_specpdl_size = specpdl_size + 100;
2945 Fsignal (Qerror,
2946 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2949 specpdl_size *= 2;
2950 if (specpdl_size > max_specpdl_size)
2951 specpdl_size = max_specpdl_size;
2952 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2953 specpdl_ptr = specpdl + count;
2956 void
2957 specbind (symbol, value)
2958 Lisp_Object symbol, value;
2960 Lisp_Object ovalue;
2961 Lisp_Object valcontents;
2963 CHECK_SYMBOL (symbol);
2964 if (specpdl_ptr == specpdl + specpdl_size)
2965 grow_specpdl ();
2967 /* The most common case is that of a non-constant symbol with a
2968 trivial value. Make that as fast as we can. */
2969 valcontents = SYMBOL_VALUE (symbol);
2970 if (!MISCP (valcontents) && !SYMBOL_CONSTANT_P (symbol))
2972 specpdl_ptr->symbol = symbol;
2973 specpdl_ptr->old_value = valcontents;
2974 specpdl_ptr->func = NULL;
2975 ++specpdl_ptr;
2976 SET_SYMBOL_VALUE (symbol, value);
2978 else
2980 Lisp_Object valcontents;
2982 ovalue = find_symbol_value (symbol);
2983 specpdl_ptr->func = 0;
2984 specpdl_ptr->old_value = ovalue;
2986 valcontents = XSYMBOL (symbol)->value;
2988 if (BUFFER_LOCAL_VALUEP (valcontents)
2989 || SOME_BUFFER_LOCAL_VALUEP (valcontents)
2990 || BUFFER_OBJFWDP (valcontents))
2992 Lisp_Object where, current_buffer;
2994 current_buffer = Fcurrent_buffer ();
2996 /* For a local variable, record both the symbol and which
2997 buffer's or frame's value we are saving. */
2998 if (!NILP (Flocal_variable_p (symbol, Qnil)))
2999 where = current_buffer;
3000 else if (!BUFFER_OBJFWDP (valcontents)
3001 && XBUFFER_LOCAL_VALUE (valcontents)->found_for_frame)
3002 where = XBUFFER_LOCAL_VALUE (valcontents)->frame;
3003 else
3004 where = Qnil;
3006 /* We're not using the `unused' slot in the specbinding
3007 structure because this would mean we have to do more
3008 work for simple variables. */
3009 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, current_buffer));
3011 /* If SYMBOL is a per-buffer variable which doesn't have a
3012 buffer-local value here, make the `let' change the global
3013 value by changing the value of SYMBOL in all buffers not
3014 having their own value. This is consistent with what
3015 happens with other buffer-local variables. */
3016 if (NILP (where)
3017 && BUFFER_OBJFWDP (valcontents))
3019 ++specpdl_ptr;
3020 Fset_default (symbol, value);
3021 return;
3024 else
3025 specpdl_ptr->symbol = symbol;
3027 specpdl_ptr++;
3028 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
3029 store_symval_forwarding (symbol, ovalue, value, NULL);
3030 else
3031 set_internal (symbol, value, 0, 1);
3035 void
3036 record_unwind_protect (function, arg)
3037 Lisp_Object (*function) P_ ((Lisp_Object));
3038 Lisp_Object arg;
3040 if (specpdl_ptr == specpdl + specpdl_size)
3041 grow_specpdl ();
3042 specpdl_ptr->func = function;
3043 specpdl_ptr->symbol = Qnil;
3044 specpdl_ptr->old_value = arg;
3045 specpdl_ptr++;
3048 Lisp_Object
3049 unbind_to (count, value)
3050 int count;
3051 Lisp_Object value;
3053 int quitf = !NILP (Vquit_flag);
3054 struct gcpro gcpro1;
3056 GCPRO1 (value);
3057 Vquit_flag = Qnil;
3059 while (specpdl_ptr != specpdl + count)
3061 --specpdl_ptr;
3063 if (specpdl_ptr->func != 0)
3064 (*specpdl_ptr->func) (specpdl_ptr->old_value);
3065 /* Note that a "binding" of nil is really an unwind protect,
3066 so in that case the "old value" is a list of forms to evaluate. */
3067 else if (NILP (specpdl_ptr->symbol))
3068 Fprogn (specpdl_ptr->old_value);
3069 /* If the symbol is a list, it is really (SYMBOL WHERE
3070 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3071 frame. If WHERE is a buffer or frame, this indicates we
3072 bound a variable that had a buffer-local or frame-local
3073 binding. WHERE nil means that the variable had the default
3074 value when it was bound. CURRENT-BUFFER is the buffer that
3075 was current when the variable was bound. */
3076 else if (CONSP (specpdl_ptr->symbol))
3078 Lisp_Object symbol, where;
3080 symbol = XCAR (specpdl_ptr->symbol);
3081 where = XCAR (XCDR (specpdl_ptr->symbol));
3083 if (NILP (where))
3084 Fset_default (symbol, specpdl_ptr->old_value);
3085 else if (BUFFERP (where))
3086 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (where), 1);
3087 else
3088 set_internal (symbol, specpdl_ptr->old_value, NULL, 1);
3090 else
3092 /* If variable has a trivial value (no forwarding), we can
3093 just set it. No need to check for constant symbols here,
3094 since that was already done by specbind. */
3095 if (!MISCP (SYMBOL_VALUE (specpdl_ptr->symbol)))
3096 SET_SYMBOL_VALUE (specpdl_ptr->symbol, specpdl_ptr->old_value);
3097 else
3098 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
3102 if (NILP (Vquit_flag) && quitf)
3103 Vquit_flag = Qt;
3105 UNGCPRO;
3106 return value;
3109 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3110 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3111 The debugger is entered when that frame exits, if the flag is non-nil. */)
3112 (level, flag)
3113 Lisp_Object level, flag;
3115 register struct backtrace *backlist = backtrace_list;
3116 register int i;
3118 CHECK_NUMBER (level);
3120 for (i = 0; backlist && i < XINT (level); i++)
3122 backlist = backlist->next;
3125 if (backlist)
3126 backlist->debug_on_exit = !NILP (flag);
3128 return flag;
3131 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3132 doc: /* Print a trace of Lisp function calls currently active.
3133 Output stream used is value of `standard-output'. */)
3136 register struct backtrace *backlist = backtrace_list;
3137 register int i;
3138 Lisp_Object tail;
3139 Lisp_Object tem;
3140 extern Lisp_Object Vprint_level;
3141 struct gcpro gcpro1;
3143 XSETFASTINT (Vprint_level, 3);
3145 tail = Qnil;
3146 GCPRO1 (tail);
3148 while (backlist)
3150 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3151 if (backlist->nargs == UNEVALLED)
3153 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3154 write_string ("\n", -1);
3156 else
3158 tem = *backlist->function;
3159 Fprin1 (tem, Qnil); /* This can QUIT */
3160 write_string ("(", -1);
3161 if (backlist->nargs == MANY)
3163 for (tail = *backlist->args, i = 0;
3164 !NILP (tail);
3165 tail = Fcdr (tail), i++)
3167 if (i) write_string (" ", -1);
3168 Fprin1 (Fcar (tail), Qnil);
3171 else
3173 for (i = 0; i < backlist->nargs; i++)
3175 if (i) write_string (" ", -1);
3176 Fprin1 (backlist->args[i], Qnil);
3179 write_string (")\n", -1);
3181 backlist = backlist->next;
3184 Vprint_level = Qnil;
3185 UNGCPRO;
3186 return Qnil;
3189 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3190 doc: /* Return the function and arguments NFRAMES up from current execution point.
3191 If that frame has not evaluated the arguments yet (or is a special form),
3192 the value is (nil FUNCTION ARG-FORMS...).
3193 If that frame has evaluated its arguments and called its function already,
3194 the value is (t FUNCTION ARG-VALUES...).
3195 A &rest arg is represented as the tail of the list ARG-VALUES.
3196 FUNCTION is whatever was supplied as car of evaluated list,
3197 or a lambda expression for macro calls.
3198 If NFRAMES is more than the number of frames, the value is nil. */)
3199 (nframes)
3200 Lisp_Object nframes;
3202 register struct backtrace *backlist = backtrace_list;
3203 register int i;
3204 Lisp_Object tem;
3206 CHECK_NATNUM (nframes);
3208 /* Find the frame requested. */
3209 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3210 backlist = backlist->next;
3212 if (!backlist)
3213 return Qnil;
3214 if (backlist->nargs == UNEVALLED)
3215 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3216 else
3218 if (backlist->nargs == MANY)
3219 tem = *backlist->args;
3220 else
3221 tem = Flist (backlist->nargs, backlist->args);
3223 return Fcons (Qt, Fcons (*backlist->function, tem));
3228 void
3229 syms_of_eval ()
3231 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3232 doc: /* *Limit on number of Lisp variable bindings & unwind-protects.
3233 If Lisp code tries to make more than this many at once,
3234 an error is signaled. */);
3236 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3237 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3238 This limit is to catch infinite recursions for you before they cause
3239 actual stack overflow in C, which would be fatal for Emacs.
3240 You can safely make it considerably larger than its default value,
3241 if that proves inconveniently small. */);
3243 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3244 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3245 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'. */);
3246 Vquit_flag = Qnil;
3248 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3249 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3250 Note that `quit-flag' will still be set by typing C-g,
3251 so a quit will be signaled as soon as `inhibit-quit' is nil.
3252 To prevent this happening, set `quit-flag' to nil
3253 before making `inhibit-quit' nil. */);
3254 Vinhibit_quit = Qnil;
3256 Qinhibit_quit = intern ("inhibit-quit");
3257 staticpro (&Qinhibit_quit);
3259 Qautoload = intern ("autoload");
3260 staticpro (&Qautoload);
3262 Qdebug_on_error = intern ("debug-on-error");
3263 staticpro (&Qdebug_on_error);
3265 Qmacro = intern ("macro");
3266 staticpro (&Qmacro);
3268 Qdeclare = intern ("declare");
3269 staticpro (&Qdeclare);
3271 /* Note that the process handling also uses Qexit, but we don't want
3272 to staticpro it twice, so we just do it here. */
3273 Qexit = intern ("exit");
3274 staticpro (&Qexit);
3276 Qinteractive = intern ("interactive");
3277 staticpro (&Qinteractive);
3279 Qcommandp = intern ("commandp");
3280 staticpro (&Qcommandp);
3282 Qdefun = intern ("defun");
3283 staticpro (&Qdefun);
3285 Qand_rest = intern ("&rest");
3286 staticpro (&Qand_rest);
3288 Qand_optional = intern ("&optional");
3289 staticpro (&Qand_optional);
3291 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3292 doc: /* *Non-nil means errors display a backtrace buffer.
3293 More precisely, this happens for any error that is handled
3294 by the editor command loop.
3295 If the value is a list, an error only means to display a backtrace
3296 if one of its condition symbols appears in the list. */);
3297 Vstack_trace_on_error = Qnil;
3299 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3300 doc: /* *Non-nil means enter debugger if an error is signaled.
3301 Does not apply to errors handled by `condition-case' or those
3302 matched by `debug-ignored-errors'.
3303 If the value is a list, an error only means to enter the debugger
3304 if one of its condition symbols appears in the list.
3305 When you evaluate an expression interactively, this variable
3306 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3307 See also variable `debug-on-quit'. */);
3308 Vdebug_on_error = Qnil;
3310 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3311 doc: /* *List of errors for which the debugger should not be called.
3312 Each element may be a condition-name or a regexp that matches error messages.
3313 If any element applies to a given error, that error skips the debugger
3314 and just returns to top level.
3315 This overrides the variable `debug-on-error'.
3316 It does not apply to errors handled by `condition-case'. */);
3317 Vdebug_ignored_errors = Qnil;
3319 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3320 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3321 Does not apply if quit is handled by a `condition-case'.
3322 When you evaluate an expression interactively, this variable
3323 is temporarily non-nil if `eval-expression-debug-on-quit' is non-nil. */);
3324 debug_on_quit = 0;
3326 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3327 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3329 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3330 doc: /* Non-nil means debugger may continue execution.
3331 This is nil when the debugger is called under circumstances where it
3332 might not be safe to continue. */);
3333 debugger_may_continue = 1;
3335 DEFVAR_LISP ("debugger", &Vdebugger,
3336 doc: /* Function to call to invoke debugger.
3337 If due to frame exit, args are `exit' and the value being returned;
3338 this function's value will be returned instead of that.
3339 If due to error, args are `error' and a list of the args to `signal'.
3340 If due to `apply' or `funcall' entry, one arg, `lambda'.
3341 If due to `eval' entry, one arg, t. */);
3342 Vdebugger = Qnil;
3344 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3345 doc: /* If non-nil, this is a function for `signal' to call.
3346 It receives the same arguments that `signal' was given.
3347 The Edebug package uses this to regain control. */);
3348 Vsignal_hook_function = Qnil;
3350 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3351 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3352 Note that `debug-on-error', `debug-on-quit' and friends
3353 still determine whether to handle the particular condition. */);
3354 Vdebug_on_signal = Qnil;
3356 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3357 doc: /* Function to process declarations in a macro definition.
3358 The function will be called with two args MACRO and DECL.
3359 MACRO is the name of the macro being defined.
3360 DECL is a list `(declare ...)' containing the declarations.
3361 The value the function returns is not used. */);
3362 Vmacro_declaration_function = Qnil;
3364 Vrun_hooks = intern ("run-hooks");
3365 staticpro (&Vrun_hooks);
3367 staticpro (&Vautoload_queue);
3368 Vautoload_queue = Qnil;
3369 staticpro (&Vsignaling_function);
3370 Vsignaling_function = Qnil;
3372 defsubr (&Sor);
3373 defsubr (&Sand);
3374 defsubr (&Sif);
3375 defsubr (&Scond);
3376 defsubr (&Sprogn);
3377 defsubr (&Sprog1);
3378 defsubr (&Sprog2);
3379 defsubr (&Ssetq);
3380 defsubr (&Squote);
3381 defsubr (&Sfunction);
3382 defsubr (&Sdefun);
3383 defsubr (&Sdefmacro);
3384 defsubr (&Sdefvar);
3385 defsubr (&Sdefvaralias);
3386 defsubr (&Sdefconst);
3387 defsubr (&Suser_variable_p);
3388 defsubr (&Slet);
3389 defsubr (&SletX);
3390 defsubr (&Swhile);
3391 defsubr (&Smacroexpand);
3392 defsubr (&Scatch);
3393 defsubr (&Sthrow);
3394 defsubr (&Sunwind_protect);
3395 defsubr (&Scondition_case);
3396 defsubr (&Ssignal);
3397 defsubr (&Sinteractive_p);
3398 defsubr (&Scommandp);
3399 defsubr (&Sautoload);
3400 defsubr (&Seval);
3401 defsubr (&Sapply);
3402 defsubr (&Sfuncall);
3403 defsubr (&Srun_hooks);
3404 defsubr (&Srun_hook_with_args);
3405 defsubr (&Srun_hook_with_args_until_success);
3406 defsubr (&Srun_hook_with_args_until_failure);
3407 defsubr (&Sfetch_bytecode);
3408 defsubr (&Sbacktrace_debug);
3409 defsubr (&Sbacktrace);
3410 defsubr (&Sbacktrace_frame);