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