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