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