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