Doc fixes
[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. The value of HOOK
2555 may be nil, a function, or a list of functions. Call each
2556 function in order with arguments ARGS. The final return value
2557 is unspecified.
2558
2559 Do not use `make-local-variable' to make a hook variable buffer-local.
2560 Instead, use `add-hook' and specify t for the LOCAL argument.
2561 usage: (run-hook-with-args HOOK &rest ARGS) */)
2562 (ptrdiff_t nargs, Lisp_Object *args)
2563 {
2564 return run_hook_with_args (nargs, args, funcall_nil);
2565 }
2566
2567 /* NB this one still documents a specific non-nil return value.
2568 (As did run-hook-with-args and run-hook-with-args-until-failure
2569 until they were changed in 24.1.) */
2570 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2571 Srun_hook_with_args_until_success, 1, MANY, 0,
2572 doc: /* Run HOOK with the specified arguments ARGS.
2573 HOOK should be a symbol, a hook variable. The value of HOOK
2574 may be nil, a function, or a list of functions. Call each
2575 function in order with arguments ARGS, stopping at the first
2576 one that returns non-nil, and return that value. Otherwise (if
2577 all functions return nil, or if there are no functions to call),
2578 return nil.
2579
2580 Do not use `make-local-variable' to make a hook variable buffer-local.
2581 Instead, use `add-hook' and specify t for the LOCAL argument.
2582 usage: (run-hook-with-args-until-success HOOK &rest ARGS) */)
2583 (ptrdiff_t nargs, Lisp_Object *args)
2584 {
2585 return run_hook_with_args (nargs, args, Ffuncall);
2586 }
2587
2588 static Lisp_Object
2589 funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2590 {
2591 return NILP (Ffuncall (nargs, args)) ? Qt : Qnil;
2592 }
2593
2594 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2595 Srun_hook_with_args_until_failure, 1, MANY, 0,
2596 doc: /* Run HOOK with the specified arguments ARGS.
2597 HOOK should be a symbol, a hook variable. The value of HOOK
2598 may be nil, a function, or a list of functions. Call each
2599 function in order with arguments ARGS, stopping at the first
2600 one that returns nil, and return nil. Otherwise (if all functions
2601 return non-nil, or if there are no functions to call), return non-nil
2602 \(do not rely on the precise return value in this case).
2603
2604 Do not use `make-local-variable' to make a hook variable buffer-local.
2605 Instead, use `add-hook' and specify t for the LOCAL argument.
2606 usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */)
2607 (ptrdiff_t nargs, Lisp_Object *args)
2608 {
2609 return NILP (run_hook_with_args (nargs, args, funcall_not)) ? Qt : Qnil;
2610 }
2611
2612 static Lisp_Object
2613 run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
2614 {
2615 Lisp_Object tmp = args[0], ret;
2616 args[0] = args[1];
2617 args[1] = tmp;
2618 ret = Ffuncall (nargs, args);
2619 args[1] = args[0];
2620 args[0] = tmp;
2621 return ret;
2622 }
2623
2624 DEFUN ("run-hook-wrapped", Frun_hook_wrapped, Srun_hook_wrapped, 2, MANY, 0,
2625 doc: /* Run HOOK, passing each function through WRAP-FUNCTION.
2626 I.e. instead of calling each function FUN directly with arguments ARGS,
2627 it calls WRAP-FUNCTION with arguments FUN and ARGS.
2628 As soon as a call to WRAP-FUNCTION returns non-nil, `run-hook-wrapped'
2629 aborts and returns that value.
2630 usage: (run-hook-wrapped HOOK WRAP-FUNCTION &rest ARGS) */)
2631 (ptrdiff_t nargs, Lisp_Object *args)
2632 {
2633 return run_hook_with_args (nargs, args, run_hook_wrapped_funcall);
2634 }
2635
2636 /* ARGS[0] should be a hook symbol.
2637 Call each of the functions in the hook value, passing each of them
2638 as arguments all the rest of ARGS (all NARGS - 1 elements).
2639 FUNCALL specifies how to call each function on the hook.
2640 The caller (or its caller, etc) must gcpro all of ARGS,
2641 except that it isn't necessary to gcpro ARGS[0]. */
2642
2643 Lisp_Object
2644 run_hook_with_args (ptrdiff_t nargs, Lisp_Object *args,
2645 Lisp_Object (*funcall) (ptrdiff_t nargs, Lisp_Object *args))
2646 {
2647 Lisp_Object sym, val, ret = Qnil;
2648 struct gcpro gcpro1, gcpro2, gcpro3;
2649
2650 /* If we are dying or still initializing,
2651 don't do anything--it would probably crash if we tried. */
2652 if (NILP (Vrun_hooks))
2653 return Qnil;
2654
2655 sym = args[0];
2656 val = find_symbol_value (sym);
2657
2658 if (EQ (val, Qunbound) || NILP (val))
2659 return ret;
2660 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2661 {
2662 args[0] = val;
2663 return funcall (nargs, args);
2664 }
2665 else
2666 {
2667 Lisp_Object global_vals = Qnil;
2668 GCPRO3 (sym, val, global_vals);
2669
2670 for (;
2671 CONSP (val) && NILP (ret);
2672 val = XCDR (val))
2673 {
2674 if (EQ (XCAR (val), Qt))
2675 {
2676 /* t indicates this hook has a local binding;
2677 it means to run the global binding too. */
2678 global_vals = Fdefault_value (sym);
2679 if (NILP (global_vals)) continue;
2680
2681 if (!CONSP (global_vals) || EQ (XCAR (global_vals), Qlambda))
2682 {
2683 args[0] = global_vals;
2684 ret = funcall (nargs, args);
2685 }
2686 else
2687 {
2688 for (;
2689 CONSP (global_vals) && NILP (ret);
2690 global_vals = XCDR (global_vals))
2691 {
2692 args[0] = XCAR (global_vals);
2693 /* In a global value, t should not occur. If it does, we
2694 must ignore it to avoid an endless loop. */
2695 if (!EQ (args[0], Qt))
2696 ret = funcall (nargs, args);
2697 }
2698 }
2699 }
2700 else
2701 {
2702 args[0] = XCAR (val);
2703 ret = funcall (nargs, args);
2704 }
2705 }
2706
2707 UNGCPRO;
2708 return ret;
2709 }
2710 }
2711
2712 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2. */
2713
2714 void
2715 run_hook_with_args_2 (Lisp_Object hook, Lisp_Object arg1, Lisp_Object arg2)
2716 {
2717 Lisp_Object temp[3];
2718 temp[0] = hook;
2719 temp[1] = arg1;
2720 temp[2] = arg2;
2721
2722 Frun_hook_with_args (3, temp);
2723 }
2724 \f
2725 /* Apply fn to arg. */
2726 Lisp_Object
2727 apply1 (Lisp_Object fn, Lisp_Object arg)
2728 {
2729 struct gcpro gcpro1;
2730
2731 GCPRO1 (fn);
2732 if (NILP (arg))
2733 RETURN_UNGCPRO (Ffuncall (1, &fn));
2734 gcpro1.nvars = 2;
2735 {
2736 Lisp_Object args[2];
2737 args[0] = fn;
2738 args[1] = arg;
2739 gcpro1.var = args;
2740 RETURN_UNGCPRO (Fapply (2, args));
2741 }
2742 }
2743
2744 /* Call function fn on no arguments. */
2745 Lisp_Object
2746 call0 (Lisp_Object fn)
2747 {
2748 struct gcpro gcpro1;
2749
2750 GCPRO1 (fn);
2751 RETURN_UNGCPRO (Ffuncall (1, &fn));
2752 }
2753
2754 /* Call function fn with 1 argument arg1. */
2755 /* ARGSUSED */
2756 Lisp_Object
2757 call1 (Lisp_Object fn, Lisp_Object arg1)
2758 {
2759 struct gcpro gcpro1;
2760 Lisp_Object args[2];
2761
2762 args[0] = fn;
2763 args[1] = arg1;
2764 GCPRO1 (args[0]);
2765 gcpro1.nvars = 2;
2766 RETURN_UNGCPRO (Ffuncall (2, args));
2767 }
2768
2769 /* Call function fn with 2 arguments arg1, arg2. */
2770 /* ARGSUSED */
2771 Lisp_Object
2772 call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2773 {
2774 struct gcpro gcpro1;
2775 Lisp_Object args[3];
2776 args[0] = fn;
2777 args[1] = arg1;
2778 args[2] = arg2;
2779 GCPRO1 (args[0]);
2780 gcpro1.nvars = 3;
2781 RETURN_UNGCPRO (Ffuncall (3, args));
2782 }
2783
2784 /* Call function fn with 3 arguments arg1, arg2, arg3. */
2785 /* ARGSUSED */
2786 Lisp_Object
2787 call3 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3)
2788 {
2789 struct gcpro gcpro1;
2790 Lisp_Object args[4];
2791 args[0] = fn;
2792 args[1] = arg1;
2793 args[2] = arg2;
2794 args[3] = arg3;
2795 GCPRO1 (args[0]);
2796 gcpro1.nvars = 4;
2797 RETURN_UNGCPRO (Ffuncall (4, args));
2798 }
2799
2800 /* Call function fn with 4 arguments arg1, arg2, arg3, arg4. */
2801 /* ARGSUSED */
2802 Lisp_Object
2803 call4 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2804 Lisp_Object arg4)
2805 {
2806 struct gcpro gcpro1;
2807 Lisp_Object args[5];
2808 args[0] = fn;
2809 args[1] = arg1;
2810 args[2] = arg2;
2811 args[3] = arg3;
2812 args[4] = arg4;
2813 GCPRO1 (args[0]);
2814 gcpro1.nvars = 5;
2815 RETURN_UNGCPRO (Ffuncall (5, args));
2816 }
2817
2818 /* Call function fn with 5 arguments arg1, arg2, arg3, arg4, arg5. */
2819 /* ARGSUSED */
2820 Lisp_Object
2821 call5 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2822 Lisp_Object arg4, Lisp_Object arg5)
2823 {
2824 struct gcpro gcpro1;
2825 Lisp_Object args[6];
2826 args[0] = fn;
2827 args[1] = arg1;
2828 args[2] = arg2;
2829 args[3] = arg3;
2830 args[4] = arg4;
2831 args[5] = arg5;
2832 GCPRO1 (args[0]);
2833 gcpro1.nvars = 6;
2834 RETURN_UNGCPRO (Ffuncall (6, args));
2835 }
2836
2837 /* Call function fn with 6 arguments arg1, arg2, arg3, arg4, arg5, arg6. */
2838 /* ARGSUSED */
2839 Lisp_Object
2840 call6 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2841 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6)
2842 {
2843 struct gcpro gcpro1;
2844 Lisp_Object args[7];
2845 args[0] = fn;
2846 args[1] = arg1;
2847 args[2] = arg2;
2848 args[3] = arg3;
2849 args[4] = arg4;
2850 args[5] = arg5;
2851 args[6] = arg6;
2852 GCPRO1 (args[0]);
2853 gcpro1.nvars = 7;
2854 RETURN_UNGCPRO (Ffuncall (7, args));
2855 }
2856
2857 /* Call function fn with 7 arguments arg1, arg2, arg3, arg4, arg5, arg6, arg7. */
2858 /* ARGSUSED */
2859 Lisp_Object
2860 call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
2861 Lisp_Object arg4, Lisp_Object arg5, Lisp_Object arg6, Lisp_Object arg7)
2862 {
2863 struct gcpro gcpro1;
2864 Lisp_Object args[8];
2865 args[0] = fn;
2866 args[1] = arg1;
2867 args[2] = arg2;
2868 args[3] = arg3;
2869 args[4] = arg4;
2870 args[5] = arg5;
2871 args[6] = arg6;
2872 args[7] = arg7;
2873 GCPRO1 (args[0]);
2874 gcpro1.nvars = 8;
2875 RETURN_UNGCPRO (Ffuncall (8, args));
2876 }
2877
2878 /* The caller should GCPRO all the elements of ARGS. */
2879
2880 DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2881 doc: /* Non-nil if OBJECT is a function. */)
2882 (Lisp_Object object)
2883 {
2884 if (SYMBOLP (object) && !NILP (Ffboundp (object)))
2885 {
2886 object = Findirect_function (object, Qt);
2887
2888 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2889 {
2890 /* Autoloaded symbols are functions, except if they load
2891 macros or keymaps. */
2892 int i;
2893 for (i = 0; i < 4 && CONSP (object); i++)
2894 object = XCDR (object);
2895
2896 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2897 }
2898 }
2899
2900 if (SUBRP (object))
2901 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2902 else if (COMPILEDP (object))
2903 return Qt;
2904 else if (CONSP (object))
2905 {
2906 Lisp_Object car = XCAR (object);
2907 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2908 }
2909 else
2910 return Qnil;
2911 }
2912
2913 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
2914 doc: /* Call first argument as a function, passing remaining arguments to it.
2915 Return the value that function returns.
2916 Thus, (funcall 'cons 'x 'y) returns (x . y).
2917 usage: (funcall FUNCTION &rest ARGUMENTS) */)
2918 (ptrdiff_t nargs, Lisp_Object *args)
2919 {
2920 Lisp_Object fun, original_fun;
2921 Lisp_Object funcar;
2922 ptrdiff_t numargs = nargs - 1;
2923 Lisp_Object lisp_numargs;
2924 Lisp_Object val;
2925 struct backtrace backtrace;
2926 register Lisp_Object *internal_args;
2927 ptrdiff_t i;
2928
2929 QUIT;
2930 if ((consing_since_gc > gc_cons_threshold
2931 && consing_since_gc > gc_relative_threshold)
2932 ||
2933 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2934 Fgarbage_collect ();
2935
2936 if (++lisp_eval_depth > max_lisp_eval_depth)
2937 {
2938 if (max_lisp_eval_depth < 100)
2939 max_lisp_eval_depth = 100;
2940 if (lisp_eval_depth > max_lisp_eval_depth)
2941 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2942 }
2943
2944 backtrace.next = backtrace_list;
2945 backtrace_list = &backtrace;
2946 backtrace.function = &args[0];
2947 backtrace.args = &args[1];
2948 backtrace.nargs = nargs - 1;
2949 backtrace.debug_on_exit = 0;
2950
2951 if (debug_on_next_call)
2952 do_debug_on_call (Qlambda);
2953
2954 CHECK_CONS_LIST ();
2955
2956 original_fun = args[0];
2957
2958 retry:
2959
2960 /* Optimize for no indirection. */
2961 fun = original_fun;
2962 if (SYMBOLP (fun) && !EQ (fun, Qunbound)
2963 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2964 fun = indirect_function (fun);
2965
2966 if (SUBRP (fun))
2967 {
2968 if (numargs < XSUBR (fun)->min_args
2969 || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
2970 {
2971 XSETFASTINT (lisp_numargs, numargs);
2972 xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
2973 }
2974
2975 else if (XSUBR (fun)->max_args == UNEVALLED)
2976 xsignal1 (Qinvalid_function, original_fun);
2977
2978 else if (XSUBR (fun)->max_args == MANY)
2979 val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
2980 else
2981 {
2982 if (XSUBR (fun)->max_args > numargs)
2983 {
2984 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
2985 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
2986 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2987 internal_args[i] = Qnil;
2988 }
2989 else
2990 internal_args = args + 1;
2991 switch (XSUBR (fun)->max_args)
2992 {
2993 case 0:
2994 val = (XSUBR (fun)->function.a0 ());
2995 break;
2996 case 1:
2997 val = (XSUBR (fun)->function.a1 (internal_args[0]));
2998 break;
2999 case 2:
3000 val = (XSUBR (fun)->function.a2
3001 (internal_args[0], internal_args[1]));
3002 break;
3003 case 3:
3004 val = (XSUBR (fun)->function.a3
3005 (internal_args[0], internal_args[1], internal_args[2]));
3006 break;
3007 case 4:
3008 val = (XSUBR (fun)->function.a4
3009 (internal_args[0], internal_args[1], internal_args[2],
3010 internal_args[3]));
3011 break;
3012 case 5:
3013 val = (XSUBR (fun)->function.a5
3014 (internal_args[0], internal_args[1], internal_args[2],
3015 internal_args[3], internal_args[4]));
3016 break;
3017 case 6:
3018 val = (XSUBR (fun)->function.a6
3019 (internal_args[0], internal_args[1], internal_args[2],
3020 internal_args[3], internal_args[4], internal_args[5]));
3021 break;
3022 case 7:
3023 val = (XSUBR (fun)->function.a7
3024 (internal_args[0], internal_args[1], internal_args[2],
3025 internal_args[3], internal_args[4], internal_args[5],
3026 internal_args[6]));
3027 break;
3028
3029 case 8:
3030 val = (XSUBR (fun)->function.a8
3031 (internal_args[0], internal_args[1], internal_args[2],
3032 internal_args[3], internal_args[4], internal_args[5],
3033 internal_args[6], internal_args[7]));
3034 break;
3035
3036 default:
3037
3038 /* If a subr takes more than 8 arguments without using MANY
3039 or UNEVALLED, we need to extend this function to support it.
3040 Until this is done, there is no way to call the function. */
3041 abort ();
3042 }
3043 }
3044 }
3045 else if (COMPILEDP (fun))
3046 val = funcall_lambda (fun, numargs, args + 1);
3047 else
3048 {
3049 if (EQ (fun, Qunbound))
3050 xsignal1 (Qvoid_function, original_fun);
3051 if (!CONSP (fun))
3052 xsignal1 (Qinvalid_function, original_fun);
3053 funcar = XCAR (fun);
3054 if (!SYMBOLP (funcar))
3055 xsignal1 (Qinvalid_function, original_fun);
3056 if (EQ (funcar, Qlambda)
3057 || EQ (funcar, Qclosure))
3058 val = funcall_lambda (fun, numargs, args + 1);
3059 else if (EQ (funcar, Qautoload))
3060 {
3061 do_autoload (fun, original_fun);
3062 CHECK_CONS_LIST ();
3063 goto retry;
3064 }
3065 else
3066 xsignal1 (Qinvalid_function, original_fun);
3067 }
3068 CHECK_CONS_LIST ();
3069 lisp_eval_depth--;
3070 if (backtrace.debug_on_exit)
3071 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil)));
3072 backtrace_list = backtrace.next;
3073 return val;
3074 }
3075 \f
3076 static Lisp_Object
3077 apply_lambda (Lisp_Object fun, Lisp_Object args)
3078 {
3079 Lisp_Object args_left;
3080 ptrdiff_t i, numargs;
3081 register Lisp_Object *arg_vector;
3082 struct gcpro gcpro1, gcpro2, gcpro3;
3083 register Lisp_Object tem;
3084 USE_SAFE_ALLOCA;
3085
3086 numargs = XFASTINT (Flength (args));
3087 SAFE_ALLOCA_LISP (arg_vector, numargs);
3088 args_left = args;
3089
3090 GCPRO3 (*arg_vector, args_left, fun);
3091 gcpro1.nvars = 0;
3092
3093 for (i = 0; i < numargs; )
3094 {
3095 tem = Fcar (args_left), args_left = Fcdr (args_left);
3096 tem = eval_sub (tem);
3097 arg_vector[i++] = tem;
3098 gcpro1.nvars = i;
3099 }
3100
3101 UNGCPRO;
3102
3103 backtrace_list->args = arg_vector;
3104 backtrace_list->nargs = i;
3105 tem = funcall_lambda (fun, numargs, arg_vector);
3106
3107 /* Do the debug-on-exit now, while arg_vector still exists. */
3108 if (backtrace_list->debug_on_exit)
3109 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil)));
3110 /* Don't do it again when we return to eval. */
3111 backtrace_list->debug_on_exit = 0;
3112 SAFE_FREE ();
3113 return tem;
3114 }
3115
3116 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3117 and return the result of evaluation.
3118 FUN must be either a lambda-expression or a compiled-code object. */
3119
3120 static Lisp_Object
3121 funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
3122 register Lisp_Object *arg_vector)
3123 {
3124 Lisp_Object val, syms_left, next, lexenv;
3125 int count = SPECPDL_INDEX ();
3126 ptrdiff_t i;
3127 int optional, rest;
3128
3129 if (CONSP (fun))
3130 {
3131 if (EQ (XCAR (fun), Qclosure))
3132 {
3133 fun = XCDR (fun); /* Drop `closure'. */
3134 lexenv = XCAR (fun);
3135 CHECK_LIST_CONS (fun, fun);
3136 }
3137 else
3138 lexenv = Qnil;
3139 syms_left = XCDR (fun);
3140 if (CONSP (syms_left))
3141 syms_left = XCAR (syms_left);
3142 else
3143 xsignal1 (Qinvalid_function, fun);
3144 }
3145 else if (COMPILEDP (fun))
3146 {
3147 syms_left = AREF (fun, COMPILED_ARGLIST);
3148 if (INTEGERP (syms_left))
3149 /* A byte-code object with a non-nil `push args' slot means we
3150 shouldn't bind any arguments, instead just call the byte-code
3151 interpreter directly; it will push arguments as necessary.
3152
3153 Byte-code objects with either a non-existent, or a nil value for
3154 the `push args' slot (the default), have dynamically-bound
3155 arguments, and use the argument-binding code below instead (as do
3156 all interpreted functions, even lexically bound ones). */
3157 {
3158 /* If we have not actually read the bytecode string
3159 and constants vector yet, fetch them from the file. */
3160 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3161 Ffetch_bytecode (fun);
3162 return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3163 AREF (fun, COMPILED_CONSTANTS),
3164 AREF (fun, COMPILED_STACK_DEPTH),
3165 syms_left,
3166 nargs, arg_vector);
3167 }
3168 lexenv = Qnil;
3169 }
3170 else
3171 abort ();
3172
3173 i = optional = rest = 0;
3174 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
3175 {
3176 QUIT;
3177
3178 next = XCAR (syms_left);
3179 if (!SYMBOLP (next))
3180 xsignal1 (Qinvalid_function, fun);
3181
3182 if (EQ (next, Qand_rest))
3183 rest = 1;
3184 else if (EQ (next, Qand_optional))
3185 optional = 1;
3186 else
3187 {
3188 Lisp_Object arg;
3189 if (rest)
3190 {
3191 arg = Flist (nargs - i, &arg_vector[i]);
3192 i = nargs;
3193 }
3194 else if (i < nargs)
3195 arg = arg_vector[i++];
3196 else if (!optional)
3197 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3198 else
3199 arg = Qnil;
3200
3201 /* Bind the argument. */
3202 if (!NILP (lexenv) && SYMBOLP (next))
3203 /* Lexically bind NEXT by adding it to the lexenv alist. */
3204 lexenv = Fcons (Fcons (next, arg), lexenv);
3205 else
3206 /* Dynamically bind NEXT. */
3207 specbind (next, arg);
3208 }
3209 }
3210
3211 if (!NILP (syms_left))
3212 xsignal1 (Qinvalid_function, fun);
3213 else if (i < nargs)
3214 xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
3215
3216 if (!EQ (lexenv, Vinternal_interpreter_environment))
3217 /* Instantiate a new lexical environment. */
3218 specbind (Qinternal_interpreter_environment, lexenv);
3219
3220 if (CONSP (fun))
3221 val = Fprogn (XCDR (XCDR (fun)));
3222 else
3223 {
3224 /* If we have not actually read the bytecode string
3225 and constants vector yet, fetch them from the file. */
3226 if (CONSP (AREF (fun, COMPILED_BYTECODE)))
3227 Ffetch_bytecode (fun);
3228 val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
3229 AREF (fun, COMPILED_CONSTANTS),
3230 AREF (fun, COMPILED_STACK_DEPTH),
3231 Qnil, 0, 0);
3232 }
3233
3234 return unbind_to (count, val);
3235 }
3236
3237 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3238 1, 1, 0,
3239 doc: /* If byte-compiled OBJECT is lazy-loaded, fetch it now. */)
3240 (Lisp_Object object)
3241 {
3242 Lisp_Object tem;
3243
3244 if (COMPILEDP (object) && CONSP (AREF (object, COMPILED_BYTECODE)))
3245 {
3246 tem = read_doc_string (AREF (object, COMPILED_BYTECODE));
3247 if (!CONSP (tem))
3248 {
3249 tem = AREF (object, COMPILED_BYTECODE);
3250 if (CONSP (tem) && STRINGP (XCAR (tem)))
3251 error ("Invalid byte code in %s", SDATA (XCAR (tem)));
3252 else
3253 error ("Invalid byte code");
3254 }
3255 ASET (object, COMPILED_BYTECODE, XCAR (tem));
3256 ASET (object, COMPILED_CONSTANTS, XCDR (tem));
3257 }
3258 return object;
3259 }
3260 \f
3261 static void
3262 grow_specpdl (void)
3263 {
3264 register int count = SPECPDL_INDEX ();
3265 int max_size =
3266 min (max_specpdl_size,
3267 min (max (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct specbinding),
3268 INT_MAX));
3269 int size;
3270 if (max_size <= specpdl_size)
3271 {
3272 if (max_specpdl_size < 400)
3273 max_size = max_specpdl_size = 400;
3274 if (max_size <= specpdl_size)
3275 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil);
3276 }
3277 size = specpdl_size < max_size / 2 ? 2 * specpdl_size : max_size;
3278 specpdl = xnrealloc (specpdl, size, sizeof *specpdl);
3279 specpdl_size = size;
3280 specpdl_ptr = specpdl + count;
3281 }
3282
3283 /* `specpdl_ptr->symbol' is a field which describes which variable is
3284 let-bound, so it can be properly undone when we unbind_to.
3285 It can have the following two shapes:
3286 - SYMBOL : if it's a plain symbol, it means that we have let-bound
3287 a symbol that is not buffer-local (at least at the time
3288 the let binding started). Note also that it should not be
3289 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3290 to record V2 here).
3291 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for
3292 variable SYMBOL which can be buffer-local. WHERE tells us
3293 which buffer is affected (or nil if the let-binding affects the
3294 global value of the variable) and BUFFER tells us which buffer was
3295 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise
3296 BUFFER did not yet have a buffer-local value). */
3297
3298 void
3299 specbind (Lisp_Object symbol, Lisp_Object value)
3300 {
3301 struct Lisp_Symbol *sym;
3302
3303 eassert (!handling_signal);
3304
3305 CHECK_SYMBOL (symbol);
3306 sym = XSYMBOL (symbol);
3307 if (specpdl_ptr == specpdl + specpdl_size)
3308 grow_specpdl ();
3309
3310 start:
3311 switch (sym->redirect)
3312 {
3313 case SYMBOL_VARALIAS:
3314 sym = indirect_variable (sym); XSETSYMBOL (symbol, sym); goto start;
3315 case SYMBOL_PLAINVAL:
3316 /* The most common case is that of a non-constant symbol with a
3317 trivial value. Make that as fast as we can. */
3318 specpdl_ptr->symbol = symbol;
3319 specpdl_ptr->old_value = SYMBOL_VAL (sym);
3320 specpdl_ptr->func = NULL;
3321 ++specpdl_ptr;
3322 if (!sym->constant)
3323 SET_SYMBOL_VAL (sym, value);
3324 else
3325 set_internal (symbol, value, Qnil, 1);
3326 break;
3327 case SYMBOL_LOCALIZED:
3328 if (SYMBOL_BLV (sym)->frame_local)
3329 error ("Frame-local vars cannot be let-bound");
3330 case SYMBOL_FORWARDED:
3331 {
3332 Lisp_Object ovalue = find_symbol_value (symbol);
3333 specpdl_ptr->func = 0;
3334 specpdl_ptr->old_value = ovalue;
3335
3336 eassert (sym->redirect != SYMBOL_LOCALIZED
3337 || (EQ (SYMBOL_BLV (sym)->where,
3338 SYMBOL_BLV (sym)->frame_local ?
3339 Fselected_frame () : Fcurrent_buffer ())));
3340
3341 if (sym->redirect == SYMBOL_LOCALIZED
3342 || BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3343 {
3344 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3345
3346 /* For a local variable, record both the symbol and which
3347 buffer's or frame's value we are saving. */
3348 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3349 {
3350 eassert (sym->redirect != SYMBOL_LOCALIZED
3351 || (BLV_FOUND (SYMBOL_BLV (sym))
3352 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3353 where = cur_buf;
3354 }
3355 else if (sym->redirect == SYMBOL_LOCALIZED
3356 && BLV_FOUND (SYMBOL_BLV (sym)))
3357 where = SYMBOL_BLV (sym)->where;
3358 else
3359 where = Qnil;
3360
3361 /* We're not using the `unused' slot in the specbinding
3362 structure because this would mean we have to do more
3363 work for simple variables. */
3364 /* FIXME: The third value `current_buffer' is only used in
3365 let_shadows_buffer_binding_p which is itself only used
3366 in set_internal for local_if_set. */
3367 eassert (NILP (where) || EQ (where, cur_buf));
3368 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3369
3370 /* If SYMBOL is a per-buffer variable which doesn't have a
3371 buffer-local value here, make the `let' change the global
3372 value by changing the value of SYMBOL in all buffers not
3373 having their own value. This is consistent with what
3374 happens with other buffer-local variables. */
3375 if (NILP (where)
3376 && sym->redirect == SYMBOL_FORWARDED)
3377 {
3378 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym)));
3379 ++specpdl_ptr;
3380 Fset_default (symbol, value);
3381 return;
3382 }
3383 }
3384 else
3385 specpdl_ptr->symbol = symbol;
3386
3387 specpdl_ptr++;
3388 set_internal (symbol, value, Qnil, 1);
3389 break;
3390 }
3391 default: abort ();
3392 }
3393 }
3394
3395 void
3396 record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg)
3397 {
3398 eassert (!handling_signal);
3399
3400 if (specpdl_ptr == specpdl + specpdl_size)
3401 grow_specpdl ();
3402 specpdl_ptr->func = function;
3403 specpdl_ptr->symbol = Qnil;
3404 specpdl_ptr->old_value = arg;
3405 specpdl_ptr++;
3406 }
3407
3408 Lisp_Object
3409 unbind_to (int count, Lisp_Object value)
3410 {
3411 Lisp_Object quitf = Vquit_flag;
3412 struct gcpro gcpro1, gcpro2;
3413
3414 GCPRO2 (value, quitf);
3415 Vquit_flag = Qnil;
3416
3417 while (specpdl_ptr != specpdl + count)
3418 {
3419 /* Copy the binding, and decrement specpdl_ptr, before we do
3420 the work to unbind it. We decrement first
3421 so that an error in unbinding won't try to unbind
3422 the same entry again, and we copy the binding first
3423 in case more bindings are made during some of the code we run. */
3424
3425 struct specbinding this_binding;
3426 this_binding = *--specpdl_ptr;
3427
3428 if (this_binding.func != 0)
3429 (*this_binding.func) (this_binding.old_value);
3430 /* If the symbol is a list, it is really (SYMBOL WHERE
3431 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3432 frame. If WHERE is a buffer or frame, this indicates we
3433 bound a variable that had a buffer-local or frame-local
3434 binding. WHERE nil means that the variable had the default
3435 value when it was bound. CURRENT-BUFFER is the buffer that
3436 was current when the variable was bound. */
3437 else if (CONSP (this_binding.symbol))
3438 {
3439 Lisp_Object symbol, where;
3440
3441 symbol = XCAR (this_binding.symbol);
3442 where = XCAR (XCDR (this_binding.symbol));
3443
3444 if (NILP (where))
3445 Fset_default (symbol, this_binding.old_value);
3446 /* If `where' is non-nil, reset the value in the appropriate
3447 local binding, but only if that binding still exists. */
3448 else if (BUFFERP (where)
3449 ? !NILP (Flocal_variable_p (symbol, where))
3450 : !NILP (Fassq (symbol, XFRAME (where)->param_alist)))
3451 set_internal (symbol, this_binding.old_value, where, 1);
3452 }
3453 /* If variable has a trivial value (no forwarding), we can
3454 just set it. No need to check for constant symbols here,
3455 since that was already done by specbind. */
3456 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3457 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3458 this_binding.old_value);
3459 else
3460 /* NOTE: we only ever come here if make_local_foo was used for
3461 the first time on this var within this let. */
3462 Fset_default (this_binding.symbol, this_binding.old_value);
3463 }
3464
3465 if (NILP (Vquit_flag) && !NILP (quitf))
3466 Vquit_flag = quitf;
3467
3468 UNGCPRO;
3469 return value;
3470 }
3471
3472 DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
3473 doc: /* Return non-nil if SYMBOL's global binding has been declared special.
3474 A special variable is one that will be bound dynamically, even in a
3475 context where binding is lexical by default. */)
3476 (Lisp_Object symbol)
3477 {
3478 CHECK_SYMBOL (symbol);
3479 return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
3480 }
3481
3482 \f
3483 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3484 doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
3485 The debugger is entered when that frame exits, if the flag is non-nil. */)
3486 (Lisp_Object level, Lisp_Object flag)
3487 {
3488 register struct backtrace *backlist = backtrace_list;
3489 register int i;
3490
3491 CHECK_NUMBER (level);
3492
3493 for (i = 0; backlist && i < XINT (level); i++)
3494 {
3495 backlist = backlist->next;
3496 }
3497
3498 if (backlist)
3499 backlist->debug_on_exit = !NILP (flag);
3500
3501 return flag;
3502 }
3503
3504 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3505 doc: /* Print a trace of Lisp function calls currently active.
3506 Output stream used is value of `standard-output'. */)
3507 (void)
3508 {
3509 register struct backtrace *backlist = backtrace_list;
3510 Lisp_Object tail;
3511 Lisp_Object tem;
3512 struct gcpro gcpro1;
3513 Lisp_Object old_print_level = Vprint_level;
3514
3515 if (NILP (Vprint_level))
3516 XSETFASTINT (Vprint_level, 8);
3517
3518 tail = Qnil;
3519 GCPRO1 (tail);
3520
3521 while (backlist)
3522 {
3523 write_string (backlist->debug_on_exit ? "* " : " ", 2);
3524 if (backlist->nargs == UNEVALLED)
3525 {
3526 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil);
3527 write_string ("\n", -1);
3528 }
3529 else
3530 {
3531 tem = *backlist->function;
3532 Fprin1 (tem, Qnil); /* This can QUIT. */
3533 write_string ("(", -1);
3534 if (backlist->nargs == MANY)
3535 { /* FIXME: Can this happen? */
3536 int i;
3537 for (tail = *backlist->args, i = 0;
3538 !NILP (tail);
3539 tail = Fcdr (tail), i = 1)
3540 {
3541 if (i) write_string (" ", -1);
3542 Fprin1 (Fcar (tail), Qnil);
3543 }
3544 }
3545 else
3546 {
3547 ptrdiff_t i;
3548 for (i = 0; i < backlist->nargs; i++)
3549 {
3550 if (i) write_string (" ", -1);
3551 Fprin1 (backlist->args[i], Qnil);
3552 }
3553 }
3554 write_string (")\n", -1);
3555 }
3556 backlist = backlist->next;
3557 }
3558
3559 Vprint_level = old_print_level;
3560 UNGCPRO;
3561 return Qnil;
3562 }
3563
3564 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL,
3565 doc: /* Return the function and arguments NFRAMES up from current execution point.
3566 If that frame has not evaluated the arguments yet (or is a special form),
3567 the value is (nil FUNCTION ARG-FORMS...).
3568 If that frame has evaluated its arguments and called its function already,
3569 the value is (t FUNCTION ARG-VALUES...).
3570 A &rest arg is represented as the tail of the list ARG-VALUES.
3571 FUNCTION is whatever was supplied as car of evaluated list,
3572 or a lambda expression for macro calls.
3573 If NFRAMES is more than the number of frames, the value is nil. */)
3574 (Lisp_Object nframes)
3575 {
3576 register struct backtrace *backlist = backtrace_list;
3577 register EMACS_INT i;
3578 Lisp_Object tem;
3579
3580 CHECK_NATNUM (nframes);
3581
3582 /* Find the frame requested. */
3583 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3584 backlist = backlist->next;
3585
3586 if (!backlist)
3587 return Qnil;
3588 if (backlist->nargs == UNEVALLED)
3589 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
3590 else
3591 {
3592 if (backlist->nargs == MANY) /* FIXME: Can this happen? */
3593 tem = *backlist->args;
3594 else
3595 tem = Flist (backlist->nargs, backlist->args);
3596
3597 return Fcons (Qt, Fcons (*backlist->function, tem));
3598 }
3599 }
3600
3601 \f
3602 #if BYTE_MARK_STACK
3603 void
3604 mark_backtrace (void)
3605 {
3606 register struct backtrace *backlist;
3607 ptrdiff_t i;
3608
3609 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3610 {
3611 mark_object (*backlist->function);
3612
3613 if (backlist->nargs == UNEVALLED
3614 || backlist->nargs == MANY) /* FIXME: Can this happen? */
3615 i = 1;
3616 else
3617 i = backlist->nargs;
3618 while (i--)
3619 mark_object (backlist->args[i]);
3620 }
3621 }
3622 #endif
3623
3624 void
3625 syms_of_eval (void)
3626 {
3627 DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
3628 doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
3629 If Lisp code tries to increase the total number past this amount,
3630 an error is signaled.
3631 You can safely use a value considerably larger than the default value,
3632 if that proves inconveniently small. However, if you increase it too far,
3633 Emacs could run out of memory trying to make the stack bigger. */);
3634
3635 DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
3636 doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
3637
3638 This limit serves to catch infinite recursions for you before they cause
3639 actual stack overflow in C, which would be fatal for Emacs.
3640 You can safely make it considerably larger than its default value,
3641 if that proves inconveniently small. However, if you increase it too far,
3642 Emacs could overflow the real C stack, and crash. */);
3643
3644 DEFVAR_LISP ("quit-flag", Vquit_flag,
3645 doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
3646 If the value is t, that means do an ordinary quit.
3647 If the value equals `throw-on-input', that means quit by throwing
3648 to the tag specified in `throw-on-input'; it's for handling `while-no-input'.
3649 Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
3650 but `inhibit-quit' non-nil prevents anything from taking notice of that. */);
3651 Vquit_flag = Qnil;
3652
3653 DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
3654 doc: /* Non-nil inhibits C-g quitting from happening immediately.
3655 Note that `quit-flag' will still be set by typing C-g,
3656 so a quit will be signaled as soon as `inhibit-quit' is nil.
3657 To prevent this happening, set `quit-flag' to nil
3658 before making `inhibit-quit' nil. */);
3659 Vinhibit_quit = Qnil;
3660
3661 DEFSYM (Qinhibit_quit, "inhibit-quit");
3662 DEFSYM (Qautoload, "autoload");
3663 DEFSYM (Qdebug_on_error, "debug-on-error");
3664 DEFSYM (Qmacro, "macro");
3665 DEFSYM (Qdeclare, "declare");
3666
3667 /* Note that the process handling also uses Qexit, but we don't want
3668 to staticpro it twice, so we just do it here. */
3669 DEFSYM (Qexit, "exit");
3670
3671 DEFSYM (Qinteractive, "interactive");
3672 DEFSYM (Qcommandp, "commandp");
3673 DEFSYM (Qdefun, "defun");
3674 DEFSYM (Qand_rest, "&rest");
3675 DEFSYM (Qand_optional, "&optional");
3676 DEFSYM (Qclosure, "closure");
3677 DEFSYM (Qdebug, "debug");
3678
3679 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3680 doc: /* *Non-nil means enter debugger if an error is signaled.
3681 Does not apply to errors handled by `condition-case' or those
3682 matched by `debug-ignored-errors'.
3683 If the value is a list, an error only means to enter the debugger
3684 if one of its condition symbols appears in the list.
3685 When you evaluate an expression interactively, this variable
3686 is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3687 The command `toggle-debug-on-error' toggles this.
3688 See also the variable `debug-on-quit'. */);
3689 Vdebug_on_error = Qnil;
3690
3691 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
3692 doc: /* *List of errors for which the debugger should not be called.
3693 Each element may be a condition-name or a regexp that matches error messages.
3694 If any element applies to a given error, that error skips the debugger
3695 and just returns to top level.
3696 This overrides the variable `debug-on-error'.
3697 It does not apply to errors handled by `condition-case'. */);
3698 Vdebug_ignored_errors = Qnil;
3699
3700 DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
3701 doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
3702 Does not apply if quit is handled by a `condition-case'. */);
3703 debug_on_quit = 0;
3704
3705 DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
3706 doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'. */);
3707
3708 DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
3709 doc: /* Non-nil means debugger may continue execution.
3710 This is nil when the debugger is called under circumstances where it
3711 might not be safe to continue. */);
3712 debugger_may_continue = 1;
3713
3714 DEFVAR_LISP ("debugger", Vdebugger,
3715 doc: /* Function to call to invoke debugger.
3716 If due to frame exit, args are `exit' and the value being returned;
3717 this function's value will be returned instead of that.
3718 If due to error, args are `error' and a list of the args to `signal'.
3719 If due to `apply' or `funcall' entry, one arg, `lambda'.
3720 If due to `eval' entry, one arg, t. */);
3721 Vdebugger = Qnil;
3722
3723 DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
3724 doc: /* If non-nil, this is a function for `signal' to call.
3725 It receives the same arguments that `signal' was given.
3726 The Edebug package uses this to regain control. */);
3727 Vsignal_hook_function = Qnil;
3728
3729 DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
3730 doc: /* *Non-nil means call the debugger regardless of condition handlers.
3731 Note that `debug-on-error', `debug-on-quit' and friends
3732 still determine whether to handle the particular condition. */);
3733 Vdebug_on_signal = Qnil;
3734
3735 DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
3736 doc: /* Function to process declarations in a macro definition.
3737 The function will be called with two args MACRO and DECL.
3738 MACRO is the name of the macro being defined.
3739 DECL is a list `(declare ...)' containing the declarations.
3740 The value the function returns is not used. */);
3741 Vmacro_declaration_function = Qnil;
3742
3743 /* When lexical binding is being used,
3744 vinternal_interpreter_environment is non-nil, and contains an alist
3745 of lexically-bound variable, or (t), indicating an empty
3746 environment. The lisp name of this variable would be
3747 `internal-interpreter-environment' if it weren't hidden.
3748 Every element of this list can be either a cons (VAR . VAL)
3749 specifying a lexical binding, or a single symbol VAR indicating
3750 that this variable should use dynamic scoping. */
3751 DEFSYM (Qinternal_interpreter_environment, "internal-interpreter-environment");
3752 DEFVAR_LISP ("internal-interpreter-environment",
3753 Vinternal_interpreter_environment,
3754 doc: /* If non-nil, the current lexical environment of the lisp interpreter.
3755 When lexical binding is not being used, this variable is nil.
3756 A value of `(t)' indicates an empty environment, otherwise it is an
3757 alist of active lexical bindings. */);
3758 Vinternal_interpreter_environment = Qnil;
3759 /* Don't export this variable to Elisp, so no one can mess with it
3760 (Just imagine if someone makes it buffer-local). */
3761 Funintern (Qinternal_interpreter_environment, Qnil);
3762
3763 DEFSYM (Vrun_hooks, "run-hooks");
3764
3765 staticpro (&Vautoload_queue);
3766 Vautoload_queue = Qnil;
3767 staticpro (&Vsignaling_function);
3768 Vsignaling_function = Qnil;
3769
3770 inhibit_lisp_code = Qnil;
3771
3772 defsubr (&Sor);
3773 defsubr (&Sand);
3774 defsubr (&Sif);
3775 defsubr (&Scond);
3776 defsubr (&Sprogn);
3777 defsubr (&Sprog1);
3778 defsubr (&Sprog2);
3779 defsubr (&Ssetq);
3780 defsubr (&Squote);
3781 defsubr (&Sfunction);
3782 defsubr (&Sdefun);
3783 defsubr (&Sdefmacro);
3784 defsubr (&Sdefvar);
3785 defsubr (&Sdefvaralias);
3786 defsubr (&Sdefconst);
3787 defsubr (&Suser_variable_p);
3788 defsubr (&Slet);
3789 defsubr (&SletX);
3790 defsubr (&Swhile);
3791 defsubr (&Smacroexpand);
3792 defsubr (&Scatch);
3793 defsubr (&Sthrow);
3794 defsubr (&Sunwind_protect);
3795 defsubr (&Scondition_case);
3796 defsubr (&Ssignal);
3797 defsubr (&Sinteractive_p);
3798 defsubr (&Scalled_interactively_p);
3799 defsubr (&Scommandp);
3800 defsubr (&Sautoload);
3801 defsubr (&Seval);
3802 defsubr (&Sapply);
3803 defsubr (&Sfuncall);
3804 defsubr (&Srun_hooks);
3805 defsubr (&Srun_hook_with_args);
3806 defsubr (&Srun_hook_with_args_until_success);
3807 defsubr (&Srun_hook_with_args_until_failure);
3808 defsubr (&Srun_hook_wrapped);
3809 defsubr (&Sfetch_bytecode);
3810 defsubr (&Sbacktrace_debug);
3811 defsubr (&Sbacktrace);
3812 defsubr (&Sbacktrace_frame);
3813 defsubr (&Sspecial_variable_p);
3814 defsubr (&Sfunctionp);
3815 }