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