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