(Fexpand_abbrev): Instead of Fcapitalize_region,
[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);
265a9e55 325 while (!NILP (args) && (tem = Fcar (args), XTYPE (tem) == Lisp_Symbol))
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);
487 if (XTYPE (fun) == Lisp_Subr
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);
db9f0278
JB
510 if (XTYPE (fun) == Lisp_Subr)
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);
639 if (XTYPE (documentation) == Lisp_Int && XINT (documentation) < 0)
640 return Qt;
641 if ((XTYPE (documentation) == Lisp_String) &&
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);
667 if (XTYPE (elt) == Lisp_Symbol)
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);
715 if (XTYPE (elt) == Lisp_Symbol)
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++];
732 if (XTYPE (elt) == Lisp_Symbol)
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. */
785 if (XTYPE (form) != Lisp_Cons)
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. */
23d6b5a6 792 while (XTYPE (def) == Lisp_Symbol)
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. */
811 if (EQ (def, Qunbound)
812 || XTYPE (def) != Lisp_Cons)
813 /* Not defined or definition not suitable */
814 break;
815 if (EQ (XCONS (def)->car, Qautoload))
816 {
817 /* Autoloading function: will it be a macro when loaded? */
ee9ee63c 818 tem = Fnth (make_number (4), def);
47ccd8b6 819 if (EQ (tem, Qt) || EQ (tem, Qmacro))
ee9ee63c
JB
820 /* Yes, load it and try again. */
821 {
822 do_autoload (def, sym);
823 continue;
824 }
825 else
db9f0278 826 break;
db9f0278
JB
827 }
828 else if (!EQ (XCONS (def)->car, Qmacro))
829 break;
830 else expander = XCONS (def)->cdr;
831 }
832 else
833 {
834 expander = XCONS (tem)->cdr;
265a9e55 835 if (NILP (expander))
db9f0278
JB
836 break;
837 }
db9f0278
JB
838 form = apply1 (expander, XCONS (form)->cdr);
839 }
840 return form;
841}
842\f
843DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
844 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
845TAG is evalled to get the tag to use. Then the BODY is executed.\n\
846Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
847If no throw happens, `catch' returns the value of the last BODY form.\n\
848If a throw happens, it specifies the value to return from `catch'.")
849 (args)
850 Lisp_Object args;
851{
852 register Lisp_Object tag;
853 struct gcpro gcpro1;
854
855 GCPRO1 (args);
856 tag = Feval (Fcar (args));
857 UNGCPRO;
858 return internal_catch (tag, Fprogn, Fcdr (args));
859}
860
861/* Set up a catch, then call C function FUNC on argument ARG.
862 FUNC should return a Lisp_Object.
863 This is how catches are done from within C code. */
864
865Lisp_Object
866internal_catch (tag, func, arg)
867 Lisp_Object tag;
868 Lisp_Object (*func) ();
869 Lisp_Object arg;
870{
871 /* This structure is made part of the chain `catchlist'. */
872 struct catchtag c;
873
874 /* Fill in the components of c, and put it on the list. */
875 c.next = catchlist;
876 c.tag = tag;
877 c.val = Qnil;
878 c.backlist = backtrace_list;
879 c.handlerlist = handlerlist;
880 c.lisp_eval_depth = lisp_eval_depth;
881 c.pdlcount = specpdl_ptr - specpdl;
882 c.poll_suppress_count = poll_suppress_count;
883 c.gcpro = gcprolist;
884 catchlist = &c;
885
886 /* Call FUNC. */
887 if (! _setjmp (c.jmp))
888 c.val = (*func) (arg);
889
890 /* Throw works by a longjmp that comes right here. */
891 catchlist = c.next;
892 return c.val;
893}
894
ba410f40
JB
895/* Unwind the specbind, catch, and handler stacks back to CATCH, and
896 jump to that CATCH, returning VALUE as the value of that catch.
db9f0278 897
ba410f40
JB
898 This is the guts Fthrow and Fsignal; they differ only in the way
899 they choose the catch tag to throw to. A catch tag for a
900 condition-case form has a TAG of Qnil.
db9f0278 901
ba410f40
JB
902 Before each catch is discarded, unbind all special bindings and
903 execute all unwind-protect clauses made above that catch. Unwind
904 the handler stack as we go, so that the proper handlers are in
905 effect for each unwind-protect clause we run. At the end, restore
906 some static info saved in CATCH, and longjmp to the location
907 specified in the
908
909 This is used for correct unwinding in Fthrow and Fsignal. */
db9f0278
JB
910
911static void
ba410f40 912unwind_to_catch (catch, value)
db9f0278 913 struct catchtag *catch;
ba410f40 914 Lisp_Object value;
db9f0278
JB
915{
916 register int last_time;
917
ba410f40
JB
918 /* Save the value in the tag. */
919 catch->val = value;
920
82da7701 921 /* Restore the polling-suppression count. */
1cdc3155 922 set_poll_suppress_count (catch->poll_suppress_count);
82da7701 923
db9f0278
JB
924 do
925 {
926 last_time = catchlist == catch;
82da7701
JB
927
928 /* Unwind the specpdl stack, and then restore the proper set of
929 handlers. */
db9f0278
JB
930 unbind_to (catchlist->pdlcount, Qnil);
931 handlerlist = catchlist->handlerlist;
932 catchlist = catchlist->next;
933 }
934 while (! last_time);
935
936 gcprolist = catch->gcpro;
937 backtrace_list = catch->backlist;
938 lisp_eval_depth = catch->lisp_eval_depth;
ba410f40
JB
939
940 _longjmp (catch->jmp, 1);
db9f0278
JB
941}
942
943DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
944 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
945Both TAG and VALUE are evalled.")
946 (tag, val)
947 register Lisp_Object tag, val;
948{
949 register struct catchtag *c;
950
951 while (1)
952 {
265a9e55 953 if (!NILP (tag))
db9f0278
JB
954 for (c = catchlist; c; c = c->next)
955 {
956 if (EQ (c->tag, tag))
ba410f40 957 unwind_to_catch (c, val);
db9f0278
JB
958 }
959 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (val, Qnil)));
960 }
961}
962
963
964DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
965 "Do BODYFORM, protecting with UNWINDFORMS.\n\
966Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
967If BODYFORM completes normally, its value is returned\n\
968after executing the UNWINDFORMS.\n\
969If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
970 (args)
971 Lisp_Object args;
972{
973 Lisp_Object val;
974 int count = specpdl_ptr - specpdl;
975
976 record_unwind_protect (0, Fcdr (args));
977 val = Feval (Fcar (args));
978 return unbind_to (count, val);
979}
980\f
981/* Chain of condition handlers currently in effect.
982 The elements of this chain are contained in the stack frames
983 of Fcondition_case and internal_condition_case.
984 When an error is signaled (by calling Fsignal, below),
985 this chain is searched for an element that applies. */
986
987struct handler *handlerlist;
988
989DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
990 "Regain control when an error is signaled.\n\
991Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
992executes BODYFORM and returns its value if no error happens.\n\
993Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
994where the BODY is made of Lisp expressions.\n\n\
995A handler is applicable to an error\n\
996if CONDITION-NAME is one of the error's condition names.\n\
997If an error happens, the first applicable handler is run.\n\
998\n\
633357d4
RS
999The car of a handler may be a list of condition names\n\
1000instead of a single condition name.\n\
1001\n\
db9f0278
JB
1002When a handler handles an error,\n\
1003control returns to the condition-case and the handler BODY... is executed\n\
1004with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1005VAR may be nil; then you do not get access to the signal information.\n\
1006\n\
1007The value of the last BODY form is returned from the condition-case.\n\
1008See also the function `signal' for more info.")
1009 (args)
1010 Lisp_Object args;
1011{
1012 Lisp_Object val;
1013 struct catchtag c;
1014 struct handler h;
82da7701 1015 register Lisp_Object var, bodyform, handlers;
db9f0278 1016
82da7701
JB
1017 var = Fcar (args);
1018 bodyform = Fcar (Fcdr (args));
1019 handlers = Fcdr (Fcdr (args));
1020 CHECK_SYMBOL (var, 0);
1021
1022 for (val = handlers; ! NILP (val); val = Fcdr (val))
1023 {
1024 Lisp_Object tem;
1025 tem = Fcar (val);
5f96776a
RS
1026 if (! (NILP (tem)
1027 || (CONSP (tem)
1028 && (SYMBOLP (XCONS (tem)->car)
1029 || CONSP (XCONS (tem)->car)))))
82da7701
JB
1030 error ("Invalid condition handler", tem);
1031 }
db9f0278
JB
1032
1033 c.tag = Qnil;
1034 c.val = Qnil;
1035 c.backlist = backtrace_list;
1036 c.handlerlist = handlerlist;
1037 c.lisp_eval_depth = lisp_eval_depth;
1038 c.pdlcount = specpdl_ptr - specpdl;
1039 c.poll_suppress_count = poll_suppress_count;
1040 c.gcpro = gcprolist;
1041 if (_setjmp (c.jmp))
1042 {
265a9e55 1043 if (!NILP (h.var))
9d58218c
RS
1044 specbind (h.var, c.val);
1045 val = Fprogn (Fcdr (h.chosen_clause));
82da7701
JB
1046
1047 /* Note that this just undoes the binding of h.var; whoever
1048 longjumped to us unwound the stack to c.pdlcount before
1049 throwing. */
db9f0278
JB
1050 unbind_to (c.pdlcount, Qnil);
1051 return val;
1052 }
1053 c.next = catchlist;
1054 catchlist = &c;
db9f0278 1055
82da7701
JB
1056 h.var = var;
1057 h.handler = handlers;
db9f0278 1058 h.next = handlerlist;
db9f0278
JB
1059 h.tag = &c;
1060 handlerlist = &h;
1061
82da7701 1062 val = Feval (bodyform);
db9f0278
JB
1063 catchlist = c.next;
1064 handlerlist = h.next;
1065 return val;
1066}
1067
1068Lisp_Object
1069internal_condition_case (bfun, handlers, hfun)
1070 Lisp_Object (*bfun) ();
1071 Lisp_Object handlers;
1072 Lisp_Object (*hfun) ();
1073{
1074 Lisp_Object val;
1075 struct catchtag c;
1076 struct handler h;
1077
1078 c.tag = Qnil;
1079 c.val = Qnil;
1080 c.backlist = backtrace_list;
1081 c.handlerlist = handlerlist;
1082 c.lisp_eval_depth = lisp_eval_depth;
1083 c.pdlcount = specpdl_ptr - specpdl;
1084 c.poll_suppress_count = poll_suppress_count;
1085 c.gcpro = gcprolist;
1086 if (_setjmp (c.jmp))
1087 {
9d58218c 1088 return (*hfun) (c.val);
db9f0278
JB
1089 }
1090 c.next = catchlist;
1091 catchlist = &c;
1092 h.handler = handlers;
1093 h.var = Qnil;
db9f0278
JB
1094 h.next = handlerlist;
1095 h.tag = &c;
1096 handlerlist = &h;
1097
1098 val = (*bfun) ();
1099 catchlist = c.next;
1100 handlerlist = h.next;
1101 return val;
1102}
1103
d227775c
RS
1104Lisp_Object
1105internal_condition_case_1 (bfun, arg, handlers, hfun)
1106 Lisp_Object (*bfun) ();
1107 Lisp_Object arg;
1108 Lisp_Object handlers;
1109 Lisp_Object (*hfun) ();
1110{
1111 Lisp_Object val;
1112 struct catchtag c;
1113 struct handler h;
1114
1115 c.tag = Qnil;
1116 c.val = Qnil;
1117 c.backlist = backtrace_list;
1118 c.handlerlist = handlerlist;
1119 c.lisp_eval_depth = lisp_eval_depth;
1120 c.pdlcount = specpdl_ptr - specpdl;
1121 c.poll_suppress_count = poll_suppress_count;
1122 c.gcpro = gcprolist;
1123 if (_setjmp (c.jmp))
1124 {
9d58218c 1125 return (*hfun) (c.val);
d227775c
RS
1126 }
1127 c.next = catchlist;
1128 catchlist = &c;
1129 h.handler = handlers;
1130 h.var = Qnil;
1131 h.next = handlerlist;
1132 h.tag = &c;
1133 handlerlist = &h;
1134
1135 val = (*bfun) (arg);
1136 catchlist = c.next;
1137 handlerlist = h.next;
1138 return val;
1139}
1140\f
db9f0278
JB
1141static Lisp_Object find_handler_clause ();
1142
1143DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
4200e719 1144 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
db9f0278 1145This function does not return.\n\n\
4200e719 1146An error symbol is a symbol with an `error-conditions' property\n\
db9f0278
JB
1147that is a list of condition names.\n\
1148A handler for any of those names will get to handle this signal.\n\
1149The symbol `error' should normally be one of them.\n\
1150\n\
1151DATA should be a list. Its elements are printed as part of the error message.\n\
1152If the signal is handled, DATA is made available to the handler.\n\
1153See also the function `condition-case'.")
4200e719
RS
1154 (error_symbol, data)
1155 Lisp_Object error_symbol, data;
db9f0278
JB
1156{
1157 register struct handler *allhandlers = handlerlist;
1158 Lisp_Object conditions;
1159 extern int gc_in_progress;
1160 extern int waiting_for_input;
1161 Lisp_Object debugger_value;
1162
1163 quit_error_check ();
1164 immediate_quit = 0;
1165 if (gc_in_progress || waiting_for_input)
1166 abort ();
1167
e5d77022 1168#ifdef HAVE_X_WINDOWS
db9f0278 1169 TOTALLY_UNBLOCK_INPUT;
e5d77022 1170#endif
db9f0278 1171
4200e719 1172 conditions = Fget (error_symbol, Qerror_conditions);
db9f0278
JB
1173
1174 for (; handlerlist; handlerlist = handlerlist->next)
1175 {
1176 register Lisp_Object clause;
1177 clause = find_handler_clause (handlerlist->handler, conditions,
4200e719 1178 error_symbol, data, &debugger_value);
db9f0278
JB
1179
1180#if 0 /* Most callers are not prepared to handle gc if this returns.
1181 So, since this feature is not very useful, take it out. */
1182 /* If have called debugger and user wants to continue,
1183 just return nil. */
1184 if (EQ (clause, Qlambda))
1185 return debugger_value;
1186#else
1187 if (EQ (clause, Qlambda))
82da7701
JB
1188 {
1189 /* We can't return values to code which signalled an error, but we
1190 can continue code which has signalled a quit. */
4200e719 1191 if (EQ (error_symbol, Qquit))
82da7701
JB
1192 return Qnil;
1193 else
d3e6f8be 1194 error ("Cannot return from the debugger in an error");
82da7701 1195 }
db9f0278
JB
1196#endif
1197
265a9e55 1198 if (!NILP (clause))
db9f0278 1199 {
9d58218c 1200 Lisp_Object unwind_data;
db9f0278 1201 struct handler *h = handlerlist;
9d58218c 1202
db9f0278 1203 handlerlist = allhandlers;
dff36a42 1204 if (EQ (data, memory_signal_data))
9d58218c
RS
1205 unwind_data = memory_signal_data;
1206 else
1207 unwind_data = Fcons (error_symbol, data);
1208 h->chosen_clause = clause;
1209 unwind_to_catch (h->tag, unwind_data);
db9f0278
JB
1210 }
1211 }
1212
1213 handlerlist = allhandlers;
1214 /* If no handler is present now, try to run the debugger,
1215 and if that fails, throw to top level. */
4200e719 1216 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
db9f0278
JB
1217 Fthrow (Qtop_level, Qt);
1218}
1219
128c0f66
RM
1220/* Return nonzero iff LIST is a non-nil atom or
1221 a list containing one of CONDITIONS. */
1222
1223static int
1224wants_debugger (list, conditions)
1225 Lisp_Object list, conditions;
1226{
4de86b16 1227 if (NILP (list))
128c0f66
RM
1228 return 0;
1229 if (! CONSP (list))
1230 return 1;
1231
ab67260b 1232 while (CONSP (conditions))
128c0f66 1233 {
ab67260b
RS
1234 Lisp_Object this, tail;
1235 this = XCONS (conditions)->car;
1236 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
1237 if (EQ (XCONS (tail)->car, this))
128c0f66 1238 return 1;
128c0f66
RM
1239 conditions = XCONS (conditions)->cdr;
1240 }
ab67260b 1241 return 0;
128c0f66
RM
1242}
1243
1244/* Value of Qlambda means we have called debugger and user has continued.
1245 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
db9f0278
JB
1246
1247static Lisp_Object
1248find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1249 Lisp_Object handlers, conditions, sig, data;
1250 Lisp_Object *debugger_value_ptr;
1251{
1252 register Lisp_Object h;
1253 register Lisp_Object tem;
db9f0278
JB
1254
1255 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1256 return Qt;
1257 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1258 {
128c0f66 1259 if (wants_debugger (Vstack_trace_on_error, conditions))
db9f0278 1260 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
ba410f40
JB
1261 if ((EQ (sig, Qquit)
1262 ? debug_on_quit
1263 : wants_debugger (Vdebug_on_error, conditions))
1264 && when_entered_debugger < num_nonmacro_input_chars)
db9f0278
JB
1265 {
1266 int count = specpdl_ptr - specpdl;
1267 specbind (Qdebug_on_error, Qnil);
1268 *debugger_value_ptr =
1269 call_debugger (Fcons (Qerror,
1270 Fcons (Fcons (sig, data),
1271 Qnil)));
1272 return unbind_to (count, Qlambda);
1273 }
1274 return Qt;
1275 }
1276 for (h = handlers; CONSP (h); h = Fcdr (h))
1277 {
5f96776a
RS
1278 Lisp_Object handler, condit;
1279
1280 handler = Fcar (h);
1281 if (!CONSP (handler))
db9f0278 1282 continue;
5f96776a
RS
1283 condit = Fcar (handler);
1284 /* Handle a single condition name in handler HANDLER. */
1285 if (SYMBOLP (condit))
1286 {
1287 tem = Fmemq (Fcar (handler), conditions);
1288 if (!NILP (tem))
1289 return handler;
1290 }
1291 /* Handle a list of condition names in handler HANDLER. */
1292 else if (CONSP (condit))
1293 {
1294 while (CONSP (condit))
1295 {
1296 tem = Fmemq (Fcar (condit), conditions);
1297 if (!NILP (tem))
1298 return handler;
1299 condit = XCONS (condit)->cdr;
1300 }
1301 }
db9f0278
JB
1302 }
1303 return Qnil;
1304}
1305
1306/* dump an error message; called like printf */
1307
1308/* VARARGS 1 */
1309void
1310error (m, a1, a2, a3)
1311 char *m;
9125da08 1312 char *a1, *a2, *a3;
db9f0278
JB
1313{
1314 char buf[200];
9125da08
RS
1315 int size = 200;
1316 int mlen;
1317 char *buffer = buf;
1318 char *args[3];
1319 int allocated = 0;
1320 Lisp_Object string;
1321
1322 args[0] = a1;
1323 args[1] = a2;
1324 args[2] = a3;
1325
1326 mlen = strlen (m);
db9f0278
JB
1327
1328 while (1)
9125da08
RS
1329 {
1330 int used = doprnt (buf, size, m, m + mlen, 3, args);
1331 if (used < size)
1332 break;
1333 size *= 2;
1334 if (allocated)
1335 buffer = (char *) xrealloc (buffer, size);
5ece1728
RS
1336 else
1337 {
1338 buffer = (char *) xmalloc (size);
1339 allocated = 1;
1340 }
9125da08
RS
1341 }
1342
1343 string = build_string (buf);
1344 if (allocated)
1345 free (buffer);
1346
1347 Fsignal (Qerror, Fcons (string, Qnil));
db9f0278
JB
1348}
1349\f
1350DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1351 "T if FUNCTION makes provisions for interactive calling.\n\
1352This means it contains a description for how to read arguments to give it.\n\
1353The value is nil for an invalid function or a symbol with no function\n\
1354definition.\n\
1355\n\
1356Interactively callable functions include strings and vectors (treated\n\
1357as keyboard macros), lambda-expressions that contain a top-level call\n\
1358to `interactive', autoload definitions made by `autoload' with non-nil\n\
1359fourth argument, and some of the built-in functions of Lisp.\n\
1360\n\
1361Also, a symbol satisfies `commandp' if its function definition does so.")
1362 (function)
1363 Lisp_Object function;
1364{
1365 register Lisp_Object fun;
1366 register Lisp_Object funcar;
1367 register Lisp_Object tem;
1368 register int i = 0;
1369
1370 fun = function;
1371
ffd56f97
JB
1372 fun = indirect_function (fun);
1373 if (EQ (fun, Qunbound))
1374 return Qnil;
db9f0278
JB
1375
1376 /* Emacs primitives are interactive if their DEFUN specifies an
1377 interactive spec. */
1378 if (XTYPE (fun) == Lisp_Subr)
1379 {
1380 if (XSUBR (fun)->prompt)
1381 return Qt;
1382 else
1383 return Qnil;
1384 }
1385
1386 /* Bytecode objects are interactive if they are long enough to
1387 have an element whose index is COMPILED_INTERACTIVE, which is
1388 where the interactive spec is stored. */
1389 else if (XTYPE (fun) == Lisp_Compiled)
1390 return (XVECTOR (fun)->size > COMPILED_INTERACTIVE
1391 ? Qt : Qnil);
1392
1393 /* Strings and vectors are keyboard macros. */
1394 if (XTYPE (fun) == Lisp_String
1395 || XTYPE (fun) == Lisp_Vector)
1396 return Qt;
1397
1398 /* Lists may represent commands. */
1399 if (!CONSP (fun))
1400 return Qnil;
1401 funcar = Fcar (fun);
1402 if (XTYPE (funcar) != Lisp_Symbol)
1403 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1404 if (EQ (funcar, Qlambda))
1405 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1406 if (EQ (funcar, Qmocklisp))
1407 return Qt; /* All mocklisp functions can be called interactively */
1408 if (EQ (funcar, Qautoload))
1409 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1410 else
1411 return Qnil;
1412}
1413
1414/* ARGSUSED */
1415DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1416 "Define FUNCTION to autoload from FILE.\n\
1417FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1418Third arg DOCSTRING is documentation for the function.\n\
1419Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
ee9ee63c
JB
1420Fifth arg TYPE indicates the type of the object:\n\
1421 nil or omitted says FUNCTION is a function,\n\
1422 `keymap' says FUNCTION is really a keymap, and\n\
1423 `macro' or t says FUNCTION is really a macro.\n\
db9f0278
JB
1424Third through fifth args give info about the real definition.\n\
1425They default to nil.\n\
1426If FUNCTION is already defined other than as an autoload,\n\
1427this does nothing and returns nil.")
ee9ee63c
JB
1428 (function, file, docstring, interactive, type)
1429 Lisp_Object function, file, docstring, interactive, type;
db9f0278
JB
1430{
1431#ifdef NO_ARG_ARRAY
1432 Lisp_Object args[4];
1433#endif
1434
1435 CHECK_SYMBOL (function, 0);
1436 CHECK_STRING (file, 1);
1437
1438 /* If function is defined and not as an autoload, don't override */
1439 if (!EQ (XSYMBOL (function)->function, Qunbound)
1440 && !(XTYPE (XSYMBOL (function)->function) == Lisp_Cons
1441 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1442 return Qnil;
1443
1444#ifdef NO_ARG_ARRAY
1445 args[0] = file;
1446 args[1] = docstring;
1447 args[2] = interactive;
ee9ee63c 1448 args[3] = type;
db9f0278
JB
1449
1450 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1451#else /* NO_ARG_ARRAY */
1452 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1453#endif /* not NO_ARG_ARRAY */
1454}
1455
1456Lisp_Object
1457un_autoload (oldqueue)
1458 Lisp_Object oldqueue;
1459{
1460 register Lisp_Object queue, first, second;
1461
1462 /* Queue to unwind is current value of Vautoload_queue.
1463 oldqueue is the shadowed value to leave in Vautoload_queue. */
1464 queue = Vautoload_queue;
1465 Vautoload_queue = oldqueue;
1466 while (CONSP (queue))
1467 {
1468 first = Fcar (queue);
1469 second = Fcdr (first);
1470 first = Fcar (first);
1471 if (EQ (second, Qnil))
1472 Vfeatures = first;
1473 else
1474 Ffset (first, second);
1475 queue = Fcdr (queue);
1476 }
1477 return Qnil;
1478}
1479
1480do_autoload (fundef, funname)
1481 Lisp_Object fundef, funname;
1482{
1483 int count = specpdl_ptr - specpdl;
2a49b6e5 1484 Lisp_Object fun, val, queue, first, second;
db9f0278
JB
1485
1486 fun = funname;
1487 CHECK_SYMBOL (funname, 0);
1488
1489 /* Value saved here is to be restored into Vautoload_queue */
1490 record_unwind_protect (un_autoload, Vautoload_queue);
1491 Vautoload_queue = Qt;
1492 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
2a49b6e5
RS
1493
1494 /* Save the old autoloads, in case we ever do an unload. */
1495 queue = Vautoload_queue;
1496 while (CONSP (queue))
1497 {
1498 first = Fcar (queue);
1499 second = Fcdr (first);
1500 first = Fcar (first);
5739ce6b
ER
1501
1502 /* Note: This test is subtle. The cdr of an autoload-queue entry
1503 may be an atom if the autoload entry was generated by a defalias
1504 or fset. */
1505 if (CONSP (second))
03e267c2 1506 Fput (first, Qautoload, (Fcdr (second)));
5739ce6b 1507
2a49b6e5
RS
1508 queue = Fcdr (queue);
1509 }
1510
db9f0278
JB
1511 /* Once loading finishes, don't undo it. */
1512 Vautoload_queue = Qt;
1513 unbind_to (count, Qnil);
1514
ffd56f97
JB
1515 fun = Findirect_function (fun);
1516
76c2b0cc 1517 if (!NILP (Fequal (fun, fundef)))
db9f0278
JB
1518 error ("Autoloading failed to define function %s",
1519 XSYMBOL (funname)->name->data);
1520}
1521\f
1522DEFUN ("eval", Feval, Seval, 1, 1, 0,
1523 "Evaluate FORM and return its value.")
1524 (form)
1525 Lisp_Object form;
1526{
1527 Lisp_Object fun, val, original_fun, original_args;
1528 Lisp_Object funcar;
1529 struct backtrace backtrace;
1530 struct gcpro gcpro1, gcpro2, gcpro3;
1531
1532 if (XTYPE (form) == Lisp_Symbol)
1533 {
1534 if (EQ (Vmocklisp_arguments, Qt))
1535 return Fsymbol_value (form);
1536 val = Fsymbol_value (form);
265a9e55 1537 if (NILP (val))
db9f0278
JB
1538 XFASTINT (val) = 0;
1539 else if (EQ (val, Qt))
1540 XFASTINT (val) = 1;
1541 return val;
1542 }
1543 if (!CONSP (form))
1544 return form;
1545
1546 QUIT;
1547 if (consing_since_gc > gc_cons_threshold)
1548 {
1549 GCPRO1 (form);
1550 Fgarbage_collect ();
1551 UNGCPRO;
1552 }
1553
1554 if (++lisp_eval_depth > max_lisp_eval_depth)
1555 {
1556 if (max_lisp_eval_depth < 100)
1557 max_lisp_eval_depth = 100;
1558 if (lisp_eval_depth > max_lisp_eval_depth)
1559 error ("Lisp nesting exceeds max-lisp-eval-depth");
1560 }
1561
1562 original_fun = Fcar (form);
1563 original_args = Fcdr (form);
1564
1565 backtrace.next = backtrace_list;
1566 backtrace_list = &backtrace;
1567 backtrace.function = &original_fun; /* This also protects them from gc */
1568 backtrace.args = &original_args;
1569 backtrace.nargs = UNEVALLED;
1570 backtrace.evalargs = 1;
1571 backtrace.debug_on_exit = 0;
1572
1573 if (debug_on_next_call)
1574 do_debug_on_call (Qt);
1575
1576 /* At this point, only original_fun and original_args
1577 have values that will be used below */
1578 retry:
ffd56f97 1579 fun = Findirect_function (original_fun);
db9f0278
JB
1580
1581 if (XTYPE (fun) == Lisp_Subr)
1582 {
1583 Lisp_Object numargs;
1584 Lisp_Object argvals[7];
1585 Lisp_Object args_left;
1586 register int i, maxargs;
1587
1588 args_left = original_args;
1589 numargs = Flength (args_left);
1590
1591 if (XINT (numargs) < XSUBR (fun)->min_args ||
1592 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1593 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1594
1595 if (XSUBR (fun)->max_args == UNEVALLED)
1596 {
1597 backtrace.evalargs = 0;
1598 val = (*XSUBR (fun)->function) (args_left);
1599 goto done;
1600 }
1601
1602 if (XSUBR (fun)->max_args == MANY)
1603 {
1604 /* Pass a vector of evaluated arguments */
1605 Lisp_Object *vals;
1606 register int argnum = 0;
1607
1608 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1609
1610 GCPRO3 (args_left, fun, fun);
1611 gcpro3.var = vals;
1612 gcpro3.nvars = 0;
1613
265a9e55 1614 while (!NILP (args_left))
db9f0278
JB
1615 {
1616 vals[argnum++] = Feval (Fcar (args_left));
1617 args_left = Fcdr (args_left);
1618 gcpro3.nvars = argnum;
1619 }
db9f0278
JB
1620
1621 backtrace.args = vals;
1622 backtrace.nargs = XINT (numargs);
1623
1624 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
a6e3fa71 1625 UNGCPRO;
db9f0278
JB
1626 goto done;
1627 }
1628
1629 GCPRO3 (args_left, fun, fun);
1630 gcpro3.var = argvals;
1631 gcpro3.nvars = 0;
1632
1633 maxargs = XSUBR (fun)->max_args;
1634 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1635 {
1636 argvals[i] = Feval (Fcar (args_left));
1637 gcpro3.nvars = ++i;
1638 }
1639
1640 UNGCPRO;
1641
1642 backtrace.args = argvals;
1643 backtrace.nargs = XINT (numargs);
1644
1645 switch (i)
1646 {
1647 case 0:
1648 val = (*XSUBR (fun)->function) ();
1649 goto done;
1650 case 1:
1651 val = (*XSUBR (fun)->function) (argvals[0]);
1652 goto done;
1653 case 2:
1654 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1655 goto done;
1656 case 3:
1657 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1658 argvals[2]);
1659 goto done;
1660 case 4:
1661 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1662 argvals[2], argvals[3]);
1663 goto done;
1664 case 5:
1665 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1666 argvals[3], argvals[4]);
1667 goto done;
1668 case 6:
1669 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1670 argvals[3], argvals[4], argvals[5]);
1671 goto done;
15c65264
RS
1672 case 7:
1673 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1674 argvals[3], argvals[4], argvals[5],
1675 argvals[6]);
1676 goto done;
db9f0278
JB
1677
1678 default:
08564963
JB
1679 /* Someone has created a subr that takes more arguments than
1680 is supported by this code. We need to either rewrite the
1681 subr to use a different argument protocol, or add more
1682 cases to this switch. */
1683 abort ();
db9f0278
JB
1684 }
1685 }
1686 if (XTYPE (fun) == Lisp_Compiled)
1687 val = apply_lambda (fun, original_args, 1);
1688 else
1689 {
1690 if (!CONSP (fun))
1691 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1692 funcar = Fcar (fun);
1693 if (XTYPE (funcar) != Lisp_Symbol)
1694 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1695 if (EQ (funcar, Qautoload))
1696 {
1697 do_autoload (fun, original_fun);
1698 goto retry;
1699 }
1700 if (EQ (funcar, Qmacro))
1701 val = Feval (apply1 (Fcdr (fun), original_args));
1702 else if (EQ (funcar, Qlambda))
1703 val = apply_lambda (fun, original_args, 1);
1704 else if (EQ (funcar, Qmocklisp))
1705 val = ml_apply (fun, original_args);
1706 else
1707 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1708 }
1709 done:
1710 if (!EQ (Vmocklisp_arguments, Qt))
1711 {
265a9e55 1712 if (NILP (val))
db9f0278
JB
1713 XFASTINT (val) = 0;
1714 else if (EQ (val, Qt))
1715 XFASTINT (val) = 1;
1716 }
1717 lisp_eval_depth--;
1718 if (backtrace.debug_on_exit)
1719 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1720 backtrace_list = backtrace.next;
1721 return val;
1722}
1723\f
1724DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1725 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1726Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1727 (nargs, args)
1728 int nargs;
1729 Lisp_Object *args;
1730{
1731 register int i, numargs;
1732 register Lisp_Object spread_arg;
1733 register Lisp_Object *funcall_args;
db9f0278 1734 Lisp_Object fun;
a6e3fa71 1735 struct gcpro gcpro1;
db9f0278
JB
1736
1737 fun = args [0];
1738 funcall_args = 0;
1739 spread_arg = args [nargs - 1];
1740 CHECK_LIST (spread_arg, nargs);
1741
1742 numargs = XINT (Flength (spread_arg));
1743
1744 if (numargs == 0)
1745 return Ffuncall (nargs - 1, args);
1746 else if (numargs == 1)
1747 {
1748 args [nargs - 1] = XCONS (spread_arg)->car;
1749 return Ffuncall (nargs, args);
1750 }
1751
a6e3fa71 1752 numargs += nargs - 2;
db9f0278 1753
ffd56f97
JB
1754 fun = indirect_function (fun);
1755 if (EQ (fun, Qunbound))
db9f0278 1756 {
ffd56f97
JB
1757 /* Let funcall get the error */
1758 fun = args[0];
1759 goto funcall;
db9f0278
JB
1760 }
1761
1762 if (XTYPE (fun) == Lisp_Subr)
1763 {
1764 if (numargs < XSUBR (fun)->min_args
1765 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1766 goto funcall; /* Let funcall get the error */
1767 else if (XSUBR (fun)->max_args > numargs)
1768 {
1769 /* Avoid making funcall cons up a yet another new vector of arguments
1770 by explicitly supplying nil's for optional values */
1771 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1772 * sizeof (Lisp_Object));
1773 for (i = numargs; i < XSUBR (fun)->max_args;)
1774 funcall_args[++i] = Qnil;
a6e3fa71
JB
1775 GCPRO1 (*funcall_args);
1776 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
db9f0278
JB
1777 }
1778 }
1779 funcall:
1780 /* We add 1 to numargs because funcall_args includes the
1781 function itself as well as its arguments. */
1782 if (!funcall_args)
a6e3fa71
JB
1783 {
1784 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1785 * sizeof (Lisp_Object));
1786 GCPRO1 (*funcall_args);
1787 gcpro1.nvars = 1 + numargs;
1788 }
1789
db9f0278
JB
1790 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1791 /* Spread the last arg we got. Its first element goes in
1792 the slot that it used to occupy, hence this value of I. */
1793 i = nargs - 1;
265a9e55 1794 while (!NILP (spread_arg))
db9f0278
JB
1795 {
1796 funcall_args [i++] = XCONS (spread_arg)->car;
1797 spread_arg = XCONS (spread_arg)->cdr;
1798 }
a6e3fa71
JB
1799
1800 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
db9f0278
JB
1801}
1802\f
1803/* Apply fn to arg */
1804Lisp_Object
1805apply1 (fn, arg)
1806 Lisp_Object fn, arg;
1807{
a6e3fa71
JB
1808 struct gcpro gcpro1;
1809
1810 GCPRO1 (fn);
265a9e55 1811 if (NILP (arg))
a6e3fa71
JB
1812 RETURN_UNGCPRO (Ffuncall (1, &fn));
1813 gcpro1.nvars = 2;
db9f0278
JB
1814#ifdef NO_ARG_ARRAY
1815 {
1816 Lisp_Object args[2];
1817 args[0] = fn;
1818 args[1] = arg;
a6e3fa71
JB
1819 gcpro1.var = args;
1820 RETURN_UNGCPRO (Fapply (2, args));
db9f0278
JB
1821 }
1822#else /* not NO_ARG_ARRAY */
a6e3fa71 1823 RETURN_UNGCPRO (Fapply (2, &fn));
db9f0278
JB
1824#endif /* not NO_ARG_ARRAY */
1825}
1826
1827/* Call function fn on no arguments */
1828Lisp_Object
1829call0 (fn)
1830 Lisp_Object fn;
1831{
a6e3fa71
JB
1832 struct gcpro gcpro1;
1833
1834 GCPRO1 (fn);
1835 RETURN_UNGCPRO (Ffuncall (1, &fn));
db9f0278
JB
1836}
1837
15285f9f 1838/* Call function fn with 1 argument arg1 */
db9f0278
JB
1839/* ARGSUSED */
1840Lisp_Object
15285f9f
RS
1841call1 (fn, arg1)
1842 Lisp_Object fn, arg1;
db9f0278 1843{
a6e3fa71 1844 struct gcpro gcpro1;
db9f0278 1845#ifdef NO_ARG_ARRAY
a6e3fa71
JB
1846 Lisp_Object args[2];
1847
db9f0278 1848 args[0] = fn;
15285f9f 1849 args[1] = arg1;
a6e3fa71
JB
1850 GCPRO1 (args[0]);
1851 gcpro1.nvars = 2;
1852 RETURN_UNGCPRO (Ffuncall (2, args));
db9f0278 1853#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1854 GCPRO1 (fn);
1855 gcpro1.nvars = 2;
1856 RETURN_UNGCPRO (Ffuncall (2, &fn));
db9f0278
JB
1857#endif /* not NO_ARG_ARRAY */
1858}
1859
15285f9f 1860/* Call function fn with 2 arguments arg1, arg2 */
db9f0278
JB
1861/* ARGSUSED */
1862Lisp_Object
15285f9f
RS
1863call2 (fn, arg1, arg2)
1864 Lisp_Object fn, arg1, arg2;
db9f0278 1865{
a6e3fa71 1866 struct gcpro gcpro1;
db9f0278
JB
1867#ifdef NO_ARG_ARRAY
1868 Lisp_Object args[3];
1869 args[0] = fn;
15285f9f
RS
1870 args[1] = arg1;
1871 args[2] = arg2;
a6e3fa71
JB
1872 GCPRO1 (args[0]);
1873 gcpro1.nvars = 3;
1874 RETURN_UNGCPRO (Ffuncall (3, args));
db9f0278 1875#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1876 GCPRO1 (fn);
1877 gcpro1.nvars = 3;
1878 RETURN_UNGCPRO (Ffuncall (3, &fn));
db9f0278
JB
1879#endif /* not NO_ARG_ARRAY */
1880}
1881
15285f9f 1882/* Call function fn with 3 arguments arg1, arg2, arg3 */
db9f0278
JB
1883/* ARGSUSED */
1884Lisp_Object
15285f9f
RS
1885call3 (fn, arg1, arg2, arg3)
1886 Lisp_Object fn, arg1, arg2, arg3;
db9f0278 1887{
a6e3fa71 1888 struct gcpro gcpro1;
db9f0278
JB
1889#ifdef NO_ARG_ARRAY
1890 Lisp_Object args[4];
1891 args[0] = fn;
15285f9f
RS
1892 args[1] = arg1;
1893 args[2] = arg2;
1894 args[3] = arg3;
a6e3fa71
JB
1895 GCPRO1 (args[0]);
1896 gcpro1.nvars = 4;
1897 RETURN_UNGCPRO (Ffuncall (4, args));
db9f0278 1898#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
1899 GCPRO1 (fn);
1900 gcpro1.nvars = 4;
1901 RETURN_UNGCPRO (Ffuncall (4, &fn));
db9f0278
JB
1902#endif /* not NO_ARG_ARRAY */
1903}
1904
15285f9f 1905/* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
a5a44b91
JB
1906/* ARGSUSED */
1907Lisp_Object
15285f9f
RS
1908call4 (fn, arg1, arg2, arg3, arg4)
1909 Lisp_Object fn, arg1, arg2, arg3, arg4;
a5a44b91
JB
1910{
1911 struct gcpro gcpro1;
1912#ifdef NO_ARG_ARRAY
1913 Lisp_Object args[5];
1914 args[0] = fn;
15285f9f
RS
1915 args[1] = arg1;
1916 args[2] = arg2;
1917 args[3] = arg3;
1918 args[4] = arg4;
a5a44b91
JB
1919 GCPRO1 (args[0]);
1920 gcpro1.nvars = 5;
1921 RETURN_UNGCPRO (Ffuncall (5, args));
1922#else /* not NO_ARG_ARRAY */
1923 GCPRO1 (fn);
1924 gcpro1.nvars = 5;
1925 RETURN_UNGCPRO (Ffuncall (5, &fn));
1926#endif /* not NO_ARG_ARRAY */
1927}
1928
15285f9f
RS
1929/* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
1930/* ARGSUSED */
1931Lisp_Object
1932call5 (fn, arg1, arg2, arg3, arg4, arg5)
1933 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
1934{
1935 struct gcpro gcpro1;
1936#ifdef NO_ARG_ARRAY
1937 Lisp_Object args[6];
1938 args[0] = fn;
1939 args[1] = arg1;
1940 args[2] = arg2;
1941 args[3] = arg3;
1942 args[4] = arg4;
1943 args[5] = arg5;
1944 GCPRO1 (args[0]);
1945 gcpro1.nvars = 6;
1946 RETURN_UNGCPRO (Ffuncall (6, args));
1947#else /* not NO_ARG_ARRAY */
1948 GCPRO1 (fn);
1949 gcpro1.nvars = 6;
1950 RETURN_UNGCPRO (Ffuncall (6, &fn));
1951#endif /* not NO_ARG_ARRAY */
1952}
1953
1954/* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
1955/* ARGSUSED */
1956Lisp_Object
1957call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
1958 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
1959{
1960 struct gcpro gcpro1;
1961#ifdef NO_ARG_ARRAY
1962 Lisp_Object args[7];
1963 args[0] = fn;
1964 args[1] = arg1;
1965 args[2] = arg2;
1966 args[3] = arg3;
1967 args[4] = arg4;
1968 args[5] = arg5;
1969 args[6] = arg6;
1970 GCPRO1 (args[0]);
1971 gcpro1.nvars = 7;
1972 RETURN_UNGCPRO (Ffuncall (7, args));
1973#else /* not NO_ARG_ARRAY */
1974 GCPRO1 (fn);
1975 gcpro1.nvars = 7;
1976 RETURN_UNGCPRO (Ffuncall (7, &fn));
1977#endif /* not NO_ARG_ARRAY */
1978}
1979
db9f0278
JB
1980DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
1981 "Call first argument as a function, passing remaining arguments to it.\n\
1982Thus, (funcall 'cons 'x 'y) returns (x . y).")
1983 (nargs, args)
1984 int nargs;
1985 Lisp_Object *args;
1986{
1987 Lisp_Object fun;
1988 Lisp_Object funcar;
1989 int numargs = nargs - 1;
1990 Lisp_Object lisp_numargs;
1991 Lisp_Object val;
1992 struct backtrace backtrace;
1993 register Lisp_Object *internal_args;
1994 register int i;
1995
1996 QUIT;
1997 if (consing_since_gc > gc_cons_threshold)
a6e3fa71 1998 Fgarbage_collect ();
db9f0278
JB
1999
2000 if (++lisp_eval_depth > max_lisp_eval_depth)
2001 {
2002 if (max_lisp_eval_depth < 100)
2003 max_lisp_eval_depth = 100;
2004 if (lisp_eval_depth > max_lisp_eval_depth)
2005 error ("Lisp nesting exceeds max-lisp-eval-depth");
2006 }
2007
2008 backtrace.next = backtrace_list;
2009 backtrace_list = &backtrace;
2010 backtrace.function = &args[0];
2011 backtrace.args = &args[1];
2012 backtrace.nargs = nargs - 1;
2013 backtrace.evalargs = 0;
2014 backtrace.debug_on_exit = 0;
2015
2016 if (debug_on_next_call)
2017 do_debug_on_call (Qlambda);
2018
2019 retry:
2020
2021 fun = args[0];
ffd56f97
JB
2022
2023 fun = Findirect_function (fun);
db9f0278
JB
2024
2025 if (XTYPE (fun) == Lisp_Subr)
2026 {
2027 if (numargs < XSUBR (fun)->min_args
2028 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2029 {
2030 XFASTINT (lisp_numargs) = numargs;
2031 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2032 }
2033
2034 if (XSUBR (fun)->max_args == UNEVALLED)
2035 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2036
2037 if (XSUBR (fun)->max_args == MANY)
2038 {
2039 val = (*XSUBR (fun)->function) (numargs, args + 1);
2040 goto done;
2041 }
2042
2043 if (XSUBR (fun)->max_args > numargs)
2044 {
2045 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2046 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2047 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2048 internal_args[i] = Qnil;
2049 }
2050 else
2051 internal_args = args + 1;
2052 switch (XSUBR (fun)->max_args)
2053 {
2054 case 0:
2055 val = (*XSUBR (fun)->function) ();
2056 goto done;
2057 case 1:
2058 val = (*XSUBR (fun)->function) (internal_args[0]);
2059 goto done;
2060 case 2:
2061 val = (*XSUBR (fun)->function) (internal_args[0],
2062 internal_args[1]);
2063 goto done;
2064 case 3:
2065 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2066 internal_args[2]);
2067 goto done;
2068 case 4:
2069 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2070 internal_args[2],
2071 internal_args[3]);
2072 goto done;
2073 case 5:
2074 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2075 internal_args[2], internal_args[3],
2076 internal_args[4]);
2077 goto done;
2078 case 6:
2079 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2080 internal_args[2], internal_args[3],
2081 internal_args[4], internal_args[5]);
2082 goto done;
15c65264
RS
2083 case 7:
2084 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2085 internal_args[2], internal_args[3],
2086 internal_args[4], internal_args[5],
2087 internal_args[6]);
2088 goto done;
db9f0278
JB
2089
2090 default:
70ee42f7
JB
2091
2092 /* If a subr takes more than 6 arguments without using MANY
2093 or UNEVALLED, we need to extend this function to support it.
2094 Until this is done, there is no way to call the function. */
2095 abort ();
db9f0278
JB
2096 }
2097 }
2098 if (XTYPE (fun) == Lisp_Compiled)
2099 val = funcall_lambda (fun, numargs, args + 1);
2100 else
2101 {
2102 if (!CONSP (fun))
2103 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2104 funcar = Fcar (fun);
2105 if (XTYPE (funcar) != Lisp_Symbol)
2106 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2107 if (EQ (funcar, Qlambda))
2108 val = funcall_lambda (fun, numargs, args + 1);
2109 else if (EQ (funcar, Qmocklisp))
2110 val = ml_apply (fun, Flist (numargs, args + 1));
2111 else if (EQ (funcar, Qautoload))
2112 {
2113 do_autoload (fun, args[0]);
2114 goto retry;
2115 }
2116 else
2117 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2118 }
2119 done:
2120 lisp_eval_depth--;
2121 if (backtrace.debug_on_exit)
2122 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2123 backtrace_list = backtrace.next;
2124 return val;
2125}
2126\f
2127Lisp_Object
2128apply_lambda (fun, args, eval_flag)
2129 Lisp_Object fun, args;
2130 int eval_flag;
2131{
2132 Lisp_Object args_left;
2133 Lisp_Object numargs;
2134 register Lisp_Object *arg_vector;
2135 struct gcpro gcpro1, gcpro2, gcpro3;
2136 register int i;
2137 register Lisp_Object tem;
2138
2139 numargs = Flength (args);
2140 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2141 args_left = args;
2142
2143 GCPRO3 (*arg_vector, args_left, fun);
2144 gcpro1.nvars = 0;
2145
2146 for (i = 0; i < XINT (numargs);)
2147 {
2148 tem = Fcar (args_left), args_left = Fcdr (args_left);
2149 if (eval_flag) tem = Feval (tem);
2150 arg_vector[i++] = tem;
2151 gcpro1.nvars = i;
2152 }
2153
2154 UNGCPRO;
2155
2156 if (eval_flag)
2157 {
2158 backtrace_list->args = arg_vector;
2159 backtrace_list->nargs = i;
2160 }
2161 backtrace_list->evalargs = 0;
2162 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2163
2164 /* Do the debug-on-exit now, while arg_vector still exists. */
2165 if (backtrace_list->debug_on_exit)
2166 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2167 /* Don't do it again when we return to eval. */
2168 backtrace_list->debug_on_exit = 0;
2169 return tem;
2170}
2171
2172/* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2173 and return the result of evaluation.
2174 FUN must be either a lambda-expression or a compiled-code object. */
2175
2176Lisp_Object
2177funcall_lambda (fun, nargs, arg_vector)
2178 Lisp_Object fun;
2179 int nargs;
2180 register Lisp_Object *arg_vector;
2181{
2182 Lisp_Object val, tem;
2183 register Lisp_Object syms_left;
2184 Lisp_Object numargs;
2185 register Lisp_Object next;
2186 int count = specpdl_ptr - specpdl;
2187 register int i;
2188 int optional = 0, rest = 0;
2189
2190 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2191
2192 XFASTINT (numargs) = nargs;
2193
2194 if (XTYPE (fun) == Lisp_Cons)
2195 syms_left = Fcar (Fcdr (fun));
2196 else if (XTYPE (fun) == Lisp_Compiled)
2197 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2198 else abort ();
2199
2200 i = 0;
265a9e55 2201 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
db9f0278
JB
2202 {
2203 QUIT;
2204 next = Fcar (syms_left);
9ffa21d4
JB
2205 while (XTYPE (next) != Lisp_Symbol)
2206 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
db9f0278
JB
2207 if (EQ (next, Qand_rest))
2208 rest = 1;
2209 else if (EQ (next, Qand_optional))
2210 optional = 1;
2211 else if (rest)
2212 {
9ffa21d4 2213 specbind (next, Flist (nargs - i, &arg_vector[i]));
db9f0278
JB
2214 i = nargs;
2215 }
2216 else if (i < nargs)
2217 {
2218 tem = arg_vector[i++];
2219 specbind (next, tem);
2220 }
2221 else if (!optional)
2222 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2223 else
2224 specbind (next, Qnil);
2225 }
2226
2227 if (i < nargs)
2228 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2229
2230 if (XTYPE (fun) == Lisp_Cons)
2231 val = Fprogn (Fcdr (Fcdr (fun)));
2232 else
2233 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2234 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2235 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2236 return unbind_to (count, val);
2237}
2238\f
2239void
2240grow_specpdl ()
2241{
2242 register int count = specpdl_ptr - specpdl;
2243 if (specpdl_size >= max_specpdl_size)
2244 {
2245 if (max_specpdl_size < 400)
2246 max_specpdl_size = 400;
2247 if (specpdl_size >= max_specpdl_size)
2248 {
debee8fe
RS
2249 if (!NILP (Vdebug_on_error))
2250 /* Leave room for some specpdl in the debugger. */
2251 max_specpdl_size = specpdl_size + 100;
db9f0278
JB
2252 Fsignal (Qerror,
2253 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
db9f0278
JB
2254 }
2255 }
2256 specpdl_size *= 2;
2257 if (specpdl_size > max_specpdl_size)
2258 specpdl_size = max_specpdl_size;
2259 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2260 specpdl_ptr = specpdl + count;
2261}
2262
2263void
2264specbind (symbol, value)
2265 Lisp_Object symbol, value;
2266{
2267 extern void store_symval_forwarding (); /* in eval.c */
2268 Lisp_Object ovalue;
2269
9ffa21d4
JB
2270 CHECK_SYMBOL (symbol, 0);
2271
db9f0278
JB
2272 if (specpdl_ptr == specpdl + specpdl_size)
2273 grow_specpdl ();
2274 specpdl_ptr->symbol = symbol;
2275 specpdl_ptr->func = 0;
d0f7cdc8 2276 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
db9f0278
JB
2277 specpdl_ptr++;
2278 if (XTYPE (ovalue) == Lisp_Buffer_Objfwd)
2279 store_symval_forwarding (symbol, ovalue, value);
2280 else
2281 Fset (symbol, value);
2282}
2283
2284void
2285record_unwind_protect (function, arg)
2286 Lisp_Object (*function)();
2287 Lisp_Object arg;
2288{
2289 if (specpdl_ptr == specpdl + specpdl_size)
2290 grow_specpdl ();
2291 specpdl_ptr->func = function;
2292 specpdl_ptr->symbol = Qnil;
2293 specpdl_ptr->old_value = arg;
2294 specpdl_ptr++;
2295}
2296
2297Lisp_Object
2298unbind_to (count, value)
2299 int count;
2300 Lisp_Object value;
2301{
265a9e55 2302 int quitf = !NILP (Vquit_flag);
db9f0278
JB
2303 struct gcpro gcpro1;
2304
2305 GCPRO1 (value);
2306
2307 Vquit_flag = Qnil;
2308
2309 while (specpdl_ptr != specpdl + count)
2310 {
2311 --specpdl_ptr;
2312 if (specpdl_ptr->func != 0)
2313 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2314 /* Note that a "binding" of nil is really an unwind protect,
2315 so in that case the "old value" is a list of forms to evaluate. */
265a9e55 2316 else if (NILP (specpdl_ptr->symbol))
db9f0278
JB
2317 Fprogn (specpdl_ptr->old_value);
2318 else
2319 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2320 }
265a9e55 2321 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
db9f0278
JB
2322
2323 UNGCPRO;
2324
2325 return value;
2326}
2327\f
2328#if 0
2329
2330/* Get the value of symbol's global binding, even if that binding
2331 is not now dynamically visible. */
2332
2333Lisp_Object
2334top_level_value (symbol)
2335 Lisp_Object symbol;
2336{
2337 register struct specbinding *ptr = specpdl;
2338
2339 CHECK_SYMBOL (symbol, 0);
2340 for (; ptr != specpdl_ptr; ptr++)
2341 {
2342 if (EQ (ptr->symbol, symbol))
2343 return ptr->old_value;
2344 }
2345 return Fsymbol_value (symbol);
2346}
2347
2348Lisp_Object
2349top_level_set (symbol, newval)
2350 Lisp_Object symbol, newval;
2351{
2352 register struct specbinding *ptr = specpdl;
2353
2354 CHECK_SYMBOL (symbol, 0);
2355 for (; ptr != specpdl_ptr; ptr++)
2356 {
2357 if (EQ (ptr->symbol, symbol))
2358 {
2359 ptr->old_value = newval;
2360 return newval;
2361 }
2362 }
2363 return Fset (symbol, newval);
2364}
2365
2366#endif /* 0 */
2367\f
2368DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2369 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2370The debugger is entered when that frame exits, if the flag is non-nil.")
2371 (level, flag)
2372 Lisp_Object level, flag;
2373{
2374 register struct backtrace *backlist = backtrace_list;
2375 register int i;
2376
2377 CHECK_NUMBER (level, 0);
2378
2379 for (i = 0; backlist && i < XINT (level); i++)
2380 {
2381 backlist = backlist->next;
2382 }
2383
2384 if (backlist)
265a9e55 2385 backlist->debug_on_exit = !NILP (flag);
db9f0278
JB
2386
2387 return flag;
2388}
2389
2390DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2391 "Print a trace of Lisp function calls currently active.\n\
2392Output stream used is value of `standard-output'.")
2393 ()
2394{
2395 register struct backtrace *backlist = backtrace_list;
2396 register int i;
2397 Lisp_Object tail;
2398 Lisp_Object tem;
2399 extern Lisp_Object Vprint_level;
2400 struct gcpro gcpro1;
2401
db9f0278
JB
2402 XFASTINT (Vprint_level) = 3;
2403
2404 tail = Qnil;
2405 GCPRO1 (tail);
2406
2407 while (backlist)
2408 {
2409 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2410 if (backlist->nargs == UNEVALLED)
2411 {
2412 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
b6703b02 2413 write_string ("\n", -1);
db9f0278
JB
2414 }
2415 else
2416 {
2417 tem = *backlist->function;
2418 Fprin1 (tem, Qnil); /* This can QUIT */
2419 write_string ("(", -1);
2420 if (backlist->nargs == MANY)
2421 {
2422 for (tail = *backlist->args, i = 0;
265a9e55 2423 !NILP (tail);
db9f0278
JB
2424 tail = Fcdr (tail), i++)
2425 {
2426 if (i) write_string (" ", -1);
2427 Fprin1 (Fcar (tail), Qnil);
2428 }
2429 }
2430 else
2431 {
2432 for (i = 0; i < backlist->nargs; i++)
2433 {
2434 if (i) write_string (" ", -1);
2435 Fprin1 (backlist->args[i], Qnil);
2436 }
2437 }
b6703b02 2438 write_string (")\n", -1);
db9f0278 2439 }
db9f0278
JB
2440 backlist = backlist->next;
2441 }
2442
2443 Vprint_level = Qnil;
2444 UNGCPRO;
2445 return Qnil;
2446}
2447
2448DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2449 "Return the function and arguments N frames up from current execution point.\n\
2450If that frame has not evaluated the arguments yet (or is a special form),\n\
2451the value is (nil FUNCTION ARG-FORMS...).\n\
2452If that frame has evaluated its arguments and called its function already,\n\
2453the value is (t FUNCTION ARG-VALUES...).\n\
2454A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2455FUNCTION is whatever was supplied as car of evaluated list,\n\
2456or a lambda expression for macro calls.\n\
2457If N is more than the number of frames, the value is nil.")
2458 (nframes)
2459 Lisp_Object nframes;
2460{
2461 register struct backtrace *backlist = backtrace_list;
2462 register int i;
2463 Lisp_Object tem;
2464
2465 CHECK_NATNUM (nframes, 0);
2466
2467 /* Find the frame requested. */
b6703b02 2468 for (i = 0; backlist && i < XFASTINT (nframes); i++)
db9f0278
JB
2469 backlist = backlist->next;
2470
2471 if (!backlist)
2472 return Qnil;
2473 if (backlist->nargs == UNEVALLED)
2474 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2475 else
2476 {
2477 if (backlist->nargs == MANY)
2478 tem = *backlist->args;
2479 else
2480 tem = Flist (backlist->nargs, backlist->args);
2481
2482 return Fcons (Qt, Fcons (*backlist->function, tem));
2483 }
2484}
2485\f
2486syms_of_eval ()
2487{
2488 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2489 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2490
2491 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2492 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2493This limit is to catch infinite recursions for you before they cause\n\
2494actual stack overflow in C, which would be fatal for Emacs.\n\
2495You can safely make it considerably larger than its default value,\n\
2496if that proves inconveniently small.");
2497
2498 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2499 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
d0b68896 2500Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
db9f0278
JB
2501 Vquit_flag = Qnil;
2502
2503 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2504 "Non-nil inhibits C-g quitting from happening immediately.\n\
2505Note that `quit-flag' will still be set by typing C-g,\n\
2506so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2507To prevent this happening, set `quit-flag' to nil\n\
2508before making `inhibit-quit' nil.");
2509 Vinhibit_quit = Qnil;
2510
ad236261
JB
2511 Qinhibit_quit = intern ("inhibit-quit");
2512 staticpro (&Qinhibit_quit);
2513
db9f0278
JB
2514 Qautoload = intern ("autoload");
2515 staticpro (&Qautoload);
2516
2517 Qdebug_on_error = intern ("debug-on-error");
2518 staticpro (&Qdebug_on_error);
2519
2520 Qmacro = intern ("macro");
2521 staticpro (&Qmacro);
2522
2523 /* Note that the process handling also uses Qexit, but we don't want
2524 to staticpro it twice, so we just do it here. */
2525 Qexit = intern ("exit");
2526 staticpro (&Qexit);
2527
2528 Qinteractive = intern ("interactive");
2529 staticpro (&Qinteractive);
2530
2531 Qcommandp = intern ("commandp");
2532 staticpro (&Qcommandp);
2533
2534 Qdefun = intern ("defun");
2535 staticpro (&Qdefun);
2536
2537 Qand_rest = intern ("&rest");
2538 staticpro (&Qand_rest);
2539
2540 Qand_optional = intern ("&optional");
2541 staticpro (&Qand_optional);
2542
128c0f66 2543 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
db9f0278 2544 "*Non-nil means automatically display a backtrace buffer\n\
128c0f66
RM
2545after any error that is handled by the editor command loop.\n\
2546If the value is a list, an error only means to display a backtrace\n\
2547if one of its condition symbols appears in the list.");
2548 Vstack_trace_on_error = Qnil;
db9f0278 2549
128c0f66 2550 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
db9f0278
JB
2551 "*Non-nil means enter debugger if an error is signaled.\n\
2552Does not apply to errors handled by `condition-case'.\n\
128c0f66
RM
2553If the value is a list, an error only means to enter the debugger\n\
2554if one of its condition symbols appears in the list.\n\
db9f0278 2555See also variable `debug-on-quit'.");
128c0f66 2556 Vdebug_on_error = Qnil;
db9f0278
JB
2557
2558 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
d0b68896 2559 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
1b7d8239 2560Does not apply if quit is handled by a `condition-case'.");
db9f0278
JB
2561 debug_on_quit = 0;
2562
2563 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2564 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2565
2566 DEFVAR_LISP ("debugger", &Vdebugger,
2567 "Function to call to invoke debugger.\n\
2568If due to frame exit, args are `exit' and the value being returned;\n\
2569 this function's value will be returned instead of that.\n\
2570If due to error, args are `error' and a list of the args to `signal'.\n\
2571If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2572If due to `eval' entry, one arg, t.");
2573 Vdebugger = Qnil;
2574
2575 Qmocklisp_arguments = intern ("mocklisp-arguments");
2576 staticpro (&Qmocklisp_arguments);
2577 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2578 "While in a mocklisp function, the list of its unevaluated args.");
2579 Vmocklisp_arguments = Qt;
2580
2581 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2582 "Set to the function `run-hooks', if that function has been defined.\n\
2583Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
db9f0278
JB
2584
2585 staticpro (&Vautoload_queue);
2586 Vautoload_queue = Qnil;
2587
2588 defsubr (&Sor);
2589 defsubr (&Sand);
2590 defsubr (&Sif);
2591 defsubr (&Scond);
2592 defsubr (&Sprogn);
2593 defsubr (&Sprog1);
2594 defsubr (&Sprog2);
2595 defsubr (&Ssetq);
2596 defsubr (&Squote);
2597 defsubr (&Sfunction);
2598 defsubr (&Sdefun);
2599 defsubr (&Sdefmacro);
2600 defsubr (&Sdefvar);
2601 defsubr (&Sdefconst);
2602 defsubr (&Suser_variable_p);
2603 defsubr (&Slet);
2604 defsubr (&SletX);
2605 defsubr (&Swhile);
2606 defsubr (&Smacroexpand);
2607 defsubr (&Scatch);
2608 defsubr (&Sthrow);
2609 defsubr (&Sunwind_protect);
2610 defsubr (&Scondition_case);
2611 defsubr (&Ssignal);
2612 defsubr (&Sinteractive_p);
2613 defsubr (&Scommandp);
2614 defsubr (&Sautoload);
2615 defsubr (&Seval);
2616 defsubr (&Sapply);
2617 defsubr (&Sfuncall);
2618 defsubr (&Sbacktrace_debug);
2619 defsubr (&Sbacktrace);
2620 defsubr (&Sbacktrace_frame);
2621}