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