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