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