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