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