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