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