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