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