Merge from trunk
[emacs.git] / src / eval.c
blob6d0a49c0d7e3b819ea9182877d7fff1f826b35aa
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 /* This definition is duplicated in alloc.c and keyboard.c */
36 /* Putting it in lisp.h makes cc bomb out! */
38 struct backtrace
40 struct backtrace *next;
41 Lisp_Object *function;
42 Lisp_Object *args; /* Points to vector of args. */
43 int nargs; /* Length of vector.
44 If nargs is UNEVALLED, args points to slot holding
45 list of unevalled args */
46 char evalargs;
47 /* Nonzero means call value of debugger when done with this operation. */
48 char debug_on_exit;
51 struct backtrace *backtrace_list;
53 struct catchtag *catchlist;
55 #ifdef DEBUG_GCPRO
56 /* Count levels of GCPRO to detect failure to UNGCPRO. */
57 int gcpro_level;
58 #endif
60 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
61 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
62 Lisp_Object Qand_rest, Qand_optional;
63 Lisp_Object Qdebug_on_error;
64 Lisp_Object Qdeclare;
65 Lisp_Object Qcurry;
66 Lisp_Object Qinternal_interpreter_environment, Qclosure;
68 Lisp_Object Qdebug;
70 /* This holds either the symbol `run-hooks' or nil.
71 It is nil at an early stage of startup, and when Emacs
72 is shutting down. */
74 Lisp_Object Vrun_hooks;
76 /* Non-nil means record all fset's and provide's, to be undone
77 if the file being autoloaded is not fully loaded.
78 They are recorded by being consed onto the front of Vautoload_queue:
79 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
81 Lisp_Object Vautoload_queue;
83 /* When lexical binding is being used, this is non-nil, and contains an
84 alist of lexically-bound variable, or t, indicating an empty
85 environment. The lisp name of this variable is
86 `internal-interpreter-lexical-environment'. */
88 Lisp_Object Vinternal_interpreter_environment;
90 /* Current number of specbindings allocated in specpdl. */
92 int specpdl_size;
94 /* Pointer to beginning of specpdl. */
96 struct specbinding *specpdl;
98 /* Pointer to first unused element in specpdl. */
100 struct specbinding *specpdl_ptr;
102 /* Maximum size allowed for specpdl allocation */
104 EMACS_INT max_specpdl_size;
106 /* Depth in Lisp evaluations and function calls. */
108 int lisp_eval_depth;
110 /* Maximum allowed depth in Lisp evaluations and function calls. */
112 EMACS_INT max_lisp_eval_depth;
114 /* Nonzero means enter debugger before next function call */
116 int debug_on_next_call;
118 /* Non-zero means debugger may continue. This is zero when the
119 debugger is called during redisplay, where it might not be safe to
120 continue the interrupted redisplay. */
122 int debugger_may_continue;
124 /* List of conditions (non-nil atom means all) which cause a backtrace
125 if an error is handled by the command loop's error handler. */
127 Lisp_Object Vstack_trace_on_error;
129 /* List of conditions (non-nil atom means all) which enter the debugger
130 if an error is handled by the command loop's error handler. */
132 Lisp_Object Vdebug_on_error;
134 /* List of conditions and regexps specifying error messages which
135 do not enter the debugger even if Vdebug_on_error says they should. */
137 Lisp_Object Vdebug_ignored_errors;
139 /* Non-nil means call the debugger even if the error will be handled. */
141 Lisp_Object Vdebug_on_signal;
143 /* Hook for edebug to use. */
145 Lisp_Object Vsignal_hook_function;
147 /* Nonzero means enter debugger if a quit signal
148 is handled by the command loop's error handler. */
150 int debug_on_quit;
152 /* The value of num_nonmacro_input_events as of the last time we
153 started to enter the debugger. If we decide to enter the debugger
154 again when this is still equal to num_nonmacro_input_events, then we
155 know that the debugger itself has an error, and we should just
156 signal the error instead of entering an infinite loop of debugger
157 invocations. */
159 int when_entered_debugger;
161 Lisp_Object Vdebugger;
163 /* The function from which the last `signal' was called. Set in
164 Fsignal. */
166 Lisp_Object Vsignaling_function;
168 /* Set to non-zero while processing X events. Checked in Feval to
169 make sure the Lisp interpreter isn't called from a signal handler,
170 which is unsafe because the interpreter isn't reentrant. */
172 int handling_signal;
174 /* Function to process declarations in defmacro forms. */
176 Lisp_Object Vmacro_declaration_function;
178 static Lisp_Object funcall_lambda (Lisp_Object, int, Lisp_Object *,
179 Lisp_Object);
180 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
182 void
183 init_eval_once (void)
185 specpdl_size = 50;
186 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
187 specpdl_ptr = specpdl;
188 /* Don't forget to update docs (lispref node "Local Variables"). */
189 max_specpdl_size = 1000;
190 max_lisp_eval_depth = 500;
192 Vrun_hooks = Qnil;
195 void
196 init_eval (void)
198 specpdl_ptr = specpdl;
199 catchlist = 0;
200 handlerlist = 0;
201 backtrace_list = 0;
202 Vquit_flag = Qnil;
203 debug_on_next_call = 0;
204 lisp_eval_depth = 0;
205 #ifdef DEBUG_GCPRO
206 gcpro_level = 0;
207 #endif
208 /* This is less than the initial value of num_nonmacro_input_events. */
209 when_entered_debugger = -1;
212 /* unwind-protect function used by call_debugger. */
214 static Lisp_Object
215 restore_stack_limits (Lisp_Object data)
217 max_specpdl_size = XINT (XCAR (data));
218 max_lisp_eval_depth = XINT (XCDR (data));
219 return Qnil;
222 /* Call the Lisp debugger, giving it argument ARG. */
224 Lisp_Object
225 call_debugger (Lisp_Object arg)
227 int debug_while_redisplaying;
228 int count = SPECPDL_INDEX ();
229 Lisp_Object val;
230 int old_max = max_specpdl_size;
232 /* Temporarily bump up the stack limits,
233 so the debugger won't run out of stack. */
235 max_specpdl_size += 1;
236 record_unwind_protect (restore_stack_limits,
237 Fcons (make_number (old_max),
238 make_number (max_lisp_eval_depth)));
239 max_specpdl_size = old_max;
241 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
242 max_lisp_eval_depth = lisp_eval_depth + 40;
244 if (SPECPDL_INDEX () + 100 > max_specpdl_size)
245 max_specpdl_size = SPECPDL_INDEX () + 100;
247 #ifdef HAVE_WINDOW_SYSTEM
248 if (display_hourglass_p)
249 cancel_hourglass ();
250 #endif
252 debug_on_next_call = 0;
253 when_entered_debugger = num_nonmacro_input_events;
255 /* Resetting redisplaying_p to 0 makes sure that debug output is
256 displayed if the debugger is invoked during redisplay. */
257 debug_while_redisplaying = redisplaying_p;
258 redisplaying_p = 0;
259 specbind (intern ("debugger-may-continue"),
260 debug_while_redisplaying ? Qnil : Qt);
261 specbind (Qinhibit_redisplay, Qnil);
262 specbind (Qdebug_on_error, Qnil);
264 #if 0 /* Binding this prevents execution of Lisp code during
265 redisplay, which necessarily leads to display problems. */
266 specbind (Qinhibit_eval_during_redisplay, Qt);
267 #endif
269 val = apply1 (Vdebugger, arg);
271 /* Interrupting redisplay and resuming it later is not safe under
272 all circumstances. So, when the debugger returns, abort the
273 interrupted redisplay by going back to the top-level. */
274 if (debug_while_redisplaying)
275 Ftop_level ();
277 return unbind_to (count, val);
280 void
281 do_debug_on_call (Lisp_Object code)
283 debug_on_next_call = 0;
284 backtrace_list->debug_on_exit = 1;
285 call_debugger (Fcons (code, Qnil));
288 /* NOTE!!! Every function that can call EVAL must protect its args
289 and temporaries from garbage collection while it needs them.
290 The definition of `For' shows what you have to do. */
292 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
293 doc: /* Eval args until one of them yields non-nil, then return that value.
294 The remaining args are not evalled at all.
295 If all args return nil, return nil.
296 usage: (or CONDITIONS...) */)
297 (Lisp_Object args)
299 register Lisp_Object val = Qnil;
300 struct gcpro gcpro1;
302 GCPRO1 (args);
304 while (CONSP (args))
306 val = Feval (XCAR (args));
307 if (!NILP (val))
308 break;
309 args = XCDR (args);
312 UNGCPRO;
313 return val;
316 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
317 doc: /* Eval args until one of them yields nil, then return nil.
318 The remaining args are not evalled at all.
319 If no arg yields nil, return the last arg's value.
320 usage: (and CONDITIONS...) */)
321 (Lisp_Object args)
323 register Lisp_Object val = Qt;
324 struct gcpro gcpro1;
326 GCPRO1 (args);
328 while (CONSP (args))
330 val = Feval (XCAR (args));
331 if (NILP (val))
332 break;
333 args = XCDR (args);
336 UNGCPRO;
337 return val;
340 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
341 doc: /* If COND yields non-nil, do THEN, else do ELSE...
342 Returns the value of THEN or the value of the last of the ELSE's.
343 THEN must be one expression, but ELSE... can be zero or more expressions.
344 If COND yields nil, and there are no ELSE's, the value is nil.
345 usage: (if COND THEN ELSE...) */)
346 (Lisp_Object args)
348 register Lisp_Object cond;
349 struct gcpro gcpro1;
351 GCPRO1 (args);
352 cond = Feval (Fcar (args));
353 UNGCPRO;
355 if (!NILP (cond))
356 return Feval (Fcar (Fcdr (args)));
357 return Fprogn (Fcdr (Fcdr (args)));
360 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
361 doc: /* Try each clause until one succeeds.
362 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
363 and, if the value is non-nil, this clause succeeds:
364 then the expressions in BODY are evaluated and the last one's
365 value is the value of the cond-form.
366 If no clause succeeds, cond returns nil.
367 If a clause has one element, as in (CONDITION),
368 CONDITION's value if non-nil is returned from the cond-form.
369 usage: (cond CLAUSES...) */)
370 (Lisp_Object args)
372 register Lisp_Object clause, val;
373 struct gcpro gcpro1;
375 val = Qnil;
376 GCPRO1 (args);
377 while (!NILP (args))
379 clause = Fcar (args);
380 val = Feval (Fcar (clause));
381 if (!NILP (val))
383 if (!EQ (XCDR (clause), Qnil))
384 val = Fprogn (XCDR (clause));
385 break;
387 args = XCDR (args);
389 UNGCPRO;
391 return val;
394 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
395 doc: /* Eval BODY forms sequentially and return value of last one.
396 usage: (progn BODY...) */)
397 (Lisp_Object args)
399 register Lisp_Object val = Qnil;
400 struct gcpro gcpro1;
402 GCPRO1 (args);
404 while (CONSP (args))
406 val = Feval (XCAR (args));
407 args = XCDR (args);
410 UNGCPRO;
411 return val;
414 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
415 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
416 The value of FIRST is saved during the evaluation of the remaining args,
417 whose values are discarded.
418 usage: (prog1 FIRST BODY...) */)
419 (Lisp_Object args)
421 Lisp_Object val;
422 register Lisp_Object args_left;
423 struct gcpro gcpro1, gcpro2;
424 register int argnum = 0;
426 if (NILP (args))
427 return Qnil;
429 args_left = args;
430 val = Qnil;
431 GCPRO2 (args, val);
435 if (!(argnum++))
436 val = Feval (Fcar (args_left));
437 else
438 Feval (Fcar (args_left));
439 args_left = Fcdr (args_left);
441 while (!NILP(args_left));
443 UNGCPRO;
444 return val;
447 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
448 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
449 The value of FORM2 is saved during the evaluation of the
450 remaining args, whose values are discarded.
451 usage: (prog2 FORM1 FORM2 BODY...) */)
452 (Lisp_Object args)
454 Lisp_Object val;
455 register Lisp_Object args_left;
456 struct gcpro gcpro1, gcpro2;
457 register int argnum = -1;
459 val = Qnil;
461 if (NILP (args))
462 return Qnil;
464 args_left = args;
465 val = Qnil;
466 GCPRO2 (args, val);
470 if (!(argnum++))
471 val = Feval (Fcar (args_left));
472 else
473 Feval (Fcar (args_left));
474 args_left = Fcdr (args_left);
476 while (!NILP (args_left));
478 UNGCPRO;
479 return val;
482 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
483 doc: /* Set each SYM to the value of its VAL.
484 The symbols SYM are variables; they are literal (not evaluated).
485 The values VAL are expressions; they are evaluated.
486 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
487 The second VAL is not computed until after the first SYM is set, and so on;
488 each VAL can use the new value of variables set earlier in the `setq'.
489 The return value of the `setq' form is the value of the last VAL.
490 usage: (setq [SYM VAL]...) */)
491 (Lisp_Object args)
493 register Lisp_Object args_left;
494 register Lisp_Object val, sym, lex_binding;
495 struct gcpro gcpro1;
497 if (NILP (args))
498 return Qnil;
500 args_left = args;
501 GCPRO1 (args);
505 val = Feval (Fcar (Fcdr (args_left)));
506 sym = Fcar (args_left);
508 if (!NILP (Vinternal_interpreter_environment)
509 && SYMBOLP (sym)
510 && !XSYMBOL (sym)->declared_special
511 && !NILP (lex_binding = Fassq (sym, Vinternal_interpreter_environment)))
512 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
513 else
514 Fset (sym, val); /* SYM is dynamically bound. */
516 args_left = Fcdr (Fcdr (args_left));
518 while (!NILP(args_left));
520 UNGCPRO;
521 return val;
524 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
525 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
526 usage: (quote ARG) */)
527 (Lisp_Object args)
529 if (!NILP (Fcdr (args)))
530 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
531 return Fcar (args);
534 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
535 doc: /* Like `quote', but preferred for objects which are functions.
536 In byte compilation, `function' causes its argument to be compiled.
537 `quote' cannot do that.
538 usage: (function ARG) */)
539 (Lisp_Object args)
541 Lisp_Object quoted = XCAR (args);
543 if (!NILP (Fcdr (args)))
544 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
546 if (!NILP (Vinternal_interpreter_environment)
547 && CONSP (quoted)
548 && EQ (XCAR (quoted), Qlambda))
549 /* This is a lambda expression within a lexical environment;
550 return an interpreted closure instead of a simple lambda. */
551 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment, quoted));
552 else
553 /* Simply quote the argument. */
554 return quoted;
558 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
559 doc: /* Return t if the containing function was run directly by user input.
560 This means that the function was called with `call-interactively'
561 \(which includes being called as the binding of a key)
562 and input is currently coming from the keyboard (not a keyboard macro),
563 and Emacs is not running in batch mode (`noninteractive' is nil).
565 The only known proper use of `interactive-p' is in deciding whether to
566 display a helpful message, or how to display it. If you're thinking
567 of using it for any other purpose, it is quite likely that you're
568 making a mistake. Think: what do you want to do when the command is
569 called from a keyboard macro?
571 To test whether your function was called with `call-interactively',
572 either (i) add an extra optional argument and give it an `interactive'
573 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
574 use `called-interactively-p'. */)
575 (void)
577 return interactive_p (1) ? Qt : Qnil;
581 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
582 doc: /* Return t if the containing function was called by `call-interactively'.
583 If KIND is `interactive', then only return t if the call was made
584 interactively by the user, i.e. not in `noninteractive' mode nor
585 when `executing-kbd-macro'.
586 If KIND is `any', on the other hand, it will return t for any kind of
587 interactive call, including being called as the binding of a key, or
588 from a keyboard macro, or in `noninteractive' mode.
590 The only known proper use of `interactive' for KIND is in deciding
591 whether to display a helpful message, or how to display it. If you're
592 thinking of using it for any other purpose, it is quite likely that
593 you're making a mistake. Think: what do you want to do when the
594 command is called from a keyboard macro?
596 This function is meant for implementing advice and other
597 function-modifying features. Instead of using this, it is sometimes
598 cleaner to give your function an extra optional argument whose
599 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
600 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
601 (Lisp_Object kind)
603 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
604 && interactive_p (1)) ? Qt : Qnil;
608 /* Return 1 if function in which this appears was called using
609 call-interactively.
611 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
612 called is a built-in. */
615 interactive_p (int exclude_subrs_p)
617 struct backtrace *btp;
618 Lisp_Object fun;
620 btp = backtrace_list;
622 /* If this isn't a byte-compiled function, there may be a frame at
623 the top for Finteractive_p. If so, skip it. */
624 fun = Findirect_function (*btp->function, Qnil);
625 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
626 || XSUBR (fun) == &Scalled_interactively_p))
627 btp = btp->next;
629 /* If we're running an Emacs 18-style byte-compiled function, there
630 may be a frame for Fbytecode at the top level. In any version of
631 Emacs there can be Fbytecode frames for subexpressions evaluated
632 inside catch and condition-case. Skip past them.
634 If this isn't a byte-compiled function, then we may now be
635 looking at several frames for special forms. Skip past them. */
636 while (btp
637 && (EQ (*btp->function, Qbytecode)
638 || btp->nargs == UNEVALLED))
639 btp = btp->next;
641 /* btp now points at the frame of the innermost function that isn't
642 a special form, ignoring frames for Finteractive_p and/or
643 Fbytecode at the top. If this frame is for a built-in function
644 (such as load or eval-region) return nil. */
645 fun = Findirect_function (*btp->function, Qnil);
646 if (exclude_subrs_p && SUBRP (fun))
647 return 0;
649 /* btp points to the frame of a Lisp function that called interactive-p.
650 Return t if that function was called interactively. */
651 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
652 return 1;
653 return 0;
657 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
658 doc: /* Define NAME as a function.
659 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
660 See also the function `interactive'.
661 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
662 (Lisp_Object args)
664 register Lisp_Object fn_name;
665 register Lisp_Object defn;
667 fn_name = Fcar (args);
668 CHECK_SYMBOL (fn_name);
669 defn = Fcons (Qlambda, Fcdr (args));
670 if (! NILP (Vinternal_interpreter_environment))
671 defn = Fcons (Qclosure, Fcons (Vinternal_interpreter_environment, defn));
672 if (!NILP (Vpurify_flag))
673 defn = Fpurecopy (defn);
674 if (CONSP (XSYMBOL (fn_name)->function)
675 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
676 LOADHIST_ATTACH (Fcons (Qt, fn_name));
677 Ffset (fn_name, defn);
678 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
679 return fn_name;
682 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
683 doc: /* Define NAME as a macro.
684 The actual definition looks like
685 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
686 When the macro is called, as in (NAME ARGS...),
687 the function (lambda ARGLIST BODY...) is applied to
688 the list ARGS... as it appears in the expression,
689 and the result should be a form to be evaluated instead of the original.
691 DECL is a declaration, optional, which can specify how to indent
692 calls to this macro, how Edebug should handle it, and which argument
693 should be treated as documentation. It looks like this:
694 (declare SPECS...)
695 The elements can look like this:
696 (indent INDENT)
697 Set NAME's `lisp-indent-function' property to INDENT.
699 (debug DEBUG)
700 Set NAME's `edebug-form-spec' property to DEBUG. (This is
701 equivalent to writing a `def-edebug-spec' for the macro.)
703 (doc-string ELT)
704 Set NAME's `doc-string-elt' property to ELT.
706 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
707 (Lisp_Object args)
709 register Lisp_Object fn_name;
710 register Lisp_Object defn;
711 Lisp_Object lambda_list, doc, tail;
713 fn_name = Fcar (args);
714 CHECK_SYMBOL (fn_name);
715 lambda_list = Fcar (Fcdr (args));
716 tail = Fcdr (Fcdr (args));
718 doc = Qnil;
719 if (STRINGP (Fcar (tail)))
721 doc = XCAR (tail);
722 tail = XCDR (tail);
725 while (CONSP (Fcar (tail))
726 && EQ (Fcar (Fcar (tail)), Qdeclare))
728 if (!NILP (Vmacro_declaration_function))
730 struct gcpro gcpro1;
731 GCPRO1 (args);
732 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
733 UNGCPRO;
736 tail = Fcdr (tail);
739 if (NILP (doc))
740 tail = Fcons (lambda_list, tail);
741 else
742 tail = Fcons (lambda_list, Fcons (doc, tail));
744 defn = Fcons (Qlambda, tail);
745 if (! NILP (Vinternal_interpreter_environment))
746 defn = Fcons (Qclosure, Fcons (Vinternal_interpreter_environment, defn));
747 defn = Fcons (Qmacro, defn);
749 if (!NILP (Vpurify_flag))
750 defn = Fpurecopy (defn);
751 if (CONSP (XSYMBOL (fn_name)->function)
752 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
753 LOADHIST_ATTACH (Fcons (Qt, fn_name));
754 Ffset (fn_name, defn);
755 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
756 return fn_name;
760 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
761 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
762 Aliased variables always have the same value; setting one sets the other.
763 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
764 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
765 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
766 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
767 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
768 The return value is BASE-VARIABLE. */)
769 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
771 struct Lisp_Symbol *sym;
773 CHECK_SYMBOL (new_alias);
774 CHECK_SYMBOL (base_variable);
776 sym = XSYMBOL (new_alias);
778 if (sym->constant)
779 /* Not sure why, but why not? */
780 error ("Cannot make a constant an alias");
782 switch (sym->redirect)
784 case SYMBOL_FORWARDED:
785 error ("Cannot make an internal variable an alias");
786 case SYMBOL_LOCALIZED:
787 error ("Don't know how to make a localized variable an alias");
790 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
791 If n_a is bound, but b_v is not, set the value of b_v to n_a,
792 so that old-code that affects n_a before the aliasing is setup
793 still works. */
794 if (NILP (Fboundp (base_variable)))
795 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
798 struct specbinding *p;
800 for (p = specpdl_ptr - 1; p >= specpdl; p--)
801 if (p->func == NULL
802 && (EQ (new_alias,
803 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
804 error ("Don't know how to make a let-bound variable an alias");
807 sym->declared_special = 1;
808 sym->redirect = SYMBOL_VARALIAS;
809 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
810 sym->constant = SYMBOL_CONSTANT_P (base_variable);
811 LOADHIST_ATTACH (new_alias);
812 /* Even if docstring is nil: remove old docstring. */
813 Fput (new_alias, Qvariable_documentation, docstring);
815 return base_variable;
819 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
820 doc: /* Define SYMBOL as a variable, and return SYMBOL.
821 You are not required to define a variable in order to use it,
822 but the definition can supply documentation and an initial value
823 in a way that tags can recognize.
825 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
826 If SYMBOL is buffer-local, its default value is what is set;
827 buffer-local values are not affected.
828 INITVALUE and DOCSTRING are optional.
829 If DOCSTRING starts with *, this variable is identified as a user option.
830 This means that M-x set-variable recognizes it.
831 See also `user-variable-p'.
832 If INITVALUE is missing, SYMBOL's value is not set.
834 If SYMBOL has a local binding, then this form affects the local
835 binding. This is usually not what you want. Thus, if you need to
836 load a file defining variables, with this form or with `defconst' or
837 `defcustom', you should always load that file _outside_ any bindings
838 for these variables. \(`defconst' and `defcustom' behave similarly in
839 this respect.)
840 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
841 (Lisp_Object args)
843 register Lisp_Object sym, tem, tail;
845 sym = Fcar (args);
846 tail = Fcdr (args);
847 if (!NILP (Fcdr (Fcdr (tail))))
848 error ("Too many arguments");
850 tem = Fdefault_boundp (sym);
851 if (!NILP (tail))
853 if (SYMBOL_CONSTANT_P (sym))
855 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
856 Lisp_Object tem = Fcar (tail);
857 if (! (CONSP (tem)
858 && EQ (XCAR (tem), Qquote)
859 && CONSP (XCDR (tem))
860 && EQ (XCAR (XCDR (tem)), sym)))
861 error ("Constant symbol `%s' specified in defvar",
862 SDATA (SYMBOL_NAME (sym)));
865 if (NILP (tem))
866 Fset_default (sym, Feval (Fcar (tail)));
867 else
868 { /* Check if there is really a global binding rather than just a let
869 binding that shadows the global unboundness of the var. */
870 volatile struct specbinding *pdl = specpdl_ptr;
871 while (--pdl >= specpdl)
873 if (EQ (pdl->symbol, sym) && !pdl->func
874 && EQ (pdl->old_value, Qunbound))
876 message_with_string ("Warning: defvar ignored because %s is let-bound",
877 SYMBOL_NAME (sym), 1);
878 break;
882 tail = Fcdr (tail);
883 tem = Fcar (tail);
884 if (!NILP (tem))
886 if (!NILP (Vpurify_flag))
887 tem = Fpurecopy (tem);
888 Fput (sym, Qvariable_documentation, tem);
890 LOADHIST_ATTACH (sym);
892 else
893 /* Simple (defvar <var>) should not count as a definition at all.
894 It could get in the way of other definitions, and unloading this
895 package could try to make the variable unbound. */
898 if (SYMBOLP (sym))
899 XSYMBOL (sym)->declared_special = 1;
901 return sym;
904 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
905 doc: /* Define SYMBOL as a constant variable.
906 The intent is that neither programs nor users should ever change this value.
907 Always sets the value of SYMBOL to the result of evalling INITVALUE.
908 If SYMBOL is buffer-local, its default value is what is set;
909 buffer-local values are not affected.
910 DOCSTRING is optional.
912 If SYMBOL has a local binding, then this form sets the local binding's
913 value. However, you should normally not make local bindings for
914 variables defined with this form.
915 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
916 (Lisp_Object args)
918 register Lisp_Object sym, tem;
920 sym = Fcar (args);
921 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
922 error ("Too many arguments");
924 tem = Feval (Fcar (Fcdr (args)));
925 if (!NILP (Vpurify_flag))
926 tem = Fpurecopy (tem);
927 Fset_default (sym, tem);
928 XSYMBOL (sym)->declared_special = 1;
929 tem = Fcar (Fcdr (Fcdr (args)));
930 if (!NILP (tem))
932 if (!NILP (Vpurify_flag))
933 tem = Fpurecopy (tem);
934 Fput (sym, Qvariable_documentation, tem);
936 Fput (sym, Qrisky_local_variable, Qt);
937 LOADHIST_ATTACH (sym);
938 return sym;
941 /* Error handler used in Fuser_variable_p. */
942 static Lisp_Object
943 user_variable_p_eh (Lisp_Object ignore)
945 return Qnil;
948 static Lisp_Object
949 lisp_indirect_variable (Lisp_Object sym)
951 XSETSYMBOL (sym, indirect_variable (XSYMBOL (sym)));
952 return sym;
955 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
956 doc: /* Return t if VARIABLE is intended to be set and modified by users.
957 \(The alternative is a variable used internally in a Lisp program.)
958 A variable is a user variable if
959 \(1) the first character of its documentation is `*', or
960 \(2) it is customizable (its property list contains a non-nil value
961 of `standard-value' or `custom-autoload'), or
962 \(3) it is an alias for another user variable.
963 Return nil if VARIABLE is an alias and there is a loop in the
964 chain of symbols. */)
965 (Lisp_Object variable)
967 Lisp_Object documentation;
969 if (!SYMBOLP (variable))
970 return Qnil;
972 /* If indirect and there's an alias loop, don't check anything else. */
973 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
974 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
975 Qt, user_variable_p_eh)))
976 return Qnil;
978 while (1)
980 documentation = Fget (variable, Qvariable_documentation);
981 if (INTEGERP (documentation) && XINT (documentation) < 0)
982 return Qt;
983 if (STRINGP (documentation)
984 && ((unsigned char) SREF (documentation, 0) == '*'))
985 return Qt;
986 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
987 if (CONSP (documentation)
988 && STRINGP (XCAR (documentation))
989 && INTEGERP (XCDR (documentation))
990 && XINT (XCDR (documentation)) < 0)
991 return Qt;
992 /* Customizable? See `custom-variable-p'. */
993 if ((!NILP (Fget (variable, intern ("standard-value"))))
994 || (!NILP (Fget (variable, intern ("custom-autoload")))))
995 return Qt;
997 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
998 return Qnil;
1000 /* An indirect variable? Let's follow the chain. */
1001 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
1005 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
1006 doc: /* Bind variables according to VARLIST then eval BODY.
1007 The value of the last form in BODY is returned.
1008 Each element of VARLIST is a symbol (which is bound to nil)
1009 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1010 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
1011 usage: (let* VARLIST BODY...) */)
1012 (Lisp_Object args)
1014 Lisp_Object varlist, var, val, elt, lexenv;
1015 int count = SPECPDL_INDEX ();
1016 struct gcpro gcpro1, gcpro2, gcpro3;
1018 GCPRO3 (args, elt, varlist);
1020 lexenv = Vinternal_interpreter_environment;
1022 varlist = Fcar (args);
1023 while (CONSP (varlist))
1025 QUIT;
1027 elt = XCAR (varlist);
1028 if (SYMBOLP (elt))
1030 var = elt;
1031 val = Qnil;
1033 else if (! NILP (Fcdr (Fcdr (elt))))
1034 signal_error ("`let' bindings can have only one value-form", elt);
1035 else
1037 var = Fcar (elt);
1038 val = Feval (Fcar (Fcdr (elt)));
1041 if (!NILP (lexenv) && SYMBOLP (var) && !XSYMBOL (var)->declared_special)
1042 /* Lexically bind VAR by adding it to the interpreter's binding
1043 alist. */
1045 lexenv = Fcons (Fcons (var, val), lexenv);
1046 specbind (Qinternal_interpreter_environment, lexenv);
1048 else
1049 specbind (var, val);
1051 varlist = XCDR (varlist);
1054 UNGCPRO;
1056 val = Fprogn (Fcdr (args));
1058 return unbind_to (count, val);
1061 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1062 doc: /* Bind variables according to VARLIST then eval BODY.
1063 The value of the last form in BODY is returned.
1064 Each element of VARLIST is a symbol (which is bound to nil)
1065 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1066 All the VALUEFORMs are evalled before any symbols are bound.
1067 usage: (let VARLIST BODY...) */)
1068 (Lisp_Object args)
1070 Lisp_Object *temps, tem, lexenv;
1071 register Lisp_Object elt, varlist;
1072 int count = SPECPDL_INDEX ();
1073 register int argnum;
1074 struct gcpro gcpro1, gcpro2;
1076 varlist = Fcar (args);
1078 /* Make space to hold the values to give the bound variables */
1079 elt = Flength (varlist);
1080 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
1082 /* Compute the values and store them in `temps' */
1084 GCPRO2 (args, *temps);
1085 gcpro2.nvars = 0;
1087 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1089 QUIT;
1090 elt = XCAR (varlist);
1091 if (SYMBOLP (elt))
1092 temps [argnum++] = Qnil;
1093 else if (! NILP (Fcdr (Fcdr (elt))))
1094 signal_error ("`let' bindings can have only one value-form", elt);
1095 else
1096 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
1097 gcpro2.nvars = argnum;
1099 UNGCPRO;
1101 lexenv = Vinternal_interpreter_environment;
1103 varlist = Fcar (args);
1104 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1106 Lisp_Object var;
1108 elt = XCAR (varlist);
1109 var = SYMBOLP (elt) ? elt : Fcar (elt);
1110 tem = temps[argnum++];
1112 if (!NILP (lexenv) && SYMBOLP (var) && !XSYMBOL (var)->declared_special)
1113 /* Lexically bind VAR by adding it to the lexenv alist. */
1114 lexenv = Fcons (Fcons (var, tem), lexenv);
1115 else
1116 /* Dynamically bind VAR. */
1117 specbind (var, tem);
1120 if (!EQ (lexenv, Vinternal_interpreter_environment))
1121 /* Instantiate a new lexical environment. */
1122 specbind (Qinternal_interpreter_environment, lexenv);
1124 elt = Fprogn (Fcdr (args));
1126 return unbind_to (count, elt);
1129 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1130 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1131 The order of execution is thus TEST, BODY, TEST, BODY and so on
1132 until TEST returns nil.
1133 usage: (while TEST BODY...) */)
1134 (Lisp_Object args)
1136 Lisp_Object test, body;
1137 struct gcpro gcpro1, gcpro2;
1139 GCPRO2 (test, body);
1141 test = Fcar (args);
1142 body = Fcdr (args);
1143 while (!NILP (Feval (test)))
1145 QUIT;
1146 Fprogn (body);
1149 UNGCPRO;
1150 return Qnil;
1153 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1154 doc: /* Return result of expanding macros at top level of FORM.
1155 If FORM is not a macro call, it is returned unchanged.
1156 Otherwise, the macro is expanded and the expansion is considered
1157 in place of FORM. When a non-macro-call results, it is returned.
1159 The second optional arg ENVIRONMENT specifies an environment of macro
1160 definitions to shadow the loaded ones for use in file byte-compilation. */)
1161 (Lisp_Object form, Lisp_Object environment)
1163 /* With cleanups from Hallvard Furuseth. */
1164 register Lisp_Object expander, sym, def, tem;
1166 while (1)
1168 /* Come back here each time we expand a macro call,
1169 in case it expands into another macro call. */
1170 if (!CONSP (form))
1171 break;
1172 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1173 def = sym = XCAR (form);
1174 tem = Qnil;
1175 /* Trace symbols aliases to other symbols
1176 until we get a symbol that is not an alias. */
1177 while (SYMBOLP (def))
1179 QUIT;
1180 sym = def;
1181 tem = Fassq (sym, environment);
1182 if (NILP (tem))
1184 def = XSYMBOL (sym)->function;
1185 if (!EQ (def, Qunbound))
1186 continue;
1188 break;
1190 /* Right now TEM is the result from SYM in ENVIRONMENT,
1191 and if TEM is nil then DEF is SYM's function definition. */
1192 if (NILP (tem))
1194 /* SYM is not mentioned in ENVIRONMENT.
1195 Look at its function definition. */
1196 if (EQ (def, Qunbound) || !CONSP (def))
1197 /* Not defined or definition not suitable */
1198 break;
1199 if (EQ (XCAR (def), Qautoload))
1201 /* Autoloading function: will it be a macro when loaded? */
1202 tem = Fnth (make_number (4), def);
1203 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1204 /* Yes, load it and try again. */
1206 struct gcpro gcpro1;
1207 GCPRO1 (form);
1208 do_autoload (def, sym);
1209 UNGCPRO;
1210 continue;
1212 else
1213 break;
1215 else if (!EQ (XCAR (def), Qmacro))
1216 break;
1217 else expander = XCDR (def);
1219 else
1221 expander = XCDR (tem);
1222 if (NILP (expander))
1223 break;
1225 form = apply1 (expander, XCDR (form));
1227 return form;
1230 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1231 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1232 TAG is evalled to get the tag to use; it must not be nil.
1234 Then the BODY is executed.
1235 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1236 If no throw happens, `catch' returns the value of the last BODY form.
1237 If a throw happens, it specifies the value to return from `catch'.
1238 usage: (catch TAG BODY...) */)
1239 (Lisp_Object args)
1241 register Lisp_Object tag;
1242 struct gcpro gcpro1;
1244 GCPRO1 (args);
1245 tag = Feval (Fcar (args));
1246 UNGCPRO;
1247 return internal_catch (tag, Fprogn, Fcdr (args));
1250 /* Set up a catch, then call C function FUNC on argument ARG.
1251 FUNC should return a Lisp_Object.
1252 This is how catches are done from within C code. */
1254 Lisp_Object
1255 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1257 /* This structure is made part of the chain `catchlist'. */
1258 struct catchtag c;
1260 /* Fill in the components of c, and put it on the list. */
1261 c.next = catchlist;
1262 c.tag = tag;
1263 c.val = Qnil;
1264 c.backlist = backtrace_list;
1265 c.handlerlist = handlerlist;
1266 c.lisp_eval_depth = lisp_eval_depth;
1267 c.pdlcount = SPECPDL_INDEX ();
1268 c.poll_suppress_count = poll_suppress_count;
1269 c.interrupt_input_blocked = interrupt_input_blocked;
1270 c.gcpro = gcprolist;
1271 c.byte_stack = byte_stack_list;
1272 catchlist = &c;
1274 /* Call FUNC. */
1275 if (! _setjmp (c.jmp))
1276 c.val = (*func) (arg);
1278 /* Throw works by a longjmp that comes right here. */
1279 catchlist = c.next;
1280 return c.val;
1283 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1284 jump to that CATCH, returning VALUE as the value of that catch.
1286 This is the guts Fthrow and Fsignal; they differ only in the way
1287 they choose the catch tag to throw to. A catch tag for a
1288 condition-case form has a TAG of Qnil.
1290 Before each catch is discarded, unbind all special bindings and
1291 execute all unwind-protect clauses made above that catch. Unwind
1292 the handler stack as we go, so that the proper handlers are in
1293 effect for each unwind-protect clause we run. At the end, restore
1294 some static info saved in CATCH, and longjmp to the location
1295 specified in the
1297 This is used for correct unwinding in Fthrow and Fsignal. */
1299 static void
1300 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1302 register int last_time;
1304 /* Save the value in the tag. */
1305 catch->val = value;
1307 /* Restore certain special C variables. */
1308 set_poll_suppress_count (catch->poll_suppress_count);
1309 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1310 handling_signal = 0;
1311 immediate_quit = 0;
1315 last_time = catchlist == catch;
1317 /* Unwind the specpdl stack, and then restore the proper set of
1318 handlers. */
1319 unbind_to (catchlist->pdlcount, Qnil);
1320 handlerlist = catchlist->handlerlist;
1321 catchlist = catchlist->next;
1323 while (! last_time);
1325 #if HAVE_X_WINDOWS
1326 /* If x_catch_errors was done, turn it off now.
1327 (First we give unbind_to a chance to do that.) */
1328 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1329 The catch must remain in effect during that delicate
1330 state. --lorentey */
1331 x_fully_uncatch_errors ();
1332 #endif
1333 #endif
1335 byte_stack_list = catch->byte_stack;
1336 gcprolist = catch->gcpro;
1337 #ifdef DEBUG_GCPRO
1338 if (gcprolist != 0)
1339 gcpro_level = gcprolist->level + 1;
1340 else
1341 gcpro_level = 0;
1342 #endif
1343 backtrace_list = catch->backlist;
1344 lisp_eval_depth = catch->lisp_eval_depth;
1346 _longjmp (catch->jmp, 1);
1349 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1350 doc: /* Throw to the catch for TAG and return VALUE from it.
1351 Both TAG and VALUE are evalled. */)
1352 (register Lisp_Object tag, Lisp_Object value)
1354 register struct catchtag *c;
1356 if (!NILP (tag))
1357 for (c = catchlist; c; c = c->next)
1359 if (EQ (c->tag, tag))
1360 unwind_to_catch (c, value);
1362 xsignal2 (Qno_catch, tag, value);
1366 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1367 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1368 If BODYFORM completes normally, its value is returned
1369 after executing the UNWINDFORMS.
1370 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1371 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1372 (Lisp_Object args)
1374 Lisp_Object val;
1375 int count = SPECPDL_INDEX ();
1377 record_unwind_protect (Fprogn, Fcdr (args));
1378 val = Feval (Fcar (args));
1379 return unbind_to (count, val);
1382 /* Chain of condition handlers currently in effect.
1383 The elements of this chain are contained in the stack frames
1384 of Fcondition_case and internal_condition_case.
1385 When an error is signaled (by calling Fsignal, below),
1386 this chain is searched for an element that applies. */
1388 struct handler *handlerlist;
1390 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1391 doc: /* Regain control when an error is signaled.
1392 Executes BODYFORM and returns its value if no error happens.
1393 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1394 where the BODY is made of Lisp expressions.
1396 A handler is applicable to an error
1397 if CONDITION-NAME is one of the error's condition names.
1398 If an error happens, the first applicable handler is run.
1400 The car of a handler may be a list of condition names
1401 instead of a single condition name. Then it handles all of them.
1403 When a handler handles an error, control returns to the `condition-case'
1404 and it executes the handler's BODY...
1405 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1406 \(If VAR is nil, the handler can't access that information.)
1407 Then the value of the last BODY form is returned from the `condition-case'
1408 expression.
1410 See also the function `signal' for more info.
1411 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1412 (Lisp_Object args)
1414 register Lisp_Object bodyform, handlers;
1415 volatile Lisp_Object var;
1417 var = Fcar (args);
1418 bodyform = Fcar (Fcdr (args));
1419 handlers = Fcdr (Fcdr (args));
1421 return internal_lisp_condition_case (var, bodyform, handlers);
1424 /* Like Fcondition_case, but the args are separate
1425 rather than passed in a list. Used by Fbyte_code. */
1427 Lisp_Object
1428 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1429 Lisp_Object handlers)
1431 Lisp_Object val;
1432 struct catchtag c;
1433 struct handler h;
1435 CHECK_SYMBOL (var);
1437 for (val = handlers; CONSP (val); val = XCDR (val))
1439 Lisp_Object tem;
1440 tem = XCAR (val);
1441 if (! (NILP (tem)
1442 || (CONSP (tem)
1443 && (SYMBOLP (XCAR (tem))
1444 || CONSP (XCAR (tem))))))
1445 error ("Invalid condition handler", tem);
1448 c.tag = Qnil;
1449 c.val = Qnil;
1450 c.backlist = backtrace_list;
1451 c.handlerlist = handlerlist;
1452 c.lisp_eval_depth = lisp_eval_depth;
1453 c.pdlcount = SPECPDL_INDEX ();
1454 c.poll_suppress_count = poll_suppress_count;
1455 c.interrupt_input_blocked = interrupt_input_blocked;
1456 c.gcpro = gcprolist;
1457 c.byte_stack = byte_stack_list;
1458 if (_setjmp (c.jmp))
1460 if (!NILP (h.var))
1461 specbind (h.var, c.val);
1462 val = Fprogn (Fcdr (h.chosen_clause));
1464 /* Note that this just undoes the binding of h.var; whoever
1465 longjumped to us unwound the stack to c.pdlcount before
1466 throwing. */
1467 unbind_to (c.pdlcount, Qnil);
1468 return val;
1470 c.next = catchlist;
1471 catchlist = &c;
1473 h.var = var;
1474 h.handler = handlers;
1475 h.next = handlerlist;
1476 h.tag = &c;
1477 handlerlist = &h;
1479 val = Feval (bodyform);
1480 catchlist = c.next;
1481 handlerlist = h.next;
1482 return val;
1485 /* Call the function BFUN with no arguments, catching errors within it
1486 according to HANDLERS. If there is an error, call HFUN with
1487 one argument which is the data that describes the error:
1488 (SIGNALNAME . DATA)
1490 HANDLERS can be a list of conditions to catch.
1491 If HANDLERS is Qt, catch all errors.
1492 If HANDLERS is Qerror, catch all errors
1493 but allow the debugger to run if that is enabled. */
1495 Lisp_Object
1496 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1497 Lisp_Object (*hfun) (Lisp_Object))
1499 Lisp_Object val;
1500 struct catchtag c;
1501 struct handler h;
1503 /* Since Fsignal will close off all calls to x_catch_errors,
1504 we will get the wrong results if some are not closed now. */
1505 #if HAVE_X_WINDOWS
1506 if (x_catching_errors ())
1507 abort ();
1508 #endif
1510 c.tag = Qnil;
1511 c.val = Qnil;
1512 c.backlist = backtrace_list;
1513 c.handlerlist = handlerlist;
1514 c.lisp_eval_depth = lisp_eval_depth;
1515 c.pdlcount = SPECPDL_INDEX ();
1516 c.poll_suppress_count = poll_suppress_count;
1517 c.interrupt_input_blocked = interrupt_input_blocked;
1518 c.gcpro = gcprolist;
1519 c.byte_stack = byte_stack_list;
1520 if (_setjmp (c.jmp))
1522 return (*hfun) (c.val);
1524 c.next = catchlist;
1525 catchlist = &c;
1526 h.handler = handlers;
1527 h.var = Qnil;
1528 h.next = handlerlist;
1529 h.tag = &c;
1530 handlerlist = &h;
1532 val = (*bfun) ();
1533 catchlist = c.next;
1534 handlerlist = h.next;
1535 return val;
1538 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1540 Lisp_Object
1541 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1542 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1544 Lisp_Object val;
1545 struct catchtag c;
1546 struct handler h;
1548 /* Since Fsignal will close off all calls to x_catch_errors,
1549 we will get the wrong results if some are not closed now. */
1550 #if HAVE_X_WINDOWS
1551 if (x_catching_errors ())
1552 abort ();
1553 #endif
1555 c.tag = Qnil;
1556 c.val = Qnil;
1557 c.backlist = backtrace_list;
1558 c.handlerlist = handlerlist;
1559 c.lisp_eval_depth = lisp_eval_depth;
1560 c.pdlcount = SPECPDL_INDEX ();
1561 c.poll_suppress_count = poll_suppress_count;
1562 c.interrupt_input_blocked = interrupt_input_blocked;
1563 c.gcpro = gcprolist;
1564 c.byte_stack = byte_stack_list;
1565 if (_setjmp (c.jmp))
1567 return (*hfun) (c.val);
1569 c.next = catchlist;
1570 catchlist = &c;
1571 h.handler = handlers;
1572 h.var = Qnil;
1573 h.next = handlerlist;
1574 h.tag = &c;
1575 handlerlist = &h;
1577 val = (*bfun) (arg);
1578 catchlist = c.next;
1579 handlerlist = h.next;
1580 return val;
1583 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1584 its arguments. */
1586 Lisp_Object
1587 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1588 Lisp_Object arg1,
1589 Lisp_Object arg2,
1590 Lisp_Object handlers,
1591 Lisp_Object (*hfun) (Lisp_Object))
1593 Lisp_Object val;
1594 struct catchtag c;
1595 struct handler h;
1597 /* Since Fsignal will close off all calls to x_catch_errors,
1598 we will get the wrong results if some are not closed now. */
1599 #if HAVE_X_WINDOWS
1600 if (x_catching_errors ())
1601 abort ();
1602 #endif
1604 c.tag = Qnil;
1605 c.val = Qnil;
1606 c.backlist = backtrace_list;
1607 c.handlerlist = handlerlist;
1608 c.lisp_eval_depth = lisp_eval_depth;
1609 c.pdlcount = SPECPDL_INDEX ();
1610 c.poll_suppress_count = poll_suppress_count;
1611 c.interrupt_input_blocked = interrupt_input_blocked;
1612 c.gcpro = gcprolist;
1613 c.byte_stack = byte_stack_list;
1614 if (_setjmp (c.jmp))
1616 return (*hfun) (c.val);
1618 c.next = catchlist;
1619 catchlist = &c;
1620 h.handler = handlers;
1621 h.var = Qnil;
1622 h.next = handlerlist;
1623 h.tag = &c;
1624 handlerlist = &h;
1626 val = (*bfun) (arg1, arg2);
1627 catchlist = c.next;
1628 handlerlist = h.next;
1629 return val;
1632 /* Like internal_condition_case but call BFUN with NARGS as first,
1633 and ARGS as second argument. */
1635 Lisp_Object
1636 internal_condition_case_n (Lisp_Object (*bfun) (int, Lisp_Object*),
1637 int nargs,
1638 Lisp_Object *args,
1639 Lisp_Object handlers,
1640 Lisp_Object (*hfun) (Lisp_Object))
1642 Lisp_Object val;
1643 struct catchtag c;
1644 struct handler h;
1646 /* Since Fsignal will close off all calls to x_catch_errors,
1647 we will get the wrong results if some are not closed now. */
1648 #if HAVE_X_WINDOWS
1649 if (x_catching_errors ())
1650 abort ();
1651 #endif
1653 c.tag = Qnil;
1654 c.val = Qnil;
1655 c.backlist = backtrace_list;
1656 c.handlerlist = handlerlist;
1657 c.lisp_eval_depth = lisp_eval_depth;
1658 c.pdlcount = SPECPDL_INDEX ();
1659 c.poll_suppress_count = poll_suppress_count;
1660 c.interrupt_input_blocked = interrupt_input_blocked;
1661 c.gcpro = gcprolist;
1662 c.byte_stack = byte_stack_list;
1663 if (_setjmp (c.jmp))
1665 return (*hfun) (c.val);
1667 c.next = catchlist;
1668 catchlist = &c;
1669 h.handler = handlers;
1670 h.var = Qnil;
1671 h.next = handlerlist;
1672 h.tag = &c;
1673 handlerlist = &h;
1675 val = (*bfun) (nargs, args);
1676 catchlist = c.next;
1677 handlerlist = h.next;
1678 return val;
1682 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object,
1683 Lisp_Object, Lisp_Object);
1685 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1686 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1687 This function does not return.
1689 An error symbol is a symbol with an `error-conditions' property
1690 that is a list of condition names.
1691 A handler for any of those names will get to handle this signal.
1692 The symbol `error' should normally be one of them.
1694 DATA should be a list. Its elements are printed as part of the error message.
1695 See Info anchor `(elisp)Definition of signal' for some details on how this
1696 error message is constructed.
1697 If the signal is handled, DATA is made available to the handler.
1698 See also the function `condition-case'. */)
1699 (Lisp_Object error_symbol, Lisp_Object data)
1701 /* When memory is full, ERROR-SYMBOL is nil,
1702 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1703 That is a special case--don't do this in other situations. */
1704 register struct handler *allhandlers = handlerlist;
1705 Lisp_Object conditions;
1706 Lisp_Object string;
1707 Lisp_Object real_error_symbol;
1708 struct backtrace *bp;
1710 immediate_quit = handling_signal = 0;
1711 abort_on_gc = 0;
1712 if (gc_in_progress || waiting_for_input)
1713 abort ();
1715 if (NILP (error_symbol))
1716 real_error_symbol = Fcar (data);
1717 else
1718 real_error_symbol = error_symbol;
1720 #if 0 /* rms: I don't know why this was here,
1721 but it is surely wrong for an error that is handled. */
1722 #ifdef HAVE_WINDOW_SYSTEM
1723 if (display_hourglass_p)
1724 cancel_hourglass ();
1725 #endif
1726 #endif
1728 /* This hook is used by edebug. */
1729 if (! NILP (Vsignal_hook_function)
1730 && ! NILP (error_symbol))
1732 /* Edebug takes care of restoring these variables when it exits. */
1733 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1734 max_lisp_eval_depth = lisp_eval_depth + 20;
1736 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1737 max_specpdl_size = SPECPDL_INDEX () + 40;
1739 call2 (Vsignal_hook_function, error_symbol, data);
1742 conditions = Fget (real_error_symbol, Qerror_conditions);
1744 /* Remember from where signal was called. Skip over the frame for
1745 `signal' itself. If a frame for `error' follows, skip that,
1746 too. Don't do this when ERROR_SYMBOL is nil, because that
1747 is a memory-full error. */
1748 Vsignaling_function = Qnil;
1749 if (backtrace_list && !NILP (error_symbol))
1751 bp = backtrace_list->next;
1752 if (bp && bp->function && EQ (*bp->function, Qerror))
1753 bp = bp->next;
1754 if (bp && bp->function)
1755 Vsignaling_function = *bp->function;
1758 for (; handlerlist; handlerlist = handlerlist->next)
1760 register Lisp_Object clause;
1762 clause = find_handler_clause (handlerlist->handler, conditions,
1763 error_symbol, data);
1765 if (EQ (clause, Qlambda))
1767 /* We can't return values to code which signaled an error, but we
1768 can continue code which has signaled a quit. */
1769 if (EQ (real_error_symbol, Qquit))
1770 return Qnil;
1771 else
1772 error ("Cannot return from the debugger in an error");
1775 if (!NILP (clause))
1777 Lisp_Object unwind_data;
1778 struct handler *h = handlerlist;
1780 handlerlist = allhandlers;
1782 if (NILP (error_symbol))
1783 unwind_data = data;
1784 else
1785 unwind_data = Fcons (error_symbol, data);
1786 h->chosen_clause = clause;
1787 unwind_to_catch (h->tag, unwind_data);
1791 handlerlist = allhandlers;
1792 /* If no handler is present now, try to run the debugger,
1793 and if that fails, throw to top level. */
1794 find_handler_clause (Qerror, conditions, error_symbol, data);
1795 if (catchlist != 0)
1796 Fthrow (Qtop_level, Qt);
1798 if (! NILP (error_symbol))
1799 data = Fcons (error_symbol, data);
1801 string = Ferror_message_string (data);
1802 fatal ("%s", SDATA (string), 0);
1805 /* Internal version of Fsignal that never returns.
1806 Used for anything but Qquit (which can return from Fsignal). */
1808 void
1809 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1811 Fsignal (error_symbol, data);
1812 abort ();
1815 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1817 void
1818 xsignal0 (Lisp_Object error_symbol)
1820 xsignal (error_symbol, Qnil);
1823 void
1824 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1826 xsignal (error_symbol, list1 (arg));
1829 void
1830 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1832 xsignal (error_symbol, list2 (arg1, arg2));
1835 void
1836 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1838 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1841 /* Signal `error' with message S, and additional arg ARG.
1842 If ARG is not a genuine list, make it a one-element list. */
1844 void
1845 signal_error (const char *s, Lisp_Object arg)
1847 Lisp_Object tortoise, hare;
1849 hare = tortoise = arg;
1850 while (CONSP (hare))
1852 hare = XCDR (hare);
1853 if (!CONSP (hare))
1854 break;
1856 hare = XCDR (hare);
1857 tortoise = XCDR (tortoise);
1859 if (EQ (hare, tortoise))
1860 break;
1863 if (!NILP (hare))
1864 arg = Fcons (arg, Qnil); /* Make it a list. */
1866 xsignal (Qerror, Fcons (build_string (s), arg));
1870 /* Return nonzero if LIST is a non-nil atom or
1871 a list containing one of CONDITIONS. */
1873 static int
1874 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1876 if (NILP (list))
1877 return 0;
1878 if (! CONSP (list))
1879 return 1;
1881 while (CONSP (conditions))
1883 Lisp_Object this, tail;
1884 this = XCAR (conditions);
1885 for (tail = list; CONSP (tail); tail = XCDR (tail))
1886 if (EQ (XCAR (tail), this))
1887 return 1;
1888 conditions = XCDR (conditions);
1890 return 0;
1893 /* Return 1 if an error with condition-symbols CONDITIONS,
1894 and described by SIGNAL-DATA, should skip the debugger
1895 according to debugger-ignored-errors. */
1897 static int
1898 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1900 Lisp_Object tail;
1901 int first_string = 1;
1902 Lisp_Object error_message;
1904 error_message = Qnil;
1905 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1907 if (STRINGP (XCAR (tail)))
1909 if (first_string)
1911 error_message = Ferror_message_string (data);
1912 first_string = 0;
1915 if (fast_string_match (XCAR (tail), error_message) >= 0)
1916 return 1;
1918 else
1920 Lisp_Object contail;
1922 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1923 if (EQ (XCAR (tail), XCAR (contail)))
1924 return 1;
1928 return 0;
1931 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1932 SIG and DATA describe the signal, as in find_handler_clause. */
1934 static int
1935 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1937 Lisp_Object combined_data;
1939 combined_data = Fcons (sig, data);
1941 if (
1942 /* Don't try to run the debugger with interrupts blocked.
1943 The editing loop would return anyway. */
1944 ! INPUT_BLOCKED_P
1945 /* Does user want to enter debugger for this kind of error? */
1946 && (EQ (sig, Qquit)
1947 ? debug_on_quit
1948 : wants_debugger (Vdebug_on_error, conditions))
1949 && ! skip_debugger (conditions, combined_data)
1950 /* rms: what's this for? */
1951 && when_entered_debugger < num_nonmacro_input_events)
1953 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1954 return 1;
1957 return 0;
1960 /* Value of Qlambda means we have called debugger and user has continued.
1961 There are two ways to pass SIG and DATA:
1962 = SIG is the error symbol, and DATA is the rest of the data.
1963 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1964 This is for memory-full errors only.
1966 We need to increase max_specpdl_size temporarily around
1967 anything we do that can push on the specpdl, so as not to get
1968 a second error here in case we're handling specpdl overflow. */
1970 static Lisp_Object
1971 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions,
1972 Lisp_Object sig, Lisp_Object data)
1974 register Lisp_Object h;
1975 register Lisp_Object tem;
1976 int debugger_called = 0;
1977 int debugger_considered = 0;
1979 /* t is used by handlers for all conditions, set up by C code. */
1980 if (EQ (handlers, Qt))
1981 return Qt;
1983 /* Don't run the debugger for a memory-full error.
1984 (There is no room in memory to do that!) */
1985 if (NILP (sig))
1986 debugger_considered = 1;
1988 /* error is used similarly, but means print an error message
1989 and run the debugger if that is enabled. */
1990 if (EQ (handlers, Qerror)
1991 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1992 there is a handler. */
1994 if (!NILP (sig) && wants_debugger (Vstack_trace_on_error, conditions))
1996 max_lisp_eval_depth += 15;
1997 max_specpdl_size++;
1998 if (noninteractive)
1999 Fbacktrace ();
2000 else
2001 internal_with_output_to_temp_buffer
2002 ("*Backtrace*",
2003 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
2004 Qnil);
2005 max_specpdl_size--;
2006 max_lisp_eval_depth -= 15;
2009 if (!debugger_considered)
2011 debugger_considered = 1;
2012 debugger_called = maybe_call_debugger (conditions, sig, data);
2015 /* If there is no handler, return saying whether we ran the debugger. */
2016 if (EQ (handlers, Qerror))
2018 if (debugger_called)
2019 return Qlambda;
2020 return Qt;
2024 for (h = handlers; CONSP (h); h = Fcdr (h))
2026 Lisp_Object handler, condit;
2028 handler = Fcar (h);
2029 if (!CONSP (handler))
2030 continue;
2031 condit = Fcar (handler);
2032 /* Handle a single condition name in handler HANDLER. */
2033 if (SYMBOLP (condit))
2035 tem = Fmemq (Fcar (handler), conditions);
2036 if (!NILP (tem))
2037 return handler;
2039 /* Handle a list of condition names in handler HANDLER. */
2040 else if (CONSP (condit))
2042 Lisp_Object tail;
2043 for (tail = condit; CONSP (tail); tail = XCDR (tail))
2045 tem = Fmemq (Fcar (tail), conditions);
2046 if (!NILP (tem))
2048 /* This handler is going to apply.
2049 Does it allow the debugger to run first? */
2050 if (! debugger_considered && !NILP (Fmemq (Qdebug, condit)))
2051 maybe_call_debugger (conditions, sig, data);
2052 return handler;
2058 return Qnil;
2062 /* dump an error message; called like vprintf */
2063 void
2064 verror (const char *m, va_list ap)
2066 char buf[200];
2067 int size = 200;
2068 int mlen;
2069 char *buffer = buf;
2070 char *args[3];
2071 int allocated = 0;
2072 Lisp_Object string;
2074 mlen = strlen (m);
2076 while (1)
2078 int used;
2079 used = doprnt (buffer, size, m, m + mlen, ap);
2080 if (used < size)
2081 break;
2082 size *= 2;
2083 if (allocated)
2084 buffer = (char *) xrealloc (buffer, size);
2085 else
2087 buffer = (char *) xmalloc (size);
2088 allocated = 1;
2092 string = build_string (buffer);
2093 if (allocated)
2094 xfree (buffer);
2096 xsignal1 (Qerror, string);
2100 /* dump an error message; called like printf */
2102 /* VARARGS 1 */
2103 void
2104 error (const char *m, ...)
2106 va_list ap;
2107 va_start (ap, m);
2108 verror (m, ap);
2109 va_end (ap);
2112 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2113 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2114 This means it contains a description for how to read arguments to give it.
2115 The value is nil for an invalid function or a symbol with no function
2116 definition.
2118 Interactively callable functions include strings and vectors (treated
2119 as keyboard macros), lambda-expressions that contain a top-level call
2120 to `interactive', autoload definitions made by `autoload' with non-nil
2121 fourth argument, and some of the built-in functions of Lisp.
2123 Also, a symbol satisfies `commandp' if its function definition does so.
2125 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2126 then strings and vectors are not accepted. */)
2127 (Lisp_Object function, Lisp_Object for_call_interactively)
2129 register Lisp_Object fun;
2130 register Lisp_Object funcar;
2131 Lisp_Object if_prop = Qnil;
2133 fun = function;
2135 fun = indirect_function (fun); /* Check cycles. */
2136 if (NILP (fun) || EQ (fun, Qunbound))
2137 return Qnil;
2139 /* Check an `interactive-form' property if present, analogous to the
2140 function-documentation property. */
2141 fun = function;
2142 while (SYMBOLP (fun))
2144 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2145 if (!NILP (tmp))
2146 if_prop = Qt;
2147 fun = Fsymbol_function (fun);
2150 /* Emacs primitives are interactive if their DEFUN specifies an
2151 interactive spec. */
2152 if (SUBRP (fun))
2153 return XSUBR (fun)->intspec ? Qt : if_prop;
2155 /* Bytecode objects are interactive if they are long enough to
2156 have an element whose index is COMPILED_INTERACTIVE, which is
2157 where the interactive spec is stored. */
2158 else if (COMPILEDP (fun))
2159 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2160 ? Qt : if_prop);
2162 /* Strings and vectors are keyboard macros. */
2163 if (STRINGP (fun) || VECTORP (fun))
2164 return (NILP (for_call_interactively) ? Qt : Qnil);
2166 /* Lists may represent commands. */
2167 if (!CONSP (fun))
2168 return Qnil;
2169 funcar = XCAR (fun);
2170 if (EQ (funcar, Qlambda))
2171 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2172 if (EQ (funcar, Qautoload))
2173 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2174 else
2175 return Qnil;
2178 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2179 doc: /* Define FUNCTION to autoload from FILE.
2180 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2181 Third arg DOCSTRING is documentation for the function.
2182 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2183 Fifth arg TYPE indicates the type of the object:
2184 nil or omitted says FUNCTION is a function,
2185 `keymap' says FUNCTION is really a keymap, and
2186 `macro' or t says FUNCTION is really a macro.
2187 Third through fifth args give info about the real definition.
2188 They default to nil.
2189 If FUNCTION is already defined other than as an autoload,
2190 this does nothing and returns nil. */)
2191 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2193 CHECK_SYMBOL (function);
2194 CHECK_STRING (file);
2196 /* If function is defined and not as an autoload, don't override */
2197 if (!EQ (XSYMBOL (function)->function, Qunbound)
2198 && !(CONSP (XSYMBOL (function)->function)
2199 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2200 return Qnil;
2202 if (NILP (Vpurify_flag))
2203 /* Only add entries after dumping, because the ones before are
2204 not useful and else we get loads of them from the loaddefs.el. */
2205 LOADHIST_ATTACH (Fcons (Qautoload, function));
2206 else
2207 /* We don't want the docstring in purespace (instead,
2208 Snarf-documentation should (hopefully) overwrite it).
2209 We used to use 0 here, but that leads to accidental sharing in
2210 purecopy's hash-consing, so we use a (hopefully) unique integer
2211 instead. */
2212 docstring = make_number (XHASH (function));
2213 return Ffset (function,
2214 Fpurecopy (list5 (Qautoload, file, docstring,
2215 interactive, type)));
2218 Lisp_Object
2219 un_autoload (Lisp_Object oldqueue)
2221 register Lisp_Object queue, first, second;
2223 /* Queue to unwind is current value of Vautoload_queue.
2224 oldqueue is the shadowed value to leave in Vautoload_queue. */
2225 queue = Vautoload_queue;
2226 Vautoload_queue = oldqueue;
2227 while (CONSP (queue))
2229 first = XCAR (queue);
2230 second = Fcdr (first);
2231 first = Fcar (first);
2232 if (EQ (first, make_number (0)))
2233 Vfeatures = second;
2234 else
2235 Ffset (first, second);
2236 queue = XCDR (queue);
2238 return Qnil;
2241 /* Load an autoloaded function.
2242 FUNNAME is the symbol which is the function's name.
2243 FUNDEF is the autoload definition (a list). */
2245 void
2246 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2248 int count = SPECPDL_INDEX ();
2249 Lisp_Object fun;
2250 struct gcpro gcpro1, gcpro2, gcpro3;
2252 /* This is to make sure that loadup.el gives a clear picture
2253 of what files are preloaded and when. */
2254 if (! NILP (Vpurify_flag))
2255 error ("Attempt to autoload %s while preparing to dump",
2256 SDATA (SYMBOL_NAME (funname)));
2258 fun = funname;
2259 CHECK_SYMBOL (funname);
2260 GCPRO3 (fun, funname, fundef);
2262 /* Preserve the match data. */
2263 record_unwind_save_match_data ();
2265 /* If autoloading gets an error (which includes the error of failing
2266 to define the function being called), we use Vautoload_queue
2267 to undo function definitions and `provide' calls made by
2268 the function. We do this in the specific case of autoloading
2269 because autoloading is not an explicit request "load this file",
2270 but rather a request to "call this function".
2272 The value saved here is to be restored into Vautoload_queue. */
2273 record_unwind_protect (un_autoload, Vautoload_queue);
2274 Vautoload_queue = Qt;
2275 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2277 /* Once loading finishes, don't undo it. */
2278 Vautoload_queue = Qt;
2279 unbind_to (count, Qnil);
2281 fun = Findirect_function (fun, Qnil);
2283 if (!NILP (Fequal (fun, fundef)))
2284 error ("Autoloading failed to define function %s",
2285 SDATA (SYMBOL_NAME (funname)));
2286 UNGCPRO;
2290 DEFUN ("eval", Feval, Seval, 1, 1, 0,
2291 doc: /* Evaluate FORM and return its value. */)
2292 (Lisp_Object form)
2294 Lisp_Object fun, val, original_fun, original_args;
2295 Lisp_Object funcar;
2296 struct backtrace backtrace;
2297 struct gcpro gcpro1, gcpro2, gcpro3;
2299 if (handling_signal)
2300 abort ();
2302 if (SYMBOLP (form))
2304 /* If there's an active lexical environment, and the variable
2305 isn't declared special, look up its binding in the lexical
2306 environment. */
2307 if (!NILP (Vinternal_interpreter_environment)
2308 && !XSYMBOL (form)->declared_special)
2310 Lisp_Object lex_binding
2311 = Fassq (form, Vinternal_interpreter_environment);
2313 /* If we found a lexical binding for FORM, return the value.
2314 Otherwise, we just drop through and look for a dynamic
2315 binding -- the variable isn't declared special, but there's
2316 not much else we can do, and Fsymbol_value will take care
2317 of signaling an error if there is no binding at all. */
2318 if (CONSP (lex_binding))
2319 return XCDR (lex_binding);
2322 return Fsymbol_value (form);
2325 if (!CONSP (form))
2326 return form;
2328 QUIT;
2329 if ((consing_since_gc > gc_cons_threshold
2330 && consing_since_gc > gc_relative_threshold)
2332 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2334 GCPRO1 (form);
2335 Fgarbage_collect ();
2336 UNGCPRO;
2339 if (++lisp_eval_depth > max_lisp_eval_depth)
2341 if (max_lisp_eval_depth < 100)
2342 max_lisp_eval_depth = 100;
2343 if (lisp_eval_depth > max_lisp_eval_depth)
2344 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2347 original_fun = Fcar (form);
2348 original_args = Fcdr (form);
2350 backtrace.next = backtrace_list;
2351 backtrace_list = &backtrace;
2352 backtrace.function = &original_fun; /* This also protects them from gc */
2353 backtrace.args = &original_args;
2354 backtrace.nargs = UNEVALLED;
2355 backtrace.evalargs = 1;
2356 backtrace.debug_on_exit = 0;
2358 if (debug_on_next_call)
2359 do_debug_on_call (Qt);
2361 /* At this point, only original_fun and original_args
2362 have values that will be used below */
2363 retry:
2365 /* Optimize for no indirection. */
2366 fun = original_fun;
2367 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2368 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2369 fun = indirect_function (fun);
2371 if (SUBRP (fun))
2373 Lisp_Object numargs;
2374 Lisp_Object argvals[8];
2375 Lisp_Object args_left;
2376 register int i, maxargs;
2378 args_left = original_args;
2379 numargs = Flength (args_left);
2381 CHECK_CONS_LIST ();
2383 if (XINT (numargs) < XSUBR (fun)->min_args ||
2384 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
2385 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2387 if (XSUBR (fun)->max_args == UNEVALLED)
2389 backtrace.evalargs = 0;
2390 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2391 goto done;
2394 if (XSUBR (fun)->max_args == MANY)
2396 /* Pass a vector of evaluated arguments */
2397 Lisp_Object *vals;
2398 register int argnum = 0;
2400 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2402 GCPRO3 (args_left, fun, fun);
2403 gcpro3.var = vals;
2404 gcpro3.nvars = 0;
2406 while (!NILP (args_left))
2408 vals[argnum++] = Feval (Fcar (args_left));
2409 args_left = Fcdr (args_left);
2410 gcpro3.nvars = argnum;
2413 backtrace.args = vals;
2414 backtrace.nargs = XINT (numargs);
2416 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2417 UNGCPRO;
2418 goto done;
2421 GCPRO3 (args_left, fun, fun);
2422 gcpro3.var = argvals;
2423 gcpro3.nvars = 0;
2425 maxargs = XSUBR (fun)->max_args;
2426 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2428 argvals[i] = Feval (Fcar (args_left));
2429 gcpro3.nvars = ++i;
2432 UNGCPRO;
2434 backtrace.args = argvals;
2435 backtrace.nargs = XINT (numargs);
2437 switch (i)
2439 case 0:
2440 val = (XSUBR (fun)->function.a0) ();
2441 goto done;
2442 case 1:
2443 val = (XSUBR (fun)->function.a1) (argvals[0]);
2444 goto done;
2445 case 2:
2446 val = (XSUBR (fun)->function.a2) (argvals[0], argvals[1]);
2447 goto done;
2448 case 3:
2449 val = (XSUBR (fun)->function.a3) (argvals[0], argvals[1],
2450 argvals[2]);
2451 goto done;
2452 case 4:
2453 val = (XSUBR (fun)->function.a4) (argvals[0], argvals[1],
2454 argvals[2], argvals[3]);
2455 goto done;
2456 case 5:
2457 val = (XSUBR (fun)->function.a5) (argvals[0], argvals[1], argvals[2],
2458 argvals[3], argvals[4]);
2459 goto done;
2460 case 6:
2461 val = (XSUBR (fun)->function.a6) (argvals[0], argvals[1], argvals[2],
2462 argvals[3], argvals[4], argvals[5]);
2463 goto done;
2464 case 7:
2465 val = (XSUBR (fun)->function.a7) (argvals[0], argvals[1], argvals[2],
2466 argvals[3], argvals[4], argvals[5],
2467 argvals[6]);
2468 goto done;
2470 case 8:
2471 val = (XSUBR (fun)->function.a8) (argvals[0], argvals[1], argvals[2],
2472 argvals[3], argvals[4], argvals[5],
2473 argvals[6], argvals[7]);
2474 goto done;
2476 default:
2477 /* Someone has created a subr that takes more arguments than
2478 is supported by this code. We need to either rewrite the
2479 subr to use a different argument protocol, or add more
2480 cases to this switch. */
2481 abort ();
2484 if (FUNVECP (fun))
2485 val = apply_lambda (fun, original_args, 1, Qnil);
2486 else
2488 if (EQ (fun, Qunbound))
2489 xsignal1 (Qvoid_function, original_fun);
2490 if (!CONSP (fun))
2491 xsignal1 (Qinvalid_function, original_fun);
2492 funcar = XCAR (fun);
2493 if (!SYMBOLP (funcar))
2494 xsignal1 (Qinvalid_function, original_fun);
2495 if (EQ (funcar, Qautoload))
2497 do_autoload (fun, original_fun);
2498 goto retry;
2500 if (EQ (funcar, Qmacro))
2501 val = Feval (apply1 (Fcdr (fun), original_args));
2502 else if (EQ (funcar, Qlambda))
2503 val = apply_lambda (fun, original_args, 1,
2504 /* Only pass down the current lexical environment
2505 if FUN is lexically embedded in FORM. */
2506 (CONSP (original_fun)
2507 ? Vinternal_interpreter_environment
2508 : Qnil));
2509 else if (EQ (funcar, Qclosure)
2510 && CONSP (XCDR (fun))
2511 && CONSP (XCDR (XCDR (fun)))
2512 && EQ (XCAR (XCDR (XCDR (fun))), Qlambda))
2513 val = apply_lambda (XCDR (XCDR (fun)), original_args, 1,
2514 XCAR (XCDR (fun)));
2515 else
2516 xsignal1 (Qinvalid_function, original_fun);
2518 done:
2519 CHECK_CONS_LIST ();
2521 lisp_eval_depth--;
2522 if (backtrace.debug_on_exit)
2523 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2524 backtrace_list = backtrace.next;
2526 return val;
2529 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2530 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2531 Then return the value FUNCTION returns.
2532 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2533 usage: (apply FUNCTION &rest ARGUMENTS) */)
2534 (int nargs, Lisp_Object *args)
2536 register int i, numargs;
2537 register Lisp_Object spread_arg;
2538 register Lisp_Object *funcall_args;
2539 Lisp_Object fun;
2540 struct gcpro gcpro1;
2542 fun = args [0];
2543 funcall_args = 0;
2544 spread_arg = args [nargs - 1];
2545 CHECK_LIST (spread_arg);
2547 numargs = XINT (Flength (spread_arg));
2549 if (numargs == 0)
2550 return Ffuncall (nargs - 1, args);
2551 else if (numargs == 1)
2553 args [nargs - 1] = XCAR (spread_arg);
2554 return Ffuncall (nargs, args);
2557 numargs += nargs - 2;
2559 /* Optimize for no indirection. */
2560 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2561 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2562 fun = indirect_function (fun);
2563 if (EQ (fun, Qunbound))
2565 /* Let funcall get the error */
2566 fun = args[0];
2567 goto funcall;
2570 if (SUBRP (fun))
2572 if (numargs < XSUBR (fun)->min_args
2573 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2574 goto funcall; /* Let funcall get the error */
2575 else if (XSUBR (fun)->max_args > numargs)
2577 /* Avoid making funcall cons up a yet another new vector of arguments
2578 by explicitly supplying nil's for optional values */
2579 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2580 * sizeof (Lisp_Object));
2581 for (i = numargs; i < XSUBR (fun)->max_args;)
2582 funcall_args[++i] = Qnil;
2583 GCPRO1 (*funcall_args);
2584 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2587 funcall:
2588 /* We add 1 to numargs because funcall_args includes the
2589 function itself as well as its arguments. */
2590 if (!funcall_args)
2592 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2593 * sizeof (Lisp_Object));
2594 GCPRO1 (*funcall_args);
2595 gcpro1.nvars = 1 + numargs;
2598 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2599 /* Spread the last arg we got. Its first element goes in
2600 the slot that it used to occupy, hence this value of I. */
2601 i = nargs - 1;
2602 while (!NILP (spread_arg))
2604 funcall_args [i++] = XCAR (spread_arg);
2605 spread_arg = XCDR (spread_arg);
2608 /* By convention, the caller needs to gcpro Ffuncall's args. */
2609 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
2612 /* Run hook variables in various ways. */
2614 enum run_hooks_condition {to_completion, until_success, until_failure};
2615 static Lisp_Object run_hook_with_args (int, Lisp_Object *,
2616 enum run_hooks_condition);
2618 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2619 doc: /* Run each hook in HOOKS.
2620 Each argument should be a symbol, a hook variable.
2621 These symbols are processed in the order specified.
2622 If a hook symbol has a non-nil value, that value may be a function
2623 or a list of functions to be called to run the hook.
2624 If the value is a function, it is called with no arguments.
2625 If it is a list, the elements are called, in order, with no arguments.
2627 Major modes should not use this function directly to run their mode
2628 hook; they should use `run-mode-hooks' instead.
2630 Do not use `make-local-variable' to make a hook variable buffer-local.
2631 Instead, use `add-hook' and specify t for the LOCAL argument.
2632 usage: (run-hooks &rest HOOKS) */)
2633 (int nargs, Lisp_Object *args)
2635 Lisp_Object hook[1];
2636 register int i;
2638 for (i = 0; i < nargs; i++)
2640 hook[0] = args[i];
2641 run_hook_with_args (1, hook, to_completion);
2644 return Qnil;
2647 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2648 Srun_hook_with_args, 1, MANY, 0,
2649 doc: /* Run HOOK with the specified arguments ARGS.
2650 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2651 value, that value may be a function or a list of functions to be
2652 called to run the hook. If the value is a function, it is called with
2653 the given arguments and its return value is returned. If it is a list
2654 of functions, those functions are called, in order,
2655 with the given arguments ARGS.
2656 It is best not to depend on the value returned by `run-hook-with-args',
2657 as that may change.
2659 Do not use `make-local-variable' to make a hook variable buffer-local.
2660 Instead, use `add-hook' and specify t for the LOCAL argument.
2661 usage: (run-hook-with-args HOOK &rest ARGS) */)
2662 (int nargs, Lisp_Object *args)
2664 return run_hook_with_args (nargs, args, to_completion);
2667 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2668 Srun_hook_with_args_until_success, 1, MANY, 0,
2669 doc: /* Run HOOK with the specified arguments ARGS.
2670 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2671 value, that value may be a function or a list of functions to be
2672 called to run the hook. If the value is a function, it is called with
2673 the given arguments and its return value is returned.
2674 If it is a list of functions, those functions are called, in order,
2675 with the given arguments ARGS, until one of them
2676 returns a non-nil value. Then we return that value.
2677 However, if they all return nil, we return nil.
2679 Do not use `make-local-variable' to make a hook variable buffer-local.
2680 Instead, use `add-hook' and specify t for the LOCAL argument.
2681 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2682 (int nargs, Lisp_Object *args)
2684 return run_hook_with_args (nargs, args, until_success);
2687 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2688 Srun_hook_with_args_until_failure, 1, MANY, 0,
2689 doc: /* Run HOOK with the specified arguments ARGS.
2690 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2691 value, that value may be a function or a list of functions to be
2692 called to run the hook. If the value is a function, it is called with
2693 the given arguments and its return value is returned.
2694 If it is a list of functions, those functions are called, in order,
2695 with the given arguments ARGS, until one of them returns nil.
2696 Then we return nil. However, if they all return non-nil, we return non-nil.
2698 Do not use `make-local-variable' to make a hook variable buffer-local.
2699 Instead, use `add-hook' and specify t for the LOCAL argument.
2700 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2701 (int nargs, Lisp_Object *args)
2703 return run_hook_with_args (nargs, args, until_failure);
2706 /* ARGS[0] should be a hook symbol.
2707 Call each of the functions in the hook value, passing each of them
2708 as arguments all the rest of ARGS (all NARGS - 1 elements).
2709 COND specifies a condition to test after each call
2710 to decide whether to stop.
2711 The caller (or its caller, etc) must gcpro all of ARGS,
2712 except that it isn't necessary to gcpro ARGS[0]. */
2714 static Lisp_Object
2715 run_hook_with_args (int nargs, Lisp_Object *args, enum run_hooks_condition cond)
2717 Lisp_Object sym, val, ret;
2718 struct gcpro gcpro1, gcpro2, gcpro3;
2720 /* If we are dying or still initializing,
2721 don't do anything--it would probably crash if we tried. */
2722 if (NILP (Vrun_hooks))
2723 return Qnil;
2725 sym = args[0];
2726 val = find_symbol_value (sym);
2727 ret = (cond == until_failure ? Qt : Qnil);
2729 if (EQ (val, Qunbound) || NILP (val))
2730 return ret;
2731 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2733 args[0] = val;
2734 return Ffuncall (nargs, args);
2736 else
2738 Lisp_Object globals = Qnil;
2739 GCPRO3 (sym, val, globals);
2741 for (;
2742 CONSP (val) && ((cond == to_completion)
2743 || (cond == until_success ? NILP (ret)
2744 : !NILP (ret)));
2745 val = XCDR (val))
2747 if (EQ (XCAR (val), Qt))
2749 /* t indicates this hook has a local binding;
2750 it means to run the global binding too. */
2751 globals = Fdefault_value (sym);
2752 if (NILP (globals)) continue;
2754 if (!CONSP (globals) || EQ (XCAR (globals), Qlambda))
2756 args[0] = globals;
2757 ret = Ffuncall (nargs, args);
2759 else
2761 for (;
2762 CONSP (globals) && ((cond == to_completion)
2763 || (cond == until_success ? NILP (ret)
2764 : !NILP (ret)));
2765 globals = XCDR (globals))
2767 args[0] = XCAR (globals);
2768 /* In a global value, t should not occur. If it does, we
2769 must ignore it to avoid an endless loop. */
2770 if (!EQ (args[0], Qt))
2771 ret = Ffuncall (nargs, args);
2775 else
2777 args[0] = XCAR (val);
2778 ret = Ffuncall (nargs, args);
2782 UNGCPRO;
2783 return ret;
2787 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2788 present value of that symbol.
2789 Call each element of FUNLIST,
2790 passing each of them the rest of ARGS.
2791 The caller (or its caller, etc) must gcpro all of ARGS,
2792 except that it isn't necessary to gcpro ARGS[0]. */
2794 Lisp_Object
2795 run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
2797 Lisp_Object sym;
2798 Lisp_Object val;
2799 Lisp_Object globals;
2800 struct gcpro gcpro1, gcpro2, gcpro3;
2802 sym = args[0];
2803 globals = Qnil;
2804 GCPRO3 (sym, val, globals);
2806 for (val = funlist; CONSP (val); val = XCDR (val))
2808 if (EQ (XCAR (val), Qt))
2810 /* t indicates this hook has a local binding;
2811 it means to run the global binding too. */
2813 for (globals = Fdefault_value (sym);
2814 CONSP (globals);
2815 globals = XCDR (globals))
2817 args[0] = XCAR (globals);
2818 /* In a global value, t should not occur. If it does, we
2819 must ignore it to avoid an endless loop. */
2820 if (!EQ (args[0], Qt))
2821 Ffuncall (nargs, args);
2824 else
2826 args[0] = XCAR (val);
2827 Ffuncall (nargs, args);
2830 UNGCPRO;
2831 return Qnil;
2834 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2836 void
2837 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2839 Lisp_Object temp[3];
2840 temp[0] = hook;
2841 temp[1] = arg1;
2842 temp[2] = arg2;
2844 Frun_hook_with_args (3, temp);
2847 /* Apply fn to arg */
2848 Lisp_Object
2849 apply1 (Lisp_Object fn, Lisp_Object arg)
2851 struct gcpro gcpro1;
2853 GCPRO1 (fn);
2854 if (NILP (arg))
2855 RETURN_UNGCPRO (Ffuncall (1, &fn));
2856 gcpro1.nvars = 2;
2858 Lisp_Object args[2];
2859 args[0] = fn;
2860 args[1] = arg;
2861 gcpro1.var = args;
2862 RETURN_UNGCPRO (Fapply (2, args));
2866 /* Call function fn on no arguments */
2867 Lisp_Object
2868 call0 (Lisp_Object fn)
2870 struct gcpro gcpro1;
2872 GCPRO1 (fn);
2873 RETURN_UNGCPRO (Ffuncall (1, &fn));
2876 /* Call function fn with 1 argument arg1 */
2877 /* ARGSUSED */
2878 Lisp_Object
2879 call1 (Lisp_Object fn, Lisp_Object arg1)
2881 struct gcpro gcpro1;
2882 Lisp_Object args[2];
2884 args[0] = fn;
2885 args[1] = arg1;
2886 GCPRO1 (args[0]);
2887 gcpro1.nvars = 2;
2888 RETURN_UNGCPRO (Ffuncall (2, args));
2891 /* Call function fn with 2 arguments arg1, arg2 */
2892 /* ARGSUSED */
2893 Lisp_Object
2894 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2896 struct gcpro gcpro1;
2897 Lisp_Object args[3];
2898 args[0] = fn;
2899 args[1] = arg1;
2900 args[2] = arg2;
2901 GCPRO1 (args[0]);
2902 gcpro1.nvars = 3;
2903 RETURN_UNGCPRO (Ffuncall (3, args));
2906 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2907 /* ARGSUSED */
2908 Lisp_Object
2909 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2911 struct gcpro gcpro1;
2912 Lisp_Object args[4];
2913 args[0] = fn;
2914 args[1] = arg1;
2915 args[2] = arg2;
2916 args[3] = arg3;
2917 GCPRO1 (args[0]);
2918 gcpro1.nvars = 4;
2919 RETURN_UNGCPRO (Ffuncall (4, args));
2922 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2923 /* ARGSUSED */
2924 Lisp_Object
2925 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2926 Lisp_Object arg4)
2928 struct gcpro gcpro1;
2929 Lisp_Object args[5];
2930 args[0] = fn;
2931 args[1] = arg1;
2932 args[2] = arg2;
2933 args[3] = arg3;
2934 args[4] = arg4;
2935 GCPRO1 (args[0]);
2936 gcpro1.nvars = 5;
2937 RETURN_UNGCPRO (Ffuncall (5, args));
2940 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2941 /* ARGSUSED */
2942 Lisp_Object
2943 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2944 Lisp_Object arg4, Lisp_Object arg5)
2946 struct gcpro gcpro1;
2947 Lisp_Object args[6];
2948 args[0] = fn;
2949 args[1] = arg1;
2950 args[2] = arg2;
2951 args[3] = arg3;
2952 args[4] = arg4;
2953 args[5] = arg5;
2954 GCPRO1 (args[0]);
2955 gcpro1.nvars = 6;
2956 RETURN_UNGCPRO (Ffuncall (6, args));
2959 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2960 /* ARGSUSED */
2961 Lisp_Object
2962 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2963 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2965 struct gcpro gcpro1;
2966 Lisp_Object args[7];
2967 args[0] = fn;
2968 args[1] = arg1;
2969 args[2] = arg2;
2970 args[3] = arg3;
2971 args[4] = arg4;
2972 args[5] = arg5;
2973 args[6] = arg6;
2974 GCPRO1 (args[0]);
2975 gcpro1.nvars = 7;
2976 RETURN_UNGCPRO (Ffuncall (7, args));
2979 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7 */
2980 /* ARGSUSED */
2981 Lisp_Object
2982 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2983 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2985 struct gcpro gcpro1;
2986 Lisp_Object args[8];
2987 args[0] = fn;
2988 args[1] = arg1;
2989 args[2] = arg2;
2990 args[3] = arg3;
2991 args[4] = arg4;
2992 args[5] = arg5;
2993 args[6] = arg6;
2994 args[7] = arg7;
2995 GCPRO1 (args[0]);
2996 gcpro1.nvars = 8;
2997 RETURN_UNGCPRO (Ffuncall (8, args));
3000 /* The caller should GCPRO all the elements of ARGS. */
3002 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
3003 doc: /* Return non-nil if OBJECT is a type of object that can be called as a function. */)
3004 (Lisp_Object object)
3006 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
3008 object = Findirect_function (object, Qnil);
3010 if (CONSP (object) && EQ (XCAR (object), Qautoload))
3012 /* Autoloaded symbols are functions, except if they load
3013 macros or keymaps. */
3014 int i;
3015 for (i = 0; i < 4 && CONSP (object); i++)
3016 object = XCDR (object);
3018 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
3022 if (SUBRP (object))
3023 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
3024 else if (FUNVECP (object))
3025 return Qt;
3026 else if (CONSP (object))
3028 Lisp_Object car = XCAR (object);
3029 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
3031 else
3032 return Qnil;
3035 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
3036 doc: /* Call first argument as a function, passing remaining arguments to it.
3037 Return the value that function returns.
3038 Thus, (funcall 'cons 'x 'y) returns (x . y).
3039 usage: (funcall FUNCTION &rest ARGUMENTS) */)
3040 (int nargs, Lisp_Object *args)
3042 Lisp_Object fun, original_fun;
3043 Lisp_Object funcar;
3044 int numargs = nargs - 1;
3045 Lisp_Object lisp_numargs;
3046 Lisp_Object val;
3047 struct backtrace backtrace;
3048 register Lisp_Object *internal_args;
3049 register int i;
3051 QUIT;
3052 if ((consing_since_gc > gc_cons_threshold
3053 && consing_since_gc > gc_relative_threshold)
3055 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
3056 Fgarbage_collect ();
3058 if (++lisp_eval_depth > max_lisp_eval_depth)
3060 if (max_lisp_eval_depth < 100)
3061 max_lisp_eval_depth = 100;
3062 if (lisp_eval_depth > max_lisp_eval_depth)
3063 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
3066 backtrace.next = backtrace_list;
3067 backtrace_list = &backtrace;
3068 backtrace.function = &args[0];
3069 backtrace.args = &args[1];
3070 backtrace.nargs = nargs - 1;
3071 backtrace.evalargs = 0;
3072 backtrace.debug_on_exit = 0;
3074 if (debug_on_next_call)
3075 do_debug_on_call (Qlambda);
3077 CHECK_CONS_LIST ();
3079 original_fun = args[0];
3081 retry:
3083 /* Optimize for no indirection. */
3084 fun = original_fun;
3085 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
3086 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
3087 fun = indirect_function (fun);
3089 if (SUBRP (fun))
3091 if (numargs < XSUBR (fun)->min_args
3092 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
3094 XSETFASTINT (lisp_numargs, numargs);
3095 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
3098 if (XSUBR (fun)->max_args == UNEVALLED)
3099 xsignal1 (Qinvalid_function, original_fun);
3101 if (XSUBR (fun)->max_args == MANY)
3103 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
3104 goto done;
3107 if (XSUBR (fun)->max_args > numargs)
3109 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
3110 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
3111 for (i = numargs; i < XSUBR (fun)->max_args; i++)
3112 internal_args[i] = Qnil;
3114 else
3115 internal_args = args + 1;
3116 switch (XSUBR (fun)->max_args)
3118 case 0:
3119 val = (XSUBR (fun)->function.a0) ();
3120 goto done;
3121 case 1:
3122 val = (XSUBR (fun)->function.a1) (internal_args[0]);
3123 goto done;
3124 case 2:
3125 val = (XSUBR (fun)->function.a2) (internal_args[0], internal_args[1]);
3126 goto done;
3127 case 3:
3128 val = (XSUBR (fun)->function.a3) (internal_args[0], internal_args[1],
3129 internal_args[2]);
3130 goto done;
3131 case 4:
3132 val = (XSUBR (fun)->function.a4) (internal_args[0], internal_args[1],
3133 internal_args[2], internal_args[3]);
3134 goto done;
3135 case 5:
3136 val = (XSUBR (fun)->function.a5) (internal_args[0], internal_args[1],
3137 internal_args[2], internal_args[3],
3138 internal_args[4]);
3139 goto done;
3140 case 6:
3141 val = (XSUBR (fun)->function.a6) (internal_args[0], internal_args[1],
3142 internal_args[2], internal_args[3],
3143 internal_args[4], internal_args[5]);
3144 goto done;
3145 case 7:
3146 val = (XSUBR (fun)->function.a7) (internal_args[0], internal_args[1],
3147 internal_args[2], internal_args[3],
3148 internal_args[4], internal_args[5],
3149 internal_args[6]);
3150 goto done;
3152 case 8:
3153 val = (XSUBR (fun)->function.a8) (internal_args[0], internal_args[1],
3154 internal_args[2], internal_args[3],
3155 internal_args[4], internal_args[5],
3156 internal_args[6], internal_args[7]);
3157 goto done;
3159 default:
3161 /* If a subr takes more than 8 arguments without using MANY
3162 or UNEVALLED, we need to extend this function to support it.
3163 Until this is done, there is no way to call the function. */
3164 abort ();
3168 if (FUNVECP (fun))
3169 val = funcall_lambda (fun, numargs, args + 1, Qnil);
3170 else
3172 if (EQ (fun, Qunbound))
3173 xsignal1 (Qvoid_function, original_fun);
3174 if (!CONSP (fun))
3175 xsignal1 (Qinvalid_function, original_fun);
3176 funcar = XCAR (fun);
3177 if (!SYMBOLP (funcar))
3178 xsignal1 (Qinvalid_function, original_fun);
3179 if (EQ (funcar, Qlambda))
3180 val = funcall_lambda (fun, numargs, args + 1, Qnil);
3181 else if (EQ (funcar, Qclosure)
3182 && CONSP (XCDR (fun))
3183 && CONSP (XCDR (XCDR (fun)))
3184 && EQ (XCAR (XCDR (XCDR (fun))), Qlambda))
3185 val = funcall_lambda (XCDR (XCDR (fun)), numargs, args + 1,
3186 XCAR (XCDR (fun)));
3187 else if (EQ (funcar, Qautoload))
3189 do_autoload (fun, original_fun);
3190 CHECK_CONS_LIST ();
3191 goto retry;
3193 else
3194 xsignal1 (Qinvalid_function, original_fun);
3196 done:
3197 CHECK_CONS_LIST ();
3198 lisp_eval_depth--;
3199 if (backtrace.debug_on_exit)
3200 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3201 backtrace_list = backtrace.next;
3202 return val;
3205 Lisp_Object
3206 apply_lambda (Lisp_Object fun, Lisp_Object args, int eval_flag,
3207 Lisp_Object lexenv)
3209 Lisp_Object args_left;
3210 Lisp_Object numargs;
3211 register Lisp_Object *arg_vector;
3212 struct gcpro gcpro1, gcpro2, gcpro3;
3213 register int i;
3214 register Lisp_Object tem;
3216 numargs = Flength (args);
3217 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
3218 args_left = args;
3220 GCPRO3 (*arg_vector, args_left, fun);
3221 gcpro1.nvars = 0;
3223 for (i = 0; i < XINT (numargs);)
3225 tem = Fcar (args_left), args_left = Fcdr (args_left);
3226 if (eval_flag) tem = Feval (tem);
3227 arg_vector[i++] = tem;
3228 gcpro1.nvars = i;
3231 UNGCPRO;
3233 if (eval_flag)
3235 backtrace_list->args = arg_vector;
3236 backtrace_list->nargs = i;
3238 backtrace_list->evalargs = 0;
3239 tem = funcall_lambda (fun, XINT (numargs), arg_vector, lexenv);
3241 /* Do the debug-on-exit now, while arg_vector still exists. */
3242 if (backtrace_list->debug_on_exit)
3243 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3244 /* Don't do it again when we return to eval. */
3245 backtrace_list->debug_on_exit = 0;
3246 return tem;
3250 /* Call a non-bytecode funvec object FUN, on the argments in ARGS (of
3251 length NARGS). */
3253 static Lisp_Object
3254 funcall_funvec (Lisp_Object fun, int nargs, Lisp_Object *args)
3256 int size = FUNVEC_SIZE (fun);
3257 Lisp_Object tag = (size > 0 ? AREF (fun, 0) : Qnil);
3259 if (EQ (tag, Qcurry))
3261 /* A curried function is a way to attach arguments to a another
3262 function. The first element of the vector is the identifier
3263 `curry', the second is the wrapped function, and remaining
3264 elements are the attached arguments. */
3265 int num_curried_args = size - 2;
3266 /* Offset of the curried and user args in the final arglist. Curried
3267 args are first in the new arg vector, after the function. User
3268 args follow. */
3269 int curried_args_offs = 1;
3270 int user_args_offs = curried_args_offs + num_curried_args;
3271 /* The curried function and arguments. */
3272 Lisp_Object *curry_params = XVECTOR (fun)->contents + 1;
3273 /* The arguments in the curry vector. */
3274 Lisp_Object *curried_args = curry_params + 1;
3275 /* The number of arguments with which we'll call funcall, and the
3276 arguments themselves. */
3277 int num_funcall_args = 1 + num_curried_args + nargs;
3278 Lisp_Object *funcall_args
3279 = (Lisp_Object *) alloca (num_funcall_args * sizeof (Lisp_Object));
3281 /* First comes the real function. */
3282 funcall_args[0] = curry_params[0];
3284 /* Then the arguments in the appropriate order. */
3285 memcpy (funcall_args + curried_args_offs, curried_args,
3286 num_curried_args * sizeof (Lisp_Object));
3287 memcpy (funcall_args + user_args_offs, args,
3288 nargs * sizeof (Lisp_Object));
3290 return Ffuncall (num_funcall_args, funcall_args);
3292 else
3293 xsignal1 (Qinvalid_function, fun);
3297 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3298 and return the result of evaluation.
3299 FUN must be either a lambda-expression or a compiled-code object. */
3301 static Lisp_Object
3302 funcall_lambda (Lisp_Object fun, int nargs,
3303 register Lisp_Object *arg_vector,
3304 Lisp_Object lexenv)
3306 Lisp_Object val, syms_left, next;
3307 int count = SPECPDL_INDEX ();
3308 int i, optional, rest;
3310 if (COMPILEDP (fun)
3311 && FUNVEC_SIZE (fun) > COMPILED_PUSH_ARGS
3312 && ! NILP (XVECTOR (fun)->contents[COMPILED_PUSH_ARGS]))
3313 /* A byte-code object with a non-nil `push args' slot means we
3314 shouldn't bind any arguments, instead just call the byte-code
3315 interpreter directly; it will push arguments as necessary.
3317 Byte-code objects with either a non-existant, or a nil value for
3318 the `push args' slot (the default), have dynamically-bound
3319 arguments, and use the argument-binding code below instead (as do
3320 all interpreted functions, even lexically bound ones). */
3322 /* If we have not actually read the bytecode string
3323 and constants vector yet, fetch them from the file. */
3324 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3325 Ffetch_bytecode (fun);
3326 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3327 AREF (fun, COMPILED_CONSTANTS),
3328 AREF (fun, COMPILED_STACK_DEPTH),
3329 AREF (fun, COMPILED_ARGLIST),
3330 nargs, arg_vector);
3333 if (FUNVECP (fun) && !FUNVEC_COMPILED_P (fun))
3334 /* Byte-compiled functions are handled directly below, but we
3335 call other funvec types via funcall_funvec. */
3336 return funcall_funvec (fun, nargs, arg_vector);
3338 if (CONSP (fun))
3340 syms_left = XCDR (fun);
3341 if (CONSP (syms_left))
3342 syms_left = XCAR (syms_left);
3343 else
3344 xsignal1 (Qinvalid_function, fun);
3346 else if (COMPILEDP (fun))
3347 syms_left = AREF (fun, COMPILED_ARGLIST);
3348 else
3349 abort ();
3351 i = optional = rest = 0;
3352 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3354 QUIT;
3356 next = XCAR (syms_left);
3357 if (!SYMBOLP (next))
3358 xsignal1 (Qinvalid_function, fun);
3360 if (EQ (next, Qand_rest))
3361 rest = 1;
3362 else if (EQ (next, Qand_optional))
3363 optional = 1;
3364 else if (rest)
3366 specbind (next, Flist (nargs - i, &arg_vector[i]));
3367 i = nargs;
3369 else
3371 Lisp_Object val;
3373 /* Get the argument's actual value. */
3374 if (i < nargs)
3375 val = arg_vector[i++];
3376 else if (!optional)
3377 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3378 else
3379 val = Qnil;
3381 /* Bind the argument. */
3382 if (!NILP (lexenv)
3383 && SYMBOLP (next) && !XSYMBOL (next)->declared_special)
3384 /* Lexically bind NEXT by adding it to the lexenv alist. */
3385 lexenv = Fcons (Fcons (next, val), lexenv);
3386 else
3387 /* Dynamically bind NEXT. */
3388 specbind (next, val);
3392 if (!NILP (syms_left))
3393 xsignal1 (Qinvalid_function, fun);
3394 else if (i < nargs)
3395 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3397 if (!EQ (lexenv, Vinternal_interpreter_environment))
3398 /* Instantiate a new lexical environment. */
3399 specbind (Qinternal_interpreter_environment, lexenv);
3401 if (CONSP (fun))
3402 val = Fprogn (XCDR (XCDR (fun)));
3403 else
3405 /* If we have not actually read the bytecode string
3406 and constants vector yet, fetch them from the file. */
3407 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3408 Ffetch_bytecode (fun);
3409 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3410 AREF (fun, COMPILED_CONSTANTS),
3411 AREF (fun, COMPILED_STACK_DEPTH),
3412 Qnil, 0, 0);
3415 return unbind_to (count, val);
3418 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3419 1, 1, 0,
3420 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3421 (Lisp_Object object)
3423 Lisp_Object tem;
3425 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3427 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3428 if (!CONSP (tem))
3430 tem = AREF (object, COMPILED_BYTECODE);
3431 if (CONSP (tem) && STRINGP (XCAR (tem)))
3432 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3433 else
3434 error ("Invalid byte code");
3436 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3437 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3439 return object;
3442 void
3443 grow_specpdl (void)
3445 register int count = SPECPDL_INDEX ();
3446 if (specpdl_size >= max_specpdl_size)
3448 if (max_specpdl_size < 400)
3449 max_specpdl_size = 400;
3450 if (specpdl_size >= max_specpdl_size)
3451 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3453 specpdl_size *= 2;
3454 if (specpdl_size > max_specpdl_size)
3455 specpdl_size = max_specpdl_size;
3456 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
3457 specpdl_ptr = specpdl + count;
3460 /* specpdl_ptr->symbol is a field which describes which variable is
3461 let-bound, so it can be properly undone when we unbind_to.
3462 It can have the following two shapes:
3463 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3464 a symbol that is not buffer-local (at least at the time
3465 the let binding started). Note also that it should not be
3466 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3467 to record V2 here).
3468 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3469 variable SYMBOL which can be buffer-local. WHERE tells us
3470 which buffer is affected (or nil if the let-binding affects the
3471 global value of the variable) and BUFFER tells us which buffer was
3472 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3473 BUFFER did not yet have a buffer-local value). */
3475 void
3476 specbind (Lisp_Object symbol, Lisp_Object value)
3478 struct Lisp_Symbol *sym;
3480 eassert (!handling_signal);
3482 CHECK_SYMBOL (symbol);
3483 sym = XSYMBOL (symbol);
3484 if (specpdl_ptr == specpdl + specpdl_size)
3485 grow_specpdl ();
3487 start:
3488 switch (sym->redirect)
3490 case SYMBOL_VARALIAS:
3491 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3492 case SYMBOL_PLAINVAL:
3493 /* The most common case is that of a non-constant symbol with a
3494 trivial value. Make that as fast as we can. */
3495 specpdl_ptr->symbol = symbol;
3496 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3497 specpdl_ptr->func = NULL;
3498 ++specpdl_ptr;
3499 if (!sym->constant)
3500 SET_SYMBOL_VAL (sym, value);
3501 else
3502 set_internal (symbol, value, Qnil, 1);
3503 break;
3504 case SYMBOL_LOCALIZED:
3505 if (SYMBOL_BLV (sym)->frame_local)
3506 error ("Frame-local vars cannot be let-bound");
3507 case SYMBOL_FORWARDED:
3509 Lisp_Object ovalue = find_symbol_value (symbol);
3510 specpdl_ptr->func = 0;
3511 specpdl_ptr->old_value = ovalue;
3513 eassert (sym->redirect != SYMBOL_LOCALIZED
3514 || (EQ (SYMBOL_BLV (sym)->where,
3515 SYMBOL_BLV (sym)->frame_local ?
3516 Fselected_frame () : Fcurrent_buffer ())));
3518 if (sym->redirect == SYMBOL_LOCALIZED
3519 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3521 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3523 /* For a local variable, record both the symbol and which
3524 buffer's or frame's value we are saving. */
3525 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3527 eassert (sym->redirect != SYMBOL_LOCALIZED
3528 || (BLV_FOUND (SYMBOL_BLV (sym))
3529 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3530 where = cur_buf;
3532 else if (sym->redirect == SYMBOL_LOCALIZED
3533 && BLV_FOUND (SYMBOL_BLV (sym)))
3534 where = SYMBOL_BLV (sym)->where;
3535 else
3536 where = Qnil;
3538 /* We're not using the `unused' slot in the specbinding
3539 structure because this would mean we have to do more
3540 work for simple variables. */
3541 /* FIXME: The third value `current_buffer' is only used in
3542 let_shadows_buffer_binding_p which is itself only used
3543 in set_internal for local_if_set. */
3544 eassert (NILP (where) || EQ (where, cur_buf));
3545 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3547 /* If SYMBOL is a per-buffer variable which doesn't have a
3548 buffer-local value here, make the `let' change the global
3549 value by changing the value of SYMBOL in all buffers not
3550 having their own value. This is consistent with what
3551 happens with other buffer-local variables. */
3552 if (NILP (where)
3553 && sym->redirect == SYMBOL_FORWARDED)
3555 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3556 ++specpdl_ptr;
3557 Fset_default (symbol, value);
3558 return;
3561 else
3562 specpdl_ptr->symbol = symbol;
3564 specpdl_ptr++;
3565 set_internal (symbol, value, Qnil, 1);
3566 break;
3568 default: abort ();
3572 void
3573 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3575 eassert (!handling_signal);
3577 if (specpdl_ptr == specpdl + specpdl_size)
3578 grow_specpdl ();
3579 specpdl_ptr->func = function;
3580 specpdl_ptr->symbol = Qnil;
3581 specpdl_ptr->old_value = arg;
3582 specpdl_ptr++;
3585 Lisp_Object
3586 unbind_to (int count, Lisp_Object value)
3588 Lisp_Object quitf = Vquit_flag;
3589 struct gcpro gcpro1, gcpro2;
3591 GCPRO2 (value, quitf);
3592 Vquit_flag = Qnil;
3594 while (specpdl_ptr != specpdl + count)
3596 /* Copy the binding, and decrement specpdl_ptr, before we do
3597 the work to unbind it. We decrement first
3598 so that an error in unbinding won't try to unbind
3599 the same entry again, and we copy the binding first
3600 in case more bindings are made during some of the code we run. */
3602 struct specbinding this_binding;
3603 this_binding = *--specpdl_ptr;
3605 if (this_binding.func != 0)
3606 (*this_binding.func) (this_binding.old_value);
3607 /* If the symbol is a list, it is really (SYMBOL WHERE
3608 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3609 frame. If WHERE is a buffer or frame, this indicates we
3610 bound a variable that had a buffer-local or frame-local
3611 binding. WHERE nil means that the variable had the default
3612 value when it was bound. CURRENT-BUFFER is the buffer that
3613 was current when the variable was bound. */
3614 else if (CONSP (this_binding.symbol))
3616 Lisp_Object symbol, where;
3618 symbol = XCAR (this_binding.symbol);
3619 where = XCAR (XCDR (this_binding.symbol));
3621 if (NILP (where))
3622 Fset_default (symbol, this_binding.old_value);
3623 /* If `where' is non-nil, reset the value in the appropriate
3624 local binding, but only if that binding still exists. */
3625 else if (BUFFERP (where)
3626 ? !NILP (Flocal_variable_p (symbol, where))
3627 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3628 set_internal (symbol, this_binding.old_value, where, 1);
3630 /* If variable has a trivial value (no forwarding), we can
3631 just set it. No need to check for constant symbols here,
3632 since that was already done by specbind. */
3633 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3634 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3635 this_binding.old_value);
3636 else
3637 /* NOTE: we only ever come here if make_local_foo was used for
3638 the first time on this var within this let. */
3639 Fset_default (this_binding.symbol, this_binding.old_value);
3642 if (NILP (Vquit_flag) && !NILP (quitf))
3643 Vquit_flag = quitf;
3645 UNGCPRO;
3646 return value;
3651 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3652 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3653 A special variable is one that will be bound dynamically, even in a
3654 context where binding is lexical by default. */)
3655 (Lisp_Object symbol)
3657 CHECK_SYMBOL (symbol);
3658 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3663 DEFUN ("curry", Fcurry, Scurry, 1, MANY, 0,
3664 doc: /* Return FUN curried with ARGS.
3665 The result is a function-like object that will append any arguments it
3666 is called with to ARGS, and call FUN with the resulting list of arguments.
3668 For instance:
3669 (funcall (curry '+ 3 4 5) 2) is the same as (funcall '+ 3 4 5 2)
3670 and:
3671 (mapcar (curry 'concat "The ") '("a" "b" "c"))
3672 => ("The a" "The b" "The c")
3674 usage: (curry FUN &rest ARGS) */)
3675 (int nargs, Lisp_Object *args)
3677 return make_funvec (Qcurry, 0, nargs, args);
3681 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3682 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3683 The debugger is entered when that frame exits, if the flag is non-nil. */)
3684 (Lisp_Object level, Lisp_Object flag)
3686 register struct backtrace *backlist = backtrace_list;
3687 register int i;
3689 CHECK_NUMBER (level);
3691 for (i = 0; backlist && i < XINT (level); i++)
3693 backlist = backlist->next;
3696 if (backlist)
3697 backlist->debug_on_exit = !NILP (flag);
3699 return flag;
3702 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3703 doc: /* Print a trace of Lisp function calls currently active.
3704 Output stream used is value of `standard-output'. */)
3705 (void)
3707 register struct backtrace *backlist = backtrace_list;
3708 register int i;
3709 Lisp_Object tail;
3710 Lisp_Object tem;
3711 struct gcpro gcpro1;
3713 XSETFASTINT (Vprint_level, 3);
3715 tail = Qnil;
3716 GCPRO1 (tail);
3718 while (backlist)
3720 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3721 if (backlist->nargs == UNEVALLED)
3723 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3724 write_string ("\n", -1);
3726 else
3728 tem = *backlist->function;
3729 Fprin1 (tem, Qnil); /* This can QUIT */
3730 write_string ("(", -1);
3731 if (backlist->nargs == MANY)
3733 for (tail = *backlist->args, i = 0;
3734 !NILP (tail);
3735 tail = Fcdr (tail), i++)
3737 if (i) write_string (" ", -1);
3738 Fprin1 (Fcar (tail), Qnil);
3741 else
3743 for (i = 0; i < backlist->nargs; i++)
3745 if (i) write_string (" ", -1);
3746 Fprin1 (backlist->args[i], Qnil);
3749 write_string (")\n", -1);
3751 backlist = backlist->next;
3754 Vprint_level = Qnil;
3755 UNGCPRO;
3756 return Qnil;
3759 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3760 doc: /* Return the function and arguments NFRAMES up from current execution point.
3761 If that frame has not evaluated the arguments yet (or is a special form),
3762 the value is (nil FUNCTION ARG-FORMS...).
3763 If that frame has evaluated its arguments and called its function already,
3764 the value is (t FUNCTION ARG-VALUES...).
3765 A &rest arg is represented as the tail of the list ARG-VALUES.
3766 FUNCTION is whatever was supplied as car of evaluated list,
3767 or a lambda expression for macro calls.
3768 If NFRAMES is more than the number of frames, the value is nil. */)
3769 (Lisp_Object nframes)
3771 register struct backtrace *backlist = backtrace_list;
3772 register int i;
3773 Lisp_Object tem;
3775 CHECK_NATNUM (nframes);
3777 /* Find the frame requested. */
3778 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3779 backlist = backlist->next;
3781 if (!backlist)
3782 return Qnil;
3783 if (backlist->nargs == UNEVALLED)
3784 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3785 else
3787 if (backlist->nargs == MANY)
3788 tem = *backlist->args;
3789 else
3790 tem = Flist (backlist->nargs, backlist->args);
3792 return Fcons (Qt, Fcons (*backlist->function, tem));
3797 void
3798 mark_backtrace (void)
3800 register struct backtrace *backlist;
3801 register int i;
3803 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3805 mark_object (*backlist->function);
3807 if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
3808 i = 0;
3809 else
3810 i = backlist->nargs - 1;
3811 for (; i >= 0; i--)
3812 mark_object (backlist->args[i]);
3816 void
3817 syms_of_eval (void)
3819 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
3820 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3821 If Lisp code tries to increase the total number past this amount,
3822 an error is signaled.
3823 You can safely use a value considerably larger than the default value,
3824 if that proves inconveniently small. However, if you increase it too far,
3825 Emacs could run out of memory trying to make the stack bigger. */);
3827 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
3828 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3830 This limit serves to catch infinite recursions for you before they cause
3831 actual stack overflow in C, which would be fatal for Emacs.
3832 You can safely make it considerably larger than its default value,
3833 if that proves inconveniently small. However, if you increase it too far,
3834 Emacs could overflow the real C stack, and crash. */);
3836 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3837 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3838 If the value is t, that means do an ordinary quit.
3839 If the value equals `throw-on-input', that means quit by throwing
3840 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3841 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3842 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3843 Vquit_flag = Qnil;
3845 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3846 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3847 Note that `quit-flag' will still be set by typing C-g,
3848 so a quit will be signaled as soon as `inhibit-quit' is nil.
3849 To prevent this happening, set `quit-flag' to nil
3850 before making `inhibit-quit' nil. */);
3851 Vinhibit_quit = Qnil;
3853 Qinhibit_quit = intern_c_string ("inhibit-quit");
3854 staticpro (&Qinhibit_quit);
3856 Qautoload = intern_c_string ("autoload");
3857 staticpro (&Qautoload);
3859 Qdebug_on_error = intern_c_string ("debug-on-error");
3860 staticpro (&Qdebug_on_error);
3862 Qmacro = intern_c_string ("macro");
3863 staticpro (&Qmacro);
3865 Qdeclare = intern_c_string ("declare");
3866 staticpro (&Qdeclare);
3868 /* Note that the process handling also uses Qexit, but we don't want
3869 to staticpro it twice, so we just do it here. */
3870 Qexit = intern_c_string ("exit");
3871 staticpro (&Qexit);
3873 Qinteractive = intern_c_string ("interactive");
3874 staticpro (&Qinteractive);
3876 Qcommandp = intern_c_string ("commandp");
3877 staticpro (&Qcommandp);
3879 Qdefun = intern_c_string ("defun");
3880 staticpro (&Qdefun);
3882 Qand_rest = intern_c_string ("&rest");
3883 staticpro (&Qand_rest);
3885 Qand_optional = intern_c_string ("&optional");
3886 staticpro (&Qand_optional);
3888 Qclosure = intern_c_string ("closure");
3889 staticpro (&Qclosure);
3891 Qcurry = intern_c_string ("curry");
3892 staticpro (&Qcurry);
3894 Qdebug = intern_c_string ("debug");
3895 staticpro (&Qdebug);
3897 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
3898 doc: /* *Non-nil means errors display a backtrace buffer.
3899 More precisely, this happens for any error that is handled
3900 by the editor command loop.
3901 If the value is a list, an error only means to display a backtrace
3902 if one of its condition symbols appears in the list. */);
3903 Vstack_trace_on_error = Qnil;
3905 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
3906 doc: /* *Non-nil means enter debugger if an error is signaled.
3907 Does not apply to errors handled by `condition-case' or those
3908 matched by `debug-ignored-errors'.
3909 If the value is a list, an error only means to enter the debugger
3910 if one of its condition symbols appears in the list.
3911 When you evaluate an expression interactively, this variable
3912 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3913 The command `toggle-debug-on-error' toggles this.
3914 See also the variable `debug-on-quit'. */);
3915 Vdebug_on_error = Qnil;
3917 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3918 doc: /* *List of errors for which the debugger should not be called.
3919 Each element may be a condition-name or a regexp that matches error messages.
3920 If any element applies to a given error, that error skips the debugger
3921 and just returns to top level.
3922 This overrides the variable `debug-on-error'.
3923 It does not apply to errors handled by `condition-case'. */);
3924 Vdebug_ignored_errors = Qnil;
3926 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
3927 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3928 Does not apply if quit is handled by a `condition-case'. */);
3929 debug_on_quit = 0;
3931 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3932 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3934 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3935 doc: /* Non-nil means debugger may continue execution.
3936 This is nil when the debugger is called under circumstances where it
3937 might not be safe to continue. */);
3938 debugger_may_continue = 1;
3940 DEFVAR_LISP ("debugger", &Vdebugger,
3941 doc: /* Function to call to invoke debugger.
3942 If due to frame exit, args are `exit' and the value being returned;
3943 this function's value will be returned instead of that.
3944 If due to error, args are `error' and a list of the args to `signal'.
3945 If due to `apply' or `funcall' entry, one arg, `lambda'.
3946 If due to `eval' entry, one arg, t. */);
3947 Vdebugger = Qnil;
3949 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3950 doc: /* If non-nil, this is a function for `signal' to call.
3951 It receives the same arguments that `signal' was given.
3952 The Edebug package uses this to regain control. */);
3953 Vsignal_hook_function = Qnil;
3955 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
3956 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3957 Note that `debug-on-error', `debug-on-quit' and friends
3958 still determine whether to handle the particular condition. */);
3959 Vdebug_on_signal = Qnil;
3961 DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
3962 doc: /* Function to process declarations in a macro definition.
3963 The function will be called with two args MACRO and DECL.
3964 MACRO is the name of the macro being defined.
3965 DECL is a list `(declare ...)' containing the declarations.
3966 The value the function returns is not used. */);
3967 Vmacro_declaration_function = Qnil;
3969 Qinternal_interpreter_environment
3970 = intern_c_string ("internal-interpreter-environment");
3971 staticpro (&Qinternal_interpreter_environment);
3972 DEFVAR_LISP ("internal-interpreter-environment",
3973 &Vinternal_interpreter_environment,
3974 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3975 When lexical binding is not being used, this variable is nil.
3976 A value of `(t)' indicates an empty environment, otherwise it is an
3977 alist of active lexical bindings. */);
3978 Vinternal_interpreter_environment = Qnil;
3980 Vrun_hooks = intern_c_string ("run-hooks");
3981 staticpro (&Vrun_hooks);
3983 staticpro (&Vautoload_queue);
3984 Vautoload_queue = Qnil;
3985 staticpro (&Vsignaling_function);
3986 Vsignaling_function = Qnil;
3988 defsubr (&Sor);
3989 defsubr (&Sand);
3990 defsubr (&Sif);
3991 defsubr (&Scond);
3992 defsubr (&Sprogn);
3993 defsubr (&Sprog1);
3994 defsubr (&Sprog2);
3995 defsubr (&Ssetq);
3996 defsubr (&Squote);
3997 defsubr (&Sfunction);
3998 defsubr (&Sdefun);
3999 defsubr (&Sdefmacro);
4000 defsubr (&Sdefvar);
4001 defsubr (&Sdefvaralias);
4002 defsubr (&Sdefconst);
4003 defsubr (&Suser_variable_p);
4004 defsubr (&Slet);
4005 defsubr (&SletX);
4006 defsubr (&Swhile);
4007 defsubr (&Smacroexpand);
4008 defsubr (&Scatch);
4009 defsubr (&Sthrow);
4010 defsubr (&Sunwind_protect);
4011 defsubr (&Scondition_case);
4012 defsubr (&Ssignal);
4013 defsubr (&Sinteractive_p);
4014 defsubr (&Scalled_interactively_p);
4015 defsubr (&Scommandp);
4016 defsubr (&Sautoload);
4017 defsubr (&Seval);
4018 defsubr (&Sapply);
4019 defsubr (&Sfuncall);
4020 defsubr (&Srun_hooks);
4021 defsubr (&Srun_hook_with_args);
4022 defsubr (&Srun_hook_with_args_until_success);
4023 defsubr (&Srun_hook_with_args_until_failure);
4024 defsubr (&Sfetch_bytecode);
4025 defsubr (&Scurry);
4026 defsubr (&Sbacktrace_debug);
4027 defsubr (&Sbacktrace);
4028 defsubr (&Sbacktrace_frame);
4029 defsubr (&Scurry);
4030 defsubr (&Sspecial_variable_p);
4031 defsubr (&Sfunctionp);
4034 /* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
4035 (do not change this comment) */