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