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