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