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