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