* doc/emacs/cal-xtra.texi: Copyedits.
[emacs.git] / src / eval.c
blob344228741cbd6d084b109e092892259e4f6d8cd3
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, but
784 defining it lets you supply an initial value and documentation, which
785 can be referred to by the Emacs help facilities and other programming
786 tools. The `defvar' form also declares the variable as \"special\",
787 so that it is always dynamically bound even if `lexical-binding' is t.
789 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
790 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
791 default value is what is set; buffer-local values are not affected.
792 If INITVALUE is missing, SYMBOL's value is not set.
794 If SYMBOL has a local binding, then this form affects the local
795 binding. This is usually not what you want. Thus, if you need to
796 load a file defining variables, with this form or with `defconst' or
797 `defcustom', you should always load that file _outside_ any bindings
798 for these variables. \(`defconst' and `defcustom' behave similarly in
799 this respect.)
801 The optional argument DOCSTRING is a documentation string for the
802 variable.
804 To define a user option, use `defcustom' instead of `defvar'.
805 The function `user-variable-p' also identifies a variable as a user
806 option if its DOCSTRING starts with *, but this behavior is obsolete.
807 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
808 (Lisp_Object args)
810 register Lisp_Object sym, tem, tail;
812 sym = Fcar (args);
813 tail = Fcdr (args);
814 if (!NILP (Fcdr (Fcdr (tail))))
815 error ("Too many arguments");
817 tem = Fdefault_boundp (sym);
818 if (!NILP (tail))
820 /* Do it before evaluating the initial value, for self-references. */
821 XSYMBOL (sym)->declared_special = 1;
823 if (SYMBOL_CONSTANT_P (sym))
825 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
826 Lisp_Object tem1 = Fcar (tail);
827 if (! (CONSP (tem1)
828 && EQ (XCAR (tem1), Qquote)
829 && CONSP (XCDR (tem1))
830 && EQ (XCAR (XCDR (tem1)), sym)))
831 error ("Constant symbol `%s' specified in defvar",
832 SDATA (SYMBOL_NAME (sym)));
835 if (NILP (tem))
836 Fset_default (sym, eval_sub (Fcar (tail)));
837 else
838 { /* Check if there is really a global binding rather than just a let
839 binding that shadows the global unboundness of the var. */
840 volatile struct specbinding *pdl = specpdl_ptr;
841 while (--pdl >= specpdl)
843 if (EQ (pdl->symbol, sym) && !pdl->func
844 && EQ (pdl->old_value, Qunbound))
846 message_with_string ("Warning: defvar ignored because %s is let-bound",
847 SYMBOL_NAME (sym), 1);
848 break;
852 tail = Fcdr (tail);
853 tem = Fcar (tail);
854 if (!NILP (tem))
856 if (!NILP (Vpurify_flag))
857 tem = Fpurecopy (tem);
858 Fput (sym, Qvariable_documentation, tem);
860 LOADHIST_ATTACH (sym);
862 else if (!NILP (Vinternal_interpreter_environment)
863 && !XSYMBOL (sym)->declared_special)
864 /* A simple (defvar foo) with lexical scoping does "nothing" except
865 declare that var to be dynamically scoped *locally* (i.e. within
866 the current file or let-block). */
867 Vinternal_interpreter_environment =
868 Fcons (sym, Vinternal_interpreter_environment);
869 else
871 /* Simple (defvar <var>) should not count as a definition at all.
872 It could get in the way of other definitions, and unloading this
873 package could try to make the variable unbound. */
876 return sym;
879 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
880 doc: /* Define SYMBOL as a constant variable.
881 This declares that neither programs nor users should ever change the
882 value. This constancy is not actually enforced by Emacs Lisp, but
883 SYMBOL is marked as a special variable so that it is never lexically
884 bound.
886 The `defconst' form always sets the value of SYMBOL to the result of
887 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
888 what is set; buffer-local values are not affected. If SYMBOL has a
889 local binding, then this form sets the local binding's value.
890 However, you should normally not make local bindings for variables
891 defined with this form.
893 The optional DOCSTRING specifies the variable's documentation string.
894 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
895 (Lisp_Object args)
897 register Lisp_Object sym, tem;
899 sym = Fcar (args);
900 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
901 error ("Too many arguments");
903 tem = eval_sub (Fcar (Fcdr (args)));
904 if (!NILP (Vpurify_flag))
905 tem = Fpurecopy (tem);
906 Fset_default (sym, tem);
907 XSYMBOL (sym)->declared_special = 1;
908 tem = Fcar (Fcdr (Fcdr (args)));
909 if (!NILP (tem))
911 if (!NILP (Vpurify_flag))
912 tem = Fpurecopy (tem);
913 Fput (sym, Qvariable_documentation, tem);
915 Fput (sym, Qrisky_local_variable, Qt);
916 LOADHIST_ATTACH (sym);
917 return sym;
920 /* Error handler used in Fuser_variable_p. */
921 static Lisp_Object
922 user_variable_p_eh (Lisp_Object ignore)
924 return Qnil;
927 static Lisp_Object
928 lisp_indirect_variable (Lisp_Object sym)
930 struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
931 XSETSYMBOL (sym, s);
932 return sym;
935 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
936 doc: /* Return t if VARIABLE is intended to be set and modified by users.
937 \(The alternative is a variable used internally in a Lisp program.)
939 This function returns t if (i) the first character of its
940 documentation is `*', or (ii) it is customizable (its property list
941 contains a non-nil value of `standard-value' or `custom-autoload'), or
942 \(iii) it is an alias for a user variable.
944 But condition (i) is considered obsolete, so for most purposes this is
945 equivalent to `custom-variable-p'. */)
946 (Lisp_Object variable)
948 Lisp_Object documentation;
950 if (!SYMBOLP (variable))
951 return Qnil;
953 /* If indirect and there's an alias loop, don't check anything else. */
954 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
955 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
956 Qt, user_variable_p_eh)))
957 return Qnil;
959 while (1)
961 documentation = Fget (variable, Qvariable_documentation);
962 if (INTEGERP (documentation) && XINT (documentation) < 0)
963 return Qt;
964 if (STRINGP (documentation)
965 && ((unsigned char) SREF (documentation, 0) == '*'))
966 return Qt;
967 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
968 if (CONSP (documentation)
969 && STRINGP (XCAR (documentation))
970 && INTEGERP (XCDR (documentation))
971 && XINT (XCDR (documentation)) < 0)
972 return Qt;
973 /* Customizable? See `custom-variable-p'. */
974 if ((!NILP (Fget (variable, intern ("standard-value"))))
975 || (!NILP (Fget (variable, intern ("custom-autoload")))))
976 return Qt;
978 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
979 return Qnil;
981 /* An indirect variable? Let's follow the chain. */
982 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
986 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
987 doc: /* Bind variables according to VARLIST then eval BODY.
988 The value of the last form in BODY is returned.
989 Each element of VARLIST is a symbol (which is bound to nil)
990 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
991 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
992 usage: (let* VARLIST BODY...) */)
993 (Lisp_Object args)
995 Lisp_Object varlist, var, val, elt, lexenv;
996 int count = SPECPDL_INDEX ();
997 struct gcpro gcpro1, gcpro2, gcpro3;
999 GCPRO3 (args, elt, varlist);
1001 lexenv = Vinternal_interpreter_environment;
1003 varlist = Fcar (args);
1004 while (CONSP (varlist))
1006 QUIT;
1008 elt = XCAR (varlist);
1009 if (SYMBOLP (elt))
1011 var = elt;
1012 val = Qnil;
1014 else if (! NILP (Fcdr (Fcdr (elt))))
1015 signal_error ("`let' bindings can have only one value-form", elt);
1016 else
1018 var = Fcar (elt);
1019 val = eval_sub (Fcar (Fcdr (elt)));
1022 if (!NILP (lexenv) && SYMBOLP (var)
1023 && !XSYMBOL (var)->declared_special
1024 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1025 /* Lexically bind VAR by adding it to the interpreter's binding
1026 alist. */
1028 Lisp_Object newenv
1029 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1030 if (EQ (Vinternal_interpreter_environment, lexenv))
1031 /* Save the old lexical environment on the specpdl stack,
1032 but only for the first lexical binding, since we'll never
1033 need to revert to one of the intermediate ones. */
1034 specbind (Qinternal_interpreter_environment, newenv);
1035 else
1036 Vinternal_interpreter_environment = newenv;
1038 else
1039 specbind (var, val);
1041 varlist = XCDR (varlist);
1043 UNGCPRO;
1044 val = Fprogn (Fcdr (args));
1045 return unbind_to (count, val);
1048 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1049 doc: /* Bind variables according to VARLIST then eval BODY.
1050 The value of the last form in BODY is returned.
1051 Each element of VARLIST is a symbol (which is bound to nil)
1052 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1053 All the VALUEFORMs are evalled before any symbols are bound.
1054 usage: (let VARLIST BODY...) */)
1055 (Lisp_Object args)
1057 Lisp_Object *temps, tem, lexenv;
1058 register Lisp_Object elt, varlist;
1059 int count = SPECPDL_INDEX ();
1060 ptrdiff_t argnum;
1061 struct gcpro gcpro1, gcpro2;
1062 USE_SAFE_ALLOCA;
1064 varlist = Fcar (args);
1066 /* Make space to hold the values to give the bound variables. */
1067 elt = Flength (varlist);
1068 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1070 /* Compute the values and store them in `temps'. */
1072 GCPRO2 (args, *temps);
1073 gcpro2.nvars = 0;
1075 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1077 QUIT;
1078 elt = XCAR (varlist);
1079 if (SYMBOLP (elt))
1080 temps [argnum++] = Qnil;
1081 else if (! NILP (Fcdr (Fcdr (elt))))
1082 signal_error ("`let' bindings can have only one value-form", elt);
1083 else
1084 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1085 gcpro2.nvars = argnum;
1087 UNGCPRO;
1089 lexenv = Vinternal_interpreter_environment;
1091 varlist = Fcar (args);
1092 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1094 Lisp_Object var;
1096 elt = XCAR (varlist);
1097 var = SYMBOLP (elt) ? elt : Fcar (elt);
1098 tem = temps[argnum++];
1100 if (!NILP (lexenv) && SYMBOLP (var)
1101 && !XSYMBOL (var)->declared_special
1102 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1103 /* Lexically bind VAR by adding it to the lexenv alist. */
1104 lexenv = Fcons (Fcons (var, tem), lexenv);
1105 else
1106 /* Dynamically bind VAR. */
1107 specbind (var, tem);
1110 if (!EQ (lexenv, Vinternal_interpreter_environment))
1111 /* Instantiate a new lexical environment. */
1112 specbind (Qinternal_interpreter_environment, lexenv);
1114 elt = Fprogn (Fcdr (args));
1115 SAFE_FREE ();
1116 return unbind_to (count, elt);
1119 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1120 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1121 The order of execution is thus TEST, BODY, TEST, BODY and so on
1122 until TEST returns nil.
1123 usage: (while TEST BODY...) */)
1124 (Lisp_Object args)
1126 Lisp_Object test, body;
1127 struct gcpro gcpro1, gcpro2;
1129 GCPRO2 (test, body);
1131 test = Fcar (args);
1132 body = Fcdr (args);
1133 while (!NILP (eval_sub (test)))
1135 QUIT;
1136 Fprogn (body);
1139 UNGCPRO;
1140 return Qnil;
1143 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1144 doc: /* Return result of expanding macros at top level of FORM.
1145 If FORM is not a macro call, it is returned unchanged.
1146 Otherwise, the macro is expanded and the expansion is considered
1147 in place of FORM. When a non-macro-call results, it is returned.
1149 The second optional arg ENVIRONMENT specifies an environment of macro
1150 definitions to shadow the loaded ones for use in file byte-compilation. */)
1151 (Lisp_Object form, Lisp_Object environment)
1153 /* With cleanups from Hallvard Furuseth. */
1154 register Lisp_Object expander, sym, def, tem;
1156 while (1)
1158 /* Come back here each time we expand a macro call,
1159 in case it expands into another macro call. */
1160 if (!CONSP (form))
1161 break;
1162 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1163 def = sym = XCAR (form);
1164 tem = Qnil;
1165 /* Trace symbols aliases to other symbols
1166 until we get a symbol that is not an alias. */
1167 while (SYMBOLP (def))
1169 QUIT;
1170 sym = def;
1171 tem = Fassq (sym, environment);
1172 if (NILP (tem))
1174 def = XSYMBOL (sym)->function;
1175 if (!EQ (def, Qunbound))
1176 continue;
1178 break;
1180 /* Right now TEM is the result from SYM in ENVIRONMENT,
1181 and if TEM is nil then DEF is SYM's function definition. */
1182 if (NILP (tem))
1184 /* SYM is not mentioned in ENVIRONMENT.
1185 Look at its function definition. */
1186 if (EQ (def, Qunbound) || !CONSP (def))
1187 /* Not defined or definition not suitable. */
1188 break;
1189 if (EQ (XCAR (def), Qautoload))
1191 /* Autoloading function: will it be a macro when loaded? */
1192 tem = Fnth (make_number (4), def);
1193 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1194 /* Yes, load it and try again. */
1196 struct gcpro gcpro1;
1197 GCPRO1 (form);
1198 do_autoload (def, sym);
1199 UNGCPRO;
1200 continue;
1202 else
1203 break;
1205 else if (!EQ (XCAR (def), Qmacro))
1206 break;
1207 else expander = XCDR (def);
1209 else
1211 expander = XCDR (tem);
1212 if (NILP (expander))
1213 break;
1215 form = apply1 (expander, XCDR (form));
1217 return form;
1220 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1221 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1222 TAG is evalled to get the tag to use; it must not be nil.
1224 Then the BODY is executed.
1225 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1226 If no throw happens, `catch' returns the value of the last BODY form.
1227 If a throw happens, it specifies the value to return from `catch'.
1228 usage: (catch TAG BODY...) */)
1229 (Lisp_Object args)
1231 register Lisp_Object tag;
1232 struct gcpro gcpro1;
1234 GCPRO1 (args);
1235 tag = eval_sub (Fcar (args));
1236 UNGCPRO;
1237 return internal_catch (tag, Fprogn, Fcdr (args));
1240 /* Set up a catch, then call C function FUNC on argument ARG.
1241 FUNC should return a Lisp_Object.
1242 This is how catches are done from within C code. */
1244 Lisp_Object
1245 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1247 /* This structure is made part of the chain `catchlist'. */
1248 struct catchtag c;
1250 /* Fill in the components of c, and put it on the list. */
1251 c.next = catchlist;
1252 c.tag = tag;
1253 c.val = Qnil;
1254 c.backlist = backtrace_list;
1255 c.handlerlist = handlerlist;
1256 c.lisp_eval_depth = lisp_eval_depth;
1257 c.pdlcount = SPECPDL_INDEX ();
1258 c.poll_suppress_count = poll_suppress_count;
1259 c.interrupt_input_blocked = interrupt_input_blocked;
1260 c.gcpro = gcprolist;
1261 c.byte_stack = byte_stack_list;
1262 catchlist = &c;
1264 /* Call FUNC. */
1265 if (! _setjmp (c.jmp))
1266 c.val = (*func) (arg);
1268 /* Throw works by a longjmp that comes right here. */
1269 catchlist = c.next;
1270 return c.val;
1273 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1274 jump to that CATCH, returning VALUE as the value of that catch.
1276 This is the guts Fthrow and Fsignal; they differ only in the way
1277 they choose the catch tag to throw to. A catch tag for a
1278 condition-case form has a TAG of Qnil.
1280 Before each catch is discarded, unbind all special bindings and
1281 execute all unwind-protect clauses made above that catch. Unwind
1282 the handler stack as we go, so that the proper handlers are in
1283 effect for each unwind-protect clause we run. At the end, restore
1284 some static info saved in CATCH, and longjmp to the location
1285 specified in the
1287 This is used for correct unwinding in Fthrow and Fsignal. */
1289 static void
1290 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1292 register int last_time;
1294 /* Save the value in the tag. */
1295 catch->val = value;
1297 /* Restore certain special C variables. */
1298 set_poll_suppress_count (catch->poll_suppress_count);
1299 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1300 handling_signal = 0;
1301 immediate_quit = 0;
1305 last_time = catchlist == catch;
1307 /* Unwind the specpdl stack, and then restore the proper set of
1308 handlers. */
1309 unbind_to (catchlist->pdlcount, Qnil);
1310 handlerlist = catchlist->handlerlist;
1311 catchlist = catchlist->next;
1313 while (! last_time);
1315 #if HAVE_X_WINDOWS
1316 /* If x_catch_errors was done, turn it off now.
1317 (First we give unbind_to a chance to do that.) */
1318 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1319 The catch must remain in effect during that delicate
1320 state. --lorentey */
1321 x_fully_uncatch_errors ();
1322 #endif
1323 #endif
1325 byte_stack_list = catch->byte_stack;
1326 gcprolist = catch->gcpro;
1327 #ifdef DEBUG_GCPRO
1328 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1329 #endif
1330 backtrace_list = catch->backlist;
1331 lisp_eval_depth = catch->lisp_eval_depth;
1333 _longjmp (catch->jmp, 1);
1336 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1337 doc: /* Throw to the catch for TAG and return VALUE from it.
1338 Both TAG and VALUE are evalled. */)
1339 (register Lisp_Object tag, Lisp_Object value)
1341 register struct catchtag *c;
1343 if (!NILP (tag))
1344 for (c = catchlist; c; c = c->next)
1346 if (EQ (c->tag, tag))
1347 unwind_to_catch (c, value);
1349 xsignal2 (Qno_catch, tag, value);
1353 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1354 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1355 If BODYFORM completes normally, its value is returned
1356 after executing the UNWINDFORMS.
1357 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1358 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1359 (Lisp_Object args)
1361 Lisp_Object val;
1362 int count = SPECPDL_INDEX ();
1364 record_unwind_protect (Fprogn, Fcdr (args));
1365 val = eval_sub (Fcar (args));
1366 return unbind_to (count, val);
1369 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1370 doc: /* Regain control when an error is signaled.
1371 Executes BODYFORM and returns its value if no error happens.
1372 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1373 where the BODY is made of Lisp expressions.
1375 A handler is applicable to an error
1376 if CONDITION-NAME is one of the error's condition names.
1377 If an error happens, the first applicable handler is run.
1379 The car of a handler may be a list of condition names instead of a
1380 single condition name; then it handles all of them. If the special
1381 condition name `debug' is present in this list, it allows another
1382 condition in the list to run the debugger if `debug-on-error' and the
1383 other usual mechanisms says it should (otherwise, `condition-case'
1384 suppresses the debugger).
1386 When a handler handles an error, control returns to the `condition-case'
1387 and it executes the handler's BODY...
1388 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1389 \(If VAR is nil, the handler can't access that information.)
1390 Then the value of the last BODY form is returned from the `condition-case'
1391 expression.
1393 See also the function `signal' for more info.
1394 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1395 (Lisp_Object args)
1397 register Lisp_Object bodyform, handlers;
1398 volatile Lisp_Object var;
1400 var = Fcar (args);
1401 bodyform = Fcar (Fcdr (args));
1402 handlers = Fcdr (Fcdr (args));
1404 return internal_lisp_condition_case (var, bodyform, handlers);
1407 /* Like Fcondition_case, but the args are separate
1408 rather than passed in a list. Used by Fbyte_code. */
1410 Lisp_Object
1411 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1412 Lisp_Object handlers)
1414 Lisp_Object val;
1415 struct catchtag c;
1416 struct handler h;
1418 CHECK_SYMBOL (var);
1420 for (val = handlers; CONSP (val); val = XCDR (val))
1422 Lisp_Object tem;
1423 tem = XCAR (val);
1424 if (! (NILP (tem)
1425 || (CONSP (tem)
1426 && (SYMBOLP (XCAR (tem))
1427 || CONSP (XCAR (tem))))))
1428 error ("Invalid condition handler: %s",
1429 SDATA (Fprin1_to_string (tem, Qt)));
1432 c.tag = Qnil;
1433 c.val = Qnil;
1434 c.backlist = backtrace_list;
1435 c.handlerlist = handlerlist;
1436 c.lisp_eval_depth = lisp_eval_depth;
1437 c.pdlcount = SPECPDL_INDEX ();
1438 c.poll_suppress_count = poll_suppress_count;
1439 c.interrupt_input_blocked = interrupt_input_blocked;
1440 c.gcpro = gcprolist;
1441 c.byte_stack = byte_stack_list;
1442 if (_setjmp (c.jmp))
1444 if (!NILP (h.var))
1445 specbind (h.var, c.val);
1446 val = Fprogn (Fcdr (h.chosen_clause));
1448 /* Note that this just undoes the binding of h.var; whoever
1449 longjumped to us unwound the stack to c.pdlcount before
1450 throwing. */
1451 unbind_to (c.pdlcount, Qnil);
1452 return val;
1454 c.next = catchlist;
1455 catchlist = &c;
1457 h.var = var;
1458 h.handler = handlers;
1459 h.next = handlerlist;
1460 h.tag = &c;
1461 handlerlist = &h;
1463 val = eval_sub (bodyform);
1464 catchlist = c.next;
1465 handlerlist = h.next;
1466 return val;
1469 /* Call the function BFUN with no arguments, catching errors within it
1470 according to HANDLERS. If there is an error, call HFUN with
1471 one argument which is the data that describes the error:
1472 (SIGNALNAME . DATA)
1474 HANDLERS can be a list of conditions to catch.
1475 If HANDLERS is Qt, catch all errors.
1476 If HANDLERS is Qerror, catch all errors
1477 but allow the debugger to run if that is enabled. */
1479 Lisp_Object
1480 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1481 Lisp_Object (*hfun) (Lisp_Object))
1483 Lisp_Object val;
1484 struct catchtag c;
1485 struct handler h;
1487 c.tag = Qnil;
1488 c.val = Qnil;
1489 c.backlist = backtrace_list;
1490 c.handlerlist = handlerlist;
1491 c.lisp_eval_depth = lisp_eval_depth;
1492 c.pdlcount = SPECPDL_INDEX ();
1493 c.poll_suppress_count = poll_suppress_count;
1494 c.interrupt_input_blocked = interrupt_input_blocked;
1495 c.gcpro = gcprolist;
1496 c.byte_stack = byte_stack_list;
1497 if (_setjmp (c.jmp))
1499 return (*hfun) (c.val);
1501 c.next = catchlist;
1502 catchlist = &c;
1503 h.handler = handlers;
1504 h.var = Qnil;
1505 h.next = handlerlist;
1506 h.tag = &c;
1507 handlerlist = &h;
1509 val = (*bfun) ();
1510 catchlist = c.next;
1511 handlerlist = h.next;
1512 return val;
1515 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1517 Lisp_Object
1518 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1519 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1521 Lisp_Object val;
1522 struct catchtag c;
1523 struct handler h;
1525 c.tag = Qnil;
1526 c.val = Qnil;
1527 c.backlist = backtrace_list;
1528 c.handlerlist = handlerlist;
1529 c.lisp_eval_depth = lisp_eval_depth;
1530 c.pdlcount = SPECPDL_INDEX ();
1531 c.poll_suppress_count = poll_suppress_count;
1532 c.interrupt_input_blocked = interrupt_input_blocked;
1533 c.gcpro = gcprolist;
1534 c.byte_stack = byte_stack_list;
1535 if (_setjmp (c.jmp))
1537 return (*hfun) (c.val);
1539 c.next = catchlist;
1540 catchlist = &c;
1541 h.handler = handlers;
1542 h.var = Qnil;
1543 h.next = handlerlist;
1544 h.tag = &c;
1545 handlerlist = &h;
1547 val = (*bfun) (arg);
1548 catchlist = c.next;
1549 handlerlist = h.next;
1550 return val;
1553 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1554 its arguments. */
1556 Lisp_Object
1557 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1558 Lisp_Object arg1,
1559 Lisp_Object arg2,
1560 Lisp_Object handlers,
1561 Lisp_Object (*hfun) (Lisp_Object))
1563 Lisp_Object val;
1564 struct catchtag c;
1565 struct handler h;
1567 c.tag = Qnil;
1568 c.val = Qnil;
1569 c.backlist = backtrace_list;
1570 c.handlerlist = handlerlist;
1571 c.lisp_eval_depth = lisp_eval_depth;
1572 c.pdlcount = SPECPDL_INDEX ();
1573 c.poll_suppress_count = poll_suppress_count;
1574 c.interrupt_input_blocked = interrupt_input_blocked;
1575 c.gcpro = gcprolist;
1576 c.byte_stack = byte_stack_list;
1577 if (_setjmp (c.jmp))
1579 return (*hfun) (c.val);
1581 c.next = catchlist;
1582 catchlist = &c;
1583 h.handler = handlers;
1584 h.var = Qnil;
1585 h.next = handlerlist;
1586 h.tag = &c;
1587 handlerlist = &h;
1589 val = (*bfun) (arg1, arg2);
1590 catchlist = c.next;
1591 handlerlist = h.next;
1592 return val;
1595 /* Like internal_condition_case but call BFUN with NARGS as first,
1596 and ARGS as second argument. */
1598 Lisp_Object
1599 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1600 ptrdiff_t nargs,
1601 Lisp_Object *args,
1602 Lisp_Object handlers,
1603 Lisp_Object (*hfun) (Lisp_Object))
1605 Lisp_Object val;
1606 struct catchtag c;
1607 struct handler h;
1609 c.tag = Qnil;
1610 c.val = Qnil;
1611 c.backlist = backtrace_list;
1612 c.handlerlist = handlerlist;
1613 c.lisp_eval_depth = lisp_eval_depth;
1614 c.pdlcount = SPECPDL_INDEX ();
1615 c.poll_suppress_count = poll_suppress_count;
1616 c.interrupt_input_blocked = interrupt_input_blocked;
1617 c.gcpro = gcprolist;
1618 c.byte_stack = byte_stack_list;
1619 if (_setjmp (c.jmp))
1621 return (*hfun) (c.val);
1623 c.next = catchlist;
1624 catchlist = &c;
1625 h.handler = handlers;
1626 h.var = Qnil;
1627 h.next = handlerlist;
1628 h.tag = &c;
1629 handlerlist = &h;
1631 val = (*bfun) (nargs, args);
1632 catchlist = c.next;
1633 handlerlist = h.next;
1634 return val;
1638 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1639 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1640 Lisp_Object data);
1642 void
1643 process_quit_flag (void)
1645 Lisp_Object flag = Vquit_flag;
1646 Vquit_flag = Qnil;
1647 if (EQ (flag, Qkill_emacs))
1648 Fkill_emacs (Qnil);
1649 if (EQ (Vthrow_on_input, flag))
1650 Fthrow (Vthrow_on_input, Qt);
1651 Fsignal (Qquit, Qnil);
1654 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1655 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1656 This function does not return.
1658 An error symbol is a symbol with an `error-conditions' property
1659 that is a list of condition names.
1660 A handler for any of those names will get to handle this signal.
1661 The symbol `error' should normally be one of them.
1663 DATA should be a list. Its elements are printed as part of the error message.
1664 See Info anchor `(elisp)Definition of signal' for some details on how this
1665 error message is constructed.
1666 If the signal is handled, DATA is made available to the handler.
1667 See also the function `condition-case'. */)
1668 (Lisp_Object error_symbol, Lisp_Object data)
1670 /* When memory is full, ERROR-SYMBOL is nil,
1671 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1672 That is a special case--don't do this in other situations. */
1673 Lisp_Object conditions;
1674 Lisp_Object string;
1675 Lisp_Object real_error_symbol
1676 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1677 register Lisp_Object clause = Qnil;
1678 struct handler *h;
1679 struct backtrace *bp;
1681 immediate_quit = handling_signal = 0;
1682 abort_on_gc = 0;
1683 if (gc_in_progress || waiting_for_input)
1684 abort ();
1686 #if 0 /* rms: I don't know why this was here,
1687 but it is surely wrong for an error that is handled. */
1688 #ifdef HAVE_WINDOW_SYSTEM
1689 if (display_hourglass_p)
1690 cancel_hourglass ();
1691 #endif
1692 #endif
1694 /* This hook is used by edebug. */
1695 if (! NILP (Vsignal_hook_function)
1696 && ! NILP (error_symbol))
1698 /* Edebug takes care of restoring these variables when it exits. */
1699 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1700 max_lisp_eval_depth = lisp_eval_depth + 20;
1702 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1703 max_specpdl_size = SPECPDL_INDEX () + 40;
1705 call2 (Vsignal_hook_function, error_symbol, data);
1708 conditions = Fget (real_error_symbol, Qerror_conditions);
1710 /* Remember from where signal was called. Skip over the frame for
1711 `signal' itself. If a frame for `error' follows, skip that,
1712 too. Don't do this when ERROR_SYMBOL is nil, because that
1713 is a memory-full error. */
1714 Vsignaling_function = Qnil;
1715 if (backtrace_list && !NILP (error_symbol))
1717 bp = backtrace_list->next;
1718 if (bp && bp->function && EQ (*bp->function, Qerror))
1719 bp = bp->next;
1720 if (bp && bp->function)
1721 Vsignaling_function = *bp->function;
1724 for (h = handlerlist; h; h = h->next)
1726 clause = find_handler_clause (h->handler, conditions);
1727 if (!NILP (clause))
1728 break;
1731 if (/* Don't run the debugger for a memory-full error.
1732 (There is no room in memory to do that!) */
1733 !NILP (error_symbol)
1734 && (!NILP (Vdebug_on_signal)
1735 /* If no handler is present now, try to run the debugger. */
1736 || NILP (clause)
1737 /* A `debug' symbol in the handler list disables the normal
1738 suppression of the debugger. */
1739 || (CONSP (clause) && CONSP (XCAR (clause))
1740 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1741 /* Special handler that means "print a message and run debugger
1742 if requested". */
1743 || EQ (h->handler, Qerror)))
1745 int debugger_called
1746 = maybe_call_debugger (conditions, error_symbol, data);
1747 /* We can't return values to code which signaled an error, but we
1748 can continue code which has signaled a quit. */
1749 if (debugger_called && EQ (real_error_symbol, Qquit))
1750 return Qnil;
1753 if (!NILP (clause))
1755 Lisp_Object unwind_data
1756 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1758 h->chosen_clause = clause;
1759 unwind_to_catch (h->tag, unwind_data);
1761 else
1763 if (catchlist != 0)
1764 Fthrow (Qtop_level, Qt);
1767 if (! NILP (error_symbol))
1768 data = Fcons (error_symbol, data);
1770 string = Ferror_message_string (data);
1771 fatal ("%s", SDATA (string));
1774 /* Internal version of Fsignal that never returns.
1775 Used for anything but Qquit (which can return from Fsignal). */
1777 void
1778 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1780 Fsignal (error_symbol, data);
1781 abort ();
1784 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1786 void
1787 xsignal0 (Lisp_Object error_symbol)
1789 xsignal (error_symbol, Qnil);
1792 void
1793 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1795 xsignal (error_symbol, list1 (arg));
1798 void
1799 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1801 xsignal (error_symbol, list2 (arg1, arg2));
1804 void
1805 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1807 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1810 /* Signal `error' with message S, and additional arg ARG.
1811 If ARG is not a genuine list, make it a one-element list. */
1813 void
1814 signal_error (const char *s, Lisp_Object arg)
1816 Lisp_Object tortoise, hare;
1818 hare = tortoise = arg;
1819 while (CONSP (hare))
1821 hare = XCDR (hare);
1822 if (!CONSP (hare))
1823 break;
1825 hare = XCDR (hare);
1826 tortoise = XCDR (tortoise);
1828 if (EQ (hare, tortoise))
1829 break;
1832 if (!NILP (hare))
1833 arg = Fcons (arg, Qnil); /* Make it a list. */
1835 xsignal (Qerror, Fcons (build_string (s), arg));
1839 /* Return nonzero if LIST is a non-nil atom or
1840 a list containing one of CONDITIONS. */
1842 static int
1843 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1845 if (NILP (list))
1846 return 0;
1847 if (! CONSP (list))
1848 return 1;
1850 while (CONSP (conditions))
1852 Lisp_Object this, tail;
1853 this = XCAR (conditions);
1854 for (tail = list; CONSP (tail); tail = XCDR (tail))
1855 if (EQ (XCAR (tail), this))
1856 return 1;
1857 conditions = XCDR (conditions);
1859 return 0;
1862 /* Return 1 if an error with condition-symbols CONDITIONS,
1863 and described by SIGNAL-DATA, should skip the debugger
1864 according to debugger-ignored-errors. */
1866 static int
1867 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1869 Lisp_Object tail;
1870 int first_string = 1;
1871 Lisp_Object error_message;
1873 error_message = Qnil;
1874 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1876 if (STRINGP (XCAR (tail)))
1878 if (first_string)
1880 error_message = Ferror_message_string (data);
1881 first_string = 0;
1884 if (fast_string_match (XCAR (tail), error_message) >= 0)
1885 return 1;
1887 else
1889 Lisp_Object contail;
1891 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1892 if (EQ (XCAR (tail), XCAR (contail)))
1893 return 1;
1897 return 0;
1900 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1901 SIG and DATA describe the signal. There are two ways to pass them:
1902 = SIG is the error symbol, and DATA is the rest of the data.
1903 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1904 This is for memory-full errors only. */
1905 static int
1906 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1908 Lisp_Object combined_data;
1910 combined_data = Fcons (sig, data);
1912 if (
1913 /* Don't try to run the debugger with interrupts blocked.
1914 The editing loop would return anyway. */
1915 ! INPUT_BLOCKED_P
1916 /* Does user want to enter debugger for this kind of error? */
1917 && (EQ (sig, Qquit)
1918 ? debug_on_quit
1919 : wants_debugger (Vdebug_on_error, conditions))
1920 && ! skip_debugger (conditions, combined_data)
1921 /* RMS: What's this for? */
1922 && when_entered_debugger < num_nonmacro_input_events)
1924 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1925 return 1;
1928 return 0;
1931 static Lisp_Object
1932 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1934 register Lisp_Object h;
1936 /* t is used by handlers for all conditions, set up by C code. */
1937 if (EQ (handlers, Qt))
1938 return Qt;
1940 /* error is used similarly, but means print an error message
1941 and run the debugger if that is enabled. */
1942 if (EQ (handlers, Qerror))
1943 return Qt;
1945 for (h = handlers; CONSP (h); h = XCDR (h))
1947 Lisp_Object handler = XCAR (h);
1948 Lisp_Object condit, tem;
1950 if (!CONSP (handler))
1951 continue;
1952 condit = XCAR (handler);
1953 /* Handle a single condition name in handler HANDLER. */
1954 if (SYMBOLP (condit))
1956 tem = Fmemq (Fcar (handler), conditions);
1957 if (!NILP (tem))
1958 return handler;
1960 /* Handle a list of condition names in handler HANDLER. */
1961 else if (CONSP (condit))
1963 Lisp_Object tail;
1964 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1966 tem = Fmemq (XCAR (tail), conditions);
1967 if (!NILP (tem))
1968 return handler;
1973 return Qnil;
1977 /* Dump an error message; called like vprintf. */
1978 void
1979 verror (const char *m, va_list ap)
1981 char buf[4000];
1982 ptrdiff_t size = sizeof buf;
1983 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1984 char *buffer = buf;
1985 ptrdiff_t used;
1986 Lisp_Object string;
1988 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1989 string = make_string (buffer, used);
1990 if (buffer != buf)
1991 xfree (buffer);
1993 xsignal1 (Qerror, string);
1997 /* Dump an error message; called like printf. */
1999 /* VARARGS 1 */
2000 void
2001 error (const char *m, ...)
2003 va_list ap;
2004 va_start (ap, m);
2005 verror (m, ap);
2006 va_end (ap);
2009 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2010 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2011 This means it contains a description for how to read arguments to give it.
2012 The value is nil for an invalid function or a symbol with no function
2013 definition.
2015 Interactively callable functions include strings and vectors (treated
2016 as keyboard macros), lambda-expressions that contain a top-level call
2017 to `interactive', autoload definitions made by `autoload' with non-nil
2018 fourth argument, and some of the built-in functions of Lisp.
2020 Also, a symbol satisfies `commandp' if its function definition does so.
2022 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2023 then strings and vectors are not accepted. */)
2024 (Lisp_Object function, Lisp_Object for_call_interactively)
2026 register Lisp_Object fun;
2027 register Lisp_Object funcar;
2028 Lisp_Object if_prop = Qnil;
2030 fun = function;
2032 fun = indirect_function (fun); /* Check cycles. */
2033 if (NILP (fun) || EQ (fun, Qunbound))
2034 return Qnil;
2036 /* Check an `interactive-form' property if present, analogous to the
2037 function-documentation property. */
2038 fun = function;
2039 while (SYMBOLP (fun))
2041 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2042 if (!NILP (tmp))
2043 if_prop = Qt;
2044 fun = Fsymbol_function (fun);
2047 /* Emacs primitives are interactive if their DEFUN specifies an
2048 interactive spec. */
2049 if (SUBRP (fun))
2050 return XSUBR (fun)->intspec ? Qt : if_prop;
2052 /* Bytecode objects are interactive if they are long enough to
2053 have an element whose index is COMPILED_INTERACTIVE, which is
2054 where the interactive spec is stored. */
2055 else if (COMPILEDP (fun))
2056 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2057 ? Qt : if_prop);
2059 /* Strings and vectors are keyboard macros. */
2060 if (STRINGP (fun) || VECTORP (fun))
2061 return (NILP (for_call_interactively) ? Qt : Qnil);
2063 /* Lists may represent commands. */
2064 if (!CONSP (fun))
2065 return Qnil;
2066 funcar = XCAR (fun);
2067 if (EQ (funcar, Qclosure))
2068 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
2069 ? Qt : if_prop);
2070 else if (EQ (funcar, Qlambda))
2071 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2072 else if (EQ (funcar, Qautoload))
2073 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2074 else
2075 return Qnil;
2078 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2079 doc: /* Define FUNCTION to autoload from FILE.
2080 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2081 Third arg DOCSTRING is documentation for the function.
2082 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2083 Fifth arg TYPE indicates the type of the object:
2084 nil or omitted says FUNCTION is a function,
2085 `keymap' says FUNCTION is really a keymap, and
2086 `macro' or t says FUNCTION is really a macro.
2087 Third through fifth args give info about the real definition.
2088 They default to nil.
2089 If FUNCTION is already defined other than as an autoload,
2090 this does nothing and returns nil. */)
2091 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2093 CHECK_SYMBOL (function);
2094 CHECK_STRING (file);
2096 /* If function is defined and not as an autoload, don't override. */
2097 if (!EQ (XSYMBOL (function)->function, Qunbound)
2098 && !(CONSP (XSYMBOL (function)->function)
2099 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2100 return Qnil;
2102 if (NILP (Vpurify_flag))
2103 /* Only add entries after dumping, because the ones before are
2104 not useful and else we get loads of them from the loaddefs.el. */
2105 LOADHIST_ATTACH (Fcons (Qautoload, function));
2106 else
2107 /* We don't want the docstring in purespace (instead,
2108 Snarf-documentation should (hopefully) overwrite it).
2109 We used to use 0 here, but that leads to accidental sharing in
2110 purecopy's hash-consing, so we use a (hopefully) unique integer
2111 instead. */
2112 docstring = make_number (XPNTR (function));
2113 return Ffset (function,
2114 Fpurecopy (list5 (Qautoload, file, docstring,
2115 interactive, type)));
2118 Lisp_Object
2119 un_autoload (Lisp_Object oldqueue)
2121 register Lisp_Object queue, first, second;
2123 /* Queue to unwind is current value of Vautoload_queue.
2124 oldqueue is the shadowed value to leave in Vautoload_queue. */
2125 queue = Vautoload_queue;
2126 Vautoload_queue = oldqueue;
2127 while (CONSP (queue))
2129 first = XCAR (queue);
2130 second = Fcdr (first);
2131 first = Fcar (first);
2132 if (EQ (first, make_number (0)))
2133 Vfeatures = second;
2134 else
2135 Ffset (first, second);
2136 queue = XCDR (queue);
2138 return Qnil;
2141 /* Load an autoloaded function.
2142 FUNNAME is the symbol which is the function's name.
2143 FUNDEF is the autoload definition (a list). */
2145 void
2146 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2148 int count = SPECPDL_INDEX ();
2149 Lisp_Object fun;
2150 struct gcpro gcpro1, gcpro2, gcpro3;
2152 /* This is to make sure that loadup.el gives a clear picture
2153 of what files are preloaded and when. */
2154 if (! NILP (Vpurify_flag))
2155 error ("Attempt to autoload %s while preparing to dump",
2156 SDATA (SYMBOL_NAME (funname)));
2158 fun = funname;
2159 CHECK_SYMBOL (funname);
2160 GCPRO3 (fun, funname, fundef);
2162 /* Preserve the match data. */
2163 record_unwind_save_match_data ();
2165 /* If autoloading gets an error (which includes the error of failing
2166 to define the function being called), we use Vautoload_queue
2167 to undo function definitions and `provide' calls made by
2168 the function. We do this in the specific case of autoloading
2169 because autoloading is not an explicit request "load this file",
2170 but rather a request to "call this function".
2172 The value saved here is to be restored into Vautoload_queue. */
2173 record_unwind_protect (un_autoload, Vautoload_queue);
2174 Vautoload_queue = Qt;
2175 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2177 /* Once loading finishes, don't undo it. */
2178 Vautoload_queue = Qt;
2179 unbind_to (count, Qnil);
2181 fun = Findirect_function (fun, Qnil);
2183 if (!NILP (Fequal (fun, fundef)))
2184 error ("Autoloading failed to define function %s",
2185 SDATA (SYMBOL_NAME (funname)));
2186 UNGCPRO;
2190 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2191 doc: /* Evaluate FORM and return its value.
2192 If LEXICAL is t, evaluate using lexical scoping. */)
2193 (Lisp_Object form, Lisp_Object lexical)
2195 int count = SPECPDL_INDEX ();
2196 specbind (Qinternal_interpreter_environment,
2197 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2198 return unbind_to (count, eval_sub (form));
2201 /* Eval a sub-expression of the current expression (i.e. in the same
2202 lexical scope). */
2203 Lisp_Object
2204 eval_sub (Lisp_Object form)
2206 Lisp_Object fun, val, original_fun, original_args;
2207 Lisp_Object funcar;
2208 struct backtrace backtrace;
2209 struct gcpro gcpro1, gcpro2, gcpro3;
2211 if (handling_signal)
2212 abort ();
2214 if (SYMBOLP (form))
2216 /* Look up its binding in the lexical environment.
2217 We do not pay attention to the declared_special flag here, since we
2218 already did that when let-binding the variable. */
2219 Lisp_Object lex_binding
2220 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2221 ? Fassq (form, Vinternal_interpreter_environment)
2222 : Qnil;
2223 if (CONSP (lex_binding))
2224 return XCDR (lex_binding);
2225 else
2226 return Fsymbol_value (form);
2229 if (!CONSP (form))
2230 return form;
2232 QUIT;
2233 if ((consing_since_gc > gc_cons_threshold
2234 && consing_since_gc > gc_relative_threshold)
2236 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2238 GCPRO1 (form);
2239 Fgarbage_collect ();
2240 UNGCPRO;
2243 if (++lisp_eval_depth > max_lisp_eval_depth)
2245 if (max_lisp_eval_depth < 100)
2246 max_lisp_eval_depth = 100;
2247 if (lisp_eval_depth > max_lisp_eval_depth)
2248 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2251 original_fun = Fcar (form);
2252 original_args = Fcdr (form);
2254 backtrace.next = backtrace_list;
2255 backtrace_list = &backtrace;
2256 backtrace.function = &original_fun; /* This also protects them from gc. */
2257 backtrace.args = &original_args;
2258 backtrace.nargs = UNEVALLED;
2259 backtrace.debug_on_exit = 0;
2261 if (debug_on_next_call)
2262 do_debug_on_call (Qt);
2264 /* At this point, only original_fun and original_args
2265 have values that will be used below. */
2266 retry:
2268 /* Optimize for no indirection. */
2269 fun = original_fun;
2270 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2271 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2272 fun = indirect_function (fun);
2274 if (SUBRP (fun))
2276 Lisp_Object numargs;
2277 Lisp_Object argvals[8];
2278 Lisp_Object args_left;
2279 register int i, maxargs;
2281 args_left = original_args;
2282 numargs = Flength (args_left);
2284 CHECK_CONS_LIST ();
2286 if (XINT (numargs) < XSUBR (fun)->min_args
2287 || (XSUBR (fun)->max_args >= 0
2288 && XSUBR (fun)->max_args < XINT (numargs)))
2289 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2291 else if (XSUBR (fun)->max_args == UNEVALLED)
2292 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2293 else if (XSUBR (fun)->max_args == MANY)
2295 /* Pass a vector of evaluated arguments. */
2296 Lisp_Object *vals;
2297 ptrdiff_t argnum = 0;
2298 USE_SAFE_ALLOCA;
2300 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2302 GCPRO3 (args_left, fun, fun);
2303 gcpro3.var = vals;
2304 gcpro3.nvars = 0;
2306 while (!NILP (args_left))
2308 vals[argnum++] = eval_sub (Fcar (args_left));
2309 args_left = Fcdr (args_left);
2310 gcpro3.nvars = argnum;
2313 backtrace.args = vals;
2314 backtrace.nargs = XINT (numargs);
2316 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2317 UNGCPRO;
2318 SAFE_FREE ();
2320 else
2322 GCPRO3 (args_left, fun, fun);
2323 gcpro3.var = argvals;
2324 gcpro3.nvars = 0;
2326 maxargs = XSUBR (fun)->max_args;
2327 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2329 argvals[i] = eval_sub (Fcar (args_left));
2330 gcpro3.nvars = ++i;
2333 UNGCPRO;
2335 backtrace.args = argvals;
2336 backtrace.nargs = XINT (numargs);
2338 switch (i)
2340 case 0:
2341 val = (XSUBR (fun)->function.a0 ());
2342 break;
2343 case 1:
2344 val = (XSUBR (fun)->function.a1 (argvals[0]));
2345 break;
2346 case 2:
2347 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2348 break;
2349 case 3:
2350 val = (XSUBR (fun)->function.a3
2351 (argvals[0], argvals[1], argvals[2]));
2352 break;
2353 case 4:
2354 val = (XSUBR (fun)->function.a4
2355 (argvals[0], argvals[1], argvals[2], argvals[3]));
2356 break;
2357 case 5:
2358 val = (XSUBR (fun)->function.a5
2359 (argvals[0], argvals[1], argvals[2], argvals[3],
2360 argvals[4]));
2361 break;
2362 case 6:
2363 val = (XSUBR (fun)->function.a6
2364 (argvals[0], argvals[1], argvals[2], argvals[3],
2365 argvals[4], argvals[5]));
2366 break;
2367 case 7:
2368 val = (XSUBR (fun)->function.a7
2369 (argvals[0], argvals[1], argvals[2], argvals[3],
2370 argvals[4], argvals[5], argvals[6]));
2371 break;
2373 case 8:
2374 val = (XSUBR (fun)->function.a8
2375 (argvals[0], argvals[1], argvals[2], argvals[3],
2376 argvals[4], argvals[5], argvals[6], argvals[7]));
2377 break;
2379 default:
2380 /* Someone has created a subr that takes more arguments than
2381 is supported by this code. We need to either rewrite the
2382 subr to use a different argument protocol, or add more
2383 cases to this switch. */
2384 abort ();
2388 else if (COMPILEDP (fun))
2389 val = apply_lambda (fun, original_args);
2390 else
2392 if (EQ (fun, Qunbound))
2393 xsignal1 (Qvoid_function, original_fun);
2394 if (!CONSP (fun))
2395 xsignal1 (Qinvalid_function, original_fun);
2396 funcar = XCAR (fun);
2397 if (!SYMBOLP (funcar))
2398 xsignal1 (Qinvalid_function, original_fun);
2399 if (EQ (funcar, Qautoload))
2401 do_autoload (fun, original_fun);
2402 goto retry;
2404 if (EQ (funcar, Qmacro))
2405 val = eval_sub (apply1 (Fcdr (fun), original_args));
2406 else if (EQ (funcar, Qlambda)
2407 || EQ (funcar, Qclosure))
2408 val = apply_lambda (fun, original_args);
2409 else
2410 xsignal1 (Qinvalid_function, original_fun);
2412 CHECK_CONS_LIST ();
2414 lisp_eval_depth--;
2415 if (backtrace.debug_on_exit)
2416 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2417 backtrace_list = backtrace.next;
2419 return val;
2422 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2423 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2424 Then return the value FUNCTION returns.
2425 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2426 usage: (apply FUNCTION &rest ARGUMENTS) */)
2427 (ptrdiff_t nargs, Lisp_Object *args)
2429 ptrdiff_t i, numargs;
2430 register Lisp_Object spread_arg;
2431 register Lisp_Object *funcall_args;
2432 Lisp_Object fun, retval;
2433 struct gcpro gcpro1;
2434 USE_SAFE_ALLOCA;
2436 fun = args [0];
2437 funcall_args = 0;
2438 spread_arg = args [nargs - 1];
2439 CHECK_LIST (spread_arg);
2441 numargs = XINT (Flength (spread_arg));
2443 if (numargs == 0)
2444 return Ffuncall (nargs - 1, args);
2445 else if (numargs == 1)
2447 args [nargs - 1] = XCAR (spread_arg);
2448 return Ffuncall (nargs, args);
2451 numargs += nargs - 2;
2453 /* Optimize for no indirection. */
2454 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2455 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2456 fun = indirect_function (fun);
2457 if (EQ (fun, Qunbound))
2459 /* Let funcall get the error. */
2460 fun = args[0];
2461 goto funcall;
2464 if (SUBRP (fun))
2466 if (numargs < XSUBR (fun)->min_args
2467 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2468 goto funcall; /* Let funcall get the error. */
2469 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2471 /* Avoid making funcall cons up a yet another new vector of arguments
2472 by explicitly supplying nil's for optional values. */
2473 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2474 for (i = numargs; i < XSUBR (fun)->max_args;)
2475 funcall_args[++i] = Qnil;
2476 GCPRO1 (*funcall_args);
2477 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2480 funcall:
2481 /* We add 1 to numargs because funcall_args includes the
2482 function itself as well as its arguments. */
2483 if (!funcall_args)
2485 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2486 GCPRO1 (*funcall_args);
2487 gcpro1.nvars = 1 + numargs;
2490 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2491 /* Spread the last arg we got. Its first element goes in
2492 the slot that it used to occupy, hence this value of I. */
2493 i = nargs - 1;
2494 while (!NILP (spread_arg))
2496 funcall_args [i++] = XCAR (spread_arg);
2497 spread_arg = XCDR (spread_arg);
2500 /* By convention, the caller needs to gcpro Ffuncall's args. */
2501 retval = Ffuncall (gcpro1.nvars, funcall_args);
2502 UNGCPRO;
2503 SAFE_FREE ();
2505 return retval;
2508 /* Run hook variables in various ways. */
2510 static Lisp_Object
2511 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2513 Ffuncall (nargs, args);
2514 return Qnil;
2517 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2518 doc: /* Run each hook in HOOKS.
2519 Each argument should be a symbol, a hook variable.
2520 These symbols are processed in the order specified.
2521 If a hook symbol has a non-nil value, that value may be a function
2522 or a list of functions to be called to run the hook.
2523 If the value is a function, it is called with no arguments.
2524 If it is a list, the elements are called, in order, with no arguments.
2526 Major modes should not use this function directly to run their mode
2527 hook; they should use `run-mode-hooks' instead.
2529 Do not use `make-local-variable' to make a hook variable buffer-local.
2530 Instead, use `add-hook' and specify t for the LOCAL argument.
2531 usage: (run-hooks &rest HOOKS) */)
2532 (ptrdiff_t nargs, Lisp_Object *args)
2534 Lisp_Object hook[1];
2535 ptrdiff_t i;
2537 for (i = 0; i < nargs; i++)
2539 hook[0] = args[i];
2540 run_hook_with_args (1, hook, funcall_nil);
2543 return Qnil;
2546 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2547 Srun_hook_with_args, 1, MANY, 0,
2548 doc: /* Run HOOK with the specified arguments ARGS.
2549 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2550 value, that value may be a function or a list of functions to be
2551 called to run the hook. If the value is a function, it is called with
2552 the given arguments and its return value is returned. If it is a list
2553 of functions, those functions are called, in order,
2554 with the given arguments ARGS.
2555 It is best not to depend on the value returned by `run-hook-with-args',
2556 as that may change.
2558 Do not use `make-local-variable' to make a hook variable buffer-local.
2559 Instead, use `add-hook' and specify t for the LOCAL argument.
2560 usage: (run-hook-with-args HOOK &rest ARGS) */)
2561 (ptrdiff_t nargs, Lisp_Object *args)
2563 return run_hook_with_args (nargs, args, funcall_nil);
2566 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2567 Srun_hook_with_args_until_success, 1, MANY, 0,
2568 doc: /* Run HOOK with the specified arguments ARGS.
2569 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2570 value, that value may be a function or a list of functions to be
2571 called to run the hook. If the value is a function, it is called with
2572 the given arguments and its return value is returned.
2573 If it is a list of functions, those functions are called, in order,
2574 with the given arguments ARGS, until one of them
2575 returns a non-nil value. Then we return that value.
2576 However, if they all return nil, we return nil.
2578 Do not use `make-local-variable' to make a hook variable buffer-local.
2579 Instead, use `add-hook' and specify t for the LOCAL argument.
2580 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2581 (ptrdiff_t nargs, Lisp_Object *args)
2583 return run_hook_with_args (nargs, args, Ffuncall);
2586 static Lisp_Object
2587 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2589 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2592 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2593 Srun_hook_with_args_until_failure, 1, MANY, 0,
2594 doc: /* Run HOOK with the specified arguments ARGS.
2595 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2596 value, that value may be a function or a list of functions to be
2597 called to run the hook. If the value is a function, it is called with
2598 the given arguments and its return value is returned.
2599 If it is a list of functions, those functions are called, in order,
2600 with the given arguments ARGS, until one of them returns nil.
2601 Then we return nil. However, if they all return non-nil, we return non-nil.
2603 Do not use `make-local-variable' to make a hook variable buffer-local.
2604 Instead, use `add-hook' and specify t for the LOCAL argument.
2605 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2606 (ptrdiff_t nargs, Lisp_Object *args)
2608 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2611 static Lisp_Object
2612 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2614 Lisp_Object tmp = args[0], ret;
2615 args[0] = args[1];
2616 args[1] = tmp;
2617 ret = Ffuncall (nargs, args);
2618 args[1] = args[0];
2619 args[0] = tmp;
2620 return ret;
2623 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2624 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2625 I.e. instead of calling each function FUN directly with arguments ARGS,
2626 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2627 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2628 aborts and returns that value.
2629 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2630 (ptrdiff_t nargs, Lisp_Object *args)
2632 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2635 /* ARGS[0] should be a hook symbol.
2636 Call each of the functions in the hook value, passing each of them
2637 as arguments all the rest of ARGS (all NARGS - 1 elements).
2638 FUNCALL specifies how to call each function on the hook.
2639 The caller (or its caller, etc) must gcpro all of ARGS,
2640 except that it isn't necessary to gcpro ARGS[0]. */
2642 Lisp_Object
2643 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2644 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2646 Lisp_Object sym, val, ret = Qnil;
2647 struct gcpro gcpro1, gcpro2, gcpro3;
2649 /* If we are dying or still initializing,
2650 don't do anything--it would probably crash if we tried. */
2651 if (NILP (Vrun_hooks))
2652 return Qnil;
2654 sym = args[0];
2655 val = find_symbol_value (sym);
2657 if (EQ (val, Qunbound) || NILP (val))
2658 return ret;
2659 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2661 args[0] = val;
2662 return funcall (nargs, args);
2664 else
2666 Lisp_Object global_vals = Qnil;
2667 GCPRO3 (sym, val, global_vals);
2669 for (;
2670 CONSP (val) && NILP (ret);
2671 val = XCDR (val))
2673 if (EQ (XCAR (val), Qt))
2675 /* t indicates this hook has a local binding;
2676 it means to run the global binding too. */
2677 global_vals = Fdefault_value (sym);
2678 if (NILP (global_vals)) continue;
2680 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2682 args[0] = global_vals;
2683 ret = funcall (nargs, args);
2685 else
2687 for (;
2688 CONSP (global_vals) && NILP (ret);
2689 global_vals = XCDR (global_vals))
2691 args[0] = XCAR (global_vals);
2692 /* In a global value, t should not occur. If it does, we
2693 must ignore it to avoid an endless loop. */
2694 if (!EQ (args[0], Qt))
2695 ret = funcall (nargs, args);
2699 else
2701 args[0] = XCAR (val);
2702 ret = funcall (nargs, args);
2706 UNGCPRO;
2707 return ret;
2711 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2713 void
2714 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2716 Lisp_Object temp[3];
2717 temp[0] = hook;
2718 temp[1] = arg1;
2719 temp[2] = arg2;
2721 Frun_hook_with_args (3, temp);
2724 /* Apply fn to arg. */
2725 Lisp_Object
2726 apply1 (Lisp_Object fn, Lisp_Object arg)
2728 struct gcpro gcpro1;
2730 GCPRO1 (fn);
2731 if (NILP (arg))
2732 RETURN_UNGCPRO (Ffuncall (1, &fn));
2733 gcpro1.nvars = 2;
2735 Lisp_Object args[2];
2736 args[0] = fn;
2737 args[1] = arg;
2738 gcpro1.var = args;
2739 RETURN_UNGCPRO (Fapply (2, args));
2743 /* Call function fn on no arguments. */
2744 Lisp_Object
2745 call0 (Lisp_Object fn)
2747 struct gcpro gcpro1;
2749 GCPRO1 (fn);
2750 RETURN_UNGCPRO (Ffuncall (1, &fn));
2753 /* Call function fn with 1 argument arg1. */
2754 /* ARGSUSED */
2755 Lisp_Object
2756 call1 (Lisp_Object fn, Lisp_Object arg1)
2758 struct gcpro gcpro1;
2759 Lisp_Object args[2];
2761 args[0] = fn;
2762 args[1] = arg1;
2763 GCPRO1 (args[0]);
2764 gcpro1.nvars = 2;
2765 RETURN_UNGCPRO (Ffuncall (2, args));
2768 /* Call function fn with 2 arguments arg1, arg2. */
2769 /* ARGSUSED */
2770 Lisp_Object
2771 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2773 struct gcpro gcpro1;
2774 Lisp_Object args[3];
2775 args[0] = fn;
2776 args[1] = arg1;
2777 args[2] = arg2;
2778 GCPRO1 (args[0]);
2779 gcpro1.nvars = 3;
2780 RETURN_UNGCPRO (Ffuncall (3, args));
2783 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2784 /* ARGSUSED */
2785 Lisp_Object
2786 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2788 struct gcpro gcpro1;
2789 Lisp_Object args[4];
2790 args[0] = fn;
2791 args[1] = arg1;
2792 args[2] = arg2;
2793 args[3] = arg3;
2794 GCPRO1 (args[0]);
2795 gcpro1.nvars = 4;
2796 RETURN_UNGCPRO (Ffuncall (4, args));
2799 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2800 /* ARGSUSED */
2801 Lisp_Object
2802 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2803 Lisp_Object arg4)
2805 struct gcpro gcpro1;
2806 Lisp_Object args[5];
2807 args[0] = fn;
2808 args[1] = arg1;
2809 args[2] = arg2;
2810 args[3] = arg3;
2811 args[4] = arg4;
2812 GCPRO1 (args[0]);
2813 gcpro1.nvars = 5;
2814 RETURN_UNGCPRO (Ffuncall (5, args));
2817 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2818 /* ARGSUSED */
2819 Lisp_Object
2820 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2821 Lisp_Object arg4, Lisp_Object arg5)
2823 struct gcpro gcpro1;
2824 Lisp_Object args[6];
2825 args[0] = fn;
2826 args[1] = arg1;
2827 args[2] = arg2;
2828 args[3] = arg3;
2829 args[4] = arg4;
2830 args[5] = arg5;
2831 GCPRO1 (args[0]);
2832 gcpro1.nvars = 6;
2833 RETURN_UNGCPRO (Ffuncall (6, args));
2836 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2837 /* ARGSUSED */
2838 Lisp_Object
2839 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2840 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2842 struct gcpro gcpro1;
2843 Lisp_Object args[7];
2844 args[0] = fn;
2845 args[1] = arg1;
2846 args[2] = arg2;
2847 args[3] = arg3;
2848 args[4] = arg4;
2849 args[5] = arg5;
2850 args[6] = arg6;
2851 GCPRO1 (args[0]);
2852 gcpro1.nvars = 7;
2853 RETURN_UNGCPRO (Ffuncall (7, args));
2856 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2857 /* ARGSUSED */
2858 Lisp_Object
2859 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2860 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2862 struct gcpro gcpro1;
2863 Lisp_Object args[8];
2864 args[0] = fn;
2865 args[1] = arg1;
2866 args[2] = arg2;
2867 args[3] = arg3;
2868 args[4] = arg4;
2869 args[5] = arg5;
2870 args[6] = arg6;
2871 args[7] = arg7;
2872 GCPRO1 (args[0]);
2873 gcpro1.nvars = 8;
2874 RETURN_UNGCPRO (Ffuncall (8, args));
2877 /* The caller should GCPRO all the elements of ARGS. */
2879 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2880 doc: /* Non-nil if OBJECT is a function. */)
2881 (Lisp_Object object)
2883 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2885 object = Findirect_function (object, Qt);
2887 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2889 /* Autoloaded symbols are functions, except if they load
2890 macros or keymaps. */
2891 int i;
2892 for (i = 0; i < 4 && CONSP (object); i++)
2893 object = XCDR (object);
2895 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2899 if (SUBRP (object))
2900 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2901 else if (COMPILEDP (object))
2902 return Qt;
2903 else if (CONSP (object))
2905 Lisp_Object car = XCAR (object);
2906 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2908 else
2909 return Qnil;
2912 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2913 doc: /* Call first argument as a function, passing remaining arguments to it.
2914 Return the value that function returns.
2915 Thus, (funcall 'cons 'x 'y) returns (x . y).
2916 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2917 (ptrdiff_t nargs, Lisp_Object *args)
2919 Lisp_Object fun, original_fun;
2920 Lisp_Object funcar;
2921 ptrdiff_t numargs = nargs - 1;
2922 Lisp_Object lisp_numargs;
2923 Lisp_Object val;
2924 struct backtrace backtrace;
2925 register Lisp_Object *internal_args;
2926 ptrdiff_t i;
2928 QUIT;
2929 if ((consing_since_gc > gc_cons_threshold
2930 && consing_since_gc > gc_relative_threshold)
2932 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2933 Fgarbage_collect ();
2935 if (++lisp_eval_depth > max_lisp_eval_depth)
2937 if (max_lisp_eval_depth < 100)
2938 max_lisp_eval_depth = 100;
2939 if (lisp_eval_depth > max_lisp_eval_depth)
2940 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2943 backtrace.next = backtrace_list;
2944 backtrace_list = &backtrace;
2945 backtrace.function = &args[0];
2946 backtrace.args = &args[1];
2947 backtrace.nargs = nargs - 1;
2948 backtrace.debug_on_exit = 0;
2950 if (debug_on_next_call)
2951 do_debug_on_call (Qlambda);
2953 CHECK_CONS_LIST ();
2955 original_fun = args[0];
2957 retry:
2959 /* Optimize for no indirection. */
2960 fun = original_fun;
2961 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2962 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2963 fun = indirect_function (fun);
2965 if (SUBRP (fun))
2967 if (numargs < XSUBR (fun)->min_args
2968 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2970 XSETFASTINT (lisp_numargs, numargs);
2971 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2974 else if (XSUBR (fun)->max_args == UNEVALLED)
2975 xsignal1 (Qinvalid_function, original_fun);
2977 else if (XSUBR (fun)->max_args == MANY)
2978 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2979 else
2981 if (XSUBR (fun)->max_args > numargs)
2983 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2984 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
2985 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2986 internal_args[i] = Qnil;
2988 else
2989 internal_args = args + 1;
2990 switch (XSUBR (fun)->max_args)
2992 case 0:
2993 val = (XSUBR (fun)->function.a0 ());
2994 break;
2995 case 1:
2996 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2997 break;
2998 case 2:
2999 val = (XSUBR (fun)->function.a2
3000 (internal_args[0], internal_args[1]));
3001 break;
3002 case 3:
3003 val = (XSUBR (fun)->function.a3
3004 (internal_args[0], internal_args[1], internal_args[2]));
3005 break;
3006 case 4:
3007 val = (XSUBR (fun)->function.a4
3008 (internal_args[0], internal_args[1], internal_args[2],
3009 internal_args[3]));
3010 break;
3011 case 5:
3012 val = (XSUBR (fun)->function.a5
3013 (internal_args[0], internal_args[1], internal_args[2],
3014 internal_args[3], internal_args[4]));
3015 break;
3016 case 6:
3017 val = (XSUBR (fun)->function.a6
3018 (internal_args[0], internal_args[1], internal_args[2],
3019 internal_args[3], internal_args[4], internal_args[5]));
3020 break;
3021 case 7:
3022 val = (XSUBR (fun)->function.a7
3023 (internal_args[0], internal_args[1], internal_args[2],
3024 internal_args[3], internal_args[4], internal_args[5],
3025 internal_args[6]));
3026 break;
3028 case 8:
3029 val = (XSUBR (fun)->function.a8
3030 (internal_args[0], internal_args[1], internal_args[2],
3031 internal_args[3], internal_args[4], internal_args[5],
3032 internal_args[6], internal_args[7]));
3033 break;
3035 default:
3037 /* If a subr takes more than 8 arguments without using MANY
3038 or UNEVALLED, we need to extend this function to support it.
3039 Until this is done, there is no way to call the function. */
3040 abort ();
3044 else if (COMPILEDP (fun))
3045 val = funcall_lambda (fun, numargs, args + 1);
3046 else
3048 if (EQ (fun, Qunbound))
3049 xsignal1 (Qvoid_function, original_fun);
3050 if (!CONSP (fun))
3051 xsignal1 (Qinvalid_function, original_fun);
3052 funcar = XCAR (fun);
3053 if (!SYMBOLP (funcar))
3054 xsignal1 (Qinvalid_function, original_fun);
3055 if (EQ (funcar, Qlambda)
3056 || EQ (funcar, Qclosure))
3057 val = funcall_lambda (fun, numargs, args + 1);
3058 else if (EQ (funcar, Qautoload))
3060 do_autoload (fun, original_fun);
3061 CHECK_CONS_LIST ();
3062 goto retry;
3064 else
3065 xsignal1 (Qinvalid_function, original_fun);
3067 CHECK_CONS_LIST ();
3068 lisp_eval_depth--;
3069 if (backtrace.debug_on_exit)
3070 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3071 backtrace_list = backtrace.next;
3072 return val;
3075 static Lisp_Object
3076 apply_lambda (Lisp_Object fun, Lisp_Object args)
3078 Lisp_Object args_left;
3079 ptrdiff_t i, numargs;
3080 register Lisp_Object *arg_vector;
3081 struct gcpro gcpro1, gcpro2, gcpro3;
3082 register Lisp_Object tem;
3083 USE_SAFE_ALLOCA;
3085 numargs = XFASTINT (Flength (args));
3086 SAFE_ALLOCA_LISP (arg_vector, numargs);
3087 args_left = args;
3089 GCPRO3 (*arg_vector, args_left, fun);
3090 gcpro1.nvars = 0;
3092 for (i = 0; i < numargs; )
3094 tem = Fcar (args_left), args_left = Fcdr (args_left);
3095 tem = eval_sub (tem);
3096 arg_vector[i++] = tem;
3097 gcpro1.nvars = i;
3100 UNGCPRO;
3102 backtrace_list->args = arg_vector;
3103 backtrace_list->nargs = i;
3104 tem = funcall_lambda (fun, numargs, arg_vector);
3106 /* Do the debug-on-exit now, while arg_vector still exists. */
3107 if (backtrace_list->debug_on_exit)
3108 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3109 /* Don't do it again when we return to eval. */
3110 backtrace_list->debug_on_exit = 0;
3111 SAFE_FREE ();
3112 return tem;
3115 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3116 and return the result of evaluation.
3117 FUN must be either a lambda-expression or a compiled-code object. */
3119 static Lisp_Object
3120 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
3121 register Lisp_Object *arg_vector)
3123 Lisp_Object val, syms_left, next, lexenv;
3124 int count = SPECPDL_INDEX ();
3125 ptrdiff_t i;
3126 int optional, rest;
3128 if (CONSP (fun))
3130 if (EQ (XCAR (fun), Qclosure))
3132 fun = XCDR (fun); /* Drop `closure'. */
3133 lexenv = XCAR (fun);
3134 CHECK_LIST_CONS (fun, fun);
3136 else
3137 lexenv = Qnil;
3138 syms_left = XCDR (fun);
3139 if (CONSP (syms_left))
3140 syms_left = XCAR (syms_left);
3141 else
3142 xsignal1 (Qinvalid_function, fun);
3144 else if (COMPILEDP (fun))
3146 syms_left = AREF (fun, COMPILED_ARGLIST);
3147 if (INTEGERP (syms_left))
3148 /* A byte-code object with a non-nil `push args' slot means we
3149 shouldn't bind any arguments, instead just call the byte-code
3150 interpreter directly; it will push arguments as necessary.
3152 Byte-code objects with either a non-existent, or a nil value for
3153 the `push args' slot (the default), have dynamically-bound
3154 arguments, and use the argument-binding code below instead (as do
3155 all interpreted functions, even lexically bound ones). */
3157 /* If we have not actually read the bytecode string
3158 and constants vector yet, fetch them from the file. */
3159 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3160 Ffetch_bytecode (fun);
3161 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3162 AREF (fun, COMPILED_CONSTANTS),
3163 AREF (fun, COMPILED_STACK_DEPTH),
3164 syms_left,
3165 nargs, arg_vector);
3167 lexenv = Qnil;
3169 else
3170 abort ();
3172 i = optional = rest = 0;
3173 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3175 QUIT;
3177 next = XCAR (syms_left);
3178 if (!SYMBOLP (next))
3179 xsignal1 (Qinvalid_function, fun);
3181 if (EQ (next, Qand_rest))
3182 rest = 1;
3183 else if (EQ (next, Qand_optional))
3184 optional = 1;
3185 else
3187 Lisp_Object arg;
3188 if (rest)
3190 arg = Flist (nargs - i, &arg_vector[i]);
3191 i = nargs;
3193 else if (i < nargs)
3194 arg = arg_vector[i++];
3195 else if (!optional)
3196 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3197 else
3198 arg = Qnil;
3200 /* Bind the argument. */
3201 if (!NILP (lexenv) && SYMBOLP (next))
3202 /* Lexically bind NEXT by adding it to the lexenv alist. */
3203 lexenv = Fcons (Fcons (next, arg), lexenv);
3204 else
3205 /* Dynamically bind NEXT. */
3206 specbind (next, arg);
3210 if (!NILP (syms_left))
3211 xsignal1 (Qinvalid_function, fun);
3212 else if (i < nargs)
3213 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3215 if (!EQ (lexenv, Vinternal_interpreter_environment))
3216 /* Instantiate a new lexical environment. */
3217 specbind (Qinternal_interpreter_environment, lexenv);
3219 if (CONSP (fun))
3220 val = Fprogn (XCDR (XCDR (fun)));
3221 else
3223 /* If we have not actually read the bytecode string
3224 and constants vector yet, fetch them from the file. */
3225 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3226 Ffetch_bytecode (fun);
3227 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3228 AREF (fun, COMPILED_CONSTANTS),
3229 AREF (fun, COMPILED_STACK_DEPTH),
3230 Qnil, 0, 0);
3233 return unbind_to (count, val);
3236 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3237 1, 1, 0,
3238 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3239 (Lisp_Object object)
3241 Lisp_Object tem;
3243 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3245 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3246 if (!CONSP (tem))
3248 tem = AREF (object, COMPILED_BYTECODE);
3249 if (CONSP (tem) && STRINGP (XCAR (tem)))
3250 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3251 else
3252 error ("Invalid byte code");
3254 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3255 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3257 return object;
3260 static void
3261 grow_specpdl (void)
3263 register int count = SPECPDL_INDEX ();
3264 int max_size =
3265 min (max_specpdl_size,
3266 min (max (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct specbinding),
3267 INT_MAX));
3268 int size;
3269 if (max_size <= specpdl_size)
3271 if (max_specpdl_size < 400)
3272 max_size = max_specpdl_size = 400;
3273 if (max_size <= specpdl_size)
3274 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3276 size = specpdl_size < max_size / 2 ? 2 * specpdl_size : max_size;
3277 specpdl = xnrealloc (specpdl, size, sizeof *specpdl);
3278 specpdl_size = size;
3279 specpdl_ptr = specpdl + count;
3282 /* `specpdl_ptr->symbol' is a field which describes which variable is
3283 let-bound, so it can be properly undone when we unbind_to.
3284 It can have the following two shapes:
3285 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3286 a symbol that is not buffer-local (at least at the time
3287 the let binding started). Note also that it should not be
3288 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3289 to record V2 here).
3290 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3291 variable SYMBOL which can be buffer-local. WHERE tells us
3292 which buffer is affected (or nil if the let-binding affects the
3293 global value of the variable) and BUFFER tells us which buffer was
3294 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3295 BUFFER did not yet have a buffer-local value). */
3297 void
3298 specbind (Lisp_Object symbol, Lisp_Object value)
3300 struct Lisp_Symbol *sym;
3302 eassert (!handling_signal);
3304 CHECK_SYMBOL (symbol);
3305 sym = XSYMBOL (symbol);
3306 if (specpdl_ptr == specpdl + specpdl_size)
3307 grow_specpdl ();
3309 start:
3310 switch (sym->redirect)
3312 case SYMBOL_VARALIAS:
3313 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3314 case SYMBOL_PLAINVAL:
3315 /* The most common case is that of a non-constant symbol with a
3316 trivial value. Make that as fast as we can. */
3317 specpdl_ptr->symbol = symbol;
3318 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3319 specpdl_ptr->func = NULL;
3320 ++specpdl_ptr;
3321 if (!sym->constant)
3322 SET_SYMBOL_VAL (sym, value);
3323 else
3324 set_internal (symbol, value, Qnil, 1);
3325 break;
3326 case SYMBOL_LOCALIZED:
3327 if (SYMBOL_BLV (sym)->frame_local)
3328 error ("Frame-local vars cannot be let-bound");
3329 case SYMBOL_FORWARDED:
3331 Lisp_Object ovalue = find_symbol_value (symbol);
3332 specpdl_ptr->func = 0;
3333 specpdl_ptr->old_value = ovalue;
3335 eassert (sym->redirect != SYMBOL_LOCALIZED
3336 || (EQ (SYMBOL_BLV (sym)->where,
3337 SYMBOL_BLV (sym)->frame_local ?
3338 Fselected_frame () : Fcurrent_buffer ())));
3340 if (sym->redirect == SYMBOL_LOCALIZED
3341 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3343 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3345 /* For a local variable, record both the symbol and which
3346 buffer's or frame's value we are saving. */
3347 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3349 eassert (sym->redirect != SYMBOL_LOCALIZED
3350 || (BLV_FOUND (SYMBOL_BLV (sym))
3351 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3352 where = cur_buf;
3354 else if (sym->redirect == SYMBOL_LOCALIZED
3355 && BLV_FOUND (SYMBOL_BLV (sym)))
3356 where = SYMBOL_BLV (sym)->where;
3357 else
3358 where = Qnil;
3360 /* We're not using the `unused' slot in the specbinding
3361 structure because this would mean we have to do more
3362 work for simple variables. */
3363 /* FIXME: The third value `current_buffer' is only used in
3364 let_shadows_buffer_binding_p which is itself only used
3365 in set_internal for local_if_set. */
3366 eassert (NILP (where) || EQ (where, cur_buf));
3367 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3369 /* If SYMBOL is a per-buffer variable which doesn't have a
3370 buffer-local value here, make the `let' change the global
3371 value by changing the value of SYMBOL in all buffers not
3372 having their own value. This is consistent with what
3373 happens with other buffer-local variables. */
3374 if (NILP (where)
3375 && sym->redirect == SYMBOL_FORWARDED)
3377 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3378 ++specpdl_ptr;
3379 Fset_default (symbol, value);
3380 return;
3383 else
3384 specpdl_ptr->symbol = symbol;
3386 specpdl_ptr++;
3387 set_internal (symbol, value, Qnil, 1);
3388 break;
3390 default: abort ();
3394 void
3395 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3397 eassert (!handling_signal);
3399 if (specpdl_ptr == specpdl + specpdl_size)
3400 grow_specpdl ();
3401 specpdl_ptr->func = function;
3402 specpdl_ptr->symbol = Qnil;
3403 specpdl_ptr->old_value = arg;
3404 specpdl_ptr++;
3407 Lisp_Object
3408 unbind_to (int count, Lisp_Object value)
3410 Lisp_Object quitf = Vquit_flag;
3411 struct gcpro gcpro1, gcpro2;
3413 GCPRO2 (value, quitf);
3414 Vquit_flag = Qnil;
3416 while (specpdl_ptr != specpdl + count)
3418 /* Copy the binding, and decrement specpdl_ptr, before we do
3419 the work to unbind it. We decrement first
3420 so that an error in unbinding won't try to unbind
3421 the same entry again, and we copy the binding first
3422 in case more bindings are made during some of the code we run. */
3424 struct specbinding this_binding;
3425 this_binding = *--specpdl_ptr;
3427 if (this_binding.func != 0)
3428 (*this_binding.func) (this_binding.old_value);
3429 /* If the symbol is a list, it is really (SYMBOL WHERE
3430 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3431 frame. If WHERE is a buffer or frame, this indicates we
3432 bound a variable that had a buffer-local or frame-local
3433 binding. WHERE nil means that the variable had the default
3434 value when it was bound. CURRENT-BUFFER is the buffer that
3435 was current when the variable was bound. */
3436 else if (CONSP (this_binding.symbol))
3438 Lisp_Object symbol, where;
3440 symbol = XCAR (this_binding.symbol);
3441 where = XCAR (XCDR (this_binding.symbol));
3443 if (NILP (where))
3444 Fset_default (symbol, this_binding.old_value);
3445 /* If `where' is non-nil, reset the value in the appropriate
3446 local binding, but only if that binding still exists. */
3447 else if (BUFFERP (where)
3448 ? !NILP (Flocal_variable_p (symbol, where))
3449 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3450 set_internal (symbol, this_binding.old_value, where, 1);
3452 /* If variable has a trivial value (no forwarding), we can
3453 just set it. No need to check for constant symbols here,
3454 since that was already done by specbind. */
3455 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3456 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3457 this_binding.old_value);
3458 else
3459 /* NOTE: we only ever come here if make_local_foo was used for
3460 the first time on this var within this let. */
3461 Fset_default (this_binding.symbol, this_binding.old_value);
3464 if (NILP (Vquit_flag) && !NILP (quitf))
3465 Vquit_flag = quitf;
3467 UNGCPRO;
3468 return value;
3471 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3472 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3473 A special variable is one that will be bound dynamically, even in a
3474 context where binding is lexical by default. */)
3475 (Lisp_Object symbol)
3477 CHECK_SYMBOL (symbol);
3478 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3482 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3483 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3484 The debugger is entered when that frame exits, if the flag is non-nil. */)
3485 (Lisp_Object level, Lisp_Object flag)
3487 register struct backtrace *backlist = backtrace_list;
3488 register int i;
3490 CHECK_NUMBER (level);
3492 for (i = 0; backlist && i < XINT (level); i++)
3494 backlist = backlist->next;
3497 if (backlist)
3498 backlist->debug_on_exit = !NILP (flag);
3500 return flag;
3503 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3504 doc: /* Print a trace of Lisp function calls currently active.
3505 Output stream used is value of `standard-output'. */)
3506 (void)
3508 register struct backtrace *backlist = backtrace_list;
3509 Lisp_Object tail;
3510 Lisp_Object tem;
3511 struct gcpro gcpro1;
3512 Lisp_Object old_print_level = Vprint_level;
3514 if (NILP (Vprint_level))
3515 XSETFASTINT (Vprint_level, 8);
3517 tail = Qnil;
3518 GCPRO1 (tail);
3520 while (backlist)
3522 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3523 if (backlist->nargs == UNEVALLED)
3525 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3526 write_string ("\n", -1);
3528 else
3530 tem = *backlist->function;
3531 Fprin1 (tem, Qnil); /* This can QUIT. */
3532 write_string ("(", -1);
3533 if (backlist->nargs == MANY)
3534 { /* FIXME: Can this happen? */
3535 int i;
3536 for (tail = *backlist->args, i = 0;
3537 !NILP (tail);
3538 tail = Fcdr (tail), i = 1)
3540 if (i) write_string (" ", -1);
3541 Fprin1 (Fcar (tail), Qnil);
3544 else
3546 ptrdiff_t i;
3547 for (i = 0; i < backlist->nargs; i++)
3549 if (i) write_string (" ", -1);
3550 Fprin1 (backlist->args[i], Qnil);
3553 write_string (")\n", -1);
3555 backlist = backlist->next;
3558 Vprint_level = old_print_level;
3559 UNGCPRO;
3560 return Qnil;
3563 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3564 doc: /* Return the function and arguments NFRAMES up from current execution point.
3565 If that frame has not evaluated the arguments yet (or is a special form),
3566 the value is (nil FUNCTION ARG-FORMS...).
3567 If that frame has evaluated its arguments and called its function already,
3568 the value is (t FUNCTION ARG-VALUES...).
3569 A &rest arg is represented as the tail of the list ARG-VALUES.
3570 FUNCTION is whatever was supplied as car of evaluated list,
3571 or a lambda expression for macro calls.
3572 If NFRAMES is more than the number of frames, the value is nil. */)
3573 (Lisp_Object nframes)
3575 register struct backtrace *backlist = backtrace_list;
3576 register EMACS_INT i;
3577 Lisp_Object tem;
3579 CHECK_NATNUM (nframes);
3581 /* Find the frame requested. */
3582 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3583 backlist = backlist->next;
3585 if (!backlist)
3586 return Qnil;
3587 if (backlist->nargs == UNEVALLED)
3588 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3589 else
3591 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3592 tem = *backlist->args;
3593 else
3594 tem = Flist (backlist->nargs, backlist->args);
3596 return Fcons (Qt, Fcons (*backlist->function, tem));
3601 #if BYTE_MARK_STACK
3602 void
3603 mark_backtrace (void)
3605 register struct backtrace *backlist;
3606 ptrdiff_t i;
3608 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3610 mark_object (*backlist->function);
3612 if (backlist->nargs == UNEVALLED
3613 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3614 i = 1;
3615 else
3616 i = backlist->nargs;
3617 while (i--)
3618 mark_object (backlist->args[i]);
3621 #endif
3623 void
3624 syms_of_eval (void)
3626 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3627 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3628 If Lisp code tries to increase the total number past this amount,
3629 an error is signaled.
3630 You can safely use a value considerably larger than the default value,
3631 if that proves inconveniently small. However, if you increase it too far,
3632 Emacs could run out of memory trying to make the stack bigger. */);
3634 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3635 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3637 This limit serves to catch infinite recursions for you before they cause
3638 actual stack overflow in C, which would be fatal for Emacs.
3639 You can safely make it considerably larger than its default value,
3640 if that proves inconveniently small. However, if you increase it too far,
3641 Emacs could overflow the real C stack, and crash. */);
3643 DEFVAR_LISP ("quit-flag", Vquit_flag,
3644 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3645 If the value is t, that means do an ordinary quit.
3646 If the value equals `throw-on-input', that means quit by throwing
3647 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3648 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3649 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3650 Vquit_flag = Qnil;
3652 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3653 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3654 Note that `quit-flag' will still be set by typing C-g,
3655 so a quit will be signaled as soon as `inhibit-quit' is nil.
3656 To prevent this happening, set `quit-flag' to nil
3657 before making `inhibit-quit' nil. */);
3658 Vinhibit_quit = Qnil;
3660 DEFSYM (Qinhibit_quit, "inhibit-quit");
3661 DEFSYM (Qautoload, "autoload");
3662 DEFSYM (Qdebug_on_error, "debug-on-error");
3663 DEFSYM (Qmacro, "macro");
3664 DEFSYM (Qdeclare, "declare");
3666 /* Note that the process handling also uses Qexit, but we don't want
3667 to staticpro it twice, so we just do it here. */
3668 DEFSYM (Qexit, "exit");
3670 DEFSYM (Qinteractive, "interactive");
3671 DEFSYM (Qcommandp, "commandp");
3672 DEFSYM (Qdefun, "defun");
3673 DEFSYM (Qand_rest, "&rest");
3674 DEFSYM (Qand_optional, "&optional");
3675 DEFSYM (Qclosure, "closure");
3676 DEFSYM (Qdebug, "debug");
3678 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3679 doc: /* *Non-nil means enter debugger if an error is signaled.
3680 Does not apply to errors handled by `condition-case' or those
3681 matched by `debug-ignored-errors'.
3682 If the value is a list, an error only means to enter the debugger
3683 if one of its condition symbols appears in the list.
3684 When you evaluate an expression interactively, this variable
3685 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3686 The command `toggle-debug-on-error' toggles this.
3687 See also the variable `debug-on-quit'. */);
3688 Vdebug_on_error = Qnil;
3690 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3691 doc: /* *List of errors for which the debugger should not be called.
3692 Each element may be a condition-name or a regexp that matches error messages.
3693 If any element applies to a given error, that error skips the debugger
3694 and just returns to top level.
3695 This overrides the variable `debug-on-error'.
3696 It does not apply to errors handled by `condition-case'. */);
3697 Vdebug_ignored_errors = Qnil;
3699 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3700 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3701 Does not apply if quit is handled by a `condition-case'. */);
3702 debug_on_quit = 0;
3704 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3705 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3707 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3708 doc: /* Non-nil means debugger may continue execution.
3709 This is nil when the debugger is called under circumstances where it
3710 might not be safe to continue. */);
3711 debugger_may_continue = 1;
3713 DEFVAR_LISP ("debugger", Vdebugger,
3714 doc: /* Function to call to invoke debugger.
3715 If due to frame exit, args are `exit' and the value being returned;
3716 this function's value will be returned instead of that.
3717 If due to error, args are `error' and a list of the args to `signal'.
3718 If due to `apply' or `funcall' entry, one arg, `lambda'.
3719 If due to `eval' entry, one arg, t. */);
3720 Vdebugger = Qnil;
3722 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3723 doc: /* If non-nil, this is a function for `signal' to call.
3724 It receives the same arguments that `signal' was given.
3725 The Edebug package uses this to regain control. */);
3726 Vsignal_hook_function = Qnil;
3728 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3729 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3730 Note that `debug-on-error', `debug-on-quit' and friends
3731 still determine whether to handle the particular condition. */);
3732 Vdebug_on_signal = Qnil;
3734 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3735 doc: /* Function to process declarations in a macro definition.
3736 The function will be called with two args MACRO and DECL.
3737 MACRO is the name of the macro being defined.
3738 DECL is a list `(declare ...)' containing the declarations.
3739 The value the function returns is not used. */);
3740 Vmacro_declaration_function = Qnil;
3742 /* When lexical binding is being used,
3743 vinternal_interpreter_environment is non-nil, and contains an alist
3744 of lexically-bound variable, or (t), indicating an empty
3745 environment. The lisp name of this variable would be
3746 `internal-interpreter-environment' if it weren't hidden.
3747 Every element of this list can be either a cons (VAR . VAL)
3748 specifying a lexical binding, or a single symbol VAR indicating
3749 that this variable should use dynamic scoping. */
3750 DEFSYM (Qinternal_interpreter_environment, "internal-interpreter-environment");
3751 DEFVAR_LISP ("internal-interpreter-environment",
3752 Vinternal_interpreter_environment,
3753 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3754 When lexical binding is not being used, this variable is nil.
3755 A value of `(t)' indicates an empty environment, otherwise it is an
3756 alist of active lexical bindings. */);
3757 Vinternal_interpreter_environment = Qnil;
3758 /* Don't export this variable to Elisp, so no one can mess with it
3759 (Just imagine if someone makes it buffer-local). */
3760 Funintern (Qinternal_interpreter_environment, Qnil);
3762 DEFSYM (Vrun_hooks, "run-hooks");
3764 staticpro (&Vautoload_queue);
3765 Vautoload_queue = Qnil;
3766 staticpro (&Vsignaling_function);
3767 Vsignaling_function = Qnil;
3769 defsubr (&Sor);
3770 defsubr (&Sand);
3771 defsubr (&Sif);
3772 defsubr (&Scond);
3773 defsubr (&Sprogn);
3774 defsubr (&Sprog1);
3775 defsubr (&Sprog2);
3776 defsubr (&Ssetq);
3777 defsubr (&Squote);
3778 defsubr (&Sfunction);
3779 defsubr (&Sdefun);
3780 defsubr (&Sdefmacro);
3781 defsubr (&Sdefvar);
3782 defsubr (&Sdefvaralias);
3783 defsubr (&Sdefconst);
3784 defsubr (&Suser_variable_p);
3785 defsubr (&Slet);
3786 defsubr (&SletX);
3787 defsubr (&Swhile);
3788 defsubr (&Smacroexpand);
3789 defsubr (&Scatch);
3790 defsubr (&Sthrow);
3791 defsubr (&Sunwind_protect);
3792 defsubr (&Scondition_case);
3793 defsubr (&Ssignal);
3794 defsubr (&Sinteractive_p);
3795 defsubr (&Scalled_interactively_p);
3796 defsubr (&Scommandp);
3797 defsubr (&Sautoload);
3798 defsubr (&Seval);
3799 defsubr (&Sapply);
3800 defsubr (&Sfuncall);
3801 defsubr (&Srun_hooks);
3802 defsubr (&Srun_hook_with_args);
3803 defsubr (&Srun_hook_with_args_until_success);
3804 defsubr (&Srun_hook_with_args_until_failure);
3805 defsubr (&Srun_hook_wrapped);
3806 defsubr (&Sfetch_bytecode);
3807 defsubr (&Sbacktrace_debug);
3808 defsubr (&Sbacktrace);
3809 defsubr (&Sbacktrace_frame);
3810 defsubr (&Sspecial_variable_p);
3811 defsubr (&Sfunctionp);