lisp/image-mode.el: Fix change in 2011-04-09T20:28:01Z!cyd@stupidchicken.com.
[emacs.git] / src / eval.c
blob0f9e012b823163711cdd5d0e2571ebcfc3af5012
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <limits.h>
22 #include <setjmp.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25 #include "commands.h"
26 #include "keyboard.h"
27 #include "dispextern.h"
28 #include "frame.h" /* For XFRAME. */
30 #if HAVE_X_WINDOWS
31 #include "xterm.h"
32 #endif
34 #ifndef SIZE_MAX
35 # define SIZE_MAX ((size_t) -1)
36 #endif
38 /* This definition is duplicated in alloc.c and keyboard.c. */
39 /* Putting it in lisp.h makes cc bomb out! */
41 struct backtrace
43 struct backtrace *next;
44 Lisp_Object *function;
45 Lisp_Object *args; /* Points to vector of args. */
46 #define NARGS_BITS (BITS_PER_INT - 2)
47 /* Let's not use size_t because we want to allow negative values (for
48 UNEVALLED). Also let's steal 2 bits so we save a word (or more for
49 alignment). In any case I doubt Emacs would survive a function call with
50 more than 500M arguments. */
51 int nargs : NARGS_BITS; /* Length of vector.
52 If nargs is UNEVALLED, args points
53 to slot holding list of unevalled args. */
54 char evalargs : 1;
55 /* Nonzero means call value of debugger when done with this operation. */
56 char debug_on_exit : 1;
59 struct backtrace *backtrace_list;
60 struct catchtag *catchlist;
62 #ifdef DEBUG_GCPRO
63 /* Count levels of GCPRO to detect failure to UNGCPRO. */
64 int gcpro_level;
65 #endif
67 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
68 Lisp_Object Qinhibit_quit;
69 Lisp_Object Qand_rest, Qand_optional;
70 Lisp_Object Qdebug_on_error;
71 Lisp_Object Qdeclare;
72 Lisp_Object Qinternal_interpreter_environment, Qclosure;
74 Lisp_Object Qdebug;
76 /* This holds either the symbol `run-hooks' or nil.
77 It is nil at an early stage of startup, and when Emacs
78 is shutting down. */
80 Lisp_Object Vrun_hooks;
82 /* Non-nil means record all fset's and provide's, to be undone
83 if the file being autoloaded is not fully loaded.
84 They are recorded by being consed onto the front of Vautoload_queue:
85 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
87 Lisp_Object Vautoload_queue;
89 /* Current number of specbindings allocated in specpdl. */
91 EMACS_INT specpdl_size;
93 /* Pointer to beginning of specpdl. */
95 struct specbinding *specpdl;
97 /* Pointer to first unused element in specpdl. */
99 struct specbinding *specpdl_ptr;
101 /* Depth in Lisp evaluations and function calls. */
103 EMACS_INT lisp_eval_depth;
105 /* The value of num_nonmacro_input_events as of the last time we
106 started to enter the debugger. If we decide to enter the debugger
107 again when this is still equal to num_nonmacro_input_events, then we
108 know that the debugger itself has an error, and we should just
109 signal the error instead of entering an infinite loop of debugger
110 invocations. */
112 int when_entered_debugger;
114 /* The function from which the last `signal' was called. Set in
115 Fsignal. */
117 Lisp_Object Vsignaling_function;
119 /* Set to non-zero while processing X events. Checked in Feval to
120 make sure the Lisp interpreter isn't called from a signal handler,
121 which is unsafe because the interpreter isn't reentrant. */
123 int handling_signal;
125 static Lisp_Object funcall_lambda (Lisp_Object, size_t, Lisp_Object *);
126 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
127 static int interactive_p (int);
128 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
130 void
131 init_eval_once (void)
133 specpdl_size = 50;
134 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
135 specpdl_ptr = specpdl;
136 /* Don't forget to update docs (lispref node "Local Variables"). */
137 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
138 max_lisp_eval_depth = 600;
140 Vrun_hooks = Qnil;
143 void
144 init_eval (void)
146 specpdl_ptr = specpdl;
147 catchlist = 0;
148 handlerlist = 0;
149 backtrace_list = 0;
150 Vquit_flag = Qnil;
151 debug_on_next_call = 0;
152 lisp_eval_depth = 0;
153 #ifdef DEBUG_GCPRO
154 gcpro_level = 0;
155 #endif
156 /* This is less than the initial value of num_nonmacro_input_events. */
157 when_entered_debugger = -1;
160 /* Unwind-protect function used by call_debugger. */
162 static Lisp_Object
163 restore_stack_limits (Lisp_Object data)
165 max_specpdl_size = XINT (XCAR (data));
166 max_lisp_eval_depth = XINT (XCDR (data));
167 return Qnil;
170 /* Call the Lisp debugger, giving it argument ARG. */
172 static Lisp_Object
173 call_debugger (Lisp_Object arg)
175 int debug_while_redisplaying;
176 int count = SPECPDL_INDEX ();
177 Lisp_Object val;
178 EMACS_INT old_max = max_specpdl_size;
180 /* Temporarily bump up the stack limits,
181 so the debugger won't run out of stack. */
183 max_specpdl_size += 1;
184 record_unwind_protect (restore_stack_limits,
185 Fcons (make_number (old_max),
186 make_number (max_lisp_eval_depth)));
187 max_specpdl_size = old_max;
189 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
190 max_lisp_eval_depth = lisp_eval_depth + 40;
192 if (SPECPDL_INDEX () + 100 > max_specpdl_size)
193 max_specpdl_size = SPECPDL_INDEX () + 100;
195 #ifdef HAVE_WINDOW_SYSTEM
196 if (display_hourglass_p)
197 cancel_hourglass ();
198 #endif
200 debug_on_next_call = 0;
201 when_entered_debugger = num_nonmacro_input_events;
203 /* Resetting redisplaying_p to 0 makes sure that debug output is
204 displayed if the debugger is invoked during redisplay. */
205 debug_while_redisplaying = redisplaying_p;
206 redisplaying_p = 0;
207 specbind (intern ("debugger-may-continue"),
208 debug_while_redisplaying ? Qnil : Qt);
209 specbind (Qinhibit_redisplay, Qnil);
210 specbind (Qdebug_on_error, Qnil);
212 #if 0 /* Binding this prevents execution of Lisp code during
213 redisplay, which necessarily leads to display problems. */
214 specbind (Qinhibit_eval_during_redisplay, Qt);
215 #endif
217 val = apply1 (Vdebugger, arg);
219 /* Interrupting redisplay and resuming it later is not safe under
220 all circumstances. So, when the debugger returns, abort the
221 interrupted redisplay by going back to the top-level. */
222 if (debug_while_redisplaying)
223 Ftop_level ();
225 return unbind_to (count, val);
228 static void
229 do_debug_on_call (Lisp_Object code)
231 debug_on_next_call = 0;
232 backtrace_list->debug_on_exit = 1;
233 call_debugger (Fcons (code, Qnil));
236 /* NOTE!!! Every function that can call EVAL must protect its args
237 and temporaries from garbage collection while it needs them.
238 The definition of `For' shows what you have to do. */
240 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
241 doc: /* Eval args until one of them yields non-nil, then return that value.
242 The remaining args are not evalled at all.
243 If all args return nil, return nil.
244 usage: (or CONDITIONS...) */)
245 (Lisp_Object args)
247 register Lisp_Object val = Qnil;
248 struct gcpro gcpro1;
250 GCPRO1 (args);
252 while (CONSP (args))
254 val = eval_sub (XCAR (args));
255 if (!NILP (val))
256 break;
257 args = XCDR (args);
260 UNGCPRO;
261 return val;
264 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
265 doc: /* Eval args until one of them yields nil, then return nil.
266 The remaining args are not evalled at all.
267 If no arg yields nil, return the last arg's value.
268 usage: (and CONDITIONS...) */)
269 (Lisp_Object args)
271 register Lisp_Object val = Qt;
272 struct gcpro gcpro1;
274 GCPRO1 (args);
276 while (CONSP (args))
278 val = eval_sub (XCAR (args));
279 if (NILP (val))
280 break;
281 args = XCDR (args);
284 UNGCPRO;
285 return val;
288 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
289 doc: /* If COND yields non-nil, do THEN, else do ELSE...
290 Returns the value of THEN or the value of the last of the ELSE's.
291 THEN must be one expression, but ELSE... can be zero or more expressions.
292 If COND yields nil, and there are no ELSE's, the value is nil.
293 usage: (if COND THEN ELSE...) */)
294 (Lisp_Object args)
296 register Lisp_Object cond;
297 struct gcpro gcpro1;
299 GCPRO1 (args);
300 cond = eval_sub (Fcar (args));
301 UNGCPRO;
303 if (!NILP (cond))
304 return eval_sub (Fcar (Fcdr (args)));
305 return Fprogn (Fcdr (Fcdr (args)));
308 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
309 doc: /* Try each clause until one succeeds.
310 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
311 and, if the value is non-nil, this clause succeeds:
312 then the expressions in BODY are evaluated and the last one's
313 value is the value of the cond-form.
314 If no clause succeeds, cond returns nil.
315 If a clause has one element, as in (CONDITION),
316 CONDITION's value if non-nil is returned from the cond-form.
317 usage: (cond CLAUSES...) */)
318 (Lisp_Object args)
320 register Lisp_Object clause, val;
321 struct gcpro gcpro1;
323 val = Qnil;
324 GCPRO1 (args);
325 while (!NILP (args))
327 clause = Fcar (args);
328 val = eval_sub (Fcar (clause));
329 if (!NILP (val))
331 if (!EQ (XCDR (clause), Qnil))
332 val = Fprogn (XCDR (clause));
333 break;
335 args = XCDR (args);
337 UNGCPRO;
339 return val;
342 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
343 doc: /* Eval BODY forms sequentially and return value of last one.
344 usage: (progn BODY...) */)
345 (Lisp_Object args)
347 register Lisp_Object val = Qnil;
348 struct gcpro gcpro1;
350 GCPRO1 (args);
352 while (CONSP (args))
354 val = eval_sub (XCAR (args));
355 args = XCDR (args);
358 UNGCPRO;
359 return val;
362 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
363 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
364 The value of FIRST is saved during the evaluation of the remaining args,
365 whose values are discarded.
366 usage: (prog1 FIRST BODY...) */)
367 (Lisp_Object args)
369 Lisp_Object val;
370 register Lisp_Object args_left;
371 struct gcpro gcpro1, gcpro2;
372 register int argnum = 0;
374 if (NILP (args))
375 return Qnil;
377 args_left = args;
378 val = Qnil;
379 GCPRO2 (args, val);
383 Lisp_Object tem = eval_sub (XCAR (args_left));
384 if (!(argnum++))
385 val = tem;
386 args_left = XCDR (args_left);
388 while (CONSP (args_left));
390 UNGCPRO;
391 return val;
394 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
395 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
396 The value of FORM2 is saved during the evaluation of the
397 remaining args, whose values are discarded.
398 usage: (prog2 FORM1 FORM2 BODY...) */)
399 (Lisp_Object args)
401 Lisp_Object val;
402 register Lisp_Object args_left;
403 struct gcpro gcpro1, gcpro2;
404 register int argnum = -1;
406 val = Qnil;
408 if (NILP (args))
409 return Qnil;
411 args_left = args;
412 val = Qnil;
413 GCPRO2 (args, val);
417 Lisp_Object tem = eval_sub (XCAR (args_left));
418 if (!(argnum++))
419 val = tem;
420 args_left = XCDR (args_left);
422 while (CONSP (args_left));
424 UNGCPRO;
425 return val;
428 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
429 doc: /* Set each SYM to the value of its VAL.
430 The symbols SYM are variables; they are literal (not evaluated).
431 The values VAL are expressions; they are evaluated.
432 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
433 The second VAL is not computed until after the first SYM is set, and so on;
434 each VAL can use the new value of variables set earlier in the `setq'.
435 The return value of the `setq' form is the value of the last VAL.
436 usage: (setq [SYM VAL]...) */)
437 (Lisp_Object args)
439 register Lisp_Object args_left;
440 register Lisp_Object val, sym, lex_binding;
441 struct gcpro gcpro1;
443 if (NILP (args))
444 return Qnil;
446 args_left = args;
447 GCPRO1 (args);
451 val = eval_sub (Fcar (Fcdr (args_left)));
452 sym = Fcar (args_left);
454 /* Like for eval_sub, we do not check declared_special here since
455 it's been done when let-binding. */
456 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
457 && SYMBOLP (sym)
458 && !NILP (lex_binding
459 = Fassq (sym, Vinternal_interpreter_environment)))
460 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
461 else
462 Fset (sym, val); /* SYM is dynamically bound. */
464 args_left = Fcdr (Fcdr (args_left));
466 while (!NILP(args_left));
468 UNGCPRO;
469 return val;
472 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
473 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
474 usage: (quote ARG) */)
475 (Lisp_Object args)
477 if (!NILP (Fcdr (args)))
478 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
479 return Fcar (args);
482 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
483 doc: /* Like `quote', but preferred for objects which are functions.
484 In byte compilation, `function' causes its argument to be compiled.
485 `quote' cannot do that.
486 usage: (function ARG) */)
487 (Lisp_Object args)
489 Lisp_Object quoted = XCAR (args);
491 if (!NILP (Fcdr (args)))
492 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
494 if (!NILP (Vinternal_interpreter_environment)
495 && CONSP (quoted)
496 && EQ (XCAR (quoted), Qlambda))
497 /* This is a lambda expression within a lexical environment;
498 return an interpreted closure instead of a simple lambda. */
499 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
500 XCDR (quoted)));
501 else
502 /* Simply quote the argument. */
503 return quoted;
507 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
508 doc: /* Return t if the containing function was run directly by user input.
509 This means that the function was called with `call-interactively'
510 \(which includes being called as the binding of a key)
511 and input is currently coming from the keyboard (not a keyboard macro),
512 and Emacs is not running in batch mode (`noninteractive' is nil).
514 The only known proper use of `interactive-p' is in deciding whether to
515 display a helpful message, or how to display it. If you're thinking
516 of using it for any other purpose, it is quite likely that you're
517 making a mistake. Think: what do you want to do when the command is
518 called from a keyboard macro?
520 To test whether your function was called with `call-interactively',
521 either (i) add an extra optional argument and give it an `interactive'
522 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
523 use `called-interactively-p'. */)
524 (void)
526 return interactive_p (1) ? Qt : Qnil;
530 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
531 doc: /* Return t if the containing function was called by `call-interactively'.
532 If KIND is `interactive', then only return t if the call was made
533 interactively by the user, i.e. not in `noninteractive' mode nor
534 when `executing-kbd-macro'.
535 If KIND is `any', on the other hand, it will return t for any kind of
536 interactive call, including being called as the binding of a key, or
537 from a keyboard macro, or in `noninteractive' mode.
539 The only known proper use of `interactive' for KIND is in deciding
540 whether to display a helpful message, or how to display it. If you're
541 thinking of using it for any other purpose, it is quite likely that
542 you're making a mistake. Think: what do you want to do when the
543 command is called from a keyboard macro?
545 This function is meant for implementing advice and other
546 function-modifying features. Instead of using this, it is sometimes
547 cleaner to give your function an extra optional argument whose
548 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
549 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
550 (Lisp_Object kind)
552 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
553 && interactive_p (1)) ? Qt : Qnil;
557 /* Return 1 if function in which this appears was called using
558 call-interactively.
560 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
561 called is a built-in. */
563 static int
564 interactive_p (int exclude_subrs_p)
566 struct backtrace *btp;
567 Lisp_Object fun;
569 btp = backtrace_list;
571 /* If this isn't a byte-compiled function, there may be a frame at
572 the top for Finteractive_p. If so, skip it. */
573 fun = Findirect_function (*btp->function, Qnil);
574 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
575 || XSUBR (fun) == &Scalled_interactively_p))
576 btp = btp->next;
578 /* If we're running an Emacs 18-style byte-compiled function, there
579 may be a frame for Fbytecode at the top level. In any version of
580 Emacs there can be Fbytecode frames for subexpressions evaluated
581 inside catch and condition-case. Skip past them.
583 If this isn't a byte-compiled function, then we may now be
584 looking at several frames for special forms. Skip past them. */
585 while (btp
586 && (EQ (*btp->function, Qbytecode)
587 || btp->nargs == UNEVALLED))
588 btp = btp->next;
590 /* `btp' now points at the frame of the innermost function that isn't
591 a special form, ignoring frames for Finteractive_p and/or
592 Fbytecode at the top. If this frame is for a built-in function
593 (such as load or eval-region) return nil. */
594 fun = Findirect_function (*btp->function, Qnil);
595 if (exclude_subrs_p && SUBRP (fun))
596 return 0;
598 /* `btp' points to the frame of a Lisp function that called interactive-p.
599 Return t if that function was called interactively. */
600 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
601 return 1;
602 return 0;
606 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
607 doc: /* Define NAME as a function.
608 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
609 See also the function `interactive'.
610 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
611 (Lisp_Object args)
613 register Lisp_Object fn_name;
614 register Lisp_Object defn;
616 fn_name = Fcar (args);
617 CHECK_SYMBOL (fn_name);
618 defn = Fcons (Qlambda, Fcdr (args));
619 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
620 defn = Ffunction (Fcons (defn, Qnil));
621 if (!NILP (Vpurify_flag))
622 defn = Fpurecopy (defn);
623 if (CONSP (XSYMBOL (fn_name)->function)
624 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
625 LOADHIST_ATTACH (Fcons (Qt, fn_name));
626 Ffset (fn_name, defn);
627 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
628 return fn_name;
631 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
632 doc: /* Define NAME as a macro.
633 The actual definition looks like
634 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
635 When the macro is called, as in (NAME ARGS...),
636 the function (lambda ARGLIST BODY...) is applied to
637 the list ARGS... as it appears in the expression,
638 and the result should be a form to be evaluated instead of the original.
640 DECL is a declaration, optional, which can specify how to indent
641 calls to this macro, how Edebug should handle it, and which argument
642 should be treated as documentation. It looks like this:
643 (declare SPECS...)
644 The elements can look like this:
645 (indent INDENT)
646 Set NAME's `lisp-indent-function' property to INDENT.
648 (debug DEBUG)
649 Set NAME's `edebug-form-spec' property to DEBUG. (This is
650 equivalent to writing a `def-edebug-spec' for the macro.)
652 (doc-string ELT)
653 Set NAME's `doc-string-elt' property to ELT.
655 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
656 (Lisp_Object args)
658 register Lisp_Object fn_name;
659 register Lisp_Object defn;
660 Lisp_Object lambda_list, doc, tail;
662 fn_name = Fcar (args);
663 CHECK_SYMBOL (fn_name);
664 lambda_list = Fcar (Fcdr (args));
665 tail = Fcdr (Fcdr (args));
667 doc = Qnil;
668 if (STRINGP (Fcar (tail)))
670 doc = XCAR (tail);
671 tail = XCDR (tail);
674 if (CONSP (Fcar (tail))
675 && EQ (Fcar (Fcar (tail)), Qdeclare))
677 if (!NILP (Vmacro_declaration_function))
679 struct gcpro gcpro1;
680 GCPRO1 (args);
681 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
682 UNGCPRO;
685 tail = Fcdr (tail);
688 if (NILP (doc))
689 tail = Fcons (lambda_list, tail);
690 else
691 tail = Fcons (lambda_list, Fcons (doc, tail));
693 defn = Fcons (Qlambda, tail);
694 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
695 defn = Ffunction (Fcons (defn, Qnil));
696 defn = Fcons (Qmacro, defn);
698 if (!NILP (Vpurify_flag))
699 defn = Fpurecopy (defn);
700 if (CONSP (XSYMBOL (fn_name)->function)
701 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
702 LOADHIST_ATTACH (Fcons (Qt, fn_name));
703 Ffset (fn_name, defn);
704 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
705 return fn_name;
709 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
710 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
711 Aliased variables always have the same value; setting one sets the other.
712 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
713 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
714 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
715 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
716 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
717 The return value is BASE-VARIABLE. */)
718 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
720 struct Lisp_Symbol *sym;
722 CHECK_SYMBOL (new_alias);
723 CHECK_SYMBOL (base_variable);
725 sym = XSYMBOL (new_alias);
727 if (sym->constant)
728 /* Not sure why, but why not? */
729 error ("Cannot make a constant an alias");
731 switch (sym->redirect)
733 case SYMBOL_FORWARDED:
734 error ("Cannot make an internal variable an alias");
735 case SYMBOL_LOCALIZED:
736 error ("Don't know how to make a localized variable an alias");
739 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
740 If n_a is bound, but b_v is not, set the value of b_v to n_a,
741 so that old-code that affects n_a before the aliasing is setup
742 still works. */
743 if (NILP (Fboundp (base_variable)))
744 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
747 struct specbinding *p;
749 for (p = specpdl_ptr - 1; p >= specpdl; p--)
750 if (p->func == NULL
751 && (EQ (new_alias,
752 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
753 error ("Don't know how to make a let-bound variable an alias");
756 sym->declared_special = 1;
757 sym->redirect = SYMBOL_VARALIAS;
758 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
759 sym->constant = SYMBOL_CONSTANT_P (base_variable);
760 LOADHIST_ATTACH (new_alias);
761 /* Even if docstring is nil: remove old docstring. */
762 Fput (new_alias, Qvariable_documentation, docstring);
764 return base_variable;
768 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
769 doc: /* Define SYMBOL as a variable, and return SYMBOL.
770 You are not required to define a variable in order to use it,
771 but the definition can supply documentation and an initial value
772 in a way that tags can recognize.
774 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
775 If SYMBOL is buffer-local, its default value is what is set;
776 buffer-local values are not affected.
777 INITVALUE and DOCSTRING are optional.
778 If DOCSTRING starts with *, this variable is identified as a user option.
779 This means that M-x set-variable recognizes it.
780 See also `user-variable-p'.
781 If INITVALUE is missing, SYMBOL's value is not set.
783 If SYMBOL has a local binding, then this form affects the local
784 binding. This is usually not what you want. Thus, if you need to
785 load a file defining variables, with this form or with `defconst' or
786 `defcustom', you should always load that file _outside_ any bindings
787 for these variables. \(`defconst' and `defcustom' behave similarly in
788 this respect.)
789 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
790 (Lisp_Object args)
792 register Lisp_Object sym, tem, tail;
794 sym = Fcar (args);
795 tail = Fcdr (args);
796 if (!NILP (Fcdr (Fcdr (tail))))
797 error ("Too many arguments");
799 tem = Fdefault_boundp (sym);
800 if (!NILP (tail))
802 /* Do it before evaluating the initial value, for self-references. */
803 XSYMBOL (sym)->declared_special = 1;
805 if (SYMBOL_CONSTANT_P (sym))
807 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
808 Lisp_Object tem1 = Fcar (tail);
809 if (! (CONSP (tem1)
810 && EQ (XCAR (tem1), Qquote)
811 && CONSP (XCDR (tem1))
812 && EQ (XCAR (XCDR (tem1)), sym)))
813 error ("Constant symbol `%s' specified in defvar",
814 SDATA (SYMBOL_NAME (sym)));
817 if (NILP (tem))
818 Fset_default (sym, eval_sub (Fcar (tail)));
819 else
820 { /* Check if there is really a global binding rather than just a let
821 binding that shadows the global unboundness of the var. */
822 volatile struct specbinding *pdl = specpdl_ptr;
823 while (--pdl >= specpdl)
825 if (EQ (pdl->symbol, sym) && !pdl->func
826 && EQ (pdl->old_value, Qunbound))
828 message_with_string ("Warning: defvar ignored because %s is let-bound",
829 SYMBOL_NAME (sym), 1);
830 break;
834 tail = Fcdr (tail);
835 tem = Fcar (tail);
836 if (!NILP (tem))
838 if (!NILP (Vpurify_flag))
839 tem = Fpurecopy (tem);
840 Fput (sym, Qvariable_documentation, tem);
842 LOADHIST_ATTACH (sym);
844 else if (!NILP (Vinternal_interpreter_environment)
845 && !XSYMBOL (sym)->declared_special)
846 /* A simple (defvar foo) with lexical scoping does "nothing" except
847 declare that var to be dynamically scoped *locally* (i.e. within
848 the current file or let-block). */
849 Vinternal_interpreter_environment =
850 Fcons (sym, Vinternal_interpreter_environment);
851 else
853 /* Simple (defvar <var>) should not count as a definition at all.
854 It could get in the way of other definitions, and unloading this
855 package could try to make the variable unbound. */
858 return sym;
861 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
862 doc: /* Define SYMBOL as a constant variable.
863 The intent is that neither programs nor users should ever change this value.
864 Always sets the value of SYMBOL to the result of evalling INITVALUE.
865 If SYMBOL is buffer-local, its default value is what is set;
866 buffer-local values are not affected.
867 DOCSTRING is optional.
869 If SYMBOL has a local binding, then this form sets the local binding's
870 value. However, you should normally not make local bindings for
871 variables defined with this form.
872 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
873 (Lisp_Object args)
875 register Lisp_Object sym, tem;
877 sym = Fcar (args);
878 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
879 error ("Too many arguments");
881 tem = eval_sub (Fcar (Fcdr (args)));
882 if (!NILP (Vpurify_flag))
883 tem = Fpurecopy (tem);
884 Fset_default (sym, tem);
885 XSYMBOL (sym)->declared_special = 1;
886 tem = Fcar (Fcdr (Fcdr (args)));
887 if (!NILP (tem))
889 if (!NILP (Vpurify_flag))
890 tem = Fpurecopy (tem);
891 Fput (sym, Qvariable_documentation, tem);
893 Fput (sym, Qrisky_local_variable, Qt);
894 LOADHIST_ATTACH (sym);
895 return sym;
898 /* Error handler used in Fuser_variable_p. */
899 static Lisp_Object
900 user_variable_p_eh (Lisp_Object ignore)
902 return Qnil;
905 static Lisp_Object
906 lisp_indirect_variable (Lisp_Object sym)
908 struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
909 XSETSYMBOL (sym, s);
910 return sym;
913 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
914 doc: /* Return t if VARIABLE is intended to be set and modified by users.
915 \(The alternative is a variable used internally in a Lisp program.)
916 A variable is a user variable if
917 \(1) the first character of its documentation is `*', or
918 \(2) it is customizable (its property list contains a non-nil value
919 of `standard-value' or `custom-autoload'), or
920 \(3) it is an alias for another user variable.
921 Return nil if VARIABLE is an alias and there is a loop in the
922 chain of symbols. */)
923 (Lisp_Object variable)
925 Lisp_Object documentation;
927 if (!SYMBOLP (variable))
928 return Qnil;
930 /* If indirect and there's an alias loop, don't check anything else. */
931 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
932 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
933 Qt, user_variable_p_eh)))
934 return Qnil;
936 while (1)
938 documentation = Fget (variable, Qvariable_documentation);
939 if (INTEGERP (documentation) && XINT (documentation) < 0)
940 return Qt;
941 if (STRINGP (documentation)
942 && ((unsigned char) SREF (documentation, 0) == '*'))
943 return Qt;
944 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
945 if (CONSP (documentation)
946 && STRINGP (XCAR (documentation))
947 && INTEGERP (XCDR (documentation))
948 && XINT (XCDR (documentation)) < 0)
949 return Qt;
950 /* Customizable? See `custom-variable-p'. */
951 if ((!NILP (Fget (variable, intern ("standard-value"))))
952 || (!NILP (Fget (variable, intern ("custom-autoload")))))
953 return Qt;
955 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
956 return Qnil;
958 /* An indirect variable? Let's follow the chain. */
959 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
963 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
964 doc: /* Bind variables according to VARLIST then eval BODY.
965 The value of the last form in BODY is returned.
966 Each element of VARLIST is a symbol (which is bound to nil)
967 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
968 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
969 usage: (let* VARLIST BODY...) */)
970 (Lisp_Object args)
972 Lisp_Object varlist, var, val, elt, lexenv;
973 int count = SPECPDL_INDEX ();
974 struct gcpro gcpro1, gcpro2, gcpro3;
976 GCPRO3 (args, elt, varlist);
978 lexenv = Vinternal_interpreter_environment;
980 varlist = Fcar (args);
981 while (CONSP (varlist))
983 QUIT;
985 elt = XCAR (varlist);
986 if (SYMBOLP (elt))
988 var = elt;
989 val = Qnil;
991 else if (! NILP (Fcdr (Fcdr (elt))))
992 signal_error ("`let' bindings can have only one value-form", elt);
993 else
995 var = Fcar (elt);
996 val = eval_sub (Fcar (Fcdr (elt)));
999 if (!NILP (lexenv) && SYMBOLP (var)
1000 && !XSYMBOL (var)->declared_special
1001 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1002 /* Lexically bind VAR by adding it to the interpreter's binding
1003 alist. */
1005 Lisp_Object newenv
1006 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1007 if (EQ (Vinternal_interpreter_environment, lexenv))
1008 /* Save the old lexical environment on the specpdl stack,
1009 but only for the first lexical binding, since we'll never
1010 need to revert to one of the intermediate ones. */
1011 specbind (Qinternal_interpreter_environment, newenv);
1012 else
1013 Vinternal_interpreter_environment = newenv;
1015 else
1016 specbind (var, val);
1018 varlist = XCDR (varlist);
1020 UNGCPRO;
1021 val = Fprogn (Fcdr (args));
1022 return unbind_to (count, val);
1025 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1026 doc: /* Bind variables according to VARLIST then eval BODY.
1027 The value of the last form in BODY is returned.
1028 Each element of VARLIST is a symbol (which is bound to nil)
1029 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1030 All the VALUEFORMs are evalled before any symbols are bound.
1031 usage: (let VARLIST BODY...) */)
1032 (Lisp_Object args)
1034 Lisp_Object *temps, tem, lexenv;
1035 register Lisp_Object elt, varlist;
1036 int count = SPECPDL_INDEX ();
1037 register size_t argnum;
1038 struct gcpro gcpro1, gcpro2;
1039 USE_SAFE_ALLOCA;
1041 varlist = Fcar (args);
1043 /* Make space to hold the values to give the bound variables. */
1044 elt = Flength (varlist);
1045 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1047 /* Compute the values and store them in `temps'. */
1049 GCPRO2 (args, *temps);
1050 gcpro2.nvars = 0;
1052 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1054 QUIT;
1055 elt = XCAR (varlist);
1056 if (SYMBOLP (elt))
1057 temps [argnum++] = Qnil;
1058 else if (! NILP (Fcdr (Fcdr (elt))))
1059 signal_error ("`let' bindings can have only one value-form", elt);
1060 else
1061 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1062 gcpro2.nvars = argnum;
1064 UNGCPRO;
1066 lexenv = Vinternal_interpreter_environment;
1068 varlist = Fcar (args);
1069 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1071 Lisp_Object var;
1073 elt = XCAR (varlist);
1074 var = SYMBOLP (elt) ? elt : Fcar (elt);
1075 tem = temps[argnum++];
1077 if (!NILP (lexenv) && SYMBOLP (var)
1078 && !XSYMBOL (var)->declared_special
1079 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1080 /* Lexically bind VAR by adding it to the lexenv alist. */
1081 lexenv = Fcons (Fcons (var, tem), lexenv);
1082 else
1083 /* Dynamically bind VAR. */
1084 specbind (var, tem);
1087 if (!EQ (lexenv, Vinternal_interpreter_environment))
1088 /* Instantiate a new lexical environment. */
1089 specbind (Qinternal_interpreter_environment, lexenv);
1091 elt = Fprogn (Fcdr (args));
1092 SAFE_FREE ();
1093 return unbind_to (count, elt);
1096 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1097 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1098 The order of execution is thus TEST, BODY, TEST, BODY and so on
1099 until TEST returns nil.
1100 usage: (while TEST BODY...) */)
1101 (Lisp_Object args)
1103 Lisp_Object test, body;
1104 struct gcpro gcpro1, gcpro2;
1106 GCPRO2 (test, body);
1108 test = Fcar (args);
1109 body = Fcdr (args);
1110 while (!NILP (eval_sub (test)))
1112 QUIT;
1113 Fprogn (body);
1116 UNGCPRO;
1117 return Qnil;
1120 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1121 doc: /* Return result of expanding macros at top level of FORM.
1122 If FORM is not a macro call, it is returned unchanged.
1123 Otherwise, the macro is expanded and the expansion is considered
1124 in place of FORM. When a non-macro-call results, it is returned.
1126 The second optional arg ENVIRONMENT specifies an environment of macro
1127 definitions to shadow the loaded ones for use in file byte-compilation. */)
1128 (Lisp_Object form, Lisp_Object environment)
1130 /* With cleanups from Hallvard Furuseth. */
1131 register Lisp_Object expander, sym, def, tem;
1133 while (1)
1135 /* Come back here each time we expand a macro call,
1136 in case it expands into another macro call. */
1137 if (!CONSP (form))
1138 break;
1139 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1140 def = sym = XCAR (form);
1141 tem = Qnil;
1142 /* Trace symbols aliases to other symbols
1143 until we get a symbol that is not an alias. */
1144 while (SYMBOLP (def))
1146 QUIT;
1147 sym = def;
1148 tem = Fassq (sym, environment);
1149 if (NILP (tem))
1151 def = XSYMBOL (sym)->function;
1152 if (!EQ (def, Qunbound))
1153 continue;
1155 break;
1157 /* Right now TEM is the result from SYM in ENVIRONMENT,
1158 and if TEM is nil then DEF is SYM's function definition. */
1159 if (NILP (tem))
1161 /* SYM is not mentioned in ENVIRONMENT.
1162 Look at its function definition. */
1163 if (EQ (def, Qunbound) || !CONSP (def))
1164 /* Not defined or definition not suitable. */
1165 break;
1166 if (EQ (XCAR (def), Qautoload))
1168 /* Autoloading function: will it be a macro when loaded? */
1169 tem = Fnth (make_number (4), def);
1170 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1171 /* Yes, load it and try again. */
1173 struct gcpro gcpro1;
1174 GCPRO1 (form);
1175 do_autoload (def, sym);
1176 UNGCPRO;
1177 continue;
1179 else
1180 break;
1182 else if (!EQ (XCAR (def), Qmacro))
1183 break;
1184 else expander = XCDR (def);
1186 else
1188 expander = XCDR (tem);
1189 if (NILP (expander))
1190 break;
1192 form = apply1 (expander, XCDR (form));
1194 return form;
1197 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1198 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1199 TAG is evalled to get the tag to use; it must not be nil.
1201 Then the BODY is executed.
1202 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1203 If no throw happens, `catch' returns the value of the last BODY form.
1204 If a throw happens, it specifies the value to return from `catch'.
1205 usage: (catch TAG BODY...) */)
1206 (Lisp_Object args)
1208 register Lisp_Object tag;
1209 struct gcpro gcpro1;
1211 GCPRO1 (args);
1212 tag = eval_sub (Fcar (args));
1213 UNGCPRO;
1214 return internal_catch (tag, Fprogn, Fcdr (args));
1217 /* Set up a catch, then call C function FUNC on argument ARG.
1218 FUNC should return a Lisp_Object.
1219 This is how catches are done from within C code. */
1221 Lisp_Object
1222 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1224 /* This structure is made part of the chain `catchlist'. */
1225 struct catchtag c;
1227 /* Fill in the components of c, and put it on the list. */
1228 c.next = catchlist;
1229 c.tag = tag;
1230 c.val = Qnil;
1231 c.backlist = backtrace_list;
1232 c.handlerlist = handlerlist;
1233 c.lisp_eval_depth = lisp_eval_depth;
1234 c.pdlcount = SPECPDL_INDEX ();
1235 c.poll_suppress_count = poll_suppress_count;
1236 c.interrupt_input_blocked = interrupt_input_blocked;
1237 c.gcpro = gcprolist;
1238 c.byte_stack = byte_stack_list;
1239 catchlist = &c;
1241 /* Call FUNC. */
1242 if (! _setjmp (c.jmp))
1243 c.val = (*func) (arg);
1245 /* Throw works by a longjmp that comes right here. */
1246 catchlist = c.next;
1247 return c.val;
1250 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1251 jump to that CATCH, returning VALUE as the value of that catch.
1253 This is the guts Fthrow and Fsignal; they differ only in the way
1254 they choose the catch tag to throw to. A catch tag for a
1255 condition-case form has a TAG of Qnil.
1257 Before each catch is discarded, unbind all special bindings and
1258 execute all unwind-protect clauses made above that catch. Unwind
1259 the handler stack as we go, so that the proper handlers are in
1260 effect for each unwind-protect clause we run. At the end, restore
1261 some static info saved in CATCH, and longjmp to the location
1262 specified in the
1264 This is used for correct unwinding in Fthrow and Fsignal. */
1266 static void
1267 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1269 register int last_time;
1271 /* Save the value in the tag. */
1272 catch->val = value;
1274 /* Restore certain special C variables. */
1275 set_poll_suppress_count (catch->poll_suppress_count);
1276 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1277 handling_signal = 0;
1278 immediate_quit = 0;
1282 last_time = catchlist == catch;
1284 /* Unwind the specpdl stack, and then restore the proper set of
1285 handlers. */
1286 unbind_to (catchlist->pdlcount, Qnil);
1287 handlerlist = catchlist->handlerlist;
1288 catchlist = catchlist->next;
1290 while (! last_time);
1292 #if HAVE_X_WINDOWS
1293 /* If x_catch_errors was done, turn it off now.
1294 (First we give unbind_to a chance to do that.) */
1295 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1296 The catch must remain in effect during that delicate
1297 state. --lorentey */
1298 x_fully_uncatch_errors ();
1299 #endif
1300 #endif
1302 byte_stack_list = catch->byte_stack;
1303 gcprolist = catch->gcpro;
1304 #ifdef DEBUG_GCPRO
1305 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1306 #endif
1307 backtrace_list = catch->backlist;
1308 lisp_eval_depth = catch->lisp_eval_depth;
1310 _longjmp (catch->jmp, 1);
1313 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1314 doc: /* Throw to the catch for TAG and return VALUE from it.
1315 Both TAG and VALUE are evalled. */)
1316 (register Lisp_Object tag, Lisp_Object value)
1318 register struct catchtag *c;
1320 if (!NILP (tag))
1321 for (c = catchlist; c; c = c->next)
1323 if (EQ (c->tag, tag))
1324 unwind_to_catch (c, value);
1326 xsignal2 (Qno_catch, tag, value);
1330 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1331 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1332 If BODYFORM completes normally, its value is returned
1333 after executing the UNWINDFORMS.
1334 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1335 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1336 (Lisp_Object args)
1338 Lisp_Object val;
1339 int count = SPECPDL_INDEX ();
1341 record_unwind_protect (Fprogn, Fcdr (args));
1342 val = eval_sub (Fcar (args));
1343 return unbind_to (count, val);
1346 /* Chain of condition handlers currently in effect.
1347 The elements of this chain are contained in the stack frames
1348 of Fcondition_case and internal_condition_case.
1349 When an error is signaled (by calling Fsignal, below),
1350 this chain is searched for an element that applies. */
1352 struct handler *handlerlist;
1354 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1355 doc: /* Regain control when an error is signaled.
1356 Executes BODYFORM and returns its value if no error happens.
1357 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1358 where the BODY is made of Lisp expressions.
1360 A handler is applicable to an error
1361 if CONDITION-NAME is one of the error's condition names.
1362 If an error happens, the first applicable handler is run.
1364 The car of a handler may be a list of condition names
1365 instead of a single condition name. Then it handles all of them.
1367 When a handler handles an error, control returns to the `condition-case'
1368 and it executes the handler's BODY...
1369 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1370 \(If VAR is nil, the handler can't access that information.)
1371 Then the value of the last BODY form is returned from the `condition-case'
1372 expression.
1374 See also the function `signal' for more info.
1375 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1376 (Lisp_Object args)
1378 register Lisp_Object bodyform, handlers;
1379 volatile Lisp_Object var;
1381 var = Fcar (args);
1382 bodyform = Fcar (Fcdr (args));
1383 handlers = Fcdr (Fcdr (args));
1385 return internal_lisp_condition_case (var, bodyform, handlers);
1388 /* Like Fcondition_case, but the args are separate
1389 rather than passed in a list. Used by Fbyte_code. */
1391 Lisp_Object
1392 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1393 Lisp_Object handlers)
1395 Lisp_Object val;
1396 struct catchtag c;
1397 struct handler h;
1399 CHECK_SYMBOL (var);
1401 for (val = handlers; CONSP (val); val = XCDR (val))
1403 Lisp_Object tem;
1404 tem = XCAR (val);
1405 if (! (NILP (tem)
1406 || (CONSP (tem)
1407 && (SYMBOLP (XCAR (tem))
1408 || CONSP (XCAR (tem))))))
1409 error ("Invalid condition handler");
1412 c.tag = Qnil;
1413 c.val = Qnil;
1414 c.backlist = backtrace_list;
1415 c.handlerlist = handlerlist;
1416 c.lisp_eval_depth = lisp_eval_depth;
1417 c.pdlcount = SPECPDL_INDEX ();
1418 c.poll_suppress_count = poll_suppress_count;
1419 c.interrupt_input_blocked = interrupt_input_blocked;
1420 c.gcpro = gcprolist;
1421 c.byte_stack = byte_stack_list;
1422 if (_setjmp (c.jmp))
1424 if (!NILP (h.var))
1425 specbind (h.var, c.val);
1426 val = Fprogn (Fcdr (h.chosen_clause));
1428 /* Note that this just undoes the binding of h.var; whoever
1429 longjumped to us unwound the stack to c.pdlcount before
1430 throwing. */
1431 unbind_to (c.pdlcount, Qnil);
1432 return val;
1434 c.next = catchlist;
1435 catchlist = &c;
1437 h.var = var;
1438 h.handler = handlers;
1439 h.next = handlerlist;
1440 h.tag = &c;
1441 handlerlist = &h;
1443 val = eval_sub (bodyform);
1444 catchlist = c.next;
1445 handlerlist = h.next;
1446 return val;
1449 /* Call the function BFUN with no arguments, catching errors within it
1450 according to HANDLERS. If there is an error, call HFUN with
1451 one argument which is the data that describes the error:
1452 (SIGNALNAME . DATA)
1454 HANDLERS can be a list of conditions to catch.
1455 If HANDLERS is Qt, catch all errors.
1456 If HANDLERS is Qerror, catch all errors
1457 but allow the debugger to run if that is enabled. */
1459 Lisp_Object
1460 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1461 Lisp_Object (*hfun) (Lisp_Object))
1463 Lisp_Object val;
1464 struct catchtag c;
1465 struct handler h;
1467 /* Since Fsignal will close off all calls to x_catch_errors,
1468 we will get the wrong results if some are not closed now. */
1469 #if HAVE_X_WINDOWS
1470 if (x_catching_errors ())
1471 abort ();
1472 #endif
1474 c.tag = Qnil;
1475 c.val = Qnil;
1476 c.backlist = backtrace_list;
1477 c.handlerlist = handlerlist;
1478 c.lisp_eval_depth = lisp_eval_depth;
1479 c.pdlcount = SPECPDL_INDEX ();
1480 c.poll_suppress_count = poll_suppress_count;
1481 c.interrupt_input_blocked = interrupt_input_blocked;
1482 c.gcpro = gcprolist;
1483 c.byte_stack = byte_stack_list;
1484 if (_setjmp (c.jmp))
1486 return (*hfun) (c.val);
1488 c.next = catchlist;
1489 catchlist = &c;
1490 h.handler = handlers;
1491 h.var = Qnil;
1492 h.next = handlerlist;
1493 h.tag = &c;
1494 handlerlist = &h;
1496 val = (*bfun) ();
1497 catchlist = c.next;
1498 handlerlist = h.next;
1499 return val;
1502 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1504 Lisp_Object
1505 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1506 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1508 Lisp_Object val;
1509 struct catchtag c;
1510 struct handler h;
1512 /* Since Fsignal will close off all calls to x_catch_errors,
1513 we will get the wrong results if some are not closed now. */
1514 #if HAVE_X_WINDOWS
1515 if (x_catching_errors ())
1516 abort ();
1517 #endif
1519 c.tag = Qnil;
1520 c.val = Qnil;
1521 c.backlist = backtrace_list;
1522 c.handlerlist = handlerlist;
1523 c.lisp_eval_depth = lisp_eval_depth;
1524 c.pdlcount = SPECPDL_INDEX ();
1525 c.poll_suppress_count = poll_suppress_count;
1526 c.interrupt_input_blocked = interrupt_input_blocked;
1527 c.gcpro = gcprolist;
1528 c.byte_stack = byte_stack_list;
1529 if (_setjmp (c.jmp))
1531 return (*hfun) (c.val);
1533 c.next = catchlist;
1534 catchlist = &c;
1535 h.handler = handlers;
1536 h.var = Qnil;
1537 h.next = handlerlist;
1538 h.tag = &c;
1539 handlerlist = &h;
1541 val = (*bfun) (arg);
1542 catchlist = c.next;
1543 handlerlist = h.next;
1544 return val;
1547 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1548 its arguments. */
1550 Lisp_Object
1551 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1552 Lisp_Object arg1,
1553 Lisp_Object arg2,
1554 Lisp_Object handlers,
1555 Lisp_Object (*hfun) (Lisp_Object))
1557 Lisp_Object val;
1558 struct catchtag c;
1559 struct handler h;
1561 /* Since Fsignal will close off all calls to x_catch_errors,
1562 we will get the wrong results if some are not closed now. */
1563 #if HAVE_X_WINDOWS
1564 if (x_catching_errors ())
1565 abort ();
1566 #endif
1568 c.tag = Qnil;
1569 c.val = Qnil;
1570 c.backlist = backtrace_list;
1571 c.handlerlist = handlerlist;
1572 c.lisp_eval_depth = lisp_eval_depth;
1573 c.pdlcount = SPECPDL_INDEX ();
1574 c.poll_suppress_count = poll_suppress_count;
1575 c.interrupt_input_blocked = interrupt_input_blocked;
1576 c.gcpro = gcprolist;
1577 c.byte_stack = byte_stack_list;
1578 if (_setjmp (c.jmp))
1580 return (*hfun) (c.val);
1582 c.next = catchlist;
1583 catchlist = &c;
1584 h.handler = handlers;
1585 h.var = Qnil;
1586 h.next = handlerlist;
1587 h.tag = &c;
1588 handlerlist = &h;
1590 val = (*bfun) (arg1, arg2);
1591 catchlist = c.next;
1592 handlerlist = h.next;
1593 return val;
1596 /* Like internal_condition_case but call BFUN with NARGS as first,
1597 and ARGS as second argument. */
1599 Lisp_Object
1600 internal_condition_case_n (Lisp_Object (*bfun) (size_t, Lisp_Object *),
1601 size_t nargs,
1602 Lisp_Object *args,
1603 Lisp_Object handlers,
1604 Lisp_Object (*hfun) (Lisp_Object))
1606 Lisp_Object val;
1607 struct catchtag c;
1608 struct handler h;
1610 /* Since Fsignal will close off all calls to x_catch_errors,
1611 we will get the wrong results if some are not closed now. */
1612 #if HAVE_X_WINDOWS
1613 if (x_catching_errors ())
1614 abort ();
1615 #endif
1617 c.tag = Qnil;
1618 c.val = Qnil;
1619 c.backlist = backtrace_list;
1620 c.handlerlist = handlerlist;
1621 c.lisp_eval_depth = lisp_eval_depth;
1622 c.pdlcount = SPECPDL_INDEX ();
1623 c.poll_suppress_count = poll_suppress_count;
1624 c.interrupt_input_blocked = interrupt_input_blocked;
1625 c.gcpro = gcprolist;
1626 c.byte_stack = byte_stack_list;
1627 if (_setjmp (c.jmp))
1629 return (*hfun) (c.val);
1631 c.next = catchlist;
1632 catchlist = &c;
1633 h.handler = handlers;
1634 h.var = Qnil;
1635 h.next = handlerlist;
1636 h.tag = &c;
1637 handlerlist = &h;
1639 val = (*bfun) (nargs, args);
1640 catchlist = c.next;
1641 handlerlist = h.next;
1642 return val;
1646 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object,
1647 Lisp_Object, Lisp_Object);
1648 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1649 Lisp_Object data);
1651 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1652 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1653 This function does not return.
1655 An error symbol is a symbol with an `error-conditions' property
1656 that is a list of condition names.
1657 A handler for any of those names will get to handle this signal.
1658 The symbol `error' should normally be one of them.
1660 DATA should be a list. Its elements are printed as part of the error message.
1661 See Info anchor `(elisp)Definition of signal' for some details on how this
1662 error message is constructed.
1663 If the signal is handled, DATA is made available to the handler.
1664 See also the function `condition-case'. */)
1665 (Lisp_Object error_symbol, Lisp_Object data)
1667 /* When memory is full, ERROR-SYMBOL is nil,
1668 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1669 That is a special case--don't do this in other situations. */
1670 Lisp_Object conditions;
1671 Lisp_Object string;
1672 Lisp_Object real_error_symbol
1673 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1674 register Lisp_Object clause = Qnil;
1675 struct handler *h;
1676 struct backtrace *bp;
1678 immediate_quit = handling_signal = 0;
1679 abort_on_gc = 0;
1680 if (gc_in_progress || waiting_for_input)
1681 abort ();
1683 #if 0 /* rms: I don't know why this was here,
1684 but it is surely wrong for an error that is handled. */
1685 #ifdef HAVE_WINDOW_SYSTEM
1686 if (display_hourglass_p)
1687 cancel_hourglass ();
1688 #endif
1689 #endif
1691 /* This hook is used by edebug. */
1692 if (! NILP (Vsignal_hook_function)
1693 && ! NILP (error_symbol))
1695 /* Edebug takes care of restoring these variables when it exits. */
1696 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1697 max_lisp_eval_depth = lisp_eval_depth + 20;
1699 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1700 max_specpdl_size = SPECPDL_INDEX () + 40;
1702 call2 (Vsignal_hook_function, error_symbol, data);
1705 conditions = Fget (real_error_symbol, Qerror_conditions);
1707 /* Remember from where signal was called. Skip over the frame for
1708 `signal' itself. If a frame for `error' follows, skip that,
1709 too. Don't do this when ERROR_SYMBOL is nil, because that
1710 is a memory-full error. */
1711 Vsignaling_function = Qnil;
1712 if (backtrace_list && !NILP (error_symbol))
1714 bp = backtrace_list->next;
1715 if (bp && bp->function && EQ (*bp->function, Qerror))
1716 bp = bp->next;
1717 if (bp && bp->function)
1718 Vsignaling_function = *bp->function;
1721 for (h = handlerlist; h; h = h->next)
1723 clause = find_handler_clause (h->handler, conditions,
1724 error_symbol, data);
1725 if (!NILP (clause))
1726 break;
1729 if (/* Don't run the debugger for a memory-full error.
1730 (There is no room in memory to do that!) */
1731 !NILP (error_symbol)
1732 && (!NILP (Vdebug_on_signal)
1733 /* If no handler is present now, try to run the debugger. */
1734 || NILP (clause)
1735 /* Special handler that means "print a message and run debugger
1736 if requested". */
1737 || EQ (h->handler, Qerror)))
1739 int debugger_called
1740 = maybe_call_debugger (conditions, error_symbol, data);
1741 /* We can't return values to code which signaled an error, but we
1742 can continue code which has signaled a quit. */
1743 if (debugger_called && EQ (real_error_symbol, Qquit))
1744 return Qnil;
1747 if (!NILP (clause))
1749 Lisp_Object unwind_data
1750 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1752 h->chosen_clause = clause;
1753 unwind_to_catch (h->tag, unwind_data);
1755 else
1757 if (catchlist != 0)
1758 Fthrow (Qtop_level, Qt);
1761 if (! NILP (error_symbol))
1762 data = Fcons (error_symbol, data);
1764 string = Ferror_message_string (data);
1765 fatal ("%s", SDATA (string));
1768 /* Internal version of Fsignal that never returns.
1769 Used for anything but Qquit (which can return from Fsignal). */
1771 void
1772 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1774 Fsignal (error_symbol, data);
1775 abort ();
1778 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1780 void
1781 xsignal0 (Lisp_Object error_symbol)
1783 xsignal (error_symbol, Qnil);
1786 void
1787 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1789 xsignal (error_symbol, list1 (arg));
1792 void
1793 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1795 xsignal (error_symbol, list2 (arg1, arg2));
1798 void
1799 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1801 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1804 /* Signal `error' with message S, and additional arg ARG.
1805 If ARG is not a genuine list, make it a one-element list. */
1807 void
1808 signal_error (const char *s, Lisp_Object arg)
1810 Lisp_Object tortoise, hare;
1812 hare = tortoise = arg;
1813 while (CONSP (hare))
1815 hare = XCDR (hare);
1816 if (!CONSP (hare))
1817 break;
1819 hare = XCDR (hare);
1820 tortoise = XCDR (tortoise);
1822 if (EQ (hare, tortoise))
1823 break;
1826 if (!NILP (hare))
1827 arg = Fcons (arg, Qnil); /* Make it a list. */
1829 xsignal (Qerror, Fcons (build_string (s), arg));
1833 /* Return nonzero if LIST is a non-nil atom or
1834 a list containing one of CONDITIONS. */
1836 static int
1837 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1839 if (NILP (list))
1840 return 0;
1841 if (! CONSP (list))
1842 return 1;
1844 while (CONSP (conditions))
1846 Lisp_Object this, tail;
1847 this = XCAR (conditions);
1848 for (tail = list; CONSP (tail); tail = XCDR (tail))
1849 if (EQ (XCAR (tail), this))
1850 return 1;
1851 conditions = XCDR (conditions);
1853 return 0;
1856 /* Return 1 if an error with condition-symbols CONDITIONS,
1857 and described by SIGNAL-DATA, should skip the debugger
1858 according to debugger-ignored-errors. */
1860 static int
1861 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1863 Lisp_Object tail;
1864 int first_string = 1;
1865 Lisp_Object error_message;
1867 error_message = Qnil;
1868 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1870 if (STRINGP (XCAR (tail)))
1872 if (first_string)
1874 error_message = Ferror_message_string (data);
1875 first_string = 0;
1878 if (fast_string_match (XCAR (tail), error_message) >= 0)
1879 return 1;
1881 else
1883 Lisp_Object contail;
1885 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1886 if (EQ (XCAR (tail), XCAR (contail)))
1887 return 1;
1891 return 0;
1894 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1895 SIG and DATA describe the signal, as in find_handler_clause. */
1897 static int
1898 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1900 Lisp_Object combined_data;
1902 combined_data = Fcons (sig, data);
1904 if (
1905 /* Don't try to run the debugger with interrupts blocked.
1906 The editing loop would return anyway. */
1907 ! INPUT_BLOCKED_P
1908 /* Does user want to enter debugger for this kind of error? */
1909 && (EQ (sig, Qquit)
1910 ? debug_on_quit
1911 : wants_debugger (Vdebug_on_error, conditions))
1912 && ! skip_debugger (conditions, combined_data)
1913 /* RMS: What's this for? */
1914 && when_entered_debugger < num_nonmacro_input_events)
1916 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1917 return 1;
1920 return 0;
1923 /* Value of Qlambda means we have called debugger and user has continued.
1924 There are two ways to pass SIG and DATA:
1925 = SIG is the error symbol, and DATA is the rest of the data.
1926 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1927 This is for memory-full errors only.
1929 We need to increase max_specpdl_size temporarily around
1930 anything we do that can push on the specpdl, so as not to get
1931 a second error here in case we're handling specpdl overflow. */
1933 static Lisp_Object
1934 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions,
1935 Lisp_Object sig, Lisp_Object data)
1937 register Lisp_Object h;
1939 /* t is used by handlers for all conditions, set up by C code. */
1940 if (EQ (handlers, Qt))
1941 return Qt;
1943 /* error is used similarly, but means print an error message
1944 and run the debugger if that is enabled. */
1945 if (EQ (handlers, Qerror))
1946 return Qt;
1948 for (h = handlers; CONSP (h); h = XCDR (h))
1950 Lisp_Object handler = XCAR (h);
1951 Lisp_Object condit, tem;
1953 if (!CONSP (handler))
1954 continue;
1955 condit = XCAR (handler);
1956 /* Handle a single condition name in handler HANDLER. */
1957 if (SYMBOLP (condit))
1959 tem = Fmemq (Fcar (handler), conditions);
1960 if (!NILP (tem))
1961 return handler;
1963 /* Handle a list of condition names in handler HANDLER. */
1964 else if (CONSP (condit))
1966 Lisp_Object tail;
1967 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1969 tem = Fmemq (XCAR (tail), conditions);
1970 if (!NILP (tem))
1971 return handler;
1976 return Qnil;
1980 /* Dump an error message; called like vprintf. */
1981 void
1982 verror (const char *m, va_list ap)
1984 char buf[4000];
1985 size_t size = sizeof buf;
1986 size_t size_max =
1987 min (MOST_POSITIVE_FIXNUM, min (INT_MAX, SIZE_MAX - 1)) + 1;
1988 char *buffer = buf;
1989 int used;
1990 Lisp_Object string;
1992 while (1)
1994 used = vsnprintf (buffer, size, m, ap);
1996 if (used < 0)
1998 /* Non-C99 vsnprintf, such as w32, returns -1 when SIZE is too small.
1999 Guess a larger USED to work around the incompatibility. */
2000 used = (size <= size_max / 2 ? 2 * size
2001 : size < size_max ? size_max - 1
2002 : size_max);
2004 else if (used < size)
2005 break;
2006 if (size_max <= used)
2007 memory_full ();
2008 size = used + 1;
2010 if (buffer != buf)
2011 xfree (buffer);
2012 buffer = (char *) xmalloc (size);
2015 string = make_string (buffer, used);
2016 if (buffer != buf)
2017 xfree (buffer);
2019 xsignal1 (Qerror, string);
2023 /* Dump an error message; called like printf. */
2025 /* VARARGS 1 */
2026 void
2027 error (const char *m, ...)
2029 va_list ap;
2030 va_start (ap, m);
2031 verror (m, ap);
2032 va_end (ap);
2035 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2036 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2037 This means it contains a description for how to read arguments to give it.
2038 The value is nil for an invalid function or a symbol with no function
2039 definition.
2041 Interactively callable functions include strings and vectors (treated
2042 as keyboard macros), lambda-expressions that contain a top-level call
2043 to `interactive', autoload definitions made by `autoload' with non-nil
2044 fourth argument, and some of the built-in functions of Lisp.
2046 Also, a symbol satisfies `commandp' if its function definition does so.
2048 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2049 then strings and vectors are not accepted. */)
2050 (Lisp_Object function, Lisp_Object for_call_interactively)
2052 register Lisp_Object fun;
2053 register Lisp_Object funcar;
2054 Lisp_Object if_prop = Qnil;
2056 fun = function;
2058 fun = indirect_function (fun); /* Check cycles. */
2059 if (NILP (fun) || EQ (fun, Qunbound))
2060 return Qnil;
2062 /* Check an `interactive-form' property if present, analogous to the
2063 function-documentation property. */
2064 fun = function;
2065 while (SYMBOLP (fun))
2067 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2068 if (!NILP (tmp))
2069 if_prop = Qt;
2070 fun = Fsymbol_function (fun);
2073 /* Emacs primitives are interactive if their DEFUN specifies an
2074 interactive spec. */
2075 if (SUBRP (fun))
2076 return XSUBR (fun)->intspec ? Qt : if_prop;
2078 /* Bytecode objects are interactive if they are long enough to
2079 have an element whose index is COMPILED_INTERACTIVE, which is
2080 where the interactive spec is stored. */
2081 else if (COMPILEDP (fun))
2082 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2083 ? Qt : if_prop);
2085 /* Strings and vectors are keyboard macros. */
2086 if (STRINGP (fun) || VECTORP (fun))
2087 return (NILP (for_call_interactively) ? Qt : Qnil);
2089 /* Lists may represent commands. */
2090 if (!CONSP (fun))
2091 return Qnil;
2092 funcar = XCAR (fun);
2093 if (EQ (funcar, Qclosure))
2094 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
2095 ? Qt : if_prop);
2096 else if (EQ (funcar, Qlambda))
2097 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2098 else if (EQ (funcar, Qautoload))
2099 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2100 else
2101 return Qnil;
2104 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2105 doc: /* Define FUNCTION to autoload from FILE.
2106 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2107 Third arg DOCSTRING is documentation for the function.
2108 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2109 Fifth arg TYPE indicates the type of the object:
2110 nil or omitted says FUNCTION is a function,
2111 `keymap' says FUNCTION is really a keymap, and
2112 `macro' or t says FUNCTION is really a macro.
2113 Third through fifth args give info about the real definition.
2114 They default to nil.
2115 If FUNCTION is already defined other than as an autoload,
2116 this does nothing and returns nil. */)
2117 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2119 CHECK_SYMBOL (function);
2120 CHECK_STRING (file);
2122 /* If function is defined and not as an autoload, don't override. */
2123 if (!EQ (XSYMBOL (function)->function, Qunbound)
2124 && !(CONSP (XSYMBOL (function)->function)
2125 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2126 return Qnil;
2128 if (NILP (Vpurify_flag))
2129 /* Only add entries after dumping, because the ones before are
2130 not useful and else we get loads of them from the loaddefs.el. */
2131 LOADHIST_ATTACH (Fcons (Qautoload, function));
2132 else
2133 /* We don't want the docstring in purespace (instead,
2134 Snarf-documentation should (hopefully) overwrite it).
2135 We used to use 0 here, but that leads to accidental sharing in
2136 purecopy's hash-consing, so we use a (hopefully) unique integer
2137 instead. */
2138 docstring = make_number (XHASH (function));
2139 return Ffset (function,
2140 Fpurecopy (list5 (Qautoload, file, docstring,
2141 interactive, type)));
2144 Lisp_Object
2145 un_autoload (Lisp_Object oldqueue)
2147 register Lisp_Object queue, first, second;
2149 /* Queue to unwind is current value of Vautoload_queue.
2150 oldqueue is the shadowed value to leave in Vautoload_queue. */
2151 queue = Vautoload_queue;
2152 Vautoload_queue = oldqueue;
2153 while (CONSP (queue))
2155 first = XCAR (queue);
2156 second = Fcdr (first);
2157 first = Fcar (first);
2158 if (EQ (first, make_number (0)))
2159 Vfeatures = second;
2160 else
2161 Ffset (first, second);
2162 queue = XCDR (queue);
2164 return Qnil;
2167 /* Load an autoloaded function.
2168 FUNNAME is the symbol which is the function's name.
2169 FUNDEF is the autoload definition (a list). */
2171 void
2172 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2174 int count = SPECPDL_INDEX ();
2175 Lisp_Object fun;
2176 struct gcpro gcpro1, gcpro2, gcpro3;
2178 /* This is to make sure that loadup.el gives a clear picture
2179 of what files are preloaded and when. */
2180 if (! NILP (Vpurify_flag))
2181 error ("Attempt to autoload %s while preparing to dump",
2182 SDATA (SYMBOL_NAME (funname)));
2184 fun = funname;
2185 CHECK_SYMBOL (funname);
2186 GCPRO3 (fun, funname, fundef);
2188 /* Preserve the match data. */
2189 record_unwind_save_match_data ();
2191 /* If autoloading gets an error (which includes the error of failing
2192 to define the function being called), we use Vautoload_queue
2193 to undo function definitions and `provide' calls made by
2194 the function. We do this in the specific case of autoloading
2195 because autoloading is not an explicit request "load this file",
2196 but rather a request to "call this function".
2198 The value saved here is to be restored into Vautoload_queue. */
2199 record_unwind_protect (un_autoload, Vautoload_queue);
2200 Vautoload_queue = Qt;
2201 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2203 /* Once loading finishes, don't undo it. */
2204 Vautoload_queue = Qt;
2205 unbind_to (count, Qnil);
2207 fun = Findirect_function (fun, Qnil);
2209 if (!NILP (Fequal (fun, fundef)))
2210 error ("Autoloading failed to define function %s",
2211 SDATA (SYMBOL_NAME (funname)));
2212 UNGCPRO;
2216 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2217 doc: /* Evaluate FORM and return its value.
2218 If LEXICAL is t, evaluate using lexical scoping. */)
2219 (Lisp_Object form, Lisp_Object lexical)
2221 int count = SPECPDL_INDEX ();
2222 specbind (Qinternal_interpreter_environment,
2223 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2224 return unbind_to (count, eval_sub (form));
2227 /* Eval a sub-expression of the current expression (i.e. in the same
2228 lexical scope). */
2229 Lisp_Object
2230 eval_sub (Lisp_Object form)
2232 Lisp_Object fun, val, original_fun, original_args;
2233 Lisp_Object funcar;
2234 struct backtrace backtrace;
2235 struct gcpro gcpro1, gcpro2, gcpro3;
2237 if (handling_signal)
2238 abort ();
2240 if (SYMBOLP (form))
2242 /* Look up its binding in the lexical environment.
2243 We do not pay attention to the declared_special flag here, since we
2244 already did that when let-binding the variable. */
2245 Lisp_Object lex_binding
2246 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2247 ? Fassq (form, Vinternal_interpreter_environment)
2248 : Qnil;
2249 if (CONSP (lex_binding))
2250 return XCDR (lex_binding);
2251 else
2252 return Fsymbol_value (form);
2255 if (!CONSP (form))
2256 return form;
2258 QUIT;
2259 if ((consing_since_gc > gc_cons_threshold
2260 && consing_since_gc > gc_relative_threshold)
2262 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2264 GCPRO1 (form);
2265 Fgarbage_collect ();
2266 UNGCPRO;
2269 if (++lisp_eval_depth > max_lisp_eval_depth)
2271 if (max_lisp_eval_depth < 100)
2272 max_lisp_eval_depth = 100;
2273 if (lisp_eval_depth > max_lisp_eval_depth)
2274 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2277 original_fun = Fcar (form);
2278 original_args = Fcdr (form);
2280 backtrace.next = backtrace_list;
2281 backtrace_list = &backtrace;
2282 backtrace.function = &original_fun; /* This also protects them from gc. */
2283 backtrace.args = &original_args;
2284 backtrace.nargs = UNEVALLED;
2285 backtrace.evalargs = 1;
2286 backtrace.debug_on_exit = 0;
2288 if (debug_on_next_call)
2289 do_debug_on_call (Qt);
2291 /* At this point, only original_fun and original_args
2292 have values that will be used below. */
2293 retry:
2295 /* Optimize for no indirection. */
2296 fun = original_fun;
2297 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2298 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2299 fun = indirect_function (fun);
2301 if (SUBRP (fun))
2303 Lisp_Object numargs;
2304 Lisp_Object argvals[8];
2305 Lisp_Object args_left;
2306 register int i, maxargs;
2308 args_left = original_args;
2309 numargs = Flength (args_left);
2311 CHECK_CONS_LIST ();
2313 if (XINT (numargs) < XSUBR (fun)->min_args
2314 || (XSUBR (fun)->max_args >= 0
2315 && XSUBR (fun)->max_args < XINT (numargs)))
2316 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2318 else if (XSUBR (fun)->max_args == UNEVALLED)
2320 backtrace.evalargs = 0;
2321 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2323 else if (XSUBR (fun)->max_args == MANY)
2325 /* Pass a vector of evaluated arguments. */
2326 Lisp_Object *vals;
2327 register size_t argnum = 0;
2328 USE_SAFE_ALLOCA;
2330 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2332 GCPRO3 (args_left, fun, fun);
2333 gcpro3.var = vals;
2334 gcpro3.nvars = 0;
2336 while (!NILP (args_left))
2338 vals[argnum++] = eval_sub (Fcar (args_left));
2339 args_left = Fcdr (args_left);
2340 gcpro3.nvars = argnum;
2343 backtrace.args = vals;
2344 backtrace.nargs = XINT (numargs);
2346 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2347 UNGCPRO;
2348 SAFE_FREE ();
2350 else
2352 GCPRO3 (args_left, fun, fun);
2353 gcpro3.var = argvals;
2354 gcpro3.nvars = 0;
2356 maxargs = XSUBR (fun)->max_args;
2357 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2359 argvals[i] = eval_sub (Fcar (args_left));
2360 gcpro3.nvars = ++i;
2363 UNGCPRO;
2365 backtrace.args = argvals;
2366 backtrace.nargs = XINT (numargs);
2368 switch (i)
2370 case 0:
2371 val = (XSUBR (fun)->function.a0 ());
2372 break;
2373 case 1:
2374 val = (XSUBR (fun)->function.a1 (argvals[0]));
2375 break;
2376 case 2:
2377 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2378 break;
2379 case 3:
2380 val = (XSUBR (fun)->function.a3
2381 (argvals[0], argvals[1], argvals[2]));
2382 break;
2383 case 4:
2384 val = (XSUBR (fun)->function.a4
2385 (argvals[0], argvals[1], argvals[2], argvals[3]));
2386 break;
2387 case 5:
2388 val = (XSUBR (fun)->function.a5
2389 (argvals[0], argvals[1], argvals[2], argvals[3],
2390 argvals[4]));
2391 break;
2392 case 6:
2393 val = (XSUBR (fun)->function.a6
2394 (argvals[0], argvals[1], argvals[2], argvals[3],
2395 argvals[4], argvals[5]));
2396 break;
2397 case 7:
2398 val = (XSUBR (fun)->function.a7
2399 (argvals[0], argvals[1], argvals[2], argvals[3],
2400 argvals[4], argvals[5], argvals[6]));
2401 break;
2403 case 8:
2404 val = (XSUBR (fun)->function.a8
2405 (argvals[0], argvals[1], argvals[2], argvals[3],
2406 argvals[4], argvals[5], argvals[6], argvals[7]));
2407 break;
2409 default:
2410 /* Someone has created a subr that takes more arguments than
2411 is supported by this code. We need to either rewrite the
2412 subr to use a different argument protocol, or add more
2413 cases to this switch. */
2414 abort ();
2418 else if (COMPILEDP (fun))
2419 val = apply_lambda (fun, original_args);
2420 else
2422 if (EQ (fun, Qunbound))
2423 xsignal1 (Qvoid_function, original_fun);
2424 if (!CONSP (fun))
2425 xsignal1 (Qinvalid_function, original_fun);
2426 funcar = XCAR (fun);
2427 if (!SYMBOLP (funcar))
2428 xsignal1 (Qinvalid_function, original_fun);
2429 if (EQ (funcar, Qautoload))
2431 do_autoload (fun, original_fun);
2432 goto retry;
2434 if (EQ (funcar, Qmacro))
2435 val = eval_sub (apply1 (Fcdr (fun), original_args));
2436 else if (EQ (funcar, Qlambda)
2437 || EQ (funcar, Qclosure))
2438 val = apply_lambda (fun, original_args);
2439 else
2440 xsignal1 (Qinvalid_function, original_fun);
2442 CHECK_CONS_LIST ();
2444 lisp_eval_depth--;
2445 if (backtrace.debug_on_exit)
2446 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2447 backtrace_list = backtrace.next;
2449 return val;
2452 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2453 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2454 Then return the value FUNCTION returns.
2455 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2456 usage: (apply FUNCTION &rest ARGUMENTS) */)
2457 (size_t nargs, Lisp_Object *args)
2459 register size_t i, numargs;
2460 register Lisp_Object spread_arg;
2461 register Lisp_Object *funcall_args;
2462 Lisp_Object fun, retval;
2463 struct gcpro gcpro1;
2464 USE_SAFE_ALLOCA;
2466 fun = args [0];
2467 funcall_args = 0;
2468 spread_arg = args [nargs - 1];
2469 CHECK_LIST (spread_arg);
2471 numargs = XINT (Flength (spread_arg));
2473 if (numargs == 0)
2474 return Ffuncall (nargs - 1, args);
2475 else if (numargs == 1)
2477 args [nargs - 1] = XCAR (spread_arg);
2478 return Ffuncall (nargs, args);
2481 numargs += nargs - 2;
2483 /* Optimize for no indirection. */
2484 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2485 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2486 fun = indirect_function (fun);
2487 if (EQ (fun, Qunbound))
2489 /* Let funcall get the error. */
2490 fun = args[0];
2491 goto funcall;
2494 if (SUBRP (fun))
2496 if (numargs < XSUBR (fun)->min_args
2497 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2498 goto funcall; /* Let funcall get the error. */
2499 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2501 /* Avoid making funcall cons up a yet another new vector of arguments
2502 by explicitly supplying nil's for optional values. */
2503 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2504 for (i = numargs; i < XSUBR (fun)->max_args;)
2505 funcall_args[++i] = Qnil;
2506 GCPRO1 (*funcall_args);
2507 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2510 funcall:
2511 /* We add 1 to numargs because funcall_args includes the
2512 function itself as well as its arguments. */
2513 if (!funcall_args)
2515 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2516 GCPRO1 (*funcall_args);
2517 gcpro1.nvars = 1 + numargs;
2520 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2521 /* Spread the last arg we got. Its first element goes in
2522 the slot that it used to occupy, hence this value of I. */
2523 i = nargs - 1;
2524 while (!NILP (spread_arg))
2526 funcall_args [i++] = XCAR (spread_arg);
2527 spread_arg = XCDR (spread_arg);
2530 /* By convention, the caller needs to gcpro Ffuncall's args. */
2531 retval = Ffuncall (gcpro1.nvars, funcall_args);
2532 UNGCPRO;
2533 SAFE_FREE ();
2535 return retval;
2538 /* Run hook variables in various ways. */
2540 static Lisp_Object
2541 funcall_nil (size_t nargs, Lisp_Object *args)
2543 Ffuncall (nargs, args);
2544 return Qnil;
2547 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2548 doc: /* Run each hook in HOOKS.
2549 Each argument should be a symbol, a hook variable.
2550 These symbols are processed in the order specified.
2551 If a hook symbol has a non-nil value, that value may be a function
2552 or a list of functions to be called to run the hook.
2553 If the value is a function, it is called with no arguments.
2554 If it is a list, the elements are called, in order, with no arguments.
2556 Major modes should not use this function directly to run their mode
2557 hook; they should use `run-mode-hooks' instead.
2559 Do not use `make-local-variable' to make a hook variable buffer-local.
2560 Instead, use `add-hook' and specify t for the LOCAL argument.
2561 usage: (run-hooks &rest HOOKS) */)
2562 (size_t nargs, Lisp_Object *args)
2564 Lisp_Object hook[1];
2565 register size_t i;
2567 for (i = 0; i < nargs; i++)
2569 hook[0] = args[i];
2570 run_hook_with_args (1, hook, funcall_nil);
2573 return Qnil;
2576 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2577 Srun_hook_with_args, 1, MANY, 0,
2578 doc: /* Run HOOK with the specified arguments ARGS.
2579 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2580 value, that value may be a function or a list of functions to be
2581 called to run the hook. If the value is a function, it is called with
2582 the given arguments and its return value is returned. If it is a list
2583 of functions, those functions are called, in order,
2584 with the given arguments ARGS.
2585 It is best not to depend on the value returned by `run-hook-with-args',
2586 as that may change.
2588 Do not use `make-local-variable' to make a hook variable buffer-local.
2589 Instead, use `add-hook' and specify t for the LOCAL argument.
2590 usage: (run-hook-with-args HOOK &rest ARGS) */)
2591 (size_t nargs, Lisp_Object *args)
2593 return run_hook_with_args (nargs, args, funcall_nil);
2596 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2597 Srun_hook_with_args_until_success, 1, MANY, 0,
2598 doc: /* Run HOOK with the specified arguments ARGS.
2599 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2600 value, that value may be a function or a list of functions to be
2601 called to run the hook. If the value is a function, it is called with
2602 the given arguments and its return value is returned.
2603 If it is a list of functions, those functions are called, in order,
2604 with the given arguments ARGS, until one of them
2605 returns a non-nil value. Then we return that value.
2606 However, if they all return nil, we return nil.
2608 Do not use `make-local-variable' to make a hook variable buffer-local.
2609 Instead, use `add-hook' and specify t for the LOCAL argument.
2610 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2611 (size_t nargs, Lisp_Object *args)
2613 return run_hook_with_args (nargs, args, Ffuncall);
2616 static Lisp_Object
2617 funcall_not (size_t nargs, Lisp_Object *args)
2619 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2622 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2623 Srun_hook_with_args_until_failure, 1, MANY, 0,
2624 doc: /* Run HOOK with the specified arguments ARGS.
2625 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2626 value, that value may be a function or a list of functions to be
2627 called to run the hook. If the value is a function, it is called with
2628 the given arguments and its return value is returned.
2629 If it is a list of functions, those functions are called, in order,
2630 with the given arguments ARGS, until one of them returns nil.
2631 Then we return nil. However, if they all return non-nil, we return non-nil.
2633 Do not use `make-local-variable' to make a hook variable buffer-local.
2634 Instead, use `add-hook' and specify t for the LOCAL argument.
2635 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2636 (size_t nargs, Lisp_Object *args)
2638 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2641 static Lisp_Object
2642 run_hook_wrapped_funcall (size_t nargs, Lisp_Object *args)
2644 Lisp_Object tmp = args[0], ret;
2645 args[0] = args[1];
2646 args[1] = tmp;
2647 ret = Ffuncall (nargs, args);
2648 args[1] = args[0];
2649 args[0] = tmp;
2650 return ret;
2653 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2654 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2655 I.e. instead of calling each function FUN directly with arguments ARGS,
2656 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2657 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2658 aborts and returns that value.
2659 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2660 (size_t nargs, Lisp_Object *args)
2662 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2665 /* ARGS[0] should be a hook symbol.
2666 Call each of the functions in the hook value, passing each of them
2667 as arguments all the rest of ARGS (all NARGS - 1 elements).
2668 FUNCALL specifies how to call each function on the hook.
2669 The caller (or its caller, etc) must gcpro all of ARGS,
2670 except that it isn't necessary to gcpro ARGS[0]. */
2672 Lisp_Object
2673 run_hook_with_args (size_t nargs, Lisp_Object *args,
2674 Lisp_Object (*funcall) (size_t nargs, Lisp_Object *args))
2676 Lisp_Object sym, val, ret = Qnil;
2677 struct gcpro gcpro1, gcpro2, gcpro3;
2679 /* If we are dying or still initializing,
2680 don't do anything--it would probably crash if we tried. */
2681 if (NILP (Vrun_hooks))
2682 return Qnil;
2684 sym = args[0];
2685 val = find_symbol_value (sym);
2687 if (EQ (val, Qunbound) || NILP (val))
2688 return ret;
2689 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2691 args[0] = val;
2692 return funcall (nargs, args);
2694 else
2696 Lisp_Object global_vals = Qnil;
2697 GCPRO3 (sym, val, global_vals);
2699 for (;
2700 CONSP (val) && NILP (ret);
2701 val = XCDR (val))
2703 if (EQ (XCAR (val), Qt))
2705 /* t indicates this hook has a local binding;
2706 it means to run the global binding too. */
2707 global_vals = Fdefault_value (sym);
2708 if (NILP (global_vals)) continue;
2710 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2712 args[0] = global_vals;
2713 ret = funcall (nargs, args);
2715 else
2717 for (;
2718 CONSP (global_vals) && NILP (ret);
2719 global_vals = XCDR (global_vals))
2721 args[0] = XCAR (global_vals);
2722 /* In a global value, t should not occur. If it does, we
2723 must ignore it to avoid an endless loop. */
2724 if (!EQ (args[0], Qt))
2725 ret = funcall (nargs, args);
2729 else
2731 args[0] = XCAR (val);
2732 ret = funcall (nargs, args);
2736 UNGCPRO;
2737 return ret;
2741 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2743 void
2744 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2746 Lisp_Object temp[3];
2747 temp[0] = hook;
2748 temp[1] = arg1;
2749 temp[2] = arg2;
2751 Frun_hook_with_args (3, temp);
2754 /* Apply fn to arg. */
2755 Lisp_Object
2756 apply1 (Lisp_Object fn, Lisp_Object arg)
2758 struct gcpro gcpro1;
2760 GCPRO1 (fn);
2761 if (NILP (arg))
2762 RETURN_UNGCPRO (Ffuncall (1, &fn));
2763 gcpro1.nvars = 2;
2765 Lisp_Object args[2];
2766 args[0] = fn;
2767 args[1] = arg;
2768 gcpro1.var = args;
2769 RETURN_UNGCPRO (Fapply (2, args));
2773 /* Call function fn on no arguments. */
2774 Lisp_Object
2775 call0 (Lisp_Object fn)
2777 struct gcpro gcpro1;
2779 GCPRO1 (fn);
2780 RETURN_UNGCPRO (Ffuncall (1, &fn));
2783 /* Call function fn with 1 argument arg1. */
2784 /* ARGSUSED */
2785 Lisp_Object
2786 call1 (Lisp_Object fn, Lisp_Object arg1)
2788 struct gcpro gcpro1;
2789 Lisp_Object args[2];
2791 args[0] = fn;
2792 args[1] = arg1;
2793 GCPRO1 (args[0]);
2794 gcpro1.nvars = 2;
2795 RETURN_UNGCPRO (Ffuncall (2, args));
2798 /* Call function fn with 2 arguments arg1, arg2. */
2799 /* ARGSUSED */
2800 Lisp_Object
2801 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2803 struct gcpro gcpro1;
2804 Lisp_Object args[3];
2805 args[0] = fn;
2806 args[1] = arg1;
2807 args[2] = arg2;
2808 GCPRO1 (args[0]);
2809 gcpro1.nvars = 3;
2810 RETURN_UNGCPRO (Ffuncall (3, args));
2813 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2814 /* ARGSUSED */
2815 Lisp_Object
2816 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2818 struct gcpro gcpro1;
2819 Lisp_Object args[4];
2820 args[0] = fn;
2821 args[1] = arg1;
2822 args[2] = arg2;
2823 args[3] = arg3;
2824 GCPRO1 (args[0]);
2825 gcpro1.nvars = 4;
2826 RETURN_UNGCPRO (Ffuncall (4, args));
2829 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2830 /* ARGSUSED */
2831 Lisp_Object
2832 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2833 Lisp_Object arg4)
2835 struct gcpro gcpro1;
2836 Lisp_Object args[5];
2837 args[0] = fn;
2838 args[1] = arg1;
2839 args[2] = arg2;
2840 args[3] = arg3;
2841 args[4] = arg4;
2842 GCPRO1 (args[0]);
2843 gcpro1.nvars = 5;
2844 RETURN_UNGCPRO (Ffuncall (5, args));
2847 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2848 /* ARGSUSED */
2849 Lisp_Object
2850 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2851 Lisp_Object arg4, Lisp_Object arg5)
2853 struct gcpro gcpro1;
2854 Lisp_Object args[6];
2855 args[0] = fn;
2856 args[1] = arg1;
2857 args[2] = arg2;
2858 args[3] = arg3;
2859 args[4] = arg4;
2860 args[5] = arg5;
2861 GCPRO1 (args[0]);
2862 gcpro1.nvars = 6;
2863 RETURN_UNGCPRO (Ffuncall (6, args));
2866 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2867 /* ARGSUSED */
2868 Lisp_Object
2869 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2870 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2872 struct gcpro gcpro1;
2873 Lisp_Object args[7];
2874 args[0] = fn;
2875 args[1] = arg1;
2876 args[2] = arg2;
2877 args[3] = arg3;
2878 args[4] = arg4;
2879 args[5] = arg5;
2880 args[6] = arg6;
2881 GCPRO1 (args[0]);
2882 gcpro1.nvars = 7;
2883 RETURN_UNGCPRO (Ffuncall (7, args));
2886 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2887 /* ARGSUSED */
2888 Lisp_Object
2889 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2890 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2892 struct gcpro gcpro1;
2893 Lisp_Object args[8];
2894 args[0] = fn;
2895 args[1] = arg1;
2896 args[2] = arg2;
2897 args[3] = arg3;
2898 args[4] = arg4;
2899 args[5] = arg5;
2900 args[6] = arg6;
2901 args[7] = arg7;
2902 GCPRO1 (args[0]);
2903 gcpro1.nvars = 8;
2904 RETURN_UNGCPRO (Ffuncall (8, args));
2907 /* The caller should GCPRO all the elements of ARGS. */
2909 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2910 doc: /* Non-nil if OBJECT is a function. */)
2911 (Lisp_Object object)
2913 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2915 object = Findirect_function (object, Qt);
2917 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2919 /* Autoloaded symbols are functions, except if they load
2920 macros or keymaps. */
2921 int i;
2922 for (i = 0; i < 4 && CONSP (object); i++)
2923 object = XCDR (object);
2925 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2929 if (SUBRP (object))
2930 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2931 else if (COMPILEDP (object))
2932 return Qt;
2933 else if (CONSP (object))
2935 Lisp_Object car = XCAR (object);
2936 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2938 else
2939 return Qnil;
2942 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2943 doc: /* Call first argument as a function, passing remaining arguments to it.
2944 Return the value that function returns.
2945 Thus, (funcall 'cons 'x 'y) returns (x . y).
2946 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2947 (size_t nargs, Lisp_Object *args)
2949 Lisp_Object fun, original_fun;
2950 Lisp_Object funcar;
2951 size_t numargs = nargs - 1;
2952 Lisp_Object lisp_numargs;
2953 Lisp_Object val;
2954 struct backtrace backtrace;
2955 register Lisp_Object *internal_args;
2956 register size_t i;
2958 QUIT;
2959 if ((consing_since_gc > gc_cons_threshold
2960 && consing_since_gc > gc_relative_threshold)
2962 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2963 Fgarbage_collect ();
2965 if (++lisp_eval_depth > max_lisp_eval_depth)
2967 if (max_lisp_eval_depth < 100)
2968 max_lisp_eval_depth = 100;
2969 if (lisp_eval_depth > max_lisp_eval_depth)
2970 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2973 backtrace.next = backtrace_list;
2974 backtrace_list = &backtrace;
2975 backtrace.function = &args[0];
2976 backtrace.args = &args[1];
2977 backtrace.nargs = nargs - 1;
2978 backtrace.evalargs = 0;
2979 backtrace.debug_on_exit = 0;
2981 if (debug_on_next_call)
2982 do_debug_on_call (Qlambda);
2984 CHECK_CONS_LIST ();
2986 original_fun = args[0];
2988 retry:
2990 /* Optimize for no indirection. */
2991 fun = original_fun;
2992 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2993 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2994 fun = indirect_function (fun);
2996 if (SUBRP (fun))
2998 if (numargs < XSUBR (fun)->min_args
2999 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
3001 XSETFASTINT (lisp_numargs, numargs);
3002 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
3005 else if (XSUBR (fun)->max_args == UNEVALLED)
3006 xsignal1 (Qinvalid_function, original_fun);
3008 else if (XSUBR (fun)->max_args == MANY)
3009 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
3010 else
3012 if (XSUBR (fun)->max_args > numargs)
3014 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
3015 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
3016 for (i = numargs; i < XSUBR (fun)->max_args; i++)
3017 internal_args[i] = Qnil;
3019 else
3020 internal_args = args + 1;
3021 switch (XSUBR (fun)->max_args)
3023 case 0:
3024 val = (XSUBR (fun)->function.a0 ());
3025 break;
3026 case 1:
3027 val = (XSUBR (fun)->function.a1 (internal_args[0]));
3028 break;
3029 case 2:
3030 val = (XSUBR (fun)->function.a2
3031 (internal_args[0], internal_args[1]));
3032 break;
3033 case 3:
3034 val = (XSUBR (fun)->function.a3
3035 (internal_args[0], internal_args[1], internal_args[2]));
3036 break;
3037 case 4:
3038 val = (XSUBR (fun)->function.a4
3039 (internal_args[0], internal_args[1], internal_args[2],
3040 internal_args[3]));
3041 break;
3042 case 5:
3043 val = (XSUBR (fun)->function.a5
3044 (internal_args[0], internal_args[1], internal_args[2],
3045 internal_args[3], internal_args[4]));
3046 break;
3047 case 6:
3048 val = (XSUBR (fun)->function.a6
3049 (internal_args[0], internal_args[1], internal_args[2],
3050 internal_args[3], internal_args[4], internal_args[5]));
3051 break;
3052 case 7:
3053 val = (XSUBR (fun)->function.a7
3054 (internal_args[0], internal_args[1], internal_args[2],
3055 internal_args[3], internal_args[4], internal_args[5],
3056 internal_args[6]));
3057 break;
3059 case 8:
3060 val = (XSUBR (fun)->function.a8
3061 (internal_args[0], internal_args[1], internal_args[2],
3062 internal_args[3], internal_args[4], internal_args[5],
3063 internal_args[6], internal_args[7]));
3064 break;
3066 default:
3068 /* If a subr takes more than 8 arguments without using MANY
3069 or UNEVALLED, we need to extend this function to support it.
3070 Until this is done, there is no way to call the function. */
3071 abort ();
3075 else if (COMPILEDP (fun))
3076 val = funcall_lambda (fun, numargs, args + 1);
3077 else
3079 if (EQ (fun, Qunbound))
3080 xsignal1 (Qvoid_function, original_fun);
3081 if (!CONSP (fun))
3082 xsignal1 (Qinvalid_function, original_fun);
3083 funcar = XCAR (fun);
3084 if (!SYMBOLP (funcar))
3085 xsignal1 (Qinvalid_function, original_fun);
3086 if (EQ (funcar, Qlambda)
3087 || EQ (funcar, Qclosure))
3088 val = funcall_lambda (fun, numargs, args + 1);
3089 else if (EQ (funcar, Qautoload))
3091 do_autoload (fun, original_fun);
3092 CHECK_CONS_LIST ();
3093 goto retry;
3095 else
3096 xsignal1 (Qinvalid_function, original_fun);
3098 CHECK_CONS_LIST ();
3099 lisp_eval_depth--;
3100 if (backtrace.debug_on_exit)
3101 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3102 backtrace_list = backtrace.next;
3103 return val;
3106 static Lisp_Object
3107 apply_lambda (Lisp_Object fun, Lisp_Object args)
3109 Lisp_Object args_left;
3110 size_t numargs;
3111 register Lisp_Object *arg_vector;
3112 struct gcpro gcpro1, gcpro2, gcpro3;
3113 register size_t i;
3114 register Lisp_Object tem;
3115 USE_SAFE_ALLOCA;
3117 numargs = XINT (Flength (args));
3118 SAFE_ALLOCA_LISP (arg_vector, numargs);
3119 args_left = args;
3121 GCPRO3 (*arg_vector, args_left, fun);
3122 gcpro1.nvars = 0;
3124 for (i = 0; i < numargs; )
3126 tem = Fcar (args_left), args_left = Fcdr (args_left);
3127 tem = eval_sub (tem);
3128 arg_vector[i++] = tem;
3129 gcpro1.nvars = i;
3132 UNGCPRO;
3134 backtrace_list->args = arg_vector;
3135 backtrace_list->nargs = i;
3136 backtrace_list->evalargs = 0;
3137 tem = funcall_lambda (fun, numargs, arg_vector);
3139 /* Do the debug-on-exit now, while arg_vector still exists. */
3140 if (backtrace_list->debug_on_exit)
3141 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3142 /* Don't do it again when we return to eval. */
3143 backtrace_list->debug_on_exit = 0;
3144 SAFE_FREE ();
3145 return tem;
3148 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3149 and return the result of evaluation.
3150 FUN must be either a lambda-expression or a compiled-code object. */
3152 static Lisp_Object
3153 funcall_lambda (Lisp_Object fun, size_t nargs,
3154 register Lisp_Object *arg_vector)
3156 Lisp_Object val, syms_left, next, lexenv;
3157 int count = SPECPDL_INDEX ();
3158 size_t i;
3159 int optional, rest;
3161 if (CONSP (fun))
3163 if (EQ (XCAR (fun), Qclosure))
3165 fun = XCDR (fun); /* Drop `closure'. */
3166 lexenv = XCAR (fun);
3167 CHECK_LIST_CONS (fun, fun);
3169 else
3170 lexenv = Qnil;
3171 syms_left = XCDR (fun);
3172 if (CONSP (syms_left))
3173 syms_left = XCAR (syms_left);
3174 else
3175 xsignal1 (Qinvalid_function, fun);
3177 else if (COMPILEDP (fun))
3179 syms_left = AREF (fun, COMPILED_ARGLIST);
3180 if (INTEGERP (syms_left))
3181 /* A byte-code object with a non-nil `push args' slot means we
3182 shouldn't bind any arguments, instead just call the byte-code
3183 interpreter directly; it will push arguments as necessary.
3185 Byte-code objects with either a non-existant, or a nil value for
3186 the `push args' slot (the default), have dynamically-bound
3187 arguments, and use the argument-binding code below instead (as do
3188 all interpreted functions, even lexically bound ones). */
3190 /* If we have not actually read the bytecode string
3191 and constants vector yet, fetch them from the file. */
3192 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3193 Ffetch_bytecode (fun);
3194 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3195 AREF (fun, COMPILED_CONSTANTS),
3196 AREF (fun, COMPILED_STACK_DEPTH),
3197 syms_left,
3198 nargs, arg_vector);
3200 lexenv = Qnil;
3202 else
3203 abort ();
3205 i = optional = rest = 0;
3206 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3208 QUIT;
3210 next = XCAR (syms_left);
3211 if (!SYMBOLP (next))
3212 xsignal1 (Qinvalid_function, fun);
3214 if (EQ (next, Qand_rest))
3215 rest = 1;
3216 else if (EQ (next, Qand_optional))
3217 optional = 1;
3218 else
3220 Lisp_Object arg;
3221 if (rest)
3223 arg = Flist (nargs - i, &arg_vector[i]);
3224 i = nargs;
3226 else if (i < nargs)
3227 arg = arg_vector[i++];
3228 else if (!optional)
3229 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3230 else
3231 arg = Qnil;
3233 /* Bind the argument. */
3234 if (!NILP (lexenv) && SYMBOLP (next))
3235 /* Lexically bind NEXT by adding it to the lexenv alist. */
3236 lexenv = Fcons (Fcons (next, arg), lexenv);
3237 else
3238 /* Dynamically bind NEXT. */
3239 specbind (next, arg);
3243 if (!NILP (syms_left))
3244 xsignal1 (Qinvalid_function, fun);
3245 else if (i < nargs)
3246 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3248 if (!EQ (lexenv, Vinternal_interpreter_environment))
3249 /* Instantiate a new lexical environment. */
3250 specbind (Qinternal_interpreter_environment, lexenv);
3252 if (CONSP (fun))
3253 val = Fprogn (XCDR (XCDR (fun)));
3254 else
3256 /* If we have not actually read the bytecode string
3257 and constants vector yet, fetch them from the file. */
3258 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3259 Ffetch_bytecode (fun);
3260 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3261 AREF (fun, COMPILED_CONSTANTS),
3262 AREF (fun, COMPILED_STACK_DEPTH),
3263 Qnil, 0, 0);
3266 return unbind_to (count, val);
3269 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3270 1, 1, 0,
3271 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3272 (Lisp_Object object)
3274 Lisp_Object tem;
3276 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3278 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3279 if (!CONSP (tem))
3281 tem = AREF (object, COMPILED_BYTECODE);
3282 if (CONSP (tem) && STRINGP (XCAR (tem)))
3283 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3284 else
3285 error ("Invalid byte code");
3287 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3288 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3290 return object;
3293 static void
3294 grow_specpdl (void)
3296 register int count = SPECPDL_INDEX ();
3297 if (specpdl_size >= max_specpdl_size)
3299 if (max_specpdl_size < 400)
3300 max_specpdl_size = 400;
3301 if (specpdl_size >= max_specpdl_size)
3302 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3304 specpdl_size *= 2;
3305 if (specpdl_size > max_specpdl_size)
3306 specpdl_size = max_specpdl_size;
3307 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
3308 specpdl_ptr = specpdl + count;
3311 /* `specpdl_ptr->symbol' is a field which describes which variable is
3312 let-bound, so it can be properly undone when we unbind_to.
3313 It can have the following two shapes:
3314 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3315 a symbol that is not buffer-local (at least at the time
3316 the let binding started). Note also that it should not be
3317 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3318 to record V2 here).
3319 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3320 variable SYMBOL which can be buffer-local. WHERE tells us
3321 which buffer is affected (or nil if the let-binding affects the
3322 global value of the variable) and BUFFER tells us which buffer was
3323 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3324 BUFFER did not yet have a buffer-local value). */
3326 void
3327 specbind (Lisp_Object symbol, Lisp_Object value)
3329 struct Lisp_Symbol *sym;
3331 eassert (!handling_signal);
3333 CHECK_SYMBOL (symbol);
3334 sym = XSYMBOL (symbol);
3335 if (specpdl_ptr == specpdl + specpdl_size)
3336 grow_specpdl ();
3338 start:
3339 switch (sym->redirect)
3341 case SYMBOL_VARALIAS:
3342 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3343 case SYMBOL_PLAINVAL:
3344 /* The most common case is that of a non-constant symbol with a
3345 trivial value. Make that as fast as we can. */
3346 specpdl_ptr->symbol = symbol;
3347 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3348 specpdl_ptr->func = NULL;
3349 ++specpdl_ptr;
3350 if (!sym->constant)
3351 SET_SYMBOL_VAL (sym, value);
3352 else
3353 set_internal (symbol, value, Qnil, 1);
3354 break;
3355 case SYMBOL_LOCALIZED:
3356 if (SYMBOL_BLV (sym)->frame_local)
3357 error ("Frame-local vars cannot be let-bound");
3358 case SYMBOL_FORWARDED:
3360 Lisp_Object ovalue = find_symbol_value (symbol);
3361 specpdl_ptr->func = 0;
3362 specpdl_ptr->old_value = ovalue;
3364 eassert (sym->redirect != SYMBOL_LOCALIZED
3365 || (EQ (SYMBOL_BLV (sym)->where,
3366 SYMBOL_BLV (sym)->frame_local ?
3367 Fselected_frame () : Fcurrent_buffer ())));
3369 if (sym->redirect == SYMBOL_LOCALIZED
3370 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3372 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3374 /* For a local variable, record both the symbol and which
3375 buffer's or frame's value we are saving. */
3376 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3378 eassert (sym->redirect != SYMBOL_LOCALIZED
3379 || (BLV_FOUND (SYMBOL_BLV (sym))
3380 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3381 where = cur_buf;
3383 else if (sym->redirect == SYMBOL_LOCALIZED
3384 && BLV_FOUND (SYMBOL_BLV (sym)))
3385 where = SYMBOL_BLV (sym)->where;
3386 else
3387 where = Qnil;
3389 /* We're not using the `unused' slot in the specbinding
3390 structure because this would mean we have to do more
3391 work for simple variables. */
3392 /* FIXME: The third value `current_buffer' is only used in
3393 let_shadows_buffer_binding_p which is itself only used
3394 in set_internal for local_if_set. */
3395 eassert (NILP (where) || EQ (where, cur_buf));
3396 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3398 /* If SYMBOL is a per-buffer variable which doesn't have a
3399 buffer-local value here, make the `let' change the global
3400 value by changing the value of SYMBOL in all buffers not
3401 having their own value. This is consistent with what
3402 happens with other buffer-local variables. */
3403 if (NILP (where)
3404 && sym->redirect == SYMBOL_FORWARDED)
3406 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3407 ++specpdl_ptr;
3408 Fset_default (symbol, value);
3409 return;
3412 else
3413 specpdl_ptr->symbol = symbol;
3415 specpdl_ptr++;
3416 set_internal (symbol, value, Qnil, 1);
3417 break;
3419 default: abort ();
3423 void
3424 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3426 eassert (!handling_signal);
3428 if (specpdl_ptr == specpdl + specpdl_size)
3429 grow_specpdl ();
3430 specpdl_ptr->func = function;
3431 specpdl_ptr->symbol = Qnil;
3432 specpdl_ptr->old_value = arg;
3433 specpdl_ptr++;
3436 Lisp_Object
3437 unbind_to (int count, Lisp_Object value)
3439 Lisp_Object quitf = Vquit_flag;
3440 struct gcpro gcpro1, gcpro2;
3442 GCPRO2 (value, quitf);
3443 Vquit_flag = Qnil;
3445 while (specpdl_ptr != specpdl + count)
3447 /* Copy the binding, and decrement specpdl_ptr, before we do
3448 the work to unbind it. We decrement first
3449 so that an error in unbinding won't try to unbind
3450 the same entry again, and we copy the binding first
3451 in case more bindings are made during some of the code we run. */
3453 struct specbinding this_binding;
3454 this_binding = *--specpdl_ptr;
3456 if (this_binding.func != 0)
3457 (*this_binding.func) (this_binding.old_value);
3458 /* If the symbol is a list, it is really (SYMBOL WHERE
3459 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3460 frame. If WHERE is a buffer or frame, this indicates we
3461 bound a variable that had a buffer-local or frame-local
3462 binding. WHERE nil means that the variable had the default
3463 value when it was bound. CURRENT-BUFFER is the buffer that
3464 was current when the variable was bound. */
3465 else if (CONSP (this_binding.symbol))
3467 Lisp_Object symbol, where;
3469 symbol = XCAR (this_binding.symbol);
3470 where = XCAR (XCDR (this_binding.symbol));
3472 if (NILP (where))
3473 Fset_default (symbol, this_binding.old_value);
3474 /* If `where' is non-nil, reset the value in the appropriate
3475 local binding, but only if that binding still exists. */
3476 else if (BUFFERP (where)
3477 ? !NILP (Flocal_variable_p (symbol, where))
3478 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3479 set_internal (symbol, this_binding.old_value, where, 1);
3481 /* If variable has a trivial value (no forwarding), we can
3482 just set it. No need to check for constant symbols here,
3483 since that was already done by specbind. */
3484 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3485 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3486 this_binding.old_value);
3487 else
3488 /* NOTE: we only ever come here if make_local_foo was used for
3489 the first time on this var within this let. */
3490 Fset_default (this_binding.symbol, this_binding.old_value);
3493 if (NILP (Vquit_flag) && !NILP (quitf))
3494 Vquit_flag = quitf;
3496 UNGCPRO;
3497 return value;
3500 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3501 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3502 A special variable is one that will be bound dynamically, even in a
3503 context where binding is lexical by default. */)
3504 (Lisp_Object symbol)
3506 CHECK_SYMBOL (symbol);
3507 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3511 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3512 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3513 The debugger is entered when that frame exits, if the flag is non-nil. */)
3514 (Lisp_Object level, Lisp_Object flag)
3516 register struct backtrace *backlist = backtrace_list;
3517 register int i;
3519 CHECK_NUMBER (level);
3521 for (i = 0; backlist && i < XINT (level); i++)
3523 backlist = backlist->next;
3526 if (backlist)
3527 backlist->debug_on_exit = !NILP (flag);
3529 return flag;
3532 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3533 doc: /* Print a trace of Lisp function calls currently active.
3534 Output stream used is value of `standard-output'. */)
3535 (void)
3537 register struct backtrace *backlist = backtrace_list;
3538 Lisp_Object tail;
3539 Lisp_Object tem;
3540 struct gcpro gcpro1;
3541 Lisp_Object old_print_level = Vprint_level;
3543 if (NILP (Vprint_level))
3544 XSETFASTINT (Vprint_level, 8);
3546 tail = Qnil;
3547 GCPRO1 (tail);
3549 while (backlist)
3551 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3552 if (backlist->nargs == UNEVALLED)
3554 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3555 write_string ("\n", -1);
3557 else
3559 tem = *backlist->function;
3560 Fprin1 (tem, Qnil); /* This can QUIT. */
3561 write_string ("(", -1);
3562 if (backlist->nargs == MANY)
3563 { /* FIXME: Can this happen? */
3564 int i;
3565 for (tail = *backlist->args, i = 0;
3566 !NILP (tail);
3567 tail = Fcdr (tail), i = 1)
3569 if (i) write_string (" ", -1);
3570 Fprin1 (Fcar (tail), Qnil);
3573 else
3575 size_t i;
3576 for (i = 0; i < backlist->nargs; i++)
3578 if (i) write_string (" ", -1);
3579 Fprin1 (backlist->args[i], Qnil);
3582 write_string (")\n", -1);
3584 backlist = backlist->next;
3587 Vprint_level = old_print_level;
3588 UNGCPRO;
3589 return Qnil;
3592 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3593 doc: /* Return the function and arguments NFRAMES up from current execution point.
3594 If that frame has not evaluated the arguments yet (or is a special form),
3595 the value is (nil FUNCTION ARG-FORMS...).
3596 If that frame has evaluated its arguments and called its function already,
3597 the value is (t FUNCTION ARG-VALUES...).
3598 A &rest arg is represented as the tail of the list ARG-VALUES.
3599 FUNCTION is whatever was supplied as car of evaluated list,
3600 or a lambda expression for macro calls.
3601 If NFRAMES is more than the number of frames, the value is nil. */)
3602 (Lisp_Object nframes)
3604 register struct backtrace *backlist = backtrace_list;
3605 register EMACS_INT i;
3606 Lisp_Object tem;
3608 CHECK_NATNUM (nframes);
3610 /* Find the frame requested. */
3611 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3612 backlist = backlist->next;
3614 if (!backlist)
3615 return Qnil;
3616 if (backlist->nargs == UNEVALLED)
3617 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3618 else
3620 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3621 tem = *backlist->args;
3622 else
3623 tem = Flist (backlist->nargs, backlist->args);
3625 return Fcons (Qt, Fcons (*backlist->function, tem));
3630 void
3631 mark_backtrace (void)
3633 register struct backtrace *backlist;
3634 register size_t i;
3636 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3638 mark_object (*backlist->function);
3640 if (backlist->nargs == UNEVALLED
3641 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3642 i = 1;
3643 else
3644 i = backlist->nargs;
3645 while (i--)
3646 mark_object (backlist->args[i]);
3650 EXFUN (Funintern, 2);
3652 void
3653 syms_of_eval (void)
3655 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3656 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3657 If Lisp code tries to increase the total number past this amount,
3658 an error is signaled.
3659 You can safely use a value considerably larger than the default value,
3660 if that proves inconveniently small. However, if you increase it too far,
3661 Emacs could run out of memory trying to make the stack bigger. */);
3663 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3664 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3666 This limit serves to catch infinite recursions for you before they cause
3667 actual stack overflow in C, which would be fatal for Emacs.
3668 You can safely make it considerably larger than its default value,
3669 if that proves inconveniently small. However, if you increase it too far,
3670 Emacs could overflow the real C stack, and crash. */);
3672 DEFVAR_LISP ("quit-flag", Vquit_flag,
3673 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3674 If the value is t, that means do an ordinary quit.
3675 If the value equals `throw-on-input', that means quit by throwing
3676 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3677 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3678 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3679 Vquit_flag = Qnil;
3681 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3682 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3683 Note that `quit-flag' will still be set by typing C-g,
3684 so a quit will be signaled as soon as `inhibit-quit' is nil.
3685 To prevent this happening, set `quit-flag' to nil
3686 before making `inhibit-quit' nil. */);
3687 Vinhibit_quit = Qnil;
3689 Qinhibit_quit = intern_c_string ("inhibit-quit");
3690 staticpro (&Qinhibit_quit);
3692 Qautoload = intern_c_string ("autoload");
3693 staticpro (&Qautoload);
3695 Qdebug_on_error = intern_c_string ("debug-on-error");
3696 staticpro (&Qdebug_on_error);
3698 Qmacro = intern_c_string ("macro");
3699 staticpro (&Qmacro);
3701 Qdeclare = intern_c_string ("declare");
3702 staticpro (&Qdeclare);
3704 /* Note that the process handling also uses Qexit, but we don't want
3705 to staticpro it twice, so we just do it here. */
3706 Qexit = intern_c_string ("exit");
3707 staticpro (&Qexit);
3709 Qinteractive = intern_c_string ("interactive");
3710 staticpro (&Qinteractive);
3712 Qcommandp = intern_c_string ("commandp");
3713 staticpro (&Qcommandp);
3715 Qdefun = intern_c_string ("defun");
3716 staticpro (&Qdefun);
3718 Qand_rest = intern_c_string ("&rest");
3719 staticpro (&Qand_rest);
3721 Qand_optional = intern_c_string ("&optional");
3722 staticpro (&Qand_optional);
3724 Qclosure = intern_c_string ("closure");
3725 staticpro (&Qclosure);
3727 Qdebug = intern_c_string ("debug");
3728 staticpro (&Qdebug);
3730 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3731 doc: /* *Non-nil means enter debugger if an error is signaled.
3732 Does not apply to errors handled by `condition-case' or those
3733 matched by `debug-ignored-errors'.
3734 If the value is a list, an error only means to enter the debugger
3735 if one of its condition symbols appears in the list.
3736 When you evaluate an expression interactively, this variable
3737 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3738 The command `toggle-debug-on-error' toggles this.
3739 See also the variable `debug-on-quit'. */);
3740 Vdebug_on_error = Qnil;
3742 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3743 doc: /* *List of errors for which the debugger should not be called.
3744 Each element may be a condition-name or a regexp that matches error messages.
3745 If any element applies to a given error, that error skips the debugger
3746 and just returns to top level.
3747 This overrides the variable `debug-on-error'.
3748 It does not apply to errors handled by `condition-case'. */);
3749 Vdebug_ignored_errors = Qnil;
3751 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3752 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3753 Does not apply if quit is handled by a `condition-case'. */);
3754 debug_on_quit = 0;
3756 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3757 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3759 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3760 doc: /* Non-nil means debugger may continue execution.
3761 This is nil when the debugger is called under circumstances where it
3762 might not be safe to continue. */);
3763 debugger_may_continue = 1;
3765 DEFVAR_LISP ("debugger", Vdebugger,
3766 doc: /* Function to call to invoke debugger.
3767 If due to frame exit, args are `exit' and the value being returned;
3768 this function's value will be returned instead of that.
3769 If due to error, args are `error' and a list of the args to `signal'.
3770 If due to `apply' or `funcall' entry, one arg, `lambda'.
3771 If due to `eval' entry, one arg, t. */);
3772 Vdebugger = Qnil;
3774 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3775 doc: /* If non-nil, this is a function for `signal' to call.
3776 It receives the same arguments that `signal' was given.
3777 The Edebug package uses this to regain control. */);
3778 Vsignal_hook_function = Qnil;
3780 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3781 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3782 Note that `debug-on-error', `debug-on-quit' and friends
3783 still determine whether to handle the particular condition. */);
3784 Vdebug_on_signal = Qnil;
3786 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3787 doc: /* Function to process declarations in a macro definition.
3788 The function will be called with two args MACRO and DECL.
3789 MACRO is the name of the macro being defined.
3790 DECL is a list `(declare ...)' containing the declarations.
3791 The value the function returns is not used. */);
3792 Vmacro_declaration_function = Qnil;
3794 /* When lexical binding is being used,
3795 vinternal_interpreter_environment is non-nil, and contains an alist
3796 of lexically-bound variable, or (t), indicating an empty
3797 environment. The lisp name of this variable would be
3798 `internal-interpreter-environment' if it weren't hidden.
3799 Every element of this list can be either a cons (VAR . VAL)
3800 specifying a lexical binding, or a single symbol VAR indicating
3801 that this variable should use dynamic scoping. */
3802 Qinternal_interpreter_environment
3803 = intern_c_string ("internal-interpreter-environment");
3804 staticpro (&Qinternal_interpreter_environment);
3805 DEFVAR_LISP ("internal-interpreter-environment",
3806 Vinternal_interpreter_environment,
3807 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3808 When lexical binding is not being used, this variable is nil.
3809 A value of `(t)' indicates an empty environment, otherwise it is an
3810 alist of active lexical bindings. */);
3811 Vinternal_interpreter_environment = Qnil;
3812 /* Don't export this variable to Elisp, so noone can mess with it
3813 (Just imagine if someone makes it buffer-local). */
3814 Funintern (Qinternal_interpreter_environment, Qnil);
3816 Vrun_hooks = intern_c_string ("run-hooks");
3817 staticpro (&Vrun_hooks);
3819 staticpro (&Vautoload_queue);
3820 Vautoload_queue = Qnil;
3821 staticpro (&Vsignaling_function);
3822 Vsignaling_function = Qnil;
3824 defsubr (&Sor);
3825 defsubr (&Sand);
3826 defsubr (&Sif);
3827 defsubr (&Scond);
3828 defsubr (&Sprogn);
3829 defsubr (&Sprog1);
3830 defsubr (&Sprog2);
3831 defsubr (&Ssetq);
3832 defsubr (&Squote);
3833 defsubr (&Sfunction);
3834 defsubr (&Sdefun);
3835 defsubr (&Sdefmacro);
3836 defsubr (&Sdefvar);
3837 defsubr (&Sdefvaralias);
3838 defsubr (&Sdefconst);
3839 defsubr (&Suser_variable_p);
3840 defsubr (&Slet);
3841 defsubr (&SletX);
3842 defsubr (&Swhile);
3843 defsubr (&Smacroexpand);
3844 defsubr (&Scatch);
3845 defsubr (&Sthrow);
3846 defsubr (&Sunwind_protect);
3847 defsubr (&Scondition_case);
3848 defsubr (&Ssignal);
3849 defsubr (&Sinteractive_p);
3850 defsubr (&Scalled_interactively_p);
3851 defsubr (&Scommandp);
3852 defsubr (&Sautoload);
3853 defsubr (&Seval);
3854 defsubr (&Sapply);
3855 defsubr (&Sfuncall);
3856 defsubr (&Srun_hooks);
3857 defsubr (&Srun_hook_with_args);
3858 defsubr (&Srun_hook_with_args_until_success);
3859 defsubr (&Srun_hook_with_args_until_failure);
3860 defsubr (&Srun_hook_wrapped);
3861 defsubr (&Sfetch_bytecode);
3862 defsubr (&Sbacktrace_debug);
3863 defsubr (&Sbacktrace);
3864 defsubr (&Sbacktrace_frame);
3865 defsubr (&Sspecial_variable_p);
3866 defsubr (&Sfunctionp);