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