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