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