]> code.delx.au - gnu-emacs/blob - src/eval.c
Don't install keyboard hook when debugged on MS-Windows
[gnu-emacs] / src / eval.c
1 /* Evaluator for GNU Emacs Lisp interpreter.
2
3 Copyright (C) 1985-1987, 1993-1995, 1999-2016 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #include "commands.h"
28 #include "keyboard.h"
29 #include "dispextern.h"
30 #include "buffer.h"
31
32 /* Chain of condition and catch handlers currently in effect. */
33
34 struct handler *handlerlist;
35
36 /* Non-nil means record all fset's and provide's, to be undone
37 if the file being autoloaded is not fully loaded.
38 They are recorded by being consed onto the front of Vautoload_queue:
39 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
40
41 Lisp_Object Vautoload_queue;
42
43 /* This holds either the symbol `run-hooks' or nil.
44 It is nil at an early stage of startup, and when Emacs
45 is shutting down. */
46 Lisp_Object Vrun_hooks;
47
48 /* Current number of specbindings allocated in specpdl, not counting
49 the dummy entry specpdl[-1]. */
50
51 ptrdiff_t specpdl_size;
52
53 /* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
54 only so that its address can be taken. */
55
56 union specbinding *specpdl;
57
58 /* Pointer to first unused element in specpdl. */
59
60 union specbinding *specpdl_ptr;
61
62 /* Depth in Lisp evaluations and function calls. */
63
64 static EMACS_INT lisp_eval_depth;
65
66 /* The value of num_nonmacro_input_events as of the last time we
67 started to enter the debugger. If we decide to enter the debugger
68 again when this is still equal to num_nonmacro_input_events, then we
69 know that the debugger itself has an error, and we should just
70 signal the error instead of entering an infinite loop of debugger
71 invocations. */
72
73 static EMACS_INT when_entered_debugger;
74
75 /* The function from which the last `signal' was called. Set in
76 Fsignal. */
77 /* FIXME: We should probably get rid of this! */
78 Lisp_Object Vsignaling_function;
79
80 /* If non-nil, Lisp code must not be run since some part of Emacs is in
81 an inconsistent state. Currently unused. */
82 Lisp_Object inhibit_lisp_code;
83
84 /* These would ordinarily be static, but they need to be visible to GDB. */
85 bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
86 Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
87 Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
88 union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
89 union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
90
91 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
92 static Lisp_Object apply_lambda (Lisp_Object, Lisp_Object, ptrdiff_t);
93 static Lisp_Object lambda_arity (Lisp_Object);
94
95 static Lisp_Object
96 specpdl_symbol (union specbinding *pdl)
97 {
98 eassert (pdl->kind >= SPECPDL_LET);
99 return pdl->let.symbol;
100 }
101
102 static Lisp_Object
103 specpdl_old_value (union specbinding *pdl)
104 {
105 eassert (pdl->kind >= SPECPDL_LET);
106 return pdl->let.old_value;
107 }
108
109 static void
110 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
111 {
112 eassert (pdl->kind >= SPECPDL_LET);
113 pdl->let.old_value = val;
114 }
115
116 static Lisp_Object
117 specpdl_where (union specbinding *pdl)
118 {
119 eassert (pdl->kind > SPECPDL_LET);
120 return pdl->let.where;
121 }
122
123 static Lisp_Object
124 specpdl_arg (union specbinding *pdl)
125 {
126 eassert (pdl->kind == SPECPDL_UNWIND);
127 return pdl->unwind.arg;
128 }
129
130 Lisp_Object
131 backtrace_function (union specbinding *pdl)
132 {
133 eassert (pdl->kind == SPECPDL_BACKTRACE);
134 return pdl->bt.function;
135 }
136
137 static ptrdiff_t
138 backtrace_nargs (union specbinding *pdl)
139 {
140 eassert (pdl->kind == SPECPDL_BACKTRACE);
141 return pdl->bt.nargs;
142 }
143
144 Lisp_Object *
145 backtrace_args (union specbinding *pdl)
146 {
147 eassert (pdl->kind == SPECPDL_BACKTRACE);
148 return pdl->bt.args;
149 }
150
151 static bool
152 backtrace_debug_on_exit (union specbinding *pdl)
153 {
154 eassert (pdl->kind == SPECPDL_BACKTRACE);
155 return pdl->bt.debug_on_exit;
156 }
157
158 /* Functions to modify slots of backtrace records. */
159
160 static void
161 set_backtrace_args (union specbinding *pdl, Lisp_Object *args, ptrdiff_t nargs)
162 {
163 eassert (pdl->kind == SPECPDL_BACKTRACE);
164 pdl->bt.args = args;
165 pdl->bt.nargs = nargs;
166 }
167
168 static void
169 set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
170 {
171 eassert (pdl->kind == SPECPDL_BACKTRACE);
172 pdl->bt.debug_on_exit = doe;
173 }
174
175 /* Helper functions to scan the backtrace. */
176
177 bool
178 backtrace_p (union specbinding *pdl)
179 { return pdl >= specpdl; }
180
181 union specbinding *
182 backtrace_top (void)
183 {
184 union specbinding *pdl = specpdl_ptr - 1;
185 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
186 pdl--;
187 return pdl;
188 }
189
190 union specbinding *
191 backtrace_next (union specbinding *pdl)
192 {
193 pdl--;
194 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
195 pdl--;
196 return pdl;
197 }
198
199 /* Return a pointer to somewhere near the top of the C stack. */
200 void *
201 near_C_stack_top (void)
202 {
203 return backtrace_args (backtrace_top ());
204 }
205
206 void
207 init_eval_once (void)
208 {
209 enum { size = 50 };
210 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
211 specpdl_size = size;
212 specpdl = specpdl_ptr = pdlvec + 1;
213 /* Don't forget to update docs (lispref node "Local Variables"). */
214 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
215 max_lisp_eval_depth = 800;
216
217 Vrun_hooks = Qnil;
218 }
219
220 static struct handler handlerlist_sentinel;
221
222 void
223 init_eval (void)
224 {
225 byte_stack_list = 0;
226 specpdl_ptr = specpdl;
227 { /* Put a dummy catcher at top-level so that handlerlist is never NULL.
228 This is important since handlerlist->nextfree holds the freelist
229 which would otherwise leak every time we unwind back to top-level. */
230 handlerlist = handlerlist_sentinel.nextfree = &handlerlist_sentinel;
231 struct handler *c = push_handler (Qunbound, CATCHER);
232 eassert (c == &handlerlist_sentinel);
233 handlerlist_sentinel.nextfree = NULL;
234 handlerlist_sentinel.next = NULL;
235 }
236 Vquit_flag = Qnil;
237 debug_on_next_call = 0;
238 lisp_eval_depth = 0;
239 /* This is less than the initial value of num_nonmacro_input_events. */
240 when_entered_debugger = -1;
241 }
242
243 /* Unwind-protect function used by call_debugger. */
244
245 static void
246 restore_stack_limits (Lisp_Object data)
247 {
248 max_specpdl_size = XINT (XCAR (data));
249 max_lisp_eval_depth = XINT (XCDR (data));
250 }
251
252 static void grow_specpdl (void);
253
254 /* Call the Lisp debugger, giving it argument ARG. */
255
256 Lisp_Object
257 call_debugger (Lisp_Object arg)
258 {
259 bool debug_while_redisplaying;
260 ptrdiff_t count = SPECPDL_INDEX ();
261 Lisp_Object val;
262 EMACS_INT old_depth = max_lisp_eval_depth;
263 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
264 EMACS_INT old_max = max (max_specpdl_size, count);
265
266 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
267 max_lisp_eval_depth = lisp_eval_depth + 40;
268
269 /* While debugging Bug#16603, previous value of 100 was found
270 too small to avoid specpdl overflow in the debugger itself. */
271 if (max_specpdl_size - 200 < count)
272 max_specpdl_size = count + 200;
273
274 if (old_max == count)
275 {
276 /* We can enter the debugger due to specpdl overflow (Bug#16603). */
277 specpdl_ptr--;
278 grow_specpdl ();
279 }
280
281 /* Restore limits after leaving the debugger. */
282 record_unwind_protect (restore_stack_limits,
283 Fcons (make_number (old_max),
284 make_number (old_depth)));
285
286 #ifdef HAVE_WINDOW_SYSTEM
287 if (display_hourglass_p)
288 cancel_hourglass ();
289 #endif
290
291 debug_on_next_call = 0;
292 when_entered_debugger = num_nonmacro_input_events;
293
294 /* Resetting redisplaying_p to 0 makes sure that debug output is
295 displayed if the debugger is invoked during redisplay. */
296 debug_while_redisplaying = redisplaying_p;
297 redisplaying_p = 0;
298 specbind (intern ("debugger-may-continue"),
299 debug_while_redisplaying ? Qnil : Qt);
300 specbind (Qinhibit_redisplay, Qnil);
301 specbind (Qinhibit_debugger, Qt);
302
303 #if 0 /* Binding this prevents execution of Lisp code during
304 redisplay, which necessarily leads to display problems. */
305 specbind (Qinhibit_eval_during_redisplay, Qt);
306 #endif
307
308 val = apply1 (Vdebugger, arg);
309
310 /* Interrupting redisplay and resuming it later is not safe under
311 all circumstances. So, when the debugger returns, abort the
312 interrupted redisplay by going back to the top-level. */
313 if (debug_while_redisplaying)
314 Ftop_level ();
315
316 return unbind_to (count, val);
317 }
318
319 static void
320 do_debug_on_call (Lisp_Object code, ptrdiff_t count)
321 {
322 debug_on_next_call = 0;
323 set_backtrace_debug_on_exit (specpdl + count, true);
324 call_debugger (list1 (code));
325 }
326 \f
327 /* NOTE!!! Every function that can call EVAL must protect its args
328 and temporaries from garbage collection while it needs them.
329 The definition of `For' shows what you have to do. */
330
331 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
332 doc: /* Eval args until one of them yields non-nil, then return that value.
333 The remaining args are not evalled at all.
334 If all args return nil, return nil.
335 usage: (or CONDITIONS...) */)
336 (Lisp_Object args)
337 {
338 Lisp_Object val = Qnil;
339
340 while (CONSP (args))
341 {
342 val = eval_sub (XCAR (args));
343 if (!NILP (val))
344 break;
345 args = XCDR (args);
346 }
347
348 return val;
349 }
350
351 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
352 doc: /* Eval args until one of them yields nil, then return nil.
353 The remaining args are not evalled at all.
354 If no arg yields nil, return the last arg's value.
355 usage: (and CONDITIONS...) */)
356 (Lisp_Object args)
357 {
358 Lisp_Object val = Qt;
359
360 while (CONSP (args))
361 {
362 val = eval_sub (XCAR (args));
363 if (NILP (val))
364 break;
365 args = XCDR (args);
366 }
367
368 return val;
369 }
370
371 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
372 doc: /* If COND yields non-nil, do THEN, else do ELSE...
373 Returns the value of THEN or the value of the last of the ELSE's.
374 THEN must be one expression, but ELSE... can be zero or more expressions.
375 If COND yields nil, and there are no ELSE's, the value is nil.
376 usage: (if COND THEN ELSE...) */)
377 (Lisp_Object args)
378 {
379 Lisp_Object cond;
380
381 cond = eval_sub (XCAR (args));
382
383 if (!NILP (cond))
384 return eval_sub (Fcar (XCDR (args)));
385 return Fprogn (XCDR (XCDR (args)));
386 }
387
388 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
389 doc: /* Try each clause until one succeeds.
390 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
391 and, if the value is non-nil, this clause succeeds:
392 then the expressions in BODY are evaluated and the last one's
393 value is the value of the cond-form.
394 If a clause has one element, as in (CONDITION), then the cond-form
395 returns CONDITION's value, if that is non-nil.
396 If no clause succeeds, cond returns nil.
397 usage: (cond CLAUSES...) */)
398 (Lisp_Object args)
399 {
400 Lisp_Object val = args;
401
402 while (CONSP (args))
403 {
404 Lisp_Object clause = XCAR (args);
405 val = eval_sub (Fcar (clause));
406 if (!NILP (val))
407 {
408 if (!NILP (XCDR (clause)))
409 val = Fprogn (XCDR (clause));
410 break;
411 }
412 args = XCDR (args);
413 }
414
415 return val;
416 }
417
418 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
419 doc: /* Eval BODY forms sequentially and return value of last one.
420 usage: (progn BODY...) */)
421 (Lisp_Object body)
422 {
423 Lisp_Object val = Qnil;
424
425 while (CONSP (body))
426 {
427 val = eval_sub (XCAR (body));
428 body = XCDR (body);
429 }
430
431 return val;
432 }
433
434 /* Evaluate BODY sequentially, discarding its value. Suitable for
435 record_unwind_protect. */
436
437 void
438 unwind_body (Lisp_Object body)
439 {
440 Fprogn (body);
441 }
442
443 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
444 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
445 The value of FIRST is saved during the evaluation of the remaining args,
446 whose values are discarded.
447 usage: (prog1 FIRST BODY...) */)
448 (Lisp_Object args)
449 {
450 Lisp_Object val;
451 Lisp_Object args_left;
452
453 args_left = args;
454 val = args;
455
456 val = eval_sub (XCAR (args_left));
457 while (CONSP (args_left = XCDR (args_left)))
458 eval_sub (XCAR (args_left));
459
460 return val;
461 }
462
463 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
464 doc: /* Eval FORM1, FORM2 and BODY sequentially; return value from FORM2.
465 The value of FORM2 is saved during the evaluation of the
466 remaining args, whose values are discarded.
467 usage: (prog2 FORM1 FORM2 BODY...) */)
468 (Lisp_Object args)
469 {
470 eval_sub (XCAR (args));
471 return Fprog1 (XCDR (args));
472 }
473
474 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
475 doc: /* Set each SYM to the value of its VAL.
476 The symbols SYM are variables; they are literal (not evaluated).
477 The values VAL are expressions; they are evaluated.
478 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
479 The second VAL is not computed until after the first SYM is set, and so on;
480 each VAL can use the new value of variables set earlier in the `setq'.
481 The return value of the `setq' form is the value of the last VAL.
482 usage: (setq [SYM VAL]...) */)
483 (Lisp_Object args)
484 {
485 Lisp_Object val, sym, lex_binding;
486
487 val = args;
488 if (CONSP (args))
489 {
490 Lisp_Object args_left = args;
491 Lisp_Object numargs = Flength (args);
492
493 if (XINT (numargs) & 1)
494 xsignal2 (Qwrong_number_of_arguments, Qsetq, numargs);
495
496 do
497 {
498 val = eval_sub (Fcar (XCDR (args_left)));
499 sym = XCAR (args_left);
500
501 /* Like for eval_sub, we do not check declared_special here since
502 it's been done when let-binding. */
503 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
504 && SYMBOLP (sym)
505 && !NILP (lex_binding
506 = Fassq (sym, Vinternal_interpreter_environment)))
507 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
508 else
509 Fset (sym, val); /* SYM is dynamically bound. */
510
511 args_left = Fcdr (XCDR (args_left));
512 }
513 while (CONSP (args_left));
514 }
515
516 return val;
517 }
518
519 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
520 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
521 Warning: `quote' does not construct its return value, but just returns
522 the value that was pre-constructed by the Lisp reader (see info node
523 `(elisp)Printed Representation').
524 This means that \\='(a . b) is not identical to (cons \\='a \\='b): the former
525 does not cons. Quoting should be reserved for constants that will
526 never be modified by side-effects, unless you like self-modifying code.
527 See the common pitfall in info node `(elisp)Rearrangement' for an example
528 of unexpected results when a quoted object is modified.
529 usage: (quote ARG) */)
530 (Lisp_Object args)
531 {
532 if (CONSP (XCDR (args)))
533 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
534 return XCAR (args);
535 }
536
537 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
538 doc: /* Like `quote', but preferred for objects which are functions.
539 In byte compilation, `function' causes its argument to be compiled.
540 `quote' cannot do that.
541 usage: (function ARG) */)
542 (Lisp_Object args)
543 {
544 Lisp_Object quoted = XCAR (args);
545
546 if (CONSP (XCDR (args)))
547 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
548
549 if (!NILP (Vinternal_interpreter_environment)
550 && CONSP (quoted)
551 && EQ (XCAR (quoted), Qlambda))
552 { /* This is a lambda expression within a lexical environment;
553 return an interpreted closure instead of a simple lambda. */
554 Lisp_Object cdr = XCDR (quoted);
555 Lisp_Object tmp = cdr;
556 if (CONSP (tmp)
557 && (tmp = XCDR (tmp), CONSP (tmp))
558 && (tmp = XCAR (tmp), CONSP (tmp))
559 && (EQ (QCdocumentation, XCAR (tmp))))
560 { /* Handle the special (:documentation <form>) to build the docstring
561 dynamically. */
562 Lisp_Object docstring = eval_sub (Fcar (XCDR (tmp)));
563 CHECK_STRING (docstring);
564 cdr = Fcons (XCAR (cdr), Fcons (docstring, XCDR (XCDR (cdr))));
565 }
566 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
567 cdr));
568 }
569 else
570 /* Simply quote the argument. */
571 return quoted;
572 }
573
574
575 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
576 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
577 Aliased variables always have the same value; setting one sets the other.
578 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
579 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
580 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
581 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
582 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
583 The return value is BASE-VARIABLE. */)
584 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
585 {
586 struct Lisp_Symbol *sym;
587
588 CHECK_SYMBOL (new_alias);
589 CHECK_SYMBOL (base_variable);
590
591 sym = XSYMBOL (new_alias);
592
593 if (sym->constant)
594 /* Not sure why, but why not? */
595 error ("Cannot make a constant an alias");
596
597 switch (sym->redirect)
598 {
599 case SYMBOL_FORWARDED:
600 error ("Cannot make an internal variable an alias");
601 case SYMBOL_LOCALIZED:
602 error ("Don't know how to make a localized variable an alias");
603 case SYMBOL_PLAINVAL:
604 case SYMBOL_VARALIAS:
605 break;
606 default:
607 emacs_abort ();
608 }
609
610 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
611 If n_a is bound, but b_v is not, set the value of b_v to n_a,
612 so that old-code that affects n_a before the aliasing is setup
613 still works. */
614 if (NILP (Fboundp (base_variable)))
615 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
616
617 {
618 union specbinding *p;
619
620 for (p = specpdl_ptr; p > specpdl; )
621 if ((--p)->kind >= SPECPDL_LET
622 && (EQ (new_alias, specpdl_symbol (p))))
623 error ("Don't know how to make a let-bound variable an alias");
624 }
625
626 sym->declared_special = 1;
627 XSYMBOL (base_variable)->declared_special = 1;
628 sym->redirect = SYMBOL_VARALIAS;
629 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
630 sym->constant = SYMBOL_CONSTANT_P (base_variable);
631 LOADHIST_ATTACH (new_alias);
632 /* Even if docstring is nil: remove old docstring. */
633 Fput (new_alias, Qvariable_documentation, docstring);
634
635 return base_variable;
636 }
637
638 static union specbinding *
639 default_toplevel_binding (Lisp_Object symbol)
640 {
641 union specbinding *binding = NULL;
642 union specbinding *pdl = specpdl_ptr;
643 while (pdl > specpdl)
644 {
645 switch ((--pdl)->kind)
646 {
647 case SPECPDL_LET_DEFAULT:
648 case SPECPDL_LET:
649 if (EQ (specpdl_symbol (pdl), symbol))
650 binding = pdl;
651 break;
652
653 case SPECPDL_UNWIND:
654 case SPECPDL_UNWIND_PTR:
655 case SPECPDL_UNWIND_INT:
656 case SPECPDL_UNWIND_VOID:
657 case SPECPDL_BACKTRACE:
658 case SPECPDL_LET_LOCAL:
659 break;
660
661 default:
662 emacs_abort ();
663 }
664 }
665 return binding;
666 }
667
668 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
669 doc: /* Return SYMBOL's toplevel default value.
670 "Toplevel" means outside of any let binding. */)
671 (Lisp_Object symbol)
672 {
673 union specbinding *binding = default_toplevel_binding (symbol);
674 Lisp_Object value
675 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
676 if (!EQ (value, Qunbound))
677 return value;
678 xsignal1 (Qvoid_variable, symbol);
679 }
680
681 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
682 Sset_default_toplevel_value, 2, 2, 0,
683 doc: /* Set SYMBOL's toplevel default value to VALUE.
684 "Toplevel" means outside of any let binding. */)
685 (Lisp_Object symbol, Lisp_Object value)
686 {
687 union specbinding *binding = default_toplevel_binding (symbol);
688 if (binding)
689 set_specpdl_old_value (binding, value);
690 else
691 Fset_default (symbol, value);
692 return Qnil;
693 }
694
695 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
696 doc: /* Define SYMBOL as a variable, and return SYMBOL.
697 You are not required to define a variable in order to use it, but
698 defining it lets you supply an initial value and documentation, which
699 can be referred to by the Emacs help facilities and other programming
700 tools. The `defvar' form also declares the variable as \"special\",
701 so that it is always dynamically bound even if `lexical-binding' is t.
702
703 The optional argument INITVALUE is evaluated, and used to set SYMBOL,
704 only if SYMBOL's value is void. If SYMBOL is buffer-local, its
705 default value is what is set; buffer-local values are not affected.
706 If INITVALUE is missing, SYMBOL's value is not set.
707
708 If SYMBOL has a local binding, then this form affects the local
709 binding. This is usually not what you want. Thus, if you need to
710 load a file defining variables, with this form or with `defconst' or
711 `defcustom', you should always load that file _outside_ any bindings
712 for these variables. (`defconst' and `defcustom' behave similarly in
713 this respect.)
714
715 The optional argument DOCSTRING is a documentation string for the
716 variable.
717
718 To define a user option, use `defcustom' instead of `defvar'.
719 usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
720 (Lisp_Object args)
721 {
722 Lisp_Object sym, tem, tail;
723
724 sym = XCAR (args);
725 tail = XCDR (args);
726
727 if (CONSP (tail))
728 {
729 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
730 error ("Too many arguments");
731
732 tem = Fdefault_boundp (sym);
733
734 /* Do it before evaluating the initial value, for self-references. */
735 XSYMBOL (sym)->declared_special = 1;
736
737 if (NILP (tem))
738 Fset_default (sym, eval_sub (XCAR (tail)));
739 else
740 { /* Check if there is really a global binding rather than just a let
741 binding that shadows the global unboundness of the var. */
742 union specbinding *binding = default_toplevel_binding (sym);
743 if (binding && EQ (specpdl_old_value (binding), Qunbound))
744 {
745 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
746 }
747 }
748 tail = XCDR (tail);
749 tem = Fcar (tail);
750 if (!NILP (tem))
751 {
752 if (!NILP (Vpurify_flag))
753 tem = Fpurecopy (tem);
754 Fput (sym, Qvariable_documentation, tem);
755 }
756 LOADHIST_ATTACH (sym);
757 }
758 else if (!NILP (Vinternal_interpreter_environment)
759 && !XSYMBOL (sym)->declared_special)
760 /* A simple (defvar foo) with lexical scoping does "nothing" except
761 declare that var to be dynamically scoped *locally* (i.e. within
762 the current file or let-block). */
763 Vinternal_interpreter_environment
764 = Fcons (sym, Vinternal_interpreter_environment);
765 else
766 {
767 /* Simple (defvar <var>) should not count as a definition at all.
768 It could get in the way of other definitions, and unloading this
769 package could try to make the variable unbound. */
770 }
771
772 return sym;
773 }
774
775 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
776 doc: /* Define SYMBOL as a constant variable.
777 This declares that neither programs nor users should ever change the
778 value. This constancy is not actually enforced by Emacs Lisp, but
779 SYMBOL is marked as a special variable so that it is never lexically
780 bound.
781
782 The `defconst' form always sets the value of SYMBOL to the result of
783 evalling INITVALUE. If SYMBOL is buffer-local, its default value is
784 what is set; buffer-local values are not affected. If SYMBOL has a
785 local binding, then this form sets the local binding's value.
786 However, you should normally not make local bindings for variables
787 defined with this form.
788
789 The optional DOCSTRING specifies the variable's documentation string.
790 usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
791 (Lisp_Object args)
792 {
793 Lisp_Object sym, tem;
794
795 sym = XCAR (args);
796 if (CONSP (Fcdr (XCDR (XCDR (args)))))
797 error ("Too many arguments");
798
799 tem = eval_sub (Fcar (XCDR (args)));
800 if (!NILP (Vpurify_flag))
801 tem = Fpurecopy (tem);
802 Fset_default (sym, tem);
803 XSYMBOL (sym)->declared_special = 1;
804 tem = Fcar (XCDR (XCDR (args)));
805 if (!NILP (tem))
806 {
807 if (!NILP (Vpurify_flag))
808 tem = Fpurecopy (tem);
809 Fput (sym, Qvariable_documentation, tem);
810 }
811 Fput (sym, Qrisky_local_variable, Qt);
812 LOADHIST_ATTACH (sym);
813 return sym;
814 }
815
816 /* Make SYMBOL lexically scoped. */
817 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
818 Smake_var_non_special, 1, 1, 0,
819 doc: /* Internal function. */)
820 (Lisp_Object symbol)
821 {
822 CHECK_SYMBOL (symbol);
823 XSYMBOL (symbol)->declared_special = 0;
824 return Qnil;
825 }
826
827 \f
828 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
829 doc: /* Bind variables according to VARLIST then eval BODY.
830 The value of the last form in BODY is returned.
831 Each element of VARLIST is a symbol (which is bound to nil)
832 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
833 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
834 usage: (let* VARLIST BODY...) */)
835 (Lisp_Object args)
836 {
837 Lisp_Object varlist, var, val, elt, lexenv;
838 ptrdiff_t count = SPECPDL_INDEX ();
839
840 lexenv = Vinternal_interpreter_environment;
841
842 varlist = XCAR (args);
843 while (CONSP (varlist))
844 {
845 QUIT;
846
847 elt = XCAR (varlist);
848 if (SYMBOLP (elt))
849 {
850 var = elt;
851 val = Qnil;
852 }
853 else if (! NILP (Fcdr (Fcdr (elt))))
854 signal_error ("`let' bindings can have only one value-form", elt);
855 else
856 {
857 var = Fcar (elt);
858 val = eval_sub (Fcar (Fcdr (elt)));
859 }
860
861 if (!NILP (lexenv) && SYMBOLP (var)
862 && !XSYMBOL (var)->declared_special
863 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
864 /* Lexically bind VAR by adding it to the interpreter's binding
865 alist. */
866 {
867 Lisp_Object newenv
868 = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
869 if (EQ (Vinternal_interpreter_environment, lexenv))
870 /* Save the old lexical environment on the specpdl stack,
871 but only for the first lexical binding, since we'll never
872 need to revert to one of the intermediate ones. */
873 specbind (Qinternal_interpreter_environment, newenv);
874 else
875 Vinternal_interpreter_environment = newenv;
876 }
877 else
878 specbind (var, val);
879
880 varlist = XCDR (varlist);
881 }
882
883 val = Fprogn (XCDR (args));
884 return unbind_to (count, val);
885 }
886
887 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
888 doc: /* Bind variables according to VARLIST then eval BODY.
889 The value of the last form in BODY is returned.
890 Each element of VARLIST is a symbol (which is bound to nil)
891 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
892 All the VALUEFORMs are evalled before any symbols are bound.
893 usage: (let VARLIST BODY...) */)
894 (Lisp_Object args)
895 {
896 Lisp_Object *temps, tem, lexenv;
897 Lisp_Object elt, varlist;
898 ptrdiff_t count = SPECPDL_INDEX ();
899 ptrdiff_t argnum;
900 USE_SAFE_ALLOCA;
901
902 varlist = XCAR (args);
903
904 /* Make space to hold the values to give the bound variables. */
905 elt = Flength (varlist);
906 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
907
908 /* Compute the values and store them in `temps'. */
909
910 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
911 {
912 QUIT;
913 elt = XCAR (varlist);
914 if (SYMBOLP (elt))
915 temps [argnum++] = Qnil;
916 else if (! NILP (Fcdr (Fcdr (elt))))
917 signal_error ("`let' bindings can have only one value-form", elt);
918 else
919 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
920 }
921
922 lexenv = Vinternal_interpreter_environment;
923
924 varlist = XCAR (args);
925 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
926 {
927 Lisp_Object var;
928
929 elt = XCAR (varlist);
930 var = SYMBOLP (elt) ? elt : Fcar (elt);
931 tem = temps[argnum++];
932
933 if (!NILP (lexenv) && SYMBOLP (var)
934 && !XSYMBOL (var)->declared_special
935 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
936 /* Lexically bind VAR by adding it to the lexenv alist. */
937 lexenv = Fcons (Fcons (var, tem), lexenv);
938 else
939 /* Dynamically bind VAR. */
940 specbind (var, tem);
941 }
942
943 if (!EQ (lexenv, Vinternal_interpreter_environment))
944 /* Instantiate a new lexical environment. */
945 specbind (Qinternal_interpreter_environment, lexenv);
946
947 elt = Fprogn (XCDR (args));
948 SAFE_FREE ();
949 return unbind_to (count, elt);
950 }
951
952 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
953 doc: /* If TEST yields non-nil, eval BODY... and repeat.
954 The order of execution is thus TEST, BODY, TEST, BODY and so on
955 until TEST returns nil.
956 usage: (while TEST BODY...) */)
957 (Lisp_Object args)
958 {
959 Lisp_Object test, body;
960
961 test = XCAR (args);
962 body = XCDR (args);
963 while (!NILP (eval_sub (test)))
964 {
965 QUIT;
966 Fprogn (body);
967 }
968
969 return Qnil;
970 }
971
972 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
973 doc: /* Return result of expanding macros at top level of FORM.
974 If FORM is not a macro call, it is returned unchanged.
975 Otherwise, the macro is expanded and the expansion is considered
976 in place of FORM. When a non-macro-call results, it is returned.
977
978 The second optional arg ENVIRONMENT specifies an environment of macro
979 definitions to shadow the loaded ones for use in file byte-compilation. */)
980 (Lisp_Object form, Lisp_Object environment)
981 {
982 /* With cleanups from Hallvard Furuseth. */
983 register Lisp_Object expander, sym, def, tem;
984
985 while (1)
986 {
987 /* Come back here each time we expand a macro call,
988 in case it expands into another macro call. */
989 if (!CONSP (form))
990 break;
991 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
992 def = sym = XCAR (form);
993 tem = Qnil;
994 /* Trace symbols aliases to other symbols
995 until we get a symbol that is not an alias. */
996 while (SYMBOLP (def))
997 {
998 QUIT;
999 sym = def;
1000 tem = Fassq (sym, environment);
1001 if (NILP (tem))
1002 {
1003 def = XSYMBOL (sym)->function;
1004 if (!NILP (def))
1005 continue;
1006 }
1007 break;
1008 }
1009 /* Right now TEM is the result from SYM in ENVIRONMENT,
1010 and if TEM is nil then DEF is SYM's function definition. */
1011 if (NILP (tem))
1012 {
1013 /* SYM is not mentioned in ENVIRONMENT.
1014 Look at its function definition. */
1015 def = Fautoload_do_load (def, sym, Qmacro);
1016 if (!CONSP (def))
1017 /* Not defined or definition not suitable. */
1018 break;
1019 if (!EQ (XCAR (def), Qmacro))
1020 break;
1021 else expander = XCDR (def);
1022 }
1023 else
1024 {
1025 expander = XCDR (tem);
1026 if (NILP (expander))
1027 break;
1028 }
1029 {
1030 Lisp_Object newform = apply1 (expander, XCDR (form));
1031 if (EQ (form, newform))
1032 break;
1033 else
1034 form = newform;
1035 }
1036 }
1037 return form;
1038 }
1039 \f
1040 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1041 doc: /* Eval BODY allowing nonlocal exits using `throw'.
1042 TAG is evalled to get the tag to use; it must not be nil.
1043
1044 Then the BODY is executed.
1045 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
1046 If no throw happens, `catch' returns the value of the last BODY form.
1047 If a throw happens, it specifies the value to return from `catch'.
1048 usage: (catch TAG BODY...) */)
1049 (Lisp_Object args)
1050 {
1051 Lisp_Object tag = eval_sub (XCAR (args));
1052 return internal_catch (tag, Fprogn, XCDR (args));
1053 }
1054
1055 /* Assert that E is true, as a comment only. Use this instead of
1056 eassert (E) when E contains variables that might be clobbered by a
1057 longjmp. */
1058
1059 #define clobbered_eassert(E) ((void) 0)
1060
1061 /* Set up a catch, then call C function FUNC on argument ARG.
1062 FUNC should return a Lisp_Object.
1063 This is how catches are done from within C code. */
1064
1065 Lisp_Object
1066 internal_catch (Lisp_Object tag,
1067 Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
1068 {
1069 /* This structure is made part of the chain `catchlist'. */
1070 struct handler *c = push_handler (tag, CATCHER);
1071
1072 /* Call FUNC. */
1073 if (! sys_setjmp (c->jmp))
1074 {
1075 Lisp_Object val = func (arg);
1076 clobbered_eassert (handlerlist == c);
1077 handlerlist = handlerlist->next;
1078 return val;
1079 }
1080 else
1081 { /* Throw works by a longjmp that comes right here. */
1082 Lisp_Object val = handlerlist->val;
1083 clobbered_eassert (handlerlist == c);
1084 handlerlist = handlerlist->next;
1085 return val;
1086 }
1087 }
1088
1089 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1090 jump to that CATCH, returning VALUE as the value of that catch.
1091
1092 This is the guts of Fthrow and Fsignal; they differ only in the way
1093 they choose the catch tag to throw to. A catch tag for a
1094 condition-case form has a TAG of Qnil.
1095
1096 Before each catch is discarded, unbind all special bindings and
1097 execute all unwind-protect clauses made above that catch. Unwind
1098 the handler stack as we go, so that the proper handlers are in
1099 effect for each unwind-protect clause we run. At the end, restore
1100 some static info saved in CATCH, and longjmp to the location
1101 specified there.
1102
1103 This is used for correct unwinding in Fthrow and Fsignal. */
1104
1105 static _Noreturn void
1106 unwind_to_catch (struct handler *catch, Lisp_Object value)
1107 {
1108 bool last_time;
1109
1110 eassert (catch->next);
1111
1112 /* Save the value in the tag. */
1113 catch->val = value;
1114
1115 /* Restore certain special C variables. */
1116 set_poll_suppress_count (catch->poll_suppress_count);
1117 unblock_input_to (catch->interrupt_input_blocked);
1118 immediate_quit = 0;
1119
1120 do
1121 {
1122 /* Unwind the specpdl stack, and then restore the proper set of
1123 handlers. */
1124 unbind_to (handlerlist->pdlcount, Qnil);
1125 last_time = handlerlist == catch;
1126 if (! last_time)
1127 handlerlist = handlerlist->next;
1128 }
1129 while (! last_time);
1130
1131 eassert (handlerlist == catch);
1132
1133 byte_stack_list = catch->byte_stack;
1134 lisp_eval_depth = catch->lisp_eval_depth;
1135
1136 sys_longjmp (catch->jmp, 1);
1137 }
1138
1139 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1140 doc: /* Throw to the catch for TAG and return VALUE from it.
1141 Both TAG and VALUE are evalled. */
1142 attributes: noreturn)
1143 (register Lisp_Object tag, Lisp_Object value)
1144 {
1145 struct handler *c;
1146
1147 if (!NILP (tag))
1148 for (c = handlerlist; c; c = c->next)
1149 {
1150 if (c->type == CATCHER_ALL)
1151 unwind_to_catch (c, Fcons (tag, value));
1152 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1153 unwind_to_catch (c, value);
1154 }
1155 xsignal2 (Qno_catch, tag, value);
1156 }
1157
1158
1159 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1160 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1161 If BODYFORM completes normally, its value is returned
1162 after executing the UNWINDFORMS.
1163 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1164 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1165 (Lisp_Object args)
1166 {
1167 Lisp_Object val;
1168 ptrdiff_t count = SPECPDL_INDEX ();
1169
1170 record_unwind_protect (unwind_body, XCDR (args));
1171 val = eval_sub (XCAR (args));
1172 return unbind_to (count, val);
1173 }
1174 \f
1175 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1176 doc: /* Regain control when an error is signaled.
1177 Executes BODYFORM and returns its value if no error happens.
1178 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1179 where the BODY is made of Lisp expressions.
1180
1181 A handler is applicable to an error
1182 if CONDITION-NAME is one of the error's condition names.
1183 If an error happens, the first applicable handler is run.
1184
1185 The car of a handler may be a list of condition names instead of a
1186 single condition name; then it handles all of them. If the special
1187 condition name `debug' is present in this list, it allows another
1188 condition in the list to run the debugger if `debug-on-error' and the
1189 other usual mechanisms says it should (otherwise, `condition-case'
1190 suppresses the debugger).
1191
1192 When a handler handles an error, control returns to the `condition-case'
1193 and it executes the handler's BODY...
1194 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1195 \(If VAR is nil, the handler can't access that information.)
1196 Then the value of the last BODY form is returned from the `condition-case'
1197 expression.
1198
1199 See also the function `signal' for more info.
1200 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1201 (Lisp_Object args)
1202 {
1203 Lisp_Object var = XCAR (args);
1204 Lisp_Object bodyform = XCAR (XCDR (args));
1205 Lisp_Object handlers = XCDR (XCDR (args));
1206
1207 return internal_lisp_condition_case (var, bodyform, handlers);
1208 }
1209
1210 /* Like Fcondition_case, but the args are separate
1211 rather than passed in a list. Used by Fbyte_code. */
1212
1213 Lisp_Object
1214 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1215 Lisp_Object handlers)
1216 {
1217 Lisp_Object val;
1218 struct handler *oldhandlerlist = handlerlist;
1219 int clausenb = 0;
1220
1221 CHECK_SYMBOL (var);
1222
1223 for (val = handlers; CONSP (val); val = XCDR (val))
1224 {
1225 Lisp_Object tem = XCAR (val);
1226 clausenb++;
1227 if (! (NILP (tem)
1228 || (CONSP (tem)
1229 && (SYMBOLP (XCAR (tem))
1230 || CONSP (XCAR (tem))))))
1231 error ("Invalid condition handler: %s",
1232 SDATA (Fprin1_to_string (tem, Qt)));
1233 }
1234
1235 { /* The first clause is the one that should be checked first, so it should
1236 be added to handlerlist last. So we build in `clauses' a table that
1237 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1238 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1239 if (MAX_ALLOCA / word_size < clausenb)
1240 memory_full (SIZE_MAX);
1241 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1242 Lisp_Object *volatile clauses_volatile = clauses;
1243 int i = clausenb;
1244 for (val = handlers; CONSP (val); val = XCDR (val))
1245 clauses[--i] = XCAR (val);
1246 for (i = 0; i < clausenb; i++)
1247 {
1248 Lisp_Object clause = clauses[i];
1249 Lisp_Object condition = CONSP (clause) ? XCAR (clause) : Qnil;
1250 if (!CONSP (condition))
1251 condition = Fcons (condition, Qnil);
1252 struct handler *c = push_handler (condition, CONDITION_CASE);
1253 if (sys_setjmp (c->jmp))
1254 {
1255 ptrdiff_t count = SPECPDL_INDEX ();
1256 Lisp_Object val = handlerlist->val;
1257 Lisp_Object *chosen_clause = clauses_volatile;
1258 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1259 chosen_clause++;
1260 handlerlist = oldhandlerlist;
1261 if (!NILP (var))
1262 {
1263 if (!NILP (Vinternal_interpreter_environment))
1264 specbind (Qinternal_interpreter_environment,
1265 Fcons (Fcons (var, val),
1266 Vinternal_interpreter_environment));
1267 else
1268 specbind (var, val);
1269 }
1270 val = Fprogn (XCDR (*chosen_clause));
1271 /* Note that this just undoes the binding of var; whoever
1272 longjumped to us unwound the stack to c.pdlcount before
1273 throwing. */
1274 if (!NILP (var))
1275 unbind_to (count, Qnil);
1276 return val;
1277 }
1278 }
1279 }
1280
1281 val = eval_sub (bodyform);
1282 handlerlist = oldhandlerlist;
1283 return val;
1284 }
1285
1286 /* Call the function BFUN with no arguments, catching errors within it
1287 according to HANDLERS. If there is an error, call HFUN with
1288 one argument which is the data that describes the error:
1289 (SIGNALNAME . DATA)
1290
1291 HANDLERS can be a list of conditions to catch.
1292 If HANDLERS is Qt, catch all errors.
1293 If HANDLERS is Qerror, catch all errors
1294 but allow the debugger to run if that is enabled. */
1295
1296 Lisp_Object
1297 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1298 Lisp_Object (*hfun) (Lisp_Object))
1299 {
1300 struct handler *c = push_handler (handlers, CONDITION_CASE);
1301 if (sys_setjmp (c->jmp))
1302 {
1303 Lisp_Object val = handlerlist->val;
1304 clobbered_eassert (handlerlist == c);
1305 handlerlist = handlerlist->next;
1306 return hfun (val);
1307 }
1308 else
1309 {
1310 Lisp_Object val = bfun ();
1311 clobbered_eassert (handlerlist == c);
1312 handlerlist = handlerlist->next;
1313 return val;
1314 }
1315 }
1316
1317 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1318
1319 Lisp_Object
1320 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1321 Lisp_Object handlers,
1322 Lisp_Object (*hfun) (Lisp_Object))
1323 {
1324 struct handler *c = push_handler (handlers, CONDITION_CASE);
1325 if (sys_setjmp (c->jmp))
1326 {
1327 Lisp_Object val = handlerlist->val;
1328 clobbered_eassert (handlerlist == c);
1329 handlerlist = handlerlist->next;
1330 return hfun (val);
1331 }
1332 else
1333 {
1334 Lisp_Object val = bfun (arg);
1335 clobbered_eassert (handlerlist == c);
1336 handlerlist = handlerlist->next;
1337 return val;
1338 }
1339 }
1340
1341 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1342 its arguments. */
1343
1344 Lisp_Object
1345 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1346 Lisp_Object arg1,
1347 Lisp_Object arg2,
1348 Lisp_Object handlers,
1349 Lisp_Object (*hfun) (Lisp_Object))
1350 {
1351 struct handler *c = push_handler (handlers, CONDITION_CASE);
1352 if (sys_setjmp (c->jmp))
1353 {
1354 Lisp_Object val = handlerlist->val;
1355 clobbered_eassert (handlerlist == c);
1356 handlerlist = handlerlist->next;
1357 return hfun (val);
1358 }
1359 else
1360 {
1361 Lisp_Object val = bfun (arg1, arg2);
1362 clobbered_eassert (handlerlist == c);
1363 handlerlist = handlerlist->next;
1364 return val;
1365 }
1366 }
1367
1368 /* Like internal_condition_case but call BFUN with NARGS as first,
1369 and ARGS as second argument. */
1370
1371 Lisp_Object
1372 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1373 ptrdiff_t nargs,
1374 Lisp_Object *args,
1375 Lisp_Object handlers,
1376 Lisp_Object (*hfun) (Lisp_Object err,
1377 ptrdiff_t nargs,
1378 Lisp_Object *args))
1379 {
1380 struct handler *c = push_handler (handlers, CONDITION_CASE);
1381 if (sys_setjmp (c->jmp))
1382 {
1383 Lisp_Object val = handlerlist->val;
1384 clobbered_eassert (handlerlist == c);
1385 handlerlist = handlerlist->next;
1386 return hfun (val, nargs, args);
1387 }
1388 else
1389 {
1390 Lisp_Object val = bfun (nargs, args);
1391 clobbered_eassert (handlerlist == c);
1392 handlerlist = handlerlist->next;
1393 return val;
1394 }
1395 }
1396
1397 struct handler *
1398 push_handler (Lisp_Object tag_ch_val, enum handlertype handlertype)
1399 {
1400 struct handler *c = push_handler_nosignal (tag_ch_val, handlertype);
1401 if (!c)
1402 memory_full (sizeof *c);
1403 return c;
1404 }
1405
1406 struct handler *
1407 push_handler_nosignal (Lisp_Object tag_ch_val, enum handlertype handlertype)
1408 {
1409 struct handler *c = handlerlist->nextfree;
1410 if (!c)
1411 {
1412 c = malloc (sizeof *c);
1413 if (!c)
1414 return c;
1415 if (profiler_memory_running)
1416 malloc_probe (sizeof *c);
1417 c->nextfree = NULL;
1418 handlerlist->nextfree = c;
1419 }
1420 c->type = handlertype;
1421 c->tag_or_ch = tag_ch_val;
1422 c->val = Qnil;
1423 c->next = handlerlist;
1424 c->lisp_eval_depth = lisp_eval_depth;
1425 c->pdlcount = SPECPDL_INDEX ();
1426 c->poll_suppress_count = poll_suppress_count;
1427 c->interrupt_input_blocked = interrupt_input_blocked;
1428 c->byte_stack = byte_stack_list;
1429 handlerlist = c;
1430 return c;
1431 }
1432
1433 \f
1434 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1435 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1436 Lisp_Object data);
1437
1438 void
1439 process_quit_flag (void)
1440 {
1441 Lisp_Object flag = Vquit_flag;
1442 Vquit_flag = Qnil;
1443 if (EQ (flag, Qkill_emacs))
1444 Fkill_emacs (Qnil);
1445 if (EQ (Vthrow_on_input, flag))
1446 Fthrow (Vthrow_on_input, Qt);
1447 Fsignal (Qquit, Qnil);
1448 }
1449
1450 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1451 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1452 This function does not return.
1453
1454 An error symbol is a symbol with an `error-conditions' property
1455 that is a list of condition names.
1456 A handler for any of those names will get to handle this signal.
1457 The symbol `error' should normally be one of them.
1458
1459 DATA should be a list. Its elements are printed as part of the error message.
1460 See Info anchor `(elisp)Definition of signal' for some details on how this
1461 error message is constructed.
1462 If the signal is handled, DATA is made available to the handler.
1463 See also the function `condition-case'. */)
1464 (Lisp_Object error_symbol, Lisp_Object data)
1465 {
1466 /* When memory is full, ERROR-SYMBOL is nil,
1467 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1468 That is a special case--don't do this in other situations. */
1469 Lisp_Object conditions;
1470 Lisp_Object string;
1471 Lisp_Object real_error_symbol
1472 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1473 register Lisp_Object clause = Qnil;
1474 struct handler *h;
1475
1476 immediate_quit = 0;
1477 abort_on_gc = 0;
1478 if (gc_in_progress || waiting_for_input)
1479 emacs_abort ();
1480
1481 #if 0 /* rms: I don't know why this was here,
1482 but it is surely wrong for an error that is handled. */
1483 #ifdef HAVE_WINDOW_SYSTEM
1484 if (display_hourglass_p)
1485 cancel_hourglass ();
1486 #endif
1487 #endif
1488
1489 /* This hook is used by edebug. */
1490 if (! NILP (Vsignal_hook_function)
1491 && ! NILP (error_symbol))
1492 {
1493 /* Edebug takes care of restoring these variables when it exits. */
1494 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1495 max_lisp_eval_depth = lisp_eval_depth + 20;
1496
1497 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1498 max_specpdl_size = SPECPDL_INDEX () + 40;
1499
1500 call2 (Vsignal_hook_function, error_symbol, data);
1501 }
1502
1503 conditions = Fget (real_error_symbol, Qerror_conditions);
1504
1505 /* Remember from where signal was called. Skip over the frame for
1506 `signal' itself. If a frame for `error' follows, skip that,
1507 too. Don't do this when ERROR_SYMBOL is nil, because that
1508 is a memory-full error. */
1509 Vsignaling_function = Qnil;
1510 if (!NILP (error_symbol))
1511 {
1512 union specbinding *pdl = backtrace_next (backtrace_top ());
1513 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1514 pdl = backtrace_next (pdl);
1515 if (backtrace_p (pdl))
1516 Vsignaling_function = backtrace_function (pdl);
1517 }
1518
1519 for (h = handlerlist; h; h = h->next)
1520 {
1521 if (h->type != CONDITION_CASE)
1522 continue;
1523 clause = find_handler_clause (h->tag_or_ch, conditions);
1524 if (!NILP (clause))
1525 break;
1526 }
1527
1528 if (/* Don't run the debugger for a memory-full error.
1529 (There is no room in memory to do that!) */
1530 !NILP (error_symbol)
1531 && (!NILP (Vdebug_on_signal)
1532 /* If no handler is present now, try to run the debugger. */
1533 || NILP (clause)
1534 /* A `debug' symbol in the handler list disables the normal
1535 suppression of the debugger. */
1536 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1537 /* Special handler that means "print a message and run debugger
1538 if requested". */
1539 || EQ (h->tag_or_ch, Qerror)))
1540 {
1541 bool debugger_called
1542 = maybe_call_debugger (conditions, error_symbol, data);
1543 /* We can't return values to code which signaled an error, but we
1544 can continue code which has signaled a quit. */
1545 if (debugger_called && EQ (real_error_symbol, Qquit))
1546 return Qnil;
1547 }
1548
1549 if (!NILP (clause))
1550 {
1551 Lisp_Object unwind_data
1552 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1553
1554 unwind_to_catch (h, unwind_data);
1555 }
1556 else
1557 {
1558 if (handlerlist != &handlerlist_sentinel)
1559 /* FIXME: This will come right back here if there's no `top-level'
1560 catcher. A better solution would be to abort here, and instead
1561 add a catch-all condition handler so we never come here. */
1562 Fthrow (Qtop_level, Qt);
1563 }
1564
1565 if (! NILP (error_symbol))
1566 data = Fcons (error_symbol, data);
1567
1568 string = Ferror_message_string (data);
1569 fatal ("%s", SDATA (string));
1570 }
1571
1572 /* Internal version of Fsignal that never returns.
1573 Used for anything but Qquit (which can return from Fsignal). */
1574
1575 void
1576 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1577 {
1578 Fsignal (error_symbol, data);
1579 emacs_abort ();
1580 }
1581
1582 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1583
1584 void
1585 xsignal0 (Lisp_Object error_symbol)
1586 {
1587 xsignal (error_symbol, Qnil);
1588 }
1589
1590 void
1591 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1592 {
1593 xsignal (error_symbol, list1 (arg));
1594 }
1595
1596 void
1597 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1598 {
1599 xsignal (error_symbol, list2 (arg1, arg2));
1600 }
1601
1602 void
1603 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1604 {
1605 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1606 }
1607
1608 /* Signal `error' with message S, and additional arg ARG.
1609 If ARG is not a genuine list, make it a one-element list. */
1610
1611 void
1612 signal_error (const char *s, Lisp_Object arg)
1613 {
1614 Lisp_Object tortoise, hare;
1615
1616 hare = tortoise = arg;
1617 while (CONSP (hare))
1618 {
1619 hare = XCDR (hare);
1620 if (!CONSP (hare))
1621 break;
1622
1623 hare = XCDR (hare);
1624 tortoise = XCDR (tortoise);
1625
1626 if (EQ (hare, tortoise))
1627 break;
1628 }
1629
1630 if (!NILP (hare))
1631 arg = list1 (arg);
1632
1633 xsignal (Qerror, Fcons (build_string (s), arg));
1634 }
1635
1636
1637 /* Return true if LIST is a non-nil atom or
1638 a list containing one of CONDITIONS. */
1639
1640 static bool
1641 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1642 {
1643 if (NILP (list))
1644 return 0;
1645 if (! CONSP (list))
1646 return 1;
1647
1648 while (CONSP (conditions))
1649 {
1650 Lisp_Object this, tail;
1651 this = XCAR (conditions);
1652 for (tail = list; CONSP (tail); tail = XCDR (tail))
1653 if (EQ (XCAR (tail), this))
1654 return 1;
1655 conditions = XCDR (conditions);
1656 }
1657 return 0;
1658 }
1659
1660 /* Return true if an error with condition-symbols CONDITIONS,
1661 and described by SIGNAL-DATA, should skip the debugger
1662 according to debugger-ignored-errors. */
1663
1664 static bool
1665 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1666 {
1667 Lisp_Object tail;
1668 bool first_string = 1;
1669 Lisp_Object error_message;
1670
1671 error_message = Qnil;
1672 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1673 {
1674 if (STRINGP (XCAR (tail)))
1675 {
1676 if (first_string)
1677 {
1678 error_message = Ferror_message_string (data);
1679 first_string = 0;
1680 }
1681
1682 if (fast_string_match (XCAR (tail), error_message) >= 0)
1683 return 1;
1684 }
1685 else
1686 {
1687 Lisp_Object contail;
1688
1689 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1690 if (EQ (XCAR (tail), XCAR (contail)))
1691 return 1;
1692 }
1693 }
1694
1695 return 0;
1696 }
1697
1698 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1699 SIG and DATA describe the signal. There are two ways to pass them:
1700 = SIG is the error symbol, and DATA is the rest of the data.
1701 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1702 This is for memory-full errors only. */
1703 static bool
1704 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1705 {
1706 Lisp_Object combined_data;
1707
1708 combined_data = Fcons (sig, data);
1709
1710 if (
1711 /* Don't try to run the debugger with interrupts blocked.
1712 The editing loop would return anyway. */
1713 ! input_blocked_p ()
1714 && NILP (Vinhibit_debugger)
1715 /* Does user want to enter debugger for this kind of error? */
1716 && (EQ (sig, Qquit)
1717 ? debug_on_quit
1718 : wants_debugger (Vdebug_on_error, conditions))
1719 && ! skip_debugger (conditions, combined_data)
1720 /* RMS: What's this for? */
1721 && when_entered_debugger < num_nonmacro_input_events)
1722 {
1723 call_debugger (list2 (Qerror, combined_data));
1724 return 1;
1725 }
1726
1727 return 0;
1728 }
1729
1730 static Lisp_Object
1731 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1732 {
1733 register Lisp_Object h;
1734
1735 /* t is used by handlers for all conditions, set up by C code. */
1736 if (EQ (handlers, Qt))
1737 return Qt;
1738
1739 /* error is used similarly, but means print an error message
1740 and run the debugger if that is enabled. */
1741 if (EQ (handlers, Qerror))
1742 return Qt;
1743
1744 for (h = handlers; CONSP (h); h = XCDR (h))
1745 {
1746 Lisp_Object handler = XCAR (h);
1747 if (!NILP (Fmemq (handler, conditions)))
1748 return handlers;
1749 }
1750
1751 return Qnil;
1752 }
1753
1754
1755 /* Format and return a string; called like vprintf. */
1756 Lisp_Object
1757 vformat_string (const char *m, va_list ap)
1758 {
1759 char buf[4000];
1760 ptrdiff_t size = sizeof buf;
1761 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1762 char *buffer = buf;
1763 ptrdiff_t used;
1764 Lisp_Object string;
1765
1766 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1767 string = make_string (buffer, used);
1768 if (buffer != buf)
1769 xfree (buffer);
1770
1771 return string;
1772 }
1773
1774 /* Dump an error message; called like vprintf. */
1775 void
1776 verror (const char *m, va_list ap)
1777 {
1778 xsignal1 (Qerror, vformat_string (m, ap));
1779 }
1780
1781
1782 /* Dump an error message; called like printf. */
1783
1784 /* VARARGS 1 */
1785 void
1786 error (const char *m, ...)
1787 {
1788 va_list ap;
1789 va_start (ap, m);
1790 verror (m, ap);
1791 }
1792 \f
1793 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1794 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1795 This means it contains a description for how to read arguments to give it.
1796 The value is nil for an invalid function or a symbol with no function
1797 definition.
1798
1799 Interactively callable functions include strings and vectors (treated
1800 as keyboard macros), lambda-expressions that contain a top-level call
1801 to `interactive', autoload definitions made by `autoload' with non-nil
1802 fourth argument, and some of the built-in functions of Lisp.
1803
1804 Also, a symbol satisfies `commandp' if its function definition does so.
1805
1806 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1807 then strings and vectors are not accepted. */)
1808 (Lisp_Object function, Lisp_Object for_call_interactively)
1809 {
1810 register Lisp_Object fun;
1811 register Lisp_Object funcar;
1812 Lisp_Object if_prop = Qnil;
1813
1814 fun = function;
1815
1816 fun = indirect_function (fun); /* Check cycles. */
1817 if (NILP (fun))
1818 return Qnil;
1819
1820 /* Check an `interactive-form' property if present, analogous to the
1821 function-documentation property. */
1822 fun = function;
1823 while (SYMBOLP (fun))
1824 {
1825 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1826 if (!NILP (tmp))
1827 if_prop = Qt;
1828 fun = Fsymbol_function (fun);
1829 }
1830
1831 /* Emacs primitives are interactive if their DEFUN specifies an
1832 interactive spec. */
1833 if (SUBRP (fun))
1834 return XSUBR (fun)->intspec ? Qt : if_prop;
1835
1836 /* Bytecode objects are interactive if they are long enough to
1837 have an element whose index is COMPILED_INTERACTIVE, which is
1838 where the interactive spec is stored. */
1839 else if (COMPILEDP (fun))
1840 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1841 ? Qt : if_prop);
1842
1843 /* Strings and vectors are keyboard macros. */
1844 if (STRINGP (fun) || VECTORP (fun))
1845 return (NILP (for_call_interactively) ? Qt : Qnil);
1846
1847 /* Lists may represent commands. */
1848 if (!CONSP (fun))
1849 return Qnil;
1850 funcar = XCAR (fun);
1851 if (EQ (funcar, Qclosure))
1852 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1853 ? Qt : if_prop);
1854 else if (EQ (funcar, Qlambda))
1855 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1856 else if (EQ (funcar, Qautoload))
1857 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1858 else
1859 return Qnil;
1860 }
1861
1862 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1863 doc: /* Define FUNCTION to autoload from FILE.
1864 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1865 Third arg DOCSTRING is documentation for the function.
1866 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1867 Fifth arg TYPE indicates the type of the object:
1868 nil or omitted says FUNCTION is a function,
1869 `keymap' says FUNCTION is really a keymap, and
1870 `macro' or t says FUNCTION is really a macro.
1871 Third through fifth args give info about the real definition.
1872 They default to nil.
1873 If FUNCTION is already defined other than as an autoload,
1874 this does nothing and returns nil. */)
1875 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1876 {
1877 CHECK_SYMBOL (function);
1878 CHECK_STRING (file);
1879
1880 /* If function is defined and not as an autoload, don't override. */
1881 if (!NILP (XSYMBOL (function)->function)
1882 && !AUTOLOADP (XSYMBOL (function)->function))
1883 return Qnil;
1884
1885 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1886 /* `read1' in lread.c has found the docstring starting with "\
1887 and assumed the docstring will be provided by Snarf-documentation, so it
1888 passed us 0 instead. But that leads to accidental sharing in purecopy's
1889 hash-consing, so we use a (hopefully) unique integer instead. */
1890 docstring = make_number (XHASH (function));
1891 return Fdefalias (function,
1892 list5 (Qautoload, file, docstring, interactive, type),
1893 Qnil);
1894 }
1895
1896 void
1897 un_autoload (Lisp_Object oldqueue)
1898 {
1899 Lisp_Object queue, first, second;
1900
1901 /* Queue to unwind is current value of Vautoload_queue.
1902 oldqueue is the shadowed value to leave in Vautoload_queue. */
1903 queue = Vautoload_queue;
1904 Vautoload_queue = oldqueue;
1905 while (CONSP (queue))
1906 {
1907 first = XCAR (queue);
1908 second = Fcdr (first);
1909 first = Fcar (first);
1910 if (EQ (first, make_number (0)))
1911 Vfeatures = second;
1912 else
1913 Ffset (first, second);
1914 queue = XCDR (queue);
1915 }
1916 }
1917
1918 /* Load an autoloaded function.
1919 FUNNAME is the symbol which is the function's name.
1920 FUNDEF is the autoload definition (a list). */
1921
1922 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1923 doc: /* Load FUNDEF which should be an autoload.
1924 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1925 in which case the function returns the new autoloaded function value.
1926 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1927 it defines a macro. */)
1928 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1929 {
1930 ptrdiff_t count = SPECPDL_INDEX ();
1931
1932 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1933 return fundef;
1934
1935 if (EQ (macro_only, Qmacro))
1936 {
1937 Lisp_Object kind = Fnth (make_number (4), fundef);
1938 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1939 return fundef;
1940 }
1941
1942 /* This is to make sure that loadup.el gives a clear picture
1943 of what files are preloaded and when. */
1944 if (! NILP (Vpurify_flag))
1945 error ("Attempt to autoload %s while preparing to dump",
1946 SDATA (SYMBOL_NAME (funname)));
1947
1948 CHECK_SYMBOL (funname);
1949
1950 /* Preserve the match data. */
1951 record_unwind_save_match_data ();
1952
1953 /* If autoloading gets an error (which includes the error of failing
1954 to define the function being called), we use Vautoload_queue
1955 to undo function definitions and `provide' calls made by
1956 the function. We do this in the specific case of autoloading
1957 because autoloading is not an explicit request "load this file",
1958 but rather a request to "call this function".
1959
1960 The value saved here is to be restored into Vautoload_queue. */
1961 record_unwind_protect (un_autoload, Vautoload_queue);
1962 Vautoload_queue = Qt;
1963 /* If `macro_only', assume this autoload to be a "best-effort",
1964 so don't signal an error if autoloading fails. */
1965 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1966
1967 /* Once loading finishes, don't undo it. */
1968 Vautoload_queue = Qt;
1969 unbind_to (count, Qnil);
1970
1971 if (NILP (funname))
1972 return Qnil;
1973 else
1974 {
1975 Lisp_Object fun = Findirect_function (funname, Qnil);
1976
1977 if (!NILP (Fequal (fun, fundef)))
1978 error ("Autoloading failed to define function %s",
1979 SDATA (SYMBOL_NAME (funname)));
1980 else
1981 return fun;
1982 }
1983 }
1984
1985 \f
1986 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1987 doc: /* Evaluate FORM and return its value.
1988 If LEXICAL is t, evaluate using lexical scoping.
1989 LEXICAL can also be an actual lexical environment, in the form of an
1990 alist mapping symbols to their value. */)
1991 (Lisp_Object form, Lisp_Object lexical)
1992 {
1993 ptrdiff_t count = SPECPDL_INDEX ();
1994 specbind (Qinternal_interpreter_environment,
1995 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1996 return unbind_to (count, eval_sub (form));
1997 }
1998
1999 /* Grow the specpdl stack by one entry.
2000 The caller should have already initialized the entry.
2001 Signal an error on stack overflow.
2002
2003 Make sure that there is always one unused entry past the top of the
2004 stack, so that the just-initialized entry is safely unwound if
2005 memory exhausted and an error is signaled here. Also, allocate a
2006 never-used entry just before the bottom of the stack; sometimes its
2007 address is taken. */
2008
2009 static void
2010 grow_specpdl (void)
2011 {
2012 specpdl_ptr++;
2013
2014 if (specpdl_ptr == specpdl + specpdl_size)
2015 {
2016 ptrdiff_t count = SPECPDL_INDEX ();
2017 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2018 union specbinding *pdlvec = specpdl - 1;
2019 ptrdiff_t pdlvecsize = specpdl_size + 1;
2020 if (max_size <= specpdl_size)
2021 {
2022 if (max_specpdl_size < 400)
2023 max_size = max_specpdl_size = 400;
2024 if (max_size <= specpdl_size)
2025 signal_error ("Variable binding depth exceeds max-specpdl-size",
2026 Qnil);
2027 }
2028 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2029 specpdl = pdlvec + 1;
2030 specpdl_size = pdlvecsize - 1;
2031 specpdl_ptr = specpdl + count;
2032 }
2033 }
2034
2035 ptrdiff_t
2036 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2037 {
2038 ptrdiff_t count = SPECPDL_INDEX ();
2039
2040 eassert (nargs >= UNEVALLED);
2041 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2042 specpdl_ptr->bt.debug_on_exit = false;
2043 specpdl_ptr->bt.function = function;
2044 specpdl_ptr->bt.args = args;
2045 specpdl_ptr->bt.nargs = nargs;
2046 grow_specpdl ();
2047
2048 return count;
2049 }
2050
2051 /* Eval a sub-expression of the current expression (i.e. in the same
2052 lexical scope). */
2053 Lisp_Object
2054 eval_sub (Lisp_Object form)
2055 {
2056 Lisp_Object fun, val, original_fun, original_args;
2057 Lisp_Object funcar;
2058 ptrdiff_t count;
2059
2060 /* Declare here, as this array may be accessed by call_debugger near
2061 the end of this function. See Bug#21245. */
2062 Lisp_Object argvals[8];
2063
2064 if (SYMBOLP (form))
2065 {
2066 /* Look up its binding in the lexical environment.
2067 We do not pay attention to the declared_special flag here, since we
2068 already did that when let-binding the variable. */
2069 Lisp_Object lex_binding
2070 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2071 ? Fassq (form, Vinternal_interpreter_environment)
2072 : Qnil;
2073 if (CONSP (lex_binding))
2074 return XCDR (lex_binding);
2075 else
2076 return Fsymbol_value (form);
2077 }
2078
2079 if (!CONSP (form))
2080 return form;
2081
2082 QUIT;
2083
2084 maybe_gc ();
2085
2086 if (++lisp_eval_depth > max_lisp_eval_depth)
2087 {
2088 if (max_lisp_eval_depth < 100)
2089 max_lisp_eval_depth = 100;
2090 if (lisp_eval_depth > max_lisp_eval_depth)
2091 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2092 }
2093
2094 original_fun = XCAR (form);
2095 original_args = XCDR (form);
2096
2097 /* This also protects them from gc. */
2098 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2099
2100 if (debug_on_next_call)
2101 do_debug_on_call (Qt, count);
2102
2103 /* At this point, only original_fun and original_args
2104 have values that will be used below. */
2105 retry:
2106
2107 /* Optimize for no indirection. */
2108 fun = original_fun;
2109 if (!SYMBOLP (fun))
2110 fun = Ffunction (Fcons (fun, Qnil));
2111 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2112 fun = indirect_function (fun);
2113
2114 if (SUBRP (fun))
2115 {
2116 Lisp_Object args_left = original_args;
2117 Lisp_Object numargs = Flength (args_left);
2118
2119 check_cons_list ();
2120
2121 if (XINT (numargs) < XSUBR (fun)->min_args
2122 || (XSUBR (fun)->max_args >= 0
2123 && XSUBR (fun)->max_args < XINT (numargs)))
2124 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2125
2126 else if (XSUBR (fun)->max_args == UNEVALLED)
2127 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2128 else if (XSUBR (fun)->max_args == MANY)
2129 {
2130 /* Pass a vector of evaluated arguments. */
2131 Lisp_Object *vals;
2132 ptrdiff_t argnum = 0;
2133 USE_SAFE_ALLOCA;
2134
2135 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2136
2137 while (!NILP (args_left))
2138 {
2139 vals[argnum++] = eval_sub (Fcar (args_left));
2140 args_left = Fcdr (args_left);
2141 }
2142
2143 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2144
2145 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2146
2147 check_cons_list ();
2148 lisp_eval_depth--;
2149 /* Do the debug-on-exit now, while VALS still exists. */
2150 if (backtrace_debug_on_exit (specpdl + count))
2151 val = call_debugger (list2 (Qexit, val));
2152 SAFE_FREE ();
2153 specpdl_ptr--;
2154 return val;
2155 }
2156 else
2157 {
2158 int i, maxargs = XSUBR (fun)->max_args;
2159
2160 for (i = 0; i < maxargs; i++)
2161 {
2162 argvals[i] = eval_sub (Fcar (args_left));
2163 args_left = Fcdr (args_left);
2164 }
2165
2166 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2167
2168 switch (i)
2169 {
2170 case 0:
2171 val = (XSUBR (fun)->function.a0 ());
2172 break;
2173 case 1:
2174 val = (XSUBR (fun)->function.a1 (argvals[0]));
2175 break;
2176 case 2:
2177 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2178 break;
2179 case 3:
2180 val = (XSUBR (fun)->function.a3
2181 (argvals[0], argvals[1], argvals[2]));
2182 break;
2183 case 4:
2184 val = (XSUBR (fun)->function.a4
2185 (argvals[0], argvals[1], argvals[2], argvals[3]));
2186 break;
2187 case 5:
2188 val = (XSUBR (fun)->function.a5
2189 (argvals[0], argvals[1], argvals[2], argvals[3],
2190 argvals[4]));
2191 break;
2192 case 6:
2193 val = (XSUBR (fun)->function.a6
2194 (argvals[0], argvals[1], argvals[2], argvals[3],
2195 argvals[4], argvals[5]));
2196 break;
2197 case 7:
2198 val = (XSUBR (fun)->function.a7
2199 (argvals[0], argvals[1], argvals[2], argvals[3],
2200 argvals[4], argvals[5], argvals[6]));
2201 break;
2202
2203 case 8:
2204 val = (XSUBR (fun)->function.a8
2205 (argvals[0], argvals[1], argvals[2], argvals[3],
2206 argvals[4], argvals[5], argvals[6], argvals[7]));
2207 break;
2208
2209 default:
2210 /* Someone has created a subr that takes more arguments than
2211 is supported by this code. We need to either rewrite the
2212 subr to use a different argument protocol, or add more
2213 cases to this switch. */
2214 emacs_abort ();
2215 }
2216 }
2217 }
2218 else if (COMPILEDP (fun))
2219 return apply_lambda (fun, original_args, count);
2220 else
2221 {
2222 if (NILP (fun))
2223 xsignal1 (Qvoid_function, original_fun);
2224 if (!CONSP (fun))
2225 xsignal1 (Qinvalid_function, original_fun);
2226 funcar = XCAR (fun);
2227 if (!SYMBOLP (funcar))
2228 xsignal1 (Qinvalid_function, original_fun);
2229 if (EQ (funcar, Qautoload))
2230 {
2231 Fautoload_do_load (fun, original_fun, Qnil);
2232 goto retry;
2233 }
2234 if (EQ (funcar, Qmacro))
2235 {
2236 ptrdiff_t count1 = SPECPDL_INDEX ();
2237 Lisp_Object exp;
2238 /* Bind lexical-binding during expansion of the macro, so the
2239 macro can know reliably if the code it outputs will be
2240 interpreted using lexical-binding or not. */
2241 specbind (Qlexical_binding,
2242 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2243 exp = apply1 (Fcdr (fun), original_args);
2244 unbind_to (count1, Qnil);
2245 val = eval_sub (exp);
2246 }
2247 else if (EQ (funcar, Qlambda)
2248 || EQ (funcar, Qclosure))
2249 return apply_lambda (fun, original_args, count);
2250 else
2251 xsignal1 (Qinvalid_function, original_fun);
2252 }
2253 check_cons_list ();
2254
2255 lisp_eval_depth--;
2256 if (backtrace_debug_on_exit (specpdl + count))
2257 val = call_debugger (list2 (Qexit, val));
2258 specpdl_ptr--;
2259
2260 return val;
2261 }
2262 \f
2263 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2264 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2265 Then return the value FUNCTION returns.
2266 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2267 usage: (apply FUNCTION &rest ARGUMENTS) */)
2268 (ptrdiff_t nargs, Lisp_Object *args)
2269 {
2270 ptrdiff_t i, numargs, funcall_nargs;
2271 register Lisp_Object *funcall_args = NULL;
2272 register Lisp_Object spread_arg = args[nargs - 1];
2273 Lisp_Object fun = args[0];
2274 Lisp_Object retval;
2275 USE_SAFE_ALLOCA;
2276
2277 CHECK_LIST (spread_arg);
2278
2279 numargs = XINT (Flength (spread_arg));
2280
2281 if (numargs == 0)
2282 return Ffuncall (nargs - 1, args);
2283 else if (numargs == 1)
2284 {
2285 args [nargs - 1] = XCAR (spread_arg);
2286 return Ffuncall (nargs, args);
2287 }
2288
2289 numargs += nargs - 2;
2290
2291 /* Optimize for no indirection. */
2292 if (SYMBOLP (fun) && !NILP (fun)
2293 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2294 {
2295 fun = indirect_function (fun);
2296 if (NILP (fun))
2297 /* Let funcall get the error. */
2298 fun = args[0];
2299 }
2300
2301 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2302 /* Don't hide an error by adding missing arguments. */
2303 && numargs >= XSUBR (fun)->min_args)
2304 {
2305 /* Avoid making funcall cons up a yet another new vector of arguments
2306 by explicitly supplying nil's for optional values. */
2307 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2308 memclear (funcall_args + numargs + 1,
2309 (XSUBR (fun)->max_args - numargs) * word_size);
2310 funcall_nargs = 1 + XSUBR (fun)->max_args;
2311 }
2312 else
2313 { /* We add 1 to numargs because funcall_args includes the
2314 function itself as well as its arguments. */
2315 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2316 funcall_nargs = 1 + numargs;
2317 }
2318
2319 memcpy (funcall_args, args, nargs * word_size);
2320 /* Spread the last arg we got. Its first element goes in
2321 the slot that it used to occupy, hence this value of I. */
2322 i = nargs - 1;
2323 while (!NILP (spread_arg))
2324 {
2325 funcall_args [i++] = XCAR (spread_arg);
2326 spread_arg = XCDR (spread_arg);
2327 }
2328
2329 retval = Ffuncall (funcall_nargs, funcall_args);
2330
2331 SAFE_FREE ();
2332 return retval;
2333 }
2334 \f
2335 /* Run hook variables in various ways. */
2336
2337 static Lisp_Object
2338 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2339 {
2340 Ffuncall (nargs, args);
2341 return Qnil;
2342 }
2343
2344 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2345 doc: /* Run each hook in HOOKS.
2346 Each argument should be a symbol, a hook variable.
2347 These symbols are processed in the order specified.
2348 If a hook symbol has a non-nil value, that value may be a function
2349 or a list of functions to be called to run the hook.
2350 If the value is a function, it is called with no arguments.
2351 If it is a list, the elements are called, in order, with no arguments.
2352
2353 Major modes should not use this function directly to run their mode
2354 hook; they should use `run-mode-hooks' instead.
2355
2356 Do not use `make-local-variable' to make a hook variable buffer-local.
2357 Instead, use `add-hook' and specify t for the LOCAL argument.
2358 usage: (run-hooks &rest HOOKS) */)
2359 (ptrdiff_t nargs, Lisp_Object *args)
2360 {
2361 ptrdiff_t i;
2362
2363 for (i = 0; i < nargs; i++)
2364 run_hook (args[i]);
2365
2366 return Qnil;
2367 }
2368
2369 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2370 Srun_hook_with_args, 1, MANY, 0,
2371 doc: /* Run HOOK with the specified arguments ARGS.
2372 HOOK should be a symbol, a hook variable. The value of HOOK
2373 may be nil, a function, or a list of functions. Call each
2374 function in order with arguments ARGS. The final return value
2375 is unspecified.
2376
2377 Do not use `make-local-variable' to make a hook variable buffer-local.
2378 Instead, use `add-hook' and specify t for the LOCAL argument.
2379 usage: (run-hook-with-args HOOK &rest ARGS) */)
2380 (ptrdiff_t nargs, Lisp_Object *args)
2381 {
2382 return run_hook_with_args (nargs, args, funcall_nil);
2383 }
2384
2385 /* NB this one still documents a specific non-nil return value.
2386 (As did run-hook-with-args and run-hook-with-args-until-failure
2387 until they were changed in 24.1.) */
2388 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2389 Srun_hook_with_args_until_success, 1, MANY, 0,
2390 doc: /* Run HOOK with the specified arguments ARGS.
2391 HOOK should be a symbol, a hook variable. The value of HOOK
2392 may be nil, a function, or a list of functions. Call each
2393 function in order with arguments ARGS, stopping at the first
2394 one that returns non-nil, and return that value. Otherwise (if
2395 all functions return nil, or if there are no functions to call),
2396 return nil.
2397
2398 Do not use `make-local-variable' to make a hook variable buffer-local.
2399 Instead, use `add-hook' and specify t for the LOCAL argument.
2400 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2401 (ptrdiff_t nargs, Lisp_Object *args)
2402 {
2403 return run_hook_with_args (nargs, args, Ffuncall);
2404 }
2405
2406 static Lisp_Object
2407 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2408 {
2409 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2410 }
2411
2412 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2413 Srun_hook_with_args_until_failure, 1, MANY, 0,
2414 doc: /* Run HOOK with the specified arguments ARGS.
2415 HOOK should be a symbol, a hook variable. The value of HOOK
2416 may be nil, a function, or a list of functions. Call each
2417 function in order with arguments ARGS, stopping at the first
2418 one that returns nil, and return nil. Otherwise (if all functions
2419 return non-nil, or if there are no functions to call), return non-nil
2420 \(do not rely on the precise return value in this case).
2421
2422 Do not use `make-local-variable' to make a hook variable buffer-local.
2423 Instead, use `add-hook' and specify t for the LOCAL argument.
2424 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2425 (ptrdiff_t nargs, Lisp_Object *args)
2426 {
2427 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2428 }
2429
2430 static Lisp_Object
2431 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2432 {
2433 Lisp_Object tmp = args[0], ret;
2434 args[0] = args[1];
2435 args[1] = tmp;
2436 ret = Ffuncall (nargs, args);
2437 args[1] = args[0];
2438 args[0] = tmp;
2439 return ret;
2440 }
2441
2442 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2443 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2444 I.e. instead of calling each function FUN directly with arguments ARGS,
2445 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2446 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2447 aborts and returns that value.
2448 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2449 (ptrdiff_t nargs, Lisp_Object *args)
2450 {
2451 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2452 }
2453
2454 /* ARGS[0] should be a hook symbol.
2455 Call each of the functions in the hook value, passing each of them
2456 as arguments all the rest of ARGS (all NARGS - 1 elements).
2457 FUNCALL specifies how to call each function on the hook. */
2458
2459 Lisp_Object
2460 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2461 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2462 {
2463 Lisp_Object sym, val, ret = Qnil;
2464
2465 /* If we are dying or still initializing,
2466 don't do anything--it would probably crash if we tried. */
2467 if (NILP (Vrun_hooks))
2468 return Qnil;
2469
2470 sym = args[0];
2471 val = find_symbol_value (sym);
2472
2473 if (EQ (val, Qunbound) || NILP (val))
2474 return ret;
2475 else if (!CONSP (val) || FUNCTIONP (val))
2476 {
2477 args[0] = val;
2478 return funcall (nargs, args);
2479 }
2480 else
2481 {
2482 Lisp_Object global_vals = Qnil;
2483
2484 for (;
2485 CONSP (val) && NILP (ret);
2486 val = XCDR (val))
2487 {
2488 if (EQ (XCAR (val), Qt))
2489 {
2490 /* t indicates this hook has a local binding;
2491 it means to run the global binding too. */
2492 global_vals = Fdefault_value (sym);
2493 if (NILP (global_vals)) continue;
2494
2495 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2496 {
2497 args[0] = global_vals;
2498 ret = funcall (nargs, args);
2499 }
2500 else
2501 {
2502 for (;
2503 CONSP (global_vals) && NILP (ret);
2504 global_vals = XCDR (global_vals))
2505 {
2506 args[0] = XCAR (global_vals);
2507 /* In a global value, t should not occur. If it does, we
2508 must ignore it to avoid an endless loop. */
2509 if (!EQ (args[0], Qt))
2510 ret = funcall (nargs, args);
2511 }
2512 }
2513 }
2514 else
2515 {
2516 args[0] = XCAR (val);
2517 ret = funcall (nargs, args);
2518 }
2519 }
2520
2521 return ret;
2522 }
2523 }
2524
2525 /* Run the hook HOOK, giving each function no args. */
2526
2527 void
2528 run_hook (Lisp_Object hook)
2529 {
2530 Frun_hook_with_args (1, &hook);
2531 }
2532
2533 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2534
2535 void
2536 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2537 {
2538 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2539 }
2540
2541 /* Apply fn to arg. */
2542 Lisp_Object
2543 apply1 (Lisp_Object fn, Lisp_Object arg)
2544 {
2545 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2546 }
2547
2548 /* Call function fn on no arguments. */
2549 Lisp_Object
2550 call0 (Lisp_Object fn)
2551 {
2552 return Ffuncall (1, &fn);
2553 }
2554
2555 /* Call function fn with 1 argument arg1. */
2556 /* ARGSUSED */
2557 Lisp_Object
2558 call1 (Lisp_Object fn, Lisp_Object arg1)
2559 {
2560 return CALLN (Ffuncall, fn, arg1);
2561 }
2562
2563 /* Call function fn with 2 arguments arg1, arg2. */
2564 /* ARGSUSED */
2565 Lisp_Object
2566 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2567 {
2568 return CALLN (Ffuncall, fn, arg1, arg2);
2569 }
2570
2571 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2572 /* ARGSUSED */
2573 Lisp_Object
2574 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2575 {
2576 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2577 }
2578
2579 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2580 /* ARGSUSED */
2581 Lisp_Object
2582 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2583 Lisp_Object arg4)
2584 {
2585 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2586 }
2587
2588 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2589 /* ARGSUSED */
2590 Lisp_Object
2591 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2592 Lisp_Object arg4, Lisp_Object arg5)
2593 {
2594 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2595 }
2596
2597 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2598 /* ARGSUSED */
2599 Lisp_Object
2600 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2601 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2602 {
2603 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2604 }
2605
2606 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2607 /* ARGSUSED */
2608 Lisp_Object
2609 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2610 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2611 {
2612 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2613 }
2614
2615 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2616 doc: /* Non-nil if OBJECT is a function. */)
2617 (Lisp_Object object)
2618 {
2619 if (FUNCTIONP (object))
2620 return Qt;
2621 return Qnil;
2622 }
2623
2624 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2625 doc: /* Call first argument as a function, passing remaining arguments to it.
2626 Return the value that function returns.
2627 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2628 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2629 (ptrdiff_t nargs, Lisp_Object *args)
2630 {
2631 Lisp_Object fun, original_fun;
2632 Lisp_Object funcar;
2633 ptrdiff_t numargs = nargs - 1;
2634 Lisp_Object lisp_numargs;
2635 Lisp_Object val;
2636 Lisp_Object *internal_args;
2637 ptrdiff_t count;
2638
2639 QUIT;
2640
2641 if (++lisp_eval_depth > max_lisp_eval_depth)
2642 {
2643 if (max_lisp_eval_depth < 100)
2644 max_lisp_eval_depth = 100;
2645 if (lisp_eval_depth > max_lisp_eval_depth)
2646 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2647 }
2648
2649 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2650
2651 maybe_gc ();
2652
2653 if (debug_on_next_call)
2654 do_debug_on_call (Qlambda, count);
2655
2656 check_cons_list ();
2657
2658 original_fun = args[0];
2659
2660 retry:
2661
2662 /* Optimize for no indirection. */
2663 fun = original_fun;
2664 if (SYMBOLP (fun) && !NILP (fun)
2665 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2666 fun = indirect_function (fun);
2667
2668 if (SUBRP (fun))
2669 {
2670 if (numargs < XSUBR (fun)->min_args
2671 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2672 {
2673 XSETFASTINT (lisp_numargs, numargs);
2674 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2675 }
2676
2677 else if (XSUBR (fun)->max_args == UNEVALLED)
2678 xsignal1 (Qinvalid_function, original_fun);
2679
2680 else if (XSUBR (fun)->max_args == MANY)
2681 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2682 else
2683 {
2684 Lisp_Object internal_argbuf[8];
2685 if (XSUBR (fun)->max_args > numargs)
2686 {
2687 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2688 internal_args = internal_argbuf;
2689 memcpy (internal_args, args + 1, numargs * word_size);
2690 memclear (internal_args + numargs,
2691 (XSUBR (fun)->max_args - numargs) * word_size);
2692 }
2693 else
2694 internal_args = args + 1;
2695 switch (XSUBR (fun)->max_args)
2696 {
2697 case 0:
2698 val = (XSUBR (fun)->function.a0 ());
2699 break;
2700 case 1:
2701 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2702 break;
2703 case 2:
2704 val = (XSUBR (fun)->function.a2
2705 (internal_args[0], internal_args[1]));
2706 break;
2707 case 3:
2708 val = (XSUBR (fun)->function.a3
2709 (internal_args[0], internal_args[1], internal_args[2]));
2710 break;
2711 case 4:
2712 val = (XSUBR (fun)->function.a4
2713 (internal_args[0], internal_args[1], internal_args[2],
2714 internal_args[3]));
2715 break;
2716 case 5:
2717 val = (XSUBR (fun)->function.a5
2718 (internal_args[0], internal_args[1], internal_args[2],
2719 internal_args[3], internal_args[4]));
2720 break;
2721 case 6:
2722 val = (XSUBR (fun)->function.a6
2723 (internal_args[0], internal_args[1], internal_args[2],
2724 internal_args[3], internal_args[4], internal_args[5]));
2725 break;
2726 case 7:
2727 val = (XSUBR (fun)->function.a7
2728 (internal_args[0], internal_args[1], internal_args[2],
2729 internal_args[3], internal_args[4], internal_args[5],
2730 internal_args[6]));
2731 break;
2732
2733 case 8:
2734 val = (XSUBR (fun)->function.a8
2735 (internal_args[0], internal_args[1], internal_args[2],
2736 internal_args[3], internal_args[4], internal_args[5],
2737 internal_args[6], internal_args[7]));
2738 break;
2739
2740 default:
2741
2742 /* If a subr takes more than 8 arguments without using MANY
2743 or UNEVALLED, we need to extend this function to support it.
2744 Until this is done, there is no way to call the function. */
2745 emacs_abort ();
2746 }
2747 }
2748 }
2749 else if (COMPILEDP (fun))
2750 val = funcall_lambda (fun, numargs, args + 1);
2751 else
2752 {
2753 if (NILP (fun))
2754 xsignal1 (Qvoid_function, original_fun);
2755 if (!CONSP (fun))
2756 xsignal1 (Qinvalid_function, original_fun);
2757 funcar = XCAR (fun);
2758 if (!SYMBOLP (funcar))
2759 xsignal1 (Qinvalid_function, original_fun);
2760 if (EQ (funcar, Qlambda)
2761 || EQ (funcar, Qclosure))
2762 val = funcall_lambda (fun, numargs, args + 1);
2763 else if (EQ (funcar, Qautoload))
2764 {
2765 Fautoload_do_load (fun, original_fun, Qnil);
2766 check_cons_list ();
2767 goto retry;
2768 }
2769 else
2770 xsignal1 (Qinvalid_function, original_fun);
2771 }
2772 check_cons_list ();
2773 lisp_eval_depth--;
2774 if (backtrace_debug_on_exit (specpdl + count))
2775 val = call_debugger (list2 (Qexit, val));
2776 specpdl_ptr--;
2777 return val;
2778 }
2779 \f
2780 static Lisp_Object
2781 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2782 {
2783 Lisp_Object args_left;
2784 ptrdiff_t i;
2785 EMACS_INT numargs;
2786 Lisp_Object *arg_vector;
2787 Lisp_Object tem;
2788 USE_SAFE_ALLOCA;
2789
2790 numargs = XFASTINT (Flength (args));
2791 SAFE_ALLOCA_LISP (arg_vector, numargs);
2792 args_left = args;
2793
2794 for (i = 0; i < numargs; )
2795 {
2796 tem = Fcar (args_left), args_left = Fcdr (args_left);
2797 tem = eval_sub (tem);
2798 arg_vector[i++] = tem;
2799 }
2800
2801 set_backtrace_args (specpdl + count, arg_vector, i);
2802 tem = funcall_lambda (fun, numargs, arg_vector);
2803
2804 check_cons_list ();
2805 lisp_eval_depth--;
2806 /* Do the debug-on-exit now, while arg_vector still exists. */
2807 if (backtrace_debug_on_exit (specpdl + count))
2808 tem = call_debugger (list2 (Qexit, tem));
2809 SAFE_FREE ();
2810 specpdl_ptr--;
2811 return tem;
2812 }
2813
2814 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2815 and return the result of evaluation.
2816 FUN must be either a lambda-expression or a compiled-code object. */
2817
2818 static Lisp_Object
2819 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2820 register Lisp_Object *arg_vector)
2821 {
2822 Lisp_Object val, syms_left, next, lexenv;
2823 ptrdiff_t count = SPECPDL_INDEX ();
2824 ptrdiff_t i;
2825 bool optional, rest;
2826
2827 if (CONSP (fun))
2828 {
2829 if (EQ (XCAR (fun), Qclosure))
2830 {
2831 fun = XCDR (fun); /* Drop `closure'. */
2832 lexenv = XCAR (fun);
2833 CHECK_LIST_CONS (fun, fun);
2834 }
2835 else
2836 lexenv = Qnil;
2837 syms_left = XCDR (fun);
2838 if (CONSP (syms_left))
2839 syms_left = XCAR (syms_left);
2840 else
2841 xsignal1 (Qinvalid_function, fun);
2842 }
2843 else if (COMPILEDP (fun))
2844 {
2845 ptrdiff_t size = ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK;
2846 if (size <= COMPILED_STACK_DEPTH)
2847 xsignal1 (Qinvalid_function, fun);
2848 syms_left = AREF (fun, COMPILED_ARGLIST);
2849 if (INTEGERP (syms_left))
2850 /* A byte-code object with a non-nil `push args' slot means we
2851 shouldn't bind any arguments, instead just call the byte-code
2852 interpreter directly; it will push arguments as necessary.
2853
2854 Byte-code objects with either a non-existent, or a nil value for
2855 the `push args' slot (the default), have dynamically-bound
2856 arguments, and use the argument-binding code below instead (as do
2857 all interpreted functions, even lexically bound ones). */
2858 {
2859 /* If we have not actually read the bytecode string
2860 and constants vector yet, fetch them from the file. */
2861 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2862 Ffetch_bytecode (fun);
2863 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2864 AREF (fun, COMPILED_CONSTANTS),
2865 AREF (fun, COMPILED_STACK_DEPTH),
2866 syms_left,
2867 nargs, arg_vector);
2868 }
2869 lexenv = Qnil;
2870 }
2871 else
2872 emacs_abort ();
2873
2874 i = optional = rest = 0;
2875 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2876 {
2877 QUIT;
2878
2879 next = XCAR (syms_left);
2880 if (!SYMBOLP (next))
2881 xsignal1 (Qinvalid_function, fun);
2882
2883 if (EQ (next, Qand_rest))
2884 rest = 1;
2885 else if (EQ (next, Qand_optional))
2886 optional = 1;
2887 else
2888 {
2889 Lisp_Object arg;
2890 if (rest)
2891 {
2892 arg = Flist (nargs - i, &arg_vector[i]);
2893 i = nargs;
2894 }
2895 else if (i < nargs)
2896 arg = arg_vector[i++];
2897 else if (!optional)
2898 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2899 else
2900 arg = Qnil;
2901
2902 /* Bind the argument. */
2903 if (!NILP (lexenv) && SYMBOLP (next))
2904 /* Lexically bind NEXT by adding it to the lexenv alist. */
2905 lexenv = Fcons (Fcons (next, arg), lexenv);
2906 else
2907 /* Dynamically bind NEXT. */
2908 specbind (next, arg);
2909 }
2910 }
2911
2912 if (!NILP (syms_left))
2913 xsignal1 (Qinvalid_function, fun);
2914 else if (i < nargs)
2915 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2916
2917 if (!EQ (lexenv, Vinternal_interpreter_environment))
2918 /* Instantiate a new lexical environment. */
2919 specbind (Qinternal_interpreter_environment, lexenv);
2920
2921 if (CONSP (fun))
2922 val = Fprogn (XCDR (XCDR (fun)));
2923 else
2924 {
2925 /* If we have not actually read the bytecode string
2926 and constants vector yet, fetch them from the file. */
2927 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2928 Ffetch_bytecode (fun);
2929 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2930 AREF (fun, COMPILED_CONSTANTS),
2931 AREF (fun, COMPILED_STACK_DEPTH),
2932 Qnil, 0, 0);
2933 }
2934
2935 return unbind_to (count, val);
2936 }
2937
2938 DEFUN ("func-arity", Ffunc_arity, Sfunc_arity, 1, 1, 0,
2939 doc: /* Return minimum and maximum number of args allowed for FUNCTION.
2940 FUNCTION must be a function of some kind.
2941 The returned value is a cons cell (MIN . MAX). MIN is the minimum number
2942 of args. MAX is the maximum number, or the symbol `many', for a
2943 function with `&rest' args, or `unevalled' for a special form. */)
2944 (Lisp_Object function)
2945 {
2946 Lisp_Object original;
2947 Lisp_Object funcar;
2948 Lisp_Object result;
2949
2950 original = function;
2951
2952 retry:
2953
2954 /* Optimize for no indirection. */
2955 function = original;
2956 if (SYMBOLP (function) && !NILP (function))
2957 {
2958 function = XSYMBOL (function)->function;
2959 if (SYMBOLP (function))
2960 function = indirect_function (function);
2961 }
2962
2963 if (CONSP (function) && EQ (XCAR (function), Qmacro))
2964 function = XCDR (function);
2965
2966 if (SUBRP (function))
2967 result = Fsubr_arity (function);
2968 else if (COMPILEDP (function))
2969 result = lambda_arity (function);
2970 else
2971 {
2972 if (NILP (function))
2973 xsignal1 (Qvoid_function, original);
2974 if (!CONSP (function))
2975 xsignal1 (Qinvalid_function, original);
2976 funcar = XCAR (function);
2977 if (!SYMBOLP (funcar))
2978 xsignal1 (Qinvalid_function, original);
2979 if (EQ (funcar, Qlambda)
2980 || EQ (funcar, Qclosure))
2981 result = lambda_arity (function);
2982 else if (EQ (funcar, Qautoload))
2983 {
2984 Fautoload_do_load (function, original, Qnil);
2985 goto retry;
2986 }
2987 else
2988 xsignal1 (Qinvalid_function, original);
2989 }
2990 return result;
2991 }
2992
2993 /* FUN must be either a lambda-expression or a compiled-code object. */
2994 static Lisp_Object
2995 lambda_arity (Lisp_Object fun)
2996 {
2997 Lisp_Object syms_left;
2998
2999 if (CONSP (fun))
3000 {
3001 if (EQ (XCAR (fun), Qclosure))
3002 {
3003 fun = XCDR (fun); /* Drop `closure'. */
3004 CHECK_LIST_CONS (fun, fun);
3005 }
3006 syms_left = XCDR (fun);
3007 if (CONSP (syms_left))
3008 syms_left = XCAR (syms_left);
3009 else
3010 xsignal1 (Qinvalid_function, fun);
3011 }
3012 else if (COMPILEDP (fun))
3013 {
3014 ptrdiff_t size = ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK;
3015 if (size <= COMPILED_STACK_DEPTH)
3016 xsignal1 (Qinvalid_function, fun);
3017 syms_left = AREF (fun, COMPILED_ARGLIST);
3018 if (INTEGERP (syms_left))
3019 return get_byte_code_arity (syms_left);
3020 }
3021 else
3022 emacs_abort ();
3023
3024 EMACS_INT minargs = 0, maxargs = 0;
3025 bool optional = false;
3026 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3027 {
3028 Lisp_Object next = XCAR (syms_left);
3029 if (!SYMBOLP (next))
3030 xsignal1 (Qinvalid_function, fun);
3031
3032 if (EQ (next, Qand_rest))
3033 return Fcons (make_number (minargs), Qmany);
3034 else if (EQ (next, Qand_optional))
3035 optional = true;
3036 else
3037 {
3038 if (!optional)
3039 minargs++;
3040 maxargs++;
3041 }
3042 }
3043
3044 if (!NILP (syms_left))
3045 xsignal1 (Qinvalid_function, fun);
3046
3047 return Fcons (make_number (minargs), make_number (maxargs));
3048 }
3049
3050 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3051 1, 1, 0,
3052 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3053 (Lisp_Object object)
3054 {
3055 Lisp_Object tem;
3056
3057 if (COMPILEDP (object))
3058 {
3059 ptrdiff_t size = ASIZE (object) & PSEUDOVECTOR_SIZE_MASK;
3060 if (size <= COMPILED_STACK_DEPTH)
3061 xsignal1 (Qinvalid_function, object);
3062 if (CONSP (AREF (object, COMPILED_BYTECODE)))
3063 {
3064 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3065 if (!CONSP (tem))
3066 {
3067 tem = AREF (object, COMPILED_BYTECODE);
3068 if (CONSP (tem) && STRINGP (XCAR (tem)))
3069 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3070 else
3071 error ("Invalid byte code");
3072 }
3073 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3074 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3075 }
3076 }
3077 return object;
3078 }
3079 \f
3080 /* Return true if SYMBOL currently has a let-binding
3081 which was made in the buffer that is now current. */
3082
3083 bool
3084 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3085 {
3086 union specbinding *p;
3087 Lisp_Object buf = Fcurrent_buffer ();
3088
3089 for (p = specpdl_ptr; p > specpdl; )
3090 if ((--p)->kind > SPECPDL_LET)
3091 {
3092 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3093 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3094 if (symbol == let_bound_symbol
3095 && EQ (specpdl_where (p), buf))
3096 return 1;
3097 }
3098
3099 return 0;
3100 }
3101
3102 bool
3103 let_shadows_global_binding_p (Lisp_Object symbol)
3104 {
3105 union specbinding *p;
3106
3107 for (p = specpdl_ptr; p > specpdl; )
3108 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3109 return 1;
3110
3111 return 0;
3112 }
3113
3114 /* `specpdl_ptr' describes which variable is
3115 let-bound, so it can be properly undone when we unbind_to.
3116 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3117 - SYMBOL is the variable being bound. Note that it should not be
3118 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3119 to record V2 here).
3120 - WHERE tells us in which buffer the binding took place.
3121 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3122 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3123 i.e. bindings to the default value of a variable which can be
3124 buffer-local. */
3125
3126 void
3127 specbind (Lisp_Object symbol, Lisp_Object value)
3128 {
3129 struct Lisp_Symbol *sym;
3130
3131 CHECK_SYMBOL (symbol);
3132 sym = XSYMBOL (symbol);
3133
3134 start:
3135 switch (sym->redirect)
3136 {
3137 case SYMBOL_VARALIAS:
3138 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3139 case SYMBOL_PLAINVAL:
3140 /* The most common case is that of a non-constant symbol with a
3141 trivial value. Make that as fast as we can. */
3142 specpdl_ptr->let.kind = SPECPDL_LET;
3143 specpdl_ptr->let.symbol = symbol;
3144 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3145 grow_specpdl ();
3146 if (!sym->constant)
3147 SET_SYMBOL_VAL (sym, value);
3148 else
3149 set_internal (symbol, value, Qnil, 1);
3150 break;
3151 case SYMBOL_LOCALIZED:
3152 if (SYMBOL_BLV (sym)->frame_local)
3153 error ("Frame-local vars cannot be let-bound");
3154 case SYMBOL_FORWARDED:
3155 {
3156 Lisp_Object ovalue = find_symbol_value (symbol);
3157 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3158 specpdl_ptr->let.symbol = symbol;
3159 specpdl_ptr->let.old_value = ovalue;
3160 specpdl_ptr->let.where = Fcurrent_buffer ();
3161
3162 eassert (sym->redirect != SYMBOL_LOCALIZED
3163 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3164
3165 if (sym->redirect == SYMBOL_LOCALIZED)
3166 {
3167 if (!blv_found (SYMBOL_BLV (sym)))
3168 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3169 }
3170 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3171 {
3172 /* If SYMBOL is a per-buffer variable which doesn't have a
3173 buffer-local value here, make the `let' change the global
3174 value by changing the value of SYMBOL in all buffers not
3175 having their own value. This is consistent with what
3176 happens with other buffer-local variables. */
3177 if (NILP (Flocal_variable_p (symbol, Qnil)))
3178 {
3179 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3180 grow_specpdl ();
3181 Fset_default (symbol, value);
3182 return;
3183 }
3184 }
3185 else
3186 specpdl_ptr->let.kind = SPECPDL_LET;
3187
3188 grow_specpdl ();
3189 set_internal (symbol, value, Qnil, 1);
3190 break;
3191 }
3192 default: emacs_abort ();
3193 }
3194 }
3195
3196 /* Push unwind-protect entries of various types. */
3197
3198 void
3199 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3200 {
3201 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3202 specpdl_ptr->unwind.func = function;
3203 specpdl_ptr->unwind.arg = arg;
3204 grow_specpdl ();
3205 }
3206
3207 void
3208 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3209 {
3210 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3211 specpdl_ptr->unwind_ptr.func = function;
3212 specpdl_ptr->unwind_ptr.arg = arg;
3213 grow_specpdl ();
3214 }
3215
3216 void
3217 record_unwind_protect_int (void (*function) (int), int arg)
3218 {
3219 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3220 specpdl_ptr->unwind_int.func = function;
3221 specpdl_ptr->unwind_int.arg = arg;
3222 grow_specpdl ();
3223 }
3224
3225 void
3226 record_unwind_protect_void (void (*function) (void))
3227 {
3228 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3229 specpdl_ptr->unwind_void.func = function;
3230 grow_specpdl ();
3231 }
3232
3233 static void
3234 do_nothing (void)
3235 {}
3236
3237 /* Push an unwind-protect entry that does nothing, so that
3238 set_unwind_protect_ptr can overwrite it later. */
3239
3240 void
3241 record_unwind_protect_nothing (void)
3242 {
3243 record_unwind_protect_void (do_nothing);
3244 }
3245
3246 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3247 It need not be at the top of the stack. */
3248
3249 void
3250 clear_unwind_protect (ptrdiff_t count)
3251 {
3252 union specbinding *p = specpdl + count;
3253 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3254 p->unwind_void.func = do_nothing;
3255 }
3256
3257 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3258 It need not be at the top of the stack. Discard the entry's
3259 previous value without invoking it. */
3260
3261 void
3262 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3263 Lisp_Object arg)
3264 {
3265 union specbinding *p = specpdl + count;
3266 p->unwind.kind = SPECPDL_UNWIND;
3267 p->unwind.func = func;
3268 p->unwind.arg = arg;
3269 }
3270
3271 void
3272 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3273 {
3274 union specbinding *p = specpdl + count;
3275 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3276 p->unwind_ptr.func = func;
3277 p->unwind_ptr.arg = arg;
3278 }
3279
3280 /* Pop and execute entries from the unwind-protect stack until the
3281 depth COUNT is reached. Return VALUE. */
3282
3283 Lisp_Object
3284 unbind_to (ptrdiff_t count, Lisp_Object value)
3285 {
3286 Lisp_Object quitf = Vquit_flag;
3287
3288 Vquit_flag = Qnil;
3289
3290 while (specpdl_ptr != specpdl + count)
3291 {
3292 /* Decrement specpdl_ptr before we do the work to unbind it, so
3293 that an error in unbinding won't try to unbind the same entry
3294 again. Take care to copy any parts of the binding needed
3295 before invoking any code that can make more bindings. */
3296
3297 specpdl_ptr--;
3298
3299 switch (specpdl_ptr->kind)
3300 {
3301 case SPECPDL_UNWIND:
3302 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3303 break;
3304 case SPECPDL_UNWIND_PTR:
3305 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3306 break;
3307 case SPECPDL_UNWIND_INT:
3308 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3309 break;
3310 case SPECPDL_UNWIND_VOID:
3311 specpdl_ptr->unwind_void.func ();
3312 break;
3313 case SPECPDL_BACKTRACE:
3314 break;
3315 case SPECPDL_LET:
3316 { /* If variable has a trivial value (no forwarding), we can
3317 just set it. No need to check for constant symbols here,
3318 since that was already done by specbind. */
3319 Lisp_Object sym = specpdl_symbol (specpdl_ptr);
3320 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3321 {
3322 SET_SYMBOL_VAL (XSYMBOL (sym),
3323 specpdl_old_value (specpdl_ptr));
3324 break;
3325 }
3326 else
3327 { /* FALLTHROUGH!!
3328 NOTE: we only ever come here if make_local_foo was used for
3329 the first time on this var within this let. */
3330 }
3331 }
3332 case SPECPDL_LET_DEFAULT:
3333 Fset_default (specpdl_symbol (specpdl_ptr),
3334 specpdl_old_value (specpdl_ptr));
3335 break;
3336 case SPECPDL_LET_LOCAL:
3337 {
3338 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3339 Lisp_Object where = specpdl_where (specpdl_ptr);
3340 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3341 eassert (BUFFERP (where));
3342
3343 /* If this was a local binding, reset the value in the appropriate
3344 buffer, but only if that buffer's binding still exists. */
3345 if (!NILP (Flocal_variable_p (symbol, where)))
3346 set_internal (symbol, old_value, where, 1);
3347 }
3348 break;
3349 }
3350 }
3351
3352 if (NILP (Vquit_flag) && !NILP (quitf))
3353 Vquit_flag = quitf;
3354
3355 return value;
3356 }
3357
3358 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3359 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3360 A special variable is one that will be bound dynamically, even in a
3361 context where binding is lexical by default. */)
3362 (Lisp_Object symbol)
3363 {
3364 CHECK_SYMBOL (symbol);
3365 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3366 }
3367
3368 \f
3369 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3370 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3371 The debugger is entered when that frame exits, if the flag is non-nil. */)
3372 (Lisp_Object level, Lisp_Object flag)
3373 {
3374 union specbinding *pdl = backtrace_top ();
3375 register EMACS_INT i;
3376
3377 CHECK_NUMBER (level);
3378
3379 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3380 pdl = backtrace_next (pdl);
3381
3382 if (backtrace_p (pdl))
3383 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3384
3385 return flag;
3386 }
3387
3388 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3389 doc: /* Print a trace of Lisp function calls currently active.
3390 Output stream used is value of `standard-output'. */)
3391 (void)
3392 {
3393 union specbinding *pdl = backtrace_top ();
3394 Lisp_Object tem;
3395 Lisp_Object old_print_level = Vprint_level;
3396
3397 if (NILP (Vprint_level))
3398 XSETFASTINT (Vprint_level, 8);
3399
3400 while (backtrace_p (pdl))
3401 {
3402 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3403 if (backtrace_nargs (pdl) == UNEVALLED)
3404 {
3405 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3406 Qnil);
3407 write_string ("\n");
3408 }
3409 else
3410 {
3411 tem = backtrace_function (pdl);
3412 Fprin1 (tem, Qnil); /* This can QUIT. */
3413 write_string ("(");
3414 {
3415 ptrdiff_t i;
3416 for (i = 0; i < backtrace_nargs (pdl); i++)
3417 {
3418 if (i) write_string (" ");
3419 Fprin1 (backtrace_args (pdl)[i], Qnil);
3420 }
3421 }
3422 write_string (")\n");
3423 }
3424 pdl = backtrace_next (pdl);
3425 }
3426
3427 Vprint_level = old_print_level;
3428 return Qnil;
3429 }
3430
3431 static union specbinding *
3432 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3433 {
3434 union specbinding *pdl = backtrace_top ();
3435 register EMACS_INT i;
3436
3437 CHECK_NATNUM (nframes);
3438
3439 if (!NILP (base))
3440 { /* Skip up to `base'. */
3441 base = Findirect_function (base, Qt);
3442 while (backtrace_p (pdl)
3443 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3444 pdl = backtrace_next (pdl);
3445 }
3446
3447 /* Find the frame requested. */
3448 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3449 pdl = backtrace_next (pdl);
3450
3451 return pdl;
3452 }
3453
3454 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3455 doc: /* Return the function and arguments NFRAMES up from current execution point.
3456 If that frame has not evaluated the arguments yet (or is a special form),
3457 the value is (nil FUNCTION ARG-FORMS...).
3458 If that frame has evaluated its arguments and called its function already,
3459 the value is (t FUNCTION ARG-VALUES...).
3460 A &rest arg is represented as the tail of the list ARG-VALUES.
3461 FUNCTION is whatever was supplied as car of evaluated list,
3462 or a lambda expression for macro calls.
3463 If NFRAMES is more than the number of frames, the value is nil.
3464 If BASE is non-nil, it should be a function and NFRAMES counts from its
3465 nearest activation frame. */)
3466 (Lisp_Object nframes, Lisp_Object base)
3467 {
3468 union specbinding *pdl = get_backtrace_frame (nframes, base);
3469
3470 if (!backtrace_p (pdl))
3471 return Qnil;
3472 if (backtrace_nargs (pdl) == UNEVALLED)
3473 return Fcons (Qnil,
3474 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3475 else
3476 {
3477 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3478
3479 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3480 }
3481 }
3482
3483 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3484 the specpdl stack, and then rewind them. We store the pre-unwind values
3485 directly in the pre-existing specpdl elements (i.e. we swap the current
3486 value and the old value stored in the specpdl), kind of like the inplace
3487 pointer-reversal trick. As it turns out, the rewind does the same as the
3488 unwind, except it starts from the other end of the specpdl stack, so we use
3489 the same function for both unwind and rewind. */
3490 static void
3491 backtrace_eval_unrewind (int distance)
3492 {
3493 union specbinding *tmp = specpdl_ptr;
3494 int step = -1;
3495 if (distance < 0)
3496 { /* It's a rewind rather than unwind. */
3497 tmp += distance - 1;
3498 step = 1;
3499 distance = -distance;
3500 }
3501
3502 for (; distance > 0; distance--)
3503 {
3504 tmp += step;
3505 switch (tmp->kind)
3506 {
3507 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3508 unwind_protect, but the problem is that we don't know how to
3509 rewind them afterwards. */
3510 case SPECPDL_UNWIND:
3511 {
3512 Lisp_Object oldarg = tmp->unwind.arg;
3513 if (tmp->unwind.func == set_buffer_if_live)
3514 tmp->unwind.arg = Fcurrent_buffer ();
3515 else if (tmp->unwind.func == save_excursion_restore)
3516 tmp->unwind.arg = save_excursion_save ();
3517 else
3518 break;
3519 tmp->unwind.func (oldarg);
3520 break;
3521 }
3522
3523 case SPECPDL_UNWIND_PTR:
3524 case SPECPDL_UNWIND_INT:
3525 case SPECPDL_UNWIND_VOID:
3526 case SPECPDL_BACKTRACE:
3527 break;
3528 case SPECPDL_LET:
3529 { /* If variable has a trivial value (no forwarding), we can
3530 just set it. No need to check for constant symbols here,
3531 since that was already done by specbind. */
3532 Lisp_Object sym = specpdl_symbol (tmp);
3533 if (SYMBOLP (sym) && XSYMBOL (sym)->redirect == SYMBOL_PLAINVAL)
3534 {
3535 Lisp_Object old_value = specpdl_old_value (tmp);
3536 set_specpdl_old_value (tmp, SYMBOL_VAL (XSYMBOL (sym)));
3537 SET_SYMBOL_VAL (XSYMBOL (sym), old_value);
3538 break;
3539 }
3540 else
3541 { /* FALLTHROUGH!!
3542 NOTE: we only ever come here if make_local_foo was used for
3543 the first time on this var within this let. */
3544 }
3545 }
3546 case SPECPDL_LET_DEFAULT:
3547 {
3548 Lisp_Object sym = specpdl_symbol (tmp);
3549 Lisp_Object old_value = specpdl_old_value (tmp);
3550 set_specpdl_old_value (tmp, Fdefault_value (sym));
3551 Fset_default (sym, old_value);
3552 }
3553 break;
3554 case SPECPDL_LET_LOCAL:
3555 {
3556 Lisp_Object symbol = specpdl_symbol (tmp);
3557 Lisp_Object where = specpdl_where (tmp);
3558 Lisp_Object old_value = specpdl_old_value (tmp);
3559 eassert (BUFFERP (where));
3560
3561 /* If this was a local binding, reset the value in the appropriate
3562 buffer, but only if that buffer's binding still exists. */
3563 if (!NILP (Flocal_variable_p (symbol, where)))
3564 {
3565 set_specpdl_old_value
3566 (tmp, Fbuffer_local_value (symbol, where));
3567 set_internal (symbol, old_value, where, 1);
3568 }
3569 }
3570 break;
3571 }
3572 }
3573 }
3574
3575 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3576 doc: /* Evaluate EXP in the context of some activation frame.
3577 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3578 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3579 {
3580 union specbinding *pdl = get_backtrace_frame (nframes, base);
3581 ptrdiff_t count = SPECPDL_INDEX ();
3582 ptrdiff_t distance = specpdl_ptr - pdl;
3583 eassert (distance >= 0);
3584
3585 if (!backtrace_p (pdl))
3586 error ("Activation frame not found!");
3587
3588 backtrace_eval_unrewind (distance);
3589 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3590
3591 /* Use eval_sub rather than Feval since the main motivation behind
3592 backtrace-eval is to be able to get/set the value of lexical variables
3593 from the debugger. */
3594 return unbind_to (count, eval_sub (exp));
3595 }
3596
3597 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3598 doc: /* Return names and values of local variables of a stack frame.
3599 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3600 (Lisp_Object nframes, Lisp_Object base)
3601 {
3602 union specbinding *frame = get_backtrace_frame (nframes, base);
3603 union specbinding *prevframe
3604 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3605 ptrdiff_t distance = specpdl_ptr - frame;
3606 Lisp_Object result = Qnil;
3607 eassert (distance >= 0);
3608
3609 if (!backtrace_p (prevframe))
3610 error ("Activation frame not found!");
3611 if (!backtrace_p (frame))
3612 error ("Activation frame not found!");
3613
3614 /* The specpdl entries normally contain the symbol being bound along with its
3615 `old_value', so it can be restored. The new value to which it is bound is
3616 available in one of two places: either in the current value of the
3617 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3618 next specpdl entry for it.
3619 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3620 and "new value", so we abuse it here, to fetch the new value.
3621 It's ugly (we'd rather not modify global data) and a bit inefficient,
3622 but it does the job for now. */
3623 backtrace_eval_unrewind (distance);
3624
3625 /* Grab values. */
3626 {
3627 union specbinding *tmp = prevframe;
3628 for (; tmp > frame; tmp--)
3629 {
3630 switch (tmp->kind)
3631 {
3632 case SPECPDL_LET:
3633 case SPECPDL_LET_DEFAULT:
3634 case SPECPDL_LET_LOCAL:
3635 {
3636 Lisp_Object sym = specpdl_symbol (tmp);
3637 Lisp_Object val = specpdl_old_value (tmp);
3638 if (EQ (sym, Qinternal_interpreter_environment))
3639 {
3640 Lisp_Object env = val;
3641 for (; CONSP (env); env = XCDR (env))
3642 {
3643 Lisp_Object binding = XCAR (env);
3644 if (CONSP (binding))
3645 result = Fcons (Fcons (XCAR (binding),
3646 XCDR (binding)),
3647 result);
3648 }
3649 }
3650 else
3651 result = Fcons (Fcons (sym, val), result);
3652 }
3653 break;
3654
3655 case SPECPDL_UNWIND:
3656 case SPECPDL_UNWIND_PTR:
3657 case SPECPDL_UNWIND_INT:
3658 case SPECPDL_UNWIND_VOID:
3659 case SPECPDL_BACKTRACE:
3660 break;
3661
3662 default:
3663 emacs_abort ();
3664 }
3665 }
3666 }
3667
3668 /* Restore values from specpdl to original place. */
3669 backtrace_eval_unrewind (-distance);
3670
3671 return result;
3672 }
3673
3674 \f
3675 void
3676 mark_specpdl (void)
3677 {
3678 union specbinding *pdl;
3679 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3680 {
3681 switch (pdl->kind)
3682 {
3683 case SPECPDL_UNWIND:
3684 mark_object (specpdl_arg (pdl));
3685 break;
3686
3687 case SPECPDL_BACKTRACE:
3688 {
3689 ptrdiff_t nargs = backtrace_nargs (pdl);
3690 mark_object (backtrace_function (pdl));
3691 if (nargs == UNEVALLED)
3692 nargs = 1;
3693 while (nargs--)
3694 mark_object (backtrace_args (pdl)[nargs]);
3695 }
3696 break;
3697
3698 case SPECPDL_LET_DEFAULT:
3699 case SPECPDL_LET_LOCAL:
3700 mark_object (specpdl_where (pdl));
3701 /* Fall through. */
3702 case SPECPDL_LET:
3703 mark_object (specpdl_symbol (pdl));
3704 mark_object (specpdl_old_value (pdl));
3705 break;
3706
3707 case SPECPDL_UNWIND_PTR:
3708 case SPECPDL_UNWIND_INT:
3709 case SPECPDL_UNWIND_VOID:
3710 break;
3711
3712 default:
3713 emacs_abort ();
3714 }
3715 }
3716 }
3717
3718 void
3719 get_backtrace (Lisp_Object array)
3720 {
3721 union specbinding *pdl = backtrace_next (backtrace_top ());
3722 ptrdiff_t i = 0, asize = ASIZE (array);
3723
3724 /* Copy the backtrace contents into working memory. */
3725 for (; i < asize; i++)
3726 {
3727 if (backtrace_p (pdl))
3728 {
3729 ASET (array, i, backtrace_function (pdl));
3730 pdl = backtrace_next (pdl);
3731 }
3732 else
3733 ASET (array, i, Qnil);
3734 }
3735 }
3736
3737 Lisp_Object backtrace_top_function (void)
3738 {
3739 union specbinding *pdl = backtrace_top ();
3740 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3741 }
3742
3743 void
3744 syms_of_eval (void)
3745 {
3746 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3747 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3748 If Lisp code tries to increase the total number past this amount,
3749 an error is signaled.
3750 You can safely use a value considerably larger than the default value,
3751 if that proves inconveniently small. However, if you increase it too far,
3752 Emacs could run out of memory trying to make the stack bigger.
3753 Note that this limit may be silently increased by the debugger
3754 if `debug-on-error' or `debug-on-quit' is set. */);
3755
3756 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3757 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3758
3759 This limit serves to catch infinite recursions for you before they cause
3760 actual stack overflow in C, which would be fatal for Emacs.
3761 You can safely make it considerably larger than its default value,
3762 if that proves inconveniently small. However, if you increase it too far,
3763 Emacs could overflow the real C stack, and crash. */);
3764
3765 DEFVAR_LISP ("quit-flag", Vquit_flag,
3766 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3767 If the value is t, that means do an ordinary quit.
3768 If the value equals `throw-on-input', that means quit by throwing
3769 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3770 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3771 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3772 Vquit_flag = Qnil;
3773
3774 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3775 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3776 Note that `quit-flag' will still be set by typing C-g,
3777 so a quit will be signaled as soon as `inhibit-quit' is nil.
3778 To prevent this happening, set `quit-flag' to nil
3779 before making `inhibit-quit' nil. */);
3780 Vinhibit_quit = Qnil;
3781
3782 DEFSYM (Qsetq, "setq");
3783 DEFSYM (Qinhibit_quit, "inhibit-quit");
3784 DEFSYM (Qautoload, "autoload");
3785 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3786 DEFSYM (Qmacro, "macro");
3787
3788 /* Note that the process handling also uses Qexit, but we don't want
3789 to staticpro it twice, so we just do it here. */
3790 DEFSYM (Qexit, "exit");
3791
3792 DEFSYM (Qinteractive, "interactive");
3793 DEFSYM (Qcommandp, "commandp");
3794 DEFSYM (Qand_rest, "&rest");
3795 DEFSYM (Qand_optional, "&optional");
3796 DEFSYM (Qclosure, "closure");
3797 DEFSYM (QCdocumentation, ":documentation");
3798 DEFSYM (Qdebug, "debug");
3799
3800 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3801 doc: /* Non-nil means never enter the debugger.
3802 Normally set while the debugger is already active, to avoid recursive
3803 invocations. */);
3804 Vinhibit_debugger = Qnil;
3805
3806 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3807 doc: /* Non-nil means enter debugger if an error is signaled.
3808 Does not apply to errors handled by `condition-case' or those
3809 matched by `debug-ignored-errors'.
3810 If the value is a list, an error only means to enter the debugger
3811 if one of its condition symbols appears in the list.
3812 When you evaluate an expression interactively, this variable
3813 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3814 The command `toggle-debug-on-error' toggles this.
3815 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3816 Vdebug_on_error = Qnil;
3817
3818 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3819 doc: /* List of errors for which the debugger should not be called.
3820 Each element may be a condition-name or a regexp that matches error messages.
3821 If any element applies to a given error, that error skips the debugger
3822 and just returns to top level.
3823 This overrides the variable `debug-on-error'.
3824 It does not apply to errors handled by `condition-case'. */);
3825 Vdebug_ignored_errors = Qnil;
3826
3827 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3828 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3829 Does not apply if quit is handled by a `condition-case'. */);
3830 debug_on_quit = 0;
3831
3832 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3833 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3834
3835 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3836 doc: /* Non-nil means debugger may continue execution.
3837 This is nil when the debugger is called under circumstances where it
3838 might not be safe to continue. */);
3839 debugger_may_continue = 1;
3840
3841 DEFVAR_LISP ("debugger", Vdebugger,
3842 doc: /* Function to call to invoke debugger.
3843 If due to frame exit, args are `exit' and the value being returned;
3844 this function's value will be returned instead of that.
3845 If due to error, args are `error' and a list of the args to `signal'.
3846 If due to `apply' or `funcall' entry, one arg, `lambda'.
3847 If due to `eval' entry, one arg, t. */);
3848 Vdebugger = Qnil;
3849
3850 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3851 doc: /* If non-nil, this is a function for `signal' to call.
3852 It receives the same arguments that `signal' was given.
3853 The Edebug package uses this to regain control. */);
3854 Vsignal_hook_function = Qnil;
3855
3856 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3857 doc: /* Non-nil means call the debugger regardless of condition handlers.
3858 Note that `debug-on-error', `debug-on-quit' and friends
3859 still determine whether to handle the particular condition. */);
3860 Vdebug_on_signal = Qnil;
3861
3862 /* When lexical binding is being used,
3863 Vinternal_interpreter_environment is non-nil, and contains an alist
3864 of lexically-bound variable, or (t), indicating an empty
3865 environment. The lisp name of this variable would be
3866 `internal-interpreter-environment' if it weren't hidden.
3867 Every element of this list can be either a cons (VAR . VAL)
3868 specifying a lexical binding, or a single symbol VAR indicating
3869 that this variable should use dynamic scoping. */
3870 DEFSYM (Qinternal_interpreter_environment,
3871 "internal-interpreter-environment");
3872 DEFVAR_LISP ("internal-interpreter-environment",
3873 Vinternal_interpreter_environment,
3874 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3875 When lexical binding is not being used, this variable is nil.
3876 A value of `(t)' indicates an empty environment, otherwise it is an
3877 alist of active lexical bindings. */);
3878 Vinternal_interpreter_environment = Qnil;
3879 /* Don't export this variable to Elisp, so no one can mess with it
3880 (Just imagine if someone makes it buffer-local). */
3881 Funintern (Qinternal_interpreter_environment, Qnil);
3882
3883 Vrun_hooks = intern_c_string ("run-hooks");
3884 staticpro (&Vrun_hooks);
3885
3886 staticpro (&Vautoload_queue);
3887 Vautoload_queue = Qnil;
3888 staticpro (&Vsignaling_function);
3889 Vsignaling_function = Qnil;
3890
3891 inhibit_lisp_code = Qnil;
3892
3893 defsubr (&Sor);
3894 defsubr (&Sand);
3895 defsubr (&Sif);
3896 defsubr (&Scond);
3897 defsubr (&Sprogn);
3898 defsubr (&Sprog1);
3899 defsubr (&Sprog2);
3900 defsubr (&Ssetq);
3901 defsubr (&Squote);
3902 defsubr (&Sfunction);
3903 defsubr (&Sdefault_toplevel_value);
3904 defsubr (&Sset_default_toplevel_value);
3905 defsubr (&Sdefvar);
3906 defsubr (&Sdefvaralias);
3907 defsubr (&Sdefconst);
3908 defsubr (&Smake_var_non_special);
3909 defsubr (&Slet);
3910 defsubr (&SletX);
3911 defsubr (&Swhile);
3912 defsubr (&Smacroexpand);
3913 defsubr (&Scatch);
3914 defsubr (&Sthrow);
3915 defsubr (&Sunwind_protect);
3916 defsubr (&Scondition_case);
3917 defsubr (&Ssignal);
3918 defsubr (&Scommandp);
3919 defsubr (&Sautoload);
3920 defsubr (&Sautoload_do_load);
3921 defsubr (&Seval);
3922 defsubr (&Sapply);
3923 defsubr (&Sfuncall);
3924 defsubr (&Sfunc_arity);
3925 defsubr (&Srun_hooks);
3926 defsubr (&Srun_hook_with_args);
3927 defsubr (&Srun_hook_with_args_until_success);
3928 defsubr (&Srun_hook_with_args_until_failure);
3929 defsubr (&Srun_hook_wrapped);
3930 defsubr (&Sfetch_bytecode);
3931 defsubr (&Sbacktrace_debug);
3932 defsubr (&Sbacktrace);
3933 defsubr (&Sbacktrace_frame);
3934 defsubr (&Sbacktrace_eval);
3935 defsubr (&Sbacktrace__locals);
3936 defsubr (&Sspecial_variable_p);
3937 defsubr (&Sfunctionp);
3938 }