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