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