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