5edfa79c92680da41011d68000692e6362125e4a
[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 static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
105 static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
106
107 static Lisp_Object
108 specpdl_symbol (union specbinding *pdl)
109 {
110 eassert (pdl->kind >= SPECPDL_LET);
111 return pdl->let.symbol;
112 }
113
114 static Lisp_Object
115 specpdl_old_value (union specbinding *pdl)
116 {
117 eassert (pdl->kind >= SPECPDL_LET);
118 return pdl->let.old_value;
119 }
120
121 static void
122 set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
123 {
124 eassert (pdl->kind >= SPECPDL_LET);
125 pdl->let.old_value = val;
126 }
127
128 static Lisp_Object
129 specpdl_where (union specbinding *pdl)
130 {
131 eassert (pdl->kind > SPECPDL_LET);
132 return pdl->let.where;
133 }
134
135 struct handler *
136 make_catch_handler (Lisp_Object tag)
137 {
138 struct handler *c = xmalloc (sizeof (*c));
139 c->type = CATCHER;
140 c->tag_or_ch = tag;
141 c->val = Qnil;
142 c->var = Qnil;
143 c->body = Qnil;
144 c->next = handlerlist;
145 c->lisp_eval_depth = lisp_eval_depth;
146 c->interrupt_input_blocked = interrupt_input_blocked;
147 c->ptag = make_prompt_tag ();
148 return c;
149 }
150
151 struct handler *
152 make_condition_handler (Lisp_Object tag)
153 {
154 struct handler *c = xmalloc (sizeof (*c));
155 c->type = CONDITION_CASE;
156 c->tag_or_ch = tag;
157 c->val = Qnil;
158 c->var = Qnil;
159 c->body = Qnil;
160 c->next = handlerlist;
161 c->lisp_eval_depth = lisp_eval_depth;
162 c->interrupt_input_blocked = interrupt_input_blocked;
163 c->ptag = make_prompt_tag ();
164 return c;
165 }
166
167 static Lisp_Object eval_fn;
168 static Lisp_Object funcall_fn;
169
170 void
171 init_eval_once (void)
172 {
173 enum { size = 50 };
174 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
175 specpdl_size = size;
176 specpdl = specpdl_ptr = pdlvec + 1;
177 /* Don't forget to update docs (lispref node "Local Variables"). */
178 max_specpdl_size = 10000; /* 1000 is not enough for CEDET's c-by.el. */
179 max_lisp_eval_depth = 10000;
180
181 Vrun_hooks = Qnil;
182
183 eval_fn = scm_c_public_ref ("language elisp runtime", "eval-elisp");
184 funcall_fn = scm_c_public_ref ("elisp-functions", "funcall");
185
186 //scm_set_smob_apply (lisp_vectorlike_tag, apply_lambda, 0, 0, 1);
187 }
188
189 static struct handler *handlerlist_sentinel;
190
191 void
192 init_eval (void)
193 {
194 specpdl_ptr = specpdl;
195 handlerlist_sentinel = make_catch_handler (Qunbound);
196 handlerlist = handlerlist_sentinel;
197 Vquit_flag = Qnil;
198 debug_on_next_call = 0;
199 lisp_eval_depth = 0;
200 #ifdef DEBUG_GCPRO
201 gcpro_level = 0;
202 #endif
203 /* This is less than the initial value of num_nonmacro_input_events. */
204 when_entered_debugger = -1;
205 }
206
207 /* Unwind-protect function used by call_debugger. */
208
209 static void
210 restore_stack_limits (Lisp_Object data)
211 {
212 max_specpdl_size = XINT (XCAR (data));
213 max_lisp_eval_depth = XINT (XCDR (data));
214 }
215
216 static void grow_specpdl (void);
217
218 /* Call the Lisp debugger, giving it argument ARG. */
219
220 Lisp_Object
221 call_debugger (Lisp_Object arg)
222 {
223 bool debug_while_redisplaying;
224 dynwind_begin ();
225 Lisp_Object val;
226 EMACS_INT old_depth = max_lisp_eval_depth;
227 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
228 EMACS_INT old_max = max_specpdl_size;
229
230 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
231 max_lisp_eval_depth = lisp_eval_depth + 40;
232
233 /* Restore limits after leaving the debugger. */
234 record_unwind_protect (restore_stack_limits,
235 Fcons (make_number (old_max),
236 make_number (old_depth)));
237
238 #ifdef HAVE_WINDOW_SYSTEM
239 if (display_hourglass_p)
240 cancel_hourglass ();
241 #endif
242
243 debug_on_next_call = 0;
244 when_entered_debugger = num_nonmacro_input_events;
245
246 /* Resetting redisplaying_p to 0 makes sure that debug output is
247 displayed if the debugger is invoked during redisplay. */
248 debug_while_redisplaying = redisplaying_p;
249 redisplaying_p = 0;
250 specbind (intern ("debugger-may-continue"),
251 debug_while_redisplaying ? Qnil : Qt);
252 specbind (Qinhibit_redisplay, Qnil);
253 specbind (Qinhibit_debugger, Qt);
254
255 #if 0 /* Binding this prevents execution of Lisp code during
256 redisplay, which necessarily leads to display problems. */
257 specbind (Qinhibit_eval_during_redisplay, Qt);
258 #endif
259
260 val = apply1 (Vdebugger, arg);
261
262 /* Interrupting redisplay and resuming it later is not safe under
263 all circumstances. So, when the debugger returns, abort the
264 interrupted redisplay by going back to the top-level. */
265 if (debug_while_redisplaying)
266 Ftop_level ();
267
268 dynwind_end ();
269 return val;
270 }
271 \f
272 static Lisp_Object
273 Fprogn (Lisp_Object body)
274 {
275 Lisp_Object val = Qnil;
276 struct gcpro gcpro1;
277
278 GCPRO1 (body);
279
280 while (CONSP (body))
281 {
282 val = eval_sub (XCAR (body));
283 body = XCDR (body);
284 }
285
286 UNGCPRO;
287 return val;
288 }
289
290 /* Evaluate BODY sequentially, discarding its value. Suitable for
291 record_unwind_protect. */
292
293 void
294 unwind_body (Lisp_Object body)
295 {
296 Fprogn (body);
297 }
298
299 Lisp_Object
300 Ffunction (Lisp_Object args)
301 {
302 Lisp_Object quoted = XCAR (args);
303
304 if (CONSP (XCDR (args)))
305 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
306
307 if (!NILP (Vinternal_interpreter_environment)
308 && CONSP (quoted)
309 && EQ (XCAR (quoted), Qlambda))
310 /* This is a lambda expression within a lexical environment;
311 return an interpreted closure instead of a simple lambda. */
312 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
313 XCDR (quoted)));
314 else
315 /* Simply quote the argument. */
316 return quoted;
317 }
318
319 DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
320 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
321 Aliased variables always have the same value; setting one sets the other.
322 Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
323 omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
324 or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
325 itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
326 then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
327 The return value is BASE-VARIABLE. */)
328 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
329 {
330 sym_t sym;
331
332 CHECK_SYMBOL (new_alias);
333 CHECK_SYMBOL (base_variable);
334
335 sym = XSYMBOL (new_alias);
336
337 if (SYMBOL_CONSTANT (sym))
338 /* Not sure why, but why not? */
339 error ("Cannot make a constant an alias");
340
341 switch (SYMBOL_REDIRECT (sym))
342 {
343 case SYMBOL_FORWARDED:
344 error ("Cannot make an internal variable an alias");
345 case SYMBOL_LOCALIZED:
346 error ("Don't know how to make a localized variable an alias");
347 }
348
349 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
350 If n_a is bound, but b_v is not, set the value of b_v to n_a,
351 so that old-code that affects n_a before the aliasing is setup
352 still works. */
353 if (NILP (Fboundp (base_variable)))
354 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
355
356 {
357 union specbinding *p;
358
359 for (p = specpdl_ptr; p > specpdl; )
360 if ((--p)->kind >= SPECPDL_LET
361 && (EQ (new_alias, specpdl_symbol (p))))
362 error ("Don't know how to make a let-bound variable an alias");
363 }
364
365 SET_SYMBOL_DECLARED_SPECIAL (sym, 1);
366 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (base_variable), 1);
367 SET_SYMBOL_REDIRECT (sym, SYMBOL_VARALIAS);
368 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
369 SET_SYMBOL_CONSTANT (sym, SYMBOL_CONSTANT_P (base_variable));
370 LOADHIST_ATTACH (new_alias);
371 /* Even if docstring is nil: remove old docstring. */
372 Fput (new_alias, Qvariable_documentation, docstring);
373
374 return base_variable;
375 }
376
377 static union specbinding *
378 default_toplevel_binding (Lisp_Object symbol)
379 {
380 union specbinding *binding = NULL;
381 union specbinding *pdl = specpdl_ptr;
382 while (pdl > specpdl)
383 {
384 switch ((--pdl)->kind)
385 {
386 case SPECPDL_LET_DEFAULT:
387 case SPECPDL_LET:
388 if (EQ (specpdl_symbol (pdl), symbol))
389 binding = pdl;
390 break;
391 }
392 }
393 return binding;
394 }
395
396 DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
397 doc: /* Return SYMBOL's toplevel default value.
398 "Toplevel" means outside of any let binding. */)
399 (Lisp_Object symbol)
400 {
401 union specbinding *binding = default_toplevel_binding (symbol);
402 Lisp_Object value
403 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
404 if (!EQ (value, Qunbound))
405 return value;
406 xsignal1 (Qvoid_variable, symbol);
407 }
408
409 DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
410 Sset_default_toplevel_value, 2, 2, 0,
411 doc: /* Set SYMBOL's toplevel default value to VALUE.
412 "Toplevel" means outside of any let binding. */)
413 (Lisp_Object symbol, Lisp_Object value)
414 {
415 union specbinding *binding = default_toplevel_binding (symbol);
416 if (binding)
417 set_specpdl_old_value (binding, value);
418 else
419 Fset_default (symbol, value);
420 return Qnil;
421 }
422
423 /* Make SYMBOL lexically scoped. */
424 DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
425 Smake_var_non_special, 1, 1, 0,
426 doc: /* Internal function. */)
427 (Lisp_Object symbol)
428 {
429 CHECK_SYMBOL (symbol);
430 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol), 0);
431 return Qnil;
432 }
433
434 \f
435 DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
436 doc: /* Return result of expanding macros at top level of FORM.
437 If FORM is not a macro call, it is returned unchanged.
438 Otherwise, the macro is expanded and the expansion is considered
439 in place of FORM. When a non-macro-call results, it is returned.
440
441 The second optional arg ENVIRONMENT specifies an environment of macro
442 definitions to shadow the loaded ones for use in file byte-compilation. */)
443 (Lisp_Object form, Lisp_Object environment)
444 {
445 /* With cleanups from Hallvard Furuseth. */
446 register Lisp_Object expander, sym, def, tem;
447
448 while (1)
449 {
450 /* Come back here each time we expand a macro call,
451 in case it expands into another macro call. */
452 if (!CONSP (form))
453 break;
454 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
455 def = sym = XCAR (form);
456 tem = Qnil;
457 /* Trace symbols aliases to other symbols
458 until we get a symbol that is not an alias. */
459 while (SYMBOLP (def))
460 {
461 QUIT;
462 sym = def;
463 tem = Fassq (sym, environment);
464 if (NILP (tem))
465 {
466 def = SYMBOL_FUNCTION (sym);
467 if (!NILP (def))
468 continue;
469 }
470 break;
471 }
472 /* Right now TEM is the result from SYM in ENVIRONMENT,
473 and if TEM is nil then DEF is SYM's function definition. */
474 if (NILP (tem))
475 {
476 /* SYM is not mentioned in ENVIRONMENT.
477 Look at its function definition. */
478 struct gcpro gcpro1;
479 GCPRO1 (form);
480 def = Fautoload_do_load (def, sym, Qmacro);
481 UNGCPRO;
482 if (!CONSP (def))
483 /* Not defined or definition not suitable. */
484 break;
485 if (!EQ (XCAR (def), Qmacro))
486 break;
487 else expander = XCDR (def);
488 }
489 else
490 {
491 expander = XCDR (tem);
492 if (NILP (expander))
493 break;
494 }
495 {
496 Lisp_Object newform = apply1 (expander, XCDR (form));
497 if (EQ (form, newform))
498 break;
499 else
500 form = newform;
501 }
502 }
503 return form;
504 }
505 \f
506 DEFUN ("call-with-catch", Fcatch, Scatch, 2, 2, 0,
507 doc: /* Eval BODY allowing nonlocal exits using `throw'.
508 TAG is evalled to get the tag to use; it must not be nil.
509
510 Then the BODY is executed.
511 Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
512 If no throw happens, `catch' returns the value of the last BODY form.
513 If a throw happens, it specifies the value to return from `catch'.
514 usage: (catch TAG BODY...) */)
515 (Lisp_Object tag, Lisp_Object thunk)
516 {
517 return internal_catch (tag, call0, thunk);
518 }
519
520 /* Assert that E is true, as a comment only. Use this instead of
521 eassert (E) when E contains variables that might be clobbered by a
522 longjmp. */
523
524 #define clobbered_eassert(E) ((void) 0)
525
526 static void
527 set_handlerlist (void *data)
528 {
529 handlerlist = data;
530 }
531
532 static void
533 restore_handler (void *data)
534 {
535 struct handler *c = data;
536 unblock_input_to (c->interrupt_input_blocked);
537 immediate_quit = 0;
538 }
539
540 struct icc_thunk_env
541 {
542 enum { ICC_0, ICC_1, ICC_2, ICC_3, ICC_N } type;
543 union
544 {
545 Lisp_Object (*fun0) (void);
546 Lisp_Object (*fun1) (Lisp_Object);
547 Lisp_Object (*fun2) (Lisp_Object, Lisp_Object);
548 Lisp_Object (*fun3) (Lisp_Object, Lisp_Object, Lisp_Object);
549 Lisp_Object (*funn) (ptrdiff_t, Lisp_Object *);
550 };
551 union
552 {
553 struct
554 {
555 Lisp_Object arg1;
556 Lisp_Object arg2;
557 Lisp_Object arg3;
558 };
559 struct
560 {
561 ptrdiff_t nargs;
562 Lisp_Object *args;
563 };
564 };
565 struct handler *c;
566 };
567
568 static Lisp_Object
569 icc_thunk (void *data)
570 {
571 Lisp_Object tem;
572 struct icc_thunk_env *e = data;
573 scm_dynwind_begin (0);
574 scm_dynwind_unwind_handler (restore_handler, e->c, 0);
575 scm_dynwind_unwind_handler (set_handlerlist,
576 handlerlist,
577 SCM_F_WIND_EXPLICITLY);
578 handlerlist = e->c;
579 switch (e->type)
580 {
581 case ICC_0:
582 tem = e->fun0 ();
583 break;
584 case ICC_1:
585 tem = e->fun1 (e->arg1);
586 break;
587 case ICC_2:
588 tem = e->fun2 (e->arg1, e->arg2);
589 break;
590 case ICC_3:
591 tem = e->fun3 (e->arg1, e->arg2, e->arg3);
592 break;
593 case ICC_N:
594 tem = e->funn (e->nargs, e->args);
595 break;
596 default:
597 emacs_abort ();
598 }
599 scm_dynwind_end ();
600 return tem;
601 }
602
603 static Lisp_Object
604 icc_handler (void *data, Lisp_Object k, Lisp_Object v)
605 {
606 Lisp_Object (*f) (Lisp_Object) = data;
607 return f (v);
608 }
609
610 struct icc_handler_n_env
611 {
612 Lisp_Object (*fun) (Lisp_Object, ptrdiff_t, Lisp_Object *);
613 ptrdiff_t nargs;
614 Lisp_Object *args;
615 };
616
617 static Lisp_Object
618 icc_handler_n (void *data, Lisp_Object k, Lisp_Object v)
619 {
620 struct icc_handler_n_env *e = data;
621 return e->fun (v, e->nargs, e->args);
622 }
623
624 static Lisp_Object
625 icc_lisp_handler (void *data, Lisp_Object k, Lisp_Object val)
626 {
627 Lisp_Object tem;
628 struct handler *h = data;
629 Lisp_Object var = h->var;
630 scm_dynwind_begin (0);
631 if (!NILP (var))
632 {
633 #if 0
634 if (!NILP (Vinternal_interpreter_environment))
635 specbind (Qinternal_interpreter_environment,
636 Fcons (Fcons (var, val),
637 Vinternal_interpreter_environment));
638 else
639 #endif
640 specbind (var, val);
641 }
642 tem = Fprogn (h->body);
643 scm_dynwind_end ();
644 return tem;
645 }
646
647 /* Set up a catch, then call C function FUNC on argument ARG.
648 FUNC should return a Lisp_Object.
649 This is how catches are done from within C code. */
650
651 Lisp_Object
652 internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
653 {
654 struct handler *c = make_catch_handler (tag);
655 struct icc_thunk_env env = { .type = ICC_1,
656 .fun1 = func,
657 .arg1 = arg,
658 .c = c };
659 return call_with_prompt (c->ptag,
660 make_c_closure (icc_thunk, &env, 0, 0),
661 make_c_closure (icc_handler, Fidentity, 2, 0));
662 }
663
664 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
665 jump to that CATCH, returning VALUE as the value of that catch.
666
667 This is the guts of Fthrow and Fsignal; they differ only in the way
668 they choose the catch tag to throw to. A catch tag for a
669 condition-case form has a TAG of Qnil.
670
671 Before each catch is discarded, unbind all special bindings and
672 execute all unwind-protect clauses made above that catch. Unwind
673 the handler stack as we go, so that the proper handlers are in
674 effect for each unwind-protect clause we run. At the end, restore
675 some static info saved in CATCH, and longjmp to the location
676 specified there.
677
678 This is used for correct unwinding in Fthrow and Fsignal. */
679
680 static Lisp_Object unbind_to_1 (ptrdiff_t, Lisp_Object, bool);
681
682 static _Noreturn void
683 unwind_to_catch (struct handler *catch, Lisp_Object value)
684 {
685 abort_to_prompt (catch->ptag, scm_list_1 (value));
686 }
687
688 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
689 doc: /* Throw to the catch for TAG and return VALUE from it.
690 Both TAG and VALUE are evalled. */)
691 (register Lisp_Object tag, Lisp_Object value)
692 {
693 struct handler *c;
694
695 if (!NILP (tag))
696 for (c = handlerlist; c; c = c->next)
697 {
698 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
699 unwind_to_catch (c, value);
700 }
701 xsignal2 (Qno_catch, tag, value);
702 }
703 \f
704 DEFUN ("call-with-handler", Fcall_with_handler, Scall_with_handler, 4, 4, 0,
705 doc: /* Regain control when an error is signaled.
706 Executes BODYFORM and returns its value if no error happens.
707 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
708 where the BODY is made of Lisp expressions.
709
710 A handler is applicable to an error
711 if CONDITION-NAME is one of the error's condition names.
712 If an error happens, the first applicable handler is run.
713
714 The car of a handler may be a list of condition names instead of a
715 single condition name; then it handles all of them. If the special
716 condition name `debug' is present in this list, it allows another
717 condition in the list to run the debugger if `debug-on-error' and the
718 other usual mechanisms says it should (otherwise, `condition-case'
719 suppresses the debugger).
720
721 When a handler handles an error, control returns to the `condition-case'
722 and it executes the handler's BODY...
723 with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
724 \(If VAR is nil, the handler can't access that information.)
725 Then the value of the last BODY form is returned from the `condition-case'
726 expression.
727
728 See also the function `signal' for more info.
729 usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
730 (Lisp_Object var,
731 Lisp_Object conditions,
732 Lisp_Object hthunk,
733 Lisp_Object thunk)
734 {
735 return internal_lisp_condition_case (var,
736 list2 (intern ("funcall"), thunk),
737 list1 (list2 (conditions, list2 (intern ("funcall"), hthunk))));
738 }
739
740 static Lisp_Object
741 ilcc1 (Lisp_Object var, Lisp_Object bodyform, Lisp_Object handlers)
742 {
743 if (CONSP (handlers))
744 {
745 Lisp_Object clause = XCAR (handlers);
746 Lisp_Object condition = XCAR (clause);
747 Lisp_Object body = XCDR (clause);
748 if (!CONSP (condition))
749 condition = Fcons (condition, Qnil);
750 struct handler *c = make_condition_handler (condition);
751 c->var = var;
752 c->body = body;
753 struct icc_thunk_env env = { .type = ICC_3,
754 .fun3 = ilcc1,
755 .arg1 = var,
756 .arg2 = bodyform,
757 .arg3 = XCDR (handlers),
758 .c = c };
759 return call_with_prompt (c->ptag,
760 make_c_closure (icc_thunk, &env, 0, 0),
761 make_c_closure (icc_lisp_handler, c, 2, 0));
762 }
763 else
764 {
765 return eval_sub (bodyform);
766 }
767 }
768
769 /* Like Fcondition_case, but the args are separate
770 rather than passed in a list. Used by Fbyte_code. */
771
772 Lisp_Object
773 internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
774 Lisp_Object handlers)
775 {
776 Lisp_Object val;
777 struct handler *c;
778 struct handler *oldhandlerlist = handlerlist;
779
780 CHECK_SYMBOL (var);
781
782 for (val = handlers; CONSP (val); val = XCDR (val))
783 {
784 Lisp_Object tem = XCAR (val);
785 if (! (NILP (tem)
786 || (CONSP (tem)
787 && (SYMBOLP (XCAR (tem))
788 || CONSP (XCAR (tem))))))
789 error ("Invalid condition handler: %s",
790 SDATA (Fprin1_to_string (tem, Qt)));
791 }
792
793 return ilcc1 (var, bodyform, Freverse (handlers));
794 }
795
796 /* Call the function BFUN with no arguments, catching errors within it
797 according to HANDLERS. If there is an error, call HFUN with
798 one argument which is the data that describes the error:
799 (SIGNALNAME . DATA)
800
801 HANDLERS can be a list of conditions to catch.
802 If HANDLERS is Qt, catch all errors.
803 If HANDLERS is Qerror, catch all errors
804 but allow the debugger to run if that is enabled. */
805
806 Lisp_Object
807 internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
808 Lisp_Object (*hfun) (Lisp_Object))
809 {
810 Lisp_Object val;
811 struct handler *c = make_condition_handler (handlers);
812
813 struct icc_thunk_env env = { .type = ICC_0, .fun0 = bfun, .c = c };
814 return call_with_prompt (c->ptag,
815 make_c_closure (icc_thunk, &env, 0, 0),
816 make_c_closure (icc_handler, hfun, 2, 0));
817 }
818
819 /* Like internal_condition_case but call BFUN with ARG as its argument. */
820
821 Lisp_Object
822 internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
823 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
824 {
825 Lisp_Object val;
826 struct handler *c = make_condition_handler (handlers);
827
828 struct icc_thunk_env env = { .type = ICC_1,
829 .fun1 = bfun,
830 .arg1 = arg,
831 .c = c };
832 return call_with_prompt (c->ptag,
833 make_c_closure (icc_thunk, &env, 0, 0),
834 make_c_closure (icc_handler, hfun, 2, 0));
835 }
836
837 /* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
838 its arguments. */
839
840 Lisp_Object
841 internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
842 Lisp_Object arg1,
843 Lisp_Object arg2,
844 Lisp_Object handlers,
845 Lisp_Object (*hfun) (Lisp_Object))
846 {
847 Lisp_Object val;
848 struct handler *c = make_condition_handler (handlers);
849 struct icc_thunk_env env = { .type = ICC_2,
850 .fun2 = bfun,
851 .arg1 = arg1,
852 .arg2 = arg2,
853 .c = c };
854 return call_with_prompt (c->ptag,
855 make_c_closure (icc_thunk, &env, 0, 0),
856 make_c_closure (icc_handler, hfun, 2, 0));
857 }
858
859 /* Like internal_condition_case but call BFUN with NARGS as first,
860 and ARGS as second argument. */
861
862 Lisp_Object
863 internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
864 ptrdiff_t nargs,
865 Lisp_Object *args,
866 Lisp_Object handlers,
867 Lisp_Object (*hfun) (Lisp_Object err,
868 ptrdiff_t nargs,
869 Lisp_Object *args))
870 {
871 Lisp_Object val;
872 struct handler *c = make_condition_handler (handlers);
873
874 struct icc_thunk_env env = { .type = ICC_N,
875 .funn = bfun,
876 .nargs = nargs,
877 .args = args,
878 .c = c };
879 struct icc_handler_n_env henv = { .fun = hfun, .nargs = nargs, .args = args };
880 return call_with_prompt (c->ptag,
881 make_c_closure (icc_thunk, &env, 0, 0),
882 make_c_closure (icc_handler_n, &henv, 2, 0));
883 }
884
885 \f
886 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
887 static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
888 Lisp_Object data);
889
890 void
891 process_quit_flag (void)
892 {
893 Lisp_Object flag = Vquit_flag;
894 Vquit_flag = Qnil;
895 if (EQ (flag, Qkill_emacs))
896 Fkill_emacs (Qnil);
897 if (EQ (Vthrow_on_input, flag))
898 Fthrow (Vthrow_on_input, Qt);
899 Fsignal (Qquit, Qnil);
900 }
901
902 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
903 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
904 This function does not return.
905
906 An error symbol is a symbol with an `error-conditions' property
907 that is a list of condition names.
908 A handler for any of those names will get to handle this signal.
909 The symbol `error' should normally be one of them.
910
911 DATA should be a list. Its elements are printed as part of the error message.
912 See Info anchor `(elisp)Definition of signal' for some details on how this
913 error message is constructed.
914 If the signal is handled, DATA is made available to the handler.
915 See also the function `condition-case'. */)
916 (Lisp_Object error_symbol, Lisp_Object data)
917 {
918 /* When memory is full, ERROR-SYMBOL is nil,
919 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
920 That is a special case--don't do this in other situations. */
921 Lisp_Object conditions;
922 Lisp_Object string;
923 Lisp_Object real_error_symbol
924 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
925 register Lisp_Object clause = Qnil;
926 struct handler *h;
927
928 immediate_quit = 0;
929 if (waiting_for_input)
930 emacs_abort ();
931
932 #if 0 /* rms: I don't know why this was here,
933 but it is surely wrong for an error that is handled. */
934 #ifdef HAVE_WINDOW_SYSTEM
935 if (display_hourglass_p)
936 cancel_hourglass ();
937 #endif
938 #endif
939
940 /* This hook is used by edebug. */
941 if (! NILP (Vsignal_hook_function)
942 && ! NILP (error_symbol))
943 {
944 /* Edebug takes care of restoring these variables when it exits. */
945 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
946 max_lisp_eval_depth = lisp_eval_depth + 20;
947
948 if (SPECPDL_INDEX () + 40 > max_specpdl_size)
949 max_specpdl_size = SPECPDL_INDEX () + 40;
950
951 call2 (Vsignal_hook_function, error_symbol, data);
952 }
953
954 conditions = Fget (real_error_symbol, Qerror_conditions);
955
956 for (h = handlerlist; h; h = h->next)
957 {
958 if (h->type != CONDITION_CASE)
959 continue;
960 clause = find_handler_clause (h->tag_or_ch, conditions);
961 if (!NILP (clause))
962 break;
963 }
964
965 if (/* Don't run the debugger for a memory-full error.
966 (There is no room in memory to do that!) */
967 !NILP (error_symbol)
968 && (!NILP (Vdebug_on_signal)
969 /* If no handler is present now, try to run the debugger. */
970 || NILP (clause)
971 /* A `debug' symbol in the handler list disables the normal
972 suppression of the debugger. */
973 || (CONSP (clause) && CONSP (clause)
974 && !NILP (Fmemq (Qdebug, clause)))
975 /* Special handler that means "print a message and run debugger
976 if requested". */
977 || EQ (h->tag_or_ch, Qerror)))
978 {
979 bool debugger_called
980 = maybe_call_debugger (conditions, error_symbol, data);
981 /* We can't return values to code which signaled an error, but we
982 can continue code which has signaled a quit. */
983 if (debugger_called && EQ (real_error_symbol, Qquit))
984 return Qnil;
985 }
986
987 if (!NILP (clause))
988 {
989 Lisp_Object unwind_data
990 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
991
992 unwind_to_catch (h, unwind_data);
993 }
994 else
995 {
996 if (handlerlist != handlerlist_sentinel)
997 /* FIXME: This will come right back here if there's no `top-level'
998 catcher. A better solution would be to abort here, and instead
999 add a catch-all condition handler so we never come here. */
1000 Fthrow (Qtop_level, Qt);
1001 }
1002
1003 if (! NILP (error_symbol))
1004 data = Fcons (error_symbol, data);
1005
1006 string = Ferror_message_string (data);
1007 fatal ("%s", SDATA (string));
1008 }
1009
1010 /* Internal version of Fsignal that never returns.
1011 Used for anything but Qquit (which can return from Fsignal). */
1012
1013 void
1014 xsignal (Lisp_Object error_symbol, Lisp_Object data)
1015 {
1016 Fsignal (error_symbol, data);
1017 emacs_abort ();
1018 }
1019
1020 /* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1021
1022 void
1023 xsignal0 (Lisp_Object error_symbol)
1024 {
1025 xsignal (error_symbol, Qnil);
1026 }
1027
1028 void
1029 xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
1030 {
1031 xsignal (error_symbol, list1 (arg));
1032 }
1033
1034 void
1035 xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
1036 {
1037 xsignal (error_symbol, list2 (arg1, arg2));
1038 }
1039
1040 void
1041 xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1042 {
1043 xsignal (error_symbol, list3 (arg1, arg2, arg3));
1044 }
1045
1046 /* Signal `error' with message S, and additional arg ARG.
1047 If ARG is not a genuine list, make it a one-element list. */
1048
1049 void
1050 signal_error (const char *s, Lisp_Object arg)
1051 {
1052 Lisp_Object tortoise, hare;
1053
1054 hare = tortoise = arg;
1055 while (CONSP (hare))
1056 {
1057 hare = XCDR (hare);
1058 if (!CONSP (hare))
1059 break;
1060
1061 hare = XCDR (hare);
1062 tortoise = XCDR (tortoise);
1063
1064 if (EQ (hare, tortoise))
1065 break;
1066 }
1067
1068 if (!NILP (hare))
1069 arg = list1 (arg);
1070
1071 xsignal (Qerror, Fcons (build_string (s), arg));
1072 }
1073
1074
1075 /* Return true if LIST is a non-nil atom or
1076 a list containing one of CONDITIONS. */
1077
1078 static bool
1079 wants_debugger (Lisp_Object list, Lisp_Object conditions)
1080 {
1081 if (NILP (list))
1082 return 0;
1083 if (! CONSP (list))
1084 return 1;
1085
1086 while (CONSP (conditions))
1087 {
1088 Lisp_Object this, tail;
1089 this = XCAR (conditions);
1090 for (tail = list; CONSP (tail); tail = XCDR (tail))
1091 if (EQ (XCAR (tail), this))
1092 return 1;
1093 conditions = XCDR (conditions);
1094 }
1095 return 0;
1096 }
1097
1098 /* Return true if an error with condition-symbols CONDITIONS,
1099 and described by SIGNAL-DATA, should skip the debugger
1100 according to debugger-ignored-errors. */
1101
1102 static bool
1103 skip_debugger (Lisp_Object conditions, Lisp_Object data)
1104 {
1105 Lisp_Object tail;
1106 bool first_string = 1;
1107 Lisp_Object error_message;
1108
1109 error_message = Qnil;
1110 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
1111 {
1112 if (STRINGP (XCAR (tail)))
1113 {
1114 if (first_string)
1115 {
1116 error_message = Ferror_message_string (data);
1117 first_string = 0;
1118 }
1119
1120 if (fast_string_match (XCAR (tail), error_message) >= 0)
1121 return 1;
1122 }
1123 else
1124 {
1125 Lisp_Object contail;
1126
1127 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
1128 if (EQ (XCAR (tail), XCAR (contail)))
1129 return 1;
1130 }
1131 }
1132
1133 return 0;
1134 }
1135
1136 /* Call the debugger if calling it is currently enabled for CONDITIONS.
1137 SIG and DATA describe the signal. There are two ways to pass them:
1138 = SIG is the error symbol, and DATA is the rest of the data.
1139 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1140 This is for memory-full errors only. */
1141 static bool
1142 maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1143 {
1144 Lisp_Object combined_data;
1145
1146 combined_data = Fcons (sig, data);
1147
1148 if (
1149 /* Don't try to run the debugger with interrupts blocked.
1150 The editing loop would return anyway. */
1151 ! input_blocked_p ()
1152 && NILP (Vinhibit_debugger)
1153 /* Does user want to enter debugger for this kind of error? */
1154 && (EQ (sig, Qquit)
1155 ? debug_on_quit
1156 : wants_debugger (Vdebug_on_error, conditions))
1157 && ! skip_debugger (conditions, combined_data)
1158 /* RMS: What's this for? */
1159 && when_entered_debugger < num_nonmacro_input_events)
1160 {
1161 call_debugger (list2 (Qerror, combined_data));
1162 return 1;
1163 }
1164
1165 return 0;
1166 }
1167
1168 static Lisp_Object
1169 find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
1170 {
1171 register Lisp_Object h;
1172
1173 /* t is used by handlers for all conditions, set up by C code. */
1174 if (EQ (handlers, Qt))
1175 return Qt;
1176
1177 /* error is used similarly, but means print an error message
1178 and run the debugger if that is enabled. */
1179 if (EQ (handlers, Qerror))
1180 return Qt;
1181
1182 for (h = handlers; CONSP (h); h = XCDR (h))
1183 {
1184 Lisp_Object handler = XCAR (h);
1185 if (!NILP (Fmemq (handler, conditions)))
1186 return handlers;
1187 }
1188
1189 return Qnil;
1190 }
1191
1192
1193 /* Dump an error message; called like vprintf. */
1194 void
1195 verror (const char *m, va_list ap)
1196 {
1197 char buf[4000];
1198 ptrdiff_t size = sizeof buf;
1199 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
1200 char *buffer = buf;
1201 ptrdiff_t used;
1202 Lisp_Object string;
1203
1204 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
1205 string = make_string (buffer, used);
1206 if (buffer != buf)
1207 xfree (buffer);
1208
1209 xsignal1 (Qerror, string);
1210 }
1211
1212
1213 /* Dump an error message; called like printf. */
1214
1215 /* VARARGS 1 */
1216 void
1217 error (const char *m, ...)
1218 {
1219 va_list ap;
1220 va_start (ap, m);
1221 verror (m, ap);
1222 }
1223 \f
1224 DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
1225 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1226 This means it contains a description for how to read arguments to give it.
1227 The value is nil for an invalid function or a symbol with no function
1228 definition.
1229
1230 Interactively callable functions include strings and vectors (treated
1231 as keyboard macros), lambda-expressions that contain a top-level call
1232 to `interactive', autoload definitions made by `autoload' with non-nil
1233 fourth argument, and some of the built-in functions of Lisp.
1234
1235 Also, a symbol satisfies `commandp' if its function definition does so.
1236
1237 If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
1238 then strings and vectors are not accepted. */)
1239 (Lisp_Object function, Lisp_Object for_call_interactively)
1240 {
1241 register Lisp_Object fun;
1242 register Lisp_Object funcar;
1243 Lisp_Object if_prop = Qnil;
1244
1245 fun = function;
1246
1247 fun = indirect_function (fun); /* Check cycles. */
1248 if (NILP (fun))
1249 return Qnil;
1250
1251 /* Check an `interactive-form' property if present, analogous to the
1252 function-documentation property. */
1253 fun = function;
1254 while (SYMBOLP (fun))
1255 {
1256 Lisp_Object tmp = Fget (fun, Qinteractive_form);
1257 if (!NILP (tmp))
1258 if_prop = Qt;
1259 fun = Fsymbol_function (fun);
1260 }
1261
1262 if (scm_is_true (scm_procedure_p (fun)))
1263 return (scm_is_pair (scm_assq (Qinteractive_form,
1264 scm_procedure_properties (fun)))
1265 ? Qt : if_prop);
1266 /* Bytecode objects are interactive if they are long enough to
1267 have an element whose index is COMPILED_INTERACTIVE, which is
1268 where the interactive spec is stored. */
1269 else if (COMPILEDP (fun))
1270 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
1271 ? Qt : if_prop);
1272
1273 /* Strings and vectors are keyboard macros. */
1274 if (STRINGP (fun) || VECTORP (fun))
1275 return (NILP (for_call_interactively) ? Qt : Qnil);
1276
1277 /* Lists may represent commands. */
1278 if (!CONSP (fun))
1279 return Qnil;
1280 funcar = XCAR (fun);
1281 if (EQ (funcar, Qclosure))
1282 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1283 ? Qt : if_prop);
1284 else if (EQ (funcar, Qlambda))
1285 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
1286 else if (EQ (funcar, Qautoload))
1287 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
1288 else
1289 return Qnil;
1290 }
1291
1292 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1293 doc: /* Define FUNCTION to autoload from FILE.
1294 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1295 Third arg DOCSTRING is documentation for the function.
1296 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1297 Fifth arg TYPE indicates the type of the object:
1298 nil or omitted says FUNCTION is a function,
1299 `keymap' says FUNCTION is really a keymap, and
1300 `macro' or t says FUNCTION is really a macro.
1301 Third through fifth args give info about the real definition.
1302 They default to nil.
1303 If FUNCTION is already defined other than as an autoload,
1304 this does nothing and returns nil. */)
1305 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
1306 {
1307 CHECK_SYMBOL (function);
1308 CHECK_STRING (file);
1309
1310 /* If function is defined and not as an autoload, don't override. */
1311 if (!NILP (SYMBOL_FUNCTION (function))
1312 && !AUTOLOADP (SYMBOL_FUNCTION (function)))
1313 return Qnil;
1314
1315 return Fdefalias (function,
1316 list5 (Qautoload, file, docstring, interactive, type),
1317 Qnil);
1318 }
1319
1320 void
1321 un_autoload (Lisp_Object oldqueue)
1322 {
1323 Lisp_Object queue, first, second;
1324
1325 /* Queue to unwind is current value of Vautoload_queue.
1326 oldqueue is the shadowed value to leave in Vautoload_queue. */
1327 queue = Vautoload_queue;
1328 Vautoload_queue = oldqueue;
1329 while (CONSP (queue))
1330 {
1331 first = XCAR (queue);
1332 second = Fcdr (first);
1333 first = Fcar (first);
1334 if (EQ (first, make_number (0)))
1335 Vfeatures = second;
1336 else
1337 Ffset (first, second);
1338 queue = XCDR (queue);
1339 }
1340 }
1341
1342 /* Load an autoloaded function.
1343 FUNNAME is the symbol which is the function's name.
1344 FUNDEF is the autoload definition (a list). */
1345
1346 DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1347 doc: /* Load FUNDEF which should be an autoload.
1348 If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1349 in which case the function returns the new autoloaded function value.
1350 If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1351 it is defines a macro. */)
1352 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1353 {
1354 dynwind_begin ();
1355 struct gcpro gcpro1, gcpro2, gcpro3;
1356
1357 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef))) {
1358 dynwind_end ();
1359 return fundef;
1360 }
1361
1362 if (EQ (macro_only, Qmacro))
1363 {
1364 Lisp_Object kind = Fnth (make_number (4), fundef);
1365 if (! (EQ (kind, Qt) || EQ (kind, Qmacro))) {
1366 dynwind_end ();
1367 return fundef;
1368 }
1369 }
1370
1371 /* This is to make sure that loadup.el gives a clear picture
1372 of what files are preloaded and when. */
1373 /*if (! NILP (Vpurify_flag))
1374 error ("Attempt to autoload %s while preparing to dump",
1375 SDATA (SYMBOL_NAME (funname)));*/
1376
1377 CHECK_SYMBOL (funname);
1378 GCPRO3 (funname, fundef, macro_only);
1379
1380 /* Preserve the match data. */
1381 record_unwind_save_match_data ();
1382
1383 /* If autoloading gets an error (which includes the error of failing
1384 to define the function being called), we use Vautoload_queue
1385 to undo function definitions and `provide' calls made by
1386 the function. We do this in the specific case of autoloading
1387 because autoloading is not an explicit request "load this file",
1388 but rather a request to "call this function".
1389
1390 The value saved here is to be restored into Vautoload_queue. */
1391 record_unwind_protect (un_autoload, Vautoload_queue);
1392 Vautoload_queue = Qt;
1393 /* If `macro_only', assume this autoload to be a "best-effort",
1394 so don't signal an error if autoloading fails. */
1395 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1396
1397 /* Once loading finishes, don't undo it. */
1398 Vautoload_queue = Qt;
1399 dynwind_end ();
1400
1401 UNGCPRO;
1402
1403 if (NILP (funname))
1404 return Qnil;
1405 else
1406 {
1407 Lisp_Object fun = Findirect_function (funname, Qnil);
1408
1409 if (!NILP (Fequal (fun, fundef)))
1410 error ("Autoloading failed to define function %s",
1411 SDATA (SYMBOL_NAME (funname)));
1412 else
1413 return fun;
1414 }
1415 }
1416
1417 \f
1418 DEFUN ("eval", Feval, Seval, 1, 2, 0,
1419 doc: /* Evaluate FORM and return its value.
1420 If LEXICAL is t, evaluate using lexical scoping.
1421 LEXICAL can also be an actual lexical environment, in the form of an
1422 alist mapping symbols to their value. */)
1423 (Lisp_Object form, Lisp_Object lexical)
1424 {
1425 dynwind_begin ();
1426 specbind (Qinternal_interpreter_environment,
1427 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
1428 Lisp_Object tem0 = eval_sub (form);
1429 dynwind_end ();
1430 return tem0;
1431 }
1432
1433 /* Grow the specpdl stack by one entry.
1434 The caller should have already initialized the entry.
1435 Signal an error on stack overflow.
1436
1437 Make sure that there is always one unused entry past the top of the
1438 stack, so that the just-initialized entry is safely unwound if
1439 memory exhausted and an error is signaled here. Also, allocate a
1440 never-used entry just before the bottom of the stack; sometimes its
1441 address is taken. */
1442
1443 static void
1444 grow_specpdl (void)
1445 {
1446 specpdl_ptr++;
1447
1448 if (specpdl_ptr == specpdl + specpdl_size)
1449 {
1450 ptrdiff_t count = SPECPDL_INDEX ();
1451 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
1452 union specbinding *pdlvec = specpdl - 1;
1453 ptrdiff_t pdlvecsize = specpdl_size + 1;
1454 if (max_size <= specpdl_size)
1455 {
1456 if (max_specpdl_size < 400)
1457 max_size = max_specpdl_size = 400;
1458 if (max_size <= specpdl_size)
1459 signal_error ("Variable binding depth exceeds max-specpdl-size",
1460 Qnil);
1461 }
1462 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
1463 specpdl = pdlvec + 1;
1464 specpdl_size = pdlvecsize - 1;
1465 specpdl_ptr = specpdl + count;
1466 }
1467 }
1468
1469 static void
1470 set_lisp_eval_depth (void *data)
1471 {
1472 EMACS_INT n = (EMACS_INT) data;
1473 lisp_eval_depth = n;
1474 }
1475
1476 /* Eval a sub-expression of the current expression (i.e. in the same
1477 lexical scope). */
1478 static Lisp_Object
1479 eval_sub_1 (Lisp_Object form)
1480 {
1481 QUIT;
1482 return scm_call_1 (eval_fn, form);
1483 }
1484
1485 Lisp_Object
1486 eval_sub (Lisp_Object form)
1487 {
1488 return scm_c_value_ref (eval_sub_1 (form), 0);
1489 }
1490 \f
1491 static Lisp_Object
1492 values_to_list (Lisp_Object values)
1493 {
1494 Lisp_Object list = Qnil;
1495 for (int i = scm_c_nvalues (values) - 1; i >= 0; i--)
1496 list = Fcons (scm_c_value_ref (values, i), list);
1497 return list;
1498 }
1499
1500 DEFUN ("multiple-value-call", Fmultiple_value_call, Smultiple_value_call,
1501 2, UNEVALLED, 0,
1502 doc: /* Call with multiple values.
1503 usage: (multiple-value-call FUNCTION-FORM FORM) */)
1504 (Lisp_Object args)
1505 {
1506 Lisp_Object function_form = eval_sub (XCAR (args));
1507 Lisp_Object values = Qnil;
1508 while (CONSP (args = XCDR (args)))
1509 values = nconc2 (Fnreverse (values_to_list (eval_sub_1 (XCAR (args)))),
1510 values);
1511 return apply1 (function_form, Fnreverse (values));
1512 }
1513
1514 DEFUN ("values", Fvalues, Svalues, 0, MANY, 0,
1515 doc: /* Return multiple values. */)
1516 (ptrdiff_t nargs, Lisp_Object *args)
1517 {
1518 return scm_c_values (args, nargs);
1519 }
1520 \f
1521 Lisp_Object
1522 Fapply (ptrdiff_t nargs, Lisp_Object *args)
1523 {
1524 ptrdiff_t i;
1525 EMACS_INT numargs;
1526 register Lisp_Object spread_arg;
1527 register Lisp_Object *funcall_args;
1528 Lisp_Object fun, retval;
1529 struct gcpro gcpro1;
1530 USE_SAFE_ALLOCA;
1531
1532 fun = args [0];
1533 funcall_args = 0;
1534 spread_arg = args [nargs - 1];
1535 CHECK_LIST (spread_arg);
1536
1537 numargs = XINT (Flength (spread_arg));
1538
1539 if (numargs == 0)
1540 return Ffuncall (nargs - 1, args);
1541 else if (numargs == 1)
1542 {
1543 args [nargs - 1] = XCAR (spread_arg);
1544 return Ffuncall (nargs, args);
1545 }
1546
1547 numargs += nargs - 2;
1548
1549 /* Optimize for no indirection. */
1550 if (SYMBOLP (fun) && !NILP (fun)
1551 && (fun = SYMBOL_FUNCTION (fun), SYMBOLP (fun)))
1552 fun = indirect_function (fun);
1553 if (NILP (fun))
1554 {
1555 /* Let funcall get the error. */
1556 fun = args[0];
1557 }
1558
1559 /* We add 1 to numargs because funcall_args includes the
1560 function itself as well as its arguments. */
1561 if (!funcall_args)
1562 {
1563 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
1564 GCPRO1 (*funcall_args);
1565 gcpro1.nvars = 1 + numargs;
1566 }
1567
1568 memcpy (funcall_args, args, nargs * word_size);
1569 /* Spread the last arg we got. Its first element goes in
1570 the slot that it used to occupy, hence this value of I. */
1571 i = nargs - 1;
1572 while (!NILP (spread_arg))
1573 {
1574 funcall_args [i++] = XCAR (spread_arg);
1575 spread_arg = XCDR (spread_arg);
1576 }
1577
1578 /* By convention, the caller needs to gcpro Ffuncall's args. */
1579 retval = Ffuncall (gcpro1.nvars, funcall_args);
1580 UNGCPRO;
1581 SAFE_FREE ();
1582
1583 return retval;
1584 }
1585 \f
1586 /* Run hook variables in various ways. */
1587
1588 static Lisp_Object
1589 funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
1590 {
1591 Ffuncall (nargs, args);
1592 return Qnil;
1593 }
1594
1595 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
1596 doc: /* Run each hook in HOOKS.
1597 Each argument should be a symbol, a hook variable.
1598 These symbols are processed in the order specified.
1599 If a hook symbol has a non-nil value, that value may be a function
1600 or a list of functions to be called to run the hook.
1601 If the value is a function, it is called with no arguments.
1602 If it is a list, the elements are called, in order, with no arguments.
1603
1604 Major modes should not use this function directly to run their mode
1605 hook; they should use `run-mode-hooks' instead.
1606
1607 Do not use `make-local-variable' to make a hook variable buffer-local.
1608 Instead, use `add-hook' and specify t for the LOCAL argument.
1609 usage: (run-hooks &rest HOOKS) */)
1610 (ptrdiff_t nargs, Lisp_Object *args)
1611 {
1612 Lisp_Object hook[1];
1613 ptrdiff_t i;
1614
1615 for (i = 0; i < nargs; i++)
1616 {
1617 hook[0] = args[i];
1618 run_hook_with_args (1, hook, funcall_nil);
1619 }
1620
1621 return Qnil;
1622 }
1623
1624 DEFUN ("run-hook-with-args", Frun_hook_with_args,
1625 Srun_hook_with_args, 1, MANY, 0,
1626 doc: /* Run HOOK with the specified arguments ARGS.
1627 HOOK should be a symbol, a hook variable. The value of HOOK
1628 may be nil, a function, or a list of functions. Call each
1629 function in order with arguments ARGS. The final return value
1630 is unspecified.
1631
1632 Do not use `make-local-variable' to make a hook variable buffer-local.
1633 Instead, use `add-hook' and specify t for the LOCAL argument.
1634 usage: (run-hook-with-args HOOK &rest ARGS) */)
1635 (ptrdiff_t nargs, Lisp_Object *args)
1636 {
1637 return run_hook_with_args (nargs, args, funcall_nil);
1638 }
1639
1640 /* NB this one still documents a specific non-nil return value.
1641 (As did run-hook-with-args and run-hook-with-args-until-failure
1642 until they were changed in 24.1.) */
1643 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
1644 Srun_hook_with_args_until_success, 1, MANY, 0,
1645 doc: /* Run HOOK with the specified arguments ARGS.
1646 HOOK should be a symbol, a hook variable. The value of HOOK
1647 may be nil, a function, or a list of functions. Call each
1648 function in order with arguments ARGS, stopping at the first
1649 one that returns non-nil, and return that value. Otherwise (if
1650 all functions return nil, or if there are no functions to call),
1651 return nil.
1652
1653 Do not use `make-local-variable' to make a hook variable buffer-local.
1654 Instead, use `add-hook' and specify t for the LOCAL argument.
1655 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
1656 (ptrdiff_t nargs, Lisp_Object *args)
1657 {
1658 return run_hook_with_args (nargs, args, Ffuncall);
1659 }
1660
1661 static Lisp_Object
1662 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
1663 {
1664 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
1665 }
1666
1667 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
1668 Srun_hook_with_args_until_failure, 1, MANY, 0,
1669 doc: /* Run HOOK with the specified arguments ARGS.
1670 HOOK should be a symbol, a hook variable. The value of HOOK
1671 may be nil, a function, or a list of functions. Call each
1672 function in order with arguments ARGS, stopping at the first
1673 one that returns nil, and return nil. Otherwise (if all functions
1674 return non-nil, or if there are no functions to call), return non-nil
1675 \(do not rely on the precise return value in this case).
1676
1677 Do not use `make-local-variable' to make a hook variable buffer-local.
1678 Instead, use `add-hook' and specify t for the LOCAL argument.
1679 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
1680 (ptrdiff_t nargs, Lisp_Object *args)
1681 {
1682 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
1683 }
1684
1685 static Lisp_Object
1686 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
1687 {
1688 Lisp_Object tmp = args[0], ret;
1689 args[0] = args[1];
1690 args[1] = tmp;
1691 ret = Ffuncall (nargs, args);
1692 args[1] = args[0];
1693 args[0] = tmp;
1694 return ret;
1695 }
1696
1697 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
1698 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
1699 I.e. instead of calling each function FUN directly with arguments ARGS,
1700 it calls WRAP-FUNCTION with arguments FUN and ARGS.
1701 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
1702 aborts and returns that value.
1703 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
1704 (ptrdiff_t nargs, Lisp_Object *args)
1705 {
1706 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
1707 }
1708
1709 /* ARGS[0] should be a hook symbol.
1710 Call each of the functions in the hook value, passing each of them
1711 as arguments all the rest of ARGS (all NARGS - 1 elements).
1712 FUNCALL specifies how to call each function on the hook.
1713 The caller (or its caller, etc) must gcpro all of ARGS,
1714 except that it isn't necessary to gcpro ARGS[0]. */
1715
1716 Lisp_Object
1717 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
1718 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
1719 {
1720 Lisp_Object sym, val, ret = Qnil;
1721 struct gcpro gcpro1, gcpro2, gcpro3;
1722
1723 /* If we are dying or still initializing,
1724 don't do anything--it would probably crash if we tried. */
1725 if (NILP (Vrun_hooks))
1726 return Qnil;
1727
1728 sym = args[0];
1729 val = find_symbol_value (sym);
1730
1731 if (EQ (val, Qunbound) || NILP (val))
1732 return ret;
1733 else if (!CONSP (val) || FUNCTIONP (val))
1734 {
1735 args[0] = val;
1736 return funcall (nargs, args);
1737 }
1738 else
1739 {
1740 Lisp_Object global_vals = Qnil;
1741 GCPRO3 (sym, val, global_vals);
1742
1743 for (;
1744 CONSP (val) && NILP (ret);
1745 val = XCDR (val))
1746 {
1747 if (EQ (XCAR (val), Qt))
1748 {
1749 /* t indicates this hook has a local binding;
1750 it means to run the global binding too. */
1751 global_vals = Fdefault_value (sym);
1752 if (NILP (global_vals)) continue;
1753
1754 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
1755 {
1756 args[0] = global_vals;
1757 ret = funcall (nargs, args);
1758 }
1759 else
1760 {
1761 for (;
1762 CONSP (global_vals) && NILP (ret);
1763 global_vals = XCDR (global_vals))
1764 {
1765 args[0] = XCAR (global_vals);
1766 /* In a global value, t should not occur. If it does, we
1767 must ignore it to avoid an endless loop. */
1768 if (!EQ (args[0], Qt))
1769 ret = funcall (nargs, args);
1770 }
1771 }
1772 }
1773 else
1774 {
1775 args[0] = XCAR (val);
1776 ret = funcall (nargs, args);
1777 }
1778 }
1779
1780 UNGCPRO;
1781 return ret;
1782 }
1783 }
1784
1785 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
1786
1787 void
1788 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
1789 {
1790 Lisp_Object temp[3];
1791 temp[0] = hook;
1792 temp[1] = arg1;
1793 temp[2] = arg2;
1794
1795 Frun_hook_with_args (3, temp);
1796 }
1797 \f
1798 /* Apply fn to arg. */
1799 Lisp_Object
1800 apply1 (Lisp_Object fn, Lisp_Object arg)
1801 {
1802 struct gcpro gcpro1;
1803
1804 GCPRO1 (fn);
1805 if (NILP (arg))
1806 return Ffuncall (1, &fn);
1807 gcpro1.nvars = 2;
1808 {
1809 Lisp_Object args[2];
1810 args[0] = fn;
1811 args[1] = arg;
1812 gcpro1.var = args;
1813 return Fapply (2, args);
1814 }
1815 }
1816
1817 /* Call function fn on no arguments. */
1818 Lisp_Object
1819 call0 (Lisp_Object fn)
1820 {
1821 struct gcpro gcpro1;
1822
1823 GCPRO1 (fn);
1824 return Ffuncall (1, &fn);
1825 }
1826
1827 /* Call function fn with 1 argument arg1. */
1828 /* ARGSUSED */
1829 Lisp_Object
1830 call1 (Lisp_Object fn, Lisp_Object arg1)
1831 {
1832 struct gcpro gcpro1;
1833 Lisp_Object args[2];
1834
1835 args[0] = fn;
1836 args[1] = arg1;
1837 GCPRO1 (args[0]);
1838 gcpro1.nvars = 2;
1839 return Ffuncall (2, args);
1840 }
1841
1842 /* Call function fn with 2 arguments arg1, arg2. */
1843 /* ARGSUSED */
1844 Lisp_Object
1845 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
1846 {
1847 struct gcpro gcpro1;
1848 Lisp_Object args[3];
1849 args[0] = fn;
1850 args[1] = arg1;
1851 args[2] = arg2;
1852 GCPRO1 (args[0]);
1853 gcpro1.nvars = 3;
1854 return Ffuncall (3, args);
1855 }
1856
1857 /* Call function fn with 3 arguments arg1, arg2, arg3. */
1858 /* ARGSUSED */
1859 Lisp_Object
1860 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
1861 {
1862 struct gcpro gcpro1;
1863 Lisp_Object args[4];
1864 args[0] = fn;
1865 args[1] = arg1;
1866 args[2] = arg2;
1867 args[3] = arg3;
1868 GCPRO1 (args[0]);
1869 gcpro1.nvars = 4;
1870 return Ffuncall (4, args);
1871 }
1872
1873 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
1874 /* ARGSUSED */
1875 Lisp_Object
1876 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1877 Lisp_Object arg4)
1878 {
1879 struct gcpro gcpro1;
1880 Lisp_Object args[5];
1881 args[0] = fn;
1882 args[1] = arg1;
1883 args[2] = arg2;
1884 args[3] = arg3;
1885 args[4] = arg4;
1886 GCPRO1 (args[0]);
1887 gcpro1.nvars = 5;
1888 return Ffuncall (5, args);
1889 }
1890
1891 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
1892 /* ARGSUSED */
1893 Lisp_Object
1894 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1895 Lisp_Object arg4, Lisp_Object arg5)
1896 {
1897 struct gcpro gcpro1;
1898 Lisp_Object args[6];
1899 args[0] = fn;
1900 args[1] = arg1;
1901 args[2] = arg2;
1902 args[3] = arg3;
1903 args[4] = arg4;
1904 args[5] = arg5;
1905 GCPRO1 (args[0]);
1906 gcpro1.nvars = 6;
1907 return Ffuncall (6, args);
1908 }
1909
1910 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
1911 /* ARGSUSED */
1912 Lisp_Object
1913 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1914 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
1915 {
1916 struct gcpro gcpro1;
1917 Lisp_Object args[7];
1918 args[0] = fn;
1919 args[1] = arg1;
1920 args[2] = arg2;
1921 args[3] = arg3;
1922 args[4] = arg4;
1923 args[5] = arg5;
1924 args[6] = arg6;
1925 GCPRO1 (args[0]);
1926 gcpro1.nvars = 7;
1927 return Ffuncall (7, args);
1928 }
1929
1930 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
1931 /* ARGSUSED */
1932 Lisp_Object
1933 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1934 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
1935 {
1936 struct gcpro gcpro1;
1937 Lisp_Object args[8];
1938 args[0] = fn;
1939 args[1] = arg1;
1940 args[2] = arg2;
1941 args[3] = arg3;
1942 args[4] = arg4;
1943 args[5] = arg5;
1944 args[6] = arg6;
1945 args[7] = arg7;
1946 GCPRO1 (args[0]);
1947 gcpro1.nvars = 8;
1948 return Ffuncall (8, args);
1949 }
1950
1951 /* The caller should GCPRO all the elements of ARGS. */
1952
1953 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
1954 doc: /* Non-nil if OBJECT is a function. */)
1955 (Lisp_Object object)
1956 {
1957 if (FUNCTIONP (object))
1958 return Qt;
1959 return Qnil;
1960 }
1961
1962 static Lisp_Object
1963 Ffuncall1 (ptrdiff_t nargs, Lisp_Object *args)
1964 {
1965 return scm_call_n (funcall_fn, args, nargs);
1966 }
1967
1968 Lisp_Object
1969 Ffuncall (ptrdiff_t nargs, Lisp_Object *args)
1970 {
1971 return scm_c_value_ref (Ffuncall1 (nargs, args), 0);
1972 }
1973 \f
1974 static Lisp_Object
1975 apply_lambda (Lisp_Object fun, Lisp_Object args)
1976 {
1977 Lisp_Object args_left;
1978 ptrdiff_t i;
1979 EMACS_INT numargs;
1980 register Lisp_Object *arg_vector;
1981 struct gcpro gcpro1, gcpro2, gcpro3;
1982 register Lisp_Object tem;
1983 USE_SAFE_ALLOCA;
1984
1985 numargs = XFASTINT (Flength (args));
1986 SAFE_ALLOCA_LISP (arg_vector, numargs);
1987 args_left = args;
1988
1989 GCPRO3 (*arg_vector, args_left, fun);
1990 gcpro1.nvars = 0;
1991
1992 for (i = 0; i < numargs; )
1993 {
1994 tem = Fcar (args_left), args_left = Fcdr (args_left);
1995 arg_vector[i++] = tem;
1996 gcpro1.nvars = i;
1997 }
1998
1999 UNGCPRO;
2000
2001 tem = funcall_lambda (fun, numargs, arg_vector);
2002
2003 SAFE_FREE ();
2004 return tem;
2005 }
2006
2007 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2008 and return the result of evaluation.
2009 FUN must be either a lambda-expression or a compiled-code object. */
2010
2011 static Lisp_Object
2012 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2013 register Lisp_Object *arg_vector)
2014 {
2015 Lisp_Object val, syms_left, next, lexenv;
2016 dynwind_begin ();
2017 ptrdiff_t i;
2018 bool optional, rest;
2019
2020 if (CONSP (fun))
2021 {
2022 if (EQ (XCAR (fun), Qclosure))
2023 {
2024 fun = XCDR (fun); /* Drop `closure'. */
2025 lexenv = XCAR (fun);
2026 CHECK_LIST_CONS (fun, fun);
2027 }
2028 else
2029 lexenv = Qnil;
2030 syms_left = XCDR (fun);
2031 if (CONSP (syms_left))
2032 syms_left = XCAR (syms_left);
2033 else
2034 xsignal1 (Qinvalid_function, fun);
2035 }
2036 else
2037 emacs_abort ();
2038
2039 i = optional = rest = 0;
2040 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
2041 {
2042 QUIT;
2043
2044 next = XCAR (syms_left);
2045 if (!SYMBOLP (next))
2046 xsignal1 (Qinvalid_function, fun);
2047
2048 if (EQ (next, Qand_rest))
2049 rest = 1;
2050 else if (EQ (next, Qand_optional))
2051 optional = 1;
2052 else
2053 {
2054 Lisp_Object arg;
2055 if (rest)
2056 {
2057 arg = Flist (nargs - i, &arg_vector[i]);
2058 i = nargs;
2059 }
2060 else if (i < nargs)
2061 arg = arg_vector[i++];
2062 else if (!optional)
2063 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2064 else
2065 arg = Qnil;
2066
2067 /* Bind the argument. */
2068 if (!NILP (lexenv) && SYMBOLP (next))
2069 /* Lexically bind NEXT by adding it to the lexenv alist. */
2070 lexenv = Fcons (Fcons (next, arg), lexenv);
2071 else
2072 /* Dynamically bind NEXT. */
2073 specbind (next, arg);
2074 }
2075 }
2076
2077 if (!NILP (syms_left))
2078 xsignal1 (Qinvalid_function, fun);
2079 else if (i < nargs)
2080 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2081
2082 if (!EQ (lexenv, Vinternal_interpreter_environment))
2083 /* Instantiate a new lexical environment. */
2084 specbind (Qinternal_interpreter_environment, lexenv);
2085
2086 val = Fprogn (XCDR (XCDR (fun)));
2087
2088 dynwind_end ();
2089 return val;
2090 }
2091
2092 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2093 1, 1, 0,
2094 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
2095 (Lisp_Object object)
2096 {
2097 Lisp_Object tem;
2098
2099 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
2100 {
2101 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
2102 if (!CONSP (tem))
2103 {
2104 tem = AREF (object, COMPILED_BYTECODE);
2105 if (CONSP (tem) && STRINGP (XCAR (tem)))
2106 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
2107 else
2108 error ("Invalid byte code");
2109 }
2110 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2111 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
2112 }
2113 return object;
2114 }
2115 \f
2116 /* Return true if SYMBOL currently has a let-binding
2117 which was made in the buffer that is now current. */
2118
2119 bool
2120 let_shadows_buffer_binding_p (sym_t symbol)
2121 {
2122 union specbinding *p;
2123 Lisp_Object buf = Fcurrent_buffer ();
2124
2125 for (p = specpdl_ptr; p > specpdl; )
2126 if ((--p)->kind > SPECPDL_LET)
2127 {
2128 sym_t let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2129 eassert (SYMBOL_REDIRECT (let_bound_symbol) != SYMBOL_VARALIAS);
2130 if (symbol == let_bound_symbol
2131 && EQ (specpdl_where (p), buf))
2132 return 1;
2133 }
2134
2135 return 0;
2136 }
2137
2138 bool
2139 let_shadows_global_binding_p (Lisp_Object symbol)
2140 {
2141 union specbinding *p;
2142
2143 for (p = specpdl_ptr; p > specpdl; )
2144 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
2145 return 1;
2146
2147 return 0;
2148 }
2149
2150 /* `specpdl_ptr' describes which variable is
2151 let-bound, so it can be properly undone when we unbind_to.
2152 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
2153 - SYMBOL is the variable being bound. Note that it should not be
2154 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2155 to record V2 here).
2156 - WHERE tells us in which buffer the binding took place.
2157 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
2158 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
2159 i.e. bindings to the default value of a variable which can be
2160 buffer-local. */
2161
2162 void
2163 specbind (Lisp_Object symbol, Lisp_Object value)
2164 {
2165 sym_t sym;
2166
2167 CHECK_SYMBOL (symbol);
2168 sym = XSYMBOL (symbol);
2169
2170 start:
2171 switch (SYMBOL_REDIRECT (sym))
2172 {
2173 case SYMBOL_VARALIAS:
2174 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
2175 case SYMBOL_PLAINVAL:
2176 /* The most common case is that of a non-constant symbol with a
2177 trivial value. Make that as fast as we can. */
2178 specpdl_ptr->let.kind = SPECPDL_LET;
2179 specpdl_ptr->let.symbol = symbol;
2180 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
2181 grow_specpdl ();
2182 if (! SYMBOL_CONSTANT (sym))
2183 SET_SYMBOL_VAL (sym, value);
2184 else
2185 set_internal (symbol, value, Qnil, 1);
2186 break;
2187 case SYMBOL_LOCALIZED:
2188 if (SYMBOL_BLV (sym)->frame_local)
2189 error ("Frame-local vars cannot be let-bound");
2190 case SYMBOL_FORWARDED:
2191 {
2192 Lisp_Object ovalue = find_symbol_value (symbol);
2193 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
2194 specpdl_ptr->let.symbol = symbol;
2195 specpdl_ptr->let.old_value = ovalue;
2196 specpdl_ptr->let.where = Fcurrent_buffer ();
2197
2198 eassert (SYMBOL_REDIRECT (sym) != SYMBOL_LOCALIZED
2199 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
2200
2201 if (SYMBOL_REDIRECT (sym) == SYMBOL_LOCALIZED)
2202 {
2203 if (!blv_found (SYMBOL_BLV (sym)))
2204 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2205 }
2206 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
2207 {
2208 /* If SYMBOL is a per-buffer variable which doesn't have a
2209 buffer-local value here, make the `let' change the global
2210 value by changing the value of SYMBOL in all buffers not
2211 having their own value. This is consistent with what
2212 happens with other buffer-local variables. */
2213 if (NILP (Flocal_variable_p (symbol, Qnil)))
2214 {
2215 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2216 grow_specpdl ();
2217 Fset_default (symbol, value);
2218 goto done;
2219 }
2220 }
2221 else
2222 specpdl_ptr->let.kind = SPECPDL_LET;
2223
2224 grow_specpdl ();
2225 set_internal (symbol, value, Qnil, 1);
2226 break;
2227 }
2228 default: emacs_abort ();
2229 }
2230
2231 done:
2232 scm_dynwind_unwind_handler (unbind_once, NULL, SCM_F_WIND_EXPLICITLY);
2233 }
2234
2235 /* Push unwind-protect entries of various types. */
2236
2237 void
2238 record_unwind_protect_1 (void (*function) (Lisp_Object), Lisp_Object arg,
2239 bool wind_explicitly)
2240 {
2241 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
2242 }
2243
2244 void
2245 record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
2246 {
2247 record_unwind_protect_1 (function, arg, true);
2248 }
2249
2250 void
2251 record_unwind_protect_ptr_1 (void (*function) (void *), void *arg,
2252 bool wind_explicitly)
2253 {
2254 scm_dynwind_unwind_handler (function,
2255 arg,
2256 (wind_explicitly
2257 ? SCM_F_WIND_EXPLICITLY
2258 : 0));
2259 }
2260
2261 void
2262 record_unwind_protect_ptr (void (*function) (void *), void *arg)
2263 {
2264 record_unwind_protect_ptr_1 (function, arg, true);
2265 }
2266
2267 void
2268 record_unwind_protect_int_1 (void (*function) (int), int arg,
2269 bool wind_explicitly)
2270 {
2271 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
2272 }
2273
2274 void
2275 record_unwind_protect_int (void (*function) (int), int arg)
2276 {
2277 record_unwind_protect_int_1 (function, arg, true);
2278 }
2279
2280 static void
2281 call_void (void *data)
2282 {
2283 ((void (*) (void)) data) ();
2284 }
2285
2286 void
2287 record_unwind_protect_void_1 (void (*function) (void),
2288 bool wind_explicitly)
2289 {
2290 record_unwind_protect_ptr_1 (call_void, function, wind_explicitly);
2291 }
2292
2293 void
2294 record_unwind_protect_void (void (*function) (void))
2295 {
2296 record_unwind_protect_void_1 (function, true);
2297 }
2298
2299 static void
2300 unbind_once (void *ignore)
2301 {
2302 /* Decrement specpdl_ptr before we do the work to unbind it, so
2303 that an error in unbinding won't try to unbind the same entry
2304 again. Take care to copy any parts of the binding needed
2305 before invoking any code that can make more bindings. */
2306
2307 specpdl_ptr--;
2308
2309 switch (specpdl_ptr->kind)
2310 {
2311 case SPECPDL_LET:
2312 { /* If variable has a trivial value (no forwarding), we can
2313 just set it. No need to check for constant symbols here,
2314 since that was already done by specbind. */
2315 sym_t sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
2316 if (SYMBOL_REDIRECT (sym) == SYMBOL_PLAINVAL)
2317 {
2318 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
2319 break;
2320 }
2321 else
2322 { /* FALLTHROUGH!!
2323 NOTE: we only ever come here if make_local_foo was used for
2324 the first time on this var within this let. */
2325 }
2326 }
2327 case SPECPDL_LET_DEFAULT:
2328 Fset_default (specpdl_symbol (specpdl_ptr),
2329 specpdl_old_value (specpdl_ptr));
2330 break;
2331 case SPECPDL_LET_LOCAL:
2332 {
2333 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
2334 Lisp_Object where = specpdl_where (specpdl_ptr);
2335 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
2336 eassert (BUFFERP (where));
2337
2338 /* If this was a local binding, reset the value in the appropriate
2339 buffer, but only if that buffer's binding still exists. */
2340 if (!NILP (Flocal_variable_p (symbol, where)))
2341 set_internal (symbol, old_value, where, 1);
2342 }
2343 break;
2344 }
2345 }
2346
2347 void
2348 dynwind_begin (void)
2349 {
2350 scm_dynwind_begin (0);
2351 }
2352
2353 void
2354 dynwind_end (void)
2355 {
2356 scm_dynwind_end ();
2357 }
2358
2359 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
2360 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
2361 A special variable is one that will be bound dynamically, even in a
2362 context where binding is lexical by default. */)
2363 (Lisp_Object symbol)
2364 {
2365 CHECK_SYMBOL (symbol);
2366 return SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol)) ? Qt : Qnil;
2367 }
2368 \f
2369 _Noreturn SCM
2370 abort_to_prompt (SCM tag, SCM arglst)
2371 {
2372 static SCM var = SCM_UNDEFINED;
2373 if (SCM_UNBNDP (var))
2374 var = scm_c_public_lookup ("guile", "abort-to-prompt");
2375
2376 scm_apply_1 (scm_variable_ref (var), tag, arglst);
2377 emacs_abort ();
2378 }
2379
2380 SCM
2381 call_with_prompt (SCM tag, SCM thunk, SCM handler)
2382 {
2383 static SCM var = SCM_UNDEFINED;
2384 if (SCM_UNBNDP (var))
2385 var = scm_c_public_lookup ("guile", "call-with-prompt");
2386
2387 return scm_call_3 (scm_variable_ref (var), tag, thunk, handler);
2388 }
2389
2390 SCM
2391 make_prompt_tag (void)
2392 {
2393 static SCM var = SCM_UNDEFINED;
2394 if (SCM_UNBNDP (var))
2395 var = scm_c_public_lookup ("guile", "make-prompt-tag");
2396
2397 return scm_call_0 (scm_variable_ref (var));
2398 }
2399 \f
2400 void
2401 syms_of_eval (void)
2402 {
2403 #include "eval.x"
2404
2405 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
2406 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
2407 If Lisp code tries to increase the total number past this amount,
2408 an error is signaled.
2409 You can safely use a value considerably larger than the default value,
2410 if that proves inconveniently small. However, if you increase it too far,
2411 Emacs could run out of memory trying to make the stack bigger.
2412 Note that this limit may be silently increased by the debugger
2413 if `debug-on-error' or `debug-on-quit' is set. */);
2414
2415 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
2416 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
2417
2418 This limit serves to catch infinite recursions for you before they cause
2419 actual stack overflow in C, which would be fatal for Emacs.
2420 You can safely make it considerably larger than its default value,
2421 if that proves inconveniently small. However, if you increase it too far,
2422 Emacs could overflow the real C stack, and crash. */);
2423
2424 DEFVAR_LISP ("quit-flag", Vquit_flag,
2425 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
2426 If the value is t, that means do an ordinary quit.
2427 If the value equals `throw-on-input', that means quit by throwing
2428 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
2429 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
2430 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
2431 Vquit_flag = Qnil;
2432
2433 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
2434 doc: /* Non-nil inhibits C-g quitting from happening immediately.
2435 Note that `quit-flag' will still be set by typing C-g,
2436 so a quit will be signaled as soon as `inhibit-quit' is nil.
2437 To prevent this happening, set `quit-flag' to nil
2438 before making `inhibit-quit' nil. */);
2439 Vinhibit_quit = Qnil;
2440
2441 DEFSYM (Qinhibit_quit, "inhibit-quit");
2442 DEFSYM (Qautoload, "autoload");
2443 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
2444 DEFSYM (Qmacro, "macro");
2445 DEFSYM (Qdeclare, "declare");
2446
2447 /* Note that the process handling also uses Qexit, but we don't want
2448 to staticpro it twice, so we just do it here. */
2449 DEFSYM (Qexit, "exit");
2450
2451 DEFSYM (Qinteractive, "interactive");
2452 DEFSYM (Qcommandp, "commandp");
2453 DEFSYM (Qand_rest, "&rest");
2454 DEFSYM (Qand_optional, "&optional");
2455 DEFSYM (Qclosure, "closure");
2456 DEFSYM (Qdebug, "debug");
2457
2458 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
2459 doc: /* Non-nil means never enter the debugger.
2460 Normally set while the debugger is already active, to avoid recursive
2461 invocations. */);
2462 Vinhibit_debugger = Qnil;
2463
2464 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
2465 doc: /* Non-nil means enter debugger if an error is signaled.
2466 Does not apply to errors handled by `condition-case' or those
2467 matched by `debug-ignored-errors'.
2468 If the value is a list, an error only means to enter the debugger
2469 if one of its condition symbols appears in the list.
2470 When you evaluate an expression interactively, this variable
2471 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
2472 The command `toggle-debug-on-error' toggles this.
2473 See also the variable `debug-on-quit' and `inhibit-debugger'. */);
2474 Vdebug_on_error = Qnil;
2475
2476 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
2477 doc: /* List of errors for which the debugger should not be called.
2478 Each element may be a condition-name or a regexp that matches error messages.
2479 If any element applies to a given error, that error skips the debugger
2480 and just returns to top level.
2481 This overrides the variable `debug-on-error'.
2482 It does not apply to errors handled by `condition-case'. */);
2483 Vdebug_ignored_errors = Qnil;
2484
2485 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
2486 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
2487 Does not apply if quit is handled by a `condition-case'. */);
2488 debug_on_quit = 0;
2489
2490 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
2491 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
2492
2493 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
2494 doc: /* Non-nil means debugger may continue execution.
2495 This is nil when the debugger is called under circumstances where it
2496 might not be safe to continue. */);
2497 debugger_may_continue = 1;
2498
2499 DEFVAR_LISP ("debugger", Vdebugger,
2500 doc: /* Function to call to invoke debugger.
2501 If due to frame exit, args are `exit' and the value being returned;
2502 this function's value will be returned instead of that.
2503 If due to error, args are `error' and a list of the args to `signal'.
2504 If due to `apply' or `funcall' entry, one arg, `lambda'.
2505 If due to `eval' entry, one arg, t. */);
2506 Vdebugger = Qnil;
2507
2508 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
2509 doc: /* If non-nil, this is a function for `signal' to call.
2510 It receives the same arguments that `signal' was given.
2511 The Edebug package uses this to regain control. */);
2512 Vsignal_hook_function = Qnil;
2513
2514 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
2515 doc: /* Non-nil means call the debugger regardless of condition handlers.
2516 Note that `debug-on-error', `debug-on-quit' and friends
2517 still determine whether to handle the particular condition. */);
2518 Vdebug_on_signal = Qnil;
2519
2520 /* When lexical binding is being used,
2521 Vinternal_interpreter_environment is non-nil, and contains an alist
2522 of lexically-bound variable, or (t), indicating an empty
2523 environment. The lisp name of this variable would be
2524 `internal-interpreter-environment' if it weren't hidden.
2525 Every element of this list can be either a cons (VAR . VAL)
2526 specifying a lexical binding, or a single symbol VAR indicating
2527 that this variable should use dynamic scoping. */
2528 DEFSYM (Qinternal_interpreter_environment,
2529 "internal-interpreter-environment");
2530 DEFVAR_LISP ("internal-interpreter-environment",
2531 Vinternal_interpreter_environment,
2532 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
2533 When lexical binding is not being used, this variable is nil.
2534 A value of `(t)' indicates an empty environment, otherwise it is an
2535 alist of active lexical bindings. */);
2536 Vinternal_interpreter_environment = Qnil;
2537 /* Don't export this variable to Elisp, so no one can mess with it
2538 (Just imagine if someone makes it buffer-local). */
2539 //Funintern (Qinternal_interpreter_environment, Qnil);
2540
2541 DEFSYM (Vrun_hooks, "run-hooks");
2542
2543 staticpro (&Vautoload_queue);
2544 Vautoload_queue = Qnil;
2545 staticpro (&Vsignaling_function);
2546 Vsignaling_function = Qnil;
2547
2548 inhibit_lisp_code = Qnil;
2549 }