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