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