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