(syms_of_eval): Initialize Vrun_hooks here.
[bpt/emacs.git] / src / eval.c
1 /* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "blockinput.h"
25
26 #ifndef standalone
27 #include "commands.h"
28 #include "keyboard.h"
29 #else
30 #define INTERACTIVE 1
31 #endif
32
33 #include <setjmp.h>
34
35 /* This definition is duplicated in alloc.c and keyboard.c */
36 /* Putting it in lisp.h makes cc bomb out! */
37
38 struct backtrace
39 {
40 struct backtrace *next;
41 Lisp_Object *function;
42 Lisp_Object *args; /* Points to vector of args. */
43 int nargs; /* Length of vector.
44 If nargs is UNEVALLED, args points to slot holding
45 list of unevalled args */
46 char evalargs;
47 /* Nonzero means call value of debugger when done with this operation. */
48 char debug_on_exit;
49 };
50
51 struct backtrace *backtrace_list;
52
53 /* This structure helps implement the `catch' and `throw' control
54 structure. A struct catchtag contains all the information needed
55 to restore the state of the interpreter after a non-local jump.
56
57 Handlers for error conditions (represented by `struct handler'
58 structures) just point to a catch tag to do the cleanup required
59 for their jumps.
60
61 catchtag structures are chained together in the C calling stack;
62 the `next' member points to the next outer catchtag.
63
64 A call like (throw TAG VAL) searches for a catchtag whose `tag'
65 member is TAG, and then unbinds to it. The `val' member is used to
66 hold VAL while the stack is unwound; `val' is returned as the value
67 of the catch form.
68
69 All the other members are concerned with restoring the interpreter
70 state. */
71 struct catchtag
72 {
73 Lisp_Object tag;
74 Lisp_Object val;
75 struct catchtag *next;
76 struct gcpro *gcpro;
77 jmp_buf jmp;
78 struct backtrace *backlist;
79 struct handler *handlerlist;
80 int lisp_eval_depth;
81 int pdlcount;
82 int poll_suppress_count;
83 };
84
85 struct catchtag *catchlist;
86
87 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
88 Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
89 Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
90 Lisp_Object Qand_rest, Qand_optional;
91 Lisp_Object Qdebug_on_error;
92
93 /* This holds either the symbol `run-hooks' or nil.
94 It is nil at an early stage of startup, and when Emacs
95 is shutting down. */
96 Lisp_Object Vrun_hooks;
97
98 /* Non-nil means record all fset's and provide's, to be undone
99 if the file being autoloaded is not fully loaded.
100 They are recorded by being consed onto the front of Vautoload_queue:
101 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
102
103 Lisp_Object Vautoload_queue;
104
105 /* Current number of specbindings allocated in specpdl. */
106 int specpdl_size;
107
108 /* Pointer to beginning of specpdl. */
109 struct specbinding *specpdl;
110
111 /* Pointer to first unused element in specpdl. */
112 struct specbinding *specpdl_ptr;
113
114 /* Maximum size allowed for specpdl allocation */
115 int max_specpdl_size;
116
117 /* Depth in Lisp evaluations and function calls. */
118 int lisp_eval_depth;
119
120 /* Maximum allowed depth in Lisp evaluations and function calls. */
121 int max_lisp_eval_depth;
122
123 /* Nonzero means enter debugger before next function call */
124 int debug_on_next_call;
125
126 /* List of conditions (non-nil atom means all) which cause a backtrace
127 if an error is handled by the command loop's error handler. */
128 Lisp_Object Vstack_trace_on_error;
129
130 /* List of conditions (non-nil atom means all) which enter the debugger
131 if an error is handled by the command loop's error handler. */
132 Lisp_Object Vdebug_on_error;
133
134 /* List of conditions and regexps specifying error messages which
135 do not enter the debugger even if Vdebug_on_errors says they should. */
136 Lisp_Object Vdebug_ignored_errors;
137
138 /* Nonzero means enter debugger if a quit signal
139 is handled by the command loop's error handler. */
140 int debug_on_quit;
141
142 /* The value of num_nonmacro_input_chars as of the last time we
143 started to enter the debugger. If we decide to enter the debugger
144 again when this is still equal to num_nonmacro_input_chars, then we
145 know that the debugger itself has an error, and we should just
146 signal the error instead of entering an infinite loop of debugger
147 invocations. */
148 int when_entered_debugger;
149
150 Lisp_Object Vdebugger;
151
152 void specbind (), record_unwind_protect ();
153
154 Lisp_Object run_hook_with_args ();
155
156 Lisp_Object funcall_lambda ();
157 extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
158
159 init_eval_once ()
160 {
161 specpdl_size = 50;
162 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
163 specpdl_ptr = specpdl;
164 max_specpdl_size = 600;
165 max_lisp_eval_depth = 200;
166
167 Vrun_hooks = Qnil;
168 }
169
170 init_eval ()
171 {
172 specpdl_ptr = specpdl;
173 catchlist = 0;
174 handlerlist = 0;
175 backtrace_list = 0;
176 Vquit_flag = Qnil;
177 debug_on_next_call = 0;
178 lisp_eval_depth = 0;
179 /* This is less than the initial value of num_nonmacro_input_chars. */
180 when_entered_debugger = -1;
181 }
182
183 Lisp_Object
184 call_debugger (arg)
185 Lisp_Object arg;
186 {
187 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
188 max_lisp_eval_depth = lisp_eval_depth + 20;
189 if (specpdl_size + 40 > max_specpdl_size)
190 max_specpdl_size = specpdl_size + 40;
191 debug_on_next_call = 0;
192 when_entered_debugger = num_nonmacro_input_chars;
193 return apply1 (Vdebugger, arg);
194 }
195
196 do_debug_on_call (code)
197 Lisp_Object code;
198 {
199 debug_on_next_call = 0;
200 backtrace_list->debug_on_exit = 1;
201 call_debugger (Fcons (code, Qnil));
202 }
203 \f
204 /* NOTE!!! Every function that can call EVAL must protect its args
205 and temporaries from garbage collection while it needs them.
206 The definition of `For' shows what you have to do. */
207
208 DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
209 "Eval args until one of them yields non-nil, then return that value.\n\
210 The remaining args are not evalled at all.\n\
211 If all args return nil, return nil.")
212 (args)
213 Lisp_Object args;
214 {
215 register Lisp_Object val;
216 Lisp_Object args_left;
217 struct gcpro gcpro1;
218
219 if (NILP(args))
220 return Qnil;
221
222 args_left = args;
223 GCPRO1 (args_left);
224
225 do
226 {
227 val = Feval (Fcar (args_left));
228 if (!NILP (val))
229 break;
230 args_left = Fcdr (args_left);
231 }
232 while (!NILP(args_left));
233
234 UNGCPRO;
235 return val;
236 }
237
238 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
239 "Eval args until one of them yields nil, then return nil.\n\
240 The remaining args are not evalled at all.\n\
241 If no arg yields nil, return the last arg's value.")
242 (args)
243 Lisp_Object args;
244 {
245 register Lisp_Object val;
246 Lisp_Object args_left;
247 struct gcpro gcpro1;
248
249 if (NILP(args))
250 return Qt;
251
252 args_left = args;
253 GCPRO1 (args_left);
254
255 do
256 {
257 val = Feval (Fcar (args_left));
258 if (NILP (val))
259 break;
260 args_left = Fcdr (args_left);
261 }
262 while (!NILP(args_left));
263
264 UNGCPRO;
265 return val;
266 }
267
268 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
269 "(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...\n\
270 Returns the value of THEN or the value of the last of the ELSE's.\n\
271 THEN must be one expression, but ELSE... can be zero or more expressions.\n\
272 If COND yields nil, and there are no ELSE's, the value is nil.")
273 (args)
274 Lisp_Object args;
275 {
276 register Lisp_Object cond;
277 struct gcpro gcpro1;
278
279 GCPRO1 (args);
280 cond = Feval (Fcar (args));
281 UNGCPRO;
282
283 if (!NILP (cond))
284 return Feval (Fcar (Fcdr (args)));
285 return Fprogn (Fcdr (Fcdr (args)));
286 }
287
288 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
289 "(cond CLAUSES...): try each clause until one succeeds.\n\
290 Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
291 and, if the value is non-nil, this clause succeeds:\n\
292 then the expressions in BODY are evaluated and the last one's\n\
293 value is the value of the cond-form.\n\
294 If no clause succeeds, cond returns nil.\n\
295 If a clause has one element, as in (CONDITION),\n\
296 CONDITION's value if non-nil is returned from the cond-form.")
297 (args)
298 Lisp_Object args;
299 {
300 register Lisp_Object clause, val;
301 struct gcpro gcpro1;
302
303 val = Qnil;
304 GCPRO1 (args);
305 while (!NILP (args))
306 {
307 clause = Fcar (args);
308 val = Feval (Fcar (clause));
309 if (!NILP (val))
310 {
311 if (!EQ (XCONS (clause)->cdr, Qnil))
312 val = Fprogn (XCONS (clause)->cdr);
313 break;
314 }
315 args = XCONS (args)->cdr;
316 }
317 UNGCPRO;
318
319 return val;
320 }
321
322 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
323 "(progn BODY...): eval BODY forms sequentially and return value of last one.")
324 (args)
325 Lisp_Object args;
326 {
327 register Lisp_Object val, tem;
328 Lisp_Object args_left;
329 struct gcpro gcpro1;
330
331 /* In Mocklisp code, symbols at the front of the progn arglist
332 are to be bound to zero. */
333 if (!EQ (Vmocklisp_arguments, Qt))
334 {
335 val = make_number (0);
336 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
337 {
338 QUIT;
339 specbind (tem, val), args = Fcdr (args);
340 }
341 }
342
343 if (NILP(args))
344 return Qnil;
345
346 args_left = args;
347 GCPRO1 (args_left);
348
349 do
350 {
351 val = Feval (Fcar (args_left));
352 args_left = Fcdr (args_left);
353 }
354 while (!NILP(args_left));
355
356 UNGCPRO;
357 return val;
358 }
359
360 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
361 "(prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.\n\
362 The value of FIRST is saved during the evaluation of the remaining args,\n\
363 whose values are discarded.")
364 (args)
365 Lisp_Object args;
366 {
367 Lisp_Object val;
368 register Lisp_Object args_left;
369 struct gcpro gcpro1, gcpro2;
370 register int argnum = 0;
371
372 if (NILP(args))
373 return Qnil;
374
375 args_left = args;
376 val = Qnil;
377 GCPRO2 (args, val);
378
379 do
380 {
381 if (!(argnum++))
382 val = Feval (Fcar (args_left));
383 else
384 Feval (Fcar (args_left));
385 args_left = Fcdr (args_left);
386 }
387 while (!NILP(args_left));
388
389 UNGCPRO;
390 return val;
391 }
392
393 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
394 "(prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.\n\
395 The value of Y is saved during the evaluation of the remaining args,\n\
396 whose values are discarded.")
397 (args)
398 Lisp_Object args;
399 {
400 Lisp_Object val;
401 register Lisp_Object args_left;
402 struct gcpro gcpro1, gcpro2;
403 register int argnum = -1;
404
405 val = Qnil;
406
407 if (NILP (args))
408 return Qnil;
409
410 args_left = args;
411 val = Qnil;
412 GCPRO2 (args, val);
413
414 do
415 {
416 if (!(argnum++))
417 val = Feval (Fcar (args_left));
418 else
419 Feval (Fcar (args_left));
420 args_left = Fcdr (args_left);
421 }
422 while (!NILP (args_left));
423
424 UNGCPRO;
425 return val;
426 }
427
428 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
429 "(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.\n\
430 The symbols SYM are variables; they are literal (not evaluated).\n\
431 The values VAL are expressions; they are evaluated.\n\
432 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
433 The second VAL is not computed until after the first SYM is set, and so on;\n\
434 each VAL can use the new value of variables set earlier in the `setq'.\n\
435 The return value of the `setq' form is the value of the last VAL.")
436 (args)
437 Lisp_Object args;
438 {
439 register Lisp_Object args_left;
440 register Lisp_Object val, sym;
441 struct gcpro gcpro1;
442
443 if (NILP(args))
444 return Qnil;
445
446 args_left = args;
447 GCPRO1 (args);
448
449 do
450 {
451 val = Feval (Fcar (Fcdr (args_left)));
452 sym = Fcar (args_left);
453 Fset (sym, val);
454 args_left = Fcdr (Fcdr (args_left));
455 }
456 while (!NILP(args_left));
457
458 UNGCPRO;
459 return val;
460 }
461
462 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
463 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
464 (args)
465 Lisp_Object args;
466 {
467 return Fcar (args);
468 }
469
470 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
471 "Like `quote', but preferred for objects which are functions.\n\
472 In byte compilation, `function' causes its argument to be compiled.\n\
473 `quote' cannot do that.")
474 (args)
475 Lisp_Object args;
476 {
477 return Fcar (args);
478 }
479
480 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
481 "Return t if function in which this appears was called interactively.\n\
482 This means that the function was called with call-interactively (which\n\
483 includes being called as the binding of a key)\n\
484 and input is currently coming from the keyboard (not in keyboard macro).")
485 ()
486 {
487 register struct backtrace *btp;
488 register Lisp_Object fun;
489
490 if (!INTERACTIVE)
491 return Qnil;
492
493 btp = backtrace_list;
494
495 /* If this isn't a byte-compiled function, there may be a frame at
496 the top for Finteractive_p itself. If so, skip it. */
497 fun = Findirect_function (*btp->function);
498 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
499 btp = btp->next;
500
501 /* If we're running an Emacs 18-style byte-compiled function, there
502 may be a frame for Fbytecode. Now, given the strictest
503 definition, this function isn't really being called
504 interactively, but because that's the way Emacs 18 always builds
505 byte-compiled functions, we'll accept it for now. */
506 if (EQ (*btp->function, Qbytecode))
507 btp = btp->next;
508
509 /* If this isn't a byte-compiled function, then we may now be
510 looking at several frames for special forms. Skip past them. */
511 while (btp &&
512 btp->nargs == UNEVALLED)
513 btp = btp->next;
514
515 /* btp now points at the frame of the innermost function that isn't
516 a special form, ignoring frames for Finteractive_p and/or
517 Fbytecode at the top. If this frame is for a built-in function
518 (such as load or eval-region) return nil. */
519 fun = Findirect_function (*btp->function);
520 if (SUBRP (fun))
521 return Qnil;
522 /* btp points to the frame of a Lisp function that called interactive-p.
523 Return t if that function was called interactively. */
524 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
525 return Qt;
526 return Qnil;
527 }
528
529 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
530 "(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.\n\
531 The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
532 See also the function `interactive'.")
533 (args)
534 Lisp_Object args;
535 {
536 register Lisp_Object fn_name;
537 register Lisp_Object defn;
538
539 fn_name = Fcar (args);
540 defn = Fcons (Qlambda, Fcdr (args));
541 if (!NILP (Vpurify_flag))
542 defn = Fpurecopy (defn);
543 Ffset (fn_name, defn);
544 LOADHIST_ATTACH (fn_name);
545 return fn_name;
546 }
547
548 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
549 "(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.\n\
550 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
551 When the macro is called, as in (NAME ARGS...),\n\
552 the function (lambda ARGLIST BODY...) is applied to\n\
553 the list ARGS... as it appears in the expression,\n\
554 and the result should be a form to be evaluated instead of the original.")
555 (args)
556 Lisp_Object args;
557 {
558 register Lisp_Object fn_name;
559 register Lisp_Object defn;
560
561 fn_name = Fcar (args);
562 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
563 if (!NILP (Vpurify_flag))
564 defn = Fpurecopy (defn);
565 Ffset (fn_name, defn);
566 LOADHIST_ATTACH (fn_name);
567 return fn_name;
568 }
569
570 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
571 "(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.\n\
572 You are not required to define a variable in order to use it,\n\
573 but the definition can supply documentation and an initial value\n\
574 in a way that tags can recognize.\n\n\
575 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
576 If SYMBOL is buffer-local, its default value is what is set;\n\
577 buffer-local values are not affected.\n\
578 INITVALUE and DOCSTRING are optional.\n\
579 If DOCSTRING starts with *, this variable is identified as a user option.\n\
580 This means that M-x set-variable and M-x edit-options recognize it.\n\
581 If INITVALUE is missing, SYMBOL's value is not set.")
582 (args)
583 Lisp_Object args;
584 {
585 register Lisp_Object sym, tem, tail;
586
587 sym = Fcar (args);
588 tail = Fcdr (args);
589 if (!NILP (Fcdr (Fcdr (tail))))
590 error ("too many arguments");
591
592 if (!NILP (tail))
593 {
594 tem = Fdefault_boundp (sym);
595 if (NILP (tem))
596 Fset_default (sym, Feval (Fcar (Fcdr (args))));
597 }
598 tail = Fcdr (Fcdr (args));
599 if (!NILP (Fcar (tail)))
600 {
601 tem = Fcar (tail);
602 if (!NILP (Vpurify_flag))
603 tem = Fpurecopy (tem);
604 Fput (sym, Qvariable_documentation, tem);
605 }
606 LOADHIST_ATTACH (sym);
607 return sym;
608 }
609
610 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
611 "(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant variable.\n\
612 The intent is that programs do not change this value, but users may.\n\
613 Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
614 If SYMBOL is buffer-local, its default value is what is set;\n\
615 buffer-local values are not affected.\n\
616 DOCSTRING is optional.\n\
617 If DOCSTRING starts with *, this variable is identified as a user option.\n\
618 This means that M-x set-variable and M-x edit-options recognize it.\n\n\
619 Note: do not use `defconst' for user options in libraries that are not\n\
620 normally loaded, since it is useful for users to be able to specify\n\
621 their own values for such variables before loading the library.\n\
622 Since `defconst' unconditionally assigns the variable,\n\
623 it would override the user's choice.")
624 (args)
625 Lisp_Object args;
626 {
627 register Lisp_Object sym, tem;
628
629 sym = Fcar (args);
630 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
631 error ("too many arguments");
632
633 Fset_default (sym, Feval (Fcar (Fcdr (args))));
634 tem = Fcar (Fcdr (Fcdr (args)));
635 if (!NILP (tem))
636 {
637 if (!NILP (Vpurify_flag))
638 tem = Fpurecopy (tem);
639 Fput (sym, Qvariable_documentation, tem);
640 }
641 LOADHIST_ATTACH (sym);
642 return sym;
643 }
644
645 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
646 "Returns t if VARIABLE is intended to be set and modified by users.\n\
647 \(The alternative is a variable used internally in a Lisp program.)\n\
648 Determined by whether the first character of the documentation\n\
649 for the variable is `*'.")
650 (variable)
651 Lisp_Object variable;
652 {
653 Lisp_Object documentation;
654
655 documentation = Fget (variable, Qvariable_documentation);
656 if (INTEGERP (documentation) && XINT (documentation) < 0)
657 return Qt;
658 if (STRINGP (documentation)
659 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
660 return Qt;
661 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
662 if (CONSP (documentation)
663 && STRINGP (XCONS (documentation)->car)
664 && INTEGERP (XCONS (documentation)->cdr)
665 && XINT (XCONS (documentation)->cdr) < 0)
666 return Qt;
667 return Qnil;
668 }
669 \f
670 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
671 "(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
672 The value of the last form in BODY is returned.\n\
673 Each element of VARLIST is a symbol (which is bound to nil)\n\
674 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
675 Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
676 (args)
677 Lisp_Object args;
678 {
679 Lisp_Object varlist, val, elt;
680 int count = specpdl_ptr - specpdl;
681 struct gcpro gcpro1, gcpro2, gcpro3;
682
683 GCPRO3 (args, elt, varlist);
684
685 varlist = Fcar (args);
686 while (!NILP (varlist))
687 {
688 QUIT;
689 elt = Fcar (varlist);
690 if (SYMBOLP (elt))
691 specbind (elt, Qnil);
692 else if (! NILP (Fcdr (Fcdr (elt))))
693 Fsignal (Qerror,
694 Fcons (build_string ("`let' bindings can have only one value-form"),
695 elt));
696 else
697 {
698 val = Feval (Fcar (Fcdr (elt)));
699 specbind (Fcar (elt), val);
700 }
701 varlist = Fcdr (varlist);
702 }
703 UNGCPRO;
704 val = Fprogn (Fcdr (args));
705 return unbind_to (count, val);
706 }
707
708 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
709 "(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.\n\
710 The value of the last form in BODY is returned.\n\
711 Each element of VARLIST is a symbol (which is bound to nil)\n\
712 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
713 All the VALUEFORMs are evalled before any symbols are bound.")
714 (args)
715 Lisp_Object args;
716 {
717 Lisp_Object *temps, tem;
718 register Lisp_Object elt, varlist;
719 int count = specpdl_ptr - specpdl;
720 register int argnum;
721 struct gcpro gcpro1, gcpro2;
722
723 varlist = Fcar (args);
724
725 /* Make space to hold the values to give the bound variables */
726 elt = Flength (varlist);
727 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
728
729 /* Compute the values and store them in `temps' */
730
731 GCPRO2 (args, *temps);
732 gcpro2.nvars = 0;
733
734 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
735 {
736 QUIT;
737 elt = Fcar (varlist);
738 if (SYMBOLP (elt))
739 temps [argnum++] = Qnil;
740 else if (! NILP (Fcdr (Fcdr (elt))))
741 Fsignal (Qerror,
742 Fcons (build_string ("`let' bindings can have only one value-form"),
743 elt));
744 else
745 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
746 gcpro2.nvars = argnum;
747 }
748 UNGCPRO;
749
750 varlist = Fcar (args);
751 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
752 {
753 elt = Fcar (varlist);
754 tem = temps[argnum++];
755 if (SYMBOLP (elt))
756 specbind (elt, tem);
757 else
758 specbind (Fcar (elt), tem);
759 }
760
761 elt = Fprogn (Fcdr (args));
762 return unbind_to (count, elt);
763 }
764
765 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
766 "(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.\n\
767 The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
768 until TEST returns nil.")
769 (args)
770 Lisp_Object args;
771 {
772 Lisp_Object test, body, tem;
773 struct gcpro gcpro1, gcpro2;
774
775 GCPRO2 (test, body);
776
777 test = Fcar (args);
778 body = Fcdr (args);
779 while (tem = Feval (test),
780 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
781 {
782 QUIT;
783 Fprogn (body);
784 }
785
786 UNGCPRO;
787 return Qnil;
788 }
789
790 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
791 "Return result of expanding macros at top level of FORM.\n\
792 If FORM is not a macro call, it is returned unchanged.\n\
793 Otherwise, the macro is expanded and the expansion is considered\n\
794 in place of FORM. When a non-macro-call results, it is returned.\n\n\
795 The second optional arg ENVIRONMENT species an environment of macro\n\
796 definitions to shadow the loaded ones for use in file byte-compilation.")
797 (form, environment)
798 Lisp_Object form;
799 Lisp_Object environment;
800 {
801 /* With cleanups from Hallvard Furuseth. */
802 register Lisp_Object expander, sym, def, tem;
803
804 while (1)
805 {
806 /* Come back here each time we expand a macro call,
807 in case it expands into another macro call. */
808 if (!CONSP (form))
809 break;
810 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
811 def = sym = XCONS (form)->car;
812 tem = Qnil;
813 /* Trace symbols aliases to other symbols
814 until we get a symbol that is not an alias. */
815 while (SYMBOLP (def))
816 {
817 QUIT;
818 sym = def;
819 tem = Fassq (sym, environment);
820 if (NILP (tem))
821 {
822 def = XSYMBOL (sym)->function;
823 if (!EQ (def, Qunbound))
824 continue;
825 }
826 break;
827 }
828 /* Right now TEM is the result from SYM in ENVIRONMENT,
829 and if TEM is nil then DEF is SYM's function definition. */
830 if (NILP (tem))
831 {
832 /* SYM is not mentioned in ENVIRONMENT.
833 Look at its function definition. */
834 if (EQ (def, Qunbound) || !CONSP (def))
835 /* Not defined or definition not suitable */
836 break;
837 if (EQ (XCONS (def)->car, Qautoload))
838 {
839 /* Autoloading function: will it be a macro when loaded? */
840 tem = Fnth (make_number (4), def);
841 if (EQ (tem, Qt) || EQ (tem, Qmacro))
842 /* Yes, load it and try again. */
843 {
844 struct gcpro gcpro1;
845 GCPRO1 (form);
846 do_autoload (def, sym);
847 UNGCPRO;
848 continue;
849 }
850 else
851 break;
852 }
853 else if (!EQ (XCONS (def)->car, Qmacro))
854 break;
855 else expander = XCONS (def)->cdr;
856 }
857 else
858 {
859 expander = XCONS (tem)->cdr;
860 if (NILP (expander))
861 break;
862 }
863 form = apply1 (expander, XCONS (form)->cdr);
864 }
865 return form;
866 }
867 \f
868 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
869 "(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.\n\
870 TAG is evalled to get the tag to use. Then the BODY is executed.\n\
871 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
872 If no throw happens, `catch' returns the value of the last BODY form.\n\
873 If a throw happens, it specifies the value to return from `catch'.")
874 (args)
875 Lisp_Object args;
876 {
877 register Lisp_Object tag;
878 struct gcpro gcpro1;
879
880 GCPRO1 (args);
881 tag = Feval (Fcar (args));
882 UNGCPRO;
883 return internal_catch (tag, Fprogn, Fcdr (args));
884 }
885
886 /* Set up a catch, then call C function FUNC on argument ARG.
887 FUNC should return a Lisp_Object.
888 This is how catches are done from within C code. */
889
890 Lisp_Object
891 internal_catch (tag, func, arg)
892 Lisp_Object tag;
893 Lisp_Object (*func) ();
894 Lisp_Object arg;
895 {
896 /* This structure is made part of the chain `catchlist'. */
897 struct catchtag c;
898
899 /* Fill in the components of c, and put it on the list. */
900 c.next = catchlist;
901 c.tag = tag;
902 c.val = Qnil;
903 c.backlist = backtrace_list;
904 c.handlerlist = handlerlist;
905 c.lisp_eval_depth = lisp_eval_depth;
906 c.pdlcount = specpdl_ptr - specpdl;
907 c.poll_suppress_count = poll_suppress_count;
908 c.gcpro = gcprolist;
909 catchlist = &c;
910
911 /* Call FUNC. */
912 if (! _setjmp (c.jmp))
913 c.val = (*func) (arg);
914
915 /* Throw works by a longjmp that comes right here. */
916 catchlist = c.next;
917 return c.val;
918 }
919
920 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
921 jump to that CATCH, returning VALUE as the value of that catch.
922
923 This is the guts Fthrow and Fsignal; they differ only in the way
924 they choose the catch tag to throw to. A catch tag for a
925 condition-case form has a TAG of Qnil.
926
927 Before each catch is discarded, unbind all special bindings and
928 execute all unwind-protect clauses made above that catch. Unwind
929 the handler stack as we go, so that the proper handlers are in
930 effect for each unwind-protect clause we run. At the end, restore
931 some static info saved in CATCH, and longjmp to the location
932 specified in the
933
934 This is used for correct unwinding in Fthrow and Fsignal. */
935
936 static void
937 unwind_to_catch (catch, value)
938 struct catchtag *catch;
939 Lisp_Object value;
940 {
941 register int last_time;
942
943 /* Save the value in the tag. */
944 catch->val = value;
945
946 /* Restore the polling-suppression count. */
947 set_poll_suppress_count (catch->poll_suppress_count);
948
949 do
950 {
951 last_time = catchlist == catch;
952
953 /* Unwind the specpdl stack, and then restore the proper set of
954 handlers. */
955 unbind_to (catchlist->pdlcount, Qnil);
956 handlerlist = catchlist->handlerlist;
957 catchlist = catchlist->next;
958 }
959 while (! last_time);
960
961 gcprolist = catch->gcpro;
962 backtrace_list = catch->backlist;
963 lisp_eval_depth = catch->lisp_eval_depth;
964
965 _longjmp (catch->jmp, 1);
966 }
967
968 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
969 "(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.\n\
970 Both TAG and VALUE are evalled.")
971 (tag, value)
972 register Lisp_Object tag, value;
973 {
974 register struct catchtag *c;
975
976 while (1)
977 {
978 if (!NILP (tag))
979 for (c = catchlist; c; c = c->next)
980 {
981 if (EQ (c->tag, tag))
982 unwind_to_catch (c, value);
983 }
984 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
985 }
986 }
987
988
989 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
990 "Do BODYFORM, protecting with UNWINDFORMS.\n\
991 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).\n\
992 If BODYFORM completes normally, its value is returned\n\
993 after executing the UNWINDFORMS.\n\
994 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
995 (args)
996 Lisp_Object args;
997 {
998 Lisp_Object val;
999 int count = specpdl_ptr - specpdl;
1000
1001 record_unwind_protect (0, Fcdr (args));
1002 val = Feval (Fcar (args));
1003 return unbind_to (count, val);
1004 }
1005 \f
1006 /* Chain of condition handlers currently in effect.
1007 The elements of this chain are contained in the stack frames
1008 of Fcondition_case and internal_condition_case.
1009 When an error is signaled (by calling Fsignal, below),
1010 this chain is searched for an element that applies. */
1011
1012 struct handler *handlerlist;
1013
1014 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1015 "Regain control when an error is signaled.\n\
1016 Usage looks like (condition-case VAR BODYFORM HANDLERS...).\n\
1017 executes BODYFORM and returns its value if no error happens.\n\
1018 Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1019 where the BODY is made of Lisp expressions.\n\n\
1020 A handler is applicable to an error\n\
1021 if CONDITION-NAME is one of the error's condition names.\n\
1022 If an error happens, the first applicable handler is run.\n\
1023 \n\
1024 The car of a handler may be a list of condition names\n\
1025 instead of a single condition name.\n\
1026 \n\
1027 When a handler handles an error,\n\
1028 control returns to the condition-case and the handler BODY... is executed\n\
1029 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1030 VAR may be nil; then you do not get access to the signal information.\n\
1031 \n\
1032 The value of the last BODY form is returned from the condition-case.\n\
1033 See also the function `signal' for more info.")
1034 (args)
1035 Lisp_Object args;
1036 {
1037 Lisp_Object val;
1038 struct catchtag c;
1039 struct handler h;
1040 register Lisp_Object var, bodyform, handlers;
1041
1042 var = Fcar (args);
1043 bodyform = Fcar (Fcdr (args));
1044 handlers = Fcdr (Fcdr (args));
1045 CHECK_SYMBOL (var, 0);
1046
1047 for (val = handlers; ! NILP (val); val = Fcdr (val))
1048 {
1049 Lisp_Object tem;
1050 tem = Fcar (val);
1051 if (! (NILP (tem)
1052 || (CONSP (tem)
1053 && (SYMBOLP (XCONS (tem)->car)
1054 || CONSP (XCONS (tem)->car)))))
1055 error ("Invalid condition handler", tem);
1056 }
1057
1058 c.tag = Qnil;
1059 c.val = Qnil;
1060 c.backlist = backtrace_list;
1061 c.handlerlist = handlerlist;
1062 c.lisp_eval_depth = lisp_eval_depth;
1063 c.pdlcount = specpdl_ptr - specpdl;
1064 c.poll_suppress_count = poll_suppress_count;
1065 c.gcpro = gcprolist;
1066 if (_setjmp (c.jmp))
1067 {
1068 if (!NILP (h.var))
1069 specbind (h.var, c.val);
1070 val = Fprogn (Fcdr (h.chosen_clause));
1071
1072 /* Note that this just undoes the binding of h.var; whoever
1073 longjumped to us unwound the stack to c.pdlcount before
1074 throwing. */
1075 unbind_to (c.pdlcount, Qnil);
1076 return val;
1077 }
1078 c.next = catchlist;
1079 catchlist = &c;
1080
1081 h.var = var;
1082 h.handler = handlers;
1083 h.next = handlerlist;
1084 h.tag = &c;
1085 handlerlist = &h;
1086
1087 val = Feval (bodyform);
1088 catchlist = c.next;
1089 handlerlist = h.next;
1090 return val;
1091 }
1092
1093 /* Call the function BFUN with no arguments, catching errors within it
1094 according to HANDLERS. If there is an error, call HFUN with
1095 one argument which is the data that describes the error:
1096 (SIGNALNAME . DATA)
1097
1098 HANDLERS can be a list of conditions to catch.
1099 If HANDLERS is Qt, catch all errors.
1100 If HANDLERS is Qerror, catch all errors
1101 but allow the debugger to run if that is enabled. */
1102
1103 Lisp_Object
1104 internal_condition_case (bfun, handlers, hfun)
1105 Lisp_Object (*bfun) ();
1106 Lisp_Object handlers;
1107 Lisp_Object (*hfun) ();
1108 {
1109 Lisp_Object val;
1110 struct catchtag c;
1111 struct handler h;
1112
1113 /* Since Fsignal resets this to 0, it had better be 0 now
1114 or else we have a potential bug. */
1115 if (interrupt_input_blocked != 0)
1116 abort ();
1117
1118 c.tag = Qnil;
1119 c.val = Qnil;
1120 c.backlist = backtrace_list;
1121 c.handlerlist = handlerlist;
1122 c.lisp_eval_depth = lisp_eval_depth;
1123 c.pdlcount = specpdl_ptr - specpdl;
1124 c.poll_suppress_count = poll_suppress_count;
1125 c.gcpro = gcprolist;
1126 if (_setjmp (c.jmp))
1127 {
1128 return (*hfun) (c.val);
1129 }
1130 c.next = catchlist;
1131 catchlist = &c;
1132 h.handler = handlers;
1133 h.var = Qnil;
1134 h.next = handlerlist;
1135 h.tag = &c;
1136 handlerlist = &h;
1137
1138 val = (*bfun) ();
1139 catchlist = c.next;
1140 handlerlist = h.next;
1141 return val;
1142 }
1143
1144 /* Like internal_condition_case but call HFUN with ARG as its argument. */
1145
1146 Lisp_Object
1147 internal_condition_case_1 (bfun, arg, handlers, hfun)
1148 Lisp_Object (*bfun) ();
1149 Lisp_Object arg;
1150 Lisp_Object handlers;
1151 Lisp_Object (*hfun) ();
1152 {
1153 Lisp_Object val;
1154 struct catchtag c;
1155 struct handler h;
1156
1157 c.tag = Qnil;
1158 c.val = Qnil;
1159 c.backlist = backtrace_list;
1160 c.handlerlist = handlerlist;
1161 c.lisp_eval_depth = lisp_eval_depth;
1162 c.pdlcount = specpdl_ptr - specpdl;
1163 c.poll_suppress_count = poll_suppress_count;
1164 c.gcpro = gcprolist;
1165 if (_setjmp (c.jmp))
1166 {
1167 return (*hfun) (c.val);
1168 }
1169 c.next = catchlist;
1170 catchlist = &c;
1171 h.handler = handlers;
1172 h.var = Qnil;
1173 h.next = handlerlist;
1174 h.tag = &c;
1175 handlerlist = &h;
1176
1177 val = (*bfun) (arg);
1178 catchlist = c.next;
1179 handlerlist = h.next;
1180 return val;
1181 }
1182 \f
1183 static Lisp_Object find_handler_clause ();
1184
1185 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1186 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
1187 This function does not return.\n\n\
1188 An error symbol is a symbol with an `error-conditions' property\n\
1189 that is a list of condition names.\n\
1190 A handler for any of those names will get to handle this signal.\n\
1191 The symbol `error' should normally be one of them.\n\
1192 \n\
1193 DATA should be a list. Its elements are printed as part of the error message.\n\
1194 If the signal is handled, DATA is made available to the handler.\n\
1195 See also the function `condition-case'.")
1196 (error_symbol, data)
1197 Lisp_Object error_symbol, data;
1198 {
1199 register struct handler *allhandlers = handlerlist;
1200 Lisp_Object conditions;
1201 extern int gc_in_progress;
1202 extern int waiting_for_input;
1203 Lisp_Object debugger_value;
1204
1205 quit_error_check ();
1206 immediate_quit = 0;
1207 if (gc_in_progress || waiting_for_input)
1208 abort ();
1209
1210 #ifdef HAVE_WINDOW_SYSTEM
1211 TOTALLY_UNBLOCK_INPUT;
1212 #endif
1213
1214 conditions = Fget (error_symbol, Qerror_conditions);
1215
1216 for (; handlerlist; handlerlist = handlerlist->next)
1217 {
1218 register Lisp_Object clause;
1219 clause = find_handler_clause (handlerlist->handler, conditions,
1220 error_symbol, data, &debugger_value);
1221
1222 #if 0 /* Most callers are not prepared to handle gc if this returns.
1223 So, since this feature is not very useful, take it out. */
1224 /* If have called debugger and user wants to continue,
1225 just return nil. */
1226 if (EQ (clause, Qlambda))
1227 return debugger_value;
1228 #else
1229 if (EQ (clause, Qlambda))
1230 {
1231 /* We can't return values to code which signaled an error, but we
1232 can continue code which has signaled a quit. */
1233 if (EQ (error_symbol, Qquit))
1234 return Qnil;
1235 else
1236 error ("Cannot return from the debugger in an error");
1237 }
1238 #endif
1239
1240 if (!NILP (clause))
1241 {
1242 Lisp_Object unwind_data;
1243 struct handler *h = handlerlist;
1244
1245 handlerlist = allhandlers;
1246 if (EQ (data, memory_signal_data))
1247 unwind_data = memory_signal_data;
1248 else
1249 unwind_data = Fcons (error_symbol, data);
1250 h->chosen_clause = clause;
1251 unwind_to_catch (h->tag, unwind_data);
1252 }
1253 }
1254
1255 handlerlist = allhandlers;
1256 /* If no handler is present now, try to run the debugger,
1257 and if that fails, throw to top level. */
1258 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
1259 Fthrow (Qtop_level, Qt);
1260 }
1261
1262 /* Return nonzero iff LIST is a non-nil atom or
1263 a list containing one of CONDITIONS. */
1264
1265 static int
1266 wants_debugger (list, conditions)
1267 Lisp_Object list, conditions;
1268 {
1269 if (NILP (list))
1270 return 0;
1271 if (! CONSP (list))
1272 return 1;
1273
1274 while (CONSP (conditions))
1275 {
1276 Lisp_Object this, tail;
1277 this = XCONS (conditions)->car;
1278 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
1279 if (EQ (XCONS (tail)->car, this))
1280 return 1;
1281 conditions = XCONS (conditions)->cdr;
1282 }
1283 return 0;
1284 }
1285
1286 /* Return 1 if an error with condition-symbols CONDITIONS,
1287 and described by SIGNAL-DATA, should skip the debugger
1288 according to debugger-ignore-errors. */
1289
1290 static int
1291 skip_debugger (conditions, data)
1292 Lisp_Object conditions, data;
1293 {
1294 Lisp_Object tail;
1295 int first_string = 1;
1296 Lisp_Object error_message;
1297
1298 for (tail = Vdebug_ignored_errors; CONSP (tail);
1299 tail = XCONS (tail)->cdr)
1300 {
1301 if (STRINGP (XCONS (tail)->car))
1302 {
1303 if (first_string)
1304 {
1305 error_message = Ferror_message_string (data);
1306 first_string = 0;
1307 }
1308 if (fast_string_match (XCONS (tail)->car, error_message) >= 0)
1309 return 1;
1310 }
1311 else
1312 {
1313 Lisp_Object contail;
1314
1315 for (contail = conditions; CONSP (contail);
1316 contail = XCONS (contail)->cdr)
1317 if (EQ (XCONS (tail)->car, XCONS (contail)->car))
1318 return 1;
1319 }
1320 }
1321
1322 return 0;
1323 }
1324
1325 /* Value of Qlambda means we have called debugger and user has continued.
1326 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
1327
1328 static Lisp_Object
1329 find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1330 Lisp_Object handlers, conditions, sig, data;
1331 Lisp_Object *debugger_value_ptr;
1332 {
1333 register Lisp_Object h;
1334 register Lisp_Object tem;
1335
1336 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1337 return Qt;
1338 if (EQ (handlers, Qerror)) /* error is used similarly, but means display a backtrace too */
1339 {
1340 if (wants_debugger (Vstack_trace_on_error, conditions))
1341 internal_with_output_to_temp_buffer ("*Backtrace*", Fbacktrace, Qnil);
1342 if ((EQ (sig, Qquit)
1343 ? debug_on_quit
1344 : wants_debugger (Vdebug_on_error, conditions))
1345 && ! skip_debugger (conditions, Fcons (sig, data))
1346 && when_entered_debugger < num_nonmacro_input_chars)
1347 {
1348 int count = specpdl_ptr - specpdl;
1349 specbind (Qdebug_on_error, Qnil);
1350 *debugger_value_ptr
1351 = call_debugger (Fcons (Qerror,
1352 Fcons (Fcons (sig, data),
1353 Qnil)));
1354 return unbind_to (count, Qlambda);
1355 }
1356 return Qt;
1357 }
1358 for (h = handlers; CONSP (h); h = Fcdr (h))
1359 {
1360 Lisp_Object handler, condit;
1361
1362 handler = Fcar (h);
1363 if (!CONSP (handler))
1364 continue;
1365 condit = Fcar (handler);
1366 /* Handle a single condition name in handler HANDLER. */
1367 if (SYMBOLP (condit))
1368 {
1369 tem = Fmemq (Fcar (handler), conditions);
1370 if (!NILP (tem))
1371 return handler;
1372 }
1373 /* Handle a list of condition names in handler HANDLER. */
1374 else if (CONSP (condit))
1375 {
1376 while (CONSP (condit))
1377 {
1378 tem = Fmemq (Fcar (condit), conditions);
1379 if (!NILP (tem))
1380 return handler;
1381 condit = XCONS (condit)->cdr;
1382 }
1383 }
1384 }
1385 return Qnil;
1386 }
1387
1388 /* dump an error message; called like printf */
1389
1390 /* VARARGS 1 */
1391 void
1392 error (m, a1, a2, a3)
1393 char *m;
1394 char *a1, *a2, *a3;
1395 {
1396 char buf[200];
1397 int size = 200;
1398 int mlen;
1399 char *buffer = buf;
1400 char *args[3];
1401 int allocated = 0;
1402 Lisp_Object string;
1403
1404 args[0] = a1;
1405 args[1] = a2;
1406 args[2] = a3;
1407
1408 mlen = strlen (m);
1409
1410 while (1)
1411 {
1412 int used = doprnt (buf, size, m, m + mlen, 3, args);
1413 if (used < size)
1414 break;
1415 size *= 2;
1416 if (allocated)
1417 buffer = (char *) xrealloc (buffer, size);
1418 else
1419 {
1420 buffer = (char *) xmalloc (size);
1421 allocated = 1;
1422 }
1423 }
1424
1425 string = build_string (buf);
1426 if (allocated)
1427 free (buffer);
1428
1429 Fsignal (Qerror, Fcons (string, Qnil));
1430 }
1431 \f
1432 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1433 "T if FUNCTION makes provisions for interactive calling.\n\
1434 This means it contains a description for how to read arguments to give it.\n\
1435 The value is nil for an invalid function or a symbol with no function\n\
1436 definition.\n\
1437 \n\
1438 Interactively callable functions include strings and vectors (treated\n\
1439 as keyboard macros), lambda-expressions that contain a top-level call\n\
1440 to `interactive', autoload definitions made by `autoload' with non-nil\n\
1441 fourth argument, and some of the built-in functions of Lisp.\n\
1442 \n\
1443 Also, a symbol satisfies `commandp' if its function definition does so.")
1444 (function)
1445 Lisp_Object function;
1446 {
1447 register Lisp_Object fun;
1448 register Lisp_Object funcar;
1449 register Lisp_Object tem;
1450 register int i = 0;
1451
1452 fun = function;
1453
1454 fun = indirect_function (fun);
1455 if (EQ (fun, Qunbound))
1456 return Qnil;
1457
1458 /* Emacs primitives are interactive if their DEFUN specifies an
1459 interactive spec. */
1460 if (SUBRP (fun))
1461 {
1462 if (XSUBR (fun)->prompt)
1463 return Qt;
1464 else
1465 return Qnil;
1466 }
1467
1468 /* Bytecode objects are interactive if they are long enough to
1469 have an element whose index is COMPILED_INTERACTIVE, which is
1470 where the interactive spec is stored. */
1471 else if (COMPILEDP (fun))
1472 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1473 ? Qt : Qnil);
1474
1475 /* Strings and vectors are keyboard macros. */
1476 if (STRINGP (fun) || VECTORP (fun))
1477 return Qt;
1478
1479 /* Lists may represent commands. */
1480 if (!CONSP (fun))
1481 return Qnil;
1482 funcar = Fcar (fun);
1483 if (!SYMBOLP (funcar))
1484 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1485 if (EQ (funcar, Qlambda))
1486 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1487 if (EQ (funcar, Qmocklisp))
1488 return Qt; /* All mocklisp functions can be called interactively */
1489 if (EQ (funcar, Qautoload))
1490 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1491 else
1492 return Qnil;
1493 }
1494
1495 /* ARGSUSED */
1496 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1497 "Define FUNCTION to autoload from FILE.\n\
1498 FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1499 Third arg DOCSTRING is documentation for the function.\n\
1500 Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
1501 Fifth arg TYPE indicates the type of the object:\n\
1502 nil or omitted says FUNCTION is a function,\n\
1503 `keymap' says FUNCTION is really a keymap, and\n\
1504 `macro' or t says FUNCTION is really a macro.\n\
1505 Third through fifth args give info about the real definition.\n\
1506 They default to nil.\n\
1507 If FUNCTION is already defined other than as an autoload,\n\
1508 this does nothing and returns nil.")
1509 (function, file, docstring, interactive, type)
1510 Lisp_Object function, file, docstring, interactive, type;
1511 {
1512 #ifdef NO_ARG_ARRAY
1513 Lisp_Object args[4];
1514 #endif
1515
1516 CHECK_SYMBOL (function, 0);
1517 CHECK_STRING (file, 1);
1518
1519 /* If function is defined and not as an autoload, don't override */
1520 if (!EQ (XSYMBOL (function)->function, Qunbound)
1521 && !(CONSP (XSYMBOL (function)->function)
1522 && EQ (XCONS (XSYMBOL (function)->function)->car, Qautoload)))
1523 return Qnil;
1524
1525 #ifdef NO_ARG_ARRAY
1526 args[0] = file;
1527 args[1] = docstring;
1528 args[2] = interactive;
1529 args[3] = type;
1530
1531 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1532 #else /* NO_ARG_ARRAY */
1533 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1534 #endif /* not NO_ARG_ARRAY */
1535 }
1536
1537 Lisp_Object
1538 un_autoload (oldqueue)
1539 Lisp_Object oldqueue;
1540 {
1541 register Lisp_Object queue, first, second;
1542
1543 /* Queue to unwind is current value of Vautoload_queue.
1544 oldqueue is the shadowed value to leave in Vautoload_queue. */
1545 queue = Vautoload_queue;
1546 Vautoload_queue = oldqueue;
1547 while (CONSP (queue))
1548 {
1549 first = Fcar (queue);
1550 second = Fcdr (first);
1551 first = Fcar (first);
1552 if (EQ (second, Qnil))
1553 Vfeatures = first;
1554 else
1555 Ffset (first, second);
1556 queue = Fcdr (queue);
1557 }
1558 return Qnil;
1559 }
1560
1561 /* Load an autoloaded function.
1562 FUNNAME is the symbol which is the function's name.
1563 FUNDEF is the autoload definition (a list). */
1564
1565 do_autoload (fundef, funname)
1566 Lisp_Object fundef, funname;
1567 {
1568 int count = specpdl_ptr - specpdl;
1569 Lisp_Object fun, val, queue, first, second;
1570 struct gcpro gcpro1, gcpro2, gcpro3;
1571
1572 fun = funname;
1573 CHECK_SYMBOL (funname, 0);
1574 GCPRO3 (fun, funname, fundef);
1575
1576 /* Value saved here is to be restored into Vautoload_queue */
1577 record_unwind_protect (un_autoload, Vautoload_queue);
1578 Vautoload_queue = Qt;
1579 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
1580
1581 /* Save the old autoloads, in case we ever do an unload. */
1582 queue = Vautoload_queue;
1583 while (CONSP (queue))
1584 {
1585 first = Fcar (queue);
1586 second = Fcdr (first);
1587 first = Fcar (first);
1588
1589 /* Note: This test is subtle. The cdr of an autoload-queue entry
1590 may be an atom if the autoload entry was generated by a defalias
1591 or fset. */
1592 if (CONSP (second))
1593 Fput (first, Qautoload, (Fcdr (second)));
1594
1595 queue = Fcdr (queue);
1596 }
1597
1598 /* Once loading finishes, don't undo it. */
1599 Vautoload_queue = Qt;
1600 unbind_to (count, Qnil);
1601
1602 fun = Findirect_function (fun);
1603
1604 if (!NILP (Fequal (fun, fundef)))
1605 error ("Autoloading failed to define function %s",
1606 XSYMBOL (funname)->name->data);
1607 UNGCPRO;
1608 }
1609 \f
1610 DEFUN ("eval", Feval, Seval, 1, 1, 0,
1611 "Evaluate FORM and return its value.")
1612 (form)
1613 Lisp_Object form;
1614 {
1615 Lisp_Object fun, val, original_fun, original_args;
1616 Lisp_Object funcar;
1617 struct backtrace backtrace;
1618 struct gcpro gcpro1, gcpro2, gcpro3;
1619
1620 if (SYMBOLP (form))
1621 {
1622 if (EQ (Vmocklisp_arguments, Qt))
1623 return Fsymbol_value (form);
1624 val = Fsymbol_value (form);
1625 if (NILP (val))
1626 XSETFASTINT (val, 0);
1627 else if (EQ (val, Qt))
1628 XSETFASTINT (val, 1);
1629 return val;
1630 }
1631 if (!CONSP (form))
1632 return form;
1633
1634 QUIT;
1635 if (consing_since_gc > gc_cons_threshold)
1636 {
1637 GCPRO1 (form);
1638 Fgarbage_collect ();
1639 UNGCPRO;
1640 }
1641
1642 if (++lisp_eval_depth > max_lisp_eval_depth)
1643 {
1644 if (max_lisp_eval_depth < 100)
1645 max_lisp_eval_depth = 100;
1646 if (lisp_eval_depth > max_lisp_eval_depth)
1647 error ("Lisp nesting exceeds max-lisp-eval-depth");
1648 }
1649
1650 original_fun = Fcar (form);
1651 original_args = Fcdr (form);
1652
1653 backtrace.next = backtrace_list;
1654 backtrace_list = &backtrace;
1655 backtrace.function = &original_fun; /* This also protects them from gc */
1656 backtrace.args = &original_args;
1657 backtrace.nargs = UNEVALLED;
1658 backtrace.evalargs = 1;
1659 backtrace.debug_on_exit = 0;
1660
1661 if (debug_on_next_call)
1662 do_debug_on_call (Qt);
1663
1664 /* At this point, only original_fun and original_args
1665 have values that will be used below */
1666 retry:
1667 fun = Findirect_function (original_fun);
1668
1669 if (SUBRP (fun))
1670 {
1671 Lisp_Object numargs;
1672 Lisp_Object argvals[7];
1673 Lisp_Object args_left;
1674 register int i, maxargs;
1675
1676 args_left = original_args;
1677 numargs = Flength (args_left);
1678
1679 if (XINT (numargs) < XSUBR (fun)->min_args ||
1680 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1681 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1682
1683 if (XSUBR (fun)->max_args == UNEVALLED)
1684 {
1685 backtrace.evalargs = 0;
1686 val = (*XSUBR (fun)->function) (args_left);
1687 goto done;
1688 }
1689
1690 if (XSUBR (fun)->max_args == MANY)
1691 {
1692 /* Pass a vector of evaluated arguments */
1693 Lisp_Object *vals;
1694 register int argnum = 0;
1695
1696 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1697
1698 GCPRO3 (args_left, fun, fun);
1699 gcpro3.var = vals;
1700 gcpro3.nvars = 0;
1701
1702 while (!NILP (args_left))
1703 {
1704 vals[argnum++] = Feval (Fcar (args_left));
1705 args_left = Fcdr (args_left);
1706 gcpro3.nvars = argnum;
1707 }
1708
1709 backtrace.args = vals;
1710 backtrace.nargs = XINT (numargs);
1711
1712 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
1713 UNGCPRO;
1714 goto done;
1715 }
1716
1717 GCPRO3 (args_left, fun, fun);
1718 gcpro3.var = argvals;
1719 gcpro3.nvars = 0;
1720
1721 maxargs = XSUBR (fun)->max_args;
1722 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1723 {
1724 argvals[i] = Feval (Fcar (args_left));
1725 gcpro3.nvars = ++i;
1726 }
1727
1728 UNGCPRO;
1729
1730 backtrace.args = argvals;
1731 backtrace.nargs = XINT (numargs);
1732
1733 switch (i)
1734 {
1735 case 0:
1736 val = (*XSUBR (fun)->function) ();
1737 goto done;
1738 case 1:
1739 val = (*XSUBR (fun)->function) (argvals[0]);
1740 goto done;
1741 case 2:
1742 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1743 goto done;
1744 case 3:
1745 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1746 argvals[2]);
1747 goto done;
1748 case 4:
1749 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1750 argvals[2], argvals[3]);
1751 goto done;
1752 case 5:
1753 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1754 argvals[3], argvals[4]);
1755 goto done;
1756 case 6:
1757 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1758 argvals[3], argvals[4], argvals[5]);
1759 goto done;
1760 case 7:
1761 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1762 argvals[3], argvals[4], argvals[5],
1763 argvals[6]);
1764 goto done;
1765
1766 default:
1767 /* Someone has created a subr that takes more arguments than
1768 is supported by this code. We need to either rewrite the
1769 subr to use a different argument protocol, or add more
1770 cases to this switch. */
1771 abort ();
1772 }
1773 }
1774 if (COMPILEDP (fun))
1775 val = apply_lambda (fun, original_args, 1);
1776 else
1777 {
1778 if (!CONSP (fun))
1779 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1780 funcar = Fcar (fun);
1781 if (!SYMBOLP (funcar))
1782 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1783 if (EQ (funcar, Qautoload))
1784 {
1785 do_autoload (fun, original_fun);
1786 goto retry;
1787 }
1788 if (EQ (funcar, Qmacro))
1789 val = Feval (apply1 (Fcdr (fun), original_args));
1790 else if (EQ (funcar, Qlambda))
1791 val = apply_lambda (fun, original_args, 1);
1792 else if (EQ (funcar, Qmocklisp))
1793 val = ml_apply (fun, original_args);
1794 else
1795 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1796 }
1797 done:
1798 if (!EQ (Vmocklisp_arguments, Qt))
1799 {
1800 if (NILP (val))
1801 XSETFASTINT (val, 0);
1802 else if (EQ (val, Qt))
1803 XSETFASTINT (val, 1);
1804 }
1805 lisp_eval_depth--;
1806 if (backtrace.debug_on_exit)
1807 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1808 backtrace_list = backtrace.next;
1809 return val;
1810 }
1811 \f
1812 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1813 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
1814 Then return the value FUNCTION returns.\n\
1815 Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1816 (nargs, args)
1817 int nargs;
1818 Lisp_Object *args;
1819 {
1820 register int i, numargs;
1821 register Lisp_Object spread_arg;
1822 register Lisp_Object *funcall_args;
1823 Lisp_Object fun;
1824 struct gcpro gcpro1;
1825
1826 fun = args [0];
1827 funcall_args = 0;
1828 spread_arg = args [nargs - 1];
1829 CHECK_LIST (spread_arg, nargs);
1830
1831 numargs = XINT (Flength (spread_arg));
1832
1833 if (numargs == 0)
1834 return Ffuncall (nargs - 1, args);
1835 else if (numargs == 1)
1836 {
1837 args [nargs - 1] = XCONS (spread_arg)->car;
1838 return Ffuncall (nargs, args);
1839 }
1840
1841 numargs += nargs - 2;
1842
1843 fun = indirect_function (fun);
1844 if (EQ (fun, Qunbound))
1845 {
1846 /* Let funcall get the error */
1847 fun = args[0];
1848 goto funcall;
1849 }
1850
1851 if (SUBRP (fun))
1852 {
1853 if (numargs < XSUBR (fun)->min_args
1854 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
1855 goto funcall; /* Let funcall get the error */
1856 else if (XSUBR (fun)->max_args > numargs)
1857 {
1858 /* Avoid making funcall cons up a yet another new vector of arguments
1859 by explicitly supplying nil's for optional values */
1860 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
1861 * sizeof (Lisp_Object));
1862 for (i = numargs; i < XSUBR (fun)->max_args;)
1863 funcall_args[++i] = Qnil;
1864 GCPRO1 (*funcall_args);
1865 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
1866 }
1867 }
1868 funcall:
1869 /* We add 1 to numargs because funcall_args includes the
1870 function itself as well as its arguments. */
1871 if (!funcall_args)
1872 {
1873 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
1874 * sizeof (Lisp_Object));
1875 GCPRO1 (*funcall_args);
1876 gcpro1.nvars = 1 + numargs;
1877 }
1878
1879 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
1880 /* Spread the last arg we got. Its first element goes in
1881 the slot that it used to occupy, hence this value of I. */
1882 i = nargs - 1;
1883 while (!NILP (spread_arg))
1884 {
1885 funcall_args [i++] = XCONS (spread_arg)->car;
1886 spread_arg = XCONS (spread_arg)->cdr;
1887 }
1888
1889 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
1890 }
1891 \f
1892 /* Run hook variables in various ways. */
1893
1894 enum run_hooks_condition {to_completion, until_success, until_failure};
1895
1896 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
1897 "Run each hook in HOOKS. Major mode functions use this.\n\
1898 Each argument should be a symbol, a hook variable.\n\
1899 These symbols are processed in the order specified.\n\
1900 If a hook symbol has a non-nil value, that value may be a function\n\
1901 or a list of functions to be called to run the hook.\n\
1902 If the value is a function, it is called with no arguments.\n\
1903 If it is a list, the elements are called, in order, with no arguments.\n\
1904 \n\
1905 To make a hook variable buffer-local, use `make-local-hook',\n\
1906 not `make-local-variable'.")
1907 (nargs, args)
1908 int nargs;
1909 Lisp_Object *args;
1910 {
1911 Lisp_Object hook[1];
1912 register int i;
1913
1914 for (i = 0; i < nargs; i++)
1915 {
1916 hook[0] = args[i];
1917 run_hook_with_args (1, hook, to_completion);
1918 }
1919
1920 return Qnil;
1921 }
1922
1923 DEFUN ("run-hook-with-args",
1924 Frun_hook_with_args, Srun_hook_with_args, 1, MANY, 0,
1925 "Run HOOK with the specified arguments ARGS.\n\
1926 HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
1927 value, that value may be a function or a list of functions to be\n\
1928 called to run the hook. If the value is a function, it is called with\n\
1929 the given arguments and its return value is returned. If it is a list\n\
1930 of functions, those functions are called, in order,\n\
1931 with the given arguments ARGS.\n\
1932 It is best not to depend on the value return by `run-hook-with-args',\n\
1933 as that may change.\n\
1934 \n\
1935 To make a hook variable buffer-local, use `make-local-hook',\n\
1936 not `make-local-variable'.")
1937 (nargs, args)
1938 int nargs;
1939 Lisp_Object *args;
1940 {
1941 return run_hook_with_args (nargs, args, to_completion);
1942 }
1943
1944 DEFUN ("run-hook-with-args-until-success",
1945 Frun_hook_with_args_until_success, Srun_hook_with_args_until_success,
1946 1, MANY, 0,
1947 "Run HOOK with the specified arguments ARGS.\n\
1948 HOOK should be a symbol, a hook variable. Its value should\n\
1949 be a list of functions. We call those functions, one by one,\n\
1950 passing arguments ARGS to each of them, until one of them\n\
1951 returns a non-nil value. Then we return that value.\n\
1952 If all the functions return nil, we return nil.\n\
1953 \n\
1954 To make a hook variable buffer-local, use `make-local-hook',\n\
1955 not `make-local-variable'.")
1956 (nargs, args)
1957 int nargs;
1958 Lisp_Object *args;
1959 {
1960 return run_hook_with_args (nargs, args, until_success);
1961 }
1962
1963 DEFUN ("run-hook-with-args-until-failure",
1964 Frun_hook_with_args_until_failure, Srun_hook_with_args_until_failure,
1965 1, MANY, 0,
1966 "Run HOOK with the specified arguments ARGS.\n\
1967 HOOK should be a symbol, a hook variable. Its value should\n\
1968 be a list of functions. We call those functions, one by one,\n\
1969 passing arguments ARGS to each of them, until one of them\n\
1970 returns nil. Then we return nil.\n\
1971 If all the functions return non-nil, we return non-nil.\n\
1972 \n\
1973 To make a hook variable buffer-local, use `make-local-hook',\n\
1974 not `make-local-variable'.")
1975 (nargs, args)
1976 int nargs;
1977 Lisp_Object *args;
1978 {
1979 return run_hook_with_args (nargs, args, until_failure);
1980 }
1981
1982 /* ARGS[0] should be a hook symbol.
1983 Call each of the functions in the hook value, passing each of them
1984 as arguments all the rest of ARGS (all NARGS - 1 elements).
1985 COND specifies a condition to test after each call
1986 to decide whether to stop.
1987 The caller (or its caller, etc) must gcpro all of ARGS,
1988 except that it isn't necessary to gcpro ARGS[0]. */
1989
1990 Lisp_Object
1991 run_hook_with_args (nargs, args, cond)
1992 int nargs;
1993 Lisp_Object *args;
1994 enum run_hooks_condition cond;
1995 {
1996 Lisp_Object sym, val, ret;
1997 struct gcpro gcpro1, gcpro2;
1998
1999 /* If we are dying or still initializing,
2000 don't do anything--it would probably crash if we tried. */
2001 if (NILP (Vrun_hooks))
2002 return;
2003
2004 sym = args[0];
2005 val = find_symbol_value (sym);
2006 ret = (cond == until_failure ? Qt : Qnil);
2007
2008 if (EQ (val, Qunbound) || NILP (val))
2009 return ret;
2010 else if (!CONSP (val) || EQ (XCONS (val)->car, Qlambda))
2011 {
2012 args[0] = val;
2013 return Ffuncall (nargs, args);
2014 }
2015 else
2016 {
2017 GCPRO2 (sym, val);
2018
2019 for (;
2020 CONSP (val) && ((cond == to_completion)
2021 || (cond == until_success ? NILP (ret)
2022 : !NILP (ret)));
2023 val = XCONS (val)->cdr)
2024 {
2025 if (EQ (XCONS (val)->car, Qt))
2026 {
2027 /* t indicates this hook has a local binding;
2028 it means to run the global binding too. */
2029 Lisp_Object globals;
2030
2031 for (globals = Fdefault_value (sym);
2032 CONSP (globals) && ((cond == to_completion)
2033 || (cond == until_success ? NILP (ret)
2034 : !NILP (ret)));
2035 globals = XCONS (globals)->cdr)
2036 {
2037 args[0] = XCONS (globals)->car;
2038 /* In a global value, t should not occur. If it does, we
2039 must ignore it to avoid an endless loop. */
2040 if (!EQ (args[0], Qt))
2041 ret = Ffuncall (nargs, args);
2042 }
2043 }
2044 else
2045 {
2046 args[0] = XCONS (val)->car;
2047 ret = Ffuncall (nargs, args);
2048 }
2049 }
2050
2051 UNGCPRO;
2052 return ret;
2053 }
2054 }
2055
2056 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2057 present value of that symbol.
2058 Call each element of FUNLIST,
2059 passing each of them the rest of ARGS.
2060 The caller (or its caller, etc) must gcpro all of ARGS,
2061 except that it isn't necessary to gcpro ARGS[0]. */
2062
2063 Lisp_Object
2064 run_hook_list_with_args (funlist, nargs, args)
2065 Lisp_Object funlist;
2066 int nargs;
2067 Lisp_Object *args;
2068 {
2069 Lisp_Object sym;
2070 Lisp_Object val;
2071 struct gcpro gcpro1, gcpro2;
2072
2073 sym = args[0];
2074 GCPRO2 (sym, val);
2075
2076 for (val = funlist; CONSP (val); val = XCONS (val)->cdr)
2077 {
2078 if (EQ (XCONS (val)->car, Qt))
2079 {
2080 /* t indicates this hook has a local binding;
2081 it means to run the global binding too. */
2082 Lisp_Object globals;
2083
2084 for (globals = Fdefault_value (sym);
2085 CONSP (globals);
2086 globals = XCONS (globals)->cdr)
2087 {
2088 args[0] = XCONS (globals)->car;
2089 /* In a global value, t should not occur. If it does, we
2090 must ignore it to avoid an endless loop. */
2091 if (!EQ (args[0], Qt))
2092 Ffuncall (nargs, args);
2093 }
2094 }
2095 else
2096 {
2097 args[0] = XCONS (val)->car;
2098 Ffuncall (nargs, args);
2099 }
2100 }
2101 UNGCPRO;
2102 return Qnil;
2103 }
2104
2105 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2106
2107 void
2108 run_hook_with_args_2 (hook, arg1, arg2)
2109 Lisp_Object hook, arg1, arg2;
2110 {
2111 Lisp_Object temp[3];
2112 temp[0] = hook;
2113 temp[1] = arg1;
2114 temp[2] = arg2;
2115
2116 Frun_hook_with_args (3, temp);
2117 }
2118 \f
2119 /* Apply fn to arg */
2120 Lisp_Object
2121 apply1 (fn, arg)
2122 Lisp_Object fn, arg;
2123 {
2124 struct gcpro gcpro1;
2125
2126 GCPRO1 (fn);
2127 if (NILP (arg))
2128 RETURN_UNGCPRO (Ffuncall (1, &fn));
2129 gcpro1.nvars = 2;
2130 #ifdef NO_ARG_ARRAY
2131 {
2132 Lisp_Object args[2];
2133 args[0] = fn;
2134 args[1] = arg;
2135 gcpro1.var = args;
2136 RETURN_UNGCPRO (Fapply (2, args));
2137 }
2138 #else /* not NO_ARG_ARRAY */
2139 RETURN_UNGCPRO (Fapply (2, &fn));
2140 #endif /* not NO_ARG_ARRAY */
2141 }
2142
2143 /* Call function fn on no arguments */
2144 Lisp_Object
2145 call0 (fn)
2146 Lisp_Object fn;
2147 {
2148 struct gcpro gcpro1;
2149
2150 GCPRO1 (fn);
2151 RETURN_UNGCPRO (Ffuncall (1, &fn));
2152 }
2153
2154 /* Call function fn with 1 argument arg1 */
2155 /* ARGSUSED */
2156 Lisp_Object
2157 call1 (fn, arg1)
2158 Lisp_Object fn, arg1;
2159 {
2160 struct gcpro gcpro1;
2161 #ifdef NO_ARG_ARRAY
2162 Lisp_Object args[2];
2163
2164 args[0] = fn;
2165 args[1] = arg1;
2166 GCPRO1 (args[0]);
2167 gcpro1.nvars = 2;
2168 RETURN_UNGCPRO (Ffuncall (2, args));
2169 #else /* not NO_ARG_ARRAY */
2170 GCPRO1 (fn);
2171 gcpro1.nvars = 2;
2172 RETURN_UNGCPRO (Ffuncall (2, &fn));
2173 #endif /* not NO_ARG_ARRAY */
2174 }
2175
2176 /* Call function fn with 2 arguments arg1, arg2 */
2177 /* ARGSUSED */
2178 Lisp_Object
2179 call2 (fn, arg1, arg2)
2180 Lisp_Object fn, arg1, arg2;
2181 {
2182 struct gcpro gcpro1;
2183 #ifdef NO_ARG_ARRAY
2184 Lisp_Object args[3];
2185 args[0] = fn;
2186 args[1] = arg1;
2187 args[2] = arg2;
2188 GCPRO1 (args[0]);
2189 gcpro1.nvars = 3;
2190 RETURN_UNGCPRO (Ffuncall (3, args));
2191 #else /* not NO_ARG_ARRAY */
2192 GCPRO1 (fn);
2193 gcpro1.nvars = 3;
2194 RETURN_UNGCPRO (Ffuncall (3, &fn));
2195 #endif /* not NO_ARG_ARRAY */
2196 }
2197
2198 /* Call function fn with 3 arguments arg1, arg2, arg3 */
2199 /* ARGSUSED */
2200 Lisp_Object
2201 call3 (fn, arg1, arg2, arg3)
2202 Lisp_Object fn, arg1, arg2, arg3;
2203 {
2204 struct gcpro gcpro1;
2205 #ifdef NO_ARG_ARRAY
2206 Lisp_Object args[4];
2207 args[0] = fn;
2208 args[1] = arg1;
2209 args[2] = arg2;
2210 args[3] = arg3;
2211 GCPRO1 (args[0]);
2212 gcpro1.nvars = 4;
2213 RETURN_UNGCPRO (Ffuncall (4, args));
2214 #else /* not NO_ARG_ARRAY */
2215 GCPRO1 (fn);
2216 gcpro1.nvars = 4;
2217 RETURN_UNGCPRO (Ffuncall (4, &fn));
2218 #endif /* not NO_ARG_ARRAY */
2219 }
2220
2221 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
2222 /* ARGSUSED */
2223 Lisp_Object
2224 call4 (fn, arg1, arg2, arg3, arg4)
2225 Lisp_Object fn, arg1, arg2, arg3, arg4;
2226 {
2227 struct gcpro gcpro1;
2228 #ifdef NO_ARG_ARRAY
2229 Lisp_Object args[5];
2230 args[0] = fn;
2231 args[1] = arg1;
2232 args[2] = arg2;
2233 args[3] = arg3;
2234 args[4] = arg4;
2235 GCPRO1 (args[0]);
2236 gcpro1.nvars = 5;
2237 RETURN_UNGCPRO (Ffuncall (5, args));
2238 #else /* not NO_ARG_ARRAY */
2239 GCPRO1 (fn);
2240 gcpro1.nvars = 5;
2241 RETURN_UNGCPRO (Ffuncall (5, &fn));
2242 #endif /* not NO_ARG_ARRAY */
2243 }
2244
2245 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2246 /* ARGSUSED */
2247 Lisp_Object
2248 call5 (fn, arg1, arg2, arg3, arg4, arg5)
2249 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2250 {
2251 struct gcpro gcpro1;
2252 #ifdef NO_ARG_ARRAY
2253 Lisp_Object args[6];
2254 args[0] = fn;
2255 args[1] = arg1;
2256 args[2] = arg2;
2257 args[3] = arg3;
2258 args[4] = arg4;
2259 args[5] = arg5;
2260 GCPRO1 (args[0]);
2261 gcpro1.nvars = 6;
2262 RETURN_UNGCPRO (Ffuncall (6, args));
2263 #else /* not NO_ARG_ARRAY */
2264 GCPRO1 (fn);
2265 gcpro1.nvars = 6;
2266 RETURN_UNGCPRO (Ffuncall (6, &fn));
2267 #endif /* not NO_ARG_ARRAY */
2268 }
2269
2270 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2271 /* ARGSUSED */
2272 Lisp_Object
2273 call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2274 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2275 {
2276 struct gcpro gcpro1;
2277 #ifdef NO_ARG_ARRAY
2278 Lisp_Object args[7];
2279 args[0] = fn;
2280 args[1] = arg1;
2281 args[2] = arg2;
2282 args[3] = arg3;
2283 args[4] = arg4;
2284 args[5] = arg5;
2285 args[6] = arg6;
2286 GCPRO1 (args[0]);
2287 gcpro1.nvars = 7;
2288 RETURN_UNGCPRO (Ffuncall (7, args));
2289 #else /* not NO_ARG_ARRAY */
2290 GCPRO1 (fn);
2291 gcpro1.nvars = 7;
2292 RETURN_UNGCPRO (Ffuncall (7, &fn));
2293 #endif /* not NO_ARG_ARRAY */
2294 }
2295
2296 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2297 "Call first argument as a function, passing remaining arguments to it.\n\
2298 Return the value that function returns.\n\
2299 Thus, (funcall 'cons 'x 'y) returns (x . y).")
2300 (nargs, args)
2301 int nargs;
2302 Lisp_Object *args;
2303 {
2304 Lisp_Object fun;
2305 Lisp_Object funcar;
2306 int numargs = nargs - 1;
2307 Lisp_Object lisp_numargs;
2308 Lisp_Object val;
2309 struct backtrace backtrace;
2310 register Lisp_Object *internal_args;
2311 register int i;
2312
2313 QUIT;
2314 if (consing_since_gc > gc_cons_threshold)
2315 Fgarbage_collect ();
2316
2317 if (++lisp_eval_depth > max_lisp_eval_depth)
2318 {
2319 if (max_lisp_eval_depth < 100)
2320 max_lisp_eval_depth = 100;
2321 if (lisp_eval_depth > max_lisp_eval_depth)
2322 error ("Lisp nesting exceeds max-lisp-eval-depth");
2323 }
2324
2325 backtrace.next = backtrace_list;
2326 backtrace_list = &backtrace;
2327 backtrace.function = &args[0];
2328 backtrace.args = &args[1];
2329 backtrace.nargs = nargs - 1;
2330 backtrace.evalargs = 0;
2331 backtrace.debug_on_exit = 0;
2332
2333 if (debug_on_next_call)
2334 do_debug_on_call (Qlambda);
2335
2336 retry:
2337
2338 fun = args[0];
2339
2340 fun = Findirect_function (fun);
2341
2342 if (SUBRP (fun))
2343 {
2344 if (numargs < XSUBR (fun)->min_args
2345 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2346 {
2347 XSETFASTINT (lisp_numargs, numargs);
2348 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2349 }
2350
2351 if (XSUBR (fun)->max_args == UNEVALLED)
2352 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2353
2354 if (XSUBR (fun)->max_args == MANY)
2355 {
2356 val = (*XSUBR (fun)->function) (numargs, args + 1);
2357 goto done;
2358 }
2359
2360 if (XSUBR (fun)->max_args > numargs)
2361 {
2362 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2363 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2364 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2365 internal_args[i] = Qnil;
2366 }
2367 else
2368 internal_args = args + 1;
2369 switch (XSUBR (fun)->max_args)
2370 {
2371 case 0:
2372 val = (*XSUBR (fun)->function) ();
2373 goto done;
2374 case 1:
2375 val = (*XSUBR (fun)->function) (internal_args[0]);
2376 goto done;
2377 case 2:
2378 val = (*XSUBR (fun)->function) (internal_args[0],
2379 internal_args[1]);
2380 goto done;
2381 case 3:
2382 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2383 internal_args[2]);
2384 goto done;
2385 case 4:
2386 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2387 internal_args[2],
2388 internal_args[3]);
2389 goto done;
2390 case 5:
2391 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2392 internal_args[2], internal_args[3],
2393 internal_args[4]);
2394 goto done;
2395 case 6:
2396 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2397 internal_args[2], internal_args[3],
2398 internal_args[4], internal_args[5]);
2399 goto done;
2400 case 7:
2401 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2402 internal_args[2], internal_args[3],
2403 internal_args[4], internal_args[5],
2404 internal_args[6]);
2405 goto done;
2406
2407 default:
2408
2409 /* If a subr takes more than 6 arguments without using MANY
2410 or UNEVALLED, we need to extend this function to support it.
2411 Until this is done, there is no way to call the function. */
2412 abort ();
2413 }
2414 }
2415 if (COMPILEDP (fun))
2416 val = funcall_lambda (fun, numargs, args + 1);
2417 else
2418 {
2419 if (!CONSP (fun))
2420 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2421 funcar = Fcar (fun);
2422 if (!SYMBOLP (funcar))
2423 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2424 if (EQ (funcar, Qlambda))
2425 val = funcall_lambda (fun, numargs, args + 1);
2426 else if (EQ (funcar, Qmocklisp))
2427 val = ml_apply (fun, Flist (numargs, args + 1));
2428 else if (EQ (funcar, Qautoload))
2429 {
2430 do_autoload (fun, args[0]);
2431 goto retry;
2432 }
2433 else
2434 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2435 }
2436 done:
2437 lisp_eval_depth--;
2438 if (backtrace.debug_on_exit)
2439 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2440 backtrace_list = backtrace.next;
2441 return val;
2442 }
2443 \f
2444 Lisp_Object
2445 apply_lambda (fun, args, eval_flag)
2446 Lisp_Object fun, args;
2447 int eval_flag;
2448 {
2449 Lisp_Object args_left;
2450 Lisp_Object numargs;
2451 register Lisp_Object *arg_vector;
2452 struct gcpro gcpro1, gcpro2, gcpro3;
2453 register int i;
2454 register Lisp_Object tem;
2455
2456 numargs = Flength (args);
2457 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2458 args_left = args;
2459
2460 GCPRO3 (*arg_vector, args_left, fun);
2461 gcpro1.nvars = 0;
2462
2463 for (i = 0; i < XINT (numargs);)
2464 {
2465 tem = Fcar (args_left), args_left = Fcdr (args_left);
2466 if (eval_flag) tem = Feval (tem);
2467 arg_vector[i++] = tem;
2468 gcpro1.nvars = i;
2469 }
2470
2471 UNGCPRO;
2472
2473 if (eval_flag)
2474 {
2475 backtrace_list->args = arg_vector;
2476 backtrace_list->nargs = i;
2477 }
2478 backtrace_list->evalargs = 0;
2479 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2480
2481 /* Do the debug-on-exit now, while arg_vector still exists. */
2482 if (backtrace_list->debug_on_exit)
2483 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2484 /* Don't do it again when we return to eval. */
2485 backtrace_list->debug_on_exit = 0;
2486 return tem;
2487 }
2488
2489 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2490 and return the result of evaluation.
2491 FUN must be either a lambda-expression or a compiled-code object. */
2492
2493 Lisp_Object
2494 funcall_lambda (fun, nargs, arg_vector)
2495 Lisp_Object fun;
2496 int nargs;
2497 register Lisp_Object *arg_vector;
2498 {
2499 Lisp_Object val, tem;
2500 register Lisp_Object syms_left;
2501 Lisp_Object numargs;
2502 register Lisp_Object next;
2503 int count = specpdl_ptr - specpdl;
2504 register int i;
2505 int optional = 0, rest = 0;
2506
2507 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
2508
2509 XSETFASTINT (numargs, nargs);
2510
2511 if (CONSP (fun))
2512 syms_left = Fcar (Fcdr (fun));
2513 else if (COMPILEDP (fun))
2514 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
2515 else abort ();
2516
2517 i = 0;
2518 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
2519 {
2520 QUIT;
2521 next = Fcar (syms_left);
2522 while (!SYMBOLP (next))
2523 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2524 if (EQ (next, Qand_rest))
2525 rest = 1;
2526 else if (EQ (next, Qand_optional))
2527 optional = 1;
2528 else if (rest)
2529 {
2530 specbind (next, Flist (nargs - i, &arg_vector[i]));
2531 i = nargs;
2532 }
2533 else if (i < nargs)
2534 {
2535 tem = arg_vector[i++];
2536 specbind (next, tem);
2537 }
2538 else if (!optional)
2539 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2540 else
2541 specbind (next, Qnil);
2542 }
2543
2544 if (i < nargs)
2545 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
2546
2547 if (CONSP (fun))
2548 val = Fprogn (Fcdr (Fcdr (fun)));
2549 else
2550 {
2551 /* If we have not actually read the bytecode string
2552 and constants vector yet, fetch them from the file. */
2553 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
2554 Ffetch_bytecode (fun);
2555 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2556 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2557 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2558 }
2559 return unbind_to (count, val);
2560 }
2561
2562 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2563 1, 1, 0,
2564 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2565 (object)
2566 Lisp_Object object;
2567 {
2568 Lisp_Object tem;
2569
2570 if (COMPILEDP (object)
2571 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2572 {
2573 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
2574 if (!CONSP (tem))
2575 error ("invalid byte code");
2576 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCONS (tem)->car;
2577 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCONS (tem)->cdr;
2578 }
2579 return object;
2580 }
2581 \f
2582 void
2583 grow_specpdl ()
2584 {
2585 register int count = specpdl_ptr - specpdl;
2586 if (specpdl_size >= max_specpdl_size)
2587 {
2588 if (max_specpdl_size < 400)
2589 max_specpdl_size = 400;
2590 if (specpdl_size >= max_specpdl_size)
2591 {
2592 if (!NILP (Vdebug_on_error))
2593 /* Leave room for some specpdl in the debugger. */
2594 max_specpdl_size = specpdl_size + 100;
2595 Fsignal (Qerror,
2596 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
2597 }
2598 }
2599 specpdl_size *= 2;
2600 if (specpdl_size > max_specpdl_size)
2601 specpdl_size = max_specpdl_size;
2602 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2603 specpdl_ptr = specpdl + count;
2604 }
2605
2606 void
2607 specbind (symbol, value)
2608 Lisp_Object symbol, value;
2609 {
2610 Lisp_Object ovalue;
2611
2612 CHECK_SYMBOL (symbol, 0);
2613
2614 if (specpdl_ptr == specpdl + specpdl_size)
2615 grow_specpdl ();
2616 specpdl_ptr->symbol = symbol;
2617 specpdl_ptr->func = 0;
2618 specpdl_ptr->old_value = ovalue = find_symbol_value (symbol);
2619 specpdl_ptr++;
2620 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2621 store_symval_forwarding (symbol, ovalue, value);
2622 else
2623 Fset (symbol, value);
2624 }
2625
2626 void
2627 record_unwind_protect (function, arg)
2628 Lisp_Object (*function)();
2629 Lisp_Object arg;
2630 {
2631 if (specpdl_ptr == specpdl + specpdl_size)
2632 grow_specpdl ();
2633 specpdl_ptr->func = function;
2634 specpdl_ptr->symbol = Qnil;
2635 specpdl_ptr->old_value = arg;
2636 specpdl_ptr++;
2637 }
2638
2639 Lisp_Object
2640 unbind_to (count, value)
2641 int count;
2642 Lisp_Object value;
2643 {
2644 int quitf = !NILP (Vquit_flag);
2645 struct gcpro gcpro1;
2646
2647 GCPRO1 (value);
2648
2649 Vquit_flag = Qnil;
2650
2651 while (specpdl_ptr != specpdl + count)
2652 {
2653 --specpdl_ptr;
2654 if (specpdl_ptr->func != 0)
2655 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2656 /* Note that a "binding" of nil is really an unwind protect,
2657 so in that case the "old value" is a list of forms to evaluate. */
2658 else if (NILP (specpdl_ptr->symbol))
2659 Fprogn (specpdl_ptr->old_value);
2660 else
2661 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
2662 }
2663 if (NILP (Vquit_flag) && quitf) Vquit_flag = Qt;
2664
2665 UNGCPRO;
2666
2667 return value;
2668 }
2669 \f
2670 #if 0
2671
2672 /* Get the value of symbol's global binding, even if that binding
2673 is not now dynamically visible. */
2674
2675 Lisp_Object
2676 top_level_value (symbol)
2677 Lisp_Object symbol;
2678 {
2679 register struct specbinding *ptr = specpdl;
2680
2681 CHECK_SYMBOL (symbol, 0);
2682 for (; ptr != specpdl_ptr; ptr++)
2683 {
2684 if (EQ (ptr->symbol, symbol))
2685 return ptr->old_value;
2686 }
2687 return Fsymbol_value (symbol);
2688 }
2689
2690 Lisp_Object
2691 top_level_set (symbol, newval)
2692 Lisp_Object symbol, newval;
2693 {
2694 register struct specbinding *ptr = specpdl;
2695
2696 CHECK_SYMBOL (symbol, 0);
2697 for (; ptr != specpdl_ptr; ptr++)
2698 {
2699 if (EQ (ptr->symbol, symbol))
2700 {
2701 ptr->old_value = newval;
2702 return newval;
2703 }
2704 }
2705 return Fset (symbol, newval);
2706 }
2707
2708 #endif /* 0 */
2709 \f
2710 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2711 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2712 The debugger is entered when that frame exits, if the flag is non-nil.")
2713 (level, flag)
2714 Lisp_Object level, flag;
2715 {
2716 register struct backtrace *backlist = backtrace_list;
2717 register int i;
2718
2719 CHECK_NUMBER (level, 0);
2720
2721 for (i = 0; backlist && i < XINT (level); i++)
2722 {
2723 backlist = backlist->next;
2724 }
2725
2726 if (backlist)
2727 backlist->debug_on_exit = !NILP (flag);
2728
2729 return flag;
2730 }
2731
2732 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2733 "Print a trace of Lisp function calls currently active.\n\
2734 Output stream used is value of `standard-output'.")
2735 ()
2736 {
2737 register struct backtrace *backlist = backtrace_list;
2738 register int i;
2739 Lisp_Object tail;
2740 Lisp_Object tem;
2741 extern Lisp_Object Vprint_level;
2742 struct gcpro gcpro1;
2743
2744 XSETFASTINT (Vprint_level, 3);
2745
2746 tail = Qnil;
2747 GCPRO1 (tail);
2748
2749 while (backlist)
2750 {
2751 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2752 if (backlist->nargs == UNEVALLED)
2753 {
2754 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
2755 write_string ("\n", -1);
2756 }
2757 else
2758 {
2759 tem = *backlist->function;
2760 Fprin1 (tem, Qnil); /* This can QUIT */
2761 write_string ("(", -1);
2762 if (backlist->nargs == MANY)
2763 {
2764 for (tail = *backlist->args, i = 0;
2765 !NILP (tail);
2766 tail = Fcdr (tail), i++)
2767 {
2768 if (i) write_string (" ", -1);
2769 Fprin1 (Fcar (tail), Qnil);
2770 }
2771 }
2772 else
2773 {
2774 for (i = 0; i < backlist->nargs; i++)
2775 {
2776 if (i) write_string (" ", -1);
2777 Fprin1 (backlist->args[i], Qnil);
2778 }
2779 }
2780 write_string (")\n", -1);
2781 }
2782 backlist = backlist->next;
2783 }
2784
2785 Vprint_level = Qnil;
2786 UNGCPRO;
2787 return Qnil;
2788 }
2789
2790 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
2791 "Return the function and arguments NFRAMES up from current execution point.\n\
2792 If that frame has not evaluated the arguments yet (or is a special form),\n\
2793 the value is (nil FUNCTION ARG-FORMS...).\n\
2794 If that frame has evaluated its arguments and called its function already,\n\
2795 the value is (t FUNCTION ARG-VALUES...).\n\
2796 A &rest arg is represented as the tail of the list ARG-VALUES.\n\
2797 FUNCTION is whatever was supplied as car of evaluated list,\n\
2798 or a lambda expression for macro calls.\n\
2799 If NFRAMES is more than the number of frames, the value is nil.")
2800 (nframes)
2801 Lisp_Object nframes;
2802 {
2803 register struct backtrace *backlist = backtrace_list;
2804 register int i;
2805 Lisp_Object tem;
2806
2807 CHECK_NATNUM (nframes, 0);
2808
2809 /* Find the frame requested. */
2810 for (i = 0; backlist && i < XFASTINT (nframes); i++)
2811 backlist = backlist->next;
2812
2813 if (!backlist)
2814 return Qnil;
2815 if (backlist->nargs == UNEVALLED)
2816 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
2817 else
2818 {
2819 if (backlist->nargs == MANY)
2820 tem = *backlist->args;
2821 else
2822 tem = Flist (backlist->nargs, backlist->args);
2823
2824 return Fcons (Qt, Fcons (*backlist->function, tem));
2825 }
2826 }
2827 \f
2828 syms_of_eval ()
2829 {
2830 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
2831 "Limit on number of Lisp variable bindings & unwind-protects before error.");
2832
2833 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
2834 "Limit on depth in `eval', `apply' and `funcall' before error.\n\
2835 This limit is to catch infinite recursions for you before they cause\n\
2836 actual stack overflow in C, which would be fatal for Emacs.\n\
2837 You can safely make it considerably larger than its default value,\n\
2838 if that proves inconveniently small.");
2839
2840 DEFVAR_LISP ("quit-flag", &Vquit_flag,
2841 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
2842 Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
2843 Vquit_flag = Qnil;
2844
2845 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
2846 "Non-nil inhibits C-g quitting from happening immediately.\n\
2847 Note that `quit-flag' will still be set by typing C-g,\n\
2848 so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
2849 To prevent this happening, set `quit-flag' to nil\n\
2850 before making `inhibit-quit' nil.");
2851 Vinhibit_quit = Qnil;
2852
2853 Qinhibit_quit = intern ("inhibit-quit");
2854 staticpro (&Qinhibit_quit);
2855
2856 Qautoload = intern ("autoload");
2857 staticpro (&Qautoload);
2858
2859 Qdebug_on_error = intern ("debug-on-error");
2860 staticpro (&Qdebug_on_error);
2861
2862 Qmacro = intern ("macro");
2863 staticpro (&Qmacro);
2864
2865 /* Note that the process handling also uses Qexit, but we don't want
2866 to staticpro it twice, so we just do it here. */
2867 Qexit = intern ("exit");
2868 staticpro (&Qexit);
2869
2870 Qinteractive = intern ("interactive");
2871 staticpro (&Qinteractive);
2872
2873 Qcommandp = intern ("commandp");
2874 staticpro (&Qcommandp);
2875
2876 Qdefun = intern ("defun");
2877 staticpro (&Qdefun);
2878
2879 Qand_rest = intern ("&rest");
2880 staticpro (&Qand_rest);
2881
2882 Qand_optional = intern ("&optional");
2883 staticpro (&Qand_optional);
2884
2885 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
2886 "*Non-nil means automatically display a backtrace buffer\n\
2887 after any error that is handled by the editor command loop.\n\
2888 If the value is a list, an error only means to display a backtrace\n\
2889 if one of its condition symbols appears in the list.");
2890 Vstack_trace_on_error = Qnil;
2891
2892 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
2893 "*Non-nil means enter debugger if an error is signaled.\n\
2894 Does not apply to errors handled by `condition-case'.\n\
2895 If the value is a list, an error only means to enter the debugger\n\
2896 if one of its condition symbols appears in the list.\n\
2897 See also variable `debug-on-quit'.");
2898 Vdebug_on_error = Qnil;
2899
2900 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
2901 "*List of errors for which the debugger should not be called.\n\
2902 Each element may be a condition-name or a regexp that matches error messages.\n\
2903 If any element applies to a given error, that error skips the debugger\n\
2904 and just returns to top level.\n\
2905 This overrides the variable `debug-on-error'.\n\
2906 It does not apply to errors handled by `condition-case'.");
2907 Vdebug_ignored_errors = Qnil;
2908
2909 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
2910 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
2911 Does not apply if quit is handled by a `condition-case'.");
2912 debug_on_quit = 0;
2913
2914 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
2915 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
2916
2917 DEFVAR_LISP ("debugger", &Vdebugger,
2918 "Function to call to invoke debugger.\n\
2919 If due to frame exit, args are `exit' and the value being returned;\n\
2920 this function's value will be returned instead of that.\n\
2921 If due to error, args are `error' and a list of the args to `signal'.\n\
2922 If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
2923 If due to `eval' entry, one arg, t.");
2924 Vdebugger = Qnil;
2925
2926 Qmocklisp_arguments = intern ("mocklisp-arguments");
2927 staticpro (&Qmocklisp_arguments);
2928 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
2929 "While in a mocklisp function, the list of its unevaluated args.");
2930 Vmocklisp_arguments = Qt;
2931
2932 Vrun_hooks = intern ("run-hooks");
2933 staticpro (&Vrun_hooks);
2934
2935 staticpro (&Vautoload_queue);
2936 Vautoload_queue = Qnil;
2937
2938 defsubr (&Sor);
2939 defsubr (&Sand);
2940 defsubr (&Sif);
2941 defsubr (&Scond);
2942 defsubr (&Sprogn);
2943 defsubr (&Sprog1);
2944 defsubr (&Sprog2);
2945 defsubr (&Ssetq);
2946 defsubr (&Squote);
2947 defsubr (&Sfunction);
2948 defsubr (&Sdefun);
2949 defsubr (&Sdefmacro);
2950 defsubr (&Sdefvar);
2951 defsubr (&Sdefconst);
2952 defsubr (&Suser_variable_p);
2953 defsubr (&Slet);
2954 defsubr (&SletX);
2955 defsubr (&Swhile);
2956 defsubr (&Smacroexpand);
2957 defsubr (&Scatch);
2958 defsubr (&Sthrow);
2959 defsubr (&Sunwind_protect);
2960 defsubr (&Scondition_case);
2961 defsubr (&Ssignal);
2962 defsubr (&Sinteractive_p);
2963 defsubr (&Scommandp);
2964 defsubr (&Sautoload);
2965 defsubr (&Seval);
2966 defsubr (&Sapply);
2967 defsubr (&Sfuncall);
2968 defsubr (&Srun_hooks);
2969 defsubr (&Srun_hook_with_args);
2970 defsubr (&Srun_hook_with_args_until_success);
2971 defsubr (&Srun_hook_with_args_until_failure);
2972 defsubr (&Sfetch_bytecode);
2973 defsubr (&Sbacktrace_debug);
2974 defsubr (&Sbacktrace);
2975 defsubr (&Sbacktrace_frame);
2976 }