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