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