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