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