entered into RCS
[bpt/emacs.git] / src / eval.c
CommitLineData
db9f0278 1/* Evaluator for GNU Emacs Lisp interpreter.
c6c5df7f 2 Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
db9f0278
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
18160b98 21#include <config.h>
db9f0278 22#include "lisp.h"
9ac0d9e0 23#include "blockinput.h"
db9f0278
JB
24
25#ifndef standalone
26#include "commands.h"
1f98fa48 27#include "keyboard.h"
db9f0278
JB
28#else
29#define INTERACTIVE 1
30#endif
31
32#include <setjmp.h>
33
34/* This definition is duplicated in alloc.c and keyboard.c */
35/* Putting it in lisp.h makes cc bomb out! */
36
37struct backtrace
38 {
39 struct backtrace *next;
40 Lisp_Object *function;
41 Lisp_Object *args; /* Points to vector of args. */
daa37602
JB
42 int nargs; /* Length of vector.
43 If nargs is UNEVALLED, args points to slot holding
44 list of unevalled args */
db9f0278
JB
45 char evalargs;
46 /* Nonzero means call value of debugger when done with this operation. */
47 char debug_on_exit;
48 };
49
50struct backtrace *backtrace_list;
51
82da7701
JB
52/* This structure helps implement the `catch' and `throw' control
53 structure. A struct catchtag contains all the information needed
54 to restore the state of the interpreter after a non-local jump.
55
56 Handlers for error conditions (represented by `struct handler'
57 structures) just point to a catch tag to do the cleanup required
58 for their jumps.
59
60 catchtag structures are chained together in the C calling stack;
61 the `next' member points to the next outer catchtag.
62
63 A call like (throw TAG VAL) searches for a catchtag whose `tag'
64 member is TAG, and then unbinds to it. The `val' member is used to
65 hold VAL while the stack is unwound; `val' is returned as the value
66 of the catch form.
67
68 All the other members are concerned with restoring the interpreter
69 state. */
db9f0278
JB
70struct catchtag
71 {
72 Lisp_Object tag;
73 Lisp_Object val;
74 struct catchtag *next;
75 struct gcpro *gcpro;
76 jmp_buf jmp;
77 struct backtrace *backlist;
78 struct handler *handlerlist;
79 int lisp_eval_depth;
80 int pdlcount;
81 int poll_suppress_count;
82 };
83
84struct catchtag *catchlist;
85
86Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
ad236261 87Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
db9f0278
JB
88Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
89Lisp_Object Qand_rest, Qand_optional;
90Lisp_Object Qdebug_on_error;
91
92Lisp_Object Vrun_hooks;
93
94/* Non-nil means record all fset's and provide's, to be undone
95 if the file being autoloaded is not fully loaded.
96 They are recorded by being consed onto the front of Vautoload_queue:
97 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
98
99Lisp_Object Vautoload_queue;
100
101/* Current number of specbindings allocated in specpdl. */
102int specpdl_size;
103
104/* Pointer to beginning of specpdl. */
105struct specbinding *specpdl;
106
107/* Pointer to first unused element in specpdl. */
108struct specbinding *specpdl_ptr;
109
110/* Maximum size allowed for specpdl allocation */
111int max_specpdl_size;
112
113/* Depth in Lisp evaluations and function calls. */
114int lisp_eval_depth;
115
116/* Maximum allowed depth in Lisp evaluations and function calls. */
117int max_lisp_eval_depth;
118
119/* Nonzero means enter debugger before next function call */
120int debug_on_next_call;
121
128c0f66 122/* List of conditions (non-nil atom means all) which cause a backtrace
4de86b16 123 if an error is handled by the command loop's error handler. */
128c0f66 124Lisp_Object Vstack_trace_on_error;
db9f0278 125
128c0f66 126/* List of conditions (non-nil atom means all) which enter the debugger
4de86b16 127 if an error is handled by the command loop's error handler. */
128c0f66 128Lisp_Object Vdebug_on_error;
db9f0278
JB
129
130/* Nonzero means enter debugger if a quit signal
128c0f66 131 is handled by the command loop's error handler. */
db9f0278
JB
132int debug_on_quit;
133
82da7701
JB
134/* The value of num_nonmacro_input_chars as of the last time we
135 started to enter the debugger. If we decide to enter the debugger
136 again when this is still equal to num_nonmacro_input_chars, then we
137 know that the debugger itself has an error, and we should just
138 signal the error instead of entering an infinite loop of debugger
139 invocations. */
140int when_entered_debugger;
db9f0278
JB
141
142Lisp_Object Vdebugger;
143
144void specbind (), record_unwind_protect ();
145
146Lisp_Object funcall_lambda ();
147extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
148
149init_eval_once ()
150{
151 specpdl_size = 50;
152 specpdl = (struct specbinding *) malloc (specpdl_size * sizeof (struct specbinding));
153 max_specpdl_size = 600;
154 max_lisp_eval_depth = 200;
155}
156
157init_eval ()
158{
159 specpdl_ptr = specpdl;
160 catchlist = 0;
161 handlerlist = 0;
162 backtrace_list = 0;
163 Vquit_flag = Qnil;
164 debug_on_next_call = 0;
165 lisp_eval_depth = 0;
82da7701 166 when_entered_debugger = 0;
db9f0278
JB
167}
168
169Lisp_Object
170call_debugger (arg)
171 Lisp_Object arg;
172{
173 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
174 max_lisp_eval_depth = lisp_eval_depth + 20;
175 if (specpdl_size + 40 > max_specpdl_size)
176 max_specpdl_size = specpdl_size + 40;
177 debug_on_next_call = 0;
82da7701 178 when_entered_debugger = num_nonmacro_input_chars;
db9f0278
JB
179 return apply1 (Vdebugger, arg);
180}
181
182do_debug_on_call (code)
183 Lisp_Object code;
184{
185 debug_on_next_call = 0;
186 backtrace_list->debug_on_exit = 1;
187 call_debugger (Fcons (code, Qnil));
188}
189\f
190/* NOTE!!! Every function that can call EVAL must protect its args
191 and temporaries from garbage collection while it needs them.
192 The definition of `For' shows what you have to do. */
193
194DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
195 "Eval args until one of them yields non-nil, then return that value.\n\
196The remaining args are not evalled at all.\n\
197If all args return nil, return nil.")
198 (args)
199 Lisp_Object args;
200{
201 register Lisp_Object val;
202 Lisp_Object args_left;
203 struct gcpro gcpro1;
204
265a9e55 205 if (NILP(args))
db9f0278
JB
206 return Qnil;
207
208 args_left = args;
209 GCPRO1 (args_left);
210
211 do
212 {
213 val = Feval (Fcar (args_left));
265a9e55 214 if (!NILP (val))
db9f0278
JB
215 break;
216 args_left = Fcdr (args_left);
217 }
265a9e55 218 while (!NILP(args_left));
db9f0278
JB
219
220 UNGCPRO;
221 return val;
222}
223
224DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
225 "Eval args until one of them yields nil, then return nil.\n\
226The remaining args are not evalled at all.\n\
227If no arg yields nil, return the last arg's value.")
228 (args)
229 Lisp_Object args;
230{
231 register Lisp_Object val;
232 Lisp_Object args_left;
233 struct gcpro gcpro1;
234
265a9e55 235 if (NILP(args))
db9f0278
JB
236 return Qt;
237
238 args_left = args;
239 GCPRO1 (args_left);
240
241 do
242 {
243 val = Feval (Fcar (args_left));
265a9e55 244 if (NILP (val))
db9f0278
JB
245 break;
246 args_left = Fcdr (args_left);
247 }
265a9e55 248 while (!NILP(args_left));
db9f0278
JB
249
250 UNGCPRO;
251 return val;
252}
253
254DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
255 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
256Returns the value of THEN or the value of the last of the ELSE's.\n\
257THEN must be one expression, but ELSE... can be zero or more expressions.\n\
258If COND yields nil, and there are no ELSE's, the value is nil.")
259 (args)
260 Lisp_Object args;
261{
262 register Lisp_Object cond;
263 struct gcpro gcpro1;
264
265 GCPRO1 (args);
266 cond = Feval (Fcar (args));
267 UNGCPRO;
268
265a9e55 269 if (!NILP (cond))
db9f0278
JB
270 return Feval (Fcar (Fcdr (args)));
271 return Fprogn (Fcdr (Fcdr (args)));
272}
273
274DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
275 "(cond CLAUSES...): try each clause until one succeeds.\n\
276Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
277and, if the value is non-nil, this clause succeeds:\n\
278then the expressions in BODY are evaluated and the last one's\n\
279value is the value of the cond-form.\n\
280If no clause succeeds, cond returns nil.\n\
281If a clause has one element, as in (CONDITION),\n\
282CONDITION's value if non-nil is returned from the cond-form.")
283 (args)
284 Lisp_Object args;
285{
286 register Lisp_Object clause, val;
287 struct gcpro gcpro1;
288
289 val = Qnil;
290 GCPRO1 (args);
265a9e55 291 while (!NILP (args))
db9f0278
JB
292 {
293 clause = Fcar (args);
294 val = Feval (Fcar (clause));
265a9e55 295 if (!NILP (val))
db9f0278
JB
296 {
297 if (!EQ (XCONS (clause)->cdr, Qnil))
298 val = Fprogn (XCONS (clause)->cdr);
299 break;
300 }
301 args = XCONS (args)->cdr;
302 }
303 UNGCPRO;
304
305 return val;
306}
307
308DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
309 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
310 (args)
311 Lisp_Object args;
312{
313 register Lisp_Object val, tem;
314 Lisp_Object args_left;
315 struct gcpro gcpro1;
316
317 /* In Mocklisp code, symbols at the front of the progn arglist
318 are to be bound to zero. */
319 if (!EQ (Vmocklisp_arguments, Qt))
320 {
321 val = make_number (0);
265a9e55 322 while (!NILP (args) && (tem = Fcar (args), XTYPE (tem) == Lisp_Symbol))
db9f0278
JB
323 {
324 QUIT;
325 specbind (tem, val), args = Fcdr (args);
326 }
327 }
328
265a9e55 329 if (NILP(args))
db9f0278
JB
330 return Qnil;
331
332 args_left = args;
333 GCPRO1 (args_left);
334
335 do
336 {
337 val = Feval (Fcar (args_left));
338 args_left = Fcdr (args_left);
339 }
265a9e55 340 while (!NILP(args_left));
db9f0278
JB
341
342 UNGCPRO;
343 return val;
344}
345
346DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
347 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
348The value of FIRST is saved during the evaluation of the remaining args,\n\
349whose values are discarded.")
350 (args)
351 Lisp_Object args;
352{
353 Lisp_Object val;
354 register Lisp_Object args_left;
355 struct gcpro gcpro1, gcpro2;
356 register int argnum = 0;
357
265a9e55 358 if (NILP(args))
db9f0278
JB
359 return Qnil;
360
361 args_left = args;
362 val = Qnil;
363 GCPRO2 (args, val);
364
365 do
366 {
367 if (!(argnum++))
368 val = Feval (Fcar (args_left));
369 else
370 Feval (Fcar (args_left));
371 args_left = Fcdr (args_left);
372 }
265a9e55 373 while (!NILP(args_left));
db9f0278
JB
374
375 UNGCPRO;
376 return val;
377}
378
379DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
380 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
381The value of Y is saved during the evaluation of the remaining args,\n\
382whose values are discarded.")
383 (args)
384 Lisp_Object args;
385{
386 Lisp_Object val;
387 register Lisp_Object args_left;
388 struct gcpro gcpro1, gcpro2;
389 register int argnum = -1;
390
391 val = Qnil;
392
87d238ba 393 if (NILP (args))
db9f0278
JB
394 return Qnil;
395
396 args_left = args;
397 val = Qnil;
398 GCPRO2 (args, val);
399
400 do
401 {
402 if (!(argnum++))
403 val = Feval (Fcar (args_left));
404 else
405 Feval (Fcar (args_left));
406 args_left = Fcdr (args_left);
407 }
87d238ba 408 while (!NILP (args_left));
db9f0278
JB
409
410 UNGCPRO;
411 return val;
412}
413
414DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
415 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
b0d75191
RS
416The symbols SYM are variables; they are literal (not evaluated).\n\
417The values VAL are expressions; they are evaluated.\n\
418Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
419The second VAL is not computed until after the first SYM is set, and so on;\n\
420each VAL can use the new value of variables set earlier in the `setq'.\n\
196e7d3f 421The return value of the `setq' form is the value of the last VAL.")
db9f0278
JB
422 (args)
423 Lisp_Object args;
424{
425 register Lisp_Object args_left;
426 register Lisp_Object val, sym;
427 struct gcpro gcpro1;
428
265a9e55 429 if (NILP(args))
db9f0278
JB
430 return Qnil;
431
432 args_left = args;
433 GCPRO1 (args);
434
435 do
436 {
437 val = Feval (Fcar (Fcdr (args_left)));
438 sym = Fcar (args_left);
439 Fset (sym, val);
440 args_left = Fcdr (Fcdr (args_left));
441 }
265a9e55 442 while (!NILP(args_left));
db9f0278
JB
443
444 UNGCPRO;
445 return val;
446}
447
448DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
449 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
450 (args)
451 Lisp_Object args;
452{
453 return Fcar (args);
454}
455
456DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
457 "Like `quote', but preferred for objects which are functions.\n\
458In byte compilation, `function' causes its argument to be compiled.\n\
459`quote' cannot do that.")
460 (args)
461 Lisp_Object args;
462{
463 return Fcar (args);
464}
465
466DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
467 "Return t if function in which this appears was called interactively.\n\
468This means that the function was called with call-interactively (which\n\
469includes being called as the binding of a key)\n\
470and input is currently coming from the keyboard (not in keyboard macro).")
471 ()
472{
473 register struct backtrace *btp;
474 register Lisp_Object fun;
475
476 if (!INTERACTIVE)
477 return Qnil;
478
db9f0278 479 btp = backtrace_list;
daa37602
JB
480
481 /* If this isn't a byte-compiled function, there may be a frame at
482 the top for Finteractive_p itself. If so, skip it. */
483 fun = Findirect_function (*btp->function);
484 if (XTYPE (fun) == Lisp_Subr
485 && (struct Lisp_Subr *) XPNTR (fun) == &Sinteractive_p)
db9f0278 486 btp = btp->next;
daa37602
JB
487
488 /* If we're running an Emacs 18-style byte-compiled function, there
489 may be a frame for Fbytecode. Now, given the strictest
490 definition, this function isn't really being called
491 interactively, but because that's the way Emacs 18 always builds
492 byte-compiled functions, we'll accept it for now. */
493 if (EQ (*btp->function, Qbytecode))
494 btp = btp->next;
495
496 /* If this isn't a byte-compiled function, then we may now be
497 looking at several frames for special forms. Skip past them. */
498 while (btp &&
499 btp->nargs == UNEVALLED)
a6e3fa71
JB
500 btp = btp->next;
501
daa37602
JB
502 /* btp now points at the frame of the innermost function that isn't
503 a special form, ignoring frames for Finteractive_p and/or
504 Fbytecode at the top. If this frame is for a built-in function
505 (such as load or eval-region) return nil. */
ffd56f97 506 fun = Findirect_function (*btp->function);
db9f0278
JB
507 if (XTYPE (fun) == Lisp_Subr)
508 return Qnil;
509 /* btp points to the frame of a Lisp function that called interactive-p.
510 Return t if that function was called interactively. */
511 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
512 return Qt;
513 return Qnil;
514}
515
516DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
517 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
518The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
519See also the function `interactive'.")
520 (args)
521 Lisp_Object args;
522{
523 register Lisp_Object fn_name;
524 register Lisp_Object defn;
525
526 fn_name = Fcar (args);
527 defn = Fcons (Qlambda, Fcdr (args));
265a9e55 528 if (!NILP (Vpurify_flag))
db9f0278
JB
529 defn = Fpurecopy (defn);
530 Ffset (fn_name, defn);
2a49b6e5 531 LOADHIST_ATTACH (fn_name);
db9f0278
JB
532 return fn_name;
533}
534
535DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
536 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
537The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
538When the macro is called, as in (NAME ARGS...),\n\
539the function (lambda ARGLIST BODY...) is applied to\n\
540the list ARGS... as it appears in the expression,\n\
541and the result should be a form to be evaluated instead of the original.")
542 (args)
543 Lisp_Object args;
544{
545 register Lisp_Object fn_name;
546 register Lisp_Object defn;
547
548 fn_name = Fcar (args);
549 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
265a9e55 550 if (!NILP (Vpurify_flag))
db9f0278
JB
551 defn = Fpurecopy (defn);
552 Ffset (fn_name, defn);
2a49b6e5 553 LOADHIST_ATTACH (fn_name);
db9f0278
JB
554 return fn_name;
555}
556
557DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
558 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
559You are not required to define a variable in order to use it,\n\
560but the definition can supply documentation and an initial value\n\
561in a way that tags can recognize.\n\n\
562INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
06ef7355
RS
563If SYMBOL is buffer-local, its default value is what is set;\n\
564 buffer-local values are not affected.\n\
db9f0278
JB
565INITVALUE and DOCSTRING are optional.\n\
566If DOCSTRING starts with *, this variable is identified as a user option.\n\
567 This means that M-x set-variable and M-x edit-options recognize it.\n\
568If INITVALUE is missing, SYMBOL's value is not set.")
569 (args)
570 Lisp_Object args;
571{
572 register Lisp_Object sym, tem;
573
574 sym = Fcar (args);
575 tem = Fcdr (args);
265a9e55 576 if (!NILP (tem))
db9f0278
JB
577 {
578 tem = Fdefault_boundp (sym);
265a9e55 579 if (NILP (tem))
db9f0278
JB
580 Fset_default (sym, Feval (Fcar (Fcdr (args))));
581 }
582 tem = Fcar (Fcdr (Fcdr (args)));
265a9e55 583 if (!NILP (tem))
db9f0278 584 {
265a9e55 585 if (!NILP (Vpurify_flag))
db9f0278
JB
586 tem = Fpurecopy (tem);
587 Fput (sym, Qvariable_documentation, tem);
588 }
2a49b6e5 589 LOADHIST_ATTACH (sym);
db9f0278
JB
590 return sym;
591}
592
593DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
594 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
595The intent is that programs do not change this value, but users may.\n\
596Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
06ef7355
RS
597If SYMBOL is buffer-local, its default value is what is set;\n\
598 buffer-local values are not affected.\n\
db9f0278
JB
599DOCSTRING is optional.\n\
600If DOCSTRING starts with *, this variable is identified as a user option.\n\
601 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
602Note: do not use `defconst' for user options in libraries that are not\n\
603normally loaded, since it is useful for users to be able to specify\n\
604their own values for such variables before loading the library.\n\
605Since `defconst' unconditionally assigns the variable,\n\
606it would override the user's choice.")
607 (args)
608 Lisp_Object args;
609{
610 register Lisp_Object sym, tem;
611
612 sym = Fcar (args);
613 Fset_default (sym, Feval (Fcar (Fcdr (args))));
614 tem = Fcar (Fcdr (Fcdr (args)));
265a9e55 615 if (!NILP (tem))
db9f0278 616 {
265a9e55 617 if (!NILP (Vpurify_flag))
db9f0278
JB
618 tem = Fpurecopy (tem);
619 Fput (sym, Qvariable_documentation, tem);
620 }
2a49b6e5 621 LOADHIST_ATTACH (sym);
db9f0278
JB
622 return sym;
623}
624
625DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
626 "Returns t if VARIABLE is intended to be set and modified by users.\n\
627\(The alternative is a variable used internally in a Lisp program.)\n\
628Determined by whether the first character of the documentation\n\
629for the variable is \"*\"")
630 (variable)
631 Lisp_Object variable;
632{
633 Lisp_Object documentation;
634
635 documentation = Fget (variable, Qvariable_documentation);
636 if (XTYPE (documentation) == Lisp_Int && XINT (documentation) < 0)
637 return Qt;
638 if ((XTYPE (documentation) == Lisp_String) &&
639 ((unsigned char) XSTRING (documentation)->data[0] == '*'))
640 return Qt;
641 return Qnil;
642}
643\f
644DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
645 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
646The value of the last form in BODY is returned.\n\
647Each element of VARLIST is a symbol (which is bound to nil)\n\
648or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
649Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
650 (args)
651 Lisp_Object args;
652{
653 Lisp_Object varlist, val, elt;
654 int count = specpdl_ptr - specpdl;
655 struct gcpro gcpro1, gcpro2, gcpro3;
656
657 GCPRO3 (args, elt, varlist);
658
659 varlist = Fcar (args);
265a9e55 660 while (!NILP (varlist))
db9f0278
JB
661 {
662 QUIT;
663 elt = Fcar (varlist);
664 if (XTYPE (elt) == Lisp_Symbol)
665 specbind (elt, Qnil);
08564963
JB
666 else if (! NILP (Fcdr (Fcdr (elt))))
667 Fsignal (Qerror,
668 Fcons (build_string ("`let' bindings can have only one value-form"),
669 elt));
db9f0278
JB
670 else
671 {
672 val = Feval (Fcar (Fcdr (elt)));
673 specbind (Fcar (elt), val);
674 }
675 varlist = Fcdr (varlist);
676 }
677 UNGCPRO;
678 val = Fprogn (Fcdr (args));
679 return unbind_to (count, val);
680}
681
682DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
683 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
684The value of the last form in BODY is returned.\n\
685Each element of VARLIST is a symbol (which is bound to nil)\n\
686or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
687All the VALUEFORMs are evalled before any symbols are bound.")
688 (args)
689 Lisp_Object args;
690{
691 Lisp_Object *temps, tem;
692 register Lisp_Object elt, varlist;
693 int count = specpdl_ptr - specpdl;
694 register int argnum;
695 struct gcpro gcpro1, gcpro2;
696
697 varlist = Fcar (args);
698
699 /* Make space to hold the values to give the bound variables */
700 elt = Flength (varlist);
701 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
702
703 /* Compute the values and store them in `temps' */
704
705 GCPRO2 (args, *temps);
706 gcpro2.nvars = 0;
707
265a9e55 708 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
db9f0278
JB
709 {
710 QUIT;
711 elt = Fcar (varlist);
712 if (XTYPE (elt) == Lisp_Symbol)
713 temps [argnum++] = Qnil;
08564963
JB
714 else if (! NILP (Fcdr (Fcdr (elt))))
715 Fsignal (Qerror,
716 Fcons (build_string ("`let' bindings can have only one value-form"),
717 elt));
db9f0278
JB
718 else
719 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
720 gcpro2.nvars = argnum;
721 }
722 UNGCPRO;
723
724 varlist = Fcar (args);
265a9e55 725 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
db9f0278
JB
726 {
727 elt = Fcar (varlist);
728 tem = temps[argnum++];
729 if (XTYPE (elt) == Lisp_Symbol)
730 specbind (elt, tem);
731 else
732 specbind (Fcar (elt), tem);
733 }
734
735 elt = Fprogn (Fcdr (args));
736 return unbind_to (count, elt);
737}
738
739DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
740 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
741The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
742until TEST returns nil.")
743 (args)
744 Lisp_Object args;
745{
746 Lisp_Object test, body, tem;
747 struct gcpro gcpro1, gcpro2;
748
749 GCPRO2 (test, body);
750
751 test = Fcar (args);
752 body = Fcdr (args);
e3c24a74
RS
753 while (tem = Feval (test),
754 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
db9f0278
JB
755 {
756 QUIT;
757 Fprogn (body);
758 }
759
760 UNGCPRO;
761 return Qnil;
762}
763
764DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
765 "Return result of expanding macros at top level of FORM.\n\
766If FORM is not a macro call, it is returned unchanged.\n\
767Otherwise, the macro is expanded and the expansion is considered\n\
768in place of FORM. When a non-macro-call results, it is returned.\n\n\
769The second optional arg ENVIRONMENT species an environment of macro\n\
770definitions to shadow the loaded ones for use in file byte-compilation.")
771 (form, env)
772 register Lisp_Object form;
773 Lisp_Object env;
774{
23d6b5a6 775 /* With cleanups from Hallvard Furuseth. */
db9f0278
JB
776 register Lisp_Object expander, sym, def, tem;
777
778 while (1)
779 {
780 /* Come back here each time we expand a macro call,
781 in case it expands into another macro call. */
782 if (XTYPE (form) != Lisp_Cons)
783 break;
23d6b5a6
JB
784 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
785 def = sym = XCONS (form)->car;
786 tem = Qnil;
db9f0278
JB
787 /* Trace symbols aliases to other symbols
788 until we get a symbol that is not an alias. */
23d6b5a6 789 while (XTYPE (def) == Lisp_Symbol)
db9f0278
JB
790 {
791 QUIT;
23d6b5a6 792 sym = def;
db9f0278 793 tem = Fassq (sym, env);
265a9e55 794 if (NILP (tem))
db9f0278
JB
795 {
796 def = XSYMBOL (sym)->function;
23d6b5a6
JB
797 if (!EQ (def, Qunbound))
798 continue;
db9f0278 799 }
23d6b5a6 800 break;
db9f0278
JB
801 }
802 /* Right now TEM is the result from SYM in ENV,
803 and if TEM is nil then DEF is SYM's function definition. */
265a9e55 804 if (NILP (tem))
db9f0278
JB
805 {
806 /* SYM is not mentioned in ENV.
807 Look at its function definition. */
808 if (EQ (def, Qunbound)
809 || XTYPE (def) != Lisp_Cons)
810 /* Not defined or definition not suitable */
811 break;
812 if (EQ (XCONS (def)->car, Qautoload))
813 {
814 /* Autoloading function: will it be a macro when loaded? */
ee9ee63c 815 tem = Fnth (make_number (4), def);
47ccd8b6 816 if (EQ (tem, Qt) || EQ (tem, Qmacro))
ee9ee63c
JB
817 /* Yes, load it and try again. */
818 {
819 do_autoload (def, sym);
820 continue;
821 }
822 else
db9f0278 823 break;
db9f0278
JB
824 }
825 else if (!EQ (XCONS (def)->car, Qmacro))
826 break;
827 else expander = XCONS (def)->cdr;
828 }
829 else
830 {
831 expander = XCONS (tem)->cdr;
265a9e55 832 if (NILP (expander))
db9f0278
JB
833 break;
834 }
db9f0278
JB
835 form = apply1 (expander, XCONS (form)->cdr);
836 }
837 return form;
838}
839\f
840DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
841 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
842TAG is evalled to get the tag to use. Then the BODY is executed.\n\
843Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
844If no throw happens, `catch' returns the value of the last BODY form.\n\
845If a throw happens, it specifies the value to return from `catch'.")
846 (args)
847 Lisp_Object args;
848{
849 register Lisp_Object tag;
850 struct gcpro gcpro1;
851
852 GCPRO1 (args);
853 tag = Feval (Fcar (args));
854 UNGCPRO;
855 return internal_catch (tag, Fprogn, Fcdr (args));
856}
857
858/* Set up a catch, then call C function FUNC on argument ARG.
859 FUNC should return a Lisp_Object.
860 This is how catches are done from within C code. */
861
862Lisp_Object
863internal_catch (tag, func, arg)
864 Lisp_Object tag;
865 Lisp_Object (*func) ();
866 Lisp_Object arg;
867{
868 /* This structure is made part of the chain `catchlist'. */
869 struct catchtag c;
870
871 /* Fill in the components of c, and put it on the list. */
872 c.next = catchlist;
873 c.tag = tag;
874 c.val = Qnil;
875 c.backlist = backtrace_list;
876 c.handlerlist = handlerlist;
877 c.lisp_eval_depth = lisp_eval_depth;
878 c.pdlcount = specpdl_ptr - specpdl;
879 c.poll_suppress_count = poll_suppress_count;
880 c.gcpro = gcprolist;
881 catchlist = &c;
882
883 /* Call FUNC. */
884 if (! _setjmp (c.jmp))
885 c.val = (*func) (arg);
886
887 /* Throw works by a longjmp that comes right here. */
888 catchlist = c.next;
889 return c.val;
890}
891
ba410f40
JB
892/* Unwind the specbind, catch, and handler stacks back to CATCH, and
893 jump to that CATCH, returning VALUE as the value of that catch.
db9f0278 894
ba410f40
JB
895 This is the guts Fthrow and Fsignal; they differ only in the way
896 they choose the catch tag to throw to. A catch tag for a
897 condition-case form has a TAG of Qnil.
db9f0278 898
ba410f40
JB
899 Before each catch is discarded, unbind all special bindings and
900 execute all unwind-protect clauses made above that catch. Unwind
901 the handler stack as we go, so that the proper handlers are in
902 effect for each unwind-protect clause we run. At the end, restore
903 some static info saved in CATCH, and longjmp to the location
904 specified in the
905
906 This is used for correct unwinding in Fthrow and Fsignal. */
db9f0278
JB
907
908static void
ba410f40 909unwind_to_catch (catch, value)
db9f0278 910 struct catchtag *catch;
ba410f40 911 Lisp_Object value;
db9f0278
JB
912{
913 register int last_time;
914
ba410f40
JB
915 /* Save the value in the tag. */
916 catch->val = value;
917
82da7701 918 /* Restore the polling-suppression count. */
1cdc3155 919 set_poll_suppress_count (catch->poll_suppress_count);
82da7701 920
db9f0278
JB
921 do
922 {
923 last_time = catchlist == catch;
82da7701
JB
924
925 /* Unwind the specpdl stack, and then restore the proper set of
926 handlers. */
db9f0278
JB
927 unbind_to (catchlist->pdlcount, Qnil);
928 handlerlist = catchlist->handlerlist;
929 catchlist = catchlist->next;
930 }
931 while (! last_time);
932
933 gcprolist = catch->gcpro;
934 backtrace_list = catch->backlist;
935 lisp_eval_depth = catch->lisp_eval_depth;
ba410f40
JB
936
937 _longjmp (catch->jmp, 1);
db9f0278
JB
938}
939
940DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
941 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
942Both TAG and VALUE are evalled.")
943 (tag, val)
944 register Lisp_Object tag, val;
945{
946 register struct catchtag *c;
947
948 while (1)
949 {
265a9e55 950 if (!NILP (tag))
db9f0278
JB
951 for (c = catchlist; c; c = c->next)
952 {
953 if (EQ (c->tag, tag))
ba410f40 954 unwind_to_catch (c, val);
db9f0278
JB
955 }
956 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (val, Qnil)));
957 }
958}
959
960
961DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
962 "Do BODYFORM, protecting with UNWINDFORMS.\n\
963Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
964If BODYFORM completes normally, its value is returned\n\
965after executing the UNWINDFORMS.\n\
966If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
967 (args)
968 Lisp_Object args;
969{
970 Lisp_Object val;
971 int count = specpdl_ptr - specpdl;
972
973 record_unwind_protect (0, Fcdr (args));
974 val = Feval (Fcar (args));
975 return unbind_to (count, val);
976}
977\f
978/* Chain of condition handlers currently in effect.
979 The elements of this chain are contained in the stack frames
980 of Fcondition_case and internal_condition_case.
981 When an error is signaled (by calling Fsignal, below),
982 this chain is searched for an element that applies. */
983
984struct handler *handlerlist;
985
986DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
987 "Regain control when an error is signaled.\n\
988Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
989executes BODYFORM and returns its value if no error happens.\n\
990Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
991where the BODY is made of Lisp expressions.\n\n\
992A handler is applicable to an error\n\
993if CONDITION-NAME is one of the error's condition names.\n\
994If an error happens, the first applicable handler is run.\n\
995\n\
633357d4
RS
996The car of a handler may be a list of condition names\n\
997instead of a single condition name.\n\
998\n\
db9f0278
JB
999When a handler handles an error,\n\
1000control returns to the condition-case and the handler BODY... is executed\n\
1001with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1002VAR may be nil; then you do not get access to the signal information.\n\
1003\n\
1004The value of the last BODY form is returned from the condition-case.\n\
1005See also the function `signal' for more info.")
1006 (args)
1007 Lisp_Object args;
1008{
1009 Lisp_Object val;
1010 struct catchtag c;
1011 struct handler h;
82da7701 1012 register Lisp_Object var, bodyform, handlers;
db9f0278 1013
82da7701
JB
1014 var = Fcar (args);
1015 bodyform = Fcar (Fcdr (args));
1016 handlers = Fcdr (Fcdr (args));
1017 CHECK_SYMBOL (var, 0);
1018
1019 for (val = handlers; ! NILP (val); val = Fcdr (val))
1020 {
1021 Lisp_Object tem;
1022 tem = Fcar (val);
5f96776a
RS
1023 if (! (NILP (tem)
1024 || (CONSP (tem)
1025 && (SYMBOLP (XCONS (tem)->car)
1026 || CONSP (XCONS (tem)->car)))))
82da7701
JB
1027 error ("Invalid condition handler", tem);
1028 }
db9f0278
JB
1029
1030 c.tag = Qnil;
1031 c.val = Qnil;
1032 c.backlist = backtrace_list;
1033 c.handlerlist = handlerlist;
1034 c.lisp_eval_depth = lisp_eval_depth;
1035 c.pdlcount = specpdl_ptr - specpdl;
1036 c.poll_suppress_count = poll_suppress_count;
1037 c.gcpro = gcprolist;
1038 if (_setjmp (c.jmp))
1039 {
265a9e55 1040 if (!NILP (h.var))
9d58218c
RS
1041 specbind (h.var, c.val);
1042 val = Fprogn (Fcdr (h.chosen_clause));
82da7701
JB
1043
1044 /* Note that this just undoes the binding of h.var; whoever
1045 longjumped to us unwound the stack to c.pdlcount before
1046 throwing. */
db9f0278
JB
1047 unbind_to (c.pdlcount, Qnil);
1048 return val;
1049 }
1050 c.next = catchlist;
1051 catchlist = &c;
db9f0278 1052
82da7701
JB
1053 h.var = var;
1054 h.handler = handlers;
db9f0278 1055 h.next = handlerlist;
db9f0278
JB
1056 h.tag = &c;
1057 handlerlist = &h;
1058
82da7701 1059 val = Feval (bodyform);
db9f0278
JB
1060 catchlist = c.next;
1061 handlerlist = h.next;
1062 return val;
1063}
1064
1065Lisp_Object
1066internal_condition_case (bfun, handlers, hfun)
1067 Lisp_Object (*bfun) ();
1068 Lisp_Object handlers;
1069 Lisp_Object (*hfun) ();
1070{
1071 Lisp_Object val;
1072 struct catchtag c;
1073 struct handler h;
1074
1075 c.tag = Qnil;
1076 c.val = Qnil;
1077 c.backlist = backtrace_list;
1078 c.handlerlist = handlerlist;
1079 c.lisp_eval_depth = lisp_eval_depth;
1080 c.pdlcount = specpdl_ptr - specpdl;
1081 c.poll_suppress_count = poll_suppress_count;
1082 c.gcpro = gcprolist;
1083 if (_setjmp (c.jmp))
1084 {
9d58218c 1085 return (*hfun) (c.val);
db9f0278
JB
1086 }
1087 c.next = catchlist;
1088 catchlist = &c;
1089 h.handler = handlers;
1090 h.var = Qnil;
db9f0278
JB
1091 h.next = handlerlist;
1092 h.tag = &c;
1093 handlerlist = &h;
1094
1095 val = (*bfun) ();
1096 catchlist = c.next;
1097 handlerlist = h.next;
1098 return val;
1099}
1100
d227775c
RS
1101Lisp_Object
1102internal_condition_case_1 (bfun, arg, handlers, hfun)
1103 Lisp_Object (*bfun) ();
1104 Lisp_Object arg;
1105 Lisp_Object handlers;
1106 Lisp_Object (*hfun) ();
1107{
1108 Lisp_Object val;
1109 struct catchtag c;
1110 struct handler h;
1111
1112 c.tag = Qnil;
1113 c.val = Qnil;
1114 c.backlist = backtrace_list;
1115 c.handlerlist = handlerlist;
1116 c.lisp_eval_depth = lisp_eval_depth;
1117 c.pdlcount = specpdl_ptr - specpdl;
1118 c.poll_suppress_count = poll_suppress_count;
1119 c.gcpro = gcprolist;
1120 if (_setjmp (c.jmp))
1121 {
9d58218c 1122 return (*hfun) (c.val);
d227775c
RS
1123 }
1124 c.next = catchlist;
1125 catchlist = &c;
1126 h.handler = handlers;
1127 h.var = Qnil;
1128 h.next = handlerlist;
1129 h.tag = &c;
1130 handlerlist = &h;
1131
1132 val = (*bfun) (arg);
1133 catchlist = c.next;
1134 handlerlist = h.next;
1135 return val;
1136}
1137\f
db9f0278
JB
1138static Lisp_Object find_handler_clause ();
1139
1140DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
4200e719 1141 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
db9f0278 1142This function does not return.\n\n\
4200e719 1143An error symbol is a symbol with an `error-conditions' property\n\
db9f0278
JB
1144that is a list of condition names.\n\
1145A handler for any of those names will get to handle this signal.\n\
1146The symbol `error' should normally be one of them.\n\
1147\n\
1148DATA should be a list. Its elements are printed as part of the error message.\n\
1149If the signal is handled, DATA is made available to the handler.\n\
1150See also the function `condition-case'.")
4200e719
RS
1151 (error_symbol, data)
1152 Lisp_Object error_symbol, data;
db9f0278
JB
1153{
1154 register struct handler *allhandlers = handlerlist;
1155 Lisp_Object conditions;
1156 extern int gc_in_progress;
1157 extern int waiting_for_input;
1158 Lisp_Object debugger_value;
1159
1160 quit_error_check ();
1161 immediate_quit = 0;
1162 if (gc_in_progress || waiting_for_input)
1163 abort ();
1164
e5d77022 1165#ifdef HAVE_X_WINDOWS
db9f0278 1166 TOTALLY_UNBLOCK_INPUT;
e5d77022 1167#endif
db9f0278 1168
4200e719 1169 conditions = Fget (error_symbol, Qerror_conditions);
db9f0278
JB
1170
1171 for (; handlerlist; handlerlist = handlerlist->next)
1172 {
1173 register Lisp_Object clause;
1174 clause = find_handler_clause (handlerlist->handler, conditions,
4200e719 1175 error_symbol, data, &debugger_value);
db9f0278
JB
1176
1177#if 0 /* Most callers are not prepared to handle gc if this returns.
1178 So, since this feature is not very useful, take it out. */
1179 /* If have called debugger and user wants to continue,
1180 just return nil. */
1181 if (EQ (clause, Qlambda))
1182 return debugger_value;
1183#else
1184 if (EQ (clause, Qlambda))
82da7701
JB
1185 {
1186 /* We can't return values to code which signalled an error, but we
1187 can continue code which has signalled a quit. */
4200e719 1188 if (EQ (error_symbol, Qquit))
82da7701
JB
1189 return Qnil;
1190 else
d3e6f8be 1191 error ("Cannot return from the debugger in an error");
82da7701 1192 }
db9f0278
JB
1193#endif
1194
265a9e55 1195 if (!NILP (clause))
db9f0278 1196 {
9d58218c 1197 Lisp_Object unwind_data;
db9f0278 1198 struct handler *h = handlerlist;
9d58218c 1199
db9f0278 1200 handlerlist = allhandlers;
9d58218c
RS
1201 if (data == memory_signal_data)
1202 unwind_data = memory_signal_data;
1203 else
1204 unwind_data = Fcons (error_symbol, data);
1205 h->chosen_clause = clause;
1206 unwind_to_catch (h->tag, unwind_data);
db9f0278
JB
1207 }
1208 }
1209
1210 handlerlist = allhandlers;
1211 /* If no handler is present now, try to run the debugger,
1212 and if that fails, throw to top level. */
4200e719 1213 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
db9f0278
JB
1214 Fthrow (Qtop_level, Qt);
1215}
1216
128c0f66
RM
1217/* Return nonzero iff LIST is a non-nil atom or
1218 a list containing one of CONDITIONS. */
1219
1220static int
1221wants_debugger (list, conditions)
1222 Lisp_Object list, conditions;
1223{
4de86b16 1224 if (NILP (list))
128c0f66
RM
1225 return 0;
1226 if (! CONSP (list))
1227 return 1;
1228
ab67260b 1229 while (CONSP (conditions))
128c0f66 1230 {
ab67260b
RS
1231 Lisp_Object this, tail;
1232 this = XCONS (conditions)->car;
1233 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
1234 if (EQ (XCONS (tail)->car, this))
128c0f66 1235 return 1;
128c0f66
RM
1236 conditions = XCONS (conditions)->cdr;
1237 }
ab67260b 1238 return 0;
128c0f66
RM
1239}
1240
1241/* Value of Qlambda means we have called debugger and user has continued.
1242 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
db9f0278
JB
1243
1244static Lisp_Object
1245find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1246 Lisp_Object handlers, conditions, sig, data;
1247 Lisp_Object *debugger_value_ptr;
1248{
1249 register Lisp_Object h;
1250 register Lisp_Object tem;
db9f0278
JB
1251
1252 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1253 return Qt;
1254 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1255 {
128c0f66 1256 if (wants_debugger (Vstack_trace_on_error, conditions))
db9f0278 1257 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
ba410f40
JB
1258 if ((EQ (sig, Qquit)
1259 ? debug_on_quit
1260 : wants_debugger (Vdebug_on_error, conditions))
1261 && when_entered_debugger < num_nonmacro_input_chars)
db9f0278
JB
1262 {
1263 int count = specpdl_ptr - specpdl;
1264 specbind (Qdebug_on_error, Qnil);
1265 *debugger_value_ptr =
1266 call_debugger (Fcons (Qerror,
1267 Fcons (Fcons (sig, data),
1268 Qnil)));
1269 return unbind_to (count, Qlambda);
1270 }
1271 return Qt;
1272 }
1273 for (h = handlers; CONSP (h); h = Fcdr (h))
1274 {
5f96776a
RS
1275 Lisp_Object handler, condit;
1276
1277 handler = Fcar (h);
1278 if (!CONSP (handler))
db9f0278 1279 continue;
5f96776a
RS
1280 condit = Fcar (handler);
1281 /* Handle a single condition name in handler HANDLER. */
1282 if (SYMBOLP (condit))
1283 {
1284 tem = Fmemq (Fcar (handler), conditions);
1285 if (!NILP (tem))
1286 return handler;
1287 }
1288 /* Handle a list of condition names in handler HANDLER. */
1289 else if (CONSP (condit))
1290 {
1291 while (CONSP (condit))
1292 {
1293 tem = Fmemq (Fcar (condit), conditions);
1294 if (!NILP (tem))
1295 return handler;
1296 condit = XCONS (condit)->cdr;
1297 }
1298 }
db9f0278
JB
1299 }
1300 return Qnil;
1301}
1302
1303/* dump an error message; called like printf */
1304
1305/* VARARGS 1 */
1306void
1307error (m, a1, a2, a3)
1308 char *m;
9125da08 1309 char *a1, *a2, *a3;
db9f0278
JB
1310{
1311 char buf[200];
9125da08
RS
1312 int size = 200;
1313 int mlen;
1314 char *buffer = buf;
1315 char *args[3];
1316 int allocated = 0;
1317 Lisp_Object string;
1318
1319 args[0] = a1;
1320 args[1] = a2;
1321 args[2] = a3;
1322
1323 mlen = strlen (m);
db9f0278
JB
1324
1325 while (1)
9125da08
RS
1326 {
1327 int used = doprnt (buf, size, m, m + mlen, 3, args);
1328 if (used < size)
1329 break;
1330 size *= 2;
1331 if (allocated)
1332 buffer = (char *) xrealloc (buffer, size);
1333 buffer = (char *) xmalloc (size);
1334 }
1335
1336 string = build_string (buf);
1337 if (allocated)
1338 free (buffer);
1339
1340 Fsignal (Qerror, Fcons (string, Qnil));
db9f0278
JB
1341}
1342\f
1343DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1344 "T if FUNCTION makes provisions for interactive calling.\n\
1345This means it contains a description for how to read arguments to give it.\n\
1346The value is nil for an invalid function or a symbol with no function\n\
1347definition.\n\
1348\n\
1349Interactively callable functions include strings and vectors (treated\n\
1350as keyboard macros), lambda-expressions that contain a top-level call\n\
1351to `interactive', autoload definitions made by `autoload' with non-nil\n\
1352fourth argument, and some of the built-in functions of Lisp.\n\
1353\n\
1354Also, a symbol satisfies `commandp' if its function definition does so.")
1355 (function)
1356 Lisp_Object function;
1357{
1358 register Lisp_Object fun;
1359 register Lisp_Object funcar;
1360 register Lisp_Object tem;
1361 register int i = 0;
1362
1363 fun = function;
1364
ffd56f97
JB
1365 fun = indirect_function (fun);
1366 if (EQ (fun, Qunbound))
1367 return Qnil;
db9f0278
JB
1368
1369 /* Emacs primitives are interactive if their DEFUN specifies an
1370 interactive spec. */
1371 if (XTYPE (fun) == Lisp_Subr)
1372 {
1373 if (XSUBR (fun)->prompt)
1374 return Qt;
1375 else
1376 return Qnil;
1377 }
1378
1379 /* Bytecode objects are interactive if they are long enough to
1380 have an element whose index is COMPILED_INTERACTIVE, which is
1381 where the interactive spec is stored. */
1382 else if (XTYPE (fun) == Lisp_Compiled)
1383 return (XVECTOR (fun)->size > COMPILED_INTERACTIVE
1384 ? Qt : Qnil);
1385
1386 /* Strings and vectors are keyboard macros. */
1387 if (XTYPE (fun) == Lisp_String
1388 || XTYPE (fun) == Lisp_Vector)
1389 return Qt;
1390
1391 /* Lists may represent commands. */
1392 if (!CONSP (fun))
1393 return Qnil;
1394 funcar = Fcar (fun);
1395 if (XTYPE (funcar) != Lisp_Symbol)
1396 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1397 if (EQ (funcar, Qlambda))
1398 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1399 if (EQ (funcar, Qmocklisp))
1400 return Qt; /* All mocklisp functions can be called interactively */
1401 if (EQ (funcar, Qautoload))
1402 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1403 else
1404 return Qnil;
1405}
1406
1407/* ARGSUSED */
1408DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1409 "Define FUNCTION to autoload from FILE.\n\
1410FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1411Third arg DOCSTRING is documentation for the function.\n\
1412Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
ee9ee63c
JB
1413Fifth arg TYPE indicates the type of the object:\n\
1414 nil or omitted says FUNCTION is a function,\n\
1415 `keymap' says FUNCTION is really a keymap, and\n\
1416 `macro' or t says FUNCTION is really a macro.\n\
db9f0278
JB
1417Third through fifth args give info about the real definition.\n\
1418They default to nil.\n\
1419If FUNCTION is already defined other than as an autoload,\n\
1420this does nothing and returns nil.")
ee9ee63c
JB
1421 (function, file, docstring, interactive, type)
1422 Lisp_Object function, file, docstring, interactive, type;
db9f0278
JB
1423{
1424#ifdef NO_ARG_ARRAY
1425 Lisp_Object args[4];
1426#endif
1427
1428 CHECK_SYMBOL (function, 0);
1429 CHECK_STRING (file, 1);
1430
1431 /* If function is defined and not as an autoload, don't override */
1432 if (!EQ (XSYMBOL (function)->function, Qunbound)
1433 && !(XTYPE (XSYMBOL (function)->function) == Lisp_Cons
1434 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1435 return Qnil;
1436
1437#ifdef NO_ARG_ARRAY
1438 args[0] = file;
1439 args[1] = docstring;
1440 args[2] = interactive;
ee9ee63c 1441 args[3] = type;
db9f0278
JB
1442
1443 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1444#else /* NO_ARG_ARRAY */
1445 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1446#endif /* not NO_ARG_ARRAY */
1447}
1448
1449Lisp_Object
1450un_autoload (oldqueue)
1451 Lisp_Object oldqueue;
1452{
1453 register Lisp_Object queue, first, second;
1454
1455 /* Queue to unwind is current value of Vautoload_queue.
1456 oldqueue is the shadowed value to leave in Vautoload_queue. */
1457 queue = Vautoload_queue;
1458 Vautoload_queue = oldqueue;
1459 while (CONSP (queue))
1460 {
1461 first = Fcar (queue);
1462 second = Fcdr (first);
1463 first = Fcar (first);
1464 if (EQ (second, Qnil))
1465 Vfeatures = first;
1466 else
1467 Ffset (first, second);
1468 queue = Fcdr (queue);
1469 }
1470 return Qnil;
1471}
1472
1473do_autoload (fundef, funname)
1474 Lisp_Object fundef, funname;
1475{
1476 int count = specpdl_ptr - specpdl;
2a49b6e5 1477 Lisp_Object fun, val, queue, first, second;
db9f0278
JB
1478
1479 fun = funname;
1480 CHECK_SYMBOL (funname, 0);
1481
1482 /* Value saved here is to be restored into Vautoload_queue */
1483 record_unwind_protect (un_autoload, Vautoload_queue);
1484 Vautoload_queue = Qt;
1485 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
2a49b6e5
RS
1486
1487 /* Save the old autoloads, in case we ever do an unload. */
1488 queue = Vautoload_queue;
1489 while (CONSP (queue))
1490 {
1491 first = Fcar (queue);
1492 second = Fcdr (first);
1493 first = Fcar (first);
5739ce6b
ER
1494
1495 /* Note: This test is subtle. The cdr of an autoload-queue entry
1496 may be an atom if the autoload entry was generated by a defalias
1497 or fset. */
1498 if (CONSP (second))
03e267c2 1499 Fput (first, Qautoload, (Fcdr (second)));
5739ce6b 1500
2a49b6e5
RS
1501 queue = Fcdr (queue);
1502 }
1503
db9f0278
JB
1504 /* Once loading finishes, don't undo it. */
1505 Vautoload_queue = Qt;
1506 unbind_to (count, Qnil);
1507
ffd56f97
JB
1508 fun = Findirect_function (fun);
1509
76c2b0cc 1510 if (!NILP (Fequal (fun, fundef)))
db9f0278
JB
1511 error ("Autoloading failed to define function %s",
1512 XSYMBOL (funname)->name->data);
1513}
1514\f
1515DEFUN ("eval", Feval, Seval, 1, 1, 0,
1516 "Evaluate FORM and return its value.")
1517 (form)
1518 Lisp_Object form;
1519{
1520 Lisp_Object fun, val, original_fun, original_args;
1521 Lisp_Object funcar;
1522 struct backtrace backtrace;
1523 struct gcpro gcpro1, gcpro2, gcpro3;
1524
1525 if (XTYPE (form) == Lisp_Symbol)
1526 {
1527 if (EQ (Vmocklisp_arguments, Qt))
1528 return Fsymbol_value (form);
1529 val = Fsymbol_value (form);
265a9e55 1530 if (NILP (val))
db9f0278
JB
1531 XFASTINT (val) = 0;
1532 else if (EQ (val, Qt))
1533 XFASTINT (val) = 1;
1534 return val;
1535 }
1536 if (!CONSP (form))
1537 return form;
1538
1539 QUIT;
1540 if (consing_since_gc > gc_cons_threshold)
1541 {
1542 GCPRO1 (form);
1543 Fgarbage_collect ();
1544 UNGCPRO;
1545 }
1546
1547 if (++lisp_eval_depth > max_lisp_eval_depth)
1548 {
1549 if (max_lisp_eval_depth < 100)
1550 max_lisp_eval_depth = 100;
1551 if (lisp_eval_depth > max_lisp_eval_depth)
1552 error ("Lisp nesting exceeds max-lisp-eval-depth");
1553 }
1554
1555 original_fun = Fcar (form);
1556 original_args = Fcdr (form);
1557
1558 backtrace.next = backtrace_list;
1559 backtrace_list = &backtrace;
1560 backtrace.function = &original_fun; /* This also protects them from gc */
1561 backtrace.args = &original_args;
1562 backtrace.nargs = UNEVALLED;
1563 backtrace.evalargs = 1;
1564 backtrace.debug_on_exit = 0;
1565
1566 if (debug_on_next_call)
1567 do_debug_on_call (Qt);
1568
1569 /* At this point, only original_fun and original_args
1570 have values that will be used below */
1571 retry:
ffd56f97 1572 fun = Findirect_function (original_fun);
db9f0278
JB
1573
1574 if (XTYPE (fun) == Lisp_Subr)
1575 {
1576 Lisp_Object numargs;
1577 Lisp_Object argvals[7];
1578 Lisp_Object args_left;
1579 register int i, maxargs;
1580
1581 args_left = original_args;
1582 numargs = Flength (args_left);
1583
1584 if (XINT (numargs) < XSUBR (fun)->min_args ||
1585 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1586 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1587
1588 if (XSUBR (fun)->max_args == UNEVALLED)
1589 {
1590 backtrace.evalargs = 0;
1591 val = (*XSUBR (fun)->function) (args_left);
1592 goto done;
1593 }
1594
1595 if (XSUBR (fun)->max_args == MANY)
1596 {
1597 /* Pass a vector of evaluated arguments */
1598 Lisp_Object *vals;
1599 register int argnum = 0;
1600
1601 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1602
1603 GCPRO3 (args_left, fun, fun);
1604 gcpro3.var = vals;
1605 gcpro3.nvars = 0;
1606
265a9e55 1607 while (!NILP (args_left))
db9f0278
JB
1608 {
1609 vals[argnum++] = Feval (Fcar (args_left));
1610 args_left = Fcdr (args_left);
1611 gcpro3.nvars = argnum;
1612 }
db9f0278
JB
1613
1614 backtrace.args = vals;
1615 backtrace.nargs = XINT (numargs);
1616
1617 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
a6e3fa71 1618 UNGCPRO;
db9f0278
JB
1619 goto done;
1620 }
1621
1622 GCPRO3 (args_left, fun, fun);
1623 gcpro3.var = argvals;
1624 gcpro3.nvars = 0;
1625
1626 maxargs = XSUBR (fun)->max_args;
1627 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1628 {
1629 argvals[i] = Feval (Fcar (args_left));
1630 gcpro3.nvars = ++i;
1631 }
1632
1633 UNGCPRO;
1634
1635 backtrace.args = argvals;
1636 backtrace.nargs = XINT (numargs);
1637
1638 switch (i)
1639 {
1640 case 0:
1641 val = (*XSUBR (fun)->function) ();
1642 goto done;
1643 case 1:
1644 val = (*XSUBR (fun)->function) (argvals[0]);
1645 goto done;
1646 case 2:
1647 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1648 goto done;
1649 case 3:
1650 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1651 argvals[2]);
1652 goto done;
1653 case 4:
1654 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1655 argvals[2], argvals[3]);
1656 goto done;
1657 case 5:
1658 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1659 argvals[3], argvals[4]);
1660 goto done;
1661 case 6:
1662 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1663 argvals[3], argvals[4], argvals[5]);
1664 goto done;
15c65264
RS
1665 case 7:
1666 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1667 argvals[3], argvals[4], argvals[5],
1668 argvals[6]);
1669 goto done;
db9f0278
JB
1670
1671 default:
08564963
JB
1672 /* Someone has created a subr that takes more arguments than
1673 is supported by this code. We need to either rewrite the
1674 subr to use a different argument protocol, or add more
1675 cases to this switch. */
1676 abort ();
db9f0278
JB
1677 }
1678 }
1679 if (XTYPE (fun) == Lisp_Compiled)
1680 val = apply_lambda (fun, original_args, 1);
1681 else
1682 {
1683 if (!CONSP (fun))
1684 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1685 funcar = Fcar (fun);
1686 if (XTYPE (funcar) != Lisp_Symbol)
1687 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1688 if (EQ (funcar, Qautoload))
1689 {
1690 do_autoload (fun, original_fun);
1691 goto retry;
1692 }
1693 if (EQ (funcar, Qmacro))
1694 val = Feval (apply1 (Fcdr (fun), original_args));
1695 else if (EQ (funcar, Qlambda))
1696 val = apply_lambda (fun, original_args, 1);
1697 else if (EQ (funcar, Qmocklisp))
1698 val = ml_apply (fun, original_args);
1699 else
1700 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1701 }
1702 done:
1703 if (!EQ (Vmocklisp_arguments, Qt))
1704 {
265a9e55 1705 if (NILP (val))
db9f0278
JB
1706 XFASTINT (val) = 0;
1707 else if (EQ (val, Qt))
1708 XFASTINT (val) = 1;
1709 }
1710 lisp_eval_depth--;
1711 if (backtrace.debug_on_exit)
1712 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1713 backtrace_list = backtrace.next;
1714 return val;
1715}
1716\f
1717DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1718 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1719Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1720 (nargs, args)
1721 int nargs;
1722 Lisp_Object *args;
1723{
1724 register int i, numargs;
1725 register Lisp_Object spread_arg;
1726 register Lisp_Object *funcall_args;
db9f0278 1727 Lisp_Object fun;
a6e3fa71 1728 struct gcpro gcpro1;
db9f0278
JB
1729
1730 fun = args [0];
1731 funcall_args = 0;
1732 spread_arg = args [nargs - 1];
1733 CHECK_LIST (spread_arg, nargs);
1734
1735 numargs = XINT (Flength (spread_arg));
1736
1737 if (numargs == 0)
1738 return Ffuncall (nargs - 1, args);
1739 else if (numargs == 1)
1740 {
1741 args [nargs - 1] = XCONS (spread_arg)->car;
1742 return Ffuncall (nargs, args);
1743 }
1744
a6e3fa71 1745 numargs += nargs - 2;
db9f0278 1746
ffd56f97
JB
1747 fun = indirect_function (fun);
1748 if (EQ (fun, Qunbound))
db9f0278 1749 {
ffd56f97
JB
1750 /* Let funcall get the error */
1751 fun = args[0];
1752 goto funcall;
db9f0278
JB
1753 }
1754
1755 if (XTYPE (fun) == Lisp_Subr)
1756 {
1757 if (numargs < XSUBR (fun)->min_args
1758 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1759 goto funcall; /* Let funcall get the error */
1760 else if (XSUBR (fun)->max_args > numargs)
1761 {
1762 /* Avoid making funcall cons up a yet another new vector of arguments
1763 by explicitly supplying nil's for optional values */
1764 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1765 * sizeof (Lisp_Object));
1766 for (i = numargs; i < XSUBR (fun)->max_args;)
1767 funcall_args[++i] = Qnil;
a6e3fa71
JB
1768 GCPRO1 (*funcall_args);
1769 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
db9f0278
JB
1770 }
1771 }
1772 funcall:
1773 /* We add 1 to numargs because funcall_args includes the
1774 function itself as well as its arguments. */
1775 if (!funcall_args)
a6e3fa71
JB
1776 {
1777 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1778 * sizeof (Lisp_Object));
1779 GCPRO1 (*funcall_args);
1780 gcpro1.nvars = 1 + numargs;
1781 }
1782
db9f0278
JB
1783 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1784 /* Spread the last arg we got. Its first element goes in
1785 the slot that it used to occupy, hence this value of I. */
1786 i = nargs - 1;
265a9e55 1787 while (!NILP (spread_arg))
db9f0278
JB
1788 {
1789 funcall_args [i++] = XCONS (spread_arg)->car;
1790 spread_arg = XCONS (spread_arg)->cdr;
1791 }
a6e3fa71
JB
1792
1793 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
db9f0278
JB
1794}
1795\f
1796/* Apply fn to arg */
1797Lisp_Object
1798apply1 (fn, arg)
1799 Lisp_Object fn, arg;
1800{
a6e3fa71
JB
1801 struct gcpro gcpro1;
1802
1803 GCPRO1 (fn);
265a9e55 1804 if (NILP (arg))
a6e3fa71
JB
1805 RETURN_UNGCPRO (Ffuncall (1, &fn));
1806 gcpro1.nvars = 2;
db9f0278
JB
1807#ifdef NO_ARG_ARRAY
1808 {
1809 Lisp_Object args[2];
1810 args[0] = fn;
1811 args[1] = arg;
a6e3fa71
JB
1812 gcpro1.var = args;
1813 RETURN_UNGCPRO (Fapply (2, args));
db9f0278
JB
1814 }
1815#else /* not NO_ARG_ARRAY */
a6e3fa71 1816 RETURN_UNGCPRO (Fapply (2, &fn));
db9f0278
JB
1817#endif /* not NO_ARG_ARRAY */
1818}
1819
1820/* Call function fn on no arguments */
1821Lisp_Object
1822call0 (fn)
1823 Lisp_Object fn;
1824{
a6e3fa71
JB
1825 struct gcpro gcpro1;
1826
1827 GCPRO1 (fn);
1828 RETURN_UNGCPRO (Ffuncall (1, &fn));
db9f0278
JB
1829}
1830
15285f9f 1831/* Call function fn with 1 argument arg1 */
db9f0278
JB
1832/* ARGSUSED */
1833Lisp_Object
15285f9f
RS
1834call1 (fn, arg1)
1835 Lisp_Object fn, arg1;
db9f0278 1836{
a6e3fa71 1837 struct gcpro gcpro1;
db9f0278 1838#ifdef NO_ARG_ARRAY
a6e3fa71
JB
1839 Lisp_Object args[2];
1840
db9f0278 1841 args[0] = fn;
15285f9f 1842 args[1] = arg1;
a6e3fa71
JB
1843 GCPRO1 (args[0]);
1844 gcpro1.nvars = 2;
1845 RETURN_UNGCPRO (Ffuncall (2, args));
db9f0278 1846#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1847 GCPRO1 (fn);
1848 gcpro1.nvars = 2;
1849 RETURN_UNGCPRO (Ffuncall (2, &fn));
db9f0278
JB
1850#endif /* not NO_ARG_ARRAY */
1851}
1852
15285f9f 1853/* Call function fn with 2 arguments arg1, arg2 */
db9f0278
JB
1854/* ARGSUSED */
1855Lisp_Object
15285f9f
RS
1856call2 (fn, arg1, arg2)
1857 Lisp_Object fn, arg1, arg2;
db9f0278 1858{
a6e3fa71 1859 struct gcpro gcpro1;
db9f0278
JB
1860#ifdef NO_ARG_ARRAY
1861 Lisp_Object args[3];
1862 args[0] = fn;
15285f9f
RS
1863 args[1] = arg1;
1864 args[2] = arg2;
a6e3fa71
JB
1865 GCPRO1 (args[0]);
1866 gcpro1.nvars = 3;
1867 RETURN_UNGCPRO (Ffuncall (3, args));
db9f0278 1868#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1869 GCPRO1 (fn);
1870 gcpro1.nvars = 3;
1871 RETURN_UNGCPRO (Ffuncall (3, &fn));
db9f0278
JB
1872#endif /* not NO_ARG_ARRAY */
1873}
1874
15285f9f 1875/* Call function fn with 3 arguments arg1, arg2, arg3 */
db9f0278
JB
1876/* ARGSUSED */
1877Lisp_Object
15285f9f
RS
1878call3 (fn, arg1, arg2, arg3)
1879 Lisp_Object fn, arg1, arg2, arg3;
db9f0278 1880{
a6e3fa71 1881 struct gcpro gcpro1;
db9f0278
JB
1882#ifdef NO_ARG_ARRAY
1883 Lisp_Object args[4];
1884 args[0] = fn;
15285f9f
RS
1885 args[1] = arg1;
1886 args[2] = arg2;
1887 args[3] = arg3;
a6e3fa71
JB
1888 GCPRO1 (args[0]);
1889 gcpro1.nvars = 4;
1890 RETURN_UNGCPRO (Ffuncall (4, args));
db9f0278 1891#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1892 GCPRO1 (fn);
1893 gcpro1.nvars = 4;
1894 RETURN_UNGCPRO (Ffuncall (4, &fn));
db9f0278
JB
1895#endif /* not NO_ARG_ARRAY */
1896}
1897
15285f9f 1898/* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
a5a44b91
JB
1899/* ARGSUSED */
1900Lisp_Object
15285f9f
RS
1901call4 (fn, arg1, arg2, arg3, arg4)
1902 Lisp_Object fn, arg1, arg2, arg3, arg4;
a5a44b91
JB
1903{
1904 struct gcpro gcpro1;
1905#ifdef NO_ARG_ARRAY
1906 Lisp_Object args[5];
1907 args[0] = fn;
15285f9f
RS
1908 args[1] = arg1;
1909 args[2] = arg2;
1910 args[3] = arg3;
1911 args[4] = arg4;
a5a44b91
JB
1912 GCPRO1 (args[0]);
1913 gcpro1.nvars = 5;
1914 RETURN_UNGCPRO (Ffuncall (5, args));
1915#else /* not NO_ARG_ARRAY */
1916 GCPRO1 (fn);
1917 gcpro1.nvars = 5;
1918 RETURN_UNGCPRO (Ffuncall (5, &fn));
1919#endif /* not NO_ARG_ARRAY */
1920}
1921
15285f9f
RS
1922/* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
1923/* ARGSUSED */
1924Lisp_Object
1925call5 (fn, arg1, arg2, arg3, arg4, arg5)
1926 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
1927{
1928 struct gcpro gcpro1;
1929#ifdef NO_ARG_ARRAY
1930 Lisp_Object args[6];
1931 args[0] = fn;
1932 args[1] = arg1;
1933 args[2] = arg2;
1934 args[3] = arg3;
1935 args[4] = arg4;
1936 args[5] = arg5;
1937 GCPRO1 (args[0]);
1938 gcpro1.nvars = 6;
1939 RETURN_UNGCPRO (Ffuncall (6, args));
1940#else /* not NO_ARG_ARRAY */
1941 GCPRO1 (fn);
1942 gcpro1.nvars = 6;
1943 RETURN_UNGCPRO (Ffuncall (6, &fn));
1944#endif /* not NO_ARG_ARRAY */
1945}
1946
1947/* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
1948/* ARGSUSED */
1949Lisp_Object
1950call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
1951 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
1952{
1953 struct gcpro gcpro1;
1954#ifdef NO_ARG_ARRAY
1955 Lisp_Object args[7];
1956 args[0] = fn;
1957 args[1] = arg1;
1958 args[2] = arg2;
1959 args[3] = arg3;
1960 args[4] = arg4;
1961 args[5] = arg5;
1962 args[6] = arg6;
1963 GCPRO1 (args[0]);
1964 gcpro1.nvars = 7;
1965 RETURN_UNGCPRO (Ffuncall (7, args));
1966#else /* not NO_ARG_ARRAY */
1967 GCPRO1 (fn);
1968 gcpro1.nvars = 7;
1969 RETURN_UNGCPRO (Ffuncall (7, &fn));
1970#endif /* not NO_ARG_ARRAY */
1971}
1972
db9f0278
JB
1973DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
1974 "Call first argument as a function, passing remaining arguments to it.\n\
1975Thus, (funcall 'cons 'x 'y) returns (x . y).")
1976 (nargs, args)
1977 int nargs;
1978 Lisp_Object *args;
1979{
1980 Lisp_Object fun;
1981 Lisp_Object funcar;
1982 int numargs = nargs - 1;
1983 Lisp_Object lisp_numargs;
1984 Lisp_Object val;
1985 struct backtrace backtrace;
1986 register Lisp_Object *internal_args;
1987 register int i;
1988
1989 QUIT;
1990 if (consing_since_gc > gc_cons_threshold)
a6e3fa71 1991 Fgarbage_collect ();
db9f0278
JB
1992
1993 if (++lisp_eval_depth > max_lisp_eval_depth)
1994 {
1995 if (max_lisp_eval_depth < 100)
1996 max_lisp_eval_depth = 100;
1997 if (lisp_eval_depth > max_lisp_eval_depth)
1998 error ("Lisp nesting exceeds max-lisp-eval-depth");
1999 }
2000
2001 backtrace.next = backtrace_list;
2002 backtrace_list = &backtrace;
2003 backtrace.function = &args[0];
2004 backtrace.args = &args[1];
2005 backtrace.nargs = nargs - 1;
2006 backtrace.evalargs = 0;
2007 backtrace.debug_on_exit = 0;
2008
2009 if (debug_on_next_call)
2010 do_debug_on_call (Qlambda);
2011
2012 retry:
2013
2014 fun = args[0];
ffd56f97
JB
2015
2016 fun = Findirect_function (fun);
db9f0278
JB
2017
2018 if (XTYPE (fun) == Lisp_Subr)
2019 {
2020 if (numargs < XSUBR (fun)->min_args
2021 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2022 {
2023 XFASTINT (lisp_numargs) = numargs;
2024 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2025 }
2026
2027 if (XSUBR (fun)->max_args == UNEVALLED)
2028 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2029
2030 if (XSUBR (fun)->max_args == MANY)
2031 {
2032 val = (*XSUBR (fun)->function) (numargs, args + 1);
2033 goto done;
2034 }
2035
2036 if (XSUBR (fun)->max_args > numargs)
2037 {
2038 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2039 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2040 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2041 internal_args[i] = Qnil;
2042 }
2043 else
2044 internal_args = args + 1;
2045 switch (XSUBR (fun)->max_args)
2046 {
2047 case 0:
2048 val = (*XSUBR (fun)->function) ();
2049 goto done;
2050 case 1:
2051 val = (*XSUBR (fun)->function) (internal_args[0]);
2052 goto done;
2053 case 2:
2054 val = (*XSUBR (fun)->function) (internal_args[0],
2055 internal_args[1]);
2056 goto done;
2057 case 3:
2058 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2059 internal_args[2]);
2060 goto done;
2061 case 4:
2062 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2063 internal_args[2],
2064 internal_args[3]);
2065 goto done;
2066 case 5:
2067 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2068 internal_args[2], internal_args[3],
2069 internal_args[4]);
2070 goto done;
2071 case 6:
2072 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2073 internal_args[2], internal_args[3],
2074 internal_args[4], internal_args[5]);
2075 goto done;
15c65264
RS
2076 case 7:
2077 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2078 internal_args[2], internal_args[3],
2079 internal_args[4], internal_args[5],
2080 internal_args[6]);
2081 goto done;
db9f0278
JB
2082
2083 default:
70ee42f7
JB
2084
2085 /* If a subr takes more than 6 arguments without using MANY
2086 or UNEVALLED, we need to extend this function to support it.
2087 Until this is done, there is no way to call the function. */
2088 abort ();
db9f0278
JB
2089 }
2090 }
2091 if (XTYPE (fun) == Lisp_Compiled)
2092 val = funcall_lambda (fun, numargs, args + 1);
2093 else
2094 {
2095 if (!CONSP (fun))
2096 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2097 funcar = Fcar (fun);
2098 if (XTYPE (funcar) != Lisp_Symbol)
2099 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2100 if (EQ (funcar, Qlambda))
2101 val = funcall_lambda (fun, numargs, args + 1);
2102 else if (EQ (funcar, Qmocklisp))
2103 val = ml_apply (fun, Flist (numargs, args + 1));
2104 else if (EQ (funcar, Qautoload))
2105 {
2106 do_autoload (fun, args[0]);
2107 goto retry;
2108 }
2109 else
2110 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2111 }
2112 done:
2113 lisp_eval_depth--;
2114 if (backtrace.debug_on_exit)
2115 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2116 backtrace_list = backtrace.next;
2117 return val;
2118}
2119\f
2120Lisp_Object
2121apply_lambda (fun, args, eval_flag)
2122 Lisp_Object fun, args;
2123 int eval_flag;
2124{
2125 Lisp_Object args_left;
2126 Lisp_Object numargs;
2127 register Lisp_Object *arg_vector;
2128 struct gcpro gcpro1, gcpro2, gcpro3;
2129 register int i;
2130 register Lisp_Object tem;
2131
2132 numargs = Flength (args);
2133 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2134 args_left = args;
2135
2136 GCPRO3 (*arg_vector, args_left, fun);
2137 gcpro1.nvars = 0;
2138
2139 for (i = 0; i < XINT (numargs);)
2140 {
2141 tem = Fcar (args_left), args_left = Fcdr (args_left);
2142 if (eval_flag) tem = Feval (tem);
2143 arg_vector[i++] = tem;
2144 gcpro1.nvars = i;
2145 }
2146
2147 UNGCPRO;
2148
2149 if (eval_flag)
2150 {
2151 backtrace_list->args = arg_vector;
2152 backtrace_list->nargs = i;
2153 }
2154 backtrace_list->evalargs = 0;
2155 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2156
2157 /* Do the debug-on-exit now, while arg_vector still exists. */
2158 if (backtrace_list->debug_on_exit)
2159 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2160 /* Don't do it again when we return to eval. */
2161 backtrace_list->debug_on_exit = 0;
2162 return tem;
2163}
2164
2165/* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2166 and return the result of evaluation.
2167 FUN must be either a lambda-expression or a compiled-code object. */
2168
2169Lisp_Object
2170funcall_lambda (fun, nargs, arg_vector)
2171 Lisp_Object fun;
2172 int nargs;
2173 register Lisp_Object *arg_vector;
2174{
2175 Lisp_Object val, tem;
2176 register Lisp_Object syms_left;
2177 Lisp_Object numargs;
2178 register Lisp_Object next;
2179 int count = specpdl_ptr - specpdl;
2180 register int i;
2181 int optional = 0, rest = 0;
2182
2183 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2184
2185 XFASTINT (numargs) = nargs;
2186
2187 if (XTYPE (fun) == Lisp_Cons)
2188 syms_left = Fcar (Fcdr (fun));
2189 else if (XTYPE (fun) == Lisp_Compiled)
2190 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2191 else abort ();
2192
2193 i = 0;
265a9e55 2194 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
db9f0278
JB
2195 {
2196 QUIT;
2197 next = Fcar (syms_left);
9ffa21d4
JB
2198 while (XTYPE (next) != Lisp_Symbol)
2199 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
db9f0278
JB
2200 if (EQ (next, Qand_rest))
2201 rest = 1;
2202 else if (EQ (next, Qand_optional))
2203 optional = 1;
2204 else if (rest)
2205 {
9ffa21d4 2206 specbind (next, Flist (nargs - i, &arg_vector[i]));
db9f0278
JB
2207 i = nargs;
2208 }
2209 else if (i < nargs)
2210 {
2211 tem = arg_vector[i++];
2212 specbind (next, tem);
2213 }
2214 else if (!optional)
2215 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2216 else
2217 specbind (next, Qnil);
2218 }
2219
2220 if (i < nargs)
2221 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2222
2223 if (XTYPE (fun) == Lisp_Cons)
2224 val = Fprogn (Fcdr (Fcdr (fun)));
2225 else
2226 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2227 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2228 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2229 return unbind_to (count, val);
2230}
2231\f
2232void
2233grow_specpdl ()
2234{
2235 register int count = specpdl_ptr - specpdl;
2236 if (specpdl_size >= max_specpdl_size)
2237 {
2238 if (max_specpdl_size < 400)
2239 max_specpdl_size = 400;
2240 if (specpdl_size >= max_specpdl_size)
2241 {
debee8fe
RS
2242 if (!NILP (Vdebug_on_error))
2243 /* Leave room for some specpdl in the debugger. */
2244 max_specpdl_size = specpdl_size + 100;
db9f0278
JB
2245 Fsignal (Qerror,
2246 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
db9f0278
JB
2247 }
2248 }
2249 specpdl_size *= 2;
2250 if (specpdl_size > max_specpdl_size)
2251 specpdl_size = max_specpdl_size;
2252 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2253 specpdl_ptr = specpdl + count;
2254}
2255
2256void
2257specbind (symbol, value)
2258 Lisp_Object symbol, value;
2259{
2260 extern void store_symval_forwarding (); /* in eval.c */
2261 Lisp_Object ovalue;
2262
9ffa21d4
JB
2263 CHECK_SYMBOL (symbol, 0);
2264
db9f0278
JB
2265 if (specpdl_ptr == specpdl + specpdl_size)
2266 grow_specpdl ();
2267 specpdl_ptr->symbol = symbol;
2268 specpdl_ptr->func = 0;
d0f7cdc8 2269 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
db9f0278
JB
2270 specpdl_ptr++;
2271 if (XTYPE (ovalue) == Lisp_Buffer_Objfwd)
2272 store_symval_forwarding (symbol, ovalue, value);
2273 else
2274 Fset (symbol, value);
2275}
2276
2277void
2278record_unwind_protect (function, arg)
2279 Lisp_Object (*function)();
2280 Lisp_Object arg;
2281{
2282 if (specpdl_ptr == specpdl + specpdl_size)
2283 grow_specpdl ();
2284 specpdl_ptr->func = function;
2285 specpdl_ptr->symbol = Qnil;
2286 specpdl_ptr->old_value = arg;
2287 specpdl_ptr++;
2288}
2289
2290Lisp_Object
2291unbind_to (count, value)
2292 int count;
2293 Lisp_Object value;
2294{
265a9e55 2295 int quitf = !NILP (Vquit_flag);
db9f0278
JB
2296 struct gcpro gcpro1;
2297
2298 GCPRO1 (value);
2299
2300 Vquit_flag = Qnil;
2301
2302 while (specpdl_ptr != specpdl + count)
2303 {
2304 --specpdl_ptr;
2305 if (specpdl_ptr->func != 0)
2306 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2307 /* Note that a "binding" of nil is really an unwind protect,
2308 so in that case the "old value" is a list of forms to evaluate. */
265a9e55 2309 else if (NILP (specpdl_ptr->symbol))
db9f0278
JB
2310 Fprogn (specpdl_ptr->old_value);
2311 else
2312 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2313 }
265a9e55 2314 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
db9f0278
JB
2315
2316 UNGCPRO;
2317
2318 return value;
2319}
2320\f
2321#if 0
2322
2323/* Get the value of symbol's global binding, even if that binding
2324 is not now dynamically visible. */
2325
2326Lisp_Object
2327top_level_value (symbol)
2328 Lisp_Object symbol;
2329{
2330 register struct specbinding *ptr = specpdl;
2331
2332 CHECK_SYMBOL (symbol, 0);
2333 for (; ptr != specpdl_ptr; ptr++)
2334 {
2335 if (EQ (ptr->symbol, symbol))
2336 return ptr->old_value;
2337 }
2338 return Fsymbol_value (symbol);
2339}
2340
2341Lisp_Object
2342top_level_set (symbol, newval)
2343 Lisp_Object symbol, newval;
2344{
2345 register struct specbinding *ptr = specpdl;
2346
2347 CHECK_SYMBOL (symbol, 0);
2348 for (; ptr != specpdl_ptr; ptr++)
2349 {
2350 if (EQ (ptr->symbol, symbol))
2351 {
2352 ptr->old_value = newval;
2353 return newval;
2354 }
2355 }
2356 return Fset (symbol, newval);
2357}
2358
2359#endif /* 0 */
2360\f
2361DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2362 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2363The debugger is entered when that frame exits, if the flag is non-nil.")
2364 (level, flag)
2365 Lisp_Object level, flag;
2366{
2367 register struct backtrace *backlist = backtrace_list;
2368 register int i;
2369
2370 CHECK_NUMBER (level, 0);
2371
2372 for (i = 0; backlist && i < XINT (level); i++)
2373 {
2374 backlist = backlist->next;
2375 }
2376
2377 if (backlist)
265a9e55 2378 backlist->debug_on_exit = !NILP (flag);
db9f0278
JB
2379
2380 return flag;
2381}
2382
2383DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2384 "Print a trace of Lisp function calls currently active.\n\
2385Output stream used is value of `standard-output'.")
2386 ()
2387{
2388 register struct backtrace *backlist = backtrace_list;
2389 register int i;
2390 Lisp_Object tail;
2391 Lisp_Object tem;
2392 extern Lisp_Object Vprint_level;
2393 struct gcpro gcpro1;
2394
db9f0278
JB
2395 XFASTINT (Vprint_level) = 3;
2396
2397 tail = Qnil;
2398 GCPRO1 (tail);
2399
2400 while (backlist)
2401 {
2402 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2403 if (backlist->nargs == UNEVALLED)
2404 {
2405 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2406 }
2407 else
2408 {
2409 tem = *backlist->function;
2410 Fprin1 (tem, Qnil); /* This can QUIT */
2411 write_string ("(", -1);
2412 if (backlist->nargs == MANY)
2413 {
2414 for (tail = *backlist->args, i = 0;
265a9e55 2415 !NILP (tail);
db9f0278
JB
2416 tail = Fcdr (tail), i++)
2417 {
2418 if (i) write_string (" ", -1);
2419 Fprin1 (Fcar (tail), Qnil);
2420 }
2421 }
2422 else
2423 {
2424 for (i = 0; i < backlist->nargs; i++)
2425 {
2426 if (i) write_string (" ", -1);
2427 Fprin1 (backlist->args[i], Qnil);
2428 }
2429 }
2430 }
2431 write_string (")\n", -1);
2432 backlist = backlist->next;
2433 }
2434
2435 Vprint_level = Qnil;
2436 UNGCPRO;
2437 return Qnil;
2438}
2439
2440DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2441 "Return the function and arguments N frames up from current execution point.\n\
2442If that frame has not evaluated the arguments yet (or is a special form),\n\
2443the value is (nil FUNCTION ARG-FORMS...).\n\
2444If that frame has evaluated its arguments and called its function already,\n\
2445the value is (t FUNCTION ARG-VALUES...).\n\
2446A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2447FUNCTION is whatever was supplied as car of evaluated list,\n\
2448or a lambda expression for macro calls.\n\
2449If N is more than the number of frames, the value is nil.")
2450 (nframes)
2451 Lisp_Object nframes;
2452{
2453 register struct backtrace *backlist = backtrace_list;
2454 register int i;
2455 Lisp_Object tem;
2456
2457 CHECK_NATNUM (nframes, 0);
2458
2459 /* Find the frame requested. */
2460 for (i = 0; i < XFASTINT (nframes); i++)
2461 backlist = backlist->next;
2462
2463 if (!backlist)
2464 return Qnil;
2465 if (backlist->nargs == UNEVALLED)
2466 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2467 else
2468 {
2469 if (backlist->nargs == MANY)
2470 tem = *backlist->args;
2471 else
2472 tem = Flist (backlist->nargs, backlist->args);
2473
2474 return Fcons (Qt, Fcons (*backlist->function, tem));
2475 }
2476}
2477\f
2478syms_of_eval ()
2479{
2480 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2481 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2482
2483 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2484 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2485This limit is to catch infinite recursions for you before they cause\n\
2486actual stack overflow in C, which would be fatal for Emacs.\n\
2487You can safely make it considerably larger than its default value,\n\
2488if that proves inconveniently small.");
2489
2490 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2491 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2492Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2493 Vquit_flag = Qnil;
2494
2495 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2496 "Non-nil inhibits C-g quitting from happening immediately.\n\
2497Note that `quit-flag' will still be set by typing C-g,\n\
2498so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2499To prevent this happening, set `quit-flag' to nil\n\
2500before making `inhibit-quit' nil.");
2501 Vinhibit_quit = Qnil;
2502
ad236261
JB
2503 Qinhibit_quit = intern ("inhibit-quit");
2504 staticpro (&Qinhibit_quit);
2505
db9f0278
JB
2506 Qautoload = intern ("autoload");
2507 staticpro (&Qautoload);
2508
2509 Qdebug_on_error = intern ("debug-on-error");
2510 staticpro (&Qdebug_on_error);
2511
2512 Qmacro = intern ("macro");
2513 staticpro (&Qmacro);
2514
2515 /* Note that the process handling also uses Qexit, but we don't want
2516 to staticpro it twice, so we just do it here. */
2517 Qexit = intern ("exit");
2518 staticpro (&Qexit);
2519
2520 Qinteractive = intern ("interactive");
2521 staticpro (&Qinteractive);
2522
2523 Qcommandp = intern ("commandp");
2524 staticpro (&Qcommandp);
2525
2526 Qdefun = intern ("defun");
2527 staticpro (&Qdefun);
2528
2529 Qand_rest = intern ("&rest");
2530 staticpro (&Qand_rest);
2531
2532 Qand_optional = intern ("&optional");
2533 staticpro (&Qand_optional);
2534
128c0f66 2535 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
db9f0278 2536 "*Non-nil means automatically display a backtrace buffer\n\
128c0f66
RM
2537after any error that is handled by the editor command loop.\n\
2538If the value is a list, an error only means to display a backtrace\n\
2539if one of its condition symbols appears in the list.");
2540 Vstack_trace_on_error = Qnil;
db9f0278 2541
128c0f66 2542 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
db9f0278
JB
2543 "*Non-nil means enter debugger if an error is signaled.\n\
2544Does not apply to errors handled by `condition-case'.\n\
128c0f66
RM
2545If the value is a list, an error only means to enter the debugger\n\
2546if one of its condition symbols appears in the list.\n\
db9f0278 2547See also variable `debug-on-quit'.");
128c0f66 2548 Vdebug_on_error = Qnil;
db9f0278
JB
2549
2550 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2551 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
1b7d8239 2552Does not apply if quit is handled by a `condition-case'.");
db9f0278
JB
2553 debug_on_quit = 0;
2554
2555 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2556 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2557
2558 DEFVAR_LISP ("debugger", &Vdebugger,
2559 "Function to call to invoke debugger.\n\
2560If due to frame exit, args are `exit' and the value being returned;\n\
2561 this function's value will be returned instead of that.\n\
2562If due to error, args are `error' and a list of the args to `signal'.\n\
2563If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2564If due to `eval' entry, one arg, t.");
2565 Vdebugger = Qnil;
2566
2567 Qmocklisp_arguments = intern ("mocklisp-arguments");
2568 staticpro (&Qmocklisp_arguments);
2569 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2570 "While in a mocklisp function, the list of its unevaluated args.");
2571 Vmocklisp_arguments = Qt;
2572
2573 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2574 "Set to the function `run-hooks', if that function has been defined.\n\
2575Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2576 Vrun_hooks = Qnil;
2577
2578 staticpro (&Vautoload_queue);
2579 Vautoload_queue = Qnil;
2580
2581 defsubr (&Sor);
2582 defsubr (&Sand);
2583 defsubr (&Sif);
2584 defsubr (&Scond);
2585 defsubr (&Sprogn);
2586 defsubr (&Sprog1);
2587 defsubr (&Sprog2);
2588 defsubr (&Ssetq);
2589 defsubr (&Squote);
2590 defsubr (&Sfunction);
2591 defsubr (&Sdefun);
2592 defsubr (&Sdefmacro);
2593 defsubr (&Sdefvar);
2594 defsubr (&Sdefconst);
2595 defsubr (&Suser_variable_p);
2596 defsubr (&Slet);
2597 defsubr (&SletX);
2598 defsubr (&Swhile);
2599 defsubr (&Smacroexpand);
2600 defsubr (&Scatch);
2601 defsubr (&Sthrow);
2602 defsubr (&Sunwind_protect);
2603 defsubr (&Scondition_case);
2604 defsubr (&Ssignal);
2605 defsubr (&Sinteractive_p);
2606 defsubr (&Scommandp);
2607 defsubr (&Sautoload);
2608 defsubr (&Seval);
2609 defsubr (&Sapply);
2610 defsubr (&Sfuncall);
2611 defsubr (&Sbacktrace_debug);
2612 defsubr (&Sbacktrace);
2613 defsubr (&Sbacktrace_frame);
2614}