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