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