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