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