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