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