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