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