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