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