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