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