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