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