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