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