quail/indian.el ("devanagari-itrans"): Add a few more useful keys (Bug#10935).
[emacs.git] / src / eval.c
blob4a3f5083b3b88a0f66cd5d3e556b98a5d4b1f07e
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 /* If non-nil, Lisp code must not be run since some part of Emacs is
128 in an inconsistent state. Currently, x-create-frame uses this to
129 avoid triggering window-configuration-change-hook while the new
130 frame is half-initialized. */
131 Lisp_Object inhibit_lisp_code;
133 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
134 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
135 static int interactive_p (int);
136 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
137 static Lisp_Object Ffetch_bytecode (Lisp_Object);
139 void
140 init_eval_once (void)
142 enum { size = 50 };
143 specpdl = (struct specbinding *) xmalloc (size * sizeof (struct specbinding));
144 specpdl_size = size;
145 specpdl_ptr = specpdl;
146 /* Don't forget to update docs (lispref node "Local Variables"). */
147 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
148 max_lisp_eval_depth = 600;
150 Vrun_hooks = Qnil;
153 void
154 init_eval (void)
156 specpdl_ptr = specpdl;
157 catchlist = 0;
158 handlerlist = 0;
159 backtrace_list = 0;
160 Vquit_flag = Qnil;
161 debug_on_next_call = 0;
162 lisp_eval_depth = 0;
163 #ifdef DEBUG_GCPRO
164 gcpro_level = 0;
165 #endif
166 /* This is less than the initial value of num_nonmacro_input_events. */
167 when_entered_debugger = -1;
170 /* Unwind-protect function used by call_debugger. */
172 static Lisp_Object
173 restore_stack_limits (Lisp_Object data)
175 max_specpdl_size = XINT (XCAR (data));
176 max_lisp_eval_depth = XINT (XCDR (data));
177 return Qnil;
180 /* Call the Lisp debugger, giving it argument ARG. */
182 static Lisp_Object
183 call_debugger (Lisp_Object arg)
185 int debug_while_redisplaying;
186 int count = SPECPDL_INDEX ();
187 Lisp_Object val;
188 EMACS_INT old_max = max_specpdl_size;
190 /* Temporarily bump up the stack limits,
191 so the debugger won't run out of stack. */
193 max_specpdl_size += 1;
194 record_unwind_protect (restore_stack_limits,
195 Fcons (make_number (old_max),
196 make_number (max_lisp_eval_depth)));
197 max_specpdl_size = old_max;
199 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
200 max_lisp_eval_depth = lisp_eval_depth + 40;
202 if (max_specpdl_size - 100 < SPECPDL_INDEX ())
203 max_specpdl_size = SPECPDL_INDEX () + 100;
205 #ifdef HAVE_WINDOW_SYSTEM
206 if (display_hourglass_p)
207 cancel_hourglass ();
208 #endif
210 debug_on_next_call = 0;
211 when_entered_debugger = num_nonmacro_input_events;
213 /* Resetting redisplaying_p to 0 makes sure that debug output is
214 displayed if the debugger is invoked during redisplay. */
215 debug_while_redisplaying = redisplaying_p;
216 redisplaying_p = 0;
217 specbind (intern ("debugger-may-continue"),
218 debug_while_redisplaying ? Qnil : Qt);
219 specbind (Qinhibit_redisplay, Qnil);
220 specbind (Qdebug_on_error, Qnil);
222 #if 0 /* Binding this prevents execution of Lisp code during
223 redisplay, which necessarily leads to display problems. */
224 specbind (Qinhibit_eval_during_redisplay, Qt);
225 #endif
227 val = apply1 (Vdebugger, arg);
229 /* Interrupting redisplay and resuming it later is not safe under
230 all circumstances. So, when the debugger returns, abort the
231 interrupted redisplay by going back to the top-level. */
232 if (debug_while_redisplaying)
233 Ftop_level ();
235 return unbind_to (count, val);
238 static void
239 do_debug_on_call (Lisp_Object code)
241 debug_on_next_call = 0;
242 backtrace_list->debug_on_exit = 1;
243 call_debugger (Fcons (code, Qnil));
246 /* NOTE!!! Every function that can call EVAL must protect its args
247 and temporaries from garbage collection while it needs them.
248 The definition of `For' shows what you have to do. */
250 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
251 doc: /* Eval args until one of them yields non-nil, then return that value.
252 The remaining args are not evalled at all.
253 If all args return nil, return nil.
254 usage: (or CONDITIONS...) */)
255 (Lisp_Object args)
257 register Lisp_Object val = Qnil;
258 struct gcpro gcpro1;
260 GCPRO1 (args);
262 while (CONSP (args))
264 val = eval_sub (XCAR (args));
265 if (!NILP (val))
266 break;
267 args = XCDR (args);
270 UNGCPRO;
271 return val;
274 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
275 doc: /* Eval args until one of them yields nil, then return nil.
276 The remaining args are not evalled at all.
277 If no arg yields nil, return the last arg's value.
278 usage: (and CONDITIONS...) */)
279 (Lisp_Object args)
281 register Lisp_Object val = Qt;
282 struct gcpro gcpro1;
284 GCPRO1 (args);
286 while (CONSP (args))
288 val = eval_sub (XCAR (args));
289 if (NILP (val))
290 break;
291 args = XCDR (args);
294 UNGCPRO;
295 return val;
298 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
299 doc: /* If COND yields non-nil, do THEN, else do ELSE...
300 Returns the value of THEN or the value of the last of the ELSE's.
301 THEN must be one expression, but ELSE... can be zero or more expressions.
302 If COND yields nil, and there are no ELSE's, the value is nil.
303 usage: (if COND THEN ELSE...) */)
304 (Lisp_Object args)
306 register Lisp_Object cond;
307 struct gcpro gcpro1;
309 GCPRO1 (args);
310 cond = eval_sub (Fcar (args));
311 UNGCPRO;
313 if (!NILP (cond))
314 return eval_sub (Fcar (Fcdr (args)));
315 return Fprogn (Fcdr (Fcdr (args)));
318 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
319 doc: /* Try each clause until one succeeds.
320 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
321 and, if the value is non-nil, this clause succeeds:
322 then the expressions in BODY are evaluated and the last one's
323 value is the value of the cond-form.
324 If no clause succeeds, cond returns nil.
325 If a clause has one element, as in (CONDITION),
326 CONDITION's value if non-nil is returned from the cond-form.
327 usage: (cond CLAUSES...) */)
328 (Lisp_Object args)
330 register Lisp_Object clause, val;
331 struct gcpro gcpro1;
333 val = Qnil;
334 GCPRO1 (args);
335 while (!NILP (args))
337 clause = Fcar (args);
338 val = eval_sub (Fcar (clause));
339 if (!NILP (val))
341 if (!EQ (XCDR (clause), Qnil))
342 val = Fprogn (XCDR (clause));
343 break;
345 args = XCDR (args);
347 UNGCPRO;
349 return val;
352 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
353 doc: /* Eval BODY forms sequentially and return value of last one.
354 usage: (progn BODY...) */)
355 (Lisp_Object args)
357 register Lisp_Object val = Qnil;
358 struct gcpro gcpro1;
360 GCPRO1 (args);
362 while (CONSP (args))
364 val = eval_sub (XCAR (args));
365 args = XCDR (args);
368 UNGCPRO;
369 return val;
372 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
373 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
374 The value of FIRST is saved during the evaluation of the remaining args,
375 whose values are discarded.
376 usage: (prog1 FIRST BODY...) */)
377 (Lisp_Object args)
379 Lisp_Object val;
380 register Lisp_Object args_left;
381 struct gcpro gcpro1, gcpro2;
382 register int argnum = 0;
384 if (NILP (args))
385 return Qnil;
387 args_left = args;
388 val = Qnil;
389 GCPRO2 (args, val);
393 Lisp_Object tem = eval_sub (XCAR (args_left));
394 if (!(argnum++))
395 val = tem;
396 args_left = XCDR (args_left);
398 while (CONSP (args_left));
400 UNGCPRO;
401 return val;
404 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
405 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
406 The value of FORM2 is saved during the evaluation of the
407 remaining args, whose values are discarded.
408 usage: (prog2 FORM1 FORM2 BODY...) */)
409 (Lisp_Object args)
411 Lisp_Object val;
412 register Lisp_Object args_left;
413 struct gcpro gcpro1, gcpro2;
414 register int argnum = -1;
416 val = Qnil;
418 if (NILP (args))
419 return Qnil;
421 args_left = args;
422 val = Qnil;
423 GCPRO2 (args, val);
427 Lisp_Object tem = eval_sub (XCAR (args_left));
428 if (!(argnum++))
429 val = tem;
430 args_left = XCDR (args_left);
432 while (CONSP (args_left));
434 UNGCPRO;
435 return val;
438 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
439 doc: /* Set each SYM to the value of its VAL.
440 The symbols SYM are variables; they are literal (not evaluated).
441 The values VAL are expressions; they are evaluated.
442 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
443 The second VAL is not computed until after the first SYM is set, and so on;
444 each VAL can use the new value of variables set earlier in the `setq'.
445 The return value of the `setq' form is the value of the last VAL.
446 usage: (setq [SYM VAL]...) */)
447 (Lisp_Object args)
449 register Lisp_Object args_left;
450 register Lisp_Object val, sym, lex_binding;
451 struct gcpro gcpro1;
453 if (NILP (args))
454 return Qnil;
456 args_left = args;
457 GCPRO1 (args);
461 val = eval_sub (Fcar (Fcdr (args_left)));
462 sym = Fcar (args_left);
464 /* Like for eval_sub, we do not check declared_special here since
465 it's been done when let-binding. */
466 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
467 && SYMBOLP (sym)
468 && !NILP (lex_binding
469 = Fassq (sym, Vinternal_interpreter_environment)))
470 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
471 else
472 Fset (sym, val); /* SYM is dynamically bound. */
474 args_left = Fcdr (Fcdr (args_left));
476 while (!NILP (args_left));
478 UNGCPRO;
479 return val;
482 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
483 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
484 Warning: `quote' does not construct its return value, but just returns
485 the value that was pre-constructed by the Lisp reader (see info node
486 `(elisp)Printed Representation').
487 This means that '(a . b) is not identical to (cons 'a 'b): the former
488 does not cons. Quoting should be reserved for constants that will
489 never be modified by side-effects, unless you like self-modifying code.
490 See the common pitfall in info node `(elisp)Rearrangement' for an example
491 of unexpected results when a quoted object is modified.
492 usage: (quote ARG) */)
493 (Lisp_Object args)
495 if (!NILP (Fcdr (args)))
496 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
497 return Fcar (args);
500 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
501 doc: /* Like `quote', but preferred for objects which are functions.
502 In byte compilation, `function' causes its argument to be compiled.
503 `quote' cannot do that.
504 usage: (function ARG) */)
505 (Lisp_Object args)
507 Lisp_Object quoted = XCAR (args);
509 if (!NILP (Fcdr (args)))
510 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
512 if (!NILP (Vinternal_interpreter_environment)
513 && CONSP (quoted)
514 && EQ (XCAR (quoted), Qlambda))
515 /* This is a lambda expression within a lexical environment;
516 return an interpreted closure instead of a simple lambda. */
517 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
518 XCDR (quoted)));
519 else
520 /* Simply quote the argument. */
521 return quoted;
525 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
526 doc: /* Return t if the containing function was run directly by user input.
527 This means that the function was called with `call-interactively'
528 \(which includes being called as the binding of a key)
529 and input is currently coming from the keyboard (not a keyboard macro),
530 and Emacs is not running in batch mode (`noninteractive' is nil).
532 The only known proper use of `interactive-p' is in deciding whether to
533 display a helpful message, or how to display it. If you're thinking
534 of using it for any other purpose, it is quite likely that you're
535 making a mistake. Think: what do you want to do when the command is
536 called from a keyboard macro?
538 To test whether your function was called with `call-interactively',
539 either (i) add an extra optional argument and give it an `interactive'
540 spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
541 use `called-interactively-p'. */)
542 (void)
544 return interactive_p (1) ? Qt : Qnil;
548 DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
549 doc: /* Return t if the containing function was called by `call-interactively'.
550 If KIND is `interactive', then only return t if the call was made
551 interactively by the user, i.e. not in `noninteractive' mode nor
552 when `executing-kbd-macro'.
553 If KIND is `any', on the other hand, it will return t for any kind of
554 interactive call, including being called as the binding of a key, or
555 from a keyboard macro, or in `noninteractive' mode.
557 The only known proper use of `interactive' for KIND is in deciding
558 whether to display a helpful message, or how to display it. If you're
559 thinking of using it for any other purpose, it is quite likely that
560 you're making a mistake. Think: what do you want to do when the
561 command is called from a keyboard macro?
563 This function is meant for implementing advice and other
564 function-modifying features. Instead of using this, it is sometimes
565 cleaner to give your function an extra optional argument whose
566 `interactive' spec specifies non-nil unconditionally (\"p\" is a good
567 way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
568 (Lisp_Object kind)
570 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
571 && interactive_p (1)) ? Qt : Qnil;
575 /* Return 1 if function in which this appears was called using
576 call-interactively.
578 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
579 called is a built-in. */
581 static int
582 interactive_p (int exclude_subrs_p)
584 struct backtrace *btp;
585 Lisp_Object fun;
587 btp = backtrace_list;
589 /* If this isn't a byte-compiled function, there may be a frame at
590 the top for Finteractive_p. If so, skip it. */
591 fun = Findirect_function (*btp->function, Qnil);
592 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
593 || XSUBR (fun) == &Scalled_interactively_p))
594 btp = btp->next;
596 /* If we're running an Emacs 18-style byte-compiled function, there
597 may be a frame for Fbytecode at the top level. In any version of
598 Emacs there can be Fbytecode frames for subexpressions evaluated
599 inside catch and condition-case. Skip past them.
601 If this isn't a byte-compiled function, then we may now be
602 looking at several frames for special forms. Skip past them. */
603 while (btp
604 && (EQ (*btp->function, Qbytecode)
605 || btp->nargs == UNEVALLED))
606 btp = btp->next;
608 /* `btp' now points at the frame of the innermost function that isn't
609 a special form, ignoring frames for Finteractive_p and/or
610 Fbytecode at the top. If this frame is for a built-in function
611 (such as load or eval-region) return nil. */
612 fun = Findirect_function (*btp->function, Qnil);
613 if (exclude_subrs_p && SUBRP (fun))
614 return 0;
616 /* `btp' points to the frame of a Lisp function that called interactive-p.
617 Return t if that function was called interactively. */
618 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
619 return 1;
620 return 0;
624 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
625 doc: /* Define NAME as a function.
626 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
627 See also the function `interactive'.
628 usage: (defun NAME ARGLIST [DOCSTRING] BODY...) */)
629 (Lisp_Object args)
631 register Lisp_Object fn_name;
632 register Lisp_Object defn;
634 fn_name = Fcar (args);
635 CHECK_SYMBOL (fn_name);
636 defn = Fcons (Qlambda, Fcdr (args));
637 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
638 defn = Ffunction (Fcons (defn, Qnil));
639 if (!NILP (Vpurify_flag))
640 defn = Fpurecopy (defn);
641 if (CONSP (XSYMBOL (fn_name)->function)
642 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
643 LOADHIST_ATTACH (Fcons (Qt, fn_name));
644 Ffset (fn_name, defn);
645 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
646 return fn_name;
649 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
650 doc: /* Define NAME as a macro.
651 The actual definition looks like
652 (macro lambda ARGLIST [DOCSTRING] [DECL] BODY...).
653 When the macro is called, as in (NAME ARGS...),
654 the function (lambda ARGLIST BODY...) is applied to
655 the list ARGS... as it appears in the expression,
656 and the result should be a form to be evaluated instead of the original.
658 DECL is a declaration, optional, which can specify how to indent
659 calls to this macro, how Edebug should handle it, and which argument
660 should be treated as documentation. It looks like this:
661 (declare SPECS...)
662 The elements can look like this:
663 (indent INDENT)
664 Set NAME's `lisp-indent-function' property to INDENT.
666 (debug DEBUG)
667 Set NAME's `edebug-form-spec' property to DEBUG. (This is
668 equivalent to writing a `def-edebug-spec' for the macro.)
670 (doc-string ELT)
671 Set NAME's `doc-string-elt' property to ELT.
673 usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...) */)
674 (Lisp_Object args)
676 register Lisp_Object fn_name;
677 register Lisp_Object defn;
678 Lisp_Object lambda_list, doc, tail;
680 fn_name = Fcar (args);
681 CHECK_SYMBOL (fn_name);
682 lambda_list = Fcar (Fcdr (args));
683 tail = Fcdr (Fcdr (args));
685 doc = Qnil;
686 if (STRINGP (Fcar (tail)))
688 doc = XCAR (tail);
689 tail = XCDR (tail);
692 if (CONSP (Fcar (tail))
693 && EQ (Fcar (Fcar (tail)), Qdeclare))
695 if (!NILP (Vmacro_declaration_function))
697 struct gcpro gcpro1;
698 GCPRO1 (args);
699 call2 (Vmacro_declaration_function, fn_name, Fcar (tail));
700 UNGCPRO;
703 tail = Fcdr (tail);
706 if (NILP (doc))
707 tail = Fcons (lambda_list, tail);
708 else
709 tail = Fcons (lambda_list, Fcons (doc, tail));
711 defn = Fcons (Qlambda, tail);
712 if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization! */
713 defn = Ffunction (Fcons (defn, Qnil));
714 defn = Fcons (Qmacro, defn);
716 if (!NILP (Vpurify_flag))
717 defn = Fpurecopy (defn);
718 if (CONSP (XSYMBOL (fn_name)->function)
719 && EQ (XCAR (XSYMBOL (fn_name)->function), Qautoload))
720 LOADHIST_ATTACH (Fcons (Qt, fn_name));
721 Ffset (fn_name, defn);
722 LOADHIST_ATTACH (Fcons (Qdefun, fn_name));
723 return fn_name;
727 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
728 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
729 Aliased variables always have the same value; setting one sets the other.
730 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
731 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
732 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
733 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
734 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
735 The return value is BASE-VARIABLE. */)
736 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
738 struct Lisp_Symbol *sym;
740 CHECK_SYMBOL (new_alias);
741 CHECK_SYMBOL (base_variable);
743 sym = XSYMBOL (new_alias);
745 if (sym->constant)
746 /* Not sure why, but why not? */
747 error ("Cannot make a constant an alias");
749 switch (sym->redirect)
751 case SYMBOL_FORWARDED:
752 error ("Cannot make an internal variable an alias");
753 case SYMBOL_LOCALIZED:
754 error ("Don't know how to make a localized variable an alias");
757 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
758 If n_a is bound, but b_v is not, set the value of b_v to n_a,
759 so that old-code that affects n_a before the aliasing is setup
760 still works. */
761 if (NILP (Fboundp (base_variable)))
762 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
765 struct specbinding *p;
767 for (p = specpdl_ptr - 1; p >= specpdl; p--)
768 if (p->func == NULL
769 && (EQ (new_alias,
770 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
771 error ("Don't know how to make a let-bound variable an alias");
774 sym->declared_special = 1;
775 XSYMBOL (base_variable)->declared_special = 1;
776 sym->redirect = SYMBOL_VARALIAS;
777 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
778 sym->constant = SYMBOL_CONSTANT_P (base_variable);
779 LOADHIST_ATTACH (new_alias);
780 /* Even if docstring is nil: remove old docstring. */
781 Fput (new_alias, Qvariable_documentation, docstring);
783 return base_variable;
787 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
788 doc: /* Define SYMBOL as a variable, and return SYMBOL.
789 You are not required to define a variable in order to use it, but
790 defining it lets you supply an initial value and documentation, which
791 can be referred to by the Emacs help facilities and other programming
792 tools. The `defvar' form also declares the variable as \"special\",
793 so that it is always dynamically bound even if `lexical-binding' is t.
795 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
796 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
797 default value is what is set; buffer-local values are not affected.
798 If INITVALUE is missing, SYMBOL's value is not set.
800 If SYMBOL has a local binding, then this form affects the local
801 binding. This is usually not what you want. Thus, if you need to
802 load a file defining variables, with this form or with `defconst' or
803 `defcustom', you should always load that file _outside_ any bindings
804 for these variables. \(`defconst' and `defcustom' behave similarly in
805 this respect.)
807 The optional argument DOCSTRING is a documentation string for the
808 variable.
810 To define a user option, use `defcustom' instead of `defvar'.
811 The function `user-variable-p' also identifies a variable as a user
812 option if its DOCSTRING starts with *, but this behavior is obsolete.
813 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
814 (Lisp_Object args)
816 register Lisp_Object sym, tem, tail;
818 sym = Fcar (args);
819 tail = Fcdr (args);
820 if (!NILP (Fcdr (Fcdr (tail))))
821 error ("Too many arguments");
823 tem = Fdefault_boundp (sym);
824 if (!NILP (tail))
826 /* Do it before evaluating the initial value, for self-references. */
827 XSYMBOL (sym)->declared_special = 1;
829 if (SYMBOL_CONSTANT_P (sym))
831 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
832 Lisp_Object tem1 = Fcar (tail);
833 if (! (CONSP (tem1)
834 && EQ (XCAR (tem1), Qquote)
835 && CONSP (XCDR (tem1))
836 && EQ (XCAR (XCDR (tem1)), sym)))
837 error ("Constant symbol `%s' specified in defvar",
838 SDATA (SYMBOL_NAME (sym)));
841 if (NILP (tem))
842 Fset_default (sym, eval_sub (Fcar (tail)));
843 else
844 { /* Check if there is really a global binding rather than just a let
845 binding that shadows the global unboundness of the var. */
846 volatile struct specbinding *pdl = specpdl_ptr;
847 while (--pdl >= specpdl)
849 if (EQ (pdl->symbol, sym) && !pdl->func
850 && EQ (pdl->old_value, Qunbound))
852 message_with_string ("Warning: defvar ignored because %s is let-bound",
853 SYMBOL_NAME (sym), 1);
854 break;
858 tail = Fcdr (tail);
859 tem = Fcar (tail);
860 if (!NILP (tem))
862 if (!NILP (Vpurify_flag))
863 tem = Fpurecopy (tem);
864 Fput (sym, Qvariable_documentation, tem);
866 LOADHIST_ATTACH (sym);
868 else if (!NILP (Vinternal_interpreter_environment)
869 && !XSYMBOL (sym)->declared_special)
870 /* A simple (defvar foo) with lexical scoping does "nothing" except
871 declare that var to be dynamically scoped *locally* (i.e. within
872 the current file or let-block). */
873 Vinternal_interpreter_environment =
874 Fcons (sym, Vinternal_interpreter_environment);
875 else
877 /* Simple (defvar <var>) should not count as a definition at all.
878 It could get in the way of other definitions, and unloading this
879 package could try to make the variable unbound. */
882 return sym;
885 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
886 doc: /* Define SYMBOL as a constant variable.
887 This declares that neither programs nor users should ever change the
888 value. This constancy is not actually enforced by Emacs Lisp, but
889 SYMBOL is marked as a special variable so that it is never lexically
890 bound.
892 The `defconst' form always sets the value of SYMBOL to the result of
893 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
894 what is set; buffer-local values are not affected. If SYMBOL has a
895 local binding, then this form sets the local binding's value.
896 However, you should normally not make local bindings for variables
897 defined with this form.
899 The optional DOCSTRING specifies the variable's documentation string.
900 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
901 (Lisp_Object args)
903 register Lisp_Object sym, tem;
905 sym = Fcar (args);
906 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
907 error ("Too many arguments");
909 tem = eval_sub (Fcar (Fcdr (args)));
910 if (!NILP (Vpurify_flag))
911 tem = Fpurecopy (tem);
912 Fset_default (sym, tem);
913 XSYMBOL (sym)->declared_special = 1;
914 tem = Fcar (Fcdr (Fcdr (args)));
915 if (!NILP (tem))
917 if (!NILP (Vpurify_flag))
918 tem = Fpurecopy (tem);
919 Fput (sym, Qvariable_documentation, tem);
921 Fput (sym, Qrisky_local_variable, Qt);
922 LOADHIST_ATTACH (sym);
923 return sym;
926 /* Error handler used in Fuser_variable_p. */
927 static Lisp_Object
928 user_variable_p_eh (Lisp_Object ignore)
930 return Qnil;
933 static Lisp_Object
934 lisp_indirect_variable (Lisp_Object sym)
936 struct Lisp_Symbol *s = indirect_variable (XSYMBOL (sym));
937 XSETSYMBOL (sym, s);
938 return sym;
941 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
942 doc: /* Return t if VARIABLE is intended to be set and modified by users.
943 \(The alternative is a variable used internally in a Lisp program.)
945 This function returns t if (i) the first character of its
946 documentation is `*', or (ii) it is customizable (its property list
947 contains a non-nil value of `standard-value' or `custom-autoload'), or
948 \(iii) it is an alias for a user variable.
950 But condition (i) is considered obsolete, so for most purposes this is
951 equivalent to `custom-variable-p'. */)
952 (Lisp_Object variable)
954 Lisp_Object documentation;
956 if (!SYMBOLP (variable))
957 return Qnil;
959 /* If indirect and there's an alias loop, don't check anything else. */
960 if (XSYMBOL (variable)->redirect == SYMBOL_VARALIAS
961 && NILP (internal_condition_case_1 (lisp_indirect_variable, variable,
962 Qt, user_variable_p_eh)))
963 return Qnil;
965 while (1)
967 documentation = Fget (variable, Qvariable_documentation);
968 if (INTEGERP (documentation) && XINT (documentation) < 0)
969 return Qt;
970 if (STRINGP (documentation)
971 && ((unsigned char) SREF (documentation, 0) == '*'))
972 return Qt;
973 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
974 if (CONSP (documentation)
975 && STRINGP (XCAR (documentation))
976 && INTEGERP (XCDR (documentation))
977 && XINT (XCDR (documentation)) < 0)
978 return Qt;
979 /* Customizable? See `custom-variable-p'. */
980 if ((!NILP (Fget (variable, intern ("standard-value"))))
981 || (!NILP (Fget (variable, intern ("custom-autoload")))))
982 return Qt;
984 if (!(XSYMBOL (variable)->redirect == SYMBOL_VARALIAS))
985 return Qnil;
987 /* An indirect variable? Let's follow the chain. */
988 XSETSYMBOL (variable, SYMBOL_ALIAS (XSYMBOL (variable)));
992 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
993 doc: /* Bind variables according to VARLIST then eval BODY.
994 The value of the last form in BODY is returned.
995 Each element of VARLIST is a symbol (which is bound to nil)
996 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
997 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
998 usage: (let* VARLIST BODY...) */)
999 (Lisp_Object args)
1001 Lisp_Object varlist, var, val, elt, lexenv;
1002 int count = SPECPDL_INDEX ();
1003 struct gcpro gcpro1, gcpro2, gcpro3;
1005 GCPRO3 (args, elt, varlist);
1007 lexenv = Vinternal_interpreter_environment;
1009 varlist = Fcar (args);
1010 while (CONSP (varlist))
1012 QUIT;
1014 elt = XCAR (varlist);
1015 if (SYMBOLP (elt))
1017 var = elt;
1018 val = Qnil;
1020 else if (! NILP (Fcdr (Fcdr (elt))))
1021 signal_error ("`let' bindings can have only one value-form", elt);
1022 else
1024 var = Fcar (elt);
1025 val = eval_sub (Fcar (Fcdr (elt)));
1028 if (!NILP (lexenv) && SYMBOLP (var)
1029 && !XSYMBOL (var)->declared_special
1030 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1031 /* Lexically bind VAR by adding it to the interpreter's binding
1032 alist. */
1034 Lisp_Object newenv
1035 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
1036 if (EQ (Vinternal_interpreter_environment, lexenv))
1037 /* Save the old lexical environment on the specpdl stack,
1038 but only for the first lexical binding, since we'll never
1039 need to revert to one of the intermediate ones. */
1040 specbind (Qinternal_interpreter_environment, newenv);
1041 else
1042 Vinternal_interpreter_environment = newenv;
1044 else
1045 specbind (var, val);
1047 varlist = XCDR (varlist);
1049 UNGCPRO;
1050 val = Fprogn (Fcdr (args));
1051 return unbind_to (count, val);
1054 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1055 doc: /* Bind variables according to VARLIST then eval BODY.
1056 The value of the last form in BODY is returned.
1057 Each element of VARLIST is a symbol (which is bound to nil)
1058 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
1059 All the VALUEFORMs are evalled before any symbols are bound.
1060 usage: (let VARLIST BODY...) */)
1061 (Lisp_Object args)
1063 Lisp_Object *temps, tem, lexenv;
1064 register Lisp_Object elt, varlist;
1065 int count = SPECPDL_INDEX ();
1066 ptrdiff_t argnum;
1067 struct gcpro gcpro1, gcpro2;
1068 USE_SAFE_ALLOCA;
1070 varlist = Fcar (args);
1072 /* Make space to hold the values to give the bound variables. */
1073 elt = Flength (varlist);
1074 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
1076 /* Compute the values and store them in `temps'. */
1078 GCPRO2 (args, *temps);
1079 gcpro2.nvars = 0;
1081 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1083 QUIT;
1084 elt = XCAR (varlist);
1085 if (SYMBOLP (elt))
1086 temps [argnum++] = Qnil;
1087 else if (! NILP (Fcdr (Fcdr (elt))))
1088 signal_error ("`let' bindings can have only one value-form", elt);
1089 else
1090 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
1091 gcpro2.nvars = argnum;
1093 UNGCPRO;
1095 lexenv = Vinternal_interpreter_environment;
1097 varlist = Fcar (args);
1098 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
1100 Lisp_Object var;
1102 elt = XCAR (varlist);
1103 var = SYMBOLP (elt) ? elt : Fcar (elt);
1104 tem = temps[argnum++];
1106 if (!NILP (lexenv) && SYMBOLP (var)
1107 && !XSYMBOL (var)->declared_special
1108 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
1109 /* Lexically bind VAR by adding it to the lexenv alist. */
1110 lexenv = Fcons (Fcons (var, tem), lexenv);
1111 else
1112 /* Dynamically bind VAR. */
1113 specbind (var, tem);
1116 if (!EQ (lexenv, Vinternal_interpreter_environment))
1117 /* Instantiate a new lexical environment. */
1118 specbind (Qinternal_interpreter_environment, lexenv);
1120 elt = Fprogn (Fcdr (args));
1121 SAFE_FREE ();
1122 return unbind_to (count, elt);
1125 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1126 doc: /* If TEST yields non-nil, eval BODY... and repeat.
1127 The order of execution is thus TEST, BODY, TEST, BODY and so on
1128 until TEST returns nil.
1129 usage: (while TEST BODY...) */)
1130 (Lisp_Object args)
1132 Lisp_Object test, body;
1133 struct gcpro gcpro1, gcpro2;
1135 GCPRO2 (test, body);
1137 test = Fcar (args);
1138 body = Fcdr (args);
1139 while (!NILP (eval_sub (test)))
1141 QUIT;
1142 Fprogn (body);
1145 UNGCPRO;
1146 return Qnil;
1149 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
1150 doc: /* Return result of expanding macros at top level of FORM.
1151 If FORM is not a macro call, it is returned unchanged.
1152 Otherwise, the macro is expanded and the expansion is considered
1153 in place of FORM. When a non-macro-call results, it is returned.
1155 The second optional arg ENVIRONMENT specifies an environment of macro
1156 definitions to shadow the loaded ones for use in file byte-compilation. */)
1157 (Lisp_Object form, Lisp_Object environment)
1159 /* With cleanups from Hallvard Furuseth. */
1160 register Lisp_Object expander, sym, def, tem;
1162 while (1)
1164 /* Come back here each time we expand a macro call,
1165 in case it expands into another macro call. */
1166 if (!CONSP (form))
1167 break;
1168 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1169 def = sym = XCAR (form);
1170 tem = Qnil;
1171 /* Trace symbols aliases to other symbols
1172 until we get a symbol that is not an alias. */
1173 while (SYMBOLP (def))
1175 QUIT;
1176 sym = def;
1177 tem = Fassq (sym, environment);
1178 if (NILP (tem))
1180 def = XSYMBOL (sym)->function;
1181 if (!EQ (def, Qunbound))
1182 continue;
1184 break;
1186 /* Right now TEM is the result from SYM in ENVIRONMENT,
1187 and if TEM is nil then DEF is SYM's function definition. */
1188 if (NILP (tem))
1190 /* SYM is not mentioned in ENVIRONMENT.
1191 Look at its function definition. */
1192 if (EQ (def, Qunbound) || !CONSP (def))
1193 /* Not defined or definition not suitable. */
1194 break;
1195 if (EQ (XCAR (def), Qautoload))
1197 /* Autoloading function: will it be a macro when loaded? */
1198 tem = Fnth (make_number (4), def);
1199 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1200 /* Yes, load it and try again. */
1202 struct gcpro gcpro1;
1203 GCPRO1 (form);
1204 do_autoload (def, sym);
1205 UNGCPRO;
1206 continue;
1208 else
1209 break;
1211 else if (!EQ (XCAR (def), Qmacro))
1212 break;
1213 else expander = XCDR (def);
1215 else
1217 expander = XCDR (tem);
1218 if (NILP (expander))
1219 break;
1221 form = apply1 (expander, XCDR (form));
1223 return form;
1226 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1227 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1228 TAG is evalled to get the tag to use; it must not be nil.
1230 Then the BODY is executed.
1231 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1232 If no throw happens, `catch' returns the value of the last BODY form.
1233 If a throw happens, it specifies the value to return from `catch'.
1234 usage: (catch TAG BODY...) */)
1235 (Lisp_Object args)
1237 register Lisp_Object tag;
1238 struct gcpro gcpro1;
1240 GCPRO1 (args);
1241 tag = eval_sub (Fcar (args));
1242 UNGCPRO;
1243 return internal_catch (tag, Fprogn, Fcdr (args));
1246 /* Set up a catch, then call C function FUNC on argument ARG.
1247 FUNC should return a Lisp_Object.
1248 This is how catches are done from within C code. */
1250 Lisp_Object
1251 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1253 /* This structure is made part of the chain `catchlist'. */
1254 struct catchtag c;
1256 /* Fill in the components of c, and put it on the list. */
1257 c.next = catchlist;
1258 c.tag = tag;
1259 c.val = Qnil;
1260 c.backlist = backtrace_list;
1261 c.handlerlist = handlerlist;
1262 c.lisp_eval_depth = lisp_eval_depth;
1263 c.pdlcount = SPECPDL_INDEX ();
1264 c.poll_suppress_count = poll_suppress_count;
1265 c.interrupt_input_blocked = interrupt_input_blocked;
1266 c.gcpro = gcprolist;
1267 c.byte_stack = byte_stack_list;
1268 catchlist = &c;
1270 /* Call FUNC. */
1271 if (! _setjmp (c.jmp))
1272 c.val = (*func) (arg);
1274 /* Throw works by a longjmp that comes right here. */
1275 catchlist = c.next;
1276 return c.val;
1279 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1280 jump to that CATCH, returning VALUE as the value of that catch.
1282 This is the guts Fthrow and Fsignal; they differ only in the way
1283 they choose the catch tag to throw to. A catch tag for a
1284 condition-case form has a TAG of Qnil.
1286 Before each catch is discarded, unbind all special bindings and
1287 execute all unwind-protect clauses made above that catch. Unwind
1288 the handler stack as we go, so that the proper handlers are in
1289 effect for each unwind-protect clause we run. At the end, restore
1290 some static info saved in CATCH, and longjmp to the location
1291 specified in the
1293 This is used for correct unwinding in Fthrow and Fsignal. */
1295 static void
1296 unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1298 register int last_time;
1300 /* Save the value in the tag. */
1301 catch->val = value;
1303 /* Restore certain special C variables. */
1304 set_poll_suppress_count (catch->poll_suppress_count);
1305 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked);
1306 handling_signal = 0;
1307 immediate_quit = 0;
1311 last_time = catchlist == catch;
1313 /* Unwind the specpdl stack, and then restore the proper set of
1314 handlers. */
1315 unbind_to (catchlist->pdlcount, Qnil);
1316 handlerlist = catchlist->handlerlist;
1317 catchlist = catchlist->next;
1319 while (! last_time);
1321 #if HAVE_X_WINDOWS
1322 /* If x_catch_errors was done, turn it off now.
1323 (First we give unbind_to a chance to do that.) */
1324 #if 0 /* This would disable x_catch_errors after x_connection_closed.
1325 The catch must remain in effect during that delicate
1326 state. --lorentey */
1327 x_fully_uncatch_errors ();
1328 #endif
1329 #endif
1331 byte_stack_list = catch->byte_stack;
1332 gcprolist = catch->gcpro;
1333 #ifdef DEBUG_GCPRO
1334 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1335 #endif
1336 backtrace_list = catch->backlist;
1337 lisp_eval_depth = catch->lisp_eval_depth;
1339 _longjmp (catch->jmp, 1);
1342 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1343 doc: /* Throw to the catch for TAG and return VALUE from it.
1344 Both TAG and VALUE are evalled. */)
1345 (register Lisp_Object tag, Lisp_Object value)
1347 register struct catchtag *c;
1349 if (!NILP (tag))
1350 for (c = catchlist; c; c = c->next)
1352 if (EQ (c->tag, tag))
1353 unwind_to_catch (c, value);
1355 xsignal2 (Qno_catch, tag, value);
1359 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1360 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1361 If BODYFORM completes normally, its value is returned
1362 after executing the UNWINDFORMS.
1363 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1364 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1365 (Lisp_Object args)
1367 Lisp_Object val;
1368 int count = SPECPDL_INDEX ();
1370 record_unwind_protect (Fprogn, Fcdr (args));
1371 val = eval_sub (Fcar (args));
1372 return unbind_to (count, val);
1375 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1376 doc: /* Regain control when an error is signaled.
1377 Executes BODYFORM and returns its value if no error happens.
1378 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1379 where the BODY is made of Lisp expressions.
1381 A handler is applicable to an error
1382 if CONDITION-NAME is one of the error's condition names.
1383 If an error happens, the first applicable handler is run.
1385 The car of a handler may be a list of condition names instead of a
1386 single condition name; then it handles all of them. If the special
1387 condition name `debug' is present in this list, it allows another
1388 condition in the list to run the debugger if `debug-on-error' and the
1389 other usual mechanisms says it should (otherwise, `condition-case'
1390 suppresses the debugger).
1392 When a handler handles an error, control returns to the `condition-case'
1393 and it executes the handler's BODY...
1394 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1395 \(If VAR is nil, the handler can't access that information.)
1396 Then the value of the last BODY form is returned from the `condition-case'
1397 expression.
1399 See also the function `signal' for more info.
1400 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1401 (Lisp_Object args)
1403 register Lisp_Object bodyform, handlers;
1404 volatile Lisp_Object var;
1406 var = Fcar (args);
1407 bodyform = Fcar (Fcdr (args));
1408 handlers = Fcdr (Fcdr (args));
1410 return internal_lisp_condition_case (var, bodyform, handlers);
1413 /* Like Fcondition_case, but the args are separate
1414 rather than passed in a list. Used by Fbyte_code. */
1416 Lisp_Object
1417 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1418 Lisp_Object handlers)
1420 Lisp_Object val;
1421 struct catchtag c;
1422 struct handler h;
1424 CHECK_SYMBOL (var);
1426 for (val = handlers; CONSP (val); val = XCDR (val))
1428 Lisp_Object tem;
1429 tem = XCAR (val);
1430 if (! (NILP (tem)
1431 || (CONSP (tem)
1432 && (SYMBOLP (XCAR (tem))
1433 || CONSP (XCAR (tem))))))
1434 error ("Invalid condition handler: %s",
1435 SDATA (Fprin1_to_string (tem, Qt)));
1438 c.tag = Qnil;
1439 c.val = Qnil;
1440 c.backlist = backtrace_list;
1441 c.handlerlist = handlerlist;
1442 c.lisp_eval_depth = lisp_eval_depth;
1443 c.pdlcount = SPECPDL_INDEX ();
1444 c.poll_suppress_count = poll_suppress_count;
1445 c.interrupt_input_blocked = interrupt_input_blocked;
1446 c.gcpro = gcprolist;
1447 c.byte_stack = byte_stack_list;
1448 if (_setjmp (c.jmp))
1450 if (!NILP (h.var))
1451 specbind (h.var, c.val);
1452 val = Fprogn (Fcdr (h.chosen_clause));
1454 /* Note that this just undoes the binding of h.var; whoever
1455 longjumped to us unwound the stack to c.pdlcount before
1456 throwing. */
1457 unbind_to (c.pdlcount, Qnil);
1458 return val;
1460 c.next = catchlist;
1461 catchlist = &c;
1463 h.var = var;
1464 h.handler = handlers;
1465 h.next = handlerlist;
1466 h.tag = &c;
1467 handlerlist = &h;
1469 val = eval_sub (bodyform);
1470 catchlist = c.next;
1471 handlerlist = h.next;
1472 return val;
1475 /* Call the function BFUN with no arguments, catching errors within it
1476 according to HANDLERS. If there is an error, call HFUN with
1477 one argument which is the data that describes the error:
1478 (SIGNALNAME . DATA)
1480 HANDLERS can be a list of conditions to catch.
1481 If HANDLERS is Qt, catch all errors.
1482 If HANDLERS is Qerror, catch all errors
1483 but allow the debugger to run if that is enabled. */
1485 Lisp_Object
1486 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1487 Lisp_Object (*hfun) (Lisp_Object))
1489 Lisp_Object val;
1490 struct catchtag c;
1491 struct handler h;
1493 c.tag = Qnil;
1494 c.val = Qnil;
1495 c.backlist = backtrace_list;
1496 c.handlerlist = handlerlist;
1497 c.lisp_eval_depth = lisp_eval_depth;
1498 c.pdlcount = SPECPDL_INDEX ();
1499 c.poll_suppress_count = poll_suppress_count;
1500 c.interrupt_input_blocked = interrupt_input_blocked;
1501 c.gcpro = gcprolist;
1502 c.byte_stack = byte_stack_list;
1503 if (_setjmp (c.jmp))
1505 return (*hfun) (c.val);
1507 c.next = catchlist;
1508 catchlist = &c;
1509 h.handler = handlers;
1510 h.var = Qnil;
1511 h.next = handlerlist;
1512 h.tag = &c;
1513 handlerlist = &h;
1515 val = (*bfun) ();
1516 catchlist = c.next;
1517 handlerlist = h.next;
1518 return val;
1521 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1523 Lisp_Object
1524 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1525 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1527 Lisp_Object val;
1528 struct catchtag c;
1529 struct handler h;
1531 c.tag = Qnil;
1532 c.val = Qnil;
1533 c.backlist = backtrace_list;
1534 c.handlerlist = handlerlist;
1535 c.lisp_eval_depth = lisp_eval_depth;
1536 c.pdlcount = SPECPDL_INDEX ();
1537 c.poll_suppress_count = poll_suppress_count;
1538 c.interrupt_input_blocked = interrupt_input_blocked;
1539 c.gcpro = gcprolist;
1540 c.byte_stack = byte_stack_list;
1541 if (_setjmp (c.jmp))
1543 return (*hfun) (c.val);
1545 c.next = catchlist;
1546 catchlist = &c;
1547 h.handler = handlers;
1548 h.var = Qnil;
1549 h.next = handlerlist;
1550 h.tag = &c;
1551 handlerlist = &h;
1553 val = (*bfun) (arg);
1554 catchlist = c.next;
1555 handlerlist = h.next;
1556 return val;
1559 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1560 its arguments. */
1562 Lisp_Object
1563 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1564 Lisp_Object arg1,
1565 Lisp_Object arg2,
1566 Lisp_Object handlers,
1567 Lisp_Object (*hfun) (Lisp_Object))
1569 Lisp_Object val;
1570 struct catchtag c;
1571 struct handler h;
1573 c.tag = Qnil;
1574 c.val = Qnil;
1575 c.backlist = backtrace_list;
1576 c.handlerlist = handlerlist;
1577 c.lisp_eval_depth = lisp_eval_depth;
1578 c.pdlcount = SPECPDL_INDEX ();
1579 c.poll_suppress_count = poll_suppress_count;
1580 c.interrupt_input_blocked = interrupt_input_blocked;
1581 c.gcpro = gcprolist;
1582 c.byte_stack = byte_stack_list;
1583 if (_setjmp (c.jmp))
1585 return (*hfun) (c.val);
1587 c.next = catchlist;
1588 catchlist = &c;
1589 h.handler = handlers;
1590 h.var = Qnil;
1591 h.next = handlerlist;
1592 h.tag = &c;
1593 handlerlist = &h;
1595 val = (*bfun) (arg1, arg2);
1596 catchlist = c.next;
1597 handlerlist = h.next;
1598 return val;
1601 /* Like internal_condition_case but call BFUN with NARGS as first,
1602 and ARGS as second argument. */
1604 Lisp_Object
1605 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1606 ptrdiff_t nargs,
1607 Lisp_Object *args,
1608 Lisp_Object handlers,
1609 Lisp_Object (*hfun) (Lisp_Object))
1611 Lisp_Object val;
1612 struct catchtag c;
1613 struct handler h;
1615 c.tag = Qnil;
1616 c.val = Qnil;
1617 c.backlist = backtrace_list;
1618 c.handlerlist = handlerlist;
1619 c.lisp_eval_depth = lisp_eval_depth;
1620 c.pdlcount = SPECPDL_INDEX ();
1621 c.poll_suppress_count = poll_suppress_count;
1622 c.interrupt_input_blocked = interrupt_input_blocked;
1623 c.gcpro = gcprolist;
1624 c.byte_stack = byte_stack_list;
1625 if (_setjmp (c.jmp))
1627 return (*hfun) (c.val);
1629 c.next = catchlist;
1630 catchlist = &c;
1631 h.handler = handlers;
1632 h.var = Qnil;
1633 h.next = handlerlist;
1634 h.tag = &c;
1635 handlerlist = &h;
1637 val = (*bfun) (nargs, args);
1638 catchlist = c.next;
1639 handlerlist = h.next;
1640 return val;
1644 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1645 static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1646 Lisp_Object data);
1648 void
1649 process_quit_flag (void)
1651 Lisp_Object flag = Vquit_flag;
1652 Vquit_flag = Qnil;
1653 if (EQ (flag, Qkill_emacs))
1654 Fkill_emacs (Qnil);
1655 if (EQ (Vthrow_on_input, flag))
1656 Fthrow (Vthrow_on_input, Qt);
1657 Fsignal (Qquit, Qnil);
1660 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1661 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1662 This function does not return.
1664 An error symbol is a symbol with an `error-conditions' property
1665 that is a list of condition names.
1666 A handler for any of those names will get to handle this signal.
1667 The symbol `error' should normally be one of them.
1669 DATA should be a list. Its elements are printed as part of the error message.
1670 See Info anchor `(elisp)Definition of signal' for some details on how this
1671 error message is constructed.
1672 If the signal is handled, DATA is made available to the handler.
1673 See also the function `condition-case'. */)
1674 (Lisp_Object error_symbol, Lisp_Object data)
1676 /* When memory is full, ERROR-SYMBOL is nil,
1677 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1678 That is a special case--don't do this in other situations. */
1679 Lisp_Object conditions;
1680 Lisp_Object string;
1681 Lisp_Object real_error_symbol
1682 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1683 register Lisp_Object clause = Qnil;
1684 struct handler *h;
1685 struct backtrace *bp;
1687 immediate_quit = handling_signal = 0;
1688 abort_on_gc = 0;
1689 if (gc_in_progress || waiting_for_input)
1690 abort ();
1692 #if 0 /* rms: I don't know why this was here,
1693 but it is surely wrong for an error that is handled. */
1694 #ifdef HAVE_WINDOW_SYSTEM
1695 if (display_hourglass_p)
1696 cancel_hourglass ();
1697 #endif
1698 #endif
1700 /* This hook is used by edebug. */
1701 if (! NILP (Vsignal_hook_function)
1702 && ! NILP (error_symbol))
1704 /* Edebug takes care of restoring these variables when it exits. */
1705 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1706 max_lisp_eval_depth = lisp_eval_depth + 20;
1708 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1709 max_specpdl_size = SPECPDL_INDEX () + 40;
1711 call2 (Vsignal_hook_function, error_symbol, data);
1714 conditions = Fget (real_error_symbol, Qerror_conditions);
1716 /* Remember from where signal was called. Skip over the frame for
1717 `signal' itself. If a frame for `error' follows, skip that,
1718 too. Don't do this when ERROR_SYMBOL is nil, because that
1719 is a memory-full error. */
1720 Vsignaling_function = Qnil;
1721 if (backtrace_list && !NILP (error_symbol))
1723 bp = backtrace_list->next;
1724 if (bp && bp->function && EQ (*bp->function, Qerror))
1725 bp = bp->next;
1726 if (bp && bp->function)
1727 Vsignaling_function = *bp->function;
1730 for (h = handlerlist; h; h = h->next)
1732 clause = find_handler_clause (h->handler, conditions);
1733 if (!NILP (clause))
1734 break;
1737 if (/* Don't run the debugger for a memory-full error.
1738 (There is no room in memory to do that!) */
1739 !NILP (error_symbol)
1740 && (!NILP (Vdebug_on_signal)
1741 /* If no handler is present now, try to run the debugger. */
1742 || NILP (clause)
1743 /* A `debug' symbol in the handler list disables the normal
1744 suppression of the debugger. */
1745 || (CONSP (clause) && CONSP (XCAR (clause))
1746 && !NILP (Fmemq (Qdebug, XCAR (clause))))
1747 /* Special handler that means "print a message and run debugger
1748 if requested". */
1749 || EQ (h->handler, Qerror)))
1751 int debugger_called
1752 = maybe_call_debugger (conditions, error_symbol, data);
1753 /* We can't return values to code which signaled an error, but we
1754 can continue code which has signaled a quit. */
1755 if (debugger_called && EQ (real_error_symbol, Qquit))
1756 return Qnil;
1759 if (!NILP (clause))
1761 Lisp_Object unwind_data
1762 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1764 h->chosen_clause = clause;
1765 unwind_to_catch (h->tag, unwind_data);
1767 else
1769 if (catchlist != 0)
1770 Fthrow (Qtop_level, Qt);
1773 if (! NILP (error_symbol))
1774 data = Fcons (error_symbol, data);
1776 string = Ferror_message_string (data);
1777 fatal ("%s", SDATA (string));
1780 /* Internal version of Fsignal that never returns.
1781 Used for anything but Qquit (which can return from Fsignal). */
1783 void
1784 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1786 Fsignal (error_symbol, data);
1787 abort ();
1790 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1792 void
1793 xsignal0 (Lisp_Object error_symbol)
1795 xsignal (error_symbol, Qnil);
1798 void
1799 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1801 xsignal (error_symbol, list1 (arg));
1804 void
1805 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1807 xsignal (error_symbol, list2 (arg1, arg2));
1810 void
1811 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1813 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1816 /* Signal `error' with message S, and additional arg ARG.
1817 If ARG is not a genuine list, make it a one-element list. */
1819 void
1820 signal_error (const char *s, Lisp_Object arg)
1822 Lisp_Object tortoise, hare;
1824 hare = tortoise = arg;
1825 while (CONSP (hare))
1827 hare = XCDR (hare);
1828 if (!CONSP (hare))
1829 break;
1831 hare = XCDR (hare);
1832 tortoise = XCDR (tortoise);
1834 if (EQ (hare, tortoise))
1835 break;
1838 if (!NILP (hare))
1839 arg = Fcons (arg, Qnil); /* Make it a list. */
1841 xsignal (Qerror, Fcons (build_string (s), arg));
1845 /* Return nonzero if LIST is a non-nil atom or
1846 a list containing one of CONDITIONS. */
1848 static int
1849 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1851 if (NILP (list))
1852 return 0;
1853 if (! CONSP (list))
1854 return 1;
1856 while (CONSP (conditions))
1858 Lisp_Object this, tail;
1859 this = XCAR (conditions);
1860 for (tail = list; CONSP (tail); tail = XCDR (tail))
1861 if (EQ (XCAR (tail), this))
1862 return 1;
1863 conditions = XCDR (conditions);
1865 return 0;
1868 /* Return 1 if an error with condition-symbols CONDITIONS,
1869 and described by SIGNAL-DATA, should skip the debugger
1870 according to debugger-ignored-errors. */
1872 static int
1873 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1875 Lisp_Object tail;
1876 int first_string = 1;
1877 Lisp_Object error_message;
1879 error_message = Qnil;
1880 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1882 if (STRINGP (XCAR (tail)))
1884 if (first_string)
1886 error_message = Ferror_message_string (data);
1887 first_string = 0;
1890 if (fast_string_match (XCAR (tail), error_message) >= 0)
1891 return 1;
1893 else
1895 Lisp_Object contail;
1897 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1898 if (EQ (XCAR (tail), XCAR (contail)))
1899 return 1;
1903 return 0;
1906 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1907 SIG and DATA describe the signal. There are two ways to pass them:
1908 = SIG is the error symbol, and DATA is the rest of the data.
1909 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1910 This is for memory-full errors only. */
1911 static int
1912 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1914 Lisp_Object combined_data;
1916 combined_data = Fcons (sig, data);
1918 if (
1919 /* Don't try to run the debugger with interrupts blocked.
1920 The editing loop would return anyway. */
1921 ! INPUT_BLOCKED_P
1922 /* Does user want to enter debugger for this kind of error? */
1923 && (EQ (sig, Qquit)
1924 ? debug_on_quit
1925 : wants_debugger (Vdebug_on_error, conditions))
1926 && ! skip_debugger (conditions, combined_data)
1927 /* RMS: What's this for? */
1928 && when_entered_debugger < num_nonmacro_input_events)
1930 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil)));
1931 return 1;
1934 return 0;
1937 static Lisp_Object
1938 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1940 register Lisp_Object h;
1942 /* t is used by handlers for all conditions, set up by C code. */
1943 if (EQ (handlers, Qt))
1944 return Qt;
1946 /* error is used similarly, but means print an error message
1947 and run the debugger if that is enabled. */
1948 if (EQ (handlers, Qerror))
1949 return Qt;
1951 for (h = handlers; CONSP (h); h = XCDR (h))
1953 Lisp_Object handler = XCAR (h);
1954 Lisp_Object condit, tem;
1956 if (!CONSP (handler))
1957 continue;
1958 condit = XCAR (handler);
1959 /* Handle a single condition name in handler HANDLER. */
1960 if (SYMBOLP (condit))
1962 tem = Fmemq (Fcar (handler), conditions);
1963 if (!NILP (tem))
1964 return handler;
1966 /* Handle a list of condition names in handler HANDLER. */
1967 else if (CONSP (condit))
1969 Lisp_Object tail;
1970 for (tail = condit; CONSP (tail); tail = XCDR (tail))
1972 tem = Fmemq (XCAR (tail), conditions);
1973 if (!NILP (tem))
1974 return handler;
1979 return Qnil;
1983 /* Dump an error message; called like vprintf. */
1984 void
1985 verror (const char *m, va_list ap)
1987 char buf[4000];
1988 ptrdiff_t size = sizeof buf;
1989 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1990 char *buffer = buf;
1991 ptrdiff_t used;
1992 Lisp_Object string;
1994 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1995 string = make_string (buffer, used);
1996 if (buffer != buf)
1997 xfree (buffer);
1999 xsignal1 (Qerror, string);
2003 /* Dump an error message; called like printf. */
2005 /* VARARGS 1 */
2006 void
2007 error (const char *m, ...)
2009 va_list ap;
2010 va_start (ap, m);
2011 verror (m, ap);
2012 va_end (ap);
2015 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
2016 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
2017 This means it contains a description for how to read arguments to give it.
2018 The value is nil for an invalid function or a symbol with no function
2019 definition.
2021 Interactively callable functions include strings and vectors (treated
2022 as keyboard macros), lambda-expressions that contain a top-level call
2023 to `interactive', autoload definitions made by `autoload' with non-nil
2024 fourth argument, and some of the built-in functions of Lisp.
2026 Also, a symbol satisfies `commandp' if its function definition does so.
2028 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
2029 then strings and vectors are not accepted. */)
2030 (Lisp_Object function, Lisp_Object for_call_interactively)
2032 register Lisp_Object fun;
2033 register Lisp_Object funcar;
2034 Lisp_Object if_prop = Qnil;
2036 fun = function;
2038 fun = indirect_function (fun); /* Check cycles. */
2039 if (NILP (fun) || EQ (fun, Qunbound))
2040 return Qnil;
2042 /* Check an `interactive-form' property if present, analogous to the
2043 function-documentation property. */
2044 fun = function;
2045 while (SYMBOLP (fun))
2047 Lisp_Object tmp = Fget (fun, Qinteractive_form);
2048 if (!NILP (tmp))
2049 if_prop = Qt;
2050 fun = Fsymbol_function (fun);
2053 /* Emacs primitives are interactive if their DEFUN specifies an
2054 interactive spec. */
2055 if (SUBRP (fun))
2056 return XSUBR (fun)->intspec ? Qt : if_prop;
2058 /* Bytecode objects are interactive if they are long enough to
2059 have an element whose index is COMPILED_INTERACTIVE, which is
2060 where the interactive spec is stored. */
2061 else if (COMPILEDP (fun))
2062 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
2063 ? Qt : if_prop);
2065 /* Strings and vectors are keyboard macros. */
2066 if (STRINGP (fun) || VECTORP (fun))
2067 return (NILP (for_call_interactively) ? Qt : Qnil);
2069 /* Lists may represent commands. */
2070 if (!CONSP (fun))
2071 return Qnil;
2072 funcar = XCAR (fun);
2073 if (EQ (funcar, Qclosure))
2074 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
2075 ? Qt : if_prop);
2076 else if (EQ (funcar, Qlambda))
2077 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
2078 else if (EQ (funcar, Qautoload))
2079 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
2080 else
2081 return Qnil;
2084 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
2085 doc: /* Define FUNCTION to autoload from FILE.
2086 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2087 Third arg DOCSTRING is documentation for the function.
2088 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2089 Fifth arg TYPE indicates the type of the object:
2090 nil or omitted says FUNCTION is a function,
2091 `keymap' says FUNCTION is really a keymap, and
2092 `macro' or t says FUNCTION is really a macro.
2093 Third through fifth args give info about the real definition.
2094 They default to nil.
2095 If FUNCTION is already defined other than as an autoload,
2096 this does nothing and returns nil. */)
2097 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
2099 CHECK_SYMBOL (function);
2100 CHECK_STRING (file);
2102 /* If function is defined and not as an autoload, don't override. */
2103 if (!EQ (XSYMBOL (function)->function, Qunbound)
2104 && !(CONSP (XSYMBOL (function)->function)
2105 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2106 return Qnil;
2108 if (NILP (Vpurify_flag))
2109 /* Only add entries after dumping, because the ones before are
2110 not useful and else we get loads of them from the loaddefs.el. */
2111 LOADHIST_ATTACH (Fcons (Qautoload, function));
2112 else
2113 /* We don't want the docstring in purespace (instead,
2114 Snarf-documentation should (hopefully) overwrite it).
2115 We used to use 0 here, but that leads to accidental sharing in
2116 purecopy's hash-consing, so we use a (hopefully) unique integer
2117 instead. */
2118 docstring = make_number (XPNTR (function));
2119 return Ffset (function,
2120 Fpurecopy (list5 (Qautoload, file, docstring,
2121 interactive, type)));
2124 Lisp_Object
2125 un_autoload (Lisp_Object oldqueue)
2127 register Lisp_Object queue, first, second;
2129 /* Queue to unwind is current value of Vautoload_queue.
2130 oldqueue is the shadowed value to leave in Vautoload_queue. */
2131 queue = Vautoload_queue;
2132 Vautoload_queue = oldqueue;
2133 while (CONSP (queue))
2135 first = XCAR (queue);
2136 second = Fcdr (first);
2137 first = Fcar (first);
2138 if (EQ (first, make_number (0)))
2139 Vfeatures = second;
2140 else
2141 Ffset (first, second);
2142 queue = XCDR (queue);
2144 return Qnil;
2147 /* Load an autoloaded function.
2148 FUNNAME is the symbol which is the function's name.
2149 FUNDEF is the autoload definition (a list). */
2151 void
2152 do_autoload (Lisp_Object fundef, Lisp_Object funname)
2154 int count = SPECPDL_INDEX ();
2155 Lisp_Object fun;
2156 struct gcpro gcpro1, gcpro2, gcpro3;
2158 /* This is to make sure that loadup.el gives a clear picture
2159 of what files are preloaded and when. */
2160 if (! NILP (Vpurify_flag))
2161 error ("Attempt to autoload %s while preparing to dump",
2162 SDATA (SYMBOL_NAME (funname)));
2164 fun = funname;
2165 CHECK_SYMBOL (funname);
2166 GCPRO3 (fun, funname, fundef);
2168 /* Preserve the match data. */
2169 record_unwind_save_match_data ();
2171 /* If autoloading gets an error (which includes the error of failing
2172 to define the function being called), we use Vautoload_queue
2173 to undo function definitions and `provide' calls made by
2174 the function. We do this in the specific case of autoloading
2175 because autoloading is not an explicit request "load this file",
2176 but rather a request to "call this function".
2178 The value saved here is to be restored into Vautoload_queue. */
2179 record_unwind_protect (un_autoload, Vautoload_queue);
2180 Vautoload_queue = Qt;
2181 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt);
2183 /* Once loading finishes, don't undo it. */
2184 Vautoload_queue = Qt;
2185 unbind_to (count, Qnil);
2187 fun = Findirect_function (fun, Qnil);
2189 if (!NILP (Fequal (fun, fundef)))
2190 error ("Autoloading failed to define function %s",
2191 SDATA (SYMBOL_NAME (funname)));
2192 UNGCPRO;
2196 DEFUN ("eval", Feval, Seval, 1, 2, 0,
2197 doc: /* Evaluate FORM and return its value.
2198 If LEXICAL is t, evaluate using lexical scoping. */)
2199 (Lisp_Object form, Lisp_Object lexical)
2201 int count = SPECPDL_INDEX ();
2202 specbind (Qinternal_interpreter_environment,
2203 NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
2204 return unbind_to (count, eval_sub (form));
2207 /* Eval a sub-expression of the current expression (i.e. in the same
2208 lexical scope). */
2209 Lisp_Object
2210 eval_sub (Lisp_Object form)
2212 Lisp_Object fun, val, original_fun, original_args;
2213 Lisp_Object funcar;
2214 struct backtrace backtrace;
2215 struct gcpro gcpro1, gcpro2, gcpro3;
2217 if (handling_signal)
2218 abort ();
2220 if (SYMBOLP (form))
2222 /* Look up its binding in the lexical environment.
2223 We do not pay attention to the declared_special flag here, since we
2224 already did that when let-binding the variable. */
2225 Lisp_Object lex_binding
2226 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2227 ? Fassq (form, Vinternal_interpreter_environment)
2228 : Qnil;
2229 if (CONSP (lex_binding))
2230 return XCDR (lex_binding);
2231 else
2232 return Fsymbol_value (form);
2235 if (!CONSP (form))
2236 return form;
2238 QUIT;
2239 if ((consing_since_gc > gc_cons_threshold
2240 && consing_since_gc > gc_relative_threshold)
2242 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2244 GCPRO1 (form);
2245 Fgarbage_collect ();
2246 UNGCPRO;
2249 if (++lisp_eval_depth > max_lisp_eval_depth)
2251 if (max_lisp_eval_depth < 100)
2252 max_lisp_eval_depth = 100;
2253 if (lisp_eval_depth > max_lisp_eval_depth)
2254 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2257 original_fun = Fcar (form);
2258 original_args = Fcdr (form);
2260 backtrace.next = backtrace_list;
2261 backtrace_list = &backtrace;
2262 backtrace.function = &original_fun; /* This also protects them from gc. */
2263 backtrace.args = &original_args;
2264 backtrace.nargs = UNEVALLED;
2265 backtrace.debug_on_exit = 0;
2267 if (debug_on_next_call)
2268 do_debug_on_call (Qt);
2270 /* At this point, only original_fun and original_args
2271 have values that will be used below. */
2272 retry:
2274 /* Optimize for no indirection. */
2275 fun = original_fun;
2276 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2277 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2278 fun = indirect_function (fun);
2280 if (SUBRP (fun))
2282 Lisp_Object numargs;
2283 Lisp_Object argvals[8];
2284 Lisp_Object args_left;
2285 register int i, maxargs;
2287 args_left = original_args;
2288 numargs = Flength (args_left);
2290 CHECK_CONS_LIST ();
2292 if (XINT (numargs) < XSUBR (fun)->min_args
2293 || (XSUBR (fun)->max_args >= 0
2294 && XSUBR (fun)->max_args < XINT (numargs)))
2295 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2297 else if (XSUBR (fun)->max_args == UNEVALLED)
2298 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2299 else if (XSUBR (fun)->max_args == MANY)
2301 /* Pass a vector of evaluated arguments. */
2302 Lisp_Object *vals;
2303 ptrdiff_t argnum = 0;
2304 USE_SAFE_ALLOCA;
2306 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2308 GCPRO3 (args_left, fun, fun);
2309 gcpro3.var = vals;
2310 gcpro3.nvars = 0;
2312 while (!NILP (args_left))
2314 vals[argnum++] = eval_sub (Fcar (args_left));
2315 args_left = Fcdr (args_left);
2316 gcpro3.nvars = argnum;
2319 backtrace.args = vals;
2320 backtrace.nargs = XINT (numargs);
2322 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2323 UNGCPRO;
2324 SAFE_FREE ();
2326 else
2328 GCPRO3 (args_left, fun, fun);
2329 gcpro3.var = argvals;
2330 gcpro3.nvars = 0;
2332 maxargs = XSUBR (fun)->max_args;
2333 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
2335 argvals[i] = eval_sub (Fcar (args_left));
2336 gcpro3.nvars = ++i;
2339 UNGCPRO;
2341 backtrace.args = argvals;
2342 backtrace.nargs = XINT (numargs);
2344 switch (i)
2346 case 0:
2347 val = (XSUBR (fun)->function.a0 ());
2348 break;
2349 case 1:
2350 val = (XSUBR (fun)->function.a1 (argvals[0]));
2351 break;
2352 case 2:
2353 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2354 break;
2355 case 3:
2356 val = (XSUBR (fun)->function.a3
2357 (argvals[0], argvals[1], argvals[2]));
2358 break;
2359 case 4:
2360 val = (XSUBR (fun)->function.a4
2361 (argvals[0], argvals[1], argvals[2], argvals[3]));
2362 break;
2363 case 5:
2364 val = (XSUBR (fun)->function.a5
2365 (argvals[0], argvals[1], argvals[2], argvals[3],
2366 argvals[4]));
2367 break;
2368 case 6:
2369 val = (XSUBR (fun)->function.a6
2370 (argvals[0], argvals[1], argvals[2], argvals[3],
2371 argvals[4], argvals[5]));
2372 break;
2373 case 7:
2374 val = (XSUBR (fun)->function.a7
2375 (argvals[0], argvals[1], argvals[2], argvals[3],
2376 argvals[4], argvals[5], argvals[6]));
2377 break;
2379 case 8:
2380 val = (XSUBR (fun)->function.a8
2381 (argvals[0], argvals[1], argvals[2], argvals[3],
2382 argvals[4], argvals[5], argvals[6], argvals[7]));
2383 break;
2385 default:
2386 /* Someone has created a subr that takes more arguments than
2387 is supported by this code. We need to either rewrite the
2388 subr to use a different argument protocol, or add more
2389 cases to this switch. */
2390 abort ();
2394 else if (COMPILEDP (fun))
2395 val = apply_lambda (fun, original_args);
2396 else
2398 if (EQ (fun, Qunbound))
2399 xsignal1 (Qvoid_function, original_fun);
2400 if (!CONSP (fun))
2401 xsignal1 (Qinvalid_function, original_fun);
2402 funcar = XCAR (fun);
2403 if (!SYMBOLP (funcar))
2404 xsignal1 (Qinvalid_function, original_fun);
2405 if (EQ (funcar, Qautoload))
2407 do_autoload (fun, original_fun);
2408 goto retry;
2410 if (EQ (funcar, Qmacro))
2411 val = eval_sub (apply1 (Fcdr (fun), original_args));
2412 else if (EQ (funcar, Qlambda)
2413 || EQ (funcar, Qclosure))
2414 val = apply_lambda (fun, original_args);
2415 else
2416 xsignal1 (Qinvalid_function, original_fun);
2418 CHECK_CONS_LIST ();
2420 lisp_eval_depth--;
2421 if (backtrace.debug_on_exit)
2422 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2423 backtrace_list = backtrace.next;
2425 return val;
2428 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
2429 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2430 Then return the value FUNCTION returns.
2431 Thus, (apply '+ 1 2 '(3 4)) returns 10.
2432 usage: (apply FUNCTION &rest ARGUMENTS) */)
2433 (ptrdiff_t nargs, Lisp_Object *args)
2435 ptrdiff_t i, numargs;
2436 register Lisp_Object spread_arg;
2437 register Lisp_Object *funcall_args;
2438 Lisp_Object fun, retval;
2439 struct gcpro gcpro1;
2440 USE_SAFE_ALLOCA;
2442 fun = args [0];
2443 funcall_args = 0;
2444 spread_arg = args [nargs - 1];
2445 CHECK_LIST (spread_arg);
2447 numargs = XINT (Flength (spread_arg));
2449 if (numargs == 0)
2450 return Ffuncall (nargs - 1, args);
2451 else if (numargs == 1)
2453 args [nargs - 1] = XCAR (spread_arg);
2454 return Ffuncall (nargs, args);
2457 numargs += nargs - 2;
2459 /* Optimize for no indirection. */
2460 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2461 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2462 fun = indirect_function (fun);
2463 if (EQ (fun, Qunbound))
2465 /* Let funcall get the error. */
2466 fun = args[0];
2467 goto funcall;
2470 if (SUBRP (fun))
2472 if (numargs < XSUBR (fun)->min_args
2473 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2474 goto funcall; /* Let funcall get the error. */
2475 else if (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args > numargs)
2477 /* Avoid making funcall cons up a yet another new vector of arguments
2478 by explicitly supplying nil's for optional values. */
2479 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2480 for (i = numargs; i < XSUBR (fun)->max_args;)
2481 funcall_args[++i] = Qnil;
2482 GCPRO1 (*funcall_args);
2483 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
2486 funcall:
2487 /* We add 1 to numargs because funcall_args includes the
2488 function itself as well as its arguments. */
2489 if (!funcall_args)
2491 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2492 GCPRO1 (*funcall_args);
2493 gcpro1.nvars = 1 + numargs;
2496 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object));
2497 /* Spread the last arg we got. Its first element goes in
2498 the slot that it used to occupy, hence this value of I. */
2499 i = nargs - 1;
2500 while (!NILP (spread_arg))
2502 funcall_args [i++] = XCAR (spread_arg);
2503 spread_arg = XCDR (spread_arg);
2506 /* By convention, the caller needs to gcpro Ffuncall's args. */
2507 retval = Ffuncall (gcpro1.nvars, funcall_args);
2508 UNGCPRO;
2509 SAFE_FREE ();
2511 return retval;
2514 /* Run hook variables in various ways. */
2516 static Lisp_Object
2517 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2519 Ffuncall (nargs, args);
2520 return Qnil;
2523 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2524 doc: /* Run each hook in HOOKS.
2525 Each argument should be a symbol, a hook variable.
2526 These symbols are processed in the order specified.
2527 If a hook symbol has a non-nil value, that value may be a function
2528 or a list of functions to be called to run the hook.
2529 If the value is a function, it is called with no arguments.
2530 If it is a list, the elements are called, in order, with no arguments.
2532 Major modes should not use this function directly to run their mode
2533 hook; they should use `run-mode-hooks' instead.
2535 Do not use `make-local-variable' to make a hook variable buffer-local.
2536 Instead, use `add-hook' and specify t for the LOCAL argument.
2537 usage: (run-hooks &rest HOOKS) */)
2538 (ptrdiff_t nargs, Lisp_Object *args)
2540 Lisp_Object hook[1];
2541 ptrdiff_t i;
2543 for (i = 0; i < nargs; i++)
2545 hook[0] = args[i];
2546 run_hook_with_args (1, hook, funcall_nil);
2549 return Qnil;
2552 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2553 Srun_hook_with_args, 1, MANY, 0,
2554 doc: /* Run HOOK with the specified arguments ARGS.
2555 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2556 value, that value may be a function or a list of functions to be
2557 called to run the hook. If the value is a function, it is called with
2558 the given arguments and its return value is returned. If it is a list
2559 of functions, those functions are called, in order,
2560 with the given arguments ARGS.
2561 It is best not to depend on the value returned by `run-hook-with-args',
2562 as that may change.
2564 Do not use `make-local-variable' to make a hook variable buffer-local.
2565 Instead, use `add-hook' and specify t for the LOCAL argument.
2566 usage: (run-hook-with-args HOOK &rest ARGS) */)
2567 (ptrdiff_t nargs, Lisp_Object *args)
2569 return run_hook_with_args (nargs, args, funcall_nil);
2572 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2573 Srun_hook_with_args_until_success, 1, MANY, 0,
2574 doc: /* Run HOOK with the specified arguments ARGS.
2575 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2576 value, that value may be a function or a list of functions to be
2577 called to run the hook. If the value is a function, it is called with
2578 the given arguments and its return value is returned.
2579 If it is a list of functions, those functions are called, in order,
2580 with the given arguments ARGS, until one of them
2581 returns a non-nil value. Then we return that value.
2582 However, if they all return nil, we return nil.
2584 Do not use `make-local-variable' to make a hook variable buffer-local.
2585 Instead, use `add-hook' and specify t for the LOCAL argument.
2586 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2587 (ptrdiff_t nargs, Lisp_Object *args)
2589 return run_hook_with_args (nargs, args, Ffuncall);
2592 static Lisp_Object
2593 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2595 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2598 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2599 Srun_hook_with_args_until_failure, 1, MANY, 0,
2600 doc: /* Run HOOK with the specified arguments ARGS.
2601 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
2602 value, that value may be a function or a list of functions to be
2603 called to run the hook. If the value is a function, it is called with
2604 the given arguments and its return value is returned.
2605 If it is a list of functions, those functions are called, in order,
2606 with the given arguments ARGS, until one of them returns nil.
2607 Then we return nil. However, if they all return non-nil, we return non-nil.
2609 Do not use `make-local-variable' to make a hook variable buffer-local.
2610 Instead, use `add-hook' and specify t for the LOCAL argument.
2611 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2612 (ptrdiff_t nargs, Lisp_Object *args)
2614 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2617 static Lisp_Object
2618 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2620 Lisp_Object tmp = args[0], ret;
2621 args[0] = args[1];
2622 args[1] = tmp;
2623 ret = Ffuncall (nargs, args);
2624 args[1] = args[0];
2625 args[0] = tmp;
2626 return ret;
2629 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2630 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2631 I.e. instead of calling each function FUN directly with arguments ARGS,
2632 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2633 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2634 aborts and returns that value.
2635 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2636 (ptrdiff_t nargs, Lisp_Object *args)
2638 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2641 /* ARGS[0] should be a hook symbol.
2642 Call each of the functions in the hook value, passing each of them
2643 as arguments all the rest of ARGS (all NARGS - 1 elements).
2644 FUNCALL specifies how to call each function on the hook.
2645 The caller (or its caller, etc) must gcpro all of ARGS,
2646 except that it isn't necessary to gcpro ARGS[0]. */
2648 Lisp_Object
2649 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2650 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2652 Lisp_Object sym, val, ret = Qnil;
2653 struct gcpro gcpro1, gcpro2, gcpro3;
2655 /* If we are dying or still initializing,
2656 don't do anything--it would probably crash if we tried. */
2657 if (NILP (Vrun_hooks))
2658 return Qnil;
2660 sym = args[0];
2661 val = find_symbol_value (sym);
2663 if (EQ (val, Qunbound) || NILP (val))
2664 return ret;
2665 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2667 args[0] = val;
2668 return funcall (nargs, args);
2670 else
2672 Lisp_Object global_vals = Qnil;
2673 GCPRO3 (sym, val, global_vals);
2675 for (;
2676 CONSP (val) && NILP (ret);
2677 val = XCDR (val))
2679 if (EQ (XCAR (val), Qt))
2681 /* t indicates this hook has a local binding;
2682 it means to run the global binding too. */
2683 global_vals = Fdefault_value (sym);
2684 if (NILP (global_vals)) continue;
2686 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2688 args[0] = global_vals;
2689 ret = funcall (nargs, args);
2691 else
2693 for (;
2694 CONSP (global_vals) && NILP (ret);
2695 global_vals = XCDR (global_vals))
2697 args[0] = XCAR (global_vals);
2698 /* In a global value, t should not occur. If it does, we
2699 must ignore it to avoid an endless loop. */
2700 if (!EQ (args[0], Qt))
2701 ret = funcall (nargs, args);
2705 else
2707 args[0] = XCAR (val);
2708 ret = funcall (nargs, args);
2712 UNGCPRO;
2713 return ret;
2717 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2719 void
2720 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2722 Lisp_Object temp[3];
2723 temp[0] = hook;
2724 temp[1] = arg1;
2725 temp[2] = arg2;
2727 Frun_hook_with_args (3, temp);
2730 /* Apply fn to arg. */
2731 Lisp_Object
2732 apply1 (Lisp_Object fn, Lisp_Object arg)
2734 struct gcpro gcpro1;
2736 GCPRO1 (fn);
2737 if (NILP (arg))
2738 RETURN_UNGCPRO (Ffuncall (1, &fn));
2739 gcpro1.nvars = 2;
2741 Lisp_Object args[2];
2742 args[0] = fn;
2743 args[1] = arg;
2744 gcpro1.var = args;
2745 RETURN_UNGCPRO (Fapply (2, args));
2749 /* Call function fn on no arguments. */
2750 Lisp_Object
2751 call0 (Lisp_Object fn)
2753 struct gcpro gcpro1;
2755 GCPRO1 (fn);
2756 RETURN_UNGCPRO (Ffuncall (1, &fn));
2759 /* Call function fn with 1 argument arg1. */
2760 /* ARGSUSED */
2761 Lisp_Object
2762 call1 (Lisp_Object fn, Lisp_Object arg1)
2764 struct gcpro gcpro1;
2765 Lisp_Object args[2];
2767 args[0] = fn;
2768 args[1] = arg1;
2769 GCPRO1 (args[0]);
2770 gcpro1.nvars = 2;
2771 RETURN_UNGCPRO (Ffuncall (2, args));
2774 /* Call function fn with 2 arguments arg1, arg2. */
2775 /* ARGSUSED */
2776 Lisp_Object
2777 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2779 struct gcpro gcpro1;
2780 Lisp_Object args[3];
2781 args[0] = fn;
2782 args[1] = arg1;
2783 args[2] = arg2;
2784 GCPRO1 (args[0]);
2785 gcpro1.nvars = 3;
2786 RETURN_UNGCPRO (Ffuncall (3, args));
2789 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2790 /* ARGSUSED */
2791 Lisp_Object
2792 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2794 struct gcpro gcpro1;
2795 Lisp_Object args[4];
2796 args[0] = fn;
2797 args[1] = arg1;
2798 args[2] = arg2;
2799 args[3] = arg3;
2800 GCPRO1 (args[0]);
2801 gcpro1.nvars = 4;
2802 RETURN_UNGCPRO (Ffuncall (4, args));
2805 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2806 /* ARGSUSED */
2807 Lisp_Object
2808 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2809 Lisp_Object arg4)
2811 struct gcpro gcpro1;
2812 Lisp_Object args[5];
2813 args[0] = fn;
2814 args[1] = arg1;
2815 args[2] = arg2;
2816 args[3] = arg3;
2817 args[4] = arg4;
2818 GCPRO1 (args[0]);
2819 gcpro1.nvars = 5;
2820 RETURN_UNGCPRO (Ffuncall (5, args));
2823 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2824 /* ARGSUSED */
2825 Lisp_Object
2826 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2827 Lisp_Object arg4, Lisp_Object arg5)
2829 struct gcpro gcpro1;
2830 Lisp_Object args[6];
2831 args[0] = fn;
2832 args[1] = arg1;
2833 args[2] = arg2;
2834 args[3] = arg3;
2835 args[4] = arg4;
2836 args[5] = arg5;
2837 GCPRO1 (args[0]);
2838 gcpro1.nvars = 6;
2839 RETURN_UNGCPRO (Ffuncall (6, args));
2842 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2843 /* ARGSUSED */
2844 Lisp_Object
2845 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2846 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2848 struct gcpro gcpro1;
2849 Lisp_Object args[7];
2850 args[0] = fn;
2851 args[1] = arg1;
2852 args[2] = arg2;
2853 args[3] = arg3;
2854 args[4] = arg4;
2855 args[5] = arg5;
2856 args[6] = arg6;
2857 GCPRO1 (args[0]);
2858 gcpro1.nvars = 7;
2859 RETURN_UNGCPRO (Ffuncall (7, args));
2862 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2863 /* ARGSUSED */
2864 Lisp_Object
2865 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2866 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2868 struct gcpro gcpro1;
2869 Lisp_Object args[8];
2870 args[0] = fn;
2871 args[1] = arg1;
2872 args[2] = arg2;
2873 args[3] = arg3;
2874 args[4] = arg4;
2875 args[5] = arg5;
2876 args[6] = arg6;
2877 args[7] = arg7;
2878 GCPRO1 (args[0]);
2879 gcpro1.nvars = 8;
2880 RETURN_UNGCPRO (Ffuncall (8, args));
2883 /* The caller should GCPRO all the elements of ARGS. */
2885 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2886 doc: /* Non-nil if OBJECT is a function. */)
2887 (Lisp_Object object)
2889 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2891 object = Findirect_function (object, Qt);
2893 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2895 /* Autoloaded symbols are functions, except if they load
2896 macros or keymaps. */
2897 int i;
2898 for (i = 0; i < 4 && CONSP (object); i++)
2899 object = XCDR (object);
2901 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2905 if (SUBRP (object))
2906 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2907 else if (COMPILEDP (object))
2908 return Qt;
2909 else if (CONSP (object))
2911 Lisp_Object car = XCAR (object);
2912 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2914 else
2915 return Qnil;
2918 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2919 doc: /* Call first argument as a function, passing remaining arguments to it.
2920 Return the value that function returns.
2921 Thus, (funcall 'cons 'x 'y) returns (x . y).
2922 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2923 (ptrdiff_t nargs, Lisp_Object *args)
2925 Lisp_Object fun, original_fun;
2926 Lisp_Object funcar;
2927 ptrdiff_t numargs = nargs - 1;
2928 Lisp_Object lisp_numargs;
2929 Lisp_Object val;
2930 struct backtrace backtrace;
2931 register Lisp_Object *internal_args;
2932 ptrdiff_t i;
2934 QUIT;
2935 if ((consing_since_gc > gc_cons_threshold
2936 && consing_since_gc > gc_relative_threshold)
2938 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2939 Fgarbage_collect ();
2941 if (++lisp_eval_depth > max_lisp_eval_depth)
2943 if (max_lisp_eval_depth < 100)
2944 max_lisp_eval_depth = 100;
2945 if (lisp_eval_depth > max_lisp_eval_depth)
2946 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2949 backtrace.next = backtrace_list;
2950 backtrace_list = &backtrace;
2951 backtrace.function = &args[0];
2952 backtrace.args = &args[1];
2953 backtrace.nargs = nargs - 1;
2954 backtrace.debug_on_exit = 0;
2956 if (debug_on_next_call)
2957 do_debug_on_call (Qlambda);
2959 CHECK_CONS_LIST ();
2961 original_fun = args[0];
2963 retry:
2965 /* Optimize for no indirection. */
2966 fun = original_fun;
2967 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2968 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2969 fun = indirect_function (fun);
2971 if (SUBRP (fun))
2973 if (numargs < XSUBR (fun)->min_args
2974 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2976 XSETFASTINT (lisp_numargs, numargs);
2977 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2980 else if (XSUBR (fun)->max_args == UNEVALLED)
2981 xsignal1 (Qinvalid_function, original_fun);
2983 else if (XSUBR (fun)->max_args == MANY)
2984 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2985 else
2987 if (XSUBR (fun)->max_args > numargs)
2989 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2990 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
2991 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2992 internal_args[i] = Qnil;
2994 else
2995 internal_args = args + 1;
2996 switch (XSUBR (fun)->max_args)
2998 case 0:
2999 val = (XSUBR (fun)->function.a0 ());
3000 break;
3001 case 1:
3002 val = (XSUBR (fun)->function.a1 (internal_args[0]));
3003 break;
3004 case 2:
3005 val = (XSUBR (fun)->function.a2
3006 (internal_args[0], internal_args[1]));
3007 break;
3008 case 3:
3009 val = (XSUBR (fun)->function.a3
3010 (internal_args[0], internal_args[1], internal_args[2]));
3011 break;
3012 case 4:
3013 val = (XSUBR (fun)->function.a4
3014 (internal_args[0], internal_args[1], internal_args[2],
3015 internal_args[3]));
3016 break;
3017 case 5:
3018 val = (XSUBR (fun)->function.a5
3019 (internal_args[0], internal_args[1], internal_args[2],
3020 internal_args[3], internal_args[4]));
3021 break;
3022 case 6:
3023 val = (XSUBR (fun)->function.a6
3024 (internal_args[0], internal_args[1], internal_args[2],
3025 internal_args[3], internal_args[4], internal_args[5]));
3026 break;
3027 case 7:
3028 val = (XSUBR (fun)->function.a7
3029 (internal_args[0], internal_args[1], internal_args[2],
3030 internal_args[3], internal_args[4], internal_args[5],
3031 internal_args[6]));
3032 break;
3034 case 8:
3035 val = (XSUBR (fun)->function.a8
3036 (internal_args[0], internal_args[1], internal_args[2],
3037 internal_args[3], internal_args[4], internal_args[5],
3038 internal_args[6], internal_args[7]));
3039 break;
3041 default:
3043 /* If a subr takes more than 8 arguments without using MANY
3044 or UNEVALLED, we need to extend this function to support it.
3045 Until this is done, there is no way to call the function. */
3046 abort ();
3050 else if (COMPILEDP (fun))
3051 val = funcall_lambda (fun, numargs, args + 1);
3052 else
3054 if (EQ (fun, Qunbound))
3055 xsignal1 (Qvoid_function, original_fun);
3056 if (!CONSP (fun))
3057 xsignal1 (Qinvalid_function, original_fun);
3058 funcar = XCAR (fun);
3059 if (!SYMBOLP (funcar))
3060 xsignal1 (Qinvalid_function, original_fun);
3061 if (EQ (funcar, Qlambda)
3062 || EQ (funcar, Qclosure))
3063 val = funcall_lambda (fun, numargs, args + 1);
3064 else if (EQ (funcar, Qautoload))
3066 do_autoload (fun, original_fun);
3067 CHECK_CONS_LIST ();
3068 goto retry;
3070 else
3071 xsignal1 (Qinvalid_function, original_fun);
3073 CHECK_CONS_LIST ();
3074 lisp_eval_depth--;
3075 if (backtrace.debug_on_exit)
3076 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3077 backtrace_list = backtrace.next;
3078 return val;
3081 static Lisp_Object
3082 apply_lambda (Lisp_Object fun, Lisp_Object args)
3084 Lisp_Object args_left;
3085 ptrdiff_t i, numargs;
3086 register Lisp_Object *arg_vector;
3087 struct gcpro gcpro1, gcpro2, gcpro3;
3088 register Lisp_Object tem;
3089 USE_SAFE_ALLOCA;
3091 numargs = XFASTINT (Flength (args));
3092 SAFE_ALLOCA_LISP (arg_vector, numargs);
3093 args_left = args;
3095 GCPRO3 (*arg_vector, args_left, fun);
3096 gcpro1.nvars = 0;
3098 for (i = 0; i < numargs; )
3100 tem = Fcar (args_left), args_left = Fcdr (args_left);
3101 tem = eval_sub (tem);
3102 arg_vector[i++] = tem;
3103 gcpro1.nvars = i;
3106 UNGCPRO;
3108 backtrace_list->args = arg_vector;
3109 backtrace_list->nargs = i;
3110 tem = funcall_lambda (fun, numargs, arg_vector);
3112 /* Do the debug-on-exit now, while arg_vector still exists. */
3113 if (backtrace_list->debug_on_exit)
3114 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3115 /* Don't do it again when we return to eval. */
3116 backtrace_list->debug_on_exit = 0;
3117 SAFE_FREE ();
3118 return tem;
3121 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3122 and return the result of evaluation.
3123 FUN must be either a lambda-expression or a compiled-code object. */
3125 static Lisp_Object
3126 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
3127 register Lisp_Object *arg_vector)
3129 Lisp_Object val, syms_left, next, lexenv;
3130 int count = SPECPDL_INDEX ();
3131 ptrdiff_t i;
3132 int optional, rest;
3134 if (CONSP (fun))
3136 if (EQ (XCAR (fun), Qclosure))
3138 fun = XCDR (fun); /* Drop `closure'. */
3139 lexenv = XCAR (fun);
3140 CHECK_LIST_CONS (fun, fun);
3142 else
3143 lexenv = Qnil;
3144 syms_left = XCDR (fun);
3145 if (CONSP (syms_left))
3146 syms_left = XCAR (syms_left);
3147 else
3148 xsignal1 (Qinvalid_function, fun);
3150 else if (COMPILEDP (fun))
3152 syms_left = AREF (fun, COMPILED_ARGLIST);
3153 if (INTEGERP (syms_left))
3154 /* A byte-code object with a non-nil `push args' slot means we
3155 shouldn't bind any arguments, instead just call the byte-code
3156 interpreter directly; it will push arguments as necessary.
3158 Byte-code objects with either a non-existent, or a nil value for
3159 the `push args' slot (the default), have dynamically-bound
3160 arguments, and use the argument-binding code below instead (as do
3161 all interpreted functions, even lexically bound ones). */
3163 /* If we have not actually read the bytecode string
3164 and constants vector yet, fetch them from the file. */
3165 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3166 Ffetch_bytecode (fun);
3167 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3168 AREF (fun, COMPILED_CONSTANTS),
3169 AREF (fun, COMPILED_STACK_DEPTH),
3170 syms_left,
3171 nargs, arg_vector);
3173 lexenv = Qnil;
3175 else
3176 abort ();
3178 i = optional = rest = 0;
3179 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3181 QUIT;
3183 next = XCAR (syms_left);
3184 if (!SYMBOLP (next))
3185 xsignal1 (Qinvalid_function, fun);
3187 if (EQ (next, Qand_rest))
3188 rest = 1;
3189 else if (EQ (next, Qand_optional))
3190 optional = 1;
3191 else
3193 Lisp_Object arg;
3194 if (rest)
3196 arg = Flist (nargs - i, &arg_vector[i]);
3197 i = nargs;
3199 else if (i < nargs)
3200 arg = arg_vector[i++];
3201 else if (!optional)
3202 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3203 else
3204 arg = Qnil;
3206 /* Bind the argument. */
3207 if (!NILP (lexenv) && SYMBOLP (next))
3208 /* Lexically bind NEXT by adding it to the lexenv alist. */
3209 lexenv = Fcons (Fcons (next, arg), lexenv);
3210 else
3211 /* Dynamically bind NEXT. */
3212 specbind (next, arg);
3216 if (!NILP (syms_left))
3217 xsignal1 (Qinvalid_function, fun);
3218 else if (i < nargs)
3219 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3221 if (!EQ (lexenv, Vinternal_interpreter_environment))
3222 /* Instantiate a new lexical environment. */
3223 specbind (Qinternal_interpreter_environment, lexenv);
3225 if (CONSP (fun))
3226 val = Fprogn (XCDR (XCDR (fun)));
3227 else
3229 /* If we have not actually read the bytecode string
3230 and constants vector yet, fetch them from the file. */
3231 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3232 Ffetch_bytecode (fun);
3233 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3234 AREF (fun, COMPILED_CONSTANTS),
3235 AREF (fun, COMPILED_STACK_DEPTH),
3236 Qnil, 0, 0);
3239 return unbind_to (count, val);
3242 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3243 1, 1, 0,
3244 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3245 (Lisp_Object object)
3247 Lisp_Object tem;
3249 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3251 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3252 if (!CONSP (tem))
3254 tem = AREF (object, COMPILED_BYTECODE);
3255 if (CONSP (tem) && STRINGP (XCAR (tem)))
3256 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3257 else
3258 error ("Invalid byte code");
3260 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3261 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3263 return object;
3266 static void
3267 grow_specpdl (void)
3269 register int count = SPECPDL_INDEX ();
3270 int max_size =
3271 min (max_specpdl_size,
3272 min (max (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct specbinding),
3273 INT_MAX));
3274 int size;
3275 if (max_size <= specpdl_size)
3277 if (max_specpdl_size < 400)
3278 max_size = max_specpdl_size = 400;
3279 if (max_size <= specpdl_size)
3280 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3282 size = specpdl_size < max_size / 2 ? 2 * specpdl_size : max_size;
3283 specpdl = xnrealloc (specpdl, size, sizeof *specpdl);
3284 specpdl_size = size;
3285 specpdl_ptr = specpdl + count;
3288 /* `specpdl_ptr->symbol' is a field which describes which variable is
3289 let-bound, so it can be properly undone when we unbind_to.
3290 It can have the following two shapes:
3291 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3292 a symbol that is not buffer-local (at least at the time
3293 the let binding started). Note also that it should not be
3294 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3295 to record V2 here).
3296 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3297 variable SYMBOL which can be buffer-local. WHERE tells us
3298 which buffer is affected (or nil if the let-binding affects the
3299 global value of the variable) and BUFFER tells us which buffer was
3300 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3301 BUFFER did not yet have a buffer-local value). */
3303 void
3304 specbind (Lisp_Object symbol, Lisp_Object value)
3306 struct Lisp_Symbol *sym;
3308 eassert (!handling_signal);
3310 CHECK_SYMBOL (symbol);
3311 sym = XSYMBOL (symbol);
3312 if (specpdl_ptr == specpdl + specpdl_size)
3313 grow_specpdl ();
3315 start:
3316 switch (sym->redirect)
3318 case SYMBOL_VARALIAS:
3319 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3320 case SYMBOL_PLAINVAL:
3321 /* The most common case is that of a non-constant symbol with a
3322 trivial value. Make that as fast as we can. */
3323 specpdl_ptr->symbol = symbol;
3324 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3325 specpdl_ptr->func = NULL;
3326 ++specpdl_ptr;
3327 if (!sym->constant)
3328 SET_SYMBOL_VAL (sym, value);
3329 else
3330 set_internal (symbol, value, Qnil, 1);
3331 break;
3332 case SYMBOL_LOCALIZED:
3333 if (SYMBOL_BLV (sym)->frame_local)
3334 error ("Frame-local vars cannot be let-bound");
3335 case SYMBOL_FORWARDED:
3337 Lisp_Object ovalue = find_symbol_value (symbol);
3338 specpdl_ptr->func = 0;
3339 specpdl_ptr->old_value = ovalue;
3341 eassert (sym->redirect != SYMBOL_LOCALIZED
3342 || (EQ (SYMBOL_BLV (sym)->where,
3343 SYMBOL_BLV (sym)->frame_local ?
3344 Fselected_frame () : Fcurrent_buffer ())));
3346 if (sym->redirect == SYMBOL_LOCALIZED
3347 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3349 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3351 /* For a local variable, record both the symbol and which
3352 buffer's or frame's value we are saving. */
3353 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3355 eassert (sym->redirect != SYMBOL_LOCALIZED
3356 || (BLV_FOUND (SYMBOL_BLV (sym))
3357 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3358 where = cur_buf;
3360 else if (sym->redirect == SYMBOL_LOCALIZED
3361 && BLV_FOUND (SYMBOL_BLV (sym)))
3362 where = SYMBOL_BLV (sym)->where;
3363 else
3364 where = Qnil;
3366 /* We're not using the `unused' slot in the specbinding
3367 structure because this would mean we have to do more
3368 work for simple variables. */
3369 /* FIXME: The third value `current_buffer' is only used in
3370 let_shadows_buffer_binding_p which is itself only used
3371 in set_internal for local_if_set. */
3372 eassert (NILP (where) || EQ (where, cur_buf));
3373 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3375 /* If SYMBOL is a per-buffer variable which doesn't have a
3376 buffer-local value here, make the `let' change the global
3377 value by changing the value of SYMBOL in all buffers not
3378 having their own value. This is consistent with what
3379 happens with other buffer-local variables. */
3380 if (NILP (where)
3381 && sym->redirect == SYMBOL_FORWARDED)
3383 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3384 ++specpdl_ptr;
3385 Fset_default (symbol, value);
3386 return;
3389 else
3390 specpdl_ptr->symbol = symbol;
3392 specpdl_ptr++;
3393 set_internal (symbol, value, Qnil, 1);
3394 break;
3396 default: abort ();
3400 void
3401 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3403 eassert (!handling_signal);
3405 if (specpdl_ptr == specpdl + specpdl_size)
3406 grow_specpdl ();
3407 specpdl_ptr->func = function;
3408 specpdl_ptr->symbol = Qnil;
3409 specpdl_ptr->old_value = arg;
3410 specpdl_ptr++;
3413 Lisp_Object
3414 unbind_to (int count, Lisp_Object value)
3416 Lisp_Object quitf = Vquit_flag;
3417 struct gcpro gcpro1, gcpro2;
3419 GCPRO2 (value, quitf);
3420 Vquit_flag = Qnil;
3422 while (specpdl_ptr != specpdl + count)
3424 /* Copy the binding, and decrement specpdl_ptr, before we do
3425 the work to unbind it. We decrement first
3426 so that an error in unbinding won't try to unbind
3427 the same entry again, and we copy the binding first
3428 in case more bindings are made during some of the code we run. */
3430 struct specbinding this_binding;
3431 this_binding = *--specpdl_ptr;
3433 if (this_binding.func != 0)
3434 (*this_binding.func) (this_binding.old_value);
3435 /* If the symbol is a list, it is really (SYMBOL WHERE
3436 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3437 frame. If WHERE is a buffer or frame, this indicates we
3438 bound a variable that had a buffer-local or frame-local
3439 binding. WHERE nil means that the variable had the default
3440 value when it was bound. CURRENT-BUFFER is the buffer that
3441 was current when the variable was bound. */
3442 else if (CONSP (this_binding.symbol))
3444 Lisp_Object symbol, where;
3446 symbol = XCAR (this_binding.symbol);
3447 where = XCAR (XCDR (this_binding.symbol));
3449 if (NILP (where))
3450 Fset_default (symbol, this_binding.old_value);
3451 /* If `where' is non-nil, reset the value in the appropriate
3452 local binding, but only if that binding still exists. */
3453 else if (BUFFERP (where)
3454 ? !NILP (Flocal_variable_p (symbol, where))
3455 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3456 set_internal (symbol, this_binding.old_value, where, 1);
3458 /* If variable has a trivial value (no forwarding), we can
3459 just set it. No need to check for constant symbols here,
3460 since that was already done by specbind. */
3461 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3462 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3463 this_binding.old_value);
3464 else
3465 /* NOTE: we only ever come here if make_local_foo was used for
3466 the first time on this var within this let. */
3467 Fset_default (this_binding.symbol, this_binding.old_value);
3470 if (NILP (Vquit_flag) && !NILP (quitf))
3471 Vquit_flag = quitf;
3473 UNGCPRO;
3474 return value;
3477 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3478 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3479 A special variable is one that will be bound dynamically, even in a
3480 context where binding is lexical by default. */)
3481 (Lisp_Object symbol)
3483 CHECK_SYMBOL (symbol);
3484 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3488 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3489 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3490 The debugger is entered when that frame exits, if the flag is non-nil. */)
3491 (Lisp_Object level, Lisp_Object flag)
3493 register struct backtrace *backlist = backtrace_list;
3494 register int i;
3496 CHECK_NUMBER (level);
3498 for (i = 0; backlist && i < XINT (level); i++)
3500 backlist = backlist->next;
3503 if (backlist)
3504 backlist->debug_on_exit = !NILP (flag);
3506 return flag;
3509 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3510 doc: /* Print a trace of Lisp function calls currently active.
3511 Output stream used is value of `standard-output'. */)
3512 (void)
3514 register struct backtrace *backlist = backtrace_list;
3515 Lisp_Object tail;
3516 Lisp_Object tem;
3517 struct gcpro gcpro1;
3518 Lisp_Object old_print_level = Vprint_level;
3520 if (NILP (Vprint_level))
3521 XSETFASTINT (Vprint_level, 8);
3523 tail = Qnil;
3524 GCPRO1 (tail);
3526 while (backlist)
3528 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3529 if (backlist->nargs == UNEVALLED)
3531 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3532 write_string ("\n", -1);
3534 else
3536 tem = *backlist->function;
3537 Fprin1 (tem, Qnil); /* This can QUIT. */
3538 write_string ("(", -1);
3539 if (backlist->nargs == MANY)
3540 { /* FIXME: Can this happen? */
3541 int i;
3542 for (tail = *backlist->args, i = 0;
3543 !NILP (tail);
3544 tail = Fcdr (tail), i = 1)
3546 if (i) write_string (" ", -1);
3547 Fprin1 (Fcar (tail), Qnil);
3550 else
3552 ptrdiff_t i;
3553 for (i = 0; i < backlist->nargs; i++)
3555 if (i) write_string (" ", -1);
3556 Fprin1 (backlist->args[i], Qnil);
3559 write_string (")\n", -1);
3561 backlist = backlist->next;
3564 Vprint_level = old_print_level;
3565 UNGCPRO;
3566 return Qnil;
3569 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3570 doc: /* Return the function and arguments NFRAMES up from current execution point.
3571 If that frame has not evaluated the arguments yet (or is a special form),
3572 the value is (nil FUNCTION ARG-FORMS...).
3573 If that frame has evaluated its arguments and called its function already,
3574 the value is (t FUNCTION ARG-VALUES...).
3575 A &rest arg is represented as the tail of the list ARG-VALUES.
3576 FUNCTION is whatever was supplied as car of evaluated list,
3577 or a lambda expression for macro calls.
3578 If NFRAMES is more than the number of frames, the value is nil. */)
3579 (Lisp_Object nframes)
3581 register struct backtrace *backlist = backtrace_list;
3582 register EMACS_INT i;
3583 Lisp_Object tem;
3585 CHECK_NATNUM (nframes);
3587 /* Find the frame requested. */
3588 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3589 backlist = backlist->next;
3591 if (!backlist)
3592 return Qnil;
3593 if (backlist->nargs == UNEVALLED)
3594 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3595 else
3597 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3598 tem = *backlist->args;
3599 else
3600 tem = Flist (backlist->nargs, backlist->args);
3602 return Fcons (Qt, Fcons (*backlist->function, tem));
3607 #if BYTE_MARK_STACK
3608 void
3609 mark_backtrace (void)
3611 register struct backtrace *backlist;
3612 ptrdiff_t i;
3614 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3616 mark_object (*backlist->function);
3618 if (backlist->nargs == UNEVALLED
3619 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3620 i = 1;
3621 else
3622 i = backlist->nargs;
3623 while (i--)
3624 mark_object (backlist->args[i]);
3627 #endif
3629 void
3630 syms_of_eval (void)
3632 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3633 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3634 If Lisp code tries to increase the total number past this amount,
3635 an error is signaled.
3636 You can safely use a value considerably larger than the default value,
3637 if that proves inconveniently small. However, if you increase it too far,
3638 Emacs could run out of memory trying to make the stack bigger. */);
3640 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3641 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3643 This limit serves to catch infinite recursions for you before they cause
3644 actual stack overflow in C, which would be fatal for Emacs.
3645 You can safely make it considerably larger than its default value,
3646 if that proves inconveniently small. However, if you increase it too far,
3647 Emacs could overflow the real C stack, and crash. */);
3649 DEFVAR_LISP ("quit-flag", Vquit_flag,
3650 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3651 If the value is t, that means do an ordinary quit.
3652 If the value equals `throw-on-input', that means quit by throwing
3653 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3654 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3655 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3656 Vquit_flag = Qnil;
3658 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3659 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3660 Note that `quit-flag' will still be set by typing C-g,
3661 so a quit will be signaled as soon as `inhibit-quit' is nil.
3662 To prevent this happening, set `quit-flag' to nil
3663 before making `inhibit-quit' nil. */);
3664 Vinhibit_quit = Qnil;
3666 DEFSYM (Qinhibit_quit, "inhibit-quit");
3667 DEFSYM (Qautoload, "autoload");
3668 DEFSYM (Qdebug_on_error, "debug-on-error");
3669 DEFSYM (Qmacro, "macro");
3670 DEFSYM (Qdeclare, "declare");
3672 /* Note that the process handling also uses Qexit, but we don't want
3673 to staticpro it twice, so we just do it here. */
3674 DEFSYM (Qexit, "exit");
3676 DEFSYM (Qinteractive, "interactive");
3677 DEFSYM (Qcommandp, "commandp");
3678 DEFSYM (Qdefun, "defun");
3679 DEFSYM (Qand_rest, "&rest");
3680 DEFSYM (Qand_optional, "&optional");
3681 DEFSYM (Qclosure, "closure");
3682 DEFSYM (Qdebug, "debug");
3684 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3685 doc: /* *Non-nil means enter debugger if an error is signaled.
3686 Does not apply to errors handled by `condition-case' or those
3687 matched by `debug-ignored-errors'.
3688 If the value is a list, an error only means to enter the debugger
3689 if one of its condition symbols appears in the list.
3690 When you evaluate an expression interactively, this variable
3691 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3692 The command `toggle-debug-on-error' toggles this.
3693 See also the variable `debug-on-quit'. */);
3694 Vdebug_on_error = Qnil;
3696 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3697 doc: /* *List of errors for which the debugger should not be called.
3698 Each element may be a condition-name or a regexp that matches error messages.
3699 If any element applies to a given error, that error skips the debugger
3700 and just returns to top level.
3701 This overrides the variable `debug-on-error'.
3702 It does not apply to errors handled by `condition-case'. */);
3703 Vdebug_ignored_errors = Qnil;
3705 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3706 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3707 Does not apply if quit is handled by a `condition-case'. */);
3708 debug_on_quit = 0;
3710 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3711 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3713 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3714 doc: /* Non-nil means debugger may continue execution.
3715 This is nil when the debugger is called under circumstances where it
3716 might not be safe to continue. */);
3717 debugger_may_continue = 1;
3719 DEFVAR_LISP ("debugger", Vdebugger,
3720 doc: /* Function to call to invoke debugger.
3721 If due to frame exit, args are `exit' and the value being returned;
3722 this function's value will be returned instead of that.
3723 If due to error, args are `error' and a list of the args to `signal'.
3724 If due to `apply' or `funcall' entry, one arg, `lambda'.
3725 If due to `eval' entry, one arg, t. */);
3726 Vdebugger = Qnil;
3728 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3729 doc: /* If non-nil, this is a function for `signal' to call.
3730 It receives the same arguments that `signal' was given.
3731 The Edebug package uses this to regain control. */);
3732 Vsignal_hook_function = Qnil;
3734 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3735 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3736 Note that `debug-on-error', `debug-on-quit' and friends
3737 still determine whether to handle the particular condition. */);
3738 Vdebug_on_signal = Qnil;
3740 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3741 doc: /* Function to process declarations in a macro definition.
3742 The function will be called with two args MACRO and DECL.
3743 MACRO is the name of the macro being defined.
3744 DECL is a list `(declare ...)' containing the declarations.
3745 The value the function returns is not used. */);
3746 Vmacro_declaration_function = Qnil;
3748 /* When lexical binding is being used,
3749 vinternal_interpreter_environment is non-nil, and contains an alist
3750 of lexically-bound variable, or (t), indicating an empty
3751 environment. The lisp name of this variable would be
3752 `internal-interpreter-environment' if it weren't hidden.
3753 Every element of this list can be either a cons (VAR . VAL)
3754 specifying a lexical binding, or a single symbol VAR indicating
3755 that this variable should use dynamic scoping. */
3756 DEFSYM (Qinternal_interpreter_environment, "internal-interpreter-environment");
3757 DEFVAR_LISP ("internal-interpreter-environment",
3758 Vinternal_interpreter_environment,
3759 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3760 When lexical binding is not being used, this variable is nil.
3761 A value of `(t)' indicates an empty environment, otherwise it is an
3762 alist of active lexical bindings. */);
3763 Vinternal_interpreter_environment = Qnil;
3764 /* Don't export this variable to Elisp, so no one can mess with it
3765 (Just imagine if someone makes it buffer-local). */
3766 Funintern (Qinternal_interpreter_environment, Qnil);
3768 DEFSYM (Vrun_hooks, "run-hooks");
3770 staticpro (&Vautoload_queue);
3771 Vautoload_queue = Qnil;
3772 staticpro (&Vsignaling_function);
3773 Vsignaling_function = Qnil;
3775 inhibit_lisp_code = Qnil;
3777 defsubr (&Sor);
3778 defsubr (&Sand);
3779 defsubr (&Sif);
3780 defsubr (&Scond);
3781 defsubr (&Sprogn);
3782 defsubr (&Sprog1);
3783 defsubr (&Sprog2);
3784 defsubr (&Ssetq);
3785 defsubr (&Squote);
3786 defsubr (&Sfunction);
3787 defsubr (&Sdefun);
3788 defsubr (&Sdefmacro);
3789 defsubr (&Sdefvar);
3790 defsubr (&Sdefvaralias);
3791 defsubr (&Sdefconst);
3792 defsubr (&Suser_variable_p);
3793 defsubr (&Slet);
3794 defsubr (&SletX);
3795 defsubr (&Swhile);
3796 defsubr (&Smacroexpand);
3797 defsubr (&Scatch);
3798 defsubr (&Sthrow);
3799 defsubr (&Sunwind_protect);
3800 defsubr (&Scondition_case);
3801 defsubr (&Ssignal);
3802 defsubr (&Sinteractive_p);
3803 defsubr (&Scalled_interactively_p);
3804 defsubr (&Scommandp);
3805 defsubr (&Sautoload);
3806 defsubr (&Seval);
3807 defsubr (&Sapply);
3808 defsubr (&Sfuncall);
3809 defsubr (&Srun_hooks);
3810 defsubr (&Srun_hook_with_args);
3811 defsubr (&Srun_hook_with_args_until_success);
3812 defsubr (&Srun_hook_with_args_until_failure);
3813 defsubr (&Srun_hook_wrapped);
3814 defsubr (&Sfetch_bytecode);
3815 defsubr (&Sbacktrace_debug);
3816 defsubr (&Sbacktrace);
3817 defsubr (&Sbacktrace_frame);
3818 defsubr (&Sspecial_variable_p);
3819 defsubr (&Sfunctionp);