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