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