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