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