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