guile-elisp bootstrap (C)
[bpt/emacs.git] / src / eval.c
CommitLineData
db9f0278 1/* Evaluator for GNU Emacs Lisp interpreter.
fa022909 2
ba318903
PE
3Copyright (C) 1985-1987, 1993-1995, 1999-2014 Free Software Foundation,
4Inc.
db9f0278
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
db9f0278 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
db9f0278
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
db9f0278
JB
20
21
18160b98 22#include <config.h>
eb3f1cc8 23#include <limits.h>
4e2fe2e6 24#include <stdio.h>
db9f0278 25#include "lisp.h"
9ac0d9e0 26#include "blockinput.h"
db9f0278 27#include "commands.h"
1f98fa48 28#include "keyboard.h"
3648c842 29#include "dispextern.h"
6376ba30
BT
30#include "guile.h"
31
32static void unbind_once (void *ignore);
b70e1a2b 33
adf2aa61 34/* Chain of condition and catch handlers currently in effect. */
244ed907 35
244ed907
PE
36struct handler *handlerlist;
37
15934ffa
RS
38#ifdef DEBUG_GCPRO
39/* Count levels of GCPRO to detect failure to UNGCPRO. */
40int gcpro_level;
41#endif
42
61b108cc 43Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
29208e82 44Lisp_Object Qinhibit_quit;
955cbe7b
PE
45Lisp_Object Qand_rest;
46static Lisp_Object Qand_optional;
45b82ad0 47static Lisp_Object Qinhibit_debugger;
955cbe7b 48static Lisp_Object Qdeclare;
b9598260
SM
49Lisp_Object Qinternal_interpreter_environment, Qclosure;
50
ed008a6d 51static Lisp_Object Qdebug;
db9f0278 52
6e6e9f08
RS
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. */
4c576a83 56
db9f0278
JB
57Lisp_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:
47b82df9 62 (FUN . ODEF) for a defun, (0 . OFEATURES) for a provide. */
db9f0278
JB
63
64Lisp_Object Vautoload_queue;
65
9349e5f7
PE
66/* Current number of specbindings allocated in specpdl, not counting
67 the dummy entry specpdl[-1]. */
4c576a83 68
d311d28c 69ptrdiff_t specpdl_size;
db9f0278 70
9349e5f7
PE
71/* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
72 only so that its address can be taken. */
4c576a83 73
9349e5f7 74union specbinding *specpdl;
db9f0278
JB
75
76/* Pointer to first unused element in specpdl. */
4c576a83 77
9349e5f7 78union specbinding *specpdl_ptr;
db9f0278 79
db9f0278 80/* Depth in Lisp evaluations and function calls. */
4c576a83 81
adf2aa61 82EMACS_INT lisp_eval_depth;
db9f0278 83
be857679 84/* The value of num_nonmacro_input_events as of the last time we
82da7701 85 started to enter the debugger. If we decide to enter the debugger
be857679 86 again when this is still equal to num_nonmacro_input_events, then we
82da7701
JB
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. */
4c576a83 90
d311d28c 91static EMACS_INT when_entered_debugger;
db9f0278 92
a2ff3819
GM
93/* The function from which the last `signal' was called. Set in
94 Fsignal. */
2f592f95 95/* FIXME: We should probably get rid of this! */
a2ff3819
GM
96Lisp_Object Vsignaling_function;
97
d1f55f16
CY
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. */
102Lisp_Object inhibit_lisp_code;
103
9c203066
PE
104/* These would ordinarily be static, but they need to be visible to GDB. */
105bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
106Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
107Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
108union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
109union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
110
f66c7cf8 111static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
7200d79c 112static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
e46f2325 113
84575e67 114static Lisp_Object
9349e5f7 115specpdl_symbol (union specbinding *pdl)
84575e67
PE
116{
117 eassert (pdl->kind >= SPECPDL_LET);
9349e5f7 118 return pdl->let.symbol;
84575e67
PE
119}
120
121static Lisp_Object
9349e5f7 122specpdl_old_value (union specbinding *pdl)
84575e67
PE
123{
124 eassert (pdl->kind >= SPECPDL_LET);
9349e5f7 125 return pdl->let.old_value;
84575e67
PE
126}
127
56ea7291
SM
128static void
129set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
130{
131 eassert (pdl->kind >= SPECPDL_LET);
132 pdl->let.old_value = val;
133}
134
84575e67 135static Lisp_Object
9349e5f7 136specpdl_where (union specbinding *pdl)
84575e67
PE
137{
138 eassert (pdl->kind > SPECPDL_LET);
9349e5f7 139 return pdl->let.where;
84575e67
PE
140}
141
9c203066 142Lisp_Object
9349e5f7 143backtrace_function (union specbinding *pdl)
84575e67
PE
144{
145 eassert (pdl->kind == SPECPDL_BACKTRACE);
9349e5f7 146 return pdl->bt.function;
84575e67
PE
147}
148
149static ptrdiff_t
9349e5f7 150backtrace_nargs (union specbinding *pdl)
84575e67
PE
151{
152 eassert (pdl->kind == SPECPDL_BACKTRACE);
9349e5f7 153 return pdl->bt.nargs;
84575e67
PE
154}
155
9c203066 156Lisp_Object *
9349e5f7 157backtrace_args (union specbinding *pdl)
84575e67
PE
158{
159 eassert (pdl->kind == SPECPDL_BACKTRACE);
9349e5f7 160 return pdl->bt.args;
84575e67
PE
161}
162
163static bool
9349e5f7 164backtrace_debug_on_exit (union specbinding *pdl)
84575e67
PE
165{
166 eassert (pdl->kind == SPECPDL_BACKTRACE);
9349e5f7 167 return pdl->bt.debug_on_exit;
84575e67
PE
168}
169
2f592f95 170/* Functions to modify slots of backtrace records. */
e46f2325 171
3d5ee10a 172static void
9349e5f7
PE
173set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
174{
175 eassert (pdl->kind == SPECPDL_BACKTRACE);
176 pdl->bt.args = args;
177}
2f592f95 178
3d5ee10a 179static void
9349e5f7
PE
180set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
181{
182 eassert (pdl->kind == SPECPDL_BACKTRACE);
183 pdl->bt.nargs = n;
184}
2f592f95 185
3d5ee10a 186static void
9349e5f7
PE
187set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
188{
189 eassert (pdl->kind == SPECPDL_BACKTRACE);
190 pdl->bt.debug_on_exit = doe;
191}
2f592f95
SM
192
193/* Helper functions to scan the backtrace. */
194
9349e5f7
PE
195bool
196backtrace_p (union specbinding *pdl)
2f592f95 197{ return pdl >= specpdl; }
a8a7c5f6 198
9349e5f7 199union specbinding *
3d5ee10a 200backtrace_top (void)
e46f2325 201{
9349e5f7 202 union specbinding *pdl = specpdl_ptr - 1;
a8a7c5f6 203 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
2f592f95
SM
204 pdl--;
205 return pdl;
e46f2325 206}
a8a7c5f6 207
9349e5f7
PE
208union specbinding *
209backtrace_next (union specbinding *pdl)
e46f2325 210{
2f592f95
SM
211 pdl--;
212 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
213 pdl--;
214 return pdl;
e46f2325
DA
215}
216
6376ba30
BT
217struct handler *
218make_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;
6376ba30
BT
228 c->interrupt_input_blocked = interrupt_input_blocked;
229 c->ptag = make_prompt_tag ();
230 return c;
231}
232
233struct handler *
234make_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;
6376ba30
BT
244 c->interrupt_input_blocked = interrupt_input_blocked;
245 c->ptag = make_prompt_tag ();
246 return c;
247}
2f592f95 248
dfcf069d 249void
d3da34e0 250init_eval_once (void)
db9f0278 251{
98e8eae1 252 enum { size = 50 };
9349e5f7 253 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
98e8eae1 254 specpdl_size = size;
9349e5f7 255 specpdl = specpdl_ptr = pdlvec + 1;
6588243d 256 /* Don't forget to update docs (lispref node "Local Variables"). */
c530e1c2 257 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
d46f6bbb 258 max_lisp_eval_depth = 600;
34d470ba
RS
259
260 Vrun_hooks = Qnil;
db9f0278
JB
261}
262
6376ba30 263static struct handler *handlerlist_sentinel;
70de5e86 264
dfcf069d 265void
d3da34e0 266init_eval (void)
db9f0278
JB
267{
268 specpdl_ptr = specpdl;
6376ba30
BT
269 handlerlist_sentinel = make_catch_handler (Qunbound);
270 handlerlist = handlerlist_sentinel;
db9f0278
JB
271 Vquit_flag = Qnil;
272 debug_on_next_call = 0;
273 lisp_eval_depth = 0;
87e21fbd 274#ifdef DEBUG_GCPRO
15934ffa 275 gcpro_level = 0;
87e21fbd 276#endif
be857679 277 /* This is less than the initial value of num_nonmacro_input_events. */
b5b911f9 278 when_entered_debugger = -1;
db9f0278
JB
279}
280
f6d62986 281/* Unwind-protect function used by call_debugger. */
9f5903bb 282
27e498e6 283static void
d3da34e0 284restore_stack_limits (Lisp_Object data)
9f5903bb
RS
285{
286 max_specpdl_size = XINT (XCAR (data));
287 max_lisp_eval_depth = XINT (XCDR (data));
288}
289
9cad4576
DA
290static void grow_specpdl (void);
291
9f5903bb
RS
292/* Call the Lisp debugger, giving it argument ARG. */
293
7f7e0167 294Lisp_Object
d3da34e0 295call_debugger (Lisp_Object arg)
db9f0278 296{
1882aa38 297 bool debug_while_redisplaying;
2bfa3d3e 298 dynwind_begin ();
3648c842 299 Lisp_Object val;
575593db
DA
300 EMACS_INT old_depth = max_lisp_eval_depth;
301 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
6376ba30 302 EMACS_INT old_max = max_specpdl_size;
9f5903bb
RS
303
304 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
305 max_lisp_eval_depth = lisp_eval_depth + 40;
306
9cad4576
DA
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)));
177c0ea7 311
d148e14d 312#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
313 if (display_hourglass_p)
314 cancel_hourglass ();
237c23b0
GM
315#endif
316
db9f0278 317 debug_on_next_call = 0;
be857679 318 when_entered_debugger = num_nonmacro_input_events;
3648c842
GM
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;
556d7314
GM
324 specbind (intern ("debugger-may-continue"),
325 debug_while_redisplaying ? Qnil : Qt);
8efb6cc7 326 specbind (Qinhibit_redisplay, Qnil);
45b82ad0 327 specbind (Qinhibit_debugger, Qt);
9db6f6b4
GM
328
329#if 0 /* Binding this prevents execution of Lisp code during
330 redisplay, which necessarily leads to display problems. */
8efb6cc7 331 specbind (Qinhibit_eval_during_redisplay, Qt);
9db6f6b4 332#endif
177c0ea7 333
3648c842
GM
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
1b1acc13 338 interrupted redisplay by going back to the top-level. */
3648c842
GM
339 if (debug_while_redisplaying)
340 Ftop_level ();
341
2bfa3d3e
BT
342 dynwind_end ();
343 return val;
db9f0278
JB
344}
345
475545b5 346static void
d3da34e0 347do_debug_on_call (Lisp_Object code)
db9f0278
JB
348{
349 debug_on_next_call = 0;
2f592f95 350 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
6c6f1994 351 call_debugger (list1 (code));
db9f0278
JB
352}
353\f
db9f0278 354DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
b8de5714 355 doc: /* If COND yields non-nil, do THEN, else do ELSE...
9dbc9081
PJ
356Returns the value of THEN or the value of the last of the ELSE's.
357THEN must be one expression, but ELSE... can be zero or more expressions.
358If COND yields nil, and there are no ELSE's, the value is nil.
7a25dc6d 359usage: (if COND THEN ELSE...) */)
5842a27b 360 (Lisp_Object args)
db9f0278 361{
16b0520a 362 Lisp_Object cond;
db9f0278
JB
363 struct gcpro gcpro1;
364
365 GCPRO1 (args);
16b0520a 366 cond = eval_sub (XCAR (args));
db9f0278
JB
367 UNGCPRO;
368
265a9e55 369 if (!NILP (cond))
16b0520a
PE
370 return eval_sub (Fcar (XCDR (args)));
371 return Fprogn (XCDR (XCDR (args)));
db9f0278
JB
372}
373
a7ca3326 374DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
9dbc9081 375 doc: /* Eval BODY forms sequentially and return value of last one.
5b4a1f50 376usage: (progn BODY...) */)
27e498e6 377 (Lisp_Object body)
db9f0278 378{
27e498e6 379 Lisp_Object val = Qnil;
db9f0278
JB
380 struct gcpro gcpro1;
381
27e498e6 382 GCPRO1 (body);
db9f0278 383
27e498e6 384 while (CONSP (body))
db9f0278 385 {
27e498e6
PE
386 val = eval_sub (XCAR (body));
387 body = XCDR (body);
db9f0278 388 }
db9f0278
JB
389
390 UNGCPRO;
391 return val;
392}
393
02c66599 394/* Evaluate BODY sequentially, discarding its value. Suitable for
27e498e6
PE
395 record_unwind_protect. */
396
397void
398unwind_body (Lisp_Object body)
399{
400 Fprogn (body);
401}
402
db9f0278 403DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
9dbc9081
PJ
404 doc: /* Set each SYM to the value of its VAL.
405The symbols SYM are variables; they are literal (not evaluated).
406The values VAL are expressions; they are evaluated.
407Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
408The second VAL is not computed until after the first SYM is set, and so on;
409each VAL can use the new value of variables set earlier in the `setq'.
410The return value of the `setq' form is the value of the last VAL.
819586b2 411usage: (setq [SYM VAL]...) */)
5842a27b 412 (Lisp_Object args)
db9f0278 413{
16b0520a 414 Lisp_Object val, sym, lex_binding;
db9f0278 415
16b0520a
PE
416 val = args;
417 if (CONSP (args))
418 {
419 Lisp_Object args_left = args;
420 struct gcpro gcpro1;
421 GCPRO1 (args);
db9f0278 422
16b0520a
PE
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. */
db9f0278 437
16b0520a
PE
438 args_left = Fcdr (XCDR (args_left));
439 }
440 while (CONSP (args_left));
b9598260 441
16b0520a 442 UNGCPRO;
db9f0278 443 }
db9f0278 444
db9f0278
JB
445 return val;
446}
177c0ea7 447
db9f0278 448DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
9dbc9081 449 doc: /* Return the argument, without evaluating it. `(quote x)' yields `x'.
91a15bc6
SM
450Warning: `quote' does not construct its return value, but just returns
451the value that was pre-constructed by the Lisp reader (see info node
452`(elisp)Printed Representation').
453This means that '(a . b) is not identical to (cons 'a 'b): the former
454does not cons. Quoting should be reserved for constants that will
455never be modified by side-effects, unless you like self-modifying code.
456See the common pitfall in info node `(elisp)Rearrangement' for an example
457of unexpected results when a quoted object is modified.
9dbc9081 458usage: (quote ARG) */)
5842a27b 459 (Lisp_Object args)
db9f0278 460{
16b0520a 461 if (CONSP (XCDR (args)))
1283140e 462 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
16b0520a 463 return XCAR (args);
db9f0278 464}
177c0ea7 465
db9f0278 466DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
9dbc9081
PJ
467 doc: /* Like `quote', but preferred for objects which are functions.
468In byte compilation, `function' causes its argument to be compiled.
469`quote' cannot do that.
470usage: (function ARG) */)
5842a27b 471 (Lisp_Object args)
db9f0278 472{
b9598260
SM
473 Lisp_Object quoted = XCAR (args);
474
16b0520a 475 if (CONSP (XCDR (args)))
1283140e 476 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
b9598260
SM
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. */
23aba0ea
SM
483 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
484 XCDR (quoted)));
b9598260
SM
485 else
486 /* Simply quote the argument. */
487 return quoted;
db9f0278
JB
488}
489
e0f331ab 490
1848d15d 491DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
4a9308b8 492 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
e102f0d8 493Aliased variables always have the same value; setting one sets the other.
4a9308b8 494Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
dd60787c
GM
495omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
496or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
497itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
498then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
4a9308b8 499The return value is BASE-VARIABLE. */)
5842a27b 500 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
19cebf5a 501{
844e0de1 502 sym_t sym;
1848d15d 503
4a9308b8
JB
504 CHECK_SYMBOL (new_alias);
505 CHECK_SYMBOL (base_variable);
19cebf5a 506
4a9308b8 507 sym = XSYMBOL (new_alias);
ce5b453a 508
844e0de1 509 if (SYMBOL_CONSTANT (sym))
178f2507
SM
510 /* Not sure why, but why not? */
511 error ("Cannot make a constant an alias");
ce5b453a 512
844e0de1 513 switch (SYMBOL_REDIRECT (sym))
ce5b453a
SM
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
dd60787c 521 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
ce5b453a
SM
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)))
94b612ad 526 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
ce5b453a
SM
527
528 {
9349e5f7 529 union specbinding *p;
ce5b453a 530
bc985141 531 for (p = specpdl_ptr; p > specpdl; )
2f592f95
SM
532 if ((--p)->kind >= SPECPDL_LET
533 && (EQ (new_alias, specpdl_symbol (p))))
ce5b453a
SM
534 error ("Don't know how to make a let-bound variable an alias");
535 }
536
844e0de1
BT
537 SET_SYMBOL_DECLARED_SPECIAL (sym, 1);
538 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (base_variable), 1);
539 SET_SYMBOL_REDIRECT (sym, SYMBOL_VARALIAS);
ce5b453a 540 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
844e0de1 541 SET_SYMBOL_CONSTANT (sym, SYMBOL_CONSTANT_P (base_variable));
4a9308b8 542 LOADHIST_ATTACH (new_alias);
ce5b453a
SM
543 /* Even if docstring is nil: remove old docstring. */
544 Fput (new_alias, Qvariable_documentation, docstring);
1848d15d 545
4a9308b8 546 return base_variable;
19cebf5a
GM
547}
548
a104f656
SM
549static union specbinding *
550default_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
568DEFUN ("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
581DEFUN ("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}
19cebf5a 594
db9f0278 595DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
29357847 596 doc: /* Define SYMBOL as a variable, and return SYMBOL.
c3a70e2b
CY
597You are not required to define a variable in order to use it, but
598defining it lets you supply an initial value and documentation, which
599can be referred to by the Emacs help facilities and other programming
600tools. The `defvar' form also declares the variable as \"special\",
601so that it is always dynamically bound even if `lexical-binding' is t.
602
603The optional argument INITVALUE is evaluated, and used to set SYMBOL,
604only if SYMBOL's value is void. If SYMBOL is buffer-local, its
605default value is what is set; buffer-local values are not affected.
9dbc9081 606If INITVALUE is missing, SYMBOL's value is not set.
733f68b6
LT
607
608If SYMBOL has a local binding, then this form affects the local
609binding. This is usually not what you want. Thus, if you need to
610load a file defining variables, with this form or with `defconst' or
611`defcustom', you should always load that file _outside_ any bindings
612for these variables. \(`defconst' and `defcustom' behave similarly in
613this respect.)
c3a70e2b
CY
614
615The optional argument DOCSTRING is a documentation string for the
616variable.
617
618To define a user option, use `defcustom' instead of `defvar'.
2df5238c 619usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
5842a27b 620 (Lisp_Object args)
db9f0278 621{
16b0520a 622 Lisp_Object sym, tem, tail;
db9f0278 623
16b0520a
PE
624 sym = XCAR (args);
625 tail = XCDR (args);
a42ba017 626
16b0520a 627 if (CONSP (tail))
db9f0278 628 {
16b0520a
PE
629 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
630 error ("Too many arguments");
631
632 tem = Fdefault_boundp (sym);
633
ba83908c 634 /* Do it before evaluating the initial value, for self-references. */
844e0de1 635 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
590130fb 636
265a9e55 637 if (NILP (tem))
16b0520a 638 Fset_default (sym, eval_sub (XCAR (tail)));
d0bce91e
SM
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. */
a104f656
SM
642 union specbinding *binding = default_toplevel_binding (sym);
643 if (binding && EQ (specpdl_old_value (binding), Qunbound))
d0bce91e 644 {
a104f656 645 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
d0bce91e
SM
646 }
647 }
16b0520a 648 tail = XCDR (tail);
e509f168
SM
649 tem = Fcar (tail);
650 if (!NILP (tem))
33568849 651 {
33568849
SM
652 if (!NILP (Vpurify_flag))
653 tem = Fpurecopy (tem);
654 Fput (sym, Qvariable_documentation, tem);
655 }
6fd797f5 656 LOADHIST_ATTACH (sym);
db9f0278 657 }
f07a954e 658 else if (!NILP (Vinternal_interpreter_environment)
844e0de1 659 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym)))
f07a954e
SM
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). */
23ba2705
SM
663 Vinternal_interpreter_environment
664 = Fcons (sym, Vinternal_interpreter_environment);
33568849 665 else
d28a2170
PE
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 }
addf35fd 671
db9f0278
JB
672 return sym;
673}
674
675DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
9dbc9081 676 doc: /* Define SYMBOL as a constant variable.
c3a70e2b
CY
677This declares that neither programs nor users should ever change the
678value. This constancy is not actually enforced by Emacs Lisp, but
679SYMBOL is marked as a special variable so that it is never lexically
680bound.
681
682The `defconst' form always sets the value of SYMBOL to the result of
683evalling INITVALUE. If SYMBOL is buffer-local, its default value is
684what is set; buffer-local values are not affected. If SYMBOL has a
685local binding, then this form sets the local binding's value.
686However, you should normally not make local bindings for variables
687defined with this form.
688
689The optional DOCSTRING specifies the variable's documentation string.
7a25dc6d 690usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
5842a27b 691 (Lisp_Object args)
db9f0278 692{
16b0520a 693 Lisp_Object sym, tem;
db9f0278 694
16b0520a
PE
695 sym = XCAR (args);
696 if (CONSP (Fcdr (XCDR (XCDR (args)))))
921baa95 697 error ("Too many arguments");
a42ba017 698
16b0520a 699 tem = eval_sub (Fcar (XCDR (args)));
1182a7cb
DL
700 if (!NILP (Vpurify_flag))
701 tem = Fpurecopy (tem);
702 Fset_default (sym, tem);
844e0de1 703 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (sym), 1);
16b0520a 704 tem = Fcar (XCDR (XCDR (args)));
265a9e55 705 if (!NILP (tem))
db9f0278 706 {
265a9e55 707 if (!NILP (Vpurify_flag))
db9f0278
JB
708 tem = Fpurecopy (tem);
709 Fput (sym, Qvariable_documentation, tem);
710 }
873759d5 711 Fput (sym, Qrisky_local_variable, Qt);
6fd797f5 712 LOADHIST_ATTACH (sym);
db9f0278
JB
713 return sym;
714}
715
513749ee
SM
716/* Make SYMBOL lexically scoped. */
717DEFUN ("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);
844e0de1 723 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol), 0);
513749ee
SM
724 return Qnil;
725}
726
db9f0278
JB
727\f
728DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
9dbc9081
PJ
729 doc: /* Bind variables according to VARLIST then eval BODY.
730The value of the last form in BODY is returned.
731Each element of VARLIST is a symbol (which is bound to nil)
732or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
733Each VALUEFORM can refer to the symbols already bound by this VARLIST.
7a25dc6d 734usage: (let* VARLIST BODY...) */)
5842a27b 735 (Lisp_Object args)
db9f0278 736{
b9598260 737 Lisp_Object varlist, var, val, elt, lexenv;
2bfa3d3e 738 dynwind_begin ();
db9f0278
JB
739 struct gcpro gcpro1, gcpro2, gcpro3;
740
741 GCPRO3 (args, elt, varlist);
742
b9598260
SM
743 lexenv = Vinternal_interpreter_environment;
744
16b0520a 745 varlist = XCAR (args);
b9598260 746 while (CONSP (varlist))
db9f0278
JB
747 {
748 QUIT;
b9598260
SM
749
750 elt = XCAR (varlist);
90165123 751 if (SYMBOLP (elt))
b9598260
SM
752 {
753 var = elt;
754 val = Qnil;
755 }
08564963 756 else if (! NILP (Fcdr (Fcdr (elt))))
734d55a2 757 signal_error ("`let' bindings can have only one value-form", elt);
db9f0278
JB
758 else
759 {
b9598260 760 var = Fcar (elt);
defb1411 761 val = eval_sub (Fcar (Fcdr (elt)));
db9f0278 762 }
b9598260 763
f07a954e 764 if (!NILP (lexenv) && SYMBOLP (var)
844e0de1 765 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (var))
f07a954e 766 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
b9598260
SM
767 /* Lexically bind VAR by adding it to the interpreter's binding
768 alist. */
769 {
f07a954e
SM
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;
db9f0278 779 }
b9598260
SM
780 else
781 specbind (var, val);
782
783 varlist = XCDR (varlist);
db9f0278
JB
784 }
785 UNGCPRO;
16b0520a 786 val = Fprogn (XCDR (args));
2bfa3d3e
BT
787 dynwind_end ();
788 return val;
db9f0278
JB
789}
790
791DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
9dbc9081
PJ
792 doc: /* Bind variables according to VARLIST then eval BODY.
793The value of the last form in BODY is returned.
794Each element of VARLIST is a symbol (which is bound to nil)
795or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
796All the VALUEFORMs are evalled before any symbols are bound.
7a25dc6d 797usage: (let VARLIST BODY...) */)
5842a27b 798 (Lisp_Object args)
db9f0278 799{
b9598260 800 Lisp_Object *temps, tem, lexenv;
db9f0278 801 register Lisp_Object elt, varlist;
2bfa3d3e 802 dynwind_begin ();
f66c7cf8 803 ptrdiff_t argnum;
db9f0278 804 struct gcpro gcpro1, gcpro2;
3a7a9129 805 USE_SAFE_ALLOCA;
db9f0278 806
16b0520a 807 varlist = XCAR (args);
db9f0278 808
f6d62986 809 /* Make space to hold the values to give the bound variables. */
db9f0278 810 elt = Flength (varlist);
b72e0717 811 SAFE_ALLOCA_LISP (temps, XFASTINT (elt));
db9f0278 812
f6d62986 813 /* Compute the values and store them in `temps'. */
db9f0278
JB
814
815 GCPRO2 (args, *temps);
816 gcpro2.nvars = 0;
817
67ee9f6e 818 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
db9f0278
JB
819 {
820 QUIT;
67ee9f6e 821 elt = XCAR (varlist);
90165123 822 if (SYMBOLP (elt))
db9f0278 823 temps [argnum++] = Qnil;
08564963 824 else if (! NILP (Fcdr (Fcdr (elt))))
734d55a2 825 signal_error ("`let' bindings can have only one value-form", elt);
db9f0278 826 else
defb1411 827 temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
db9f0278
JB
828 gcpro2.nvars = argnum;
829 }
830 UNGCPRO;
831
b9598260
SM
832 lexenv = Vinternal_interpreter_environment;
833
16b0520a 834 varlist = XCAR (args);
67ee9f6e 835 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
db9f0278 836 {
b9598260
SM
837 Lisp_Object var;
838
67ee9f6e 839 elt = XCAR (varlist);
b9598260 840 var = SYMBOLP (elt) ? elt : Fcar (elt);
db9f0278 841 tem = temps[argnum++];
b9598260 842
f07a954e 843 if (!NILP (lexenv) && SYMBOLP (var)
844e0de1 844 && ! SYMBOL_DECLARED_SPECIAL (XSYMBOL (var))
f07a954e 845 && NILP (Fmemq (var, Vinternal_interpreter_environment)))
b9598260
SM
846 /* Lexically bind VAR by adding it to the lexenv alist. */
847 lexenv = Fcons (Fcons (var, tem), lexenv);
db9f0278 848 else
b9598260
SM
849 /* Dynamically bind VAR. */
850 specbind (var, tem);
db9f0278
JB
851 }
852
b9598260
SM
853 if (!EQ (lexenv, Vinternal_interpreter_environment))
854 /* Instantiate a new lexical environment. */
855 specbind (Qinternal_interpreter_environment, lexenv);
856
16b0520a 857 elt = Fprogn (XCDR (args));
3a7a9129 858 SAFE_FREE ();
2bfa3d3e
BT
859 dynwind_end ();
860 return elt;
db9f0278
JB
861}
862
863DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
9dbc9081
PJ
864 doc: /* If TEST yields non-nil, eval BODY... and repeat.
865The order of execution is thus TEST, BODY, TEST, BODY and so on
866until TEST returns nil.
7a25dc6d 867usage: (while TEST BODY...) */)
5842a27b 868 (Lisp_Object args)
db9f0278 869{
2b9bde76 870 Lisp_Object test, body;
db9f0278
JB
871 struct gcpro gcpro1, gcpro2;
872
873 GCPRO2 (test, body);
874
16b0520a
PE
875 test = XCAR (args);
876 body = XCDR (args);
defb1411 877 while (!NILP (eval_sub (test)))
db9f0278
JB
878 {
879 QUIT;
880 Fprogn (body);
881 }
882
883 UNGCPRO;
884 return Qnil;
885}
886
887DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
9dbc9081
PJ
888 doc: /* Return result of expanding macros at top level of FORM.
889If FORM is not a macro call, it is returned unchanged.
890Otherwise, the macro is expanded and the expansion is considered
891in place of FORM. When a non-macro-call results, it is returned.
892
893The second optional arg ENVIRONMENT specifies an environment of macro
894definitions to shadow the loaded ones for use in file byte-compilation. */)
5842a27b 895 (Lisp_Object form, Lisp_Object environment)
db9f0278 896{
23d6b5a6 897 /* With cleanups from Hallvard Furuseth. */
db9f0278
JB
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. */
90165123 904 if (!CONSP (form))
db9f0278 905 break;
23d6b5a6 906 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
03699b14 907 def = sym = XCAR (form);
23d6b5a6 908 tem = Qnil;
db9f0278
JB
909 /* Trace symbols aliases to other symbols
910 until we get a symbol that is not an alias. */
90165123 911 while (SYMBOLP (def))
db9f0278
JB
912 {
913 QUIT;
23d6b5a6 914 sym = def;
79e8bfbf 915 tem = Fassq (sym, environment);
265a9e55 916 if (NILP (tem))
db9f0278 917 {
1d59fbe3 918 def = SYMBOL_FUNCTION (sym);
eadf1faa 919 if (!NILP (def))
23d6b5a6 920 continue;
db9f0278 921 }
23d6b5a6 922 break;
db9f0278 923 }
79e8bfbf 924 /* Right now TEM is the result from SYM in ENVIRONMENT,
db9f0278 925 and if TEM is nil then DEF is SYM's function definition. */
265a9e55 926 if (NILP (tem))
db9f0278 927 {
79e8bfbf 928 /* SYM is not mentioned in ENVIRONMENT.
db9f0278 929 Look at its function definition. */
7abaf5cc
SM
930 struct gcpro gcpro1;
931 GCPRO1 (form);
932 def = Fautoload_do_load (def, sym, Qmacro);
933 UNGCPRO;
eadf1faa 934 if (!CONSP (def))
f6d62986 935 /* Not defined or definition not suitable. */
db9f0278 936 break;
7abaf5cc 937 if (!EQ (XCAR (def), Qmacro))
db9f0278 938 break;
03699b14 939 else expander = XCDR (def);
db9f0278
JB
940 }
941 else
942 {
03699b14 943 expander = XCDR (tem);
265a9e55 944 if (NILP (expander))
db9f0278
JB
945 break;
946 }
4f18a4ed
SM
947 {
948 Lisp_Object newform = apply1 (expander, XCDR (form));
949 if (EQ (form, newform))
950 break;
951 else
952 form = newform;
953 }
db9f0278
JB
954 }
955 return form;
956}
957\f
958DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
9dbc9081
PJ
959 doc: /* Eval BODY allowing nonlocal exits using `throw'.
960TAG is evalled to get the tag to use; it must not be nil.
961
962Then the BODY is executed.
1d632ccf 963Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
9dbc9081
PJ
964If no throw happens, `catch' returns the value of the last BODY form.
965If a throw happens, it specifies the value to return from `catch'.
7a25dc6d 966usage: (catch TAG BODY...) */)
5842a27b 967 (Lisp_Object args)
db9f0278
JB
968{
969 register Lisp_Object tag;
970 struct gcpro gcpro1;
971
972 GCPRO1 (args);
16b0520a 973 tag = eval_sub (XCAR (args));
db9f0278 974 UNGCPRO;
16b0520a 975 return internal_catch (tag, Fprogn, XCDR (args));
db9f0278
JB
976}
977
b52f569d
PE
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
6376ba30
BT
984static void
985set_handlerlist (void *data)
986{
987 handlerlist = data;
988}
989
990static void
991restore_handler (void *data)
992{
993 struct handler *c = data;
6376ba30
BT
994 unblock_input_to (c->interrupt_input_blocked);
995 immediate_quit = 0;
996}
997
998struct 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
1026static Lisp_Object
1027icc_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
1061static Lisp_Object
1062icc_handler (void *data, Lisp_Object k, Lisp_Object v)
1063{
1064 Lisp_Object (*f) (Lisp_Object) = data;
1065 return f (v);
1066}
1067
1068struct 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
1075static Lisp_Object
1076icc_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
1082static Lisp_Object
1083icc_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
db9f0278
JB
1103/* Set up a catch, then call C function FUNC on argument ARG.
1104 FUNC should return a Lisp_Object.
2f592f95 1105 This is how catches are done from within C code. */
db9f0278
JB
1106
1107Lisp_Object
d3da34e0 1108internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
db9f0278 1109{
6376ba30
BT
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));
db9f0278
JB
1118}
1119
ba410f40
JB
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.
db9f0278 1122
4d7e6e51 1123 This is the guts of Fthrow and Fsignal; they differ only in the way
ba410f40
JB
1124 they choose the catch tag to throw to. A catch tag for a
1125 condition-case form has a TAG of Qnil.
db9f0278 1126
ba410f40
JB
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
4d7e6e51 1132 specified there.
ba410f40
JB
1133
1134 This is used for correct unwinding in Fthrow and Fsignal. */
db9f0278 1135
5d1922cd
BT
1136static Lisp_Object unbind_to_1 (ptrdiff_t, Lisp_Object, bool);
1137
845ca893 1138static _Noreturn void
adf2aa61 1139unwind_to_catch (struct handler *catch, Lisp_Object value)
db9f0278 1140{
6376ba30 1141 abort_to_prompt (catch->ptag, scm_list_1 (value));
db9f0278
JB
1142}
1143
a7ca3326 1144DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
9dbc9081
PJ
1145 doc: /* Throw to the catch for TAG and return VALUE from it.
1146Both TAG and VALUE are evalled. */)
5842a27b 1147 (register Lisp_Object tag, Lisp_Object value)
db9f0278 1148{
adf2aa61 1149 struct handler *c;
db9f0278 1150
8788120f 1151 if (!NILP (tag))
adf2aa61 1152 for (c = handlerlist; c; c = c->next)
8788120f 1153 {
adf2aa61 1154 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
8788120f
KS
1155 unwind_to_catch (c, value);
1156 }
734d55a2 1157 xsignal2 (Qno_catch, tag, value);
db9f0278
JB
1158}
1159
1160
1161DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
9dbc9081
PJ
1162 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1163If BODYFORM completes normally, its value is returned
1164after executing the UNWINDFORMS.
1165If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
7a25dc6d 1166usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
5842a27b 1167 (Lisp_Object args)
db9f0278
JB
1168{
1169 Lisp_Object val;
2bfa3d3e 1170 dynwind_begin ();
db9f0278 1171
16b0520a
PE
1172 record_unwind_protect (unwind_body, XCDR (args));
1173 val = eval_sub (XCAR (args));
2bfa3d3e
BT
1174 dynwind_end ();
1175 return val;
db9f0278
JB
1176}
1177\f
db9f0278 1178DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
9dbc9081 1179 doc: /* Regain control when an error is signaled.
1b1acc13 1180Executes BODYFORM and returns its value if no error happens.
9dbc9081
PJ
1181Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1182where the BODY is made of Lisp expressions.
1183
1184A handler is applicable to an error
1185if CONDITION-NAME is one of the error's condition names.
1186If an error happens, the first applicable handler is run.
1187
024a2d76
CY
1188The car of a handler may be a list of condition names instead of a
1189single condition name; then it handles all of them. If the special
1190condition name `debug' is present in this list, it allows another
1191condition in the list to run the debugger if `debug-on-error' and the
1192other usual mechanisms says it should (otherwise, `condition-case'
1193suppresses the debugger).
9dbc9081 1194
c997bb25
RS
1195When a handler handles an error, control returns to the `condition-case'
1196and it executes the handler's BODY...
d0acbbaf 1197with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
bb8e180f 1198\(If VAR is nil, the handler can't access that information.)
c997bb25
RS
1199Then the value of the last BODY form is returned from the `condition-case'
1200expression.
9dbc9081 1201
9dbc9081 1202See also the function `signal' for more info.
2b47b74d 1203usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
bb8e180f 1204 (Lisp_Object args)
db9f0278 1205{
16b0520a
PE
1206 Lisp_Object var = XCAR (args);
1207 Lisp_Object bodyform = XCAR (XCDR (args));
1208 Lisp_Object handlers = XCDR (XCDR (args));
ee830945
RS
1209
1210 return internal_lisp_condition_case (var, bodyform, handlers);
1211}
1212
6376ba30
BT
1213static Lisp_Object
1214ilcc1 (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
ee830945
RS
1242/* Like Fcondition_case, but the args are separate
1243 rather than passed in a list. Used by Fbyte_code. */
1244
1245Lisp_Object
d3da34e0
JB
1246internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1247 Lisp_Object handlers)
ee830945
RS
1248{
1249 Lisp_Object val;
adf2aa61
SM
1250 struct handler *c;
1251 struct handler *oldhandlerlist = handlerlist;
ee830945 1252
b7826503 1253 CHECK_SYMBOL (var);
82da7701 1254
2b47b74d 1255 for (val = handlers; CONSP (val); val = XCDR (val))
82da7701 1256 {
adf2aa61 1257 Lisp_Object tem = XCAR (val);
5f96776a
RS
1258 if (! (NILP (tem)
1259 || (CONSP (tem)
03699b14
KR
1260 && (SYMBOLP (XCAR (tem))
1261 || CONSP (XCAR (tem))))))
e6c3da20
EZ
1262 error ("Invalid condition handler: %s",
1263 SDATA (Fprin1_to_string (tem, Qt)));
82da7701 1264 }
db9f0278 1265
6376ba30 1266 return ilcc1 (var, bodyform, Freverse (handlers));
db9f0278
JB
1267}
1268
f029ca5f
RS
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
db9f0278 1279Lisp_Object
d3da34e0
JB
1280internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1281 Lisp_Object (*hfun) (Lisp_Object))
db9f0278
JB
1282{
1283 Lisp_Object val;
6376ba30 1284 struct handler *c = make_condition_handler (handlers);
adf2aa61 1285
6376ba30
BT
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));
db9f0278
JB
1290}
1291
2659a09f 1292/* Like internal_condition_case but call BFUN with ARG as its argument. */
f029ca5f 1293
d227775c 1294Lisp_Object
d3da34e0
JB
1295internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1296 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
d227775c
RS
1297{
1298 Lisp_Object val;
6376ba30 1299 struct handler *c = make_condition_handler (handlers);
adf2aa61 1300
6376ba30
BT
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));
d227775c 1308}
10b29d41 1309
53967e09
CY
1310/* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1311 its arguments. */
1312
1313Lisp_Object
178f2507
SM
1314internal_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))
53967e09
CY
1319{
1320 Lisp_Object val;
6376ba30
BT
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));
53967e09 1330}
10b29d41 1331
2659a09f 1332/* Like internal_condition_case but call BFUN with NARGS as first,
10b29d41
GM
1333 and ARGS as second argument. */
1334
1335Lisp_Object
f66c7cf8
PE
1336internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1337 ptrdiff_t nargs,
178f2507
SM
1338 Lisp_Object *args,
1339 Lisp_Object handlers,
cc92c454
SM
1340 Lisp_Object (*hfun) (Lisp_Object err,
1341 ptrdiff_t nargs,
1342 Lisp_Object *args))
10b29d41
GM
1343{
1344 Lisp_Object val;
6376ba30 1345 struct handler *c = make_condition_handler (handlers);
adf2aa61 1346
6376ba30
BT
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));
10b29d41
GM
1356}
1357
d227775c 1358\f
7d47b580 1359static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1882aa38
PE
1360static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1361 Lisp_Object data);
db9f0278 1362
6d5eb5b0
SM
1363void
1364process_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
a7ca3326 1375DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
9dbc9081
PJ
1376 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1377This function does not return.
1378
1379An error symbol is a symbol with an `error-conditions' property
1380that is a list of condition names.
1381A handler for any of those names will get to handle this signal.
1382The symbol `error' should normally be one of them.
1383
1384DATA should be a list. Its elements are printed as part of the error message.
3297ec22
LT
1385See Info anchor `(elisp)Definition of signal' for some details on how this
1386error message is constructed.
9dbc9081
PJ
1387If the signal is handled, DATA is made available to the handler.
1388See also the function `condition-case'. */)
5842a27b 1389 (Lisp_Object error_symbol, Lisp_Object data)
db9f0278 1390{
bfa8ca43 1391 /* When memory is full, ERROR-SYMBOL is nil,
26631f2b
RS
1392 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1393 That is a special case--don't do this in other situations. */
db9f0278 1394 Lisp_Object conditions;
c11d3d17 1395 Lisp_Object string;
e7f7fbaa
SM
1396 Lisp_Object real_error_symbol
1397 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1398 register Lisp_Object clause = Qnil;
1399 struct handler *h;
db9f0278 1400
0caaedb1 1401 immediate_quit = 0;
b782773b 1402 if (waiting_for_input)
1088b922 1403 emacs_abort ();
db9f0278 1404
26631f2b
RS
1405#if 0 /* rms: I don't know why this was here,
1406 but it is surely wrong for an error that is handled. */
d148e14d 1407#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
1408 if (display_hourglass_p)
1409 cancel_hourglass ();
48f8dfa3 1410#endif
177c0ea7 1411#endif
48f8dfa3 1412
61ede770 1413 /* This hook is used by edebug. */
26631f2b
RS
1414 if (! NILP (Vsignal_hook_function)
1415 && ! NILP (error_symbol))
9f5903bb
RS
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 }
61ede770 1426
1ea9dec4 1427 conditions = Fget (real_error_symbol, Qerror_conditions);
db9f0278 1428
a2ff3819
GM
1429 /* Remember from where signal was called. Skip over the frame for
1430 `signal' itself. If a frame for `error' follows, skip that,
26631f2b
RS
1431 too. Don't do this when ERROR_SYMBOL is nil, because that
1432 is a memory-full error. */
090a072f 1433 Vsignaling_function = Qnil;
2f592f95 1434 if (!NILP (error_symbol))
090a072f 1435 {
9349e5f7 1436 union specbinding *pdl = backtrace_next (backtrace_top ());
2f592f95
SM
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);
090a072f 1441 }
a2ff3819 1442
e7f7fbaa 1443 for (h = handlerlist; h; h = h->next)
db9f0278 1444 {
adf2aa61
SM
1445 if (h->type != CONDITION_CASE)
1446 continue;
1447 clause = find_handler_clause (h->tag_or_ch, conditions);
265a9e55 1448 if (!NILP (clause))
e7f7fbaa 1449 break;
db9f0278 1450 }
475545b5 1451
e7f7fbaa 1452 if (/* Don't run the debugger for a memory-full error.
e7c1b6ef 1453 (There is no room in memory to do that!) */
e7f7fbaa
SM
1454 !NILP (error_symbol)
1455 && (!NILP (Vdebug_on_signal)
1456 /* If no handler is present now, try to run the debugger. */
1457 || NILP (clause)
bd1ba3e8
CY
1458 /* A `debug' symbol in the handler list disables the normal
1459 suppression of the debugger. */
afd4479f
SM
1460 || (CONSP (clause) && CONSP (clause)
1461 && !NILP (Fmemq (Qdebug, clause)))
e7f7fbaa
SM
1462 /* Special handler that means "print a message and run debugger
1463 if requested". */
adf2aa61 1464 || EQ (h->tag_or_ch, Qerror)))
e7f7fbaa 1465 {
1882aa38 1466 bool debugger_called
e7f7fbaa
SM
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;
475545b5 1472 }
db9f0278 1473
e7f7fbaa
SM
1474 if (!NILP (clause))
1475 {
1476 Lisp_Object unwind_data
1477 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
475545b5 1478
adf2aa61 1479 unwind_to_catch (h, unwind_data);
e7f7fbaa
SM
1480 }
1481 else
1482 {
6376ba30 1483 if (handlerlist != handlerlist_sentinel)
70de5e86
SM
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. */
e7f7fbaa
SM
1487 Fthrow (Qtop_level, Qt);
1488 }
c11d3d17 1489
1ea9dec4 1490 if (! NILP (error_symbol))
c11d3d17 1491 data = Fcons (error_symbol, data);
475545b5 1492
c11d3d17 1493 string = Ferror_message_string (data);
583f48b9 1494 fatal ("%s", SDATA (string));
db9f0278
JB
1495}
1496
734d55a2
KS
1497/* Internal version of Fsignal that never returns.
1498 Used for anything but Qquit (which can return from Fsignal). */
1499
1500void
d3da34e0 1501xsignal (Lisp_Object error_symbol, Lisp_Object data)
734d55a2
KS
1502{
1503 Fsignal (error_symbol, data);
1088b922 1504 emacs_abort ();
734d55a2
KS
1505}
1506
1507/* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1508
1509void
d3da34e0 1510xsignal0 (Lisp_Object error_symbol)
734d55a2
KS
1511{
1512 xsignal (error_symbol, Qnil);
1513}
1514
1515void
d3da34e0 1516xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
734d55a2
KS
1517{
1518 xsignal (error_symbol, list1 (arg));
1519}
1520
1521void
d3da34e0 1522xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
734d55a2
KS
1523{
1524 xsignal (error_symbol, list2 (arg1, arg2));
1525}
1526
1527void
d3da34e0 1528xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
734d55a2
KS
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
1536void
a8fe7202 1537signal_error (const char *s, Lisp_Object arg)
734d55a2
KS
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))
6c6f1994 1556 arg = list1 (arg);
734d55a2
KS
1557
1558 xsignal (Qerror, Fcons (build_string (s), arg));
1559}
1560
1561
1882aa38 1562/* Return true if LIST is a non-nil atom or
128c0f66
RM
1563 a list containing one of CONDITIONS. */
1564
1882aa38 1565static bool
d3da34e0 1566wants_debugger (Lisp_Object list, Lisp_Object conditions)
128c0f66 1567{
4de86b16 1568 if (NILP (list))
128c0f66
RM
1569 return 0;
1570 if (! CONSP (list))
1571 return 1;
1572
ab67260b 1573 while (CONSP (conditions))
128c0f66 1574 {
ab67260b 1575 Lisp_Object this, tail;
03699b14
KR
1576 this = XCAR (conditions);
1577 for (tail = list; CONSP (tail); tail = XCDR (tail))
1578 if (EQ (XCAR (tail), this))
128c0f66 1579 return 1;
03699b14 1580 conditions = XCDR (conditions);
128c0f66 1581 }
ab67260b 1582 return 0;
128c0f66
RM
1583}
1584
1882aa38 1585/* Return true if an error with condition-symbols CONDITIONS,
fc950e09 1586 and described by SIGNAL-DATA, should skip the debugger
1b1acc13 1587 according to debugger-ignored-errors. */
fc950e09 1588
1882aa38 1589static bool
d3da34e0 1590skip_debugger (Lisp_Object conditions, Lisp_Object data)
fc950e09
KH
1591{
1592 Lisp_Object tail;
1882aa38 1593 bool first_string = 1;
fc950e09
KH
1594 Lisp_Object error_message;
1595
17401c97
GM
1596 error_message = Qnil;
1597 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
fc950e09 1598 {
03699b14 1599 if (STRINGP (XCAR (tail)))
fc950e09
KH
1600 {
1601 if (first_string)
1602 {
1603 error_message = Ferror_message_string (data);
1604 first_string = 0;
1605 }
177c0ea7 1606
03699b14 1607 if (fast_string_match (XCAR (tail), error_message) >= 0)
fc950e09
KH
1608 return 1;
1609 }
1610 else
1611 {
1612 Lisp_Object contail;
1613
17401c97 1614 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
03699b14 1615 if (EQ (XCAR (tail), XCAR (contail)))
fc950e09
KH
1616 return 1;
1617 }
1618 }
1619
1620 return 0;
1621}
1622
ddaa36e1 1623/* Call the debugger if calling it is currently enabled for CONDITIONS.
7d47b580
JB
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. */
1882aa38 1628static bool
d3da34e0 1629maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
ddaa36e1
AS
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. */
4d7e6e51 1638 ! input_blocked_p ()
45b82ad0 1639 && NILP (Vinhibit_debugger)
ddaa36e1
AS
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)
f6d62986 1645 /* RMS: What's this for? */
ddaa36e1
AS
1646 && when_entered_debugger < num_nonmacro_input_events)
1647 {
6c6f1994 1648 call_debugger (list2 (Qerror, combined_data));
ddaa36e1
AS
1649 return 1;
1650 }
1651
1652 return 0;
1653}
1654
db9f0278 1655static Lisp_Object
7d47b580 1656find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
db9f0278
JB
1657{
1658 register Lisp_Object h;
db9f0278 1659
f01cbfdd
RS
1660 /* t is used by handlers for all conditions, set up by C code. */
1661 if (EQ (handlers, Qt))
db9f0278 1662 return Qt;
f01cbfdd 1663
61ede770
RS
1664 /* error is used similarly, but means print an error message
1665 and run the debugger if that is enabled. */
e7f7fbaa
SM
1666 if (EQ (handlers, Qerror))
1667 return Qt;
f01cbfdd 1668
e7f7fbaa 1669 for (h = handlers; CONSP (h); h = XCDR (h))
db9f0278 1670 {
e7f7fbaa 1671 Lisp_Object handler = XCAR (h);
adf2aa61
SM
1672 if (!NILP (Fmemq (handler, conditions)))
1673 return handlers;
db9f0278 1674 }
f01cbfdd 1675
db9f0278
JB
1676 return Qnil;
1677}
1678
db9f0278 1679
f6d62986 1680/* Dump an error message; called like vprintf. */
db9f0278 1681void
b3ffc17c 1682verror (const char *m, va_list ap)
db9f0278 1683{
70476b54 1684 char buf[4000];
c2d1e36d
PE
1685 ptrdiff_t size = sizeof buf;
1686 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
9125da08 1687 char *buffer = buf;
c2d1e36d 1688 ptrdiff_t used;
9125da08
RS
1689 Lisp_Object string;
1690
d749b01b 1691 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
5fdb398c 1692 string = make_string (buffer, used);
eb3f1cc8 1693 if (buffer != buf)
9ae6734f 1694 xfree (buffer);
9125da08 1695
734d55a2 1696 xsignal1 (Qerror, string);
db9f0278 1697}
b3ffc17c
DN
1698
1699
f6d62986 1700/* Dump an error message; called like printf. */
b3ffc17c
DN
1701
1702/* VARARGS 1 */
1703void
1704error (const char *m, ...)
1705{
1706 va_list ap;
1707 va_start (ap, m);
1708 verror (m, ap);
b3ffc17c 1709}
db9f0278 1710\f
a7ca3326 1711DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
9dbc9081
PJ
1712 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1713This means it contains a description for how to read arguments to give it.
1714The value is nil for an invalid function or a symbol with no function
1715definition.
1716
1717Interactively callable functions include strings and vectors (treated
1718as keyboard macros), lambda-expressions that contain a top-level call
1719to `interactive', autoload definitions made by `autoload' with non-nil
1720fourth argument, and some of the built-in functions of Lisp.
1721
e72706be
RS
1722Also, a symbol satisfies `commandp' if its function definition does so.
1723
1724If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
769b4fb2 1725then strings and vectors are not accepted. */)
5842a27b 1726 (Lisp_Object function, Lisp_Object for_call_interactively)
db9f0278
JB
1727{
1728 register Lisp_Object fun;
1729 register Lisp_Object funcar;
52b71f49 1730 Lisp_Object if_prop = Qnil;
db9f0278
JB
1731
1732 fun = function;
1733
eadf1faa
SM
1734 fun = indirect_function (fun); /* Check cycles. */
1735 if (NILP (fun))
ffd56f97 1736 return Qnil;
db9f0278 1737
52b71f49 1738 /* Check an `interactive-form' property if present, analogous to the
eadf1faa 1739 function-documentation property. */
52b71f49
SM
1740 fun = function;
1741 while (SYMBOLP (fun))
1742 {
2b9aa051 1743 Lisp_Object tmp = Fget (fun, Qinteractive_form);
52b71f49
SM
1744 if (!NILP (tmp))
1745 if_prop = Qt;
1746 fun = Fsymbol_function (fun);
1747 }
1748
62947569
BT
1749 if (scm_is_true (scm_procedure_p (fun)))
1750 return (scm_is_true (scm_procedure_property (fun, Qinteractive_form))
1751 ? Qt : if_prop);
db9f0278
JB
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. */
90165123 1755 else if (COMPILEDP (fun))
845975f5 1756 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
52b71f49 1757 ? Qt : if_prop);
db9f0278
JB
1758
1759 /* Strings and vectors are keyboard macros. */
52b71f49 1760 if (STRINGP (fun) || VECTORP (fun))
6e33efc4 1761 return (NILP (for_call_interactively) ? Qt : Qnil);
db9f0278
JB
1762
1763 /* Lists may represent commands. */
1764 if (!CONSP (fun))
1765 return Qnil;
ed16fb98 1766 funcar = XCAR (fun);
b38b1ec0 1767 if (EQ (funcar, Qclosure))
7200d79c
SM
1768 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1769 ? Qt : if_prop);
23aba0ea 1770 else if (EQ (funcar, Qlambda))
52b71f49 1771 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
b38b1ec0 1772 else if (EQ (funcar, Qautoload))
52b71f49 1773 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
db9f0278
JB
1774 else
1775 return Qnil;
1776}
1777
db9f0278 1778DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
9dbc9081
PJ
1779 doc: /* Define FUNCTION to autoload from FILE.
1780FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1781Third arg DOCSTRING is documentation for the function.
1782Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1783Fifth 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.
1787Third through fifth args give info about the real definition.
1788They default to nil.
1789If FUNCTION is already defined other than as an autoload,
1790this does nothing and returns nil. */)
5842a27b 1791 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
db9f0278 1792{
b7826503
PJ
1793 CHECK_SYMBOL (function);
1794 CHECK_STRING (file);
db9f0278 1795
f6d62986 1796 /* If function is defined and not as an autoload, don't override. */
1d59fbe3
BT
1797 if (!NILP (SYMBOL_FUNCTION (function))
1798 && !AUTOLOADP (SYMBOL_FUNCTION (function)))
db9f0278
JB
1799 return Qnil;
1800
32e5c58c
SM
1801 return Fdefalias (function,
1802 list5 (Qautoload, file, docstring, interactive, type),
1803 Qnil);
db9f0278
JB
1804}
1805
27e498e6 1806void
d3da34e0 1807un_autoload (Lisp_Object oldqueue)
db9f0278 1808{
27e498e6 1809 Lisp_Object queue, first, second;
db9f0278
JB
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 {
e509f168 1817 first = XCAR (queue);
db9f0278
JB
1818 second = Fcdr (first);
1819 first = Fcar (first);
47b82df9
RS
1820 if (EQ (first, make_number (0)))
1821 Vfeatures = second;
db9f0278
JB
1822 else
1823 Ffset (first, second);
e509f168 1824 queue = XCDR (queue);
db9f0278 1825 }
db9f0278
JB
1826}
1827
ca20916b
RS
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
7abaf5cc
SM
1832DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1833 doc: /* Load FUNDEF which should be an autoload.
1834If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1835in which case the function returns the new autoloaded function value.
1836If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1837it is defines a macro. */)
1838 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
db9f0278 1839{
2bfa3d3e 1840 dynwind_begin ();
ca20916b 1841 struct gcpro gcpro1, gcpro2, gcpro3;
db9f0278 1842
2bfa3d3e
BT
1843 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef))) {
1844 dynwind_end ();
7abaf5cc 1845 return fundef;
2bfa3d3e 1846 }
7abaf5cc
SM
1847
1848 if (EQ (macro_only, Qmacro))
1849 {
1850 Lisp_Object kind = Fnth (make_number (4), fundef);
2bfa3d3e
BT
1851 if (! (EQ (kind, Qt) || EQ (kind, Qmacro))) {
1852 dynwind_end ();
1853 return fundef;
1854 }
7abaf5cc
SM
1855 }
1856
aea6173f
RS
1857 /* This is to make sure that loadup.el gives a clear picture
1858 of what files are preloaded and when. */
ab4db096
RS
1859 if (! NILP (Vpurify_flag))
1860 error ("Attempt to autoload %s while preparing to dump",
d5db4077 1861 SDATA (SYMBOL_NAME (funname)));
ab4db096 1862
b7826503 1863 CHECK_SYMBOL (funname);
7abaf5cc 1864 GCPRO3 (funname, fundef, macro_only);
db9f0278 1865
f87740dc 1866 /* Preserve the match data. */
89f2614d 1867 record_unwind_save_match_data ();
177c0ea7 1868
a04ee161
RS
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".
d3da34e0 1875
a04ee161 1876 The value saved here is to be restored into Vautoload_queue. */
db9f0278
JB
1877 record_unwind_protect (un_autoload, Vautoload_queue);
1878 Vautoload_queue = Qt;
7abaf5cc
SM
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);
2a49b6e5 1882
db9f0278
JB
1883 /* Once loading finishes, don't undo it. */
1884 Vautoload_queue = Qt;
2bfa3d3e 1885 dynwind_end ();
db9f0278 1886
ca20916b 1887 UNGCPRO;
7abaf5cc
SM
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 }
db9f0278 1901}
4c576a83 1902
db9f0278 1903\f
a7ca3326 1904DEFUN ("eval", Feval, Seval, 1, 2, 0,
a0ee6f27 1905 doc: /* Evaluate FORM and return its value.
8c27f5ff
SM
1906If LEXICAL is t, evaluate using lexical scoping.
1907LEXICAL can also be an actual lexical environment, in the form of an
1908alist mapping symbols to their value. */)
a0ee6f27 1909 (Lisp_Object form, Lisp_Object lexical)
defb1411 1910{
2bfa3d3e 1911 dynwind_begin ();
a0ee6f27 1912 specbind (Qinternal_interpreter_environment,
6c6f1994 1913 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2bfa3d3e
BT
1914 Lisp_Object tem0 = eval_sub (form);
1915 dynwind_end ();
1916 return tem0;
defb1411
SM
1917}
1918
5e301d76
PE
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
2f592f95
SM
1929static void
1930grow_specpdl (void)
1931{
5e301d76
PE
1932 specpdl_ptr++;
1933
1934 if (specpdl_ptr == specpdl + specpdl_size)
2f592f95 1935 {
5e301d76
PE
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;
2f592f95 1940 if (max_size <= specpdl_size)
5e301d76
PE
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;
2f592f95 1952 }
2f592f95
SM
1953}
1954
3d5ee10a 1955void
2f592f95
SM
1956record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
1957{
1958 eassert (nargs >= UNEVALLED);
9349e5f7
PE
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;
5e301d76 1964 grow_specpdl ();
6376ba30
BT
1965 scm_dynwind_unwind_handler (unbind_once, NULL, SCM_F_WIND_EXPLICITLY);
1966}
1967
1968static void
1969set_lisp_eval_depth (void *data)
1970{
1971 EMACS_INT n = (EMACS_INT) data;
1972 lisp_eval_depth = n;
2f592f95
SM
1973}
1974
defb1411
SM
1975/* Eval a sub-expression of the current expression (i.e. in the same
1976 lexical scope). */
c0ff0ac2
BT
1977static Lisp_Object
1978eval_sub_1 (Lisp_Object form)
db9f0278
JB
1979{
1980 Lisp_Object fun, val, original_fun, original_args;
1981 Lisp_Object funcar;
db9f0278
JB
1982 struct gcpro gcpro1, gcpro2, gcpro3;
1983
90165123 1984 if (SYMBOLP (form))
b9598260 1985 {
f07a954e
SM
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);
b9598260
SM
1997 }
1998
db9f0278
JB
1999 if (!CONSP (form))
2000 return form;
2001
2002 QUIT;
9d5a1260
DA
2003
2004 GCPRO1 (form);
765e61e3 2005 maybe_gc ();
9d5a1260 2006 UNGCPRO;
db9f0278 2007
6376ba30
BT
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
db9f0278
JB
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)
921baa95 2018 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
db9f0278
JB
2019 }
2020
7d7bbefd
DA
2021 original_fun = XCAR (form);
2022 original_args = XCDR (form);
db9f0278 2023
2f592f95
SM
2024 /* This also protects them from gc. */
2025 record_in_backtrace (original_fun, &original_args, UNEVALLED);
db9f0278
JB
2026
2027 if (debug_on_next_call)
2028 do_debug_on_call (Qt);
2029
2030 /* At this point, only original_fun and original_args
f6d62986 2031 have values that will be used below. */
db9f0278 2032 retry:
8788120f
KS
2033
2034 /* Optimize for no indirection. */
2035 fun = original_fun;
306d67bd 2036 if (!SYMBOLP (fun))
3ec7babc 2037 fun = Ffunction (Fcons (fun, Qnil));
1d59fbe3 2038 else if (!NILP (fun) && (fun = SYMBOL_FUNCTION (fun), SYMBOLP (fun)))
306d67bd 2039 fun = indirect_function (fun);
db9f0278 2040
0a436d7d
BT
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 }
62947569
BT
2055 set_backtrace_args (specpdl_ptr - 1, args);
2056 set_backtrace_nargs (specpdl_ptr - 1, argnum);
0a436d7d
BT
2057 val = scm_call_n (fun, args, argnum);
2058 }
62947569 2059 else if (CONSP (fun) && EQ (XCAR (fun), Qspecial_operator))
db9f0278 2060 {
62947569 2061 val = scm_apply_0 (XCDR (fun), original_args);
db9f0278 2062 }
ef1b0ba7 2063 else if (COMPILEDP (fun))
defb1411 2064 val = apply_lambda (fun, original_args);
db9f0278
JB
2065 else
2066 {
eadf1faa 2067 if (NILP (fun))
734d55a2 2068 xsignal1 (Qvoid_function, original_fun);
db9f0278 2069 if (!CONSP (fun))
734d55a2
KS
2070 xsignal1 (Qinvalid_function, original_fun);
2071 funcar = XCAR (fun);
90165123 2072 if (!SYMBOLP (funcar))
734d55a2 2073 xsignal1 (Qinvalid_function, original_fun);
db9f0278
JB
2074 if (EQ (funcar, Qautoload))
2075 {
7abaf5cc 2076 Fautoload_do_load (fun, original_fun, Qnil);
db9f0278
JB
2077 goto retry;
2078 }
2079 if (EQ (funcar, Qmacro))
8be3a09c 2080 {
2bfa3d3e 2081 dynwind_begin ();
8be3a09c
SM
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);
2bfa3d3e 2089 dynwind_end ();
8be3a09c
SM
2090 val = eval_sub (exp);
2091 }
defb1411
SM
2092 else if (EQ (funcar, Qlambda)
2093 || EQ (funcar, Qclosure))
2094 val = apply_lambda (fun, original_args);
db9f0278 2095 else
734d55a2 2096 xsignal1 (Qinvalid_function, original_fun);
db9f0278 2097 }
c1788fbc 2098
2f592f95 2099 if (backtrace_debug_on_exit (specpdl_ptr - 1))
6c6f1994 2100 val = call_debugger (list2 (Qexit, val));
6376ba30 2101 scm_dynwind_end ();
824eb35e 2102
db9f0278
JB
2103 return val;
2104}
c0ff0ac2
BT
2105
2106Lisp_Object
2107eval_sub (Lisp_Object form)
2108{
2109 return scm_c_value_ref (eval_sub_1 (form), 0);
2110}
2111\f
2112static Lisp_Object
2113values_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
2121DEFUN ("multiple-value-call", Fmultiple_value_call, Smultiple_value_call,
2122 2, UNEVALLED, 0,
2123 doc: /* Call with multiple values.
2124usage: (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
2135DEFUN ("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}
db9f0278 2141\f
8edd4a2b 2142DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
9dbc9081
PJ
2143 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2144Then return the value FUNCTION returns.
2145Thus, (apply '+ 1 2 '(3 4)) returns 10.
2146usage: (apply FUNCTION &rest ARGUMENTS) */)
f66c7cf8 2147 (ptrdiff_t nargs, Lisp_Object *args)
db9f0278 2148{
d311d28c
PE
2149 ptrdiff_t i;
2150 EMACS_INT numargs;
db9f0278
JB
2151 register Lisp_Object spread_arg;
2152 register Lisp_Object *funcall_args;
3a7a9129 2153 Lisp_Object fun, retval;
96d44c64 2154 struct gcpro gcpro1;
3a7a9129 2155 USE_SAFE_ALLOCA;
db9f0278
JB
2156
2157 fun = args [0];
2158 funcall_args = 0;
2159 spread_arg = args [nargs - 1];
b7826503 2160 CHECK_LIST (spread_arg);
177c0ea7 2161
db9f0278
JB
2162 numargs = XINT (Flength (spread_arg));
2163
2164 if (numargs == 0)
2165 return Ffuncall (nargs - 1, args);
2166 else if (numargs == 1)
2167 {
03699b14 2168 args [nargs - 1] = XCAR (spread_arg);
db9f0278
JB
2169 return Ffuncall (nargs, args);
2170 }
2171
a6e3fa71 2172 numargs += nargs - 2;
db9f0278 2173
8788120f 2174 /* Optimize for no indirection. */
eadf1faa 2175 if (SYMBOLP (fun) && !NILP (fun)
1d59fbe3 2176 && (fun = SYMBOL_FUNCTION (fun), SYMBOLP (fun)))
8788120f 2177 fun = indirect_function (fun);
eadf1faa 2178 if (NILP (fun))
db9f0278 2179 {
f6d62986 2180 /* Let funcall get the error. */
ffd56f97 2181 fun = args[0];
db9f0278
JB
2182 }
2183
db9f0278
JB
2184 /* We add 1 to numargs because funcall_args includes the
2185 function itself as well as its arguments. */
2186 if (!funcall_args)
a6e3fa71 2187 {
b72e0717 2188 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
96d44c64
SM
2189 GCPRO1 (*funcall_args);
2190 gcpro1.nvars = 1 + numargs;
a6e3fa71
JB
2191 }
2192
663e2b3f 2193 memcpy (funcall_args, args, nargs * word_size);
db9f0278
JB
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;
265a9e55 2197 while (!NILP (spread_arg))
db9f0278 2198 {
03699b14
KR
2199 funcall_args [i++] = XCAR (spread_arg);
2200 spread_arg = XCDR (spread_arg);
db9f0278 2201 }
a6e3fa71 2202
96d44c64 2203 /* By convention, the caller needs to gcpro Ffuncall's args. */
3a7a9129
CY
2204 retval = Ffuncall (gcpro1.nvars, funcall_args);
2205 UNGCPRO;
2206 SAFE_FREE ();
2207
2208 return retval;
db9f0278
JB
2209}
2210\f
ff936e53
SM
2211/* Run hook variables in various ways. */
2212
f6d62986 2213static Lisp_Object
f66c7cf8 2214funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
2215{
2216 Ffuncall (nargs, args);
2217 return Qnil;
2218}
ff936e53 2219
a7ca3326 2220DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
9f685258 2221 doc: /* Run each hook in HOOKS.
9dbc9081
PJ
2222Each argument should be a symbol, a hook variable.
2223These symbols are processed in the order specified.
2224If a hook symbol has a non-nil value, that value may be a function
2225or a list of functions to be called to run the hook.
2226If the value is a function, it is called with no arguments.
2227If it is a list, the elements are called, in order, with no arguments.
2228
9f685258
LK
2229Major modes should not use this function directly to run their mode
2230hook; they should use `run-mode-hooks' instead.
2231
72e85d5d
RS
2232Do not use `make-local-variable' to make a hook variable buffer-local.
2233Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 2234usage: (run-hooks &rest HOOKS) */)
f66c7cf8 2235 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53
SM
2236{
2237 Lisp_Object hook[1];
f66c7cf8 2238 ptrdiff_t i;
ff936e53
SM
2239
2240 for (i = 0; i < nargs; i++)
2241 {
2242 hook[0] = args[i];
f6d62986 2243 run_hook_with_args (1, hook, funcall_nil);
ff936e53
SM
2244 }
2245
2246 return Qnil;
2247}
177c0ea7 2248
a7ca3326 2249DEFUN ("run-hook-with-args", Frun_hook_with_args,
9dbc9081
PJ
2250 Srun_hook_with_args, 1, MANY, 0,
2251 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
2252HOOK should be a symbol, a hook variable. The value of HOOK
2253may be nil, a function, or a list of functions. Call each
2254function in order with arguments ARGS. The final return value
2255is unspecified.
9dbc9081 2256
72e85d5d
RS
2257Do not use `make-local-variable' to make a hook variable buffer-local.
2258Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 2259usage: (run-hook-with-args HOOK &rest ARGS) */)
f66c7cf8 2260 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53 2261{
f6d62986 2262 return run_hook_with_args (nargs, args, funcall_nil);
ff936e53
SM
2263}
2264
d393cefb
GM
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.) */
a0d76c27 2268DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
9dbc9081
PJ
2269 Srun_hook_with_args_until_success, 1, MANY, 0,
2270 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
2271HOOK should be a symbol, a hook variable. The value of HOOK
2272may be nil, a function, or a list of functions. Call each
2273function in order with arguments ARGS, stopping at the first
2274one that returns non-nil, and return that value. Otherwise (if
2275all functions return nil, or if there are no functions to call),
2276return nil.
9dbc9081 2277
72e85d5d
RS
2278Do not use `make-local-variable' to make a hook variable buffer-local.
2279Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 2280usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
f66c7cf8 2281 (ptrdiff_t nargs, Lisp_Object *args)
b0b667cb 2282{
f6d62986
SM
2283 return run_hook_with_args (nargs, args, Ffuncall);
2284}
2285
2286static Lisp_Object
f66c7cf8 2287funcall_not (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
2288{
2289 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
ff936e53
SM
2290}
2291
a7ca3326 2292DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
9dbc9081
PJ
2293 Srun_hook_with_args_until_failure, 1, MANY, 0,
2294 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
2295HOOK should be a symbol, a hook variable. The value of HOOK
2296may be nil, a function, or a list of functions. Call each
2297function in order with arguments ARGS, stopping at the first
2298one that returns nil, and return nil. Otherwise (if all functions
2299return 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).
9dbc9081 2301
72e85d5d
RS
2302Do not use `make-local-variable' to make a hook variable buffer-local.
2303Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 2304usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
f66c7cf8 2305 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53 2306{
f6d62986 2307 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
ff936e53
SM
2308}
2309
f6d62986 2310static Lisp_Object
f66c7cf8 2311run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
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
2322DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2323 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2324I.e. instead of calling each function FUN directly with arguments ARGS,
2325it calls WRAP-FUNCTION with arguments FUN and ARGS.
2326As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2327aborts and returns that value.
2328usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
f66c7cf8 2329 (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
2330{
2331 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2332}
ff936e53 2333
c933ea05
RS
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).
f6d62986 2337 FUNCALL specifies how to call each function on the hook.
c933ea05
RS
2338 The caller (or its caller, etc) must gcpro all of ARGS,
2339 except that it isn't necessary to gcpro ARGS[0]. */
2340
f6d62986 2341Lisp_Object
f66c7cf8
PE
2342run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2343 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
ff936e53 2344{
f6d62986 2345 Lisp_Object sym, val, ret = Qnil;
fada05d6 2346 struct gcpro gcpro1, gcpro2, gcpro3;
b0b667cb 2347
f029ca5f
RS
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))
caff32a7 2351 return Qnil;
f029ca5f 2352
b0b667cb 2353 sym = args[0];
aa681b51 2354 val = find_symbol_value (sym);
ff936e53 2355
b0b667cb 2356 if (EQ (val, Qunbound) || NILP (val))
ff936e53 2357 return ret;
dee4ba59 2358 else if (!CONSP (val) || FUNCTIONP (val))
b0b667cb
KH
2359 {
2360 args[0] = val;
f6d62986 2361 return funcall (nargs, args);
b0b667cb
KH
2362 }
2363 else
2364 {
1faed8ae
PE
2365 Lisp_Object global_vals = Qnil;
2366 GCPRO3 (sym, val, global_vals);
cb9d21f8 2367
ff936e53 2368 for (;
f6d62986 2369 CONSP (val) && NILP (ret);
03699b14 2370 val = XCDR (val))
b0b667cb 2371 {
03699b14 2372 if (EQ (XCAR (val), Qt))
b0b667cb
KH
2373 {
2374 /* t indicates this hook has a local binding;
2375 it means to run the global binding too. */
1faed8ae
PE
2376 global_vals = Fdefault_value (sym);
2377 if (NILP (global_vals)) continue;
b0b667cb 2378
1faed8ae 2379 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
b0b667cb 2380 {
1faed8ae 2381 args[0] = global_vals;
f6d62986 2382 ret = funcall (nargs, args);
8932b1c2
CY
2383 }
2384 else
2385 {
2386 for (;
f6d62986 2387 CONSP (global_vals) && NILP (ret);
1faed8ae 2388 global_vals = XCDR (global_vals))
8932b1c2 2389 {
1faed8ae 2390 args[0] = XCAR (global_vals);
8932b1c2
CY
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))
f6d62986 2394 ret = funcall (nargs, args);
8932b1c2 2395 }
b0b667cb
KH
2396 }
2397 }
2398 else
2399 {
03699b14 2400 args[0] = XCAR (val);
f6d62986 2401 ret = funcall (nargs, args);
b0b667cb
KH
2402 }
2403 }
cb9d21f8
RS
2404
2405 UNGCPRO;
ff936e53 2406 return ret;
b0b667cb
KH
2407 }
2408}
c933ea05 2409
7d48558f
RS
2410/* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2411
2412void
d3da34e0 2413run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
7d48558f
RS
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}
ff936e53 2422\f
f6d62986 2423/* Apply fn to arg. */
db9f0278 2424Lisp_Object
d3da34e0 2425apply1 (Lisp_Object fn, Lisp_Object arg)
db9f0278 2426{
a6e3fa71
JB
2427 struct gcpro gcpro1;
2428
2429 GCPRO1 (fn);
265a9e55 2430 if (NILP (arg))
2bfa3d3e 2431 return Ffuncall (1, &fn);
a6e3fa71 2432 gcpro1.nvars = 2;
db9f0278
JB
2433 {
2434 Lisp_Object args[2];
2435 args[0] = fn;
2436 args[1] = arg;
a6e3fa71 2437 gcpro1.var = args;
2bfa3d3e 2438 return Fapply (2, args);
db9f0278 2439 }
db9f0278
JB
2440}
2441
f6d62986 2442/* Call function fn on no arguments. */
db9f0278 2443Lisp_Object
d3da34e0 2444call0 (Lisp_Object fn)
db9f0278 2445{
a6e3fa71
JB
2446 struct gcpro gcpro1;
2447
2448 GCPRO1 (fn);
2bfa3d3e 2449 return Ffuncall (1, &fn);
db9f0278
JB
2450}
2451
f6d62986 2452/* Call function fn with 1 argument arg1. */
db9f0278
JB
2453/* ARGSUSED */
2454Lisp_Object
d3da34e0 2455call1 (Lisp_Object fn, Lisp_Object arg1)
db9f0278 2456{
a6e3fa71 2457 struct gcpro gcpro1;
177c0ea7 2458 Lisp_Object args[2];
a6e3fa71 2459
db9f0278 2460 args[0] = fn;
15285f9f 2461 args[1] = arg1;
a6e3fa71
JB
2462 GCPRO1 (args[0]);
2463 gcpro1.nvars = 2;
2bfa3d3e 2464 return Ffuncall (2, args);
db9f0278
JB
2465}
2466
f6d62986 2467/* Call function fn with 2 arguments arg1, arg2. */
db9f0278
JB
2468/* ARGSUSED */
2469Lisp_Object
d3da34e0 2470call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
db9f0278 2471{
a6e3fa71 2472 struct gcpro gcpro1;
db9f0278
JB
2473 Lisp_Object args[3];
2474 args[0] = fn;
15285f9f
RS
2475 args[1] = arg1;
2476 args[2] = arg2;
a6e3fa71
JB
2477 GCPRO1 (args[0]);
2478 gcpro1.nvars = 3;
2bfa3d3e 2479 return Ffuncall (3, args);
db9f0278
JB
2480}
2481
f6d62986 2482/* Call function fn with 3 arguments arg1, arg2, arg3. */
db9f0278
JB
2483/* ARGSUSED */
2484Lisp_Object
d3da34e0 2485call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
db9f0278 2486{
a6e3fa71 2487 struct gcpro gcpro1;
db9f0278
JB
2488 Lisp_Object args[4];
2489 args[0] = fn;
15285f9f
RS
2490 args[1] = arg1;
2491 args[2] = arg2;
2492 args[3] = arg3;
a6e3fa71
JB
2493 GCPRO1 (args[0]);
2494 gcpro1.nvars = 4;
2bfa3d3e 2495 return Ffuncall (4, args);
db9f0278
JB
2496}
2497
f6d62986 2498/* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
a5a44b91
JB
2499/* ARGSUSED */
2500Lisp_Object
d3da34e0
JB
2501call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2502 Lisp_Object arg4)
a5a44b91
JB
2503{
2504 struct gcpro gcpro1;
a5a44b91
JB
2505 Lisp_Object args[5];
2506 args[0] = fn;
15285f9f
RS
2507 args[1] = arg1;
2508 args[2] = arg2;
2509 args[3] = arg3;
2510 args[4] = arg4;
a5a44b91
JB
2511 GCPRO1 (args[0]);
2512 gcpro1.nvars = 5;
2bfa3d3e 2513 return Ffuncall (5, args);
a5a44b91
JB
2514}
2515
f6d62986 2516/* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
15285f9f
RS
2517/* ARGSUSED */
2518Lisp_Object
d3da34e0
JB
2519call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2520 Lisp_Object arg4, Lisp_Object arg5)
15285f9f
RS
2521{
2522 struct gcpro gcpro1;
15285f9f
RS
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;
2bfa3d3e 2532 return Ffuncall (6, args);
15285f9f
RS
2533}
2534
f6d62986 2535/* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
15285f9f
RS
2536/* ARGSUSED */
2537Lisp_Object
d3da34e0
JB
2538call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2539 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
15285f9f
RS
2540{
2541 struct gcpro gcpro1;
15285f9f
RS
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;
2bfa3d3e 2552 return Ffuncall (7, args);
15285f9f
RS
2553}
2554
f6d62986 2555/* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
574c05e2
KK
2556/* ARGSUSED */
2557Lisp_Object
d3da34e0
JB
2558call7 (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)
574c05e2
KK
2560{
2561 struct gcpro gcpro1;
574c05e2
KK
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;
2bfa3d3e 2573 return Ffuncall (8, args);
574c05e2
KK
2574}
2575
6c2ef893
RS
2576/* The caller should GCPRO all the elements of ARGS. */
2577
a7ca3326 2578DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
7200d79c 2579 doc: /* Non-nil if OBJECT is a function. */)
c566235d 2580 (Lisp_Object object)
b9598260 2581{
e1f29348 2582 if (FUNCTIONP (object))
b9598260 2583 return Qt;
e1f29348 2584 return Qnil;
b9598260
SM
2585}
2586
c0ff0ac2 2587DEFUN ("funcall", Ffuncall1, Sfuncall, 1, MANY, 0,
9dbc9081
PJ
2588 doc: /* Call first argument as a function, passing remaining arguments to it.
2589Return the value that function returns.
2590Thus, (funcall 'cons 'x 'y) returns (x . y).
2591usage: (funcall FUNCTION &rest ARGUMENTS) */)
f66c7cf8 2592 (ptrdiff_t nargs, Lisp_Object *args)
db9f0278 2593{
8788120f 2594 Lisp_Object fun, original_fun;
db9f0278 2595 Lisp_Object funcar;
f66c7cf8 2596 ptrdiff_t numargs = nargs - 1;
db9f0278
JB
2597 Lisp_Object lisp_numargs;
2598 Lisp_Object val;
db9f0278 2599 register Lisp_Object *internal_args;
f66c7cf8 2600 ptrdiff_t i;
db9f0278
JB
2601
2602 QUIT;
db9f0278 2603
6376ba30
BT
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
db9f0278
JB
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)
921baa95 2614 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
db9f0278
JB
2615 }
2616
2f592f95
SM
2617 /* This also GCPROs them. */
2618 record_in_backtrace (args[0], &args[1], nargs - 1);
db9f0278 2619
7abaf5cc
SM
2620 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2621 maybe_gc ();
2622
db9f0278
JB
2623 if (debug_on_next_call)
2624 do_debug_on_call (Qlambda);
2625
8788120f
KS
2626 original_fun = args[0];
2627
db9f0278
JB
2628 retry:
2629
8788120f
KS
2630 /* Optimize for no indirection. */
2631 fun = original_fun;
eadf1faa 2632 if (SYMBOLP (fun) && !NILP (fun)
1d59fbe3 2633 && (fun = SYMBOL_FUNCTION (fun), SYMBOLP (fun)))
8788120f 2634 fun = indirect_function (fun);
db9f0278 2635
0a436d7d
BT
2636 if (scm_is_true (scm_procedure_p (fun)))
2637 {
2638 val = scm_call_n (fun, args + 1, numargs);
2639 }
ef1b0ba7 2640 else if (COMPILEDP (fun))
db9f0278
JB
2641 val = funcall_lambda (fun, numargs, args + 1);
2642 else
2643 {
eadf1faa 2644 if (NILP (fun))
734d55a2 2645 xsignal1 (Qvoid_function, original_fun);
db9f0278 2646 if (!CONSP (fun))
734d55a2
KS
2647 xsignal1 (Qinvalid_function, original_fun);
2648 funcar = XCAR (fun);
90165123 2649 if (!SYMBOLP (funcar))
734d55a2 2650 xsignal1 (Qinvalid_function, original_fun);
defb1411
SM
2651 if (EQ (funcar, Qlambda)
2652 || EQ (funcar, Qclosure))
db9f0278 2653 val = funcall_lambda (fun, numargs, args + 1);
db9f0278
JB
2654 else if (EQ (funcar, Qautoload))
2655 {
7abaf5cc 2656 Fautoload_do_load (fun, original_fun, Qnil);
db9f0278
JB
2657 goto retry;
2658 }
2659 else
734d55a2 2660 xsignal1 (Qinvalid_function, original_fun);
db9f0278 2661 }
2f592f95 2662 if (backtrace_debug_on_exit (specpdl_ptr - 1))
6c6f1994 2663 val = call_debugger (list2 (Qexit, val));
6376ba30 2664 scm_dynwind_end ();
db9f0278
JB
2665 return val;
2666}
c0ff0ac2
BT
2667
2668Lisp_Object
2669Ffuncall (ptrdiff_t nargs, Lisp_Object *args)
2670{
2671 return scm_c_value_ref (Ffuncall1 (nargs, args), 0);
2672}
db9f0278 2673\f
2f7c71a1 2674static Lisp_Object
defb1411 2675apply_lambda (Lisp_Object fun, Lisp_Object args)
db9f0278
JB
2676{
2677 Lisp_Object args_left;
d311d28c
PE
2678 ptrdiff_t i;
2679 EMACS_INT numargs;
db9f0278
JB
2680 register Lisp_Object *arg_vector;
2681 struct gcpro gcpro1, gcpro2, gcpro3;
db9f0278 2682 register Lisp_Object tem;
3a7a9129 2683 USE_SAFE_ALLOCA;
db9f0278 2684
f66c7cf8 2685 numargs = XFASTINT (Flength (args));
c5101a77 2686 SAFE_ALLOCA_LISP (arg_vector, numargs);
db9f0278
JB
2687 args_left = args;
2688
2689 GCPRO3 (*arg_vector, args_left, fun);
2690 gcpro1.nvars = 0;
2691
c5101a77 2692 for (i = 0; i < numargs; )
db9f0278
JB
2693 {
2694 tem = Fcar (args_left), args_left = Fcdr (args_left);
defb1411 2695 tem = eval_sub (tem);
db9f0278
JB
2696 arg_vector[i++] = tem;
2697 gcpro1.nvars = i;
2698 }
2699
2700 UNGCPRO;
2701
2f592f95
SM
2702 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2703 set_backtrace_nargs (specpdl_ptr - 1, i);
c5101a77 2704 tem = funcall_lambda (fun, numargs, arg_vector);
db9f0278
JB
2705
2706 /* Do the debug-on-exit now, while arg_vector still exists. */
2f592f95
SM
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);
6c6f1994 2711 tem = call_debugger (list2 (Qexit, tem));
2f592f95 2712 }
3a7a9129 2713 SAFE_FREE ();
db9f0278
JB
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
2901f1d1 2721static Lisp_Object
f66c7cf8 2722funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
c5101a77 2723 register Lisp_Object *arg_vector)
db9f0278 2724{
defb1411 2725 Lisp_Object val, syms_left, next, lexenv;
2bfa3d3e 2726 dynwind_begin ();
f66c7cf8 2727 ptrdiff_t i;
1882aa38 2728 bool optional, rest;
db9f0278 2729
90165123 2730 if (CONSP (fun))
9ab90667 2731 {
defb1411
SM
2732 if (EQ (XCAR (fun), Qclosure))
2733 {
2734 fun = XCDR (fun); /* Drop `closure'. */
2735 lexenv = XCAR (fun);
23aba0ea 2736 CHECK_LIST_CONS (fun, fun);
defb1411
SM
2737 }
2738 else
2739 lexenv = Qnil;
9ab90667
GM
2740 syms_left = XCDR (fun);
2741 if (CONSP (syms_left))
2742 syms_left = XCAR (syms_left);
2743 else
734d55a2 2744 xsignal1 (Qinvalid_function, fun);
9ab90667 2745 }
90165123 2746 else if (COMPILEDP (fun))
defb1411 2747 {
798cb644
SM
2748 syms_left = AREF (fun, COMPILED_ARGLIST);
2749 if (INTEGERP (syms_left))
876c194c
SM
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
9173deec 2754 Byte-code objects with either a non-existent, or a nil value for
876c194c
SM
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);
2bfa3d3e 2763 dynwind_end ();
876c194c
SM
2764 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2765 AREF (fun, COMPILED_CONSTANTS),
2766 AREF (fun, COMPILED_STACK_DEPTH),
798cb644 2767 syms_left,
876c194c
SM
2768 nargs, arg_vector);
2769 }
defb1411
SM
2770 lexenv = Qnil;
2771 }
9ab90667 2772 else
1088b922 2773 emacs_abort ();
db9f0278 2774
9ab90667
GM
2775 i = optional = rest = 0;
2776 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
db9f0278
JB
2777 {
2778 QUIT;
177c0ea7 2779
9ab90667 2780 next = XCAR (syms_left);
8788120f 2781 if (!SYMBOLP (next))
734d55a2 2782 xsignal1 (Qinvalid_function, fun);
177c0ea7 2783
db9f0278
JB
2784 if (EQ (next, Qand_rest))
2785 rest = 1;
2786 else if (EQ (next, Qand_optional))
2787 optional = 1;
db9f0278 2788 else
db9f0278 2789 {
e610eaca 2790 Lisp_Object arg;
defb1411
SM
2791 if (rest)
2792 {
e610eaca 2793 arg = Flist (nargs - i, &arg_vector[i]);
defb1411
SM
2794 i = nargs;
2795 }
2796 else if (i < nargs)
e610eaca 2797 arg = arg_vector[i++];
b9598260
SM
2798 else if (!optional)
2799 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2800 else
e610eaca 2801 arg = Qnil;
7200d79c 2802
b9598260 2803 /* Bind the argument. */
876c194c 2804 if (!NILP (lexenv) && SYMBOLP (next))
b9598260 2805 /* Lexically bind NEXT by adding it to the lexenv alist. */
e610eaca 2806 lexenv = Fcons (Fcons (next, arg), lexenv);
b9598260
SM
2807 else
2808 /* Dynamically bind NEXT. */
e610eaca 2809 specbind (next, arg);
db9f0278 2810 }
db9f0278
JB
2811 }
2812
9ab90667 2813 if (!NILP (syms_left))
734d55a2 2814 xsignal1 (Qinvalid_function, fun);
9ab90667 2815 else if (i < nargs)
734d55a2 2816 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
db9f0278 2817
b9598260
SM
2818 if (!EQ (lexenv, Vinternal_interpreter_environment))
2819 /* Instantiate a new lexical environment. */
2820 specbind (Qinternal_interpreter_environment, lexenv);
2821
90165123 2822 if (CONSP (fun))
9ab90667 2823 val = Fprogn (XCDR (XCDR (fun)));
db9f0278 2824 else
ca248607
RS
2825 {
2826 /* If we have not actually read the bytecode string
2827 and constants vector yet, fetch them from the file. */
845975f5 2828 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
661c7d6e 2829 Ffetch_bytecode (fun);
b9598260
SM
2830 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2831 AREF (fun, COMPILED_CONSTANTS),
2832 AREF (fun, COMPILED_STACK_DEPTH),
2833 Qnil, 0, 0);
ca248607 2834 }
177c0ea7 2835
2bfa3d3e
BT
2836 dynwind_end ();
2837 return val;
db9f0278 2838}
661c7d6e
KH
2839
2840DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
9dbc9081
PJ
2841 1, 1, 0,
2842 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
5842a27b 2843 (Lisp_Object object)
661c7d6e
KH
2844{
2845 Lisp_Object tem;
2846
845975f5 2847 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
661c7d6e 2848 {
845975f5 2849 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
5bbdb090 2850 if (!CONSP (tem))
845975f5
SM
2851 {
2852 tem = AREF (object, COMPILED_BYTECODE);
2853 if (CONSP (tem) && STRINGP (XCAR (tem)))
d5db4077 2854 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
845975f5
SM
2855 else
2856 error ("Invalid byte code");
2857 }
3ae565b3
SM
2858 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2859 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
661c7d6e
KH
2860 }
2861 return object;
2862}
db9f0278 2863\f
2f592f95
SM
2864/* Return true if SYMBOL currently has a let-binding
2865 which was made in the buffer that is now current. */
2866
2867bool
844e0de1 2868let_shadows_buffer_binding_p (sym_t symbol)
db9f0278 2869{
9349e5f7 2870 union specbinding *p;
2f592f95
SM
2871 Lisp_Object buf = Fcurrent_buffer ();
2872
2873 for (p = specpdl_ptr; p > specpdl; )
2874 if ((--p)->kind > SPECPDL_LET)
2875 {
844e0de1
BT
2876 sym_t let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2877 eassert (SYMBOL_REDIRECT (let_bound_symbol) != SYMBOL_VARALIAS);
2f592f95
SM
2878 if (symbol == let_bound_symbol
2879 && EQ (specpdl_where (p), buf))
2880 return 1;
2881 }
2882
2883 return 0;
2884}
2885
2886bool
2887let_shadows_global_binding_p (Lisp_Object symbol)
2888{
9349e5f7 2889 union specbinding *p;
2f592f95
SM
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;
db9f0278
JB
2896}
2897
3ec7babc 2898/* `specpdl_ptr' describes which variable is
4e2db1fe 2899 let-bound, so it can be properly undone when we unbind_to.
3ec7babc
SM
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
4e2db1fe
SM
2902 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2903 to record V2 here).
3ec7babc
SM
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. */
4e2db1fe 2909
db9f0278 2910void
d3da34e0 2911specbind (Lisp_Object symbol, Lisp_Object value)
db9f0278 2912{
844e0de1 2913 sym_t sym;
ce5b453a 2914
b7826503 2915 CHECK_SYMBOL (symbol);
ce5b453a 2916 sym = XSYMBOL (symbol);
719177b3 2917
ce5b453a 2918 start:
844e0de1 2919 switch (SYMBOL_REDIRECT (sym))
719177b3 2920 {
ce5b453a
SM
2921 case SYMBOL_VARALIAS:
2922 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
2923 case SYMBOL_PLAINVAL:
bb8e180f
AS
2924 /* The most common case is that of a non-constant symbol with a
2925 trivial value. Make that as fast as we can. */
9349e5f7
PE
2926 specpdl_ptr->let.kind = SPECPDL_LET;
2927 specpdl_ptr->let.symbol = symbol;
2928 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
5e301d76 2929 grow_specpdl ();
844e0de1 2930 if (! SYMBOL_CONSTANT (sym))
bb8e180f
AS
2931 SET_SYMBOL_VAL (sym, value);
2932 else
2933 set_internal (symbol, value, Qnil, 1);
2934 break;
4e2db1fe
SM
2935 case SYMBOL_LOCALIZED:
2936 if (SYMBOL_BLV (sym)->frame_local)
2937 error ("Frame-local vars cannot be let-bound");
2938 case SYMBOL_FORWARDED:
ce5b453a
SM
2939 {
2940 Lisp_Object ovalue = find_symbol_value (symbol);
9349e5f7
PE
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 ();
ce5b453a 2945
844e0de1 2946 eassert (SYMBOL_REDIRECT (sym) != SYMBOL_LOCALIZED
2f592f95 2947 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
ce5b453a 2948
844e0de1 2949 if (SYMBOL_REDIRECT (sym) == SYMBOL_LOCALIZED)
2f592f95
SM
2950 {
2951 if (!blv_found (SYMBOL_BLV (sym)))
9349e5f7 2952 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2f592f95
SM
2953 }
2954 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
ce5b453a 2955 {
ce5b453a
SM
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. */
2f592f95 2961 if (NILP (Flocal_variable_p (symbol, Qnil)))
ce5b453a 2962 {
9349e5f7 2963 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
5e301d76 2964 grow_specpdl ();
ce5b453a 2965 Fset_default (symbol, value);
6376ba30 2966 goto done;
ce5b453a
SM
2967 }
2968 }
2969 else
9349e5f7 2970 specpdl_ptr->let.kind = SPECPDL_LET;
ce5b453a 2971
5e301d76 2972 grow_specpdl ();
94b612ad 2973 set_internal (symbol, value, Qnil, 1);
ce5b453a
SM
2974 break;
2975 }
1088b922 2976 default: emacs_abort ();
9ab90667 2977 }
6376ba30
BT
2978
2979 done:
2980 scm_dynwind_unwind_handler (unbind_once, NULL, SCM_F_WIND_EXPLICITLY);
db9f0278
JB
2981}
2982
f4b1eb36
PE
2983/* Push unwind-protect entries of various types. */
2984
db9f0278 2985void
5d1922cd
BT
2986record_unwind_protect_1 (void (*function) (Lisp_Object), Lisp_Object arg,
2987 bool wind_explicitly)
db9f0278 2988{
6376ba30 2989 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
db9f0278
JB
2990}
2991
27e498e6 2992void
5d1922cd 2993record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
27e498e6 2994{
5d1922cd 2995 record_unwind_protect_1 (function, arg, true);
27e498e6
PE
2996}
2997
2998void
5d1922cd
BT
2999record_unwind_protect_ptr_1 (void (*function) (void *), void *arg,
3000 bool wind_explicitly)
27e498e6 3001{
6376ba30
BT
3002 scm_dynwind_unwind_handler (function,
3003 arg,
3004 (wind_explicitly
3005 ? SCM_F_WIND_EXPLICITLY
3006 : 0));
27e498e6
PE
3007}
3008
3009void
5d1922cd 3010record_unwind_protect_ptr (void (*function) (void *), void *arg)
27e498e6 3011{
5d1922cd 3012 record_unwind_protect_ptr_1 (function, arg, true);
27e498e6
PE
3013}
3014
f4b1eb36 3015void
5d1922cd
BT
3016record_unwind_protect_int_1 (void (*function) (int), int arg,
3017 bool wind_explicitly)
f4b1eb36 3018{
6376ba30 3019 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
f4b1eb36
PE
3020}
3021
a0931322 3022void
5d1922cd 3023record_unwind_protect_int (void (*function) (int), int arg)
a0931322 3024{
5d1922cd 3025 record_unwind_protect_int_1 (function, arg, true);
a0931322
PE
3026}
3027
6376ba30
BT
3028static void
3029call_void (void *data)
3030{
3031 ((void (*) (void)) data) ();
3032}
3033
94fcd171 3034void
5d1922cd
BT
3035record_unwind_protect_void_1 (void (*function) (void),
3036 bool wind_explicitly)
94fcd171 3037{
6376ba30 3038 record_unwind_protect_ptr_1 (call_void, function, wind_explicitly);
94fcd171
PE
3039}
3040
a0931322 3041void
5d1922cd 3042record_unwind_protect_void (void (*function) (void))
a0931322 3043{
5d1922cd 3044 record_unwind_protect_void_1 (function, true);
a0931322
PE
3045}
3046
6376ba30
BT
3047static void
3048unbind_once (void *ignore)
a26b12fa
BT
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 {
a26b12fa
BT
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. */
844e0de1
BT
3065 sym_t sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3066 if (SYMBOL_REDIRECT (sym) == SYMBOL_PLAINVAL)
a26b12fa
BT
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
5d1922cd
BT
3097void
3098dynwind_begin (void)
3099{
6376ba30 3100 scm_dynwind_begin (0);
5d1922cd 3101}
f4b1eb36 3102
5d1922cd
BT
3103void
3104dynwind_end (void)
3105{
6376ba30 3106 scm_dynwind_end ();
5d1922cd
BT
3107}
3108
4a330052 3109DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
b9598260
SM
3110 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3111A special variable is one that will be bound dynamically, even in a
3112context where binding is lexical by default. */)
c566235d 3113 (Lisp_Object symbol)
b9598260
SM
3114{
3115 CHECK_SYMBOL (symbol);
844e0de1 3116 return SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol)) ? Qt : Qnil;
b9598260
SM
3117}
3118
db9f0278 3119\f
db9f0278 3120DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
9dbc9081
PJ
3121 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3122The debugger is entered when that frame exits, if the flag is non-nil. */)
5842a27b 3123 (Lisp_Object level, Lisp_Object flag)
db9f0278 3124{
9349e5f7 3125 union specbinding *pdl = backtrace_top ();
d311d28c 3126 register EMACS_INT i;
db9f0278 3127
b7826503 3128 CHECK_NUMBER (level);
db9f0278 3129
2f592f95
SM
3130 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3131 pdl = backtrace_next (pdl);
db9f0278 3132
2f592f95
SM
3133 if (backtrace_p (pdl))
3134 set_backtrace_debug_on_exit (pdl, !NILP (flag));
db9f0278
JB
3135
3136 return flag;
3137}
3138
3139DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
9dbc9081
PJ
3140 doc: /* Print a trace of Lisp function calls currently active.
3141Output stream used is value of `standard-output'. */)
5842a27b 3142 (void)
db9f0278 3143{
9349e5f7 3144 union specbinding *pdl = backtrace_top ();
db9f0278 3145 Lisp_Object tem;
d4b6d95d 3146 Lisp_Object old_print_level = Vprint_level;
db9f0278 3147
d4b6d95d
LMI
3148 if (NILP (Vprint_level))
3149 XSETFASTINT (Vprint_level, 8);
db9f0278 3150
2f592f95 3151 while (backtrace_p (pdl))
db9f0278 3152 {
2f592f95
SM
3153 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3154 if (backtrace_nargs (pdl) == UNEVALLED)
db9f0278 3155 {
2f592f95
SM
3156 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3157 Qnil);
b6703b02 3158 write_string ("\n", -1);
db9f0278
JB
3159 }
3160 else
3161 {
2f592f95 3162 tem = backtrace_function (pdl);
f6d62986 3163 Fprin1 (tem, Qnil); /* This can QUIT. */
db9f0278 3164 write_string ("(", -1);
2f592f95
SM
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 }
b6703b02 3173 write_string (")\n", -1);
db9f0278 3174 }
2f592f95 3175 pdl = backtrace_next (pdl);
db9f0278
JB
3176 }
3177
d4b6d95d 3178 Vprint_level = old_print_level;
db9f0278
JB
3179 return Qnil;
3180}
3181
d5a7a9d9 3182static union specbinding *
56ea7291
SM
3183get_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
3205DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
9dbc9081
PJ
3206 doc: /* Return the function and arguments NFRAMES up from current execution point.
3207If that frame has not evaluated the arguments yet (or is a special form),
3208the value is (nil FUNCTION ARG-FORMS...).
3209If that frame has evaluated its arguments and called its function already,
3210the value is (t FUNCTION ARG-VALUES...).
3211A &rest arg is represented as the tail of the list ARG-VALUES.
3212FUNCTION is whatever was supplied as car of evaluated list,
3213or a lambda expression for macro calls.
56ea7291
SM
3214If NFRAMES is more than the number of frames, the value is nil.
3215If BASE is non-nil, it should be a function and NFRAMES counts from its
3216nearest activation frame. */)
3217 (Lisp_Object nframes, Lisp_Object base)
db9f0278 3218{
56ea7291 3219 union specbinding *pdl = get_backtrace_frame (nframes, base);
db9f0278 3220
2f592f95 3221 if (!backtrace_p (pdl))
db9f0278 3222 return Qnil;
2f592f95
SM
3223 if (backtrace_nargs (pdl) == UNEVALLED)
3224 return Fcons (Qnil,
3225 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
db9f0278
JB
3226 else
3227 {
2f592f95 3228 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
db9f0278 3229
2f592f95 3230 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
db9f0278
JB
3231 }
3232}
a2ff3819 3233
56ea7291
SM
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
94fea300 3239 unwind, except it starts from the other end of the specpdl stack, so we use
56ea7291 3240 the same function for both unwind and rewind. */
d5a7a9d9 3241static void
56ea7291
SM
3242backtrace_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 {
56ea7291
SM
3259 case SPECPDL_BACKTRACE:
3260 break;
3261 case SPECPDL_LET:
a104f656
SM
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. */
844e0de1
BT
3265 sym_t sym = XSYMBOL (specpdl_symbol (tmp));
3266 if (SYMBOL_REDIRECT (sym) == SYMBOL_PLAINVAL)
a104f656
SM
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 }
56ea7291
SM
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
3308DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3309 doc: /* Evaluate EXP in the context of some activation frame.
3310NFRAMES 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);
2bfa3d3e 3314 dynwind_begin ();
56ea7291
SM
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. */
2bfa3d3e
BT
3327 Lisp_Object tem1 = eval_sub (exp);
3328 dynwind_end ();
3329 return tem1;
56ea7291 3330}
f345395c
HE
3331
3332DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3333 doc: /* Return names and values of local variables of a stack frame.
3334NFRAMES 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
09a7c0fe 3352 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
f345395c
HE
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
db9f0278 3398\f
2f592f95
SM
3399void
3400get_backtrace (Lisp_Object array)
3401{
9349e5f7 3402 union specbinding *pdl = backtrace_next (backtrace_top ());
2f592f95 3403 ptrdiff_t i = 0, asize = ASIZE (array);
4ce0541e 3404
2f592f95
SM
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 }
4ce0541e 3413 else
2f592f95 3414 ASET (array, i, Qnil);
4ce0541e
SM
3415 }
3416}
2f592f95
SM
3417
3418Lisp_Object backtrace_top_function (void)
3419{
9349e5f7 3420 union specbinding *pdl = backtrace_top ();
2f592f95
SM
3421 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3422}
6376ba30
BT
3423\f
3424_Noreturn SCM
3425abort_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");
4ce0541e 3430
6376ba30
BT
3431 scm_apply_1 (scm_variable_ref (var), tag, arglst);
3432 emacs_abort ();
3433}
3434
3435SCM
3436call_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
3445SCM
3446make_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
dfcf069d 3455void
d3da34e0 3456syms_of_eval (void)
db9f0278 3457{
fe6aa7a1
BT
3458#include "eval.x"
3459
29208e82 3460 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
fb7ada5f 3461 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
9f5903bb 3462If Lisp code tries to increase the total number past this amount,
2520dc0c
RS
3463an error is signaled.
3464You can safely use a value considerably larger than the default value,
3465if that proves inconveniently small. However, if you increase it too far,
575593db
DA
3466Emacs could run out of memory trying to make the stack bigger.
3467Note that this limit may be silently increased by the debugger
3468if `debug-on-error' or `debug-on-quit' is set. */);
db9f0278 3469
29208e82 3470 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
fb7ada5f 3471 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
2520dc0c
RS
3472
3473This limit serves to catch infinite recursions for you before they cause
9dbc9081
PJ
3474actual stack overflow in C, which would be fatal for Emacs.
3475You can safely make it considerably larger than its default value,
2520dc0c
RS
3476if that proves inconveniently small. However, if you increase it too far,
3477Emacs could overflow the real C stack, and crash. */);
db9f0278 3478
29208e82 3479 DEFVAR_LISP ("quit-flag", Vquit_flag,
9dbc9081 3480 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
42ed718e
RS
3481If the value is t, that means do an ordinary quit.
3482If the value equals `throw-on-input', that means quit by throwing
3483to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3484Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3485but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
db9f0278
JB
3486 Vquit_flag = Qnil;
3487
29208e82 3488 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
9dbc9081
PJ
3489 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3490Note that `quit-flag' will still be set by typing C-g,
3491so a quit will be signaled as soon as `inhibit-quit' is nil.
3492To prevent this happening, set `quit-flag' to nil
3493before making `inhibit-quit' nil. */);
db9f0278
JB
3494 Vinhibit_quit = Qnil;
3495
cd3520a4
JB
3496 DEFSYM (Qinhibit_quit, "inhibit-quit");
3497 DEFSYM (Qautoload, "autoload");
45b82ad0 3498 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
cd3520a4
JB
3499 DEFSYM (Qmacro, "macro");
3500 DEFSYM (Qdeclare, "declare");
177c0ea7 3501
db9f0278
JB
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. */
cd3520a4 3504 DEFSYM (Qexit, "exit");
b9598260 3505
cd3520a4
JB
3506 DEFSYM (Qinteractive, "interactive");
3507 DEFSYM (Qcommandp, "commandp");
cd3520a4
JB
3508 DEFSYM (Qand_rest, "&rest");
3509 DEFSYM (Qand_optional, "&optional");
3510 DEFSYM (Qclosure, "closure");
3511 DEFSYM (Qdebug, "debug");
f01cbfdd 3512
45b82ad0
SM
3513 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3514 doc: /* Non-nil means never enter the debugger.
3515Normally set while the debugger is already active, to avoid recursive
3516invocations. */);
3517 Vinhibit_debugger = Qnil;
3518
29208e82 3519 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
fb7ada5f 3520 doc: /* Non-nil means enter debugger if an error is signaled.
9dbc9081
PJ
3521Does not apply to errors handled by `condition-case' or those
3522matched by `debug-ignored-errors'.
3523If the value is a list, an error only means to enter the debugger
3524if one of its condition symbols appears in the list.
3525When you evaluate an expression interactively, this variable
3526is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
fbbdcf2f 3527The command `toggle-debug-on-error' toggles this.
45b82ad0 3528See also the variable `debug-on-quit' and `inhibit-debugger'. */);
128c0f66 3529 Vdebug_on_error = Qnil;
db9f0278 3530
29208e82 3531 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
fb7ada5f 3532 doc: /* List of errors for which the debugger should not be called.
9dbc9081
PJ
3533Each element may be a condition-name or a regexp that matches error messages.
3534If any element applies to a given error, that error skips the debugger
3535and just returns to top level.
3536This overrides the variable `debug-on-error'.
3537It does not apply to errors handled by `condition-case'. */);
fc950e09
KH
3538 Vdebug_ignored_errors = Qnil;
3539
29208e82 3540 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
fb7ada5f 3541 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
82fc29a1 3542Does not apply if quit is handled by a `condition-case'. */);
db9f0278
JB
3543 debug_on_quit = 0;
3544
29208e82 3545 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
9dbc9081 3546 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
db9f0278 3547
29208e82 3548 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
9dbc9081
PJ
3549 doc: /* Non-nil means debugger may continue execution.
3550This is nil when the debugger is called under circumstances where it
3551might not be safe to continue. */);
dac204bc 3552 debugger_may_continue = 1;
556d7314 3553
29208e82 3554 DEFVAR_LISP ("debugger", Vdebugger,
9dbc9081
PJ
3555 doc: /* Function to call to invoke debugger.
3556If due to frame exit, args are `exit' and the value being returned;
3557 this function's value will be returned instead of that.
3558If due to error, args are `error' and a list of the args to `signal'.
3559If due to `apply' or `funcall' entry, one arg, `lambda'.
3560If due to `eval' entry, one arg, t. */);
db9f0278
JB
3561 Vdebugger = Qnil;
3562
29208e82 3563 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
9dbc9081
PJ
3564 doc: /* If non-nil, this is a function for `signal' to call.
3565It receives the same arguments that `signal' was given.
3566The Edebug package uses this to regain control. */);
61ede770
RS
3567 Vsignal_hook_function = Qnil;
3568
29208e82 3569 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
fb7ada5f 3570 doc: /* Non-nil means call the debugger regardless of condition handlers.
9dbc9081
PJ
3571Note that `debug-on-error', `debug-on-quit' and friends
3572still determine whether to handle the particular condition. */);
57a6e758 3573 Vdebug_on_signal = Qnil;
61ede770 3574
b38b1ec0 3575 /* When lexical binding is being used,
61b108cc 3576 Vinternal_interpreter_environment is non-nil, and contains an alist
b38b1ec0
SM
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. */
61b108cc
SM
3583 DEFSYM (Qinternal_interpreter_environment,
3584 "internal-interpreter-environment");
b38b1ec0
SM
3585 DEFVAR_LISP ("internal-interpreter-environment",
3586 Vinternal_interpreter_environment,
b9598260
SM
3587 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3588When lexical binding is not being used, this variable is nil.
3589A value of `(t)' indicates an empty environment, otherwise it is an
3590alist of active lexical bindings. */);
3591 Vinternal_interpreter_environment = Qnil;
c80e3b4a 3592 /* Don't export this variable to Elisp, so no one can mess with it
b38b1ec0
SM
3593 (Just imagine if someone makes it buffer-local). */
3594 Funintern (Qinternal_interpreter_environment, Qnil);
b9598260 3595
cd3520a4 3596 DEFSYM (Vrun_hooks, "run-hooks");
db9f0278
JB
3597
3598 staticpro (&Vautoload_queue);
3599 Vautoload_queue = Qnil;
a2ff3819
GM
3600 staticpro (&Vsignaling_function);
3601 Vsignaling_function = Qnil;
db9f0278 3602
d1f55f16 3603 inhibit_lisp_code = Qnil;
db9f0278 3604}