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