comparison src/eval.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children 080151679be2
comparison
equal deleted inserted replaced
427:0a0253eac470 428:3ecd8885ac67
1 /* Evaluator for XEmacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1992-1994 Free Software Foundation, Inc.
3 Copyright (C) 1995 Sun Microsystems, Inc.
4
5 This file is part of XEmacs.
6
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
10 later version.
11
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
15 for more details.
16
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. */
21
22 /* Synched up with: FSF 19.30 (except for Fsignal), Mule 2.0. */
23
24 #include <config.h>
25 #include "lisp.h"
26
27 #include "commands.h"
28 #include "backtrace.h"
29 #include "bytecode.h"
30 #include "buffer.h"
31 #include "console.h"
32 #include "opaque.h"
33
34 #ifdef ERROR_CHECK_GC
35 int always_gc; /* Debugging hack */
36 #else
37 #define always_gc 0
38 #endif
39
40 struct backtrace *backtrace_list;
41
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
44 on this. */
45
46 #define PUSH_BACKTRACE(bt) do { \
47 (bt).next = backtrace_list; \
48 backtrace_list = &(bt); \
49 } while (0)
50
51 #define POP_BACKTRACE(bt) do { \
52 backtrace_list = (bt).next; \
53 } while (0)
54
55 /* Macros for calling subrs with an argument list whose length is only
56 known at runtime. See EXFUN and DEFUN for similar hackery. */
57
58 #define AV_0(av)
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]
67
68 #define PRIMITIVE_FUNCALL_1(fn, av, ac) \
69 (((Lisp_Object (*)(EXFUN_##ac)) (fn)) (AV_##ac (av)))
70
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); \
78 switch (ac) \
79 { \
80 default: abort(); \
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; \
90 } \
91 } while (0)
92
93 #define FUNCALL_SUBR(rv, subr, av, ac) \
94 PRIMITIVE_FUNCALL (rv, subr_function (subr), av, ac);
95
96
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').
107
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().
114 */
115
116 struct catchtag *catchlist;
117
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;
127 Lisp_Object Qsetq;
128 Lisp_Object Qdisplay_warning;
129 Lisp_Object Vpending_warnings, Vpending_warnings_tail;
130 Lisp_Object Qif;
131
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.
135
136 See call_with_suspended_errors(). */
137 Lisp_Object Vcurrent_error_state;
138
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;
143
144 /* Special catch tag used in call_with_suspended_errors(). */
145 Lisp_Object Qunbound_suspended_errors_tag;
146
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;
150
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;
156
157 /* Current number of specbindings allocated in specpdl. */
158 int specpdl_size;
159
160 /* Pointer to beginning of specpdl. */
161 struct specbinding *specpdl;
162
163 /* Pointer to first unused element in specpdl. */
164 struct specbinding *specpdl_ptr;
165
166 /* specpdl_ptr - specpdl */
167 int specpdl_depth_counter;
168
169 /* Maximum size allowed for specpdl allocation */
170 int max_specpdl_size;
171
172 /* Depth in Lisp evaluations and function calls. */
173 static int lisp_eval_depth;
174
175 /* Maximum allowed depth in Lisp evaluations and function calls. */
176 int max_lisp_eval_depth;
177
178 /* Nonzero means enter debugger before next function call */
179 static int debug_on_next_call;
180
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;
184
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;
188
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;
192
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;
196
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;
200
201 /* Nonzero means enter debugger if a quit signal
202 is handled by the command loop's error handler.
203
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(). */
208 int debug_on_quit;
209
210 #if 0 /* FSFmacs */
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
217 invocations. */
218 int when_entered_debugger;
219 #endif
220
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;
225
226 /* Function to call to invoke the debugger */
227 Lisp_Object Vdebugger;
228
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.
234
235 Each element of this list is one of the following:
236
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.).
240
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.
246
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
251 called for.
252
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'
256 handler.
257
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
266 in.
267 */
268 static Lisp_Object Vcondition_handlers;
269
270
271 #if 0 /* no longer used */
272 /* Used for error catching purposes by throw_or_bomb_out */
273 static int throw_level;
274 #endif /* unused */
275
276
277 /************************************************************************/
278 /* The subr object type */
279 /************************************************************************/
280
281 static void
282 print_subr (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
283 {
284 Lisp_Subr *subr = XSUBR (obj);
285 CONST char *header =
286 (subr->max_args == UNEVALLED) ? "#<special-form " : "#<subr ";
287 CONST char *name = subr_name (subr);
288 CONST char *trailer = subr->prompt ? " (interactive)>" : ">";
289
290 if (print_readably)
291 error ("printing unreadable object %s%s%s", header, name, trailer);
292
293 write_c_string (header, printcharfun);
294 write_c_string (name, printcharfun);
295 write_c_string (trailer, printcharfun);
296 }
297
298 static const struct lrecord_description subr_description[] = {
299 { XD_DOC_STRING, offsetof(Lisp_Subr, doc) },
300 { XD_END }
301 };
302
303 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("subr", subr,
304 this_one_is_unmarkable, print_subr, 0, 0, 0,
305 subr_description,
306 Lisp_Subr);
307
308 /************************************************************************/
309 /* Entering the debugger */
310 /************************************************************************/
311
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.) */
315
316 static Lisp_Object
317 restore_entering_debugger (Lisp_Object arg)
318 {
319 entering_debugger = ! NILP (arg);
320 return arg;
321 }
322
323 /* Actually call the debugger. ARG is a list of args that will be
324 passed to the debugger function, as follows;
325
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.
331
332 */
333
334 static Lisp_Object
335 call_debugger_259 (Lisp_Object arg)
336 {
337 return apply1 (Vdebugger, arg);
338 }
339
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
348 handled normally.
349
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
355 this value.)
356
357 The difference between 'c' and 'r' is as follows:
358
359 debug-on-call:
360 No difference. The call proceeds as normal.
361 debug-on-exit:
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.
365 signal:
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.
372 */
373
374 static Lisp_Object
375 call_debugger (Lisp_Object arg)
376 {
377 int threw;
378 Lisp_Object val;
379 int speccount;
380
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;
386
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);
392
393 return unbind_to (speccount, ((threw)
394 ? Qunbound /* Not returning a value */
395 : val));
396 }
397
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. */
401
402 static Lisp_Object
403 do_debug_on_exit (Lisp_Object val)
404 {
405 /* This is falsified by call_debugger */
406 Lisp_Object v = call_debugger (list2 (Qexit, val));
407
408 return !UNBOUNDP (v) ? v : val;
409 }
410
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'.
414
415 #### The differentiation here between EVAL and FUNCALL is bogus.
416 FUNCALL can be defined as
417
418 (defmacro func (fun &rest args)
419 (cons (eval fun) args))
420
421 and should be treated as such.
422 */
423
424 static void
425 do_debug_on_call (Lisp_Object code)
426 {
427 debug_on_next_call = 0;
428 backtrace_list->debug_on_exit = 1;
429 call_debugger (list1 (code));
430 }
431
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.) */
440
441 static int
442 wants_debugger (Lisp_Object list, Lisp_Object conditions)
443 {
444 if (NILP (list))
445 return 0;
446 if (! CONSP (list))
447 return 1;
448
449 while (CONSP (conditions))
450 {
451 Lisp_Object this, tail;
452 this = XCAR (conditions);
453 for (tail = list; CONSP (tail); tail = XCDR (tail))
454 if (EQ (XCAR (tail), this))
455 return 1;
456 conditions = XCDR (conditions);
457 }
458 return 0;
459 }
460
461
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. */
465
466 static int
467 skip_debugger (Lisp_Object conditions, Lisp_Object data)
468 {
469 /* This function can GC */
470 Lisp_Object tail;
471 int first_string = 1;
472 Lisp_Object error_message = Qnil;
473
474 for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
475 {
476 if (STRINGP (XCAR (tail)))
477 {
478 if (first_string)
479 {
480 error_message = Ferror_message_string (data);
481 first_string = 0;
482 }
483 if (fast_lisp_string_match (XCAR (tail), error_message) >= 0)
484 return 1;
485 }
486 else
487 {
488 Lisp_Object contail;
489
490 for (contail = conditions; CONSP (contail); contail = XCDR (contail))
491 if (EQ (XCAR (tail), XCAR (contail)))
492 return 1;
493 }
494 }
495
496 return 0;
497 }
498
499 /* Actually generate a backtrace on STREAM. */
500
501 static Lisp_Object
502 backtrace_259 (Lisp_Object stream)
503 {
504 return Fbacktrace (stream, Qt);
505 }
506
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.
517
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.)
524
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.
529
530 STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that
531 those functions aren't done more than once in a single `signal'
532 session. */
533
534 static Lisp_Object
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)
541 {
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);
549
550 Vcondition_handlers = active_handlers;
551
552 temp_data = Fcons (sig, data); /* needed for skip_debugger */
553
554 if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only
555 && wants_debugger (Vstack_trace_on_error, conditions)
556 && !skip_debugger (conditions, temp_data))
557 {
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);
562
563 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"),
564 backtrace_259,
565 Qnil,
566 Qnil);
567 unbind_to (speccount, Qnil);
568 *stack_trace_displayed = 1;
569 }
570
571 if (!entering_debugger && !*debugger_entered && !signal_vars_only
572 && (EQ (sig, Qquit)
573 ? debug_on_quit
574 : wants_debugger (Vdebug_on_error, conditions))
575 && !skip_debugger (conditions, temp_data))
576 {
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);
582
583 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
584 *debugger_entered = 1;
585 }
586
587 if (!entering_debugger && !*stack_trace_displayed
588 && wants_debugger (Vstack_trace_on_signal, conditions))
589 {
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);
594
595 internal_with_output_to_temp_buffer (build_string ("*Backtrace*"),
596 backtrace_259,
597 Qnil,
598 Qnil);
599 unbind_to (speccount, Qnil);
600 *stack_trace_displayed = 1;
601 }
602
603 if (!entering_debugger && !*debugger_entered
604 && (EQ (sig, Qquit)
605 ? debug_on_quit
606 : wants_debugger (Vdebug_on_signal, conditions)))
607 {
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);
613
614 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
615 *debugger_entered = 1;
616 }
617
618 UNGCPRO;
619 Vcondition_handlers = all_handlers;
620 return unbind_to (speccount, val);
621 }
622
623
624 /************************************************************************/
625 /* The basic special forms */
626 /************************************************************************/
627
628 /* Except for Fprogn(), the basic special forms below are only called
629 from interpreted code. The byte compiler turns them into bytecodes. */
630
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.
635 */
636 (args))
637 {
638 /* This function can GC */
639 REGISTER Lisp_Object arg, val;
640
641 LIST_LOOP_2 (arg, args)
642 {
643 if (!NILP (val = Feval (arg)))
644 return val;
645 }
646
647 return Qnil;
648 }
649
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.
654 */
655 (args))
656 {
657 /* This function can GC */
658 REGISTER Lisp_Object arg, val = Qt;
659
660 LIST_LOOP_2 (arg, args)
661 {
662 if (NILP (val = Feval (arg)))
663 return val;
664 }
665
666 return val;
667 }
668
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.
674 */
675 (args))
676 {
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));
681
682 if (!NILP (Feval (condition)))
683 return Feval (then_form);
684 else
685 return Fprogn (else_forms);
686 }
687
688 /* Macros `when' and `unless' are trivially defined in Lisp,
689 but it helps for bootstrapping to have them ALWAYS defined. */
690
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.
694 */
695 (int nargs, Lisp_Object *args))
696 {
697 Lisp_Object cond = args[0];
698 Lisp_Object body;
699
700 switch (nargs)
701 {
702 case 1: body = Qnil; break;
703 case 2: body = args[1]; break;
704 default: body = Fcons (Qprogn, Flist (nargs-1, args+1)); break;
705 }
706
707 return list3 (Qif, cond, body);
708 }
709
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.
713 */
714 (int nargs, Lisp_Object *args))
715 {
716 Lisp_Object cond = args[0];
717 Lisp_Object body = Flist (nargs-1, args+1);
718 return Fcons (Qif, Fcons (cond, Fcons (Qnil, body)));
719 }
720
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.
730 */
731 (args))
732 {
733 /* This function can GC */
734 REGISTER Lisp_Object val, clause;
735
736 LIST_LOOP_2 (clause, args)
737 {
738 CHECK_CONS (clause);
739 if (!NILP (val = Feval (XCAR (clause))))
740 {
741 if (!NILP (clause = XCDR (clause)))
742 {
743 CHECK_TRUE_LIST (clause);
744 val = Fprogn (clause);
745 }
746 return val;
747 }
748 }
749
750 return Qnil;
751 }
752
753 DEFUN ("progn", Fprogn, 0, UNEVALLED, 0, /*
754 \(progn BODY...): eval BODY forms sequentially and return value of last one.
755 */
756 (args))
757 {
758 /* This function can GC */
759 /* Caller must provide a true list in ARGS */
760 REGISTER Lisp_Object form, val = Qnil;
761 struct gcpro gcpro1;
762
763 GCPRO1 (args);
764
765 {
766 LIST_LOOP_2 (form, args)
767 val = Feval (form);
768 }
769
770 UNGCPRO;
771 return val;
772 }
773
774 /* Fprog1() is the canonical example of a function that must GCPRO a
775 Lisp_Object across calls to Feval(). */
776
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.
782 */
783 (args))
784 {
785 /* This function can GC */
786 REGISTER Lisp_Object val, form;
787 struct gcpro gcpro1;
788
789 val = Feval (XCAR (args));
790
791 GCPRO1 (val);
792
793 {
794 LIST_LOOP_2 (form, XCDR (args))
795 Feval (form);
796 }
797
798 UNGCPRO;
799 return val;
800 }
801
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.
807 */
808 (args))
809 {
810 /* This function can GC */
811 REGISTER Lisp_Object val, form, tail;
812 struct gcpro gcpro1;
813
814 Feval (XCAR (args));
815 args = XCDR (args);
816 val = Feval (XCAR (args));
817 args = XCDR (args);
818
819 GCPRO1 (val);
820
821 LIST_LOOP_3 (form, args, tail)
822 Feval (form);
823
824 UNGCPRO;
825 return val;
826 }
827
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.
834 */
835 (args))
836 {
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();
842
843 EXTERNAL_LIST_LOOP_3 (var, varlist, tail)
844 {
845 Lisp_Object symbol, value, tem;
846 if (SYMBOLP (var))
847 symbol = var, value = Qnil;
848 else
849 {
850 CHECK_CONS (var);
851 symbol = XCAR (var);
852 tem = XCDR (var);
853 if (NILP (tem))
854 value = Qnil;
855 else
856 {
857 CHECK_CONS (tem);
858 value = Feval (XCAR (tem));
859 if (!NILP (XCDR (tem)))
860 signal_simple_error
861 ("`let' bindings can have only one value-form", var);
862 }
863 }
864 specbind (symbol, value);
865 }
866 return unbind_to (speccount, Fprogn (body));
867 }
868
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.
875 */
876 (args))
877 {
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();
883 Lisp_Object *temps;
884 int idx;
885 struct gcpro gcpro1;
886
887 /* Make space to hold the values to give the bound variables. */
888 {
889 int varcount;
890 GET_EXTERNAL_LIST_LENGTH (varlist, varcount);
891 temps = alloca_array (Lisp_Object, varcount);
892 }
893
894 /* Compute the values and store them in `temps' */
895 GCPRO1 (*temps);
896 gcpro1.nvars = 0;
897
898 idx = 0;
899 LIST_LOOP_3 (var, varlist, tail)
900 {
901 Lisp_Object *value = &temps[idx++];
902 if (SYMBOLP (var))
903 *value = Qnil;
904 else
905 {
906 Lisp_Object tem;
907 CHECK_CONS (var);
908 tem = XCDR (var);
909 if (NILP (tem))
910 *value = Qnil;
911 else
912 {
913 CHECK_CONS (tem);
914 *value = Feval (XCAR (tem));
915 gcpro1.nvars = idx;
916
917 if (!NILP (XCDR (tem)))
918 signal_simple_error
919 ("`let' bindings can have only one value-form", var);
920 }
921 }
922 }
923
924 idx = 0;
925 LIST_LOOP_3 (var, varlist, tail)
926 {
927 specbind (SYMBOLP (var) ? var : XCAR (var), temps[idx++]);
928 }
929
930 UNGCPRO;
931
932 return unbind_to (speccount, Fprogn (body));
933 }
934
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.
939 */
940 (args))
941 {
942 /* This function can GC */
943 Lisp_Object test = XCAR (args);
944 Lisp_Object body = XCDR (args);
945
946 while (!NILP (Feval (test)))
947 {
948 QUIT;
949 Fprogn (body);
950 }
951
952 return Qnil;
953 }
954
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.
963 */
964 (args))
965 {
966 /* This function can GC */
967 Lisp_Object symbol, tail, val = Qnil;
968 int nargs;
969 struct gcpro gcpro1;
970
971 GET_LIST_LENGTH (args, nargs);
972
973 if (nargs & 1) /* Odd number of arguments? */
974 Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, make_int (nargs)));
975
976 GCPRO1 (val);
977
978 PROPERTY_LIST_LOOP (tail, symbol, val, args)
979 {
980 val = Feval (val);
981 Fset (symbol, val);
982 }
983
984 UNGCPRO;
985 return val;
986 }
987
988 DEFUN ("quote", Fquote, 1, UNEVALLED, 0, /*
989 Return the argument, without evaluating it. `(quote x)' yields `x'.
990 */
991 (args))
992 {
993 return XCAR (args);
994 }
995
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.
1000 */
1001 (args))
1002 {
1003 return XCAR (args);
1004 }
1005
1006
1007 /************************************************************************/
1008 /* Defining functions/variables */
1009 /************************************************************************/
1010 static Lisp_Object
1011 define_function (Lisp_Object name, Lisp_Object defn)
1012 {
1013 Ffset (name, defn);
1014 LOADHIST_ATTACH (name);
1015 return name;
1016 }
1017
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'.
1022 */
1023 (args))
1024 {
1025 /* This function can GC */
1026 return define_function (XCAR (args),
1027 Fcons (Qlambda, XCDR (args)));
1028 }
1029
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.
1037 */
1038 (args))
1039 {
1040 /* This function can GC */
1041 return define_function (XCAR (args),
1042 Fcons (Qmacro, Fcons (Qlambda, XCDR (args))));
1043 }
1044
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.
1050
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
1054 void.)
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.
1061
1062 In lisp-interaction-mode defvar is treated as defconst.
1063 */
1064 (args))
1065 {
1066 /* This function can GC */
1067 Lisp_Object sym = XCAR (args);
1068
1069 if (!NILP (args = XCDR (args)))
1070 {
1071 Lisp_Object val = XCAR (args);
1072
1073 if (NILP (Fdefault_boundp (sym)))
1074 {
1075 struct gcpro gcpro1;
1076 GCPRO1 (val);
1077 val = Feval (val);
1078 Fset_default (sym, val);
1079 UNGCPRO;
1080 }
1081
1082 if (!NILP (args = XCDR (args)))
1083 {
1084 Lisp_Object doc = XCAR (args);
1085 Fput (sym, Qvariable_documentation, doc);
1086 if (!NILP (args = XCDR (args)))
1087 error ("too many arguments");
1088 }
1089 }
1090
1091 #ifdef I18N3
1092 if (!NILP (Vfile_domain))
1093 Fput (sym, Qvariable_domain, Vfile_domain);
1094 #endif
1095
1096 LOADHIST_ATTACH (sym);
1097 return sym;
1098 }
1099
1100 DEFUN ("defconst", Fdefconst, 2, UNEVALLED, 0, /*
1101 \(defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant
1102 variable.
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.
1110
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.
1116 */
1117 (args))
1118 {
1119 /* This function can GC */
1120 Lisp_Object sym = XCAR (args);
1121 Lisp_Object val = Feval (XCAR (args = XCDR (args)));
1122 struct gcpro gcpro1;
1123
1124 GCPRO1 (val);
1125
1126 Fset_default (sym, val);
1127
1128 UNGCPRO;
1129
1130 if (!NILP (args = XCDR (args)))
1131 {
1132 Lisp_Object doc = XCAR (args);
1133 Fput (sym, Qvariable_documentation, doc);
1134 if (!NILP (args = XCDR (args)))
1135 error ("too many arguments");
1136 }
1137
1138 #ifdef I18N3
1139 if (!NILP (Vfile_domain))
1140 Fput (sym, Qvariable_domain, Vfile_domain);
1141 #endif
1142
1143 LOADHIST_ATTACH (sym);
1144 return sym;
1145 }
1146
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 `*'.
1152 */
1153 (variable))
1154 {
1155 Lisp_Object documentation = Fget (variable, Qvariable_documentation, Qnil);
1156
1157 return
1158 ((INTP (documentation) && XINT (documentation) < 0) ||
1159
1160 (STRINGP (documentation) &&
1161 (string_byte (XSTRING (documentation), 0) == '*')) ||
1162
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)) ?
1168 Qt : Qnil;
1169 }
1170
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.
1176
1177 The second optional arg ENVIRONMENT species an environment of macro
1178 definitions to shadow the loaded ones for use in file byte-compilation.
1179 */
1180 (form, env))
1181 {
1182 /* This function can GC */
1183 /* With cleanups from Hallvard Furuseth. */
1184 REGISTER Lisp_Object expander, sym, def, tem;
1185
1186 while (1)
1187 {
1188 /* Come back here each time we expand a macro call,
1189 in case it expands into another macro call. */
1190 if (!CONSP (form))
1191 break;
1192 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1193 def = sym = XCAR (form);
1194 tem = Qnil;
1195 /* Trace symbols aliases to other symbols
1196 until we get a symbol that is not an alias. */
1197 while (SYMBOLP (def))
1198 {
1199 QUIT;
1200 sym = def;
1201 tem = Fassq (sym, env);
1202 if (NILP (tem))
1203 {
1204 def = XSYMBOL (sym)->function;
1205 if (!UNBOUNDP (def))
1206 continue;
1207 }
1208 break;
1209 }
1210 /* Right now TEM is the result from SYM in ENV,
1211 and if TEM is nil then DEF is SYM's function definition. */
1212 if (NILP (tem))
1213 {
1214 /* SYM is not mentioned in ENV.
1215 Look at its function definition. */
1216 if (UNBOUNDP (def)
1217 || !CONSP (def))
1218 /* Not defined or definition not suitable */
1219 break;
1220 if (EQ (XCAR (def), Qautoload))
1221 {
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))
1225 {
1226 /* Yes, load it and try again. */
1227 do_autoload (def, sym);
1228 continue;
1229 }
1230 else
1231 break;
1232 }
1233 else if (!EQ (XCAR (def), Qmacro))
1234 break;
1235 else expander = XCDR (def);
1236 }
1237 else
1238 {
1239 expander = XCDR (tem);
1240 if (NILP (expander))
1241 break;
1242 }
1243 form = apply1 (expander, XCDR (form));
1244 }
1245 return form;
1246 }
1247
1248
1249 /************************************************************************/
1250 /* Non-local exits */
1251 /************************************************************************/
1252
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'.
1259 */
1260 (args))
1261 {
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);
1266 }
1267
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. */
1271
1272 Lisp_Object
1273 internal_catch (Lisp_Object tag,
1274 Lisp_Object (*func) (Lisp_Object arg),
1275 Lisp_Object arg,
1276 int * volatile threw)
1277 {
1278 /* This structure is made part of the chain `catchlist'. */
1279 struct catchtag c;
1280
1281 /* Fill in the components of c, and put it on the list. */
1282 c.next = catchlist;
1283 c.tag = tag;
1284 c.val = Qnil;
1285 c.backlist = backtrace_list;
1286 #if 0 /* FSFmacs */
1287 /* #### */
1288 c.handlerlist = handlerlist;
1289 #endif
1290 c.lisp_eval_depth = lisp_eval_depth;
1291 c.pdlcount = specpdl_depth();
1292 #if 0 /* FSFmacs */
1293 c.poll_suppress_count = async_timer_suppress_count;
1294 #endif
1295 c.gcpro = gcprolist;
1296 catchlist = &c;
1297
1298 /* Call FUNC. */
1299 if (SETJMP (c.jmp))
1300 {
1301 /* Throw works by a longjmp that comes right here. */
1302 if (threw) *threw = 1;
1303 return c.val;
1304 }
1305 c.val = (*func) (arg);
1306 if (threw) *threw = 0;
1307 catchlist = c.next;
1308 return c.val;
1309 }
1310
1311
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.
1314
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.
1318
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
1324 specified in the
1325
1326 This is used for correct unwinding in Fthrow and Fsignal. */
1327
1328 static void
1329 unwind_to_catch (struct catchtag *c, Lisp_Object val)
1330 {
1331 #if 0 /* FSFmacs */
1332 /* #### */
1333 REGISTER int last_time;
1334 #endif
1335
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.
1341 */
1342
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.) */
1346 c->val = val;
1347
1348 #if 0 /* FSFmacs */
1349 /* Restore the polling-suppression count. */
1350 set_poll_suppress_count (catch->poll_suppress_count);
1351 #endif
1352
1353 #if 0 /* FSFmacs */
1354 /* #### FSFmacs has the following loop. Is it more correct? */
1355 do
1356 {
1357 last_time = catchlist == c;
1358
1359 /* Unwind the specpdl stack, and then restore the proper set of
1360 handlers. */
1361 unbind_to (catchlist->pdlcount, Qnil);
1362 handlerlist = catchlist->handlerlist;
1363 catchlist = catchlist->next;
1364 }
1365 while (! last_time);
1366 #else /* Actual XEmacs code */
1367 /* Unwind the specpdl stack */
1368 unbind_to (c->pdlcount, Qnil);
1369 catchlist = c->next;
1370 #endif
1371
1372 gcprolist = c->gcpro;
1373 backtrace_list = c->backlist;
1374 lisp_eval_depth = c->lisp_eval_depth;
1375
1376 #if 0 /* no longer used */
1377 throw_level = 0;
1378 #endif
1379 LONGJMP (c->jmp, 1);
1380 }
1381
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)
1385 {
1386 #if 0
1387 /* die if we recurse more than is reasonable */
1388 if (++throw_level > 20)
1389 abort();
1390 #endif
1391
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.
1399
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/
1404 top_level_1.
1405
1406 #### Fix this horrifitude!
1407 */
1408
1409 while (1)
1410 {
1411 REGISTER struct catchtag *c;
1412
1413 #if 0 /* FSFmacs */
1414 if (!NILP (tag)) /* #### */
1415 #endif
1416 for (c = catchlist; c; c = c->next)
1417 {
1418 if (EQ (c->tag, tag))
1419 unwind_to_catch (c, val);
1420 }
1421 if (!bomb_out_p)
1422 tag = Fsignal (Qno_catch, list2 (tag, val));
1423 else
1424 call1 (Qreally_early_error_handler, Fcons (sig, data));
1425 }
1426
1427 /* can't happen. who cares? - (Sun's compiler does) */
1428 /* throw_level--; */
1429 /* getting tired of compilation warnings */
1430 /* return Qnil; */
1431 }
1432
1433 /* See above, where CATCHLIST is defined, for a description of how
1434 Fthrow() works.
1435
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.
1450 */
1451
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.
1455 */
1456 (tag, val))
1457 {
1458 throw_or_bomb_out (tag, val, 0, Qnil, Qnil); /* Doesn't return */
1459 return Qnil;
1460 }
1461
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.
1468 */
1469 (args))
1470 {
1471 /* This function can GC */
1472 int speccount = specpdl_depth();
1473
1474 record_unwind_protect (Fprogn, XCDR (args));
1475 return unbind_to (speccount, Feval (XCAR (args)));
1476 }
1477
1478
1479 /************************************************************************/
1480 /* Signalling and trapping errors */
1481 /************************************************************************/
1482
1483 static Lisp_Object
1484 condition_bind_unwind (Lisp_Object loser)
1485 {
1486 struct Lisp_Cons *victim;
1487 /* ((handler-fun . handler-args) ... other handlers) */
1488 Lisp_Object tem = XCAR (loser);
1489
1490 while (CONSP (tem))
1491 {
1492 victim = XCONS (tem);
1493 tem = victim->cdr;
1494 free_cons (victim);
1495 }
1496 victim = XCONS (loser);
1497
1498 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1499 Vcondition_handlers = victim->cdr;
1500
1501 free_cons (victim);
1502 return Qnil;
1503 }
1504
1505 static Lisp_Object
1506 condition_case_unwind (Lisp_Object loser)
1507 {
1508 struct Lisp_Cons *victim;
1509
1510 /* ((<unbound> . clauses) ... other handlers */
1511 victim = XCONS (XCAR (loser));
1512 free_cons (victim);
1513
1514 victim = XCONS (loser);
1515 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1516 Vcondition_handlers = victim->cdr;
1517
1518 free_cons (victim);
1519 return Qnil;
1520 }
1521
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. */
1524
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
1532 HANDLERS:
1533
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.
1539
1540 A HANDLERS value of Qerror is the same as Qt except that the
1541 debugger is invoked if `debug-on-error' was set.
1542
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.
1548
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.
1558
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
1570 loop. */
1571
1572 Lisp_Object
1573 condition_case_1 (Lisp_Object handlers,
1574 Lisp_Object (*bfun) (Lisp_Object barg),
1575 Lisp_Object barg,
1576 Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg),
1577 Lisp_Object harg)
1578 {
1579 int speccount = specpdl_depth();
1580 struct catchtag c;
1581 struct gcpro gcpro1;
1582
1583 #if 0 /* FSFmacs */
1584 c.tag = Qnil;
1585 #else
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);
1594 #endif
1595 c.val = Qnil;
1596 c.backlist = backtrace_list;
1597 #if 0 /* FSFmacs */
1598 /* #### */
1599 c.handlerlist = handlerlist;
1600 #endif
1601 c.lisp_eval_depth = lisp_eval_depth;
1602 c.pdlcount = specpdl_depth();
1603 #if 0 /* FSFmacs */
1604 c.poll_suppress_count = async_timer_suppress_count;
1605 #endif
1606 c.gcpro = gcprolist;
1607 /* #### FSFmacs does the following statement *after* the setjmp(). */
1608 c.next = catchlist;
1609
1610 if (SETJMP (c.jmp))
1611 {
1612 /* throw does ungcpro, etc */
1613 return (*hfun) (c.val, harg);
1614 }
1615
1616 record_unwind_protect (condition_case_unwind, c.tag);
1617
1618 catchlist = &c;
1619 #if 0 /* FSFmacs */
1620 h.handler = handlers;
1621 h.var = Qnil;
1622 h.next = handlerlist;
1623 h.tag = &c;
1624 handlerlist = &h;
1625 #else
1626 Vcondition_handlers = c.tag;
1627 #endif
1628 GCPRO1 (harg); /* Somebody has to gc-protect */
1629
1630 c.val = ((*bfun) (barg));
1631
1632 /* The following is *not* true: (ben)
1633
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.*/
1637 UNGCPRO;
1638 catchlist = c.next;
1639 Vcondition_handlers = XCDR (c.tag);
1640
1641 return unbind_to (speccount, c.val);
1642 }
1643
1644 static Lisp_Object
1645 run_condition_case_handlers (Lisp_Object val, Lisp_Object var)
1646 {
1647 /* This function can GC */
1648 #if 0 /* FSFmacs */
1649 if (!NILP (h.var))
1650 specbind (h.var, c.val);
1651 val = Fprogn (Fcdr (h.chosen_clause));
1652
1653 /* Note that this just undoes the binding of h.var; whoever
1654 longjmp()ed to us unwound the stack to c.pdlcount before
1655 throwing. */
1656 unbind_to (c.pdlcount, Qnil);
1657 return val;
1658 #else
1659 int speccount;
1660
1661 CHECK_TRUE_LIST (val);
1662 if (NILP (var))
1663 return Fprogn (Fcdr (val)); /* tail call */
1664
1665 speccount = specpdl_depth();
1666 specbind (var, Fcar (val));
1667 val = Fprogn (Fcdr (val));
1668 return unbind_to (speccount, val);
1669 #endif
1670 }
1671
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. */
1675 Lisp_Object
1676 condition_case_3 (Lisp_Object bodyform, Lisp_Object var, Lisp_Object handlers)
1677 {
1678 /* This function can GC */
1679 Lisp_Object handler;
1680
1681 EXTERNAL_LIST_LOOP_2 (handler, handlers)
1682 {
1683 if (NILP (handler))
1684 ;
1685 else if (CONSP (handler))
1686 {
1687 Lisp_Object conditions = XCAR (handler);
1688 /* CONDITIONS must a condition name or a list of condition names */
1689 if (SYMBOLP (conditions))
1690 ;
1691 else
1692 {
1693 Lisp_Object condition;
1694 EXTERNAL_LIST_LOOP_2 (condition, conditions)
1695 if (!SYMBOLP (condition))
1696 goto invalid_condition_handler;
1697 }
1698 }
1699 else
1700 {
1701 invalid_condition_handler:
1702 signal_simple_error ("Invalid condition handler", handler);
1703 }
1704 }
1705
1706 CHECK_SYMBOL (var);
1707
1708 return condition_case_1 (handlers,
1709 Feval, bodyform,
1710 run_condition_case_handlers,
1711 var);
1712 }
1713
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.
1720
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
1725 \(e.g. `quit').
1726
1727 The car of a handler may be a list of condition names
1728 instead of a single condition name.
1729
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.
1734
1735 The value of the last BODY form is returned from the condition-case.
1736 See also the function `signal' for more info.
1737
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.
1743
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'.
1747 */
1748 (args))
1749 {
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);
1755 }
1756
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
1763 was established.
1764
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.)
1772 */
1773 (int nargs, Lisp_Object *args)) /* Note! Args side-effected! */
1774 {
1775 /* This function can GC */
1776 int speccount = specpdl_depth();
1777 Lisp_Object tem;
1778
1779 /* #### If there were a way to check that args[0] were a function
1780 which accepted one arg, that should be done here ... */
1781
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;
1786
1787 /* Caller should have GC-protected args */
1788 return unbind_to (speccount, Ffuncall (nargs - 1, args + 1));
1789 }
1790
1791 static int
1792 condition_type_p (Lisp_Object type, Lisp_Object conditions)
1793 {
1794 if (EQ (type, Qt))
1795 /* (condition-case c # (t c)) catches -all- signals
1796 * Use with caution! */
1797 return 1;
1798
1799 if (SYMBOLP (type))
1800 return !NILP (Fmemq (type, conditions));
1801
1802 for (; CONSP (type); type = XCDR (type))
1803 if (!NILP (Fmemq (XCAR (type), conditions)))
1804 return 1;
1805
1806 return 0;
1807 }
1808
1809 static Lisp_Object
1810 return_from_signal (Lisp_Object value)
1811 {
1812 #if 1
1813 /* Most callers are not prepared to handle gc if this
1814 returns. So, since this feature is not very useful,
1815 take it out. */
1816 /* Have called debugger; return value to signaller */
1817 return value;
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");
1822 #endif
1823 }
1824
1825 extern int in_display;
1826
1827
1828 /************************************************************************/
1829 /* the workhorse error-signaling function */
1830 /************************************************************************/
1831
1832 /* #### This function has not been synched with FSF. It diverges
1833 significantly. */
1834
1835 static Lisp_Object
1836 signal_1 (Lisp_Object sig, Lisp_Object data)
1837 {
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);
1850
1851 if (!initialized)
1852 {
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");
1856 abort ();
1857 }
1858
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. */
1862 abort ();
1863
1864 conditions = Fget (sig, Qerror_conditions, Qnil);
1865
1866 for (handlers = Vcondition_handlers;
1867 CONSP (handlers);
1868 handlers = XCDR (handlers))
1869 {
1870 Lisp_Object handler_fun = XCAR (XCAR (handlers));
1871 Lisp_Object handler_data = XCDR (XCAR (handlers));
1872 Lisp_Object outer_handlers = XCDR (handlers);
1873
1874 if (!UNBOUNDP (handler_fun))
1875 {
1876 /* call-with-condition-handler */
1877 Lisp_Object tem;
1878 Lisp_Object all_handlers = Vcondition_handlers;
1879 struct gcpro ngcpro1;
1880 NGCPRO1 (all_handlers);
1881 Vcondition_handlers = outer_handlers;
1882
1883 tem = signal_call_debugger (conditions, sig, data,
1884 outer_handlers, 1,
1885 &stack_trace_displayed,
1886 &debugger_entered);
1887 if (!UNBOUNDP (tem))
1888 RETURN_NUNGCPRO (return_from_signal (tem));
1889
1890 tem = Fcons (sig, data);
1891 if (NILP (handler_data))
1892 tem = call1 (handler_fun, tem);
1893 else
1894 {
1895 /* (This code won't be used (for now?).) */
1896 struct gcpro nngcpro1;
1897 Lisp_Object args[3];
1898 NNGCPRO1 (args[0]);
1899 nngcpro1.nvars = 3;
1900 args[0] = handler_fun;
1901 args[1] = tem;
1902 args[2] = handler_data;
1903 nngcpro1.var = args;
1904 tem = Fapply (3, args);
1905 NNUNGCPRO;
1906 }
1907 NUNGCPRO;
1908 #if 0
1909 if (!EQ (tem, Qsignal))
1910 return return_from_signal (tem);
1911 #endif
1912 /* If handler didn't throw, try another handler */
1913 Vcondition_handlers = all_handlers;
1914 }
1915
1916 /* It's a condition-case handler */
1917
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))
1921 {
1922 UNGCPRO;
1923 return Fthrow (handlers, Fcons (sig, data));
1924 }
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
1928 handler. */
1929 else if (EQ (handler_data, Qerror))
1930 {
1931 Lisp_Object tem = signal_call_debugger (conditions, sig, data,
1932 outer_handlers, 0,
1933 &stack_trace_displayed,
1934 &debugger_entered);
1935
1936 UNGCPRO;
1937 if (!UNBOUNDP (tem))
1938 return return_from_signal (tem);
1939
1940 tem = Fcons (sig, data);
1941 return Fthrow (handlers, tem);
1942 }
1943 else
1944 {
1945 /* handler established by real (Lisp) condition-case */
1946 Lisp_Object h;
1947
1948 for (h = handler_data; CONSP (h); h = Fcdr (h))
1949 {
1950 Lisp_Object clause = Fcar (h);
1951 Lisp_Object tem = Fcar (clause);
1952
1953 if (condition_type_p (tem, conditions))
1954 {
1955 tem = signal_call_debugger (conditions, sig, data,
1956 outer_handlers, 1,
1957 &stack_trace_displayed,
1958 &debugger_entered);
1959 UNGCPRO;
1960 if (!UNBOUNDP (tem))
1961 return return_from_signal (tem);
1962
1963 /* Doesn't return */
1964 tem = Fcons (Fcons (sig, data), Fcdr (clause));
1965 return Fthrow (handlers, tem);
1966 }
1967 }
1968 }
1969 }
1970
1971 /* If no handler is present now, try to run the debugger,
1972 and if that fails, throw to top level.
1973
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.)
1978
1979 #### Fix this horrifitude!
1980 */
1981 signal_call_debugger (conditions, sig, data, Qnil, 0,
1982 &stack_trace_displayed,
1983 &debugger_entered);
1984 UNGCPRO;
1985 throw_or_bomb_out (Qtop_level, Qt, 1, sig, data); /* Doesn't return */
1986 return Qnil;
1987 }
1988
1989
1990 /****************** Error functions class 1 ******************/
1991
1992 /* Class 1: General functions that signal an error.
1993 These functions take an error type and a list of associated error
1994 data. */
1995
1996 /* The simplest external error function: it would be called
1997 signal_continuable_error() in the terminology below, but it's
1998 Lisp-callable. */
1999
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'.
2007
2008 Note that this function can return, if the debugger is invoked and the
2009 user invokes the "return from signal" option.
2010 */
2011 (error_symbol, data))
2012 {
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
2016 happen. */
2017
2018 struct gcpro gcpro1;
2019
2020 GCPRO1 (data);
2021 if (!NILP (Vcurrent_error_state))
2022 {
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! */
2028 }
2029 RETURN_UNGCPRO (signal_1 (error_symbol, data));
2030 }
2031
2032 /* Signal a non-continuable error. */
2033
2034 DOESNT_RETURN
2035 signal_error (Lisp_Object sig, Lisp_Object data)
2036 {
2037 for (;;)
2038 Fsignal (sig, data);
2039 }
2040
2041 static Lisp_Object
2042 call_with_suspended_errors_1 (Lisp_Object opaque_arg)
2043 {
2044 Lisp_Object val;
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]));
2048 return val;
2049 }
2050
2051 static Lisp_Object
2052 restore_current_warning_class (Lisp_Object warning_class)
2053 {
2054 Vcurrent_warning_class = warning_class;
2055 return Qnil;
2056 }
2057
2058 static Lisp_Object
2059 restore_current_error_state (Lisp_Object error_state)
2060 {
2061 Vcurrent_error_state = error_state;
2062 return Qnil;
2063 }
2064
2065 /* Many functions would like to do one of three things if an error
2066 occurs:
2067
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.
2071
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
2077 to not be errors.
2078 */
2079
2080 Lisp_Object
2081 call_with_suspended_errors (lisp_fn_t fun, volatile Lisp_Object retval,
2082 Lisp_Object class, Error_behavior errb,
2083 int nargs, ...)
2084 {
2085 va_list vargs;
2086 int speccount;
2087 Lisp_Object kludgy_args[22];
2088 Lisp_Object *args = kludgy_args + 2;
2089 int i;
2090 Lisp_Object no_error;
2091
2092 assert (SYMBOLP (class)); /* sanity-check */
2093 assert (!NILP (class));
2094 assert (nargs >= 0 && nargs < 20);
2095
2096 /* ERROR_ME means don't trap errors. (However, if errors are
2097 already trapped, we leave them trapped.)
2098
2099 Otherwise, we trap errors, and trap warnings if ERROR_ME_WARN.
2100
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))
2105 {
2106 if (ERRB_EQ (errb, ERROR_ME_NOT)) /* person wants no warnings */
2107 class = Qnil;
2108 errb = ERROR_ME_NOT;
2109 no_error = Qt;
2110 }
2111 else
2112 no_error = Qnil;
2113
2114 va_start (vargs, nargs);
2115 for (i = 0; i < nargs; i++)
2116 args[i] = va_arg (vargs, Lisp_Object);
2117 va_end (vargs);
2118
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. */
2122
2123 if (ERRB_EQ (errb, ERROR_ME))
2124 {
2125 Lisp_Object val;
2126 PRIMITIVE_FUNCALL (val, fun, args, nargs);
2127 return val;
2128 }
2129
2130 speccount = specpdl_depth();
2131 if (NILP (class) || NILP (Vcurrent_warning_class))
2132 {
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;
2140 }
2141 if (!EQ (Vcurrent_error_state, no_error))
2142 {
2143 record_unwind_protect (restore_current_error_state,
2144 Vcurrent_error_state);
2145 Vcurrent_error_state = no_error;
2146 }
2147
2148 {
2149 int threw;
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;
2154
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,
2160 opaque1, &threw);
2161 free_opaque_ptr (opaque1);
2162 free_opaque_ptr (opaque2);
2163 UNGCPRO;
2164 /* Use the returned value except in non-local exit, when
2165 RETVAL applies. */
2166 /* Some perverse compilers require the perverse cast below. */
2167 return unbind_to (speccount,
2168 threw ? *((Lisp_Object*) &(retval)) : the_retval);
2169 }
2170 }
2171
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.). */
2176
2177 void
2178 maybe_signal_error (Lisp_Object sig, Lisp_Object data, Lisp_Object class,
2179 Error_behavior errb)
2180 {
2181 if (ERRB_EQ (errb, ERROR_ME_NOT))
2182 return;
2183 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2184 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2185 else
2186 for (;;)
2187 Fsignal (sig, data);
2188 }
2189
2190 /* Signal a continuable error or display a warning or do nothing,
2191 according to ERRB. */
2192
2193 Lisp_Object
2194 maybe_signal_continuable_error (Lisp_Object sig, Lisp_Object data,
2195 Lisp_Object class, Error_behavior errb)
2196 {
2197 if (ERRB_EQ (errb, ERROR_ME_NOT))
2198 return Qnil;
2199 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2200 {
2201 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2202 return Qnil;
2203 }
2204 else
2205 return Fsignal (sig, data);
2206 }
2207
2208
2209 /****************** Error functions class 2 ******************/
2210
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. */
2214
2215 /* dump an error message; called like printf */
2216
2217 DOESNT_RETURN
2218 error (CONST char *fmt, ...)
2219 {
2220 Lisp_Object obj;
2221 va_list args;
2222
2223 va_start (args, fmt);
2224 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2225 args);
2226 va_end (args);
2227
2228 /* Fsignal GC-protects its args */
2229 signal_error (Qerror, list1 (obj));
2230 }
2231
2232 void
2233 maybe_error (Lisp_Object class, Error_behavior errb, CONST char *fmt, ...)
2234 {
2235 Lisp_Object obj;
2236 va_list args;
2237
2238 /* Optimization: */
2239 if (ERRB_EQ (errb, ERROR_ME_NOT))
2240 return;
2241
2242 va_start (args, fmt);
2243 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2244 args);
2245 va_end (args);
2246
2247 /* Fsignal GC-protects its args */
2248 maybe_signal_error (Qerror, list1 (obj), class, errb);
2249 }
2250
2251 Lisp_Object
2252 continuable_error (CONST char *fmt, ...)
2253 {
2254 Lisp_Object obj;
2255 va_list args;
2256
2257 va_start (args, fmt);
2258 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2259 args);
2260 va_end (args);
2261
2262 /* Fsignal GC-protects its args */
2263 return Fsignal (Qerror, list1 (obj));
2264 }
2265
2266 Lisp_Object
2267 maybe_continuable_error (Lisp_Object class, Error_behavior errb,
2268 CONST char *fmt, ...)
2269 {
2270 Lisp_Object obj;
2271 va_list args;
2272
2273 /* Optimization: */
2274 if (ERRB_EQ (errb, ERROR_ME_NOT))
2275 return Qnil;
2276
2277 va_start (args, fmt);
2278 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2279 args);
2280 va_end (args);
2281
2282 /* Fsignal GC-protects its args */
2283 return maybe_signal_continuable_error (Qerror, list1 (obj), class, errb);
2284 }
2285
2286
2287 /****************** Error functions class 3 ******************/
2288
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). */
2293
2294 DOESNT_RETURN
2295 signal_simple_error (CONST char *reason, Lisp_Object frob)
2296 {
2297 signal_error (Qerror, list2 (build_translated_string (reason), frob));
2298 }
2299
2300 void
2301 maybe_signal_simple_error (CONST char *reason, Lisp_Object frob,
2302 Lisp_Object class, Error_behavior errb)
2303 {
2304 /* Optimization: */
2305 if (ERRB_EQ (errb, ERROR_ME_NOT))
2306 return;
2307 maybe_signal_error (Qerror, list2 (build_translated_string (reason), frob),
2308 class, errb);
2309 }
2310
2311 Lisp_Object
2312 signal_simple_continuable_error (CONST char *reason, Lisp_Object frob)
2313 {
2314 return Fsignal (Qerror, list2 (build_translated_string (reason), frob));
2315 }
2316
2317 Lisp_Object
2318 maybe_signal_simple_continuable_error (CONST char *reason, Lisp_Object frob,
2319 Lisp_Object class, Error_behavior errb)
2320 {
2321 /* Optimization: */
2322 if (ERRB_EQ (errb, ERROR_ME_NOT))
2323 return Qnil;
2324 return maybe_signal_continuable_error
2325 (Qerror, list2 (build_translated_string (reason),
2326 frob), class, errb);
2327 }
2328
2329
2330 /****************** Error functions class 4 ******************/
2331
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
2335 Lisp object.
2336 */
2337
2338 DOESNT_RETURN
2339 error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2340 {
2341 Lisp_Object obj;
2342 va_list args;
2343
2344 va_start (args, fmt);
2345 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2346 args);
2347 va_end (args);
2348
2349 /* Fsignal GC-protects its args */
2350 signal_error (Qerror, list2 (obj, frob));
2351 }
2352
2353 void
2354 maybe_error_with_frob (Lisp_Object frob, Lisp_Object class,
2355 Error_behavior errb, CONST char *fmt, ...)
2356 {
2357 Lisp_Object obj;
2358 va_list args;
2359
2360 /* Optimization: */
2361 if (ERRB_EQ (errb, ERROR_ME_NOT))
2362 return;
2363
2364 va_start (args, fmt);
2365 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2366 args);
2367 va_end (args);
2368
2369 /* Fsignal GC-protects its args */
2370 maybe_signal_error (Qerror, list2 (obj, frob), class, errb);
2371 }
2372
2373 Lisp_Object
2374 continuable_error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2375 {
2376 Lisp_Object obj;
2377 va_list args;
2378
2379 va_start (args, fmt);
2380 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2381 args);
2382 va_end (args);
2383
2384 /* Fsignal GC-protects its args */
2385 return Fsignal (Qerror, list2 (obj, frob));
2386 }
2387
2388 Lisp_Object
2389 maybe_continuable_error_with_frob (Lisp_Object frob, Lisp_Object class,
2390 Error_behavior errb, CONST char *fmt, ...)
2391 {
2392 Lisp_Object obj;
2393 va_list args;
2394
2395 /* Optimization: */
2396 if (ERRB_EQ (errb, ERROR_ME_NOT))
2397 return Qnil;
2398
2399 va_start (args, fmt);
2400 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2401 args);
2402 va_end (args);
2403
2404 /* Fsignal GC-protects its args */
2405 return maybe_signal_continuable_error (Qerror, list2 (obj, frob),
2406 class, errb);
2407 }
2408
2409
2410 /****************** Error functions class 5 ******************/
2411
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. */
2415
2416 DOESNT_RETURN
2417 signal_simple_error_2 (CONST char *reason,
2418 Lisp_Object frob0, Lisp_Object frob1)
2419 {
2420 signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2421 frob1));
2422 }
2423
2424 void
2425 maybe_signal_simple_error_2 (CONST char *reason, Lisp_Object frob0,
2426 Lisp_Object frob1, Lisp_Object class,
2427 Error_behavior errb)
2428 {
2429 /* Optimization: */
2430 if (ERRB_EQ (errb, ERROR_ME_NOT))
2431 return;
2432 maybe_signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2433 frob1), class, errb);
2434 }
2435
2436
2437 Lisp_Object
2438 signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2439 Lisp_Object frob1)
2440 {
2441 return Fsignal (Qerror, list3 (build_translated_string (reason), frob0,
2442 frob1));
2443 }
2444
2445 Lisp_Object
2446 maybe_signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2447 Lisp_Object frob1, Lisp_Object class,
2448 Error_behavior errb)
2449 {
2450 /* Optimization: */
2451 if (ERRB_EQ (errb, ERROR_ME_NOT))
2452 return Qnil;
2453 return maybe_signal_continuable_error
2454 (Qerror, list3 (build_translated_string (reason), frob0,
2455 frob1),
2456 class, errb);
2457 }
2458
2459
2460 /* This is what the QUIT macro calls to signal a quit */
2461 void
2462 signal_quit (void)
2463 {
2464 /* This function can GC */
2465 if (EQ (Vquit_flag, Qcritical))
2466 debug_on_quit |= 2; /* set critical bit. */
2467 Vquit_flag = Qnil;
2468 /* note that this is continuable. */
2469 Fsignal (Qquit, Qnil);
2470 }
2471
2472
2473 /* Used in core lisp functions for efficiency */
2474 void
2475 signal_void_function_error (Lisp_Object function)
2476 {
2477 Fsignal (Qvoid_function, list1 (function));
2478 }
2479
2480 static void
2481 signal_invalid_function_error (Lisp_Object function)
2482 {
2483 Fsignal (Qinvalid_function, list1 (function));
2484 }
2485
2486 static void
2487 signal_wrong_number_of_arguments_error (Lisp_Object function, int nargs)
2488 {
2489 Fsignal (Qwrong_number_of_arguments, list2 (function, make_int (nargs)));
2490 }
2491
2492 /* Used in list traversal macros for efficiency. */
2493 void
2494 signal_malformed_list_error (Lisp_Object list)
2495 {
2496 Fsignal (Qmalformed_list, list1 (list));
2497 }
2498
2499 void
2500 signal_malformed_property_list_error (Lisp_Object list)
2501 {
2502 Fsignal (Qmalformed_property_list, list1 (list));
2503 }
2504
2505 void
2506 signal_circular_list_error (Lisp_Object list)
2507 {
2508 Fsignal (Qcircular_list, list1 (list));
2509 }
2510
2511 void
2512 signal_circular_property_list_error (Lisp_Object list)
2513 {
2514 Fsignal (Qcircular_property_list, list1 (list));
2515 }
2516
2517 /************************************************************************/
2518 /* User commands */
2519 /************************************************************************/
2520
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
2525 definition.
2526
2527 Interactively callable functions include
2528
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'
2534 value
2535 -- subrs (built-in functions) that are interactively callable
2536
2537 Also, a symbol satisfies `commandp' if its function definition does so.
2538 */
2539 (function))
2540 {
2541 Lisp_Object fun = indirect_function (function, 0);
2542
2543 if (COMPILED_FUNCTIONP (fun))
2544 return XCOMPILED_FUNCTION (fun)->flags.interactivep ? Qt : Qnil;
2545
2546 /* Lists may represent commands. */
2547 if (CONSP (fun))
2548 {
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))));
2554 else
2555 return Qnil;
2556 }
2557
2558 /* Emacs primitives are interactive if their DEFUN specifies an
2559 interactive spec. */
2560 if (SUBRP (fun))
2561 return XSUBR (fun)->prompt ? Qt : Qnil;
2562
2563 /* Strings and vectors are keyboard macros. */
2564 if (VECTORP (fun) || STRINGP (fun))
2565 return Qt;
2566
2567 /* Everything else (including Qunbound) is not a command. */
2568 return Qnil;
2569 }
2570
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.
2577 */
2578 (cmd, record, keys))
2579 {
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);
2585
2586 prefixarg = con->prefix_arg;
2587 con->prefix_arg = Qnil;
2588 Vcurrent_prefix_arg = prefixarg;
2589 debug_on_next_call = 0; /* #### from FSFmacs; correct? */
2590
2591 if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil)))
2592 return run_hook (Vdisabled_command_hook);
2593
2594 for (;;)
2595 {
2596 final = indirect_function (cmd, 1);
2597 if (CONSP (final) && EQ (Fcar (final), Qautoload))
2598 do_autoload (final, cmd);
2599 else
2600 break;
2601 }
2602
2603 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final))
2604 {
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);
2612
2613 final = Fcall_interactively (cmd, record, keys);
2614
2615 POP_BACKTRACE (backtrace);
2616 return final;
2617 }
2618 else if (STRINGP (final) || VECTORP (final))
2619 {
2620 return Fexecute_kbd_macro (final, prefixarg);
2621 }
2622 else
2623 {
2624 Fsignal (Qwrong_type_argument,
2625 Fcons (Qcommandp,
2626 (EQ (cmd, final)
2627 ? list1 (cmd)
2628 : list2 (cmd, final))));
2629 return Qnil;
2630 }
2631 }
2632
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).
2638 */
2639 ())
2640 {
2641 REGISTER struct backtrace *btp;
2642 REGISTER Lisp_Object fun;
2643
2644 if (!INTERACTIVE)
2645 return Qnil;
2646
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;
2652
2653 #if 0 /* FSFmacs */
2654
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)
2661 btp = btp->next;
2662
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))
2669 btp = btp->next;
2670
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. */
2673 while (btp &&
2674 btp->nargs == UNEVALLED)
2675 btp = btp->next;
2676
2677 #else
2678
2679 if (! (COMPILED_FUNCTIONP (Findirect_function (*btp->function))))
2680 btp = btp->next;
2681 for (;
2682 btp && (btp->nargs == UNEVALLED
2683 || EQ (*btp->function, Qbyte_code));
2684 btp = btp->next)
2685 {}
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)
2689 return nil. */
2690 /* Beats me why this is necessary, but it is */
2691 if (btp && EQ (*btp->function, Qcall_interactively))
2692 return Qt;
2693
2694 #endif
2695
2696 fun = Findirect_function (*btp->function);
2697 if (SUBRP (fun))
2698 return Qnil;
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))
2702 return Qt;
2703 return Qnil;
2704 }
2705
2706
2707 /************************************************************************/
2708 /* Autoloading */
2709 /************************************************************************/
2710
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.
2724 */
2725 (function, file, docstring, interactive, type))
2726 {
2727 /* This function can GC */
2728 CHECK_SYMBOL (function);
2729 CHECK_STRING (file);
2730
2731 /* If function is defined and not as an autoload, don't override */
2732 {
2733 Lisp_Object f = XSYMBOL (function)->function;
2734 if (!UNBOUNDP (f) && !(CONSP (f) && EQ (XCAR (f), Qautoload)))
2735 return Qnil;
2736 }
2737
2738 if (purify_flag)
2739 {
2740 /* Attempt to avoid consing identical (string=) pure strings. */
2741 file = Fsymbol_name (Fintern (file, Qnil));
2742 }
2743
2744 return Ffset (function, Fcons (Qautoload, list4 (file,
2745 docstring,
2746 interactive,
2747 type)));
2748 }
2749
2750 Lisp_Object
2751 un_autoload (Lisp_Object oldqueue)
2752 {
2753 /* This function can GC */
2754 REGISTER Lisp_Object queue, first, second;
2755
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))
2761 {
2762 first = XCAR (queue);
2763 second = Fcdr (first);
2764 first = Fcar (first);
2765 if (NILP (second))
2766 Vfeatures = first;
2767 else
2768 Ffset (first, second);
2769 queue = Fcdr (queue);
2770 }
2771 return Qnil;
2772 }
2773
2774 void
2775 do_autoload (Lisp_Object fundef,
2776 Lisp_Object funname)
2777 {
2778 /* This function can GC */
2779 int speccount = specpdl_depth();
2780 Lisp_Object fun = funname;
2781 struct gcpro gcpro1, gcpro2;
2782
2783 CHECK_SYMBOL (funname);
2784 GCPRO2 (fun, funname);
2785
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);
2790
2791 {
2792 Lisp_Object queue;
2793
2794 /* Save the old autoloads, in case we ever do an unload. */
2795 for (queue = Vautoload_queue; CONSP (queue); queue = XCDR (queue))
2796 {
2797 Lisp_Object first = XCAR (queue);
2798 Lisp_Object second = Fcdr (first);
2799
2800 first = Fcar (first);
2801
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
2804 or fset. */
2805 if (CONSP (second))
2806 Fput (first, Qautoload, (XCDR (second)));
2807 }
2808 }
2809
2810 /* Once loading finishes, don't undo it. */
2811 Vautoload_queue = Qt;
2812 unbind_to (speccount, Qnil);
2813
2814 fun = indirect_function (fun, 0);
2815
2816 #if 0 /* FSFmacs */
2817 if (!NILP (Fequal (fun, fundef)))
2818 #else
2819 if (UNBOUNDP (fun)
2820 || (CONSP (fun)
2821 && EQ (XCAR (fun), Qautoload)))
2822 #endif
2823 error ("Autoloading failed to define function %s",
2824 string_data (XSYMBOL (funname)->name));
2825 UNGCPRO;
2826 }
2827
2828
2829 /************************************************************************/
2830 /* eval, funcall, apply */
2831 /************************************************************************/
2832
2833 static Lisp_Object funcall_lambda (Lisp_Object fun,
2834 int nargs, Lisp_Object args[]);
2835 static int in_warnings;
2836
2837 static Lisp_Object
2838 in_warnings_restore (Lisp_Object minimus)
2839 {
2840 in_warnings = 0;
2841 return Qnil;
2842 }
2843
2844 DEFUN ("eval", Feval, 1, 1, 0, /*
2845 Evaluate FORM and return its value.
2846 */
2847 (form))
2848 {
2849 /* This function can GC */
2850 Lisp_Object fun, val, original_fun, original_args;
2851 int nargs;
2852 struct backtrace backtrace;
2853
2854 /* I think this is a pretty safe place to call Lisp code, don't you? */
2855 while (!in_warnings && !NILP (Vpending_warnings))
2856 {
2857 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2858 int speccount = specpdl_depth();
2859 Lisp_Object this_warning_cons, this_warning, class, level, messij;
2860
2861 record_unwind_protect (in_warnings_restore, Qnil);
2862 in_warnings = 1;
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);
2873
2874 if (NILP (Vpending_warnings))
2875 Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary,
2876 but safer */
2877
2878 GCPRO4 (form, class, level, messij);
2879 if (!STRINGP (messij))
2880 messij = Fprin1_to_string (messij, Qnil);
2881 call3 (Qdisplay_warning, class, messij, level);
2882 UNGCPRO;
2883 unbind_to (speccount, Qnil);
2884 }
2885
2886 if (!CONSP (form))
2887 {
2888 if (SYMBOLP (form))
2889 return Fsymbol_value (form);
2890 else
2891 return form;
2892 }
2893
2894 QUIT;
2895 if ((consing_since_gc > gc_cons_threshold) || always_gc)
2896 {
2897 struct gcpro gcpro1;
2898 GCPRO1 (form);
2899 garbage_collect_1 ();
2900 UNGCPRO;
2901 }
2902
2903 if (++lisp_eval_depth > max_lisp_eval_depth)
2904 {
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'");
2909 }
2910
2911 /* We guaranteed CONSP (form) above */
2912 original_fun = XCAR (form);
2913 original_args = XCDR (form);
2914
2915 GET_EXTERNAL_LIST_LENGTH (original_args, nargs);
2916
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);
2924
2925 if (debug_on_next_call)
2926 do_debug_on_call (Qt);
2927
2928 if (profiling_active)
2929 profile_increase_call_count (original_fun);
2930
2931 /* At this point, only original_fun and original_args
2932 have values that will be used below. */
2933 retry:
2934 fun = indirect_function (original_fun, 1);
2935
2936 if (SUBRP (fun))
2937 {
2938 Lisp_Subr *subr = XSUBR (fun);
2939 int max_args = subr->max_args;
2940
2941 if (nargs < subr->min_args)
2942 goto wrong_number_of_arguments;
2943
2944 if (max_args == UNEVALLED) /* Optimize for the common case */
2945 {
2946 backtrace.evalargs = 0;
2947 val = (((Lisp_Object (*) (Lisp_Object)) subr_function (subr))
2948 (original_args));
2949 }
2950 else if (nargs <= max_args)
2951 {
2952 struct gcpro gcpro1;
2953 Lisp_Object args[SUBR_MAX_ARGS];
2954 REGISTER Lisp_Object *p = args;
2955
2956 GCPRO1 (args[0]);
2957 gcpro1.nvars = 0;
2958
2959 {
2960 REGISTER Lisp_Object arg;
2961 LIST_LOOP_2 (arg, original_args)
2962 {
2963 *p++ = Feval (arg);
2964 gcpro1.nvars++;
2965 }
2966 }
2967
2968 /* &optional args default to nil. */
2969 while (p - args < max_args)
2970 *p++ = Qnil;
2971
2972 backtrace.args = args;
2973 backtrace.nargs = nargs;
2974
2975 FUNCALL_SUBR (val, subr, args, max_args);
2976
2977 UNGCPRO;
2978 }
2979 else if (max_args == MANY)
2980 {
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;
2985
2986 GCPRO1 (args[0]);
2987 gcpro1.nvars = 0;
2988
2989 {
2990 REGISTER Lisp_Object arg;
2991 LIST_LOOP_2 (arg, original_args)
2992 {
2993 *p++ = Feval (arg);
2994 gcpro1.nvars++;
2995 }
2996 }
2997
2998 backtrace.args = args;
2999 backtrace.nargs = nargs;
3000
3001 val = (((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr))
3002 (nargs, args));
3003
3004 UNGCPRO;
3005 }
3006 else
3007 {
3008 wrong_number_of_arguments:
3009 signal_wrong_number_of_arguments_error (fun, nargs);
3010 }
3011 }
3012 else if (COMPILED_FUNCTIONP (fun))
3013 {
3014 struct gcpro gcpro1;
3015 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
3016 REGISTER Lisp_Object *p = args;
3017
3018 GCPRO1 (args[0]);
3019 gcpro1.nvars = 0;
3020
3021 {
3022 REGISTER Lisp_Object arg;
3023 LIST_LOOP_2 (arg, original_args)
3024 {
3025 *p++ = Feval (arg);
3026 gcpro1.nvars++;
3027 }
3028 }
3029
3030 backtrace.args = args;
3031 backtrace.nargs = nargs;
3032 backtrace.evalargs = 0;
3033
3034 val = funcall_compiled_function (fun, nargs, args);
3035
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;
3041
3042 UNGCPRO;
3043 }
3044 else if (CONSP (fun))
3045 {
3046 Lisp_Object funcar = XCAR (fun);
3047
3048 if (EQ (funcar, Qautoload))
3049 {
3050 do_autoload (fun, original_fun);
3051 goto retry;
3052 }
3053 else if (EQ (funcar, Qmacro))
3054 {
3055 val = Feval (apply1 (XCDR (fun), original_args));
3056 }
3057 else if (EQ (funcar, Qlambda))
3058 {
3059 struct gcpro gcpro1;
3060 Lisp_Object *args = alloca_array (Lisp_Object, nargs);
3061 REGISTER Lisp_Object *p = args;
3062
3063 GCPRO1 (args[0]);
3064 gcpro1.nvars = 0;
3065
3066 {
3067 REGISTER Lisp_Object arg;
3068 LIST_LOOP_2 (arg, original_args)
3069 {
3070 *p++ = Feval (arg);
3071 gcpro1.nvars++;
3072 }
3073 }
3074
3075 UNGCPRO;
3076
3077 backtrace.args = args; /* this also GCPROs `args' */
3078 backtrace.nargs = nargs;
3079 backtrace.evalargs = 0;
3080
3081 val = funcall_lambda (fun, nargs, args);
3082
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;
3088 }
3089 else
3090 {
3091 goto invalid_function;
3092 }
3093 }
3094 else /* ! (SUBRP (fun) || COMPILED_FUNCTIONP (fun) || CONSP (fun)) */
3095 {
3096 invalid_function:
3097 signal_invalid_function_error (fun);
3098 }
3099
3100 lisp_eval_depth--;
3101 if (backtrace.debug_on_exit)
3102 val = do_debug_on_exit (val);
3103 POP_BACKTRACE (backtrace);
3104 return val;
3105 }
3106
3107
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).
3111 */
3112 (int nargs, Lisp_Object *args))
3113 {
3114 /* This function can GC */
3115 Lisp_Object fun;
3116 Lisp_Object val;
3117 struct backtrace backtrace;
3118 int fun_nargs = nargs - 1;
3119 Lisp_Object *fun_args = args + 1;
3120
3121 QUIT;
3122 if ((consing_since_gc > gc_cons_threshold) || always_gc)
3123 /* Callers should gcpro lexpr args */
3124 garbage_collect_1 ();
3125
3126 if (++lisp_eval_depth > max_lisp_eval_depth)
3127 {
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'");
3132 }
3133
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);
3141
3142 if (debug_on_next_call)
3143 do_debug_on_call (Qlambda);
3144
3145 retry:
3146
3147 fun = args[0];
3148
3149 /* It might be useful to place this *after* all the checks. */
3150 if (profiling_active)
3151 profile_increase_call_count (fun);
3152
3153 /* We could call indirect_function directly, but profiling shows
3154 this is worth optimizing by partially unrolling the loop. */
3155 if (SYMBOLP (fun))
3156 {
3157 fun = XSYMBOL (fun)->function;
3158 if (SYMBOLP (fun))
3159 {
3160 fun = XSYMBOL (fun)->function;
3161 if (SYMBOLP (fun))
3162 fun = indirect_function (fun, 1);
3163 }
3164 }
3165
3166 if (SUBRP (fun))
3167 {
3168 Lisp_Subr *subr = XSUBR (fun);
3169 int max_args = subr->max_args;
3170 Lisp_Object spacious_args[SUBR_MAX_ARGS];
3171
3172 if (fun_nargs < subr->min_args)
3173 goto wrong_number_of_arguments;
3174
3175 if (fun_nargs == max_args) /* Optimize for the common case */
3176 {
3177 funcall_subr:
3178 FUNCALL_SUBR (val, subr, fun_args, max_args);
3179 }
3180 else if (fun_nargs < max_args)
3181 {
3182 Lisp_Object *p = spacious_args;
3183
3184 /* Default optionals to nil */
3185 while (fun_nargs--)
3186 *p++ = *fun_args++;
3187 while (p - spacious_args < max_args)
3188 *p++ = Qnil;
3189
3190 fun_args = spacious_args;
3191 goto funcall_subr;
3192 }
3193 else if (max_args == MANY)
3194 {
3195 val = ((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr))
3196 (fun_nargs, fun_args);
3197 }
3198 else if (max_args == UNEVALLED) /* Can't funcall a special form */
3199 {
3200 goto invalid_function;
3201 }
3202 else
3203 {
3204 wrong_number_of_arguments:
3205 signal_wrong_number_of_arguments_error (fun, fun_nargs);
3206 }
3207 }
3208 else if (COMPILED_FUNCTIONP (fun))
3209 {
3210 val = funcall_compiled_function (fun, fun_nargs, fun_args);
3211 }
3212 else if (CONSP (fun))
3213 {
3214 Lisp_Object funcar = XCAR (fun);
3215
3216 if (EQ (funcar, Qlambda))
3217 {
3218 val = funcall_lambda (fun, fun_nargs, fun_args);
3219 }
3220 else if (EQ (funcar, Qautoload))
3221 {
3222 do_autoload (fun, args[0]);
3223 goto retry;
3224 }
3225 else /* Can't funcall a macro */
3226 {
3227 goto invalid_function;
3228 }
3229 }
3230 else if (UNBOUNDP (fun))
3231 {
3232 signal_void_function_error (args[0]);
3233 }
3234 else
3235 {
3236 invalid_function:
3237 signal_invalid_function_error (fun);
3238 }
3239
3240 lisp_eval_depth--;
3241 if (backtrace.debug_on_exit)
3242 val = do_debug_on_exit (val);
3243 POP_BACKTRACE (backtrace);
3244 return val;
3245 }
3246
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'.
3251 */
3252 (object))
3253 {
3254 if (SYMBOLP (object))
3255 object = indirect_function (object, 0);
3256
3257 return
3258 (SUBRP (object) ||
3259 COMPILED_FUNCTIONP (object) ||
3260 (CONSP (object) &&
3261 (EQ (XCAR (object), Qlambda) ||
3262 EQ (XCAR (object), Qautoload))))
3263 ? Qt : Qnil;
3264 }
3265
3266 static Lisp_Object
3267 function_argcount (Lisp_Object function, int function_min_args_p)
3268 {
3269 Lisp_Object orig_function = function;
3270 Lisp_Object arglist;
3271
3272 retry:
3273
3274 if (SYMBOLP (function))
3275 function = indirect_function (function, 1);
3276
3277 if (SUBRP (function))
3278 {
3279 return function_min_args_p ?
3280 Fsubr_min_args (function):
3281 Fsubr_max_args (function);
3282 }
3283 else if (COMPILED_FUNCTIONP (function))
3284 {
3285 arglist = compiled_function_arglist (XCOMPILED_FUNCTION (function));
3286 }
3287 else if (CONSP (function))
3288 {
3289 Lisp_Object funcar = XCAR (function);
3290
3291 if (EQ (funcar, Qmacro))
3292 {
3293 function = XCDR (function);
3294 goto retry;
3295 }
3296 else if (EQ (funcar, Qautoload))
3297 {
3298 do_autoload (function, orig_function);
3299 goto retry;
3300 }
3301 else if (EQ (funcar, Qlambda))
3302 {
3303 arglist = Fcar (XCDR (function));
3304 }
3305 else
3306 {
3307 goto invalid_function;
3308 }
3309 }
3310 else
3311 {
3312 invalid_function:
3313 return Fsignal (Qinvalid_function, list1 (function));
3314 }
3315
3316 {
3317 int argcount = 0;
3318 Lisp_Object arg;
3319
3320 EXTERNAL_LIST_LOOP_2 (arg, arglist)
3321 {
3322 if (EQ (arg, Qand_optional))
3323 {
3324 if (function_min_args_p)
3325 break;
3326 }
3327 else if (EQ (arg, Qand_rest))
3328 {
3329 if (function_min_args_p)
3330 break;
3331 else
3332 return Qnil;
3333 }
3334 else
3335 {
3336 argcount++;
3337 }
3338 }
3339
3340 return make_int (argcount);
3341 }
3342 }
3343
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.
3348 */
3349 (function))
3350 {
3351 return function_argcount (function, 1);
3352 }
3353
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.
3360 */
3361 (function))
3362 {
3363 return function_argcount (function, 0);
3364 }
3365
3366
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.
3370 */
3371 (int nargs, Lisp_Object *args))
3372 {
3373 /* This function can GC */
3374 Lisp_Object fun = args[0];
3375 Lisp_Object spread_arg = args [nargs - 1];
3376 int numargs;
3377 int funcall_nargs;
3378
3379 GET_EXTERNAL_LIST_LENGTH (spread_arg, numargs);
3380
3381 if (numargs == 0)
3382 /* (apply foo 0 1 '()) */
3383 return Ffuncall (nargs - 1, args);
3384 else if (numargs == 1)
3385 {
3386 /* (apply foo 0 1 '(2)) */
3387 args [nargs - 1] = XCAR (spread_arg);
3388 return Ffuncall (nargs, args);
3389 }
3390
3391 /* -1 for function, -1 for spread arg */
3392 numargs = nargs - 2 + numargs;
3393 /* +1 for function */
3394 funcall_nargs = 1 + numargs;
3395
3396 if (SYMBOLP (fun))
3397 fun = indirect_function (fun, 0);
3398
3399 if (SUBRP (fun))
3400 {
3401 Lisp_Subr *subr = XSUBR (fun);
3402 int max_args = subr->max_args;
3403
3404 if (numargs < subr->min_args
3405 || (max_args >= 0 && max_args < numargs))
3406 {
3407 /* Let funcall get the error */
3408 }
3409 else if (max_args > numargs)
3410 {
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);
3414 }
3415 }
3416 else if (UNBOUNDP (fun))
3417 {
3418 /* Let funcall get the error */
3419 fun = args[0];
3420 }
3421
3422 {
3423 REGISTER int i;
3424 Lisp_Object *funcall_args = alloca_array (Lisp_Object, funcall_nargs);
3425 struct gcpro gcpro1;
3426
3427 GCPRO1 (*funcall_args);
3428 gcpro1.nvars = funcall_nargs;
3429
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. */
3434 for (i = nargs - 1;
3435 !NILP (spread_arg); /* i < 1 + numargs */
3436 i++, spread_arg = XCDR (spread_arg))
3437 {
3438 funcall_args [i] = XCAR (spread_arg);
3439 }
3440 /* Supply nil for optional args (to subrs) */
3441 for (; i < funcall_nargs; i++)
3442 funcall_args[i] = Qnil;
3443
3444
3445 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
3446 }
3447 }
3448
3449
3450 /* Apply lambda list FUN to the NARGS evaluated arguments in ARGS and
3451 return the result of evaluation. */
3452
3453 static Lisp_Object
3454 funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object args[])
3455 {
3456 /* This function can GC */
3457 Lisp_Object symbol, arglist, body, tail;
3458 int speccount = specpdl_depth();
3459 REGISTER int i = 0;
3460
3461 tail = XCDR (fun);
3462
3463 if (!CONSP (tail))
3464 goto invalid_function;
3465
3466 arglist = XCAR (tail);
3467 body = XCDR (tail);
3468
3469 {
3470 int optional = 0, rest = 0;
3471
3472 EXTERNAL_LIST_LOOP_3 (symbol, arglist, tail)
3473 {
3474 if (!SYMBOLP (symbol))
3475 goto invalid_function;
3476 if (EQ (symbol, Qand_rest))
3477 rest = 1;
3478 else if (EQ (symbol, Qand_optional))
3479 optional = 1;
3480 else if (rest)
3481 {
3482 specbind (symbol, Flist (nargs - i, &args[i]));
3483 i = nargs;
3484 }
3485 else if (i < nargs)
3486 specbind (symbol, args[i++]);
3487 else if (!optional)
3488 goto wrong_number_of_arguments;
3489 else
3490 specbind (symbol, Qnil);
3491 }
3492 }
3493
3494 if (i < nargs)
3495 goto wrong_number_of_arguments;
3496
3497 return unbind_to (speccount, Fprogn (body));
3498
3499 wrong_number_of_arguments:
3500 return Fsignal (Qwrong_number_of_arguments, list2 (fun, make_int (nargs)));
3501
3502 invalid_function:
3503 return Fsignal (Qinvalid_function, list1 (fun));
3504 }
3505
3506
3507 /************************************************************************/
3508 /* Run hook variables in various ways. */
3509 /************************************************************************/
3510
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.
3519
3520 To make a hook variable buffer-local, use `make-local-hook',
3521 not `make-local-variable'.
3522 */
3523 (int nargs, Lisp_Object *args))
3524 {
3525 REGISTER int i;
3526
3527 for (i = 0; i < nargs; i++)
3528 run_hook_with_args (1, args + i, RUN_HOOKS_TO_COMPLETION);
3529
3530 return Qnil;
3531 }
3532
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',
3542 as that may change.
3543
3544 To make a hook variable buffer-local, use `make-local-hook',
3545 not `make-local-variable'.
3546 */
3547 (int nargs, Lisp_Object *args))
3548 {
3549 return run_hook_with_args (nargs, args, RUN_HOOKS_TO_COMPLETION);
3550 }
3551
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.
3559
3560 To make a hook variable buffer-local, use `make-local-hook',
3561 not `make-local-variable'.
3562 */
3563 (int nargs, Lisp_Object *args))
3564 {
3565 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_SUCCESS);
3566 }
3567
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.
3575
3576 To make a hook variable buffer-local, use `make-local-hook',
3577 not `make-local-variable'.
3578 */
3579 (int nargs, Lisp_Object *args))
3580 {
3581 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_FAILURE);
3582 }
3583
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]. */
3591
3592 Lisp_Object
3593 run_hook_with_args_in_buffer (struct buffer *buf, int nargs, Lisp_Object *args,
3594 enum run_hooks_condition cond)
3595 {
3596 Lisp_Object sym, val, ret;
3597
3598 if (!initialized || preparing_for_armageddon)
3599 /* We need to bail out of here pronto. */
3600 return Qnil;
3601
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);
3605
3606 sym = args[0];
3607 val = symbol_value_in_buffer (sym, make_buffer (buf));
3608 ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil);
3609
3610 if (UNBOUNDP (val) || NILP (val))
3611 return ret;
3612 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3613 {
3614 args[0] = val;
3615 return Ffuncall (nargs, args);
3616 }
3617 else
3618 {
3619 struct gcpro gcpro1, gcpro2, gcpro3;
3620 Lisp_Object globals = Qnil;
3621 GCPRO3 (sym, val, globals);
3622
3623 for (;
3624 CONSP (val) && ((cond == RUN_HOOKS_TO_COMPLETION)
3625 || (cond == RUN_HOOKS_UNTIL_SUCCESS ? NILP (ret)
3626 : !NILP (ret)));
3627 val = XCDR (val))
3628 {
3629 if (EQ (XCAR (val), Qt))
3630 {
3631 /* t indicates this hook has a local binding;
3632 it means to run the global binding too. */
3633 globals = Fdefault_value (sym);
3634
3635 if ((! CONSP (globals) || EQ (XCAR (globals), Qlambda)) &&
3636 ! NILP (globals))
3637 {
3638 args[0] = globals;
3639 ret = Ffuncall (nargs, args);
3640 }
3641 else
3642 {
3643 for (;
3644 CONSP (globals) && ((cond == RUN_HOOKS_TO_COMPLETION)
3645 || (cond == RUN_HOOKS_UNTIL_SUCCESS
3646 ? NILP (ret)
3647 : !NILP (ret)));
3648 globals = XCDR (globals))
3649 {
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);
3655 }
3656 }
3657 }
3658 else
3659 {
3660 args[0] = XCAR (val);
3661 ret = Ffuncall (nargs, args);
3662 }
3663 }
3664
3665 UNGCPRO;
3666 return ret;
3667 }
3668 }
3669
3670 Lisp_Object
3671 run_hook_with_args (int nargs, Lisp_Object *args,
3672 enum run_hooks_condition cond)
3673 {
3674 return run_hook_with_args_in_buffer (current_buffer, nargs, args, cond);
3675 }
3676
3677 #if 0
3678
3679 /* From FSF 19.30, not currently used */
3680
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]. */
3687
3688 Lisp_Object
3689 run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
3690 {
3691 Lisp_Object sym = args[0];
3692 Lisp_Object val;
3693 struct gcpro gcpro1, gcpro2;
3694
3695 GCPRO2 (sym, val);
3696
3697 for (val = funlist; CONSP (val); val = XCDR (val))
3698 {
3699 if (EQ (XCAR (val), Qt))
3700 {
3701 /* t indicates this hook has a local binding;
3702 it means to run the global binding too. */
3703 Lisp_Object globals;
3704
3705 for (globals = Fdefault_value (sym);
3706 CONSP (globals);
3707 globals = XCDR (globals))
3708 {
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);
3714 }
3715 }
3716 else
3717 {
3718 args[0] = XCAR (val);
3719 Ffuncall (nargs, args);
3720 }
3721 }
3722 UNGCPRO;
3723 return Qnil;
3724 }
3725
3726 #endif /* 0 */
3727
3728 void
3729 va_run_hook_with_args (Lisp_Object hook_var, int nargs, ...)
3730 {
3731 /* This function can GC */
3732 struct gcpro gcpro1;
3733 int i;
3734 va_list vargs;
3735 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
3736
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);
3741 va_end (vargs);
3742
3743 GCPRO1 (*funcall_args);
3744 gcpro1.nvars = nargs + 1;
3745 run_hook_with_args (nargs + 1, funcall_args, RUN_HOOKS_TO_COMPLETION);
3746 UNGCPRO;
3747 }
3748
3749 void
3750 va_run_hook_with_args_in_buffer (struct buffer *buf, Lisp_Object hook_var,
3751 int nargs, ...)
3752 {
3753 /* This function can GC */
3754 struct gcpro gcpro1;
3755 int i;
3756 va_list vargs;
3757 Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
3758
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);
3763 va_end (vargs);
3764
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);
3769 UNGCPRO;
3770 }
3771
3772 Lisp_Object
3773 run_hook (Lisp_Object hook)
3774 {
3775 Frun_hooks (1, &hook);
3776 return Qnil;
3777 }
3778
3779
3780 /************************************************************************/
3781 /* Front-ends to eval, funcall, apply */
3782 /************************************************************************/
3783
3784 /* Apply fn to arg */
3785 Lisp_Object
3786 apply1 (Lisp_Object fn, Lisp_Object arg)
3787 {
3788 /* This function can GC */
3789 struct gcpro gcpro1;
3790 Lisp_Object args[2];
3791
3792 if (NILP (arg))
3793 return Ffuncall (1, &fn);
3794 GCPRO1 (args[0]);
3795 gcpro1.nvars = 2;
3796 args[0] = fn;
3797 args[1] = arg;
3798 RETURN_UNGCPRO (Fapply (2, args));
3799 }
3800
3801 /* Call function fn on no arguments */
3802 Lisp_Object
3803 call0 (Lisp_Object fn)
3804 {
3805 /* This function can GC */
3806 struct gcpro gcpro1;
3807
3808 GCPRO1 (fn);
3809 RETURN_UNGCPRO (Ffuncall (1, &fn));
3810 }
3811
3812 /* Call function fn with argument arg0 */
3813 Lisp_Object
3814 call1 (Lisp_Object fn,
3815 Lisp_Object arg0)
3816 {
3817 /* This function can GC */
3818 struct gcpro gcpro1;
3819 Lisp_Object args[2];
3820 args[0] = fn;
3821 args[1] = arg0;
3822 GCPRO1 (args[0]);
3823 gcpro1.nvars = 2;
3824 RETURN_UNGCPRO (Ffuncall (2, args));
3825 }
3826
3827 /* Call function fn with arguments arg0, arg1 */
3828 Lisp_Object
3829 call2 (Lisp_Object fn,
3830 Lisp_Object arg0, Lisp_Object arg1)
3831 {
3832 /* This function can GC */
3833 struct gcpro gcpro1;
3834 Lisp_Object args[3];
3835 args[0] = fn;
3836 args[1] = arg0;
3837 args[2] = arg1;
3838 GCPRO1 (args[0]);
3839 gcpro1.nvars = 3;
3840 RETURN_UNGCPRO (Ffuncall (3, args));
3841 }
3842
3843 /* Call function fn with arguments arg0, arg1, arg2 */
3844 Lisp_Object
3845 call3 (Lisp_Object fn,
3846 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
3847 {
3848 /* This function can GC */
3849 struct gcpro gcpro1;
3850 Lisp_Object args[4];
3851 args[0] = fn;
3852 args[1] = arg0;
3853 args[2] = arg1;
3854 args[3] = arg2;
3855 GCPRO1 (args[0]);
3856 gcpro1.nvars = 4;
3857 RETURN_UNGCPRO (Ffuncall (4, args));
3858 }
3859
3860 /* Call function fn with arguments arg0, arg1, arg2, arg3 */
3861 Lisp_Object
3862 call4 (Lisp_Object fn,
3863 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3864 Lisp_Object arg3)
3865 {
3866 /* This function can GC */
3867 struct gcpro gcpro1;
3868 Lisp_Object args[5];
3869 args[0] = fn;
3870 args[1] = arg0;
3871 args[2] = arg1;
3872 args[3] = arg2;
3873 args[4] = arg3;
3874 GCPRO1 (args[0]);
3875 gcpro1.nvars = 5;
3876 RETURN_UNGCPRO (Ffuncall (5, args));
3877 }
3878
3879 /* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */
3880 Lisp_Object
3881 call5 (Lisp_Object fn,
3882 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
3883 Lisp_Object arg3, Lisp_Object arg4)
3884 {
3885 /* This function can GC */
3886 struct gcpro gcpro1;
3887 Lisp_Object args[6];
3888 args[0] = fn;
3889 args[1] = arg0;
3890 args[2] = arg1;
3891 args[3] = arg2;
3892 args[4] = arg3;
3893 args[5] = arg4;
3894 GCPRO1 (args[0]);
3895 gcpro1.nvars = 6;
3896 RETURN_UNGCPRO (Ffuncall (6, args));
3897 }
3898
3899 Lisp_Object
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)
3903 {
3904 /* This function can GC */
3905 struct gcpro gcpro1;
3906 Lisp_Object args[7];
3907 args[0] = fn;
3908 args[1] = arg0;
3909 args[2] = arg1;
3910 args[3] = arg2;
3911 args[4] = arg3;
3912 args[5] = arg4;
3913 args[6] = arg5;
3914 GCPRO1 (args[0]);
3915 gcpro1.nvars = 7;
3916 RETURN_UNGCPRO (Ffuncall (7, args));
3917 }
3918
3919 Lisp_Object
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,
3923 Lisp_Object arg6)
3924 {
3925 /* This function can GC */
3926 struct gcpro gcpro1;
3927 Lisp_Object args[8];
3928 args[0] = fn;
3929 args[1] = arg0;
3930 args[2] = arg1;
3931 args[3] = arg2;
3932 args[4] = arg3;
3933 args[5] = arg4;
3934 args[6] = arg5;
3935 args[7] = arg6;
3936 GCPRO1 (args[0]);
3937 gcpro1.nvars = 8;
3938 RETURN_UNGCPRO (Ffuncall (8, args));
3939 }
3940
3941 Lisp_Object
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)
3946 {
3947 /* This function can GC */
3948 struct gcpro gcpro1;
3949 Lisp_Object args[9];
3950 args[0] = fn;
3951 args[1] = arg0;
3952 args[2] = arg1;
3953 args[3] = arg2;
3954 args[4] = arg3;
3955 args[5] = arg4;
3956 args[6] = arg5;
3957 args[7] = arg6;
3958 args[8] = arg7;
3959 GCPRO1 (args[0]);
3960 gcpro1.nvars = 9;
3961 RETURN_UNGCPRO (Ffuncall (9, args));
3962 }
3963
3964 Lisp_Object
3965 call0_in_buffer (struct buffer *buf, Lisp_Object fn)
3966 {
3967 if (current_buffer == buf)
3968 return call0 (fn);
3969 else
3970 {
3971 Lisp_Object val;
3972 int speccount = specpdl_depth();
3973 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3974 set_buffer_internal (buf);
3975 val = call0 (fn);
3976 unbind_to (speccount, Qnil);
3977 return val;
3978 }
3979 }
3980
3981 Lisp_Object
3982 call1_in_buffer (struct buffer *buf, Lisp_Object fn,
3983 Lisp_Object arg0)
3984 {
3985 if (current_buffer == buf)
3986 return call1 (fn, arg0);
3987 else
3988 {
3989 Lisp_Object val;
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);
3995 return val;
3996 }
3997 }
3998
3999 Lisp_Object
4000 call2_in_buffer (struct buffer *buf, Lisp_Object fn,
4001 Lisp_Object arg0, Lisp_Object arg1)
4002 {
4003 if (current_buffer == buf)
4004 return call2 (fn, arg0, arg1);
4005 else
4006 {
4007 Lisp_Object val;
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);
4013 return val;
4014 }
4015 }
4016
4017 Lisp_Object
4018 call3_in_buffer (struct buffer *buf, Lisp_Object fn,
4019 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
4020 {
4021 if (current_buffer == buf)
4022 return call3 (fn, arg0, arg1, arg2);
4023 else
4024 {
4025 Lisp_Object val;
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);
4031 return val;
4032 }
4033 }
4034
4035 Lisp_Object
4036 call4_in_buffer (struct buffer *buf, Lisp_Object fn,
4037 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4038 Lisp_Object arg3)
4039 {
4040 if (current_buffer == buf)
4041 return call4 (fn, arg0, arg1, arg2, arg3);
4042 else
4043 {
4044 Lisp_Object val;
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);
4050 return val;
4051 }
4052 }
4053
4054 Lisp_Object
4055 eval_in_buffer (struct buffer *buf, Lisp_Object form)
4056 {
4057 if (current_buffer == buf)
4058 return Feval (form);
4059 else
4060 {
4061 Lisp_Object val;
4062 int speccount = specpdl_depth();
4063 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4064 set_buffer_internal (buf);
4065 val = Feval (form);
4066 unbind_to (speccount, Qnil);
4067 return val;
4068 }
4069 }
4070
4071
4072 /************************************************************************/
4073 /* Error-catching front-ends to eval, funcall, apply */
4074 /************************************************************************/
4075
4076 /* Call function fn on no arguments, with condition handler */
4077 Lisp_Object
4078 call0_with_handler (Lisp_Object handler, Lisp_Object fn)
4079 {
4080 /* This function can GC */
4081 struct gcpro gcpro1;
4082 Lisp_Object args[2];
4083 args[0] = handler;
4084 args[1] = fn;
4085 GCPRO1 (args[0]);
4086 gcpro1.nvars = 2;
4087 RETURN_UNGCPRO (Fcall_with_condition_handler (2, args));
4088 }
4089
4090 /* Call function fn with argument arg0, with condition handler */
4091 Lisp_Object
4092 call1_with_handler (Lisp_Object handler, Lisp_Object fn,
4093 Lisp_Object arg0)
4094 {
4095 /* This function can GC */
4096 struct gcpro gcpro1;
4097 Lisp_Object args[3];
4098 args[0] = handler;
4099 args[1] = fn;
4100 args[2] = arg0;
4101 GCPRO1 (args[0]);
4102 gcpro1.nvars = 3;
4103 RETURN_UNGCPRO (Fcall_with_condition_handler (3, args));
4104 }
4105
4106
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
4114 return value.
4115 */
4116
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(). */
4120
4121 static Lisp_Object
4122 caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4123 {
4124 if (!NILP (errordata))
4125 {
4126 Lisp_Object args[2];
4127
4128 if (!NILP (arg))
4129 {
4130 char *str = (char *) get_opaque_ptr (arg);
4131 args[0] = build_string (str);
4132 }
4133 else
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
4140 (Qerror, Qwarning,
4141 emacs_doprnt_string_lisp ((CONST Bufbyte *) "%s: %s",
4142 Qnil, -1, 2, args));
4143 }
4144 return Qunbound;
4145 }
4146
4147 static Lisp_Object
4148 allow_quit_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4149 {
4150 if (CONSP (errordata) && EQ (XCAR (errordata), Qquit))
4151 return Fsignal (Qquit, XCDR (errordata));
4152 return caught_a_squirmer (errordata, arg);
4153 }
4154
4155 static Lisp_Object
4156 safe_run_hook_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4157 {
4158 Lisp_Object hook = Fcar (arg);
4159 arg = Fcdr (arg);
4160 /* Clear out the hook. */
4161 Fset (hook, Qnil);
4162 return caught_a_squirmer (errordata, arg);
4163 }
4164
4165 static Lisp_Object
4166 allow_quit_safe_run_hook_caught_a_squirmer (Lisp_Object errordata,
4167 Lisp_Object arg)
4168 {
4169 Lisp_Object hook = Fcar (arg);
4170 arg = Fcdr (arg);
4171 if (!CONSP (errordata) || !EQ (XCAR (errordata), Qquit))
4172 /* Clear out the hook. */
4173 Fset (hook, Qnil);
4174 return allow_quit_caught_a_squirmer (errordata, arg);
4175 }
4176
4177 static Lisp_Object
4178 catch_them_squirmers_eval_in_buffer (Lisp_Object cons)
4179 {
4180 return eval_in_buffer (XBUFFER (XCAR (cons)), XCDR (cons));
4181 }
4182
4183 Lisp_Object
4184 eval_in_buffer_trapping_errors (CONST char *warning_string,
4185 struct buffer *buf, Lisp_Object form)
4186 {
4187 int speccount = specpdl_depth();
4188 Lisp_Object tem;
4189 Lisp_Object buffer;
4190 Lisp_Object cons;
4191 Lisp_Object opaque;
4192 struct gcpro gcpro1, gcpro2;
4193
4194 XSETBUFFER (buffer, buf);
4195
4196 specbind (Qinhibit_quit, Qt);
4197 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4198
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);
4209 UNGCPRO;
4210
4211 /* gc_currently_forbidden = 0; */
4212 return unbind_to (speccount, tem);
4213 }
4214
4215 static Lisp_Object
4216 catch_them_squirmers_run_hook (Lisp_Object hook_symbol)
4217 {
4218 /* This function can GC */
4219 run_hook (hook_symbol);
4220 return Qnil;
4221 }
4222
4223 Lisp_Object
4224 run_hook_trapping_errors (CONST char *warning_string, Lisp_Object hook_symbol)
4225 {
4226 int speccount;
4227 Lisp_Object tem;
4228 Lisp_Object opaque;
4229 struct gcpro gcpro1;
4230
4231 if (!initialized || preparing_for_armageddon)
4232 return Qnil;
4233 tem = find_symbol_value (hook_symbol);
4234 if (NILP (tem) || UNBOUNDP (tem))
4235 return Qnil;
4236
4237 speccount = specpdl_depth();
4238 specbind (Qinhibit_quit, Qt);
4239
4240 opaque = (warning_string ? make_opaque_ptr ((void *)warning_string) : Qnil);
4241 GCPRO1 (opaque);
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);
4248 UNGCPRO;
4249
4250 return unbind_to (speccount, tem);
4251 }
4252
4253 /* Same as run_hook_trapping_errors() but also set the hook to nil
4254 if an error occurs. */
4255
4256 Lisp_Object
4257 safe_run_hook_trapping_errors (CONST char *warning_string,
4258 Lisp_Object hook_symbol,
4259 int allow_quit)
4260 {
4261 int speccount = specpdl_depth();
4262 Lisp_Object tem;
4263 Lisp_Object cons = Qnil;
4264 struct gcpro gcpro1;
4265
4266 if (!initialized || preparing_for_armageddon)
4267 return Qnil;
4268 tem = find_symbol_value (hook_symbol);
4269 if (NILP (tem) || UNBOUNDP (tem))
4270 return Qnil;
4271
4272 if (!allow_quit)
4273 specbind (Qinhibit_quit, Qt);
4274
4275 cons = noseeum_cons (hook_symbol,
4276 warning_string ? make_opaque_ptr ((void *)warning_string)
4277 : Qnil);
4278 GCPRO1 (cons);
4279 /* Qerror not Qt, so you can get a backtrace */
4280 tem = condition_case_1 (Qerror,
4281 catch_them_squirmers_run_hook,
4282 hook_symbol,
4283 allow_quit ?
4284 allow_quit_safe_run_hook_caught_a_squirmer :
4285 safe_run_hook_caught_a_squirmer,
4286 cons);
4287 if (OPAQUE_PTRP (XCDR (cons)))
4288 free_opaque_ptr (XCDR (cons));
4289 free_cons (XCONS (cons));
4290 UNGCPRO;
4291
4292 return unbind_to (speccount, tem);
4293 }
4294
4295 static Lisp_Object
4296 catch_them_squirmers_call0 (Lisp_Object function)
4297 {
4298 /* This function can GC */
4299 return call0 (function);
4300 }
4301
4302 Lisp_Object
4303 call0_trapping_errors (CONST char *warning_string, Lisp_Object function)
4304 {
4305 int speccount;
4306 Lisp_Object tem;
4307 Lisp_Object opaque = Qnil;
4308 struct gcpro gcpro1, gcpro2;
4309
4310 if (SYMBOLP (function))
4311 {
4312 tem = XSYMBOL (function)->function;
4313 if (NILP (tem) || UNBOUNDP (tem))
4314 return Qnil;
4315 }
4316
4317 GCPRO2 (opaque, function);
4318 speccount = specpdl_depth();
4319 specbind (Qinhibit_quit, Qt);
4320 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4321
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);
4329 UNGCPRO;
4330
4331 /* gc_currently_forbidden = 0; */
4332 return unbind_to (speccount, tem);
4333 }
4334
4335 static Lisp_Object
4336 catch_them_squirmers_call1 (Lisp_Object cons)
4337 {
4338 /* This function can GC */
4339 return call1 (XCAR (cons), XCDR (cons));
4340 }
4341
4342 static Lisp_Object
4343 catch_them_squirmers_call2 (Lisp_Object cons)
4344 {
4345 /* This function can GC */
4346 return call2 (XCAR (cons), XCAR (XCDR (cons)), XCAR (XCDR (XCDR (cons))));
4347 }
4348
4349 Lisp_Object
4350 call1_trapping_errors (CONST char *warning_string, Lisp_Object function,
4351 Lisp_Object object)
4352 {
4353 int speccount = specpdl_depth();
4354 Lisp_Object tem;
4355 Lisp_Object cons = Qnil;
4356 Lisp_Object opaque = Qnil;
4357 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4358
4359 if (SYMBOLP (function))
4360 {
4361 tem = XSYMBOL (function)->function;
4362 if (NILP (tem) || UNBOUNDP (tem))
4363 return Qnil;
4364 }
4365
4366 GCPRO4 (cons, opaque, function, object);
4367
4368 specbind (Qinhibit_quit, Qt);
4369 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4370
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));
4380 UNGCPRO;
4381
4382 /* gc_currently_forbidden = 0; */
4383 return unbind_to (speccount, tem);
4384 }
4385
4386 Lisp_Object
4387 call2_trapping_errors (CONST char *warning_string, Lisp_Object function,
4388 Lisp_Object object1, Lisp_Object object2)
4389 {
4390 int speccount = specpdl_depth();
4391 Lisp_Object tem;
4392 Lisp_Object cons = Qnil;
4393 Lisp_Object opaque = Qnil;
4394 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
4395
4396 if (SYMBOLP (function))
4397 {
4398 tem = XSYMBOL (function)->function;
4399 if (NILP (tem) || UNBOUNDP (tem))
4400 return Qnil;
4401 }
4402
4403 GCPRO5 (cons, opaque, function, object1, object2);
4404 specbind (Qinhibit_quit, Qt);
4405 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4406
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);
4415 free_list (cons);
4416 UNGCPRO;
4417
4418 /* gc_currently_forbidden = 0; */
4419 return unbind_to (speccount, tem);
4420 }
4421
4422
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 /************************************************************************/
4428
4429 #define min_max_specpdl_size 400
4430
4431 void
4432 grow_specpdl (size_t reserved)
4433 {
4434 size_t size_needed = specpdl_depth() + reserved;
4435 if (size_needed >= max_specpdl_size)
4436 {
4437 if (max_specpdl_size < min_max_specpdl_size)
4438 max_specpdl_size = min_max_specpdl_size;
4439 if (size_needed >= max_specpdl_size)
4440 {
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;
4445 continuable_error
4446 ("Variable binding depth exceeds max-specpdl-size");
4447 }
4448 }
4449 while (specpdl_size < size_needed)
4450 {
4451 specpdl_size *= 2;
4452 if (specpdl_size > max_specpdl_size)
4453 specpdl_size = max_specpdl_size;
4454 }
4455 XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size);
4456 specpdl_ptr = specpdl + specpdl_depth();
4457 }
4458
4459
4460 /* Handle unbinding buffer-local variables */
4461 static Lisp_Object
4462 specbind_unwind_local (Lisp_Object ovalue)
4463 {
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;
4469
4470 free_cons (victim);
4471
4472 if (NILP (buf))
4473 {
4474 /* Deleted buffer -- do nothing */
4475 }
4476 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0)
4477 {
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.
4481 */
4482 }
4483 else if (EQ (buf, current))
4484 Fset (symbol, ovalue);
4485 else
4486 {
4487 /* Urk! Somebody switched buffers */
4488 struct gcpro gcpro1;
4489 GCPRO1 (current);
4490 Fset_buffer (buf);
4491 Fset (symbol, ovalue);
4492 Fset_buffer (current);
4493 UNGCPRO;
4494 }
4495 return symbol;
4496 }
4497
4498 static Lisp_Object
4499 specbind_unwind_wasnt_local (Lisp_Object buffer)
4500 {
4501 Lisp_Object current = Fcurrent_buffer ();
4502 Lisp_Object symbol = specpdl_ptr->symbol;
4503
4504 buffer = get_buffer (buffer, 0);
4505 if (NILP (buffer))
4506 {
4507 /* Deleted buffer -- do nothing */
4508 }
4509 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0)
4510 {
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.
4514 */
4515 }
4516 else if (EQ (buffer, current))
4517 Fkill_local_variable (symbol);
4518 else
4519 {
4520 /* Urk! Somebody switched buffers */
4521 struct gcpro gcpro1;
4522 GCPRO1 (current);
4523 Fset_buffer (buffer);
4524 Fkill_local_variable (symbol);
4525 Fset_buffer (current);
4526 UNGCPRO;
4527 }
4528 return symbol;
4529 }
4530
4531
4532 void
4533 specbind (Lisp_Object symbol, Lisp_Object value)
4534 {
4535 SPECBIND (symbol, value);
4536 }
4537
4538 void
4539 specbind_magic (Lisp_Object symbol, Lisp_Object value)
4540 {
4541 int buffer_local =
4542 symbol_value_buffer_local_info (symbol, current_buffer);
4543
4544 if (buffer_local == 0)
4545 {
4546 specpdl_ptr->old_value = find_symbol_value (symbol);
4547 specpdl_ptr->func = 0; /* Handled specially by unbind_to */
4548 }
4549 else if (buffer_local > 0)
4550 {
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;
4555 }
4556 else
4557 {
4558 /* About to become buffer-local */
4559 specpdl_ptr->old_value = Fcurrent_buffer ();
4560 specpdl_ptr->func = specbind_unwind_wasnt_local;
4561 }
4562
4563 specpdl_ptr->symbol = symbol;
4564 specpdl_ptr++;
4565 specpdl_depth_counter++;
4566
4567 Fset (symbol, value);
4568 }
4569
4570 void
4571 record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg),
4572 Lisp_Object arg)
4573 {
4574 SPECPDL_RESERVE (1);
4575 specpdl_ptr->func = function;
4576 specpdl_ptr->symbol = Qnil;
4577 specpdl_ptr->old_value = arg;
4578 specpdl_ptr++;
4579 specpdl_depth_counter++;
4580 }
4581
4582 extern int check_sigio (void);
4583
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. */
4587 Lisp_Object
4588 unbind_to (int count, Lisp_Object value)
4589 {
4590 UNBIND_TO_GCPRO (count, value);
4591 return value;
4592 }
4593
4594 /* Don't call this directly.
4595 Only for use by UNBIND_TO* macros in backtrace.h */
4596 void
4597 unbind_to_hairy (int count)
4598 {
4599 int quitf;
4600
4601 check_quit (); /* make Vquit_flag accurate */
4602 quitf = !NILP (Vquit_flag);
4603 Vquit_flag = Qnil;
4604
4605 ++specpdl_ptr;
4606 ++specpdl_depth_counter;
4607
4608 while (specpdl_depth_counter != count)
4609 {
4610 --specpdl_ptr;
4611 --specpdl_depth_counter;
4612
4613 if (specpdl_ptr->func != 0)
4614 /* An unwind-protect */
4615 (*specpdl_ptr->func) (specpdl_ptr->old_value);
4616 else
4617 {
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;
4623 else
4624 Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
4625 }
4626
4627 #if 0 /* martin */
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 */
4634
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)
4640 {
4641 catchlist = catchlist->next;
4642 /* Don't mess with gcprolist, backtrace_list here */
4643 }
4644 #endif
4645 #endif
4646 }
4647 if (quitf)
4648 Vquit_flag = Qt;
4649 }
4650
4651
4652
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. */
4655
4656 Lisp_Object
4657 top_level_value (Lisp_Object symbol)
4658 {
4659 REGISTER struct specbinding *ptr = specpdl;
4660
4661 CHECK_SYMBOL (symbol);
4662 for (; ptr != specpdl_ptr; ptr++)
4663 {
4664 if (EQ (ptr->symbol, symbol))
4665 return ptr->old_value;
4666 }
4667 return XSYMBOL (symbol)->value;
4668 }
4669
4670 #if 0
4671
4672 Lisp_Object
4673 top_level_set (Lisp_Object symbol, Lisp_Object newval)
4674 {
4675 REGISTER struct specbinding *ptr = specpdl;
4676
4677 CHECK_SYMBOL (symbol);
4678 for (; ptr != specpdl_ptr; ptr++)
4679 {
4680 if (EQ (ptr->symbol, symbol))
4681 {
4682 ptr->old_value = newval;
4683 return newval;
4684 }
4685 }
4686 return Fset (symbol, newval);
4687 }
4688
4689 #endif /* 0 */
4690
4691
4692 /************************************************************************/
4693 /* Backtraces */
4694 /************************************************************************/
4695
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.
4699 */
4700 (level, flag))
4701 {
4702 REGISTER struct backtrace *backlist = backtrace_list;
4703 REGISTER int i;
4704
4705 CHECK_INT (level);
4706
4707 for (i = 0; backlist && i < XINT (level); i++)
4708 {
4709 backlist = backlist->next;
4710 }
4711
4712 if (backlist)
4713 backlist->debug_on_exit = !NILP (flag);
4714
4715 return flag;
4716 }
4717
4718 static void
4719 backtrace_specials (int speccount, int speclimit, Lisp_Object stream)
4720 {
4721 int printing_bindings = 0;
4722
4723 for (; speccount > speclimit; speccount--)
4724 {
4725 if (specpdl[speccount - 1].func == 0
4726 || specpdl[speccount - 1].func == specbind_unwind_local
4727 || specpdl[speccount - 1].func == specbind_unwind_wasnt_local)
4728 {
4729 write_c_string (((!printing_bindings) ? " # bind (" : " "),
4730 stream);
4731 Fprin1 (specpdl[speccount - 1].symbol, stream);
4732 printing_bindings = 1;
4733 }
4734 else
4735 {
4736 if (printing_bindings) write_c_string (")\n", stream);
4737 write_c_string (" # (unwind-protect ...)\n", stream);
4738 printing_bindings = 0;
4739 }
4740 }
4741 if (printing_bindings) write_c_string (")\n", stream);
4742 }
4743
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
4750 function calls.
4751 */
4752 (stream, detailed))
4753 {
4754 /* This function can GC */
4755 struct backtrace *backlist = backtrace_list;
4756 struct catchtag *catches = catchlist;
4757 int speccount = specpdl_depth();
4758
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;
4764
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. */
4769 Vinhibit_quit = Qt;
4770
4771 entering_debugger = 0;
4772
4773 Vprint_level = make_int (3);
4774 print_readably = 0;
4775 print_escape_newlines = 1;
4776
4777 GCPRO2 (stream, old_level);
4778
4779 if (NILP (stream))
4780 stream = Vstandard_output;
4781 if (!noninteractive && (NILP (stream) || EQ (stream, Qt)))
4782 stream = Fselected_frame (Qnil);
4783
4784 for (;;)
4785 {
4786 if (!NILP (detailed) && catches && catches->backlist == backlist)
4787 {
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;
4793
4794 backtrace_specials (speccount, catchpdl, stream);
4795
4796 speccount = catches->pdlcount;
4797 if (catchpdl == speccount)
4798 {
4799 write_c_string (" # (catch ", stream);
4800 Fprin1 (catches->tag, stream);
4801 write_c_string (" ...)\n", stream);
4802 }
4803 else
4804 {
4805 write_c_string (" # (condition-case ... . ", stream);
4806 Fprin1 (Fcdr (Fcar (catches->tag)), stream);
4807 write_c_string (")\n", stream);
4808 }
4809 catches = catches->next;
4810 }
4811 else if (!backlist)
4812 break;
4813 else
4814 {
4815 if (!NILP (detailed) && backlist->pdlcount < speccount)
4816 {
4817 backtrace_specials (speccount, backlist->pdlcount, stream);
4818 speccount = backlist->pdlcount;
4819 }
4820 write_c_string (((backlist->debug_on_exit) ? "* " : " "),
4821 stream);
4822 if (backlist->nargs == UNEVALLED)
4823 {
4824 Fprin1 (Fcons (*backlist->function, *backlist->args), stream);
4825 write_c_string ("\n", stream); /* from FSFmacs 19.30 */
4826 }
4827 else
4828 {
4829 Lisp_Object tem = *backlist->function;
4830 Fprin1 (tem, stream); /* This can QUIT */
4831 write_c_string ("(", stream);
4832 if (backlist->nargs == MANY)
4833 {
4834 int i;
4835 Lisp_Object tail = Qnil;
4836 struct gcpro ngcpro1;
4837
4838 NGCPRO1 (tail);
4839 for (tail = *backlist->args, i = 0;
4840 !NILP (tail);
4841 tail = Fcdr (tail), i++)
4842 {
4843 if (i != 0) write_c_string (" ", stream);
4844 Fprin1 (Fcar (tail), stream);
4845 }
4846 NUNGCPRO;
4847 }
4848 else
4849 {
4850 int i;
4851 for (i = 0; i < backlist->nargs; i++)
4852 {
4853 if (!i && EQ(tem, Qbyte_code)) {
4854 write_c_string("\"...\"", stream);
4855 continue;
4856 }
4857 if (i != 0) write_c_string (" ", stream);
4858 Fprin1 (backlist->args[i], stream);
4859 }
4860 }
4861 }
4862 write_c_string (")\n", stream);
4863 backlist = backlist->next;
4864 }
4865 }
4866 Vprint_level = old_level;
4867 print_readably = old_pr;
4868 print_escape_newlines = old_nl;
4869 UNGCPRO;
4870 Vinhibit_quit = oiq;
4871 return Qnil;
4872 }
4873
4874
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.
4885 */
4886 (nframes))
4887 {
4888 REGISTER struct backtrace *backlist = backtrace_list;
4889 REGISTER int i;
4890 Lisp_Object tem;
4891
4892 CHECK_NATNUM (nframes);
4893
4894 /* Find the frame requested. */
4895 for (i = XINT (nframes); backlist && (i-- > 0);)
4896 backlist = backlist->next;
4897
4898 if (!backlist)
4899 return Qnil;
4900 if (backlist->nargs == UNEVALLED)
4901 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
4902 else
4903 {
4904 if (backlist->nargs == MANY)
4905 tem = *backlist->args;
4906 else
4907 tem = Flist (backlist->nargs, backlist->args);
4908
4909 return Fcons (Qt, Fcons (*backlist->function, tem));
4910 }
4911 }
4912
4913
4914 /************************************************************************/
4915 /* Warnings */
4916 /************************************************************************/
4917
4918 void
4919 warn_when_safe_lispobj (Lisp_Object class, Lisp_Object level,
4920 Lisp_Object obj)
4921 {
4922 obj = list1 (list3 (class, level, obj));
4923 if (NILP (Vpending_warnings))
4924 Vpending_warnings = Vpending_warnings_tail = obj;
4925 else
4926 {
4927 Fsetcdr (Vpending_warnings_tail, obj);
4928 Vpending_warnings_tail = obj;
4929 }
4930 }
4931
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.
4934
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. */
4938
4939 void
4940 warn_when_safe (Lisp_Object class, Lisp_Object level, CONST char *fmt, ...)
4941 {
4942 Lisp_Object obj;
4943 va_list args;
4944
4945 va_start (args, fmt);
4946 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt),
4947 Qnil, -1, args);
4948 va_end (args);
4949
4950 warn_when_safe_lispobj (class, level, obj);
4951 }
4952
4953
4954
4955
4956 /************************************************************************/
4957 /* Initialization */
4958 /************************************************************************/
4959
4960 void
4961 syms_of_eval (void)
4962 {
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");
4984
4985 DEFSUBR (For);
4986 DEFSUBR (Fand);
4987 DEFSUBR (Fif);
4988 DEFSUBR_MACRO (Fwhen);
4989 DEFSUBR_MACRO (Funless);
4990 DEFSUBR (Fcond);
4991 DEFSUBR (Fprogn);
4992 DEFSUBR (Fprog1);
4993 DEFSUBR (Fprog2);
4994 DEFSUBR (Fsetq);
4995 DEFSUBR (Fquote);
4996 DEFSUBR (Ffunction);
4997 DEFSUBR (Fdefun);
4998 DEFSUBR (Fdefmacro);
4999 DEFSUBR (Fdefvar);
5000 DEFSUBR (Fdefconst);
5001 DEFSUBR (Fuser_variable_p);
5002 DEFSUBR (Flet);
5003 DEFSUBR (FletX);
5004 DEFSUBR (Fwhile);
5005 DEFSUBR (Fmacroexpand_internal);
5006 DEFSUBR (Fcatch);
5007 DEFSUBR (Fthrow);
5008 DEFSUBR (Funwind_protect);
5009 DEFSUBR (Fcondition_case);
5010 DEFSUBR (Fcall_with_condition_handler);
5011 DEFSUBR (Fsignal);
5012 DEFSUBR (Finteractive_p);
5013 DEFSUBR (Fcommandp);
5014 DEFSUBR (Fcommand_execute);
5015 DEFSUBR (Fautoload);
5016 DEFSUBR (Feval);
5017 DEFSUBR (Fapply);
5018 DEFSUBR (Ffuncall);
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);
5029 }
5030
5031 void
5032 reinit_eval (void)
5033 {
5034 specpdl_ptr = specpdl;
5035 specpdl_depth_counter = 0;
5036 catchlist = 0;
5037 Vcondition_handlers = Qnil;
5038 backtrace_list = 0;
5039 Vquit_flag = Qnil;
5040 debug_on_next_call = 0;
5041 lisp_eval_depth = 0;
5042 entering_debugger = 0;
5043 }
5044
5045 void
5046 reinit_vars_of_eval (void)
5047 {
5048 preparing_for_armageddon = 0;
5049 in_warnings = 0;
5050 Qunbound_suspended_errors_tag = make_opaque_ptr (&Qunbound_suspended_errors_tag);
5051 staticpro_nodump (&Qunbound_suspended_errors_tag);
5052
5053 specpdl_size = 50;
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 */
5059 throw_level = 0;
5060 #endif
5061 }
5062
5063 void
5064 vars_of_eval (void)
5065 {
5066 reinit_vars_of_eval ();
5067
5068 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size /*
5069 Limit on number of Lisp variable bindings & unwind-protects before error.
5070 */ );
5071
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.
5078 */ );
5079
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'.
5083 */ );
5084 Vquit_flag = Qnil;
5085
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
5093 an X frame.
5094 */ );
5095 Vinhibit_quit = Qnil;
5096
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'.
5103 */ );
5104 Vstack_trace_on_error = Qnil;
5105
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
5109 a `condition-case'.
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'.
5113 */ );
5114 Vstack_trace_on_signal = Qnil;
5115
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'.
5123 */ );
5124 Vdebug_ignored_errors = Qnil;
5125
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
5129 a `condition-case'.
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'.
5134 */ );
5135 Vdebug_on_error = Qnil;
5136
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
5140 a `condition-case'.
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'.
5144 */ );
5145 Vdebug_on_signal = Qnil;
5146
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.
5152 */ );
5153 debug_on_quit = 0;
5154
5155 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call /*
5156 Non-nil means enter debugger before next `eval', `apply' or `funcall'.
5157 */ );
5158
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.
5166 */ );
5167 Vdebugger = Qnil;
5168
5169 staticpro (&Vpending_warnings);
5170 Vpending_warnings = Qnil;
5171 pdump_wire (&Vpending_warnings_tail);
5172 Vpending_warnings_tail = Qnil;
5173
5174 staticpro (&Vautoload_queue);
5175 Vautoload_queue = Qnil;
5176
5177 staticpro (&Vcondition_handlers);
5178
5179 staticpro (&Vcurrent_warning_class);
5180 Vcurrent_warning_class = Qnil;
5181
5182 staticpro (&Vcurrent_error_state);
5183 Vcurrent_error_state = Qnil; /* errors as normal */
5184
5185 reinit_eval ();
5186 }