Don't macro-inline non-performance-critical code.
[emacs.git] / src / eval.c
blob079c7ecb6c2306f6ddbc062c3bb27cde9cb1979b
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 <stdio.h>
24 #include "lisp.h"
25 #include "blockinput.h"
26 #include "commands.h"
27 #include "keyboard.h"
28 #include "dispextern.h"
29 #include "frame.h" /* For XFRAME. */
31 #if HAVE_X_WINDOWS
32 #include "xterm.h"
33 #endif
35 struct backtrace
37 struct backtrace *next;
38 Lisp_Object *function;
39 Lisp_Object *args; /* Points to vector of args. */
40 ptrdiff_t nargs; /* Length of vector. */
41 /* Nonzero means call value of debugger when done with this operation. */
42 unsigned int debug_on_exit : 1;
45 static struct backtrace *backtrace_list;
47 #if !BYTE_MARK_STACK
48 static
49 #endif
50 struct catchtag *catchlist;
52 /* Chain of condition handlers currently in effect.
53 The elements of this chain are contained in the stack frames
54 of Fcondition_case and internal_condition_case.
55 When an error is signaled (by calling Fsignal, below),
56 this chain is searched for an element that applies. */
58 #if !BYTE_MARK_STACK
59 static
60 #endif
61 struct handler *handlerlist;
63 #ifdef DEBUG_GCPRO
64 /* Count levels of GCPRO to detect failure to UNGCPRO. */
65 int gcpro_level;
66 #endif
68 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
69 Lisp_Object Qinhibit_quit;
70 Lisp_Object Qand_rest;
71 static Lisp_Object Qand_optional;
72 static Lisp_Object Qdebug_on_error;
73 static Lisp_Object Qdeclare;
74 Lisp_Object Qinternal_interpreter_environment, Qclosure;
76 static Lisp_Object Qdebug;
78 /* This holds either the symbol `run-hooks' or nil.
79 It is nil at an early stage of startup, and when Emacs
80 is shutting down. */
82 Lisp_Object Vrun_hooks;
84 /* Non-nil means record all fset's and provide's, to be undone
85 if the file being autoloaded is not fully loaded.
86 They are recorded by being consed onto the front of Vautoload_queue:
87 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
89 Lisp_Object Vautoload_queue;
91 /* Current number of specbindings allocated in specpdl. */
93 EMACS_INT specpdl_size;
95 /* Pointer to beginning of specpdl. */
97 struct specbinding *specpdl;
99 /* Pointer to first unused element in specpdl. */
101 struct specbinding *specpdl_ptr;
103 /* Depth in Lisp evaluations and function calls. */
105 static EMACS_INT lisp_eval_depth;
107 /* The value of num_nonmacro_input_events as of the last time we
108 started to enter the debugger. If we decide to enter the debugger
109 again when this is still equal to num_nonmacro_input_events, then we
110 know that the debugger itself has an error, and we should just
111 signal the error instead of entering an infinite loop of debugger
112 invocations. */
114 static int when_entered_debugger;
116 /* The function from which the last `signal' was called. Set in
117 Fsignal. */
119 Lisp_Object Vsignaling_function;
121 /* Set to non-zero while processing X events. Checked in Feval to
122 make sure the Lisp interpreter isn't called from a signal handler,
123 which is unsafe because the interpreter isn't reentrant. */
125 int handling_signal;
127 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
128 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
129 static int interactive_p (int);
130 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
131 static Lisp_Object Ffetch_bytecode (Lisp_Object);
133 void
134 init_eval_once (void)
136 enum { size = 50 };
137 specpdl = (struct specbinding *) xmalloc (size * sizeof (struct specbinding));
138 specpdl_size = size;
139 specpdl_ptr = specpdl;
140 /* Don't forget to update docs (lispref node "Local Variables"). */
141 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
142 max_lisp_eval_depth = 600;
144 Vrun_hooks = Qnil;
147 void
148 init_eval (void)
150 specpdl_ptr = specpdl;
151 catchlist = 0;
152 handlerlist = 0;
153 backtrace_list = 0;
154 Vquit_flag = Qnil;
155 debug_on_next_call = 0;
156 lisp_eval_depth = 0;
157 #ifdef DEBUG_GCPRO
158 gcpro_level = 0;
159 #endif
160 /* This is less than the initial value of num_nonmacro_input_events. */
161 when_entered_debugger = -1;
164 /* Unwind-protect function used by call_debugger. */
166 static Lisp_Object
167 restore_stack_limits (Lisp_Object data)
169 max_specpdl_size = XINT (XCAR (data));
170 max_lisp_eval_depth = XINT (XCDR (data));
171 return Qnil;
174 /* Call the Lisp debugger, giving it argument ARG. */
176 static Lisp_Object
177 call_debugger (Lisp_Object arg)
179 int debug_while_redisplaying;
180 int count = SPECPDL_INDEX ();
181 Lisp_Object val;
182 EMACS_INT old_max = max_specpdl_size;
184 /* Temporarily bump up the stack limits,
185 so the debugger won't run out of stack. */
187 max_specpdl_size += 1;
188 record_unwind_protect (restore_stack_limits,
189 Fcons (make_number (old_max),
190 make_number (max_lisp_eval_depth)));
191 max_specpdl_size = old_max;
193 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
194 max_lisp_eval_depth = lisp_eval_depth + 40;
196 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
197 max_specpdl_size = SPECPDL_INDEX () + 100;
199 #ifdef HAVE_WINDOW_SYSTEM
200 if (display_hourglass_p)
201 cancel_hourglass ();
202 #endif
204 debug_on_next_call = 0;
205 when_entered_debugger = num_nonmacro_input_events;
207 /* Resetting redisplaying_p to 0 makes sure that debug output is
208 displayed if the debugger is invoked during redisplay. */
209 debug_while_redisplaying = redisplaying_p;
210 redisplaying_p = 0;
211 specbind (intern ("debugger-may-continue"),
212 debug_while_redisplaying ? Qnil : Qt);
213 specbind (Qinhibit_redisplay, Qnil);
214 specbind (Qdebug_on_error, Qnil);
216 #if 0 /* Binding this prevents execution of Lisp code during
217 redisplay, which necessarily leads to display problems. */
218 specbind (Qinhibit_eval_during_redisplay, Qt);
219 #endif
221 val = apply1 (Vdebugger, arg);
223 /* Interrupting redisplay and resuming it later is not safe under
224 all circumstances. So, when the debugger returns, abort the
225 interrupted redisplay by going back to the top-level. */
226 if (debug_while_redisplaying)
227 Ftop_level ();
229 return unbind_to (count, val);
232 static void
233 do_debug_on_call (Lisp_Object code)
235 debug_on_next_call = 0;
236 backtrace_list->debug_on_exit = 1;
237 call_debugger (Fcons (code, Qnil));
240 /* NOTE!!! Every function that can call EVAL must protect its args
241 and temporaries from garbage collection while it needs them.
242 The definition of `For' shows what you have to do. */
244 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
245 doc: /* Eval args until one of them yields non-nil, then return that value.
246 The remaining args are not evalled at all.
247 If all args return nil, return nil.
248 usage: (or CONDITIONS...) */)
249 (Lisp_Object args)
251 register Lisp_Object val = Qnil;
252 struct gcpro gcpro1;
254 GCPRO1 (args);
256 while (CONSP (args))
258 val = eval_sub (XCAR (args));
259 if (!NILP (val))
260 break;
261 args = XCDR (args);
264 UNGCPRO;
265 return val;
268 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
269 doc: /* Eval args until one of them yields nil, then return nil.
270 The remaining args are not evalled at all.
271 If no arg yields nil, return the last arg's value.
272 usage: (and CONDITIONS...) */)
273 (Lisp_Object args)
275 register Lisp_Object val = Qt;
276 struct gcpro gcpro1;
278 GCPRO1 (args);
280 while (CONSP (args))
282 val = eval_sub (XCAR (args));
283 if (NILP (val))
284 break;
285 args = XCDR (args);
288 UNGCPRO;
289 return val;
292 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
293 doc: /* If COND yields non-nil, do THEN, else do ELSE...
294 Returns the value of THEN or the value of the last of the ELSE's.
295 THEN must be one expression, but ELSE... can be zero or more expressions.
296 If COND yields nil, and there are no ELSE's, the value is nil.
297 usage: (if COND THEN ELSE...) */)
298 (Lisp_Object args)
300 register Lisp_Object cond;
301 struct gcpro gcpro1;
303 GCPRO1 (args);
304 cond = eval_sub (Fcar (args));
305 UNGCPRO;
307 if (!NILP (cond))
308 return eval_sub (Fcar (Fcdr (args)));
309 return Fprogn (Fcdr (Fcdr (args)));
312 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
313 doc: /* Try each clause until one succeeds.
314 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
315 and, if the value is non-nil, this clause succeeds:
316 then the expressions in BODY are evaluated and the last one's
317 value is the value of the cond-form.
318 If no clause succeeds, cond returns nil.
319 If a clause has one element, as in (CONDITION),
320 CONDITION's value if non-nil is returned from the cond-form.
321 usage: (cond CLAUSES...) */)
322 (Lisp_Object args)
324 register Lisp_Object clause, val;
325 struct gcpro gcpro1;
327 val = Qnil;
328 GCPRO1 (args);
329 while (!NILP (args))
331 clause = Fcar (args);
332 val = eval_sub (Fcar (clause));
333 if (!NILP (val))
335 if (!EQ (XCDR (clause), Qnil))
336 val = Fprogn (XCDR (clause));
337 break;
339 args = XCDR (args);
341 UNGCPRO;
343 return val;
346 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
347 doc: /* Eval BODY forms sequentially and return value of last one.
348 usage: (progn BODY...) */)
349 (Lisp_Object args)
351 register Lisp_Object val = Qnil;
352 struct gcpro gcpro1;
354 GCPRO1 (args);
356 while (CONSP (args))
358 val = eval_sub (XCAR (args));
359 args = XCDR (args);
362 UNGCPRO;
363 return val;
366 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
367 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
368 The value of FIRST is saved during the evaluation of the remaining args,
369 whose values are discarded.
370 usage: (prog1 FIRST BODY...) */)
371 (Lisp_Object args)
373 Lisp_Object val;
374 register Lisp_Object args_left;
375 struct gcpro gcpro1, gcpro2;
376 register int argnum = 0;
378 if (NILP (args))
379 return Qnil;
381 args_left = args;
382 val = Qnil;
383 GCPRO2 (args, val);
387 Lisp_Object tem = eval_sub (XCAR (args_left));
388 if (!(argnum++))
389 val = tem;
390 args_left = XCDR (args_left);
392 while (CONSP (args_left));
394 UNGCPRO;
395 return val;
398 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
399 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
400 The value of FORM2 is saved during the evaluation of the
401 remaining args, whose values are discarded.
402 usage: (prog2 FORM1 FORM2 BODY...) */)
403 (Lisp_Object args)
405 Lisp_Object val;
406 register Lisp_Object args_left;
407 struct gcpro gcpro1, gcpro2;
408 register int argnum = -1;
410 val = Qnil;
412 if (NILP (args))
413 return Qnil;
415 args_left = args;
416 val = Qnil;
417 GCPRO2 (args, val);
421 Lisp_Object tem = eval_sub (XCAR (args_left));
422 if (!(argnum++))
423 val = tem;
424 args_left = XCDR (args_left);
426 while (CONSP (args_left));
428 UNGCPRO;
429 return val;
432 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
433 doc: /* Set each SYM to the value of its VAL.
434 The symbols SYM are variables; they are literal (not evaluated).
435 The values VAL are expressions; they are evaluated.
436 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
437 The second VAL is not computed until after the first SYM is set, and so on;
438 each VAL can use the new value of variables set earlier in the `setq'.
439 The return value of the `setq' form is the value of the last VAL.
440 usage: (setq [SYM VAL]...) */)
441 (Lisp_Object args)
443 register Lisp_Object args_left;
444 register Lisp_Object val, sym, lex_binding;
445 struct gcpro gcpro1;
447 if (NILP (args))
448 return Qnil;
450 args_left = args;
451 GCPRO1 (args);
455 val = eval_sub (Fcar (Fcdr (args_left)));
456 sym = Fcar (args_left);
458 /* Like for eval_sub, we do not check declared_special here since
459 it's been done when let-binding. */
460 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
461 && SYMBOLP (sym)
462 && !NILP (lex_binding
463 = Fassq (sym, Vinternal_interpreter_environment)))
464 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
465 else
466 Fset (sym, val); /* SYM is dynamically bound. */
468 args_left = Fcdr (Fcdr (args_left));
470 while (!NILP (args_left));
472 UNGCPRO;
473 return val;
476 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
477 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
478 Warning: `quote' does not construct its return value, but just returns
479 the value that was pre-constructed by the Lisp reader (see info node
480 `(elisp)Printed Representation').
481 This means that '(a . b) is not identical to (cons 'a 'b): the former
482 does not cons. Quoting should be reserved for constants that will
483 never be modified by side-effects, unless you like self-modifying code.
484 See the common pitfall in info node `(elisp)Rearrangement' for an example
485 of unexpected results when a quoted object is modified.
486 usage: (quote ARG) */)
487 (Lisp_Object args)
489 if (!NILP (Fcdr (args)))
490 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
491 return Fcar (args);
494 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
495 doc: /* Like `quote', but preferred for objects which are functions.
496 In byte compilation, `function' causes its argument to be compiled.
497 `quote' cannot do that.
498 usage: (function ARG) */)
499 (Lisp_Object args)
501 Lisp_Object quoted = XCAR (args);
503 if (!NILP (Fcdr (args)))
504 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
506 if (!NILP (Vinternal_interpreter_environment)
507 && CONSP (quoted)
508 && EQ (XCAR (quoted), Qlambda))
509 /* This is a lambda expression within a lexical environment;
510 return an interpreted closure instead of a simple lambda. */
511 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
512 XCDR (quoted)));
513 else
514 /* Simply quote the argument. */
515 return quoted;
519 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
520 doc: /* Return t if the containing function was run directly by user input.
521 This means that the function was called with `call-interactively'
522 \(which includes being called as the binding of a key)
523 and input is currently coming from the keyboard (not a keyboard macro),
524 and Emacs is not running in batch mode (`noninteractive' is nil).
526 The only known proper use of `interactive-p' is in deciding whether to
527 display a helpful message, or how to display it. If you're thinking
528 of using it for any other purpose, it is quite likely that you're
529 making a mistake. Think: what do you want to do when the command is
530 called from a keyboard macro?
532 To test whether your function was called with `call-interactively',
533 either (i) add an extra optional argument and give it an `interactive'
534 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
535 use `called-interactively-p'. */)
536 (void)
538 return interactive_p (1) ? Qt : Qnil;
542 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
543 doc: /* Return t if the containing function was called by `call-interactively'.
544 If KIND is `interactive', then only return t if the call was made
545 interactively by the user, i.e. not in `noninteractive' mode nor
546 when `executing-kbd-macro'.
547 If KIND is `any', on the other hand, it will return t for any kind of
548 interactive call, including being called as the binding of a key, or
549 from a keyboard macro, or in `noninteractive' mode.
551 The only known proper use of `interactive' for KIND is in deciding
552 whether to display a helpful message, or how to display it. If you're
553 thinking of using it for any other purpose, it is quite likely that
554 you're making a mistake. Think: what do you want to do when the
555 command is called from a keyboard macro?
557 This function is meant for implementing advice and other
558 function-modifying features. Instead of using this, it is sometimes
559 cleaner to give your function an extra optional argument whose
560 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
561 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
562 (Lisp_Object kind)
564 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
565 && interactive_p (1)) ? Qt : Qnil;
569 /* Return 1 if function in which this appears was called using
570 call-interactively.
572 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
573 called is a built-in. */
575 static int
576 interactive_p (int exclude_subrs_p)
578 struct backtrace *btp;
579 Lisp_Object fun;
581 btp = backtrace_list;
583 /* If this isn't a byte-compiled function, there may be a frame at
584 the top for Finteractive_p. If so, skip it. */
585 fun = Findirect_function (*btp->function, Qnil);
586 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
587 || XSUBR (fun) == &Scalled_interactively_p))
588 btp = btp->next;
590 /* If we're running an Emacs 18-style byte-compiled function, there
591 may be a frame for Fbytecode at the top level. In any version of
592 Emacs there can be Fbytecode frames for subexpressions evaluated
593 inside catch and condition-case. Skip past them.
595 If this isn't a byte-compiled function, then we may now be
596 looking at several frames for special forms. Skip past them. */
597 while (btp
598 && (EQ (*btp->function, Qbytecode)
599 || btp->nargs == UNEVALLED))
600 btp = btp->next;
602 /* `btp' now points at the frame of the innermost function that isn't
603 a special form, ignoring frames for Finteractive_p and/or
604 Fbytecode at the top. If this frame is for a built-in function
605 (such as load or eval-region) return nil. */
606 fun = Findirect_function (*btp->function, Qnil);
607 if (exclude_subrs_p && SUBRP (fun))
608 return 0;
610 /* `btp' points to the frame of a Lisp function that called interactive-p.
611 Return t if that function was called interactively. */
612 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
613 return 1;
614 return 0;
618 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
619 doc: /* Define NAME as a function.
620 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
621 See also the function `interactive'.
622 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
623 (Lisp_Object args)
625 register Lisp_Object fn_name;
626 register Lisp_Object defn;
628 fn_name = Fcar (args);
629 CHECK_SYMBOL (fn_name);
630 defn = Fcons (Qlambda, Fcdr (args));
631 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
632 defn = Ffunction (Fcons (defn, Qnil));
633 if (!NILP (Vpurify_flag))
634 defn = Fpurecopy (defn);
635 if (CONSP (XSYMBOL (fn_name)->function)
636 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
637 LOADHIST_ATTACH (Fcons (Qt, fn_name));
638 Ffset (fn_name, defn);
639 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
640 return fn_name;
643 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
644 doc: /* Define NAME as a macro.
645 The actual definition looks like
646 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
647 When the macro is called, as in (NAME ARGS...),
648 the function (lambda ARGLIST BODY...) is applied to
649 the list ARGS... as it appears in the expression,
650 and the result should be a form to be evaluated instead of the original.
652 DECL is a declaration, optional, which can specify how to indent
653 calls to this macro, how Edebug should handle it, and which argument
654 should be treated as documentation. It looks like this:
655 (declare SPECS...)
656 The elements can look like this:
657 (indent INDENT)
658 Set NAME's `lisp-indent-function' property to INDENT.
660 (debug DEBUG)
661 Set NAME's `edebug-form-spec' property to DEBUG. (This is
662 equivalent to writing a `def-edebug-spec' for the macro.)
664 (doc-string ELT)
665 Set NAME's `doc-string-elt' property to ELT.
667 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
668 (Lisp_Object args)
670 register Lisp_Object fn_name;
671 register Lisp_Object defn;
672 Lisp_Object lambda_list, doc, tail;
674 fn_name = Fcar (args);
675 CHECK_SYMBOL (fn_name);
676 lambda_list = Fcar (Fcdr (args));
677 tail = Fcdr (Fcdr (args));
679 doc = Qnil;
680 if (STRINGP (Fcar (tail)))
682 doc = XCAR (tail);
683 tail = XCDR (tail);
686 if (CONSP (Fcar (tail))
687 && EQ (Fcar (Fcar (tail)), Qdeclare))
689 if (!NILP (Vmacro_declaration_function))
691 struct gcpro gcpro1;
692 GCPRO1 (args);
693 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
694 UNGCPRO;
697 tail = Fcdr (tail);
700 if (NILP (doc))
701 tail = Fcons (lambda_list, tail);
702 else
703 tail = Fcons (lambda_list, Fcons (doc, tail));
705 defn = Fcons (Qlambda, tail);
706 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
707 defn = Ffunction (Fcons (defn, Qnil));
708 defn = Fcons (Qmacro, defn);
710 if (!NILP (Vpurify_flag))
711 defn = Fpurecopy (defn);
712 if (CONSP (XSYMBOL (fn_name)->function)
713 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
714 LOADHIST_ATTACH (Fcons (Qt, fn_name));
715 Ffset (fn_name, defn);
716 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
717 return fn_name;
721 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
722 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
723 Aliased variables always have the same value; setting one sets the other.
724 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
725 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
726 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
727 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
728 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
729 The return value is BASE-VARIABLE. */)
730 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
732 struct Lisp_Symbol *sym;
734 CHECK_SYMBOL (new_alias);
735 CHECK_SYMBOL (base_variable);
737 sym = XSYMBOL (new_alias);
739 if (sym->constant)
740 /* Not sure why, but why not? */
741 error ("Cannot make a constant an alias");
743 switch (sym->redirect)
745 case SYMBOL_FORWARDED:
746 error ("Cannot make an internal variable an alias");
747 case SYMBOL_LOCALIZED:
748 error ("Don't know how to make a localized variable an alias");
751 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
752 If n_a is bound, but b_v is not, set the value of b_v to n_a,
753 so that old-code that affects n_a before the aliasing is setup
754 still works. */
755 if (NILP (Fboundp (base_variable)))
756 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
759 struct specbinding *p;
761 for (p = specpdl_ptr - 1; p >= specpdl; p--)
762 if (p->func == NULL
763 && (EQ (new_alias,
764 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
765 error ("Don't know how to make a let-bound variable an alias");
768 sym->declared_special = 1;
769 XSYMBOL (base_variable)->declared_special = 1;
770 sym->redirect = SYMBOL_VARALIAS;
771 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
772 sym->constant = SYMBOL_CONSTANT_P (base_variable);
773 LOADHIST_ATTACH (new_alias);
774 /* Even if docstring is nil: remove old docstring. */
775 Fput (new_alias, Qvariable_documentation, docstring);
777 return base_variable;
781 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
782 doc: /* Define SYMBOL as a variable, and return SYMBOL.
783 You are not required to define a variable in order to use it,
784 but the definition can supply documentation and an initial value
785 in a way that tags can recognize.
787 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.
788 If SYMBOL is buffer-local, its default value is what is set;
789 buffer-local values are not affected.
790 INITVALUE and DOCSTRING are optional.
791 If DOCSTRING starts with *, this variable is identified as a user option.
792 This means that M-x set-variable recognizes it.
793 See also `user-variable-p'.
794 If INITVALUE is missing, SYMBOL's value is not set.
796 If SYMBOL has a local binding, then this form affects the local
797 binding. This is usually not what you want. Thus, if you need to
798 load a file defining variables, with this form or with `defconst' or
799 `defcustom', you should always load that file _outside_ any bindings
800 for these variables. \(`defconst' and `defcustom' behave similarly in
801 this respect.)
802 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
803 (Lisp_Object args)
805 register Lisp_Object sym, tem, tail;
807 sym = Fcar (args);
808 tail = Fcdr (args);
809 if (!NILP (Fcdr (Fcdr (tail))))
810 error ("Too many arguments");
812 tem = Fdefault_boundp (sym);
813 if (!NILP (tail))
815 /* Do it before evaluating the initial value, for self-references. */
816 XSYMBOL (sym)->declared_special = 1;
818 if (SYMBOL_CONSTANT_P (sym))
820 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
821 Lisp_Object tem1 = Fcar (tail);
822 if (! (CONSP (tem1)
823 && EQ (XCAR (tem1), Qquote)
824 && CONSP (XCDR (tem1))
825 && EQ (XCAR (XCDR (tem1)), sym)))
826 error ("Constant symbol `%s' specified in defvar",
827 SDATA (SYMBOL_NAME (sym)));
830 if (NILP (tem))
831 Fset_default (sym, eval_sub (Fcar (tail)));
832 else
833 { /* Check if there is really a global binding rather than just a let
834 binding that shadows the global unboundness of the var. */
835 volatile struct specbinding *pdl = specpdl_ptr;
836 while (--pdl >= specpdl)
838 if (EQ (pdl->symbol, sym) && !pdl->func
839 && EQ (pdl->old_value, Qunbound))
841 message_with_string ("Warning: defvar ignored because %s is let-bound",
842 SYMBOL_NAME (sym), 1);
843 break;
847 tail = Fcdr (tail);
848 tem = Fcar (tail);
849 if (!NILP (tem))
851 if (!NILP (Vpurify_flag))
852 tem = Fpurecopy (tem);
853 Fput (sym, Qvariable_documentation, tem);
855 LOADHIST_ATTACH (sym);
857 else if (!NILP (Vinternal_interpreter_environment)
858 && !XSYMBOL (sym)->declared_special)
859 /* A simple (defvar foo) with lexical scoping does "nothing" except
860 declare that var to be dynamically scoped *locally* (i.e. within
861 the current file or let-block). */
862 Vinternal_interpreter_environment =
863 Fcons (sym, Vinternal_interpreter_environment);
864 else
866 /* Simple (defvar <var>) should not count as a definition at all.
867 It could get in the way of other definitions, and unloading this
868 package could try to make the variable unbound. */
871 return sym;
874 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
875 doc: /* Define SYMBOL as a constant variable.
876 The intent is that neither programs nor users should ever change this value.
877 Always sets the value of SYMBOL to the result of evalling INITVALUE.
878 If SYMBOL is buffer-local, its default value is what is set;
879 buffer-local values are not affected.
880 DOCSTRING is optional.
882 If SYMBOL has a local binding, then this form sets the local binding's
883 value. However, you should normally not make local bindings for
884 variables defined with this form.
885 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
886 (Lisp_Object args)
888 register Lisp_Object sym, tem;
890 sym = Fcar (args);
891 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
892 error ("Too many arguments");
894 tem = eval_sub (Fcar (Fcdr (args)));
895 if (!NILP (Vpurify_flag))
896 tem = Fpurecopy (tem);
897 Fset_default (sym, tem);
898 XSYMBOL (sym)->declared_special = 1;
899 tem = Fcar (Fcdr (Fcdr (args)));
900 if (!NILP (tem))
902 if (!NILP (Vpurify_flag))
903 tem = Fpurecopy (tem);
904 Fput (sym, Qvariable_documentation, tem);
906 Fput (sym, Qrisky_local_variable, Qt);
907 LOADHIST_ATTACH (sym);
908 return sym;
911 /* Error handler used in Fuser_variable_p. */
912 static Lisp_Object
913 user_variable_p_eh (Lisp_Object ignore)
915 return Qnil;
918 static Lisp_Object
919 lisp_indirect_variable (Lisp_Object sym)
921 struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
922 XSETSYMBOL (sym, s);
923 return sym;
926 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
927 doc: /* Return t if VARIABLE is intended to be set and modified by users.
928 \(The alternative is a variable used internally in a Lisp program.)
929 A variable is a user variable if
930 \(1) the first character of its documentation is `*', or
931 \(2) it is customizable (its property list contains a non-nil value
932 of `standard-value' or `custom-autoload'), or
933 \(3) it is an alias for another user variable.
934 Return nil if VARIABLE is an alias and there is a loop in the
935 chain of symbols. */)
936 (Lisp_Object variable)
938 Lisp_Object documentation;
940 if (!SYMBOLP (variable))
941 return Qnil;
943 /* If indirect and there's an alias loop, don't check anything else. */
944 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
945 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
946 Qt, user_variable_p_eh)))
947 return Qnil;
949 while (1)
951 documentation = Fget (variable, Qvariable_documentation);
952 if (INTEGERP (documentation) && XINT (documentation) < 0)
953 return Qt;
954 if (STRINGP (documentation)
955 && ((unsigned char) SREF (documentation, 0) == '*'))
956 return Qt;
957 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
958 if (CONSP (documentation)
959 && STRINGP (XCAR (documentation))
960 && INTEGERP (XCDR (documentation))
961 && XINT (XCDR (documentation)) < 0)
962 return Qt;
963 /* Customizable? See `custom-variable-p'. */
964 if ((!NILP (Fget (variable, intern ("standard-value"))))
965 || (!NILP (Fget (variable, intern ("custom-autoload")))))
966 return Qt;
968 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
969 return Qnil;
971 /* An indirect variable? Let's follow the chain. */
972 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
976 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
977 doc: /* Bind variables according to VARLIST then eval BODY.
978 The value of the last form in BODY is returned.
979 Each element of VARLIST is a symbol (which is bound to nil)
980 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
981 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
982 usage: (let* VARLIST BODY...) */)
983 (Lisp_Object args)
985 Lisp_Object varlist, var, val, elt, lexenv;
986 int count = SPECPDL_INDEX ();
987 struct gcpro gcpro1, gcpro2, gcpro3;
989 GCPRO3 (args, elt, varlist);
991 lexenv = Vinternal_interpreter_environment;
993 varlist = Fcar (args);
994 while (CONSP (varlist))
996 QUIT;
998 elt = XCAR (varlist);
999 if (SYMBOLP (elt))
1001 var = elt;
1002 val = Qnil;
1004 else if (! NILP (Fcdr (Fcdr (elt))))
1005 signal_error ("`let' bindings can have only one value-form", elt);
1006 else
1008 var = Fcar (elt);
1009 val = eval_sub (Fcar (Fcdr (elt)));
1012 if (!NILP (lexenv) && SYMBOLP (var)
1013 && !XSYMBOL (var)->declared_special
1014 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1015 /* Lexically bind VAR by adding it to the interpreter's binding
1016 alist. */
1018 Lisp_Object newenv
1019 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1020 if (EQ (Vinternal_interpreter_environment, lexenv))
1021 /* Save the old lexical environment on the specpdl stack,
1022 but only for the first lexical binding, since we'll never
1023 need to revert to one of the intermediate ones. */
1024 specbind (Qinternal_interpreter_environment, newenv);
1025 else
1026 Vinternal_interpreter_environment = newenv;
1028 else
1029 specbind (var, val);
1031 varlist = XCDR (varlist);
1033 UNGCPRO;
1034 val = Fprogn (Fcdr (args));
1035 return unbind_to (count, val);
1038 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1039 doc: /* Bind variables according to VARLIST then eval BODY.
1040 The value of the last form in BODY is returned.
1041 Each element of VARLIST is a symbol (which is bound to nil)
1042 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1043 All the VALUEFORMs are evalled before any symbols are bound.
1044 usage: (let VARLIST BODY...) */)
1045 (Lisp_Object args)
1047 Lisp_Object *temps, tem, lexenv;
1048 register Lisp_Object elt, varlist;
1049 int count = SPECPDL_INDEX ();
1050 ptrdiff_t argnum;
1051 struct gcpro gcpro1, gcpro2;
1052 USE_SAFE_ALLOCA;
1054 varlist = Fcar (args);
1056 /* Make space to hold the values to give the bound variables. */
1057 elt = Flength (varlist);
1058 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1060 /* Compute the values and store them in `temps'. */
1062 GCPRO2 (args, *temps);
1063 gcpro2.nvars = 0;
1065 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1067 QUIT;
1068 elt = XCAR (varlist);
1069 if (SYMBOLP (elt))
1070 temps [argnum++] = Qnil;
1071 else if (! NILP (Fcdr (Fcdr (elt))))
1072 signal_error ("`let' bindings can have only one value-form", elt);
1073 else
1074 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1075 gcpro2.nvars = argnum;
1077 UNGCPRO;
1079 lexenv = Vinternal_interpreter_environment;
1081 varlist = Fcar (args);
1082 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1084 Lisp_Object var;
1086 elt = XCAR (varlist);
1087 var = SYMBOLP (elt) ? elt : Fcar (elt);
1088 tem = temps[argnum++];
1090 if (!NILP (lexenv) && SYMBOLP (var)
1091 && !XSYMBOL (var)->declared_special
1092 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1093 /* Lexically bind VAR by adding it to the lexenv alist. */
1094 lexenv = Fcons (Fcons (var, tem), lexenv);
1095 else
1096 /* Dynamically bind VAR. */
1097 specbind (var, tem);
1100 if (!EQ (lexenv, Vinternal_interpreter_environment))
1101 /* Instantiate a new lexical environment. */
1102 specbind (Qinternal_interpreter_environment, lexenv);
1104 elt = Fprogn (Fcdr (args));
1105 SAFE_FREE ();
1106 return unbind_to (count, elt);
1109 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1110 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1111 The order of execution is thus TEST, BODY, TEST, BODY and so on
1112 until TEST returns nil.
1113 usage: (while TEST BODY...) */)
1114 (Lisp_Object args)
1116 Lisp_Object test, body;
1117 struct gcpro gcpro1, gcpro2;
1119 GCPRO2 (test, body);
1121 test = Fcar (args);
1122 body = Fcdr (args);
1123 while (!NILP (eval_sub (test)))
1125 QUIT;
1126 Fprogn (body);
1129 UNGCPRO;
1130 return Qnil;
1133 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1134 doc: /* Return result of expanding macros at top level of FORM.
1135 If FORM is not a macro call, it is returned unchanged.
1136 Otherwise, the macro is expanded and the expansion is considered
1137 in place of FORM. When a non-macro-call results, it is returned.
1139 The second optional arg ENVIRONMENT specifies an environment of macro
1140 definitions to shadow the loaded ones for use in file byte-compilation. */)
1141 (Lisp_Object form, Lisp_Object environment)
1143 /* With cleanups from Hallvard Furuseth. */
1144 register Lisp_Object expander, sym, def, tem;
1146 while (1)
1148 /* Come back here each time we expand a macro call,
1149 in case it expands into another macro call. */
1150 if (!CONSP (form))
1151 break;
1152 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1153 def = sym = XCAR (form);
1154 tem = Qnil;
1155 /* Trace symbols aliases to other symbols
1156 until we get a symbol that is not an alias. */
1157 while (SYMBOLP (def))
1159 QUIT;
1160 sym = def;
1161 tem = Fassq (sym, environment);
1162 if (NILP (tem))
1164 def = XSYMBOL (sym)->function;
1165 if (!EQ (def, Qunbound))
1166 continue;
1168 break;
1170 /* Right now TEM is the result from SYM in ENVIRONMENT,
1171 and if TEM is nil then DEF is SYM's function definition. */
1172 if (NILP (tem))
1174 /* SYM is not mentioned in ENVIRONMENT.
1175 Look at its function definition. */
1176 if (EQ (def, Qunbound) || !CONSP (def))
1177 /* Not defined or definition not suitable. */
1178 break;
1179 if (EQ (XCAR (def), Qautoload))
1181 /* Autoloading function: will it be a macro when loaded? */
1182 tem = Fnth (make_number (4), def);
1183 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1184 /* Yes, load it and try again. */
1186 struct gcpro gcpro1;
1187 GCPRO1 (form);
1188 do_autoload (def, sym);
1189 UNGCPRO;
1190 continue;
1192 else
1193 break;
1195 else if (!EQ (XCAR (def), Qmacro))
1196 break;
1197 else expander = XCDR (def);
1199 else
1201 expander = XCDR (tem);
1202 if (NILP (expander))
1203 break;
1205 form = apply1 (expander, XCDR (form));
1207 return form;
1210 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1211 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1212 TAG is evalled to get the tag to use; it must not be nil.
1214 Then the BODY is executed.
1215 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1216 If no throw happens, `catch' returns the value of the last BODY form.
1217 If a throw happens, it specifies the value to return from `catch'.
1218 usage: (catch TAG BODY...) */)
1219 (Lisp_Object args)
1221 register Lisp_Object tag;
1222 struct gcpro gcpro1;
1224 GCPRO1 (args);
1225 tag = eval_sub (Fcar (args));
1226 UNGCPRO;
1227 return internal_catch (tag, Fprogn, Fcdr (args));
1230 /* Set up a catch, then call C function FUNC on argument ARG.
1231 FUNC should return a Lisp_Object.
1232 This is how catches are done from within C code. */
1234 Lisp_Object
1235 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1237 /* This structure is made part of the chain `catchlist'. */
1238 struct catchtag c;
1240 /* Fill in the components of c, and put it on the list. */
1241 c.next = catchlist;
1242 c.tag = tag;
1243 c.val = Qnil;
1244 c.backlist = backtrace_list;
1245 c.handlerlist = handlerlist;
1246 c.lisp_eval_depth = lisp_eval_depth;
1247 c.pdlcount = SPECPDL_INDEX ();
1248 c.poll_suppress_count = poll_suppress_count;
1249 c.interrupt_input_blocked = interrupt_input_blocked;
1250 c.gcpro = gcprolist;
1251 c.byte_stack = byte_stack_list;
1252 catchlist = &c;
1254 /* Call FUNC. */
1255 if (! _setjmp (c.jmp))
1256 c.val = (*func) (arg);
1258 /* Throw works by a longjmp that comes right here. */
1259 catchlist = c.next;
1260 return c.val;
1263 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1264 jump to that CATCH, returning VALUE as the value of that catch.
1266 This is the guts Fthrow and Fsignal; they differ only in the way
1267 they choose the catch tag to throw to. A catch tag for a
1268 condition-case form has a TAG of Qnil.
1270 Before each catch is discarded, unbind all special bindings and
1271 execute all unwind-protect clauses made above that catch. Unwind
1272 the handler stack as we go, so that the proper handlers are in
1273 effect for each unwind-protect clause we run. At the end, restore
1274 some static info saved in CATCH, and longjmp to the location
1275 specified in the
1277 This is used for correct unwinding in Fthrow and Fsignal. */
1279 static void
1280 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1282 register int last_time;
1284 /* Save the value in the tag. */
1285 catch->val = value;
1287 /* Restore certain special C variables. */
1288 set_poll_suppress_count (catch->poll_suppress_count);
1289 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1290 handling_signal = 0;
1291 immediate_quit = 0;
1295 last_time = catchlist == catch;
1297 /* Unwind the specpdl stack, and then restore the proper set of
1298 handlers. */
1299 unbind_to (catchlist->pdlcount, Qnil);
1300 handlerlist = catchlist->handlerlist;
1301 catchlist = catchlist->next;
1303 while (! last_time);
1305 #if HAVE_X_WINDOWS
1306 /* If x_catch_errors was done, turn it off now.
1307 (First we give unbind_to a chance to do that.) */
1308 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1309 The catch must remain in effect during that delicate
1310 state. --lorentey */
1311 x_fully_uncatch_errors ();
1312 #endif
1313 #endif
1315 byte_stack_list = catch->byte_stack;
1316 gcprolist = catch->gcpro;
1317 #ifdef DEBUG_GCPRO
1318 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1319 #endif
1320 backtrace_list = catch->backlist;
1321 lisp_eval_depth = catch->lisp_eval_depth;
1323 _longjmp (catch->jmp, 1);
1326 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1327 doc: /* Throw to the catch for TAG and return VALUE from it.
1328 Both TAG and VALUE are evalled. */)
1329 (register Lisp_Object tag, Lisp_Object value)
1331 register struct catchtag *c;
1333 if (!NILP (tag))
1334 for (c = catchlist; c; c = c->next)
1336 if (EQ (c->tag, tag))
1337 unwind_to_catch (c, value);
1339 xsignal2 (Qno_catch, tag, value);
1343 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1344 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1345 If BODYFORM completes normally, its value is returned
1346 after executing the UNWINDFORMS.
1347 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1348 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1349 (Lisp_Object args)
1351 Lisp_Object val;
1352 int count = SPECPDL_INDEX ();
1354 record_unwind_protect (Fprogn, Fcdr (args));
1355 val = eval_sub (Fcar (args));
1356 return unbind_to (count, val);
1359 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1360 doc: /* Regain control when an error is signaled.
1361 Executes BODYFORM and returns its value if no error happens.
1362 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1363 where the BODY is made of Lisp expressions.
1365 A handler is applicable to an error
1366 if CONDITION-NAME is one of the error's condition names.
1367 If an error happens, the first applicable handler is run.
1369 The car of a handler may be a list of condition names instead of a
1370 single condition name; then it handles all of them. If the special
1371 condition name `debug' is present in this list, it allows another
1372 condition in the list to run the debugger if `debug-on-error' and the
1373 other usual mechanisms says it should (otherwise, `condition-case'
1374 suppresses the debugger).
1376 When a handler handles an error, control returns to the `condition-case'
1377 and it executes the handler's BODY...
1378 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1379 \(If VAR is nil, the handler can't access that information.)
1380 Then the value of the last BODY form is returned from the `condition-case'
1381 expression.
1383 See also the function `signal' for more info.
1384 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1385 (Lisp_Object args)
1387 register Lisp_Object bodyform, handlers;
1388 volatile Lisp_Object var;
1390 var = Fcar (args);
1391 bodyform = Fcar (Fcdr (args));
1392 handlers = Fcdr (Fcdr (args));
1394 return internal_lisp_condition_case (var, bodyform, handlers);
1397 /* Like Fcondition_case, but the args are separate
1398 rather than passed in a list. Used by Fbyte_code. */
1400 Lisp_Object
1401 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1402 Lisp_Object handlers)
1404 Lisp_Object val;
1405 struct catchtag c;
1406 struct handler h;
1408 CHECK_SYMBOL (var);
1410 for (val = handlers; CONSP (val); val = XCDR (val))
1412 Lisp_Object tem;
1413 tem = XCAR (val);
1414 if (! (NILP (tem)
1415 || (CONSP (tem)
1416 && (SYMBOLP (XCAR (tem))
1417 || CONSP (XCAR (tem))))))
1418 error ("Invalid condition handler: %s",
1419 SDATA (Fprin1_to_string (tem, Qt)));
1422 c.tag = Qnil;
1423 c.val = Qnil;
1424 c.backlist = backtrace_list;
1425 c.handlerlist = handlerlist;
1426 c.lisp_eval_depth = lisp_eval_depth;
1427 c.pdlcount = SPECPDL_INDEX ();
1428 c.poll_suppress_count = poll_suppress_count;
1429 c.interrupt_input_blocked = interrupt_input_blocked;
1430 c.gcpro = gcprolist;
1431 c.byte_stack = byte_stack_list;
1432 if (_setjmp (c.jmp))
1434 if (!NILP (h.var))
1435 specbind (h.var, c.val);
1436 val = Fprogn (Fcdr (h.chosen_clause));
1438 /* Note that this just undoes the binding of h.var; whoever
1439 longjumped to us unwound the stack to c.pdlcount before
1440 throwing. */
1441 unbind_to (c.pdlcount, Qnil);
1442 return val;
1444 c.next = catchlist;
1445 catchlist = &c;
1447 h.var = var;
1448 h.handler = handlers;
1449 h.next = handlerlist;
1450 h.tag = &c;
1451 handlerlist = &h;
1453 val = eval_sub (bodyform);
1454 catchlist = c.next;
1455 handlerlist = h.next;
1456 return val;
1459 /* Call the function BFUN with no arguments, catching errors within it
1460 according to HANDLERS. If there is an error, call HFUN with
1461 one argument which is the data that describes the error:
1462 (SIGNALNAME . DATA)
1464 HANDLERS can be a list of conditions to catch.
1465 If HANDLERS is Qt, catch all errors.
1466 If HANDLERS is Qerror, catch all errors
1467 but allow the debugger to run if that is enabled. */
1469 Lisp_Object
1470 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1471 Lisp_Object (*hfun) (Lisp_Object))
1473 Lisp_Object val;
1474 struct catchtag c;
1475 struct handler h;
1477 c.tag = Qnil;
1478 c.val = Qnil;
1479 c.backlist = backtrace_list;
1480 c.handlerlist = handlerlist;
1481 c.lisp_eval_depth = lisp_eval_depth;
1482 c.pdlcount = SPECPDL_INDEX ();
1483 c.poll_suppress_count = poll_suppress_count;
1484 c.interrupt_input_blocked = interrupt_input_blocked;
1485 c.gcpro = gcprolist;
1486 c.byte_stack = byte_stack_list;
1487 if (_setjmp (c.jmp))
1489 return (*hfun) (c.val);
1491 c.next = catchlist;
1492 catchlist = &c;
1493 h.handler = handlers;
1494 h.var = Qnil;
1495 h.next = handlerlist;
1496 h.tag = &c;
1497 handlerlist = &h;
1499 val = (*bfun) ();
1500 catchlist = c.next;
1501 handlerlist = h.next;
1502 return val;
1505 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1507 Lisp_Object
1508 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1509 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1511 Lisp_Object val;
1512 struct catchtag c;
1513 struct handler h;
1515 c.tag = Qnil;
1516 c.val = Qnil;
1517 c.backlist = backtrace_list;
1518 c.handlerlist = handlerlist;
1519 c.lisp_eval_depth = lisp_eval_depth;
1520 c.pdlcount = SPECPDL_INDEX ();
1521 c.poll_suppress_count = poll_suppress_count;
1522 c.interrupt_input_blocked = interrupt_input_blocked;
1523 c.gcpro = gcprolist;
1524 c.byte_stack = byte_stack_list;
1525 if (_setjmp (c.jmp))
1527 return (*hfun) (c.val);
1529 c.next = catchlist;
1530 catchlist = &c;
1531 h.handler = handlers;
1532 h.var = Qnil;
1533 h.next = handlerlist;
1534 h.tag = &c;
1535 handlerlist = &h;
1537 val = (*bfun) (arg);
1538 catchlist = c.next;
1539 handlerlist = h.next;
1540 return val;
1543 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1544 its arguments. */
1546 Lisp_Object
1547 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1548 Lisp_Object arg1,
1549 Lisp_Object arg2,
1550 Lisp_Object handlers,
1551 Lisp_Object (*hfun) (Lisp_Object))
1553 Lisp_Object val;
1554 struct catchtag c;
1555 struct handler h;
1557 c.tag = Qnil;
1558 c.val = Qnil;
1559 c.backlist = backtrace_list;
1560 c.handlerlist = handlerlist;
1561 c.lisp_eval_depth = lisp_eval_depth;
1562 c.pdlcount = SPECPDL_INDEX ();
1563 c.poll_suppress_count = poll_suppress_count;
1564 c.interrupt_input_blocked = interrupt_input_blocked;
1565 c.gcpro = gcprolist;
1566 c.byte_stack = byte_stack_list;
1567 if (_setjmp (c.jmp))
1569 return (*hfun) (c.val);
1571 c.next = catchlist;
1572 catchlist = &c;
1573 h.handler = handlers;
1574 h.var = Qnil;
1575 h.next = handlerlist;
1576 h.tag = &c;
1577 handlerlist = &h;
1579 val = (*bfun) (arg1, arg2);
1580 catchlist = c.next;
1581 handlerlist = h.next;
1582 return val;
1585 /* Like internal_condition_case but call BFUN with NARGS as first,
1586 and ARGS as second argument. */
1588 Lisp_Object
1589 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1590 ptrdiff_t nargs,
1591 Lisp_Object *args,
1592 Lisp_Object handlers,
1593 Lisp_Object (*hfun) (Lisp_Object))
1595 Lisp_Object val;
1596 struct catchtag c;
1597 struct handler h;
1599 c.tag = Qnil;
1600 c.val = Qnil;
1601 c.backlist = backtrace_list;
1602 c.handlerlist = handlerlist;
1603 c.lisp_eval_depth = lisp_eval_depth;
1604 c.pdlcount = SPECPDL_INDEX ();
1605 c.poll_suppress_count = poll_suppress_count;
1606 c.interrupt_input_blocked = interrupt_input_blocked;
1607 c.gcpro = gcprolist;
1608 c.byte_stack = byte_stack_list;
1609 if (_setjmp (c.jmp))
1611 return (*hfun) (c.val);
1613 c.next = catchlist;
1614 catchlist = &c;
1615 h.handler = handlers;
1616 h.var = Qnil;
1617 h.next = handlerlist;
1618 h.tag = &c;
1619 handlerlist = &h;
1621 val = (*bfun) (nargs, args);
1622 catchlist = c.next;
1623 handlerlist = h.next;
1624 return val;
1628 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1629 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1630 Lisp_Object data);
1632 void
1633 process_quit_flag (void)
1635 Lisp_Object flag = Vquit_flag;
1636 Vquit_flag = Qnil;
1637 if (EQ (flag, Qkill_emacs))
1638 Fkill_emacs (Qnil);
1639 if (EQ (Vthrow_on_input, flag))
1640 Fthrow (Vthrow_on_input, Qt);
1641 Fsignal (Qquit, Qnil);
1644 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1645 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1646 This function does not return.
1648 An error symbol is a symbol with an `error-conditions' property
1649 that is a list of condition names.
1650 A handler for any of those names will get to handle this signal.
1651 The symbol `error' should normally be one of them.
1653 DATA should be a list. Its elements are printed as part of the error message.
1654 See Info anchor `(elisp)Definition of signal' for some details on how this
1655 error message is constructed.
1656 If the signal is handled, DATA is made available to the handler.
1657 See also the function `condition-case'. */)
1658 (Lisp_Object error_symbol, Lisp_Object data)
1660 /* When memory is full, ERROR-SYMBOL is nil,
1661 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1662 That is a special case--don't do this in other situations. */
1663 Lisp_Object conditions;
1664 Lisp_Object string;
1665 Lisp_Object real_error_symbol
1666 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1667 register Lisp_Object clause = Qnil;
1668 struct handler *h;
1669 struct backtrace *bp;
1671 immediate_quit = handling_signal = 0;
1672 abort_on_gc = 0;
1673 if (gc_in_progress || waiting_for_input)
1674 abort ();
1676 #if 0 /* rms: I don't know why this was here,
1677 but it is surely wrong for an error that is handled. */
1678 #ifdef HAVE_WINDOW_SYSTEM
1679 if (display_hourglass_p)
1680 cancel_hourglass ();
1681 #endif
1682 #endif
1684 /* This hook is used by edebug. */
1685 if (! NILP (Vsignal_hook_function)
1686 && ! NILP (error_symbol))
1688 /* Edebug takes care of restoring these variables when it exits. */
1689 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1690 max_lisp_eval_depth = lisp_eval_depth + 20;
1692 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1693 max_specpdl_size = SPECPDL_INDEX () + 40;
1695 call2 (Vsignal_hook_function, error_symbol, data);
1698 conditions = Fget (real_error_symbol, Qerror_conditions);
1700 /* Remember from where signal was called. Skip over the frame for
1701 `signal' itself. If a frame for `error' follows, skip that,
1702 too. Don't do this when ERROR_SYMBOL is nil, because that
1703 is a memory-full error. */
1704 Vsignaling_function = Qnil;
1705 if (backtrace_list && !NILP (error_symbol))
1707 bp = backtrace_list->next;
1708 if (bp && bp->function && EQ (*bp->function, Qerror))
1709 bp = bp->next;
1710 if (bp && bp->function)
1711 Vsignaling_function = *bp->function;
1714 for (h = handlerlist; h; h = h->next)
1716 clause = find_handler_clause (h->handler, conditions);
1717 if (!NILP (clause))
1718 break;
1721 if (/* Don't run the debugger for a memory-full error.
1722 (There is no room in memory to do that!) */
1723 !NILP (error_symbol)
1724 && (!NILP (Vdebug_on_signal)
1725 /* If no handler is present now, try to run the debugger. */
1726 || NILP (clause)
1727 /* A `debug' symbol in the handler list disables the normal
1728 suppression of the debugger. */
1729 || (CONSP (clause) && CONSP (XCAR (clause))
1730 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1731 /* Special handler that means "print a message and run debugger
1732 if requested". */
1733 || EQ (h->handler, Qerror)))
1735 int debugger_called
1736 = maybe_call_debugger (conditions, error_symbol, data);
1737 /* We can't return values to code which signaled an error, but we
1738 can continue code which has signaled a quit. */
1739 if (debugger_called && EQ (real_error_symbol, Qquit))
1740 return Qnil;
1743 if (!NILP (clause))
1745 Lisp_Object unwind_data
1746 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1748 h->chosen_clause = clause;
1749 unwind_to_catch (h->tag, unwind_data);
1751 else
1753 if (catchlist != 0)
1754 Fthrow (Qtop_level, Qt);
1757 if (! NILP (error_symbol))
1758 data = Fcons (error_symbol, data);
1760 string = Ferror_message_string (data);
1761 fatal ("%s", SDATA (string));
1764 /* Internal version of Fsignal that never returns.
1765 Used for anything but Qquit (which can return from Fsignal). */
1767 void
1768 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1770 Fsignal (error_symbol, data);
1771 abort ();
1774 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1776 void
1777 xsignal0 (Lisp_Object error_symbol)
1779 xsignal (error_symbol, Qnil);
1782 void
1783 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1785 xsignal (error_symbol, list1 (arg));
1788 void
1789 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1791 xsignal (error_symbol, list2 (arg1, arg2));
1794 void
1795 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1797 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1800 /* Signal `error' with message S, and additional arg ARG.
1801 If ARG is not a genuine list, make it a one-element list. */
1803 void
1804 signal_error (const char *s, Lisp_Object arg)
1806 Lisp_Object tortoise, hare;
1808 hare = tortoise = arg;
1809 while (CONSP (hare))
1811 hare = XCDR (hare);
1812 if (!CONSP (hare))
1813 break;
1815 hare = XCDR (hare);
1816 tortoise = XCDR (tortoise);
1818 if (EQ (hare, tortoise))
1819 break;
1822 if (!NILP (hare))
1823 arg = Fcons (arg, Qnil); /* Make it a list. */
1825 xsignal (Qerror, Fcons (build_string (s), arg));
1829 /* Return nonzero if LIST is a non-nil atom or
1830 a list containing one of CONDITIONS. */
1832 static int
1833 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1835 if (NILP (list))
1836 return 0;
1837 if (! CONSP (list))
1838 return 1;
1840 while (CONSP (conditions))
1842 Lisp_Object this, tail;
1843 this = XCAR (conditions);
1844 for (tail = list; CONSP (tail); tail = XCDR (tail))
1845 if (EQ (XCAR (tail), this))
1846 return 1;
1847 conditions = XCDR (conditions);
1849 return 0;
1852 /* Return 1 if an error with condition-symbols CONDITIONS,
1853 and described by SIGNAL-DATA, should skip the debugger
1854 according to debugger-ignored-errors. */
1856 static int
1857 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1859 Lisp_Object tail;
1860 int first_string = 1;
1861 Lisp_Object error_message;
1863 error_message = Qnil;
1864 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1866 if (STRINGP (XCAR (tail)))
1868 if (first_string)
1870 error_message = Ferror_message_string (data);
1871 first_string = 0;
1874 if (fast_string_match (XCAR (tail), error_message) >= 0)
1875 return 1;
1877 else
1879 Lisp_Object contail;
1881 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1882 if (EQ (XCAR (tail), XCAR (contail)))
1883 return 1;
1887 return 0;
1890 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1891 SIG and DATA describe the signal. There are two ways to pass them:
1892 = SIG is the error symbol, and DATA is the rest of the data.
1893 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1894 This is for memory-full errors only. */
1895 static int
1896 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1898 Lisp_Object combined_data;
1900 combined_data = Fcons (sig, data);
1902 if (
1903 /* Don't try to run the debugger with interrupts blocked.
1904 The editing loop would return anyway. */
1905 ! INPUT_BLOCKED_P
1906 /* Does user want to enter debugger for this kind of error? */
1907 && (EQ (sig, Qquit)
1908 ? debug_on_quit
1909 : wants_debugger (Vdebug_on_error, conditions))
1910 && ! skip_debugger (conditions, combined_data)
1911 /* RMS: What's this for? */
1912 && when_entered_debugger < num_nonmacro_input_events)
1914 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1915 return 1;
1918 return 0;
1921 static Lisp_Object
1922 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1924 register Lisp_Object h;
1926 /* t is used by handlers for all conditions, set up by C code. */
1927 if (EQ (handlers, Qt))
1928 return Qt;
1930 /* error is used similarly, but means print an error message
1931 and run the debugger if that is enabled. */
1932 if (EQ (handlers, Qerror))
1933 return Qt;
1935 for (h = handlers; CONSP (h); h = XCDR (h))
1937 Lisp_Object handler = XCAR (h);
1938 Lisp_Object condit, tem;
1940 if (!CONSP (handler))
1941 continue;
1942 condit = XCAR (handler);
1943 /* Handle a single condition name in handler HANDLER. */
1944 if (SYMBOLP (condit))
1946 tem = Fmemq (Fcar (handler), conditions);
1947 if (!NILP (tem))
1948 return handler;
1950 /* Handle a list of condition names in handler HANDLER. */
1951 else if (CONSP (condit))
1953 Lisp_Object tail;
1954 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1956 tem = Fmemq (XCAR (tail), conditions);
1957 if (!NILP (tem))
1958 return handler;
1963 return Qnil;
1967 /* Dump an error message; called like vprintf. */
1968 void
1969 verror (const char *m, va_list ap)
1971 char buf[4000];
1972 ptrdiff_t size = sizeof buf;
1973 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1974 char *buffer = buf;
1975 ptrdiff_t used;
1976 Lisp_Object string;
1978 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1979 string = make_string (buffer, used);
1980 if (buffer != buf)
1981 xfree (buffer);
1983 xsignal1 (Qerror, string);
1987 /* Dump an error message; called like printf. */
1989 /* VARARGS 1 */
1990 void
1991 error (const char *m, ...)
1993 va_list ap;
1994 va_start (ap, m);
1995 verror (m, ap);
1996 va_end (ap);
1999 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2000 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2001 This means it contains a description for how to read arguments to give it.
2002 The value is nil for an invalid function or a symbol with no function
2003 definition.
2005 Interactively callable functions include strings and vectors (treated
2006 as keyboard macros), lambda-expressions that contain a top-level call
2007 to `interactive', autoload definitions made by `autoload' with non-nil
2008 fourth argument, and some of the built-in functions of Lisp.
2010 Also, a symbol satisfies `commandp' if its function definition does so.
2012 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2013 then strings and vectors are not accepted. */)
2014 (Lisp_Object function, Lisp_Object for_call_interactively)
2016 register Lisp_Object fun;
2017 register Lisp_Object funcar;
2018 Lisp_Object if_prop = Qnil;
2020 fun = function;
2022 fun = indirect_function (fun); /* Check cycles. */
2023 if (NILP (fun) || EQ (fun, Qunbound))
2024 return Qnil;
2026 /* Check an `interactive-form' property if present, analogous to the
2027 function-documentation property. */
2028 fun = function;
2029 while (SYMBOLP (fun))
2031 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2032 if (!NILP (tmp))
2033 if_prop = Qt;
2034 fun = Fsymbol_function (fun);
2037 /* Emacs primitives are interactive if their DEFUN specifies an
2038 interactive spec. */
2039 if (SUBRP (fun))
2040 return XSUBR (fun)->intspec ? Qt : if_prop;
2042 /* Bytecode objects are interactive if they are long enough to
2043 have an element whose index is COMPILED_INTERACTIVE, which is
2044 where the interactive spec is stored. */
2045 else if (COMPILEDP (fun))
2046 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2047 ? Qt : if_prop);
2049 /* Strings and vectors are keyboard macros. */
2050 if (STRINGP (fun) || VECTORP (fun))
2051 return (NILP (for_call_interactively) ? Qt : Qnil);
2053 /* Lists may represent commands. */
2054 if (!CONSP (fun))
2055 return Qnil;
2056 funcar = XCAR (fun);
2057 if (EQ (funcar, Qclosure))
2058 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
2059 ? Qt : if_prop);
2060 else if (EQ (funcar, Qlambda))
2061 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2062 else if (EQ (funcar, Qautoload))
2063 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2064 else
2065 return Qnil;
2068 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2069 doc: /* Define FUNCTION to autoload from FILE.
2070 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2071 Third arg DOCSTRING is documentation for the function.
2072 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2073 Fifth arg TYPE indicates the type of the object:
2074 nil or omitted says FUNCTION is a function,
2075 `keymap' says FUNCTION is really a keymap, and
2076 `macro' or t says FUNCTION is really a macro.
2077 Third through fifth args give info about the real definition.
2078 They default to nil.
2079 If FUNCTION is already defined other than as an autoload,
2080 this does nothing and returns nil. */)
2081 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2083 CHECK_SYMBOL (function);
2084 CHECK_STRING (file);
2086 /* If function is defined and not as an autoload, don't override. */
2087 if (!EQ (XSYMBOL (function)->function, Qunbound)
2088 && !(CONSP (XSYMBOL (function)->function)
2089 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2090 return Qnil;
2092 if (NILP (Vpurify_flag))
2093 /* Only add entries after dumping, because the ones before are
2094 not useful and else we get loads of them from the loaddefs.el. */
2095 LOADHIST_ATTACH (Fcons (Qautoload, function));
2096 else
2097 /* We don't want the docstring in purespace (instead,
2098 Snarf-documentation should (hopefully) overwrite it).
2099 We used to use 0 here, but that leads to accidental sharing in
2100 purecopy's hash-consing, so we use a (hopefully) unique integer
2101 instead. */
2102 docstring = make_number (XPNTR (function));
2103 return Ffset (function,
2104 Fpurecopy (list5 (Qautoload, file, docstring,
2105 interactive, type)));
2108 Lisp_Object
2109 un_autoload (Lisp_Object oldqueue)
2111 register Lisp_Object queue, first, second;
2113 /* Queue to unwind is current value of Vautoload_queue.
2114 oldqueue is the shadowed value to leave in Vautoload_queue. */
2115 queue = Vautoload_queue;
2116 Vautoload_queue = oldqueue;
2117 while (CONSP (queue))
2119 first = XCAR (queue);
2120 second = Fcdr (first);
2121 first = Fcar (first);
2122 if (EQ (first, make_number (0)))
2123 Vfeatures = second;
2124 else
2125 Ffset (first, second);
2126 queue = XCDR (queue);
2128 return Qnil;
2131 /* Load an autoloaded function.
2132 FUNNAME is the symbol which is the function's name.
2133 FUNDEF is the autoload definition (a list). */
2135 void
2136 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2138 int count = SPECPDL_INDEX ();
2139 Lisp_Object fun;
2140 struct gcpro gcpro1, gcpro2, gcpro3;
2142 /* This is to make sure that loadup.el gives a clear picture
2143 of what files are preloaded and when. */
2144 if (! NILP (Vpurify_flag))
2145 error ("Attempt to autoload %s while preparing to dump",
2146 SDATA (SYMBOL_NAME (funname)));
2148 fun = funname;
2149 CHECK_SYMBOL (funname);
2150 GCPRO3 (fun, funname, fundef);
2152 /* Preserve the match data. */
2153 record_unwind_save_match_data ();
2155 /* If autoloading gets an error (which includes the error of failing
2156 to define the function being called), we use Vautoload_queue
2157 to undo function definitions and `provide' calls made by
2158 the function. We do this in the specific case of autoloading
2159 because autoloading is not an explicit request "load this file",
2160 but rather a request to "call this function".
2162 The value saved here is to be restored into Vautoload_queue. */
2163 record_unwind_protect (un_autoload, Vautoload_queue);
2164 Vautoload_queue = Qt;
2165 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2167 /* Once loading finishes, don't undo it. */
2168 Vautoload_queue = Qt;
2169 unbind_to (count, Qnil);
2171 fun = Findirect_function (fun, Qnil);
2173 if (!NILP (Fequal (fun, fundef)))
2174 error ("Autoloading failed to define function %s",
2175 SDATA (SYMBOL_NAME (funname)));
2176 UNGCPRO;
2180 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2181 doc: /* Evaluate FORM and return its value.
2182 If LEXICAL is t, evaluate using lexical scoping. */)
2183 (Lisp_Object form, Lisp_Object lexical)
2185 int count = SPECPDL_INDEX ();
2186 specbind (Qinternal_interpreter_environment,
2187 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2188 return unbind_to (count, eval_sub (form));
2191 /* Eval a sub-expression of the current expression (i.e. in the same
2192 lexical scope). */
2193 Lisp_Object
2194 eval_sub (Lisp_Object form)
2196 Lisp_Object fun, val, original_fun, original_args;
2197 Lisp_Object funcar;
2198 struct backtrace backtrace;
2199 struct gcpro gcpro1, gcpro2, gcpro3;
2201 if (handling_signal)
2202 abort ();
2204 if (SYMBOLP (form))
2206 /* Look up its binding in the lexical environment.
2207 We do not pay attention to the declared_special flag here, since we
2208 already did that when let-binding the variable. */
2209 Lisp_Object lex_binding
2210 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2211 ? Fassq (form, Vinternal_interpreter_environment)
2212 : Qnil;
2213 if (CONSP (lex_binding))
2214 return XCDR (lex_binding);
2215 else
2216 return Fsymbol_value (form);
2219 if (!CONSP (form))
2220 return form;
2222 QUIT;
2223 if ((consing_since_gc > gc_cons_threshold
2224 && consing_since_gc > gc_relative_threshold)
2226 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2228 GCPRO1 (form);
2229 Fgarbage_collect ();
2230 UNGCPRO;
2233 if (++lisp_eval_depth > max_lisp_eval_depth)
2235 if (max_lisp_eval_depth < 100)
2236 max_lisp_eval_depth = 100;
2237 if (lisp_eval_depth > max_lisp_eval_depth)
2238 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2241 original_fun = Fcar (form);
2242 original_args = Fcdr (form);
2244 backtrace.next = backtrace_list;
2245 backtrace_list = &backtrace;
2246 backtrace.function = &original_fun; /* This also protects them from gc. */
2247 backtrace.args = &original_args;
2248 backtrace.nargs = UNEVALLED;
2249 backtrace.debug_on_exit = 0;
2251 if (debug_on_next_call)
2252 do_debug_on_call (Qt);
2254 /* At this point, only original_fun and original_args
2255 have values that will be used below. */
2256 retry:
2258 /* Optimize for no indirection. */
2259 fun = original_fun;
2260 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2261 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2262 fun = indirect_function (fun);
2264 if (SUBRP (fun))
2266 Lisp_Object numargs;
2267 Lisp_Object argvals[8];
2268 Lisp_Object args_left;
2269 register int i, maxargs;
2271 args_left = original_args;
2272 numargs = Flength (args_left);
2274 CHECK_CONS_LIST ();
2276 if (XINT (numargs) < XSUBR (fun)->min_args
2277 || (XSUBR (fun)->max_args >= 0
2278 && XSUBR (fun)->max_args < XINT (numargs)))
2279 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2281 else if (XSUBR (fun)->max_args == UNEVALLED)
2282 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2283 else if (XSUBR (fun)->max_args == MANY)
2285 /* Pass a vector of evaluated arguments. */
2286 Lisp_Object *vals;
2287 ptrdiff_t argnum = 0;
2288 USE_SAFE_ALLOCA;
2290 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2292 GCPRO3 (args_left, fun, fun);
2293 gcpro3.var = vals;
2294 gcpro3.nvars = 0;
2296 while (!NILP (args_left))
2298 vals[argnum++] = eval_sub (Fcar (args_left));
2299 args_left = Fcdr (args_left);
2300 gcpro3.nvars = argnum;
2303 backtrace.args = vals;
2304 backtrace.nargs = XINT (numargs);
2306 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2307 UNGCPRO;
2308 SAFE_FREE ();
2310 else
2312 GCPRO3 (args_left, fun, fun);
2313 gcpro3.var = argvals;
2314 gcpro3.nvars = 0;
2316 maxargs = XSUBR (fun)->max_args;
2317 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2319 argvals[i] = eval_sub (Fcar (args_left));
2320 gcpro3.nvars = ++i;
2323 UNGCPRO;
2325 backtrace.args = argvals;
2326 backtrace.nargs = XINT (numargs);
2328 switch (i)
2330 case 0:
2331 val = (XSUBR (fun)->function.a0 ());
2332 break;
2333 case 1:
2334 val = (XSUBR (fun)->function.a1 (argvals[0]));
2335 break;
2336 case 2:
2337 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2338 break;
2339 case 3:
2340 val = (XSUBR (fun)->function.a3
2341 (argvals[0], argvals[1], argvals[2]));
2342 break;
2343 case 4:
2344 val = (XSUBR (fun)->function.a4
2345 (argvals[0], argvals[1], argvals[2], argvals[3]));
2346 break;
2347 case 5:
2348 val = (XSUBR (fun)->function.a5
2349 (argvals[0], argvals[1], argvals[2], argvals[3],
2350 argvals[4]));
2351 break;
2352 case 6:
2353 val = (XSUBR (fun)->function.a6
2354 (argvals[0], argvals[1], argvals[2], argvals[3],
2355 argvals[4], argvals[5]));
2356 break;
2357 case 7:
2358 val = (XSUBR (fun)->function.a7
2359 (argvals[0], argvals[1], argvals[2], argvals[3],
2360 argvals[4], argvals[5], argvals[6]));
2361 break;
2363 case 8:
2364 val = (XSUBR (fun)->function.a8
2365 (argvals[0], argvals[1], argvals[2], argvals[3],
2366 argvals[4], argvals[5], argvals[6], argvals[7]));
2367 break;
2369 default:
2370 /* Someone has created a subr that takes more arguments than
2371 is supported by this code. We need to either rewrite the
2372 subr to use a different argument protocol, or add more
2373 cases to this switch. */
2374 abort ();
2378 else if (COMPILEDP (fun))
2379 val = apply_lambda (fun, original_args);
2380 else
2382 if (EQ (fun, Qunbound))
2383 xsignal1 (Qvoid_function, original_fun);
2384 if (!CONSP (fun))
2385 xsignal1 (Qinvalid_function, original_fun);
2386 funcar = XCAR (fun);
2387 if (!SYMBOLP (funcar))
2388 xsignal1 (Qinvalid_function, original_fun);
2389 if (EQ (funcar, Qautoload))
2391 do_autoload (fun, original_fun);
2392 goto retry;
2394 if (EQ (funcar, Qmacro))
2395 val = eval_sub (apply1 (Fcdr (fun), original_args));
2396 else if (EQ (funcar, Qlambda)
2397 || EQ (funcar, Qclosure))
2398 val = apply_lambda (fun, original_args);
2399 else
2400 xsignal1 (Qinvalid_function, original_fun);
2402 CHECK_CONS_LIST ();
2404 lisp_eval_depth--;
2405 if (backtrace.debug_on_exit)
2406 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2407 backtrace_list = backtrace.next;
2409 return val;
2412 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2413 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2414 Then return the value FUNCTION returns.
2415 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2416 usage: (apply FUNCTION &rest ARGUMENTS) */)
2417 (ptrdiff_t nargs, Lisp_Object *args)
2419 ptrdiff_t i, numargs;
2420 register Lisp_Object spread_arg;
2421 register Lisp_Object *funcall_args;
2422 Lisp_Object fun, retval;
2423 struct gcpro gcpro1;
2424 USE_SAFE_ALLOCA;
2426 fun = args [0];
2427 funcall_args = 0;
2428 spread_arg = args [nargs - 1];
2429 CHECK_LIST (spread_arg);
2431 numargs = XINT (Flength (spread_arg));
2433 if (numargs == 0)
2434 return Ffuncall (nargs - 1, args);
2435 else if (numargs == 1)
2437 args [nargs - 1] = XCAR (spread_arg);
2438 return Ffuncall (nargs, args);
2441 numargs += nargs - 2;
2443 /* Optimize for no indirection. */
2444 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2445 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2446 fun = indirect_function (fun);
2447 if (EQ (fun, Qunbound))
2449 /* Let funcall get the error. */
2450 fun = args[0];
2451 goto funcall;
2454 if (SUBRP (fun))
2456 if (numargs < XSUBR (fun)->min_args
2457 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2458 goto funcall; /* Let funcall get the error. */
2459 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2461 /* Avoid making funcall cons up a yet another new vector of arguments
2462 by explicitly supplying nil's for optional values. */
2463 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2464 for (i = numargs; i < XSUBR (fun)->max_args;)
2465 funcall_args[++i] = Qnil;
2466 GCPRO1 (*funcall_args);
2467 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2470 funcall:
2471 /* We add 1 to numargs because funcall_args includes the
2472 function itself as well as its arguments. */
2473 if (!funcall_args)
2475 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2476 GCPRO1 (*funcall_args);
2477 gcpro1.nvars = 1 + numargs;
2480 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2481 /* Spread the last arg we got. Its first element goes in
2482 the slot that it used to occupy, hence this value of I. */
2483 i = nargs - 1;
2484 while (!NILP (spread_arg))
2486 funcall_args [i++] = XCAR (spread_arg);
2487 spread_arg = XCDR (spread_arg);
2490 /* By convention, the caller needs to gcpro Ffuncall's args. */
2491 retval = Ffuncall (gcpro1.nvars, funcall_args);
2492 UNGCPRO;
2493 SAFE_FREE ();
2495 return retval;
2498 /* Run hook variables in various ways. */
2500 static Lisp_Object
2501 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2503 Ffuncall (nargs, args);
2504 return Qnil;
2507 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2508 doc: /* Run each hook in HOOKS.
2509 Each argument should be a symbol, a hook variable.
2510 These symbols are processed in the order specified.
2511 If a hook symbol has a non-nil value, that value may be a function
2512 or a list of functions to be called to run the hook.
2513 If the value is a function, it is called with no arguments.
2514 If it is a list, the elements are called, in order, with no arguments.
2516 Major modes should not use this function directly to run their mode
2517 hook; they should use `run-mode-hooks' instead.
2519 Do not use `make-local-variable' to make a hook variable buffer-local.
2520 Instead, use `add-hook' and specify t for the LOCAL argument.
2521 usage: (run-hooks &rest HOOKS) */)
2522 (ptrdiff_t nargs, Lisp_Object *args)
2524 Lisp_Object hook[1];
2525 ptrdiff_t i;
2527 for (i = 0; i < nargs; i++)
2529 hook[0] = args[i];
2530 run_hook_with_args (1, hook, funcall_nil);
2533 return Qnil;
2536 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2537 Srun_hook_with_args, 1, MANY, 0,
2538 doc: /* Run HOOK with the specified arguments ARGS.
2539 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2540 value, that value may be a function or a list of functions to be
2541 called to run the hook. If the value is a function, it is called with
2542 the given arguments and its return value is returned. If it is a list
2543 of functions, those functions are called, in order,
2544 with the given arguments ARGS.
2545 It is best not to depend on the value returned by `run-hook-with-args',
2546 as that may change.
2548 Do not use `make-local-variable' to make a hook variable buffer-local.
2549 Instead, use `add-hook' and specify t for the LOCAL argument.
2550 usage: (run-hook-with-args HOOK &rest ARGS) */)
2551 (ptrdiff_t nargs, Lisp_Object *args)
2553 return run_hook_with_args (nargs, args, funcall_nil);
2556 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2557 Srun_hook_with_args_until_success, 1, MANY, 0,
2558 doc: /* Run HOOK with the specified arguments ARGS.
2559 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2560 value, that value may be a function or a list of functions to be
2561 called to run the hook. If the value is a function, it is called with
2562 the given arguments and its return value is returned.
2563 If it is a list of functions, those functions are called, in order,
2564 with the given arguments ARGS, until one of them
2565 returns a non-nil value. Then we return that value.
2566 However, if they all return nil, we return nil.
2568 Do not use `make-local-variable' to make a hook variable buffer-local.
2569 Instead, use `add-hook' and specify t for the LOCAL argument.
2570 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2571 (ptrdiff_t nargs, Lisp_Object *args)
2573 return run_hook_with_args (nargs, args, Ffuncall);
2576 static Lisp_Object
2577 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2579 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2582 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2583 Srun_hook_with_args_until_failure, 1, MANY, 0,
2584 doc: /* Run HOOK with the specified arguments ARGS.
2585 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2586 value, that value may be a function or a list of functions to be
2587 called to run the hook. If the value is a function, it is called with
2588 the given arguments and its return value is returned.
2589 If it is a list of functions, those functions are called, in order,
2590 with the given arguments ARGS, until one of them returns nil.
2591 Then we return nil. However, if they all return non-nil, we return non-nil.
2593 Do not use `make-local-variable' to make a hook variable buffer-local.
2594 Instead, use `add-hook' and specify t for the LOCAL argument.
2595 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2596 (ptrdiff_t nargs, Lisp_Object *args)
2598 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2601 static Lisp_Object
2602 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2604 Lisp_Object tmp = args[0], ret;
2605 args[0] = args[1];
2606 args[1] = tmp;
2607 ret = Ffuncall (nargs, args);
2608 args[1] = args[0];
2609 args[0] = tmp;
2610 return ret;
2613 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2614 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2615 I.e. instead of calling each function FUN directly with arguments ARGS,
2616 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2617 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2618 aborts and returns that value.
2619 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2620 (ptrdiff_t nargs, Lisp_Object *args)
2622 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2625 /* ARGS[0] should be a hook symbol.
2626 Call each of the functions in the hook value, passing each of them
2627 as arguments all the rest of ARGS (all NARGS - 1 elements).
2628 FUNCALL specifies how to call each function on the hook.
2629 The caller (or its caller, etc) must gcpro all of ARGS,
2630 except that it isn't necessary to gcpro ARGS[0]. */
2632 Lisp_Object
2633 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2634 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2636 Lisp_Object sym, val, ret = Qnil;
2637 struct gcpro gcpro1, gcpro2, gcpro3;
2639 /* If we are dying or still initializing,
2640 don't do anything--it would probably crash if we tried. */
2641 if (NILP (Vrun_hooks))
2642 return Qnil;
2644 sym = args[0];
2645 val = find_symbol_value (sym);
2647 if (EQ (val, Qunbound) || NILP (val))
2648 return ret;
2649 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2651 args[0] = val;
2652 return funcall (nargs, args);
2654 else
2656 Lisp_Object global_vals = Qnil;
2657 GCPRO3 (sym, val, global_vals);
2659 for (;
2660 CONSP (val) && NILP (ret);
2661 val = XCDR (val))
2663 if (EQ (XCAR (val), Qt))
2665 /* t indicates this hook has a local binding;
2666 it means to run the global binding too. */
2667 global_vals = Fdefault_value (sym);
2668 if (NILP (global_vals)) continue;
2670 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2672 args[0] = global_vals;
2673 ret = funcall (nargs, args);
2675 else
2677 for (;
2678 CONSP (global_vals) && NILP (ret);
2679 global_vals = XCDR (global_vals))
2681 args[0] = XCAR (global_vals);
2682 /* In a global value, t should not occur. If it does, we
2683 must ignore it to avoid an endless loop. */
2684 if (!EQ (args[0], Qt))
2685 ret = funcall (nargs, args);
2689 else
2691 args[0] = XCAR (val);
2692 ret = funcall (nargs, args);
2696 UNGCPRO;
2697 return ret;
2701 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2703 void
2704 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2706 Lisp_Object temp[3];
2707 temp[0] = hook;
2708 temp[1] = arg1;
2709 temp[2] = arg2;
2711 Frun_hook_with_args (3, temp);
2714 /* Apply fn to arg. */
2715 Lisp_Object
2716 apply1 (Lisp_Object fn, Lisp_Object arg)
2718 struct gcpro gcpro1;
2720 GCPRO1 (fn);
2721 if (NILP (arg))
2722 RETURN_UNGCPRO (Ffuncall (1, &fn));
2723 gcpro1.nvars = 2;
2725 Lisp_Object args[2];
2726 args[0] = fn;
2727 args[1] = arg;
2728 gcpro1.var = args;
2729 RETURN_UNGCPRO (Fapply (2, args));
2733 /* Call function fn on no arguments. */
2734 Lisp_Object
2735 call0 (Lisp_Object fn)
2737 struct gcpro gcpro1;
2739 GCPRO1 (fn);
2740 RETURN_UNGCPRO (Ffuncall (1, &fn));
2743 /* Call function fn with 1 argument arg1. */
2744 /* ARGSUSED */
2745 Lisp_Object
2746 call1 (Lisp_Object fn, Lisp_Object arg1)
2748 struct gcpro gcpro1;
2749 Lisp_Object args[2];
2751 args[0] = fn;
2752 args[1] = arg1;
2753 GCPRO1 (args[0]);
2754 gcpro1.nvars = 2;
2755 RETURN_UNGCPRO (Ffuncall (2, args));
2758 /* Call function fn with 2 arguments arg1, arg2. */
2759 /* ARGSUSED */
2760 Lisp_Object
2761 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2763 struct gcpro gcpro1;
2764 Lisp_Object args[3];
2765 args[0] = fn;
2766 args[1] = arg1;
2767 args[2] = arg2;
2768 GCPRO1 (args[0]);
2769 gcpro1.nvars = 3;
2770 RETURN_UNGCPRO (Ffuncall (3, args));
2773 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2774 /* ARGSUSED */
2775 Lisp_Object
2776 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2778 struct gcpro gcpro1;
2779 Lisp_Object args[4];
2780 args[0] = fn;
2781 args[1] = arg1;
2782 args[2] = arg2;
2783 args[3] = arg3;
2784 GCPRO1 (args[0]);
2785 gcpro1.nvars = 4;
2786 RETURN_UNGCPRO (Ffuncall (4, args));
2789 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2790 /* ARGSUSED */
2791 Lisp_Object
2792 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2793 Lisp_Object arg4)
2795 struct gcpro gcpro1;
2796 Lisp_Object args[5];
2797 args[0] = fn;
2798 args[1] = arg1;
2799 args[2] = arg2;
2800 args[3] = arg3;
2801 args[4] = arg4;
2802 GCPRO1 (args[0]);
2803 gcpro1.nvars = 5;
2804 RETURN_UNGCPRO (Ffuncall (5, args));
2807 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2808 /* ARGSUSED */
2809 Lisp_Object
2810 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2811 Lisp_Object arg4, Lisp_Object arg5)
2813 struct gcpro gcpro1;
2814 Lisp_Object args[6];
2815 args[0] = fn;
2816 args[1] = arg1;
2817 args[2] = arg2;
2818 args[3] = arg3;
2819 args[4] = arg4;
2820 args[5] = arg5;
2821 GCPRO1 (args[0]);
2822 gcpro1.nvars = 6;
2823 RETURN_UNGCPRO (Ffuncall (6, args));
2826 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2827 /* ARGSUSED */
2828 Lisp_Object
2829 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2830 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2832 struct gcpro gcpro1;
2833 Lisp_Object args[7];
2834 args[0] = fn;
2835 args[1] = arg1;
2836 args[2] = arg2;
2837 args[3] = arg3;
2838 args[4] = arg4;
2839 args[5] = arg5;
2840 args[6] = arg6;
2841 GCPRO1 (args[0]);
2842 gcpro1.nvars = 7;
2843 RETURN_UNGCPRO (Ffuncall (7, args));
2846 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2847 /* ARGSUSED */
2848 Lisp_Object
2849 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2850 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2852 struct gcpro gcpro1;
2853 Lisp_Object args[8];
2854 args[0] = fn;
2855 args[1] = arg1;
2856 args[2] = arg2;
2857 args[3] = arg3;
2858 args[4] = arg4;
2859 args[5] = arg5;
2860 args[6] = arg6;
2861 args[7] = arg7;
2862 GCPRO1 (args[0]);
2863 gcpro1.nvars = 8;
2864 RETURN_UNGCPRO (Ffuncall (8, args));
2867 /* The caller should GCPRO all the elements of ARGS. */
2869 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2870 doc: /* Non-nil if OBJECT is a function. */)
2871 (Lisp_Object object)
2873 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2875 object = Findirect_function (object, Qt);
2877 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2879 /* Autoloaded symbols are functions, except if they load
2880 macros or keymaps. */
2881 int i;
2882 for (i = 0; i < 4 && CONSP (object); i++)
2883 object = XCDR (object);
2885 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2889 if (SUBRP (object))
2890 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2891 else if (COMPILEDP (object))
2892 return Qt;
2893 else if (CONSP (object))
2895 Lisp_Object car = XCAR (object);
2896 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2898 else
2899 return Qnil;
2902 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2903 doc: /* Call first argument as a function, passing remaining arguments to it.
2904 Return the value that function returns.
2905 Thus, (funcall 'cons 'x 'y) returns (x . y).
2906 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2907 (ptrdiff_t nargs, Lisp_Object *args)
2909 Lisp_Object fun, original_fun;
2910 Lisp_Object funcar;
2911 ptrdiff_t numargs = nargs - 1;
2912 Lisp_Object lisp_numargs;
2913 Lisp_Object val;
2914 struct backtrace backtrace;
2915 register Lisp_Object *internal_args;
2916 ptrdiff_t i;
2918 QUIT;
2919 if ((consing_since_gc > gc_cons_threshold
2920 && consing_since_gc > gc_relative_threshold)
2922 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2923 Fgarbage_collect ();
2925 if (++lisp_eval_depth > max_lisp_eval_depth)
2927 if (max_lisp_eval_depth < 100)
2928 max_lisp_eval_depth = 100;
2929 if (lisp_eval_depth > max_lisp_eval_depth)
2930 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2933 backtrace.next = backtrace_list;
2934 backtrace_list = &backtrace;
2935 backtrace.function = &args[0];
2936 backtrace.args = &args[1];
2937 backtrace.nargs = nargs - 1;
2938 backtrace.debug_on_exit = 0;
2940 if (debug_on_next_call)
2941 do_debug_on_call (Qlambda);
2943 CHECK_CONS_LIST ();
2945 original_fun = args[0];
2947 retry:
2949 /* Optimize for no indirection. */
2950 fun = original_fun;
2951 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2952 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2953 fun = indirect_function (fun);
2955 if (SUBRP (fun))
2957 if (numargs < XSUBR (fun)->min_args
2958 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2960 XSETFASTINT (lisp_numargs, numargs);
2961 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2964 else if (XSUBR (fun)->max_args == UNEVALLED)
2965 xsignal1 (Qinvalid_function, original_fun);
2967 else if (XSUBR (fun)->max_args == MANY)
2968 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2969 else
2971 if (XSUBR (fun)->max_args > numargs)
2973 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2974 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
2975 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2976 internal_args[i] = Qnil;
2978 else
2979 internal_args = args + 1;
2980 switch (XSUBR (fun)->max_args)
2982 case 0:
2983 val = (XSUBR (fun)->function.a0 ());
2984 break;
2985 case 1:
2986 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2987 break;
2988 case 2:
2989 val = (XSUBR (fun)->function.a2
2990 (internal_args[0], internal_args[1]));
2991 break;
2992 case 3:
2993 val = (XSUBR (fun)->function.a3
2994 (internal_args[0], internal_args[1], internal_args[2]));
2995 break;
2996 case 4:
2997 val = (XSUBR (fun)->function.a4
2998 (internal_args[0], internal_args[1], internal_args[2],
2999 internal_args[3]));
3000 break;
3001 case 5:
3002 val = (XSUBR (fun)->function.a5
3003 (internal_args[0], internal_args[1], internal_args[2],
3004 internal_args[3], internal_args[4]));
3005 break;
3006 case 6:
3007 val = (XSUBR (fun)->function.a6
3008 (internal_args[0], internal_args[1], internal_args[2],
3009 internal_args[3], internal_args[4], internal_args[5]));
3010 break;
3011 case 7:
3012 val = (XSUBR (fun)->function.a7
3013 (internal_args[0], internal_args[1], internal_args[2],
3014 internal_args[3], internal_args[4], internal_args[5],
3015 internal_args[6]));
3016 break;
3018 case 8:
3019 val = (XSUBR (fun)->function.a8
3020 (internal_args[0], internal_args[1], internal_args[2],
3021 internal_args[3], internal_args[4], internal_args[5],
3022 internal_args[6], internal_args[7]));
3023 break;
3025 default:
3027 /* If a subr takes more than 8 arguments without using MANY
3028 or UNEVALLED, we need to extend this function to support it.
3029 Until this is done, there is no way to call the function. */
3030 abort ();
3034 else if (COMPILEDP (fun))
3035 val = funcall_lambda (fun, numargs, args + 1);
3036 else
3038 if (EQ (fun, Qunbound))
3039 xsignal1 (Qvoid_function, original_fun);
3040 if (!CONSP (fun))
3041 xsignal1 (Qinvalid_function, original_fun);
3042 funcar = XCAR (fun);
3043 if (!SYMBOLP (funcar))
3044 xsignal1 (Qinvalid_function, original_fun);
3045 if (EQ (funcar, Qlambda)
3046 || EQ (funcar, Qclosure))
3047 val = funcall_lambda (fun, numargs, args + 1);
3048 else if (EQ (funcar, Qautoload))
3050 do_autoload (fun, original_fun);
3051 CHECK_CONS_LIST ();
3052 goto retry;
3054 else
3055 xsignal1 (Qinvalid_function, original_fun);
3057 CHECK_CONS_LIST ();
3058 lisp_eval_depth--;
3059 if (backtrace.debug_on_exit)
3060 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3061 backtrace_list = backtrace.next;
3062 return val;
3065 static Lisp_Object
3066 apply_lambda (Lisp_Object fun, Lisp_Object args)
3068 Lisp_Object args_left;
3069 ptrdiff_t i, numargs;
3070 register Lisp_Object *arg_vector;
3071 struct gcpro gcpro1, gcpro2, gcpro3;
3072 register Lisp_Object tem;
3073 USE_SAFE_ALLOCA;
3075 numargs = XFASTINT (Flength (args));
3076 SAFE_ALLOCA_LISP (arg_vector, numargs);
3077 args_left = args;
3079 GCPRO3 (*arg_vector, args_left, fun);
3080 gcpro1.nvars = 0;
3082 for (i = 0; i < numargs; )
3084 tem = Fcar (args_left), args_left = Fcdr (args_left);
3085 tem = eval_sub (tem);
3086 arg_vector[i++] = tem;
3087 gcpro1.nvars = i;
3090 UNGCPRO;
3092 backtrace_list->args = arg_vector;
3093 backtrace_list->nargs = i;
3094 tem = funcall_lambda (fun, numargs, arg_vector);
3096 /* Do the debug-on-exit now, while arg_vector still exists. */
3097 if (backtrace_list->debug_on_exit)
3098 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3099 /* Don't do it again when we return to eval. */
3100 backtrace_list->debug_on_exit = 0;
3101 SAFE_FREE ();
3102 return tem;
3105 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3106 and return the result of evaluation.
3107 FUN must be either a lambda-expression or a compiled-code object. */
3109 static Lisp_Object
3110 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
3111 register Lisp_Object *arg_vector)
3113 Lisp_Object val, syms_left, next, lexenv;
3114 int count = SPECPDL_INDEX ();
3115 ptrdiff_t i;
3116 int optional, rest;
3118 if (CONSP (fun))
3120 if (EQ (XCAR (fun), Qclosure))
3122 fun = XCDR (fun); /* Drop `closure'. */
3123 lexenv = XCAR (fun);
3124 CHECK_LIST_CONS (fun, fun);
3126 else
3127 lexenv = Qnil;
3128 syms_left = XCDR (fun);
3129 if (CONSP (syms_left))
3130 syms_left = XCAR (syms_left);
3131 else
3132 xsignal1 (Qinvalid_function, fun);
3134 else if (COMPILEDP (fun))
3136 syms_left = AREF (fun, COMPILED_ARGLIST);
3137 if (INTEGERP (syms_left))
3138 /* A byte-code object with a non-nil `push args' slot means we
3139 shouldn't bind any arguments, instead just call the byte-code
3140 interpreter directly; it will push arguments as necessary.
3142 Byte-code objects with either a non-existent, or a nil value for
3143 the `push args' slot (the default), have dynamically-bound
3144 arguments, and use the argument-binding code below instead (as do
3145 all interpreted functions, even lexically bound ones). */
3147 /* If we have not actually read the bytecode string
3148 and constants vector yet, fetch them from the file. */
3149 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3150 Ffetch_bytecode (fun);
3151 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3152 AREF (fun, COMPILED_CONSTANTS),
3153 AREF (fun, COMPILED_STACK_DEPTH),
3154 syms_left,
3155 nargs, arg_vector);
3157 lexenv = Qnil;
3159 else
3160 abort ();
3162 i = optional = rest = 0;
3163 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3165 QUIT;
3167 next = XCAR (syms_left);
3168 if (!SYMBOLP (next))
3169 xsignal1 (Qinvalid_function, fun);
3171 if (EQ (next, Qand_rest))
3172 rest = 1;
3173 else if (EQ (next, Qand_optional))
3174 optional = 1;
3175 else
3177 Lisp_Object arg;
3178 if (rest)
3180 arg = Flist (nargs - i, &arg_vector[i]);
3181 i = nargs;
3183 else if (i < nargs)
3184 arg = arg_vector[i++];
3185 else if (!optional)
3186 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3187 else
3188 arg = Qnil;
3190 /* Bind the argument. */
3191 if (!NILP (lexenv) && SYMBOLP (next))
3192 /* Lexically bind NEXT by adding it to the lexenv alist. */
3193 lexenv = Fcons (Fcons (next, arg), lexenv);
3194 else
3195 /* Dynamically bind NEXT. */
3196 specbind (next, arg);
3200 if (!NILP (syms_left))
3201 xsignal1 (Qinvalid_function, fun);
3202 else if (i < nargs)
3203 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3205 if (!EQ (lexenv, Vinternal_interpreter_environment))
3206 /* Instantiate a new lexical environment. */
3207 specbind (Qinternal_interpreter_environment, lexenv);
3209 if (CONSP (fun))
3210 val = Fprogn (XCDR (XCDR (fun)));
3211 else
3213 /* If we have not actually read the bytecode string
3214 and constants vector yet, fetch them from the file. */
3215 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3216 Ffetch_bytecode (fun);
3217 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3218 AREF (fun, COMPILED_CONSTANTS),
3219 AREF (fun, COMPILED_STACK_DEPTH),
3220 Qnil, 0, 0);
3223 return unbind_to (count, val);
3226 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3227 1, 1, 0,
3228 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3229 (Lisp_Object object)
3231 Lisp_Object tem;
3233 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3235 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3236 if (!CONSP (tem))
3238 tem = AREF (object, COMPILED_BYTECODE);
3239 if (CONSP (tem) && STRINGP (XCAR (tem)))
3240 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3241 else
3242 error ("Invalid byte code");
3244 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3245 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3247 return object;
3250 static void
3251 grow_specpdl (void)
3253 register int count = SPECPDL_INDEX ();
3254 int max_size =
3255 min (max_specpdl_size,
3256 min (max (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct specbinding),
3257 INT_MAX));
3258 int size;
3259 if (max_size <= specpdl_size)
3261 if (max_specpdl_size < 400)
3262 max_size = max_specpdl_size = 400;
3263 if (max_size <= specpdl_size)
3264 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3266 size = specpdl_size < max_size / 2 ? 2 * specpdl_size : max_size;
3267 specpdl = xnrealloc (specpdl, size, sizeof *specpdl);
3268 specpdl_size = size;
3269 specpdl_ptr = specpdl + count;
3272 /* `specpdl_ptr->symbol' is a field which describes which variable is
3273 let-bound, so it can be properly undone when we unbind_to.
3274 It can have the following two shapes:
3275 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3276 a symbol that is not buffer-local (at least at the time
3277 the let binding started). Note also that it should not be
3278 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3279 to record V2 here).
3280 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3281 variable SYMBOL which can be buffer-local. WHERE tells us
3282 which buffer is affected (or nil if the let-binding affects the
3283 global value of the variable) and BUFFER tells us which buffer was
3284 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3285 BUFFER did not yet have a buffer-local value). */
3287 void
3288 specbind (Lisp_Object symbol, Lisp_Object value)
3290 struct Lisp_Symbol *sym;
3292 eassert (!handling_signal);
3294 CHECK_SYMBOL (symbol);
3295 sym = XSYMBOL (symbol);
3296 if (specpdl_ptr == specpdl + specpdl_size)
3297 grow_specpdl ();
3299 start:
3300 switch (sym->redirect)
3302 case SYMBOL_VARALIAS:
3303 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3304 case SYMBOL_PLAINVAL:
3305 /* The most common case is that of a non-constant symbol with a
3306 trivial value. Make that as fast as we can. */
3307 specpdl_ptr->symbol = symbol;
3308 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3309 specpdl_ptr->func = NULL;
3310 ++specpdl_ptr;
3311 if (!sym->constant)
3312 SET_SYMBOL_VAL (sym, value);
3313 else
3314 set_internal (symbol, value, Qnil, 1);
3315 break;
3316 case SYMBOL_LOCALIZED:
3317 if (SYMBOL_BLV (sym)->frame_local)
3318 error ("Frame-local vars cannot be let-bound");
3319 case SYMBOL_FORWARDED:
3321 Lisp_Object ovalue = find_symbol_value (symbol);
3322 specpdl_ptr->func = 0;
3323 specpdl_ptr->old_value = ovalue;
3325 eassert (sym->redirect != SYMBOL_LOCALIZED
3326 || (EQ (SYMBOL_BLV (sym)->where,
3327 SYMBOL_BLV (sym)->frame_local ?
3328 Fselected_frame () : Fcurrent_buffer ())));
3330 if (sym->redirect == SYMBOL_LOCALIZED
3331 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3333 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3335 /* For a local variable, record both the symbol and which
3336 buffer's or frame's value we are saving. */
3337 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3339 eassert (sym->redirect != SYMBOL_LOCALIZED
3340 || (BLV_FOUND (SYMBOL_BLV (sym))
3341 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3342 where = cur_buf;
3344 else if (sym->redirect == SYMBOL_LOCALIZED
3345 && BLV_FOUND (SYMBOL_BLV (sym)))
3346 where = SYMBOL_BLV (sym)->where;
3347 else
3348 where = Qnil;
3350 /* We're not using the `unused' slot in the specbinding
3351 structure because this would mean we have to do more
3352 work for simple variables. */
3353 /* FIXME: The third value `current_buffer' is only used in
3354 let_shadows_buffer_binding_p which is itself only used
3355 in set_internal for local_if_set. */
3356 eassert (NILP (where) || EQ (where, cur_buf));
3357 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3359 /* If SYMBOL is a per-buffer variable which doesn't have a
3360 buffer-local value here, make the `let' change the global
3361 value by changing the value of SYMBOL in all buffers not
3362 having their own value. This is consistent with what
3363 happens with other buffer-local variables. */
3364 if (NILP (where)
3365 && sym->redirect == SYMBOL_FORWARDED)
3367 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3368 ++specpdl_ptr;
3369 Fset_default (symbol, value);
3370 return;
3373 else
3374 specpdl_ptr->symbol = symbol;
3376 specpdl_ptr++;
3377 set_internal (symbol, value, Qnil, 1);
3378 break;
3380 default: abort ();
3384 void
3385 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3387 eassert (!handling_signal);
3389 if (specpdl_ptr == specpdl + specpdl_size)
3390 grow_specpdl ();
3391 specpdl_ptr->func = function;
3392 specpdl_ptr->symbol = Qnil;
3393 specpdl_ptr->old_value = arg;
3394 specpdl_ptr++;
3397 Lisp_Object
3398 unbind_to (int count, Lisp_Object value)
3400 Lisp_Object quitf = Vquit_flag;
3401 struct gcpro gcpro1, gcpro2;
3403 GCPRO2 (value, quitf);
3404 Vquit_flag = Qnil;
3406 while (specpdl_ptr != specpdl + count)
3408 /* Copy the binding, and decrement specpdl_ptr, before we do
3409 the work to unbind it. We decrement first
3410 so that an error in unbinding won't try to unbind
3411 the same entry again, and we copy the binding first
3412 in case more bindings are made during some of the code we run. */
3414 struct specbinding this_binding;
3415 this_binding = *--specpdl_ptr;
3417 if (this_binding.func != 0)
3418 (*this_binding.func) (this_binding.old_value);
3419 /* If the symbol is a list, it is really (SYMBOL WHERE
3420 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3421 frame. If WHERE is a buffer or frame, this indicates we
3422 bound a variable that had a buffer-local or frame-local
3423 binding. WHERE nil means that the variable had the default
3424 value when it was bound. CURRENT-BUFFER is the buffer that
3425 was current when the variable was bound. */
3426 else if (CONSP (this_binding.symbol))
3428 Lisp_Object symbol, where;
3430 symbol = XCAR (this_binding.symbol);
3431 where = XCAR (XCDR (this_binding.symbol));
3433 if (NILP (where))
3434 Fset_default (symbol, this_binding.old_value);
3435 /* If `where' is non-nil, reset the value in the appropriate
3436 local binding, but only if that binding still exists. */
3437 else if (BUFFERP (where)
3438 ? !NILP (Flocal_variable_p (symbol, where))
3439 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3440 set_internal (symbol, this_binding.old_value, where, 1);
3442 /* If variable has a trivial value (no forwarding), we can
3443 just set it. No need to check for constant symbols here,
3444 since that was already done by specbind. */
3445 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3446 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3447 this_binding.old_value);
3448 else
3449 /* NOTE: we only ever come here if make_local_foo was used for
3450 the first time on this var within this let. */
3451 Fset_default (this_binding.symbol, this_binding.old_value);
3454 if (NILP (Vquit_flag) && !NILP (quitf))
3455 Vquit_flag = quitf;
3457 UNGCPRO;
3458 return value;
3461 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3462 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3463 A special variable is one that will be bound dynamically, even in a
3464 context where binding is lexical by default. */)
3465 (Lisp_Object symbol)
3467 CHECK_SYMBOL (symbol);
3468 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3472 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3473 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3474 The debugger is entered when that frame exits, if the flag is non-nil. */)
3475 (Lisp_Object level, Lisp_Object flag)
3477 register struct backtrace *backlist = backtrace_list;
3478 register int i;
3480 CHECK_NUMBER (level);
3482 for (i = 0; backlist && i < XINT (level); i++)
3484 backlist = backlist->next;
3487 if (backlist)
3488 backlist->debug_on_exit = !NILP (flag);
3490 return flag;
3493 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3494 doc: /* Print a trace of Lisp function calls currently active.
3495 Output stream used is value of `standard-output'. */)
3496 (void)
3498 register struct backtrace *backlist = backtrace_list;
3499 Lisp_Object tail;
3500 Lisp_Object tem;
3501 struct gcpro gcpro1;
3502 Lisp_Object old_print_level = Vprint_level;
3504 if (NILP (Vprint_level))
3505 XSETFASTINT (Vprint_level, 8);
3507 tail = Qnil;
3508 GCPRO1 (tail);
3510 while (backlist)
3512 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3513 if (backlist->nargs == UNEVALLED)
3515 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3516 write_string ("\n", -1);
3518 else
3520 tem = *backlist->function;
3521 Fprin1 (tem, Qnil); /* This can QUIT. */
3522 write_string ("(", -1);
3523 if (backlist->nargs == MANY)
3524 { /* FIXME: Can this happen? */
3525 int i;
3526 for (tail = *backlist->args, i = 0;
3527 !NILP (tail);
3528 tail = Fcdr (tail), i = 1)
3530 if (i) write_string (" ", -1);
3531 Fprin1 (Fcar (tail), Qnil);
3534 else
3536 ptrdiff_t i;
3537 for (i = 0; i < backlist->nargs; i++)
3539 if (i) write_string (" ", -1);
3540 Fprin1 (backlist->args[i], Qnil);
3543 write_string (")\n", -1);
3545 backlist = backlist->next;
3548 Vprint_level = old_print_level;
3549 UNGCPRO;
3550 return Qnil;
3553 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3554 doc: /* Return the function and arguments NFRAMES up from current execution point.
3555 If that frame has not evaluated the arguments yet (or is a special form),
3556 the value is (nil FUNCTION ARG-FORMS...).
3557 If that frame has evaluated its arguments and called its function already,
3558 the value is (t FUNCTION ARG-VALUES...).
3559 A &rest arg is represented as the tail of the list ARG-VALUES.
3560 FUNCTION is whatever was supplied as car of evaluated list,
3561 or a lambda expression for macro calls.
3562 If NFRAMES is more than the number of frames, the value is nil. */)
3563 (Lisp_Object nframes)
3565 register struct backtrace *backlist = backtrace_list;
3566 register EMACS_INT i;
3567 Lisp_Object tem;
3569 CHECK_NATNUM (nframes);
3571 /* Find the frame requested. */
3572 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3573 backlist = backlist->next;
3575 if (!backlist)
3576 return Qnil;
3577 if (backlist->nargs == UNEVALLED)
3578 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3579 else
3581 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3582 tem = *backlist->args;
3583 else
3584 tem = Flist (backlist->nargs, backlist->args);
3586 return Fcons (Qt, Fcons (*backlist->function, tem));
3591 #if BYTE_MARK_STACK
3592 void
3593 mark_backtrace (void)
3595 register struct backtrace *backlist;
3596 ptrdiff_t i;
3598 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3600 mark_object (*backlist->function);
3602 if (backlist->nargs == UNEVALLED
3603 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3604 i = 1;
3605 else
3606 i = backlist->nargs;
3607 while (i--)
3608 mark_object (backlist->args[i]);
3611 #endif
3613 void
3614 syms_of_eval (void)
3616 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3617 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3618 If Lisp code tries to increase the total number past this amount,
3619 an error is signaled.
3620 You can safely use a value considerably larger than the default value,
3621 if that proves inconveniently small. However, if you increase it too far,
3622 Emacs could run out of memory trying to make the stack bigger. */);
3624 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3625 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3627 This limit serves to catch infinite recursions for you before they cause
3628 actual stack overflow in C, which would be fatal for Emacs.
3629 You can safely make it considerably larger than its default value,
3630 if that proves inconveniently small. However, if you increase it too far,
3631 Emacs could overflow the real C stack, and crash. */);
3633 DEFVAR_LISP ("quit-flag", Vquit_flag,
3634 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3635 If the value is t, that means do an ordinary quit.
3636 If the value equals `throw-on-input', that means quit by throwing
3637 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3638 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3639 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3640 Vquit_flag = Qnil;
3642 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3643 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3644 Note that `quit-flag' will still be set by typing C-g,
3645 so a quit will be signaled as soon as `inhibit-quit' is nil.
3646 To prevent this happening, set `quit-flag' to nil
3647 before making `inhibit-quit' nil. */);
3648 Vinhibit_quit = Qnil;
3650 DEFSYM (Qinhibit_quit, "inhibit-quit");
3651 DEFSYM (Qautoload, "autoload");
3652 DEFSYM (Qdebug_on_error, "debug-on-error");
3653 DEFSYM (Qmacro, "macro");
3654 DEFSYM (Qdeclare, "declare");
3656 /* Note that the process handling also uses Qexit, but we don't want
3657 to staticpro it twice, so we just do it here. */
3658 DEFSYM (Qexit, "exit");
3660 DEFSYM (Qinteractive, "interactive");
3661 DEFSYM (Qcommandp, "commandp");
3662 DEFSYM (Qdefun, "defun");
3663 DEFSYM (Qand_rest, "&rest");
3664 DEFSYM (Qand_optional, "&optional");
3665 DEFSYM (Qclosure, "closure");
3666 DEFSYM (Qdebug, "debug");
3668 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3669 doc: /* *Non-nil means enter debugger if an error is signaled.
3670 Does not apply to errors handled by `condition-case' or those
3671 matched by `debug-ignored-errors'.
3672 If the value is a list, an error only means to enter the debugger
3673 if one of its condition symbols appears in the list.
3674 When you evaluate an expression interactively, this variable
3675 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3676 The command `toggle-debug-on-error' toggles this.
3677 See also the variable `debug-on-quit'. */);
3678 Vdebug_on_error = Qnil;
3680 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3681 doc: /* *List of errors for which the debugger should not be called.
3682 Each element may be a condition-name or a regexp that matches error messages.
3683 If any element applies to a given error, that error skips the debugger
3684 and just returns to top level.
3685 This overrides the variable `debug-on-error'.
3686 It does not apply to errors handled by `condition-case'. */);
3687 Vdebug_ignored_errors = Qnil;
3689 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3690 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3691 Does not apply if quit is handled by a `condition-case'. */);
3692 debug_on_quit = 0;
3694 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3695 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3697 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3698 doc: /* Non-nil means debugger may continue execution.
3699 This is nil when the debugger is called under circumstances where it
3700 might not be safe to continue. */);
3701 debugger_may_continue = 1;
3703 DEFVAR_LISP ("debugger", Vdebugger,
3704 doc: /* Function to call to invoke debugger.
3705 If due to frame exit, args are `exit' and the value being returned;
3706 this function's value will be returned instead of that.
3707 If due to error, args are `error' and a list of the args to `signal'.
3708 If due to `apply' or `funcall' entry, one arg, `lambda'.
3709 If due to `eval' entry, one arg, t. */);
3710 Vdebugger = Qnil;
3712 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3713 doc: /* If non-nil, this is a function for `signal' to call.
3714 It receives the same arguments that `signal' was given.
3715 The Edebug package uses this to regain control. */);
3716 Vsignal_hook_function = Qnil;
3718 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3719 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3720 Note that `debug-on-error', `debug-on-quit' and friends
3721 still determine whether to handle the particular condition. */);
3722 Vdebug_on_signal = Qnil;
3724 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3725 doc: /* Function to process declarations in a macro definition.
3726 The function will be called with two args MACRO and DECL.
3727 MACRO is the name of the macro being defined.
3728 DECL is a list `(declare ...)' containing the declarations.
3729 The value the function returns is not used. */);
3730 Vmacro_declaration_function = Qnil;
3732 /* When lexical binding is being used,
3733 vinternal_interpreter_environment is non-nil, and contains an alist
3734 of lexically-bound variable, or (t), indicating an empty
3735 environment. The lisp name of this variable would be
3736 `internal-interpreter-environment' if it weren't hidden.
3737 Every element of this list can be either a cons (VAR . VAL)
3738 specifying a lexical binding, or a single symbol VAR indicating
3739 that this variable should use dynamic scoping. */
3740 DEFSYM (Qinternal_interpreter_environment, "internal-interpreter-environment");
3741 DEFVAR_LISP ("internal-interpreter-environment",
3742 Vinternal_interpreter_environment,
3743 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3744 When lexical binding is not being used, this variable is nil.
3745 A value of `(t)' indicates an empty environment, otherwise it is an
3746 alist of active lexical bindings. */);
3747 Vinternal_interpreter_environment = Qnil;
3748 /* Don't export this variable to Elisp, so no one can mess with it
3749 (Just imagine if someone makes it buffer-local). */
3750 Funintern (Qinternal_interpreter_environment, Qnil);
3752 DEFSYM (Vrun_hooks, "run-hooks");
3754 staticpro (&Vautoload_queue);
3755 Vautoload_queue = Qnil;
3756 staticpro (&Vsignaling_function);
3757 Vsignaling_function = Qnil;
3759 defsubr (&Sor);
3760 defsubr (&Sand);
3761 defsubr (&Sif);
3762 defsubr (&Scond);
3763 defsubr (&Sprogn);
3764 defsubr (&Sprog1);
3765 defsubr (&Sprog2);
3766 defsubr (&Ssetq);
3767 defsubr (&Squote);
3768 defsubr (&Sfunction);
3769 defsubr (&Sdefun);
3770 defsubr (&Sdefmacro);
3771 defsubr (&Sdefvar);
3772 defsubr (&Sdefvaralias);
3773 defsubr (&Sdefconst);
3774 defsubr (&Suser_variable_p);
3775 defsubr (&Slet);
3776 defsubr (&SletX);
3777 defsubr (&Swhile);
3778 defsubr (&Smacroexpand);
3779 defsubr (&Scatch);
3780 defsubr (&Sthrow);
3781 defsubr (&Sunwind_protect);
3782 defsubr (&Scondition_case);
3783 defsubr (&Ssignal);
3784 defsubr (&Sinteractive_p);
3785 defsubr (&Scalled_interactively_p);
3786 defsubr (&Scommandp);
3787 defsubr (&Sautoload);
3788 defsubr (&Seval);
3789 defsubr (&Sapply);
3790 defsubr (&Sfuncall);
3791 defsubr (&Srun_hooks);
3792 defsubr (&Srun_hook_with_args);
3793 defsubr (&Srun_hook_with_args_until_success);
3794 defsubr (&Srun_hook_with_args_until_failure);
3795 defsubr (&Srun_hook_wrapped);
3796 defsubr (&Sfetch_bytecode);
3797 defsubr (&Sbacktrace_debug);
3798 defsubr (&Sbacktrace);
3799 defsubr (&Sbacktrace_frame);
3800 defsubr (&Sspecial_variable_p);
3801 defsubr (&Sfunctionp);