*** empty log message ***
[bpt/emacs.git] / src / eval.c
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the 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
38 struct 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
50 struct backtrace *backtrace_list;
51
52 struct 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
66 struct catchtag *catchlist;
67
68 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
69 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
70 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
71 Lisp_Object Qand_rest, Qand_optional;
72 Lisp_Object Qdebug_on_error;
73
74 Lisp_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
81 Lisp_Object Vautoload_queue;
82
83 /* Current number of specbindings allocated in specpdl. */
84 int specpdl_size;
85
86 /* Pointer to beginning of specpdl. */
87 struct specbinding *specpdl;
88
89 /* Pointer to first unused element in specpdl. */
90 struct specbinding *specpdl_ptr;
91
92 /* Maximum size allowed for specpdl allocation */
93 int max_specpdl_size;
94
95 /* Depth in Lisp evaluations and function calls. */
96 int lisp_eval_depth;
97
98 /* Maximum allowed depth in Lisp evaluations and function calls. */
99 int max_lisp_eval_depth;
100
101 /* Nonzero means enter debugger before next function call */
102 int debug_on_next_call;
103
104 /* Nonzero means display a backtrace if an error
105 is handled by the command loop's error handler. */
106 int stack_trace_on_error;
107
108 /* Nonzero means enter debugger if an error
109 is handled by the command loop's error handler. */
110 int debug_on_error;
111
112 /* Nonzero means enter debugger if a quit signal
113 is handled by the command loop's error handler. */
114 int debug_on_quit;
115
116 /* Nonzero means we are trying to enter the debugger.
117 This is to prevent recursive attempts. */
118 int entering_debugger;
119
120 Lisp_Object Vdebugger;
121
122 void specbind (), record_unwind_protect ();
123
124 Lisp_Object funcall_lambda ();
125 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
126
127 init_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
135 init_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
147 Lisp_Object
148 call_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
160 do_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
172 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
173 "Eval args until one of them yields non-nil, then return that value.\n\
174 The remaining args are not evalled at all.\n\
175 If 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 (NILP(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 (!NILP (val))
193 break;
194 args_left = Fcdr (args_left);
195 }
196 while (!NILP(args_left));
197
198 UNGCPRO;
199 return val;
200 }
201
202 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
203 "Eval args until one of them yields nil, then return nil.\n\
204 The remaining args are not evalled at all.\n\
205 If 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 (NILP(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 (NILP (val))
223 break;
224 args_left = Fcdr (args_left);
225 }
226 while (!NILP(args_left));
227
228 UNGCPRO;
229 return val;
230 }
231
232 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
233 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
234 Returns the value of THEN or the value of the last of the ELSE's.\n\
235 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
236 If 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 (!NILP (cond))
248 return Feval (Fcar (Fcdr (args)));
249 return Fprogn (Fcdr (Fcdr (args)));
250 }
251
252 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
253 "(cond CLAUSES...): try each clause until one succeeds.\n\
254 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
255 and, if the value is non-nil, this clause succeeds:\n\
256 then the expressions in BODY are evaluated and the last one's\n\
257 value is the value of the cond-form.\n\
258 If no clause succeeds, cond returns nil.\n\
259 If a clause has one element, as in (CONDITION),\n\
260 CONDITION'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 (!NILP (args))
270 {
271 clause = Fcar (args);
272 val = Feval (Fcar (clause));
273 if (!NILP (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
286 DEFUN ("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 (!NILP (args) && (tem = Fcar (args), XTYPE (tem) == Lisp_Symbol))
301 {
302 QUIT;
303 specbind (tem, val), args = Fcdr (args);
304 }
305 }
306
307 if (NILP(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 (!NILP(args_left));
319
320 UNGCPRO;
321 return val;
322 }
323
324 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
325 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
326 The value of FIRST is saved during the evaluation of the remaining args,\n\
327 whose 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 (NILP(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 (!NILP(args_left));
352
353 UNGCPRO;
354 return val;
355 }
356
357 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
358 "(prog1 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
359 The value of Y is saved during the evaluation of the remaining args,\n\
360 whose 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 (NILP(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 (!NILP(args_left));
387
388 UNGCPRO;
389 return val;
390 }
391
392 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
393 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
394 The SYMs are not evaluated. Thus (setq x y) sets x to the value of y.\n\
395 Each 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 (NILP(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 (!NILP(args_left));
417
418 UNGCPRO;
419 return val;
420 }
421
422 DEFUN ("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
430 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
431 "Like `quote', but preferred for objects which are functions.\n\
432 In 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
440 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
441 "Return t if function in which this appears was called interactively.\n\
442 This means that the function was called with call-interactively (which\n\
443 includes being called as the binding of a key)\n\
444 and 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;
457 if (XTYPE (*btp->function) != Lisp_Compiled)
458 btp = btp->next;
459 while (btp
460 && (btp->nargs == UNEVALLED || EQ (*btp->function, Qbytecode)))
461 btp = btp->next;
462
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
482 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
483 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
484 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
485 See 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 (!NILP (Vpurify_flag))
495 defn = Fpurecopy (defn);
496 Ffset (fn_name, defn);
497 return fn_name;
498 }
499
500 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
501 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
502 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
503 When the macro is called, as in (NAME ARGS...),\n\
504 the function (lambda ARGLIST BODY...) is applied to\n\
505 the list ARGS... as it appears in the expression,\n\
506 and 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 (!NILP (Vpurify_flag))
516 defn = Fpurecopy (defn);
517 Ffset (fn_name, defn);
518 return fn_name;
519 }
520
521 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
522 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
523 You are not required to define a variable in order to use it,\n\
524 but the definition can supply documentation and an initial value\n\
525 in a way that tags can recognize.\n\n\
526 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
527 If SYMBOL is buffer-local, its default value is initialized in this way.\n\
528 INITVALUE and DOCSTRING are optional.\n\
529 If 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\
531 If 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 (!NILP (tem))
540 {
541 tem = Fdefault_boundp (sym);
542 if (NILP (tem))
543 Fset_default (sym, Feval (Fcar (Fcdr (args))));
544 }
545 tem = Fcar (Fcdr (Fcdr (args)));
546 if (!NILP (tem))
547 {
548 if (!NILP (Vpurify_flag))
549 tem = Fpurecopy (tem);
550 Fput (sym, Qvariable_documentation, tem);
551 }
552 return sym;
553 }
554
555 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
556 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
557 The intent is that programs do not change this value, but users may.\n\
558 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
559 If SYMBOL is buffer-local, its default value is initialized in this way.\n\
560 DOCSTRING is optional.\n\
561 If 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\
563 Note: do not use `defconst' for user options in libraries that are not\n\
564 normally loaded, since it is useful for users to be able to specify\n\
565 their own values for such variables before loading the library.\n\
566 Since `defconst' unconditionally assigns the variable,\n\
567 it 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 (!NILP (tem))
577 {
578 if (!NILP (Vpurify_flag))
579 tem = Fpurecopy (tem);
580 Fput (sym, Qvariable_documentation, tem);
581 }
582 return sym;
583 }
584
585 DEFUN ("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\
588 Determined by whether the first character of the documentation\n\
589 for 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
604 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
605 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
606 The value of the last form in BODY is returned.\n\
607 Each element of VARLIST is a symbol (which is bound to nil)\n\
608 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
609 Each 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 (!NILP (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
638 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
639 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
640 The value of the last form in BODY is returned.\n\
641 Each element of VARLIST is a symbol (which is bound to nil)\n\
642 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
643 All 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; !NILP (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; !NILP (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
691 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
692 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
693 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
694 until 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), !NILP (tem))
706 {
707 QUIT;
708 Fprogn (body);
709 }
710
711 UNGCPRO;
712 return Qnil;
713 }
714
715 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
716 "Return result of expanding macros at top level of FORM.\n\
717 If FORM is not a macro call, it is returned unchanged.\n\
718 Otherwise, the macro is expanded and the expansion is considered\n\
719 in place of FORM. When a non-macro-call results, it is returned.\n\n\
720 The second optional arg ENVIRONMENT species an environment of macro\n\
721 definitions 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 (NILP (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 (NILP (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 (NILP (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 (NILP (expander))
799 break;
800 }
801 explicit:
802 form = apply1 (expander, XCONS (form)->cdr);
803 }
804 return form;
805 }
806 \f
807 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
808 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
809 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
810 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
811 If no throw happens, `catch' returns the value of the last BODY form.\n\
812 If 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
829 Lisp_Object
830 internal_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
870 static void
871 unbind_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
890 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
891 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
892 Both 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 (!NILP (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
920 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
921 "Do BODYFORM, protecting with UNWINDFORMS.\n\
922 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
923 If BODYFORM completes normally, its value is returned\n\
924 after executing the UNWINDFORMS.\n\
925 If 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
943 struct handler *handlerlist;
944
945 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
946 "Regain control when an error is signaled.\n\
947 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
948 executes BODYFORM and returns its value if no error happens.\n\
949 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
950 where the BODY is made of Lisp expressions.\n\n\
951 A handler is applicable to an error\n\
952 if CONDITION-NAME is one of the error's condition names.\n\
953 If an error happens, the first applicable handler is run.\n\
954 \n\
955 When a handler handles an error,\n\
956 control returns to the condition-case and the handler BODY... is executed\n\
957 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
958 VAR may be nil; then you do not get access to the signal information.\n\
959 \n\
960 The value of the last BODY form is returned from the condition-case.\n\
961 See 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 (!NILP (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; ! NILP (val); val = Fcdr (val))
995 {
996 tem = Fcar (val);
997 if ((!NILP (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
1013 Lisp_Object
1014 internal_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
1050 static Lisp_Object find_handler_clause ();
1051
1052 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1053 "Signal an error. Args are SIGNAL-NAME, and associated DATA.\n\
1054 This function does not return.\n\n\
1055 A signal name is a symbol with an `error-conditions' property\n\
1056 that is a list of condition names.\n\
1057 A handler for any of those names will get to handle this signal.\n\
1058 The symbol `error' should normally be one of them.\n\
1059 \n\
1060 DATA should be a list. Its elements are printed as part of the error message.\n\
1061 If the signal is handled, DATA is made available to the handler.\n\
1062 See 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 (!NILP (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
1124 static Lisp_Object
1125 find_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 (!NILP (tem))
1159 return tem1;
1160 }
1161 return Qnil;
1162 }
1163
1164 /* dump an error message; called like printf */
1165
1166 /* VARARGS 1 */
1167 void
1168 error (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
1178 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1179 "T if FUNCTION makes provisions for interactive calling.\n\
1180 This means it contains a description for how to read arguments to give it.\n\
1181 The value is nil for an invalid function or a symbol with no function\n\
1182 definition.\n\
1183 \n\
1184 Interactively callable functions include strings and vectors (treated\n\
1185 as keyboard macros), lambda-expressions that contain a top-level call\n\
1186 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1187 fourth argument, and some of the built-in functions of Lisp.\n\
1188 \n\
1189 Also, 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 (NILP (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 */
1248 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1249 "Define FUNCTION to autoload from FILE.\n\
1250 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1251 Third arg DOCSTRING is documentation for the function.\n\
1252 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1253 Fifth arg MACRO if non-nil says the function is really a macro.\n\
1254 Third through fifth args give info about the real definition.\n\
1255 They default to nil.\n\
1256 If FUNCTION is already defined other than as an autoload,\n\
1257 this 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
1286 Lisp_Object
1287 un_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
1310 do_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
1341 DEFUN ("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 (NILP (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 (!NILP (args_left))
1442 {
1443 vals[argnum++] = Feval (Fcar (args_left));
1444 args_left = Fcdr (args_left);
1445 gcpro3.nvars = argnum;
1446 }
1447
1448 backtrace.args = vals;
1449 backtrace.nargs = XINT (numargs);
1450
1451 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1452 UNGCPRO;
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 (NILP (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
1543 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1544 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1545 Thus, (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;
1553 Lisp_Object fun;
1554 struct gcpro gcpro1;
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
1571 numargs += nargs - 2;
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;
1598 GCPRO1 (*funcall_args);
1599 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
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)
1606 {
1607 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1608 * sizeof (Lisp_Object));
1609 GCPRO1 (*funcall_args);
1610 gcpro1.nvars = 1 + numargs;
1611 }
1612
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 (!NILP (spread_arg))
1618 {
1619 funcall_args [i++] = XCONS (spread_arg)->car;
1620 spread_arg = XCONS (spread_arg)->cdr;
1621 }
1622
1623 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1624 }
1625 \f
1626 /* Apply fn to arg */
1627 Lisp_Object
1628 apply1 (fn, arg)
1629 Lisp_Object fn, arg;
1630 {
1631 struct gcpro gcpro1;
1632
1633 GCPRO1 (fn);
1634 if (NILP (arg))
1635 RETURN_UNGCPRO (Ffuncall (1, &fn));
1636 gcpro1.nvars = 2;
1637 #ifdef NO_ARG_ARRAY
1638 {
1639 Lisp_Object args[2];
1640 args[0] = fn;
1641 args[1] = arg;
1642 gcpro1.var = args;
1643 RETURN_UNGCPRO (Fapply (2, args));
1644 }
1645 #else /* not NO_ARG_ARRAY */
1646 RETURN_UNGCPRO (Fapply (2, &fn));
1647 #endif /* not NO_ARG_ARRAY */
1648 }
1649
1650 /* Call function fn on no arguments */
1651 Lisp_Object
1652 call0 (fn)
1653 Lisp_Object fn;
1654 {
1655 struct gcpro gcpro1;
1656
1657 GCPRO1 (fn);
1658 RETURN_UNGCPRO (Ffuncall (1, &fn));
1659 }
1660
1661 /* Call function fn with argument arg */
1662 /* ARGSUSED */
1663 Lisp_Object
1664 call1 (fn, arg)
1665 Lisp_Object fn, arg;
1666 {
1667 struct gcpro gcpro1;
1668 #ifdef NO_ARG_ARRAY
1669 Lisp_Object args[2];
1670
1671 args[0] = fn;
1672 args[1] = arg;
1673 GCPRO1 (args[0]);
1674 gcpro1.nvars = 2;
1675 RETURN_UNGCPRO (Ffuncall (2, args));
1676 #else /* not NO_ARG_ARRAY */
1677 GCPRO1 (fn);
1678 gcpro1.nvars = 2;
1679 RETURN_UNGCPRO (Ffuncall (2, &fn));
1680 #endif /* not NO_ARG_ARRAY */
1681 }
1682
1683 /* Call function fn with arguments arg, arg1 */
1684 /* ARGSUSED */
1685 Lisp_Object
1686 call2 (fn, arg, arg1)
1687 Lisp_Object fn, arg, arg1;
1688 {
1689 struct gcpro gcpro1;
1690 #ifdef NO_ARG_ARRAY
1691 Lisp_Object args[3];
1692 args[0] = fn;
1693 args[1] = arg;
1694 args[2] = arg1;
1695 GCPRO1 (args[0]);
1696 gcpro1.nvars = 3;
1697 RETURN_UNGCPRO (Ffuncall (3, args));
1698 #else /* not NO_ARG_ARRAY */
1699 GCPRO1 (fn);
1700 gcpro1.nvars = 3;
1701 RETURN_UNGCPRO (Ffuncall (3, &fn));
1702 #endif /* not NO_ARG_ARRAY */
1703 }
1704
1705 /* Call function fn with arguments arg, arg1, arg2 */
1706 /* ARGSUSED */
1707 Lisp_Object
1708 call3 (fn, arg, arg1, arg2)
1709 Lisp_Object fn, arg, arg1, arg2;
1710 {
1711 struct gcpro gcpro1;
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;
1718 GCPRO1 (args[0]);
1719 gcpro1.nvars = 4;
1720 RETURN_UNGCPRO (Ffuncall (4, args));
1721 #else /* not NO_ARG_ARRAY */
1722 GCPRO1 (fn);
1723 gcpro1.nvars = 4;
1724 RETURN_UNGCPRO (Ffuncall (4, &fn));
1725 #endif /* not NO_ARG_ARRAY */
1726 }
1727
1728 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
1729 "Call first argument as a function, passing remaining arguments to it.\n\
1730 Thus, (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)
1746 Fgarbage_collect ();
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
1871 Lisp_Object
1872 apply_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
1920 Lisp_Object
1921 funcall_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 (; !NILP (syms_left); syms_left = Fcdr (syms_left))
1946 {
1947 QUIT;
1948 next = Fcar (syms_left);
1949 while (XTYPE (next) != Lisp_Symbol)
1950 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1951 if (EQ (next, Qand_rest))
1952 rest = 1;
1953 else if (EQ (next, Qand_optional))
1954 optional = 1;
1955 else if (rest)
1956 {
1957 specbind (next, Flist (nargs - i, &arg_vector[i]));
1958 i = nargs;
1959 }
1960 else if (i < nargs)
1961 {
1962 tem = arg_vector[i++];
1963 specbind (next, tem);
1964 }
1965 else if (!optional)
1966 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1967 else
1968 specbind (next, Qnil);
1969 }
1970
1971 if (i < nargs)
1972 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1973
1974 if (XTYPE (fun) == Lisp_Cons)
1975 val = Fprogn (Fcdr (Fcdr (fun)));
1976 else
1977 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
1978 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
1979 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
1980 return unbind_to (count, val);
1981 }
1982 \f
1983 void
1984 grow_specpdl ()
1985 {
1986 register int count = specpdl_ptr - specpdl;
1987 if (specpdl_size >= max_specpdl_size)
1988 {
1989 if (max_specpdl_size < 400)
1990 max_specpdl_size = 400;
1991 if (specpdl_size >= max_specpdl_size)
1992 {
1993 Fsignal (Qerror,
1994 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
1995 max_specpdl_size *= 2;
1996 }
1997 }
1998 specpdl_size *= 2;
1999 if (specpdl_size > max_specpdl_size)
2000 specpdl_size = max_specpdl_size;
2001 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2002 specpdl_ptr = specpdl + count;
2003 }
2004
2005 void
2006 specbind (symbol, value)
2007 Lisp_Object symbol, value;
2008 {
2009 extern void store_symval_forwarding (); /* in eval.c */
2010 Lisp_Object ovalue;
2011
2012 CHECK_SYMBOL (symbol, 0);
2013
2014 if (specpdl_ptr == specpdl + specpdl_size)
2015 grow_specpdl ();
2016 specpdl_ptr->symbol = symbol;
2017 specpdl_ptr->func = 0;
2018 ovalue = XSYMBOL (symbol)->value;
2019 specpdl_ptr->old_value = EQ (ovalue, Qunbound) ? Qunbound : Fsymbol_value (symbol);
2020 specpdl_ptr++;
2021 if (XTYPE (ovalue) == Lisp_Buffer_Objfwd)
2022 store_symval_forwarding (symbol, ovalue, value);
2023 else
2024 Fset (symbol, value);
2025 }
2026
2027 void
2028 record_unwind_protect (function, arg)
2029 Lisp_Object (*function)();
2030 Lisp_Object arg;
2031 {
2032 if (specpdl_ptr == specpdl + specpdl_size)
2033 grow_specpdl ();
2034 specpdl_ptr->func = function;
2035 specpdl_ptr->symbol = Qnil;
2036 specpdl_ptr->old_value = arg;
2037 specpdl_ptr++;
2038 }
2039
2040 Lisp_Object
2041 unbind_to (count, value)
2042 int count;
2043 Lisp_Object value;
2044 {
2045 int quitf = !NILP (Vquit_flag);
2046 struct gcpro gcpro1;
2047
2048 GCPRO1 (value);
2049
2050 Vquit_flag = Qnil;
2051
2052 while (specpdl_ptr != specpdl + count)
2053 {
2054 --specpdl_ptr;
2055 if (specpdl_ptr->func != 0)
2056 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2057 /* Note that a "binding" of nil is really an unwind protect,
2058 so in that case the "old value" is a list of forms to evaluate. */
2059 else if (NILP (specpdl_ptr->symbol))
2060 Fprogn (specpdl_ptr->old_value);
2061 else
2062 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2063 }
2064 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2065
2066 UNGCPRO;
2067
2068 return value;
2069 }
2070 \f
2071 #if 0
2072
2073 /* Get the value of symbol's global binding, even if that binding
2074 is not now dynamically visible. */
2075
2076 Lisp_Object
2077 top_level_value (symbol)
2078 Lisp_Object symbol;
2079 {
2080 register struct specbinding *ptr = specpdl;
2081
2082 CHECK_SYMBOL (symbol, 0);
2083 for (; ptr != specpdl_ptr; ptr++)
2084 {
2085 if (EQ (ptr->symbol, symbol))
2086 return ptr->old_value;
2087 }
2088 return Fsymbol_value (symbol);
2089 }
2090
2091 Lisp_Object
2092 top_level_set (symbol, newval)
2093 Lisp_Object symbol, newval;
2094 {
2095 register struct specbinding *ptr = specpdl;
2096
2097 CHECK_SYMBOL (symbol, 0);
2098 for (; ptr != specpdl_ptr; ptr++)
2099 {
2100 if (EQ (ptr->symbol, symbol))
2101 {
2102 ptr->old_value = newval;
2103 return newval;
2104 }
2105 }
2106 return Fset (symbol, newval);
2107 }
2108
2109 #endif /* 0 */
2110 \f
2111 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2112 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2113 The debugger is entered when that frame exits, if the flag is non-nil.")
2114 (level, flag)
2115 Lisp_Object level, flag;
2116 {
2117 register struct backtrace *backlist = backtrace_list;
2118 register int i;
2119
2120 CHECK_NUMBER (level, 0);
2121
2122 for (i = 0; backlist && i < XINT (level); i++)
2123 {
2124 backlist = backlist->next;
2125 }
2126
2127 if (backlist)
2128 backlist->debug_on_exit = !NILP (flag);
2129
2130 return flag;
2131 }
2132
2133 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2134 "Print a trace of Lisp function calls currently active.\n\
2135 Output stream used is value of `standard-output'.")
2136 ()
2137 {
2138 register struct backtrace *backlist = backtrace_list;
2139 register int i;
2140 Lisp_Object tail;
2141 Lisp_Object tem;
2142 extern Lisp_Object Vprint_level;
2143 struct gcpro gcpro1;
2144
2145 entering_debugger = 0;
2146
2147 XFASTINT (Vprint_level) = 3;
2148
2149 tail = Qnil;
2150 GCPRO1 (tail);
2151
2152 while (backlist)
2153 {
2154 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2155 if (backlist->nargs == UNEVALLED)
2156 {
2157 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2158 }
2159 else
2160 {
2161 tem = *backlist->function;
2162 Fprin1 (tem, Qnil); /* This can QUIT */
2163 write_string ("(", -1);
2164 if (backlist->nargs == MANY)
2165 {
2166 for (tail = *backlist->args, i = 0;
2167 !NILP (tail);
2168 tail = Fcdr (tail), i++)
2169 {
2170 if (i) write_string (" ", -1);
2171 Fprin1 (Fcar (tail), Qnil);
2172 }
2173 }
2174 else
2175 {
2176 for (i = 0; i < backlist->nargs; i++)
2177 {
2178 if (i) write_string (" ", -1);
2179 Fprin1 (backlist->args[i], Qnil);
2180 }
2181 }
2182 }
2183 write_string (")\n", -1);
2184 backlist = backlist->next;
2185 }
2186
2187 Vprint_level = Qnil;
2188 UNGCPRO;
2189 return Qnil;
2190 }
2191
2192 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2193 "Return the function and arguments N frames up from current execution point.\n\
2194 If that frame has not evaluated the arguments yet (or is a special form),\n\
2195 the value is (nil FUNCTION ARG-FORMS...).\n\
2196 If that frame has evaluated its arguments and called its function already,\n\
2197 the value is (t FUNCTION ARG-VALUES...).\n\
2198 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2199 FUNCTION is whatever was supplied as car of evaluated list,\n\
2200 or a lambda expression for macro calls.\n\
2201 If N is more than the number of frames, the value is nil.")
2202 (nframes)
2203 Lisp_Object nframes;
2204 {
2205 register struct backtrace *backlist = backtrace_list;
2206 register int i;
2207 Lisp_Object tem;
2208
2209 CHECK_NATNUM (nframes, 0);
2210
2211 /* Find the frame requested. */
2212 for (i = 0; i < XFASTINT (nframes); i++)
2213 backlist = backlist->next;
2214
2215 if (!backlist)
2216 return Qnil;
2217 if (backlist->nargs == UNEVALLED)
2218 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2219 else
2220 {
2221 if (backlist->nargs == MANY)
2222 tem = *backlist->args;
2223 else
2224 tem = Flist (backlist->nargs, backlist->args);
2225
2226 return Fcons (Qt, Fcons (*backlist->function, tem));
2227 }
2228 }
2229 \f
2230 syms_of_eval ()
2231 {
2232 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2233 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2234
2235 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2236 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2237 This limit is to catch infinite recursions for you before they cause\n\
2238 actual stack overflow in C, which would be fatal for Emacs.\n\
2239 You can safely make it considerably larger than its default value,\n\
2240 if that proves inconveniently small.");
2241
2242 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2243 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2244 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2245 Vquit_flag = Qnil;
2246
2247 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2248 "Non-nil inhibits C-g quitting from happening immediately.\n\
2249 Note that `quit-flag' will still be set by typing C-g,\n\
2250 so a quit will be signalled as soon as `inhibit-quit' is nil.\n\
2251 To prevent this happening, set `quit-flag' to nil\n\
2252 before making `inhibit-quit' nil.");
2253 Vinhibit_quit = Qnil;
2254
2255 Qinhibit_quit = intern ("inhibit-quit");
2256 staticpro (&Qinhibit_quit);
2257
2258 Qautoload = intern ("autoload");
2259 staticpro (&Qautoload);
2260
2261 Qdebug_on_error = intern ("debug-on-error");
2262 staticpro (&Qdebug_on_error);
2263
2264 Qmacro = intern ("macro");
2265 staticpro (&Qmacro);
2266
2267 /* Note that the process handling also uses Qexit, but we don't want
2268 to staticpro it twice, so we just do it here. */
2269 Qexit = intern ("exit");
2270 staticpro (&Qexit);
2271
2272 Qinteractive = intern ("interactive");
2273 staticpro (&Qinteractive);
2274
2275 Qcommandp = intern ("commandp");
2276 staticpro (&Qcommandp);
2277
2278 Qdefun = intern ("defun");
2279 staticpro (&Qdefun);
2280
2281 Qand_rest = intern ("&rest");
2282 staticpro (&Qand_rest);
2283
2284 Qand_optional = intern ("&optional");
2285 staticpro (&Qand_optional);
2286
2287 DEFVAR_BOOL ("stack-trace-on-error", &stack_trace_on_error,
2288 "*Non-nil means automatically display a backtrace buffer\n\
2289 after any error that is handled by the editor command loop.");
2290 stack_trace_on_error = 0;
2291
2292 DEFVAR_BOOL ("debug-on-error", &debug_on_error,
2293 "*Non-nil means enter debugger if an error is signaled.\n\
2294 Does not apply to errors handled by `condition-case'.\n\
2295 See also variable `debug-on-quit'.");
2296 debug_on_error = 0;
2297
2298 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2299 "*Non-nil means enter debugger if quit is signaled (C-G, for example).\n\
2300 Does not apply if quit is handled by a `condition-case'.");
2301 debug_on_quit = 0;
2302
2303 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2304 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2305
2306 DEFVAR_LISP ("debugger", &Vdebugger,
2307 "Function to call to invoke debugger.\n\
2308 If due to frame exit, args are `exit' and the value being returned;\n\
2309 this function's value will be returned instead of that.\n\
2310 If due to error, args are `error' and a list of the args to `signal'.\n\
2311 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2312 If due to `eval' entry, one arg, t.");
2313 Vdebugger = Qnil;
2314
2315 Qmocklisp_arguments = intern ("mocklisp-arguments");
2316 staticpro (&Qmocklisp_arguments);
2317 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2318 "While in a mocklisp function, the list of its unevaluated args.");
2319 Vmocklisp_arguments = Qt;
2320
2321 DEFVAR_LISP ("run-hooks", &Vrun_hooks,
2322 "Set to the function `run-hooks', if that function has been defined.\n\
2323 Otherwise, nil (in a bare Emacs without preloaded Lisp code).");
2324 Vrun_hooks = Qnil;
2325
2326 staticpro (&Vautoload_queue);
2327 Vautoload_queue = Qnil;
2328
2329 defsubr (&Sor);
2330 defsubr (&Sand);
2331 defsubr (&Sif);
2332 defsubr (&Scond);
2333 defsubr (&Sprogn);
2334 defsubr (&Sprog1);
2335 defsubr (&Sprog2);
2336 defsubr (&Ssetq);
2337 defsubr (&Squote);
2338 defsubr (&Sfunction);
2339 defsubr (&Sdefun);
2340 defsubr (&Sdefmacro);
2341 defsubr (&Sdefvar);
2342 defsubr (&Sdefconst);
2343 defsubr (&Suser_variable_p);
2344 defsubr (&Slet);
2345 defsubr (&SletX);
2346 defsubr (&Swhile);
2347 defsubr (&Smacroexpand);
2348 defsubr (&Scatch);
2349 defsubr (&Sthrow);
2350 defsubr (&Sunwind_protect);
2351 defsubr (&Scondition_case);
2352 defsubr (&Ssignal);
2353 defsubr (&Sinteractive_p);
2354 defsubr (&Scommandp);
2355 defsubr (&Sautoload);
2356 defsubr (&Seval);
2357 defsubr (&Sapply);
2358 defsubr (&Sfuncall);
2359 defsubr (&Sbacktrace_debug);
2360 defsubr (&Sbacktrace);
2361 defsubr (&Sbacktrace_frame);
2362 }