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