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