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