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