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