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