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