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