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