(easy-menu-define): Docstring fix.
[bpt/emacs.git] / src / eval.c
CommitLineData
db9f0278 1/* Evaluator for GNU Emacs Lisp interpreter.
1182a7cb
DL
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 99, 2000
3 Free Software Foundation, Inc.
db9f0278
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
96277b2d 9the Free Software Foundation; either version 2, or (at your option)
db9f0278
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
db9f0278
JB
21
22
18160b98 23#include <config.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"
db9f0278
JB
29#include <setjmp.h>
30
31/* This definition is duplicated in alloc.c and keyboard.c */
32/* Putting it in lisp.h makes cc bomb out! */
33
34struct backtrace
35 {
36 struct backtrace *next;
37 Lisp_Object *function;
38 Lisp_Object *args; /* Points to vector of args. */
daa37602
JB
39 int nargs; /* Length of vector.
40 If nargs is UNEVALLED, args points to slot holding
41 list of unevalled args */
db9f0278
JB
42 char evalargs;
43 /* Nonzero means call value of debugger when done with this operation. */
44 char debug_on_exit;
45 };
46
47struct backtrace *backtrace_list;
48
82da7701
JB
49/* This structure helps implement the `catch' and `throw' control
50 structure. A struct catchtag contains all the information needed
51 to restore the state of the interpreter after a non-local jump.
52
53 Handlers for error conditions (represented by `struct handler'
54 structures) just point to a catch tag to do the cleanup required
55 for their jumps.
56
57 catchtag structures are chained together in the C calling stack;
58 the `next' member points to the next outer catchtag.
59
60 A call like (throw TAG VAL) searches for a catchtag whose `tag'
61 member is TAG, and then unbinds to it. The `val' member is used to
62 hold VAL while the stack is unwound; `val' is returned as the value
63 of the catch form.
64
65 All the other members are concerned with restoring the interpreter
66 state. */
db9f0278
JB
67struct catchtag
68 {
69 Lisp_Object tag;
70 Lisp_Object val;
71 struct catchtag *next;
72 struct gcpro *gcpro;
73 jmp_buf jmp;
74 struct backtrace *backlist;
75 struct handler *handlerlist;
76 int lisp_eval_depth;
77 int pdlcount;
78 int poll_suppress_count;
bcf28080 79 struct byte_stack *byte_stack;
db9f0278
JB
80 };
81
82struct catchtag *catchlist;
83
15934ffa
RS
84#ifdef DEBUG_GCPRO
85/* Count levels of GCPRO to detect failure to UNGCPRO. */
86int gcpro_level;
87#endif
88
db9f0278 89Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
ad236261 90Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
db9f0278
JB
91Lisp_Object Qmocklisp_arguments, Vmocklisp_arguments, Qmocklisp;
92Lisp_Object Qand_rest, Qand_optional;
93Lisp_Object Qdebug_on_error;
94
6e6e9f08
RS
95/* This holds either the symbol `run-hooks' or nil.
96 It is nil at an early stage of startup, and when Emacs
97 is shutting down. */
db9f0278
JB
98Lisp_Object Vrun_hooks;
99
100/* Non-nil means record all fset's and provide's, to be undone
101 if the file being autoloaded is not fully loaded.
102 They are recorded by being consed onto the front of Vautoload_queue:
103 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
104
105Lisp_Object Vautoload_queue;
106
107/* Current number of specbindings allocated in specpdl. */
108int specpdl_size;
109
110/* Pointer to beginning of specpdl. */
111struct specbinding *specpdl;
112
113/* Pointer to first unused element in specpdl. */
114struct specbinding *specpdl_ptr;
115
116/* Maximum size allowed for specpdl allocation */
117int max_specpdl_size;
118
119/* Depth in Lisp evaluations and function calls. */
120int lisp_eval_depth;
121
122/* Maximum allowed depth in Lisp evaluations and function calls. */
123int max_lisp_eval_depth;
124
125/* Nonzero means enter debugger before next function call */
126int debug_on_next_call;
127
556d7314
GM
128/* Non-zero means debuffer may continue. This is zero when the
129 debugger is called during redisplay, where it might not be safe to
130 continue the interrupted redisplay. */
131
132int debugger_may_continue;
133
128c0f66 134/* List of conditions (non-nil atom means all) which cause a backtrace
4de86b16 135 if an error is handled by the command loop's error handler. */
128c0f66 136Lisp_Object Vstack_trace_on_error;
db9f0278 137
128c0f66 138/* List of conditions (non-nil atom means all) which enter the debugger
4de86b16 139 if an error is handled by the command loop's error handler. */
128c0f66 140Lisp_Object Vdebug_on_error;
db9f0278 141
fc950e09
KH
142/* List of conditions and regexps specifying error messages which
143 do not enter the debugger even if Vdebug_on_errors says they should. */
144Lisp_Object Vdebug_ignored_errors;
145
61ede770 146/* Non-nil means call the debugger even if the error will be handled. */
57a6e758 147Lisp_Object Vdebug_on_signal;
61ede770
RS
148
149/* Hook for edebug to use. */
150Lisp_Object Vsignal_hook_function;
151
db9f0278 152/* Nonzero means enter debugger if a quit signal
128c0f66 153 is handled by the command loop's error handler. */
db9f0278
JB
154int debug_on_quit;
155
be857679 156/* The value of num_nonmacro_input_events as of the last time we
82da7701 157 started to enter the debugger. If we decide to enter the debugger
be857679 158 again when this is still equal to num_nonmacro_input_events, then we
82da7701
JB
159 know that the debugger itself has an error, and we should just
160 signal the error instead of entering an infinite loop of debugger
161 invocations. */
162int when_entered_debugger;
db9f0278
JB
163
164Lisp_Object Vdebugger;
165
166void specbind (), record_unwind_protect ();
167
420e60e6
RS
168Lisp_Object run_hook_with_args ();
169
db9f0278
JB
170Lisp_Object funcall_lambda ();
171extern Lisp_Object ml_apply (); /* Apply a mocklisp function to unevaluated argument list */
172
dfcf069d 173void
db9f0278
JB
174init_eval_once ()
175{
176 specpdl_size = 50;
716acfce 177 specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
270e8074 178 specpdl_ptr = specpdl;
db9f0278 179 max_specpdl_size = 600;
969f5145 180 max_lisp_eval_depth = 300;
34d470ba
RS
181
182 Vrun_hooks = Qnil;
db9f0278
JB
183}
184
dfcf069d 185void
db9f0278
JB
186init_eval ()
187{
188 specpdl_ptr = specpdl;
189 catchlist = 0;
190 handlerlist = 0;
191 backtrace_list = 0;
192 Vquit_flag = Qnil;
193 debug_on_next_call = 0;
194 lisp_eval_depth = 0;
87e21fbd 195#ifdef DEBUG_GCPRO
15934ffa 196 gcpro_level = 0;
87e21fbd 197#endif
be857679 198 /* This is less than the initial value of num_nonmacro_input_events. */
b5b911f9 199 when_entered_debugger = -1;
db9f0278
JB
200}
201
202Lisp_Object
203call_debugger (arg)
204 Lisp_Object arg;
205{
3648c842 206 int debug_while_redisplaying;
556d7314 207 int count = specpdl_ptr - specpdl;
3648c842
GM
208 Lisp_Object val;
209
db9f0278
JB
210 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
211 max_lisp_eval_depth = lisp_eval_depth + 20;
3648c842 212
db9f0278
JB
213 if (specpdl_size + 40 > max_specpdl_size)
214 max_specpdl_size = specpdl_size + 40;
3648c842 215
237c23b0
GM
216#ifdef HAVE_X_WINDOWS
217 if (display_busy_cursor_p)
218 cancel_busy_cursor ();
219#endif
220
db9f0278 221 debug_on_next_call = 0;
be857679 222 when_entered_debugger = num_nonmacro_input_events;
3648c842
GM
223
224 /* Resetting redisplaying_p to 0 makes sure that debug output is
225 displayed if the debugger is invoked during redisplay. */
226 debug_while_redisplaying = redisplaying_p;
227 redisplaying_p = 0;
556d7314
GM
228 specbind (intern ("debugger-may-continue"),
229 debug_while_redisplaying ? Qnil : Qt);
3648c842
GM
230
231 val = apply1 (Vdebugger, arg);
232
233 /* Interrupting redisplay and resuming it later is not safe under
234 all circumstances. So, when the debugger returns, abort the
235 interupted redisplay by going back to the top-level. */
236 if (debug_while_redisplaying)
237 Ftop_level ();
238
556d7314 239 return unbind_to (count, val);
db9f0278
JB
240}
241
dfcf069d 242void
db9f0278
JB
243do_debug_on_call (code)
244 Lisp_Object code;
245{
246 debug_on_next_call = 0;
247 backtrace_list->debug_on_exit = 1;
248 call_debugger (Fcons (code, Qnil));
249}
250\f
251/* NOTE!!! Every function that can call EVAL must protect its args
252 and temporaries from garbage collection while it needs them.
253 The definition of `For' shows what you have to do. */
254
255DEFUN ("or", For, Sor, 0, UNEVALLED, 0,
256 "Eval args until one of them yields non-nil, then return that value.\n\
257The remaining args are not evalled at all.\n\
258If all args return nil, return nil.")
259 (args)
260 Lisp_Object args;
261{
262 register Lisp_Object val;
263 Lisp_Object args_left;
264 struct gcpro gcpro1;
265
265a9e55 266 if (NILP(args))
db9f0278
JB
267 return Qnil;
268
269 args_left = args;
270 GCPRO1 (args_left);
271
272 do
273 {
274 val = Feval (Fcar (args_left));
265a9e55 275 if (!NILP (val))
db9f0278
JB
276 break;
277 args_left = Fcdr (args_left);
278 }
265a9e55 279 while (!NILP(args_left));
db9f0278
JB
280
281 UNGCPRO;
282 return val;
283}
284
285DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0,
286 "Eval args until one of them yields nil, then return nil.\n\
287The remaining args are not evalled at all.\n\
288If no arg yields nil, return the last arg's value.")
289 (args)
290 Lisp_Object args;
291{
292 register Lisp_Object val;
293 Lisp_Object args_left;
294 struct gcpro gcpro1;
295
265a9e55 296 if (NILP(args))
db9f0278
JB
297 return Qt;
298
299 args_left = args;
300 GCPRO1 (args_left);
301
302 do
303 {
304 val = Feval (Fcar (args_left));
265a9e55 305 if (NILP (val))
db9f0278
JB
306 break;
307 args_left = Fcdr (args_left);
308 }
265a9e55 309 while (!NILP(args_left));
db9f0278
JB
310
311 UNGCPRO;
312 return val;
313}
314
315DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0,
1182a7cb 316 "If COND yields non-nil, do THEN, else do ELSE...\n\
db9f0278
JB
317Returns the value of THEN or the value of the last of the ELSE's.\n\
318THEN must be one expression, but ELSE... can be zero or more expressions.\n\
319If COND yields nil, and there are no ELSE's, the value is nil.")
320 (args)
321 Lisp_Object args;
322{
323 register Lisp_Object cond;
324 struct gcpro gcpro1;
325
326 GCPRO1 (args);
327 cond = Feval (Fcar (args));
328 UNGCPRO;
329
265a9e55 330 if (!NILP (cond))
db9f0278
JB
331 return Feval (Fcar (Fcdr (args)));
332 return Fprogn (Fcdr (Fcdr (args)));
333}
334
335DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
1182a7cb 336 "Try each clause until one succeeds.\n\
db9f0278
JB
337Each clause looks like (CONDITION BODY...). CONDITION is evaluated\n\
338and, if the value is non-nil, this clause succeeds:\n\
339then the expressions in BODY are evaluated and the last one's\n\
340value is the value of the cond-form.\n\
341If no clause succeeds, cond returns nil.\n\
342If a clause has one element, as in (CONDITION),\n\
343CONDITION's value if non-nil is returned from the cond-form.")
344 (args)
345 Lisp_Object args;
346{
347 register Lisp_Object clause, val;
348 struct gcpro gcpro1;
349
350 val = Qnil;
351 GCPRO1 (args);
265a9e55 352 while (!NILP (args))
db9f0278
JB
353 {
354 clause = Fcar (args);
355 val = Feval (Fcar (clause));
265a9e55 356 if (!NILP (val))
db9f0278 357 {
03699b14
KR
358 if (!EQ (XCDR (clause), Qnil))
359 val = Fprogn (XCDR (clause));
db9f0278
JB
360 break;
361 }
03699b14 362 args = XCDR (args);
db9f0278
JB
363 }
364 UNGCPRO;
365
366 return val;
367}
368
369DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
1182a7cb 370 "Eval BODY forms sequentially and return value of last one.")
db9f0278
JB
371 (args)
372 Lisp_Object args;
373{
374 register Lisp_Object val, tem;
375 Lisp_Object args_left;
376 struct gcpro gcpro1;
377
378 /* In Mocklisp code, symbols at the front of the progn arglist
379 are to be bound to zero. */
380 if (!EQ (Vmocklisp_arguments, Qt))
381 {
382 val = make_number (0);
90165123 383 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
db9f0278
JB
384 {
385 QUIT;
386 specbind (tem, val), args = Fcdr (args);
387 }
388 }
389
265a9e55 390 if (NILP(args))
db9f0278
JB
391 return Qnil;
392
393 args_left = args;
394 GCPRO1 (args_left);
395
396 do
397 {
398 val = Feval (Fcar (args_left));
399 args_left = Fcdr (args_left);
400 }
265a9e55 401 while (!NILP(args_left));
db9f0278
JB
402
403 UNGCPRO;
404 return val;
405}
406
407DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
1182a7cb 408 "Eval FIRST and BODY sequentially; value from FIRST.\n\
db9f0278
JB
409The value of FIRST is saved during the evaluation of the remaining args,\n\
410whose values are discarded.")
411 (args)
412 Lisp_Object args;
413{
414 Lisp_Object val;
415 register Lisp_Object args_left;
416 struct gcpro gcpro1, gcpro2;
417 register int argnum = 0;
418
265a9e55 419 if (NILP(args))
db9f0278
JB
420 return Qnil;
421
422 args_left = args;
423 val = Qnil;
424 GCPRO2 (args, val);
425
426 do
427 {
428 if (!(argnum++))
429 val = Feval (Fcar (args_left));
430 else
431 Feval (Fcar (args_left));
432 args_left = Fcdr (args_left);
433 }
265a9e55 434 while (!NILP(args_left));
db9f0278
JB
435
436 UNGCPRO;
437 return val;
438}
439
440DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0,
1182a7cb 441 "Eval X, Y and BODY sequentially; value from Y.\n\
db9f0278
JB
442The value of Y is saved during the evaluation of the remaining args,\n\
443whose values are discarded.")
444 (args)
445 Lisp_Object args;
446{
447 Lisp_Object val;
448 register Lisp_Object args_left;
449 struct gcpro gcpro1, gcpro2;
450 register int argnum = -1;
451
452 val = Qnil;
453
87d238ba 454 if (NILP (args))
db9f0278
JB
455 return Qnil;
456
457 args_left = args;
458 val = Qnil;
459 GCPRO2 (args, val);
460
461 do
462 {
463 if (!(argnum++))
464 val = Feval (Fcar (args_left));
465 else
466 Feval (Fcar (args_left));
467 args_left = Fcdr (args_left);
468 }
87d238ba 469 while (!NILP (args_left));
db9f0278
JB
470
471 UNGCPRO;
472 return val;
473}
474
475DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
1182a7cb 476 "Set each SYM to the value of its VAL.\n\
b0d75191
RS
477The symbols SYM are variables; they are literal (not evaluated).\n\
478The values VAL are expressions; they are evaluated.\n\
479Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.\n\
480The second VAL is not computed until after the first SYM is set, and so on;\n\
481each VAL can use the new value of variables set earlier in the `setq'.\n\
196e7d3f 482The return value of the `setq' form is the value of the last VAL.")
db9f0278
JB
483 (args)
484 Lisp_Object args;
485{
486 register Lisp_Object args_left;
487 register Lisp_Object val, sym;
488 struct gcpro gcpro1;
489
265a9e55 490 if (NILP(args))
db9f0278
JB
491 return Qnil;
492
493 args_left = args;
494 GCPRO1 (args);
495
496 do
497 {
498 val = Feval (Fcar (Fcdr (args_left)));
499 sym = Fcar (args_left);
500 Fset (sym, val);
501 args_left = Fcdr (Fcdr (args_left));
502 }
265a9e55 503 while (!NILP(args_left));
db9f0278
JB
504
505 UNGCPRO;
506 return val;
507}
508
509DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0,
510 "Return the argument, without evaluating it. `(quote x)' yields `x'.")
511 (args)
512 Lisp_Object args;
513{
514 return Fcar (args);
515}
516
517DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
518 "Like `quote', but preferred for objects which are functions.\n\
519In byte compilation, `function' causes its argument to be compiled.\n\
520`quote' cannot do that.")
521 (args)
522 Lisp_Object args;
523{
524 return Fcar (args);
525}
526
527DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
528 "Return t if function in which this appears was called interactively.\n\
529This means that the function was called with call-interactively (which\n\
530includes being called as the binding of a key)\n\
531and input is currently coming from the keyboard (not in keyboard macro).")
532 ()
533{
534 register struct backtrace *btp;
535 register Lisp_Object fun;
536
537 if (!INTERACTIVE)
538 return Qnil;
539
db9f0278 540 btp = backtrace_list;
daa37602
JB
541
542 /* If this isn't a byte-compiled function, there may be a frame at
543 the top for Finteractive_p itself. If so, skip it. */
544 fun = Findirect_function (*btp->function);
0598f773 545 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
db9f0278 546 btp = btp->next;
daa37602
JB
547
548 /* If we're running an Emacs 18-style byte-compiled function, there
549 may be a frame for Fbytecode. Now, given the strictest
550 definition, this function isn't really being called
551 interactively, but because that's the way Emacs 18 always builds
552 byte-compiled functions, we'll accept it for now. */
553 if (EQ (*btp->function, Qbytecode))
554 btp = btp->next;
555
556 /* If this isn't a byte-compiled function, then we may now be
557 looking at several frames for special forms. Skip past them. */
558 while (btp &&
559 btp->nargs == UNEVALLED)
a6e3fa71
JB
560 btp = btp->next;
561
daa37602
JB
562 /* btp now points at the frame of the innermost function that isn't
563 a special form, ignoring frames for Finteractive_p and/or
564 Fbytecode at the top. If this frame is for a built-in function
565 (such as load or eval-region) return nil. */
ffd56f97 566 fun = Findirect_function (*btp->function);
90165123 567 if (SUBRP (fun))
db9f0278
JB
568 return Qnil;
569 /* btp points to the frame of a Lisp function that called interactive-p.
570 Return t if that function was called interactively. */
571 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
572 return Qt;
573 return Qnil;
574}
575
576DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0,
1182a7cb 577 "Define NAME as a function.\n\
db9f0278
JB
578The definition is (lambda ARGLIST [DOCSTRING] BODY...).\n\
579See also the function `interactive'.")
580 (args)
581 Lisp_Object args;
582{
583 register Lisp_Object fn_name;
584 register Lisp_Object defn;
585
586 fn_name = Fcar (args);
587 defn = Fcons (Qlambda, Fcdr (args));
265a9e55 588 if (!NILP (Vpurify_flag))
db9f0278
JB
589 defn = Fpurecopy (defn);
590 Ffset (fn_name, defn);
2a49b6e5 591 LOADHIST_ATTACH (fn_name);
db9f0278
JB
592 return fn_name;
593}
594
595DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0,
1182a7cb 596 "Define NAME as a macro.\n\
db9f0278
JB
597The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).\n\
598When the macro is called, as in (NAME ARGS...),\n\
599the function (lambda ARGLIST BODY...) is applied to\n\
600the list ARGS... as it appears in the expression,\n\
601and the result should be a form to be evaluated instead of the original.")
602 (args)
603 Lisp_Object args;
604{
605 register Lisp_Object fn_name;
606 register Lisp_Object defn;
607
608 fn_name = Fcar (args);
609 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
265a9e55 610 if (!NILP (Vpurify_flag))
db9f0278
JB
611 defn = Fpurecopy (defn);
612 Ffset (fn_name, defn);
2a49b6e5 613 LOADHIST_ATTACH (fn_name);
db9f0278
JB
614 return fn_name;
615}
616
617DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
1182a7cb 618 "Define SYMBOL as a variable.\n\
db9f0278
JB
619You are not required to define a variable in order to use it,\n\
620but the definition can supply documentation and an initial value\n\
621in a way that tags can recognize.\n\n\
622INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is void.\n\
06ef7355
RS
623If SYMBOL is buffer-local, its default value is what is set;\n\
624 buffer-local values are not affected.\n\
db9f0278
JB
625INITVALUE and DOCSTRING are optional.\n\
626If DOCSTRING starts with *, this variable is identified as a user option.\n\
627 This means that M-x set-variable and M-x edit-options recognize it.\n\
628If INITVALUE is missing, SYMBOL's value is not set.")
629 (args)
630 Lisp_Object args;
631{
a42ba017 632 register Lisp_Object sym, tem, tail;
db9f0278
JB
633
634 sym = Fcar (args);
a42ba017
RS
635 tail = Fcdr (args);
636 if (!NILP (Fcdr (Fcdr (tail))))
637 error ("too many arguments");
638
639 if (!NILP (tail))
db9f0278
JB
640 {
641 tem = Fdefault_boundp (sym);
265a9e55 642 if (NILP (tem))
db9f0278
JB
643 Fset_default (sym, Feval (Fcar (Fcdr (args))));
644 }
a42ba017
RS
645 tail = Fcdr (Fcdr (args));
646 if (!NILP (Fcar (tail)))
db9f0278 647 {
ca248607 648 tem = Fcar (tail);
265a9e55 649 if (!NILP (Vpurify_flag))
db9f0278
JB
650 tem = Fpurecopy (tem);
651 Fput (sym, Qvariable_documentation, tem);
652 }
2a49b6e5 653 LOADHIST_ATTACH (sym);
db9f0278
JB
654 return sym;
655}
656
657DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
1182a7cb 658 "Define SYMBOL as a constant variable.\n\
c569512a 659The intent is that neither programs nor users should ever change this value.\n\
db9f0278 660Always sets the value of SYMBOL to the result of evalling INITVALUE.\n\
06ef7355
RS
661If SYMBOL is buffer-local, its default value is what is set;\n\
662 buffer-local values are not affected.\n\
701b1ec2 663DOCSTRING is optional.")
db9f0278
JB
664 (args)
665 Lisp_Object args;
666{
667 register Lisp_Object sym, tem;
668
669 sym = Fcar (args);
a42ba017
RS
670 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
671 error ("too many arguments");
672
1182a7cb
DL
673 tem = Feval (Fcar (Fcdr (args)));
674 if (!NILP (Vpurify_flag))
675 tem = Fpurecopy (tem);
676 Fset_default (sym, tem);
db9f0278 677 tem = Fcar (Fcdr (Fcdr (args)));
265a9e55 678 if (!NILP (tem))
db9f0278 679 {
265a9e55 680 if (!NILP (Vpurify_flag))
db9f0278
JB
681 tem = Fpurecopy (tem);
682 Fput (sym, Qvariable_documentation, tem);
683 }
2a49b6e5 684 LOADHIST_ATTACH (sym);
db9f0278
JB
685 return sym;
686}
687
688DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0,
689 "Returns t if VARIABLE is intended to be set and modified by users.\n\
690\(The alternative is a variable used internally in a Lisp program.)\n\
691Determined by whether the first character of the documentation\n\
caff32a7
DL
692for the variable is `*' or if the variable is customizable (has a non-nil\n\
693value of any of `custom-type', `custom-loads' or `standard-value'\n\
694on its property list).")
db9f0278
JB
695 (variable)
696 Lisp_Object variable;
697{
698 Lisp_Object documentation;
699
5e78e475
RS
700 if (!SYMBOLP (variable))
701 return Qnil;
702
db9f0278 703 documentation = Fget (variable, Qvariable_documentation);
90165123 704 if (INTEGERP (documentation) && XINT (documentation) < 0)
db9f0278 705 return Qt;
65411977
RS
706 if (STRINGP (documentation)
707 && ((unsigned char) XSTRING (documentation)->data[0] == '*'))
708 return Qt;
709 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
710 if (CONSP (documentation)
03699b14
KR
711 && STRINGP (XCAR (documentation))
712 && INTEGERP (XCDR (documentation))
713 && XINT (XCDR (documentation)) < 0)
db9f0278 714 return Qt;
caff32a7
DL
715 /* Customizable? */
716 if ((!NILP (Fget (variable, intern ("custom-type"))))
717 || (!NILP (Fget (variable, intern ("custom-loads"))))
718 || (!NILP (Fget (variable, intern ("standard-value")))))
719 return Qt;
db9f0278
JB
720 return Qnil;
721}
722\f
723DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
1182a7cb 724 "Bind variables according to VARLIST then eval BODY.\n\
db9f0278
JB
725The value of the last form in BODY is returned.\n\
726Each element of VARLIST is a symbol (which is bound to nil)\n\
727or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
728Each VALUEFORM can refer to the symbols already bound by this VARLIST.")
729 (args)
730 Lisp_Object args;
731{
732 Lisp_Object varlist, val, elt;
733 int count = specpdl_ptr - specpdl;
734 struct gcpro gcpro1, gcpro2, gcpro3;
735
736 GCPRO3 (args, elt, varlist);
737
738 varlist = Fcar (args);
265a9e55 739 while (!NILP (varlist))
db9f0278
JB
740 {
741 QUIT;
742 elt = Fcar (varlist);
90165123 743 if (SYMBOLP (elt))
db9f0278 744 specbind (elt, Qnil);
08564963
JB
745 else if (! NILP (Fcdr (Fcdr (elt))))
746 Fsignal (Qerror,
747 Fcons (build_string ("`let' bindings can have only one value-form"),
748 elt));
db9f0278
JB
749 else
750 {
751 val = Feval (Fcar (Fcdr (elt)));
752 specbind (Fcar (elt), val);
753 }
754 varlist = Fcdr (varlist);
755 }
756 UNGCPRO;
757 val = Fprogn (Fcdr (args));
758 return unbind_to (count, val);
759}
760
761DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
1182a7cb 762 "Bind variables according to VARLIST then eval BODY.\n\
db9f0278
JB
763The value of the last form in BODY is returned.\n\
764Each element of VARLIST is a symbol (which is bound to nil)\n\
765or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).\n\
766All the VALUEFORMs are evalled before any symbols are bound.")
767 (args)
768 Lisp_Object args;
769{
770 Lisp_Object *temps, tem;
771 register Lisp_Object elt, varlist;
772 int count = specpdl_ptr - specpdl;
773 register int argnum;
774 struct gcpro gcpro1, gcpro2;
775
776 varlist = Fcar (args);
777
778 /* Make space to hold the values to give the bound variables */
779 elt = Flength (varlist);
780 temps = (Lisp_Object *) alloca (XFASTINT (elt) * sizeof (Lisp_Object));
781
782 /* Compute the values and store them in `temps' */
783
784 GCPRO2 (args, *temps);
785 gcpro2.nvars = 0;
786
265a9e55 787 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
db9f0278
JB
788 {
789 QUIT;
790 elt = Fcar (varlist);
90165123 791 if (SYMBOLP (elt))
db9f0278 792 temps [argnum++] = Qnil;
08564963
JB
793 else if (! NILP (Fcdr (Fcdr (elt))))
794 Fsignal (Qerror,
795 Fcons (build_string ("`let' bindings can have only one value-form"),
796 elt));
db9f0278
JB
797 else
798 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
799 gcpro2.nvars = argnum;
800 }
801 UNGCPRO;
802
803 varlist = Fcar (args);
265a9e55 804 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
db9f0278
JB
805 {
806 elt = Fcar (varlist);
807 tem = temps[argnum++];
90165123 808 if (SYMBOLP (elt))
db9f0278
JB
809 specbind (elt, tem);
810 else
811 specbind (Fcar (elt), tem);
812 }
813
814 elt = Fprogn (Fcdr (args));
815 return unbind_to (count, elt);
816}
817
818DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0,
1182a7cb 819 "If TEST yields non-nil, eval BODY... and repeat.\n\
db9f0278
JB
820The order of execution is thus TEST, BODY, TEST, BODY and so on\n\
821until TEST returns nil.")
822 (args)
823 Lisp_Object args;
824{
825 Lisp_Object test, body, tem;
826 struct gcpro gcpro1, gcpro2;
827
828 GCPRO2 (test, body);
829
830 test = Fcar (args);
831 body = Fcdr (args);
e3c24a74
RS
832 while (tem = Feval (test),
833 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
db9f0278
JB
834 {
835 QUIT;
836 Fprogn (body);
837 }
838
839 UNGCPRO;
840 return Qnil;
841}
842
843DEFUN ("macroexpand", Fmacroexpand, Smacroexpand, 1, 2, 0,
844 "Return result of expanding macros at top level of FORM.\n\
845If FORM is not a macro call, it is returned unchanged.\n\
846Otherwise, the macro is expanded and the expansion is considered\n\
847in place of FORM. When a non-macro-call results, it is returned.\n\n\
848The second optional arg ENVIRONMENT species an environment of macro\n\
849definitions to shadow the loaded ones for use in file byte-compilation.")
79e8bfbf 850 (form, environment)
2e267a2e 851 Lisp_Object form;
79e8bfbf 852 Lisp_Object environment;
db9f0278 853{
23d6b5a6 854 /* With cleanups from Hallvard Furuseth. */
db9f0278
JB
855 register Lisp_Object expander, sym, def, tem;
856
857 while (1)
858 {
859 /* Come back here each time we expand a macro call,
860 in case it expands into another macro call. */
90165123 861 if (!CONSP (form))
db9f0278 862 break;
23d6b5a6 863 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
03699b14 864 def = sym = XCAR (form);
23d6b5a6 865 tem = Qnil;
db9f0278
JB
866 /* Trace symbols aliases to other symbols
867 until we get a symbol that is not an alias. */
90165123 868 while (SYMBOLP (def))
db9f0278
JB
869 {
870 QUIT;
23d6b5a6 871 sym = def;
79e8bfbf 872 tem = Fassq (sym, environment);
265a9e55 873 if (NILP (tem))
db9f0278
JB
874 {
875 def = XSYMBOL (sym)->function;
23d6b5a6
JB
876 if (!EQ (def, Qunbound))
877 continue;
db9f0278 878 }
23d6b5a6 879 break;
db9f0278 880 }
79e8bfbf 881 /* Right now TEM is the result from SYM in ENVIRONMENT,
db9f0278 882 and if TEM is nil then DEF is SYM's function definition. */
265a9e55 883 if (NILP (tem))
db9f0278 884 {
79e8bfbf 885 /* SYM is not mentioned in ENVIRONMENT.
db9f0278 886 Look at its function definition. */
90165123 887 if (EQ (def, Qunbound) || !CONSP (def))
db9f0278
JB
888 /* Not defined or definition not suitable */
889 break;
03699b14 890 if (EQ (XCAR (def), Qautoload))
db9f0278
JB
891 {
892 /* Autoloading function: will it be a macro when loaded? */
ee9ee63c 893 tem = Fnth (make_number (4), def);
47ccd8b6 894 if (EQ (tem, Qt) || EQ (tem, Qmacro))
ee9ee63c
JB
895 /* Yes, load it and try again. */
896 {
ca20916b
RS
897 struct gcpro gcpro1;
898 GCPRO1 (form);
ee9ee63c 899 do_autoload (def, sym);
ca20916b 900 UNGCPRO;
ee9ee63c
JB
901 continue;
902 }
903 else
db9f0278 904 break;
db9f0278 905 }
03699b14 906 else if (!EQ (XCAR (def), Qmacro))
db9f0278 907 break;
03699b14 908 else expander = XCDR (def);
db9f0278
JB
909 }
910 else
911 {
03699b14 912 expander = XCDR (tem);
265a9e55 913 if (NILP (expander))
db9f0278
JB
914 break;
915 }
03699b14 916 form = apply1 (expander, XCDR (form));
db9f0278
JB
917 }
918 return form;
919}
920\f
921DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0,
1182a7cb 922 "Eval BODY allowing nonlocal exits using `throw'.\n\
4e306308
RS
923TAG is evalled to get the tag to use; it must not be nil.\n\
924\n\
925Then the BODY is executed.\n\
db9f0278
JB
926Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.\n\
927If no throw happens, `catch' returns the value of the last BODY form.\n\
928If a throw happens, it specifies the value to return from `catch'.")
929 (args)
930 Lisp_Object args;
931{
932 register Lisp_Object tag;
933 struct gcpro gcpro1;
934
935 GCPRO1 (args);
936 tag = Feval (Fcar (args));
937 UNGCPRO;
938 return internal_catch (tag, Fprogn, Fcdr (args));
939}
940
941/* Set up a catch, then call C function FUNC on argument ARG.
942 FUNC should return a Lisp_Object.
943 This is how catches are done from within C code. */
944
945Lisp_Object
946internal_catch (tag, func, arg)
947 Lisp_Object tag;
948 Lisp_Object (*func) ();
949 Lisp_Object arg;
950{
951 /* This structure is made part of the chain `catchlist'. */
952 struct catchtag c;
953
954 /* Fill in the components of c, and put it on the list. */
955 c.next = catchlist;
956 c.tag = tag;
957 c.val = Qnil;
958 c.backlist = backtrace_list;
959 c.handlerlist = handlerlist;
960 c.lisp_eval_depth = lisp_eval_depth;
961 c.pdlcount = specpdl_ptr - specpdl;
962 c.poll_suppress_count = poll_suppress_count;
963 c.gcpro = gcprolist;
bcf28080 964 c.byte_stack = byte_stack_list;
db9f0278
JB
965 catchlist = &c;
966
967 /* Call FUNC. */
968 if (! _setjmp (c.jmp))
969 c.val = (*func) (arg);
970
971 /* Throw works by a longjmp that comes right here. */
972 catchlist = c.next;
973 return c.val;
974}
975
ba410f40
JB
976/* Unwind the specbind, catch, and handler stacks back to CATCH, and
977 jump to that CATCH, returning VALUE as the value of that catch.
db9f0278 978
ba410f40
JB
979 This is the guts Fthrow and Fsignal; they differ only in the way
980 they choose the catch tag to throw to. A catch tag for a
981 condition-case form has a TAG of Qnil.
db9f0278 982
ba410f40
JB
983 Before each catch is discarded, unbind all special bindings and
984 execute all unwind-protect clauses made above that catch. Unwind
985 the handler stack as we go, so that the proper handlers are in
986 effect for each unwind-protect clause we run. At the end, restore
987 some static info saved in CATCH, and longjmp to the location
988 specified in the
989
990 This is used for correct unwinding in Fthrow and Fsignal. */
db9f0278
JB
991
992static void
ba410f40 993unwind_to_catch (catch, value)
db9f0278 994 struct catchtag *catch;
ba410f40 995 Lisp_Object value;
db9f0278
JB
996{
997 register int last_time;
998
ba410f40
JB
999 /* Save the value in the tag. */
1000 catch->val = value;
1001
82da7701 1002 /* Restore the polling-suppression count. */
1cdc3155 1003 set_poll_suppress_count (catch->poll_suppress_count);
82da7701 1004
db9f0278
JB
1005 do
1006 {
1007 last_time = catchlist == catch;
82da7701
JB
1008
1009 /* Unwind the specpdl stack, and then restore the proper set of
1010 handlers. */
db9f0278
JB
1011 unbind_to (catchlist->pdlcount, Qnil);
1012 handlerlist = catchlist->handlerlist;
1013 catchlist = catchlist->next;
1014 }
1015 while (! last_time);
1016
bcf28080 1017 byte_stack_list = catch->byte_stack;
db9f0278 1018 gcprolist = catch->gcpro;
15934ffa
RS
1019#ifdef DEBUG_GCPRO
1020 if (gcprolist != 0)
1021 gcpro_level = gcprolist->level + 1;
1022 else
1023 gcpro_level = 0;
1024#endif
db9f0278
JB
1025 backtrace_list = catch->backlist;
1026 lisp_eval_depth = catch->lisp_eval_depth;
ba410f40
JB
1027
1028 _longjmp (catch->jmp, 1);
db9f0278
JB
1029}
1030
1031DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
1182a7cb 1032 "Throw to the catch for TAG and return VALUE from it.\n\
db9f0278 1033Both TAG and VALUE are evalled.")
79e8bfbf
EN
1034 (tag, value)
1035 register Lisp_Object tag, value;
db9f0278
JB
1036{
1037 register struct catchtag *c;
1038
1039 while (1)
1040 {
265a9e55 1041 if (!NILP (tag))
db9f0278
JB
1042 for (c = catchlist; c; c = c->next)
1043 {
1044 if (EQ (c->tag, tag))
79e8bfbf 1045 unwind_to_catch (c, value);
db9f0278 1046 }
79e8bfbf 1047 tag = Fsignal (Qno_catch, Fcons (tag, Fcons (value, Qnil)));
db9f0278
JB
1048 }
1049}
1050
1051
1052DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0,
1053 "Do BODYFORM, protecting with UNWINDFORMS.\n\
db9f0278
JB
1054If BODYFORM completes normally, its value is returned\n\
1055after executing the UNWINDFORMS.\n\
1056If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.")
1057 (args)
1058 Lisp_Object args;
1059{
1060 Lisp_Object val;
1061 int count = specpdl_ptr - specpdl;
1062
1063 record_unwind_protect (0, Fcdr (args));
1064 val = Feval (Fcar (args));
1065 return unbind_to (count, val);
1066}
1067\f
1068/* Chain of condition handlers currently in effect.
1069 The elements of this chain are contained in the stack frames
1070 of Fcondition_case and internal_condition_case.
1071 When an error is signaled (by calling Fsignal, below),
1072 this chain is searched for an element that applies. */
1073
1074struct handler *handlerlist;
1075
1076DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0,
1077 "Regain control when an error is signaled.\n\
db9f0278
JB
1078executes BODYFORM and returns its value if no error happens.\n\
1079Each element of HANDLERS looks like (CONDITION-NAME BODY...)\n\
1080where the BODY is made of Lisp expressions.\n\n\
1081A handler is applicable to an error\n\
1082if CONDITION-NAME is one of the error's condition names.\n\
1083If an error happens, the first applicable handler is run.\n\
1084\n\
633357d4
RS
1085The car of a handler may be a list of condition names\n\
1086instead of a single condition name.\n\
1087\n\
db9f0278
JB
1088When a handler handles an error,\n\
1089control returns to the condition-case and the handler BODY... is executed\n\
1090with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).\n\
1091VAR may be nil; then you do not get access to the signal information.\n\
1092\n\
1093The value of the last BODY form is returned from the condition-case.\n\
1094See also the function `signal' for more info.")
1095 (args)
1096 Lisp_Object args;
1097{
1098 Lisp_Object val;
1099 struct catchtag c;
1100 struct handler h;
82da7701 1101 register Lisp_Object var, bodyform, handlers;
db9f0278 1102
82da7701
JB
1103 var = Fcar (args);
1104 bodyform = Fcar (Fcdr (args));
1105 handlers = Fcdr (Fcdr (args));
1106 CHECK_SYMBOL (var, 0);
1107
1108 for (val = handlers; ! NILP (val); val = Fcdr (val))
1109 {
1110 Lisp_Object tem;
1111 tem = Fcar (val);
5f96776a
RS
1112 if (! (NILP (tem)
1113 || (CONSP (tem)
03699b14
KR
1114 && (SYMBOLP (XCAR (tem))
1115 || CONSP (XCAR (tem))))))
82da7701
JB
1116 error ("Invalid condition handler", tem);
1117 }
db9f0278
JB
1118
1119 c.tag = Qnil;
1120 c.val = Qnil;
1121 c.backlist = backtrace_list;
1122 c.handlerlist = handlerlist;
1123 c.lisp_eval_depth = lisp_eval_depth;
1124 c.pdlcount = specpdl_ptr - specpdl;
1125 c.poll_suppress_count = poll_suppress_count;
1126 c.gcpro = gcprolist;
bcf28080 1127 c.byte_stack = byte_stack_list;
db9f0278
JB
1128 if (_setjmp (c.jmp))
1129 {
265a9e55 1130 if (!NILP (h.var))
9d58218c
RS
1131 specbind (h.var, c.val);
1132 val = Fprogn (Fcdr (h.chosen_clause));
82da7701
JB
1133
1134 /* Note that this just undoes the binding of h.var; whoever
1135 longjumped to us unwound the stack to c.pdlcount before
1136 throwing. */
db9f0278
JB
1137 unbind_to (c.pdlcount, Qnil);
1138 return val;
1139 }
1140 c.next = catchlist;
1141 catchlist = &c;
db9f0278 1142
82da7701
JB
1143 h.var = var;
1144 h.handler = handlers;
db9f0278 1145 h.next = handlerlist;
db9f0278
JB
1146 h.tag = &c;
1147 handlerlist = &h;
1148
82da7701 1149 val = Feval (bodyform);
db9f0278
JB
1150 catchlist = c.next;
1151 handlerlist = h.next;
1152 return val;
1153}
1154
f029ca5f
RS
1155/* Call the function BFUN with no arguments, catching errors within it
1156 according to HANDLERS. If there is an error, call HFUN with
1157 one argument which is the data that describes the error:
1158 (SIGNALNAME . DATA)
1159
1160 HANDLERS can be a list of conditions to catch.
1161 If HANDLERS is Qt, catch all errors.
1162 If HANDLERS is Qerror, catch all errors
1163 but allow the debugger to run if that is enabled. */
1164
db9f0278
JB
1165Lisp_Object
1166internal_condition_case (bfun, handlers, hfun)
1167 Lisp_Object (*bfun) ();
1168 Lisp_Object handlers;
1169 Lisp_Object (*hfun) ();
1170{
1171 Lisp_Object val;
1172 struct catchtag c;
1173 struct handler h;
1174
01591d17
RS
1175 /* Since Fsignal resets this to 0, it had better be 0 now
1176 or else we have a potential bug. */
1177 if (interrupt_input_blocked != 0)
1178 abort ();
1179
db9f0278
JB
1180 c.tag = Qnil;
1181 c.val = Qnil;
1182 c.backlist = backtrace_list;
1183 c.handlerlist = handlerlist;
1184 c.lisp_eval_depth = lisp_eval_depth;
1185 c.pdlcount = specpdl_ptr - specpdl;
1186 c.poll_suppress_count = poll_suppress_count;
1187 c.gcpro = gcprolist;
bcf28080 1188 c.byte_stack = byte_stack_list;
db9f0278
JB
1189 if (_setjmp (c.jmp))
1190 {
9d58218c 1191 return (*hfun) (c.val);
db9f0278
JB
1192 }
1193 c.next = catchlist;
1194 catchlist = &c;
1195 h.handler = handlers;
1196 h.var = Qnil;
db9f0278
JB
1197 h.next = handlerlist;
1198 h.tag = &c;
1199 handlerlist = &h;
1200
1201 val = (*bfun) ();
1202 catchlist = c.next;
1203 handlerlist = h.next;
1204 return val;
1205}
1206
f029ca5f
RS
1207/* Like internal_condition_case but call HFUN with ARG as its argument. */
1208
d227775c
RS
1209Lisp_Object
1210internal_condition_case_1 (bfun, arg, handlers, hfun)
1211 Lisp_Object (*bfun) ();
1212 Lisp_Object arg;
1213 Lisp_Object handlers;
1214 Lisp_Object (*hfun) ();
1215{
1216 Lisp_Object val;
1217 struct catchtag c;
1218 struct handler h;
1219
1220 c.tag = Qnil;
1221 c.val = Qnil;
1222 c.backlist = backtrace_list;
1223 c.handlerlist = handlerlist;
1224 c.lisp_eval_depth = lisp_eval_depth;
1225 c.pdlcount = specpdl_ptr - specpdl;
1226 c.poll_suppress_count = poll_suppress_count;
1227 c.gcpro = gcprolist;
bcf28080 1228 c.byte_stack = byte_stack_list;
d227775c
RS
1229 if (_setjmp (c.jmp))
1230 {
9d58218c 1231 return (*hfun) (c.val);
d227775c
RS
1232 }
1233 c.next = catchlist;
1234 catchlist = &c;
1235 h.handler = handlers;
1236 h.var = Qnil;
1237 h.next = handlerlist;
1238 h.tag = &c;
1239 handlerlist = &h;
1240
1241 val = (*bfun) (arg);
1242 catchlist = c.next;
1243 handlerlist = h.next;
1244 return val;
1245}
1246\f
db9f0278
JB
1247static Lisp_Object find_handler_clause ();
1248
1249DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
4200e719 1250 "Signal an error. Args are ERROR-SYMBOL and associated DATA.\n\
db9f0278 1251This function does not return.\n\n\
4200e719 1252An error symbol is a symbol with an `error-conditions' property\n\
db9f0278
JB
1253that is a list of condition names.\n\
1254A handler for any of those names will get to handle this signal.\n\
1255The symbol `error' should normally be one of them.\n\
1256\n\
1257DATA should be a list. Its elements are printed as part of the error message.\n\
1258If the signal is handled, DATA is made available to the handler.\n\
1259See also the function `condition-case'.")
4200e719
RS
1260 (error_symbol, data)
1261 Lisp_Object error_symbol, data;
db9f0278 1262{
bfa8ca43
AS
1263 /* When memory is full, ERROR-SYMBOL is nil,
1264 and DATA is (REAL-ERROR-SYMBOL . REAL-DATA). */
db9f0278
JB
1265 register struct handler *allhandlers = handlerlist;
1266 Lisp_Object conditions;
1267 extern int gc_in_progress;
1268 extern int waiting_for_input;
1269 Lisp_Object debugger_value;
c11d3d17 1270 Lisp_Object string;
1ea9dec4 1271 Lisp_Object real_error_symbol;
48f8dfa3 1272 extern int display_busy_cursor_p;
db9f0278 1273
db9f0278
JB
1274 immediate_quit = 0;
1275 if (gc_in_progress || waiting_for_input)
1276 abort ();
1277
1278 TOTALLY_UNBLOCK_INPUT;
1279
1ea9dec4
RS
1280 if (NILP (error_symbol))
1281 real_error_symbol = Fcar (data);
1282 else
1283 real_error_symbol = error_symbol;
1284
48f8dfa3
GM
1285#ifdef HAVE_X_WINDOWS
1286 if (display_busy_cursor_p)
d67235d8 1287 cancel_busy_cursor ();
48f8dfa3 1288#endif
48f8dfa3 1289
61ede770
RS
1290 /* This hook is used by edebug. */
1291 if (! NILP (Vsignal_hook_function))
f01a9c5b 1292 call2 (Vsignal_hook_function, error_symbol, data);
61ede770 1293
1ea9dec4 1294 conditions = Fget (real_error_symbol, Qerror_conditions);
db9f0278
JB
1295
1296 for (; handlerlist; handlerlist = handlerlist->next)
1297 {
1298 register Lisp_Object clause;
8515044c
GM
1299
1300 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
1301 max_lisp_eval_depth = lisp_eval_depth + 20;
1302
1303 if (specpdl_size + 40 > max_specpdl_size)
1304 max_specpdl_size = specpdl_size + 40;
1305
db9f0278 1306 clause = find_handler_clause (handlerlist->handler, conditions,
4200e719 1307 error_symbol, data, &debugger_value);
db9f0278
JB
1308
1309#if 0 /* Most callers are not prepared to handle gc if this returns.
1310 So, since this feature is not very useful, take it out. */
1311 /* If have called debugger and user wants to continue,
1312 just return nil. */
1313 if (EQ (clause, Qlambda))
1314 return debugger_value;
1315#else
1316 if (EQ (clause, Qlambda))
82da7701 1317 {
690337b7
KH
1318 /* We can't return values to code which signaled an error, but we
1319 can continue code which has signaled a quit. */
1ea9dec4 1320 if (EQ (real_error_symbol, Qquit))
82da7701
JB
1321 return Qnil;
1322 else
d3e6f8be 1323 error ("Cannot return from the debugger in an error");
82da7701 1324 }
db9f0278
JB
1325#endif
1326
265a9e55 1327 if (!NILP (clause))
db9f0278 1328 {
9d58218c 1329 Lisp_Object unwind_data;
db9f0278 1330 struct handler *h = handlerlist;
9d58218c 1331
db9f0278 1332 handlerlist = allhandlers;
1ea9dec4
RS
1333
1334 if (NILP (error_symbol))
1335 unwind_data = data;
9d58218c
RS
1336 else
1337 unwind_data = Fcons (error_symbol, data);
1338 h->chosen_clause = clause;
1339 unwind_to_catch (h->tag, unwind_data);
db9f0278
JB
1340 }
1341 }
1342
1343 handlerlist = allhandlers;
1344 /* If no handler is present now, try to run the debugger,
1345 and if that fails, throw to top level. */
4200e719 1346 find_handler_clause (Qerror, conditions, error_symbol, data, &debugger_value);
c11d3d17
RS
1347 if (catchlist != 0)
1348 Fthrow (Qtop_level, Qt);
1349
1ea9dec4 1350 if (! NILP (error_symbol))
c11d3d17
RS
1351 data = Fcons (error_symbol, data);
1352
1353 string = Ferror_message_string (data);
377127ce 1354 fatal ("%s", XSTRING (string)->data, 0);
db9f0278
JB
1355}
1356
128c0f66
RM
1357/* Return nonzero iff LIST is a non-nil atom or
1358 a list containing one of CONDITIONS. */
1359
1360static int
1361wants_debugger (list, conditions)
1362 Lisp_Object list, conditions;
1363{
4de86b16 1364 if (NILP (list))
128c0f66
RM
1365 return 0;
1366 if (! CONSP (list))
1367 return 1;
1368
ab67260b 1369 while (CONSP (conditions))
128c0f66 1370 {
ab67260b 1371 Lisp_Object this, tail;
03699b14
KR
1372 this = XCAR (conditions);
1373 for (tail = list; CONSP (tail); tail = XCDR (tail))
1374 if (EQ (XCAR (tail), this))
128c0f66 1375 return 1;
03699b14 1376 conditions = XCDR (conditions);
128c0f66 1377 }
ab67260b 1378 return 0;
128c0f66
RM
1379}
1380
fc950e09
KH
1381/* Return 1 if an error with condition-symbols CONDITIONS,
1382 and described by SIGNAL-DATA, should skip the debugger
1383 according to debugger-ignore-errors. */
1384
1385static int
1386skip_debugger (conditions, data)
1387 Lisp_Object conditions, data;
1388{
1389 Lisp_Object tail;
1390 int first_string = 1;
1391 Lisp_Object error_message;
1392
1393 for (tail = Vdebug_ignored_errors; CONSP (tail);
03699b14 1394 tail = XCDR (tail))
fc950e09 1395 {
03699b14 1396 if (STRINGP (XCAR (tail)))
fc950e09
KH
1397 {
1398 if (first_string)
1399 {
1400 error_message = Ferror_message_string (data);
1401 first_string = 0;
1402 }
03699b14 1403 if (fast_string_match (XCAR (tail), error_message) >= 0)
fc950e09
KH
1404 return 1;
1405 }
1406 else
1407 {
1408 Lisp_Object contail;
1409
1410 for (contail = conditions; CONSP (contail);
03699b14
KR
1411 contail = XCDR (contail))
1412 if (EQ (XCAR (tail), XCAR (contail)))
fc950e09
KH
1413 return 1;
1414 }
1415 }
1416
1417 return 0;
1418}
1419
128c0f66 1420/* Value of Qlambda means we have called debugger and user has continued.
1ea9dec4 1421 There are two ways to pass SIG and DATA:
9b942ebd 1422 = SIG is the error symbol, and DATA is the rest of the data.
1ea9dec4 1423 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
9b942ebd 1424 This is for memory-full errors only.
1ea9dec4 1425
128c0f66 1426 Store value returned from debugger into *DEBUGGER_VALUE_PTR. */
db9f0278
JB
1427
1428static Lisp_Object
1429find_handler_clause (handlers, conditions, sig, data, debugger_value_ptr)
1430 Lisp_Object handlers, conditions, sig, data;
1431 Lisp_Object *debugger_value_ptr;
1432{
1433 register Lisp_Object h;
1434 register Lisp_Object tem;
db9f0278
JB
1435
1436 if (EQ (handlers, Qt)) /* t is used by handlers for all conditions, set up by C code. */
1437 return Qt;
61ede770
RS
1438 /* error is used similarly, but means print an error message
1439 and run the debugger if that is enabled. */
1440 if (EQ (handlers, Qerror)
57a6e758
RS
1441 || !NILP (Vdebug_on_signal)) /* This says call debugger even if
1442 there is a handler. */
db9f0278 1443 {
61ede770
RS
1444 int count = specpdl_ptr - specpdl;
1445 int debugger_called = 0;
1ea9dec4 1446 Lisp_Object sig_symbol, combined_data;
9b942ebd
RS
1447 /* This is set to 1 if we are handling a memory-full error,
1448 because these must not run the debugger.
1449 (There is no room in memory to do that!) */
1450 int no_debugger = 0;
1ea9dec4
RS
1451
1452 if (NILP (sig))
1453 {
1454 combined_data = data;
1455 sig_symbol = Fcar (data);
9b942ebd 1456 no_debugger = 1;
1ea9dec4
RS
1457 }
1458 else
1459 {
1460 combined_data = Fcons (sig, data);
1461 sig_symbol = sig;
1462 }
61ede770 1463
128c0f66 1464 if (wants_debugger (Vstack_trace_on_error, conditions))
88817f3b 1465 {
b369fa65 1466#ifdef PROTOTYPES
88817f3b
RS
1467 internal_with_output_to_temp_buffer ("*Backtrace*",
1468 (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
1469 Qnil);
1470#else
1471 internal_with_output_to_temp_buffer ("*Backtrace*",
1472 Fbacktrace, Qnil);
1473#endif
1474 }
9b942ebd
RS
1475 if (! no_debugger
1476 && (EQ (sig_symbol, Qquit)
1477 ? debug_on_quit
1478 : wants_debugger (Vdebug_on_error, conditions))
1ea9dec4 1479 && ! skip_debugger (conditions, combined_data)
be857679 1480 && when_entered_debugger < num_nonmacro_input_events)
db9f0278 1481 {
db9f0278 1482 specbind (Qdebug_on_error, Qnil);
fc950e09
KH
1483 *debugger_value_ptr
1484 = call_debugger (Fcons (Qerror,
1ea9dec4 1485 Fcons (combined_data, Qnil)));
61ede770
RS
1486 debugger_called = 1;
1487 }
1488 /* If there is no handler, return saying whether we ran the debugger. */
1489 if (EQ (handlers, Qerror))
1490 {
1491 if (debugger_called)
1492 return unbind_to (count, Qlambda);
1493 return Qt;
db9f0278 1494 }
db9f0278
JB
1495 }
1496 for (h = handlers; CONSP (h); h = Fcdr (h))
1497 {
5f96776a
RS
1498 Lisp_Object handler, condit;
1499
1500 handler = Fcar (h);
1501 if (!CONSP (handler))
db9f0278 1502 continue;
5f96776a
RS
1503 condit = Fcar (handler);
1504 /* Handle a single condition name in handler HANDLER. */
1505 if (SYMBOLP (condit))
1506 {
1507 tem = Fmemq (Fcar (handler), conditions);
1508 if (!NILP (tem))
1509 return handler;
1510 }
1511 /* Handle a list of condition names in handler HANDLER. */
1512 else if (CONSP (condit))
1513 {
1514 while (CONSP (condit))
1515 {
1516 tem = Fmemq (Fcar (condit), conditions);
1517 if (!NILP (tem))
1518 return handler;
03699b14 1519 condit = XCDR (condit);
5f96776a
RS
1520 }
1521 }
db9f0278
JB
1522 }
1523 return Qnil;
1524}
1525
1526/* dump an error message; called like printf */
1527
1528/* VARARGS 1 */
1529void
1530error (m, a1, a2, a3)
1531 char *m;
9125da08 1532 char *a1, *a2, *a3;
db9f0278
JB
1533{
1534 char buf[200];
9125da08
RS
1535 int size = 200;
1536 int mlen;
1537 char *buffer = buf;
1538 char *args[3];
1539 int allocated = 0;
1540 Lisp_Object string;
1541
1542 args[0] = a1;
1543 args[1] = a2;
1544 args[2] = a3;
1545
1546 mlen = strlen (m);
db9f0278
JB
1547
1548 while (1)
9125da08 1549 {
955f3ff9 1550 int used = doprnt (buffer, size, m, m + mlen, 3, args);
9125da08
RS
1551 if (used < size)
1552 break;
1553 size *= 2;
1554 if (allocated)
1555 buffer = (char *) xrealloc (buffer, size);
5ece1728
RS
1556 else
1557 {
1558 buffer = (char *) xmalloc (size);
1559 allocated = 1;
1560 }
9125da08
RS
1561 }
1562
955f3ff9 1563 string = build_string (buffer);
9125da08
RS
1564 if (allocated)
1565 free (buffer);
1566
1567 Fsignal (Qerror, Fcons (string, Qnil));
db9f0278
JB
1568}
1569\f
1570DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0,
1571 "T if FUNCTION makes provisions for interactive calling.\n\
1572This means it contains a description for how to read arguments to give it.\n\
1573The value is nil for an invalid function or a symbol with no function\n\
1574definition.\n\
1575\n\
1576Interactively callable functions include strings and vectors (treated\n\
1577as keyboard macros), lambda-expressions that contain a top-level call\n\
1578to `interactive', autoload definitions made by `autoload' with non-nil\n\
1579fourth argument, and some of the built-in functions of Lisp.\n\
1580\n\
1581Also, a symbol satisfies `commandp' if its function definition does so.")
1582 (function)
1583 Lisp_Object function;
1584{
1585 register Lisp_Object fun;
1586 register Lisp_Object funcar;
db9f0278
JB
1587
1588 fun = function;
1589
ffd56f97
JB
1590 fun = indirect_function (fun);
1591 if (EQ (fun, Qunbound))
1592 return Qnil;
db9f0278
JB
1593
1594 /* Emacs primitives are interactive if their DEFUN specifies an
1595 interactive spec. */
90165123 1596 if (SUBRP (fun))
db9f0278
JB
1597 {
1598 if (XSUBR (fun)->prompt)
1599 return Qt;
1600 else
1601 return Qnil;
1602 }
1603
1604 /* Bytecode objects are interactive if they are long enough to
1605 have an element whose index is COMPILED_INTERACTIVE, which is
1606 where the interactive spec is stored. */
90165123 1607 else if (COMPILEDP (fun))
f9b4aacf 1608 return ((XVECTOR (fun)->size & PSEUDOVECTOR_SIZE_MASK) > COMPILED_INTERACTIVE
db9f0278
JB
1609 ? Qt : Qnil);
1610
1611 /* Strings and vectors are keyboard macros. */
90165123 1612 if (STRINGP (fun) || VECTORP (fun))
db9f0278
JB
1613 return Qt;
1614
1615 /* Lists may represent commands. */
1616 if (!CONSP (fun))
1617 return Qnil;
1618 funcar = Fcar (fun);
90165123 1619 if (!SYMBOLP (funcar))
db9f0278
JB
1620 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1621 if (EQ (funcar, Qlambda))
1622 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
1623 if (EQ (funcar, Qmocklisp))
1624 return Qt; /* All mocklisp functions can be called interactively */
1625 if (EQ (funcar, Qautoload))
1626 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
1627 else
1628 return Qnil;
1629}
1630
1631/* ARGSUSED */
1632DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0,
1633 "Define FUNCTION to autoload from FILE.\n\
1634FUNCTION is a symbol; FILE is a file name string to pass to `load'.\n\
1635Third arg DOCSTRING is documentation for the function.\n\
1636Fourth arg INTERACTIVE if non-nil says function can be called interactively.\n\
ee9ee63c
JB
1637Fifth arg TYPE indicates the type of the object:\n\
1638 nil or omitted says FUNCTION is a function,\n\
1639 `keymap' says FUNCTION is really a keymap, and\n\
1640 `macro' or t says FUNCTION is really a macro.\n\
db9f0278
JB
1641Third through fifth args give info about the real definition.\n\
1642They default to nil.\n\
1643If FUNCTION is already defined other than as an autoload,\n\
1644this does nothing and returns nil.")
ee9ee63c
JB
1645 (function, file, docstring, interactive, type)
1646 Lisp_Object function, file, docstring, interactive, type;
db9f0278
JB
1647{
1648#ifdef NO_ARG_ARRAY
1649 Lisp_Object args[4];
1650#endif
1651
1652 CHECK_SYMBOL (function, 0);
1653 CHECK_STRING (file, 1);
1654
1655 /* If function is defined and not as an autoload, don't override */
1656 if (!EQ (XSYMBOL (function)->function, Qunbound)
90165123 1657 && !(CONSP (XSYMBOL (function)->function)
03699b14 1658 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
db9f0278
JB
1659 return Qnil;
1660
7973e637
SM
1661 if (NILP (Vpurify_flag))
1662 /* Only add entries after dumping, because the ones before are
1663 not useful and else we get loads of them from the loaddefs.el. */
1664 LOADHIST_ATTACH (Fcons (Qautoload, function));
1665
db9f0278
JB
1666#ifdef NO_ARG_ARRAY
1667 args[0] = file;
1668 args[1] = docstring;
1669 args[2] = interactive;
ee9ee63c 1670 args[3] = type;
db9f0278
JB
1671
1672 return Ffset (function, Fcons (Qautoload, Flist (4, &args[0])));
1673#else /* NO_ARG_ARRAY */
1674 return Ffset (function, Fcons (Qautoload, Flist (4, &file)));
1675#endif /* not NO_ARG_ARRAY */
1676}
1677
1678Lisp_Object
1679un_autoload (oldqueue)
1680 Lisp_Object oldqueue;
1681{
1682 register Lisp_Object queue, first, second;
1683
1684 /* Queue to unwind is current value of Vautoload_queue.
1685 oldqueue is the shadowed value to leave in Vautoload_queue. */
1686 queue = Vautoload_queue;
1687 Vautoload_queue = oldqueue;
1688 while (CONSP (queue))
1689 {
1690 first = Fcar (queue);
1691 second = Fcdr (first);
1692 first = Fcar (first);
1693 if (EQ (second, Qnil))
1694 Vfeatures = first;
1695 else
1696 Ffset (first, second);
1697 queue = Fcdr (queue);
1698 }
1699 return Qnil;
1700}
1701
ca20916b
RS
1702/* Load an autoloaded function.
1703 FUNNAME is the symbol which is the function's name.
1704 FUNDEF is the autoload definition (a list). */
1705
045ba794 1706void
db9f0278
JB
1707do_autoload (fundef, funname)
1708 Lisp_Object fundef, funname;
1709{
1710 int count = specpdl_ptr - specpdl;
cb81ac97 1711 Lisp_Object fun, queue, first, second;
ca20916b 1712 struct gcpro gcpro1, gcpro2, gcpro3;
db9f0278
JB
1713
1714 fun = funname;
1715 CHECK_SYMBOL (funname, 0);
ca20916b 1716 GCPRO3 (fun, funname, fundef);
db9f0278 1717
f87740dc
RS
1718 /* Preserve the match data. */
1719 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
1720
1721 /* Value saved here is to be restored into Vautoload_queue. */
db9f0278
JB
1722 record_unwind_protect (un_autoload, Vautoload_queue);
1723 Vautoload_queue = Qt;
4aac2302 1724 Fload (Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil, Qt);
2a49b6e5 1725
f87740dc 1726 /* Save the old autoloads, in case we ever do an unload. */
2a49b6e5
RS
1727 queue = Vautoload_queue;
1728 while (CONSP (queue))
1729 {
1730 first = Fcar (queue);
1731 second = Fcdr (first);
1732 first = Fcar (first);
5739ce6b
ER
1733
1734 /* Note: This test is subtle. The cdr of an autoload-queue entry
1735 may be an atom if the autoload entry was generated by a defalias
f87740dc 1736 or fset. */
5739ce6b 1737 if (CONSP (second))
03e267c2 1738 Fput (first, Qautoload, (Fcdr (second)));
5739ce6b 1739
2a49b6e5
RS
1740 queue = Fcdr (queue);
1741 }
1742
db9f0278
JB
1743 /* Once loading finishes, don't undo it. */
1744 Vautoload_queue = Qt;
1745 unbind_to (count, Qnil);
1746
ffd56f97
JB
1747 fun = Findirect_function (fun);
1748
76c2b0cc 1749 if (!NILP (Fequal (fun, fundef)))
db9f0278
JB
1750 error ("Autoloading failed to define function %s",
1751 XSYMBOL (funname)->name->data);
ca20916b 1752 UNGCPRO;
db9f0278
JB
1753}
1754\f
1755DEFUN ("eval", Feval, Seval, 1, 1, 0,
1756 "Evaluate FORM and return its value.")
1757 (form)
1758 Lisp_Object form;
1759{
1760 Lisp_Object fun, val, original_fun, original_args;
1761 Lisp_Object funcar;
1762 struct backtrace backtrace;
1763 struct gcpro gcpro1, gcpro2, gcpro3;
1764
cadf6ba2
GM
1765#if 0 /* Can't do this check anymore because realize_basic_faces has
1766 to BLOCK_INPUT, and can call Lisp. What's really needed is a
1767 flag indicating that we're currently handling a signal. */
48f8dfa3
GM
1768 /* Since Fsignal resets this to 0, it had better be 0 now
1769 or else we have a potential bug. */
1770 if (interrupt_input_blocked != 0)
1771 abort ();
cadf6ba2 1772#endif
48f8dfa3 1773
90165123 1774 if (SYMBOLP (form))
db9f0278
JB
1775 {
1776 if (EQ (Vmocklisp_arguments, Qt))
1777 return Fsymbol_value (form);
1778 val = Fsymbol_value (form);
265a9e55 1779 if (NILP (val))
a631e24c 1780 XSETFASTINT (val, 0);
db9f0278 1781 else if (EQ (val, Qt))
a631e24c 1782 XSETFASTINT (val, 1);
db9f0278
JB
1783 return val;
1784 }
1785 if (!CONSP (form))
1786 return form;
1787
1788 QUIT;
1789 if (consing_since_gc > gc_cons_threshold)
1790 {
1791 GCPRO1 (form);
1792 Fgarbage_collect ();
1793 UNGCPRO;
1794 }
1795
1796 if (++lisp_eval_depth > max_lisp_eval_depth)
1797 {
1798 if (max_lisp_eval_depth < 100)
1799 max_lisp_eval_depth = 100;
1800 if (lisp_eval_depth > max_lisp_eval_depth)
1801 error ("Lisp nesting exceeds max-lisp-eval-depth");
1802 }
1803
1804 original_fun = Fcar (form);
1805 original_args = Fcdr (form);
1806
1807 backtrace.next = backtrace_list;
1808 backtrace_list = &backtrace;
1809 backtrace.function = &original_fun; /* This also protects them from gc */
1810 backtrace.args = &original_args;
1811 backtrace.nargs = UNEVALLED;
1812 backtrace.evalargs = 1;
1813 backtrace.debug_on_exit = 0;
1814
1815 if (debug_on_next_call)
1816 do_debug_on_call (Qt);
1817
1818 /* At this point, only original_fun and original_args
1819 have values that will be used below */
1820 retry:
ffd56f97 1821 fun = Findirect_function (original_fun);
db9f0278 1822
90165123 1823 if (SUBRP (fun))
db9f0278
JB
1824 {
1825 Lisp_Object numargs;
166c822d 1826 Lisp_Object argvals[8];
db9f0278
JB
1827 Lisp_Object args_left;
1828 register int i, maxargs;
1829
1830 args_left = original_args;
1831 numargs = Flength (args_left);
1832
1833 if (XINT (numargs) < XSUBR (fun)->min_args ||
1834 (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
1835 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (numargs, Qnil)));
1836
1837 if (XSUBR (fun)->max_args == UNEVALLED)
1838 {
1839 backtrace.evalargs = 0;
1840 val = (*XSUBR (fun)->function) (args_left);
1841 goto done;
1842 }
1843
1844 if (XSUBR (fun)->max_args == MANY)
1845 {
1846 /* Pass a vector of evaluated arguments */
1847 Lisp_Object *vals;
1848 register int argnum = 0;
1849
1850 vals = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
1851
1852 GCPRO3 (args_left, fun, fun);
1853 gcpro3.var = vals;
1854 gcpro3.nvars = 0;
1855
265a9e55 1856 while (!NILP (args_left))
db9f0278
JB
1857 {
1858 vals[argnum++] = Feval (Fcar (args_left));
1859 args_left = Fcdr (args_left);
1860 gcpro3.nvars = argnum;
1861 }
db9f0278
JB
1862
1863 backtrace.args = vals;
1864 backtrace.nargs = XINT (numargs);
1865
1866 val = (*XSUBR (fun)->function) (XINT (numargs), vals);
a6e3fa71 1867 UNGCPRO;
db9f0278
JB
1868 goto done;
1869 }
1870
1871 GCPRO3 (args_left, fun, fun);
1872 gcpro3.var = argvals;
1873 gcpro3.nvars = 0;
1874
1875 maxargs = XSUBR (fun)->max_args;
1876 for (i = 0; i < maxargs; args_left = Fcdr (args_left))
1877 {
1878 argvals[i] = Feval (Fcar (args_left));
1879 gcpro3.nvars = ++i;
1880 }
1881
1882 UNGCPRO;
1883
1884 backtrace.args = argvals;
1885 backtrace.nargs = XINT (numargs);
1886
1887 switch (i)
1888 {
1889 case 0:
1890 val = (*XSUBR (fun)->function) ();
1891 goto done;
1892 case 1:
1893 val = (*XSUBR (fun)->function) (argvals[0]);
1894 goto done;
1895 case 2:
1896 val = (*XSUBR (fun)->function) (argvals[0], argvals[1]);
1897 goto done;
1898 case 3:
1899 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1900 argvals[2]);
1901 goto done;
1902 case 4:
1903 val = (*XSUBR (fun)->function) (argvals[0], argvals[1],
1904 argvals[2], argvals[3]);
1905 goto done;
1906 case 5:
1907 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1908 argvals[3], argvals[4]);
1909 goto done;
1910 case 6:
1911 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1912 argvals[3], argvals[4], argvals[5]);
1913 goto done;
15c65264
RS
1914 case 7:
1915 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1916 argvals[3], argvals[4], argvals[5],
1917 argvals[6]);
1918 goto done;
db9f0278 1919
166c822d
KH
1920 case 8:
1921 val = (*XSUBR (fun)->function) (argvals[0], argvals[1], argvals[2],
1922 argvals[3], argvals[4], argvals[5],
1923 argvals[6], argvals[7]);
1924 goto done;
1925
db9f0278 1926 default:
08564963
JB
1927 /* Someone has created a subr that takes more arguments than
1928 is supported by this code. We need to either rewrite the
1929 subr to use a different argument protocol, or add more
1930 cases to this switch. */
1931 abort ();
db9f0278
JB
1932 }
1933 }
90165123 1934 if (COMPILEDP (fun))
db9f0278
JB
1935 val = apply_lambda (fun, original_args, 1);
1936 else
1937 {
1938 if (!CONSP (fun))
1939 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1940 funcar = Fcar (fun);
90165123 1941 if (!SYMBOLP (funcar))
db9f0278
JB
1942 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1943 if (EQ (funcar, Qautoload))
1944 {
1945 do_autoload (fun, original_fun);
1946 goto retry;
1947 }
1948 if (EQ (funcar, Qmacro))
1949 val = Feval (apply1 (Fcdr (fun), original_args));
1950 else if (EQ (funcar, Qlambda))
1951 val = apply_lambda (fun, original_args, 1);
1952 else if (EQ (funcar, Qmocklisp))
1953 val = ml_apply (fun, original_args);
1954 else
1955 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
1956 }
1957 done:
1958 if (!EQ (Vmocklisp_arguments, Qt))
1959 {
265a9e55 1960 if (NILP (val))
a631e24c 1961 XSETFASTINT (val, 0);
db9f0278 1962 else if (EQ (val, Qt))
a631e24c 1963 XSETFASTINT (val, 1);
db9f0278
JB
1964 }
1965 lisp_eval_depth--;
1966 if (backtrace.debug_on_exit)
1967 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
1968 backtrace_list = backtrace.next;
1969 return val;
1970}
1971\f
1972DEFUN ("apply", Fapply, Sapply, 2, MANY, 0,
1973 "Call FUNCTION with our remaining args, using our last arg as list of args.\n\
fd7fe9a1 1974Then return the value FUNCTION returns.\n\
db9f0278
JB
1975Thus, (apply '+ 1 2 '(3 4)) returns 10.")
1976 (nargs, args)
1977 int nargs;
1978 Lisp_Object *args;
1979{
1980 register int i, numargs;
1981 register Lisp_Object spread_arg;
1982 register Lisp_Object *funcall_args;
db9f0278 1983 Lisp_Object fun;
a6e3fa71 1984 struct gcpro gcpro1;
db9f0278
JB
1985
1986 fun = args [0];
1987 funcall_args = 0;
1988 spread_arg = args [nargs - 1];
1989 CHECK_LIST (spread_arg, nargs);
1990
1991 numargs = XINT (Flength (spread_arg));
1992
1993 if (numargs == 0)
1994 return Ffuncall (nargs - 1, args);
1995 else if (numargs == 1)
1996 {
03699b14 1997 args [nargs - 1] = XCAR (spread_arg);
db9f0278
JB
1998 return Ffuncall (nargs, args);
1999 }
2000
a6e3fa71 2001 numargs += nargs - 2;
db9f0278 2002
ffd56f97
JB
2003 fun = indirect_function (fun);
2004 if (EQ (fun, Qunbound))
db9f0278 2005 {
ffd56f97
JB
2006 /* Let funcall get the error */
2007 fun = args[0];
2008 goto funcall;
db9f0278
JB
2009 }
2010
90165123 2011 if (SUBRP (fun))
db9f0278
JB
2012 {
2013 if (numargs < XSUBR (fun)->min_args
2014 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2015 goto funcall; /* Let funcall get the error */
2016 else if (XSUBR (fun)->max_args > numargs)
2017 {
2018 /* Avoid making funcall cons up a yet another new vector of arguments
2019 by explicitly supplying nil's for optional values */
2020 funcall_args = (Lisp_Object *) alloca ((1 + XSUBR (fun)->max_args)
2021 * sizeof (Lisp_Object));
2022 for (i = numargs; i < XSUBR (fun)->max_args;)
2023 funcall_args[++i] = Qnil;
a6e3fa71
JB
2024 GCPRO1 (*funcall_args);
2025 gcpro1.nvars = 1 + XSUBR (fun)->max_args;
db9f0278
JB
2026 }
2027 }
2028 funcall:
2029 /* We add 1 to numargs because funcall_args includes the
2030 function itself as well as its arguments. */
2031 if (!funcall_args)
a6e3fa71
JB
2032 {
2033 funcall_args = (Lisp_Object *) alloca ((1 + numargs)
2034 * sizeof (Lisp_Object));
2035 GCPRO1 (*funcall_args);
2036 gcpro1.nvars = 1 + numargs;
2037 }
2038
db9f0278
JB
2039 bcopy (args, funcall_args, nargs * sizeof (Lisp_Object));
2040 /* Spread the last arg we got. Its first element goes in
2041 the slot that it used to occupy, hence this value of I. */
2042 i = nargs - 1;
265a9e55 2043 while (!NILP (spread_arg))
db9f0278 2044 {
03699b14
KR
2045 funcall_args [i++] = XCAR (spread_arg);
2046 spread_arg = XCDR (spread_arg);
db9f0278 2047 }
a6e3fa71
JB
2048
2049 RETURN_UNGCPRO (Ffuncall (gcpro1.nvars, funcall_args));
db9f0278
JB
2050}
2051\f
ff936e53
SM
2052/* Run hook variables in various ways. */
2053
2054enum run_hooks_condition {to_completion, until_success, until_failure};
2055
2056DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0,
2057 "Run each hook in HOOKS. Major mode functions use this.\n\
2058Each argument should be a symbol, a hook variable.\n\
2059These symbols are processed in the order specified.\n\
2060If a hook symbol has a non-nil value, that value may be a function\n\
2061or a list of functions to be called to run the hook.\n\
2062If the value is a function, it is called with no arguments.\n\
2063If it is a list, the elements are called, in order, with no arguments.\n\
2064\n\
2065To make a hook variable buffer-local, use `make-local-hook',\n\
2066not `make-local-variable'.")
2067 (nargs, args)
2068 int nargs;
2069 Lisp_Object *args;
2070{
2071 Lisp_Object hook[1];
2072 register int i;
2073
2074 for (i = 0; i < nargs; i++)
2075 {
2076 hook[0] = args[i];
2077 run_hook_with_args (1, hook, to_completion);
2078 }
2079
2080 return Qnil;
2081}
2082
a0d76c27
EN
2083DEFUN ("run-hook-with-args", Frun_hook_with_args,
2084 Srun_hook_with_args, 1, MANY, 0,
b0b667cb
KH
2085 "Run HOOK with the specified arguments ARGS.\n\
2086HOOK should be a symbol, a hook variable. If HOOK has a non-nil\n\
2087value, that value may be a function or a list of functions to be\n\
2088called to run the hook. If the value is a function, it is called with\n\
2089the given arguments and its return value is returned. If it is a list\n\
2090of functions, those functions are called, in order,\n\
2091with the given arguments ARGS.\n\
2092It is best not to depend on the value return by `run-hook-with-args',\n\
2093as that may change.\n\
2094\n\
ff936e53
SM
2095To make a hook variable buffer-local, use `make-local-hook',\n\
2096not `make-local-variable'.")
2097 (nargs, args)
2098 int nargs;
2099 Lisp_Object *args;
2100{
2101 return run_hook_with_args (nargs, args, to_completion);
2102}
2103
a0d76c27
EN
2104DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2105 Srun_hook_with_args_until_success, 1, MANY, 0,
ff936e53
SM
2106 "Run HOOK with the specified arguments ARGS.\n\
2107HOOK should be a symbol, a hook variable. Its value should\n\
2108be a list of functions. We call those functions, one by one,\n\
2109passing arguments ARGS to each of them, until one of them\n\
2110returns a non-nil value. Then we return that value.\n\
2111If all the functions return nil, we return nil.\n\
2112\n\
2113To make a hook variable buffer-local, use `make-local-hook',\n\
2114not `make-local-variable'.")
b0b667cb
KH
2115 (nargs, args)
2116 int nargs;
2117 Lisp_Object *args;
2118{
ff936e53
SM
2119 return run_hook_with_args (nargs, args, until_success);
2120}
2121
a0d76c27
EN
2122DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2123 Srun_hook_with_args_until_failure, 1, MANY, 0,
ff936e53
SM
2124 "Run HOOK with the specified arguments ARGS.\n\
2125HOOK should be a symbol, a hook variable. Its value should\n\
2126be a list of functions. We call those functions, one by one,\n\
2127passing arguments ARGS to each of them, until one of them\n\
2128returns nil. Then we return nil.\n\
2129If all the functions return non-nil, we return non-nil.\n\
2130\n\
2131To make a hook variable buffer-local, use `make-local-hook',\n\
2132not `make-local-variable'.")
2133 (nargs, args)
2134 int nargs;
2135 Lisp_Object *args;
2136{
2137 return run_hook_with_args (nargs, args, until_failure);
2138}
2139
c933ea05
RS
2140/* ARGS[0] should be a hook symbol.
2141 Call each of the functions in the hook value, passing each of them
2142 as arguments all the rest of ARGS (all NARGS - 1 elements).
2143 COND specifies a condition to test after each call
2144 to decide whether to stop.
2145 The caller (or its caller, etc) must gcpro all of ARGS,
2146 except that it isn't necessary to gcpro ARGS[0]. */
2147
ff936e53
SM
2148Lisp_Object
2149run_hook_with_args (nargs, args, cond)
2150 int nargs;
2151 Lisp_Object *args;
2152 enum run_hooks_condition cond;
2153{
2154 Lisp_Object sym, val, ret;
fada05d6
KH
2155 Lisp_Object globals;
2156 struct gcpro gcpro1, gcpro2, gcpro3;
b0b667cb 2157
f029ca5f
RS
2158 /* If we are dying or still initializing,
2159 don't do anything--it would probably crash if we tried. */
2160 if (NILP (Vrun_hooks))
caff32a7 2161 return Qnil;
f029ca5f 2162
b0b667cb 2163 sym = args[0];
aa681b51 2164 val = find_symbol_value (sym);
ff936e53
SM
2165 ret = (cond == until_failure ? Qt : Qnil);
2166
b0b667cb 2167 if (EQ (val, Qunbound) || NILP (val))
ff936e53 2168 return ret;
03699b14 2169 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
b0b667cb
KH
2170 {
2171 args[0] = val;
2172 return Ffuncall (nargs, args);
2173 }
2174 else
2175 {
fada05d6
KH
2176 globals = Qnil;
2177 GCPRO3 (sym, val, globals);
cb9d21f8 2178
ff936e53
SM
2179 for (;
2180 CONSP (val) && ((cond == to_completion)
2181 || (cond == until_success ? NILP (ret)
2182 : !NILP (ret)));
03699b14 2183 val = XCDR (val))
b0b667cb 2184 {
03699b14 2185 if (EQ (XCAR (val), Qt))
b0b667cb
KH
2186 {
2187 /* t indicates this hook has a local binding;
2188 it means to run the global binding too. */
b0b667cb 2189
ff936e53
SM
2190 for (globals = Fdefault_value (sym);
2191 CONSP (globals) && ((cond == to_completion)
2192 || (cond == until_success ? NILP (ret)
2193 : !NILP (ret)));
03699b14 2194 globals = XCDR (globals))
b0b667cb 2195 {
03699b14 2196 args[0] = XCAR (globals);
77d92e05
RS
2197 /* In a global value, t should not occur. If it does, we
2198 must ignore it to avoid an endless loop. */
2199 if (!EQ (args[0], Qt))
2200 ret = Ffuncall (nargs, args);
b0b667cb
KH
2201 }
2202 }
2203 else
2204 {
03699b14 2205 args[0] = XCAR (val);
ff936e53 2206 ret = Ffuncall (nargs, args);
b0b667cb
KH
2207 }
2208 }
cb9d21f8
RS
2209
2210 UNGCPRO;
ff936e53 2211 return ret;
b0b667cb
KH
2212 }
2213}
c933ea05
RS
2214
2215/* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
2216 present value of that symbol.
2217 Call each element of FUNLIST,
2218 passing each of them the rest of ARGS.
2219 The caller (or its caller, etc) must gcpro all of ARGS,
2220 except that it isn't necessary to gcpro ARGS[0]. */
2221
2222Lisp_Object
2223run_hook_list_with_args (funlist, nargs, args)
2224 Lisp_Object funlist;
2225 int nargs;
2226 Lisp_Object *args;
2227{
2228 Lisp_Object sym;
2229 Lisp_Object val;
fada05d6
KH
2230 Lisp_Object globals;
2231 struct gcpro gcpro1, gcpro2, gcpro3;
c933ea05
RS
2232
2233 sym = args[0];
fada05d6
KH
2234 globals = Qnil;
2235 GCPRO3 (sym, val, globals);
c933ea05 2236
03699b14 2237 for (val = funlist; CONSP (val); val = XCDR (val))
c933ea05 2238 {
03699b14 2239 if (EQ (XCAR (val), Qt))
c933ea05
RS
2240 {
2241 /* t indicates this hook has a local binding;
2242 it means to run the global binding too. */
c933ea05
RS
2243
2244 for (globals = Fdefault_value (sym);
2245 CONSP (globals);
03699b14 2246 globals = XCDR (globals))
c933ea05 2247 {
03699b14 2248 args[0] = XCAR (globals);
77d92e05
RS
2249 /* In a global value, t should not occur. If it does, we
2250 must ignore it to avoid an endless loop. */
2251 if (!EQ (args[0], Qt))
2252 Ffuncall (nargs, args);
c933ea05
RS
2253 }
2254 }
2255 else
2256 {
03699b14 2257 args[0] = XCAR (val);
c933ea05
RS
2258 Ffuncall (nargs, args);
2259 }
2260 }
2261 UNGCPRO;
2262 return Qnil;
2263}
7d48558f
RS
2264
2265/* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2266
2267void
2268run_hook_with_args_2 (hook, arg1, arg2)
2269 Lisp_Object hook, arg1, arg2;
2270{
2271 Lisp_Object temp[3];
2272 temp[0] = hook;
2273 temp[1] = arg1;
2274 temp[2] = arg2;
2275
2276 Frun_hook_with_args (3, temp);
2277}
ff936e53 2278\f
db9f0278
JB
2279/* Apply fn to arg */
2280Lisp_Object
2281apply1 (fn, arg)
2282 Lisp_Object fn, arg;
2283{
a6e3fa71
JB
2284 struct gcpro gcpro1;
2285
2286 GCPRO1 (fn);
265a9e55 2287 if (NILP (arg))
a6e3fa71
JB
2288 RETURN_UNGCPRO (Ffuncall (1, &fn));
2289 gcpro1.nvars = 2;
db9f0278
JB
2290#ifdef NO_ARG_ARRAY
2291 {
2292 Lisp_Object args[2];
2293 args[0] = fn;
2294 args[1] = arg;
a6e3fa71
JB
2295 gcpro1.var = args;
2296 RETURN_UNGCPRO (Fapply (2, args));
db9f0278
JB
2297 }
2298#else /* not NO_ARG_ARRAY */
a6e3fa71 2299 RETURN_UNGCPRO (Fapply (2, &fn));
db9f0278
JB
2300#endif /* not NO_ARG_ARRAY */
2301}
2302
2303/* Call function fn on no arguments */
2304Lisp_Object
2305call0 (fn)
2306 Lisp_Object fn;
2307{
a6e3fa71
JB
2308 struct gcpro gcpro1;
2309
2310 GCPRO1 (fn);
2311 RETURN_UNGCPRO (Ffuncall (1, &fn));
db9f0278
JB
2312}
2313
15285f9f 2314/* Call function fn with 1 argument arg1 */
db9f0278
JB
2315/* ARGSUSED */
2316Lisp_Object
15285f9f
RS
2317call1 (fn, arg1)
2318 Lisp_Object fn, arg1;
db9f0278 2319{
a6e3fa71 2320 struct gcpro gcpro1;
db9f0278 2321#ifdef NO_ARG_ARRAY
a6e3fa71
JB
2322 Lisp_Object args[2];
2323
db9f0278 2324 args[0] = fn;
15285f9f 2325 args[1] = arg1;
a6e3fa71
JB
2326 GCPRO1 (args[0]);
2327 gcpro1.nvars = 2;
2328 RETURN_UNGCPRO (Ffuncall (2, args));
db9f0278 2329#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
2330 GCPRO1 (fn);
2331 gcpro1.nvars = 2;
2332 RETURN_UNGCPRO (Ffuncall (2, &fn));
db9f0278
JB
2333#endif /* not NO_ARG_ARRAY */
2334}
2335
15285f9f 2336/* Call function fn with 2 arguments arg1, arg2 */
db9f0278
JB
2337/* ARGSUSED */
2338Lisp_Object
15285f9f
RS
2339call2 (fn, arg1, arg2)
2340 Lisp_Object fn, arg1, arg2;
db9f0278 2341{
a6e3fa71 2342 struct gcpro gcpro1;
db9f0278
JB
2343#ifdef NO_ARG_ARRAY
2344 Lisp_Object args[3];
2345 args[0] = fn;
15285f9f
RS
2346 args[1] = arg1;
2347 args[2] = arg2;
a6e3fa71
JB
2348 GCPRO1 (args[0]);
2349 gcpro1.nvars = 3;
2350 RETURN_UNGCPRO (Ffuncall (3, args));
db9f0278 2351#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
2352 GCPRO1 (fn);
2353 gcpro1.nvars = 3;
2354 RETURN_UNGCPRO (Ffuncall (3, &fn));
db9f0278
JB
2355#endif /* not NO_ARG_ARRAY */
2356}
2357
15285f9f 2358/* Call function fn with 3 arguments arg1, arg2, arg3 */
db9f0278
JB
2359/* ARGSUSED */
2360Lisp_Object
15285f9f
RS
2361call3 (fn, arg1, arg2, arg3)
2362 Lisp_Object fn, arg1, arg2, arg3;
db9f0278 2363{
a6e3fa71 2364 struct gcpro gcpro1;
db9f0278
JB
2365#ifdef NO_ARG_ARRAY
2366 Lisp_Object args[4];
2367 args[0] = fn;
15285f9f
RS
2368 args[1] = arg1;
2369 args[2] = arg2;
2370 args[3] = arg3;
a6e3fa71
JB
2371 GCPRO1 (args[0]);
2372 gcpro1.nvars = 4;
2373 RETURN_UNGCPRO (Ffuncall (4, args));
db9f0278 2374#else /* not NO_ARG_ARRAY */
a6e3fa71
JB
2375 GCPRO1 (fn);
2376 gcpro1.nvars = 4;
2377 RETURN_UNGCPRO (Ffuncall (4, &fn));
db9f0278
JB
2378#endif /* not NO_ARG_ARRAY */
2379}
2380
15285f9f 2381/* Call function fn with 4 arguments arg1, arg2, arg3, arg4 */
a5a44b91
JB
2382/* ARGSUSED */
2383Lisp_Object
15285f9f
RS
2384call4 (fn, arg1, arg2, arg3, arg4)
2385 Lisp_Object fn, arg1, arg2, arg3, arg4;
a5a44b91
JB
2386{
2387 struct gcpro gcpro1;
2388#ifdef NO_ARG_ARRAY
2389 Lisp_Object args[5];
2390 args[0] = fn;
15285f9f
RS
2391 args[1] = arg1;
2392 args[2] = arg2;
2393 args[3] = arg3;
2394 args[4] = arg4;
a5a44b91
JB
2395 GCPRO1 (args[0]);
2396 gcpro1.nvars = 5;
2397 RETURN_UNGCPRO (Ffuncall (5, args));
2398#else /* not NO_ARG_ARRAY */
2399 GCPRO1 (fn);
2400 gcpro1.nvars = 5;
2401 RETURN_UNGCPRO (Ffuncall (5, &fn));
2402#endif /* not NO_ARG_ARRAY */
2403}
2404
15285f9f
RS
2405/* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5 */
2406/* ARGSUSED */
2407Lisp_Object
2408call5 (fn, arg1, arg2, arg3, arg4, arg5)
2409 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5;
2410{
2411 struct gcpro gcpro1;
2412#ifdef NO_ARG_ARRAY
2413 Lisp_Object args[6];
2414 args[0] = fn;
2415 args[1] = arg1;
2416 args[2] = arg2;
2417 args[3] = arg3;
2418 args[4] = arg4;
2419 args[5] = arg5;
2420 GCPRO1 (args[0]);
2421 gcpro1.nvars = 6;
2422 RETURN_UNGCPRO (Ffuncall (6, args));
2423#else /* not NO_ARG_ARRAY */
2424 GCPRO1 (fn);
2425 gcpro1.nvars = 6;
2426 RETURN_UNGCPRO (Ffuncall (6, &fn));
2427#endif /* not NO_ARG_ARRAY */
2428}
2429
2430/* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6 */
2431/* ARGSUSED */
2432Lisp_Object
2433call6 (fn, arg1, arg2, arg3, arg4, arg5, arg6)
2434 Lisp_Object fn, arg1, arg2, arg3, arg4, arg5, arg6;
2435{
2436 struct gcpro gcpro1;
2437#ifdef NO_ARG_ARRAY
2438 Lisp_Object args[7];
2439 args[0] = fn;
2440 args[1] = arg1;
2441 args[2] = arg2;
2442 args[3] = arg3;
2443 args[4] = arg4;
2444 args[5] = arg5;
2445 args[6] = arg6;
2446 GCPRO1 (args[0]);
2447 gcpro1.nvars = 7;
2448 RETURN_UNGCPRO (Ffuncall (7, args));
2449#else /* not NO_ARG_ARRAY */
2450 GCPRO1 (fn);
2451 gcpro1.nvars = 7;
2452 RETURN_UNGCPRO (Ffuncall (7, &fn));
2453#endif /* not NO_ARG_ARRAY */
2454}
2455
db9f0278
JB
2456DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2457 "Call first argument as a function, passing remaining arguments to it.\n\
fd7fe9a1 2458Return the value that function returns.\n\
db9f0278
JB
2459Thus, (funcall 'cons 'x 'y) returns (x . y).")
2460 (nargs, args)
2461 int nargs;
2462 Lisp_Object *args;
2463{
2464 Lisp_Object fun;
2465 Lisp_Object funcar;
2466 int numargs = nargs - 1;
2467 Lisp_Object lisp_numargs;
2468 Lisp_Object val;
2469 struct backtrace backtrace;
2470 register Lisp_Object *internal_args;
2471 register int i;
2472
2473 QUIT;
2474 if (consing_since_gc > gc_cons_threshold)
a6e3fa71 2475 Fgarbage_collect ();
db9f0278
JB
2476
2477 if (++lisp_eval_depth > max_lisp_eval_depth)
2478 {
2479 if (max_lisp_eval_depth < 100)
2480 max_lisp_eval_depth = 100;
2481 if (lisp_eval_depth > max_lisp_eval_depth)
2482 error ("Lisp nesting exceeds max-lisp-eval-depth");
2483 }
2484
2485 backtrace.next = backtrace_list;
2486 backtrace_list = &backtrace;
2487 backtrace.function = &args[0];
2488 backtrace.args = &args[1];
2489 backtrace.nargs = nargs - 1;
2490 backtrace.evalargs = 0;
2491 backtrace.debug_on_exit = 0;
2492
2493 if (debug_on_next_call)
2494 do_debug_on_call (Qlambda);
2495
2496 retry:
2497
2498 fun = args[0];
ffd56f97
JB
2499
2500 fun = Findirect_function (fun);
db9f0278 2501
90165123 2502 if (SUBRP (fun))
db9f0278
JB
2503 {
2504 if (numargs < XSUBR (fun)->min_args
2505 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2506 {
a631e24c 2507 XSETFASTINT (lisp_numargs, numargs);
db9f0278
JB
2508 return Fsignal (Qwrong_number_of_arguments, Fcons (fun, Fcons (lisp_numargs, Qnil)));
2509 }
2510
2511 if (XSUBR (fun)->max_args == UNEVALLED)
2512 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2513
2514 if (XSUBR (fun)->max_args == MANY)
2515 {
2516 val = (*XSUBR (fun)->function) (numargs, args + 1);
2517 goto done;
2518 }
2519
2520 if (XSUBR (fun)->max_args > numargs)
2521 {
2522 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2523 bcopy (args + 1, internal_args, numargs * sizeof (Lisp_Object));
2524 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2525 internal_args[i] = Qnil;
2526 }
2527 else
2528 internal_args = args + 1;
2529 switch (XSUBR (fun)->max_args)
2530 {
2531 case 0:
2532 val = (*XSUBR (fun)->function) ();
2533 goto done;
2534 case 1:
2535 val = (*XSUBR (fun)->function) (internal_args[0]);
2536 goto done;
2537 case 2:
2538 val = (*XSUBR (fun)->function) (internal_args[0],
2539 internal_args[1]);
2540 goto done;
2541 case 3:
2542 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2543 internal_args[2]);
2544 goto done;
2545 case 4:
2546 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2547 internal_args[2],
2548 internal_args[3]);
2549 goto done;
2550 case 5:
2551 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2552 internal_args[2], internal_args[3],
2553 internal_args[4]);
2554 goto done;
2555 case 6:
2556 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2557 internal_args[2], internal_args[3],
2558 internal_args[4], internal_args[5]);
2559 goto done;
15c65264
RS
2560 case 7:
2561 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2562 internal_args[2], internal_args[3],
2563 internal_args[4], internal_args[5],
2564 internal_args[6]);
2565 goto done;
db9f0278 2566
166c822d
KH
2567 case 8:
2568 val = (*XSUBR (fun)->function) (internal_args[0], internal_args[1],
2569 internal_args[2], internal_args[3],
2570 internal_args[4], internal_args[5],
2571 internal_args[6], internal_args[7]);
2572 goto done;
2573
db9f0278 2574 default:
70ee42f7 2575
166c822d 2576 /* If a subr takes more than 8 arguments without using MANY
70ee42f7
JB
2577 or UNEVALLED, we need to extend this function to support it.
2578 Until this is done, there is no way to call the function. */
2579 abort ();
db9f0278
JB
2580 }
2581 }
90165123 2582 if (COMPILEDP (fun))
db9f0278
JB
2583 val = funcall_lambda (fun, numargs, args + 1);
2584 else
2585 {
2586 if (!CONSP (fun))
2587 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2588 funcar = Fcar (fun);
90165123 2589 if (!SYMBOLP (funcar))
db9f0278
JB
2590 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2591 if (EQ (funcar, Qlambda))
2592 val = funcall_lambda (fun, numargs, args + 1);
2593 else if (EQ (funcar, Qmocklisp))
2594 val = ml_apply (fun, Flist (numargs, args + 1));
2595 else if (EQ (funcar, Qautoload))
2596 {
2597 do_autoload (fun, args[0]);
2598 goto retry;
2599 }
2600 else
2601 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2602 }
2603 done:
2604 lisp_eval_depth--;
2605 if (backtrace.debug_on_exit)
2606 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
2607 backtrace_list = backtrace.next;
2608 return val;
2609}
2610\f
2611Lisp_Object
2612apply_lambda (fun, args, eval_flag)
2613 Lisp_Object fun, args;
2614 int eval_flag;
2615{
2616 Lisp_Object args_left;
2617 Lisp_Object numargs;
2618 register Lisp_Object *arg_vector;
2619 struct gcpro gcpro1, gcpro2, gcpro3;
2620 register int i;
2621 register Lisp_Object tem;
2622
2623 numargs = Flength (args);
2624 arg_vector = (Lisp_Object *) alloca (XINT (numargs) * sizeof (Lisp_Object));
2625 args_left = args;
2626
2627 GCPRO3 (*arg_vector, args_left, fun);
2628 gcpro1.nvars = 0;
2629
2630 for (i = 0; i < XINT (numargs);)
2631 {
2632 tem = Fcar (args_left), args_left = Fcdr (args_left);
2633 if (eval_flag) tem = Feval (tem);
2634 arg_vector[i++] = tem;
2635 gcpro1.nvars = i;
2636 }
2637
2638 UNGCPRO;
2639
2640 if (eval_flag)
2641 {
2642 backtrace_list->args = arg_vector;
2643 backtrace_list->nargs = i;
2644 }
2645 backtrace_list->evalargs = 0;
2646 tem = funcall_lambda (fun, XINT (numargs), arg_vector);
2647
2648 /* Do the debug-on-exit now, while arg_vector still exists. */
2649 if (backtrace_list->debug_on_exit)
2650 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
2651 /* Don't do it again when we return to eval. */
2652 backtrace_list->debug_on_exit = 0;
2653 return tem;
2654}
2655
2656/* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
2657 and return the result of evaluation.
2658 FUN must be either a lambda-expression or a compiled-code object. */
2659
2660Lisp_Object
2661funcall_lambda (fun, nargs, arg_vector)
2662 Lisp_Object fun;
2663 int nargs;
2664 register Lisp_Object *arg_vector;
2665{
9ab90667 2666 Lisp_Object val, syms_left, next;
db9f0278 2667 int count = specpdl_ptr - specpdl;
9ab90667 2668 int i, optional, rest;
db9f0278 2669
9ab90667
GM
2670 if (NILP (Vmocklisp_arguments))
2671 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
db9f0278 2672
90165123 2673 if (CONSP (fun))
9ab90667
GM
2674 {
2675 syms_left = XCDR (fun);
2676 if (CONSP (syms_left))
2677 syms_left = XCAR (syms_left);
2678 else
2679 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2680 }
90165123 2681 else if (COMPILEDP (fun))
db9f0278 2682 syms_left = XVECTOR (fun)->contents[COMPILED_ARGLIST];
9ab90667
GM
2683 else
2684 abort ();
db9f0278 2685
9ab90667
GM
2686 i = optional = rest = 0;
2687 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
db9f0278
JB
2688 {
2689 QUIT;
9ab90667
GM
2690
2691 next = XCAR (syms_left);
90165123 2692 while (!SYMBOLP (next))
9ffa21d4 2693 next = Fsignal (Qinvalid_function, Fcons (fun, Qnil));
9ab90667 2694
db9f0278
JB
2695 if (EQ (next, Qand_rest))
2696 rest = 1;
2697 else if (EQ (next, Qand_optional))
2698 optional = 1;
2699 else if (rest)
2700 {
9ffa21d4 2701 specbind (next, Flist (nargs - i, &arg_vector[i]));
db9f0278
JB
2702 i = nargs;
2703 }
2704 else if (i < nargs)
9ab90667 2705 specbind (next, arg_vector[i++]);
db9f0278 2706 else if (!optional)
9ab90667
GM
2707 return Fsignal (Qwrong_number_of_arguments,
2708 Fcons (fun, Fcons (make_number (nargs), Qnil)));
db9f0278
JB
2709 else
2710 specbind (next, Qnil);
2711 }
2712
9ab90667
GM
2713 if (!NILP (syms_left))
2714 return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
2715 else if (i < nargs)
2716 return Fsignal (Qwrong_number_of_arguments,
2717 Fcons (fun, Fcons (make_number (nargs), Qnil)));
db9f0278 2718
90165123 2719 if (CONSP (fun))
9ab90667 2720 val = Fprogn (XCDR (XCDR (fun)));
db9f0278 2721 else
ca248607
RS
2722 {
2723 /* If we have not actually read the bytecode string
2724 and constants vector yet, fetch them from the file. */
2725 if (CONSP (XVECTOR (fun)->contents[COMPILED_BYTECODE]))
661c7d6e 2726 Ffetch_bytecode (fun);
ca248607
RS
2727 val = Fbyte_code (XVECTOR (fun)->contents[COMPILED_BYTECODE],
2728 XVECTOR (fun)->contents[COMPILED_CONSTANTS],
2729 XVECTOR (fun)->contents[COMPILED_STACK_DEPTH]);
2730 }
9ab90667 2731
db9f0278
JB
2732 return unbind_to (count, val);
2733}
661c7d6e
KH
2734
2735DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
2736 1, 1, 0,
2737 "If byte-compiled OBJECT is lazy-loaded, fetch it now.")
2738 (object)
2739 Lisp_Object object;
2740{
2741 Lisp_Object tem;
2742
2743 if (COMPILEDP (object)
2744 && CONSP (XVECTOR (object)->contents[COMPILED_BYTECODE]))
2745 {
2746 tem = read_doc_string (XVECTOR (object)->contents[COMPILED_BYTECODE]);
5bbdb090
RS
2747 if (!CONSP (tem))
2748 error ("invalid byte code");
03699b14
KR
2749 XVECTOR (object)->contents[COMPILED_BYTECODE] = XCAR (tem);
2750 XVECTOR (object)->contents[COMPILED_CONSTANTS] = XCDR (tem);
661c7d6e
KH
2751 }
2752 return object;
2753}
db9f0278
JB
2754\f
2755void
2756grow_specpdl ()
2757{
2758 register int count = specpdl_ptr - specpdl;
2759 if (specpdl_size >= max_specpdl_size)
2760 {
2761 if (max_specpdl_size < 400)
2762 max_specpdl_size = 400;
2763 if (specpdl_size >= max_specpdl_size)
2764 {
debee8fe
RS
2765 if (!NILP (Vdebug_on_error))
2766 /* Leave room for some specpdl in the debugger. */
2767 max_specpdl_size = specpdl_size + 100;
db9f0278
JB
2768 Fsignal (Qerror,
2769 Fcons (build_string ("Variable binding depth exceeds max-specpdl-size"), Qnil));
db9f0278
JB
2770 }
2771 }
2772 specpdl_size *= 2;
2773 if (specpdl_size > max_specpdl_size)
2774 specpdl_size = max_specpdl_size;
2775 specpdl = (struct specbinding *) xrealloc (specpdl, specpdl_size * sizeof (struct specbinding));
2776 specpdl_ptr = specpdl + count;
2777}
2778
2779void
2780specbind (symbol, value)
2781 Lisp_Object symbol, value;
2782{
db9f0278
JB
2783 Lisp_Object ovalue;
2784
9ffa21d4 2785 CHECK_SYMBOL (symbol, 0);
db9f0278
JB
2786 if (specpdl_ptr == specpdl + specpdl_size)
2787 grow_specpdl ();
719177b3 2788
9ab90667
GM
2789 /* The most common case is that a non-constant symbol with a trivial
2790 value. Make that as fast as we can. */
2791 if (!MISCP (XSYMBOL (symbol)->value)
2792 && !EQ (symbol, Qnil)
2793 && !EQ (symbol, Qt)
2794 && !(XSYMBOL (symbol)->name->data[0] == ':'
2795 && EQ (XSYMBOL (symbol)->obarray, initial_obarray)
9ab90667 2796 && !EQ (value, symbol)))
719177b3 2797 {
9ab90667
GM
2798 specpdl_ptr->symbol = symbol;
2799 specpdl_ptr->old_value = XSYMBOL (symbol)->value;
2800 specpdl_ptr->func = NULL;
2801 ++specpdl_ptr;
2802 XSYMBOL (symbol)->value = value;
719177b3
RS
2803 }
2804 else
9ab90667
GM
2805 {
2806 ovalue = find_symbol_value (symbol);
2807 specpdl_ptr->func = 0;
2808 specpdl_ptr->old_value = ovalue;
719177b3 2809
9ab90667
GM
2810 if (BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2811 || SOME_BUFFER_LOCAL_VALUEP (XSYMBOL (symbol)->value)
2812 || BUFFER_OBJFWDP (XSYMBOL (symbol)->value))
2813 {
2814 Lisp_Object current_buffer, binding_buffer;
2815 /* For a local variable, record both the symbol and which
2816 buffer's value we are saving. */
2817 current_buffer = Fcurrent_buffer ();
2818 binding_buffer = current_buffer;
2819 /* If the variable is not local in this buffer,
2820 we are saving the global value, so restore that. */
2821 if (NILP (Flocal_variable_p (symbol, binding_buffer)))
2822 binding_buffer = Qnil;
2823 specpdl_ptr->symbol
2824 = Fcons (symbol, Fcons (binding_buffer, current_buffer));
2825 }
2826 else
2827 specpdl_ptr->symbol = symbol;
2828
2829 specpdl_ptr++;
2830 if (BUFFER_OBJFWDP (ovalue) || KBOARD_OBJFWDP (ovalue))
2831 store_symval_forwarding (symbol, ovalue, value);
2832 else
2833 set_internal (symbol, value, 0, 1);
2834 }
db9f0278
JB
2835}
2836
2837void
2838record_unwind_protect (function, arg)
1d159538 2839 Lisp_Object (*function) P_ ((Lisp_Object));
db9f0278
JB
2840 Lisp_Object arg;
2841{
2842 if (specpdl_ptr == specpdl + specpdl_size)
2843 grow_specpdl ();
2844 specpdl_ptr->func = function;
2845 specpdl_ptr->symbol = Qnil;
2846 specpdl_ptr->old_value = arg;
2847 specpdl_ptr++;
2848}
2849
2850Lisp_Object
2851unbind_to (count, value)
2852 int count;
2853 Lisp_Object value;
2854{
265a9e55 2855 int quitf = !NILP (Vquit_flag);
db9f0278
JB
2856 struct gcpro gcpro1;
2857
2858 GCPRO1 (value);
db9f0278
JB
2859 Vquit_flag = Qnil;
2860
2861 while (specpdl_ptr != specpdl + count)
2862 {
2863 --specpdl_ptr;
9ab90667 2864
db9f0278
JB
2865 if (specpdl_ptr->func != 0)
2866 (*specpdl_ptr->func) (specpdl_ptr->old_value);
2867 /* Note that a "binding" of nil is really an unwind protect,
719177b3 2868 so in that case the "old value" is a list of forms to evaluate. */
265a9e55 2869 else if (NILP (specpdl_ptr->symbol))
db9f0278 2870 Fprogn (specpdl_ptr->old_value);
27a6c729
RS
2871 /* If the symbol is a list, it is really
2872 (SYMBOL BINDING_BUFFER . CURRENT_BUFFER)
2873 and it indicates we bound a variable that has
2874 buffer-local bindings. */
719177b3
RS
2875 else if (CONSP (specpdl_ptr->symbol))
2876 {
2877 Lisp_Object symbol, buffer;
2878
2879 symbol = XCAR (specpdl_ptr->symbol);
27a6c729 2880 buffer = XCAR (XCDR (specpdl_ptr->symbol));
719177b3
RS
2881
2882 /* Handle restoring a default value. */
2883 if (NILP (buffer))
2884 Fset_default (symbol, specpdl_ptr->old_value);
2885 /* Handle restoring a value saved from a live buffer. */
2886 else
2887 set_internal (symbol, specpdl_ptr->old_value, XBUFFER (buffer), 1);
2888 }
db9f0278 2889 else
9ab90667
GM
2890 {
2891 /* If variable has a trivial value (no forwarding), we can
2892 just set it. No need to check for constant symbols here,
2893 since that was already done by specbind. */
2894 if (!MISCP (XSYMBOL (specpdl_ptr->symbol)->value))
2895 XSYMBOL (specpdl_ptr->symbol)->value = specpdl_ptr->old_value;
2896 else
2897 set_internal (specpdl_ptr->symbol, specpdl_ptr->old_value, 0, 1);
2898 }
db9f0278 2899 }
9ab90667
GM
2900
2901 if (NILP (Vquit_flag) && quitf)
2902 Vquit_flag = Qt;
db9f0278
JB
2903
2904 UNGCPRO;
db9f0278
JB
2905 return value;
2906}
2907\f
2908#if 0
2909
2910/* Get the value of symbol's global binding, even if that binding
2911 is not now dynamically visible. */
2912
2913Lisp_Object
2914top_level_value (symbol)
2915 Lisp_Object symbol;
2916{
2917 register struct specbinding *ptr = specpdl;
2918
2919 CHECK_SYMBOL (symbol, 0);
2920 for (; ptr != specpdl_ptr; ptr++)
2921 {
2922 if (EQ (ptr->symbol, symbol))
2923 return ptr->old_value;
2924 }
2925 return Fsymbol_value (symbol);
2926}
2927
2928Lisp_Object
2929top_level_set (symbol, newval)
2930 Lisp_Object symbol, newval;
2931{
2932 register struct specbinding *ptr = specpdl;
2933
2934 CHECK_SYMBOL (symbol, 0);
2935 for (; ptr != specpdl_ptr; ptr++)
2936 {
2937 if (EQ (ptr->symbol, symbol))
2938 {
2939 ptr->old_value = newval;
2940 return newval;
2941 }
2942 }
2943 return Fset (symbol, newval);
2944}
2945
2946#endif /* 0 */
2947\f
2948DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
2949 "Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.\n\
2950The debugger is entered when that frame exits, if the flag is non-nil.")
2951 (level, flag)
2952 Lisp_Object level, flag;
2953{
2954 register struct backtrace *backlist = backtrace_list;
2955 register int i;
2956
2957 CHECK_NUMBER (level, 0);
2958
2959 for (i = 0; backlist && i < XINT (level); i++)
2960 {
2961 backlist = backlist->next;
2962 }
2963
2964 if (backlist)
265a9e55 2965 backlist->debug_on_exit = !NILP (flag);
db9f0278
JB
2966
2967 return flag;
2968}
2969
2970DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
2971 "Print a trace of Lisp function calls currently active.\n\
2972Output stream used is value of `standard-output'.")
2973 ()
2974{
2975 register struct backtrace *backlist = backtrace_list;
2976 register int i;
2977 Lisp_Object tail;
2978 Lisp_Object tem;
2979 extern Lisp_Object Vprint_level;
2980 struct gcpro gcpro1;
2981
a631e24c 2982 XSETFASTINT (Vprint_level, 3);
db9f0278
JB
2983
2984 tail = Qnil;
2985 GCPRO1 (tail);
2986
2987 while (backlist)
2988 {
2989 write_string (backlist->debug_on_exit ? "* " : " ", 2);
2990 if (backlist->nargs == UNEVALLED)
2991 {
2992 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
b6703b02 2993 write_string ("\n", -1);
db9f0278
JB
2994 }
2995 else
2996 {
2997 tem = *backlist->function;
2998 Fprin1 (tem, Qnil); /* This can QUIT */
2999 write_string ("(", -1);
3000 if (backlist->nargs == MANY)
3001 {
3002 for (tail = *backlist->args, i = 0;
265a9e55 3003 !NILP (tail);
db9f0278
JB
3004 tail = Fcdr (tail), i++)
3005 {
3006 if (i) write_string (" ", -1);
3007 Fprin1 (Fcar (tail), Qnil);
3008 }
3009 }
3010 else
3011 {
3012 for (i = 0; i < backlist->nargs; i++)
3013 {
3014 if (i) write_string (" ", -1);
3015 Fprin1 (backlist->args[i], Qnil);
3016 }
3017 }
b6703b02 3018 write_string (")\n", -1);
db9f0278 3019 }
db9f0278
JB
3020 backlist = backlist->next;
3021 }
3022
3023 Vprint_level = Qnil;
3024 UNGCPRO;
3025 return Qnil;
3026}
3027
3028DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "",
79e8bfbf 3029 "Return the function and arguments NFRAMES up from current execution point.\n\
db9f0278
JB
3030If that frame has not evaluated the arguments yet (or is a special form),\n\
3031the value is (nil FUNCTION ARG-FORMS...).\n\
3032If that frame has evaluated its arguments and called its function already,\n\
3033the value is (t FUNCTION ARG-VALUES...).\n\
3034A &rest arg is represented as the tail of the list ARG-VALUES.\n\
3035FUNCTION is whatever was supplied as car of evaluated list,\n\
3036or a lambda expression for macro calls.\n\
79e8bfbf 3037If NFRAMES is more than the number of frames, the value is nil.")
db9f0278
JB
3038 (nframes)
3039 Lisp_Object nframes;
3040{
3041 register struct backtrace *backlist = backtrace_list;
3042 register int i;
3043 Lisp_Object tem;
3044
3045 CHECK_NATNUM (nframes, 0);
3046
3047 /* Find the frame requested. */
b6703b02 3048 for (i = 0; backlist && i < XFASTINT (nframes); i++)
db9f0278
JB
3049 backlist = backlist->next;
3050
3051 if (!backlist)
3052 return Qnil;
3053 if (backlist->nargs == UNEVALLED)
3054 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3055 else
3056 {
3057 if (backlist->nargs == MANY)
3058 tem = *backlist->args;
3059 else
3060 tem = Flist (backlist->nargs, backlist->args);
3061
3062 return Fcons (Qt, Fcons (*backlist->function, tem));
3063 }
3064}
3065\f
dfcf069d 3066void
db9f0278
JB
3067syms_of_eval ()
3068{
3069 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
7eaada67
RS
3070 "*Limit on number of Lisp variable bindings & unwind-protects.\n\
3071If Lisp code tries to make more than this many at once,\n\
3072an error is signaled.");
db9f0278
JB
3073
3074 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
7eaada67 3075 "*Limit on depth in `eval', `apply' and `funcall' before error.\n\
db9f0278
JB
3076This limit is to catch infinite recursions for you before they cause\n\
3077actual stack overflow in C, which would be fatal for Emacs.\n\
3078You can safely make it considerably larger than its default value,\n\
3079if that proves inconveniently small.");
3080
3081 DEFVAR_LISP ("quit-flag", &Vquit_flag,
3082 "Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.\n\
d0b68896 3083Typing C-g sets `quit-flag' non-nil, regardless of `inhibit-quit'.");
db9f0278
JB
3084 Vquit_flag = Qnil;
3085
3086 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
3087 "Non-nil inhibits C-g quitting from happening immediately.\n\
3088Note that `quit-flag' will still be set by typing C-g,\n\
690337b7 3089so a quit will be signaled as soon as `inhibit-quit' is nil.\n\
db9f0278
JB
3090To prevent this happening, set `quit-flag' to nil\n\
3091before making `inhibit-quit' nil.");
3092 Vinhibit_quit = Qnil;
3093
ad236261
JB
3094 Qinhibit_quit = intern ("inhibit-quit");
3095 staticpro (&Qinhibit_quit);
3096
db9f0278
JB
3097 Qautoload = intern ("autoload");
3098 staticpro (&Qautoload);
3099
3100 Qdebug_on_error = intern ("debug-on-error");
3101 staticpro (&Qdebug_on_error);
3102
3103 Qmacro = intern ("macro");
3104 staticpro (&Qmacro);
3105
3106 /* Note that the process handling also uses Qexit, but we don't want
3107 to staticpro it twice, so we just do it here. */
3108 Qexit = intern ("exit");
3109 staticpro (&Qexit);
3110
3111 Qinteractive = intern ("interactive");
3112 staticpro (&Qinteractive);
3113
3114 Qcommandp = intern ("commandp");
3115 staticpro (&Qcommandp);
3116
3117 Qdefun = intern ("defun");
3118 staticpro (&Qdefun);
3119
3120 Qand_rest = intern ("&rest");
3121 staticpro (&Qand_rest);
3122
3123 Qand_optional = intern ("&optional");
3124 staticpro (&Qand_optional);
3125
128c0f66 3126 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
db9f0278 3127 "*Non-nil means automatically display a backtrace buffer\n\
128c0f66
RM
3128after any error that is handled by the editor command loop.\n\
3129If the value is a list, an error only means to display a backtrace\n\
3130if one of its condition symbols appears in the list.");
3131 Vstack_trace_on_error = Qnil;
db9f0278 3132
128c0f66 3133 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
db9f0278
JB
3134 "*Non-nil means enter debugger if an error is signaled.\n\
3135Does not apply to errors handled by `condition-case'.\n\
128c0f66
RM
3136If the value is a list, an error only means to enter the debugger\n\
3137if one of its condition symbols appears in the list.\n\
db9f0278 3138See also variable `debug-on-quit'.");
128c0f66 3139 Vdebug_on_error = Qnil;
db9f0278 3140
fc950e09
KH
3141 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
3142 "*List of errors for which the debugger should not be called.\n\
3143Each element may be a condition-name or a regexp that matches error messages.\n\
3144If any element applies to a given error, that error skips the debugger\n\
3145and just returns to top level.\n\
3146This overrides the variable `debug-on-error'.\n\
3147It does not apply to errors handled by `condition-case'.");
3148 Vdebug_ignored_errors = Qnil;
3149
db9f0278 3150 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
d0b68896 3151 "*Non-nil means enter debugger if quit is signaled (C-g, for example).\n\
1b7d8239 3152Does not apply if quit is handled by a `condition-case'.");
db9f0278
JB
3153 debug_on_quit = 0;
3154
3155 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
3156 "Non-nil means enter debugger before next `eval', `apply' or `funcall'.");
3157
556d7314
GM
3158 DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
3159 "Non-nil means debugger may continue execution.\n\
3160This is nil when the debugger is called under circumstances where it\n\
3161might not be safe to continue.");
dac204bc 3162 debugger_may_continue = 1;
556d7314 3163
db9f0278
JB
3164 DEFVAR_LISP ("debugger", &Vdebugger,
3165 "Function to call to invoke debugger.\n\
3166If due to frame exit, args are `exit' and the value being returned;\n\
3167 this function's value will be returned instead of that.\n\
3168If due to error, args are `error' and a list of the args to `signal'.\n\
3169If due to `apply' or `funcall' entry, one arg, `lambda'.\n\
3170If due to `eval' entry, one arg, t.");
3171 Vdebugger = Qnil;
3172
61ede770
RS
3173 DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
3174 "If non-nil, this is a function for `signal' to call.\n\
3175It receives the same arguments that `signal' was given.\n\
3176The Edebug package uses this to regain control.");
3177 Vsignal_hook_function = Qnil;
3178
db9f0278
JB
3179 Qmocklisp_arguments = intern ("mocklisp-arguments");
3180 staticpro (&Qmocklisp_arguments);
3181 DEFVAR_LISP ("mocklisp-arguments", &Vmocklisp_arguments,
3182 "While in a mocklisp function, the list of its unevaluated args.");
3183 Vmocklisp_arguments = Qt;
3184
57a6e758 3185 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
61ede770
RS
3186 "*Non-nil means call the debugger regardless of condition handlers.\n\
3187Note that `debug-on-error', `debug-on-quit' and friends\n\
3188still determine whether to handle the particular condition.");
57a6e758 3189 Vdebug_on_signal = Qnil;
61ede770 3190
6e6e9f08
RS
3191 Vrun_hooks = intern ("run-hooks");
3192 staticpro (&Vrun_hooks);
db9f0278
JB
3193
3194 staticpro (&Vautoload_queue);
3195 Vautoload_queue = Qnil;
3196
3197 defsubr (&Sor);
3198 defsubr (&Sand);
3199 defsubr (&Sif);
3200 defsubr (&Scond);
3201 defsubr (&Sprogn);
3202 defsubr (&Sprog1);
3203 defsubr (&Sprog2);
3204 defsubr (&Ssetq);
3205 defsubr (&Squote);
3206 defsubr (&Sfunction);
3207 defsubr (&Sdefun);
3208 defsubr (&Sdefmacro);
3209 defsubr (&Sdefvar);
3210 defsubr (&Sdefconst);
3211 defsubr (&Suser_variable_p);
3212 defsubr (&Slet);
3213 defsubr (&SletX);
3214 defsubr (&Swhile);
3215 defsubr (&Smacroexpand);
3216 defsubr (&Scatch);
3217 defsubr (&Sthrow);
3218 defsubr (&Sunwind_protect);
3219 defsubr (&Scondition_case);
3220 defsubr (&Ssignal);
3221 defsubr (&Sinteractive_p);
3222 defsubr (&Scommandp);
3223 defsubr (&Sautoload);
3224 defsubr (&Seval);
3225 defsubr (&Sapply);
3226 defsubr (&Sfuncall);
ff936e53
SM
3227 defsubr (&Srun_hooks);
3228 defsubr (&Srun_hook_with_args);
3229 defsubr (&Srun_hook_with_args_until_success);
3230 defsubr (&Srun_hook_with_args_until_failure);
661c7d6e 3231 defsubr (&Sfetch_bytecode);
db9f0278
JB
3232 defsubr (&Sbacktrace_debug);
3233 defsubr (&Sbacktrace);
3234 defsubr (&Sbacktrace_frame);
3235}