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