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