]> code.delx.au - gnu-emacs/blob - src/eval.c
22ee4d1afd1bb01f4eda2a8de7967b9722d9736a
[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 static 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_ALL)
1149 unwind_to_catch (c, Fcons (tag, value));
1150 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
1151 unwind_to_catch (c, value);
1152 }
1153 xsignal2 (Qno_catch, tag, value);
1154 }
1155
1156
1157 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1158 doc: /* Do BODYFORM, protecting with UNWINDFORMS.
1159 If BODYFORM completes normally, its value is returned
1160 after executing the UNWINDFORMS.
1161 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1162 usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1163 (Lisp_Object args)
1164 {
1165 Lisp_Object val;
1166 ptrdiff_t count = SPECPDL_INDEX ();
1167
1168 record_unwind_protect (unwind_body, XCDR (args));
1169 val = eval_sub (XCAR (args));
1170 return unbind_to (count, val);
1171 }
1172 \f
1173 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1174 doc: /* Regain control when an error is signaled.
1175 Executes BODYFORM and returns its value if no error happens.
1176 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1177 where the BODY is made of Lisp expressions.
1178
1179 A handler is applicable to an error
1180 if CONDITION-NAME is one of the error's condition names.
1181 If an error happens, the first applicable handler is run.
1182
1183 The car of a handler may be a list of condition names instead of a
1184 single condition name; then it handles all of them. If the special
1185 condition name `debug' is present in this list, it allows another
1186 condition in the list to run the debugger if `debug-on-error' and the
1187 other usual mechanisms says it should (otherwise, `condition-case'
1188 suppresses the debugger).
1189
1190 When a handler handles an error, control returns to the `condition-case'
1191 and it executes the handler's BODY...
1192 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
1193 (If VAR is nil, the handler can't access that information.)
1194 Then the value of the last BODY form is returned from the `condition-case'
1195 expression.
1196
1197 See also the function `signal' for more info.
1198 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1199 (Lisp_Object args)
1200 {
1201 Lisp_Object var = XCAR (args);
1202 Lisp_Object bodyform = XCAR (XCDR (args));
1203 Lisp_Object handlers = XCDR (XCDR (args));
1204
1205 return internal_lisp_condition_case (var, bodyform, handlers);
1206 }
1207
1208 /* Like Fcondition_case, but the args are separate
1209 rather than passed in a list. Used by Fbyte_code. */
1210
1211 Lisp_Object
1212 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1213 Lisp_Object handlers)
1214 {
1215 Lisp_Object val;
1216 struct handler *c;
1217 struct handler *oldhandlerlist = handlerlist;
1218 int clausenb = 0;
1219
1220 CHECK_SYMBOL (var);
1221
1222 for (val = handlers; CONSP (val); val = XCDR (val))
1223 {
1224 Lisp_Object tem = XCAR (val);
1225 clausenb++;
1226 if (! (NILP (tem)
1227 || (CONSP (tem)
1228 && (SYMBOLP (XCAR (tem))
1229 || CONSP (XCAR (tem))))))
1230 error ("Invalid condition handler: %s",
1231 SDATA (Fprin1_to_string (tem, Qt)));
1232 }
1233
1234 { /* The first clause is the one that should be checked first, so it should
1235 be added to handlerlist last. So we build in `clauses' a table that
1236 contains `handlers' but in reverse order. SAFE_ALLOCA won't work
1237 here due to the setjmp, so impose a MAX_ALLOCA limit. */
1238 if (MAX_ALLOCA / word_size < clausenb)
1239 memory_full (SIZE_MAX);
1240 Lisp_Object *clauses = alloca (clausenb * sizeof *clauses);
1241 Lisp_Object *volatile clauses_volatile = clauses;
1242 int i = clausenb;
1243 for (val = handlers; CONSP (val); val = XCDR (val))
1244 clauses[--i] = XCAR (val);
1245 for (i = 0; i < clausenb; i++)
1246 {
1247 Lisp_Object clause = clauses[i];
1248 Lisp_Object condition = XCAR (clause);
1249 if (!CONSP (condition))
1250 condition = Fcons (condition, Qnil);
1251 PUSH_HANDLER (c, condition, CONDITION_CASE);
1252 if (sys_setjmp (c->jmp))
1253 {
1254 ptrdiff_t count = SPECPDL_INDEX ();
1255 Lisp_Object val = handlerlist->val;
1256 Lisp_Object *chosen_clause = clauses_volatile;
1257 for (c = handlerlist->next; c != oldhandlerlist; c = c->next)
1258 chosen_clause++;
1259 handlerlist = oldhandlerlist;
1260 if (!NILP (var))
1261 {
1262 if (!NILP (Vinternal_interpreter_environment))
1263 specbind (Qinternal_interpreter_environment,
1264 Fcons (Fcons (var, val),
1265 Vinternal_interpreter_environment));
1266 else
1267 specbind (var, val);
1268 }
1269 val = Fprogn (XCDR (*chosen_clause));
1270 /* Note that this just undoes the binding of var; whoever
1271 longjumped to us unwound the stack to c.pdlcount before
1272 throwing. */
1273 if (!NILP (var))
1274 unbind_to (count, Qnil);
1275 return val;
1276 }
1277 }
1278 }
1279
1280 val = eval_sub (bodyform);
1281 handlerlist = oldhandlerlist;
1282 return val;
1283 }
1284
1285 /* Call the function BFUN with no arguments, catching errors within it
1286 according to HANDLERS. If there is an error, call HFUN with
1287 one argument which is the data that describes the error:
1288 (SIGNALNAME . DATA)
1289
1290 HANDLERS can be a list of conditions to catch.
1291 If HANDLERS is Qt, catch all errors.
1292 If HANDLERS is Qerror, catch all errors
1293 but allow the debugger to run if that is enabled. */
1294
1295 Lisp_Object
1296 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1297 Lisp_Object (*hfun) (Lisp_Object))
1298 {
1299 Lisp_Object val;
1300 struct handler *c;
1301
1302 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1303 if (sys_setjmp (c->jmp))
1304 {
1305 Lisp_Object val = handlerlist->val;
1306 clobbered_eassert (handlerlist == c);
1307 handlerlist = handlerlist->next;
1308 return (*hfun) (val);
1309 }
1310
1311 val = (*bfun) ();
1312 clobbered_eassert (handlerlist == c);
1313 handlerlist = handlerlist->next;
1314 return val;
1315 }
1316
1317 /* Like internal_condition_case but call BFUN with ARG as its argument. */
1318
1319 Lisp_Object
1320 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1321 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
1322 {
1323 Lisp_Object val;
1324 struct handler *c;
1325
1326 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1327 if (sys_setjmp (c->jmp))
1328 {
1329 Lisp_Object val = handlerlist->val;
1330 clobbered_eassert (handlerlist == c);
1331 handlerlist = handlerlist->next;
1332 return (*hfun) (val);
1333 }
1334
1335 val = (*bfun) (arg);
1336 clobbered_eassert (handlerlist == c);
1337 handlerlist = handlerlist->next;
1338 return val;
1339 }
1340
1341 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
1342 its arguments. */
1343
1344 Lisp_Object
1345 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1346 Lisp_Object arg1,
1347 Lisp_Object arg2,
1348 Lisp_Object handlers,
1349 Lisp_Object (*hfun) (Lisp_Object))
1350 {
1351 Lisp_Object val;
1352 struct handler *c;
1353
1354 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1355 if (sys_setjmp (c->jmp))
1356 {
1357 Lisp_Object val = handlerlist->val;
1358 clobbered_eassert (handlerlist == c);
1359 handlerlist = handlerlist->next;
1360 return (*hfun) (val);
1361 }
1362
1363 val = (*bfun) (arg1, arg2);
1364 clobbered_eassert (handlerlist == c);
1365 handlerlist = handlerlist->next;
1366 return val;
1367 }
1368
1369 /* Like internal_condition_case but call BFUN with NARGS as first,
1370 and ARGS as second argument. */
1371
1372 Lisp_Object
1373 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1374 ptrdiff_t nargs,
1375 Lisp_Object *args,
1376 Lisp_Object handlers,
1377 Lisp_Object (*hfun) (Lisp_Object err,
1378 ptrdiff_t nargs,
1379 Lisp_Object *args))
1380 {
1381 Lisp_Object val;
1382 struct handler *c;
1383
1384 PUSH_HANDLER (c, handlers, CONDITION_CASE);
1385 if (sys_setjmp (c->jmp))
1386 {
1387 Lisp_Object val = handlerlist->val;
1388 clobbered_eassert (handlerlist == c);
1389 handlerlist = handlerlist->next;
1390 return (*hfun) (val, nargs, args);
1391 }
1392
1393 val = (*bfun) (nargs, args);
1394 clobbered_eassert (handlerlist == c);
1395 handlerlist = handlerlist->next;
1396 return val;
1397 }
1398
1399 static void init_handler (struct handler *c, Lisp_Object tag_ch_val,
1400 enum handlertype handlertype);
1401
1402 void
1403 push_handler (struct handler **c, Lisp_Object tag_ch_val,
1404 enum handlertype handlertype)
1405 {
1406 if (handlerlist->nextfree)
1407 *c = handlerlist->nextfree;
1408 else
1409 {
1410 *c = xmalloc (sizeof (struct handler));
1411 (*c)->nextfree = NULL;
1412 handlerlist->nextfree = *c;
1413 }
1414 init_handler (*c, tag_ch_val, handlertype);
1415 }
1416
1417 bool
1418 push_handler_nosignal (struct handler **c, Lisp_Object tag_ch_val,
1419 enum handlertype handlertype)
1420 {
1421 if (handlerlist->nextfree)
1422 *c = handlerlist->nextfree;
1423 else
1424 {
1425 struct handler *h = malloc (sizeof (struct handler));
1426 if (! h) return false;
1427 *c = h;
1428 h->nextfree = NULL;
1429 handlerlist->nextfree = h;
1430 }
1431 init_handler (*c, tag_ch_val, handlertype);
1432 return true;
1433 }
1434
1435 static void
1436 init_handler (struct handler *c, Lisp_Object tag_ch_val,
1437 enum handlertype handlertype)
1438 {
1439 c->type = handlertype;
1440 c->tag_or_ch = tag_ch_val;
1441 c->val = Qnil;
1442 c->next = handlerlist;
1443 c->lisp_eval_depth = lisp_eval_depth;
1444 c->pdlcount = SPECPDL_INDEX ();
1445 c->poll_suppress_count = poll_suppress_count;
1446 c->interrupt_input_blocked = interrupt_input_blocked;
1447 c->byte_stack = byte_stack_list;
1448 handlerlist = c;
1449 }
1450
1451 \f
1452 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1453 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1454 Lisp_Object data);
1455
1456 void
1457 process_quit_flag (void)
1458 {
1459 Lisp_Object flag = Vquit_flag;
1460 Vquit_flag = Qnil;
1461 if (EQ (flag, Qkill_emacs))
1462 Fkill_emacs (Qnil);
1463 if (EQ (Vthrow_on_input, flag))
1464 Fthrow (Vthrow_on_input, Qt);
1465 Fsignal (Qquit, Qnil);
1466 }
1467
1468 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
1469 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
1470 This function does not return.
1471
1472 An error symbol is a symbol with an `error-conditions' property
1473 that is a list of condition names.
1474 A handler for any of those names will get to handle this signal.
1475 The symbol `error' should normally be one of them.
1476
1477 DATA should be a list. Its elements are printed as part of the error message.
1478 See Info anchor `(elisp)Definition of signal' for some details on how this
1479 error message is constructed.
1480 If the signal is handled, DATA is made available to the handler.
1481 See also the function `condition-case'. */)
1482 (Lisp_Object error_symbol, Lisp_Object data)
1483 {
1484 /* When memory is full, ERROR-SYMBOL is nil,
1485 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
1486 That is a special case--don't do this in other situations. */
1487 Lisp_Object conditions;
1488 Lisp_Object string;
1489 Lisp_Object real_error_symbol
1490 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1491 register Lisp_Object clause = Qnil;
1492 struct handler *h;
1493
1494 immediate_quit = 0;
1495 abort_on_gc = 0;
1496 if (gc_in_progress || waiting_for_input)
1497 emacs_abort ();
1498
1499 #if 0 /* rms: I don't know why this was here,
1500 but it is surely wrong for an error that is handled. */
1501 #ifdef HAVE_WINDOW_SYSTEM
1502 if (display_hourglass_p)
1503 cancel_hourglass ();
1504 #endif
1505 #endif
1506
1507 /* This hook is used by edebug. */
1508 if (! NILP (Vsignal_hook_function)
1509 && ! NILP (error_symbol))
1510 {
1511 /* Edebug takes care of restoring these variables when it exits. */
1512 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1513 max_lisp_eval_depth = lisp_eval_depth + 20;
1514
1515 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
1516 max_specpdl_size = SPECPDL_INDEX () + 40;
1517
1518 call2 (Vsignal_hook_function, error_symbol, data);
1519 }
1520
1521 conditions = Fget (real_error_symbol, Qerror_conditions);
1522
1523 /* Remember from where signal was called. Skip over the frame for
1524 `signal' itself. If a frame for `error' follows, skip that,
1525 too. Don't do this when ERROR_SYMBOL is nil, because that
1526 is a memory-full error. */
1527 Vsignaling_function = Qnil;
1528 if (!NILP (error_symbol))
1529 {
1530 union specbinding *pdl = backtrace_next (backtrace_top ());
1531 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1532 pdl = backtrace_next (pdl);
1533 if (backtrace_p (pdl))
1534 Vsignaling_function = backtrace_function (pdl);
1535 }
1536
1537 for (h = handlerlist; h; h = h->next)
1538 {
1539 if (h->type != CONDITION_CASE)
1540 continue;
1541 clause = find_handler_clause (h->tag_or_ch, conditions);
1542 if (!NILP (clause))
1543 break;
1544 }
1545
1546 if (/* Don't run the debugger for a memory-full error.
1547 (There is no room in memory to do that!) */
1548 !NILP (error_symbol)
1549 && (!NILP (Vdebug_on_signal)
1550 /* If no handler is present now, try to run the debugger. */
1551 || NILP (clause)
1552 /* A `debug' symbol in the handler list disables the normal
1553 suppression of the debugger. */
1554 || (CONSP (clause) && !NILP (Fmemq (Qdebug, clause)))
1555 /* Special handler that means "print a message and run debugger
1556 if requested". */
1557 || EQ (h->tag_or_ch, Qerror)))
1558 {
1559 bool debugger_called
1560 = maybe_call_debugger (conditions, error_symbol, data);
1561 /* We can't return values to code which signaled an error, but we
1562 can continue code which has signaled a quit. */
1563 if (debugger_called && EQ (real_error_symbol, Qquit))
1564 return Qnil;
1565 }
1566
1567 if (!NILP (clause))
1568 {
1569 Lisp_Object unwind_data
1570 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
1571
1572 unwind_to_catch (h, unwind_data);
1573 }
1574 else
1575 {
1576 if (handlerlist != &handlerlist_sentinel)
1577 /* FIXME: This will come right back here if there's no `top-level'
1578 catcher. A better solution would be to abort here, and instead
1579 add a catch-all condition handler so we never come here. */
1580 Fthrow (Qtop_level, Qt);
1581 }
1582
1583 if (! NILP (error_symbol))
1584 data = Fcons (error_symbol, data);
1585
1586 string = Ferror_message_string (data);
1587 fatal ("%s", SDATA (string));
1588 }
1589
1590 /* Internal version of Fsignal that never returns.
1591 Used for anything but Qquit (which can return from Fsignal). */
1592
1593 void
1594 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1595 {
1596 Fsignal (error_symbol, data);
1597 emacs_abort ();
1598 }
1599
1600 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1601
1602 void
1603 xsignal0 (Lisp_Object error_symbol)
1604 {
1605 xsignal (error_symbol, Qnil);
1606 }
1607
1608 void
1609 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1610 {
1611 xsignal (error_symbol, list1 (arg));
1612 }
1613
1614 void
1615 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1616 {
1617 xsignal (error_symbol, list2 (arg1, arg2));
1618 }
1619
1620 void
1621 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1622 {
1623 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1624 }
1625
1626 /* Signal `error' with message S, and additional arg ARG.
1627 If ARG is not a genuine list, make it a one-element list. */
1628
1629 void
1630 signal_error (const char *s, Lisp_Object arg)
1631 {
1632 Lisp_Object tortoise, hare;
1633
1634 hare = tortoise = arg;
1635 while (CONSP (hare))
1636 {
1637 hare = XCDR (hare);
1638 if (!CONSP (hare))
1639 break;
1640
1641 hare = XCDR (hare);
1642 tortoise = XCDR (tortoise);
1643
1644 if (EQ (hare, tortoise))
1645 break;
1646 }
1647
1648 if (!NILP (hare))
1649 arg = list1 (arg);
1650
1651 xsignal (Qerror, Fcons (build_string (s), arg));
1652 }
1653
1654
1655 /* Return true if LIST is a non-nil atom or
1656 a list containing one of CONDITIONS. */
1657
1658 static bool
1659 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1660 {
1661 if (NILP (list))
1662 return 0;
1663 if (! CONSP (list))
1664 return 1;
1665
1666 while (CONSP (conditions))
1667 {
1668 Lisp_Object this, tail;
1669 this = XCAR (conditions);
1670 for (tail = list; CONSP (tail); tail = XCDR (tail))
1671 if (EQ (XCAR (tail), this))
1672 return 1;
1673 conditions = XCDR (conditions);
1674 }
1675 return 0;
1676 }
1677
1678 /* Return true if an error with condition-symbols CONDITIONS,
1679 and described by SIGNAL-DATA, should skip the debugger
1680 according to debugger-ignored-errors. */
1681
1682 static bool
1683 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1684 {
1685 Lisp_Object tail;
1686 bool first_string = 1;
1687 Lisp_Object error_message;
1688
1689 error_message = Qnil;
1690 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1691 {
1692 if (STRINGP (XCAR (tail)))
1693 {
1694 if (first_string)
1695 {
1696 error_message = Ferror_message_string (data);
1697 first_string = 0;
1698 }
1699
1700 if (fast_string_match (XCAR (tail), error_message) >= 0)
1701 return 1;
1702 }
1703 else
1704 {
1705 Lisp_Object contail;
1706
1707 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1708 if (EQ (XCAR (tail), XCAR (contail)))
1709 return 1;
1710 }
1711 }
1712
1713 return 0;
1714 }
1715
1716 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1717 SIG and DATA describe the signal. There are two ways to pass them:
1718 = SIG is the error symbol, and DATA is the rest of the data.
1719 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1720 This is for memory-full errors only. */
1721 static bool
1722 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1723 {
1724 Lisp_Object combined_data;
1725
1726 combined_data = Fcons (sig, data);
1727
1728 if (
1729 /* Don't try to run the debugger with interrupts blocked.
1730 The editing loop would return anyway. */
1731 ! input_blocked_p ()
1732 && NILP (Vinhibit_debugger)
1733 /* Does user want to enter debugger for this kind of error? */
1734 && (EQ (sig, Qquit)
1735 ? debug_on_quit
1736 : wants_debugger (Vdebug_on_error, conditions))
1737 && ! skip_debugger (conditions, combined_data)
1738 /* RMS: What's this for? */
1739 && when_entered_debugger < num_nonmacro_input_events)
1740 {
1741 call_debugger (list2 (Qerror, combined_data));
1742 return 1;
1743 }
1744
1745 return 0;
1746 }
1747
1748 static Lisp_Object
1749 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1750 {
1751 register Lisp_Object h;
1752
1753 /* t is used by handlers for all conditions, set up by C code. */
1754 if (EQ (handlers, Qt))
1755 return Qt;
1756
1757 /* error is used similarly, but means print an error message
1758 and run the debugger if that is enabled. */
1759 if (EQ (handlers, Qerror))
1760 return Qt;
1761
1762 for (h = handlers; CONSP (h); h = XCDR (h))
1763 {
1764 Lisp_Object handler = XCAR (h);
1765 if (!NILP (Fmemq (handler, conditions)))
1766 return handlers;
1767 }
1768
1769 return Qnil;
1770 }
1771
1772
1773 /* Dump an error message; called like vprintf. */
1774 void
1775 verror (const char *m, va_list ap)
1776 {
1777 char buf[4000];
1778 ptrdiff_t size = sizeof buf;
1779 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1780 char *buffer = buf;
1781 ptrdiff_t used;
1782 Lisp_Object string;
1783
1784 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1785 string = make_string (buffer, used);
1786 if (buffer != buf)
1787 xfree (buffer);
1788
1789 xsignal1 (Qerror, string);
1790 }
1791
1792
1793 /* Dump an error message; called like printf. */
1794
1795 /* VARARGS 1 */
1796 void
1797 error (const char *m, ...)
1798 {
1799 va_list ap;
1800 va_start (ap, m);
1801 verror (m, ap);
1802 }
1803 \f
1804 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1805 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1806 This means it contains a description for how to read arguments to give it.
1807 The value is nil for an invalid function or a symbol with no function
1808 definition.
1809
1810 Interactively callable functions include strings and vectors (treated
1811 as keyboard macros), lambda-expressions that contain a top-level call
1812 to `interactive', autoload definitions made by `autoload' with non-nil
1813 fourth argument, and some of the built-in functions of Lisp.
1814
1815 Also, a symbol satisfies `commandp' if its function definition does so.
1816
1817 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1818 then strings and vectors are not accepted. */)
1819 (Lisp_Object function, Lisp_Object for_call_interactively)
1820 {
1821 register Lisp_Object fun;
1822 register Lisp_Object funcar;
1823 Lisp_Object if_prop = Qnil;
1824
1825 fun = function;
1826
1827 fun = indirect_function (fun); /* Check cycles. */
1828 if (NILP (fun))
1829 return Qnil;
1830
1831 /* Check an `interactive-form' property if present, analogous to the
1832 function-documentation property. */
1833 fun = function;
1834 while (SYMBOLP (fun))
1835 {
1836 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1837 if (!NILP (tmp))
1838 if_prop = Qt;
1839 fun = Fsymbol_function (fun);
1840 }
1841
1842 /* Emacs primitives are interactive if their DEFUN specifies an
1843 interactive spec. */
1844 if (SUBRP (fun))
1845 return XSUBR (fun)->intspec ? Qt : if_prop;
1846
1847 /* Bytecode objects are interactive if they are long enough to
1848 have an element whose index is COMPILED_INTERACTIVE, which is
1849 where the interactive spec is stored. */
1850 else if (COMPILEDP (fun))
1851 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1852 ? Qt : if_prop);
1853
1854 /* Strings and vectors are keyboard macros. */
1855 if (STRINGP (fun) || VECTORP (fun))
1856 return (NILP (for_call_interactively) ? Qt : Qnil);
1857
1858 /* Lists may represent commands. */
1859 if (!CONSP (fun))
1860 return Qnil;
1861 funcar = XCAR (fun);
1862 if (EQ (funcar, Qclosure))
1863 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1864 ? Qt : if_prop);
1865 else if (EQ (funcar, Qlambda))
1866 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1867 else if (EQ (funcar, Qautoload))
1868 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1869 else
1870 return Qnil;
1871 }
1872
1873 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1874 doc: /* Define FUNCTION to autoload from FILE.
1875 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1876 Third arg DOCSTRING is documentation for the function.
1877 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1878 Fifth arg TYPE indicates the type of the object:
1879 nil or omitted says FUNCTION is a function,
1880 `keymap' says FUNCTION is really a keymap, and
1881 `macro' or t says FUNCTION is really a macro.
1882 Third through fifth args give info about the real definition.
1883 They default to nil.
1884 If FUNCTION is already defined other than as an autoload,
1885 this does nothing and returns nil. */)
1886 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1887 {
1888 CHECK_SYMBOL (function);
1889 CHECK_STRING (file);
1890
1891 /* If function is defined and not as an autoload, don't override. */
1892 if (!NILP (XSYMBOL (function)->function)
1893 && !AUTOLOADP (XSYMBOL (function)->function))
1894 return Qnil;
1895
1896 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1897 /* `read1' in lread.c has found the docstring starting with "\
1898 and assumed the docstring will be provided by Snarf-documentation, so it
1899 passed us 0 instead. But that leads to accidental sharing in purecopy's
1900 hash-consing, so we use a (hopefully) unique integer instead. */
1901 docstring = make_number (XHASH (function));
1902 return Fdefalias (function,
1903 list5 (Qautoload, file, docstring, interactive, type),
1904 Qnil);
1905 }
1906
1907 void
1908 un_autoload (Lisp_Object oldqueue)
1909 {
1910 Lisp_Object queue, first, second;
1911
1912 /* Queue to unwind is current value of Vautoload_queue.
1913 oldqueue is the shadowed value to leave in Vautoload_queue. */
1914 queue = Vautoload_queue;
1915 Vautoload_queue = oldqueue;
1916 while (CONSP (queue))
1917 {
1918 first = XCAR (queue);
1919 second = Fcdr (first);
1920 first = Fcar (first);
1921 if (EQ (first, make_number (0)))
1922 Vfeatures = second;
1923 else
1924 Ffset (first, second);
1925 queue = XCDR (queue);
1926 }
1927 }
1928
1929 /* Load an autoloaded function.
1930 FUNNAME is the symbol which is the function's name.
1931 FUNDEF is the autoload definition (a list). */
1932
1933 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1934 doc: /* Load FUNDEF which should be an autoload.
1935 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1936 in which case the function returns the new autoloaded function value.
1937 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1938 it defines a macro. */)
1939 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1940 {
1941 ptrdiff_t count = SPECPDL_INDEX ();
1942
1943 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1944 return fundef;
1945
1946 if (EQ (macro_only, Qmacro))
1947 {
1948 Lisp_Object kind = Fnth (make_number (4), fundef);
1949 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1950 return fundef;
1951 }
1952
1953 /* This is to make sure that loadup.el gives a clear picture
1954 of what files are preloaded and when. */
1955 if (! NILP (Vpurify_flag))
1956 error ("Attempt to autoload %s while preparing to dump",
1957 SDATA (SYMBOL_NAME (funname)));
1958
1959 CHECK_SYMBOL (funname);
1960
1961 /* Preserve the match data. */
1962 record_unwind_save_match_data ();
1963
1964 /* If autoloading gets an error (which includes the error of failing
1965 to define the function being called), we use Vautoload_queue
1966 to undo function definitions and `provide' calls made by
1967 the function. We do this in the specific case of autoloading
1968 because autoloading is not an explicit request "load this file",
1969 but rather a request to "call this function".
1970
1971 The value saved here is to be restored into Vautoload_queue. */
1972 record_unwind_protect (un_autoload, Vautoload_queue);
1973 Vautoload_queue = Qt;
1974 /* If `macro_only', assume this autoload to be a "best-effort",
1975 so don't signal an error if autoloading fails. */
1976 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1977
1978 /* Once loading finishes, don't undo it. */
1979 Vautoload_queue = Qt;
1980 unbind_to (count, Qnil);
1981
1982 if (NILP (funname))
1983 return Qnil;
1984 else
1985 {
1986 Lisp_Object fun = Findirect_function (funname, Qnil);
1987
1988 if (!NILP (Fequal (fun, fundef)))
1989 error ("Autoloading failed to define function %s",
1990 SDATA (SYMBOL_NAME (funname)));
1991 else
1992 return fun;
1993 }
1994 }
1995
1996 \f
1997 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1998 doc: /* Evaluate FORM and return its value.
1999 If LEXICAL is t, evaluate using lexical scoping.
2000 LEXICAL can also be an actual lexical environment, in the form of an
2001 alist mapping symbols to their value. */)
2002 (Lisp_Object form, Lisp_Object lexical)
2003 {
2004 ptrdiff_t count = SPECPDL_INDEX ();
2005 specbind (Qinternal_interpreter_environment,
2006 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2007 return unbind_to (count, eval_sub (form));
2008 }
2009
2010 /* Grow the specpdl stack by one entry.
2011 The caller should have already initialized the entry.
2012 Signal an error on stack overflow.
2013
2014 Make sure that there is always one unused entry past the top of the
2015 stack, so that the just-initialized entry is safely unwound if
2016 memory exhausted and an error is signaled here. Also, allocate a
2017 never-used entry just before the bottom of the stack; sometimes its
2018 address is taken. */
2019
2020 static void
2021 grow_specpdl (void)
2022 {
2023 specpdl_ptr++;
2024
2025 if (specpdl_ptr == specpdl + specpdl_size)
2026 {
2027 ptrdiff_t count = SPECPDL_INDEX ();
2028 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2029 union specbinding *pdlvec = specpdl - 1;
2030 ptrdiff_t pdlvecsize = specpdl_size + 1;
2031 if (max_size <= specpdl_size)
2032 {
2033 if (max_specpdl_size < 400)
2034 max_size = max_specpdl_size = 400;
2035 if (max_size <= specpdl_size)
2036 signal_error ("Variable binding depth exceeds max-specpdl-size",
2037 Qnil);
2038 }
2039 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2040 specpdl = pdlvec + 1;
2041 specpdl_size = pdlvecsize - 1;
2042 specpdl_ptr = specpdl + count;
2043 }
2044 }
2045
2046 ptrdiff_t
2047 record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2048 {
2049 ptrdiff_t count = SPECPDL_INDEX ();
2050
2051 eassert (nargs >= UNEVALLED);
2052 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2053 specpdl_ptr->bt.debug_on_exit = false;
2054 specpdl_ptr->bt.function = function;
2055 specpdl_ptr->bt.args = args;
2056 specpdl_ptr->bt.nargs = nargs;
2057 grow_specpdl ();
2058
2059 return count;
2060 }
2061
2062 /* Eval a sub-expression of the current expression (i.e. in the same
2063 lexical scope). */
2064 Lisp_Object
2065 eval_sub (Lisp_Object form)
2066 {
2067 Lisp_Object fun, val, original_fun, original_args;
2068 Lisp_Object funcar;
2069 ptrdiff_t count;
2070
2071 /* Declare here, as this array may be accessed by call_debugger near
2072 the end of this function. See Bug#21245. */
2073 Lisp_Object argvals[8];
2074
2075 if (SYMBOLP (form))
2076 {
2077 /* Look up its binding in the lexical environment.
2078 We do not pay attention to the declared_special flag here, since we
2079 already did that when let-binding the variable. */
2080 Lisp_Object lex_binding
2081 = !NILP (Vinternal_interpreter_environment) /* Mere optimization! */
2082 ? Fassq (form, Vinternal_interpreter_environment)
2083 : Qnil;
2084 if (CONSP (lex_binding))
2085 return XCDR (lex_binding);
2086 else
2087 return Fsymbol_value (form);
2088 }
2089
2090 if (!CONSP (form))
2091 return form;
2092
2093 QUIT;
2094
2095 maybe_gc ();
2096
2097 if (++lisp_eval_depth > max_lisp_eval_depth)
2098 {
2099 if (max_lisp_eval_depth < 100)
2100 max_lisp_eval_depth = 100;
2101 if (lisp_eval_depth > max_lisp_eval_depth)
2102 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2103 }
2104
2105 original_fun = XCAR (form);
2106 original_args = XCDR (form);
2107
2108 /* This also protects them from gc. */
2109 count = record_in_backtrace (original_fun, &original_args, UNEVALLED);
2110
2111 if (debug_on_next_call)
2112 do_debug_on_call (Qt, count);
2113
2114 /* At this point, only original_fun and original_args
2115 have values that will be used below. */
2116 retry:
2117
2118 /* Optimize for no indirection. */
2119 fun = original_fun;
2120 if (!SYMBOLP (fun))
2121 fun = Ffunction (Fcons (fun, Qnil));
2122 else if (!NILP (fun) && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2123 fun = indirect_function (fun);
2124
2125 if (SUBRP (fun))
2126 {
2127 Lisp_Object args_left = original_args;
2128 Lisp_Object numargs = Flength (args_left);
2129
2130 check_cons_list ();
2131
2132 if (XINT (numargs) < XSUBR (fun)->min_args
2133 || (XSUBR (fun)->max_args >= 0
2134 && XSUBR (fun)->max_args < XINT (numargs)))
2135 xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
2136
2137 else if (XSUBR (fun)->max_args == UNEVALLED)
2138 val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
2139 else if (XSUBR (fun)->max_args == MANY)
2140 {
2141 /* Pass a vector of evaluated arguments. */
2142 Lisp_Object *vals;
2143 ptrdiff_t argnum = 0;
2144 USE_SAFE_ALLOCA;
2145
2146 SAFE_ALLOCA_LISP (vals, XINT (numargs));
2147
2148 while (!NILP (args_left))
2149 {
2150 vals[argnum++] = eval_sub (Fcar (args_left));
2151 args_left = Fcdr (args_left);
2152 }
2153
2154 set_backtrace_args (specpdl + count, vals, XINT (numargs));
2155
2156 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2157
2158 check_cons_list ();
2159 lisp_eval_depth--;
2160 /* Do the debug-on-exit now, while VALS still exists. */
2161 if (backtrace_debug_on_exit (specpdl + count))
2162 val = call_debugger (list2 (Qexit, val));
2163 SAFE_FREE ();
2164 specpdl_ptr--;
2165 return val;
2166 }
2167 else
2168 {
2169 int i, maxargs = XSUBR (fun)->max_args;
2170
2171 for (i = 0; i < maxargs; i++)
2172 {
2173 argvals[i] = eval_sub (Fcar (args_left));
2174 args_left = Fcdr (args_left);
2175 }
2176
2177 set_backtrace_args (specpdl + count, argvals, XINT (numargs));
2178
2179 switch (i)
2180 {
2181 case 0:
2182 val = (XSUBR (fun)->function.a0 ());
2183 break;
2184 case 1:
2185 val = (XSUBR (fun)->function.a1 (argvals[0]));
2186 break;
2187 case 2:
2188 val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
2189 break;
2190 case 3:
2191 val = (XSUBR (fun)->function.a3
2192 (argvals[0], argvals[1], argvals[2]));
2193 break;
2194 case 4:
2195 val = (XSUBR (fun)->function.a4
2196 (argvals[0], argvals[1], argvals[2], argvals[3]));
2197 break;
2198 case 5:
2199 val = (XSUBR (fun)->function.a5
2200 (argvals[0], argvals[1], argvals[2], argvals[3],
2201 argvals[4]));
2202 break;
2203 case 6:
2204 val = (XSUBR (fun)->function.a6
2205 (argvals[0], argvals[1], argvals[2], argvals[3],
2206 argvals[4], argvals[5]));
2207 break;
2208 case 7:
2209 val = (XSUBR (fun)->function.a7
2210 (argvals[0], argvals[1], argvals[2], argvals[3],
2211 argvals[4], argvals[5], argvals[6]));
2212 break;
2213
2214 case 8:
2215 val = (XSUBR (fun)->function.a8
2216 (argvals[0], argvals[1], argvals[2], argvals[3],
2217 argvals[4], argvals[5], argvals[6], argvals[7]));
2218 break;
2219
2220 default:
2221 /* Someone has created a subr that takes more arguments than
2222 is supported by this code. We need to either rewrite the
2223 subr to use a different argument protocol, or add more
2224 cases to this switch. */
2225 emacs_abort ();
2226 }
2227 }
2228 }
2229 else if (COMPILEDP (fun))
2230 return apply_lambda (fun, original_args, count);
2231 else
2232 {
2233 if (NILP (fun))
2234 xsignal1 (Qvoid_function, original_fun);
2235 if (!CONSP (fun))
2236 xsignal1 (Qinvalid_function, original_fun);
2237 funcar = XCAR (fun);
2238 if (!SYMBOLP (funcar))
2239 xsignal1 (Qinvalid_function, original_fun);
2240 if (EQ (funcar, Qautoload))
2241 {
2242 Fautoload_do_load (fun, original_fun, Qnil);
2243 goto retry;
2244 }
2245 if (EQ (funcar, Qmacro))
2246 {
2247 ptrdiff_t count1 = SPECPDL_INDEX ();
2248 Lisp_Object exp;
2249 /* Bind lexical-binding during expansion of the macro, so the
2250 macro can know reliably if the code it outputs will be
2251 interpreted using lexical-binding or not. */
2252 specbind (Qlexical_binding,
2253 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2254 exp = apply1 (Fcdr (fun), original_args);
2255 unbind_to (count1, Qnil);
2256 val = eval_sub (exp);
2257 }
2258 else if (EQ (funcar, Qlambda)
2259 || EQ (funcar, Qclosure))
2260 return apply_lambda (fun, original_args, count);
2261 else
2262 xsignal1 (Qinvalid_function, original_fun);
2263 }
2264 check_cons_list ();
2265
2266 lisp_eval_depth--;
2267 if (backtrace_debug_on_exit (specpdl + count))
2268 val = call_debugger (list2 (Qexit, val));
2269 specpdl_ptr--;
2270
2271 return val;
2272 }
2273 \f
2274 DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2275 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2276 Then return the value FUNCTION returns.
2277 Thus, (apply \\='+ 1 2 \\='(3 4)) returns 10.
2278 usage: (apply FUNCTION &rest ARGUMENTS) */)
2279 (ptrdiff_t nargs, Lisp_Object *args)
2280 {
2281 ptrdiff_t i, numargs, funcall_nargs;
2282 register Lisp_Object *funcall_args = NULL;
2283 register Lisp_Object spread_arg = args[nargs - 1];
2284 Lisp_Object fun = args[0];
2285 Lisp_Object retval;
2286 USE_SAFE_ALLOCA;
2287
2288 CHECK_LIST (spread_arg);
2289
2290 numargs = XINT (Flength (spread_arg));
2291
2292 if (numargs == 0)
2293 return Ffuncall (nargs - 1, args);
2294 else if (numargs == 1)
2295 {
2296 args [nargs - 1] = XCAR (spread_arg);
2297 return Ffuncall (nargs, args);
2298 }
2299
2300 numargs += nargs - 2;
2301
2302 /* Optimize for no indirection. */
2303 if (SYMBOLP (fun) && !NILP (fun)
2304 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2305 {
2306 fun = indirect_function (fun);
2307 if (NILP (fun))
2308 /* Let funcall get the error. */
2309 fun = args[0];
2310 }
2311
2312 if (SUBRP (fun) && XSUBR (fun)->max_args > numargs
2313 /* Don't hide an error by adding missing arguments. */
2314 && numargs >= XSUBR (fun)->min_args)
2315 {
2316 /* Avoid making funcall cons up a yet another new vector of arguments
2317 by explicitly supplying nil's for optional values. */
2318 SAFE_ALLOCA_LISP (funcall_args, 1 + XSUBR (fun)->max_args);
2319 memclear (funcall_args + numargs + 1,
2320 (XSUBR (fun)->max_args - numargs) * word_size);
2321 funcall_nargs = 1 + XSUBR (fun)->max_args;
2322 }
2323 else
2324 { /* We add 1 to numargs because funcall_args includes the
2325 function itself as well as its arguments. */
2326 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
2327 funcall_nargs = 1 + numargs;
2328 }
2329
2330 memcpy (funcall_args, args, nargs * word_size);
2331 /* Spread the last arg we got. Its first element goes in
2332 the slot that it used to occupy, hence this value of I. */
2333 i = nargs - 1;
2334 while (!NILP (spread_arg))
2335 {
2336 funcall_args [i++] = XCAR (spread_arg);
2337 spread_arg = XCDR (spread_arg);
2338 }
2339
2340 retval = Ffuncall (funcall_nargs, funcall_args);
2341
2342 SAFE_FREE ();
2343 return retval;
2344 }
2345 \f
2346 /* Run hook variables in various ways. */
2347
2348 static Lisp_Object
2349 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
2350 {
2351 Ffuncall (nargs, args);
2352 return Qnil;
2353 }
2354
2355 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
2356 doc: /* Run each hook in HOOKS.
2357 Each argument should be a symbol, a hook variable.
2358 These symbols are processed in the order specified.
2359 If a hook symbol has a non-nil value, that value may be a function
2360 or a list of functions to be called to run the hook.
2361 If the value is a function, it is called with no arguments.
2362 If it is a list, the elements are called, in order, with no arguments.
2363
2364 Major modes should not use this function directly to run their mode
2365 hook; they should use `run-mode-hooks' instead.
2366
2367 Do not use `make-local-variable' to make a hook variable buffer-local.
2368 Instead, use `add-hook' and specify t for the LOCAL argument.
2369 usage: (run-hooks &rest HOOKS) */)
2370 (ptrdiff_t nargs, Lisp_Object *args)
2371 {
2372 ptrdiff_t i;
2373
2374 for (i = 0; i < nargs; i++)
2375 run_hook (args[i]);
2376
2377 return Qnil;
2378 }
2379
2380 DEFUN ("run-hook-with-args", Frun_hook_with_args,
2381 Srun_hook_with_args, 1, MANY, 0,
2382 doc: /* Run HOOK with the specified arguments ARGS.
2383 HOOK should be a symbol, a hook variable. The value of HOOK
2384 may be nil, a function, or a list of functions. Call each
2385 function in order with arguments ARGS. The final return value
2386 is unspecified.
2387
2388 Do not use `make-local-variable' to make a hook variable buffer-local.
2389 Instead, use `add-hook' and specify t for the LOCAL argument.
2390 usage: (run-hook-with-args HOOK &rest ARGS) */)
2391 (ptrdiff_t nargs, Lisp_Object *args)
2392 {
2393 return run_hook_with_args (nargs, args, funcall_nil);
2394 }
2395
2396 /* NB this one still documents a specific non-nil return value.
2397 (As did run-hook-with-args and run-hook-with-args-until-failure
2398 until they were changed in 24.1.) */
2399 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2400 Srun_hook_with_args_until_success, 1, MANY, 0,
2401 doc: /* Run HOOK with the specified arguments ARGS.
2402 HOOK should be a symbol, a hook variable. The value of HOOK
2403 may be nil, a function, or a list of functions. Call each
2404 function in order with arguments ARGS, stopping at the first
2405 one that returns non-nil, and return that value. Otherwise (if
2406 all functions return nil, or if there are no functions to call),
2407 return nil.
2408
2409 Do not use `make-local-variable' to make a hook variable buffer-local.
2410 Instead, use `add-hook' and specify t for the LOCAL argument.
2411 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2412 (ptrdiff_t nargs, Lisp_Object *args)
2413 {
2414 return run_hook_with_args (nargs, args, Ffuncall);
2415 }
2416
2417 static Lisp_Object
2418 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2419 {
2420 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2421 }
2422
2423 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2424 Srun_hook_with_args_until_failure, 1, MANY, 0,
2425 doc: /* Run HOOK with the specified arguments ARGS.
2426 HOOK should be a symbol, a hook variable. The value of HOOK
2427 may be nil, a function, or a list of functions. Call each
2428 function in order with arguments ARGS, stopping at the first
2429 one that returns nil, and return nil. Otherwise (if all functions
2430 return non-nil, or if there are no functions to call), return non-nil
2431 (do not rely on the precise return value in this case).
2432
2433 Do not use `make-local-variable' to make a hook variable buffer-local.
2434 Instead, use `add-hook' and specify t for the LOCAL argument.
2435 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2436 (ptrdiff_t nargs, Lisp_Object *args)
2437 {
2438 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2439 }
2440
2441 static Lisp_Object
2442 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2443 {
2444 Lisp_Object tmp = args[0], ret;
2445 args[0] = args[1];
2446 args[1] = tmp;
2447 ret = Ffuncall (nargs, args);
2448 args[1] = args[0];
2449 args[0] = tmp;
2450 return ret;
2451 }
2452
2453 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2454 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2455 I.e. instead of calling each function FUN directly with arguments ARGS,
2456 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2457 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2458 aborts and returns that value.
2459 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2460 (ptrdiff_t nargs, Lisp_Object *args)
2461 {
2462 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2463 }
2464
2465 /* ARGS[0] should be a hook symbol.
2466 Call each of the functions in the hook value, passing each of them
2467 as arguments all the rest of ARGS (all NARGS - 1 elements).
2468 FUNCALL specifies how to call each function on the hook. */
2469
2470 Lisp_Object
2471 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2472 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2473 {
2474 Lisp_Object sym, val, ret = Qnil;
2475
2476 /* If we are dying or still initializing,
2477 don't do anything--it would probably crash if we tried. */
2478 if (NILP (Vrun_hooks))
2479 return Qnil;
2480
2481 sym = args[0];
2482 val = find_symbol_value (sym);
2483
2484 if (EQ (val, Qunbound) || NILP (val))
2485 return ret;
2486 else if (!CONSP (val) || FUNCTIONP (val))
2487 {
2488 args[0] = val;
2489 return funcall (nargs, args);
2490 }
2491 else
2492 {
2493 Lisp_Object global_vals = Qnil;
2494
2495 for (;
2496 CONSP (val) && NILP (ret);
2497 val = XCDR (val))
2498 {
2499 if (EQ (XCAR (val), Qt))
2500 {
2501 /* t indicates this hook has a local binding;
2502 it means to run the global binding too. */
2503 global_vals = Fdefault_value (sym);
2504 if (NILP (global_vals)) continue;
2505
2506 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2507 {
2508 args[0] = global_vals;
2509 ret = funcall (nargs, args);
2510 }
2511 else
2512 {
2513 for (;
2514 CONSP (global_vals) && NILP (ret);
2515 global_vals = XCDR (global_vals))
2516 {
2517 args[0] = XCAR (global_vals);
2518 /* In a global value, t should not occur. If it does, we
2519 must ignore it to avoid an endless loop. */
2520 if (!EQ (args[0], Qt))
2521 ret = funcall (nargs, args);
2522 }
2523 }
2524 }
2525 else
2526 {
2527 args[0] = XCAR (val);
2528 ret = funcall (nargs, args);
2529 }
2530 }
2531
2532 return ret;
2533 }
2534 }
2535
2536 /* Run the hook HOOK, giving each function no args. */
2537
2538 void
2539 run_hook (Lisp_Object hook)
2540 {
2541 Frun_hook_with_args (1, &hook);
2542 }
2543
2544 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2545
2546 void
2547 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2548 {
2549 CALLN (Frun_hook_with_args, hook, arg1, arg2);
2550 }
2551
2552 /* Apply fn to arg. */
2553 Lisp_Object
2554 apply1 (Lisp_Object fn, Lisp_Object arg)
2555 {
2556 return NILP (arg) ? Ffuncall (1, &fn) : CALLN (Fapply, fn, arg);
2557 }
2558
2559 /* Call function fn on no arguments. */
2560 Lisp_Object
2561 call0 (Lisp_Object fn)
2562 {
2563 return Ffuncall (1, &fn);
2564 }
2565
2566 /* Call function fn with 1 argument arg1. */
2567 /* ARGSUSED */
2568 Lisp_Object
2569 call1 (Lisp_Object fn, Lisp_Object arg1)
2570 {
2571 return CALLN (Ffuncall, fn, arg1);
2572 }
2573
2574 /* Call function fn with 2 arguments arg1, arg2. */
2575 /* ARGSUSED */
2576 Lisp_Object
2577 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2578 {
2579 return CALLN (Ffuncall, fn, arg1, arg2);
2580 }
2581
2582 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2583 /* ARGSUSED */
2584 Lisp_Object
2585 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2586 {
2587 return CALLN (Ffuncall, fn, arg1, arg2, arg3);
2588 }
2589
2590 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2591 /* ARGSUSED */
2592 Lisp_Object
2593 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2594 Lisp_Object arg4)
2595 {
2596 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4);
2597 }
2598
2599 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2600 /* ARGSUSED */
2601 Lisp_Object
2602 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2603 Lisp_Object arg4, Lisp_Object arg5)
2604 {
2605 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5);
2606 }
2607
2608 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2609 /* ARGSUSED */
2610 Lisp_Object
2611 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2612 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2613 {
2614 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6);
2615 }
2616
2617 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2618 /* ARGSUSED */
2619 Lisp_Object
2620 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2621 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2622 {
2623 return CALLN (Ffuncall, fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
2624 }
2625
2626 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2627 doc: /* Non-nil if OBJECT is a function. */)
2628 (Lisp_Object object)
2629 {
2630 if (FUNCTIONP (object))
2631 return Qt;
2632 return Qnil;
2633 }
2634
2635 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2636 doc: /* Call first argument as a function, passing remaining arguments to it.
2637 Return the value that function returns.
2638 Thus, (funcall \\='cons \\='x \\='y) returns (x . y).
2639 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2640 (ptrdiff_t nargs, Lisp_Object *args)
2641 {
2642 Lisp_Object fun, original_fun;
2643 Lisp_Object funcar;
2644 ptrdiff_t numargs = nargs - 1;
2645 Lisp_Object lisp_numargs;
2646 Lisp_Object val;
2647 Lisp_Object *internal_args;
2648 ptrdiff_t count;
2649
2650 QUIT;
2651
2652 if (++lisp_eval_depth > max_lisp_eval_depth)
2653 {
2654 if (max_lisp_eval_depth < 100)
2655 max_lisp_eval_depth = 100;
2656 if (lisp_eval_depth > max_lisp_eval_depth)
2657 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2658 }
2659
2660 count = record_in_backtrace (args[0], &args[1], nargs - 1);
2661
2662 maybe_gc ();
2663
2664 if (debug_on_next_call)
2665 do_debug_on_call (Qlambda, count);
2666
2667 check_cons_list ();
2668
2669 original_fun = args[0];
2670
2671 retry:
2672
2673 /* Optimize for no indirection. */
2674 fun = original_fun;
2675 if (SYMBOLP (fun) && !NILP (fun)
2676 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2677 fun = indirect_function (fun);
2678
2679 if (SUBRP (fun))
2680 {
2681 if (numargs < XSUBR (fun)->min_args
2682 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2683 {
2684 XSETFASTINT (lisp_numargs, numargs);
2685 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2686 }
2687
2688 else if (XSUBR (fun)->max_args == UNEVALLED)
2689 xsignal1 (Qinvalid_function, original_fun);
2690
2691 else if (XSUBR (fun)->max_args == MANY)
2692 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2693 else
2694 {
2695 Lisp_Object internal_argbuf[8];
2696 if (XSUBR (fun)->max_args > numargs)
2697 {
2698 eassert (XSUBR (fun)->max_args <= ARRAYELTS (internal_argbuf));
2699 internal_args = internal_argbuf;
2700 memcpy (internal_args, args + 1, numargs * word_size);
2701 memclear (internal_args + numargs,
2702 (XSUBR (fun)->max_args - numargs) * word_size);
2703 }
2704 else
2705 internal_args = args + 1;
2706 switch (XSUBR (fun)->max_args)
2707 {
2708 case 0:
2709 val = (XSUBR (fun)->function.a0 ());
2710 break;
2711 case 1:
2712 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2713 break;
2714 case 2:
2715 val = (XSUBR (fun)->function.a2
2716 (internal_args[0], internal_args[1]));
2717 break;
2718 case 3:
2719 val = (XSUBR (fun)->function.a3
2720 (internal_args[0], internal_args[1], internal_args[2]));
2721 break;
2722 case 4:
2723 val = (XSUBR (fun)->function.a4
2724 (internal_args[0], internal_args[1], internal_args[2],
2725 internal_args[3]));
2726 break;
2727 case 5:
2728 val = (XSUBR (fun)->function.a5
2729 (internal_args[0], internal_args[1], internal_args[2],
2730 internal_args[3], internal_args[4]));
2731 break;
2732 case 6:
2733 val = (XSUBR (fun)->function.a6
2734 (internal_args[0], internal_args[1], internal_args[2],
2735 internal_args[3], internal_args[4], internal_args[5]));
2736 break;
2737 case 7:
2738 val = (XSUBR (fun)->function.a7
2739 (internal_args[0], internal_args[1], internal_args[2],
2740 internal_args[3], internal_args[4], internal_args[5],
2741 internal_args[6]));
2742 break;
2743
2744 case 8:
2745 val = (XSUBR (fun)->function.a8
2746 (internal_args[0], internal_args[1], internal_args[2],
2747 internal_args[3], internal_args[4], internal_args[5],
2748 internal_args[6], internal_args[7]));
2749 break;
2750
2751 default:
2752
2753 /* If a subr takes more than 8 arguments without using MANY
2754 or UNEVALLED, we need to extend this function to support it.
2755 Until this is done, there is no way to call the function. */
2756 emacs_abort ();
2757 }
2758 }
2759 }
2760 else if (COMPILEDP (fun))
2761 val = funcall_lambda (fun, numargs, args + 1);
2762 else
2763 {
2764 if (NILP (fun))
2765 xsignal1 (Qvoid_function, original_fun);
2766 if (!CONSP (fun))
2767 xsignal1 (Qinvalid_function, original_fun);
2768 funcar = XCAR (fun);
2769 if (!SYMBOLP (funcar))
2770 xsignal1 (Qinvalid_function, original_fun);
2771 if (EQ (funcar, Qlambda)
2772 || EQ (funcar, Qclosure))
2773 val = funcall_lambda (fun, numargs, args + 1);
2774 else if (EQ (funcar, Qautoload))
2775 {
2776 Fautoload_do_load (fun, original_fun, Qnil);
2777 check_cons_list ();
2778 goto retry;
2779 }
2780 else
2781 xsignal1 (Qinvalid_function, original_fun);
2782 }
2783 check_cons_list ();
2784 lisp_eval_depth--;
2785 if (backtrace_debug_on_exit (specpdl + count))
2786 val = call_debugger (list2 (Qexit, val));
2787 specpdl_ptr--;
2788 return val;
2789 }
2790 \f
2791 static Lisp_Object
2792 apply_lambda (Lisp_Object fun, Lisp_Object args, ptrdiff_t count)
2793 {
2794 Lisp_Object args_left;
2795 ptrdiff_t i;
2796 EMACS_INT numargs;
2797 Lisp_Object *arg_vector;
2798 Lisp_Object tem;
2799 USE_SAFE_ALLOCA;
2800
2801 numargs = XFASTINT (Flength (args));
2802 SAFE_ALLOCA_LISP (arg_vector, numargs);
2803 args_left = args;
2804
2805 for (i = 0; i < numargs; )
2806 {
2807 tem = Fcar (args_left), args_left = Fcdr (args_left);
2808 tem = eval_sub (tem);
2809 arg_vector[i++] = tem;
2810 }
2811
2812 set_backtrace_args (specpdl + count, arg_vector, i);
2813 tem = funcall_lambda (fun, numargs, arg_vector);
2814
2815 check_cons_list ();
2816 lisp_eval_depth--;
2817 /* Do the debug-on-exit now, while arg_vector still exists. */
2818 if (backtrace_debug_on_exit (specpdl + count))
2819 tem = call_debugger (list2 (Qexit, tem));
2820 SAFE_FREE ();
2821 specpdl_ptr--;
2822 return tem;
2823 }
2824
2825 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2826 and return the result of evaluation.
2827 FUN must be either a lambda-expression or a compiled-code object. */
2828
2829 static Lisp_Object
2830 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2831 register Lisp_Object *arg_vector)
2832 {
2833 Lisp_Object val, syms_left, next, lexenv;
2834 ptrdiff_t count = SPECPDL_INDEX ();
2835 ptrdiff_t i;
2836 bool optional, rest;
2837
2838 if (CONSP (fun))
2839 {
2840 if (EQ (XCAR (fun), Qclosure))
2841 {
2842 fun = XCDR (fun); /* Drop `closure'. */
2843 lexenv = XCAR (fun);
2844 CHECK_LIST_CONS (fun, fun);
2845 }
2846 else
2847 lexenv = Qnil;
2848 syms_left = XCDR (fun);
2849 if (CONSP (syms_left))
2850 syms_left = XCAR (syms_left);
2851 else
2852 xsignal1 (Qinvalid_function, fun);
2853 }
2854 else if (COMPILEDP (fun))
2855 {
2856 ptrdiff_t size = ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK;
2857 if (size <= COMPILED_STACK_DEPTH)
2858 xsignal1 (Qinvalid_function, fun);
2859 syms_left = AREF (fun, COMPILED_ARGLIST);
2860 if (INTEGERP (syms_left))
2861 /* A byte-code object with a non-nil `push args' slot means we
2862 shouldn't bind any arguments, instead just call the byte-code
2863 interpreter directly; it will push arguments as necessary.
2864
2865 Byte-code objects with either a non-existent, or a nil value for
2866 the `push args' slot (the default), have dynamically-bound
2867 arguments, and use the argument-binding code below instead (as do
2868 all interpreted functions, even lexically bound ones). */
2869 {
2870 /* If we have not actually read the bytecode string
2871 and constants vector yet, fetch them from the file. */
2872 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2873 Ffetch_bytecode (fun);
2874 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2875 AREF (fun, COMPILED_CONSTANTS),
2876 AREF (fun, COMPILED_STACK_DEPTH),
2877 syms_left,
2878 nargs, arg_vector);
2879 }
2880 lexenv = Qnil;
2881 }
2882 else
2883 emacs_abort ();
2884
2885 i = optional = rest = 0;
2886 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2887 {
2888 QUIT;
2889
2890 next = XCAR (syms_left);
2891 if (!SYMBOLP (next))
2892 xsignal1 (Qinvalid_function, fun);
2893
2894 if (EQ (next, Qand_rest))
2895 rest = 1;
2896 else if (EQ (next, Qand_optional))
2897 optional = 1;
2898 else
2899 {
2900 Lisp_Object arg;
2901 if (rest)
2902 {
2903 arg = Flist (nargs - i, &arg_vector[i]);
2904 i = nargs;
2905 }
2906 else if (i < nargs)
2907 arg = arg_vector[i++];
2908 else if (!optional)
2909 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2910 else
2911 arg = Qnil;
2912
2913 /* Bind the argument. */
2914 if (!NILP (lexenv) && SYMBOLP (next))
2915 /* Lexically bind NEXT by adding it to the lexenv alist. */
2916 lexenv = Fcons (Fcons (next, arg), lexenv);
2917 else
2918 /* Dynamically bind NEXT. */
2919 specbind (next, arg);
2920 }
2921 }
2922
2923 if (!NILP (syms_left))
2924 xsignal1 (Qinvalid_function, fun);
2925 else if (i < nargs)
2926 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2927
2928 if (!EQ (lexenv, Vinternal_interpreter_environment))
2929 /* Instantiate a new lexical environment. */
2930 specbind (Qinternal_interpreter_environment, lexenv);
2931
2932 if (CONSP (fun))
2933 val = Fprogn (XCDR (XCDR (fun)));
2934 else
2935 {
2936 /* If we have not actually read the bytecode string
2937 and constants vector yet, fetch them from the file. */
2938 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
2939 Ffetch_bytecode (fun);
2940 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
2941 AREF (fun, COMPILED_CONSTANTS),
2942 AREF (fun, COMPILED_STACK_DEPTH),
2943 Qnil, 0, 0);
2944 }
2945
2946 return unbind_to (count, val);
2947 }
2948
2949 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2950 1, 1, 0,
2951 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2952 (Lisp_Object object)
2953 {
2954 Lisp_Object tem;
2955
2956 if (COMPILEDP (object))
2957 {
2958 ptrdiff_t size = ASIZE (object) & PSEUDOVECTOR_SIZE_MASK;
2959 if (size <= COMPILED_STACK_DEPTH)
2960 xsignal1 (Qinvalid_function, object);
2961 if (CONSP (AREF (object, COMPILED_BYTECODE)))
2962 {
2963 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2964 if (!CONSP (tem))
2965 {
2966 tem = AREF (object, COMPILED_BYTECODE);
2967 if (CONSP (tem) && STRINGP (XCAR (tem)))
2968 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2969 else
2970 error ("Invalid byte code");
2971 }
2972 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2973 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2974 }
2975 }
2976 return object;
2977 }
2978 \f
2979 /* Return true if SYMBOL currently has a let-binding
2980 which was made in the buffer that is now current. */
2981
2982 bool
2983 let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
2984 {
2985 union specbinding *p;
2986 Lisp_Object buf = Fcurrent_buffer ();
2987
2988 for (p = specpdl_ptr; p > specpdl; )
2989 if ((--p)->kind > SPECPDL_LET)
2990 {
2991 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2992 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
2993 if (symbol == let_bound_symbol
2994 && EQ (specpdl_where (p), buf))
2995 return 1;
2996 }
2997
2998 return 0;
2999 }
3000
3001 bool
3002 let_shadows_global_binding_p (Lisp_Object symbol)
3003 {
3004 union specbinding *p;
3005
3006 for (p = specpdl_ptr; p > specpdl; )
3007 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3008 return 1;
3009
3010 return 0;
3011 }
3012
3013 /* `specpdl_ptr' describes which variable is
3014 let-bound, so it can be properly undone when we unbind_to.
3015 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3016 - SYMBOL is the variable being bound. Note that it should not be
3017 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3018 to record V2 here).
3019 - WHERE tells us in which buffer the binding took place.
3020 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3021 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3022 i.e. bindings to the default value of a variable which can be
3023 buffer-local. */
3024
3025 void
3026 specbind (Lisp_Object symbol, Lisp_Object value)
3027 {
3028 struct Lisp_Symbol *sym;
3029
3030 CHECK_SYMBOL (symbol);
3031 sym = XSYMBOL (symbol);
3032
3033 start:
3034 switch (sym->redirect)
3035 {
3036 case SYMBOL_VARALIAS:
3037 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3038 case SYMBOL_PLAINVAL:
3039 /* The most common case is that of a non-constant symbol with a
3040 trivial value. Make that as fast as we can. */
3041 specpdl_ptr->let.kind = SPECPDL_LET;
3042 specpdl_ptr->let.symbol = symbol;
3043 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3044 grow_specpdl ();
3045 if (!sym->constant)
3046 SET_SYMBOL_VAL (sym, value);
3047 else
3048 set_internal (symbol, value, Qnil, 1);
3049 break;
3050 case SYMBOL_LOCALIZED:
3051 if (SYMBOL_BLV (sym)->frame_local)
3052 error ("Frame-local vars cannot be let-bound");
3053 case SYMBOL_FORWARDED:
3054 {
3055 Lisp_Object ovalue = find_symbol_value (symbol);
3056 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3057 specpdl_ptr->let.symbol = symbol;
3058 specpdl_ptr->let.old_value = ovalue;
3059 specpdl_ptr->let.where = Fcurrent_buffer ();
3060
3061 eassert (sym->redirect != SYMBOL_LOCALIZED
3062 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3063
3064 if (sym->redirect == SYMBOL_LOCALIZED)
3065 {
3066 if (!blv_found (SYMBOL_BLV (sym)))
3067 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3068 }
3069 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3070 {
3071 /* If SYMBOL is a per-buffer variable which doesn't have a
3072 buffer-local value here, make the `let' change the global
3073 value by changing the value of SYMBOL in all buffers not
3074 having their own value. This is consistent with what
3075 happens with other buffer-local variables. */
3076 if (NILP (Flocal_variable_p (symbol, Qnil)))
3077 {
3078 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3079 grow_specpdl ();
3080 Fset_default (symbol, value);
3081 return;
3082 }
3083 }
3084 else
3085 specpdl_ptr->let.kind = SPECPDL_LET;
3086
3087 grow_specpdl ();
3088 set_internal (symbol, value, Qnil, 1);
3089 break;
3090 }
3091 default: emacs_abort ();
3092 }
3093 }
3094
3095 /* Push unwind-protect entries of various types. */
3096
3097 void
3098 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3099 {
3100 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3101 specpdl_ptr->unwind.func = function;
3102 specpdl_ptr->unwind.arg = arg;
3103 grow_specpdl ();
3104 }
3105
3106 void
3107 record_unwind_protect_ptr (void (*function) (void *), void *arg)
3108 {
3109 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3110 specpdl_ptr->unwind_ptr.func = function;
3111 specpdl_ptr->unwind_ptr.arg = arg;
3112 grow_specpdl ();
3113 }
3114
3115 void
3116 record_unwind_protect_int (void (*function) (int), int arg)
3117 {
3118 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3119 specpdl_ptr->unwind_int.func = function;
3120 specpdl_ptr->unwind_int.arg = arg;
3121 grow_specpdl ();
3122 }
3123
3124 void
3125 record_unwind_protect_void (void (*function) (void))
3126 {
3127 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3128 specpdl_ptr->unwind_void.func = function;
3129 grow_specpdl ();
3130 }
3131
3132 static void
3133 do_nothing (void)
3134 {}
3135
3136 /* Push an unwind-protect entry that does nothing, so that
3137 set_unwind_protect_ptr can overwrite it later. */
3138
3139 void
3140 record_unwind_protect_nothing (void)
3141 {
3142 record_unwind_protect_void (do_nothing);
3143 }
3144
3145 /* Clear the unwind-protect entry COUNT, so that it does nothing.
3146 It need not be at the top of the stack. */
3147
3148 void
3149 clear_unwind_protect (ptrdiff_t count)
3150 {
3151 union specbinding *p = specpdl + count;
3152 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3153 p->unwind_void.func = do_nothing;
3154 }
3155
3156 /* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3157 It need not be at the top of the stack. Discard the entry's
3158 previous value without invoking it. */
3159
3160 void
3161 set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3162 Lisp_Object arg)
3163 {
3164 union specbinding *p = specpdl + count;
3165 p->unwind.kind = SPECPDL_UNWIND;
3166 p->unwind.func = func;
3167 p->unwind.arg = arg;
3168 }
3169
3170 void
3171 set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3172 {
3173 union specbinding *p = specpdl + count;
3174 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3175 p->unwind_ptr.func = func;
3176 p->unwind_ptr.arg = arg;
3177 }
3178
3179 /* Pop and execute entries from the unwind-protect stack until the
3180 depth COUNT is reached. Return VALUE. */
3181
3182 Lisp_Object
3183 unbind_to (ptrdiff_t count, Lisp_Object value)
3184 {
3185 Lisp_Object quitf = Vquit_flag;
3186
3187 Vquit_flag = Qnil;
3188
3189 while (specpdl_ptr != specpdl + count)
3190 {
3191 /* Decrement specpdl_ptr before we do the work to unbind it, so
3192 that an error in unbinding won't try to unbind the same entry
3193 again. Take care to copy any parts of the binding needed
3194 before invoking any code that can make more bindings. */
3195
3196 specpdl_ptr--;
3197
3198 switch (specpdl_ptr->kind)
3199 {
3200 case SPECPDL_UNWIND:
3201 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3202 break;
3203 case SPECPDL_UNWIND_PTR:
3204 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3205 break;
3206 case SPECPDL_UNWIND_INT:
3207 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3208 break;
3209 case SPECPDL_UNWIND_VOID:
3210 specpdl_ptr->unwind_void.func ();
3211 break;
3212 case SPECPDL_BACKTRACE:
3213 break;
3214 case SPECPDL_LET:
3215 { /* If variable has a trivial value (no forwarding), we can
3216 just set it. No need to check for constant symbols here,
3217 since that was already done by specbind. */
3218 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3219 if (sym->redirect == SYMBOL_PLAINVAL)
3220 {
3221 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3222 break;
3223 }
3224 else
3225 { /* FALLTHROUGH!!
3226 NOTE: we only ever come here if make_local_foo was used for
3227 the first time on this var within this let. */
3228 }
3229 }
3230 case SPECPDL_LET_DEFAULT:
3231 Fset_default (specpdl_symbol (specpdl_ptr),
3232 specpdl_old_value (specpdl_ptr));
3233 break;
3234 case SPECPDL_LET_LOCAL:
3235 {
3236 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3237 Lisp_Object where = specpdl_where (specpdl_ptr);
3238 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3239 eassert (BUFFERP (where));
3240
3241 /* If this was a local binding, reset the value in the appropriate
3242 buffer, but only if that buffer's binding still exists. */
3243 if (!NILP (Flocal_variable_p (symbol, where)))
3244 set_internal (symbol, old_value, where, 1);
3245 }
3246 break;
3247 }
3248 }
3249
3250 if (NILP (Vquit_flag) && !NILP (quitf))
3251 Vquit_flag = quitf;
3252
3253 return value;
3254 }
3255
3256 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3257 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3258 A special variable is one that will be bound dynamically, even in a
3259 context where binding is lexical by default. */)
3260 (Lisp_Object symbol)
3261 {
3262 CHECK_SYMBOL (symbol);
3263 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3264 }
3265
3266 \f
3267 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3268 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3269 The debugger is entered when that frame exits, if the flag is non-nil. */)
3270 (Lisp_Object level, Lisp_Object flag)
3271 {
3272 union specbinding *pdl = backtrace_top ();
3273 register EMACS_INT i;
3274
3275 CHECK_NUMBER (level);
3276
3277 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3278 pdl = backtrace_next (pdl);
3279
3280 if (backtrace_p (pdl))
3281 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3282
3283 return flag;
3284 }
3285
3286 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3287 doc: /* Print a trace of Lisp function calls currently active.
3288 Output stream used is value of `standard-output'. */)
3289 (void)
3290 {
3291 union specbinding *pdl = backtrace_top ();
3292 Lisp_Object tem;
3293 Lisp_Object old_print_level = Vprint_level;
3294
3295 if (NILP (Vprint_level))
3296 XSETFASTINT (Vprint_level, 8);
3297
3298 while (backtrace_p (pdl))
3299 {
3300 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ");
3301 if (backtrace_nargs (pdl) == UNEVALLED)
3302 {
3303 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3304 Qnil);
3305 write_string ("\n");
3306 }
3307 else
3308 {
3309 tem = backtrace_function (pdl);
3310 Fprin1 (tem, Qnil); /* This can QUIT. */
3311 write_string ("(");
3312 {
3313 ptrdiff_t i;
3314 for (i = 0; i < backtrace_nargs (pdl); i++)
3315 {
3316 if (i) write_string (" ");
3317 Fprin1 (backtrace_args (pdl)[i], Qnil);
3318 }
3319 }
3320 write_string (")\n");
3321 }
3322 pdl = backtrace_next (pdl);
3323 }
3324
3325 Vprint_level = old_print_level;
3326 return Qnil;
3327 }
3328
3329 static union specbinding *
3330 get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3331 {
3332 union specbinding *pdl = backtrace_top ();
3333 register EMACS_INT i;
3334
3335 CHECK_NATNUM (nframes);
3336
3337 if (!NILP (base))
3338 { /* Skip up to `base'. */
3339 base = Findirect_function (base, Qt);
3340 while (backtrace_p (pdl)
3341 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3342 pdl = backtrace_next (pdl);
3343 }
3344
3345 /* Find the frame requested. */
3346 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3347 pdl = backtrace_next (pdl);
3348
3349 return pdl;
3350 }
3351
3352 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3353 doc: /* Return the function and arguments NFRAMES up from current execution point.
3354 If that frame has not evaluated the arguments yet (or is a special form),
3355 the value is (nil FUNCTION ARG-FORMS...).
3356 If that frame has evaluated its arguments and called its function already,
3357 the value is (t FUNCTION ARG-VALUES...).
3358 A &rest arg is represented as the tail of the list ARG-VALUES.
3359 FUNCTION is whatever was supplied as car of evaluated list,
3360 or a lambda expression for macro calls.
3361 If NFRAMES is more than the number of frames, the value is nil.
3362 If BASE is non-nil, it should be a function and NFRAMES counts from its
3363 nearest activation frame. */)
3364 (Lisp_Object nframes, Lisp_Object base)
3365 {
3366 union specbinding *pdl = get_backtrace_frame (nframes, base);
3367
3368 if (!backtrace_p (pdl))
3369 return Qnil;
3370 if (backtrace_nargs (pdl) == UNEVALLED)
3371 return Fcons (Qnil,
3372 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3373 else
3374 {
3375 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3376
3377 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3378 }
3379 }
3380
3381 /* For backtrace-eval, we want to temporarily unwind the last few elements of
3382 the specpdl stack, and then rewind them. We store the pre-unwind values
3383 directly in the pre-existing specpdl elements (i.e. we swap the current
3384 value and the old value stored in the specpdl), kind of like the inplace
3385 pointer-reversal trick. As it turns out, the rewind does the same as the
3386 unwind, except it starts from the other end of the specpdl stack, so we use
3387 the same function for both unwind and rewind. */
3388 static void
3389 backtrace_eval_unrewind (int distance)
3390 {
3391 union specbinding *tmp = specpdl_ptr;
3392 int step = -1;
3393 if (distance < 0)
3394 { /* It's a rewind rather than unwind. */
3395 tmp += distance - 1;
3396 step = 1;
3397 distance = -distance;
3398 }
3399
3400 for (; distance > 0; distance--)
3401 {
3402 tmp += step;
3403 switch (tmp->kind)
3404 {
3405 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3406 unwind_protect, but the problem is that we don't know how to
3407 rewind them afterwards. */
3408 case SPECPDL_UNWIND:
3409 {
3410 Lisp_Object oldarg = tmp->unwind.arg;
3411 if (tmp->unwind.func == set_buffer_if_live)
3412 tmp->unwind.arg = Fcurrent_buffer ();
3413 else if (tmp->unwind.func == save_excursion_restore)
3414 tmp->unwind.arg = save_excursion_save ();
3415 else
3416 break;
3417 tmp->unwind.func (oldarg);
3418 break;
3419 }
3420
3421 case SPECPDL_UNWIND_PTR:
3422 case SPECPDL_UNWIND_INT:
3423 case SPECPDL_UNWIND_VOID:
3424 case SPECPDL_BACKTRACE:
3425 break;
3426 case SPECPDL_LET:
3427 { /* If variable has a trivial value (no forwarding), we can
3428 just set it. No need to check for constant symbols here,
3429 since that was already done by specbind. */
3430 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3431 if (sym->redirect == SYMBOL_PLAINVAL)
3432 {
3433 Lisp_Object old_value = specpdl_old_value (tmp);
3434 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3435 SET_SYMBOL_VAL (sym, old_value);
3436 break;
3437 }
3438 else
3439 { /* FALLTHROUGH!!
3440 NOTE: we only ever come here if make_local_foo was used for
3441 the first time on this var within this let. */
3442 }
3443 }
3444 case SPECPDL_LET_DEFAULT:
3445 {
3446 Lisp_Object sym = specpdl_symbol (tmp);
3447 Lisp_Object old_value = specpdl_old_value (tmp);
3448 set_specpdl_old_value (tmp, Fdefault_value (sym));
3449 Fset_default (sym, old_value);
3450 }
3451 break;
3452 case SPECPDL_LET_LOCAL:
3453 {
3454 Lisp_Object symbol = specpdl_symbol (tmp);
3455 Lisp_Object where = specpdl_where (tmp);
3456 Lisp_Object old_value = specpdl_old_value (tmp);
3457 eassert (BUFFERP (where));
3458
3459 /* If this was a local binding, reset the value in the appropriate
3460 buffer, but only if that buffer's binding still exists. */
3461 if (!NILP (Flocal_variable_p (symbol, where)))
3462 {
3463 set_specpdl_old_value
3464 (tmp, Fbuffer_local_value (symbol, where));
3465 set_internal (symbol, old_value, where, 1);
3466 }
3467 }
3468 break;
3469 }
3470 }
3471 }
3472
3473 DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3474 doc: /* Evaluate EXP in the context of some activation frame.
3475 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3476 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3477 {
3478 union specbinding *pdl = get_backtrace_frame (nframes, base);
3479 ptrdiff_t count = SPECPDL_INDEX ();
3480 ptrdiff_t distance = specpdl_ptr - pdl;
3481 eassert (distance >= 0);
3482
3483 if (!backtrace_p (pdl))
3484 error ("Activation frame not found!");
3485
3486 backtrace_eval_unrewind (distance);
3487 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3488
3489 /* Use eval_sub rather than Feval since the main motivation behind
3490 backtrace-eval is to be able to get/set the value of lexical variables
3491 from the debugger. */
3492 return unbind_to (count, eval_sub (exp));
3493 }
3494
3495 DEFUN ("backtrace--locals", Fbacktrace__locals, Sbacktrace__locals, 1, 2, NULL,
3496 doc: /* Return names and values of local variables of a stack frame.
3497 NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3498 (Lisp_Object nframes, Lisp_Object base)
3499 {
3500 union specbinding *frame = get_backtrace_frame (nframes, base);
3501 union specbinding *prevframe
3502 = get_backtrace_frame (make_number (XFASTINT (nframes) - 1), base);
3503 ptrdiff_t distance = specpdl_ptr - frame;
3504 Lisp_Object result = Qnil;
3505 eassert (distance >= 0);
3506
3507 if (!backtrace_p (prevframe))
3508 error ("Activation frame not found!");
3509 if (!backtrace_p (frame))
3510 error ("Activation frame not found!");
3511
3512 /* The specpdl entries normally contain the symbol being bound along with its
3513 `old_value', so it can be restored. The new value to which it is bound is
3514 available in one of two places: either in the current value of the
3515 variable (if it hasn't been rebound yet) or in the `old_value' slot of the
3516 next specpdl entry for it.
3517 `backtrace_eval_unrewind' happens to swap the role of `old_value'
3518 and "new value", so we abuse it here, to fetch the new value.
3519 It's ugly (we'd rather not modify global data) and a bit inefficient,
3520 but it does the job for now. */
3521 backtrace_eval_unrewind (distance);
3522
3523 /* Grab values. */
3524 {
3525 union specbinding *tmp = prevframe;
3526 for (; tmp > frame; tmp--)
3527 {
3528 switch (tmp->kind)
3529 {
3530 case SPECPDL_LET:
3531 case SPECPDL_LET_DEFAULT:
3532 case SPECPDL_LET_LOCAL:
3533 {
3534 Lisp_Object sym = specpdl_symbol (tmp);
3535 Lisp_Object val = specpdl_old_value (tmp);
3536 if (EQ (sym, Qinternal_interpreter_environment))
3537 {
3538 Lisp_Object env = val;
3539 for (; CONSP (env); env = XCDR (env))
3540 {
3541 Lisp_Object binding = XCAR (env);
3542 if (CONSP (binding))
3543 result = Fcons (Fcons (XCAR (binding),
3544 XCDR (binding)),
3545 result);
3546 }
3547 }
3548 else
3549 result = Fcons (Fcons (sym, val), result);
3550 }
3551 break;
3552
3553 case SPECPDL_UNWIND:
3554 case SPECPDL_UNWIND_PTR:
3555 case SPECPDL_UNWIND_INT:
3556 case SPECPDL_UNWIND_VOID:
3557 case SPECPDL_BACKTRACE:
3558 break;
3559
3560 default:
3561 emacs_abort ();
3562 }
3563 }
3564 }
3565
3566 /* Restore values from specpdl to original place. */
3567 backtrace_eval_unrewind (-distance);
3568
3569 return result;
3570 }
3571
3572 \f
3573 void
3574 mark_specpdl (void)
3575 {
3576 union specbinding *pdl;
3577 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3578 {
3579 switch (pdl->kind)
3580 {
3581 case SPECPDL_UNWIND:
3582 mark_object (specpdl_arg (pdl));
3583 break;
3584
3585 case SPECPDL_BACKTRACE:
3586 {
3587 ptrdiff_t nargs = backtrace_nargs (pdl);
3588 mark_object (backtrace_function (pdl));
3589 if (nargs == UNEVALLED)
3590 nargs = 1;
3591 while (nargs--)
3592 mark_object (backtrace_args (pdl)[nargs]);
3593 }
3594 break;
3595
3596 case SPECPDL_LET_DEFAULT:
3597 case SPECPDL_LET_LOCAL:
3598 mark_object (specpdl_where (pdl));
3599 /* Fall through. */
3600 case SPECPDL_LET:
3601 mark_object (specpdl_symbol (pdl));
3602 mark_object (specpdl_old_value (pdl));
3603 break;
3604
3605 case SPECPDL_UNWIND_PTR:
3606 case SPECPDL_UNWIND_INT:
3607 case SPECPDL_UNWIND_VOID:
3608 break;
3609
3610 default:
3611 emacs_abort ();
3612 }
3613 }
3614 }
3615
3616 void
3617 get_backtrace (Lisp_Object array)
3618 {
3619 union specbinding *pdl = backtrace_next (backtrace_top ());
3620 ptrdiff_t i = 0, asize = ASIZE (array);
3621
3622 /* Copy the backtrace contents into working memory. */
3623 for (; i < asize; i++)
3624 {
3625 if (backtrace_p (pdl))
3626 {
3627 ASET (array, i, backtrace_function (pdl));
3628 pdl = backtrace_next (pdl);
3629 }
3630 else
3631 ASET (array, i, Qnil);
3632 }
3633 }
3634
3635 Lisp_Object backtrace_top_function (void)
3636 {
3637 union specbinding *pdl = backtrace_top ();
3638 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3639 }
3640
3641 void
3642 syms_of_eval (void)
3643 {
3644 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3645 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
3646 If Lisp code tries to increase the total number past this amount,
3647 an error is signaled.
3648 You can safely use a value considerably larger than the default value,
3649 if that proves inconveniently small. However, if you increase it too far,
3650 Emacs could run out of memory trying to make the stack bigger.
3651 Note that this limit may be silently increased by the debugger
3652 if `debug-on-error' or `debug-on-quit' is set. */);
3653
3654 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3655 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
3656
3657 This limit serves to catch infinite recursions for you before they cause
3658 actual stack overflow in C, which would be fatal for Emacs.
3659 You can safely make it considerably larger than its default value,
3660 if that proves inconveniently small. However, if you increase it too far,
3661 Emacs could overflow the real C stack, and crash. */);
3662
3663 DEFVAR_LISP ("quit-flag", Vquit_flag,
3664 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3665 If the value is t, that means do an ordinary quit.
3666 If the value equals `throw-on-input', that means quit by throwing
3667 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3668 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3669 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3670 Vquit_flag = Qnil;
3671
3672 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3673 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3674 Note that `quit-flag' will still be set by typing C-g,
3675 so a quit will be signaled as soon as `inhibit-quit' is nil.
3676 To prevent this happening, set `quit-flag' to nil
3677 before making `inhibit-quit' nil. */);
3678 Vinhibit_quit = Qnil;
3679
3680 DEFSYM (Qinhibit_quit, "inhibit-quit");
3681 DEFSYM (Qautoload, "autoload");
3682 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3683 DEFSYM (Qmacro, "macro");
3684
3685 /* Note that the process handling also uses Qexit, but we don't want
3686 to staticpro it twice, so we just do it here. */
3687 DEFSYM (Qexit, "exit");
3688
3689 DEFSYM (Qinteractive, "interactive");
3690 DEFSYM (Qcommandp, "commandp");
3691 DEFSYM (Qand_rest, "&rest");
3692 DEFSYM (Qand_optional, "&optional");
3693 DEFSYM (Qclosure, "closure");
3694 DEFSYM (QCdocumentation, ":documentation");
3695 DEFSYM (Qdebug, "debug");
3696
3697 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3698 doc: /* Non-nil means never enter the debugger.
3699 Normally set while the debugger is already active, to avoid recursive
3700 invocations. */);
3701 Vinhibit_debugger = Qnil;
3702
3703 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3704 doc: /* Non-nil means enter debugger if an error is signaled.
3705 Does not apply to errors handled by `condition-case' or those
3706 matched by `debug-ignored-errors'.
3707 If the value is a list, an error only means to enter the debugger
3708 if one of its condition symbols appears in the list.
3709 When you evaluate an expression interactively, this variable
3710 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3711 The command `toggle-debug-on-error' toggles this.
3712 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3713 Vdebug_on_error = Qnil;
3714
3715 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3716 doc: /* List of errors for which the debugger should not be called.
3717 Each element may be a condition-name or a regexp that matches error messages.
3718 If any element applies to a given error, that error skips the debugger
3719 and just returns to top level.
3720 This overrides the variable `debug-on-error'.
3721 It does not apply to errors handled by `condition-case'. */);
3722 Vdebug_ignored_errors = Qnil;
3723
3724 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3725 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
3726 Does not apply if quit is handled by a `condition-case'. */);
3727 debug_on_quit = 0;
3728
3729 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3730 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3731
3732 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3733 doc: /* Non-nil means debugger may continue execution.
3734 This is nil when the debugger is called under circumstances where it
3735 might not be safe to continue. */);
3736 debugger_may_continue = 1;
3737
3738 DEFVAR_LISP ("debugger", Vdebugger,
3739 doc: /* Function to call to invoke debugger.
3740 If due to frame exit, args are `exit' and the value being returned;
3741 this function's value will be returned instead of that.
3742 If due to error, args are `error' and a list of the args to `signal'.
3743 If due to `apply' or `funcall' entry, one arg, `lambda'.
3744 If due to `eval' entry, one arg, t. */);
3745 Vdebugger = Qnil;
3746
3747 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3748 doc: /* If non-nil, this is a function for `signal' to call.
3749 It receives the same arguments that `signal' was given.
3750 The Edebug package uses this to regain control. */);
3751 Vsignal_hook_function = Qnil;
3752
3753 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3754 doc: /* Non-nil means call the debugger regardless of condition handlers.
3755 Note that `debug-on-error', `debug-on-quit' and friends
3756 still determine whether to handle the particular condition. */);
3757 Vdebug_on_signal = Qnil;
3758
3759 /* When lexical binding is being used,
3760 Vinternal_interpreter_environment is non-nil, and contains an alist
3761 of lexically-bound variable, or (t), indicating an empty
3762 environment. The lisp name of this variable would be
3763 `internal-interpreter-environment' if it weren't hidden.
3764 Every element of this list can be either a cons (VAR . VAL)
3765 specifying a lexical binding, or a single symbol VAR indicating
3766 that this variable should use dynamic scoping. */
3767 DEFSYM (Qinternal_interpreter_environment,
3768 "internal-interpreter-environment");
3769 DEFVAR_LISP ("internal-interpreter-environment",
3770 Vinternal_interpreter_environment,
3771 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3772 When lexical binding is not being used, this variable is nil.
3773 A value of `(t)' indicates an empty environment, otherwise it is an
3774 alist of active lexical bindings. */);
3775 Vinternal_interpreter_environment = Qnil;
3776 /* Don't export this variable to Elisp, so no one can mess with it
3777 (Just imagine if someone makes it buffer-local). */
3778 Funintern (Qinternal_interpreter_environment, Qnil);
3779
3780 Vrun_hooks = intern_c_string ("run-hooks");
3781 staticpro (&Vrun_hooks);
3782
3783 staticpro (&Vautoload_queue);
3784 Vautoload_queue = Qnil;
3785 staticpro (&Vsignaling_function);
3786 Vsignaling_function = Qnil;
3787
3788 inhibit_lisp_code = Qnil;
3789
3790 defsubr (&Sor);
3791 defsubr (&Sand);
3792 defsubr (&Sif);
3793 defsubr (&Scond);
3794 defsubr (&Sprogn);
3795 defsubr (&Sprog1);
3796 defsubr (&Sprog2);
3797 defsubr (&Ssetq);
3798 defsubr (&Squote);
3799 defsubr (&Sfunction);
3800 defsubr (&Sdefault_toplevel_value);
3801 defsubr (&Sset_default_toplevel_value);
3802 defsubr (&Sdefvar);
3803 defsubr (&Sdefvaralias);
3804 defsubr (&Sdefconst);
3805 defsubr (&Smake_var_non_special);
3806 defsubr (&Slet);
3807 defsubr (&SletX);
3808 defsubr (&Swhile);
3809 defsubr (&Smacroexpand);
3810 defsubr (&Scatch);
3811 defsubr (&Sthrow);
3812 defsubr (&Sunwind_protect);
3813 defsubr (&Scondition_case);
3814 defsubr (&Ssignal);
3815 defsubr (&Scommandp);
3816 defsubr (&Sautoload);
3817 defsubr (&Sautoload_do_load);
3818 defsubr (&Seval);
3819 defsubr (&Sapply);
3820 defsubr (&Sfuncall);
3821 defsubr (&Srun_hooks);
3822 defsubr (&Srun_hook_with_args);
3823 defsubr (&Srun_hook_with_args_until_success);
3824 defsubr (&Srun_hook_with_args_until_failure);
3825 defsubr (&Srun_hook_wrapped);
3826 defsubr (&Sfetch_bytecode);
3827 defsubr (&Sbacktrace_debug);
3828 defsubr (&Sbacktrace);
3829 defsubr (&Sbacktrace_frame);
3830 defsubr (&Sbacktrace_eval);
3831 defsubr (&Sbacktrace__locals);
3832 defsubr (&Sspecial_variable_p);
3833 defsubr (&Sfunctionp);
3834 }