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