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