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