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