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