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