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