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