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