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