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