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