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