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