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