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