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