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