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