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