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