Fix bug #10129: add positional information to "C-u C-x =".
[emacs.git] / src / eval.c
blobdbd06e7c1b1ae9dde15cf314589f47cc9f5095b1
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2012 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.)
930 This function returns t if (i) the first character of its
931 documentation is `*', or (ii) it is customizable (its property list
932 contains a non-nil value of `standard-value' or `custom-autoload'), or
933 \(iii) it is an alias for a user variable.
935 But condition (i) is considered obsolete, so for most purposes this is
936 equivalent to `custom-variable-p'. */)
937 (Lisp_Object variable)
939 Lisp_Object documentation;
941 if (!SYMBOLP (variable))
942 return Qnil;
944 /* If indirect and there's an alias loop, don't check anything else. */
945 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
946 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
947 Qt, user_variable_p_eh)))
948 return Qnil;
950 while (1)
952 documentation = Fget (variable, Qvariable_documentation);
953 if (INTEGERP (documentation) && XINT (documentation) < 0)
954 return Qt;
955 if (STRINGP (documentation)
956 && ((unsigned char) SREF (documentation, 0) == '*'))
957 return Qt;
958 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
959 if (CONSP (documentation)
960 && STRINGP (XCAR (documentation))
961 && INTEGERP (XCDR (documentation))
962 && XINT (XCDR (documentation)) < 0)
963 return Qt;
964 /* Customizable? See `custom-variable-p'. */
965 if ((!NILP (Fget (variable, intern ("standard-value"))))
966 || (!NILP (Fget (variable, intern ("custom-autoload")))))
967 return Qt;
969 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
970 return Qnil;
972 /* An indirect variable? Let's follow the chain. */
973 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
977 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
978 doc: /* Bind variables according to VARLIST then eval BODY.
979 The value of the last form in BODY is returned.
980 Each element of VARLIST is a symbol (which is bound to nil)
981 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
982 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
983 usage: (let* VARLIST BODY...) */)
984 (Lisp_Object args)
986 Lisp_Object varlist, var, val, elt, lexenv;
987 int count = SPECPDL_INDEX ();
988 struct gcpro gcpro1, gcpro2, gcpro3;
990 GCPRO3 (args, elt, varlist);
992 lexenv = Vinternal_interpreter_environment;
994 varlist = Fcar (args);
995 while (CONSP (varlist))
997 QUIT;
999 elt = XCAR (varlist);
1000 if (SYMBOLP (elt))
1002 var = elt;
1003 val = Qnil;
1005 else if (! NILP (Fcdr (Fcdr (elt))))
1006 signal_error ("`let' bindings can have only one value-form", elt);
1007 else
1009 var = Fcar (elt);
1010 val = eval_sub (Fcar (Fcdr (elt)));
1013 if (!NILP (lexenv) && SYMBOLP (var)
1014 && !XSYMBOL (var)->declared_special
1015 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1016 /* Lexically bind VAR by adding it to the interpreter's binding
1017 alist. */
1019 Lisp_Object newenv
1020 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1021 if (EQ (Vinternal_interpreter_environment, lexenv))
1022 /* Save the old lexical environment on the specpdl stack,
1023 but only for the first lexical binding, since we'll never
1024 need to revert to one of the intermediate ones. */
1025 specbind (Qinternal_interpreter_environment, newenv);
1026 else
1027 Vinternal_interpreter_environment = newenv;
1029 else
1030 specbind (var, val);
1032 varlist = XCDR (varlist);
1034 UNGCPRO;
1035 val = Fprogn (Fcdr (args));
1036 return unbind_to (count, val);
1039 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1040 doc: /* Bind variables according to VARLIST then eval BODY.
1041 The value of the last form in BODY is returned.
1042 Each element of VARLIST is a symbol (which is bound to nil)
1043 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1044 All the VALUEFORMs are evalled before any symbols are bound.
1045 usage: (let VARLIST BODY...) */)
1046 (Lisp_Object args)
1048 Lisp_Object *temps, tem, lexenv;
1049 register Lisp_Object elt, varlist;
1050 int count = SPECPDL_INDEX ();
1051 ptrdiff_t argnum;
1052 struct gcpro gcpro1, gcpro2;
1053 USE_SAFE_ALLOCA;
1055 varlist = Fcar (args);
1057 /* Make space to hold the values to give the bound variables. */
1058 elt = Flength (varlist);
1059 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1061 /* Compute the values and store them in `temps'. */
1063 GCPRO2 (args, *temps);
1064 gcpro2.nvars = 0;
1066 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1068 QUIT;
1069 elt = XCAR (varlist);
1070 if (SYMBOLP (elt))
1071 temps [argnum++] = Qnil;
1072 else if (! NILP (Fcdr (Fcdr (elt))))
1073 signal_error ("`let' bindings can have only one value-form", elt);
1074 else
1075 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1076 gcpro2.nvars = argnum;
1078 UNGCPRO;
1080 lexenv = Vinternal_interpreter_environment;
1082 varlist = Fcar (args);
1083 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1085 Lisp_Object var;
1087 elt = XCAR (varlist);
1088 var = SYMBOLP (elt) ? elt : Fcar (elt);
1089 tem = temps[argnum++];
1091 if (!NILP (lexenv) && SYMBOLP (var)
1092 && !XSYMBOL (var)->declared_special
1093 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1094 /* Lexically bind VAR by adding it to the lexenv alist. */
1095 lexenv = Fcons (Fcons (var, tem), lexenv);
1096 else
1097 /* Dynamically bind VAR. */
1098 specbind (var, tem);
1101 if (!EQ (lexenv, Vinternal_interpreter_environment))
1102 /* Instantiate a new lexical environment. */
1103 specbind (Qinternal_interpreter_environment, lexenv);
1105 elt = Fprogn (Fcdr (args));
1106 SAFE_FREE ();
1107 return unbind_to (count, elt);
1110 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1111 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1112 The order of execution is thus TEST, BODY, TEST, BODY and so on
1113 until TEST returns nil.
1114 usage: (while TEST BODY...) */)
1115 (Lisp_Object args)
1117 Lisp_Object test, body;
1118 struct gcpro gcpro1, gcpro2;
1120 GCPRO2 (test, body);
1122 test = Fcar (args);
1123 body = Fcdr (args);
1124 while (!NILP (eval_sub (test)))
1126 QUIT;
1127 Fprogn (body);
1130 UNGCPRO;
1131 return Qnil;
1134 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1135 doc: /* Return result of expanding macros at top level of FORM.
1136 If FORM is not a macro call, it is returned unchanged.
1137 Otherwise, the macro is expanded and the expansion is considered
1138 in place of FORM. When a non-macro-call results, it is returned.
1140 The second optional arg ENVIRONMENT specifies an environment of macro
1141 definitions to shadow the loaded ones for use in file byte-compilation. */)
1142 (Lisp_Object form, Lisp_Object environment)
1144 /* With cleanups from Hallvard Furuseth. */
1145 register Lisp_Object expander, sym, def, tem;
1147 while (1)
1149 /* Come back here each time we expand a macro call,
1150 in case it expands into another macro call. */
1151 if (!CONSP (form))
1152 break;
1153 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1154 def = sym = XCAR (form);
1155 tem = Qnil;
1156 /* Trace symbols aliases to other symbols
1157 until we get a symbol that is not an alias. */
1158 while (SYMBOLP (def))
1160 QUIT;
1161 sym = def;
1162 tem = Fassq (sym, environment);
1163 if (NILP (tem))
1165 def = XSYMBOL (sym)->function;
1166 if (!EQ (def, Qunbound))
1167 continue;
1169 break;
1171 /* Right now TEM is the result from SYM in ENVIRONMENT,
1172 and if TEM is nil then DEF is SYM's function definition. */
1173 if (NILP (tem))
1175 /* SYM is not mentioned in ENVIRONMENT.
1176 Look at its function definition. */
1177 if (EQ (def, Qunbound) || !CONSP (def))
1178 /* Not defined or definition not suitable. */
1179 break;
1180 if (EQ (XCAR (def), Qautoload))
1182 /* Autoloading function: will it be a macro when loaded? */
1183 tem = Fnth (make_number (4), def);
1184 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1185 /* Yes, load it and try again. */
1187 struct gcpro gcpro1;
1188 GCPRO1 (form);
1189 do_autoload (def, sym);
1190 UNGCPRO;
1191 continue;
1193 else
1194 break;
1196 else if (!EQ (XCAR (def), Qmacro))
1197 break;
1198 else expander = XCDR (def);
1200 else
1202 expander = XCDR (tem);
1203 if (NILP (expander))
1204 break;
1206 form = apply1 (expander, XCDR (form));
1208 return form;
1211 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1212 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1213 TAG is evalled to get the tag to use; it must not be nil.
1215 Then the BODY is executed.
1216 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1217 If no throw happens, `catch' returns the value of the last BODY form.
1218 If a throw happens, it specifies the value to return from `catch'.
1219 usage: (catch TAG BODY...) */)
1220 (Lisp_Object args)
1222 register Lisp_Object tag;
1223 struct gcpro gcpro1;
1225 GCPRO1 (args);
1226 tag = eval_sub (Fcar (args));
1227 UNGCPRO;
1228 return internal_catch (tag, Fprogn, Fcdr (args));
1231 /* Set up a catch, then call C function FUNC on argument ARG.
1232 FUNC should return a Lisp_Object.
1233 This is how catches are done from within C code. */
1235 Lisp_Object
1236 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1238 /* This structure is made part of the chain `catchlist'. */
1239 struct catchtag c;
1241 /* Fill in the components of c, and put it on the list. */
1242 c.next = catchlist;
1243 c.tag = tag;
1244 c.val = Qnil;
1245 c.backlist = backtrace_list;
1246 c.handlerlist = handlerlist;
1247 c.lisp_eval_depth = lisp_eval_depth;
1248 c.pdlcount = SPECPDL_INDEX ();
1249 c.poll_suppress_count = poll_suppress_count;
1250 c.interrupt_input_blocked = interrupt_input_blocked;
1251 c.gcpro = gcprolist;
1252 c.byte_stack = byte_stack_list;
1253 catchlist = &c;
1255 /* Call FUNC. */
1256 if (! _setjmp (c.jmp))
1257 c.val = (*func) (arg);
1259 /* Throw works by a longjmp that comes right here. */
1260 catchlist = c.next;
1261 return c.val;
1264 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1265 jump to that CATCH, returning VALUE as the value of that catch.
1267 This is the guts Fthrow and Fsignal; they differ only in the way
1268 they choose the catch tag to throw to. A catch tag for a
1269 condition-case form has a TAG of Qnil.
1271 Before each catch is discarded, unbind all special bindings and
1272 execute all unwind-protect clauses made above that catch. Unwind
1273 the handler stack as we go, so that the proper handlers are in
1274 effect for each unwind-protect clause we run. At the end, restore
1275 some static info saved in CATCH, and longjmp to the location
1276 specified in the
1278 This is used for correct unwinding in Fthrow and Fsignal. */
1280 static void
1281 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1283 register int last_time;
1285 /* Save the value in the tag. */
1286 catch->val = value;
1288 /* Restore certain special C variables. */
1289 set_poll_suppress_count (catch->poll_suppress_count);
1290 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1291 handling_signal = 0;
1292 immediate_quit = 0;
1296 last_time = catchlist == catch;
1298 /* Unwind the specpdl stack, and then restore the proper set of
1299 handlers. */
1300 unbind_to (catchlist->pdlcount, Qnil);
1301 handlerlist = catchlist->handlerlist;
1302 catchlist = catchlist->next;
1304 while (! last_time);
1306 #if HAVE_X_WINDOWS
1307 /* If x_catch_errors was done, turn it off now.
1308 (First we give unbind_to a chance to do that.) */
1309 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1310 The catch must remain in effect during that delicate
1311 state. --lorentey */
1312 x_fully_uncatch_errors ();
1313 #endif
1314 #endif
1316 byte_stack_list = catch->byte_stack;
1317 gcprolist = catch->gcpro;
1318 #ifdef DEBUG_GCPRO
1319 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1320 #endif
1321 backtrace_list = catch->backlist;
1322 lisp_eval_depth = catch->lisp_eval_depth;
1324 _longjmp (catch->jmp, 1);
1327 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1328 doc: /* Throw to the catch for TAG and return VALUE from it.
1329 Both TAG and VALUE are evalled. */)
1330 (register Lisp_Object tag, Lisp_Object value)
1332 register struct catchtag *c;
1334 if (!NILP (tag))
1335 for (c = catchlist; c; c = c->next)
1337 if (EQ (c->tag, tag))
1338 unwind_to_catch (c, value);
1340 xsignal2 (Qno_catch, tag, value);
1344 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1345 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1346 If BODYFORM completes normally, its value is returned
1347 after executing the UNWINDFORMS.
1348 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1349 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1350 (Lisp_Object args)
1352 Lisp_Object val;
1353 int count = SPECPDL_INDEX ();
1355 record_unwind_protect (Fprogn, Fcdr (args));
1356 val = eval_sub (Fcar (args));
1357 return unbind_to (count, val);
1360 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1361 doc: /* Regain control when an error is signaled.
1362 Executes BODYFORM and returns its value if no error happens.
1363 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1364 where the BODY is made of Lisp expressions.
1366 A handler is applicable to an error
1367 if CONDITION-NAME is one of the error's condition names.
1368 If an error happens, the first applicable handler is run.
1370 The car of a handler may be a list of condition names instead of a
1371 single condition name; then it handles all of them. If the special
1372 condition name `debug' is present in this list, it allows another
1373 condition in the list to run the debugger if `debug-on-error' and the
1374 other usual mechanisms says it should (otherwise, `condition-case'
1375 suppresses the debugger).
1377 When a handler handles an error, control returns to the `condition-case'
1378 and it executes the handler's BODY...
1379 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1380 \(If VAR is nil, the handler can't access that information.)
1381 Then the value of the last BODY form is returned from the `condition-case'
1382 expression.
1384 See also the function `signal' for more info.
1385 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1386 (Lisp_Object args)
1388 register Lisp_Object bodyform, handlers;
1389 volatile Lisp_Object var;
1391 var = Fcar (args);
1392 bodyform = Fcar (Fcdr (args));
1393 handlers = Fcdr (Fcdr (args));
1395 return internal_lisp_condition_case (var, bodyform, handlers);
1398 /* Like Fcondition_case, but the args are separate
1399 rather than passed in a list. Used by Fbyte_code. */
1401 Lisp_Object
1402 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1403 Lisp_Object handlers)
1405 Lisp_Object val;
1406 struct catchtag c;
1407 struct handler h;
1409 CHECK_SYMBOL (var);
1411 for (val = handlers; CONSP (val); val = XCDR (val))
1413 Lisp_Object tem;
1414 tem = XCAR (val);
1415 if (! (NILP (tem)
1416 || (CONSP (tem)
1417 && (SYMBOLP (XCAR (tem))
1418 || CONSP (XCAR (tem))))))
1419 error ("Invalid condition handler: %s",
1420 SDATA (Fprin1_to_string (tem, Qt)));
1423 c.tag = Qnil;
1424 c.val = Qnil;
1425 c.backlist = backtrace_list;
1426 c.handlerlist = handlerlist;
1427 c.lisp_eval_depth = lisp_eval_depth;
1428 c.pdlcount = SPECPDL_INDEX ();
1429 c.poll_suppress_count = poll_suppress_count;
1430 c.interrupt_input_blocked = interrupt_input_blocked;
1431 c.gcpro = gcprolist;
1432 c.byte_stack = byte_stack_list;
1433 if (_setjmp (c.jmp))
1435 if (!NILP (h.var))
1436 specbind (h.var, c.val);
1437 val = Fprogn (Fcdr (h.chosen_clause));
1439 /* Note that this just undoes the binding of h.var; whoever
1440 longjumped to us unwound the stack to c.pdlcount before
1441 throwing. */
1442 unbind_to (c.pdlcount, Qnil);
1443 return val;
1445 c.next = catchlist;
1446 catchlist = &c;
1448 h.var = var;
1449 h.handler = handlers;
1450 h.next = handlerlist;
1451 h.tag = &c;
1452 handlerlist = &h;
1454 val = eval_sub (bodyform);
1455 catchlist = c.next;
1456 handlerlist = h.next;
1457 return val;
1460 /* Call the function BFUN with no arguments, catching errors within it
1461 according to HANDLERS. If there is an error, call HFUN with
1462 one argument which is the data that describes the error:
1463 (SIGNALNAME . DATA)
1465 HANDLERS can be a list of conditions to catch.
1466 If HANDLERS is Qt, catch all errors.
1467 If HANDLERS is Qerror, catch all errors
1468 but allow the debugger to run if that is enabled. */
1470 Lisp_Object
1471 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1472 Lisp_Object (*hfun) (Lisp_Object))
1474 Lisp_Object val;
1475 struct catchtag c;
1476 struct handler h;
1478 c.tag = Qnil;
1479 c.val = Qnil;
1480 c.backlist = backtrace_list;
1481 c.handlerlist = handlerlist;
1482 c.lisp_eval_depth = lisp_eval_depth;
1483 c.pdlcount = SPECPDL_INDEX ();
1484 c.poll_suppress_count = poll_suppress_count;
1485 c.interrupt_input_blocked = interrupt_input_blocked;
1486 c.gcpro = gcprolist;
1487 c.byte_stack = byte_stack_list;
1488 if (_setjmp (c.jmp))
1490 return (*hfun) (c.val);
1492 c.next = catchlist;
1493 catchlist = &c;
1494 h.handler = handlers;
1495 h.var = Qnil;
1496 h.next = handlerlist;
1497 h.tag = &c;
1498 handlerlist = &h;
1500 val = (*bfun) ();
1501 catchlist = c.next;
1502 handlerlist = h.next;
1503 return val;
1506 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1508 Lisp_Object
1509 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1510 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1512 Lisp_Object val;
1513 struct catchtag c;
1514 struct handler h;
1516 c.tag = Qnil;
1517 c.val = Qnil;
1518 c.backlist = backtrace_list;
1519 c.handlerlist = handlerlist;
1520 c.lisp_eval_depth = lisp_eval_depth;
1521 c.pdlcount = SPECPDL_INDEX ();
1522 c.poll_suppress_count = poll_suppress_count;
1523 c.interrupt_input_blocked = interrupt_input_blocked;
1524 c.gcpro = gcprolist;
1525 c.byte_stack = byte_stack_list;
1526 if (_setjmp (c.jmp))
1528 return (*hfun) (c.val);
1530 c.next = catchlist;
1531 catchlist = &c;
1532 h.handler = handlers;
1533 h.var = Qnil;
1534 h.next = handlerlist;
1535 h.tag = &c;
1536 handlerlist = &h;
1538 val = (*bfun) (arg);
1539 catchlist = c.next;
1540 handlerlist = h.next;
1541 return val;
1544 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1545 its arguments. */
1547 Lisp_Object
1548 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1549 Lisp_Object arg1,
1550 Lisp_Object arg2,
1551 Lisp_Object handlers,
1552 Lisp_Object (*hfun) (Lisp_Object))
1554 Lisp_Object val;
1555 struct catchtag c;
1556 struct handler h;
1558 c.tag = Qnil;
1559 c.val = Qnil;
1560 c.backlist = backtrace_list;
1561 c.handlerlist = handlerlist;
1562 c.lisp_eval_depth = lisp_eval_depth;
1563 c.pdlcount = SPECPDL_INDEX ();
1564 c.poll_suppress_count = poll_suppress_count;
1565 c.interrupt_input_blocked = interrupt_input_blocked;
1566 c.gcpro = gcprolist;
1567 c.byte_stack = byte_stack_list;
1568 if (_setjmp (c.jmp))
1570 return (*hfun) (c.val);
1572 c.next = catchlist;
1573 catchlist = &c;
1574 h.handler = handlers;
1575 h.var = Qnil;
1576 h.next = handlerlist;
1577 h.tag = &c;
1578 handlerlist = &h;
1580 val = (*bfun) (arg1, arg2);
1581 catchlist = c.next;
1582 handlerlist = h.next;
1583 return val;
1586 /* Like internal_condition_case but call BFUN with NARGS as first,
1587 and ARGS as second argument. */
1589 Lisp_Object
1590 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1591 ptrdiff_t nargs,
1592 Lisp_Object *args,
1593 Lisp_Object handlers,
1594 Lisp_Object (*hfun) (Lisp_Object))
1596 Lisp_Object val;
1597 struct catchtag c;
1598 struct handler h;
1600 c.tag = Qnil;
1601 c.val = Qnil;
1602 c.backlist = backtrace_list;
1603 c.handlerlist = handlerlist;
1604 c.lisp_eval_depth = lisp_eval_depth;
1605 c.pdlcount = SPECPDL_INDEX ();
1606 c.poll_suppress_count = poll_suppress_count;
1607 c.interrupt_input_blocked = interrupt_input_blocked;
1608 c.gcpro = gcprolist;
1609 c.byte_stack = byte_stack_list;
1610 if (_setjmp (c.jmp))
1612 return (*hfun) (c.val);
1614 c.next = catchlist;
1615 catchlist = &c;
1616 h.handler = handlers;
1617 h.var = Qnil;
1618 h.next = handlerlist;
1619 h.tag = &c;
1620 handlerlist = &h;
1622 val = (*bfun) (nargs, args);
1623 catchlist = c.next;
1624 handlerlist = h.next;
1625 return val;
1629 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1630 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1631 Lisp_Object data);
1633 void
1634 process_quit_flag (void)
1636 Lisp_Object flag = Vquit_flag;
1637 Vquit_flag = Qnil;
1638 if (EQ (flag, Qkill_emacs))
1639 Fkill_emacs (Qnil);
1640 if (EQ (Vthrow_on_input, flag))
1641 Fthrow (Vthrow_on_input, Qt);
1642 Fsignal (Qquit, Qnil);
1645 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1646 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1647 This function does not return.
1649 An error symbol is a symbol with an `error-conditions' property
1650 that is a list of condition names.
1651 A handler for any of those names will get to handle this signal.
1652 The symbol `error' should normally be one of them.
1654 DATA should be a list. Its elements are printed as part of the error message.
1655 See Info anchor `(elisp)Definition of signal' for some details on how this
1656 error message is constructed.
1657 If the signal is handled, DATA is made available to the handler.
1658 See also the function `condition-case'. */)
1659 (Lisp_Object error_symbol, Lisp_Object data)
1661 /* When memory is full, ERROR-SYMBOL is nil,
1662 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1663 That is a special case--don't do this in other situations. */
1664 Lisp_Object conditions;
1665 Lisp_Object string;
1666 Lisp_Object real_error_symbol
1667 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1668 register Lisp_Object clause = Qnil;
1669 struct handler *h;
1670 struct backtrace *bp;
1672 immediate_quit = handling_signal = 0;
1673 abort_on_gc = 0;
1674 if (gc_in_progress || waiting_for_input)
1675 abort ();
1677 #if 0 /* rms: I don't know why this was here,
1678 but it is surely wrong for an error that is handled. */
1679 #ifdef HAVE_WINDOW_SYSTEM
1680 if (display_hourglass_p)
1681 cancel_hourglass ();
1682 #endif
1683 #endif
1685 /* This hook is used by edebug. */
1686 if (! NILP (Vsignal_hook_function)
1687 && ! NILP (error_symbol))
1689 /* Edebug takes care of restoring these variables when it exits. */
1690 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1691 max_lisp_eval_depth = lisp_eval_depth + 20;
1693 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1694 max_specpdl_size = SPECPDL_INDEX () + 40;
1696 call2 (Vsignal_hook_function, error_symbol, data);
1699 conditions = Fget (real_error_symbol, Qerror_conditions);
1701 /* Remember from where signal was called. Skip over the frame for
1702 `signal' itself. If a frame for `error' follows, skip that,
1703 too. Don't do this when ERROR_SYMBOL is nil, because that
1704 is a memory-full error. */
1705 Vsignaling_function = Qnil;
1706 if (backtrace_list && !NILP (error_symbol))
1708 bp = backtrace_list->next;
1709 if (bp && bp->function && EQ (*bp->function, Qerror))
1710 bp = bp->next;
1711 if (bp && bp->function)
1712 Vsignaling_function = *bp->function;
1715 for (h = handlerlist; h; h = h->next)
1717 clause = find_handler_clause (h->handler, conditions);
1718 if (!NILP (clause))
1719 break;
1722 if (/* Don't run the debugger for a memory-full error.
1723 (There is no room in memory to do that!) */
1724 !NILP (error_symbol)
1725 && (!NILP (Vdebug_on_signal)
1726 /* If no handler is present now, try to run the debugger. */
1727 || NILP (clause)
1728 /* A `debug' symbol in the handler list disables the normal
1729 suppression of the debugger. */
1730 || (CONSP (clause) && CONSP (XCAR (clause))
1731 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1732 /* Special handler that means "print a message and run debugger
1733 if requested". */
1734 || EQ (h->handler, Qerror)))
1736 int debugger_called
1737 = maybe_call_debugger (conditions, error_symbol, data);
1738 /* We can't return values to code which signaled an error, but we
1739 can continue code which has signaled a quit. */
1740 if (debugger_called && EQ (real_error_symbol, Qquit))
1741 return Qnil;
1744 if (!NILP (clause))
1746 Lisp_Object unwind_data
1747 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1749 h->chosen_clause = clause;
1750 unwind_to_catch (h->tag, unwind_data);
1752 else
1754 if (catchlist != 0)
1755 Fthrow (Qtop_level, Qt);
1758 if (! NILP (error_symbol))
1759 data = Fcons (error_symbol, data);
1761 string = Ferror_message_string (data);
1762 fatal ("%s", SDATA (string));
1765 /* Internal version of Fsignal that never returns.
1766 Used for anything but Qquit (which can return from Fsignal). */
1768 void
1769 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1771 Fsignal (error_symbol, data);
1772 abort ();
1775 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1777 void
1778 xsignal0 (Lisp_Object error_symbol)
1780 xsignal (error_symbol, Qnil);
1783 void
1784 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1786 xsignal (error_symbol, list1 (arg));
1789 void
1790 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1792 xsignal (error_symbol, list2 (arg1, arg2));
1795 void
1796 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1798 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1801 /* Signal `error' with message S, and additional arg ARG.
1802 If ARG is not a genuine list, make it a one-element list. */
1804 void
1805 signal_error (const char *s, Lisp_Object arg)
1807 Lisp_Object tortoise, hare;
1809 hare = tortoise = arg;
1810 while (CONSP (hare))
1812 hare = XCDR (hare);
1813 if (!CONSP (hare))
1814 break;
1816 hare = XCDR (hare);
1817 tortoise = XCDR (tortoise);
1819 if (EQ (hare, tortoise))
1820 break;
1823 if (!NILP (hare))
1824 arg = Fcons (arg, Qnil); /* Make it a list. */
1826 xsignal (Qerror, Fcons (build_string (s), arg));
1830 /* Return nonzero if LIST is a non-nil atom or
1831 a list containing one of CONDITIONS. */
1833 static int
1834 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1836 if (NILP (list))
1837 return 0;
1838 if (! CONSP (list))
1839 return 1;
1841 while (CONSP (conditions))
1843 Lisp_Object this, tail;
1844 this = XCAR (conditions);
1845 for (tail = list; CONSP (tail); tail = XCDR (tail))
1846 if (EQ (XCAR (tail), this))
1847 return 1;
1848 conditions = XCDR (conditions);
1850 return 0;
1853 /* Return 1 if an error with condition-symbols CONDITIONS,
1854 and described by SIGNAL-DATA, should skip the debugger
1855 according to debugger-ignored-errors. */
1857 static int
1858 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1860 Lisp_Object tail;
1861 int first_string = 1;
1862 Lisp_Object error_message;
1864 error_message = Qnil;
1865 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1867 if (STRINGP (XCAR (tail)))
1869 if (first_string)
1871 error_message = Ferror_message_string (data);
1872 first_string = 0;
1875 if (fast_string_match (XCAR (tail), error_message) >= 0)
1876 return 1;
1878 else
1880 Lisp_Object contail;
1882 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1883 if (EQ (XCAR (tail), XCAR (contail)))
1884 return 1;
1888 return 0;
1891 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1892 SIG and DATA describe the signal. There are two ways to pass them:
1893 = SIG is the error symbol, and DATA is the rest of the data.
1894 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1895 This is for memory-full errors only. */
1896 static int
1897 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1899 Lisp_Object combined_data;
1901 combined_data = Fcons (sig, data);
1903 if (
1904 /* Don't try to run the debugger with interrupts blocked.
1905 The editing loop would return anyway. */
1906 ! INPUT_BLOCKED_P
1907 /* Does user want to enter debugger for this kind of error? */
1908 && (EQ (sig, Qquit)
1909 ? debug_on_quit
1910 : wants_debugger (Vdebug_on_error, conditions))
1911 && ! skip_debugger (conditions, combined_data)
1912 /* RMS: What's this for? */
1913 && when_entered_debugger < num_nonmacro_input_events)
1915 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1916 return 1;
1919 return 0;
1922 static Lisp_Object
1923 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1925 register Lisp_Object h;
1927 /* t is used by handlers for all conditions, set up by C code. */
1928 if (EQ (handlers, Qt))
1929 return Qt;
1931 /* error is used similarly, but means print an error message
1932 and run the debugger if that is enabled. */
1933 if (EQ (handlers, Qerror))
1934 return Qt;
1936 for (h = handlers; CONSP (h); h = XCDR (h))
1938 Lisp_Object handler = XCAR (h);
1939 Lisp_Object condit, tem;
1941 if (!CONSP (handler))
1942 continue;
1943 condit = XCAR (handler);
1944 /* Handle a single condition name in handler HANDLER. */
1945 if (SYMBOLP (condit))
1947 tem = Fmemq (Fcar (handler), conditions);
1948 if (!NILP (tem))
1949 return handler;
1951 /* Handle a list of condition names in handler HANDLER. */
1952 else if (CONSP (condit))
1954 Lisp_Object tail;
1955 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1957 tem = Fmemq (XCAR (tail), conditions);
1958 if (!NILP (tem))
1959 return handler;
1964 return Qnil;
1968 /* Dump an error message; called like vprintf. */
1969 void
1970 verror (const char *m, va_list ap)
1972 char buf[4000];
1973 ptrdiff_t size = sizeof buf;
1974 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1975 char *buffer = buf;
1976 ptrdiff_t used;
1977 Lisp_Object string;
1979 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1980 string = make_string (buffer, used);
1981 if (buffer != buf)
1982 xfree (buffer);
1984 xsignal1 (Qerror, string);
1988 /* Dump an error message; called like printf. */
1990 /* VARARGS 1 */
1991 void
1992 error (const char *m, ...)
1994 va_list ap;
1995 va_start (ap, m);
1996 verror (m, ap);
1997 va_end (ap);
2000 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2001 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2002 This means it contains a description for how to read arguments to give it.
2003 The value is nil for an invalid function or a symbol with no function
2004 definition.
2006 Interactively callable functions include strings and vectors (treated
2007 as keyboard macros), lambda-expressions that contain a top-level call
2008 to `interactive', autoload definitions made by `autoload' with non-nil
2009 fourth argument, and some of the built-in functions of Lisp.
2011 Also, a symbol satisfies `commandp' if its function definition does so.
2013 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2014 then strings and vectors are not accepted. */)
2015 (Lisp_Object function, Lisp_Object for_call_interactively)
2017 register Lisp_Object fun;
2018 register Lisp_Object funcar;
2019 Lisp_Object if_prop = Qnil;
2021 fun = function;
2023 fun = indirect_function (fun); /* Check cycles. */
2024 if (NILP (fun) || EQ (fun, Qunbound))
2025 return Qnil;
2027 /* Check an `interactive-form' property if present, analogous to the
2028 function-documentation property. */
2029 fun = function;
2030 while (SYMBOLP (fun))
2032 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2033 if (!NILP (tmp))
2034 if_prop = Qt;
2035 fun = Fsymbol_function (fun);
2038 /* Emacs primitives are interactive if their DEFUN specifies an
2039 interactive spec. */
2040 if (SUBRP (fun))
2041 return XSUBR (fun)->intspec ? Qt : if_prop;
2043 /* Bytecode objects are interactive if they are long enough to
2044 have an element whose index is COMPILED_INTERACTIVE, which is
2045 where the interactive spec is stored. */
2046 else if (COMPILEDP (fun))
2047 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2048 ? Qt : if_prop);
2050 /* Strings and vectors are keyboard macros. */
2051 if (STRINGP (fun) || VECTORP (fun))
2052 return (NILP (for_call_interactively) ? Qt : Qnil);
2054 /* Lists may represent commands. */
2055 if (!CONSP (fun))
2056 return Qnil;
2057 funcar = XCAR (fun);
2058 if (EQ (funcar, Qclosure))
2059 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
2060 ? Qt : if_prop);
2061 else if (EQ (funcar, Qlambda))
2062 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2063 else if (EQ (funcar, Qautoload))
2064 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2065 else
2066 return Qnil;
2069 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2070 doc: /* Define FUNCTION to autoload from FILE.
2071 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2072 Third arg DOCSTRING is documentation for the function.
2073 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2074 Fifth arg TYPE indicates the type of the object:
2075 nil or omitted says FUNCTION is a function,
2076 `keymap' says FUNCTION is really a keymap, and
2077 `macro' or t says FUNCTION is really a macro.
2078 Third through fifth args give info about the real definition.
2079 They default to nil.
2080 If FUNCTION is already defined other than as an autoload,
2081 this does nothing and returns nil. */)
2082 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2084 CHECK_SYMBOL (function);
2085 CHECK_STRING (file);
2087 /* If function is defined and not as an autoload, don't override. */
2088 if (!EQ (XSYMBOL (function)->function, Qunbound)
2089 && !(CONSP (XSYMBOL (function)->function)
2090 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2091 return Qnil;
2093 if (NILP (Vpurify_flag))
2094 /* Only add entries after dumping, because the ones before are
2095 not useful and else we get loads of them from the loaddefs.el. */
2096 LOADHIST_ATTACH (Fcons (Qautoload, function));
2097 else
2098 /* We don't want the docstring in purespace (instead,
2099 Snarf-documentation should (hopefully) overwrite it).
2100 We used to use 0 here, but that leads to accidental sharing in
2101 purecopy's hash-consing, so we use a (hopefully) unique integer
2102 instead. */
2103 docstring = make_number (XPNTR (function));
2104 return Ffset (function,
2105 Fpurecopy (list5 (Qautoload, file, docstring,
2106 interactive, type)));
2109 Lisp_Object
2110 un_autoload (Lisp_Object oldqueue)
2112 register Lisp_Object queue, first, second;
2114 /* Queue to unwind is current value of Vautoload_queue.
2115 oldqueue is the shadowed value to leave in Vautoload_queue. */
2116 queue = Vautoload_queue;
2117 Vautoload_queue = oldqueue;
2118 while (CONSP (queue))
2120 first = XCAR (queue);
2121 second = Fcdr (first);
2122 first = Fcar (first);
2123 if (EQ (first, make_number (0)))
2124 Vfeatures = second;
2125 else
2126 Ffset (first, second);
2127 queue = XCDR (queue);
2129 return Qnil;
2132 /* Load an autoloaded function.
2133 FUNNAME is the symbol which is the function's name.
2134 FUNDEF is the autoload definition (a list). */
2136 void
2137 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2139 int count = SPECPDL_INDEX ();
2140 Lisp_Object fun;
2141 struct gcpro gcpro1, gcpro2, gcpro3;
2143 /* This is to make sure that loadup.el gives a clear picture
2144 of what files are preloaded and when. */
2145 if (! NILP (Vpurify_flag))
2146 error ("Attempt to autoload %s while preparing to dump",
2147 SDATA (SYMBOL_NAME (funname)));
2149 fun = funname;
2150 CHECK_SYMBOL (funname);
2151 GCPRO3 (fun, funname, fundef);
2153 /* Preserve the match data. */
2154 record_unwind_save_match_data ();
2156 /* If autoloading gets an error (which includes the error of failing
2157 to define the function being called), we use Vautoload_queue
2158 to undo function definitions and `provide' calls made by
2159 the function. We do this in the specific case of autoloading
2160 because autoloading is not an explicit request "load this file",
2161 but rather a request to "call this function".
2163 The value saved here is to be restored into Vautoload_queue. */
2164 record_unwind_protect (un_autoload, Vautoload_queue);
2165 Vautoload_queue = Qt;
2166 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2168 /* Once loading finishes, don't undo it. */
2169 Vautoload_queue = Qt;
2170 unbind_to (count, Qnil);
2172 fun = Findirect_function (fun, Qnil);
2174 if (!NILP (Fequal (fun, fundef)))
2175 error ("Autoloading failed to define function %s",
2176 SDATA (SYMBOL_NAME (funname)));
2177 UNGCPRO;
2181 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2182 doc: /* Evaluate FORM and return its value.
2183 If LEXICAL is t, evaluate using lexical scoping. */)
2184 (Lisp_Object form, Lisp_Object lexical)
2186 int count = SPECPDL_INDEX ();
2187 specbind (Qinternal_interpreter_environment,
2188 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2189 return unbind_to (count, eval_sub (form));
2192 /* Eval a sub-expression of the current expression (i.e. in the same
2193 lexical scope). */
2194 Lisp_Object
2195 eval_sub (Lisp_Object form)
2197 Lisp_Object fun, val, original_fun, original_args;
2198 Lisp_Object funcar;
2199 struct backtrace backtrace;
2200 struct gcpro gcpro1, gcpro2, gcpro3;
2202 if (handling_signal)
2203 abort ();
2205 if (SYMBOLP (form))
2207 /* Look up its binding in the lexical environment.
2208 We do not pay attention to the declared_special flag here, since we
2209 already did that when let-binding the variable. */
2210 Lisp_Object lex_binding
2211 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2212 ? Fassq (form, Vinternal_interpreter_environment)
2213 : Qnil;
2214 if (CONSP (lex_binding))
2215 return XCDR (lex_binding);
2216 else
2217 return Fsymbol_value (form);
2220 if (!CONSP (form))
2221 return form;
2223 QUIT;
2224 if ((consing_since_gc > gc_cons_threshold
2225 && consing_since_gc > gc_relative_threshold)
2227 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2229 GCPRO1 (form);
2230 Fgarbage_collect ();
2231 UNGCPRO;
2234 if (++lisp_eval_depth > max_lisp_eval_depth)
2236 if (max_lisp_eval_depth < 100)
2237 max_lisp_eval_depth = 100;
2238 if (lisp_eval_depth > max_lisp_eval_depth)
2239 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2242 original_fun = Fcar (form);
2243 original_args = Fcdr (form);
2245 backtrace.next = backtrace_list;
2246 backtrace_list = &backtrace;
2247 backtrace.function = &original_fun; /* This also protects them from gc. */
2248 backtrace.args = &original_args;
2249 backtrace.nargs = UNEVALLED;
2250 backtrace.debug_on_exit = 0;
2252 if (debug_on_next_call)
2253 do_debug_on_call (Qt);
2255 /* At this point, only original_fun and original_args
2256 have values that will be used below. */
2257 retry:
2259 /* Optimize for no indirection. */
2260 fun = original_fun;
2261 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2262 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2263 fun = indirect_function (fun);
2265 if (SUBRP (fun))
2267 Lisp_Object numargs;
2268 Lisp_Object argvals[8];
2269 Lisp_Object args_left;
2270 register int i, maxargs;
2272 args_left = original_args;
2273 numargs = Flength (args_left);
2275 CHECK_CONS_LIST ();
2277 if (XINT (numargs) < XSUBR (fun)->min_args
2278 || (XSUBR (fun)->max_args >= 0
2279 && XSUBR (fun)->max_args < XINT (numargs)))
2280 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2282 else if (XSUBR (fun)->max_args == UNEVALLED)
2283 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2284 else if (XSUBR (fun)->max_args == MANY)
2286 /* Pass a vector of evaluated arguments. */
2287 Lisp_Object *vals;
2288 ptrdiff_t argnum = 0;
2289 USE_SAFE_ALLOCA;
2291 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2293 GCPRO3 (args_left, fun, fun);
2294 gcpro3.var = vals;
2295 gcpro3.nvars = 0;
2297 while (!NILP (args_left))
2299 vals[argnum++] = eval_sub (Fcar (args_left));
2300 args_left = Fcdr (args_left);
2301 gcpro3.nvars = argnum;
2304 backtrace.args = vals;
2305 backtrace.nargs = XINT (numargs);
2307 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2308 UNGCPRO;
2309 SAFE_FREE ();
2311 else
2313 GCPRO3 (args_left, fun, fun);
2314 gcpro3.var = argvals;
2315 gcpro3.nvars = 0;
2317 maxargs = XSUBR (fun)->max_args;
2318 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2320 argvals[i] = eval_sub (Fcar (args_left));
2321 gcpro3.nvars = ++i;
2324 UNGCPRO;
2326 backtrace.args = argvals;
2327 backtrace.nargs = XINT (numargs);
2329 switch (i)
2331 case 0:
2332 val = (XSUBR (fun)->function.a0 ());
2333 break;
2334 case 1:
2335 val = (XSUBR (fun)->function.a1 (argvals[0]));
2336 break;
2337 case 2:
2338 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2339 break;
2340 case 3:
2341 val = (XSUBR (fun)->function.a3
2342 (argvals[0], argvals[1], argvals[2]));
2343 break;
2344 case 4:
2345 val = (XSUBR (fun)->function.a4
2346 (argvals[0], argvals[1], argvals[2], argvals[3]));
2347 break;
2348 case 5:
2349 val = (XSUBR (fun)->function.a5
2350 (argvals[0], argvals[1], argvals[2], argvals[3],
2351 argvals[4]));
2352 break;
2353 case 6:
2354 val = (XSUBR (fun)->function.a6
2355 (argvals[0], argvals[1], argvals[2], argvals[3],
2356 argvals[4], argvals[5]));
2357 break;
2358 case 7:
2359 val = (XSUBR (fun)->function.a7
2360 (argvals[0], argvals[1], argvals[2], argvals[3],
2361 argvals[4], argvals[5], argvals[6]));
2362 break;
2364 case 8:
2365 val = (XSUBR (fun)->function.a8
2366 (argvals[0], argvals[1], argvals[2], argvals[3],
2367 argvals[4], argvals[5], argvals[6], argvals[7]));
2368 break;
2370 default:
2371 /* Someone has created a subr that takes more arguments than
2372 is supported by this code. We need to either rewrite the
2373 subr to use a different argument protocol, or add more
2374 cases to this switch. */
2375 abort ();
2379 else if (COMPILEDP (fun))
2380 val = apply_lambda (fun, original_args);
2381 else
2383 if (EQ (fun, Qunbound))
2384 xsignal1 (Qvoid_function, original_fun);
2385 if (!CONSP (fun))
2386 xsignal1 (Qinvalid_function, original_fun);
2387 funcar = XCAR (fun);
2388 if (!SYMBOLP (funcar))
2389 xsignal1 (Qinvalid_function, original_fun);
2390 if (EQ (funcar, Qautoload))
2392 do_autoload (fun, original_fun);
2393 goto retry;
2395 if (EQ (funcar, Qmacro))
2396 val = eval_sub (apply1 (Fcdr (fun), original_args));
2397 else if (EQ (funcar, Qlambda)
2398 || EQ (funcar, Qclosure))
2399 val = apply_lambda (fun, original_args);
2400 else
2401 xsignal1 (Qinvalid_function, original_fun);
2403 CHECK_CONS_LIST ();
2405 lisp_eval_depth--;
2406 if (backtrace.debug_on_exit)
2407 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2408 backtrace_list = backtrace.next;
2410 return val;
2413 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2414 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2415 Then return the value FUNCTION returns.
2416 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2417 usage: (apply FUNCTION &rest ARGUMENTS) */)
2418 (ptrdiff_t nargs, Lisp_Object *args)
2420 ptrdiff_t i, numargs;
2421 register Lisp_Object spread_arg;
2422 register Lisp_Object *funcall_args;
2423 Lisp_Object fun, retval;
2424 struct gcpro gcpro1;
2425 USE_SAFE_ALLOCA;
2427 fun = args [0];
2428 funcall_args = 0;
2429 spread_arg = args [nargs - 1];
2430 CHECK_LIST (spread_arg);
2432 numargs = XINT (Flength (spread_arg));
2434 if (numargs == 0)
2435 return Ffuncall (nargs - 1, args);
2436 else if (numargs == 1)
2438 args [nargs - 1] = XCAR (spread_arg);
2439 return Ffuncall (nargs, args);
2442 numargs += nargs - 2;
2444 /* Optimize for no indirection. */
2445 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2446 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2447 fun = indirect_function (fun);
2448 if (EQ (fun, Qunbound))
2450 /* Let funcall get the error. */
2451 fun = args[0];
2452 goto funcall;
2455 if (SUBRP (fun))
2457 if (numargs < XSUBR (fun)->min_args
2458 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2459 goto funcall; /* Let funcall get the error. */
2460 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2462 /* Avoid making funcall cons up a yet another new vector of arguments
2463 by explicitly supplying nil's for optional values. */
2464 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2465 for (i = numargs; i < XSUBR (fun)->max_args;)
2466 funcall_args[++i] = Qnil;
2467 GCPRO1 (*funcall_args);
2468 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2471 funcall:
2472 /* We add 1 to numargs because funcall_args includes the
2473 function itself as well as its arguments. */
2474 if (!funcall_args)
2476 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2477 GCPRO1 (*funcall_args);
2478 gcpro1.nvars = 1 + numargs;
2481 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2482 /* Spread the last arg we got. Its first element goes in
2483 the slot that it used to occupy, hence this value of I. */
2484 i = nargs - 1;
2485 while (!NILP (spread_arg))
2487 funcall_args [i++] = XCAR (spread_arg);
2488 spread_arg = XCDR (spread_arg);
2491 /* By convention, the caller needs to gcpro Ffuncall's args. */
2492 retval = Ffuncall (gcpro1.nvars, funcall_args);
2493 UNGCPRO;
2494 SAFE_FREE ();
2496 return retval;
2499 /* Run hook variables in various ways. */
2501 static Lisp_Object
2502 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2504 Ffuncall (nargs, args);
2505 return Qnil;
2508 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2509 doc: /* Run each hook in HOOKS.
2510 Each argument should be a symbol, a hook variable.
2511 These symbols are processed in the order specified.
2512 If a hook symbol has a non-nil value, that value may be a function
2513 or a list of functions to be called to run the hook.
2514 If the value is a function, it is called with no arguments.
2515 If it is a list, the elements are called, in order, with no arguments.
2517 Major modes should not use this function directly to run their mode
2518 hook; they should use `run-mode-hooks' instead.
2520 Do not use `make-local-variable' to make a hook variable buffer-local.
2521 Instead, use `add-hook' and specify t for the LOCAL argument.
2522 usage: (run-hooks &rest HOOKS) */)
2523 (ptrdiff_t nargs, Lisp_Object *args)
2525 Lisp_Object hook[1];
2526 ptrdiff_t i;
2528 for (i = 0; i < nargs; i++)
2530 hook[0] = args[i];
2531 run_hook_with_args (1, hook, funcall_nil);
2534 return Qnil;
2537 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2538 Srun_hook_with_args, 1, MANY, 0,
2539 doc: /* Run HOOK with the specified arguments ARGS.
2540 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2541 value, that value may be a function or a list of functions to be
2542 called to run the hook. If the value is a function, it is called with
2543 the given arguments and its return value is returned. If it is a list
2544 of functions, those functions are called, in order,
2545 with the given arguments ARGS.
2546 It is best not to depend on the value returned by `run-hook-with-args',
2547 as that may change.
2549 Do not use `make-local-variable' to make a hook variable buffer-local.
2550 Instead, use `add-hook' and specify t for the LOCAL argument.
2551 usage: (run-hook-with-args HOOK &rest ARGS) */)
2552 (ptrdiff_t nargs, Lisp_Object *args)
2554 return run_hook_with_args (nargs, args, funcall_nil);
2557 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2558 Srun_hook_with_args_until_success, 1, MANY, 0,
2559 doc: /* Run HOOK with the specified arguments ARGS.
2560 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2561 value, that value may be a function or a list of functions to be
2562 called to run the hook. If the value is a function, it is called with
2563 the given arguments and its return value is returned.
2564 If it is a list of functions, those functions are called, in order,
2565 with the given arguments ARGS, until one of them
2566 returns a non-nil value. Then we return that value.
2567 However, if they all return nil, we return nil.
2569 Do not use `make-local-variable' to make a hook variable buffer-local.
2570 Instead, use `add-hook' and specify t for the LOCAL argument.
2571 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2572 (ptrdiff_t nargs, Lisp_Object *args)
2574 return run_hook_with_args (nargs, args, Ffuncall);
2577 static Lisp_Object
2578 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2580 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2583 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2584 Srun_hook_with_args_until_failure, 1, MANY, 0,
2585 doc: /* Run HOOK with the specified arguments ARGS.
2586 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2587 value, that value may be a function or a list of functions to be
2588 called to run the hook. If the value is a function, it is called with
2589 the given arguments and its return value is returned.
2590 If it is a list of functions, those functions are called, in order,
2591 with the given arguments ARGS, until one of them returns nil.
2592 Then we return nil. However, if they all return non-nil, we return non-nil.
2594 Do not use `make-local-variable' to make a hook variable buffer-local.
2595 Instead, use `add-hook' and specify t for the LOCAL argument.
2596 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2597 (ptrdiff_t nargs, Lisp_Object *args)
2599 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2602 static Lisp_Object
2603 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2605 Lisp_Object tmp = args[0], ret;
2606 args[0] = args[1];
2607 args[1] = tmp;
2608 ret = Ffuncall (nargs, args);
2609 args[1] = args[0];
2610 args[0] = tmp;
2611 return ret;
2614 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2615 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2616 I.e. instead of calling each function FUN directly with arguments ARGS,
2617 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2618 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2619 aborts and returns that value.
2620 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2621 (ptrdiff_t nargs, Lisp_Object *args)
2623 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2626 /* ARGS[0] should be a hook symbol.
2627 Call each of the functions in the hook value, passing each of them
2628 as arguments all the rest of ARGS (all NARGS - 1 elements).
2629 FUNCALL specifies how to call each function on the hook.
2630 The caller (or its caller, etc) must gcpro all of ARGS,
2631 except that it isn't necessary to gcpro ARGS[0]. */
2633 Lisp_Object
2634 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2635 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2637 Lisp_Object sym, val, ret = Qnil;
2638 struct gcpro gcpro1, gcpro2, gcpro3;
2640 /* If we are dying or still initializing,
2641 don't do anything--it would probably crash if we tried. */
2642 if (NILP (Vrun_hooks))
2643 return Qnil;
2645 sym = args[0];
2646 val = find_symbol_value (sym);
2648 if (EQ (val, Qunbound) || NILP (val))
2649 return ret;
2650 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2652 args[0] = val;
2653 return funcall (nargs, args);
2655 else
2657 Lisp_Object global_vals = Qnil;
2658 GCPRO3 (sym, val, global_vals);
2660 for (;
2661 CONSP (val) && NILP (ret);
2662 val = XCDR (val))
2664 if (EQ (XCAR (val), Qt))
2666 /* t indicates this hook has a local binding;
2667 it means to run the global binding too. */
2668 global_vals = Fdefault_value (sym);
2669 if (NILP (global_vals)) continue;
2671 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2673 args[0] = global_vals;
2674 ret = funcall (nargs, args);
2676 else
2678 for (;
2679 CONSP (global_vals) && NILP (ret);
2680 global_vals = XCDR (global_vals))
2682 args[0] = XCAR (global_vals);
2683 /* In a global value, t should not occur. If it does, we
2684 must ignore it to avoid an endless loop. */
2685 if (!EQ (args[0], Qt))
2686 ret = funcall (nargs, args);
2690 else
2692 args[0] = XCAR (val);
2693 ret = funcall (nargs, args);
2697 UNGCPRO;
2698 return ret;
2702 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2704 void
2705 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2707 Lisp_Object temp[3];
2708 temp[0] = hook;
2709 temp[1] = arg1;
2710 temp[2] = arg2;
2712 Frun_hook_with_args (3, temp);
2715 /* Apply fn to arg. */
2716 Lisp_Object
2717 apply1 (Lisp_Object fn, Lisp_Object arg)
2719 struct gcpro gcpro1;
2721 GCPRO1 (fn);
2722 if (NILP (arg))
2723 RETURN_UNGCPRO (Ffuncall (1, &fn));
2724 gcpro1.nvars = 2;
2726 Lisp_Object args[2];
2727 args[0] = fn;
2728 args[1] = arg;
2729 gcpro1.var = args;
2730 RETURN_UNGCPRO (Fapply (2, args));
2734 /* Call function fn on no arguments. */
2735 Lisp_Object
2736 call0 (Lisp_Object fn)
2738 struct gcpro gcpro1;
2740 GCPRO1 (fn);
2741 RETURN_UNGCPRO (Ffuncall (1, &fn));
2744 /* Call function fn with 1 argument arg1. */
2745 /* ARGSUSED */
2746 Lisp_Object
2747 call1 (Lisp_Object fn, Lisp_Object arg1)
2749 struct gcpro gcpro1;
2750 Lisp_Object args[2];
2752 args[0] = fn;
2753 args[1] = arg1;
2754 GCPRO1 (args[0]);
2755 gcpro1.nvars = 2;
2756 RETURN_UNGCPRO (Ffuncall (2, args));
2759 /* Call function fn with 2 arguments arg1, arg2. */
2760 /* ARGSUSED */
2761 Lisp_Object
2762 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2764 struct gcpro gcpro1;
2765 Lisp_Object args[3];
2766 args[0] = fn;
2767 args[1] = arg1;
2768 args[2] = arg2;
2769 GCPRO1 (args[0]);
2770 gcpro1.nvars = 3;
2771 RETURN_UNGCPRO (Ffuncall (3, args));
2774 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2775 /* ARGSUSED */
2776 Lisp_Object
2777 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2779 struct gcpro gcpro1;
2780 Lisp_Object args[4];
2781 args[0] = fn;
2782 args[1] = arg1;
2783 args[2] = arg2;
2784 args[3] = arg3;
2785 GCPRO1 (args[0]);
2786 gcpro1.nvars = 4;
2787 RETURN_UNGCPRO (Ffuncall (4, args));
2790 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2791 /* ARGSUSED */
2792 Lisp_Object
2793 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2794 Lisp_Object arg4)
2796 struct gcpro gcpro1;
2797 Lisp_Object args[5];
2798 args[0] = fn;
2799 args[1] = arg1;
2800 args[2] = arg2;
2801 args[3] = arg3;
2802 args[4] = arg4;
2803 GCPRO1 (args[0]);
2804 gcpro1.nvars = 5;
2805 RETURN_UNGCPRO (Ffuncall (5, args));
2808 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2809 /* ARGSUSED */
2810 Lisp_Object
2811 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2812 Lisp_Object arg4, Lisp_Object arg5)
2814 struct gcpro gcpro1;
2815 Lisp_Object args[6];
2816 args[0] = fn;
2817 args[1] = arg1;
2818 args[2] = arg2;
2819 args[3] = arg3;
2820 args[4] = arg4;
2821 args[5] = arg5;
2822 GCPRO1 (args[0]);
2823 gcpro1.nvars = 6;
2824 RETURN_UNGCPRO (Ffuncall (6, args));
2827 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2828 /* ARGSUSED */
2829 Lisp_Object
2830 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2831 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2833 struct gcpro gcpro1;
2834 Lisp_Object args[7];
2835 args[0] = fn;
2836 args[1] = arg1;
2837 args[2] = arg2;
2838 args[3] = arg3;
2839 args[4] = arg4;
2840 args[5] = arg5;
2841 args[6] = arg6;
2842 GCPRO1 (args[0]);
2843 gcpro1.nvars = 7;
2844 RETURN_UNGCPRO (Ffuncall (7, args));
2847 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2848 /* ARGSUSED */
2849 Lisp_Object
2850 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2851 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2853 struct gcpro gcpro1;
2854 Lisp_Object args[8];
2855 args[0] = fn;
2856 args[1] = arg1;
2857 args[2] = arg2;
2858 args[3] = arg3;
2859 args[4] = arg4;
2860 args[5] = arg5;
2861 args[6] = arg6;
2862 args[7] = arg7;
2863 GCPRO1 (args[0]);
2864 gcpro1.nvars = 8;
2865 RETURN_UNGCPRO (Ffuncall (8, args));
2868 /* The caller should GCPRO all the elements of ARGS. */
2870 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2871 doc: /* Non-nil if OBJECT is a function. */)
2872 (Lisp_Object object)
2874 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2876 object = Findirect_function (object, Qt);
2878 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2880 /* Autoloaded symbols are functions, except if they load
2881 macros or keymaps. */
2882 int i;
2883 for (i = 0; i < 4 && CONSP (object); i++)
2884 object = XCDR (object);
2886 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2890 if (SUBRP (object))
2891 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2892 else if (COMPILEDP (object))
2893 return Qt;
2894 else if (CONSP (object))
2896 Lisp_Object car = XCAR (object);
2897 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2899 else
2900 return Qnil;
2903 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2904 doc: /* Call first argument as a function, passing remaining arguments to it.
2905 Return the value that function returns.
2906 Thus, (funcall 'cons 'x 'y) returns (x . y).
2907 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2908 (ptrdiff_t nargs, Lisp_Object *args)
2910 Lisp_Object fun, original_fun;
2911 Lisp_Object funcar;
2912 ptrdiff_t numargs = nargs - 1;
2913 Lisp_Object lisp_numargs;
2914 Lisp_Object val;
2915 struct backtrace backtrace;
2916 register Lisp_Object *internal_args;
2917 ptrdiff_t i;
2919 QUIT;
2920 if ((consing_since_gc > gc_cons_threshold
2921 && consing_since_gc > gc_relative_threshold)
2923 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2924 Fgarbage_collect ();
2926 if (++lisp_eval_depth > max_lisp_eval_depth)
2928 if (max_lisp_eval_depth < 100)
2929 max_lisp_eval_depth = 100;
2930 if (lisp_eval_depth > max_lisp_eval_depth)
2931 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2934 backtrace.next = backtrace_list;
2935 backtrace_list = &backtrace;
2936 backtrace.function = &args[0];
2937 backtrace.args = &args[1];
2938 backtrace.nargs = nargs - 1;
2939 backtrace.debug_on_exit = 0;
2941 if (debug_on_next_call)
2942 do_debug_on_call (Qlambda);
2944 CHECK_CONS_LIST ();
2946 original_fun = args[0];
2948 retry:
2950 /* Optimize for no indirection. */
2951 fun = original_fun;
2952 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2953 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2954 fun = indirect_function (fun);
2956 if (SUBRP (fun))
2958 if (numargs < XSUBR (fun)->min_args
2959 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2961 XSETFASTINT (lisp_numargs, numargs);
2962 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2965 else if (XSUBR (fun)->max_args == UNEVALLED)
2966 xsignal1 (Qinvalid_function, original_fun);
2968 else if (XSUBR (fun)->max_args == MANY)
2969 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2970 else
2972 if (XSUBR (fun)->max_args > numargs)
2974 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2975 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
2976 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2977 internal_args[i] = Qnil;
2979 else
2980 internal_args = args + 1;
2981 switch (XSUBR (fun)->max_args)
2983 case 0:
2984 val = (XSUBR (fun)->function.a0 ());
2985 break;
2986 case 1:
2987 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2988 break;
2989 case 2:
2990 val = (XSUBR (fun)->function.a2
2991 (internal_args[0], internal_args[1]));
2992 break;
2993 case 3:
2994 val = (XSUBR (fun)->function.a3
2995 (internal_args[0], internal_args[1], internal_args[2]));
2996 break;
2997 case 4:
2998 val = (XSUBR (fun)->function.a4
2999 (internal_args[0], internal_args[1], internal_args[2],
3000 internal_args[3]));
3001 break;
3002 case 5:
3003 val = (XSUBR (fun)->function.a5
3004 (internal_args[0], internal_args[1], internal_args[2],
3005 internal_args[3], internal_args[4]));
3006 break;
3007 case 6:
3008 val = (XSUBR (fun)->function.a6
3009 (internal_args[0], internal_args[1], internal_args[2],
3010 internal_args[3], internal_args[4], internal_args[5]));
3011 break;
3012 case 7:
3013 val = (XSUBR (fun)->function.a7
3014 (internal_args[0], internal_args[1], internal_args[2],
3015 internal_args[3], internal_args[4], internal_args[5],
3016 internal_args[6]));
3017 break;
3019 case 8:
3020 val = (XSUBR (fun)->function.a8
3021 (internal_args[0], internal_args[1], internal_args[2],
3022 internal_args[3], internal_args[4], internal_args[5],
3023 internal_args[6], internal_args[7]));
3024 break;
3026 default:
3028 /* If a subr takes more than 8 arguments without using MANY
3029 or UNEVALLED, we need to extend this function to support it.
3030 Until this is done, there is no way to call the function. */
3031 abort ();
3035 else if (COMPILEDP (fun))
3036 val = funcall_lambda (fun, numargs, args + 1);
3037 else
3039 if (EQ (fun, Qunbound))
3040 xsignal1 (Qvoid_function, original_fun);
3041 if (!CONSP (fun))
3042 xsignal1 (Qinvalid_function, original_fun);
3043 funcar = XCAR (fun);
3044 if (!SYMBOLP (funcar))
3045 xsignal1 (Qinvalid_function, original_fun);
3046 if (EQ (funcar, Qlambda)
3047 || EQ (funcar, Qclosure))
3048 val = funcall_lambda (fun, numargs, args + 1);
3049 else if (EQ (funcar, Qautoload))
3051 do_autoload (fun, original_fun);
3052 CHECK_CONS_LIST ();
3053 goto retry;
3055 else
3056 xsignal1 (Qinvalid_function, original_fun);
3058 CHECK_CONS_LIST ();
3059 lisp_eval_depth--;
3060 if (backtrace.debug_on_exit)
3061 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3062 backtrace_list = backtrace.next;
3063 return val;
3066 static Lisp_Object
3067 apply_lambda (Lisp_Object fun, Lisp_Object args)
3069 Lisp_Object args_left;
3070 ptrdiff_t i, numargs;
3071 register Lisp_Object *arg_vector;
3072 struct gcpro gcpro1, gcpro2, gcpro3;
3073 register Lisp_Object tem;
3074 USE_SAFE_ALLOCA;
3076 numargs = XFASTINT (Flength (args));
3077 SAFE_ALLOCA_LISP (arg_vector, numargs);
3078 args_left = args;
3080 GCPRO3 (*arg_vector, args_left, fun);
3081 gcpro1.nvars = 0;
3083 for (i = 0; i < numargs; )
3085 tem = Fcar (args_left), args_left = Fcdr (args_left);
3086 tem = eval_sub (tem);
3087 arg_vector[i++] = tem;
3088 gcpro1.nvars = i;
3091 UNGCPRO;
3093 backtrace_list->args = arg_vector;
3094 backtrace_list->nargs = i;
3095 tem = funcall_lambda (fun, numargs, arg_vector);
3097 /* Do the debug-on-exit now, while arg_vector still exists. */
3098 if (backtrace_list->debug_on_exit)
3099 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3100 /* Don't do it again when we return to eval. */
3101 backtrace_list->debug_on_exit = 0;
3102 SAFE_FREE ();
3103 return tem;
3106 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3107 and return the result of evaluation.
3108 FUN must be either a lambda-expression or a compiled-code object. */
3110 static Lisp_Object
3111 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
3112 register Lisp_Object *arg_vector)
3114 Lisp_Object val, syms_left, next, lexenv;
3115 int count = SPECPDL_INDEX ();
3116 ptrdiff_t i;
3117 int optional, rest;
3119 if (CONSP (fun))
3121 if (EQ (XCAR (fun), Qclosure))
3123 fun = XCDR (fun); /* Drop `closure'. */
3124 lexenv = XCAR (fun);
3125 CHECK_LIST_CONS (fun, fun);
3127 else
3128 lexenv = Qnil;
3129 syms_left = XCDR (fun);
3130 if (CONSP (syms_left))
3131 syms_left = XCAR (syms_left);
3132 else
3133 xsignal1 (Qinvalid_function, fun);
3135 else if (COMPILEDP (fun))
3137 syms_left = AREF (fun, COMPILED_ARGLIST);
3138 if (INTEGERP (syms_left))
3139 /* A byte-code object with a non-nil `push args' slot means we
3140 shouldn't bind any arguments, instead just call the byte-code
3141 interpreter directly; it will push arguments as necessary.
3143 Byte-code objects with either a non-existent, or a nil value for
3144 the `push args' slot (the default), have dynamically-bound
3145 arguments, and use the argument-binding code below instead (as do
3146 all interpreted functions, even lexically bound ones). */
3148 /* If we have not actually read the bytecode string
3149 and constants vector yet, fetch them from the file. */
3150 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3151 Ffetch_bytecode (fun);
3152 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3153 AREF (fun, COMPILED_CONSTANTS),
3154 AREF (fun, COMPILED_STACK_DEPTH),
3155 syms_left,
3156 nargs, arg_vector);
3158 lexenv = Qnil;
3160 else
3161 abort ();
3163 i = optional = rest = 0;
3164 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3166 QUIT;
3168 next = XCAR (syms_left);
3169 if (!SYMBOLP (next))
3170 xsignal1 (Qinvalid_function, fun);
3172 if (EQ (next, Qand_rest))
3173 rest = 1;
3174 else if (EQ (next, Qand_optional))
3175 optional = 1;
3176 else
3178 Lisp_Object arg;
3179 if (rest)
3181 arg = Flist (nargs - i, &arg_vector[i]);
3182 i = nargs;
3184 else if (i < nargs)
3185 arg = arg_vector[i++];
3186 else if (!optional)
3187 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3188 else
3189 arg = Qnil;
3191 /* Bind the argument. */
3192 if (!NILP (lexenv) && SYMBOLP (next))
3193 /* Lexically bind NEXT by adding it to the lexenv alist. */
3194 lexenv = Fcons (Fcons (next, arg), lexenv);
3195 else
3196 /* Dynamically bind NEXT. */
3197 specbind (next, arg);
3201 if (!NILP (syms_left))
3202 xsignal1 (Qinvalid_function, fun);
3203 else if (i < nargs)
3204 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3206 if (!EQ (lexenv, Vinternal_interpreter_environment))
3207 /* Instantiate a new lexical environment. */
3208 specbind (Qinternal_interpreter_environment, lexenv);
3210 if (CONSP (fun))
3211 val = Fprogn (XCDR (XCDR (fun)));
3212 else
3214 /* If we have not actually read the bytecode string
3215 and constants vector yet, fetch them from the file. */
3216 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3217 Ffetch_bytecode (fun);
3218 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3219 AREF (fun, COMPILED_CONSTANTS),
3220 AREF (fun, COMPILED_STACK_DEPTH),
3221 Qnil, 0, 0);
3224 return unbind_to (count, val);
3227 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3228 1, 1, 0,
3229 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3230 (Lisp_Object object)
3232 Lisp_Object tem;
3234 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3236 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3237 if (!CONSP (tem))
3239 tem = AREF (object, COMPILED_BYTECODE);
3240 if (CONSP (tem) && STRINGP (XCAR (tem)))
3241 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3242 else
3243 error ("Invalid byte code");
3245 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3246 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3248 return object;
3251 static void
3252 grow_specpdl (void)
3254 register int count = SPECPDL_INDEX ();
3255 int max_size =
3256 min (max_specpdl_size,
3257 min (max (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct specbinding),
3258 INT_MAX));
3259 int size;
3260 if (max_size <= specpdl_size)
3262 if (max_specpdl_size < 400)
3263 max_size = max_specpdl_size = 400;
3264 if (max_size <= specpdl_size)
3265 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3267 size = specpdl_size < max_size / 2 ? 2 * specpdl_size : max_size;
3268 specpdl = xnrealloc (specpdl, size, sizeof *specpdl);
3269 specpdl_size = size;
3270 specpdl_ptr = specpdl + count;
3273 /* `specpdl_ptr->symbol' is a field which describes which variable is
3274 let-bound, so it can be properly undone when we unbind_to.
3275 It can have the following two shapes:
3276 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3277 a symbol that is not buffer-local (at least at the time
3278 the let binding started). Note also that it should not be
3279 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3280 to record V2 here).
3281 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3282 variable SYMBOL which can be buffer-local. WHERE tells us
3283 which buffer is affected (or nil if the let-binding affects the
3284 global value of the variable) and BUFFER tells us which buffer was
3285 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3286 BUFFER did not yet have a buffer-local value). */
3288 void
3289 specbind (Lisp_Object symbol, Lisp_Object value)
3291 struct Lisp_Symbol *sym;
3293 eassert (!handling_signal);
3295 CHECK_SYMBOL (symbol);
3296 sym = XSYMBOL (symbol);
3297 if (specpdl_ptr == specpdl + specpdl_size)
3298 grow_specpdl ();
3300 start:
3301 switch (sym->redirect)
3303 case SYMBOL_VARALIAS:
3304 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3305 case SYMBOL_PLAINVAL:
3306 /* The most common case is that of a non-constant symbol with a
3307 trivial value. Make that as fast as we can. */
3308 specpdl_ptr->symbol = symbol;
3309 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3310 specpdl_ptr->func = NULL;
3311 ++specpdl_ptr;
3312 if (!sym->constant)
3313 SET_SYMBOL_VAL (sym, value);
3314 else
3315 set_internal (symbol, value, Qnil, 1);
3316 break;
3317 case SYMBOL_LOCALIZED:
3318 if (SYMBOL_BLV (sym)->frame_local)
3319 error ("Frame-local vars cannot be let-bound");
3320 case SYMBOL_FORWARDED:
3322 Lisp_Object ovalue = find_symbol_value (symbol);
3323 specpdl_ptr->func = 0;
3324 specpdl_ptr->old_value = ovalue;
3326 eassert (sym->redirect != SYMBOL_LOCALIZED
3327 || (EQ (SYMBOL_BLV (sym)->where,
3328 SYMBOL_BLV (sym)->frame_local ?
3329 Fselected_frame () : Fcurrent_buffer ())));
3331 if (sym->redirect == SYMBOL_LOCALIZED
3332 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3334 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3336 /* For a local variable, record both the symbol and which
3337 buffer's or frame's value we are saving. */
3338 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3340 eassert (sym->redirect != SYMBOL_LOCALIZED
3341 || (BLV_FOUND (SYMBOL_BLV (sym))
3342 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3343 where = cur_buf;
3345 else if (sym->redirect == SYMBOL_LOCALIZED
3346 && BLV_FOUND (SYMBOL_BLV (sym)))
3347 where = SYMBOL_BLV (sym)->where;
3348 else
3349 where = Qnil;
3351 /* We're not using the `unused' slot in the specbinding
3352 structure because this would mean we have to do more
3353 work for simple variables. */
3354 /* FIXME: The third value `current_buffer' is only used in
3355 let_shadows_buffer_binding_p which is itself only used
3356 in set_internal for local_if_set. */
3357 eassert (NILP (where) || EQ (where, cur_buf));
3358 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3360 /* If SYMBOL is a per-buffer variable which doesn't have a
3361 buffer-local value here, make the `let' change the global
3362 value by changing the value of SYMBOL in all buffers not
3363 having their own value. This is consistent with what
3364 happens with other buffer-local variables. */
3365 if (NILP (where)
3366 && sym->redirect == SYMBOL_FORWARDED)
3368 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3369 ++specpdl_ptr;
3370 Fset_default (symbol, value);
3371 return;
3374 else
3375 specpdl_ptr->symbol = symbol;
3377 specpdl_ptr++;
3378 set_internal (symbol, value, Qnil, 1);
3379 break;
3381 default: abort ();
3385 void
3386 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3388 eassert (!handling_signal);
3390 if (specpdl_ptr == specpdl + specpdl_size)
3391 grow_specpdl ();
3392 specpdl_ptr->func = function;
3393 specpdl_ptr->symbol = Qnil;
3394 specpdl_ptr->old_value = arg;
3395 specpdl_ptr++;
3398 Lisp_Object
3399 unbind_to (int count, Lisp_Object value)
3401 Lisp_Object quitf = Vquit_flag;
3402 struct gcpro gcpro1, gcpro2;
3404 GCPRO2 (value, quitf);
3405 Vquit_flag = Qnil;
3407 while (specpdl_ptr != specpdl + count)
3409 /* Copy the binding, and decrement specpdl_ptr, before we do
3410 the work to unbind it. We decrement first
3411 so that an error in unbinding won't try to unbind
3412 the same entry again, and we copy the binding first
3413 in case more bindings are made during some of the code we run. */
3415 struct specbinding this_binding;
3416 this_binding = *--specpdl_ptr;
3418 if (this_binding.func != 0)
3419 (*this_binding.func) (this_binding.old_value);
3420 /* If the symbol is a list, it is really (SYMBOL WHERE
3421 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3422 frame. If WHERE is a buffer or frame, this indicates we
3423 bound a variable that had a buffer-local or frame-local
3424 binding. WHERE nil means that the variable had the default
3425 value when it was bound. CURRENT-BUFFER is the buffer that
3426 was current when the variable was bound. */
3427 else if (CONSP (this_binding.symbol))
3429 Lisp_Object symbol, where;
3431 symbol = XCAR (this_binding.symbol);
3432 where = XCAR (XCDR (this_binding.symbol));
3434 if (NILP (where))
3435 Fset_default (symbol, this_binding.old_value);
3436 /* If `where' is non-nil, reset the value in the appropriate
3437 local binding, but only if that binding still exists. */
3438 else if (BUFFERP (where)
3439 ? !NILP (Flocal_variable_p (symbol, where))
3440 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3441 set_internal (symbol, this_binding.old_value, where, 1);
3443 /* If variable has a trivial value (no forwarding), we can
3444 just set it. No need to check for constant symbols here,
3445 since that was already done by specbind. */
3446 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3447 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3448 this_binding.old_value);
3449 else
3450 /* NOTE: we only ever come here if make_local_foo was used for
3451 the first time on this var within this let. */
3452 Fset_default (this_binding.symbol, this_binding.old_value);
3455 if (NILP (Vquit_flag) && !NILP (quitf))
3456 Vquit_flag = quitf;
3458 UNGCPRO;
3459 return value;
3462 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3463 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3464 A special variable is one that will be bound dynamically, even in a
3465 context where binding is lexical by default. */)
3466 (Lisp_Object symbol)
3468 CHECK_SYMBOL (symbol);
3469 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3473 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3474 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3475 The debugger is entered when that frame exits, if the flag is non-nil. */)
3476 (Lisp_Object level, Lisp_Object flag)
3478 register struct backtrace *backlist = backtrace_list;
3479 register int i;
3481 CHECK_NUMBER (level);
3483 for (i = 0; backlist && i < XINT (level); i++)
3485 backlist = backlist->next;
3488 if (backlist)
3489 backlist->debug_on_exit = !NILP (flag);
3491 return flag;
3494 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3495 doc: /* Print a trace of Lisp function calls currently active.
3496 Output stream used is value of `standard-output'. */)
3497 (void)
3499 register struct backtrace *backlist = backtrace_list;
3500 Lisp_Object tail;
3501 Lisp_Object tem;
3502 struct gcpro gcpro1;
3503 Lisp_Object old_print_level = Vprint_level;
3505 if (NILP (Vprint_level))
3506 XSETFASTINT (Vprint_level, 8);
3508 tail = Qnil;
3509 GCPRO1 (tail);
3511 while (backlist)
3513 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3514 if (backlist->nargs == UNEVALLED)
3516 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3517 write_string ("\n", -1);
3519 else
3521 tem = *backlist->function;
3522 Fprin1 (tem, Qnil); /* This can QUIT. */
3523 write_string ("(", -1);
3524 if (backlist->nargs == MANY)
3525 { /* FIXME: Can this happen? */
3526 int i;
3527 for (tail = *backlist->args, i = 0;
3528 !NILP (tail);
3529 tail = Fcdr (tail), i = 1)
3531 if (i) write_string (" ", -1);
3532 Fprin1 (Fcar (tail), Qnil);
3535 else
3537 ptrdiff_t i;
3538 for (i = 0; i < backlist->nargs; i++)
3540 if (i) write_string (" ", -1);
3541 Fprin1 (backlist->args[i], Qnil);
3544 write_string (")\n", -1);
3546 backlist = backlist->next;
3549 Vprint_level = old_print_level;
3550 UNGCPRO;
3551 return Qnil;
3554 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3555 doc: /* Return the function and arguments NFRAMES up from current execution point.
3556 If that frame has not evaluated the arguments yet (or is a special form),
3557 the value is (nil FUNCTION ARG-FORMS...).
3558 If that frame has evaluated its arguments and called its function already,
3559 the value is (t FUNCTION ARG-VALUES...).
3560 A &rest arg is represented as the tail of the list ARG-VALUES.
3561 FUNCTION is whatever was supplied as car of evaluated list,
3562 or a lambda expression for macro calls.
3563 If NFRAMES is more than the number of frames, the value is nil. */)
3564 (Lisp_Object nframes)
3566 register struct backtrace *backlist = backtrace_list;
3567 register EMACS_INT i;
3568 Lisp_Object tem;
3570 CHECK_NATNUM (nframes);
3572 /* Find the frame requested. */
3573 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3574 backlist = backlist->next;
3576 if (!backlist)
3577 return Qnil;
3578 if (backlist->nargs == UNEVALLED)
3579 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3580 else
3582 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3583 tem = *backlist->args;
3584 else
3585 tem = Flist (backlist->nargs, backlist->args);
3587 return Fcons (Qt, Fcons (*backlist->function, tem));
3592 #if BYTE_MARK_STACK
3593 void
3594 mark_backtrace (void)
3596 register struct backtrace *backlist;
3597 ptrdiff_t i;
3599 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3601 mark_object (*backlist->function);
3603 if (backlist->nargs == UNEVALLED
3604 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3605 i = 1;
3606 else
3607 i = backlist->nargs;
3608 while (i--)
3609 mark_object (backlist->args[i]);
3612 #endif
3614 void
3615 syms_of_eval (void)
3617 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3618 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3619 If Lisp code tries to increase the total number past this amount,
3620 an error is signaled.
3621 You can safely use a value considerably larger than the default value,
3622 if that proves inconveniently small. However, if you increase it too far,
3623 Emacs could run out of memory trying to make the stack bigger. */);
3625 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3626 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3628 This limit serves to catch infinite recursions for you before they cause
3629 actual stack overflow in C, which would be fatal for Emacs.
3630 You can safely make it considerably larger than its default value,
3631 if that proves inconveniently small. However, if you increase it too far,
3632 Emacs could overflow the real C stack, and crash. */);
3634 DEFVAR_LISP ("quit-flag", Vquit_flag,
3635 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3636 If the value is t, that means do an ordinary quit.
3637 If the value equals `throw-on-input', that means quit by throwing
3638 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3639 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3640 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3641 Vquit_flag = Qnil;
3643 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3644 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3645 Note that `quit-flag' will still be set by typing C-g,
3646 so a quit will be signaled as soon as `inhibit-quit' is nil.
3647 To prevent this happening, set `quit-flag' to nil
3648 before making `inhibit-quit' nil. */);
3649 Vinhibit_quit = Qnil;
3651 DEFSYM (Qinhibit_quit, "inhibit-quit");
3652 DEFSYM (Qautoload, "autoload");
3653 DEFSYM (Qdebug_on_error, "debug-on-error");
3654 DEFSYM (Qmacro, "macro");
3655 DEFSYM (Qdeclare, "declare");
3657 /* Note that the process handling also uses Qexit, but we don't want
3658 to staticpro it twice, so we just do it here. */
3659 DEFSYM (Qexit, "exit");
3661 DEFSYM (Qinteractive, "interactive");
3662 DEFSYM (Qcommandp, "commandp");
3663 DEFSYM (Qdefun, "defun");
3664 DEFSYM (Qand_rest, "&rest");
3665 DEFSYM (Qand_optional, "&optional");
3666 DEFSYM (Qclosure, "closure");
3667 DEFSYM (Qdebug, "debug");
3669 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3670 doc: /* *Non-nil means enter debugger if an error is signaled.
3671 Does not apply to errors handled by `condition-case' or those
3672 matched by `debug-ignored-errors'.
3673 If the value is a list, an error only means to enter the debugger
3674 if one of its condition symbols appears in the list.
3675 When you evaluate an expression interactively, this variable
3676 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3677 The command `toggle-debug-on-error' toggles this.
3678 See also the variable `debug-on-quit'. */);
3679 Vdebug_on_error = Qnil;
3681 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3682 doc: /* *List of errors for which the debugger should not be called.
3683 Each element may be a condition-name or a regexp that matches error messages.
3684 If any element applies to a given error, that error skips the debugger
3685 and just returns to top level.
3686 This overrides the variable `debug-on-error'.
3687 It does not apply to errors handled by `condition-case'. */);
3688 Vdebug_ignored_errors = Qnil;
3690 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3691 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3692 Does not apply if quit is handled by a `condition-case'. */);
3693 debug_on_quit = 0;
3695 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3696 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3698 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3699 doc: /* Non-nil means debugger may continue execution.
3700 This is nil when the debugger is called under circumstances where it
3701 might not be safe to continue. */);
3702 debugger_may_continue = 1;
3704 DEFVAR_LISP ("debugger", Vdebugger,
3705 doc: /* Function to call to invoke debugger.
3706 If due to frame exit, args are `exit' and the value being returned;
3707 this function's value will be returned instead of that.
3708 If due to error, args are `error' and a list of the args to `signal'.
3709 If due to `apply' or `funcall' entry, one arg, `lambda'.
3710 If due to `eval' entry, one arg, t. */);
3711 Vdebugger = Qnil;
3713 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3714 doc: /* If non-nil, this is a function for `signal' to call.
3715 It receives the same arguments that `signal' was given.
3716 The Edebug package uses this to regain control. */);
3717 Vsignal_hook_function = Qnil;
3719 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3720 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3721 Note that `debug-on-error', `debug-on-quit' and friends
3722 still determine whether to handle the particular condition. */);
3723 Vdebug_on_signal = Qnil;
3725 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3726 doc: /* Function to process declarations in a macro definition.
3727 The function will be called with two args MACRO and DECL.
3728 MACRO is the name of the macro being defined.
3729 DECL is a list `(declare ...)' containing the declarations.
3730 The value the function returns is not used. */);
3731 Vmacro_declaration_function = Qnil;
3733 /* When lexical binding is being used,
3734 vinternal_interpreter_environment is non-nil, and contains an alist
3735 of lexically-bound variable, or (t), indicating an empty
3736 environment. The lisp name of this variable would be
3737 `internal-interpreter-environment' if it weren't hidden.
3738 Every element of this list can be either a cons (VAR . VAL)
3739 specifying a lexical binding, or a single symbol VAR indicating
3740 that this variable should use dynamic scoping. */
3741 DEFSYM (Qinternal_interpreter_environment, "internal-interpreter-environment");
3742 DEFVAR_LISP ("internal-interpreter-environment",
3743 Vinternal_interpreter_environment,
3744 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3745 When lexical binding is not being used, this variable is nil.
3746 A value of `(t)' indicates an empty environment, otherwise it is an
3747 alist of active lexical bindings. */);
3748 Vinternal_interpreter_environment = Qnil;
3749 /* Don't export this variable to Elisp, so no one can mess with it
3750 (Just imagine if someone makes it buffer-local). */
3751 Funintern (Qinternal_interpreter_environment, Qnil);
3753 DEFSYM (Vrun_hooks, "run-hooks");
3755 staticpro (&Vautoload_queue);
3756 Vautoload_queue = Qnil;
3757 staticpro (&Vsignaling_function);
3758 Vsignaling_function = Qnil;
3760 defsubr (&Sor);
3761 defsubr (&Sand);
3762 defsubr (&Sif);
3763 defsubr (&Scond);
3764 defsubr (&Sprogn);
3765 defsubr (&Sprog1);
3766 defsubr (&Sprog2);
3767 defsubr (&Ssetq);
3768 defsubr (&Squote);
3769 defsubr (&Sfunction);
3770 defsubr (&Sdefun);
3771 defsubr (&Sdefmacro);
3772 defsubr (&Sdefvar);
3773 defsubr (&Sdefvaralias);
3774 defsubr (&Sdefconst);
3775 defsubr (&Suser_variable_p);
3776 defsubr (&Slet);
3777 defsubr (&SletX);
3778 defsubr (&Swhile);
3779 defsubr (&Smacroexpand);
3780 defsubr (&Scatch);
3781 defsubr (&Sthrow);
3782 defsubr (&Sunwind_protect);
3783 defsubr (&Scondition_case);
3784 defsubr (&Ssignal);
3785 defsubr (&Sinteractive_p);
3786 defsubr (&Scalled_interactively_p);
3787 defsubr (&Scommandp);
3788 defsubr (&Sautoload);
3789 defsubr (&Seval);
3790 defsubr (&Sapply);
3791 defsubr (&Sfuncall);
3792 defsubr (&Srun_hooks);
3793 defsubr (&Srun_hook_with_args);
3794 defsubr (&Srun_hook_with_args_until_success);
3795 defsubr (&Srun_hook_with_args_until_failure);
3796 defsubr (&Srun_hook_wrapped);
3797 defsubr (&Sfetch_bytecode);
3798 defsubr (&Sbacktrace_debug);
3799 defsubr (&Sbacktrace);
3800 defsubr (&Sbacktrace_frame);
3801 defsubr (&Sspecial_variable_p);
3802 defsubr (&Sfunctionp);