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