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