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