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