1 /* Evaluator for XEmacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1992-1994 Free Software Foundation, Inc.
3 Copyright (C) 1995 Sun Microsystems, Inc.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: FSF 19.30 (except for Fsignal), Mule 2.0. */
28 #include "backtrace.h"
35 int always_gc; /* Debugging hack */
40 struct backtrace *backtrace_list;
42 /* Note: you must always fill in all of the fields in a backtrace structure
43 before pushing them on the backtrace_list. The profiling code depends
46 #define PUSH_BACKTRACE(bt) do { \
47 (bt).next = backtrace_list; \
48 backtrace_list = &(bt); \
51 #define POP_BACKTRACE(bt) do { \
52 backtrace_list = (bt).next; \
55 /* Macros for calling subrs with an argument list whose length is only
56 known at runtime. See EXFUN and DEFUN for similar hackery. */
59 #define AV_1(av) av[0]
60 #define AV_2(av) AV_1(av), av[1]
61 #define AV_3(av) AV_2(av), av[2]
62 #define AV_4(av) AV_3(av), av[3]
63 #define AV_5(av) AV_4(av), av[4]
64 #define AV_6(av) AV_5(av), av[5]
65 #define AV_7(av) AV_6(av), av[6]
66 #define AV_8(av) AV_7(av), av[7]
68 #define PRIMITIVE_FUNCALL_1(fn, av, ac) \
69 (((Lisp_Object (*)(EXFUN_##ac)) (fn)) (AV_##ac (av)))
71 /* If subrs take more than 8 arguments, more cases need to be added
72 to this switch. (But wait - don't do it - if you really need
73 a SUBR with more than 8 arguments, use max_args == MANY.
74 See the DEFUN macro in lisp.h) */
75 #define PRIMITIVE_FUNCALL(rv, fn, av, ac) do { \
76 void (*PF_fn)(void) = (void (*)(void)) fn; \
77 Lisp_Object *PF_av = (av); \
81 case 0: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 0); break; \
82 case 1: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 1); break; \
83 case 2: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 2); break; \
84 case 3: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 3); break; \
85 case 4: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 4); break; \
86 case 5: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 5); break; \
87 case 6: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 6); break; \
88 case 7: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 7); break; \
89 case 8: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 8); break; \
93 #define FUNCALL_SUBR(rv, subr, av, ac) \
94 PRIMITIVE_FUNCALL (rv, subr_function (subr), av, ac);
97 /* This is the list of current catches (and also condition-cases).
98 This is a stack: the most recent catch is at the head of the
99 list. Catches are created by declaring a 'struct catchtag'
100 locally, filling the .TAG field in with the tag, and doing
101 a setjmp() on .JMP. Fthrow() will store the value passed
102 to it in .VAL and longjmp() back to .JMP, back to the function
103 that established the catch. This will always be either
104 internal_catch() (catches established internally or through
105 `catch') or condition_case_1 (condition-cases established
106 internally or through `condition-case').
108 The catchtag also records the current position in the
109 call stack (stored in BACKTRACE_LIST), the current position
110 in the specpdl stack (used for variable bindings and
111 unwind-protects), the value of LISP_EVAL_DEPTH, and the
112 current position in the GCPRO stack. All of these are
113 restored by Fthrow().
116 struct catchtag *catchlist;
118 Lisp_Object Qautoload, Qmacro, Qexit;
119 Lisp_Object Qinteractive, Qcommandp, Qdefun, Qprogn, Qvalues;
120 Lisp_Object Vquit_flag, Vinhibit_quit;
121 Lisp_Object Qand_rest, Qand_optional;
122 Lisp_Object Qdebug_on_error, Qstack_trace_on_error;
123 Lisp_Object Qdebug_on_signal, Qstack_trace_on_signal;
124 Lisp_Object Qdebugger;
125 Lisp_Object Qinhibit_quit;
126 Lisp_Object Qrun_hooks;
128 Lisp_Object Qdisplay_warning;
129 Lisp_Object Vpending_warnings, Vpending_warnings_tail;
132 /* Records whether we want errors to occur. This will be a boolean,
133 nil (errors OK) or t (no errors). If t, an error will cause a
134 throw to Qunbound_suspended_errors_tag.
136 See call_with_suspended_errors(). */
137 Lisp_Object Vcurrent_error_state;
139 /* Current warning class when warnings occur, or nil for no warnings.
140 Only meaningful when Vcurrent_error_state is non-nil.
141 See call_with_suspended_errors(). */
142 Lisp_Object Vcurrent_warning_class;
144 /* Special catch tag used in call_with_suspended_errors(). */
145 Lisp_Object Qunbound_suspended_errors_tag;
147 /* Non-nil means we're going down, so we better not run any hooks
148 or do other non-essential stuff. */
149 int preparing_for_armageddon;
151 /* Non-nil means record all fset's and provide's, to be undone
152 if the file being autoloaded is not fully loaded.
153 They are recorded by being consed onto the front of Vautoload_queue:
154 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
155 Lisp_Object Vautoload_queue;
157 /* Current number of specbindings allocated in specpdl. */
160 /* Pointer to beginning of specpdl. */
161 struct specbinding *specpdl;
163 /* Pointer to first unused element in specpdl. */
164 struct specbinding *specpdl_ptr;
166 /* specpdl_ptr - specpdl */
167 int specpdl_depth_counter;
169 /* Maximum size allowed for specpdl allocation */
170 int max_specpdl_size;
172 /* Depth in Lisp evaluations and function calls. */
173 static int lisp_eval_depth;
175 /* Maximum allowed depth in Lisp evaluations and function calls. */
176 int max_lisp_eval_depth;
178 /* Nonzero means enter debugger before next function call */
179 static int debug_on_next_call;
181 /* List of conditions (non-nil atom means all) which cause a backtrace
182 if an error is handled by the command loop's error handler. */
183 Lisp_Object Vstack_trace_on_error;
185 /* List of conditions (non-nil atom means all) which enter the debugger
186 if an error is handled by the command loop's error handler. */
187 Lisp_Object Vdebug_on_error;
189 /* List of conditions and regexps specifying error messages which
190 do not enter the debugger even if Vdebug_on_error says they should. */
191 Lisp_Object Vdebug_ignored_errors;
193 /* List of conditions (non-nil atom means all) which cause a backtrace
194 if any error is signalled. */
195 Lisp_Object Vstack_trace_on_signal;
197 /* List of conditions (non-nil atom means all) which enter the debugger
198 if any error is signalled. */
199 Lisp_Object Vdebug_on_signal;
201 /* Nonzero means enter debugger if a quit signal
202 is handled by the command loop's error handler.
204 From lisp, this is a boolean variable and may have the values 0 and 1.
205 But, eval.c temporarily uses the second bit of this variable to indicate
206 that a critical_quit is in progress. The second bit is reset immediately
207 after it is processed in signal_call_debugger(). */
211 /* entering_debugger is basically equivalent */
212 /* The value of num_nonmacro_input_chars as of the last time we
213 started to enter the debugger. If we decide to enter the debugger
214 again when this is still equal to num_nonmacro_input_chars, then we
215 know that the debugger itself has an error, and we should just
216 signal the error instead of entering an infinite loop of debugger
218 int when_entered_debugger;
221 /* Nonzero means we are trying to enter the debugger.
222 This is to prevent recursive attempts.
223 Cleared by the debugger calling Fbacktrace */
224 static int entering_debugger;
226 /* Function to call to invoke the debugger */
227 Lisp_Object Vdebugger;
229 /* Chain of condition handlers currently in effect.
230 The elements of this chain are contained in the stack frames
231 of Fcondition_case and internal_condition_case.
232 When an error is signaled (by calling Fsignal, below),
233 this chain is searched for an element that applies.
235 Each element of this list is one of the following:
237 A list of a handler function and possibly args to pass to
238 the function. This is a handler established with
239 `call-with-condition-handler' (q.v.).
241 A list whose car is Qunbound and whose cdr is Qt.
242 This is a special condition-case handler established
243 by C code with condition_case_1(). All errors are
244 trapped; the debugger is not invoked even if
245 `debug-on-error' was set.
247 A list whose car is Qunbound and whose cdr is Qerror.
248 This is a special condition-case handler established
249 by C code with condition_case_1(). It is like Qt
250 except that the debugger is invoked normally if it is
253 A list whose car is Qunbound and whose cdr is a list
254 of lists (CONDITION-NAME BODY ...) exactly as in
255 `condition-case'. This is a normal `condition-case'
258 Note that in all cases *except* the first, there is a
259 corresponding catch, whose TAG is the value of
260 Vcondition_handlers just after the handler data just
261 described is pushed onto it. The reason is that
262 `condition-case' handlers need to throw back to the
263 place where the handler was installed before invoking
264 it, while `call-with-condition-handler' handlers are
265 invoked in the environment that `signal' was invoked
268 static Lisp_Object Vcondition_handlers;
271 #if 0 /* no longer used */
272 /* Used for error catching purposes by throw_or_bomb_out */
273 static int throw_level;
277 /************************************************************************/
278 /* The subr object type */
279 /************************************************************************/
282 print_subr (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
284 Lisp_Subr *subr = XSUBR (obj);
286 (subr->max_args == UNEVALLED) ? "#<special-form " : "#<subr ";
287 CONST char *name = subr_name (subr);
288 CONST char *trailer = subr->prompt ? " (interactive)>" : ">";
291 error ("printing unreadable object %s%s%s", header, name, trailer);
293 write_c_string (header, printcharfun);
294 write_c_string (name, printcharfun);
295 write_c_string (trailer, printcharfun);
298 static const struct lrecord_description subr_description[] = {
299 { XD_DOC_STRING, offsetof(Lisp_Subr, doc) },
303 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("subr", subr,
304 this_one_is_unmarkable, print_subr, 0, 0, 0,
308 /************************************************************************/
309 /* Entering the debugger */
310 /************************************************************************/
312 /* unwind-protect used by call_debugger() to restore the value of
313 entering_debugger. (We cannot use specbind() because the
314 variable is not Lisp-accessible.) */
317 restore_entering_debugger (Lisp_Object arg)
319 entering_debugger = ! NILP (arg);
323 /* Actually call the debugger. ARG is a list of args that will be
324 passed to the debugger function, as follows;
326 If due to frame exit, args are `exit' and the value being returned;
327 this function's value will be returned instead of that.
328 If due to error, args are `error' and a list of the args to `signal'.
329 If due to `apply' or `funcall' entry, one arg, `lambda'.
330 If due to `eval' entry, one arg, t.
335 call_debugger_259 (Lisp_Object arg)
337 return apply1 (Vdebugger, arg);
340 /* Call the debugger, doing some encapsulation. We make sure we have
341 some room on the eval and specpdl stacks, and bind entering_debugger
342 to 1 during this call. This is used to trap errors that may occur
343 when entering the debugger (e.g. the value of `debugger' is invalid),
344 so that the debugger will not be recursively entered if debug-on-error
345 is set. (Otherwise, XEmacs would infinitely recurse, attempting to
346 enter the debugger.) entering_debugger gets reset to 0 as soon
347 as a backtrace is displayed, so that further errors can indeed be
350 We also establish a catch for 'debugger. If the debugger function
351 throws to this instead of returning a value, it means that the user
352 pressed 'c' (pretend like the debugger was never entered). The
353 function then returns Qunbound. (If the user pressed 'r', for
354 return a value, then the debugger function returns normally with
357 The difference between 'c' and 'r' is as follows:
360 No difference. The call proceeds as normal.
362 With 'r', the specified value is returned as the function's
363 return value. With 'c', the value that would normally be
364 returned is returned.
366 With 'r', the specified value is returned as the return
367 value of `signal'. (This is the only time that `signal'
368 can return, instead of making a non-local exit.) With `c',
369 `signal' will continue looking for handlers as if the
370 debugger was never entered, and will probably end up
371 throwing to a handler or to top-level.
375 call_debugger (Lisp_Object arg)
381 if (lisp_eval_depth + 20 > max_lisp_eval_depth)
382 max_lisp_eval_depth = lisp_eval_depth + 20;
383 if (specpdl_size + 40 > max_specpdl_size)
384 max_specpdl_size = specpdl_size + 40;
385 debug_on_next_call = 0;
387 speccount = specpdl_depth();
388 record_unwind_protect (restore_entering_debugger,
389 (entering_debugger ? Qt : Qnil));
390 entering_debugger = 1;
391 val = internal_catch (Qdebugger, call_debugger_259, arg, &threw);
393 return unbind_to (speccount, ((threw)
394 ? Qunbound /* Not returning a value */
398 /* Called when debug-on-exit behavior is called for. Enter the debugger
399 with the appropriate args for this. VAL is the exit value that is
400 about to be returned. */
403 do_debug_on_exit (Lisp_Object val)
405 /* This is falsified by call_debugger */
406 Lisp_Object v = call_debugger (list2 (Qexit, val));
408 return !UNBOUNDP (v) ? v : val;
411 /* Called when debug-on-call behavior is called for. Enter the debugger
412 with the appropriate args for this. VAL is either t for a call
413 through `eval' or 'lambda for a call through `funcall'.
415 #### The differentiation here between EVAL and FUNCALL is bogus.
416 FUNCALL can be defined as
418 (defmacro func (fun &rest args)
419 (cons (eval fun) args))
421 and should be treated as such.
425 do_debug_on_call (Lisp_Object code)
427 debug_on_next_call = 0;
428 backtrace_list->debug_on_exit = 1;
429 call_debugger (list1 (code));
432 /* LIST is the value of one of the variables `debug-on-error',
433 `debug-on-signal', `stack-trace-on-error', or `stack-trace-on-signal',
434 and CONDITIONS is the list of error conditions associated with
435 the error being signalled. This returns non-nil if LIST
436 matches CONDITIONS. (A nil value for LIST does not match
437 CONDITIONS. A non-list value for LIST does match CONDITIONS.
438 A list matches CONDITIONS when one of the symbols in LIST is the
439 same as one of the symbols in CONDITIONS.) */
442 wants_debugger (Lisp_Object list, Lisp_Object conditions)
449 while (CONSP (conditions))
451 Lisp_Object this, tail;
452 this = XCAR (conditions);
453 for (tail = list; CONSP (tail); tail = XCDR (tail))
454 if (EQ (XCAR (tail), this))
456 conditions = XCDR (conditions);
462 /* Return 1 if an error with condition-symbols CONDITIONS,
463 and described by SIGNAL-DATA, should skip the debugger
464 according to debugger-ignore-errors. */
467 skip_debugger (Lisp_Object conditions, Lisp_Object data)
469 /* This function can GC */
471 int first_string = 1;
472 Lisp_Object error_message = Qnil;
474 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
476 if (STRINGP (XCAR (tail)))
480 error_message = Ferror_message_string (data);
483 if (fast_lisp_string_match (XCAR (tail), error_message) >= 0)
490 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
491 if (EQ (XCAR (tail), XCAR (contail)))
499 /* Actually generate a backtrace on STREAM. */
502 backtrace_259 (Lisp_Object stream)
504 return Fbacktrace (stream, Qt);
507 /* An error was signaled. Maybe call the debugger, if the `debug-on-error'
508 etc. variables call for this. CONDITIONS is the list of conditions
509 associated with the error being signalled. SIG is the actual error
510 being signalled, and DATA is the associated data (these are exactly
511 the same as the arguments to `signal'). ACTIVE_HANDLERS is the
512 list of error handlers that are to be put in place while the debugger
513 is called. This is generally the remaining handlers that are
514 outside of the innermost handler trapping this error. This way,
515 if the same error occurs inside of the debugger, you usually don't get
516 the debugger entered recursively.
518 This function returns Qunbound if it didn't call the debugger or if
519 the user asked (through 'c') that XEmacs should pretend like the
520 debugger was never entered. Otherwise, it returns the value
521 that the user specified with `r'. (Note that much of the time,
522 the user will abort with C-], and we will never have a chance to
523 return anything at all.)
525 SIGNAL_VARS_ONLY means we should only look at debug-on-signal
526 and stack-trace-on-signal to control whether we do anything.
527 This is so that debug-on-error doesn't make handled errors
528 cause the debugger to get invoked.
530 STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that
531 those functions aren't done more than once in a single `signal'
535 signal_call_debugger (Lisp_Object conditions,
536 Lisp_Object sig, Lisp_Object data,
537 Lisp_Object active_handlers,
538 int signal_vars_only,
539 int *stack_trace_displayed,
540 int *debugger_entered)
542 /* This function can GC */
543 Lisp_Object val = Qunbound;
544 Lisp_Object all_handlers = Vcondition_handlers;
545 Lisp_Object temp_data = Qnil;
546 int speccount = specpdl_depth();
547 struct gcpro gcpro1, gcpro2;
548 GCPRO2 (all_handlers, temp_data);
550 Vcondition_handlers = active_handlers;
552 temp_data = Fcons (sig, data); /* needed for skip_debugger */
554 if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only
555 && wants_debugger (Vstack_trace_on_error, conditions)
556 && !skip_debugger (conditions, temp_data))
558 specbind (Qdebug_on_error, Qnil);
559 specbind (Qstack_trace_on_error, Qnil);
560 specbind (Qdebug_on_signal, Qnil);
561 specbind (Qstack_trace_on_signal, Qnil);
563 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"),
567 unbind_to (speccount, Qnil);
568 *stack_trace_displayed = 1;
571 if (!entering_debugger && !*debugger_entered && !signal_vars_only
574 : wants_debugger (Vdebug_on_error, conditions))
575 && !skip_debugger (conditions, temp_data))
577 debug_on_quit &= ~2; /* reset critical bit */
578 specbind (Qdebug_on_error, Qnil);
579 specbind (Qstack_trace_on_error, Qnil);
580 specbind (Qdebug_on_signal, Qnil);
581 specbind (Qstack_trace_on_signal, Qnil);
583 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
584 *debugger_entered = 1;
587 if (!entering_debugger && !*stack_trace_displayed
588 && wants_debugger (Vstack_trace_on_signal, conditions))
590 specbind (Qdebug_on_error, Qnil);
591 specbind (Qstack_trace_on_error, Qnil);
592 specbind (Qdebug_on_signal, Qnil);
593 specbind (Qstack_trace_on_signal, Qnil);
595 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"),
599 unbind_to (speccount, Qnil);
600 *stack_trace_displayed = 1;
603 if (!entering_debugger && !*debugger_entered
606 : wants_debugger (Vdebug_on_signal, conditions)))
608 debug_on_quit &= ~2; /* reset critical bit */
609 specbind (Qdebug_on_error, Qnil);
610 specbind (Qstack_trace_on_error, Qnil);
611 specbind (Qdebug_on_signal, Qnil);
612 specbind (Qstack_trace_on_signal, Qnil);
614 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
615 *debugger_entered = 1;
619 Vcondition_handlers = all_handlers;
620 return unbind_to (speccount, val);
624 /************************************************************************/
625 /* The basic special forms */
626 /************************************************************************/
628 /* Except for Fprogn(), the basic special forms below are only called
629 from interpreted code. The byte compiler turns them into bytecodes. */
631 DEFUN ("or", For, 0, UNEVALLED, 0, /*
632 Eval args until one of them yields non-nil, then return that value.
633 The remaining args are not evalled at all.
634 If all args return nil, return nil.
638 /* This function can GC */
639 REGISTER Lisp_Object arg, val;
641 LIST_LOOP_2 (arg, args)
643 if (!NILP (val = Feval (arg)))
650 DEFUN ("and", Fand, 0, UNEVALLED, 0, /*
651 Eval args until one of them yields nil, then return nil.
652 The remaining args are not evalled at all.
653 If no arg yields nil, return the last arg's value.
657 /* This function can GC */
658 REGISTER Lisp_Object arg, val = Qt;
660 LIST_LOOP_2 (arg, args)
662 if (NILP (val = Feval (arg)))
669 DEFUN ("if", Fif, 2, UNEVALLED, 0, /*
670 \(if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...
671 Returns the value of THEN or the value of the last of the ELSE's.
672 THEN must be one expression, but ELSE... can be zero or more expressions.
673 If COND yields nil, and there are no ELSE's, the value is nil.
677 /* This function can GC */
678 Lisp_Object condition = XCAR (args);
679 Lisp_Object then_form = XCAR (XCDR (args));
680 Lisp_Object else_forms = XCDR (XCDR (args));
682 if (!NILP (Feval (condition)))
683 return Feval (then_form);
685 return Fprogn (else_forms);
688 /* Macros `when' and `unless' are trivially defined in Lisp,
689 but it helps for bootstrapping to have them ALWAYS defined. */
691 DEFUN ("when", Fwhen, 1, MANY, 0, /*
692 \(when COND BODY...): if COND yields non-nil, do BODY, else return nil.
693 BODY can be zero or more expressions. If BODY is nil, return nil.
695 (int nargs, Lisp_Object *args))
697 Lisp_Object cond = args[0];
702 case 1: body = Qnil; break;
703 case 2: body = args[1]; break;
704 default: body = Fcons (Qprogn, Flist (nargs-1, args+1)); break;
707 return list3 (Qif, cond, body);
710 DEFUN ("unless", Funless, 1, MANY, 0, /*
711 \(unless COND BODY...): if COND yields nil, do BODY, else return nil.
712 BODY can be zero or more expressions. If BODY is nil, return nil.
714 (int nargs, Lisp_Object *args))
716 Lisp_Object cond = args[0];
717 Lisp_Object body = Flist (nargs-1, args+1);
718 return Fcons (Qif, Fcons (cond, Fcons (Qnil, body)));
721 DEFUN ("cond", Fcond, 0, UNEVALLED, 0, /*
722 (cond CLAUSES...): try each clause until one succeeds.
723 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
724 and, if the value is non-nil, this clause succeeds:
725 then the expressions in BODY are evaluated and the last one's
726 value is the value of the cond-form.
727 If no clause succeeds, cond returns nil.
728 If a clause has one element, as in (CONDITION),
729 CONDITION's value if non-nil is returned from the cond-form.
733 /* This function can GC */
734 REGISTER Lisp_Object val, clause;
736 LIST_LOOP_2 (clause, args)
739 if (!NILP (val = Feval (XCAR (clause))))
741 if (!NILP (clause = XCDR (clause)))
743 CHECK_TRUE_LIST (clause);
744 val = Fprogn (clause);
753 DEFUN ("progn", Fprogn, 0, UNEVALLED, 0, /*
754 \(progn BODY...): eval BODY forms sequentially and return value of last one.
758 /* This function can GC */
759 /* Caller must provide a true list in ARGS */
760 REGISTER Lisp_Object form, val = Qnil;
766 LIST_LOOP_2 (form, args)
774 /* Fprog1() is the canonical example of a function that must GCPRO a
775 Lisp_Object across calls to Feval(). */
777 DEFUN ("prog1", Fprog1, 1, UNEVALLED, 0, /*
778 Similar to `progn', but the value of the first form is returned.
779 \(prog1 FIRST BODY...): All the arguments are evaluated sequentially.
780 The value of FIRST is saved during evaluation of the remaining args,
781 whose values are discarded.
785 /* This function can GC */
786 REGISTER Lisp_Object val, form;
789 val = Feval (XCAR (args));
794 LIST_LOOP_2 (form, XCDR (args))
802 DEFUN ("prog2", Fprog2, 2, UNEVALLED, 0, /*
803 Similar to `progn', but the value of the second form is returned.
804 \(prog2 FIRST SECOND BODY...): All the arguments are evaluated sequentially.
805 The value of SECOND is saved during evaluation of the remaining args,
806 whose values are discarded.
810 /* This function can GC */
811 REGISTER Lisp_Object val, form, tail;
816 val = Feval (XCAR (args));
821 LIST_LOOP_3 (form, args, tail)
828 DEFUN ("let*", FletX, 1, UNEVALLED, 0, /*
829 \(let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.
830 The value of the last form in BODY is returned.
831 Each element of VARLIST is a symbol (which is bound to nil)
832 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
833 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
837 /* This function can GC */
838 Lisp_Object var, tail;
839 Lisp_Object varlist = XCAR (args);
840 Lisp_Object body = XCDR (args);
841 int speccount = specpdl_depth();
843 EXTERNAL_LIST_LOOP_3 (var, varlist, tail)
845 Lisp_Object symbol, value, tem;
847 symbol = var, value = Qnil;
858 value = Feval (XCAR (tem));
859 if (!NILP (XCDR (tem)))
861 ("`let' bindings can have only one value-form", var);
864 specbind (symbol, value);
866 return unbind_to (speccount, Fprogn (body));
869 DEFUN ("let", Flet, 1, UNEVALLED, 0, /*
870 \(let VARLIST BODY...): bind variables according to VARLIST then eval BODY.
871 The value of the last form in BODY is returned.
872 Each element of VARLIST is a symbol (which is bound to nil)
873 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
874 All the VALUEFORMs are evalled before any symbols are bound.
878 /* This function can GC */
879 Lisp_Object var, tail;
880 Lisp_Object varlist = XCAR (args);
881 Lisp_Object body = XCDR (args);
882 int speccount = specpdl_depth();
887 /* Make space to hold the values to give the bound variables. */
890 GET_EXTERNAL_LIST_LENGTH (varlist, varcount);
891 temps = alloca_array (Lisp_Object, varcount);
894 /* Compute the values and store them in `temps' */
899 LIST_LOOP_3 (var, varlist, tail)
901 Lisp_Object *value = &temps[idx++];
914 *value = Feval (XCAR (tem));
917 if (!NILP (XCDR (tem)))
919 ("`let' bindings can have only one value-form", var);
925 LIST_LOOP_3 (var, varlist, tail)
927 specbind (SYMBOLP (var) ? var : XCAR (var), temps[idx++]);
932 return unbind_to (speccount, Fprogn (body));
935 DEFUN ("while", Fwhile, 1, UNEVALLED, 0, /*
936 \(while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.
937 The order of execution is thus TEST, BODY, TEST, BODY and so on
938 until TEST returns nil.
942 /* This function can GC */
943 Lisp_Object test = XCAR (args);
944 Lisp_Object body = XCDR (args);
946 while (!NILP (Feval (test)))
955 DEFUN ("setq", Fsetq, 0, UNEVALLED, 0, /*
956 \(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.
957 The symbols SYM are variables; they are literal (not evaluated).
958 The values VAL are expressions; they are evaluated.
959 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
960 The second VAL is not computed until after the first SYM is set, and so on;
961 each VAL can use the new value of variables set earlier in the `setq'.
962 The return value of the `setq' form is the value of the last VAL.
966 /* This function can GC */
967 Lisp_Object symbol, tail, val = Qnil;
971 GET_LIST_LENGTH (args, nargs);
973 if (nargs & 1) /* Odd number of arguments? */
974 Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, make_int (nargs)));
978 PROPERTY_LIST_LOOP (tail, symbol, val, args)
988 DEFUN ("quote", Fquote, 1, UNEVALLED, 0, /*
989 Return the argument, without evaluating it. `(quote x)' yields `x'.
996 DEFUN ("function", Ffunction, 1, UNEVALLED, 0, /*
997 Like `quote', but preferred for objects which are functions.
998 In byte compilation, `function' causes its argument to be compiled.
999 `quote' cannot do that.
1007 /************************************************************************/
1008 /* Defining functions/variables */
1009 /************************************************************************/
1011 define_function (Lisp_Object name, Lisp_Object defn)
1014 LOADHIST_ATTACH (name);
1018 DEFUN ("defun", Fdefun, 2, UNEVALLED, 0, /*
1019 \(defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.
1020 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
1021 See also the function `interactive'.
1025 /* This function can GC */
1026 return define_function (XCAR (args),
1027 Fcons (Qlambda, XCDR (args)));
1030 DEFUN ("defmacro", Fdefmacro, 2, UNEVALLED, 0, /*
1031 \(defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.
1032 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).
1033 When the macro is called, as in (NAME ARGS...),
1034 the function (lambda ARGLIST BODY...) is applied to
1035 the list ARGS... as it appears in the expression,
1036 and the result should be a form to be evaluated instead of the original.
1040 /* This function can GC */
1041 return define_function (XCAR (args),
1042 Fcons (Qmacro, Fcons (Qlambda, XCDR (args))));
1045 DEFUN ("defvar", Fdefvar, 1, UNEVALLED, 0, /*
1046 \(defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.
1047 You are not required to define a variable in order to use it,
1048 but the definition can supply documentation and an initial value
1049 in a way that tags can recognize.
1051 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is
1052 void. (However, when you evaluate a defvar interactively, it acts like a
1053 defconst: SYMBOL's value is always set regardless of whether it's currently
1055 If SYMBOL is buffer-local, its default value is what is set;
1056 buffer-local values are not affected.
1057 INITVALUE and DOCSTRING are optional.
1058 If DOCSTRING starts with *, this variable is identified as a user option.
1059 This means that M-x set-variable and M-x edit-options recognize it.
1060 If INITVALUE is missing, SYMBOL's value is not set.
1062 In lisp-interaction-mode defvar is treated as defconst.
1066 /* This function can GC */
1067 Lisp_Object sym = XCAR (args);
1069 if (!NILP (args = XCDR (args)))
1071 Lisp_Object val = XCAR (args);
1073 if (NILP (Fdefault_boundp (sym)))
1075 struct gcpro gcpro1;
1078 Fset_default (sym, val);
1082 if (!NILP (args = XCDR (args)))
1084 Lisp_Object doc = XCAR (args);
1085 Fput (sym, Qvariable_documentation, doc);
1086 if (!NILP (args = XCDR (args)))
1087 error ("too many arguments");
1092 if (!NILP (Vfile_domain))
1093 Fput (sym, Qvariable_domain, Vfile_domain);
1096 LOADHIST_ATTACH (sym);
1100 DEFUN ("defconst", Fdefconst, 2, UNEVALLED, 0, /*
1101 \(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant
1103 The intent is that programs do not change this value, but users may.
1104 Always sets the value of SYMBOL to the result of evalling INITVALUE.
1105 If SYMBOL is buffer-local, its default value is what is set;
1106 buffer-local values are not affected.
1107 DOCSTRING is optional.
1108 If DOCSTRING starts with *, this variable is identified as a user option.
1109 This means that M-x set-variable and M-x edit-options recognize it.
1111 Note: do not use `defconst' for user options in libraries that are not
1112 normally loaded, since it is useful for users to be able to specify
1113 their own values for such variables before loading the library.
1114 Since `defconst' unconditionally assigns the variable,
1115 it would override the user's choice.
1119 /* This function can GC */
1120 Lisp_Object sym = XCAR (args);
1121 Lisp_Object val = Feval (XCAR (args = XCDR (args)));
1122 struct gcpro gcpro1;
1126 Fset_default (sym, val);
1130 if (!NILP (args = XCDR (args)))
1132 Lisp_Object doc = XCAR (args);
1133 Fput (sym, Qvariable_documentation, doc);
1134 if (!NILP (args = XCDR (args)))
1135 error ("too many arguments");
1139 if (!NILP (Vfile_domain))
1140 Fput (sym, Qvariable_domain, Vfile_domain);
1143 LOADHIST_ATTACH (sym);
1147 DEFUN ("user-variable-p", Fuser_variable_p, 1, 1, 0, /*
1148 Return t if VARIABLE is intended to be set and modified by users.
1149 \(The alternative is a variable used internally in a Lisp program.)
1150 Determined by whether the first character of the documentation
1151 for the variable is `*'.
1155 Lisp_Object documentation = Fget (variable, Qvariable_documentation, Qnil);
1158 ((INTP (documentation) && XINT (documentation) < 0) ||
1160 (STRINGP (documentation) &&
1161 (string_byte (XSTRING (documentation), 0) == '*')) ||
1163 /* If (STRING . INTEGER), a negative integer means a user variable. */
1164 (CONSP (documentation)
1165 && STRINGP (XCAR (documentation))
1166 && INTP (XCDR (documentation))
1167 && XINT (XCDR (documentation)) < 0)) ?
1171 DEFUN ("macroexpand-internal", Fmacroexpand_internal, 1, 2, 0, /*
1172 Return result of expanding macros at top level of FORM.
1173 If FORM is not a macro call, it is returned unchanged.
1174 Otherwise, the macro is expanded and the expansion is considered
1175 in place of FORM. When a non-macro-call results, it is returned.
1177 The second optional arg ENVIRONMENT species an environment of macro
1178 definitions to shadow the loaded ones for use in file byte-compilation.
1182 /* This function can GC */
1183 /* With cleanups from Hallvard Furuseth. */
1184 REGISTER Lisp_Object expander, sym, def, tem;
1188 /* Come back here each time we expand a macro call,
1189 in case it expands into another macro call. */
1192 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1193 def = sym = XCAR (form);
1195 /* Trace symbols aliases to other symbols
1196 until we get a symbol that is not an alias. */
1197 while (SYMBOLP (def))
1201 tem = Fassq (sym, env);
1204 def = XSYMBOL (sym)->function;
1205 if (!UNBOUNDP (def))
1210 /* Right now TEM is the result from SYM in ENV,
1211 and if TEM is nil then DEF is SYM's function definition. */
1214 /* SYM is not mentioned in ENV.
1215 Look at its function definition. */
1218 /* Not defined or definition not suitable */
1220 if (EQ (XCAR (def), Qautoload))
1222 /* Autoloading function: will it be a macro when loaded? */
1223 tem = Felt (def, make_int (4));
1224 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1226 /* Yes, load it and try again. */
1227 do_autoload (def, sym);
1233 else if (!EQ (XCAR (def), Qmacro))
1235 else expander = XCDR (def);
1239 expander = XCDR (tem);
1240 if (NILP (expander))
1243 form = apply1 (expander, XCDR (form));
1249 /************************************************************************/
1250 /* Non-local exits */
1251 /************************************************************************/
1253 DEFUN ("catch", Fcatch, 1, UNEVALLED, 0, /*
1254 \(catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.
1255 TAG is evalled to get the tag to use. Then the BODY is executed.
1256 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1257 If no throw happens, `catch' returns the value of the last BODY form.
1258 If a throw happens, it specifies the value to return from `catch'.
1262 /* This function can GC */
1263 Lisp_Object tag = Feval (XCAR (args));
1264 Lisp_Object body = XCDR (args);
1265 return internal_catch (tag, Fprogn, body, 0);
1268 /* Set up a catch, then call C function FUNC on argument ARG.
1269 FUNC should return a Lisp_Object.
1270 This is how catches are done from within C code. */
1273 internal_catch (Lisp_Object tag,
1274 Lisp_Object (*func) (Lisp_Object arg),
1276 int * volatile threw)
1278 /* This structure is made part of the chain `catchlist'. */
1281 /* Fill in the components of c, and put it on the list. */
1285 c.backlist = backtrace_list;
1288 c.handlerlist = handlerlist;
1290 c.lisp_eval_depth = lisp_eval_depth;
1291 c.pdlcount = specpdl_depth();
1293 c.poll_suppress_count = async_timer_suppress_count;
1295 c.gcpro = gcprolist;
1301 /* Throw works by a longjmp that comes right here. */
1302 if (threw) *threw = 1;
1305 c.val = (*func) (arg);
1306 if (threw) *threw = 0;
1312 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1313 jump to that CATCH, returning VALUE as the value of that catch.
1315 This is the guts Fthrow and Fsignal; they differ only in the way
1316 they choose the catch tag to throw to. A catch tag for a
1317 condition-case form has a TAG of Qnil.
1319 Before each catch is discarded, unbind all special bindings and
1320 execute all unwind-protect clauses made above that catch. Unwind
1321 the handler stack as we go, so that the proper handlers are in
1322 effect for each unwind-protect clause we run. At the end, restore
1323 some static info saved in CATCH, and longjmp to the location
1326 This is used for correct unwinding in Fthrow and Fsignal. */
1329 unwind_to_catch (struct catchtag *c, Lisp_Object val)
1333 REGISTER int last_time;
1336 /* Unwind the specbind, catch, and handler stacks back to CATCH
1337 Before each catch is discarded, unbind all special bindings
1338 and execute all unwind-protect clauses made above that catch.
1339 At the end, restore some static info saved in CATCH,
1340 and longjmp to the location specified.
1343 /* Save the value somewhere it will be GC'ed.
1344 (Can't overwrite tag slot because an unwind-protect may
1345 want to throw to this same tag, which isn't yet invalid.) */
1349 /* Restore the polling-suppression count. */
1350 set_poll_suppress_count (catch->poll_suppress_count);
1354 /* #### FSFmacs has the following loop. Is it more correct? */
1357 last_time = catchlist == c;
1359 /* Unwind the specpdl stack, and then restore the proper set of
1361 unbind_to (catchlist->pdlcount, Qnil);
1362 handlerlist = catchlist->handlerlist;
1363 catchlist = catchlist->next;
1365 while (! last_time);
1366 #else /* Actual XEmacs code */
1367 /* Unwind the specpdl stack */
1368 unbind_to (c->pdlcount, Qnil);
1369 catchlist = c->next;
1372 gcprolist = c->gcpro;
1373 backtrace_list = c->backlist;
1374 lisp_eval_depth = c->lisp_eval_depth;
1376 #if 0 /* no longer used */
1379 LONGJMP (c->jmp, 1);
1382 static DOESNT_RETURN
1383 throw_or_bomb_out (Lisp_Object tag, Lisp_Object val, int bomb_out_p,
1384 Lisp_Object sig, Lisp_Object data)
1387 /* die if we recurse more than is reasonable */
1388 if (++throw_level > 20)
1392 /* If bomb_out_p is t, this is being called from Fsignal as a
1393 "last resort" when there is no handler for this error and
1394 the debugger couldn't be invoked, so we are throwing to
1395 'top-level. If this tag doesn't exist (happens during the
1396 initialization stages) we would get in an infinite recursive
1397 Fsignal/Fthrow loop, so instead we bomb out to the
1398 really-early-error-handler.
1400 Note that in fact the only time that the "last resort"
1401 occurs is when there's no catch for 'top-level -- the
1402 'top-level catch and the catch-all error handler are
1403 established at the same time, in initial_command_loop/
1406 #### Fix this horrifitude!
1411 REGISTER struct catchtag *c;
1414 if (!NILP (tag)) /* #### */
1416 for (c = catchlist; c; c = c->next)
1418 if (EQ (c->tag, tag))
1419 unwind_to_catch (c, val);
1422 tag = Fsignal (Qno_catch, list2 (tag, val));
1424 call1 (Qreally_early_error_handler, Fcons (sig, data));
1427 /* can't happen. who cares? - (Sun's compiler does) */
1428 /* throw_level--; */
1429 /* getting tired of compilation warnings */
1433 /* See above, where CATCHLIST is defined, for a description of how
1436 Fthrow() is also called by Fsignal(), to do a non-local jump
1437 back to the appropriate condition-case handler after (maybe)
1438 the debugger is entered. In that case, TAG is the value
1439 of Vcondition_handlers that was in place just after the
1440 condition-case handler was set up. The car of this will be
1441 some data referring to the handler: Its car will be Qunbound
1442 (thus, this tag can never be generated by Lisp code), and
1443 its CDR will be the HANDLERS argument to condition_case_1()
1444 (either Qerror, Qt, or a list of handlers as in `condition-case').
1445 This works fine because Fthrow() does not care what TAG was
1446 passed to it: it just looks up the catch list for something
1447 that is EQ() to TAG. When it finds it, it will longjmp()
1448 back to the place that established the catch (in this case,
1449 condition_case_1). See below for more info.
1452 DEFUN ("throw", Fthrow, 2, 2, 0, /*
1453 \(throw TAG VALUE): throw to the catch for TAG and return VALUE from it.
1454 Both TAG and VALUE are evalled.
1458 throw_or_bomb_out (tag, val, 0, Qnil, Qnil); /* Doesn't return */
1462 DEFUN ("unwind-protect", Funwind_protect, 1, UNEVALLED, 0, /*
1463 Do BODYFORM, protecting with UNWINDFORMS.
1464 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).
1465 If BODYFORM completes normally, its value is returned
1466 after executing the UNWINDFORMS.
1467 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1471 /* This function can GC */
1472 int speccount = specpdl_depth();
1474 record_unwind_protect (Fprogn, XCDR (args));
1475 return unbind_to (speccount, Feval (XCAR (args)));
1479 /************************************************************************/
1480 /* Signalling and trapping errors */
1481 /************************************************************************/
1484 condition_bind_unwind (Lisp_Object loser)
1486 struct Lisp_Cons *victim;
1487 /* ((handler-fun . handler-args) ... other handlers) */
1488 Lisp_Object tem = XCAR (loser);
1492 victim = XCONS (tem);
1496 victim = XCONS (loser);
1498 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1499 Vcondition_handlers = victim->cdr;
1506 condition_case_unwind (Lisp_Object loser)
1508 struct Lisp_Cons *victim;
1510 /* ((<unbound> . clauses) ... other handlers */
1511 victim = XCONS (XCAR (loser));
1514 victim = XCONS (loser);
1515 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1516 Vcondition_handlers = victim->cdr;
1522 /* Split out from condition_case_3 so that primitive C callers
1523 don't have to cons up a lisp handler form to be evaluated. */
1525 /* Call a function BFUN of one argument BARG, trapping errors as
1526 specified by HANDLERS. If no error occurs that is indicated by
1527 HANDLERS as something to be caught, the return value of this
1528 function is the return value from BFUN. If such an error does
1529 occur, HFUN is called, and its return value becomes the
1530 return value of condition_case_1(). The second argument passed
1531 to HFUN will always be HARG. The first argument depends on
1534 If HANDLERS is Qt, all errors (this includes QUIT, but not
1535 non-local exits with `throw') cause HFUN to be invoked, and VAL
1536 (the first argument to HFUN) is a cons (SIG . DATA) of the
1537 arguments passed to `signal'. The debugger is not invoked even if
1538 `debug-on-error' was set.
1540 A HANDLERS value of Qerror is the same as Qt except that the
1541 debugger is invoked if `debug-on-error' was set.
1543 Otherwise, HANDLERS should be a list of lists (CONDITION-NAME BODY ...)
1544 exactly as in `condition-case', and errors will be trapped
1545 as indicated in HANDLERS. VAL (the first argument to HFUN) will
1546 be a cons whose car is the cons (SIG . DATA) and whose CDR is the
1547 list (BODY ...) from the appropriate slot in HANDLERS.
1549 This function pushes HANDLERS onto the front of Vcondition_handlers
1550 (actually with a Qunbound marker as well -- see Fthrow() above
1551 for why), establishes a catch whose tag is this new value of
1552 Vcondition_handlers, and calls BFUN. When Fsignal() is called,
1553 it calls Fthrow(), setting TAG to this same new value of
1554 Vcondition_handlers and setting VAL to the same thing that will
1555 be passed to HFUN, as above. Fthrow() longjmp()s back to the
1556 jump point we just established, and we in turn just call the
1557 HFUN and return its value.
1559 For a real condition-case, HFUN will always be
1560 run_condition_case_handlers() and HARG is the argument VAR
1561 to condition-case. That function just binds VAR to the cons
1562 (SIG . DATA) that is the CAR of VAL, and calls the handler
1563 (BODY ...) that is the CDR of VAL. Note that before calling
1564 Fthrow(), Fsignal() restored Vcondition_handlers to the value
1565 it had *before* condition_case_1() was called. This maintains
1566 consistency (so that the state of things at exit of
1567 condition_case_1() is the same as at entry), and implies
1568 that the handler can signal the same error again (possibly
1569 after processing of its own), without getting in an infinite
1573 condition_case_1 (Lisp_Object handlers,
1574 Lisp_Object (*bfun) (Lisp_Object barg),
1576 Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg),
1579 int speccount = specpdl_depth();
1581 struct gcpro gcpro1;
1586 /* Do consing now so out-of-memory error happens up front */
1587 /* (unbound . stuff) is a special condition-case kludge marker
1588 which is known specially by Fsignal.
1589 This is an abomination, but to fix it would require either
1590 making condition_case cons (a union of the conditions of the clauses)
1591 or changing the byte-compiler output (no thanks). */
1592 c.tag = noseeum_cons (noseeum_cons (Qunbound, handlers),
1593 Vcondition_handlers);
1596 c.backlist = backtrace_list;
1599 c.handlerlist = handlerlist;
1601 c.lisp_eval_depth = lisp_eval_depth;
1602 c.pdlcount = specpdl_depth();
1604 c.poll_suppress_count = async_timer_suppress_count;
1606 c.gcpro = gcprolist;
1607 /* #### FSFmacs does the following statement *after* the setjmp(). */
1612 /* throw does ungcpro, etc */
1613 return (*hfun) (c.val, harg);
1616 record_unwind_protect (condition_case_unwind, c.tag);
1620 h.handler = handlers;
1622 h.next = handlerlist;
1626 Vcondition_handlers = c.tag;
1628 GCPRO1 (harg); /* Somebody has to gc-protect */
1630 c.val = ((*bfun) (barg));
1632 /* The following is *not* true: (ben)
1634 ungcpro, restoring catchlist and condition_handlers are actually
1635 redundant since unbind_to now restores them. But it looks funny not to
1636 have this code here, and it doesn't cost anything, so I'm leaving it.*/
1639 Vcondition_handlers = XCDR (c.tag);
1641 return unbind_to (speccount, c.val);
1645 run_condition_case_handlers (Lisp_Object val, Lisp_Object var)
1647 /* This function can GC */
1650 specbind (h.var, c.val);
1651 val = Fprogn (Fcdr (h.chosen_clause));
1653 /* Note that this just undoes the binding of h.var; whoever
1654 longjmp()ed to us unwound the stack to c.pdlcount before
1656 unbind_to (c.pdlcount, Qnil);
1661 CHECK_TRUE_LIST (val);
1663 return Fprogn (Fcdr (val)); /* tail call */
1665 speccount = specpdl_depth();
1666 specbind (var, Fcar (val));
1667 val = Fprogn (Fcdr (val));
1668 return unbind_to (speccount, val);
1672 /* Here for bytecode to call non-consfully. This is exactly like
1673 condition-case except that it takes three arguments rather
1674 than a single list of arguments. */
1676 condition_case_3 (Lisp_Object bodyform, Lisp_Object var, Lisp_Object handlers)
1678 /* This function can GC */
1679 Lisp_Object handler;
1681 EXTERNAL_LIST_LOOP_2 (handler, handlers)
1685 else if (CONSP (handler))
1687 Lisp_Object conditions = XCAR (handler);
1688 /* CONDITIONS must a condition name or a list of condition names */
1689 if (SYMBOLP (conditions))
1693 Lisp_Object condition;
1694 EXTERNAL_LIST_LOOP_2 (condition, conditions)
1695 if (!SYMBOLP (condition))
1696 goto invalid_condition_handler;
1701 invalid_condition_handler:
1702 signal_simple_error ("Invalid condition handler", handler);
1708 return condition_case_1 (handlers,
1710 run_condition_case_handlers,
1714 DEFUN ("condition-case", Fcondition_case, 2, UNEVALLED, 0, /*
1715 Regain control when an error is signalled.
1716 Usage looks like (condition-case VAR BODYFORM HANDLERS...).
1717 Executes BODYFORM and returns its value if no error happens.
1718 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1719 where the BODY is made of Lisp expressions.
1721 A handler is applicable to an error if CONDITION-NAME is one of the
1722 error's condition names. If an error happens, the first applicable
1723 handler is run. As a special case, a CONDITION-NAME of t matches
1724 all errors, even those without the `error' condition name on them
1727 The car of a handler may be a list of condition names
1728 instead of a single condition name.
1730 When a handler handles an error,
1731 control returns to the condition-case and the handler BODY... is executed
1732 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1733 VAR may be nil; then you do not get access to the signal information.
1735 The value of the last BODY form is returned from the condition-case.
1736 See also the function `signal' for more info.
1738 Note that at the time the condition handler is invoked, the Lisp stack
1739 and the current catches, condition-cases, and bindings have all been
1740 popped back to the state they were in just before the call to
1741 `condition-case'. This means that resignalling the error from
1742 within the handler will not result in an infinite loop.
1744 If you want to establish an error handler that is called with the
1745 Lisp stack, bindings, etc. as they were when `signal' was called,
1746 rather than when the handler was set, use `call-with-condition-handler'.
1750 /* This function can GC */
1751 Lisp_Object var = XCAR (args);
1752 Lisp_Object bodyform = XCAR (XCDR (args));
1753 Lisp_Object handlers = XCDR (XCDR (args));
1754 return condition_case_3 (bodyform, var, handlers);
1757 DEFUN ("call-with-condition-handler", Fcall_with_condition_handler, 2, MANY, 0, /*
1758 Regain control when an error is signalled, without popping the stack.
1759 Usage looks like (call-with-condition-handler HANDLER FUNCTION &rest ARGS).
1760 This function is similar to `condition-case', but the handler is invoked
1761 with the same environment (Lisp stack, bindings, catches, condition-cases)
1762 that was current when `signal' was called, rather than when the handler
1765 HANDLER should be a function of one argument, which is a cons of the args
1766 \(SIG . DATA) that were passed to `signal'. It is invoked whenever
1767 `signal' is called (this differs from `condition-case', which allows
1768 you to specify which errors are trapped). If the handler function
1769 returns, `signal' continues as if the handler were never invoked.
1770 \(It continues to look for handlers established earlier than this one,
1771 and invokes the standard error-handler if none is found.)
1773 (int nargs, Lisp_Object *args)) /* Note! Args side-effected! */
1775 /* This function can GC */
1776 int speccount = specpdl_depth();
1779 /* #### If there were a way to check that args[0] were a function
1780 which accepted one arg, that should be done here ... */
1782 /* (handler-fun . handler-args) */
1783 tem = noseeum_cons (list1 (args[0]), Vcondition_handlers);
1784 record_unwind_protect (condition_bind_unwind, tem);
1785 Vcondition_handlers = tem;
1787 /* Caller should have GC-protected args */
1788 return unbind_to (speccount, Ffuncall (nargs - 1, args + 1));
1792 condition_type_p (Lisp_Object type, Lisp_Object conditions)
1795 /* (condition-case c # (t c)) catches -all- signals
1796 * Use with caution! */
1800 return !NILP (Fmemq (type, conditions));
1802 for (; CONSP (type); type = XCDR (type))
1803 if (!NILP (Fmemq (XCAR (type), conditions)))
1810 return_from_signal (Lisp_Object value)
1813 /* Most callers are not prepared to handle gc if this
1814 returns. So, since this feature is not very useful,
1816 /* Have called debugger; return value to signaller */
1818 #else /* But the reality is that that stinks, because: */
1819 /* GACK!!! Really want some way for debug-on-quit errors
1820 to be continuable!! */
1821 error ("Returning a value from an error is no longer supported");
1825 extern int in_display;
1828 /************************************************************************/
1829 /* the workhorse error-signaling function */
1830 /************************************************************************/
1832 /* #### This function has not been synched with FSF. It diverges
1836 signal_1 (Lisp_Object sig, Lisp_Object data)
1838 /* This function can GC */
1839 struct gcpro gcpro1, gcpro2;
1840 Lisp_Object conditions;
1841 Lisp_Object handlers;
1842 /* signal_call_debugger() could get called more than once
1843 (once when a call-with-condition-handler is about to
1844 be dealt with, and another when a condition-case handler
1845 is about to be invoked). So make sure the debugger and/or
1846 stack trace aren't done more than once. */
1847 int stack_trace_displayed = 0;
1848 int debugger_entered = 0;
1849 GCPRO2 (conditions, handlers);
1853 /* who knows how much has been initialized? Safest bet is
1854 just to bomb out immediately. */
1855 fprintf (stderr, "Error before initialization is complete!\n");
1859 if (gc_in_progress || in_display)
1860 /* This is one of many reasons why you can't run lisp code from redisplay.
1861 There is no sensible way to handle errors there. */
1864 conditions = Fget (sig, Qerror_conditions, Qnil);
1866 for (handlers = Vcondition_handlers;
1868 handlers = XCDR (handlers))
1870 Lisp_Object handler_fun = XCAR (XCAR (handlers));
1871 Lisp_Object handler_data = XCDR (XCAR (handlers));
1872 Lisp_Object outer_handlers = XCDR (handlers);
1874 if (!UNBOUNDP (handler_fun))
1876 /* call-with-condition-handler */
1878 Lisp_Object all_handlers = Vcondition_handlers;
1879 struct gcpro ngcpro1;
1880 NGCPRO1 (all_handlers);
1881 Vcondition_handlers = outer_handlers;
1883 tem = signal_call_debugger (conditions, sig, data,
1885 &stack_trace_displayed,
1887 if (!UNBOUNDP (tem))
1888 RETURN_NUNGCPRO (return_from_signal (tem));
1890 tem = Fcons (sig, data);
1891 if (NILP (handler_data))
1892 tem = call1 (handler_fun, tem);
1895 /* (This code won't be used (for now?).) */
1896 struct gcpro nngcpro1;
1897 Lisp_Object args[3];
1900 args[0] = handler_fun;
1902 args[2] = handler_data;
1903 nngcpro1.var = args;
1904 tem = Fapply (3, args);
1909 if (!EQ (tem, Qsignal))
1910 return return_from_signal (tem);
1912 /* If handler didn't throw, try another handler */
1913 Vcondition_handlers = all_handlers;
1916 /* It's a condition-case handler */
1918 /* t is used by handlers for all conditions, set up by C code.
1919 * debugger is not called even if debug_on_error */
1920 else if (EQ (handler_data, Qt))
1923 return Fthrow (handlers, Fcons (sig, data));
1925 /* `error' is used similarly to the way `t' is used, but in
1926 addition it invokes the debugger if debug_on_error.
1927 This is normally used for the outer command-loop error
1929 else if (EQ (handler_data, Qerror))
1931 Lisp_Object tem = signal_call_debugger (conditions, sig, data,
1933 &stack_trace_displayed,
1937 if (!UNBOUNDP (tem))
1938 return return_from_signal (tem);
1940 tem = Fcons (sig, data);
1941 return Fthrow (handlers, tem);
1945 /* handler established by real (Lisp) condition-case */
1948 for (h = handler_data; CONSP (h); h = Fcdr (h))
1950 Lisp_Object clause = Fcar (h);
1951 Lisp_Object tem = Fcar (clause);
1953 if (condition_type_p (tem, conditions))
1955 tem = signal_call_debugger (conditions, sig, data,
1957 &stack_trace_displayed,
1960 if (!UNBOUNDP (tem))
1961 return return_from_signal (tem);
1963 /* Doesn't return */
1964 tem = Fcons (Fcons (sig, data), Fcdr (clause));
1965 return Fthrow (handlers, tem);
1971 /* If no handler is present now, try to run the debugger,
1972 and if that fails, throw to top level.
1974 #### The only time that no handler is present is during
1975 temacs or perhaps very early in XEmacs. In both cases,
1976 there is no 'top-level catch. (That's why the
1977 "bomb-out" hack was added.)
1979 #### Fix this horrifitude!
1981 signal_call_debugger (conditions, sig, data, Qnil, 0,
1982 &stack_trace_displayed,
1985 throw_or_bomb_out (Qtop_level, Qt, 1, sig, data); /* Doesn't return */
1990 /****************** Error functions class 1 ******************/
1992 /* Class 1: General functions that signal an error.
1993 These functions take an error type and a list of associated error
1996 /* The simplest external error function: it would be called
1997 signal_continuable_error() in the terminology below, but it's
2000 DEFUN ("signal", Fsignal, 2, 2, 0, /*
2001 Signal a continuable error. Args are ERROR-SYMBOL, and associated DATA.
2002 An error symbol is a symbol defined using `define-error'.
2003 DATA should be a list. Its elements are printed as part of the error message.
2004 If the signal is handled, DATA is made available to the handler.
2005 See also the function `signal-error', and the functions to handle errors:
2006 `condition-case' and `call-with-condition-handler'.
2008 Note that this function can return, if the debugger is invoked and the
2009 user invokes the "return from signal" option.
2011 (error_symbol, data))
2013 /* Fsignal() is one of these functions that's called all the time
2014 with newly-created Lisp objects. We allow this; but we must GC-
2015 protect the objects because all sorts of weird stuff could
2018 struct gcpro gcpro1;
2021 if (!NILP (Vcurrent_error_state))
2023 if (!NILP (Vcurrent_warning_class))
2024 warn_when_safe_lispobj (Vcurrent_warning_class, Qwarning,
2025 Fcons (error_symbol, data));
2026 Fthrow (Qunbound_suspended_errors_tag, Qnil);
2027 abort (); /* Better not get here! */
2029 RETURN_UNGCPRO (signal_1 (error_symbol, data));
2032 /* Signal a non-continuable error. */
2035 signal_error (Lisp_Object sig, Lisp_Object data)
2038 Fsignal (sig, data);
2042 call_with_suspended_errors_1 (Lisp_Object opaque_arg)
2045 Lisp_Object *kludgy_args = (Lisp_Object *) get_opaque_ptr (opaque_arg);
2046 PRIMITIVE_FUNCALL (val, get_opaque_ptr (kludgy_args[0]),
2047 kludgy_args + 2, XINT (kludgy_args[1]));
2052 restore_current_warning_class (Lisp_Object warning_class)
2054 Vcurrent_warning_class = warning_class;
2059 restore_current_error_state (Lisp_Object error_state)
2061 Vcurrent_error_state = error_state;
2065 /* Many functions would like to do one of three things if an error
2068 (1) signal the error, as usual.
2069 (2) silently fail and return some error value.
2070 (3) do as (2) but issue a warning in the process.
2072 Currently there's lots of stuff that passes an Error_behavior
2073 value and calls maybe_signal_error() and other such functions.
2074 This approach is inherently error-prone and broken. A much
2075 more robust and easier approach is to use call_with_suspended_errors().
2076 Wrap this around any function in which you might want errors
2081 call_with_suspended_errors (lisp_fn_t fun, volatile Lisp_Object retval,
2082 Lisp_Object class, Error_behavior errb,
2087 Lisp_Object kludgy_args[22];
2088 Lisp_Object *args = kludgy_args + 2;
2090 Lisp_Object no_error;
2092 assert (SYMBOLP (class)); /* sanity-check */
2093 assert (!NILP (class));
2094 assert (nargs >= 0 && nargs < 20);
2096 /* ERROR_ME means don't trap errors. (However, if errors are
2097 already trapped, we leave them trapped.)
2099 Otherwise, we trap errors, and trap warnings if ERROR_ME_WARN.
2101 If ERROR_ME_NOT, it causes no warnings even if warnings
2102 were previously enabled. However, we never change the
2103 warning class from one to another. */
2104 if (!ERRB_EQ (errb, ERROR_ME))
2106 if (ERRB_EQ (errb, ERROR_ME_NOT)) /* person wants no warnings */
2108 errb = ERROR_ME_NOT;
2114 va_start (vargs, nargs);
2115 for (i = 0; i < nargs; i++)
2116 args[i] = va_arg (vargs, Lisp_Object);
2119 /* If error-checking is not disabled, just call the function.
2120 It's important not to override disabled error-checking with
2121 enabled error-checking. */
2123 if (ERRB_EQ (errb, ERROR_ME))
2126 PRIMITIVE_FUNCALL (val, fun, args, nargs);
2130 speccount = specpdl_depth();
2131 if (NILP (class) || NILP (Vcurrent_warning_class))
2133 /* If we're currently calling for no warnings, then make it so.
2134 If we're currently calling for warnings and we weren't
2135 previously, then set our warning class; otherwise, leave
2136 the existing one alone. */
2137 record_unwind_protect (restore_current_warning_class,
2138 Vcurrent_warning_class);
2139 Vcurrent_warning_class = class;
2141 if (!EQ (Vcurrent_error_state, no_error))
2143 record_unwind_protect (restore_current_error_state,
2144 Vcurrent_error_state);
2145 Vcurrent_error_state = no_error;
2150 Lisp_Object the_retval;
2151 Lisp_Object opaque1 = make_opaque_ptr (kludgy_args);
2152 Lisp_Object opaque2 = make_opaque_ptr ((void *) fun);
2153 struct gcpro gcpro1, gcpro2;
2155 GCPRO2 (opaque1, opaque2);
2156 kludgy_args[0] = opaque2;
2157 kludgy_args[1] = make_int (nargs);
2158 the_retval = internal_catch (Qunbound_suspended_errors_tag,
2159 call_with_suspended_errors_1,
2161 free_opaque_ptr (opaque1);
2162 free_opaque_ptr (opaque2);
2164 /* Use the returned value except in non-local exit, when
2166 /* Some perverse compilers require the perverse cast below. */
2167 return unbind_to (speccount,
2168 threw ? *((Lisp_Object*) &(retval)) : the_retval);
2172 /* Signal a non-continuable error or display a warning or do nothing,
2173 according to ERRB. CLASS is the class of warning and should
2174 refer to what sort of operation is being done (e.g. Qtoolbar,
2175 Qresource, etc.). */
2178 maybe_signal_error (Lisp_Object sig, Lisp_Object data, Lisp_Object class,
2179 Error_behavior errb)
2181 if (ERRB_EQ (errb, ERROR_ME_NOT))
2183 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2184 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2187 Fsignal (sig, data);
2190 /* Signal a continuable error or display a warning or do nothing,
2191 according to ERRB. */
2194 maybe_signal_continuable_error (Lisp_Object sig, Lisp_Object data,
2195 Lisp_Object class, Error_behavior errb)
2197 if (ERRB_EQ (errb, ERROR_ME_NOT))
2199 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2201 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2205 return Fsignal (sig, data);
2209 /****************** Error functions class 2 ******************/
2211 /* Class 2: Printf-like functions that signal an error.
2212 These functions signal an error of type Qerror, whose data
2213 is a single string, created using the arguments. */
2215 /* dump an error message; called like printf */
2218 error (CONST char *fmt, ...)
2223 va_start (args, fmt);
2224 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2228 /* Fsignal GC-protects its args */
2229 signal_error (Qerror, list1 (obj));
2233 maybe_error (Lisp_Object class, Error_behavior errb, CONST char *fmt, ...)
2239 if (ERRB_EQ (errb, ERROR_ME_NOT))
2242 va_start (args, fmt);
2243 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2247 /* Fsignal GC-protects its args */
2248 maybe_signal_error (Qerror, list1 (obj), class, errb);
2252 continuable_error (CONST char *fmt, ...)
2257 va_start (args, fmt);
2258 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2262 /* Fsignal GC-protects its args */
2263 return Fsignal (Qerror, list1 (obj));
2267 maybe_continuable_error (Lisp_Object class, Error_behavior errb,
2268 CONST char *fmt, ...)
2274 if (ERRB_EQ (errb, ERROR_ME_NOT))
2277 va_start (args, fmt);
2278 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2282 /* Fsignal GC-protects its args */
2283 return maybe_signal_continuable_error (Qerror, list1 (obj), class, errb);
2287 /****************** Error functions class 3 ******************/
2289 /* Class 3: Signal an error with a string and an associated object.
2290 These functions signal an error of type Qerror, whose data
2291 is two objects, a string and a related Lisp object (usually the object
2292 where the error is occurring). */
2295 signal_simple_error (CONST char *reason, Lisp_Object frob)
2297 signal_error (Qerror, list2 (build_translated_string (reason), frob));
2301 maybe_signal_simple_error (CONST char *reason, Lisp_Object frob,
2302 Lisp_Object class, Error_behavior errb)
2305 if (ERRB_EQ (errb, ERROR_ME_NOT))
2307 maybe_signal_error (Qerror, list2 (build_translated_string (reason), frob),
2312 signal_simple_continuable_error (CONST char *reason, Lisp_Object frob)
2314 return Fsignal (Qerror, list2 (build_translated_string (reason), frob));
2318 maybe_signal_simple_continuable_error (CONST char *reason, Lisp_Object frob,
2319 Lisp_Object class, Error_behavior errb)
2322 if (ERRB_EQ (errb, ERROR_ME_NOT))
2324 return maybe_signal_continuable_error
2325 (Qerror, list2 (build_translated_string (reason),
2326 frob), class, errb);
2330 /****************** Error functions class 4 ******************/
2332 /* Class 4: Printf-like functions that signal an error.
2333 These functions signal an error of type Qerror, whose data
2334 is a two objects, a string (created using the arguments) and a
2339 error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2344 va_start (args, fmt);
2345 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2349 /* Fsignal GC-protects its args */
2350 signal_error (Qerror, list2 (obj, frob));
2354 maybe_error_with_frob (Lisp_Object frob, Lisp_Object class,
2355 Error_behavior errb, CONST char *fmt, ...)
2361 if (ERRB_EQ (errb, ERROR_ME_NOT))
2364 va_start (args, fmt);
2365 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2369 /* Fsignal GC-protects its args */
2370 maybe_signal_error (Qerror, list2 (obj, frob), class, errb);
2374 continuable_error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2379 va_start (args, fmt);
2380 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2384 /* Fsignal GC-protects its args */
2385 return Fsignal (Qerror, list2 (obj, frob));
2389 maybe_continuable_error_with_frob (Lisp_Object frob, Lisp_Object class,
2390 Error_behavior errb, CONST char *fmt, ...)
2396 if (ERRB_EQ (errb, ERROR_ME_NOT))
2399 va_start (args, fmt);
2400 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2404 /* Fsignal GC-protects its args */
2405 return maybe_signal_continuable_error (Qerror, list2 (obj, frob),
2410 /****************** Error functions class 5 ******************/
2412 /* Class 5: Signal an error with a string and two associated objects.
2413 These functions signal an error of type Qerror, whose data
2414 is three objects, a string and two related Lisp objects. */
2417 signal_simple_error_2 (CONST char *reason,
2418 Lisp_Object frob0, Lisp_Object frob1)
2420 signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2425 maybe_signal_simple_error_2 (CONST char *reason, Lisp_Object frob0,
2426 Lisp_Object frob1, Lisp_Object class,
2427 Error_behavior errb)
2430 if (ERRB_EQ (errb, ERROR_ME_NOT))
2432 maybe_signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2433 frob1), class, errb);
2438 signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2441 return Fsignal (Qerror, list3 (build_translated_string (reason), frob0,
2446 maybe_signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2447 Lisp_Object frob1, Lisp_Object class,
2448 Error_behavior errb)
2451 if (ERRB_EQ (errb, ERROR_ME_NOT))
2453 return maybe_signal_continuable_error
2454 (Qerror, list3 (build_translated_string (reason), frob0,
2460 /* This is what the QUIT macro calls to signal a quit */
2464 /* This function can GC */
2465 if (EQ (Vquit_flag, Qcritical))
2466 debug_on_quit |= 2; /* set critical bit. */
2468 /* note that this is continuable. */
2469 Fsignal (Qquit, Qnil);
2473 /* Used in core lisp functions for efficiency */
2475 signal_void_function_error (Lisp_Object function)
2477 Fsignal (Qvoid_function, list1 (function));
2481 signal_invalid_function_error (Lisp_Object function)
2483 Fsignal (Qinvalid_function, list1 (function));
2487 signal_wrong_number_of_arguments_error (Lisp_Object function, int nargs)
2489 Fsignal (Qwrong_number_of_arguments, list2 (function, make_int (nargs)));
2492 /* Used in list traversal macros for efficiency. */
2494 signal_malformed_list_error (Lisp_Object list)
2496 Fsignal (Qmalformed_list, list1 (list));
2500 signal_malformed_property_list_error (Lisp_Object list)
2502 Fsignal (Qmalformed_property_list, list1 (list));
2506 signal_circular_list_error (Lisp_Object list)
2508 Fsignal (Qcircular_list, list1 (list));
2512 signal_circular_property_list_error (Lisp_Object list)
2514 Fsignal (Qcircular_property_list, list1 (list));
2517 /************************************************************************/
2519 /************************************************************************/
2521 DEFUN ("commandp", Fcommandp, 1, 1, 0, /*
2522 Return t if FUNCTION makes provisions for interactive calling.
2523 This means it contains a description for how to read arguments to give it.
2524 The value is nil for an invalid function or a symbol with no function
2527 Interactively callable functions include
2529 -- strings and vectors (treated as keyboard macros)
2530 -- lambda-expressions that contain a top-level call to `interactive'
2531 -- autoload definitions made by `autoload' with non-nil fourth argument
2532 (i.e. the interactive flag)
2533 -- compiled-function objects with a non-nil `compiled-function-interactive'
2535 -- subrs (built-in functions) that are interactively callable
2537 Also, a symbol satisfies `commandp' if its function definition does so.
2541 Lisp_Object fun = indirect_function (function, 0);
2543 if (COMPILED_FUNCTIONP (fun))
2544 return XCOMPILED_FUNCTION (fun)->flags.interactivep ? Qt : Qnil;
2546 /* Lists may represent commands. */
2549 Lisp_Object funcar = XCAR (fun);
2550 if (EQ (funcar, Qlambda))
2551 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
2552 if (EQ (funcar, Qautoload))
2553 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
2558 /* Emacs primitives are interactive if their DEFUN specifies an
2559 interactive spec. */
2561 return XSUBR (fun)->prompt ? Qt : Qnil;
2563 /* Strings and vectors are keyboard macros. */
2564 if (VECTORP (fun) || STRINGP (fun))
2567 /* Everything else (including Qunbound) is not a command. */
2571 DEFUN ("command-execute", Fcommand_execute, 1, 3, 0, /*
2572 Execute CMD as an editor command.
2573 CMD must be an object that satisfies the `commandp' predicate.
2574 Optional second arg RECORD-FLAG is as in `call-interactively'.
2575 The argument KEYS specifies the value to use instead of (this-command-keys)
2576 when reading the arguments.
2578 (cmd, record, keys))
2580 /* This function can GC */
2581 Lisp_Object prefixarg;
2582 Lisp_Object final = cmd;
2583 struct backtrace backtrace;
2584 struct console *con = XCONSOLE (Vselected_console);
2586 prefixarg = con->prefix_arg;
2587 con->prefix_arg = Qnil;
2588 Vcurrent_prefix_arg = prefixarg;
2589 debug_on_next_call = 0; /* #### from FSFmacs; correct? */
2591 if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil)))
2592 return run_hook (Vdisabled_command_hook);
2596 final = indirect_function (cmd, 1);
2597 if (CONSP (final) && EQ (Fcar (final), Qautoload))
2598 do_autoload (final, cmd);
2603 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final))
2605 backtrace.function = &Qcall_interactively;
2606 backtrace.args = &cmd;
2607 backtrace.nargs = 1;
2608 backtrace.evalargs = 0;
2609 backtrace.pdlcount = specpdl_depth();
2610 backtrace.debug_on_exit = 0;
2611 PUSH_BACKTRACE (backtrace);
2613 final = Fcall_interactively (cmd, record, keys);
2615 POP_BACKTRACE (backtrace);
2618 else if (STRINGP (final) || VECTORP (final))
2620 return Fexecute_kbd_macro (final, prefixarg);
2624 Fsignal (Qwrong_type_argument,
2628 : list2 (cmd, final))));
2633 DEFUN ("interactive-p", Finteractive_p, 0, 0, 0, /*
2634 Return t if function in which this appears was called interactively.
2635 This means that the function was called with call-interactively (which
2636 includes being called as the binding of a key)
2637 and input is currently coming from the keyboard (not in keyboard macro).
2641 REGISTER struct backtrace *btp;
2642 REGISTER Lisp_Object fun;
2647 /* Unless the object was compiled, skip the frame of interactive-p itself
2648 (if interpreted) or the frame of byte-code (if called from a compiled
2649 function). Note that *btp->function may be a symbol pointing at a
2650 compiled function. */
2651 btp = backtrace_list;
2655 /* #### FSFmacs does the following instead. I can't figure
2656 out which one is more correct. */
2657 /* If this isn't a byte-compiled function, there may be a frame at
2658 the top for Finteractive_p itself. If so, skip it. */
2659 fun = Findirect_function (*btp->function);
2660 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
2663 /* If we're running an Emacs 18-style byte-compiled function, there
2664 may be a frame for Fbyte_code. Now, given the strictest
2665 definition, this function isn't really being called
2666 interactively, but because that's the way Emacs 18 always builds
2667 byte-compiled functions, we'll accept it for now. */
2668 if (EQ (*btp->function, Qbyte_code))
2671 /* If this isn't a byte-compiled function, then we may now be
2672 looking at several frames for special forms. Skip past them. */
2674 btp->nargs == UNEVALLED)
2679 if (! (COMPILED_FUNCTIONP (Findirect_function (*btp->function))))
2682 btp && (btp->nargs == UNEVALLED
2683 || EQ (*btp->function, Qbyte_code));
2686 /* btp now points at the frame of the innermost function
2687 that DOES eval its args.
2688 If it is a built-in function (such as load or eval-region)
2690 /* Beats me why this is necessary, but it is */
2691 if (btp && EQ (*btp->function, Qcall_interactively))
2696 fun = Findirect_function (*btp->function);
2699 /* btp points to the frame of a Lisp function that called interactive-p.
2700 Return t if that function was called interactively. */
2701 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
2707 /************************************************************************/
2709 /************************************************************************/
2711 DEFUN ("autoload", Fautoload, 2, 5, 0, /*
2712 Define FUNCTION to autoload from FILE.
2713 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2714 Third arg DOCSTRING is documentation for the function.
2715 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2716 Fifth arg TYPE indicates the type of the object:
2717 nil or omitted says FUNCTION is a function,
2718 `keymap' says FUNCTION is really a keymap, and
2719 `macro' or t says FUNCTION is really a macro.
2720 Third through fifth args give info about the real definition.
2721 They default to nil.
2722 If FUNCTION is already defined other than as an autoload,
2723 this does nothing and returns nil.
2725 (function, file, docstring, interactive, type))
2727 /* This function can GC */
2728 CHECK_SYMBOL (function);
2729 CHECK_STRING (file);
2731 /* If function is defined and not as an autoload, don't override */
2733 Lisp_Object f = XSYMBOL (function)->function;
2734 if (!UNBOUNDP (f) && !(CONSP (f) && EQ (XCAR (f), Qautoload)))
2740 /* Attempt to avoid consing identical (string=) pure strings. */
2741 file = Fsymbol_name (Fintern (file, Qnil));
2744 return Ffset (function, Fcons (Qautoload, list4 (file,
2751 un_autoload (Lisp_Object oldqueue)
2753 /* This function can GC */
2754 REGISTER Lisp_Object queue, first, second;
2756 /* Queue to unwind is current value of Vautoload_queue.
2757 oldqueue is the shadowed value to leave in Vautoload_queue. */
2758 queue = Vautoload_queue;
2759 Vautoload_queue = oldqueue;
2760 while (CONSP (queue))
2762 first = XCAR (queue);
2763 second = Fcdr (first);
2764 first = Fcar (first);
2768 Ffset (first, second);
2769 queue = Fcdr (queue);
2775 do_autoload (Lisp_Object fundef,
2776 Lisp_Object funname)
2778 /* This function can GC */
2779 int speccount = specpdl_depth();
2780 Lisp_Object fun = funname;
2781 struct gcpro gcpro1, gcpro2;
2783 CHECK_SYMBOL (funname);
2784 GCPRO2 (fun, funname);
2786 /* Value saved here is to be restored into Vautoload_queue */
2787 record_unwind_protect (un_autoload, Vautoload_queue);
2788 Vautoload_queue = Qt;
2789 call4 (Qload, Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);
2794 /* Save the old autoloads, in case we ever do an unload. */
2795 for (queue = Vautoload_queue; CONSP (queue); queue = XCDR (queue))
2797 Lisp_Object first = XCAR (queue);
2798 Lisp_Object second = Fcdr (first);
2800 first = Fcar (first);
2802 /* Note: This test is subtle. The cdr of an autoload-queue entry
2803 may be an atom if the autoload entry was generated by a defalias
2806 Fput (first, Qautoload, (XCDR (second)));
2810 /* Once loading finishes, don't undo it. */
2811 Vautoload_queue = Qt;
2812 unbind_to (speccount, Qnil);
2814 fun = indirect_function (fun, 0);
2817 if (!NILP (Fequal (fun, fundef)))
2821 && EQ (XCAR (fun), Qautoload)))
2823 error ("Autoloading failed to define function %s",
2824 string_data (XSYMBOL (funname)->name));
2829 /************************************************************************/
2830 /* eval, funcall, apply */
2831 /************************************************************************/
2833 static Lisp_Object funcall_lambda (Lisp_Object fun,
2834 int nargs, Lisp_Object args[]);
2835 static int in_warnings;
2838 in_warnings_restore (Lisp_Object minimus)
2844 DEFUN ("eval", Feval, 1, 1, 0, /*
2845 Evaluate FORM and return its value.
2849 /* This function can GC */
2850 Lisp_Object fun, val, original_fun, original_args;
2852 struct backtrace backtrace;
2854 /* I think this is a pretty safe place to call Lisp code, don't you? */
2855 while (!in_warnings && !NILP (Vpending_warnings))
2857 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2858 int speccount = specpdl_depth();
2859 Lisp_Object this_warning_cons, this_warning, class, level, messij;
2861 record_unwind_protect (in_warnings_restore, Qnil);
2863 this_warning_cons = Vpending_warnings;
2864 this_warning = XCAR (this_warning_cons);
2865 /* in case an error occurs in the warn function, at least
2866 it won't happen infinitely */
2867 Vpending_warnings = XCDR (Vpending_warnings);
2868 free_cons (XCONS (this_warning_cons));
2869 class = XCAR (this_warning);
2870 level = XCAR (XCDR (this_warning));
2871 messij = XCAR (XCDR (XCDR (this_warning)));
2872 free_list (this_warning);
2874 if (NILP (Vpending_warnings))
2875 Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary,
2878 GCPRO4 (form, class, level, messij);
2879 if (!STRINGP (messij))
2880 messij = Fprin1_to_string (messij, Qnil);
2881 call3 (Qdisplay_warning, class, messij, level);
2883 unbind_to (speccount, Qnil);
2889 return Fsymbol_value (form);
2895 if ((consing_since_gc > gc_cons_threshold) || always_gc)
2897 struct gcpro gcpro1;
2899 garbage_collect_1 ();
2903 if (++lisp_eval_depth > max_lisp_eval_depth)
2905 if (max_lisp_eval_depth < 100)
2906 max_lisp_eval_depth = 100;
2907 if (lisp_eval_depth > max_lisp_eval_depth)
2908 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2911 /* We guaranteed CONSP (form) above */
2912 original_fun = XCAR (form);
2913 original_args = XCDR (form);
2915 GET_EXTERNAL_LIST_LENGTH (original_args, nargs);
2917 backtrace.pdlcount = specpdl_depth();
2918 backtrace.function = &original_fun; /* This also protects them from gc */
2919 backtrace.args = &original_args;
2920 backtrace.nargs = UNEVALLED;
2921 backtrace.evalargs = 1;
2922 backtrace.debug_on_exit = 0;
2923 PUSH_BACKTRACE (backtrace);
2925 if (debug_on_next_call)
2926 do_debug_on_call (Qt);
2928 if (profiling_active)
2929 profile_increase_call_count (original_fun);
2931 /* At this point, only original_fun and original_args
2932 have values that will be used below. */
2934 fun = indirect_function (original_fun, 1);
2938 Lisp_Subr *subr = XSUBR (fun);
2939 int max_args = subr->max_args;
2941 if (nargs < subr->min_args)
2942 goto wrong_number_of_arguments;
2944 if (max_args == UNEVALLED) /* Optimize for the common case */
2946 backtrace.evalargs = 0;
2947 val = (((Lisp_Object (*) (Lisp_Object)) subr_function (subr))
2950 else if (nargs <= max_args)
2952 struct gcpro gcpro1;
2953 Lisp_Object args[SUBR_MAX_ARGS];
2954 REGISTER Lisp_Object *p = args;
2960 REGISTER Lisp_Object arg;
2961 LIST_LOOP_2 (arg, original_args)
2968 /* &optional args default to nil. */
2969 while (p - args < max_args)
2972 backtrace.args = args;
2973 backtrace.nargs = nargs;
2975 FUNCALL_SUBR (val, subr, args, max_args);
2979 else if (max_args == MANY)
2981 /* Pass a vector of evaluated arguments */
2982 struct gcpro gcpro1;
2983 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
2984 REGISTER Lisp_Object *p = args;
2990 REGISTER Lisp_Object arg;
2991 LIST_LOOP_2 (arg, original_args)
2998 backtrace.args = args;
2999 backtrace.nargs = nargs;
3001 val = (((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr))
3008 wrong_number_of_arguments:
3009 signal_wrong_number_of_arguments_error (fun, nargs);
3012 else if (COMPILED_FUNCTIONP (fun))
3014 struct gcpro gcpro1;
3015 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
3016 REGISTER Lisp_Object *p = args;
3022 REGISTER Lisp_Object arg;
3023 LIST_LOOP_2 (arg, original_args)
3030 backtrace.args = args;
3031 backtrace.nargs = nargs;
3032 backtrace.evalargs = 0;
3034 val = funcall_compiled_function (fun, nargs, args);
3036 /* Do the debug-on-exit now, while args is still GCPROed. */
3037 if (backtrace.debug_on_exit)
3038 val = do_debug_on_exit (val);
3039 /* Don't do it again when we return to eval. */
3040 backtrace.debug_on_exit = 0;
3044 else if (CONSP (fun))
3046 Lisp_Object funcar = XCAR (fun);
3048 if (EQ (funcar, Qautoload))
3050 do_autoload (fun, original_fun);
3053 else if (EQ (funcar, Qmacro))
3055 val = Feval (apply1 (XCDR (fun), original_args));
3057 else if (EQ (funcar, Qlambda))
3059 struct gcpro gcpro1;
3060 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
3061 REGISTER Lisp_Object *p = args;
3067 REGISTER Lisp_Object arg;
3068 LIST_LOOP_2 (arg, original_args)
3077 backtrace.args = args; /* this also GCPROs `args' */
3078 backtrace.nargs = nargs;
3079 backtrace.evalargs = 0;
3081 val = funcall_lambda (fun, nargs, args);
3083 /* Do the debug-on-exit now, while args is still GCPROed. */
3084 if (backtrace.debug_on_exit)
3085 val = do_debug_on_exit (val);
3086 /* Don't do it again when we return to eval. */
3087 backtrace.debug_on_exit = 0;
3091 goto invalid_function;
3094 else /* ! (SUBRP (fun) || COMPILED_FUNCTIONP (fun) || CONSP (fun)) */
3097 signal_invalid_function_error (fun);
3101 if (backtrace.debug_on_exit)
3102 val = do_debug_on_exit (val);
3103 POP_BACKTRACE (backtrace);
3108 DEFUN ("funcall", Ffuncall, 1, MANY, 0, /*
3109 Call first argument as a function, passing the remaining arguments to it.
3110 Thus, (funcall 'cons 'x 'y) returns (x . y).
3112 (int nargs, Lisp_Object *args))
3114 /* This function can GC */
3117 struct backtrace backtrace;
3118 int fun_nargs = nargs - 1;
3119 Lisp_Object *fun_args = args + 1;
3122 if ((consing_since_gc > gc_cons_threshold) || always_gc)
3123 /* Callers should gcpro lexpr args */
3124 garbage_collect_1 ();
3126 if (++lisp_eval_depth > max_lisp_eval_depth)
3128 if (max_lisp_eval_depth < 100)
3129 max_lisp_eval_depth = 100;
3130 if (lisp_eval_depth > max_lisp_eval_depth)
3131 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
3134 backtrace.pdlcount = specpdl_depth();
3135 backtrace.function = &args[0];
3136 backtrace.args = fun_args;
3137 backtrace.nargs = fun_nargs;
3138 backtrace.evalargs = 0;
3139 backtrace.debug_on_exit = 0;
3140 PUSH_BACKTRACE (backtrace);
3142 if (debug_on_next_call)
3143 do_debug_on_call (Qlambda);
3149 /* It might be useful to place this *after* all the checks. */
3150 if (profiling_active)
3151 profile_increase_call_count (fun);
3153 /* We could call indirect_function directly, but profiling shows
3154 this is worth optimizing by partially unrolling the loop. */
3157 fun = XSYMBOL (fun)->function;
3160 fun = XSYMBOL (fun)->function;
3162 fun = indirect_function (fun, 1);
3168 Lisp_Subr *subr = XSUBR (fun);
3169 int max_args = subr->max_args;
3170 Lisp_Object spacious_args[SUBR_MAX_ARGS];
3172 if (fun_nargs < subr->min_args)
3173 goto wrong_number_of_arguments;
3175 if (fun_nargs == max_args) /* Optimize for the common case */
3178 FUNCALL_SUBR (val, subr, fun_args, max_args);
3180 else if (fun_nargs < max_args)
3182 Lisp_Object *p = spacious_args;
3184 /* Default optionals to nil */
3187 while (p - spacious_args < max_args)
3190 fun_args = spacious_args;
3193 else if (max_args == MANY)
3195 val = ((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr))
3196 (fun_nargs, fun_args);
3198 else if (max_args == UNEVALLED) /* Can't funcall a special form */
3200 goto invalid_function;
3204 wrong_number_of_arguments:
3205 signal_wrong_number_of_arguments_error (fun, fun_nargs);
3208 else if (COMPILED_FUNCTIONP (fun))
3210 val = funcall_compiled_function (fun, fun_nargs, fun_args);
3212 else if (CONSP (fun))
3214 Lisp_Object funcar = XCAR (fun);
3216 if (EQ (funcar, Qlambda))
3218 val = funcall_lambda (fun, fun_nargs, fun_args);
3220 else if (EQ (funcar, Qautoload))
3222 do_autoload (fun, args[0]);
3225 else /* Can't funcall a macro */
3227 goto invalid_function;
3230 else if (UNBOUNDP (fun))
3232 signal_void_function_error (args[0]);
3237 signal_invalid_function_error (fun);
3241 if (backtrace.debug_on_exit)
3242 val = do_debug_on_exit (val);
3243 POP_BACKTRACE (backtrace);
3247 DEFUN ("functionp", Ffunctionp, 1, 1, 0, /*
3248 Return t if OBJECT can be called as a function, else nil.
3249 A function is an object that can be applied to arguments,
3250 using for example `funcall' or `apply'.
3254 if (SYMBOLP (object))
3255 object = indirect_function (object, 0);
3259 COMPILED_FUNCTIONP (object) ||
3261 (EQ (XCAR (object), Qlambda) ||
3262 EQ (XCAR (object), Qautoload))))
3267 function_argcount (Lisp_Object function, int function_min_args_p)
3269 Lisp_Object orig_function = function;
3270 Lisp_Object arglist;
3274 if (SYMBOLP (function))
3275 function = indirect_function (function, 1);
3277 if (SUBRP (function))
3279 return function_min_args_p ?
3280 Fsubr_min_args (function):
3281 Fsubr_max_args (function);
3283 else if (COMPILED_FUNCTIONP (function))
3285 arglist = compiled_function_arglist (XCOMPILED_FUNCTION (function));
3287 else if (CONSP (function))
3289 Lisp_Object funcar = XCAR (function);
3291 if (EQ (funcar, Qmacro))
3293 function = XCDR (function);
3296 else if (EQ (funcar, Qautoload))
3298 do_autoload (function, orig_function);
3301 else if (EQ (funcar, Qlambda))
3303 arglist = Fcar (XCDR (function));
3307 goto invalid_function;
3313 return Fsignal (Qinvalid_function, list1 (function));
3320 EXTERNAL_LIST_LOOP_2 (arg, arglist)
3322 if (EQ (arg, Qand_optional))
3324 if (function_min_args_p)
3327 else if (EQ (arg, Qand_rest))
3329 if (function_min_args_p)
3340 return make_int (argcount);
3344 DEFUN ("function-min-args", Ffunction_min_args, 1, 1, 0, /*
3345 Return the number of arguments a function may be called with.
3346 The function may be any form that can be passed to `funcall',
3347 any special form, or any macro.
3351 return function_argcount (function, 1);
3354 DEFUN ("function-max-args", Ffunction_max_args, 1, 1, 0, /*
3355 Return the number of arguments a function may be called with.
3356 The function may be any form that can be passed to `funcall',
3357 any special form, or any macro.
3358 If the function takes an arbitrary number of arguments or is
3359 a built-in special form, nil is returned.
3363 return function_argcount (function, 0);
3367 DEFUN ("apply", Fapply, 2, MANY, 0, /*
3368 Call FUNCTION with the remaining args, using the last arg as a list of args.
3369 Thus, (apply '+ 1 2 '(3 4)) returns 10.
3371 (int nargs, Lisp_Object *args))
3373 /* This function can GC */
3374 Lisp_Object fun = args[0];
3375 Lisp_Object spread_arg = args [nargs - 1];
3379 GET_EXTERNAL_LIST_LENGTH (spread_arg, numargs);
3382 /* (apply foo 0 1 '()) */
3383 return Ffuncall (nargs - 1, args);
3384 else if (numargs == 1)
3386 /* (apply foo 0 1 '(2)) */
3387 args [nargs - 1] = XCAR (spread_arg);
3388 return Ffuncall (nargs, args);
3391 /* -1 for function, -1 for spread arg */
3392 numargs = nargs - 2 + numargs;
3393 /* +1 for function */
3394 funcall_nargs = 1 + numargs;
3397 fun = indirect_function (fun, 0);
3401 Lisp_Subr *subr = XSUBR (fun);
3402 int max_args = subr->max_args;
3404 if (numargs < subr->min_args
3405 || (max_args >= 0 && max_args < numargs))
3407 /* Let funcall get the error */
3409 else if (max_args > numargs)
3411 /* Avoid having funcall cons up yet another new vector of arguments
3412 by explicitly supplying nil's for optional values */
3413 funcall_nargs += (max_args - numargs);
3416 else if (UNBOUNDP (fun))
3418 /* Let funcall get the error */
3424 Lisp_Object *funcall_args = alloca_array (Lisp_Object, funcall_nargs);
3425 struct gcpro gcpro1;
3427 GCPRO1 (*funcall_args);
3428 gcpro1.nvars = funcall_nargs;
3430 /* Copy in the unspread args */
3431 memcpy (funcall_args, args, (nargs - 1) * sizeof (Lisp_Object));
3432 /* Spread the last arg we got. Its first element goes in
3433 the slot that it used to occupy, hence this value of I. */
3435 !NILP (spread_arg); /* i < 1 + numargs */
3436 i++, spread_arg = XCDR (spread_arg))
3438 funcall_args [i] = XCAR (spread_arg);
3440 /* Supply nil for optional args (to subrs) */
3441 for (; i < funcall_nargs; i++)
3442 funcall_args[i] = Qnil;
3445 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
3450 /* Apply lambda list FUN to the NARGS evaluated arguments in ARGS and
3451 return the result of evaluation. */
3454 funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object args[])
3456 /* This function can GC */
3457 Lisp_Object symbol, arglist, body, tail;
3458 int speccount = specpdl_depth();
3464 goto invalid_function;
3466 arglist = XCAR (tail);
3470 int optional = 0, rest = 0;
3472 EXTERNAL_LIST_LOOP_3 (symbol, arglist, tail)
3474 if (!SYMBOLP (symbol))
3475 goto invalid_function;
3476 if (EQ (symbol, Qand_rest))
3478 else if (EQ (symbol, Qand_optional))
3482 specbind (symbol, Flist (nargs - i, &args[i]));
3486 specbind (symbol, args[i++]);
3488 goto wrong_number_of_arguments;
3490 specbind (symbol, Qnil);
3495 goto wrong_number_of_arguments;
3497 return unbind_to (speccount, Fprogn (body));
3499 wrong_number_of_arguments:
3500 return Fsignal (Qwrong_number_of_arguments, list2 (fun, make_int (nargs)));
3503 return Fsignal (Qinvalid_function, list1 (fun));
3507 /************************************************************************/
3508 /* Run hook variables in various ways. */
3509 /************************************************************************/
3511 DEFUN ("run-hooks", Frun_hooks, 1, MANY, 0, /*
3512 Run each hook in HOOKS. Major mode functions use this.
3513 Each argument should be a symbol, a hook variable.
3514 These symbols are processed in the order specified.
3515 If a hook symbol has a non-nil value, that value may be a function
3516 or a list of functions to be called to run the hook.
3517 If the value is a function, it is called with no arguments.
3518 If it is a list, the elements are called, in order, with no arguments.
3520 To make a hook variable buffer-local, use `make-local-hook',
3521 not `make-local-variable'.
3523 (int nargs, Lisp_Object *args))
3527 for (i = 0; i < nargs; i++)
3528 run_hook_with_args (1, args + i, RUN_HOOKS_TO_COMPLETION);
3533 DEFUN ("run-hook-with-args", Frun_hook_with_args, 1, MANY, 0, /*
3534 Run HOOK with the specified arguments ARGS.
3535 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
3536 value, that value may be a function or a list of functions to be
3537 called to run the hook. If the value is a function, it is called with
3538 the given arguments and its return value is returned. If it is a list
3539 of functions, those functions are called, in order,
3540 with the given arguments ARGS.
3541 It is best not to depend on the value return by `run-hook-with-args',
3544 To make a hook variable buffer-local, use `make-local-hook',
3545 not `make-local-variable'.
3547 (int nargs, Lisp_Object *args))
3549 return run_hook_with_args (nargs, args, RUN_HOOKS_TO_COMPLETION);
3552 DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, 1, MANY, 0, /*
3553 Run HOOK with the specified arguments ARGS.
3554 HOOK should be a symbol, a hook variable. Its value should
3555 be a list of functions. We call those functions, one by one,
3556 passing arguments ARGS to each of them, until one of them
3557 returns a non-nil value. Then we return that value.
3558 If all the functions return nil, we return nil.
3560 To make a hook variable buffer-local, use `make-local-hook',
3561 not `make-local-variable'.
3563 (int nargs, Lisp_Object *args))
3565 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_SUCCESS);
3568 DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, 1, MANY, 0, /*
3569 Run HOOK with the specified arguments ARGS.
3570 HOOK should be a symbol, a hook variable. Its value should
3571 be a list of functions. We call those functions, one by one,
3572 passing arguments ARGS to each of them, until one of them
3573 returns nil. Then we return nil.
3574 If all the functions return non-nil, we return non-nil.
3576 To make a hook variable buffer-local, use `make-local-hook',
3577 not `make-local-variable'.
3579 (int nargs, Lisp_Object *args))
3581 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_FAILURE);
3584 /* ARGS[0] should be a hook symbol.
3585 Call each of the functions in the hook value, passing each of them
3586 as arguments all the rest of ARGS (all NARGS - 1 elements).
3587 COND specifies a condition to test after each call
3588 to decide whether to stop.
3589 The caller (or its caller, etc) must gcpro all of ARGS,
3590 except that it isn't necessary to gcpro ARGS[0]. */
3593 run_hook_with_args_in_buffer (struct buffer *buf, int nargs, Lisp_Object *args,
3594 enum run_hooks_condition cond)
3596 Lisp_Object sym, val, ret;
3598 if (!initialized || preparing_for_armageddon)
3599 /* We need to bail out of here pronto. */
3602 /* Whenever gc_in_progress is true, preparing_for_armageddon
3603 will also be true unless something is really hosed. */
3604 assert (!gc_in_progress);
3607 val = symbol_value_in_buffer (sym, make_buffer (buf));
3608 ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil);
3610 if (UNBOUNDP (val) || NILP (val))
3612 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3615 return Ffuncall (nargs, args);
3619 struct gcpro gcpro1, gcpro2, gcpro3;
3620 Lisp_Object globals = Qnil;
3621 GCPRO3 (sym, val, globals);
3624 CONSP (val) && ((cond == RUN_HOOKS_TO_COMPLETION)
3625 || (cond == RUN_HOOKS_UNTIL_SUCCESS ? NILP (ret)
3629 if (EQ (XCAR (val), Qt))
3631 /* t indicates this hook has a local binding;
3632 it means to run the global binding too. */
3633 globals = Fdefault_value (sym);
3635 if ((! CONSP (globals) || EQ (XCAR (globals), Qlambda)) &&
3639 ret = Ffuncall (nargs, args);
3644 CONSP (globals) && ((cond == RUN_HOOKS_TO_COMPLETION)
3645 || (cond == RUN_HOOKS_UNTIL_SUCCESS
3648 globals = XCDR (globals))
3650 args[0] = XCAR (globals);
3651 /* In a global value, t should not occur. If it does, we
3652 must ignore it to avoid an endless loop. */
3653 if (!EQ (args[0], Qt))
3654 ret = Ffuncall (nargs, args);
3660 args[0] = XCAR (val);
3661 ret = Ffuncall (nargs, args);
3671 run_hook_with_args (int nargs, Lisp_Object *args,
3672 enum run_hooks_condition cond)
3674 return run_hook_with_args_in_buffer (current_buffer, nargs, args, cond);
3679 /* From FSF 19.30, not currently used */
3681 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
3682 present value of that symbol.
3683 Call each element of FUNLIST,
3684 passing each of them the rest of ARGS.
3685 The caller (or its caller, etc) must gcpro all of ARGS,
3686 except that it isn't necessary to gcpro ARGS[0]. */
3689 run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
3691 Lisp_Object sym = args[0];
3693 struct gcpro gcpro1, gcpro2;
3697 for (val = funlist; CONSP (val); val = XCDR (val))
3699 if (EQ (XCAR (val), Qt))
3701 /* t indicates this hook has a local binding;
3702 it means to run the global binding too. */
3703 Lisp_Object globals;
3705 for (globals = Fdefault_value (sym);
3707 globals = XCDR (globals))
3709 args[0] = XCAR (globals);
3710 /* In a global value, t should not occur. If it does, we
3711 must ignore it to avoid an endless loop. */
3712 if (!EQ (args[0], Qt))
3713 Ffuncall (nargs, args);
3718 args[0] = XCAR (val);
3719 Ffuncall (nargs, args);
3729 va_run_hook_with_args (Lisp_Object hook_var, int nargs, ...)
3731 /* This function can GC */
3732 struct gcpro gcpro1;
3735 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
3737 va_start (vargs, nargs);
3738 funcall_args[0] = hook_var;
3739 for (i = 0; i < nargs; i++)
3740 funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
3743 GCPRO1 (*funcall_args);
3744 gcpro1.nvars = nargs + 1;
3745 run_hook_with_args (nargs + 1, funcall_args, RUN_HOOKS_TO_COMPLETION);
3750 va_run_hook_with_args_in_buffer (struct buffer *buf, Lisp_Object hook_var,
3753 /* This function can GC */
3754 struct gcpro gcpro1;
3757 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
3759 va_start (vargs, nargs);
3760 funcall_args[0] = hook_var;
3761 for (i = 0; i < nargs; i++)
3762 funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
3765 GCPRO1 (*funcall_args);
3766 gcpro1.nvars = nargs + 1;
3767 run_hook_with_args_in_buffer (buf, nargs + 1, funcall_args,
3768 RUN_HOOKS_TO_COMPLETION);
3773 run_hook (Lisp_Object hook)
3775 Frun_hooks (1, &hook);
3780 /************************************************************************/
3781 /* Front-ends to eval, funcall, apply */
3782 /************************************************************************/
3784 /* Apply fn to arg */
3786 apply1 (Lisp_Object fn, Lisp_Object arg)
3788 /* This function can GC */
3789 struct gcpro gcpro1;
3790 Lisp_Object args[2];
3793 return Ffuncall (1, &fn);
3798 RETURN_UNGCPRO (Fapply (2, args));
3801 /* Call function fn on no arguments */
3803 call0 (Lisp_Object fn)
3805 /* This function can GC */
3806 struct gcpro gcpro1;
3809 RETURN_UNGCPRO (Ffuncall (1, &fn));
3812 /* Call function fn with argument arg0 */
3814 call1 (Lisp_Object fn,
3817 /* This function can GC */
3818 struct gcpro gcpro1;
3819 Lisp_Object args[2];
3824 RETURN_UNGCPRO (Ffuncall (2, args));
3827 /* Call function fn with arguments arg0, arg1 */
3829 call2 (Lisp_Object fn,
3830 Lisp_Object arg0, Lisp_Object arg1)
3832 /* This function can GC */
3833 struct gcpro gcpro1;
3834 Lisp_Object args[3];
3840 RETURN_UNGCPRO (Ffuncall (3, args));
3843 /* Call function fn with arguments arg0, arg1, arg2 */
3845 call3 (Lisp_Object fn,
3846 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
3848 /* This function can GC */
3849 struct gcpro gcpro1;
3850 Lisp_Object args[4];
3857 RETURN_UNGCPRO (Ffuncall (4, args));
3860 /* Call function fn with arguments arg0, arg1, arg2, arg3 */
3862 call4 (Lisp_Object fn,
3863 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3866 /* This function can GC */
3867 struct gcpro gcpro1;
3868 Lisp_Object args[5];
3876 RETURN_UNGCPRO (Ffuncall (5, args));
3879 /* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */
3881 call5 (Lisp_Object fn,
3882 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3883 Lisp_Object arg3, Lisp_Object arg4)
3885 /* This function can GC */
3886 struct gcpro gcpro1;
3887 Lisp_Object args[6];
3896 RETURN_UNGCPRO (Ffuncall (6, args));
3900 call6 (Lisp_Object fn,
3901 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3902 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
3904 /* This function can GC */
3905 struct gcpro gcpro1;
3906 Lisp_Object args[7];
3916 RETURN_UNGCPRO (Ffuncall (7, args));
3920 call7 (Lisp_Object fn,
3921 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3922 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
3925 /* This function can GC */
3926 struct gcpro gcpro1;
3927 Lisp_Object args[8];
3938 RETURN_UNGCPRO (Ffuncall (8, args));
3942 call8 (Lisp_Object fn,
3943 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3944 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
3945 Lisp_Object arg6, Lisp_Object arg7)
3947 /* This function can GC */
3948 struct gcpro gcpro1;
3949 Lisp_Object args[9];
3961 RETURN_UNGCPRO (Ffuncall (9, args));
3965 call0_in_buffer (struct buffer *buf, Lisp_Object fn)
3967 if (current_buffer == buf)
3972 int speccount = specpdl_depth();
3973 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3974 set_buffer_internal (buf);
3976 unbind_to (speccount, Qnil);
3982 call1_in_buffer (struct buffer *buf, Lisp_Object fn,
3985 if (current_buffer == buf)
3986 return call1 (fn, arg0);
3990 int speccount = specpdl_depth();
3991 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3992 set_buffer_internal (buf);
3993 val = call1 (fn, arg0);
3994 unbind_to (speccount, Qnil);
4000 call2_in_buffer (struct buffer *buf, Lisp_Object fn,
4001 Lisp_Object arg0, Lisp_Object arg1)
4003 if (current_buffer == buf)
4004 return call2 (fn, arg0, arg1);
4008 int speccount = specpdl_depth();
4009 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4010 set_buffer_internal (buf);
4011 val = call2 (fn, arg0, arg1);
4012 unbind_to (speccount, Qnil);
4018 call3_in_buffer (struct buffer *buf, Lisp_Object fn,
4019 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
4021 if (current_buffer == buf)
4022 return call3 (fn, arg0, arg1, arg2);
4026 int speccount = specpdl_depth();
4027 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4028 set_buffer_internal (buf);
4029 val = call3 (fn, arg0, arg1, arg2);
4030 unbind_to (speccount, Qnil);
4036 call4_in_buffer (struct buffer *buf, Lisp_Object fn,
4037 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4040 if (current_buffer == buf)
4041 return call4 (fn, arg0, arg1, arg2, arg3);
4045 int speccount = specpdl_depth();
4046 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4047 set_buffer_internal (buf);
4048 val = call4 (fn, arg0, arg1, arg2, arg3);
4049 unbind_to (speccount, Qnil);
4055 eval_in_buffer (struct buffer *buf, Lisp_Object form)
4057 if (current_buffer == buf)
4058 return Feval (form);
4062 int speccount = specpdl_depth();
4063 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4064 set_buffer_internal (buf);
4066 unbind_to (speccount, Qnil);
4072 /************************************************************************/
4073 /* Error-catching front-ends to eval, funcall, apply */
4074 /************************************************************************/
4076 /* Call function fn on no arguments, with condition handler */
4078 call0_with_handler (Lisp_Object handler, Lisp_Object fn)
4080 /* This function can GC */
4081 struct gcpro gcpro1;
4082 Lisp_Object args[2];
4087 RETURN_UNGCPRO (Fcall_with_condition_handler (2, args));
4090 /* Call function fn with argument arg0, with condition handler */
4092 call1_with_handler (Lisp_Object handler, Lisp_Object fn,
4095 /* This function can GC */
4096 struct gcpro gcpro1;
4097 Lisp_Object args[3];
4103 RETURN_UNGCPRO (Fcall_with_condition_handler (3, args));
4107 /* The following functions provide you with error-trapping versions
4108 of the various front-ends above. They take an additional
4109 "warning_string" argument; if non-zero, a warning with this
4110 string and the actual error that occurred will be displayed
4111 in the *Warnings* buffer if an error occurs. In all cases,
4112 QUIT is inhibited while these functions are running, and if
4113 an error occurs, Qunbound is returned instead of the normal
4117 /* #### This stuff needs to catch throws as well. We need to
4118 improve internal_catch() so it can take a "catch anything"
4119 argument similar to Qt or Qerror for condition_case_1(). */
4122 caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4124 if (!NILP (errordata))
4126 Lisp_Object args[2];
4130 char *str = (char *) get_opaque_ptr (arg);
4131 args[0] = build_string (str);
4134 args[0] = build_string ("error");
4135 /* #### This should call
4136 (with-output-to-string (display-error errordata))
4137 but that stuff is all in Lisp currently. */
4138 args[1] = errordata;
4139 warn_when_safe_lispobj
4141 emacs_doprnt_string_lisp ((CONST Bufbyte *) "%s: %s",
4142 Qnil, -1, 2, args));
4148 allow_quit_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4150 if (CONSP (errordata) && EQ (XCAR (errordata), Qquit))
4151 return Fsignal (Qquit, XCDR (errordata));
4152 return caught_a_squirmer (errordata, arg);
4156 safe_run_hook_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4158 Lisp_Object hook = Fcar (arg);
4160 /* Clear out the hook. */
4162 return caught_a_squirmer (errordata, arg);
4166 allow_quit_safe_run_hook_caught_a_squirmer (Lisp_Object errordata,
4169 Lisp_Object hook = Fcar (arg);
4171 if (!CONSP (errordata) || !EQ (XCAR (errordata), Qquit))
4172 /* Clear out the hook. */
4174 return allow_quit_caught_a_squirmer (errordata, arg);
4178 catch_them_squirmers_eval_in_buffer (Lisp_Object cons)
4180 return eval_in_buffer (XBUFFER (XCAR (cons)), XCDR (cons));
4184 eval_in_buffer_trapping_errors (CONST char *warning_string,
4185 struct buffer *buf, Lisp_Object form)
4187 int speccount = specpdl_depth();
4192 struct gcpro gcpro1, gcpro2;
4194 XSETBUFFER (buffer, buf);
4196 specbind (Qinhibit_quit, Qt);
4197 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4199 cons = noseeum_cons (buffer, form);
4200 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4201 GCPRO2 (cons, opaque);
4202 /* Qerror not Qt, so you can get a backtrace */
4203 tem = condition_case_1 (Qerror,
4204 catch_them_squirmers_eval_in_buffer, cons,
4205 caught_a_squirmer, opaque);
4206 free_cons (XCONS (cons));
4207 if (OPAQUE_PTRP (opaque))
4208 free_opaque_ptr (opaque);
4211 /* gc_currently_forbidden = 0; */
4212 return unbind_to (speccount, tem);
4216 catch_them_squirmers_run_hook (Lisp_Object hook_symbol)
4218 /* This function can GC */
4219 run_hook (hook_symbol);
4224 run_hook_trapping_errors (CONST char *warning_string, Lisp_Object hook_symbol)
4229 struct gcpro gcpro1;
4231 if (!initialized || preparing_for_armageddon)
4233 tem = find_symbol_value (hook_symbol);
4234 if (NILP (tem) || UNBOUNDP (tem))
4237 speccount = specpdl_depth();
4238 specbind (Qinhibit_quit, Qt);
4240 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4242 /* Qerror not Qt, so you can get a backtrace */
4243 tem = condition_case_1 (Qerror,
4244 catch_them_squirmers_run_hook, hook_symbol,
4245 caught_a_squirmer, opaque);
4246 if (OPAQUE_PTRP (opaque))
4247 free_opaque_ptr (opaque);
4250 return unbind_to (speccount, tem);
4253 /* Same as run_hook_trapping_errors() but also set the hook to nil
4254 if an error occurs. */
4257 safe_run_hook_trapping_errors (CONST char *warning_string,
4258 Lisp_Object hook_symbol,
4261 int speccount = specpdl_depth();
4263 Lisp_Object cons = Qnil;
4264 struct gcpro gcpro1;
4266 if (!initialized || preparing_for_armageddon)
4268 tem = find_symbol_value (hook_symbol);
4269 if (NILP (tem) || UNBOUNDP (tem))
4273 specbind (Qinhibit_quit, Qt);
4275 cons = noseeum_cons (hook_symbol,
4276 warning_string ? make_opaque_ptr ((void *)warning_string)
4279 /* Qerror not Qt, so you can get a backtrace */
4280 tem = condition_case_1 (Qerror,
4281 catch_them_squirmers_run_hook,
4284 allow_quit_safe_run_hook_caught_a_squirmer :
4285 safe_run_hook_caught_a_squirmer,
4287 if (OPAQUE_PTRP (XCDR (cons)))
4288 free_opaque_ptr (XCDR (cons));
4289 free_cons (XCONS (cons));
4292 return unbind_to (speccount, tem);
4296 catch_them_squirmers_call0 (Lisp_Object function)
4298 /* This function can GC */
4299 return call0 (function);
4303 call0_trapping_errors (CONST char *warning_string, Lisp_Object function)
4307 Lisp_Object opaque = Qnil;
4308 struct gcpro gcpro1, gcpro2;
4310 if (SYMBOLP (function))
4312 tem = XSYMBOL (function)->function;
4313 if (NILP (tem) || UNBOUNDP (tem))
4317 GCPRO2 (opaque, function);
4318 speccount = specpdl_depth();
4319 specbind (Qinhibit_quit, Qt);
4320 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4322 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4323 /* Qerror not Qt, so you can get a backtrace */
4324 tem = condition_case_1 (Qerror,
4325 catch_them_squirmers_call0, function,
4326 caught_a_squirmer, opaque);
4327 if (OPAQUE_PTRP (opaque))
4328 free_opaque_ptr (opaque);
4331 /* gc_currently_forbidden = 0; */
4332 return unbind_to (speccount, tem);
4336 catch_them_squirmers_call1 (Lisp_Object cons)
4338 /* This function can GC */
4339 return call1 (XCAR (cons), XCDR (cons));
4343 catch_them_squirmers_call2 (Lisp_Object cons)
4345 /* This function can GC */
4346 return call2 (XCAR (cons), XCAR (XCDR (cons)), XCAR (XCDR (XCDR (cons))));
4350 call1_trapping_errors (CONST char *warning_string, Lisp_Object function,
4353 int speccount = specpdl_depth();
4355 Lisp_Object cons = Qnil;
4356 Lisp_Object opaque = Qnil;
4357 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4359 if (SYMBOLP (function))
4361 tem = XSYMBOL (function)->function;
4362 if (NILP (tem) || UNBOUNDP (tem))
4366 GCPRO4 (cons, opaque, function, object);
4368 specbind (Qinhibit_quit, Qt);
4369 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4371 cons = noseeum_cons (function, object);
4372 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4373 /* Qerror not Qt, so you can get a backtrace */
4374 tem = condition_case_1 (Qerror,
4375 catch_them_squirmers_call1, cons,
4376 caught_a_squirmer, opaque);
4377 if (OPAQUE_PTRP (opaque))
4378 free_opaque_ptr (opaque);
4379 free_cons (XCONS (cons));
4382 /* gc_currently_forbidden = 0; */
4383 return unbind_to (speccount, tem);
4387 call2_trapping_errors (CONST char *warning_string, Lisp_Object function,
4388 Lisp_Object object1, Lisp_Object object2)
4390 int speccount = specpdl_depth();
4392 Lisp_Object cons = Qnil;
4393 Lisp_Object opaque = Qnil;
4394 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
4396 if (SYMBOLP (function))
4398 tem = XSYMBOL (function)->function;
4399 if (NILP (tem) || UNBOUNDP (tem))
4403 GCPRO5 (cons, opaque, function, object1, object2);
4404 specbind (Qinhibit_quit, Qt);
4405 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4407 cons = list3 (function, object1, object2);
4408 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4409 /* Qerror not Qt, so you can get a backtrace */
4410 tem = condition_case_1 (Qerror,
4411 catch_them_squirmers_call2, cons,
4412 caught_a_squirmer, opaque);
4413 if (OPAQUE_PTRP (opaque))
4414 free_opaque_ptr (opaque);
4418 /* gc_currently_forbidden = 0; */
4419 return unbind_to (speccount, tem);
4423 /************************************************************************/
4424 /* The special binding stack */
4425 /* Most C code should simply use specbind() and unbind_to(). */
4426 /* When performance is critical, use the macros in backtrace.h. */
4427 /************************************************************************/
4429 #define min_max_specpdl_size 400
4432 grow_specpdl (size_t reserved)
4434 size_t size_needed = specpdl_depth() + reserved;
4435 if (size_needed >= max_specpdl_size)
4437 if (max_specpdl_size < min_max_specpdl_size)
4438 max_specpdl_size = min_max_specpdl_size;
4439 if (size_needed >= max_specpdl_size)
4441 if (!NILP (Vdebug_on_error) ||
4442 !NILP (Vdebug_on_signal))
4443 /* Leave room for some specpdl in the debugger. */
4444 max_specpdl_size = size_needed + 100;
4446 ("Variable binding depth exceeds max-specpdl-size");
4449 while (specpdl_size < size_needed)
4452 if (specpdl_size > max_specpdl_size)
4453 specpdl_size = max_specpdl_size;
4455 XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size);
4456 specpdl_ptr = specpdl + specpdl_depth();
4460 /* Handle unbinding buffer-local variables */
4462 specbind_unwind_local (Lisp_Object ovalue)
4464 Lisp_Object current = Fcurrent_buffer ();
4465 Lisp_Object symbol = specpdl_ptr->symbol;
4466 struct Lisp_Cons *victim = XCONS (ovalue);
4467 Lisp_Object buf = get_buffer (victim->car, 0);
4468 ovalue = victim->cdr;
4474 /* Deleted buffer -- do nothing */
4476 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0)
4478 /* Was buffer-local when binding was made, now no longer is.
4479 * (kill-local-variable can do this.)
4480 * Do nothing in this case.
4483 else if (EQ (buf, current))
4484 Fset (symbol, ovalue);
4487 /* Urk! Somebody switched buffers */
4488 struct gcpro gcpro1;
4491 Fset (symbol, ovalue);
4492 Fset_buffer (current);
4499 specbind_unwind_wasnt_local (Lisp_Object buffer)
4501 Lisp_Object current = Fcurrent_buffer ();
4502 Lisp_Object symbol = specpdl_ptr->symbol;
4504 buffer = get_buffer (buffer, 0);
4507 /* Deleted buffer -- do nothing */
4509 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0)
4511 /* Was buffer-local when binding was made, now no longer is.
4512 * (kill-local-variable can do this.)
4513 * Do nothing in this case.
4516 else if (EQ (buffer, current))
4517 Fkill_local_variable (symbol);
4520 /* Urk! Somebody switched buffers */
4521 struct gcpro gcpro1;
4523 Fset_buffer (buffer);
4524 Fkill_local_variable (symbol);
4525 Fset_buffer (current);
4533 specbind (Lisp_Object symbol, Lisp_Object value)
4535 SPECBIND (symbol, value);
4539 specbind_magic (Lisp_Object symbol, Lisp_Object value)
4542 symbol_value_buffer_local_info (symbol, current_buffer);
4544 if (buffer_local == 0)
4546 specpdl_ptr->old_value = find_symbol_value (symbol);
4547 specpdl_ptr->func = 0; /* Handled specially by unbind_to */
4549 else if (buffer_local > 0)
4551 /* Already buffer-local */
4552 specpdl_ptr->old_value = noseeum_cons (Fcurrent_buffer (),
4553 find_symbol_value (symbol));
4554 specpdl_ptr->func = specbind_unwind_local;
4558 /* About to become buffer-local */
4559 specpdl_ptr->old_value = Fcurrent_buffer ();
4560 specpdl_ptr->func = specbind_unwind_wasnt_local;
4563 specpdl_ptr->symbol = symbol;
4565 specpdl_depth_counter++;
4567 Fset (symbol, value);
4571 record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg),
4574 SPECPDL_RESERVE (1);
4575 specpdl_ptr->func = function;
4576 specpdl_ptr->symbol = Qnil;
4577 specpdl_ptr->old_value = arg;
4579 specpdl_depth_counter++;
4582 extern int check_sigio (void);
4584 /* Unwind the stack till specpdl_depth() == COUNT.
4585 VALUE is not used, except that, purely as a convenience to the
4586 caller, it is protected from garbage-protection. */
4588 unbind_to (int count, Lisp_Object value)
4590 UNBIND_TO_GCPRO (count, value);
4594 /* Don't call this directly.
4595 Only for use by UNBIND_TO* macros in backtrace.h */
4597 unbind_to_hairy (int count)
4601 check_quit (); /* make Vquit_flag accurate */
4602 quitf = !NILP (Vquit_flag);
4606 ++specpdl_depth_counter;
4608 while (specpdl_depth_counter != count)
4611 --specpdl_depth_counter;
4613 if (specpdl_ptr->func != 0)
4614 /* An unwind-protect */
4615 (*specpdl_ptr->func) (specpdl_ptr->old_value);
4618 /* We checked symbol for validity when we specbound it,
4619 so only need to call Fset if symbol has magic value. */
4620 struct Lisp_Symbol *sym = XSYMBOL (specpdl_ptr->symbol);
4621 if (!SYMBOL_VALUE_MAGIC_P (sym->value))
4622 sym->value = specpdl_ptr->old_value;
4624 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
4628 #ifndef EXCEEDINGLY_QUESTIONABLE_CODE
4629 /* There should never be anything here for us to remove.
4630 If so, it indicates a logic error in Emacs. Catches
4631 should get removed when a throw or signal occurs, or
4632 when a catch or condition-case exits normally. But
4633 it's too dangerous to just remove this code. --ben */
4635 /* Furthermore, this code is not in FSFmacs!!!
4636 Braino on mly's part? */
4637 /* If we're unwound past the pdlcount of a catch frame,
4638 that catch can't possibly still be valid. */
4639 while (catchlist && catchlist->pdlcount > specpdl_depth_counter)
4641 catchlist = catchlist->next;
4642 /* Don't mess with gcprolist, backtrace_list here */
4653 /* Get the value of symbol's global binding, even if that binding is
4654 not now dynamically visible. May return Qunbound or magic values. */
4657 top_level_value (Lisp_Object symbol)
4659 REGISTER struct specbinding *ptr = specpdl;
4661 CHECK_SYMBOL (symbol);
4662 for (; ptr != specpdl_ptr; ptr++)
4664 if (EQ (ptr->symbol, symbol))
4665 return ptr->old_value;
4667 return XSYMBOL (symbol)->value;
4673 top_level_set (Lisp_Object symbol, Lisp_Object newval)
4675 REGISTER struct specbinding *ptr = specpdl;
4677 CHECK_SYMBOL (symbol);
4678 for (; ptr != specpdl_ptr; ptr++)
4680 if (EQ (ptr->symbol, symbol))
4682 ptr->old_value = newval;
4686 return Fset (symbol, newval);
4692 /************************************************************************/
4694 /************************************************************************/
4696 DEFUN ("backtrace-debug", Fbacktrace_debug, 2, 2, 0, /*
4697 Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
4698 The debugger is entered when that frame exits, if the flag is non-nil.
4702 REGISTER struct backtrace *backlist = backtrace_list;
4707 for (i = 0; backlist && i < XINT (level); i++)
4709 backlist = backlist->next;
4713 backlist->debug_on_exit = !NILP (flag);
4719 backtrace_specials (int speccount, int speclimit, Lisp_Object stream)
4721 int printing_bindings = 0;
4723 for (; speccount > speclimit; speccount--)
4725 if (specpdl[speccount - 1].func == 0
4726 || specpdl[speccount - 1].func == specbind_unwind_local
4727 || specpdl[speccount - 1].func == specbind_unwind_wasnt_local)
4729 write_c_string (((!printing_bindings) ? " # bind (" : " "),
4731 Fprin1 (specpdl[speccount - 1].symbol, stream);
4732 printing_bindings = 1;
4736 if (printing_bindings) write_c_string (")\n", stream);
4737 write_c_string (" # (unwind-protect ...)\n", stream);
4738 printing_bindings = 0;
4741 if (printing_bindings) write_c_string (")\n", stream);
4744 DEFUN ("backtrace", Fbacktrace, 0, 2, "", /*
4745 Print a trace of Lisp function calls currently active.
4746 Option arg STREAM specifies the output stream to send the backtrace to,
4747 and defaults to the value of `standard-output'. Optional second arg
4748 DETAILED means show places where currently active variable bindings,
4749 catches, condition-cases, and unwind-protects were made as well as
4754 /* This function can GC */
4755 struct backtrace *backlist = backtrace_list;
4756 struct catchtag *catches = catchlist;
4757 int speccount = specpdl_depth();
4759 int old_nl = print_escape_newlines;
4760 int old_pr = print_readably;
4761 Lisp_Object old_level = Vprint_level;
4762 Lisp_Object oiq = Vinhibit_quit;
4763 struct gcpro gcpro1, gcpro2;
4765 /* We can't allow quits in here because that could cause the values
4766 of print_readably and print_escape_newlines to get screwed up.
4767 Normally we would use a record_unwind_protect but that would
4768 screw up the functioning of this function. */
4771 entering_debugger = 0;
4773 Vprint_level = make_int (3);
4775 print_escape_newlines = 1;
4777 GCPRO2 (stream, old_level);
4780 stream = Vstandard_output;
4781 if (!noninteractive && (NILP (stream) || EQ (stream, Qt)))
4782 stream = Fselected_frame (Qnil);
4786 if (!NILP (detailed) && catches && catches->backlist == backlist)
4788 int catchpdl = catches->pdlcount;
4789 if (specpdl[catchpdl].func == condition_case_unwind
4790 && speccount > catchpdl)
4791 /* This is a condition-case catchpoint */
4792 catchpdl = catchpdl + 1;
4794 backtrace_specials (speccount, catchpdl, stream);
4796 speccount = catches->pdlcount;
4797 if (catchpdl == speccount)
4799 write_c_string (" # (catch ", stream);
4800 Fprin1 (catches->tag, stream);
4801 write_c_string (" ...)\n", stream);
4805 write_c_string (" # (condition-case ... . ", stream);
4806 Fprin1 (Fcdr (Fcar (catches->tag)), stream);
4807 write_c_string (")\n", stream);
4809 catches = catches->next;
4815 if (!NILP (detailed) && backlist->pdlcount < speccount)
4817 backtrace_specials (speccount, backlist->pdlcount, stream);
4818 speccount = backlist->pdlcount;
4820 write_c_string (((backlist->debug_on_exit) ? "* " : " "),
4822 if (backlist->nargs == UNEVALLED)
4824 Fprin1 (Fcons (*backlist->function, *backlist->args), stream);
4825 write_c_string ("\n", stream); /* from FSFmacs 19.30 */
4829 Lisp_Object tem = *backlist->function;
4830 Fprin1 (tem, stream); /* This can QUIT */
4831 write_c_string ("(", stream);
4832 if (backlist->nargs == MANY)
4835 Lisp_Object tail = Qnil;
4836 struct gcpro ngcpro1;
4839 for (tail = *backlist->args, i = 0;
4841 tail = Fcdr (tail), i++)
4843 if (i != 0) write_c_string (" ", stream);
4844 Fprin1 (Fcar (tail), stream);
4851 for (i = 0; i < backlist->nargs; i++)
4853 if (!i && EQ(tem, Qbyte_code)) {
4854 write_c_string("\"...\"", stream);
4857 if (i != 0) write_c_string (" ", stream);
4858 Fprin1 (backlist->args[i], stream);
4862 write_c_string (")\n", stream);
4863 backlist = backlist->next;
4866 Vprint_level = old_level;
4867 print_readably = old_pr;
4868 print_escape_newlines = old_nl;
4870 Vinhibit_quit = oiq;
4875 DEFUN ("backtrace-frame", Fbacktrace_frame, 1, 1, "", /*
4876 Return the function and arguments N frames up from current execution point.
4877 If that frame has not evaluated the arguments yet (or is a special form),
4878 the value is (nil FUNCTION ARG-FORMS...).
4879 If that frame has evaluated its arguments and called its function already,
4880 the value is (t FUNCTION ARG-VALUES...).
4881 A &rest arg is represented as the tail of the list ARG-VALUES.
4882 FUNCTION is whatever was supplied as car of evaluated list,
4883 or a lambda expression for macro calls.
4884 If N is more than the number of frames, the value is nil.
4888 REGISTER struct backtrace *backlist = backtrace_list;
4892 CHECK_NATNUM (nframes);
4894 /* Find the frame requested. */
4895 for (i = XINT (nframes); backlist && (i-- > 0);)
4896 backlist = backlist->next;
4900 if (backlist->nargs == UNEVALLED)
4901 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
4904 if (backlist->nargs == MANY)
4905 tem = *backlist->args;
4907 tem = Flist (backlist->nargs, backlist->args);
4909 return Fcons (Qt, Fcons (*backlist->function, tem));
4914 /************************************************************************/
4916 /************************************************************************/
4919 warn_when_safe_lispobj (Lisp_Object class, Lisp_Object level,
4922 obj = list1 (list3 (class, level, obj));
4923 if (NILP (Vpending_warnings))
4924 Vpending_warnings = Vpending_warnings_tail = obj;
4927 Fsetcdr (Vpending_warnings_tail, obj);
4928 Vpending_warnings_tail = obj;
4932 /* #### This should probably accept Lisp objects; but then we have
4933 to make sure that Feval() isn't called, since it might not be safe.
4935 An alternative approach is to just pass some non-string type of
4936 Lisp_Object to warn_when_safe_lispobj(); `prin1-to-string' will
4937 automatically be called when it is safe to do so. */
4940 warn_when_safe (Lisp_Object class, Lisp_Object level, CONST char *fmt, ...)
4945 va_start (args, fmt);
4946 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt),
4950 warn_when_safe_lispobj (class, level, obj);
4956 /************************************************************************/
4957 /* Initialization */
4958 /************************************************************************/
4963 defsymbol (&Qinhibit_quit, "inhibit-quit");
4964 defsymbol (&Qautoload, "autoload");
4965 defsymbol (&Qdebug_on_error, "debug-on-error");
4966 defsymbol (&Qstack_trace_on_error, "stack-trace-on-error");
4967 defsymbol (&Qdebug_on_signal, "debug-on-signal");
4968 defsymbol (&Qstack_trace_on_signal, "stack-trace-on-signal");
4969 defsymbol (&Qdebugger, "debugger");
4970 defsymbol (&Qmacro, "macro");
4971 defsymbol (&Qand_rest, "&rest");
4972 defsymbol (&Qand_optional, "&optional");
4973 /* Note that the process code also uses Qexit */
4974 defsymbol (&Qexit, "exit");
4975 defsymbol (&Qsetq, "setq");
4976 defsymbol (&Qinteractive, "interactive");
4977 defsymbol (&Qcommandp, "commandp");
4978 defsymbol (&Qdefun, "defun");
4979 defsymbol (&Qprogn, "progn");
4980 defsymbol (&Qvalues, "values");
4981 defsymbol (&Qdisplay_warning, "display-warning");
4982 defsymbol (&Qrun_hooks, "run-hooks");
4983 defsymbol (&Qif, "if");
4988 DEFSUBR_MACRO (Fwhen);
4989 DEFSUBR_MACRO (Funless);
4996 DEFSUBR (Ffunction);
4998 DEFSUBR (Fdefmacro);
5000 DEFSUBR (Fdefconst);
5001 DEFSUBR (Fuser_variable_p);
5005 DEFSUBR (Fmacroexpand_internal);
5008 DEFSUBR (Funwind_protect);
5009 DEFSUBR (Fcondition_case);
5010 DEFSUBR (Fcall_with_condition_handler);
5012 DEFSUBR (Finteractive_p);
5013 DEFSUBR (Fcommandp);
5014 DEFSUBR (Fcommand_execute);
5015 DEFSUBR (Fautoload);
5019 DEFSUBR (Ffunctionp);
5020 DEFSUBR (Ffunction_min_args);
5021 DEFSUBR (Ffunction_max_args);
5022 DEFSUBR (Frun_hooks);
5023 DEFSUBR (Frun_hook_with_args);
5024 DEFSUBR (Frun_hook_with_args_until_success);
5025 DEFSUBR (Frun_hook_with_args_until_failure);
5026 DEFSUBR (Fbacktrace_debug);
5027 DEFSUBR (Fbacktrace);
5028 DEFSUBR (Fbacktrace_frame);
5034 specpdl_ptr = specpdl;
5035 specpdl_depth_counter = 0;
5037 Vcondition_handlers = Qnil;
5040 debug_on_next_call = 0;
5041 lisp_eval_depth = 0;
5042 entering_debugger = 0;
5046 reinit_vars_of_eval (void)
5048 preparing_for_armageddon = 0;
5050 Qunbound_suspended_errors_tag = make_opaque_ptr (&Qunbound_suspended_errors_tag);
5051 staticpro_nodump (&Qunbound_suspended_errors_tag);
5054 specpdl = xnew_array (struct specbinding, specpdl_size);
5055 /* XEmacs change: increase these values. */
5056 max_specpdl_size = 3000;
5057 max_lisp_eval_depth = 500;
5058 #if 0 /* no longer used */
5066 reinit_vars_of_eval ();
5068 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size /*
5069 Limit on number of Lisp variable bindings & unwind-protects before error.
5072 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth /*
5073 Limit on depth in `eval', `apply' and `funcall' before error.
5074 This limit is to catch infinite recursions for you before they cause
5075 actual stack overflow in C, which would be fatal for Emacs.
5076 You can safely make it considerably larger than its default value,
5077 if that proves inconveniently small.
5080 DEFVAR_LISP ("quit-flag", &Vquit_flag /*
5081 Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
5082 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.
5086 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit /*
5087 Non-nil inhibits C-g quitting from happening immediately.
5088 Note that `quit-flag' will still be set by typing C-g,
5089 so a quit will be signalled as soon as `inhibit-quit' is nil.
5090 To prevent this happening, set `quit-flag' to nil
5091 before making `inhibit-quit' nil. The value of `inhibit-quit' is
5092 ignored if a critical quit is requested by typing control-shift-G in
5095 Vinhibit_quit = Qnil;
5097 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error /*
5098 *Non-nil means automatically display a backtrace buffer
5099 after any error that is not handled by a `condition-case'.
5100 If the value is a list, an error only means to display a backtrace
5101 if one of its condition symbols appears in the list.
5102 See also variable `stack-trace-on-signal'.
5104 Vstack_trace_on_error = Qnil;
5106 DEFVAR_LISP ("stack-trace-on-signal", &Vstack_trace_on_signal /*
5107 *Non-nil means automatically display a backtrace buffer
5108 after any error that is signalled, whether or not it is handled by
5110 If the value is a list, an error only means to display a backtrace
5111 if one of its condition symbols appears in the list.
5112 See also variable `stack-trace-on-error'.
5114 Vstack_trace_on_signal = Qnil;
5116 DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors /*
5117 *List of errors for which the debugger should not be called.
5118 Each element may be a condition-name or a regexp that matches error messages.
5119 If any element applies to a given error, that error skips the debugger
5120 and just returns to top level.
5121 This overrides the variable `debug-on-error'.
5122 It does not apply to errors handled by `condition-case'.
5124 Vdebug_ignored_errors = Qnil;
5126 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error /*
5127 *Non-nil means enter debugger if an unhandled error is signalled.
5128 The debugger will not be entered if the error is handled by
5130 If the value is a list, an error only means to enter the debugger
5131 if one of its condition symbols appears in the list.
5132 This variable is overridden by `debug-ignored-errors'.
5133 See also variables `debug-on-quit' and `debug-on-signal'.
5135 Vdebug_on_error = Qnil;
5137 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal /*
5138 *Non-nil means enter debugger if an error is signalled.
5139 The debugger will be entered whether or not the error is handled by
5141 If the value is a list, an error only means to enter the debugger
5142 if one of its condition symbols appears in the list.
5143 See also variable `debug-on-quit'.
5145 Vdebug_on_signal = Qnil;
5147 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit /*
5148 *Non-nil means enter debugger if quit is signalled (C-G, for example).
5149 Does not apply if quit is handled by a `condition-case'. Entering the
5150 debugger can also be achieved at any time (for X11 console) by typing
5151 control-shift-G to signal a critical quit.
5155 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call /*
5156 Non-nil means enter debugger before next `eval', `apply' or `funcall'.
5159 DEFVAR_LISP ("debugger", &Vdebugger /*
5160 Function to call to invoke debugger.
5161 If due to frame exit, args are `exit' and the value being returned;
5162 this function's value will be returned instead of that.
5163 If due to error, args are `error' and a list of the args to `signal'.
5164 If due to `apply' or `funcall' entry, one arg, `lambda'.
5165 If due to `eval' entry, one arg, t.
5169 staticpro (&Vpending_warnings);
5170 Vpending_warnings = Qnil;
5171 pdump_wire (&Vpending_warnings_tail);
5172 Vpending_warnings_tail = Qnil;
5174 staticpro (&Vautoload_queue);
5175 Vautoload_queue = Qnil;
5177 staticpro (&Vcondition_handlers);
5179 staticpro (&Vcurrent_warning_class);
5180 Vcurrent_warning_class = Qnil;
5182 staticpro (&Vcurrent_error_state);
5183 Vcurrent_error_state = Qnil; /* errors as normal */