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