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