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