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