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