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