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