don't use function-equal in nadvice
[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"
316bec86
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
JB
75
76/* Pointer to first unused element in specpdl. */
4c576a83 77
9349e5f7 78union specbinding *specpdl_ptr;
db9f0278 79
db9f0278 80/* Depth in Lisp evaluations and function calls. */
4c576a83 81
adf2aa61 82EMACS_INT lisp_eval_depth;
db9f0278 83
be857679 84/* The value of num_nonmacro_input_events as of the last time we
82da7701 85 started to enter the debugger. If we decide to enter the debugger
be857679 86 again when this is still equal to num_nonmacro_input_events, then we
82da7701
JB
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. */
4c576a83 90
d311d28c 91static EMACS_INT when_entered_debugger;
db9f0278 92
a2ff3819
GM
93/* The function from which the last `signal' was called. Set in
94 Fsignal. */
2f592f95 95/* FIXME: We should probably get rid of this! */
a2ff3819
GM
96Lisp_Object Vsignaling_function;
97
d1f55f16
CY
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. */
102Lisp_Object inhibit_lisp_code;
103
f66c7cf8 104static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
7200d79c 105static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
e46f2325 106
84575e67 107static Lisp_Object
9349e5f7 108specpdl_symbol (union specbinding *pdl)
84575e67
PE
109{
110 eassert (pdl->kind >= SPECPDL_LET);
9349e5f7 111 return pdl->let.symbol;
84575e67
PE
112}
113
114static Lisp_Object
9349e5f7 115specpdl_old_value (union specbinding *pdl)
84575e67
PE
116{
117 eassert (pdl->kind >= SPECPDL_LET);
9349e5f7 118 return pdl->let.old_value;
84575e67
PE
119}
120
56ea7291
SM
121static void
122set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
123{
124 eassert (pdl->kind >= SPECPDL_LET);
125 pdl->let.old_value = val;
126}
127
84575e67 128static Lisp_Object
9349e5f7 129specpdl_where (union specbinding *pdl)
84575e67
PE
130{
131 eassert (pdl->kind > SPECPDL_LET);
9349e5f7 132 return pdl->let.where;
84575e67
PE
133}
134
316bec86
BT
135struct handler *
136make_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;
316bec86
BT
146 c->interrupt_input_blocked = interrupt_input_blocked;
147 c->ptag = make_prompt_tag ();
148 return c;
149}
150
151struct handler *
152make_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;
316bec86
BT
162 c->interrupt_input_blocked = interrupt_input_blocked;
163 c->ptag = make_prompt_tag ();
164 return c;
165}
2f592f95 166
1bf3ed7c
RT
167static Lisp_Object eval_fn;
168static Lisp_Object funcall_fn;
169
dfcf069d 170void
d3da34e0 171init_eval_once (void)
db9f0278 172{
98e8eae1 173 enum { size = 50 };
9349e5f7 174 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
98e8eae1 175 specpdl_size = size;
9349e5f7 176 specpdl = specpdl_ptr = pdlvec + 1;
6588243d 177 /* Don't forget to update docs (lispref node "Local Variables"). */
1bf3ed7c
RT
178 max_specpdl_size = 10000; /* 1000 is not enough for CEDET's c-by.el. */
179 max_lisp_eval_depth = 10000;
34d470ba
RS
180
181 Vrun_hooks = Qnil;
1bf3ed7c
RT
182
183 eval_fn = scm_c_public_ref ("language elisp runtime", "eval-elisp");
184 funcall_fn = scm_c_public_ref ("elisp-functions", "funcall");
7a7dfc01 185
4c103b64 186 //scm_set_smob_apply (lisp_vectorlike_tag, apply_lambda, 0, 0, 1);
db9f0278
JB
187}
188
316bec86 189static struct handler *handlerlist_sentinel;
70de5e86 190
dfcf069d 191void
d3da34e0 192init_eval (void)
db9f0278
JB
193{
194 specpdl_ptr = specpdl;
316bec86
BT
195 handlerlist_sentinel = make_catch_handler (Qunbound);
196 handlerlist = handlerlist_sentinel;
db9f0278
JB
197 Vquit_flag = Qnil;
198 debug_on_next_call = 0;
199 lisp_eval_depth = 0;
87e21fbd 200#ifdef DEBUG_GCPRO
15934ffa 201 gcpro_level = 0;
87e21fbd 202#endif
be857679 203 /* This is less than the initial value of num_nonmacro_input_events. */
b5b911f9 204 when_entered_debugger = -1;
db9f0278
JB
205}
206
f6d62986 207/* Unwind-protect function used by call_debugger. */
9f5903bb 208
27e498e6 209static void
d3da34e0 210restore_stack_limits (Lisp_Object data)
9f5903bb
RS
211{
212 max_specpdl_size = XINT (XCAR (data));
213 max_lisp_eval_depth = XINT (XCDR (data));
214}
215
9cad4576
DA
216static void grow_specpdl (void);
217
9f5903bb
RS
218/* Call the Lisp debugger, giving it argument ARG. */
219
7f7e0167 220Lisp_Object
d3da34e0 221call_debugger (Lisp_Object arg)
db9f0278 222{
1882aa38 223 bool debug_while_redisplaying;
3200038e 224 dynwind_begin ();
3648c842 225 Lisp_Object val;
575593db
DA
226 EMACS_INT old_depth = max_lisp_eval_depth;
227 /* Do not allow max_specpdl_size less than actual depth (Bug#16603). */
316bec86 228 EMACS_INT old_max = max_specpdl_size;
9f5903bb
RS
229
230 if (lisp_eval_depth + 40 > max_lisp_eval_depth)
231 max_lisp_eval_depth = lisp_eval_depth + 40;
232
9cad4576
DA
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)));
177c0ea7 237
d148e14d 238#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
239 if (display_hourglass_p)
240 cancel_hourglass ();
237c23b0
GM
241#endif
242
db9f0278 243 debug_on_next_call = 0;
be857679 244 when_entered_debugger = num_nonmacro_input_events;
3648c842
GM
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;
556d7314
GM
250 specbind (intern ("debugger-may-continue"),
251 debug_while_redisplaying ? Qnil : Qt);
8efb6cc7 252 specbind (Qinhibit_redisplay, Qnil);
45b82ad0 253 specbind (Qinhibit_debugger, Qt);
9db6f6b4
GM
254
255#if 0 /* Binding this prevents execution of Lisp code during
256 redisplay, which necessarily leads to display problems. */
8efb6cc7 257 specbind (Qinhibit_eval_during_redisplay, Qt);
9db6f6b4 258#endif
177c0ea7 259
3648c842
GM
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
1b1acc13 264 interrupted redisplay by going back to the top-level. */
3648c842
GM
265 if (debug_while_redisplaying)
266 Ftop_level ();
267
3200038e
BT
268 dynwind_end ();
269 return val;
db9f0278 270}
db9f0278 271\f
707dd106
RT
272static Lisp_Object
273Fprogn (Lisp_Object body)
db9f0278 274{
27e498e6 275 Lisp_Object val = Qnil;
db9f0278
JB
276 struct gcpro gcpro1;
277
27e498e6 278 GCPRO1 (body);
db9f0278 279
27e498e6 280 while (CONSP (body))
db9f0278 281 {
27e498e6
PE
282 val = eval_sub (XCAR (body));
283 body = XCDR (body);
db9f0278 284 }
db9f0278
JB
285
286 UNGCPRO;
287 return val;
288}
289
02c66599 290/* Evaluate BODY sequentially, discarding its value. Suitable for
27e498e6
PE
291 record_unwind_protect. */
292
293void
294unwind_body (Lisp_Object body)
295{
296 Fprogn (body);
297}
298
1bf3ed7c
RT
299Lisp_Object
300Ffunction (Lisp_Object args)
db9f0278 301{
b9598260
SM
302 Lisp_Object quoted = XCAR (args);
303
16b0520a 304 if (CONSP (XCDR (args)))
1283140e 305 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
b9598260
SM
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. */
23aba0ea
SM
312 return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment,
313 XCDR (quoted)));
b9598260
SM
314 else
315 /* Simply quote the argument. */
316 return quoted;
db9f0278
JB
317}
318
1848d15d 319DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
4a9308b8 320 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
e102f0d8 321Aliased variables always have the same value; setting one sets the other.
4a9308b8 322Third arg DOCSTRING, if non-nil, is documentation for NEW-ALIAS. If it is
dd60787c
GM
323omitted or nil, NEW-ALIAS gets the documentation string of BASE-VARIABLE,
324or of the variable at the end of the chain of aliases, if BASE-VARIABLE is
325itself an alias. If NEW-ALIAS is bound, and BASE-VARIABLE is not,
326then the value of BASE-VARIABLE is set to that of NEW-ALIAS.
4a9308b8 327The return value is BASE-VARIABLE. */)
5842a27b 328 (Lisp_Object new_alias, Lisp_Object base_variable, Lisp_Object docstring)
19cebf5a 329{
db633ecc 330 sym_t sym;
1848d15d 331
4a9308b8
JB
332 CHECK_SYMBOL (new_alias);
333 CHECK_SYMBOL (base_variable);
19cebf5a 334
4a9308b8 335 sym = XSYMBOL (new_alias);
ce5b453a 336
db633ecc 337 if (SYMBOL_CONSTANT (sym))
178f2507
SM
338 /* Not sure why, but why not? */
339 error ("Cannot make a constant an alias");
ce5b453a 340
db633ecc 341 switch (SYMBOL_REDIRECT (sym))
ce5b453a
SM
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
dd60787c 349 /* http://lists.gnu.org/archive/html/emacs-devel/2008-04/msg00834.html
ce5b453a
SM
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)))
94b612ad 354 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
ce5b453a
SM
355
356 {
9349e5f7 357 union specbinding *p;
ce5b453a 358
bc985141 359 for (p = specpdl_ptr; p > specpdl; )
2f592f95
SM
360 if ((--p)->kind >= SPECPDL_LET
361 && (EQ (new_alias, specpdl_symbol (p))))
ce5b453a
SM
362 error ("Don't know how to make a let-bound variable an alias");
363 }
364
db633ecc
BT
365 SET_SYMBOL_DECLARED_SPECIAL (sym, 1);
366 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (base_variable), 1);
367 SET_SYMBOL_REDIRECT (sym, SYMBOL_VARALIAS);
ce5b453a 368 SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
db633ecc 369 SET_SYMBOL_CONSTANT (sym, SYMBOL_CONSTANT_P (base_variable));
4a9308b8 370 LOADHIST_ATTACH (new_alias);
ce5b453a
SM
371 /* Even if docstring is nil: remove old docstring. */
372 Fput (new_alias, Qvariable_documentation, docstring);
1848d15d 373
4a9308b8 374 return base_variable;
19cebf5a
GM
375}
376
a104f656
SM
377static union specbinding *
378default_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
396DEFUN ("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
409DEFUN ("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}
19cebf5a 422
513749ee
SM
423/* Make SYMBOL lexically scoped. */
424DEFUN ("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);
db633ecc 430 SET_SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol), 0);
513749ee
SM
431 return Qnil;
432}
433
db9f0278 434\f
db9f0278 435DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
9dbc9081
PJ
436 doc: /* Return result of expanding macros at top level of FORM.
437If FORM is not a macro call, it is returned unchanged.
438Otherwise, the macro is expanded and the expansion is considered
439in place of FORM. When a non-macro-call results, it is returned.
440
441The second optional arg ENVIRONMENT specifies an environment of macro
442definitions to shadow the loaded ones for use in file byte-compilation. */)
5842a27b 443 (Lisp_Object form, Lisp_Object environment)
db9f0278 444{
23d6b5a6 445 /* With cleanups from Hallvard Furuseth. */
db9f0278
JB
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. */
90165123 452 if (!CONSP (form))
db9f0278 453 break;
23d6b5a6 454 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
03699b14 455 def = sym = XCAR (form);
23d6b5a6 456 tem = Qnil;
db9f0278
JB
457 /* Trace symbols aliases to other symbols
458 until we get a symbol that is not an alias. */
90165123 459 while (SYMBOLP (def))
db9f0278
JB
460 {
461 QUIT;
23d6b5a6 462 sym = def;
79e8bfbf 463 tem = Fassq (sym, environment);
265a9e55 464 if (NILP (tem))
db9f0278 465 {
d8ccbf1a 466 def = SYMBOL_FUNCTION (sym);
eadf1faa 467 if (!NILP (def))
23d6b5a6 468 continue;
db9f0278 469 }
23d6b5a6 470 break;
db9f0278 471 }
79e8bfbf 472 /* Right now TEM is the result from SYM in ENVIRONMENT,
db9f0278 473 and if TEM is nil then DEF is SYM's function definition. */
265a9e55 474 if (NILP (tem))
db9f0278 475 {
79e8bfbf 476 /* SYM is not mentioned in ENVIRONMENT.
db9f0278 477 Look at its function definition. */
7abaf5cc
SM
478 struct gcpro gcpro1;
479 GCPRO1 (form);
480 def = Fautoload_do_load (def, sym, Qmacro);
481 UNGCPRO;
eadf1faa 482 if (!CONSP (def))
f6d62986 483 /* Not defined or definition not suitable. */
db9f0278 484 break;
7abaf5cc 485 if (!EQ (XCAR (def), Qmacro))
db9f0278 486 break;
03699b14 487 else expander = XCDR (def);
db9f0278
JB
488 }
489 else
490 {
03699b14 491 expander = XCDR (tem);
265a9e55 492 if (NILP (expander))
db9f0278
JB
493 break;
494 }
4f18a4ed
SM
495 {
496 Lisp_Object newform = apply1 (expander, XCDR (form));
497 if (EQ (form, newform))
498 break;
499 else
500 form = newform;
501 }
db9f0278
JB
502 }
503 return form;
504}
505\f
1bf3ed7c 506DEFUN ("call-with-catch", Fcatch, Scatch, 2, 2, 0,
9dbc9081
PJ
507 doc: /* Eval BODY allowing nonlocal exits using `throw'.
508TAG is evalled to get the tag to use; it must not be nil.
509
510Then the BODY is executed.
1d632ccf 511Within BODY, a call to `throw' with the same TAG exits BODY and this `catch'.
9dbc9081
PJ
512If no throw happens, `catch' returns the value of the last BODY form.
513If a throw happens, it specifies the value to return from `catch'.
7a25dc6d 514usage: (catch TAG BODY...) */)
1bf3ed7c 515 (Lisp_Object tag, Lisp_Object thunk)
db9f0278 516{
1bf3ed7c 517 return internal_catch (tag, call0, thunk);
db9f0278
JB
518}
519
b52f569d
PE
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
316bec86
BT
526static void
527set_handlerlist (void *data)
528{
529 handlerlist = data;
530}
531
532static void
533restore_handler (void *data)
534{
535 struct handler *c = data;
316bec86
BT
536 unblock_input_to (c->interrupt_input_blocked);
537 immediate_quit = 0;
538}
539
540struct 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
568static Lisp_Object
569icc_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
603static Lisp_Object
604icc_handler (void *data, Lisp_Object k, Lisp_Object v)
605{
606 Lisp_Object (*f) (Lisp_Object) = data;
607 return f (v);
608}
609
610struct 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
617static Lisp_Object
618icc_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
624static Lisp_Object
625icc_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 {
34c1b9bb 633#if 0
316bec86
BT
634 if (!NILP (Vinternal_interpreter_environment))
635 specbind (Qinternal_interpreter_environment,
636 Fcons (Fcons (var, val),
637 Vinternal_interpreter_environment));
638 else
34c1b9bb 639#endif
316bec86
BT
640 specbind (var, val);
641 }
642 tem = Fprogn (h->body);
643 scm_dynwind_end ();
644 return tem;
645}
646
db9f0278
JB
647/* Set up a catch, then call C function FUNC on argument ARG.
648 FUNC should return a Lisp_Object.
2f592f95 649 This is how catches are done from within C code. */
db9f0278
JB
650
651Lisp_Object
d3da34e0 652internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
db9f0278 653{
316bec86
BT
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));
db9f0278
JB
662}
663
ba410f40
JB
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.
db9f0278 666
4d7e6e51 667 This is the guts of Fthrow and Fsignal; they differ only in the way
ba410f40
JB
668 they choose the catch tag to throw to. A catch tag for a
669 condition-case form has a TAG of Qnil.
db9f0278 670
ba410f40
JB
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
4d7e6e51 676 specified there.
ba410f40
JB
677
678 This is used for correct unwinding in Fthrow and Fsignal. */
db9f0278 679
e1ad689c
BT
680static Lisp_Object unbind_to_1 (ptrdiff_t, Lisp_Object, bool);
681
845ca893 682static _Noreturn void
adf2aa61 683unwind_to_catch (struct handler *catch, Lisp_Object value)
db9f0278 684{
316bec86 685 abort_to_prompt (catch->ptag, scm_list_1 (value));
db9f0278
JB
686}
687
a7ca3326 688DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
9dbc9081
PJ
689 doc: /* Throw to the catch for TAG and return VALUE from it.
690Both TAG and VALUE are evalled. */)
5842a27b 691 (register Lisp_Object tag, Lisp_Object value)
db9f0278 692{
adf2aa61 693 struct handler *c;
db9f0278 694
8788120f 695 if (!NILP (tag))
adf2aa61 696 for (c = handlerlist; c; c = c->next)
8788120f 697 {
adf2aa61 698 if (c->type == CATCHER && EQ (c->tag_or_ch, tag))
8788120f
KS
699 unwind_to_catch (c, value);
700 }
734d55a2 701 xsignal2 (Qno_catch, tag, value);
db9f0278 702}
db9f0278 703\f
1bf3ed7c 704DEFUN ("call-with-handler", Fcall_with_handler, Scall_with_handler, 4, 4, 0,
9dbc9081 705 doc: /* Regain control when an error is signaled.
1b1acc13 706Executes BODYFORM and returns its value if no error happens.
9dbc9081
PJ
707Each element of HANDLERS looks like (CONDITION-NAME BODY...)
708where the BODY is made of Lisp expressions.
709
710A handler is applicable to an error
711if CONDITION-NAME is one of the error's condition names.
712If an error happens, the first applicable handler is run.
713
024a2d76
CY
714The car of a handler may be a list of condition names instead of a
715single condition name; then it handles all of them. If the special
716condition name `debug' is present in this list, it allows another
717condition in the list to run the debugger if `debug-on-error' and the
718other usual mechanisms says it should (otherwise, `condition-case'
719suppresses the debugger).
9dbc9081 720
c997bb25
RS
721When a handler handles an error, control returns to the `condition-case'
722and it executes the handler's BODY...
d0acbbaf 723with VAR bound to (ERROR-SYMBOL . SIGNAL-DATA) from the error.
bb8e180f 724\(If VAR is nil, the handler can't access that information.)
c997bb25
RS
725Then the value of the last BODY form is returned from the `condition-case'
726expression.
9dbc9081 727
9dbc9081 728See also the function `signal' for more info.
2b47b74d 729usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1bf3ed7c
RT
730 (Lisp_Object var,
731 Lisp_Object conditions,
732 Lisp_Object hthunk,
733 Lisp_Object thunk)
db9f0278 734{
1bf3ed7c
RT
735 return internal_lisp_condition_case (var,
736 list2 (intern ("funcall"), thunk),
737 list1 (list2 (conditions, list2 (intern ("funcall"), hthunk))));
ee830945
RS
738}
739
316bec86
BT
740static Lisp_Object
741ilcc1 (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
ee830945
RS
769/* Like Fcondition_case, but the args are separate
770 rather than passed in a list. Used by Fbyte_code. */
771
772Lisp_Object
d3da34e0
JB
773internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
774 Lisp_Object handlers)
ee830945
RS
775{
776 Lisp_Object val;
adf2aa61
SM
777 struct handler *c;
778 struct handler *oldhandlerlist = handlerlist;
ee830945 779
b7826503 780 CHECK_SYMBOL (var);
82da7701 781
2b47b74d 782 for (val = handlers; CONSP (val); val = XCDR (val))
82da7701 783 {
adf2aa61 784 Lisp_Object tem = XCAR (val);
5f96776a
RS
785 if (! (NILP (tem)
786 || (CONSP (tem)
03699b14
KR
787 && (SYMBOLP (XCAR (tem))
788 || CONSP (XCAR (tem))))))
e6c3da20
EZ
789 error ("Invalid condition handler: %s",
790 SDATA (Fprin1_to_string (tem, Qt)));
82da7701 791 }
db9f0278 792
316bec86 793 return ilcc1 (var, bodyform, Freverse (handlers));
db9f0278
JB
794}
795
f029ca5f
RS
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
db9f0278 806Lisp_Object
d3da34e0
JB
807internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
808 Lisp_Object (*hfun) (Lisp_Object))
db9f0278
JB
809{
810 Lisp_Object val;
316bec86 811 struct handler *c = make_condition_handler (handlers);
adf2aa61 812
316bec86
BT
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));
db9f0278
JB
817}
818
2659a09f 819/* Like internal_condition_case but call BFUN with ARG as its argument. */
f029ca5f 820
d227775c 821Lisp_Object
d3da34e0
JB
822internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
823 Lisp_Object handlers, Lisp_Object (*hfun) (Lisp_Object))
d227775c
RS
824{
825 Lisp_Object val;
316bec86 826 struct handler *c = make_condition_handler (handlers);
adf2aa61 827
316bec86
BT
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));
d227775c 835}
10b29d41 836
53967e09
CY
837/* Like internal_condition_case_1 but call BFUN with ARG1 and ARG2 as
838 its arguments. */
839
840Lisp_Object
178f2507
SM
841internal_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))
53967e09
CY
846{
847 Lisp_Object val;
316bec86
BT
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));
53967e09 857}
10b29d41 858
2659a09f 859/* Like internal_condition_case but call BFUN with NARGS as first,
10b29d41
GM
860 and ARGS as second argument. */
861
862Lisp_Object
f66c7cf8
PE
863internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
864 ptrdiff_t nargs,
178f2507
SM
865 Lisp_Object *args,
866 Lisp_Object handlers,
cc92c454
SM
867 Lisp_Object (*hfun) (Lisp_Object err,
868 ptrdiff_t nargs,
869 Lisp_Object *args))
10b29d41
GM
870{
871 Lisp_Object val;
316bec86 872 struct handler *c = make_condition_handler (handlers);
adf2aa61 873
316bec86
BT
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));
10b29d41
GM
883}
884
d227775c 885\f
7d47b580 886static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1882aa38
PE
887static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
888 Lisp_Object data);
db9f0278 889
6d5eb5b0
SM
890void
891process_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
a7ca3326 902DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
9dbc9081
PJ
903 doc: /* Signal an error. Args are ERROR-SYMBOL and associated DATA.
904This function does not return.
905
906An error symbol is a symbol with an `error-conditions' property
907that is a list of condition names.
908A handler for any of those names will get to handle this signal.
909The symbol `error' should normally be one of them.
910
911DATA should be a list. Its elements are printed as part of the error message.
3297ec22
LT
912See Info anchor `(elisp)Definition of signal' for some details on how this
913error message is constructed.
9dbc9081
PJ
914If the signal is handled, DATA is made available to the handler.
915See also the function `condition-case'. */)
5842a27b 916 (Lisp_Object error_symbol, Lisp_Object data)
db9f0278 917{
bfa8ca43 918 /* When memory is full, ERROR-SYMBOL is nil,
26631f2b
RS
919 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
920 That is a special case--don't do this in other situations. */
db9f0278 921 Lisp_Object conditions;
c11d3d17 922 Lisp_Object string;
e7f7fbaa
SM
923 Lisp_Object real_error_symbol
924 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
925 register Lisp_Object clause = Qnil;
926 struct handler *h;
db9f0278 927
0caaedb1 928 immediate_quit = 0;
d90fb7af 929 if (waiting_for_input)
1088b922 930 emacs_abort ();
db9f0278 931
26631f2b
RS
932#if 0 /* rms: I don't know why this was here,
933 but it is surely wrong for an error that is handled. */
d148e14d 934#ifdef HAVE_WINDOW_SYSTEM
df6c90d8
GM
935 if (display_hourglass_p)
936 cancel_hourglass ();
48f8dfa3 937#endif
177c0ea7 938#endif
48f8dfa3 939
61ede770 940 /* This hook is used by edebug. */
26631f2b
RS
941 if (! NILP (Vsignal_hook_function)
942 && ! NILP (error_symbol))
9f5903bb
RS
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 }
61ede770 953
1ea9dec4 954 conditions = Fget (real_error_symbol, Qerror_conditions);
db9f0278 955
e7f7fbaa 956 for (h = handlerlist; h; h = h->next)
db9f0278 957 {
adf2aa61
SM
958 if (h->type != CONDITION_CASE)
959 continue;
960 clause = find_handler_clause (h->tag_or_ch, conditions);
265a9e55 961 if (!NILP (clause))
e7f7fbaa 962 break;
db9f0278 963 }
475545b5 964
e7f7fbaa 965 if (/* Don't run the debugger for a memory-full error.
e7c1b6ef 966 (There is no room in memory to do that!) */
e7f7fbaa
SM
967 !NILP (error_symbol)
968 && (!NILP (Vdebug_on_signal)
969 /* If no handler is present now, try to run the debugger. */
970 || NILP (clause)
bd1ba3e8
CY
971 /* A `debug' symbol in the handler list disables the normal
972 suppression of the debugger. */
afd4479f
SM
973 || (CONSP (clause) && CONSP (clause)
974 && !NILP (Fmemq (Qdebug, clause)))
e7f7fbaa
SM
975 /* Special handler that means "print a message and run debugger
976 if requested". */
adf2aa61 977 || EQ (h->tag_or_ch, Qerror)))
e7f7fbaa 978 {
1882aa38 979 bool debugger_called
e7f7fbaa
SM
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;
475545b5 985 }
db9f0278 986
e7f7fbaa
SM
987 if (!NILP (clause))
988 {
989 Lisp_Object unwind_data
990 = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
475545b5 991
adf2aa61 992 unwind_to_catch (h, unwind_data);
e7f7fbaa
SM
993 }
994 else
995 {
316bec86 996 if (handlerlist != handlerlist_sentinel)
70de5e86
SM
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. */
e7f7fbaa
SM
1000 Fthrow (Qtop_level, Qt);
1001 }
c11d3d17 1002
1ea9dec4 1003 if (! NILP (error_symbol))
c11d3d17 1004 data = Fcons (error_symbol, data);
475545b5 1005
c11d3d17 1006 string = Ferror_message_string (data);
583f48b9 1007 fatal ("%s", SDATA (string));
db9f0278
JB
1008}
1009
734d55a2
KS
1010/* Internal version of Fsignal that never returns.
1011 Used for anything but Qquit (which can return from Fsignal). */
1012
1013void
d3da34e0 1014xsignal (Lisp_Object error_symbol, Lisp_Object data)
734d55a2
KS
1015{
1016 Fsignal (error_symbol, data);
1088b922 1017 emacs_abort ();
734d55a2
KS
1018}
1019
1020/* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
1021
1022void
d3da34e0 1023xsignal0 (Lisp_Object error_symbol)
734d55a2
KS
1024{
1025 xsignal (error_symbol, Qnil);
1026}
1027
1028void
d3da34e0 1029xsignal1 (Lisp_Object error_symbol, Lisp_Object arg)
734d55a2
KS
1030{
1031 xsignal (error_symbol, list1 (arg));
1032}
1033
1034void
d3da34e0 1035xsignal2 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2)
734d55a2
KS
1036{
1037 xsignal (error_symbol, list2 (arg1, arg2));
1038}
1039
1040void
d3da34e0 1041xsignal3 (Lisp_Object error_symbol, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
734d55a2
KS
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
1049void
a8fe7202 1050signal_error (const char *s, Lisp_Object arg)
734d55a2
KS
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))
6c6f1994 1069 arg = list1 (arg);
734d55a2
KS
1070
1071 xsignal (Qerror, Fcons (build_string (s), arg));
1072}
1073
1074
1882aa38 1075/* Return true if LIST is a non-nil atom or
128c0f66
RM
1076 a list containing one of CONDITIONS. */
1077
1882aa38 1078static bool
d3da34e0 1079wants_debugger (Lisp_Object list, Lisp_Object conditions)
128c0f66 1080{
4de86b16 1081 if (NILP (list))
128c0f66
RM
1082 return 0;
1083 if (! CONSP (list))
1084 return 1;
1085
ab67260b 1086 while (CONSP (conditions))
128c0f66 1087 {
ab67260b 1088 Lisp_Object this, tail;
03699b14
KR
1089 this = XCAR (conditions);
1090 for (tail = list; CONSP (tail); tail = XCDR (tail))
1091 if (EQ (XCAR (tail), this))
128c0f66 1092 return 1;
03699b14 1093 conditions = XCDR (conditions);
128c0f66 1094 }
ab67260b 1095 return 0;
128c0f66
RM
1096}
1097
1882aa38 1098/* Return true if an error with condition-symbols CONDITIONS,
fc950e09 1099 and described by SIGNAL-DATA, should skip the debugger
1b1acc13 1100 according to debugger-ignored-errors. */
fc950e09 1101
1882aa38 1102static bool
d3da34e0 1103skip_debugger (Lisp_Object conditions, Lisp_Object data)
fc950e09
KH
1104{
1105 Lisp_Object tail;
1882aa38 1106 bool first_string = 1;
fc950e09
KH
1107 Lisp_Object error_message;
1108
17401c97
GM
1109 error_message = Qnil;
1110 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
fc950e09 1111 {
03699b14 1112 if (STRINGP (XCAR (tail)))
fc950e09
KH
1113 {
1114 if (first_string)
1115 {
1116 error_message = Ferror_message_string (data);
1117 first_string = 0;
1118 }
177c0ea7 1119
03699b14 1120 if (fast_string_match (XCAR (tail), error_message) >= 0)
fc950e09
KH
1121 return 1;
1122 }
1123 else
1124 {
1125 Lisp_Object contail;
1126
17401c97 1127 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
03699b14 1128 if (EQ (XCAR (tail), XCAR (contail)))
fc950e09
KH
1129 return 1;
1130 }
1131 }
1132
1133 return 0;
1134}
1135
ddaa36e1 1136/* Call the debugger if calling it is currently enabled for CONDITIONS.
7d47b580
JB
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. */
1882aa38 1141static bool
d3da34e0 1142maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
ddaa36e1
AS
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. */
4d7e6e51 1151 ! input_blocked_p ()
45b82ad0 1152 && NILP (Vinhibit_debugger)
ddaa36e1
AS
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)
f6d62986 1158 /* RMS: What's this for? */
ddaa36e1
AS
1159 && when_entered_debugger < num_nonmacro_input_events)
1160 {
6c6f1994 1161 call_debugger (list2 (Qerror, combined_data));
ddaa36e1
AS
1162 return 1;
1163 }
1164
1165 return 0;
1166}
1167
db9f0278 1168static Lisp_Object
7d47b580 1169find_handler_clause (Lisp_Object handlers, Lisp_Object conditions)
db9f0278
JB
1170{
1171 register Lisp_Object h;
db9f0278 1172
f01cbfdd
RS
1173 /* t is used by handlers for all conditions, set up by C code. */
1174 if (EQ (handlers, Qt))
db9f0278 1175 return Qt;
f01cbfdd 1176
61ede770
RS
1177 /* error is used similarly, but means print an error message
1178 and run the debugger if that is enabled. */
e7f7fbaa
SM
1179 if (EQ (handlers, Qerror))
1180 return Qt;
f01cbfdd 1181
e7f7fbaa 1182 for (h = handlers; CONSP (h); h = XCDR (h))
db9f0278 1183 {
e7f7fbaa 1184 Lisp_Object handler = XCAR (h);
adf2aa61
SM
1185 if (!NILP (Fmemq (handler, conditions)))
1186 return handlers;
db9f0278 1187 }
f01cbfdd 1188
db9f0278
JB
1189 return Qnil;
1190}
1191
db9f0278 1192
f6d62986 1193/* Dump an error message; called like vprintf. */
db9f0278 1194void
b3ffc17c 1195verror (const char *m, va_list ap)
db9f0278 1196{
70476b54 1197 char buf[4000];
c2d1e36d
PE
1198 ptrdiff_t size = sizeof buf;
1199 ptrdiff_t size_max = STRING_BYTES_BOUND + 1;
9125da08 1200 char *buffer = buf;
c2d1e36d 1201 ptrdiff_t used;
9125da08
RS
1202 Lisp_Object string;
1203
d749b01b 1204 used = evxprintf (&buffer, &size, buf, size_max, m, ap);
5fdb398c 1205 string = make_string (buffer, used);
eb3f1cc8 1206 if (buffer != buf)
9ae6734f 1207 xfree (buffer);
9125da08 1208
734d55a2 1209 xsignal1 (Qerror, string);
db9f0278 1210}
b3ffc17c
DN
1211
1212
f6d62986 1213/* Dump an error message; called like printf. */
b3ffc17c
DN
1214
1215/* VARARGS 1 */
1216void
1217error (const char *m, ...)
1218{
1219 va_list ap;
1220 va_start (ap, m);
1221 verror (m, ap);
b3ffc17c 1222}
db9f0278 1223\f
a7ca3326 1224DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
9dbc9081
PJ
1225 doc: /* Non-nil if FUNCTION makes provisions for interactive calling.
1226This means it contains a description for how to read arguments to give it.
1227The value is nil for an invalid function or a symbol with no function
1228definition.
1229
1230Interactively callable functions include strings and vectors (treated
1231as keyboard macros), lambda-expressions that contain a top-level call
1232to `interactive', autoload definitions made by `autoload' with non-nil
1233fourth argument, and some of the built-in functions of Lisp.
1234
e72706be
RS
1235Also, a symbol satisfies `commandp' if its function definition does so.
1236
1237If the optional argument FOR-CALL-INTERACTIVELY is non-nil,
769b4fb2 1238then strings and vectors are not accepted. */)
5842a27b 1239 (Lisp_Object function, Lisp_Object for_call_interactively)
db9f0278
JB
1240{
1241 register Lisp_Object fun;
1242 register Lisp_Object funcar;
52b71f49 1243 Lisp_Object if_prop = Qnil;
db9f0278
JB
1244
1245 fun = function;
1246
eadf1faa
SM
1247 fun = indirect_function (fun); /* Check cycles. */
1248 if (NILP (fun))
ffd56f97 1249 return Qnil;
db9f0278 1250
52b71f49 1251 /* Check an `interactive-form' property if present, analogous to the
eadf1faa 1252 function-documentation property. */
52b71f49
SM
1253 fun = function;
1254 while (SYMBOLP (fun))
1255 {
2b9aa051 1256 Lisp_Object tmp = Fget (fun, Qinteractive_form);
52b71f49
SM
1257 if (!NILP (tmp))
1258 if_prop = Qt;
1259 fun = Fsymbol_function (fun);
1260 }
1261
b7faa6e4 1262 if (scm_is_true (scm_procedure_p (fun)))
7aad910c
BT
1263 return (scm_is_pair (scm_assq (Qinteractive_form,
1264 scm_procedure_properties (fun)))
b7faa6e4 1265 ? Qt : if_prop);
db9f0278
JB
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. */
90165123 1269 else if (COMPILEDP (fun))
845975f5 1270 return ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
52b71f49 1271 ? Qt : if_prop);
db9f0278
JB
1272
1273 /* Strings and vectors are keyboard macros. */
52b71f49 1274 if (STRINGP (fun) || VECTORP (fun))
6e33efc4 1275 return (NILP (for_call_interactively) ? Qt : Qnil);
db9f0278
JB
1276
1277 /* Lists may represent commands. */
1278 if (!CONSP (fun))
1279 return Qnil;
ed16fb98 1280 funcar = XCAR (fun);
b38b1ec0 1281 if (EQ (funcar, Qclosure))
7200d79c
SM
1282 return (!NILP (Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun)))))
1283 ? Qt : if_prop);
23aba0ea 1284 else if (EQ (funcar, Qlambda))
52b71f49 1285 return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
b38b1ec0 1286 else if (EQ (funcar, Qautoload))
52b71f49 1287 return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
db9f0278
JB
1288 else
1289 return Qnil;
1290}
1291
db9f0278 1292DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
9dbc9081
PJ
1293 doc: /* Define FUNCTION to autoload from FILE.
1294FUNCTION is a symbol; FILE is a file name string to pass to `load'.
1295Third arg DOCSTRING is documentation for the function.
1296Fourth arg INTERACTIVE if non-nil says function can be called interactively.
1297Fifth 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.
1301Third through fifth args give info about the real definition.
1302They default to nil.
1303If FUNCTION is already defined other than as an autoload,
1304this does nothing and returns nil. */)
5842a27b 1305 (Lisp_Object function, Lisp_Object file, Lisp_Object docstring, Lisp_Object interactive, Lisp_Object type)
db9f0278 1306{
b7826503
PJ
1307 CHECK_SYMBOL (function);
1308 CHECK_STRING (file);
db9f0278 1309
f6d62986 1310 /* If function is defined and not as an autoload, don't override. */
d8ccbf1a
BT
1311 if (!NILP (SYMBOL_FUNCTION (function))
1312 && !AUTOLOADP (SYMBOL_FUNCTION (function)))
db9f0278
JB
1313 return Qnil;
1314
32e5c58c
SM
1315 return Fdefalias (function,
1316 list5 (Qautoload, file, docstring, interactive, type),
1317 Qnil);
db9f0278
JB
1318}
1319
27e498e6 1320void
d3da34e0 1321un_autoload (Lisp_Object oldqueue)
db9f0278 1322{
27e498e6 1323 Lisp_Object queue, first, second;
db9f0278
JB
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 {
e509f168 1331 first = XCAR (queue);
db9f0278
JB
1332 second = Fcdr (first);
1333 first = Fcar (first);
47b82df9
RS
1334 if (EQ (first, make_number (0)))
1335 Vfeatures = second;
db9f0278
JB
1336 else
1337 Ffset (first, second);
e509f168 1338 queue = XCDR (queue);
db9f0278 1339 }
db9f0278
JB
1340}
1341
ca20916b
RS
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
7abaf5cc
SM
1346DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1347 doc: /* Load FUNDEF which should be an autoload.
1348If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1349in which case the function returns the new autoloaded function value.
1350If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1351it is defines a macro. */)
1352 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
db9f0278 1353{
3200038e 1354 dynwind_begin ();
ca20916b 1355 struct gcpro gcpro1, gcpro2, gcpro3;
db9f0278 1356
3200038e
BT
1357 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef))) {
1358 dynwind_end ();
7abaf5cc 1359 return fundef;
3200038e 1360 }
7abaf5cc
SM
1361
1362 if (EQ (macro_only, Qmacro))
1363 {
1364 Lisp_Object kind = Fnth (make_number (4), fundef);
3200038e
BT
1365 if (! (EQ (kind, Qt) || EQ (kind, Qmacro))) {
1366 dynwind_end ();
1367 return fundef;
1368 }
7abaf5cc
SM
1369 }
1370
aea6173f
RS
1371 /* This is to make sure that loadup.el gives a clear picture
1372 of what files are preloaded and when. */
4c103b64 1373 /*if (! NILP (Vpurify_flag))
ab4db096 1374 error ("Attempt to autoload %s while preparing to dump",
4c103b64 1375 SDATA (SYMBOL_NAME (funname)));*/
ab4db096 1376
b7826503 1377 CHECK_SYMBOL (funname);
7abaf5cc 1378 GCPRO3 (funname, fundef, macro_only);
db9f0278 1379
f87740dc 1380 /* Preserve the match data. */
89f2614d 1381 record_unwind_save_match_data ();
177c0ea7 1382
a04ee161
RS
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".
d3da34e0 1389
a04ee161 1390 The value saved here is to be restored into Vautoload_queue. */
db9f0278
JB
1391 record_unwind_protect (un_autoload, Vautoload_queue);
1392 Vautoload_queue = Qt;
7abaf5cc
SM
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);
2a49b6e5 1396
db9f0278
JB
1397 /* Once loading finishes, don't undo it. */
1398 Vautoload_queue = Qt;
3200038e 1399 dynwind_end ();
db9f0278 1400
ca20916b 1401 UNGCPRO;
7abaf5cc
SM
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 }
db9f0278 1415}
4c576a83 1416
db9f0278 1417\f
a7ca3326 1418DEFUN ("eval", Feval, Seval, 1, 2, 0,
a0ee6f27 1419 doc: /* Evaluate FORM and return its value.
8c27f5ff
SM
1420If LEXICAL is t, evaluate using lexical scoping.
1421LEXICAL can also be an actual lexical environment, in the form of an
1422alist mapping symbols to their value. */)
a0ee6f27 1423 (Lisp_Object form, Lisp_Object lexical)
defb1411 1424{
3200038e 1425 dynwind_begin ();
a0ee6f27 1426 specbind (Qinternal_interpreter_environment,
6c6f1994 1427 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
3200038e
BT
1428 Lisp_Object tem0 = eval_sub (form);
1429 dynwind_end ();
1430 return tem0;
defb1411
SM
1431}
1432
5e301d76
PE
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
2f592f95
SM
1443static void
1444grow_specpdl (void)
1445{
5e301d76
PE
1446 specpdl_ptr++;
1447
1448 if (specpdl_ptr == specpdl + specpdl_size)
2f592f95 1449 {
5e301d76
PE
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;
2f592f95 1454 if (max_size <= specpdl_size)
5e301d76
PE
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;
2f592f95 1466 }
2f592f95
SM
1467}
1468
316bec86
BT
1469static void
1470set_lisp_eval_depth (void *data)
1471{
1472 EMACS_INT n = (EMACS_INT) data;
1473 lisp_eval_depth = n;
2f592f95
SM
1474}
1475
defb1411
SM
1476/* Eval a sub-expression of the current expression (i.e. in the same
1477 lexical scope). */
1f183cec
BT
1478static Lisp_Object
1479eval_sub_1 (Lisp_Object form)
db9f0278 1480{
db9f0278 1481 QUIT;
7a7dfc01 1482 return scm_call_1 (eval_fn, form);
db9f0278 1483}
1f183cec
BT
1484
1485Lisp_Object
1486eval_sub (Lisp_Object form)
1487{
1488 return scm_c_value_ref (eval_sub_1 (form), 0);
1489}
1490\f
1491static Lisp_Object
1492values_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
1500DEFUN ("multiple-value-call", Fmultiple_value_call, Smultiple_value_call,
1501 2, UNEVALLED, 0,
1502 doc: /* Call with multiple values.
1503usage: (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
1514DEFUN ("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}
db9f0278 1520\f
1bf3ed7c
RT
1521Lisp_Object
1522Fapply (ptrdiff_t nargs, Lisp_Object *args)
db9f0278 1523{
d311d28c
PE
1524 ptrdiff_t i;
1525 EMACS_INT numargs;
db9f0278
JB
1526 register Lisp_Object spread_arg;
1527 register Lisp_Object *funcall_args;
3a7a9129 1528 Lisp_Object fun, retval;
96d44c64 1529 struct gcpro gcpro1;
3a7a9129 1530 USE_SAFE_ALLOCA;
db9f0278
JB
1531
1532 fun = args [0];
1533 funcall_args = 0;
1534 spread_arg = args [nargs - 1];
b7826503 1535 CHECK_LIST (spread_arg);
177c0ea7 1536
db9f0278
JB
1537 numargs = XINT (Flength (spread_arg));
1538
1539 if (numargs == 0)
1540 return Ffuncall (nargs - 1, args);
1541 else if (numargs == 1)
1542 {
03699b14 1543 args [nargs - 1] = XCAR (spread_arg);
db9f0278
JB
1544 return Ffuncall (nargs, args);
1545 }
1546
a6e3fa71 1547 numargs += nargs - 2;
db9f0278 1548
8788120f 1549 /* Optimize for no indirection. */
eadf1faa 1550 if (SYMBOLP (fun) && !NILP (fun)
d8ccbf1a 1551 && (fun = SYMBOL_FUNCTION (fun), SYMBOLP (fun)))
8788120f 1552 fun = indirect_function (fun);
eadf1faa 1553 if (NILP (fun))
db9f0278 1554 {
f6d62986 1555 /* Let funcall get the error. */
ffd56f97 1556 fun = args[0];
db9f0278
JB
1557 }
1558
db9f0278
JB
1559 /* We add 1 to numargs because funcall_args includes the
1560 function itself as well as its arguments. */
1561 if (!funcall_args)
a6e3fa71 1562 {
b72e0717 1563 SAFE_ALLOCA_LISP (funcall_args, 1 + numargs);
96d44c64
SM
1564 GCPRO1 (*funcall_args);
1565 gcpro1.nvars = 1 + numargs;
a6e3fa71
JB
1566 }
1567
663e2b3f 1568 memcpy (funcall_args, args, nargs * word_size);
db9f0278
JB
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;
265a9e55 1572 while (!NILP (spread_arg))
db9f0278 1573 {
03699b14
KR
1574 funcall_args [i++] = XCAR (spread_arg);
1575 spread_arg = XCDR (spread_arg);
db9f0278 1576 }
a6e3fa71 1577
96d44c64 1578 /* By convention, the caller needs to gcpro Ffuncall's args. */
3a7a9129
CY
1579 retval = Ffuncall (gcpro1.nvars, funcall_args);
1580 UNGCPRO;
1581 SAFE_FREE ();
1582
1583 return retval;
db9f0278
JB
1584}
1585\f
ff936e53
SM
1586/* Run hook variables in various ways. */
1587
f6d62986 1588static Lisp_Object
f66c7cf8 1589funcall_nil (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
1590{
1591 Ffuncall (nargs, args);
1592 return Qnil;
1593}
ff936e53 1594
a7ca3326 1595DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 0, MANY, 0,
9f685258 1596 doc: /* Run each hook in HOOKS.
9dbc9081
PJ
1597Each argument should be a symbol, a hook variable.
1598These symbols are processed in the order specified.
1599If a hook symbol has a non-nil value, that value may be a function
1600or a list of functions to be called to run the hook.
1601If the value is a function, it is called with no arguments.
1602If it is a list, the elements are called, in order, with no arguments.
1603
9f685258
LK
1604Major modes should not use this function directly to run their mode
1605hook; they should use `run-mode-hooks' instead.
1606
72e85d5d
RS
1607Do not use `make-local-variable' to make a hook variable buffer-local.
1608Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 1609usage: (run-hooks &rest HOOKS) */)
f66c7cf8 1610 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53
SM
1611{
1612 Lisp_Object hook[1];
f66c7cf8 1613 ptrdiff_t i;
ff936e53
SM
1614
1615 for (i = 0; i < nargs; i++)
1616 {
1617 hook[0] = args[i];
f6d62986 1618 run_hook_with_args (1, hook, funcall_nil);
ff936e53
SM
1619 }
1620
1621 return Qnil;
1622}
177c0ea7 1623
a7ca3326 1624DEFUN ("run-hook-with-args", Frun_hook_with_args,
9dbc9081
PJ
1625 Srun_hook_with_args, 1, MANY, 0,
1626 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
1627HOOK should be a symbol, a hook variable. The value of HOOK
1628may be nil, a function, or a list of functions. Call each
1629function in order with arguments ARGS. The final return value
1630is unspecified.
9dbc9081 1631
72e85d5d
RS
1632Do not use `make-local-variable' to make a hook variable buffer-local.
1633Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 1634usage: (run-hook-with-args HOOK &rest ARGS) */)
f66c7cf8 1635 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53 1636{
f6d62986 1637 return run_hook_with_args (nargs, args, funcall_nil);
ff936e53
SM
1638}
1639
d393cefb
GM
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.) */
a0d76c27 1643DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
9dbc9081
PJ
1644 Srun_hook_with_args_until_success, 1, MANY, 0,
1645 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
1646HOOK should be a symbol, a hook variable. The value of HOOK
1647may be nil, a function, or a list of functions. Call each
1648function in order with arguments ARGS, stopping at the first
1649one that returns non-nil, and return that value. Otherwise (if
1650all functions return nil, or if there are no functions to call),
1651return nil.
9dbc9081 1652
72e85d5d
RS
1653Do not use `make-local-variable' to make a hook variable buffer-local.
1654Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 1655usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
f66c7cf8 1656 (ptrdiff_t nargs, Lisp_Object *args)
b0b667cb 1657{
f6d62986
SM
1658 return run_hook_with_args (nargs, args, Ffuncall);
1659}
1660
1661static Lisp_Object
f66c7cf8 1662funcall_not (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
1663{
1664 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
ff936e53
SM
1665}
1666
a7ca3326 1667DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
9dbc9081
PJ
1668 Srun_hook_with_args_until_failure, 1, MANY, 0,
1669 doc: /* Run HOOK with the specified arguments ARGS.
d393cefb
GM
1670HOOK should be a symbol, a hook variable. The value of HOOK
1671may be nil, a function, or a list of functions. Call each
1672function in order with arguments ARGS, stopping at the first
1673one that returns nil, and return nil. Otherwise (if all functions
1674return 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).
9dbc9081 1676
72e85d5d
RS
1677Do not use `make-local-variable' to make a hook variable buffer-local.
1678Instead, use `add-hook' and specify t for the LOCAL argument.
9dbc9081 1679usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
f66c7cf8 1680 (ptrdiff_t nargs, Lisp_Object *args)
ff936e53 1681{
f6d62986 1682 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
ff936e53
SM
1683}
1684
f6d62986 1685static Lisp_Object
f66c7cf8 1686run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
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
1697DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
1698 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
1699I.e. instead of calling each function FUN directly with arguments ARGS,
1700it calls WRAP-FUNCTION with arguments FUN and ARGS.
1701As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
1702aborts and returns that value.
1703usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
f66c7cf8 1704 (ptrdiff_t nargs, Lisp_Object *args)
f6d62986
SM
1705{
1706 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
1707}
ff936e53 1708
c933ea05
RS
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).
f6d62986 1712 FUNCALL specifies how to call each function on the hook.
c933ea05
RS
1713 The caller (or its caller, etc) must gcpro all of ARGS,
1714 except that it isn't necessary to gcpro ARGS[0]. */
1715
f6d62986 1716Lisp_Object
f66c7cf8
PE
1717run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
1718 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
ff936e53 1719{
f6d62986 1720 Lisp_Object sym, val, ret = Qnil;
fada05d6 1721 struct gcpro gcpro1, gcpro2, gcpro3;
b0b667cb 1722
f029ca5f
RS
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))
caff32a7 1726 return Qnil;
f029ca5f 1727
b0b667cb 1728 sym = args[0];
aa681b51 1729 val = find_symbol_value (sym);
ff936e53 1730
b0b667cb 1731 if (EQ (val, Qunbound) || NILP (val))
ff936e53 1732 return ret;
dee4ba59 1733 else if (!CONSP (val) || FUNCTIONP (val))
b0b667cb
KH
1734 {
1735 args[0] = val;
f6d62986 1736 return funcall (nargs, args);
b0b667cb
KH
1737 }
1738 else
1739 {
1faed8ae
PE
1740 Lisp_Object global_vals = Qnil;
1741 GCPRO3 (sym, val, global_vals);
cb9d21f8 1742
ff936e53 1743 for (;
f6d62986 1744 CONSP (val) && NILP (ret);
03699b14 1745 val = XCDR (val))
b0b667cb 1746 {
03699b14 1747 if (EQ (XCAR (val), Qt))
b0b667cb
KH
1748 {
1749 /* t indicates this hook has a local binding;
1750 it means to run the global binding too. */
1faed8ae
PE
1751 global_vals = Fdefault_value (sym);
1752 if (NILP (global_vals)) continue;
b0b667cb 1753
1faed8ae 1754 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
b0b667cb 1755 {
1faed8ae 1756 args[0] = global_vals;
f6d62986 1757 ret = funcall (nargs, args);
8932b1c2
CY
1758 }
1759 else
1760 {
1761 for (;
f6d62986 1762 CONSP (global_vals) && NILP (ret);
1faed8ae 1763 global_vals = XCDR (global_vals))
8932b1c2 1764 {
1faed8ae 1765 args[0] = XCAR (global_vals);
8932b1c2
CY
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))
f6d62986 1769 ret = funcall (nargs, args);
8932b1c2 1770 }
b0b667cb
KH
1771 }
1772 }
1773 else
1774 {
03699b14 1775 args[0] = XCAR (val);
f6d62986 1776 ret = funcall (nargs, args);
b0b667cb
KH
1777 }
1778 }
cb9d21f8
RS
1779
1780 UNGCPRO;
ff936e53 1781 return ret;
b0b667cb
KH
1782 }
1783}
c933ea05 1784
7d48558f
RS
1785/* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
1786
1787void
d3da34e0 1788run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
7d48558f
RS
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}
ff936e53 1797\f
f6d62986 1798/* Apply fn to arg. */
db9f0278 1799Lisp_Object
d3da34e0 1800apply1 (Lisp_Object fn, Lisp_Object arg)
db9f0278 1801{
a6e3fa71
JB
1802 struct gcpro gcpro1;
1803
1804 GCPRO1 (fn);
265a9e55 1805 if (NILP (arg))
3200038e 1806 return Ffuncall (1, &fn);
a6e3fa71 1807 gcpro1.nvars = 2;
db9f0278
JB
1808 {
1809 Lisp_Object args[2];
1810 args[0] = fn;
1811 args[1] = arg;
a6e3fa71 1812 gcpro1.var = args;
3200038e 1813 return Fapply (2, args);
db9f0278 1814 }
db9f0278
JB
1815}
1816
f6d62986 1817/* Call function fn on no arguments. */
db9f0278 1818Lisp_Object
d3da34e0 1819call0 (Lisp_Object fn)
db9f0278 1820{
a6e3fa71
JB
1821 struct gcpro gcpro1;
1822
1823 GCPRO1 (fn);
3200038e 1824 return Ffuncall (1, &fn);
db9f0278
JB
1825}
1826
f6d62986 1827/* Call function fn with 1 argument arg1. */
db9f0278
JB
1828/* ARGSUSED */
1829Lisp_Object
d3da34e0 1830call1 (Lisp_Object fn, Lisp_Object arg1)
db9f0278 1831{
a6e3fa71 1832 struct gcpro gcpro1;
177c0ea7 1833 Lisp_Object args[2];
a6e3fa71 1834
db9f0278 1835 args[0] = fn;
15285f9f 1836 args[1] = arg1;
a6e3fa71
JB
1837 GCPRO1 (args[0]);
1838 gcpro1.nvars = 2;
3200038e 1839 return Ffuncall (2, args);
db9f0278
JB
1840}
1841
f6d62986 1842/* Call function fn with 2 arguments arg1, arg2. */
db9f0278
JB
1843/* ARGSUSED */
1844Lisp_Object
d3da34e0 1845call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
db9f0278 1846{
a6e3fa71 1847 struct gcpro gcpro1;
db9f0278
JB
1848 Lisp_Object args[3];
1849 args[0] = fn;
15285f9f
RS
1850 args[1] = arg1;
1851 args[2] = arg2;
a6e3fa71
JB
1852 GCPRO1 (args[0]);
1853 gcpro1.nvars = 3;
3200038e 1854 return Ffuncall (3, args);
db9f0278
JB
1855}
1856
f6d62986 1857/* Call function fn with 3 arguments arg1, arg2, arg3. */
db9f0278
JB
1858/* ARGSUSED */
1859Lisp_Object
d3da34e0 1860call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
db9f0278 1861{
a6e3fa71 1862 struct gcpro gcpro1;
db9f0278
JB
1863 Lisp_Object args[4];
1864 args[0] = fn;
15285f9f
RS
1865 args[1] = arg1;
1866 args[2] = arg2;
1867 args[3] = arg3;
a6e3fa71
JB
1868 GCPRO1 (args[0]);
1869 gcpro1.nvars = 4;
3200038e 1870 return Ffuncall (4, args);
db9f0278
JB
1871}
1872
f6d62986 1873/* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
a5a44b91
JB
1874/* ARGSUSED */
1875Lisp_Object
d3da34e0
JB
1876call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1877 Lisp_Object arg4)
a5a44b91
JB
1878{
1879 struct gcpro gcpro1;
a5a44b91
JB
1880 Lisp_Object args[5];
1881 args[0] = fn;
15285f9f
RS
1882 args[1] = arg1;
1883 args[2] = arg2;
1884 args[3] = arg3;
1885 args[4] = arg4;
a5a44b91
JB
1886 GCPRO1 (args[0]);
1887 gcpro1.nvars = 5;
3200038e 1888 return Ffuncall (5, args);
a5a44b91
JB
1889}
1890
f6d62986 1891/* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
15285f9f
RS
1892/* ARGSUSED */
1893Lisp_Object
d3da34e0
JB
1894call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1895 Lisp_Object arg4, Lisp_Object arg5)
15285f9f
RS
1896{
1897 struct gcpro gcpro1;
15285f9f
RS
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;
3200038e 1907 return Ffuncall (6, args);
15285f9f
RS
1908}
1909
f6d62986 1910/* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
15285f9f
RS
1911/* ARGSUSED */
1912Lisp_Object
d3da34e0
JB
1913call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
1914 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
15285f9f
RS
1915{
1916 struct gcpro gcpro1;
15285f9f
RS
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;
3200038e 1927 return Ffuncall (7, args);
15285f9f
RS
1928}
1929
f6d62986 1930/* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
574c05e2
KK
1931/* ARGSUSED */
1932Lisp_Object
d3da34e0
JB
1933call7 (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)
574c05e2
KK
1935{
1936 struct gcpro gcpro1;
574c05e2
KK
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;
3200038e 1948 return Ffuncall (8, args);
574c05e2
KK
1949}
1950
6c2ef893
RS
1951/* The caller should GCPRO all the elements of ARGS. */
1952
a7ca3326 1953DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
7200d79c 1954 doc: /* Non-nil if OBJECT is a function. */)
c566235d 1955 (Lisp_Object object)
b9598260 1956{
e1f29348 1957 if (FUNCTIONP (object))
b9598260 1958 return Qt;
e1f29348 1959 return Qnil;
b9598260
SM
1960}
1961
7a7dfc01 1962static Lisp_Object
1bf3ed7c 1963Ffuncall1 (ptrdiff_t nargs, Lisp_Object *args)
db9f0278 1964{
1bf3ed7c 1965 return scm_call_n (funcall_fn, args, nargs);
db9f0278 1966}
1f183cec
BT
1967
1968Lisp_Object
1969Ffuncall (ptrdiff_t nargs, Lisp_Object *args)
1970{
1971 return scm_c_value_ref (Ffuncall1 (nargs, args), 0);
1972}
db9f0278 1973\f
2f7c71a1 1974static Lisp_Object
defb1411 1975apply_lambda (Lisp_Object fun, Lisp_Object args)
db9f0278
JB
1976{
1977 Lisp_Object args_left;
d311d28c
PE
1978 ptrdiff_t i;
1979 EMACS_INT numargs;
db9f0278
JB
1980 register Lisp_Object *arg_vector;
1981 struct gcpro gcpro1, gcpro2, gcpro3;
db9f0278 1982 register Lisp_Object tem;
3a7a9129 1983 USE_SAFE_ALLOCA;
db9f0278 1984
f66c7cf8 1985 numargs = XFASTINT (Flength (args));
c5101a77 1986 SAFE_ALLOCA_LISP (arg_vector, numargs);
db9f0278
JB
1987 args_left = args;
1988
1989 GCPRO3 (*arg_vector, args_left, fun);
1990 gcpro1.nvars = 0;
1991
c5101a77 1992 for (i = 0; i < numargs; )
db9f0278
JB
1993 {
1994 tem = Fcar (args_left), args_left = Fcdr (args_left);
db9f0278
JB
1995 arg_vector[i++] = tem;
1996 gcpro1.nvars = i;
1997 }
1998
1999 UNGCPRO;
2000
c5101a77 2001 tem = funcall_lambda (fun, numargs, arg_vector);
db9f0278 2002
3a7a9129 2003 SAFE_FREE ();
db9f0278
JB
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
2901f1d1 2011static Lisp_Object
f66c7cf8 2012funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
c5101a77 2013 register Lisp_Object *arg_vector)
db9f0278 2014{
defb1411 2015 Lisp_Object val, syms_left, next, lexenv;
3200038e 2016 dynwind_begin ();
f66c7cf8 2017 ptrdiff_t i;
1882aa38 2018 bool optional, rest;
db9f0278 2019
90165123 2020 if (CONSP (fun))
9ab90667 2021 {
defb1411
SM
2022 if (EQ (XCAR (fun), Qclosure))
2023 {
2024 fun = XCDR (fun); /* Drop `closure'. */
2025 lexenv = XCAR (fun);
23aba0ea 2026 CHECK_LIST_CONS (fun, fun);
defb1411
SM
2027 }
2028 else
2029 lexenv = Qnil;
9ab90667
GM
2030 syms_left = XCDR (fun);
2031 if (CONSP (syms_left))
2032 syms_left = XCAR (syms_left);
2033 else
734d55a2 2034 xsignal1 (Qinvalid_function, fun);
9ab90667 2035 }
9ab90667 2036 else
1088b922 2037 emacs_abort ();
db9f0278 2038
9ab90667
GM
2039 i = optional = rest = 0;
2040 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
db9f0278
JB
2041 {
2042 QUIT;
177c0ea7 2043
9ab90667 2044 next = XCAR (syms_left);
8788120f 2045 if (!SYMBOLP (next))
734d55a2 2046 xsignal1 (Qinvalid_function, fun);
177c0ea7 2047
db9f0278
JB
2048 if (EQ (next, Qand_rest))
2049 rest = 1;
2050 else if (EQ (next, Qand_optional))
2051 optional = 1;
db9f0278 2052 else
db9f0278 2053 {
e610eaca 2054 Lisp_Object arg;
defb1411
SM
2055 if (rest)
2056 {
e610eaca 2057 arg = Flist (nargs - i, &arg_vector[i]);
defb1411
SM
2058 i = nargs;
2059 }
2060 else if (i < nargs)
e610eaca 2061 arg = arg_vector[i++];
b9598260
SM
2062 else if (!optional)
2063 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
2064 else
e610eaca 2065 arg = Qnil;
7200d79c 2066
b9598260 2067 /* Bind the argument. */
876c194c 2068 if (!NILP (lexenv) && SYMBOLP (next))
b9598260 2069 /* Lexically bind NEXT by adding it to the lexenv alist. */
e610eaca 2070 lexenv = Fcons (Fcons (next, arg), lexenv);
b9598260
SM
2071 else
2072 /* Dynamically bind NEXT. */
e610eaca 2073 specbind (next, arg);
db9f0278 2074 }
db9f0278
JB
2075 }
2076
9ab90667 2077 if (!NILP (syms_left))
734d55a2 2078 xsignal1 (Qinvalid_function, fun);
9ab90667 2079 else if (i < nargs)
734d55a2 2080 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
db9f0278 2081
b9598260
SM
2082 if (!EQ (lexenv, Vinternal_interpreter_environment))
2083 /* Instantiate a new lexical environment. */
2084 specbind (Qinternal_interpreter_environment, lexenv);
2085
98c87aa3 2086 val = Fprogn (XCDR (XCDR (fun)));
177c0ea7 2087
3200038e
BT
2088 dynwind_end ();
2089 return val;
db9f0278 2090}
661c7d6e
KH
2091
2092DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
9dbc9081
PJ
2093 1, 1, 0,
2094 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
5842a27b 2095 (Lisp_Object object)
661c7d6e
KH
2096{
2097 Lisp_Object tem;
2098
845975f5 2099 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
661c7d6e 2100 {
845975f5 2101 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
5bbdb090 2102 if (!CONSP (tem))
845975f5
SM
2103 {
2104 tem = AREF (object, COMPILED_BYTECODE);
2105 if (CONSP (tem) && STRINGP (XCAR (tem)))
d5db4077 2106 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
845975f5
SM
2107 else
2108 error ("Invalid byte code");
2109 }
3ae565b3
SM
2110 ASET (object, COMPILED_BYTECODE, XCAR (tem));
2111 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
661c7d6e
KH
2112 }
2113 return object;
2114}
db9f0278 2115\f
2f592f95
SM
2116/* Return true if SYMBOL currently has a let-binding
2117 which was made in the buffer that is now current. */
2118
2119bool
db633ecc 2120let_shadows_buffer_binding_p (sym_t symbol)
db9f0278 2121{
9349e5f7 2122 union specbinding *p;
2f592f95
SM
2123 Lisp_Object buf = Fcurrent_buffer ();
2124
2125 for (p = specpdl_ptr; p > specpdl; )
2126 if ((--p)->kind > SPECPDL_LET)
2127 {
db633ecc
BT
2128 sym_t let_bound_symbol = XSYMBOL (specpdl_symbol (p));
2129 eassert (SYMBOL_REDIRECT (let_bound_symbol) != SYMBOL_VARALIAS);
2f592f95
SM
2130 if (symbol == let_bound_symbol
2131 && EQ (specpdl_where (p), buf))
2132 return 1;
2133 }
2134
2135 return 0;
2136}
2137
2138bool
2139let_shadows_global_binding_p (Lisp_Object symbol)
2140{
9349e5f7 2141 union specbinding *p;
2f592f95
SM
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;
db9f0278
JB
2148}
2149
3ec7babc 2150/* `specpdl_ptr' describes which variable is
4e2db1fe 2151 let-bound, so it can be properly undone when we unbind_to.
3ec7babc
SM
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
4e2db1fe
SM
2154 aliased (i.e. when let-binding V1 that's aliased to V2, we want
2155 to record V2 here).
3ec7babc
SM
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. */
4e2db1fe 2161
db9f0278 2162void
d3da34e0 2163specbind (Lisp_Object symbol, Lisp_Object value)
db9f0278 2164{
db633ecc 2165 sym_t sym;
ce5b453a 2166
b7826503 2167 CHECK_SYMBOL (symbol);
ce5b453a 2168 sym = XSYMBOL (symbol);
719177b3 2169
ce5b453a 2170 start:
db633ecc 2171 switch (SYMBOL_REDIRECT (sym))
719177b3 2172 {
ce5b453a
SM
2173 case SYMBOL_VARALIAS:
2174 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
2175 case SYMBOL_PLAINVAL:
bb8e180f
AS
2176 /* The most common case is that of a non-constant symbol with a
2177 trivial value. Make that as fast as we can. */
9349e5f7
PE
2178 specpdl_ptr->let.kind = SPECPDL_LET;
2179 specpdl_ptr->let.symbol = symbol;
2180 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
5e301d76 2181 grow_specpdl ();
db633ecc 2182 if (! SYMBOL_CONSTANT (sym))
bb8e180f
AS
2183 SET_SYMBOL_VAL (sym, value);
2184 else
2185 set_internal (symbol, value, Qnil, 1);
2186 break;
4e2db1fe
SM
2187 case SYMBOL_LOCALIZED:
2188 if (SYMBOL_BLV (sym)->frame_local)
2189 error ("Frame-local vars cannot be let-bound");
2190 case SYMBOL_FORWARDED:
ce5b453a
SM
2191 {
2192 Lisp_Object ovalue = find_symbol_value (symbol);
9349e5f7
PE
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 ();
ce5b453a 2197
db633ecc 2198 eassert (SYMBOL_REDIRECT (sym) != SYMBOL_LOCALIZED
2f592f95 2199 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
ce5b453a 2200
db633ecc 2201 if (SYMBOL_REDIRECT (sym) == SYMBOL_LOCALIZED)
2f592f95
SM
2202 {
2203 if (!blv_found (SYMBOL_BLV (sym)))
9349e5f7 2204 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
2f592f95
SM
2205 }
2206 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
ce5b453a 2207 {
ce5b453a
SM
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. */
2f592f95 2213 if (NILP (Flocal_variable_p (symbol, Qnil)))
ce5b453a 2214 {
9349e5f7 2215 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
5e301d76 2216 grow_specpdl ();
ce5b453a 2217 Fset_default (symbol, value);
316bec86 2218 goto done;
ce5b453a
SM
2219 }
2220 }
2221 else
9349e5f7 2222 specpdl_ptr->let.kind = SPECPDL_LET;
ce5b453a 2223
5e301d76 2224 grow_specpdl ();
94b612ad 2225 set_internal (symbol, value, Qnil, 1);
ce5b453a
SM
2226 break;
2227 }
1088b922 2228 default: emacs_abort ();
9ab90667 2229 }
316bec86
BT
2230
2231 done:
2232 scm_dynwind_unwind_handler (unbind_once, NULL, SCM_F_WIND_EXPLICITLY);
db9f0278
JB
2233}
2234
f4b1eb36
PE
2235/* Push unwind-protect entries of various types. */
2236
db9f0278 2237void
e1ad689c
BT
2238record_unwind_protect_1 (void (*function) (Lisp_Object), Lisp_Object arg,
2239 bool wind_explicitly)
db9f0278 2240{
316bec86 2241 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
db9f0278
JB
2242}
2243
27e498e6 2244void
e1ad689c 2245record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
27e498e6 2246{
e1ad689c 2247 record_unwind_protect_1 (function, arg, true);
27e498e6
PE
2248}
2249
2250void
e1ad689c
BT
2251record_unwind_protect_ptr_1 (void (*function) (void *), void *arg,
2252 bool wind_explicitly)
27e498e6 2253{
316bec86
BT
2254 scm_dynwind_unwind_handler (function,
2255 arg,
2256 (wind_explicitly
2257 ? SCM_F_WIND_EXPLICITLY
2258 : 0));
27e498e6
PE
2259}
2260
2261void
e1ad689c 2262record_unwind_protect_ptr (void (*function) (void *), void *arg)
27e498e6 2263{
e1ad689c 2264 record_unwind_protect_ptr_1 (function, arg, true);
27e498e6
PE
2265}
2266
f4b1eb36 2267void
e1ad689c
BT
2268record_unwind_protect_int_1 (void (*function) (int), int arg,
2269 bool wind_explicitly)
f4b1eb36 2270{
316bec86 2271 record_unwind_protect_ptr_1 (function, arg, wind_explicitly);
f4b1eb36
PE
2272}
2273
a0931322 2274void
e1ad689c 2275record_unwind_protect_int (void (*function) (int), int arg)
a0931322 2276{
e1ad689c 2277 record_unwind_protect_int_1 (function, arg, true);
a0931322
PE
2278}
2279
316bec86
BT
2280static void
2281call_void (void *data)
2282{
2283 ((void (*) (void)) data) ();
2284}
2285
94fcd171 2286void
e1ad689c
BT
2287record_unwind_protect_void_1 (void (*function) (void),
2288 bool wind_explicitly)
94fcd171 2289{
316bec86 2290 record_unwind_protect_ptr_1 (call_void, function, wind_explicitly);
94fcd171
PE
2291}
2292
a0931322 2293void
e1ad689c 2294record_unwind_protect_void (void (*function) (void))
a0931322 2295{
e1ad689c 2296 record_unwind_protect_void_1 (function, true);
a0931322
PE
2297}
2298
316bec86
BT
2299static void
2300unbind_once (void *ignore)
c26b5ed1
BT
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 {
c26b5ed1
BT
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. */
db633ecc
BT
2315 sym_t sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
2316 if (SYMBOL_REDIRECT (sym) == SYMBOL_PLAINVAL)
c26b5ed1
BT
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
e1ad689c
BT
2347void
2348dynwind_begin (void)
2349{
316bec86 2350 scm_dynwind_begin (0);
e1ad689c 2351}
f4b1eb36 2352
e1ad689c
BT
2353void
2354dynwind_end (void)
2355{
316bec86 2356 scm_dynwind_end ();
e1ad689c
BT
2357}
2358
4a330052 2359DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
b9598260
SM
2360 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
2361A special variable is one that will be bound dynamically, even in a
2362context where binding is lexical by default. */)
c566235d 2363 (Lisp_Object symbol)
b9598260
SM
2364{
2365 CHECK_SYMBOL (symbol);
db633ecc 2366 return SYMBOL_DECLARED_SPECIAL (XSYMBOL (symbol)) ? Qt : Qnil;
b9598260 2367}
316bec86
BT
2368\f
2369_Noreturn SCM
2370abort_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");
4ce0541e 2375
316bec86
BT
2376 scm_apply_1 (scm_variable_ref (var), tag, arglst);
2377 emacs_abort ();
2378}
2379
2380SCM
2381call_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
2390SCM
2391make_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
dfcf069d 2400void
d3da34e0 2401syms_of_eval (void)
db9f0278 2402{
172a66a1
BT
2403#include "eval.x"
2404
29208e82 2405 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
fb7ada5f 2406 doc: /* Limit on number of Lisp variable bindings and `unwind-protect's.
9f5903bb 2407If Lisp code tries to increase the total number past this amount,
2520dc0c
RS
2408an error is signaled.
2409You can safely use a value considerably larger than the default value,
2410if that proves inconveniently small. However, if you increase it too far,
575593db
DA
2411Emacs could run out of memory trying to make the stack bigger.
2412Note that this limit may be silently increased by the debugger
2413if `debug-on-error' or `debug-on-quit' is set. */);
db9f0278 2414
29208e82 2415 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
fb7ada5f 2416 doc: /* Limit on depth in `eval', `apply' and `funcall' before error.
2520dc0c
RS
2417
2418This limit serves to catch infinite recursions for you before they cause
9dbc9081
PJ
2419actual stack overflow in C, which would be fatal for Emacs.
2420You can safely make it considerably larger than its default value,
2520dc0c
RS
2421if that proves inconveniently small. However, if you increase it too far,
2422Emacs could overflow the real C stack, and crash. */);
db9f0278 2423
29208e82 2424 DEFVAR_LISP ("quit-flag", Vquit_flag,
9dbc9081 2425 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
42ed718e
RS
2426If the value is t, that means do an ordinary quit.
2427If the value equals `throw-on-input', that means quit by throwing
2428to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
2429Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
2430but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
db9f0278
JB
2431 Vquit_flag = Qnil;
2432
29208e82 2433 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
9dbc9081
PJ
2434 doc: /* Non-nil inhibits C-g quitting from happening immediately.
2435Note that `quit-flag' will still be set by typing C-g,
2436so a quit will be signaled as soon as `inhibit-quit' is nil.
2437To prevent this happening, set `quit-flag' to nil
2438before making `inhibit-quit' nil. */);
db9f0278
JB
2439 Vinhibit_quit = Qnil;
2440
cd3520a4
JB
2441 DEFSYM (Qinhibit_quit, "inhibit-quit");
2442 DEFSYM (Qautoload, "autoload");
45b82ad0 2443 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
cd3520a4
JB
2444 DEFSYM (Qmacro, "macro");
2445 DEFSYM (Qdeclare, "declare");
177c0ea7 2446
db9f0278
JB
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. */
cd3520a4 2449 DEFSYM (Qexit, "exit");
b9598260 2450
cd3520a4
JB
2451 DEFSYM (Qinteractive, "interactive");
2452 DEFSYM (Qcommandp, "commandp");
cd3520a4
JB
2453 DEFSYM (Qand_rest, "&rest");
2454 DEFSYM (Qand_optional, "&optional");
2455 DEFSYM (Qclosure, "closure");
2456 DEFSYM (Qdebug, "debug");
f01cbfdd 2457
45b82ad0
SM
2458 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
2459 doc: /* Non-nil means never enter the debugger.
2460Normally set while the debugger is already active, to avoid recursive
2461invocations. */);
2462 Vinhibit_debugger = Qnil;
2463
29208e82 2464 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
fb7ada5f 2465 doc: /* Non-nil means enter debugger if an error is signaled.
9dbc9081
PJ
2466Does not apply to errors handled by `condition-case' or those
2467matched by `debug-ignored-errors'.
2468If the value is a list, an error only means to enter the debugger
2469if one of its condition symbols appears in the list.
2470When you evaluate an expression interactively, this variable
2471is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
fbbdcf2f 2472The command `toggle-debug-on-error' toggles this.
45b82ad0 2473See also the variable `debug-on-quit' and `inhibit-debugger'. */);
128c0f66 2474 Vdebug_on_error = Qnil;
db9f0278 2475
29208e82 2476 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
fb7ada5f 2477 doc: /* List of errors for which the debugger should not be called.
9dbc9081
PJ
2478Each element may be a condition-name or a regexp that matches error messages.
2479If any element applies to a given error, that error skips the debugger
2480and just returns to top level.
2481This overrides the variable `debug-on-error'.
2482It does not apply to errors handled by `condition-case'. */);
fc950e09
KH
2483 Vdebug_ignored_errors = Qnil;
2484
29208e82 2485 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
fb7ada5f 2486 doc: /* Non-nil means enter debugger if quit is signaled (C-g, for example).
82fc29a1 2487Does not apply if quit is handled by a `condition-case'. */);
db9f0278
JB
2488 debug_on_quit = 0;
2489
29208e82 2490 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
9dbc9081 2491 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
db9f0278 2492
29208e82 2493 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
9dbc9081
PJ
2494 doc: /* Non-nil means debugger may continue execution.
2495This is nil when the debugger is called under circumstances where it
2496might not be safe to continue. */);
dac204bc 2497 debugger_may_continue = 1;
556d7314 2498
29208e82 2499 DEFVAR_LISP ("debugger", Vdebugger,
9dbc9081
PJ
2500 doc: /* Function to call to invoke debugger.
2501If due to frame exit, args are `exit' and the value being returned;
2502 this function's value will be returned instead of that.
2503If due to error, args are `error' and a list of the args to `signal'.
2504If due to `apply' or `funcall' entry, one arg, `lambda'.
2505If due to `eval' entry, one arg, t. */);
db9f0278
JB
2506 Vdebugger = Qnil;
2507
29208e82 2508 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
9dbc9081
PJ
2509 doc: /* If non-nil, this is a function for `signal' to call.
2510It receives the same arguments that `signal' was given.
2511The Edebug package uses this to regain control. */);
61ede770
RS
2512 Vsignal_hook_function = Qnil;
2513
29208e82 2514 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
fb7ada5f 2515 doc: /* Non-nil means call the debugger regardless of condition handlers.
9dbc9081
PJ
2516Note that `debug-on-error', `debug-on-quit' and friends
2517still determine whether to handle the particular condition. */);
57a6e758 2518 Vdebug_on_signal = Qnil;
61ede770 2519
b38b1ec0 2520 /* When lexical binding is being used,
61b108cc 2521 Vinternal_interpreter_environment is non-nil, and contains an alist
b38b1ec0
SM
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. */
61b108cc
SM
2528 DEFSYM (Qinternal_interpreter_environment,
2529 "internal-interpreter-environment");
b38b1ec0
SM
2530 DEFVAR_LISP ("internal-interpreter-environment",
2531 Vinternal_interpreter_environment,
b9598260
SM
2532 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
2533When lexical binding is not being used, this variable is nil.
2534A value of `(t)' indicates an empty environment, otherwise it is an
2535alist of active lexical bindings. */);
2536 Vinternal_interpreter_environment = Qnil;
c80e3b4a 2537 /* Don't export this variable to Elisp, so no one can mess with it
b38b1ec0 2538 (Just imagine if someone makes it buffer-local). */
1bf3ed7c 2539 //Funintern (Qinternal_interpreter_environment, Qnil);
b9598260 2540
cd3520a4 2541 DEFSYM (Vrun_hooks, "run-hooks");
db9f0278
JB
2542
2543 staticpro (&Vautoload_queue);
2544 Vautoload_queue = Qnil;
a2ff3819
GM
2545 staticpro (&Vsignaling_function);
2546 Vsignaling_function = Qnil;
db9f0278 2547
d1f55f16 2548 inhibit_lisp_code = Qnil;
db9f0278 2549}