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