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