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