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