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