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