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