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