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