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