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