comparison src/eval.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
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 /* Debugging hack */
25 int always_gc;
26
27
28 #include <config.h>
29 #include "lisp.h"
30
31 #ifndef standalone
32 #include "commands.h"
33 #endif
34
35 #include "symeval.h"
36 #include "backtrace.h"
37 #include "bytecode.h"
38 #include "buffer.h"
39 #include "console.h"
40 #include "opaque.h"
41
42 struct backtrace *backtrace_list;
43
44 /* This is the list of current catches (and also condition-cases).
45 This is a stack: the most recent catch is at the head of the
46 list. Catches are created by declaring a 'struct catchtag'
47 locally, filling the .TAG field in with the tag, and doing
48 a setjmp() on .JMP. Fthrow() will store the value passed
49 to it in .VAL and longjmp() back to .JMP, back to the function
50 that established the catch. This will always be either
51 internal_catch() (catches established internally or through
52 `catch') or condition_case_1 (condition-cases established
53 internally or through `condition-case').
54
55 The catchtag also records the current position in the
56 call stack (stored in BACKTRACE_LIST), the current position
57 in the specpdl stack (used for variable bindings and
58 unwind-protects), the value of LISP_EVAL_DEPTH, and the
59 current position in the GCPRO stack. All of these are
60 restored by Fthrow().
61 */
62
63 struct catchtag *catchlist;
64
65 Lisp_Object Qautoload, Qmacro, Qexit;
66 Lisp_Object Qinteractive, Qcommandp, Qdefun, Qprogn, Qvalues;
67 Lisp_Object Vquit_flag, Vinhibit_quit;
68 Lisp_Object Qand_rest, Qand_optional;
69 Lisp_Object Qdebug_on_error;
70 Lisp_Object Qstack_trace_on_error;
71 Lisp_Object Qdebug_on_signal;
72 Lisp_Object Qstack_trace_on_signal;
73 Lisp_Object Qdebugger;
74 Lisp_Object Qinhibit_quit;
75 Lisp_Object Qrun_hooks;
76
77 Lisp_Object Qsetq;
78
79 Lisp_Object Qdisplay_warning;
80 Lisp_Object Vpending_warnings, Vpending_warnings_tail;
81
82 /* Records whether we want errors to occur. This will be a boolean,
83 nil (errors OK) or t (no errors). If t, an error will cause a
84 throw to Qunbound_suspended_errors_tag.
85
86 See call_with_suspended_errors(). */
87 Lisp_Object Vcurrent_error_state;
88
89 /* Current warning class when warnings occur, or nil for no warnings.
90 Only meaningful when Vcurrent_error_state is non-nil.
91 See call_with_suspended_errors(). */
92 Lisp_Object Vcurrent_warning_class;
93
94 /* Special catch tag used in call_with_suspended_errors(). */
95 Lisp_Object Qunbound_suspended_errors_tag;
96
97 /* Non-nil means we're going down, so we better not run any hooks
98 or do other non-essential stuff. */
99 int preparing_for_armageddon;
100
101 /* Non-nil means record all fset's and provide's, to be undone
102 if the file being autoloaded is not fully loaded.
103 They are recorded by being consed onto the front of Vautoload_queue:
104 (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide. */
105
106 Lisp_Object Vautoload_queue;
107
108 /* Current number of specbindings allocated in specpdl. */
109 static int specpdl_size;
110
111 /* Pointer to beginning of specpdl. */
112 struct specbinding *specpdl;
113
114 /* Pointer to first unused element in specpdl. */
115 struct specbinding *specpdl_ptr;
116
117 /* specpdl_ptr - specpdl. Callers outside this this file should use
118 * specpdl_depth () function-call */
119 static int specpdl_depth_counter;
120
121 /* Maximum size allowed for specpdl allocation */
122 int max_specpdl_size;
123
124 /* Depth in Lisp evaluations and function calls. */
125 int lisp_eval_depth;
126
127 /* Maximum allowed depth in Lisp evaluations and function calls. */
128 int max_lisp_eval_depth;
129
130 /* Nonzero means enter debugger before next function call */
131 static int debug_on_next_call;
132
133 /* List of conditions (non-nil atom means all) which cause a backtrace
134 if an error is handled by the command loop's error handler. */
135 Lisp_Object Vstack_trace_on_error;
136
137 /* List of conditions (non-nil atom means all) which enter the debugger
138 if an error is handled by the command loop's error handler. */
139 Lisp_Object Vdebug_on_error;
140
141 /* List of conditions (non-nil atom means all) which cause a backtrace
142 if any error is signalled. */
143 Lisp_Object Vstack_trace_on_signal;
144
145 /* List of conditions (non-nil atom means all) which enter the debugger
146 if any error is signalled. */
147 Lisp_Object Vdebug_on_signal;
148
149 /* Nonzero means enter debugger if a quit signal
150 is handled by the command loop's error handler.
151
152 From lisp, this is a boolean variable and may have the values 0 and 1.
153 But, eval.c temporarily uses the second bit of this variable to indicate
154 that a critical_quit is in progress. The second bit is reset immediately
155 after it is processed in signal_call_debugger(). */
156 int debug_on_quit;
157
158 #if 0 /* FSFmacs */
159 /* entering_debugger is basically equivalent */
160 /* The value of num_nonmacro_input_chars as of the last time we
161 started to enter the debugger. If we decide to enter the debugger
162 again when this is still equal to num_nonmacro_input_chars, then we
163 know that the debugger itself has an error, and we should just
164 signal the error instead of entering an infinite loop of debugger
165 invocations. */
166 int when_entered_debugger;
167 #endif
168
169 /* Nonzero means we are trying to enter the debugger.
170 This is to prevent recursive attempts.
171 Cleared by the debugger calling Fbacktrace */
172 static int entering_debugger;
173
174 /* Function to call to invoke the debugger */
175 Lisp_Object Vdebugger;
176
177 /* Chain of condition handlers currently in effect.
178 The elements of this chain are contained in the stack frames
179 of Fcondition_case and internal_condition_case.
180 When an error is signaled (by calling Fsignal, below),
181 this chain is searched for an element that applies.
182
183 Each element of this list is one of the following:
184
185 A list of a handler function and possibly args to pass to
186 the function. This is a handler established with
187 `call-with-condition-handler' (q.v.).
188
189 A list whose car is Qunbound and whose cdr is Qt.
190 This is a special condition-case handler established
191 by C code with condition_case_1(). All errors are
192 trapped; the debugger is not invoked even if
193 `debug-on-error' was set.
194
195 A list whose car is Qunbound and whose cdr is Qerror.
196 This is a special condition-case handler established
197 by C code with condition_case_1(). It is like Qt
198 except that the debugger is invoked normally if it is
199 called for.
200
201 A list whose car is Qunbound and whose cdr is a list
202 of lists (CONDITION-NAME BODY ...) exactly as in
203 `condition-case'. This is a normal `condition-case'
204 handler.
205
206 Note that in all cases *except* the first, there is a
207 corresponding catch, whose TAG is the value of
208 Vcondition_handlers just after the handler data just
209 described is pushed onto it. The reason is that
210 `condition-case' handlers need to throw back to the
211 place where the handler was installed before invoking
212 it, while `call-with-condition-handler' handlers are
213 invoked in the environment that `signal' was invoked
214 in.
215 */
216 static Lisp_Object Vcondition_handlers;
217
218 /* Used for error catching purposes by throw_or_bomb_out */
219 static int throw_level;
220
221 static Lisp_Object primitive_funcall (Lisp_Object (*fn) (), int nargs,
222 Lisp_Object args[]);
223
224
225 /**********************************************************************/
226 /* The subr and compiled-function types */
227 /**********************************************************************/
228
229 static void print_subr (Lisp_Object, Lisp_Object, int);
230 DEFINE_LRECORD_IMPLEMENTATION ("subr", subr,
231 this_one_is_unmarkable, print_subr, 0, 0, 0,
232 struct Lisp_Subr);
233
234 static void
235 print_subr (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
236 {
237 struct Lisp_Subr *subr = XSUBR (obj);
238
239 if (print_readably)
240 error ("printing unreadable object #<subr %s>",
241 subr_name (subr));
242
243 write_c_string (((subr->max_args == UNEVALLED)
244 ? "#<special-form "
245 : "#<subr "),
246 printcharfun);
247
248 write_c_string (subr_name (subr), printcharfun);
249 write_c_string (((subr->prompt) ? " (interactive)>" : ">"),
250 printcharfun);
251 }
252
253
254 static Lisp_Object mark_compiled_function (Lisp_Object,
255 void (*) (Lisp_Object));
256 extern void print_compiled_function (Lisp_Object, Lisp_Object, int);
257 static int compiled_function_equal (Lisp_Object, Lisp_Object, int);
258 static unsigned long compiled_function_hash (Lisp_Object obj, int depth);
259 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("compiled-function", compiled_function,
260 mark_compiled_function,
261 print_compiled_function, 0,
262 compiled_function_equal,
263 compiled_function_hash,
264 struct Lisp_Compiled_Function);
265
266 static Lisp_Object
267 mark_compiled_function (Lisp_Object obj, void (*markobj) (Lisp_Object))
268 {
269 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (obj);
270
271 ((markobj) (b->bytecodes));
272 ((markobj) (b->arglist));
273 ((markobj) (b->doc_and_interactive));
274 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
275 ((markobj) (b->annotated));
276 #endif
277 /* tail-recurse on constants */
278 return (b->constants);
279 }
280
281 static int
282 compiled_function_equal (Lisp_Object o1, Lisp_Object o2, int depth)
283 {
284 struct Lisp_Compiled_Function *b1 = XCOMPILED_FUNCTION (o1);
285 struct Lisp_Compiled_Function *b2 = XCOMPILED_FUNCTION (o2);
286 return (b1->flags.documentationp == b2->flags.documentationp
287 && b1->flags.interactivep == b2->flags.interactivep
288 && b1->flags.domainp == b2->flags.domainp /* I18N3 */
289 && internal_equal (b1->bytecodes, b2->bytecodes, depth + 1)
290 && internal_equal (b1->constants, b2->constants, depth + 1)
291 && internal_equal (b1->arglist, b2->arglist, depth + 1)
292 && internal_equal (b1->doc_and_interactive,
293 b2->doc_and_interactive, depth + 1));
294 }
295
296 static unsigned long
297 compiled_function_hash (Lisp_Object obj, int depth)
298 {
299 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (obj);
300 return HASH3 ((b->flags.documentationp << 2) +
301 (b->flags.interactivep << 1) +
302 b->flags.domainp,
303 internal_hash (b->bytecodes, depth + 1),
304 internal_hash (b->constants, depth + 1));
305 }
306
307
308 /**********************************************************************/
309 /* Entering the debugger */
310 /**********************************************************************/
311
312 /* unwind-protect used by call_debugger() to restore the value of
313 enterring_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)) ? 0 : 1);
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 enterring_debugger
342 to 1 during this call. This is used to trap errors that may occur
343 when enterring 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.) enterring_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_counter;
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 int old_debug_on_next_call = debug_on_next_call;
407 Lisp_Object v = call_debugger (list2 (Qexit, val));
408 debug_on_next_call = old_debug_on_next_call;
409 return ((!UNBOUNDP (v)) ? v : val);
410 }
411
412 /* Called when debug-on-call behavior is called for. Enter the debugger
413 with the appropriate args for this. VAL is either t for a call
414 through `eval' or 'lambda for a call through `funcall'.
415
416 #### The differentiation here between EVAL and FUNCALL is bogus.
417 FUNCALL can be defined as
418
419 (defmacro func (fun &rest args)
420 (cons (eval fun) args))
421
422 and should be treated as such.
423 */
424
425 static void
426 do_debug_on_call (Lisp_Object code)
427 {
428 debug_on_next_call = 0;
429 backtrace_list->debug_on_exit = 1;
430 call_debugger (list1 (code));
431 }
432
433 /* LIST is the value of one of the variables `debug-on-error',
434 `debug-on-signal', `stack-trace-on-error', or `stack-trace-on-signal',
435 and CONDITIONS is the list of error conditions associated with
436 the error being signalled. This returns non-nil if LIST
437 matches CONDITIONS. (A nil value for LIST does not match
438 CONDITIONS. A non-list value for LIST does match CONDITIONS.
439 A list matches CONDITIONS when one of the symbols in LIST is the
440 same as one of the symbols in CONDITIONS.) */
441
442 static int
443 wants_debugger (Lisp_Object list, Lisp_Object conditions)
444 {
445 if (NILP (list))
446 return 0;
447 if (! CONSP (list))
448 return 1;
449
450 while (CONSP (conditions))
451 {
452 Lisp_Object this, tail;
453 this = XCAR (conditions);
454 for (tail = list; CONSP (tail); tail = XCDR (tail))
455 if (EQ (XCAR (tail), this))
456 return 1;
457 conditions = XCDR (conditions);
458 }
459 return 0;
460 }
461
462 /* Actually generate a backtrace on STREAM. */
463
464 static Lisp_Object
465 backtrace_259 (Lisp_Object stream)
466 {
467 return (Fbacktrace (stream, Qt));
468 }
469
470 /* An error was signalled. Maybe call the debugger, if the `debug-on-error'
471 etc. variables call for this. CONDITIONS is the list of conditions
472 associated with the error being signalled. SIG is the actual error
473 being signalled, and DATA is the associated data (these are exactly
474 the same as the arguments to `signal'). ACTIVE_HANDLERS is the
475 list of error handlers that are to be put in place while the debugger
476 is called. This is generally the remaining handlers that are
477 outside of the innermost handler trapping this error. This way,
478 if the same error occurs inside of the debugger, you usually don't get
479 the debugger entered recursively.
480
481 This function returns Qunbound if it didn't call the debugger or if
482 the user asked (through 'c') that XEmacs should pretend like the
483 debugger was never entered. Otherwise, it returns the value
484 that the user specified with `r'. (Note that much of the time,
485 the user will abort with C-], and we will never have a chance to
486 return anything at all.)
487
488 SIGNAL_VARS_ONLY means we should only look at debug-on-signal
489 and stack-trace-on-signal to control whether we do anything.
490 This is so that debug-on-error doesn't make handled errors
491 cause the debugger to get invoked.
492
493 STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that
494 those functions aren't done more than once in a single `signal'
495 session. */
496
497 static Lisp_Object
498 signal_call_debugger (Lisp_Object conditions,
499 Lisp_Object sig, Lisp_Object data,
500 Lisp_Object active_handlers,
501 int signal_vars_only,
502 int *stack_trace_displayed,
503 int *debugger_entered)
504 {
505 /* This function can GC */
506 Lisp_Object val = Qunbound;
507 Lisp_Object all_handlers = Vcondition_handlers;
508 int speccount = specpdl_depth_counter;
509 struct gcpro gcpro1;
510 GCPRO1 (all_handlers);
511
512 Vcondition_handlers = active_handlers;
513
514 if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only
515 && wants_debugger (Vstack_trace_on_error, conditions))
516 {
517 specbind (Qdebug_on_error, Qnil);
518 specbind (Qstack_trace_on_error, Qnil);
519 specbind (Qdebug_on_signal, Qnil);
520 specbind (Qstack_trace_on_signal, Qnil);
521
522 internal_with_output_to_temp_buffer ("*Backtrace*",
523 backtrace_259,
524 Qnil,
525 Qnil);
526 unbind_to (speccount, Qnil);
527 *stack_trace_displayed = 1;
528 }
529
530 if (!entering_debugger && !*debugger_entered && !signal_vars_only
531 && (EQ (sig, Qquit)
532 ? debug_on_quit
533 : wants_debugger (Vdebug_on_error, conditions)))
534 {
535 debug_on_quit &= ~2; /* reset critical bit */
536 specbind (Qdebug_on_error, Qnil);
537 specbind (Qstack_trace_on_error, Qnil);
538 specbind (Qdebug_on_signal, Qnil);
539 specbind (Qstack_trace_on_signal, Qnil);
540
541 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
542 *debugger_entered = 1;
543 }
544
545 if (!entering_debugger && !*stack_trace_displayed
546 && wants_debugger (Vstack_trace_on_signal, conditions))
547 {
548 specbind (Qdebug_on_error, Qnil);
549 specbind (Qstack_trace_on_error, Qnil);
550 specbind (Qdebug_on_signal, Qnil);
551 specbind (Qstack_trace_on_signal, Qnil);
552
553 internal_with_output_to_temp_buffer ("*Backtrace*",
554 backtrace_259,
555 Qnil,
556 Qnil);
557 unbind_to (speccount, Qnil);
558 *stack_trace_displayed = 1;
559 }
560
561 if (!entering_debugger && !*debugger_entered
562 && (EQ (sig, Qquit)
563 ? debug_on_quit
564 : wants_debugger (Vdebug_on_signal, conditions)))
565 {
566 debug_on_quit &= ~2; /* reset critical bit */
567 specbind (Qdebug_on_error, Qnil);
568 specbind (Qstack_trace_on_error, Qnil);
569 specbind (Qdebug_on_signal, Qnil);
570 specbind (Qstack_trace_on_signal, Qnil);
571
572 val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
573 *debugger_entered = 1;
574 }
575
576 UNGCPRO;
577 Vcondition_handlers = all_handlers;
578 return (unbind_to (speccount, val));
579 }
580
581
582 /**********************************************************************/
583 /* The basic special forms */
584 /**********************************************************************/
585
586 /* NOTE!!! Every function that can call EVAL must protect its args
587 and temporaries from garbage collection while it needs them.
588 The definition of `For' shows what you have to do. */
589
590 DEFUN ("or", For, Sor, 0, UNEVALLED, 0 /*
591 Eval args until one of them yields non-nil, then return that value.
592 The remaining args are not evalled at all.
593 If all args return nil, return nil.
594 */ )
595 (args)
596 Lisp_Object args;
597 {
598 /* This function can GC */
599 REGISTER Lisp_Object val;
600 Lisp_Object args_left;
601 struct gcpro gcpro1;
602
603 if (NILP (args))
604 return Qnil;
605
606 args_left = args;
607 GCPRO1 (args_left);
608
609 do
610 {
611 val = Feval (Fcar (args_left));
612 if (!NILP (val))
613 break;
614 args_left = Fcdr (args_left);
615 }
616 while (!NILP (args_left));
617
618 UNGCPRO;
619 return val;
620 }
621
622 DEFUN ("and", Fand, Sand, 0, UNEVALLED, 0 /*
623 Eval args until one of them yields nil, then return nil.
624 The remaining args are not evalled at all.
625 If no arg yields nil, return the last arg's value.
626 */ )
627 (args)
628 Lisp_Object args;
629 {
630 /* This function can GC */
631 REGISTER Lisp_Object val;
632 Lisp_Object args_left;
633 struct gcpro gcpro1;
634
635 if (NILP (args))
636 return Qt;
637
638 args_left = args;
639 GCPRO1 (args_left);
640
641 do
642 {
643 val = Feval (Fcar (args_left));
644 if (NILP (val))
645 break;
646 args_left = Fcdr (args_left);
647 }
648 while (!NILP (args_left));
649
650 UNGCPRO;
651 return val;
652 }
653
654 DEFUN ("if", Fif, Sif, 2, UNEVALLED, 0 /*
655 (if COND THEN ELSE...): if COND yields non-nil, do THEN, else do ELSE...
656 Returns the value of THEN or the value of the last of the ELSE's.
657 THEN must be one expression, but ELSE... can be zero or more expressions.
658 If COND yields nil, and there are no ELSE's, the value is nil.
659 */ )
660 (args)
661 Lisp_Object args;
662 {
663 /* This function can GC */
664 Lisp_Object cond;
665 struct gcpro gcpro1;
666
667 GCPRO1 (args);
668 cond = Feval (Fcar (args));
669 UNGCPRO;
670
671 if (!NILP (cond))
672 return Feval (Fcar (Fcdr (args)));
673 return Fprogn (Fcdr (Fcdr (args)));
674 }
675
676 DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0 /*
677 (cond CLAUSES...): try each clause until one succeeds.
678 Each clause looks like (CONDITION BODY...). CONDITION is evaluated
679 and, if the value is non-nil, this clause succeeds:
680 then the expressions in BODY are evaluated and the last one's
681 value is the value of the cond-form.
682 If no clause succeeds, cond returns nil.
683 If a clause has one element, as in (CONDITION),
684 CONDITION's value if non-nil is returned from the cond-form.
685 */ )
686 (args)
687 Lisp_Object args;
688 {
689 /* This function can GC */
690 REGISTER Lisp_Object clause, val;
691 struct gcpro gcpro1;
692
693 val = Qnil;
694 GCPRO1 (args);
695 while (!NILP (args))
696 {
697 clause = Fcar (args);
698 val = Feval (Fcar (clause));
699 if (!NILP (val))
700 {
701 if (!EQ (XCDR (clause), Qnil))
702 val = Fprogn (XCDR (clause));
703 break;
704 }
705 args = XCDR (args);
706 }
707 UNGCPRO;
708
709 return val;
710 }
711
712 DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0 /*
713 (progn BODY...): eval BODY forms sequentially and return value of last one.
714 */ )
715 (args)
716 Lisp_Object args;
717 {
718 /* This function can GC */
719 REGISTER Lisp_Object val;
720 Lisp_Object args_left;
721 struct gcpro gcpro1;
722
723 #ifdef MOCKLISP_SUPPORT
724 /* In Mucklisp code, symbols at the front of the progn arglist
725 are to be bound to zero. */
726 if (!EQ (Vmocklisp_arguments, Qt))
727 {
728 Lisp_Object tem;
729 val = Qzero;
730 while (!NILP (args) && (tem = Fcar (args), SYMBOLP (tem)))
731 {
732 QUIT;
733 specbind (tem, val), args = Fcdr (args);
734 }
735 }
736 #endif
737
738 if (NILP (args))
739 return Qnil;
740
741 args_left = args;
742 GCPRO1 (args_left);
743
744 do
745 {
746 val = Feval (Fcar (args_left));
747 args_left = Fcdr (args_left);
748 }
749 while (!NILP (args_left));
750
751 UNGCPRO;
752 return val;
753 }
754
755 DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0 /*
756 (prog1 FIRST BODY...): eval FIRST and BODY sequentially; value from FIRST.
757 The value of FIRST is saved during the evaluation of the remaining args,
758 whose values are discarded.
759 */ )
760 (args)
761 Lisp_Object args;
762 {
763 /* This function can GC */
764 Lisp_Object val;
765 REGISTER Lisp_Object args_left;
766 struct gcpro gcpro1, gcpro2;
767 REGISTER int argnum = 0;
768
769 if (NILP (args))
770 return Qnil;
771
772 args_left = args;
773 val = Qnil;
774 GCPRO2 (args, val);
775
776 do
777 {
778 if (!(argnum++))
779 val = Feval (Fcar (args_left));
780 else
781 Feval (Fcar (args_left));
782 args_left = Fcdr (args_left);
783 }
784 while (!NILP (args_left));
785
786 UNGCPRO;
787 return val;
788 }
789
790 DEFUN ("prog2", Fprog2, Sprog2, 2, UNEVALLED, 0 /*
791 (prog2 X Y BODY...): eval X, Y and BODY sequentially; value from Y.
792 The value of Y is saved during the evaluation of the remaining args,
793 whose values are discarded.
794 */ )
795 (args)
796 Lisp_Object args;
797 {
798 /* This function can GC */
799 Lisp_Object val;
800 REGISTER Lisp_Object args_left;
801 struct gcpro gcpro1, gcpro2;
802 REGISTER int argnum = -1;
803
804 val = Qnil;
805
806 if (NILP (args))
807 return Qnil;
808
809 args_left = args;
810 val = Qnil;
811 GCPRO2 (args, val);
812
813 do
814 {
815 if (!(argnum++))
816 val = Feval (Fcar (args_left));
817 else
818 Feval (Fcar (args_left));
819 args_left = Fcdr (args_left);
820 }
821 while (!NILP (args_left));
822
823 UNGCPRO;
824 return val;
825 }
826
827 DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0 /*
828 (let* VARLIST BODY...): bind variables according to VARLIST then eval BODY.
829 The value of the last form in BODY is returned.
830 Each element of VARLIST is a symbol (which is bound to nil)
831 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
832 Each VALUEFORM can refer to the symbols already bound by this VARLIST.
833 */ )
834 (args)
835 Lisp_Object args;
836 {
837 /* This function can GC */
838 Lisp_Object varlist, val, elt;
839 int speccount = specpdl_depth_counter;
840 struct gcpro gcpro1, gcpro2, gcpro3;
841
842 GCPRO3 (args, elt, varlist);
843
844 varlist = Fcar (args);
845 while (!NILP (varlist))
846 {
847 QUIT;
848 elt = Fcar (varlist);
849 if (SYMBOLP (elt))
850 specbind (elt, Qnil);
851 else if (! NILP (Fcdr (Fcdr (elt))))
852 signal_simple_error ("`let' bindings can have only one value-form",
853 elt);
854 else
855 {
856 val = Feval (Fcar (Fcdr (elt)));
857 specbind (Fcar (elt), val);
858 }
859 varlist = Fcdr (varlist);
860 }
861 UNGCPRO;
862 val = Fprogn (Fcdr (args));
863 return unbind_to (speccount, val);
864 }
865
866 DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0 /*
867 (let VARLIST BODY...): bind variables according to VARLIST then eval BODY.
868 The value of the last form in BODY is returned.
869 Each element of VARLIST is a symbol (which is bound to nil)
870 or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
871 All the VALUEFORMs are evalled before any symbols are bound.
872 */ )
873 (args)
874 Lisp_Object args;
875 {
876 /* This function can GC */
877 Lisp_Object *temps, tem;
878 REGISTER Lisp_Object elt, varlist;
879 int speccount = specpdl_depth_counter;
880 REGISTER int argnum;
881 struct gcpro gcpro1, gcpro2;
882
883 varlist = Fcar (args);
884
885 /* Make space to hold the values to give the bound variables */
886 elt = Flength (varlist);
887 temps = (Lisp_Object *) alloca (XINT (elt) * sizeof (Lisp_Object));
888
889 /* Compute the values and store them in `temps' */
890
891 GCPRO2 (args, *temps);
892 gcpro2.nvars = 0;
893
894 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
895 {
896 QUIT;
897 elt = Fcar (varlist);
898 if (SYMBOLP (elt))
899 temps [argnum++] = Qnil;
900 else if (! NILP (Fcdr (Fcdr (elt))))
901 signal_simple_error ("`let' bindings can have only one value-form",
902 elt);
903 else
904 temps [argnum++] = Feval (Fcar (Fcdr (elt)));
905 gcpro2.nvars = argnum;
906 }
907 UNGCPRO;
908
909 varlist = Fcar (args);
910 for (argnum = 0; !NILP (varlist); varlist = Fcdr (varlist))
911 {
912 elt = Fcar (varlist);
913 tem = temps[argnum++];
914 if (SYMBOLP (elt))
915 specbind (elt, tem);
916 else
917 specbind (Fcar (elt), tem);
918 }
919
920 elt = Fprogn (Fcdr (args));
921 return unbind_to (speccount, elt);
922 }
923
924 DEFUN ("while", Fwhile, Swhile, 1, UNEVALLED, 0 /*
925 (while TEST BODY...): if TEST yields non-nil, eval BODY... and repeat.
926 The order of execution is thus TEST, BODY, TEST, BODY and so on
927 until TEST returns nil.
928 */ )
929 (args)
930 Lisp_Object args;
931 {
932 /* This function can GC */
933 Lisp_Object test, body, tem;
934 struct gcpro gcpro1, gcpro2;
935
936 GCPRO2 (test, body);
937
938 test = Fcar (args);
939 body = Fcdr (args);
940 #ifdef MOCKLISP_SUPPORT
941 while (tem = Feval (test),
942 (!EQ (Vmocklisp_arguments, Qt) ? XINT (tem) : !NILP (tem)))
943 #else
944 while (tem = Feval (test), !NILP (tem))
945 #endif
946 {
947 QUIT;
948 Fprogn (body);
949 }
950
951 UNGCPRO;
952 return Qnil;
953 }
954
955 Lisp_Object Qsetq;
956
957 DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0 /*
958 (setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.
959 The symbols SYM are variables; they are literal (not evaluated).
960 The values VAL are expressions; they are evaluated.
961 Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
962 The second VAL is not computed until after the first SYM is set, and so on;
963 each VAL can use the new value of variables set earlier in the `setq'.
964 The return value of the `setq' form is the value of the last VAL.
965 */ )
966 (args)
967 Lisp_Object args;
968 {
969 /* This function can GC */
970 REGISTER Lisp_Object args_left;
971 REGISTER Lisp_Object val, sym;
972 struct gcpro gcpro1;
973
974 if (NILP (args))
975 return Qnil;
976
977 val = Flength (args);
978 if (XINT (val) & 1) /* Odd number of arguments? */
979 Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, val));
980
981 args_left = args;
982 GCPRO1 (args);
983
984 do
985 {
986 val = Feval (Fcar (Fcdr (args_left)));
987 sym = Fcar (args_left);
988 Fset (sym, val);
989 args_left = Fcdr (Fcdr (args_left));
990 }
991 while (!NILP (args_left));
992
993 UNGCPRO;
994 return val;
995 }
996
997 DEFUN ("quote", Fquote, Squote, 1, UNEVALLED, 0 /*
998 Return the argument, without evaluating it. `(quote x)' yields `x'.
999 */ )
1000 (args)
1001 Lisp_Object args;
1002 {
1003 return Fcar (args);
1004 }
1005
1006 DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0 /*
1007 Like `quote', but preferred for objects which are functions.
1008 In byte compilation, `function' causes its argument to be compiled.
1009 `quote' cannot do that.
1010 */ )
1011 (args)
1012 Lisp_Object args;
1013 {
1014 return Fcar (args);
1015 }
1016
1017
1018 /**********************************************************************/
1019 /* Defining functions/variables */
1020 /**********************************************************************/
1021
1022 DEFUN ("defun", Fdefun, Sdefun, 2, UNEVALLED, 0 /*
1023 (defun NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.
1024 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
1025 See also the function `interactive'.
1026 */ )
1027 (args)
1028 Lisp_Object args;
1029 {
1030 /* This function can GC */
1031 Lisp_Object fn_name;
1032 Lisp_Object defn;
1033
1034 fn_name = Fcar (args);
1035 defn = Fcons (Qlambda, Fcdr (args));
1036 if (purify_flag)
1037 defn = Fpurecopy (defn);
1038 Ffset (fn_name, defn);
1039 LOADHIST_ATTACH (fn_name);
1040 return fn_name;
1041 }
1042
1043 DEFUN ("defmacro", Fdefmacro, Sdefmacro, 2, UNEVALLED, 0 /*
1044 (defmacro NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.
1045 The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).
1046 When the macro is called, as in (NAME ARGS...),
1047 the function (lambda ARGLIST BODY...) is applied to
1048 the list ARGS... as it appears in the expression,
1049 and the result should be a form to be evaluated instead of the original.
1050 */ )
1051 (args)
1052 Lisp_Object args;
1053 {
1054 /* This function can GC */
1055 Lisp_Object fn_name;
1056 Lisp_Object defn;
1057
1058 fn_name = Fcar (args);
1059 defn = Fcons (Qmacro, Fcons (Qlambda, Fcdr (args)));
1060 if (purify_flag)
1061 defn = Fpurecopy (defn);
1062 Ffset (fn_name, defn);
1063 LOADHIST_ATTACH (fn_name);
1064 return fn_name;
1065 }
1066
1067 DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0 /*
1068 (defvar SYMBOL INITVALUE DOCSTRING): define SYMBOL as a variable.
1069 You are not required to define a variable in order to use it,
1070 but the definition can supply documentation and an initial value
1071 in a way that tags can recognize.
1072
1073 INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is
1074 void. (However, when you evaluate a defvar interactively, it acts like a
1075 defconst: SYMBOL's value is always set regardless of whether it's currently
1076 void.)
1077 If SYMBOL is buffer-local, its default value is what is set;
1078 buffer-local values are not affected.
1079 INITVALUE and DOCSTRING are optional.
1080 If DOCSTRING starts with *, this variable is identified as a user option.
1081 This means that M-x set-variable and M-x edit-options recognize it.
1082 If INITVALUE is missing, SYMBOL's value is not set.
1083
1084 In lisp-interaction-mode defvar is treated as defconst.
1085 */ )
1086 (args)
1087 Lisp_Object args;
1088 {
1089 /* This function can GC */
1090 REGISTER Lisp_Object sym, tem, tail;
1091
1092 sym = Fcar (args);
1093 tail = Fcdr (args);
1094 if (!NILP (Fcdr (Fcdr (tail))))
1095 error ("too many arguments");
1096
1097 if (!NILP (tail))
1098 {
1099 tem = Fdefault_boundp (sym);
1100 if (NILP (tem))
1101 Fset_default (sym, Feval (Fcar (Fcdr (args))));
1102 }
1103
1104 #ifdef I18N3
1105 if (!NILP (Vfile_domain))
1106 pure_put (sym, Qvariable_domain, Vfile_domain);
1107 #endif
1108
1109 tail = Fcdr (Fcdr (args));
1110 if (!NILP (Fcar (tail)))
1111 {
1112 tem = Fcar (tail);
1113 #if 0 /* FSFmacs */
1114 /* #### We should probably do this but it might be dangerous */
1115 if (purify_flag)
1116 tem = Fpurecopy (tem);
1117 Fput (sym, Qvariable_documentation, tem);
1118 #else
1119 pure_put (sym, Qvariable_documentation, tem);
1120 #endif
1121 }
1122
1123 LOADHIST_ATTACH (sym);
1124 return sym;
1125 }
1126
1127 DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0 /*
1128 (defconst SYMBOL INITVALUE DOCSTRING): define SYMBOL as a constant
1129 variable.
1130 The intent is that programs do not change this value, but users may.
1131 Always sets the value of SYMBOL to the result of evalling INITVALUE.
1132 If SYMBOL is buffer-local, its default value is what is set;
1133 buffer-local values are not affected.
1134 DOCSTRING is optional.
1135 If DOCSTRING starts with *, this variable is identified as a user option.
1136 This means that M-x set-variable and M-x edit-options recognize it.
1137
1138 Note: do not use `defconst' for user options in libraries that are not
1139 normally loaded, since it is useful for users to be able to specify
1140 their own values for such variables before loading the library.
1141 Since `defconst' unconditionally assigns the variable,
1142 it would override the user's choice.
1143 */ )
1144 (args)
1145 Lisp_Object args;
1146 {
1147 /* This function can GC */
1148 REGISTER Lisp_Object sym, tem;
1149
1150 sym = Fcar (args);
1151 if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
1152 error ("too many arguments");
1153
1154 Fset_default (sym, Feval (Fcar (Fcdr (args))));
1155
1156 #ifdef I18N3
1157 if (!NILP (Vfile_domain))
1158 pure_put (sym, Qvariable_domain, Vfile_domain);
1159 #endif
1160
1161 tem = Fcar (Fcdr (Fcdr (args)));
1162
1163 if (!NILP (tem))
1164 #if 0 /* FSFmacs */
1165 /* #### We should probably do this but it might be dangerous */
1166 {
1167 if (purify_flag)
1168 tem = Fpurecopy (tem);
1169 Fput (sym, Qvariable_documentation, tem);
1170 }
1171 #else
1172 pure_put (sym, Qvariable_documentation, tem);
1173 #endif
1174
1175 LOADHIST_ATTACH (sym);
1176 return sym;
1177 }
1178
1179 DEFUN ("user-variable-p", Fuser_variable_p, Suser_variable_p, 1, 1, 0 /*
1180 Return t if VARIABLE is intended to be set and modified by users.
1181 \(The alternative is a variable used internally in a Lisp program.)
1182 Determined by whether the first character of the documentation
1183 for the variable is `*'.
1184 */ )
1185 (variable)
1186 Lisp_Object variable;
1187 {
1188 Lisp_Object documentation;
1189
1190 documentation = Fget (variable, Qvariable_documentation, Qnil);
1191 if (INTP (documentation) && XINT (documentation) < 0)
1192 return Qt;
1193 if ((STRINGP (documentation)) &&
1194 (string_byte (XSTRING (documentation), 0) == '*'))
1195 return Qt;
1196 /* If it is (STRING . INTEGER), a negative integer means a user variable. */
1197 if (CONSP (documentation)
1198 && STRINGP (XCAR (documentation))
1199 && INTP (XCDR (documentation))
1200 && XINT (XCDR (documentation)) < 0)
1201 return Qt;
1202 return Qnil;
1203 }
1204
1205 DEFUN ("macroexpand-internal", Fmacroexpand_internal, Smacroexpand_internal,
1206 1, 2, 0 /*
1207 Return result of expanding macros at top level of FORM.
1208 If FORM is not a macro call, it is returned unchanged.
1209 Otherwise, the macro is expanded and the expansion is considered
1210 in place of FORM. When a non-macro-call results, it is returned.
1211
1212 The second optional arg ENVIRONMENT species an environment of macro
1213 definitions to shadow the loaded ones for use in file byte-compilation.
1214 */ )
1215 (form, env)
1216 Lisp_Object form;
1217 Lisp_Object env;
1218 {
1219 /* This function can GC */
1220 /* With cleanups from Hallvard Furuseth. */
1221 REGISTER Lisp_Object expander, sym, def, tem;
1222
1223 while (1)
1224 {
1225 /* Come back here each time we expand a macro call,
1226 in case it expands into another macro call. */
1227 if (!CONSP (form))
1228 break;
1229 /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
1230 def = sym = XCAR (form);
1231 tem = Qnil;
1232 /* Trace symbols aliases to other symbols
1233 until we get a symbol that is not an alias. */
1234 while (SYMBOLP (def))
1235 {
1236 QUIT;
1237 sym = def;
1238 tem = Fassq (sym, env);
1239 if (NILP (tem))
1240 {
1241 def = XSYMBOL (sym)->function;
1242 if (!UNBOUNDP (def))
1243 continue;
1244 }
1245 break;
1246 }
1247 /* Right now TEM is the result from SYM in ENV,
1248 and if TEM is nil then DEF is SYM's function definition. */
1249 if (NILP (tem))
1250 {
1251 /* SYM is not mentioned in ENV.
1252 Look at its function definition. */
1253 if (UNBOUNDP (def)
1254 || !CONSP (def))
1255 /* Not defined or definition not suitable */
1256 break;
1257 if (EQ (XCAR (def), Qautoload))
1258 {
1259 /* Autoloading function: will it be a macro when loaded? */
1260 tem = Felt (def, make_int (4));
1261 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1262 {
1263 /* Yes, load it and try again. */
1264 do_autoload (def, sym);
1265 continue;
1266 }
1267 else
1268 break;
1269 }
1270 else if (!EQ (XCAR (def), Qmacro))
1271 break;
1272 else expander = XCDR (def);
1273 }
1274 else
1275 {
1276 expander = XCDR (tem);
1277 if (NILP (expander))
1278 break;
1279 }
1280 form = apply1 (expander, XCDR (form));
1281 }
1282 return form;
1283 }
1284
1285
1286 /**********************************************************************/
1287 /* Non-local exits */
1288 /**********************************************************************/
1289
1290 DEFUN ("catch", Fcatch, Scatch, 1, UNEVALLED, 0 /*
1291 (catch TAG BODY...): eval BODY allowing nonlocal exits using `throw'.
1292 TAG is evalled to get the tag to use. Then the BODY is executed.
1293 Within BODY, (throw TAG) with same tag exits BODY and exits this `catch'.
1294 If no throw happens, `catch' returns the value of the last BODY form.
1295 If a throw happens, it specifies the value to return from `catch'.
1296 */ )
1297 (args)
1298 Lisp_Object args;
1299 {
1300 /* This function can GC */
1301 Lisp_Object tag;
1302 struct gcpro gcpro1;
1303
1304 GCPRO1 (args);
1305 tag = Feval (Fcar (args));
1306 UNGCPRO;
1307 return internal_catch (tag, Fprogn, Fcdr (args), 0);
1308 }
1309
1310 /* Set up a catch, then call C function FUNC on argument ARG.
1311 FUNC should return a Lisp_Object.
1312 This is how catches are done from within C code. */
1313
1314 Lisp_Object
1315 internal_catch (Lisp_Object tag,
1316 Lisp_Object (*func) (Lisp_Object arg),
1317 Lisp_Object arg,
1318 int *threw)
1319 {
1320 /* This structure is made part of the chain `catchlist'. */
1321 struct catchtag c;
1322
1323 /* Fill in the components of c, and put it on the list. */
1324 c.next = catchlist;
1325 c.tag = tag;
1326 c.val = Qnil;
1327 c.backlist = backtrace_list;
1328 #if 0 /* FSFmacs */
1329 /* #### */
1330 c.handlerlist = handlerlist;
1331 #endif
1332 c.lisp_eval_depth = lisp_eval_depth;
1333 c.pdlcount = specpdl_depth_counter;
1334 #if 0 /* FSFmacs */
1335 c.poll_suppress_count = async_timer_suppress_count;
1336 #endif
1337 c.gcpro = gcprolist;
1338 catchlist = &c;
1339
1340 /* Call FUNC. */
1341 if (SETJMP (c.jmp))
1342 {
1343 /* Throw works by a longjmp that comes right here. */
1344 if (threw) *threw = 1;
1345 return (c.val);
1346 }
1347 c.val = (*func) (arg);
1348 if (threw) *threw = 0;
1349 catchlist = c.next;
1350 return (c.val);
1351 }
1352
1353
1354 /* Unwind the specbind, catch, and handler stacks back to CATCH, and
1355 jump to that CATCH, returning VALUE as the value of that catch.
1356
1357 This is the guts Fthrow and Fsignal; they differ only in the way
1358 they choose the catch tag to throw to. A catch tag for a
1359 condition-case form has a TAG of Qnil.
1360
1361 Before each catch is discarded, unbind all special bindings and
1362 execute all unwind-protect clauses made above that catch. Unwind
1363 the handler stack as we go, so that the proper handlers are in
1364 effect for each unwind-protect clause we run. At the end, restore
1365 some static info saved in CATCH, and longjmp to the location
1366 specified in the
1367
1368 This is used for correct unwinding in Fthrow and Fsignal. */
1369
1370 static void
1371 unwind_to_catch (struct catchtag *c, Lisp_Object val)
1372 {
1373 #if 0 /* FSFmacs */
1374 /* #### */
1375 register int last_time;
1376 #endif
1377
1378 /* Unwind the specbind, catch, and handler stacks back to CATCH
1379 Before each catch is discarded, unbind all special bindings
1380 and execute all unwind-protect clauses made above that catch.
1381 At the end, restore some static info saved in CATCH,
1382 and longjmp to the location specified.
1383 */
1384
1385 /* Save the value somewhere it will be GC'ed.
1386 (Can't overwrite tag slot because an unwind-protect may
1387 want to throw to this same tag, which isn't yet invalid.) */
1388 c->val = val;
1389
1390 #if 0 /* FSFmacs */
1391 /* Restore the polling-suppression count. */
1392 set_poll_suppress_count (catch->poll_suppress_count);
1393 #endif
1394
1395 #if 0 /* FSFmacs */
1396 /* #### FSFmacs has the following loop. Is it more correct? */
1397 do
1398 {
1399 last_time = catchlist == c;
1400
1401 /* Unwind the specpdl stack, and then restore the proper set of
1402 handlers. */
1403 unbind_to (catchlist->pdlcount, Qnil);
1404 handlerlist = catchlist->handlerlist;
1405 catchlist = catchlist->next;
1406 }
1407 while (! last_time);
1408 #else /* Actual XEmacs code */
1409 /* Unwind the specpdl stack */
1410 unbind_to (c->pdlcount, Qnil);
1411 catchlist = c->next;
1412 #endif
1413
1414 gcprolist = c->gcpro;
1415 backtrace_list = c->backlist;
1416 lisp_eval_depth = c->lisp_eval_depth;
1417
1418 throw_level = 0;
1419 LONGJMP (c->jmp, 1);
1420 }
1421
1422 static DOESNT_RETURN
1423 throw_or_bomb_out (Lisp_Object tag, Lisp_Object val, int bomb_out_p,
1424 Lisp_Object sig, Lisp_Object data)
1425 {
1426 /* die if we recurse more than is reasonable */
1427 if (++throw_level > 20)
1428 abort();
1429
1430 /* If bomb_out_p is t, this is being called from Fsignal as a
1431 "last resort" when there is no handler for this error and
1432 the debugger couldn't be invoked, so we are throwing to
1433 'top-level. If this tag doesn't exist (happens during the
1434 initialization stages) we would get in an infinite recursive
1435 Fsignal/Fthrow loop, so instead we bomb out to the
1436 really-early-error-handler.
1437
1438 Note that in fact the only time that the "last resort"
1439 occurs is when there's no catch for 'top-level -- the
1440 'top-level catch and the catch-all error handler are
1441 established at the same time, in initial_command_loop/
1442 top_level_1.
1443
1444 #### Fix this horrifitude!
1445 */
1446
1447 while (1)
1448 {
1449 REGISTER struct catchtag *c;
1450
1451 #if 0 /* FSFmacs */
1452 if (!NILP (tag)) /* #### */
1453 #endif
1454 for (c = catchlist; c; c = c->next)
1455 {
1456 if (EQ (c->tag, tag))
1457 unwind_to_catch (c, val);
1458 }
1459 if (!bomb_out_p)
1460 tag = Fsignal (Qno_catch, list2 (tag, val));
1461 else
1462 call1 (Qreally_early_error_handler, Fcons (sig, data));
1463 }
1464
1465 /* can't happen. who cares? - (Sun's compiler does) */
1466 /* throw_level--; */
1467 /* getting tired of compilation warnings */
1468 /* return Qnil; */
1469 }
1470
1471 /* See above, where CATCHLIST is defined, for a description of how
1472 Fthrow() works.
1473
1474 Fthrow() is also called by Fsignal(), to do a non-local jump
1475 back to the appropriate condition-case handler after (maybe)
1476 the debugger is entered. In that case, TAG is the value
1477 of Vcondition_handlers that was in place just after the
1478 condition-case handler was set up. The car of this will be
1479 some data referring to the handler: Its car will be Qunbound
1480 (thus, this tag can never be generated by Lisp code), and
1481 its CDR will be the HANDLERS argument to condition_case_1()
1482 (either Qerror, Qt, or a list of handlers as in `condition-case').
1483 This works fine because Fthrow() does not care what TAG was
1484 passed to it: it just looks up the catch list for something
1485 that is EQ() to TAG. When it finds it, it will longjmp()
1486 back to the place that established the catch (in this case,
1487 condition_case_1). See below for more info.
1488 */
1489
1490 DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0 /*
1491 (throw TAG VALUE): throw to the catch for TAG and return VALUE from it.
1492 Both TAG and VALUE are evalled.
1493 */ )
1494 (tag, val)
1495 Lisp_Object tag, val;
1496 {
1497 throw_or_bomb_out (tag, val, 0, Qnil, Qnil); /* Doesn't return */
1498 return Qnil;
1499 }
1500
1501 DEFUN ("unwind-protect", Funwind_protect, Sunwind_protect, 1, UNEVALLED, 0 /*
1502 Do BODYFORM, protecting with UNWINDFORMS.
1503 Usage looks like (unwind-protect BODYFORM UNWINDFORMS...).
1504 If BODYFORM completes normally, its value is returned
1505 after executing the UNWINDFORMS.
1506 If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.
1507 */ )
1508 (args)
1509 Lisp_Object args;
1510 {
1511 /* This function can GC */
1512 Lisp_Object val;
1513 int speccount = specpdl_depth_counter;
1514
1515 record_unwind_protect (Fprogn, Fcdr (args));
1516 val = Feval (Fcar (args));
1517 return unbind_to (speccount, val);
1518 }
1519
1520
1521 /**********************************************************************/
1522 /* Signalling and trapping errors */
1523 /**********************************************************************/
1524
1525 static Lisp_Object
1526 condition_bind_unwind (Lisp_Object loser)
1527 {
1528 struct Lisp_Cons *victim;
1529 /* ((handler-fun . handler-args) ... other handlers) */
1530 Lisp_Object tem = XCAR (loser);
1531
1532 while (CONSP (tem))
1533 {
1534 victim = XCONS (tem);
1535 tem = victim->cdr;
1536 free_cons (victim);
1537 }
1538 victim = XCONS (loser);
1539
1540 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1541 Vcondition_handlers = victim->cdr;
1542
1543 free_cons (victim);
1544 return (Qnil);
1545 }
1546
1547 static Lisp_Object
1548 condition_case_unwind (Lisp_Object loser)
1549 {
1550 struct Lisp_Cons *victim;
1551
1552 /* ((<unbound> . clauses) ... other handlers */
1553 victim = XCONS (XCAR (loser));
1554 free_cons (victim);
1555
1556 victim = XCONS (loser);
1557 if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
1558 Vcondition_handlers = victim->cdr;
1559
1560 free_cons (victim);
1561 return (Qnil);
1562 }
1563
1564 /* Split out from condition_case_3 so that primitive C callers
1565 don't have to cons up a lisp handler form to be evaluated. */
1566
1567 /* Call a function BFUN of one argument BARG, trapping errors as
1568 specified by HANDLERS. If no error occurs that is indicated by
1569 HANDLERS as something to be caught, the return value of this
1570 function is the return value from BFUN. If such an error does
1571 occur, HFUN is called, and its return value becomes the
1572 return value of condition_case_1(). The second argument passed
1573 to HFUN will always be HARG. The first argument depends on
1574 HANDLERS:
1575
1576 If HANDLERS is Qt, all errors (this includes QUIT, but not
1577 non-local exits with `throw') cause HFUN to be invoked, and VAL
1578 (the first argument to HFUN) is a cons (SIG . DATA) of the
1579 arguments passed to `signal'. The debugger is not invoked even if
1580 `debug-on-error' was set.
1581
1582 A HANDLERS value of Qerror is the same as Qt except that the
1583 debugger is invoked if `debug-on-error' was set.
1584
1585 Otherwise, HANDLERS should be a list of lists (CONDITION-NAME BODY ...)
1586 exactly as in `condition-case', and errors will be trapped
1587 as indicated in HANDLERS. VAL (the first argument to HFUN) will
1588 be a cons whose car is the cons (SIG . DATA) and whose CDR is the
1589 list (BODY ...) from the appropriate slot in HANDLERS.
1590
1591 This function pushes HANDLERS onto the front of Vcondition_handlers
1592 (actually with a Qunbound marker as well -- see Fthrow() above
1593 for why), establishes a catch whose tag is this new value of
1594 Vcondition_handlers, and calls BFUN. When Fsignal() is called,
1595 it calls Fthrow(), setting TAG to this same new value of
1596 Vcondition_handlers and setting VAL to the same thing that will
1597 be passed to HFUN, as above. Fthrow() longjmp()s back to the
1598 jump point we just established, and we in turn just call the
1599 HFUN and return its value.
1600
1601 For a real condition-case, HFUN will always be
1602 run_condition_case_handlers() and HARG is the argument VAR
1603 to condition-case. That function just binds VAR to the cons
1604 (SIG . DATA) that is the CAR of VAL, and calls the handler
1605 (BODY ...) that is the CDR of VAL. Note that before calling
1606 Fthrow(), Fsignal() restored Vcondition_handlers to the value
1607 it had *before* condition_case_1() was called. This maintains
1608 consistency (so that the state of things at exit of
1609 condition_case_1() is the same as at entry), and implies
1610 that the handler can signal the same error again (possibly
1611 after processing of its own), without getting in an infinite
1612 loop. */
1613
1614 Lisp_Object
1615 condition_case_1 (Lisp_Object handlers,
1616 Lisp_Object (*bfun) (Lisp_Object barg),
1617 Lisp_Object barg,
1618 Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg),
1619 Lisp_Object harg)
1620 {
1621 int speccount = specpdl_depth_counter;
1622 struct catchtag c;
1623 struct gcpro gcpro1;
1624
1625 #if 0 /* FSFmacs */
1626 c.tag = Qnil;
1627 #else
1628 /* Do consing now so out-of-memory error happens up front */
1629 /* (unbound . stuff) is a special condition-case kludge marker
1630 which is known specially by Fsignal.
1631 This is an abomination, but to fix it would require either
1632 making condition_case cons (a union of the conditions of the clauses)
1633 or changing the byte-compiler output (no thanks). */
1634 c.tag = noseeum_cons (noseeum_cons (Qunbound, handlers),
1635 Vcondition_handlers);
1636 #endif
1637 c.val = Qnil;
1638 c.backlist = backtrace_list;
1639 #if 0 /* FSFmacs */
1640 /* #### */
1641 c.handlerlist = handlerlist;
1642 #endif
1643 c.lisp_eval_depth = lisp_eval_depth;
1644 c.pdlcount = specpdl_depth_counter;
1645 #if 0 /* FSFmacs */
1646 c.poll_suppress_count = async_timer_suppress_count;
1647 #endif
1648 c.gcpro = gcprolist;
1649 /* #### FSFmacs does the following statement *after* the setjmp(). */
1650 c.next = catchlist;
1651
1652 if (SETJMP (c.jmp))
1653 {
1654 /* throw does ungcpro, etc */
1655 return ((*hfun) (c.val, harg));
1656 }
1657
1658 record_unwind_protect (condition_case_unwind, c.tag);
1659
1660 catchlist = &c;
1661 #if 0 /* FSFmacs */
1662 h.handler = handlers;
1663 h.var = Qnil;
1664 h.next = handlerlist;
1665 h.tag = &c;
1666 handlerlist = &h;
1667 #else
1668 Vcondition_handlers = c.tag;
1669 #endif
1670 GCPRO1 (harg); /* Somebody has to gc-protect */
1671
1672 c.val = ((*bfun) (barg));
1673
1674 /* The following is *not* true: (ben)
1675
1676 ungcpro, restoring catchlist and condition_handlers are actually
1677 redundant since unbind_to now restores them. But it looks funny not to
1678 have this code here, and it doesn't cost anything, so I'm leaving it.*/
1679 UNGCPRO;
1680 catchlist = c.next;
1681 Vcondition_handlers = XCDR (c.tag);
1682
1683 return (unbind_to (speccount, c.val));
1684 }
1685
1686 static Lisp_Object
1687 run_condition_case_handlers (Lisp_Object val, Lisp_Object var)
1688 {
1689 /* This function can GC */
1690 #if 0 /* FSFmacs */
1691 if (!NILP (h.var))
1692 specbind (h.var, c.val);
1693 val = Fprogn (Fcdr (h.chosen_clause));
1694
1695 /* Note that this just undoes the binding of h.var; whoever
1696 longjumped to us unwound the stack to c.pdlcount before
1697 throwing. */
1698 unbind_to (c.pdlcount, Qnil);
1699 return val;
1700 #else
1701 int speccount;
1702
1703 if (NILP (var))
1704 return (Fprogn (Fcdr (val))); /* tailcall */
1705
1706 speccount = specpdl_depth_counter;
1707 specbind (var, Fcar (val));
1708 val = Fprogn (Fcdr (val));
1709 return unbind_to (speccount, val);
1710 #endif
1711 }
1712
1713 /* Here for bytecode to call non-consfully. This is exactly like
1714 condition-case except that it takes three arguments rather
1715 than a single list of arguments. */
1716 Lisp_Object
1717 Fcondition_case_3 (Lisp_Object bodyform,
1718 Lisp_Object var, Lisp_Object handlers)
1719 {
1720 /* This function can GC */
1721 Lisp_Object val;
1722
1723 CHECK_SYMBOL (var);
1724
1725 for (val = handlers; ! NILP (val); val = Fcdr (val))
1726 {
1727 Lisp_Object tem;
1728 tem = Fcar (val);
1729 if ((!NILP (tem))
1730 && (!CONSP (tem)
1731 || (!SYMBOLP (XCAR (tem)) && !CONSP (XCAR (tem)))))
1732 signal_simple_error ("Invalid condition handler", tem);
1733 }
1734
1735 return condition_case_1 (handlers,
1736 Feval, bodyform,
1737 run_condition_case_handlers,
1738 var);
1739 }
1740
1741 DEFUN ("condition-case", Fcondition_case, Scondition_case, 2, UNEVALLED, 0 /*
1742 Regain control when an error is signalled.
1743 Usage looks like (condition-case VAR BODYFORM HANDLERS...).
1744 executes BODYFORM and returns its value if no error happens.
1745 Each element of HANDLERS looks like (CONDITION-NAME BODY...)
1746 where the BODY is made of Lisp expressions.
1747
1748 A handler is applicable to an error if CONDITION-NAME is one of the
1749 error's condition names. If an error happens, the first applicable
1750 handler is run. As a special case, a CONDITION-NAME of t matches
1751 all errors, even those without the `error' condition name on them
1752 (e.g. `quit').
1753
1754 The car of a handler may be a list of condition names
1755 instead of a single condition name.
1756
1757 When a handler handles an error,
1758 control returns to the condition-case and the handler BODY... is executed
1759 with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
1760 VAR may be nil; then you do not get access to the signal information.
1761
1762 The value of the last BODY form is returned from the condition-case.
1763 See also the function `signal' for more info.
1764
1765 Note that at the time the condition handler is invoked, the Lisp stack
1766 and the current catches, condition-cases, and bindings have all been
1767 popped back to the state they were in just before the call to
1768 `condition-case'. This means that resignalling the error from
1769 within the handler will not result in an infinite loop.
1770
1771 If you want to establish an error handler that is called with the
1772 Lisp stack, bindings, etc. as they were when `signal' was called,
1773 rather than when the handler was set, use `call-with-condition-handler'.
1774 */ )
1775 (args)
1776 Lisp_Object args;
1777 {
1778 /* This function can GC */
1779 return Fcondition_case_3 (Fcar (Fcdr (args)),
1780 Fcar (args),
1781 Fcdr (Fcdr (args)));
1782 }
1783
1784 DEFUN ("call-with-condition-handler",
1785 Fcall_with_condition_handler,
1786 Scall_with_condition_handler, 2, MANY, 0 /*
1787 Regain control when an error is signalled, without popping the stack.
1788 Usage looks like (call-with-condition-handler HANDLER FUNCTION &rest ARGS).
1789 This function is similar to `condition-case', but the handler is invoked
1790 with the same environment (Lisp stack, bindings, catches, condition-cases)
1791 that was current when `signal' was called, rather than when the handler
1792 was established.
1793
1794 HANDLER should be a function of one argument, which is a cons of the args
1795 (SIG . DATA) that were passed to `signal'. It is invoked whenever
1796 `signal' is called (this differs from `condition-case', which allows
1797 you to specify which errors are trapped). If the handler function
1798 returns, `signal' continues as if the handler were never invoked.
1799 (It continues to look for handlers established earlier than this one,
1800 and invokes the standard error-handler if none is found.)
1801 */ )
1802 (nargs, args) /* Note! Args side-effected! */
1803 int nargs;
1804 Lisp_Object *args;
1805 {
1806 /* This function can GC */
1807 int speccount = specpdl_depth_counter;
1808 Lisp_Object tem;
1809
1810 /* #### If there were a way to check that args[0] were a function
1811 which accepted one arg, that should be done here ... */
1812
1813 /* (handler-fun . handler-args) */
1814 tem = noseeum_cons (list1 (args[0]), Vcondition_handlers);
1815 record_unwind_protect (condition_bind_unwind, tem);
1816 Vcondition_handlers = tem;
1817
1818 /* Caller should have GC-protected args */
1819 tem = Ffuncall (nargs - 1, args + 1);
1820 return (unbind_to (speccount, tem));
1821 }
1822
1823 static int
1824 condition_type_p (Lisp_Object type, Lisp_Object conditions)
1825 {
1826 if (EQ (type, Qt))
1827 /* (condition-case c # (t c)) catches -all- signals
1828 * Use with caution! */
1829 return (1);
1830 else
1831 {
1832 if (SYMBOLP (type))
1833 {
1834 return (!NILP (Fmemq (type, conditions)));
1835 }
1836 else if (CONSP (type))
1837 {
1838 while (CONSP (type))
1839 {
1840 if (!NILP (Fmemq (Fcar (type), conditions)))
1841 return 1;
1842 type = XCDR (type);
1843 }
1844 return 0;
1845 }
1846 else
1847 return 0;
1848 }
1849 }
1850
1851 static Lisp_Object
1852 return_from_signal (Lisp_Object value)
1853 {
1854 #if 1 /* RMS Claims: */
1855 /* Most callers are not prepared to handle gc if this
1856 returns. So, since this feature is not very useful,
1857 take it out. */
1858 /* Have called debugger; return value to signaller */
1859 return (value);
1860 #else /* But the reality is that that stinks, because: */
1861 /* GACK!!! Really want some way for debug-on-quit errors
1862 to be continuable!! */
1863 error ("Returning a value from an error is no longer supported");
1864 #endif
1865 }
1866
1867 extern int in_display;
1868 extern int gc_in_progress;
1869
1870
1871 /****************** the workhorse error-signaling function ******************/
1872
1873 /* #### This function has not been synched with FSF. It diverges
1874 significantly. */
1875
1876 static Lisp_Object
1877 signal_1 (Lisp_Object sig, Lisp_Object data)
1878 {
1879 /* This function can GC */
1880 struct gcpro gcpro1, gcpro2;
1881 Lisp_Object conditions;
1882 Lisp_Object handlers;
1883 /* signal_call_debugger() could get called more than once
1884 (once when a call-with-condition-handler is about to
1885 be dealt with, and another when a condition-case handler
1886 is about to be invoked). So make sure the debugger and/or
1887 stack trace aren't done more than once. */
1888 int stack_trace_displayed = 0;
1889 int debugger_entered = 0;
1890 GCPRO2 (conditions, handlers);
1891
1892 if (!initialized)
1893 {
1894 /* who knows how much has been initialized? Safest bet is
1895 just to bomb out immediately. */
1896 fprintf (stderr, "Error before initialization is complete!\n");
1897 abort ();
1898 }
1899
1900 if (gc_in_progress || in_display)
1901 /* This is one of many reasons why you can't run lisp code from redisplay.
1902 There is no sensible way to handle errors there. */
1903 abort ();
1904
1905 conditions = Fget (sig, Qerror_conditions, Qnil);
1906
1907 for (handlers = Vcondition_handlers;
1908 CONSP (handlers);
1909 handlers = XCDR (handlers))
1910 {
1911 Lisp_Object handler_fun = XCAR (XCAR (handlers));
1912 Lisp_Object handler_data = XCDR (XCAR (handlers));
1913 Lisp_Object outer_handlers = XCDR (handlers);
1914
1915 if (!UNBOUNDP (handler_fun))
1916 {
1917 /* call-with-condition-handler */
1918 Lisp_Object tem;
1919 Lisp_Object all_handlers = Vcondition_handlers;
1920 struct gcpro ngcpro1;
1921 NGCPRO1 (all_handlers);
1922 Vcondition_handlers = outer_handlers;
1923
1924 tem = signal_call_debugger (conditions, sig, data,
1925 outer_handlers, 1,
1926 &stack_trace_displayed,
1927 &debugger_entered);
1928 if (!UNBOUNDP (tem))
1929 RETURN_NUNGCPRO (return_from_signal (tem));
1930
1931 tem = Fcons (sig, data);
1932 if (NILP (handler_data))
1933 tem = call1 (handler_fun, tem);
1934 else
1935 {
1936 /* (This code won't be used (for now?).) */
1937 struct gcpro nngcpro1;
1938 Lisp_Object args[3];
1939 NNGCPRO1 (args[0]);
1940 nngcpro1.nvars = 3;
1941 args[0] = handler_fun;
1942 args[1] = tem;
1943 args[2] = handler_data;
1944 nngcpro1.var = args;
1945 tem = Fapply (3, args);
1946 NNUNGCPRO;
1947 }
1948 NUNGCPRO;
1949 #if 0
1950 if (!EQ (tem, Qsignal))
1951 return (return_from_signal (tem));
1952 #endif
1953 /* If handler didn't throw, try another handler */
1954 Vcondition_handlers = all_handlers;
1955 }
1956
1957 /* It's a condition-case handler */
1958
1959 /* t is used by handlers for all conditions, set up by C code.
1960 * debugger is not called even if debug_on_error */
1961 else if (EQ (handler_data, Qt))
1962 {
1963 UNGCPRO;
1964 return (Fthrow (handlers, Fcons (sig, data)));
1965 }
1966 /* `error' is used similarly to the way `t' is used, but in
1967 addition it invokes the debugger if debug_on_error.
1968 This is normally used for the outer command-loop error
1969 handler. */
1970 else if (EQ (handler_data, Qerror))
1971 {
1972 Lisp_Object tem = signal_call_debugger (conditions, sig, data,
1973 outer_handlers, 0,
1974 &stack_trace_displayed,
1975 &debugger_entered);
1976
1977 UNGCPRO;
1978 if (!UNBOUNDP (tem))
1979 return (return_from_signal (tem));
1980
1981 tem = Fcons (sig, data);
1982 return (Fthrow (handlers, tem));
1983 }
1984 else
1985 {
1986 /* handler established by real (Lisp) condition-case */
1987 Lisp_Object h;
1988
1989 for (h = handler_data; CONSP (h); h = Fcdr (h))
1990 {
1991 Lisp_Object clause = Fcar (h);
1992 Lisp_Object tem = Fcar (clause);
1993
1994 if (condition_type_p (tem, conditions))
1995 {
1996 tem = signal_call_debugger (conditions, sig, data,
1997 outer_handlers, 1,
1998 &stack_trace_displayed,
1999 &debugger_entered);
2000 UNGCPRO;
2001 if (!UNBOUNDP (tem))
2002 return (return_from_signal (tem));
2003
2004 /* Doesn't return */
2005 tem = Fcons (Fcons (sig, data), Fcdr (clause));
2006 return (Fthrow (handlers, tem));
2007 }
2008 }
2009 }
2010 }
2011
2012 /* If no handler is present now, try to run the debugger,
2013 and if that fails, throw to top level.
2014
2015 #### The only time that no handler is present is during
2016 temacs or perhaps very early in XEmacs. In both cases,
2017 there is no 'top-level catch. (That's why the
2018 "bomb-out" hack was added.)
2019
2020 #### Fix this horrifitude!
2021 */
2022 signal_call_debugger (conditions, sig, data, Qnil, 0,
2023 &stack_trace_displayed,
2024 &debugger_entered);
2025 UNGCPRO;
2026 throw_or_bomb_out (Qtop_level, Qt, 1, sig, data); /* Doesn't return */
2027 return Qnil;
2028 }
2029
2030
2031 /****************** Error functions class 1 ******************/
2032
2033 /* Class 1: General functions that signal an error.
2034 These functions take an error type and a list of associated error
2035 data. */
2036
2037 /* The simplest external error function: it would be called
2038 signal_continuable_error() in the terminology below, but it's
2039 Lisp-callable. */
2040
2041 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0 /*
2042 Signal a continuable error. Args are ERROR-SYMBOL, and associated DATA.
2043 An error symbol is a symbol defined using `define-error'.
2044 DATA should be a list. Its elements are printed as part of the error message.
2045 If the signal is handled, DATA is made available to the handler.
2046 See also the function `signal-error', and the functions to handle errors:
2047 `condition-case' and `call-with-condition-handler'.
2048
2049 Note that this function can return, if the debugger is invoked and the
2050 user invokes the "return from signal" option.
2051 */ )
2052 (error_symbol, data)
2053 Lisp_Object error_symbol, data;
2054 {
2055 /* Fsignal() is one of these functions that's called all the time
2056 with newly-created Lisp objects. We allow this; but we must GC-
2057 protect the objects because all sorts of weird stuff could
2058 happen. */
2059
2060 struct gcpro gcpro1;
2061
2062 GCPRO1 (data);
2063 if (!NILP (Vcurrent_error_state))
2064 {
2065 if (!NILP (Vcurrent_warning_class))
2066 warn_when_safe_lispobj (Vcurrent_warning_class, Qwarning,
2067 Fcons (error_symbol, data));
2068 Fthrow (Qunbound_suspended_errors_tag, Qnil);
2069 abort (); /* Better not get here! */
2070 }
2071 RETURN_UNGCPRO (signal_1 (error_symbol, data));
2072 }
2073
2074 /* Signal a non-continuable error. */
2075
2076 DOESNT_RETURN
2077 signal_error (Lisp_Object sig, Lisp_Object data)
2078 {
2079 for (;;)
2080 Fsignal (sig, data);
2081 }
2082
2083 static Lisp_Object
2084 call_with_suspended_errors_1 (Lisp_Object opaque_arg)
2085 {
2086 Lisp_Object *kludgy_args = (Lisp_Object *) get_opaque_ptr (opaque_arg);
2087 return (primitive_funcall
2088 ((Lisp_Object (*)()) get_opaque_ptr (kludgy_args[0]),
2089 XINT (kludgy_args[1]), kludgy_args + 2));
2090 }
2091
2092 static Lisp_Object
2093 restore_current_warning_class (Lisp_Object warning_class)
2094 {
2095 Vcurrent_warning_class = warning_class;
2096 return Qnil;
2097 }
2098
2099 static Lisp_Object
2100 restore_current_error_state (Lisp_Object error_state)
2101 {
2102 Vcurrent_error_state = error_state;
2103 return Qnil;
2104 }
2105
2106 /* Many functions would like to do one of three things if an error
2107 occurs:
2108
2109 (1) signal the error, as usual.
2110 (2) silently fail and return some error value.
2111 (3) do as (2) but issue a warning in the process.
2112
2113 Currently there's lots of stuff that passes an Error_behavior
2114 value and calls maybe_signal_error() and other such functions.
2115 This approach is inherently error-prone and broken. A much
2116 more robust and easier approach is to use call_with_suspended_errors().
2117 Wrap this around any function in which you might want errors
2118 to not be errors.
2119 */
2120
2121 Lisp_Object
2122 call_with_suspended_errors (Lisp_Object (*fun)(), Lisp_Object retval,
2123 Lisp_Object class, Error_behavior errb,
2124 int nargs, ...)
2125 {
2126 va_list vargs;
2127 int speccount;
2128 Lisp_Object kludgy_args[22];
2129 Lisp_Object *args = kludgy_args + 2;
2130 int i;
2131 Lisp_Object no_error;
2132
2133 assert (SYMBOLP (class)); /* sanity-check */
2134 assert (!NILP (class));
2135 assert (nargs >= 0 && nargs < 20);
2136
2137 /* ERROR_ME means don't trap errors. (However, if errors are
2138 already trapped, we leave them trapped.)
2139
2140 Otherwise, we trap errors, and trap warnings if ERROR_ME_WARN.
2141
2142 If ERROR_ME_NOT, it causes no warnings even if warnings
2143 were previously enabled. However, we never change the
2144 warning class from one to another. */
2145 if (!ERRB_EQ (errb, ERROR_ME))
2146 {
2147 if (ERRB_EQ (errb, ERROR_ME_NOT)) /* person wants no warnings */
2148 class = Qnil;
2149 errb = ERROR_ME_NOT;
2150 no_error = Qt;
2151 }
2152 else
2153 no_error = Qnil;
2154
2155 va_start (vargs, nargs);
2156 for (i = 0; i < nargs; i++)
2157 args[i] = va_arg (vargs, Lisp_Object);
2158 va_end (vargs);
2159
2160 /* If error-checking is not disabled, just call the function.
2161 It's important not to override disabled error-checking with
2162 enabled error-checking. */
2163
2164 if (ERRB_EQ (errb, ERROR_ME))
2165 return primitive_funcall (fun, nargs, args);
2166
2167 speccount = specpdl_depth ();
2168 if (NILP (class) || NILP (Vcurrent_warning_class))
2169 {
2170 /* If we're currently calling for no warnings, then make it so.
2171 If we're currently calling for warnings and we weren't
2172 previously, then set our warning class; otherwise, leave
2173 the existing one alone. */
2174 record_unwind_protect (restore_current_warning_class,
2175 Vcurrent_warning_class);
2176 Vcurrent_warning_class = class;
2177 }
2178 if (!EQ (Vcurrent_error_state, no_error))
2179 {
2180 record_unwind_protect (restore_current_error_state,
2181 Vcurrent_error_state);
2182 Vcurrent_error_state = no_error;
2183 }
2184
2185 {
2186 int threw;
2187 Lisp_Object the_retval;
2188 Lisp_Object opaque1 = make_opaque_ptr (kludgy_args);
2189 Lisp_Object opaque2 = make_opaque_ptr ((void *) fun);
2190 struct gcpro gcpro1, gcpro2;
2191
2192 GCPRO2 (opaque1, opaque2);
2193 kludgy_args[0] = opaque2;
2194 kludgy_args[1] = make_int (nargs);
2195 the_retval = internal_catch (Qunbound_suspended_errors_tag,
2196 call_with_suspended_errors_1,
2197 opaque1, &threw);
2198 free_opaque_ptr (opaque1);
2199 free_opaque_ptr (opaque2);
2200 UNGCPRO;
2201 /* Use the returned value except in non-local exit, when
2202 RETVAL applies. */
2203 if (!threw)
2204 retval = the_retval;
2205 return unbind_to (speccount, retval);
2206 }
2207 }
2208
2209 /* Signal a non-continuable error or display a warning or do nothing,
2210 according to ERRB. CLASS is the class of warning and should
2211 refer to what sort of operation is being done (e.g. Qtoolbar,
2212 Qresource, etc.). */
2213
2214 void
2215 maybe_signal_error (Lisp_Object sig, Lisp_Object data, Lisp_Object class,
2216 Error_behavior errb)
2217 {
2218 if (ERRB_EQ (errb, ERROR_ME_NOT))
2219 return;
2220 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2221 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2222 else
2223 for (;;)
2224 Fsignal (sig, data);
2225 }
2226
2227 /* Signal a continuable error or display a warning or do nothing,
2228 according to ERRB. */
2229
2230 Lisp_Object
2231 maybe_signal_continuable_error (Lisp_Object sig, Lisp_Object data,
2232 Lisp_Object class, Error_behavior errb)
2233 {
2234 if (ERRB_EQ (errb, ERROR_ME_NOT))
2235 return Qnil;
2236 else if (ERRB_EQ (errb, ERROR_ME_WARN))
2237 {
2238 warn_when_safe_lispobj (class, Qwarning, Fcons (sig, data));
2239 return Qnil;
2240 }
2241 else
2242 return Fsignal (sig, data);
2243 }
2244
2245
2246 /****************** Error functions class 2 ******************/
2247
2248 /* Class 2: Printf-like functions that signal an error.
2249 These functions signal an error of type Qerror, whose data
2250 is a single string, created using the arguments. */
2251
2252 /* dump an error message; called like printf */
2253
2254 DOESNT_RETURN
2255 error (CONST char *fmt, ...)
2256 {
2257 Lisp_Object obj;
2258 va_list args;
2259
2260 va_start (args, fmt);
2261 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2262 args);
2263 va_end (args);
2264
2265 /* Fsignal GC-protects its args */
2266 signal_error (Qerror, list1 (obj));
2267 }
2268
2269 void
2270 maybe_error (Lisp_Object class, Error_behavior errb, CONST char *fmt, ...)
2271 {
2272 Lisp_Object obj;
2273 va_list args;
2274
2275 /* Optimization: */
2276 if (ERRB_EQ (errb, ERROR_ME_NOT))
2277 return;
2278
2279 va_start (args, fmt);
2280 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2281 args);
2282 va_end (args);
2283
2284 /* Fsignal GC-protects its args */
2285 maybe_signal_error (Qerror, list1 (obj), class, errb);
2286 }
2287
2288 Lisp_Object
2289 continuable_error (CONST char *fmt, ...)
2290 {
2291 Lisp_Object obj;
2292 va_list args;
2293
2294 va_start (args, fmt);
2295 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2296 args);
2297 va_end (args);
2298
2299 /* Fsignal GC-protects its args */
2300 return Fsignal (Qerror, list1 (obj));
2301 }
2302
2303 Lisp_Object
2304 maybe_continuable_error (Lisp_Object class, Error_behavior errb,
2305 CONST char *fmt, ...)
2306 {
2307 Lisp_Object obj;
2308 va_list args;
2309
2310 /* Optimization: */
2311 if (ERRB_EQ (errb, ERROR_ME_NOT))
2312 return Qnil;
2313
2314 va_start (args, fmt);
2315 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2316 args);
2317 va_end (args);
2318
2319 /* Fsignal GC-protects its args */
2320 return maybe_signal_continuable_error (Qerror, list1 (obj), class, errb);
2321 }
2322
2323
2324 /****************** Error functions class 3 ******************/
2325
2326 /* Class 3: Signal an error with a string and an associated object.
2327 These functions signal an error of type Qerror, whose data
2328 is two objects, a string and a related Lisp object (usually the object
2329 where the error is occurring). */
2330
2331 DOESNT_RETURN
2332 signal_simple_error (CONST char *reason, Lisp_Object frob)
2333 {
2334 signal_error (Qerror, list2 (build_translated_string (reason), frob));
2335 }
2336
2337 void
2338 maybe_signal_simple_error (CONST char *reason, Lisp_Object frob,
2339 Lisp_Object class, Error_behavior errb)
2340 {
2341 /* Optimization: */
2342 if (ERRB_EQ (errb, ERROR_ME_NOT))
2343 return;
2344 maybe_signal_error (Qerror, list2 (build_translated_string (reason), frob),
2345 class, errb);
2346 }
2347
2348 Lisp_Object
2349 signal_simple_continuable_error (CONST char *reason, Lisp_Object frob)
2350 {
2351 return Fsignal (Qerror, list2 (build_translated_string (reason), frob));
2352 }
2353
2354 Lisp_Object
2355 maybe_signal_simple_continuable_error (CONST char *reason, Lisp_Object frob,
2356 Lisp_Object class, Error_behavior errb)
2357 {
2358 /* Optimization: */
2359 if (ERRB_EQ (errb, ERROR_ME_NOT))
2360 return Qnil;
2361 return maybe_signal_continuable_error
2362 (Qerror, list2 (build_translated_string (reason),
2363 frob), class, errb);
2364 }
2365
2366
2367 /****************** Error functions class 4 ******************/
2368
2369 /* Class 4: Printf-like functions that signal an error.
2370 These functions signal an error of type Qerror, whose data
2371 is a two objects, a string (created using the arguments) and a
2372 Lisp object.
2373 */
2374
2375 DOESNT_RETURN
2376 error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2377 {
2378 Lisp_Object obj;
2379 va_list args;
2380
2381 va_start (args, fmt);
2382 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2383 args);
2384 va_end (args);
2385
2386 /* Fsignal GC-protects its args */
2387 signal_error (Qerror, list2 (obj, frob));
2388 }
2389
2390 void
2391 maybe_error_with_frob (Lisp_Object frob, Lisp_Object class,
2392 Error_behavior errb, CONST char *fmt, ...)
2393 {
2394 Lisp_Object obj;
2395 va_list args;
2396
2397 /* Optimization: */
2398 if (ERRB_EQ (errb, ERROR_ME_NOT))
2399 return;
2400
2401 va_start (args, fmt);
2402 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2403 args);
2404 va_end (args);
2405
2406 /* Fsignal GC-protects its args */
2407 maybe_signal_error (Qerror, list2 (obj, frob), class, errb);
2408 }
2409
2410 Lisp_Object
2411 continuable_error_with_frob (Lisp_Object frob, CONST char *fmt, ...)
2412 {
2413 Lisp_Object obj;
2414 va_list args;
2415
2416 va_start (args, fmt);
2417 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2418 args);
2419 va_end (args);
2420
2421 /* Fsignal GC-protects its args */
2422 return Fsignal (Qerror, list2 (obj, frob));
2423 }
2424
2425 Lisp_Object
2426 maybe_continuable_error_with_frob (Lisp_Object frob, Lisp_Object class,
2427 Error_behavior errb, CONST char *fmt, ...)
2428 {
2429 Lisp_Object obj;
2430 va_list args;
2431
2432 /* Optimization: */
2433 if (ERRB_EQ (errb, ERROR_ME_NOT))
2434 return Qnil;
2435
2436 va_start (args, fmt);
2437 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt), Qnil, -1,
2438 args);
2439 va_end (args);
2440
2441 /* Fsignal GC-protects its args */
2442 return maybe_signal_continuable_error (Qerror, list2 (obj, frob),
2443 class, errb);
2444 }
2445
2446
2447 /****************** Error functions class 5 ******************/
2448
2449 /* Class 5: Signal an error with a string and two associated objects.
2450 These functions signal an error of type Qerror, whose data
2451 is three objects, a string and two related Lisp objects. */
2452
2453 DOESNT_RETURN
2454 signal_simple_error_2 (CONST char *reason,
2455 Lisp_Object frob0, Lisp_Object frob1)
2456 {
2457 signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2458 frob1));
2459 }
2460
2461 void
2462 maybe_signal_simple_error_2 (CONST char *reason, Lisp_Object frob0,
2463 Lisp_Object frob1, Lisp_Object class,
2464 Error_behavior errb)
2465 {
2466 /* Optimization: */
2467 if (ERRB_EQ (errb, ERROR_ME_NOT))
2468 return;
2469 maybe_signal_error (Qerror, list3 (build_translated_string (reason), frob0,
2470 frob1), class, errb);
2471 }
2472
2473
2474 Lisp_Object
2475 signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2476 Lisp_Object frob1)
2477 {
2478 return Fsignal (Qerror, list3 (build_translated_string (reason), frob0,
2479 frob1));
2480 }
2481
2482 Lisp_Object
2483 maybe_signal_simple_continuable_error_2 (CONST char *reason, Lisp_Object frob0,
2484 Lisp_Object frob1, Lisp_Object class,
2485 Error_behavior errb)
2486 {
2487 /* Optimization: */
2488 if (ERRB_EQ (errb, ERROR_ME_NOT))
2489 return Qnil;
2490 return maybe_signal_continuable_error
2491 (Qerror, list3 (build_translated_string (reason), frob0,
2492 frob1),
2493 class, errb);
2494 }
2495
2496
2497 /* This is what the QUIT macro calls to signal a quit */
2498 void
2499 signal_quit (void)
2500 {
2501 /* This function can GC */
2502 if (EQ (Vquit_flag, Qcritical))
2503 debug_on_quit |= 2; /* set critical bit. */
2504 Vquit_flag = Qnil;
2505 /* note that this is continuable. */
2506 Fsignal (Qquit, Qnil);
2507 }
2508
2509
2510 /**********************************************************************/
2511 /* User commands */
2512 /**********************************************************************/
2513
2514 DEFUN ("commandp", Fcommandp, Scommandp, 1, 1, 0 /*
2515 T if FUNCTION makes provisions for interactive calling.
2516 This means it contains a description for how to read arguments to give it.
2517 The value is nil for an invalid function or a symbol with no function
2518 definition.
2519
2520 Interactively callable functions include
2521
2522 -- strings and vectors (treated as keyboard macros)
2523 -- lambda-expressions that contain a top-level call to `interactive'
2524 -- autoload definitions made by `autoload' with non-nil fourth argument
2525 (i.e. the interactive flag)
2526 -- compiled-function objects with a non-nil `compiled-function-interactive'
2527 value
2528 -- subrs (built-in functions) that are interactively callable
2529
2530 Also, a symbol satisfies `commandp' if its function definition does so.
2531 */ )
2532 (function)
2533 Lisp_Object function;
2534 {
2535 REGISTER Lisp_Object fun;
2536 REGISTER Lisp_Object funcar;
2537
2538 fun = function;
2539
2540 fun = indirect_function (fun, 0);
2541 if (UNBOUNDP (fun))
2542 return Qnil;
2543
2544 /* Emacs primitives are interactive if their DEFUN specifies an
2545 interactive spec. */
2546 if (SUBRP (fun))
2547 {
2548 if (XSUBR (fun)->prompt)
2549 return Qt;
2550 else
2551 return Qnil;
2552 }
2553
2554 else if (COMPILED_FUNCTIONP (fun))
2555 {
2556 return (((XCOMPILED_FUNCTION (fun)->flags.interactivep) ? Qt : Qnil));
2557 }
2558
2559 /* Strings and vectors are keyboard macros. */
2560 if (VECTORP (fun) || STRINGP (fun))
2561 return Qt;
2562
2563 /* Lists may represent commands. */
2564 if (!CONSP (fun))
2565 return Qnil;
2566 funcar = Fcar (fun);
2567 if (!SYMBOLP (funcar))
2568 return Fsignal (Qinvalid_function, list1 (fun));
2569 if (EQ (funcar, Qlambda))
2570 return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
2571 #ifdef MOCKLISP_SUPPORT
2572 if (EQ (funcar, Qmocklisp))
2573 return Qt; /* All mocklisp functions can be called interactively */
2574 #endif
2575 if (EQ (funcar, Qautoload))
2576 return Fcar (Fcdr (Fcdr (Fcdr (fun))));
2577 else
2578 return Qnil;
2579 }
2580
2581 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 3, 0 /*
2582 Execute CMD as an editor command.
2583 CMD must be an object that satisfies the `commandp' predicate.
2584 Optional second arg RECORD-FLAG is as in `call-interactively'.
2585 The argument KEYS specifies the value to use instead of (this-command-keys)
2586 when reading the arguments.
2587 */ )
2588 (cmd, record, keys)
2589 Lisp_Object cmd, record, keys;
2590 {
2591 /* This function can GC */
2592 Lisp_Object prefixarg;
2593 Lisp_Object final = cmd;
2594 struct backtrace backtrace;
2595 struct console *con = XCONSOLE (Vselected_console);
2596
2597 prefixarg = con->prefix_arg;
2598 con->prefix_arg = Qnil;
2599 Vcurrent_prefix_arg = prefixarg;
2600 debug_on_next_call = 0; /* #### from FSFmacs; correct? */
2601
2602 if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil)))
2603 return run_hook (Vdisabled_command_hook);
2604
2605 for (;;)
2606 {
2607 final = indirect_function (cmd, 1);
2608 if (CONSP (final) && EQ (Fcar (final), Qautoload))
2609 do_autoload (final, cmd);
2610 else
2611 break;
2612 }
2613
2614 if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final))
2615 {
2616 #ifdef EMACS_BTL
2617 backtrace.id_number = 0;
2618 #endif
2619 backtrace.next = backtrace_list;
2620 backtrace_list = &backtrace;
2621 backtrace.function = &Qcall_interactively;
2622 backtrace.args = &cmd;
2623 backtrace.nargs = 1;
2624 backtrace.evalargs = 0;
2625 backtrace.pdlcount = specpdl_depth ();
2626 backtrace.debug_on_exit = 0;
2627
2628 final = Fcall_interactively (cmd, record, keys);
2629
2630 backtrace_list = backtrace.next;
2631 return (final);
2632 }
2633 else if (STRINGP (final) || VECTORP (final))
2634 {
2635 return Fexecute_kbd_macro (final, prefixarg);
2636 }
2637 else
2638 {
2639 Fsignal (Qwrong_type_argument,
2640 Fcons (Qcommandp,
2641 ((EQ (cmd, final))
2642 ? list1 (cmd)
2643 : list2 (cmd, final))));
2644 return Qnil;
2645 }
2646 }
2647
2648 DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0 /*
2649 Return t if function in which this appears was called interactively.
2650 This means that the function was called with call-interactively (which
2651 includes being called as the binding of a key)
2652 and input is currently coming from the keyboard (not in keyboard macro).
2653 */ )
2654 ()
2655 {
2656 REGISTER struct backtrace *btp;
2657 REGISTER Lisp_Object fun;
2658
2659 if (!INTERACTIVE)
2660 return Qnil;
2661
2662 /* Unless the object was compiled, skip the frame of interactive-p itself
2663 (if interpreted) or the frame of byte-code (if called from a compiled
2664 function). Note that *btp->function may be a symbol pointing at a
2665 compiled function. */
2666 btp = backtrace_list;
2667
2668 #if 0 /* FSFmacs */
2669
2670 /* #### FSFmacs does the following instead. I can't figure
2671 out which one is more correct. */
2672 /* If this isn't a byte-compiled function, there may be a frame at
2673 the top for Finteractive_p itself. If so, skip it. */
2674 fun = Findirect_function (*btp->function);
2675 if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
2676 btp = btp->next;
2677
2678 /* If we're running an Emacs 18-style byte-compiled function, there
2679 may be a frame for Fbyte_code. Now, given the strictest
2680 definition, this function isn't really being called
2681 interactively, but because that's the way Emacs 18 always builds
2682 byte-compiled functions, we'll accept it for now. */
2683 if (EQ (*btp->function, Qbyte_code))
2684 btp = btp->next;
2685
2686 /* If this isn't a byte-compiled function, then we may now be
2687 looking at several frames for special forms. Skip past them. */
2688 while (btp &&
2689 btp->nargs == UNEVALLED)
2690 btp = btp->next;
2691
2692 #else
2693
2694 if (! (COMPILED_FUNCTIONP (Findirect_function (*btp->function))))
2695 btp = btp->next;
2696 for (;
2697 btp && (btp->nargs == UNEVALLED
2698 || EQ (*btp->function, Qbyte_code));
2699 btp = btp->next)
2700 {}
2701 /* btp now points at the frame of the innermost function
2702 that DOES eval its args.
2703 If it is a built-in function (such as load or eval-region)
2704 return nil. */
2705 /* Beats me why this is necessary, but it is */
2706 if (btp && EQ (*btp->function, Qcall_interactively))
2707 return Qt;
2708
2709 #endif
2710
2711 fun = Findirect_function (*btp->function);
2712 if (SUBRP (fun))
2713 return Qnil;
2714 /* btp points to the frame of a Lisp function that called interactive-p.
2715 Return t if that function was called interactively. */
2716 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
2717 return Qt;
2718 return Qnil;
2719 }
2720
2721
2722 /**********************************************************************/
2723 /* Autoloading */
2724 /**********************************************************************/
2725
2726 DEFUN ("autoload", Fautoload, Sautoload, 2, 5, 0 /*
2727 Define FUNCTION to autoload from FILE.
2728 FUNCTION is a symbol; FILE is a file name string to pass to `load'.
2729 Third arg DOCSTRING is documentation for the function.
2730 Fourth arg INTERACTIVE if non-nil says function can be called interactively.
2731 Fifth arg TYPE indicates the type of the object:
2732 nil or omitted says FUNCTION is a function,
2733 `keymap' says FUNCTION is really a keymap, and
2734 `macro' or t says FUNCTION is really a macro.
2735 Third through fifth args give info about the real definition.
2736 They default to nil.
2737 If FUNCTION is already defined other than as an autoload,
2738 this does nothing and returns nil.
2739 */ )
2740 (function, file, docstring, interactive, type)
2741 Lisp_Object function, file, docstring, interactive, type;
2742 {
2743 /* This function can GC */
2744 CHECK_SYMBOL (function);
2745 CHECK_STRING (file);
2746
2747 /* If function is defined and not as an autoload, don't override */
2748 if (!UNBOUNDP (XSYMBOL (function)->function)
2749 && !(CONSP (XSYMBOL (function)->function)
2750 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
2751 return Qnil;
2752
2753 if (purify_flag)
2754 {
2755 /* Attempt to avoid consing identical (string=) pure strings. */
2756 file = Fsymbol_name (Fintern (file, Qnil));
2757 }
2758
2759 return Ffset (function,
2760 Fpurecopy (Fcons (Qautoload, list4 (file,
2761 docstring,
2762 interactive,
2763 type))));
2764 }
2765
2766 Lisp_Object
2767 un_autoload (Lisp_Object oldqueue)
2768 {
2769 /* This function can GC */
2770 REGISTER Lisp_Object queue, first, second;
2771
2772 /* Queue to unwind is current value of Vautoload_queue.
2773 oldqueue is the shadowed value to leave in Vautoload_queue. */
2774 queue = Vautoload_queue;
2775 Vautoload_queue = oldqueue;
2776 while (CONSP (queue))
2777 {
2778 first = Fcar (queue);
2779 second = Fcdr (first);
2780 first = Fcar (first);
2781 if (NILP (second))
2782 Vfeatures = first;
2783 else
2784 Ffset (first, second);
2785 queue = Fcdr (queue);
2786 }
2787 return Qnil;
2788 }
2789
2790 void
2791 do_autoload (Lisp_Object fundef,
2792 Lisp_Object funname)
2793 {
2794 /* This function can GC */
2795 int speccount = specpdl_depth_counter;
2796 Lisp_Object fun = funname;
2797 struct gcpro gcpro1, gcpro2;
2798
2799 CHECK_SYMBOL (funname);
2800 GCPRO2 (fun, funname);
2801
2802 /* Value saved here is to be restored into Vautoload_queue */
2803 record_unwind_protect (un_autoload, Vautoload_queue);
2804 Vautoload_queue = Qt;
2805 call4 (Qload, Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil,
2806 Qnil);
2807
2808 {
2809 Lisp_Object queue = Vautoload_queue;
2810
2811 /* Save the old autoloads, in case we ever do an unload. */
2812 queue = Vautoload_queue;
2813 while (CONSP (queue))
2814 {
2815 Lisp_Object first = Fcar (queue);
2816 Lisp_Object second = Fcdr (first);
2817
2818 first = Fcar (first);
2819
2820 /* Note: This test is subtle. The cdr of an autoload-queue entry
2821 may be an atom if the autoload entry was generated by a defalias
2822 or fset. */
2823 if (CONSP (second))
2824 Fput (first, Qautoload, (Fcdr (second)));
2825
2826 queue = Fcdr (queue);
2827 }
2828 }
2829
2830 /* Once loading finishes, don't undo it. */
2831 Vautoload_queue = Qt;
2832 unbind_to (speccount, Qnil);
2833
2834 fun = indirect_function (fun, 0);
2835
2836 #if 0 /* FSFmacs */
2837 if (!NILP (Fequal (fun, fundef)))
2838 #else
2839 if (UNBOUNDP (fun)
2840 || (CONSP (fun)
2841 && EQ (XCAR (fun), Qautoload)))
2842 #endif
2843 error ("Autoloading failed to define function %s",
2844 string_data (XSYMBOL (funname)->name));
2845 UNGCPRO;
2846 }
2847
2848
2849 /**********************************************************************/
2850 /* eval, funcall, apply */
2851 /**********************************************************************/
2852
2853 static Lisp_Object funcall_lambda (Lisp_Object fun,
2854 int nargs, Lisp_Object args[]);
2855 static Lisp_Object apply_lambda (Lisp_Object fun,
2856 int nargs, Lisp_Object args);
2857 static Lisp_Object funcall_subr (struct Lisp_Subr *sub, Lisp_Object args[]);
2858
2859 static int in_warnings;
2860
2861 static Lisp_Object
2862 in_warnings_restore (Lisp_Object minimus)
2863 {
2864 in_warnings = 0;
2865 return Qnil;
2866 }
2867
2868
2869 DEFUN ("eval", Feval, Seval, 1, 1, 0 /*
2870 Evaluate FORM and return its value.
2871 */ )
2872 (form)
2873 Lisp_Object form;
2874 {
2875 /* This function can GC */
2876 Lisp_Object fun, val, original_fun, original_args;
2877 int nargs;
2878 struct backtrace backtrace;
2879
2880 /* I think this is a pretty safe place to call Lisp code, don't you? */
2881 while (!in_warnings && !NILP (Vpending_warnings))
2882 {
2883 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2884 int speccount = specpdl_depth ();
2885 Lisp_Object this_warning_cons, this_warning, class, level, messij;
2886
2887 record_unwind_protect (in_warnings_restore, Qnil);
2888 in_warnings = 1;
2889 this_warning_cons = Vpending_warnings;
2890 this_warning = XCAR (this_warning_cons);
2891 /* in case an error occurs in the warn function, at least
2892 it won't happen infinitely */
2893 Vpending_warnings = XCDR (Vpending_warnings);
2894 free_cons (XCONS (this_warning_cons));
2895 class = XCAR (this_warning);
2896 level = XCAR (XCDR (this_warning));
2897 messij = XCAR (XCDR (XCDR (this_warning)));
2898 free_list (this_warning);
2899
2900 if (NILP (Vpending_warnings))
2901 Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary,
2902 but safer */
2903
2904 GCPRO4 (form, class, level, messij);
2905 if (!STRINGP (messij))
2906 messij = Fprin1_to_string (messij, Qnil);
2907 call3 (Qdisplay_warning, class, messij, level);
2908 UNGCPRO;
2909 unbind_to (speccount, Qnil);
2910 }
2911
2912 if (!CONSP (form))
2913 {
2914 if (!SYMBOLP (form))
2915 return form;
2916
2917 val = Fsymbol_value (form);
2918
2919 #ifdef MOCKLISP_SUPPORT
2920 if (!EQ (Vmocklisp_arguments, Qt))
2921 {
2922 if (NILP (val))
2923 val = Qzero;
2924 else if (EQ (val, Qt))
2925 val = make_int (1);
2926 }
2927 #endif
2928 return val;
2929 }
2930
2931 QUIT;
2932 if ((consing_since_gc > gc_cons_threshold) || always_gc)
2933 {
2934 struct gcpro gcpro1;
2935 GCPRO1 (form);
2936 garbage_collect_1 ();
2937 UNGCPRO;
2938 }
2939
2940 if (++lisp_eval_depth > max_lisp_eval_depth)
2941 {
2942 if (max_lisp_eval_depth < 100)
2943 max_lisp_eval_depth = 100;
2944 if (lisp_eval_depth > max_lisp_eval_depth)
2945 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2946 }
2947
2948 original_fun = Fcar (form);
2949 original_args = Fcdr (form);
2950 nargs = XINT (Flength (original_args));
2951
2952 #ifdef EMACS_BTL
2953 backtrace.id_number = 0;
2954 #endif
2955 backtrace.pdlcount = specpdl_depth_counter;
2956 backtrace.next = backtrace_list;
2957 backtrace_list = &backtrace;
2958 backtrace.function = &original_fun; /* This also protects them from gc */
2959 backtrace.args = &original_args;
2960 backtrace.nargs = UNEVALLED;
2961 backtrace.evalargs = 1;
2962 backtrace.debug_on_exit = 0;
2963
2964 if (debug_on_next_call)
2965 do_debug_on_call (Qt);
2966
2967 /* At this point, only original_fun and original_args
2968 have values that will be used below */
2969 retry:
2970 fun = indirect_function (original_fun, 1);
2971
2972 if (SUBRP (fun))
2973 {
2974 struct Lisp_Subr *subr = XSUBR (fun);
2975 int max_args = subr->max_args;
2976 Lisp_Object argvals[SUBR_MAX_ARGS];
2977 Lisp_Object args_left;
2978 REGISTER int i;
2979
2980 args_left = original_args;
2981
2982 if (nargs < subr->min_args
2983 || (max_args >= 0 && max_args < nargs))
2984 {
2985 return Fsignal (Qwrong_number_of_arguments,
2986 list2 (fun, make_int (nargs)));
2987 }
2988
2989 if (max_args == UNEVALLED)
2990 {
2991 backtrace.evalargs = 0;
2992 val = ((subr_function (subr)) (args_left));
2993 }
2994
2995 else if (max_args == MANY)
2996 {
2997 /* Pass a vector of evaluated arguments */
2998 Lisp_Object *vals;
2999 REGISTER int argnum;
3000 struct gcpro gcpro1, gcpro2, gcpro3;
3001
3002 vals = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object));
3003
3004 GCPRO3 (args_left, fun, vals[0]);
3005 gcpro3.nvars = 0;
3006
3007 argnum = 0;
3008 while (!NILP (args_left))
3009 {
3010 vals[argnum++] = Feval (Fcar (args_left));
3011 args_left = Fcdr (args_left);
3012 gcpro3.nvars = argnum;
3013 }
3014
3015 backtrace.args = vals;
3016 backtrace.nargs = nargs;
3017
3018 val = ((subr_function (subr)) (nargs, vals));
3019
3020 /* Have to duplicate this code because if the
3021 * debugger is called it must be in a scope in
3022 * which the `alloca'-ed data in vals is still valid.
3023 * (And GC-protected.)
3024 */
3025 lisp_eval_depth--;
3026 #ifdef MOCKLISP_SUPPORT
3027 if (!EQ (Vmocklisp_arguments, Qt))
3028 {
3029 if (NILP (val))
3030 val = Qzero;
3031 else if (EQ (val, Qt))
3032 val = make_int (1);
3033 }
3034 #endif
3035 if (backtrace.debug_on_exit)
3036 val = do_debug_on_exit (val);
3037 backtrace_list = backtrace.next;
3038 UNGCPRO;
3039 return (val);
3040 }
3041
3042 else
3043 {
3044 struct gcpro gcpro1, gcpro2, gcpro3;
3045
3046 GCPRO3 (args_left, fun, fun);
3047 gcpro3.var = argvals;
3048 gcpro3.nvars = 0;
3049
3050 for (i = 0; i < nargs; args_left = Fcdr (args_left))
3051 {
3052 argvals[i] = Feval (Fcar (args_left));
3053 gcpro3.nvars = ++i;
3054 }
3055
3056 UNGCPRO;
3057
3058 for (i = nargs; i < max_args; i++)
3059 argvals[i] = Qnil;
3060
3061 backtrace.args = argvals;
3062 backtrace.nargs = nargs;
3063
3064 val = funcall_subr (subr, argvals);
3065 }
3066 }
3067 else if (COMPILED_FUNCTIONP (fun))
3068 val = apply_lambda (fun, nargs, original_args);
3069 else
3070 {
3071 Lisp_Object funcar;
3072
3073 if (!CONSP (fun))
3074 goto invalid_function;
3075 funcar = Fcar (fun);
3076 if (!SYMBOLP (funcar))
3077 goto invalid_function;
3078 if (EQ (funcar, Qautoload))
3079 {
3080 do_autoload (fun, original_fun);
3081 goto retry;
3082 }
3083 if (EQ (funcar, Qmacro))
3084 val = Feval (apply1 (Fcdr (fun), original_args));
3085 else if (EQ (funcar, Qlambda))
3086 val = apply_lambda (fun, nargs, original_args);
3087 #ifdef MOCKLISP_SUPPORT
3088 else if (EQ (funcar, Qmocklisp))
3089 val = ml_apply (fun, original_args);
3090 #endif
3091 else
3092 {
3093 invalid_function:
3094 return Fsignal (Qinvalid_function, list1 (fun));
3095 }
3096 }
3097
3098 lisp_eval_depth--;
3099 #ifdef MOCKLISP_SUPPORT
3100 if (!EQ (Vmocklisp_arguments, Qt))
3101 {
3102 if (NILP (val))
3103 val = Qzero;
3104 else if (EQ (val, Qt))
3105 val = make_int (1);
3106 }
3107 #endif
3108 if (backtrace.debug_on_exit)
3109 val = do_debug_on_exit (val);
3110 backtrace_list = backtrace.next;
3111 return (val);
3112 }
3113
3114
3115 Lisp_Object
3116 funcall_recording_as (Lisp_Object recorded_as, int nargs,
3117 Lisp_Object *args)
3118 {
3119 /* This function can GC */
3120 Lisp_Object fun;
3121 Lisp_Object val;
3122 struct backtrace backtrace;
3123 REGISTER int i;
3124
3125 QUIT;
3126 if ((consing_since_gc > gc_cons_threshold) || always_gc)
3127 /* Callers should gcpro lexpr args */
3128 garbage_collect_1 ();
3129
3130 if (++lisp_eval_depth > max_lisp_eval_depth)
3131 {
3132 if (max_lisp_eval_depth < 100)
3133 max_lisp_eval_depth = 100;
3134 if (lisp_eval_depth > max_lisp_eval_depth)
3135 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
3136 }
3137
3138 /* Count number of arguments to function */
3139 nargs = nargs - 1;
3140
3141 #ifdef EMACS_BTL
3142 backtrace.id_number = 0;
3143 #endif
3144 backtrace.pdlcount = specpdl_depth_counter;
3145 backtrace.next = backtrace_list;
3146 backtrace.function = &args[0];
3147 backtrace.args = &args[1];
3148 backtrace.nargs = nargs;
3149 backtrace.evalargs = 0;
3150 backtrace.debug_on_exit = 0;
3151 /* XEmacs: make sure this is done last so we don't get race
3152 conditions in the profiling code. */
3153 backtrace_list = &backtrace;
3154
3155 if (debug_on_next_call)
3156 do_debug_on_call (Qlambda);
3157
3158 retry:
3159
3160 fun = args[0];
3161
3162 #ifdef EMACS_BTL
3163 {
3164 extern int emacs_btl_elisp_only_p;
3165 extern int btl_symbol_id_number ();
3166 if (emacs_btl_elisp_only_p)
3167 backtrace.id_number = btl_symbol_id_number (fun);
3168 }
3169 #endif
3170
3171 if (SYMBOLP (fun))
3172 fun = indirect_function (fun, 1);
3173
3174 if (SUBRP (fun))
3175 {
3176 struct Lisp_Subr *subr = XSUBR (fun);
3177 int max_args = subr->max_args;
3178
3179 if (max_args == UNEVALLED)
3180 return Fsignal (Qinvalid_function, list1 (fun));
3181
3182 if (nargs < subr->min_args
3183 || (max_args >= 0 && max_args < nargs))
3184 {
3185 return Fsignal (Qwrong_number_of_arguments,
3186 list2 (fun, make_int (nargs)));
3187 }
3188
3189 if (max_args == MANY)
3190 {
3191 val = ((subr_function (subr)) (nargs, args + 1));
3192 }
3193
3194 else if (max_args > nargs)
3195 {
3196 Lisp_Object argvals[SUBR_MAX_ARGS];
3197
3198 /* Default optionals to nil */
3199 for (i = 0; i < nargs; i++)
3200 argvals[i] = args[i + 1];
3201 for (i = nargs; i < max_args; i++)
3202 argvals[i] = Qnil;
3203
3204 val = funcall_subr (subr, argvals);
3205 }
3206 else
3207 val = funcall_subr (subr, args + 1);
3208 }
3209 else if (COMPILED_FUNCTIONP (fun))
3210 val = funcall_lambda (fun, nargs, args + 1);
3211 else if (!CONSP (fun))
3212 {
3213 invalid_function:
3214 return Fsignal (Qinvalid_function, list1 (fun));
3215 }
3216 else
3217 {
3218 Lisp_Object funcar = Fcar (fun);
3219
3220 if (!SYMBOLP (funcar))
3221 goto invalid_function;
3222 if (EQ (funcar, Qlambda))
3223 val = funcall_lambda (fun, nargs, args + 1);
3224 #ifdef MOCKLISP_SUPPORT
3225 else if (EQ (funcar, Qmocklisp))
3226 val = ml_apply (fun, Flist (nargs, args + 1));
3227 #endif
3228 else if (EQ (funcar, Qautoload))
3229 {
3230 do_autoload (fun, args[0]);
3231 goto retry;
3232 }
3233 else
3234 {
3235 goto invalid_function;
3236 }
3237 }
3238 lisp_eval_depth--;
3239 if (backtrace.debug_on_exit)
3240 val = do_debug_on_exit (val);
3241 backtrace_list = backtrace.next;
3242 return val;
3243 }
3244
3245 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0 /*
3246 Call first argument as a function, passing remaining arguments to it.
3247 Thus, (funcall 'cons 'x 'y) returns (x . y).
3248 */ )
3249 (nargs, args)
3250 int nargs;
3251 Lisp_Object *args;
3252 {
3253 return funcall_recording_as (args[0], nargs, args);
3254 }
3255
3256 DEFUN ("function-min-args", Ffunction_min_args, Sfunction_min_args,
3257 1, 1, 0 /*
3258 Return the number of arguments a function may be called with. The
3259 function may be any form that can be passed to `funcall', any special
3260 form, or any macro.
3261 */ )
3262 (function)
3263 Lisp_Object function;
3264 {
3265 Lisp_Object orig_function = function;
3266 Lisp_Object arglist;
3267 int argcount;
3268
3269 retry:
3270
3271 if (SYMBOLP (function))
3272 function = indirect_function (function, 1);
3273
3274 if (SUBRP (function))
3275 return Fsubr_min_args (function);
3276 else if (!COMPILED_FUNCTIONP (function) && !CONSP (function))
3277 {
3278 invalid_function:
3279 return Fsignal (Qinvalid_function, list1 (function));
3280 }
3281
3282 if (CONSP (function))
3283 {
3284 Lisp_Object funcar = Fcar (function);
3285
3286 if (!SYMBOLP (funcar))
3287 goto invalid_function;
3288 if (EQ (funcar, Qmacro))
3289 {
3290 function = Fcdr (function);
3291 goto retry;
3292 }
3293 if (EQ (funcar, Qautoload))
3294 {
3295 do_autoload (function, orig_function);
3296 goto retry;
3297 }
3298 if (EQ (funcar, Qlambda))
3299 arglist = Fcar (Fcdr (function));
3300 else
3301 goto invalid_function;
3302 }
3303 else
3304 arglist = XCOMPILED_FUNCTION (function)->arglist;
3305
3306 argcount = 0;
3307 while (!NILP (arglist))
3308 {
3309 QUIT;
3310 if (EQ (Fcar (arglist), Qand_optional)
3311 || EQ (Fcar (arglist), Qand_rest))
3312 break;
3313 argcount++;
3314 arglist = Fcdr (arglist);
3315 }
3316
3317 return make_int (argcount);
3318 }
3319
3320 DEFUN ("function-max-args", Ffunction_max_args, Sfunction_max_args,
3321 1, 1, 0 /*
3322 Return the number of arguments a function may be called with. If the
3323 function takes an arbitrary number of arguments or is a built-in
3324 special form, nil is returned. The function may be any form that can
3325 be passed to `funcall', any special form, or any macro.
3326 */ )
3327 (function)
3328 Lisp_Object function;
3329 {
3330 Lisp_Object orig_function = function;
3331 Lisp_Object arglist;
3332 int argcount;
3333
3334 retry:
3335
3336 if (SYMBOLP (function))
3337 function = indirect_function (function, 1);
3338
3339 if (SUBRP (function))
3340 return Fsubr_max_args (function);
3341 else if (!COMPILED_FUNCTIONP (function) && !CONSP (function))
3342 {
3343 invalid_function:
3344 return Fsignal (Qinvalid_function, list1 (function));
3345 }
3346
3347 if (CONSP (function))
3348 {
3349 Lisp_Object funcar = Fcar (function);
3350
3351 if (!SYMBOLP (funcar))
3352 goto invalid_function;
3353 if (EQ (funcar, Qmacro))
3354 {
3355 function = Fcdr (function);
3356 goto retry;
3357 }
3358 if (EQ (funcar, Qautoload))
3359 {
3360 do_autoload (function, orig_function);
3361 goto retry;
3362 }
3363 if (EQ (funcar, Qlambda))
3364 arglist = Fcar (Fcdr (function));
3365 else
3366 goto invalid_function;
3367 }
3368 else
3369 arglist = XCOMPILED_FUNCTION (function)->arglist;
3370
3371 argcount = 0;
3372 while (!NILP (arglist))
3373 {
3374 QUIT;
3375 if (EQ (Fcar (arglist), Qand_optional))
3376 {
3377 arglist = Fcdr (arglist);
3378 continue;
3379 }
3380 if (EQ (Fcar (arglist), Qand_rest))
3381 return Qnil;
3382 argcount++;
3383 arglist = Fcdr (arglist);
3384 }
3385
3386 return make_int (argcount);
3387 }
3388
3389
3390 DEFUN ("apply", Fapply, Sapply, 2, MANY, 0 /*
3391 Call FUNCTION with our remaining args, using our last arg as list of args.
3392 Thus, (apply '+ 1 2 '(3 4)) returns 10.
3393 */ )
3394 (nargs, args)
3395 int nargs;
3396 Lisp_Object *args;
3397 {
3398 /* This function can GC */
3399 Lisp_Object fun = args[0];
3400 Lisp_Object spread_arg = args [nargs - 1];
3401 int numargs;
3402 int funcall_nargs;
3403
3404 CHECK_LIST (spread_arg);
3405
3406 numargs = XINT (Flength (spread_arg));
3407
3408 if (numargs == 0)
3409 /* (apply foo 0 1 '()) */
3410 return Ffuncall (nargs - 1, args);
3411 else if (numargs == 1)
3412 {
3413 /* (apply foo 0 1 '(2)) */
3414 args [nargs - 1] = XCAR (spread_arg);
3415 return Ffuncall (nargs, args);
3416 }
3417
3418 /* -1 for function, -1 for spread arg */
3419 numargs = nargs - 2 + numargs;
3420 /* +1 for function */
3421 funcall_nargs = 1 + numargs;
3422
3423 if (SYMBOLP (fun))
3424 fun = indirect_function (fun, 0);
3425 if (UNBOUNDP (fun))
3426 {
3427 /* Let funcall get the error */
3428 fun = args[0];
3429 }
3430 else if (SUBRP (fun))
3431 {
3432 struct Lisp_Subr *subr = XSUBR (fun);
3433 int max_args = subr->max_args;
3434
3435 if (numargs < subr->min_args
3436 || (max_args >= 0 && max_args < numargs))
3437 {
3438 /* Let funcall get the error */
3439 }
3440 else if (max_args > numargs)
3441 {
3442 /* Avoid having funcall cons up yet another new vector of arguments
3443 by explicitly supplying nil's for optional values */
3444 funcall_nargs += (max_args - numargs);
3445 }
3446 }
3447 {
3448 REGISTER int i;
3449 REGISTER Lisp_Object *funcall_args
3450 = (Lisp_Object *) alloca (funcall_nargs * sizeof (Lisp_Object));
3451 struct gcpro gcpro1;
3452
3453 GCPRO1 (*funcall_args);
3454 gcpro1.nvars = funcall_nargs;
3455
3456 /* Copy in the unspread args */
3457 memcpy (funcall_args, args, (nargs - 1) * sizeof (Lisp_Object));
3458 /* Spread the last arg we got. Its first element goes in
3459 the slot that it used to occupy, hence this value of I. */
3460 for (i = nargs - 1;
3461 !NILP (spread_arg); /* i < 1 + numargs */
3462 i++, spread_arg = XCDR (spread_arg))
3463 {
3464 funcall_args [i] = XCAR (spread_arg);
3465 }
3466 /* Supply nil for optional args (to subrs) */
3467 for (; i < funcall_nargs; i++)
3468 funcall_args[i] = Qnil;
3469
3470
3471 RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
3472 }
3473 }
3474
3475
3476 static Lisp_Object
3477 primitive_funcall (Lisp_Object (*fn) (), int nargs, Lisp_Object args[])
3478 {
3479 switch (nargs)
3480 {
3481 case 0:
3482 return ((*fn) ());
3483 case 1:
3484 return ((*fn) (args[0]));
3485 case 2:
3486 return ((*fn) (args[0], args[1]));
3487 case 3:
3488 return ((*fn) (args[0], args[1], args[2]));
3489 case 4:
3490 return ((*fn) (args[0], args[1], args[2], args[3]));
3491 case 5:
3492 return ((*fn) (args[0], args[1], args[2], args[3], args[4]));
3493 case 6:
3494 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5]));
3495 case 7:
3496 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3497 args[6]));
3498 case 8:
3499 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3500 args[6], args[7]));
3501 case 9:
3502 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3503 args[6], args[7], args[8]));
3504 case 10:
3505 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3506 args[6], args[7], args[8], args[9]));
3507 case 11:
3508 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3509 args[6], args[7], args[8], args[9], args[10]));
3510 case 12:
3511 return ((*fn) (args[0], args[1], args[2], args[3], args[4], args[5],
3512 args[6], args[7], args[8], args[9], args[10], args[11]));
3513 default:
3514 /* Someone has created a subr that takes more arguments than
3515 is supported by this code. We need to either rewrite the
3516 subr to use a different argument protocol, or add more
3517 cases to this switch. */
3518 abort ();
3519 }
3520 return Qnil; /* suppress compiler warning */
3521 }
3522
3523 static Lisp_Object
3524 funcall_subr (struct Lisp_Subr *subr, Lisp_Object args[])
3525 {
3526 return primitive_funcall (subr_function (subr), subr->max_args, args);
3527 }
3528
3529 /* FSFmacs has an extra arg EVAL_FLAG. If false, some of
3530 the statements below are not done. But it's always true
3531 in all the calls to apply_lambda(). */
3532
3533 static Lisp_Object
3534 apply_lambda (Lisp_Object fun, int numargs, Lisp_Object unevalled_args)
3535 {
3536 /* This function can GC */
3537 struct gcpro gcpro1, gcpro2, gcpro3;
3538 REGISTER int i;
3539 REGISTER Lisp_Object tem;
3540 REGISTER Lisp_Object *arg_vector
3541 = (Lisp_Object *) alloca (numargs * sizeof (Lisp_Object));
3542
3543 GCPRO3 (*arg_vector, unevalled_args, fun);
3544 gcpro1.nvars = 0;
3545
3546 for (i = 0; i < numargs;)
3547 {
3548 tem = Fcar (unevalled_args), unevalled_args = Fcdr (unevalled_args);
3549 tem = Feval (tem);
3550 arg_vector[i++] = tem;
3551 gcpro1.nvars = i;
3552 }
3553
3554 UNGCPRO;
3555
3556 backtrace_list->args = arg_vector;
3557 backtrace_list->nargs = i;
3558 backtrace_list->evalargs = 0;
3559 tem = funcall_lambda (fun, numargs, arg_vector);
3560
3561 /* Do the debug-on-exit now, while arg_vector still exists. */
3562 if (backtrace_list->debug_on_exit)
3563 tem = do_debug_on_exit (tem);
3564 /* Don't do it again when we return to eval. */
3565 backtrace_list->debug_on_exit = 0;
3566 return (tem);
3567 }
3568
3569 /* Apply a Lisp function FUN to the NARGS evaluated arguments in ARG_VECTOR
3570 and return the result of evaluation.
3571 FUN must be either a lambda-expression or a compiled-code object. */
3572
3573 static Lisp_Object
3574 funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object arg_vector[])
3575 {
3576 /* This function can GC */
3577 Lisp_Object val, tem;
3578 REGISTER Lisp_Object syms_left;
3579 REGISTER Lisp_Object next;
3580 int speccount = specpdl_depth_counter;
3581 REGISTER int i;
3582 int optional = 0, rest = 0;
3583
3584 #ifdef MOCKLISP_SUPPORT
3585 if (!EQ (Vmocklisp_arguments, Qt))
3586 specbind (Qmocklisp_arguments, Qt); /* t means NOT mocklisp! */
3587 #endif
3588
3589 if (CONSP (fun))
3590 syms_left = Fcar (Fcdr (fun));
3591 else if (COMPILED_FUNCTIONP (fun))
3592 syms_left = XCOMPILED_FUNCTION (fun)->arglist;
3593 else abort ();
3594
3595 i = 0;
3596 for (; !NILP (syms_left); syms_left = Fcdr (syms_left))
3597 {
3598 QUIT;
3599 next = Fcar (syms_left);
3600 if (!SYMBOLP (next))
3601 signal_error (Qinvalid_function, list1 (fun));
3602 if (EQ (next, Qand_rest))
3603 rest = 1;
3604 else if (EQ (next, Qand_optional))
3605 optional = 1;
3606 else if (rest)
3607 {
3608 specbind (next, Flist (nargs - i, &arg_vector[i]));
3609 i = nargs;
3610 }
3611 else if (i < nargs)
3612 {
3613 tem = arg_vector[i++];
3614 specbind (next, tem);
3615 }
3616 else if (!optional)
3617 return Fsignal (Qwrong_number_of_arguments,
3618 list2 (fun, make_int (nargs)));
3619 else
3620 specbind (next, Qnil);
3621 }
3622
3623 if (i < nargs)
3624 return Fsignal (Qwrong_number_of_arguments,
3625 list2 (fun, make_int (nargs)));
3626
3627 if (CONSP (fun))
3628 val = Fprogn (Fcdr (Fcdr (fun)));
3629 else
3630 {
3631 struct Lisp_Compiled_Function *b = XCOMPILED_FUNCTION (fun);
3632 /* If we have not actually read the bytecode string
3633 and constants vector yet, fetch them from the file. */
3634 if (CONSP (b->bytecodes))
3635 Ffetch_bytecode (fun);
3636 val = Fbyte_code (b->bytecodes,
3637 b->constants,
3638 make_int (b->maxdepth));
3639 }
3640 return unbind_to (speccount, val);
3641 }
3642
3643 DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3644 1, 1, 0 /*
3645 If byte-compiled OBJECT is lazy-loaded, fetch it now.
3646 */ )
3647 (object)
3648 Lisp_Object object;
3649 {
3650 Lisp_Object tem;
3651
3652 if (COMPILED_FUNCTIONP (object)
3653 && CONSP (XCOMPILED_FUNCTION (object)->bytecodes))
3654 {
3655 tem = read_doc_string (XCOMPILED_FUNCTION (object)->bytecodes);
3656 if (!CONSP (tem))
3657 signal_simple_error ("invalid lazy-loaded byte code", tem);
3658 /* VERY IMPORTANT to purecopy here!!!!!
3659 See load_force_doc_string_unwind. */
3660 XCOMPILED_FUNCTION (object)->bytecodes = Fpurecopy (XCAR (tem));
3661 XCOMPILED_FUNCTION (object)->constants = Fpurecopy (XCDR (tem));
3662 }
3663 return object;
3664 }
3665
3666
3667 /**********************************************************************/
3668 /* Run hook variables in various ways. */
3669 /**********************************************************************/
3670
3671 DEFUN ("run-hooks", Frun_hooks, Srun_hooks, 1, MANY, 0 /*
3672 Run each hook in HOOKS. Major mode functions use this.
3673 Each argument should be a symbol, a hook variable.
3674 These symbols are processed in the order specified.
3675 If a hook symbol has a non-nil value, that value may be a function
3676 or a list of functions to be called to run the hook.
3677 If the value is a function, it is called with no arguments.
3678 If it is a list, the elements are called, in order, with no arguments.
3679
3680 To make a hook variable buffer-local, use `make-local-hook',
3681 not `make-local-variable'.
3682 */ )
3683 (nargs, args)
3684 int nargs;
3685 Lisp_Object *args;
3686 {
3687 Lisp_Object hook[1];
3688 REGISTER int i;
3689
3690 for (i = 0; i < nargs; i++)
3691 {
3692 hook[0] = args[i];
3693 run_hook_with_args (1, hook, RUN_HOOKS_TO_COMPLETION);
3694 }
3695
3696 return Qnil;
3697 }
3698
3699 DEFUN ("run-hook-with-args",
3700 Frun_hook_with_args, Srun_hook_with_args, 1, MANY, 0 /*
3701 Run HOOK with the specified arguments ARGS.
3702 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
3703 value, that value may be a function or a list of functions to be
3704 called to run the hook. If the value is a function, it is called with
3705 the given arguments and its return value is returned. If it is a list
3706 of functions, those functions are called, in order,
3707 with the given arguments ARGS.
3708 It is best not to depend on the value return by `run-hook-with-args',
3709 as that may change.
3710
3711 To make a hook variable buffer-local, use `make-local-hook',
3712 not `make-local-variable'.
3713 */ )
3714 (nargs, args)
3715 int nargs;
3716 Lisp_Object *args;
3717 {
3718 return run_hook_with_args (nargs, args, RUN_HOOKS_TO_COMPLETION);
3719 }
3720
3721 DEFUN ("run-hook-with-args-until-success",
3722 Frun_hook_with_args_until_success, Srun_hook_with_args_until_success,
3723 1, MANY, 0 /*
3724 Run HOOK with the specified arguments ARGS.
3725 HOOK should be a symbol, a hook variable. Its value should
3726 be a list of functions. We call those functions, one by one,
3727 passing arguments ARGS to each of them, until one of them
3728 returns a non-nil value. Then we return that value.
3729 If all the functions return nil, we return nil.
3730
3731 To make a hook variable buffer-local, use `make-local-hook',
3732 not `make-local-variable'.
3733 */ )
3734 (nargs, args)
3735 int nargs;
3736 Lisp_Object *args;
3737 {
3738 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_SUCCESS);
3739 }
3740
3741 DEFUN ("run-hook-with-args-until-failure",
3742 Frun_hook_with_args_until_failure, Srun_hook_with_args_until_failure,
3743 1, MANY, 0 /*
3744 Run HOOK with the specified arguments ARGS.
3745 HOOK should be a symbol, a hook variable. Its value should
3746 be a list of functions. We call those functions, one by one,
3747 passing arguments ARGS to each of them, until one of them
3748 returns nil. Then we return nil.
3749 If all the functions return non-nil, we return non-nil.
3750
3751 To make a hook variable buffer-local, use `make-local-hook',
3752 not `make-local-variable'.
3753 */ )
3754 (nargs, args)
3755 int nargs;
3756 Lisp_Object *args;
3757 {
3758 return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_FAILURE);
3759 }
3760
3761 /* ARGS[0] should be a hook symbol.
3762 Call each of the functions in the hook value, passing each of them
3763 as arguments all the rest of ARGS (all NARGS - 1 elements).
3764 COND specifies a condition to test after each call
3765 to decide whether to stop.
3766 The caller (or its caller, etc) must gcpro all of ARGS,
3767 except that it isn't necessary to gcpro ARGS[0]. */
3768
3769 Lisp_Object
3770 run_hook_with_args_in_buffer (struct buffer *buf, int nargs, Lisp_Object *args,
3771 enum run_hooks_condition cond)
3772 {
3773 Lisp_Object sym, val, ret;
3774 struct gcpro gcpro1, gcpro2;
3775
3776 if (!initialized || preparing_for_armageddon)
3777 /* We need to bail out of here pronto. */
3778 return Qnil;
3779
3780 /* Whenever gc_in_progress is true, preparing_for_armageddon
3781 will also be true unless something is really hosed. */
3782 assert (!gc_in_progress);
3783
3784 sym = args[0];
3785 val = symbol_value_in_buffer (sym, make_buffer (buf));
3786 ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil);
3787
3788 if (UNBOUNDP (val) || NILP (val))
3789 return ret;
3790 else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3791 {
3792 args[0] = val;
3793 return Ffuncall (nargs, args);
3794 }
3795 else
3796 {
3797 GCPRO2 (sym, val);
3798
3799 for (;
3800 CONSP (val) && ((cond == RUN_HOOKS_TO_COMPLETION)
3801 || (cond == RUN_HOOKS_UNTIL_SUCCESS ? NILP (ret)
3802 : !NILP (ret)));
3803 val = XCDR (val))
3804 {
3805 if (EQ (XCAR (val), Qt))
3806 {
3807 /* t indicates this hook has a local binding;
3808 it means to run the global binding too. */
3809 Lisp_Object globals;
3810
3811 for (globals = Fdefault_value (sym);
3812 CONSP (globals) && ((cond == RUN_HOOKS_TO_COMPLETION)
3813 || (cond == RUN_HOOKS_UNTIL_SUCCESS
3814 ? NILP (ret)
3815 : !NILP (ret)));
3816 globals = XCDR (globals))
3817 {
3818 args[0] = XCAR (globals);
3819 /* In a global value, t should not occur. If it does, we
3820 must ignore it to avoid an endless loop. */
3821 if (!EQ (args[0], Qt))
3822 ret = Ffuncall (nargs, args);
3823 }
3824 }
3825 else
3826 {
3827 args[0] = XCAR (val);
3828 ret = Ffuncall (nargs, args);
3829 }
3830 }
3831
3832 UNGCPRO;
3833 return ret;
3834 }
3835 }
3836
3837 Lisp_Object
3838 run_hook_with_args (int nargs, Lisp_Object *args,
3839 enum run_hooks_condition cond)
3840 {
3841 return run_hook_with_args_in_buffer (current_buffer, nargs, args, cond);
3842 }
3843
3844 #if 0
3845
3846 /* From FSF 19.30, not currently used */
3847
3848 /* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
3849 present value of that symbol.
3850 Call each element of FUNLIST,
3851 passing each of them the rest of ARGS.
3852 The caller (or its caller, etc) must gcpro all of ARGS,
3853 except that it isn't necessary to gcpro ARGS[0]. */
3854
3855 Lisp_Object
3856 run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
3857 {
3858 Lisp_Object sym;
3859 Lisp_Object val;
3860 struct gcpro gcpro1, gcpro2;
3861
3862 sym = args[0];
3863 GCPRO2 (sym, val);
3864
3865 for (val = funlist; CONSP (val); val = XCDR (val))
3866 {
3867 if (EQ (XCAR (val), Qt))
3868 {
3869 /* t indicates this hook has a local binding;
3870 it means to run the global binding too. */
3871 Lisp_Object globals;
3872
3873 for (globals = Fdefault_value (sym);
3874 CONSP (globals);
3875 globals = XCDR (globals))
3876 {
3877 args[0] = XCAR (globals);
3878 /* In a global value, t should not occur. If it does, we
3879 must ignore it to avoid an endless loop. */
3880 if (!EQ (args[0], Qt))
3881 Ffuncall (nargs, args);
3882 }
3883 }
3884 else
3885 {
3886 args[0] = XCAR (val);
3887 Ffuncall (nargs, args);
3888 }
3889 }
3890 UNGCPRO;
3891 return Qnil;
3892 }
3893
3894 #endif /* 0 */
3895
3896 void
3897 va_run_hook_with_args (Lisp_Object hook_var, int nargs, ...)
3898 {
3899 /* This function can GC */
3900 struct gcpro gcpro1;
3901 int i;
3902 va_list vargs;
3903 Lisp_Object *funcall_args =
3904 (Lisp_Object *) alloca ((1 + nargs) * sizeof (Lisp_Object));
3905
3906 va_start (vargs, nargs);
3907 funcall_args[0] = hook_var;
3908 for (i = 0; i < nargs; i++)
3909 funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
3910 va_end (vargs);
3911
3912 GCPRO1 (*funcall_args);
3913 gcpro1.nvars = nargs + 1;
3914 run_hook_with_args (nargs + 1, funcall_args, RUN_HOOKS_TO_COMPLETION);
3915 UNGCPRO;
3916 }
3917
3918 void
3919 va_run_hook_with_args_in_buffer (struct buffer *buf, Lisp_Object hook_var,
3920 int nargs, ...)
3921 {
3922 /* This function can GC */
3923 struct gcpro gcpro1;
3924 int i;
3925 va_list vargs;
3926 Lisp_Object *funcall_args =
3927 (Lisp_Object *) alloca ((1 + nargs) * sizeof (Lisp_Object));
3928
3929 va_start (vargs, nargs);
3930 funcall_args[0] = hook_var;
3931 for (i = 0; i < nargs; i++)
3932 funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
3933 va_end (vargs);
3934
3935 GCPRO1 (*funcall_args);
3936 gcpro1.nvars = nargs + 1;
3937 run_hook_with_args_in_buffer (buf, nargs + 1, funcall_args,
3938 RUN_HOOKS_TO_COMPLETION);
3939 UNGCPRO;
3940 }
3941
3942 Lisp_Object
3943 run_hook (Lisp_Object hook)
3944 {
3945 Frun_hooks (1, &hook);
3946 return Qnil;
3947 }
3948
3949
3950 /**********************************************************************/
3951 /* Front-ends to eval, funcall, apply */
3952 /**********************************************************************/
3953
3954 /* Apply fn to arg */
3955 Lisp_Object
3956 apply1 (Lisp_Object fn, Lisp_Object arg)
3957 {
3958 /* This function can GC */
3959 struct gcpro gcpro1;
3960 Lisp_Object args[2];
3961
3962 if (NILP (arg))
3963 return (Ffuncall (1, &fn));
3964 GCPRO1 (args[0]);
3965 gcpro1.nvars = 2;
3966 args[0] = fn;
3967 args[1] = arg;
3968 RETURN_UNGCPRO (Fapply (2, args));
3969 }
3970
3971 /* Call function fn on no arguments */
3972 Lisp_Object
3973 call0 (Lisp_Object fn)
3974 {
3975 /* This function can GC */
3976 struct gcpro gcpro1;
3977
3978 GCPRO1 (fn);
3979 RETURN_UNGCPRO (Ffuncall (1, &fn));
3980 }
3981
3982 /* Call function fn with argument arg0 */
3983 Lisp_Object
3984 call1 (Lisp_Object fn,
3985 Lisp_Object arg0)
3986 {
3987 /* This function can GC */
3988 struct gcpro gcpro1;
3989 Lisp_Object args[2];
3990 args[0] = fn;
3991 args[1] = arg0;
3992 GCPRO1 (args[0]);
3993 gcpro1.nvars = 2;
3994 RETURN_UNGCPRO (Ffuncall (2, args));
3995 }
3996
3997 /* Call function fn with arguments arg0, arg1 */
3998 Lisp_Object
3999 call2 (Lisp_Object fn,
4000 Lisp_Object arg0, Lisp_Object arg1)
4001 {
4002 /* This function can GC */
4003 struct gcpro gcpro1;
4004 Lisp_Object args[3];
4005 args[0] = fn;
4006 args[1] = arg0;
4007 args[2] = arg1;
4008 GCPRO1 (args[0]);
4009 gcpro1.nvars = 3;
4010 RETURN_UNGCPRO (Ffuncall (3, args));
4011 }
4012
4013 /* Call function fn with arguments arg0, arg1, arg2 */
4014 Lisp_Object
4015 call3 (Lisp_Object fn,
4016 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
4017 {
4018 /* This function can GC */
4019 struct gcpro gcpro1;
4020 Lisp_Object args[4];
4021 args[0] = fn;
4022 args[1] = arg0;
4023 args[2] = arg1;
4024 args[3] = arg2;
4025 GCPRO1 (args[0]);
4026 gcpro1.nvars = 4;
4027 RETURN_UNGCPRO (Ffuncall (4, args));
4028 }
4029
4030 /* Call function fn with arguments arg0, arg1, arg2, arg3 */
4031 Lisp_Object
4032 call4 (Lisp_Object fn,
4033 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4034 Lisp_Object arg3)
4035 {
4036 /* This function can GC */
4037 struct gcpro gcpro1;
4038 Lisp_Object args[5];
4039 args[0] = fn;
4040 args[1] = arg0;
4041 args[2] = arg1;
4042 args[3] = arg2;
4043 args[4] = arg3;
4044 GCPRO1 (args[0]);
4045 gcpro1.nvars = 5;
4046 RETURN_UNGCPRO (Ffuncall (5, args));
4047 }
4048
4049 /* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */
4050 Lisp_Object
4051 call5 (Lisp_Object fn,
4052 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4053 Lisp_Object arg3, Lisp_Object arg4)
4054 {
4055 /* This function can GC */
4056 struct gcpro gcpro1;
4057 Lisp_Object args[6];
4058 args[0] = fn;
4059 args[1] = arg0;
4060 args[2] = arg1;
4061 args[3] = arg2;
4062 args[4] = arg3;
4063 args[5] = arg4;
4064 GCPRO1 (args[0]);
4065 gcpro1.nvars = 6;
4066 RETURN_UNGCPRO (Ffuncall (6, args));
4067 }
4068
4069 Lisp_Object
4070 call6 (Lisp_Object fn,
4071 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4072 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
4073 {
4074 /* This function can GC */
4075 struct gcpro gcpro1;
4076 Lisp_Object args[7];
4077 args[0] = fn;
4078 args[1] = arg0;
4079 args[2] = arg1;
4080 args[3] = arg2;
4081 args[4] = arg3;
4082 args[5] = arg4;
4083 args[6] = arg5;
4084 GCPRO1 (args[0]);
4085 gcpro1.nvars = 7;
4086 RETURN_UNGCPRO (Ffuncall (7, args));
4087 }
4088
4089 Lisp_Object
4090 call7 (Lisp_Object fn,
4091 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4092 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
4093 Lisp_Object arg6)
4094 {
4095 /* This function can GC */
4096 struct gcpro gcpro1;
4097 Lisp_Object args[8];
4098 args[0] = fn;
4099 args[1] = arg0;
4100 args[2] = arg1;
4101 args[3] = arg2;
4102 args[4] = arg3;
4103 args[5] = arg4;
4104 args[6] = arg5;
4105 args[7] = arg6;
4106 GCPRO1 (args[0]);
4107 gcpro1.nvars = 8;
4108 RETURN_UNGCPRO (Ffuncall (8, args));
4109 }
4110
4111 Lisp_Object
4112 call8 (Lisp_Object fn,
4113 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4114 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
4115 Lisp_Object arg6, Lisp_Object arg7)
4116 {
4117 /* This function can GC */
4118 struct gcpro gcpro1;
4119 Lisp_Object args[9];
4120 args[0] = fn;
4121 args[1] = arg0;
4122 args[2] = arg1;
4123 args[3] = arg2;
4124 args[4] = arg3;
4125 args[5] = arg4;
4126 args[6] = arg5;
4127 args[7] = arg6;
4128 args[8] = arg7;
4129 GCPRO1 (args[0]);
4130 gcpro1.nvars = 9;
4131 RETURN_UNGCPRO (Ffuncall (9, args));
4132 }
4133
4134 Lisp_Object
4135 call0_in_buffer (struct buffer *buf, Lisp_Object fn)
4136 {
4137 int speccount = specpdl_depth ();
4138 Lisp_Object val;
4139
4140 if (current_buffer != buf)
4141 {
4142 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4143 set_buffer_internal (buf);
4144 }
4145 val = call0 (fn);
4146 unbind_to (speccount, Qnil);
4147 return val;
4148 }
4149
4150 Lisp_Object
4151 call1_in_buffer (struct buffer *buf, Lisp_Object fn,
4152 Lisp_Object arg0)
4153 {
4154 int speccount = specpdl_depth ();
4155 Lisp_Object val;
4156
4157 if (current_buffer != buf)
4158 {
4159 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4160 set_buffer_internal (buf);
4161 }
4162 val = call1 (fn, arg0);
4163 unbind_to (speccount, Qnil);
4164 return val;
4165 }
4166
4167 Lisp_Object
4168 call2_in_buffer (struct buffer *buf, Lisp_Object fn,
4169 Lisp_Object arg0, Lisp_Object arg1)
4170 {
4171 int speccount = specpdl_depth ();
4172 Lisp_Object val;
4173
4174 if (current_buffer != buf)
4175 {
4176 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4177 set_buffer_internal (buf);
4178 }
4179 val = call2 (fn, arg0, arg1);
4180 unbind_to (speccount, Qnil);
4181 return val;
4182 }
4183
4184 Lisp_Object
4185 call3_in_buffer (struct buffer *buf, Lisp_Object fn,
4186 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
4187 {
4188 int speccount = specpdl_depth ();
4189 Lisp_Object val;
4190
4191 if (current_buffer != buf)
4192 {
4193 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4194 set_buffer_internal (buf);
4195 }
4196 val = call3 (fn, arg0, arg1, arg2);
4197 unbind_to (speccount, Qnil);
4198 return val;
4199 }
4200
4201 Lisp_Object
4202 call4_in_buffer (struct buffer *buf, Lisp_Object fn,
4203 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4204 Lisp_Object arg3)
4205 {
4206 int speccount = specpdl_depth ();
4207 Lisp_Object val;
4208
4209 if (current_buffer != buf)
4210 {
4211 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4212 set_buffer_internal (buf);
4213 }
4214 val = call4 (fn, arg0, arg1, arg2, arg3);
4215 unbind_to (speccount, Qnil);
4216 return val;
4217 }
4218
4219 Lisp_Object
4220 call5_in_buffer (struct buffer *buf, Lisp_Object fn,
4221 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4222 Lisp_Object arg3, Lisp_Object arg4)
4223 {
4224 int speccount = specpdl_depth ();
4225 Lisp_Object val;
4226
4227 if (current_buffer != buf)
4228 {
4229 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4230 set_buffer_internal (buf);
4231 }
4232 val = call5 (fn, arg0, arg1, arg2, arg3, arg4);
4233 unbind_to (speccount, Qnil);
4234 return val;
4235 }
4236
4237 Lisp_Object
4238 call6_in_buffer (struct buffer *buf, Lisp_Object fn,
4239 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
4240 Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
4241 {
4242 int speccount = specpdl_depth ();
4243 Lisp_Object val;
4244
4245 if (current_buffer != buf)
4246 {
4247 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4248 set_buffer_internal (buf);
4249 }
4250 val = call6 (fn, arg0, arg1, arg2, arg3, arg4, arg5);
4251 unbind_to (speccount, Qnil);
4252 return val;
4253 }
4254
4255 Lisp_Object
4256 eval_in_buffer (struct buffer *buf, Lisp_Object form)
4257 {
4258 int speccount = specpdl_depth ();
4259 Lisp_Object val;
4260
4261 if (current_buffer != buf)
4262 {
4263 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4264 set_buffer_internal (buf);
4265 }
4266 val = Feval (form);
4267 unbind_to (speccount, Qnil);
4268 return val;
4269 }
4270
4271
4272 /***** Error-catching front-ends to eval, funcall, apply */
4273
4274 /* Call function fn on no arguments, with condition handler */
4275 Lisp_Object
4276 call0_with_handler (Lisp_Object handler, Lisp_Object fn)
4277 {
4278 /* This function can GC */
4279 struct gcpro gcpro1;
4280 Lisp_Object args[2];
4281 args[0] = handler;
4282 args[1] = fn;
4283 GCPRO1 (args[0]);
4284 gcpro1.nvars = 2;
4285 RETURN_UNGCPRO (Fcall_with_condition_handler (2, args));
4286 }
4287
4288 /* Call function fn with argument arg0, with condition handler */
4289 Lisp_Object
4290 call1_with_handler (Lisp_Object handler, Lisp_Object fn,
4291 Lisp_Object arg0)
4292 {
4293 /* This function can GC */
4294 struct gcpro gcpro1;
4295 Lisp_Object args[3];
4296 args[0] = handler;
4297 args[1] = fn;
4298 args[2] = arg0;
4299 GCPRO1 (args[0]);
4300 gcpro1.nvars = 3;
4301 RETURN_UNGCPRO (Fcall_with_condition_handler (3, args));
4302 }
4303
4304
4305 /* The following functions provide you with error-trapping versions
4306 of the various front-ends above. They take an additional
4307 "warning_string" argument; if non-zero, a warning with this
4308 string and the actual error that occurred will be displayed
4309 in the *Warnings* buffer if an error occurs. In all cases,
4310 QUIT is inhibited while these functions are running, and if
4311 an error occurs, Qunbound is returned instead of the normal
4312 return value.
4313 */
4314
4315 /* #### This stuff needs to catch throws as well. We need to
4316 improve internal_catch() so it can take a "catch anything"
4317 argument similar to Qt or Qerror for condition_case_1(). */
4318
4319 static Lisp_Object
4320 caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4321 {
4322 if (!NILP (errordata))
4323 {
4324 Lisp_Object args[2];
4325
4326 if (!NILP (arg))
4327 {
4328 char *str = (char *) get_opaque_ptr (arg);
4329 args[0] = build_string (str);
4330 }
4331 else
4332 args[0] = build_string ("error");
4333 /* #### This should call
4334 (with-output-to-string (display-error errordata))
4335 but that stuff is all in Lisp currently. */
4336 args[1] = errordata;
4337 warn_when_safe_lispobj
4338 (Qerror, Qwarning,
4339 emacs_doprnt_string_lisp ((CONST Bufbyte *) "%s: %s",
4340 Qnil, -1, 2, args));
4341 }
4342 return Qunbound;
4343 }
4344
4345 static Lisp_Object
4346 allow_quit_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4347 {
4348 if (CONSP (errordata) && EQ (XCAR (errordata), Qquit))
4349 return Fsignal (Qquit, XCDR (errordata));
4350 return caught_a_squirmer (errordata, arg);
4351 }
4352
4353 static Lisp_Object
4354 safe_run_hook_caught_a_squirmer (Lisp_Object errordata, Lisp_Object arg)
4355 {
4356 Lisp_Object hook = Fcar (arg);
4357 arg = Fcdr (arg);
4358 /* Clear out the hook. */
4359 Fset (hook, Qnil);
4360 return caught_a_squirmer (errordata, arg);
4361 }
4362
4363 static Lisp_Object
4364 allow_quit_safe_run_hook_caught_a_squirmer (Lisp_Object errordata,
4365 Lisp_Object arg)
4366 {
4367 Lisp_Object hook = Fcar (arg);
4368 arg = Fcdr (arg);
4369 if (!CONSP (errordata) || !EQ (XCAR (errordata), Qquit))
4370 /* Clear out the hook. */
4371 Fset (hook, Qnil);
4372 return allow_quit_caught_a_squirmer (errordata, arg);
4373 }
4374
4375 static Lisp_Object
4376 catch_them_squirmers_eval_in_buffer (Lisp_Object cons)
4377 {
4378 return eval_in_buffer (XBUFFER (XCAR (cons)), XCDR (cons));
4379 }
4380
4381 Lisp_Object
4382 eval_in_buffer_trapping_errors (CONST char *warning_string,
4383 struct buffer *buf, Lisp_Object form)
4384 {
4385 int speccount = specpdl_depth ();
4386 Lisp_Object tem;
4387 Lisp_Object buffer = Qnil;
4388 Lisp_Object cons;
4389 Lisp_Object opaque;
4390 struct gcpro gcpro1, gcpro2;
4391
4392 XSETBUFFER (buffer, buf);
4393
4394 specbind (Qinhibit_quit, Qt);
4395 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4396
4397 cons = noseeum_cons (buffer, form);
4398 opaque = (warning_string ? make_opaque_ptr (warning_string) : Qnil);
4399 GCPRO2 (cons, opaque);
4400 /* Qerror not Qt, so you can get a backtrace */
4401 tem = condition_case_1 (Qerror,
4402 catch_them_squirmers_eval_in_buffer, cons,
4403 caught_a_squirmer, opaque);
4404 free_cons (XCONS (cons));
4405 if (OPAQUEP (opaque))
4406 free_opaque_ptr (opaque);
4407 UNGCPRO;
4408
4409 /* gc_currently_forbidden = 0; */
4410 return unbind_to (speccount, tem);
4411 }
4412
4413 static Lisp_Object
4414 catch_them_squirmers_run_hook (Lisp_Object hook_symbol)
4415 {
4416 /* This function can GC */
4417 run_hook (hook_symbol);
4418 return Qnil;
4419 }
4420
4421 Lisp_Object
4422 run_hook_trapping_errors (CONST char *warning_string, Lisp_Object hook_symbol)
4423 {
4424 int speccount = specpdl_depth ();
4425 Lisp_Object tem;
4426 Lisp_Object opaque;
4427 struct gcpro gcpro1;
4428
4429 if (!initialized || preparing_for_armageddon)
4430 return Qnil;
4431 tem = find_symbol_value (hook_symbol);
4432 if (NILP (tem) || UNBOUNDP (tem))
4433 return Qnil;
4434
4435 specbind (Qinhibit_quit, Qt);
4436
4437 opaque = (warning_string ? make_opaque_ptr (warning_string) : Qnil);
4438 GCPRO1 (opaque);
4439 /* Qerror not Qt, so you can get a backtrace */
4440 tem = condition_case_1 (Qerror,
4441 catch_them_squirmers_run_hook, hook_symbol,
4442 caught_a_squirmer, opaque);
4443 if (OPAQUEP (opaque))
4444 free_opaque_ptr (opaque);
4445 UNGCPRO;
4446
4447 return unbind_to (speccount, tem);
4448 }
4449
4450 /* Same as run_hook_trapping_errors() but also set the hook to nil
4451 if an error occurs. */
4452
4453 Lisp_Object
4454 safe_run_hook_trapping_errors (CONST char *warning_string,
4455 Lisp_Object hook_symbol,
4456 int allow_quit)
4457 {
4458 int speccount = specpdl_depth ();
4459 Lisp_Object tem;
4460 Lisp_Object cons = Qnil;
4461 struct gcpro gcpro1;
4462
4463 if (!initialized || preparing_for_armageddon)
4464 return Qnil;
4465 tem = find_symbol_value (hook_symbol);
4466 if (NILP (tem) || UNBOUNDP (tem))
4467 return Qnil;
4468
4469 if (!allow_quit)
4470 specbind (Qinhibit_quit, Qt);
4471
4472 cons = noseeum_cons (hook_symbol,
4473 warning_string ? make_opaque_ptr (warning_string)
4474 : Qnil);
4475 GCPRO1 (cons);
4476 /* Qerror not Qt, so you can get a backtrace */
4477 tem = condition_case_1 (Qerror,
4478 catch_them_squirmers_run_hook,
4479 hook_symbol,
4480 allow_quit ?
4481 allow_quit_safe_run_hook_caught_a_squirmer :
4482 safe_run_hook_caught_a_squirmer,
4483 cons);
4484 if (OPAQUEP (XCDR (cons)))
4485 free_opaque_ptr (XCDR (cons));
4486 free_cons (XCONS (cons));
4487 UNGCPRO;
4488
4489 return unbind_to (speccount, tem);
4490 }
4491
4492 static Lisp_Object
4493 catch_them_squirmers_call0 (Lisp_Object function)
4494 {
4495 /* This function can GC */
4496 return call0 (function);
4497 }
4498
4499 Lisp_Object
4500 call0_trapping_errors (CONST char *warning_string, Lisp_Object function)
4501 {
4502 int speccount = specpdl_depth ();
4503 Lisp_Object tem;
4504 Lisp_Object opaque = Qnil;
4505 struct gcpro gcpro1, gcpro2;
4506
4507 if (SYMBOLP (function))
4508 {
4509 tem = XSYMBOL (function)->function;
4510 if (NILP (tem) || UNBOUNDP (tem))
4511 return (Qnil);
4512 }
4513
4514 GCPRO2 (opaque, function);
4515 specbind (Qinhibit_quit, Qt);
4516 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4517
4518 opaque = (warning_string ? make_opaque_ptr (warning_string) : Qnil);
4519 /* Qerror not Qt, so you can get a backtrace */
4520 tem = condition_case_1 (Qerror,
4521 catch_them_squirmers_call0, function,
4522 caught_a_squirmer, opaque);
4523 if (OPAQUEP (opaque))
4524 free_opaque_ptr (opaque);
4525 UNGCPRO;
4526
4527 /* gc_currently_forbidden = 0; */
4528 return unbind_to (speccount, tem);
4529 }
4530
4531 static Lisp_Object
4532 catch_them_squirmers_call1 (Lisp_Object cons)
4533 {
4534 /* This function can GC */
4535 return call1 (XCAR (cons), XCDR (cons));
4536 }
4537
4538 static Lisp_Object
4539 catch_them_squirmers_call2 (Lisp_Object cons)
4540 {
4541 /* This function can GC */
4542 return call2 (XCAR (cons), XCAR (XCDR (cons)), XCAR (XCDR (XCDR (cons))));
4543 }
4544
4545 Lisp_Object
4546 call1_trapping_errors (CONST char *warning_string, Lisp_Object function,
4547 Lisp_Object object)
4548 {
4549 int speccount = specpdl_depth ();
4550 Lisp_Object tem;
4551 Lisp_Object cons = Qnil;
4552 Lisp_Object opaque = Qnil;
4553 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4554
4555 if (SYMBOLP (function))
4556 {
4557 tem = XSYMBOL (function)->function;
4558 if (NILP (tem) || UNBOUNDP (tem))
4559 return (Qnil);
4560 }
4561
4562 GCPRO4 (cons, opaque, function, object);
4563
4564 specbind (Qinhibit_quit, Qt);
4565 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4566
4567 cons = noseeum_cons (function, object);
4568 opaque = (warning_string ? make_opaque_ptr (warning_string) : Qnil);
4569 /* Qerror not Qt, so you can get a backtrace */
4570 tem = condition_case_1 (Qerror,
4571 catch_them_squirmers_call1, cons,
4572 caught_a_squirmer, opaque);
4573 if (OPAQUEP (opaque))
4574 free_opaque_ptr (opaque);
4575 free_cons (XCONS (cons));
4576 UNGCPRO;
4577
4578 /* gc_currently_forbidden = 0; */
4579 return unbind_to (speccount, tem);
4580 }
4581
4582 Lisp_Object
4583 call2_trapping_errors (CONST char *warning_string, Lisp_Object function,
4584 Lisp_Object object1, Lisp_Object object2)
4585 {
4586 int speccount = specpdl_depth ();
4587 Lisp_Object tem;
4588 Lisp_Object cons = Qnil;
4589 Lisp_Object opaque = Qnil;
4590 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
4591
4592 if (SYMBOLP (function))
4593 {
4594 tem = XSYMBOL (function)->function;
4595 if (NILP (tem) || UNBOUNDP (tem))
4596 return (Qnil);
4597 }
4598
4599 GCPRO5 (cons, opaque, function, object1, object2);
4600 specbind (Qinhibit_quit, Qt);
4601 /* gc_currently_forbidden = 1; Currently no reason to do this; */
4602
4603 cons = list3 (function, object1, object2);
4604 opaque = (warning_string ? make_opaque_ptr (warning_string) : Qnil);
4605 /* Qerror not Qt, so you can get a backtrace */
4606 tem = condition_case_1 (Qerror,
4607 catch_them_squirmers_call2, cons,
4608 caught_a_squirmer, opaque);
4609 if (OPAQUEP (opaque))
4610 free_opaque_ptr (opaque);
4611 free_list (cons);
4612 UNGCPRO;
4613
4614 /* gc_currently_forbidden = 0; */
4615 return unbind_to (speccount, tem);
4616 }
4617
4618
4619 /**********************************************************************/
4620 /* The special binding stack */
4621 /**********************************************************************/
4622
4623 static void
4624 grow_specpdl (void)
4625 {
4626 if (specpdl_size >= max_specpdl_size)
4627 {
4628 if (max_specpdl_size < 400)
4629 max_specpdl_size = 400;
4630 if (specpdl_size >= max_specpdl_size)
4631 {
4632 if (!NILP (Vdebug_on_error) || !NILP (Vdebug_on_signal))
4633 /* Leave room for some specpdl in the debugger. */
4634 max_specpdl_size = specpdl_size + 100;
4635 continuable_error
4636 ("Variable binding depth exceeds max-specpdl-size");
4637 }
4638 }
4639 specpdl_size *= 2;
4640 if (specpdl_size > max_specpdl_size)
4641 specpdl_size = max_specpdl_size;
4642 specpdl = ((struct specbinding *)
4643 xrealloc (specpdl, specpdl_size * sizeof (struct specbinding)));
4644 specpdl_ptr = specpdl + specpdl_depth_counter;
4645 }
4646
4647
4648 /* Handle unbinding buffer-local variables */
4649 static Lisp_Object
4650 specbind_unwind_local (Lisp_Object ovalue)
4651 {
4652 Lisp_Object current = Fcurrent_buffer ();
4653 Lisp_Object symbol = specpdl_ptr->symbol;
4654 struct Lisp_Cons *victim = XCONS (ovalue);
4655 Lisp_Object buf = get_buffer (victim->car, 0);
4656 ovalue = victim->cdr;
4657
4658 free_cons (victim);
4659
4660 if (NILP (buf))
4661 {
4662 /* Deleted buffer -- do nothing */
4663 }
4664 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0)
4665 {
4666 /* Was buffer-local when binding was made, now no longer is.
4667 * (kill-local-variable can do this.)
4668 * Do nothing in this case.
4669 */
4670 }
4671 else if (EQ (buf, current))
4672 Fset (symbol, ovalue);
4673 else
4674 {
4675 /* Urk! Somebody switched buffers */
4676 struct gcpro gcpro1;
4677 GCPRO1 (current);
4678 Fset_buffer (buf);
4679 Fset (symbol, ovalue);
4680 Fset_buffer (current);
4681 UNGCPRO;
4682 }
4683 return (symbol);
4684 }
4685
4686 static Lisp_Object
4687 specbind_unwind_wasnt_local (Lisp_Object buffer)
4688 {
4689 Lisp_Object current = Fcurrent_buffer ();
4690 Lisp_Object symbol = specpdl_ptr->symbol;
4691
4692 buffer = get_buffer (buffer, 0);
4693 if (NILP (buffer))
4694 {
4695 /* Deleted buffer -- do nothing */
4696 }
4697 else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0)
4698 {
4699 /* Was buffer-local when binding was made, now no longer is.
4700 * (kill-local-variable can do this.)
4701 * Do nothing in this case.
4702 */
4703 }
4704 else if (EQ (buffer, current))
4705 Fkill_local_variable (symbol);
4706 else
4707 {
4708 /* Urk! Somebody switched buffers */
4709 struct gcpro gcpro1;
4710 GCPRO1 (current);
4711 Fset_buffer (buffer);
4712 Fkill_local_variable (symbol);
4713 Fset_buffer (current);
4714 UNGCPRO;
4715 }
4716 return (symbol);
4717 }
4718
4719
4720 /* Don't want to include buffer.h just for this */
4721 extern struct buffer *current_buffer;
4722
4723 void
4724 specbind (Lisp_Object symbol, Lisp_Object value)
4725 {
4726 int buffer_local;
4727
4728 CHECK_SYMBOL (symbol);
4729
4730 if (specpdl_depth_counter >= specpdl_size)
4731 grow_specpdl ();
4732
4733 buffer_local = symbol_value_buffer_local_info (symbol, current_buffer);
4734 if (buffer_local == 0)
4735 {
4736 specpdl_ptr->old_value = find_symbol_value (symbol);
4737 specpdl_ptr->func = 0; /* Handled specially by unbind_to */
4738 }
4739 else if (buffer_local > 0)
4740 {
4741 /* Already buffer-local */
4742 specpdl_ptr->old_value = noseeum_cons (Fcurrent_buffer (),
4743 find_symbol_value (symbol));
4744 specpdl_ptr->func = specbind_unwind_local;
4745 }
4746 else
4747 {
4748 /* About to become buffer-local */
4749 specpdl_ptr->old_value = Fcurrent_buffer ();
4750 specpdl_ptr->func = specbind_unwind_wasnt_local;
4751 }
4752
4753 specpdl_ptr->symbol = symbol;
4754 specpdl_ptr++;
4755 specpdl_depth_counter++;
4756
4757 Fset (symbol, value);
4758 }
4759
4760 void
4761 record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg),
4762 Lisp_Object arg)
4763 {
4764 if (specpdl_depth_counter >= specpdl_size)
4765 grow_specpdl ();
4766 specpdl_ptr->func = function;
4767 specpdl_ptr->symbol = Qnil;
4768 specpdl_ptr->old_value = arg;
4769 specpdl_ptr++;
4770 specpdl_depth_counter++;
4771 }
4772
4773 extern int check_sigio (void);
4774
4775 Lisp_Object
4776 unbind_to (int count, Lisp_Object value)
4777 {
4778 int quitf;
4779 struct gcpro gcpro1;
4780
4781 GCPRO1 (value);
4782
4783 check_quit (); /* make Vquit_flag accurate */
4784 quitf = !NILP (Vquit_flag);
4785 Vquit_flag = Qnil;
4786
4787 while (specpdl_depth_counter != count)
4788 {
4789 Lisp_Object ovalue;
4790 --specpdl_ptr;
4791 --specpdl_depth_counter;
4792
4793 ovalue = specpdl_ptr->old_value;
4794 if (specpdl_ptr->func != 0)
4795 /* An unwind-protect */
4796 (*specpdl_ptr->func) (ovalue);
4797 else
4798 Fset (specpdl_ptr->symbol, ovalue);
4799
4800 #ifndef EXCEEDINGLY_QUESTIONABLE_CODE
4801 /* There should never be anything here for us to remove.
4802 If so, it indicates a logic error in Emacs. Catches
4803 should get removed when a throw or signal occurs, or
4804 when a catch or condition-case exits normally. But
4805 it's too dangerous to just remove this code. --ben */
4806
4807 /* Furthermore, this code is not in FSFmacs!!!
4808 Braino on mly's part? */
4809 /* If we're unwound past the pdlcount of a catch frame,
4810 that catch can't possibly still be valid. */
4811 while (catchlist && catchlist->pdlcount > specpdl_depth_counter)
4812 {
4813 catchlist = catchlist->next;
4814 /* Don't mess with gcprolist, backtrace_list here */
4815 }
4816 #endif
4817 }
4818 if (quitf)
4819 Vquit_flag = Qt;
4820
4821 UNGCPRO;
4822
4823 return (value);
4824 }
4825
4826
4827 int
4828 specpdl_depth (void)
4829 {
4830 return (specpdl_depth_counter);
4831 }
4832
4833
4834 /* Get the value of symbol's global binding, even if that binding is
4835 not now dynamically visible. May return Qunbound or magic values. */
4836
4837 Lisp_Object
4838 top_level_value (Lisp_Object symbol)
4839 {
4840 REGISTER struct specbinding *ptr = specpdl;
4841
4842 CHECK_SYMBOL (symbol);
4843 for (; ptr != specpdl_ptr; ptr++)
4844 {
4845 if (EQ (ptr->symbol, symbol))
4846 return ptr->old_value;
4847 }
4848 return XSYMBOL (symbol)->value;
4849 }
4850
4851 #if 0
4852
4853 Lisp_Object
4854 top_level_set (Lisp_Object symbol, Lisp_Object newval)
4855 {
4856 REGISTER struct specbinding *ptr = specpdl;
4857
4858 CHECK_SYMBOL (symbol);
4859 for (; ptr != specpdl_ptr; ptr++)
4860 {
4861 if (EQ (ptr->symbol, symbol))
4862 {
4863 ptr->old_value = newval;
4864 return newval;
4865 }
4866 }
4867 return Fset (symbol, newval);
4868 }
4869
4870 #endif /* 0 */
4871
4872
4873 /**********************************************************************/
4874 /* Backtraces */
4875 /**********************************************************************/
4876
4877 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0 /*
4878 Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
4879 The debugger is entered when that frame exits, if the flag is non-nil.
4880 */ )
4881 (level, flag)
4882 Lisp_Object level, flag;
4883 {
4884 REGISTER struct backtrace *backlist = backtrace_list;
4885 REGISTER int i;
4886
4887 CHECK_INT (level);
4888
4889 for (i = 0; backlist && i < XINT (level); i++)
4890 {
4891 backlist = backlist->next;
4892 }
4893
4894 if (backlist)
4895 backlist->debug_on_exit = !NILP (flag);
4896
4897 return flag;
4898 }
4899
4900 static void
4901 backtrace_specials (int speccount, int speclimit, Lisp_Object stream)
4902 {
4903 int printing_bindings = 0;
4904
4905 for (; speccount > speclimit; speccount--)
4906 {
4907 if (specpdl[speccount - 1].func == 0
4908 || specpdl[speccount - 1].func == specbind_unwind_local
4909 || specpdl[speccount - 1].func == specbind_unwind_wasnt_local)
4910 {
4911 write_c_string (((!printing_bindings) ? " # bind (" : " "),
4912 stream);
4913 Fprin1 (specpdl[speccount - 1].symbol, stream);
4914 printing_bindings = 1;
4915 }
4916 else
4917 {
4918 if (printing_bindings) write_c_string (")\n", stream);
4919 write_c_string (" # (unwind-protect ...)\n", stream);
4920 printing_bindings = 0;
4921 }
4922 }
4923 if (printing_bindings) write_c_string (")\n", stream);
4924 }
4925
4926 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 2, "" /*
4927 Print a trace of Lisp function calls currently active.
4928 Option arg STREAM specifies the output stream to send the backtrace to,
4929 and defaults to the value of `standard-output'. Optional second arg
4930 DETAILED means show places where currently active variable bindings,
4931 catches, condition-cases, and unwind-protects were made as well as
4932 function calls.
4933 */ )
4934 (stream, detailed)
4935 Lisp_Object stream, detailed;
4936 {
4937 struct backtrace *backlist = backtrace_list;
4938 struct catchtag *catches = catchlist;
4939 int speccount = specpdl_depth_counter;
4940
4941 int old_nl = print_escape_newlines;
4942 int old_pr = print_readably;
4943 Lisp_Object old_level = Vprint_level;
4944 Lisp_Object oiq = Vinhibit_quit;
4945 struct gcpro gcpro1, gcpro2;
4946
4947 /* We can't allow quits in here because that could cause the values
4948 of print_readably and print_escape_newlines to get screwed up.
4949 Normally we would use a record_unwind_protect but that would
4950 screw up the functioning of this function. */
4951 Vinhibit_quit = Qt;
4952
4953 entering_debugger = 0;
4954
4955 Vprint_level = make_int (3);
4956 print_readably = 0;
4957 print_escape_newlines = 1;
4958
4959 GCPRO2 (stream, old_level);
4960
4961 if (NILP (stream))
4962 stream = Vstandard_output;
4963 if (!noninteractive && (NILP (stream) || EQ (stream, Qt)))
4964 stream = Fselected_frame (Qnil);
4965
4966 for (;;)
4967 {
4968 if (!NILP (detailed) && catches && catches->backlist == backlist)
4969 {
4970 int catchpdl = catches->pdlcount;
4971 if (specpdl[catchpdl].func == condition_case_unwind
4972 && speccount > catchpdl)
4973 /* This is a condition-case catchpoint */
4974 catchpdl = catchpdl + 1;
4975
4976 backtrace_specials (speccount, catchpdl, stream);
4977
4978 speccount = catches->pdlcount;
4979 if (catchpdl == speccount)
4980 {
4981 write_c_string (" # (catch ", stream);
4982 Fprin1 (catches->tag, stream);
4983 write_c_string (" ...)\n", stream);
4984 }
4985 else
4986 {
4987 write_c_string (" # (condition-case ... . ", stream);
4988 Fprin1 (Fcdr (Fcar (catches->tag)), stream);
4989 write_c_string (")\n", stream);
4990 }
4991 catches = catches->next;
4992 }
4993 else if (!backlist)
4994 break;
4995 else
4996 {
4997 if (!NILP (detailed) && backlist->pdlcount < speccount)
4998 {
4999 backtrace_specials (speccount, backlist->pdlcount, stream);
5000 speccount = backlist->pdlcount;
5001 }
5002 write_c_string (((backlist->debug_on_exit) ? "* " : " "),
5003 stream);
5004 if (backlist->nargs == UNEVALLED)
5005 {
5006 Fprin1 (Fcons (*backlist->function, *backlist->args), stream);
5007 write_c_string ("\n", stream); /* from FSFmacs 19.30 */
5008 }
5009 else
5010 {
5011 Lisp_Object tem = *backlist->function;
5012 Fprin1 (tem, stream); /* This can QUIT */
5013 write_c_string ("(", stream);
5014 if (backlist->nargs == MANY)
5015 {
5016 int i;
5017 Lisp_Object tail = Qnil;
5018 struct gcpro ngcpro1;
5019
5020 NGCPRO1 (tail);
5021 for (tail = *backlist->args, i = 0;
5022 !NILP (tail);
5023 tail = Fcdr (tail), i++)
5024 {
5025 if (i != 0) write_c_string (" ", stream);
5026 Fprin1 (Fcar (tail), stream);
5027 }
5028 NUNGCPRO;
5029 }
5030 else
5031 {
5032 int i;
5033 for (i = 0; i < backlist->nargs; i++)
5034 {
5035 if (i != 0) write_c_string (" ", stream);
5036 Fprin1 (backlist->args[i], stream);
5037 }
5038 }
5039 }
5040 write_c_string (")\n", stream);
5041 backlist = backlist->next;
5042 }
5043 }
5044 Vprint_level = old_level;
5045 print_readably = old_pr;
5046 print_escape_newlines = old_nl;
5047 UNGCPRO;
5048 Vinhibit_quit = oiq;
5049 return Qnil;
5050 }
5051
5052
5053 DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, "" /*
5054 Return the function and arguments N frames up from current execution point.
5055 If that frame has not evaluated the arguments yet (or is a special form),
5056 the value is (nil FUNCTION ARG-FORMS...).
5057 If that frame has evaluated its arguments and called its function already,
5058 the value is (t FUNCTION ARG-VALUES...).
5059 A &rest arg is represented as the tail of the list ARG-VALUES.
5060 FUNCTION is whatever was supplied as car of evaluated list,
5061 or a lambda expression for macro calls.
5062 If N is more than the number of frames, the value is nil.
5063 */ )
5064 (nframes)
5065 Lisp_Object nframes;
5066 {
5067 REGISTER struct backtrace *backlist = backtrace_list;
5068 REGISTER int i;
5069 Lisp_Object tem;
5070
5071 CHECK_NATNUM (nframes);
5072
5073 /* Find the frame requested. */
5074 for (i = XINT (nframes); backlist && (i-- > 0);)
5075 backlist = backlist->next;
5076
5077 if (!backlist)
5078 return Qnil;
5079 if (backlist->nargs == UNEVALLED)
5080 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args));
5081 else
5082 {
5083 if (backlist->nargs == MANY)
5084 tem = *backlist->args;
5085 else
5086 tem = Flist (backlist->nargs, backlist->args);
5087
5088 return Fcons (Qt, Fcons (*backlist->function, tem));
5089 }
5090 }
5091
5092
5093 /**********************************************************************/
5094 /* Warnings */
5095 /**********************************************************************/
5096
5097 void
5098 warn_when_safe_lispobj (Lisp_Object class, Lisp_Object level,
5099 Lisp_Object obj)
5100 {
5101 obj = list1 (list3 (class, level, obj));
5102 if (NILP (Vpending_warnings))
5103 Vpending_warnings = Vpending_warnings_tail = obj;
5104 else
5105 {
5106 Fsetcdr (Vpending_warnings_tail, obj);
5107 Vpending_warnings_tail = obj;
5108 }
5109 }
5110
5111 /* #### This should probably accept Lisp objects; but then we have
5112 to make sure that Feval() isn't called, since it might not be safe.
5113
5114 An alternative approach is to just pass some non-string type of
5115 Lisp Object to warn_when_safe_lispobj(); `prin1-to-string' will
5116 automatically be called when it is safe to do so. */
5117
5118 void
5119 warn_when_safe (Lisp_Object class, Lisp_Object level, CONST char *fmt, ...)
5120 {
5121 Lisp_Object obj;
5122 va_list args;
5123
5124 va_start (args, fmt);
5125 obj = emacs_doprnt_string_va ((CONST Bufbyte *) GETTEXT (fmt),
5126 Qnil, -1, args);
5127 va_end (args);
5128
5129 warn_when_safe_lispobj (class, level, obj);
5130 }
5131
5132
5133
5134
5135 /**********************************************************************/
5136 /* Initialization */
5137 /**********************************************************************/
5138
5139 void
5140 syms_of_eval (void)
5141 {
5142 defsymbol (&Qinhibit_quit, "inhibit-quit");
5143 defsymbol (&Qautoload, "autoload");
5144 defsymbol (&Qdebug_on_error, "debug-on-error");
5145 defsymbol (&Qstack_trace_on_error, "stack-trace-on-error");
5146 defsymbol (&Qdebug_on_signal, "debug-on-signal");
5147 defsymbol (&Qstack_trace_on_signal, "stack-trace-on-signal");
5148 defsymbol (&Qdebugger, "debugger");
5149 defsymbol (&Qmacro, "macro");
5150 defsymbol (&Qand_rest, "&rest");
5151 defsymbol (&Qand_optional, "&optional");
5152 /* Note that the process code also uses Qexit */
5153 defsymbol (&Qexit, "exit");
5154 defsymbol (&Qsetq, "setq");
5155 defsymbol (&Qinteractive, "interactive");
5156 defsymbol (&Qcommandp, "commandp");
5157 defsymbol (&Qdefun, "defun");
5158 defsymbol (&Qprogn, "progn");
5159 defsymbol (&Qvalues, "values");
5160 defsymbol (&Qdisplay_warning, "display-warning");
5161 defsymbol (&Qrun_hooks, "run-hooks");
5162
5163 defsubr (&Sor);
5164 defsubr (&Sand);
5165 defsubr (&Sif);
5166 defsubr (&Scond);
5167 defsubr (&Sprogn);
5168 defsubr (&Sprog1);
5169 defsubr (&Sprog2);
5170 defsubr (&Ssetq);
5171 defsubr (&Squote);
5172 defsubr (&Sfunction);
5173 defsubr (&Sdefun);
5174 defsubr (&Sdefmacro);
5175 defsubr (&Sdefvar);
5176 defsubr (&Sdefconst);
5177 defsubr (&Suser_variable_p);
5178 defsubr (&Slet);
5179 defsubr (&SletX);
5180 defsubr (&Swhile);
5181 defsubr (&Smacroexpand_internal);
5182 defsubr (&Scatch);
5183 defsubr (&Sthrow);
5184 defsubr (&Sunwind_protect);
5185 defsubr (&Scondition_case);
5186 defsubr (&Scall_with_condition_handler);
5187 defsubr (&Ssignal);
5188 defsubr (&Sinteractive_p);
5189 defsubr (&Scommandp);
5190 defsubr (&Scommand_execute);
5191 defsubr (&Sautoload);
5192 defsubr (&Seval);
5193 defsubr (&Sapply);
5194 defsubr (&Sfuncall);
5195 defsubr (&Sfunction_min_args);
5196 defsubr (&Sfunction_max_args);
5197 defsubr (&Srun_hooks);
5198 defsubr (&Srun_hook_with_args);
5199 defsubr (&Srun_hook_with_args_until_success);
5200 defsubr (&Srun_hook_with_args_until_failure);
5201 defsubr (&Sfetch_bytecode);
5202 defsubr (&Sbacktrace_debug);
5203 defsubr (&Sbacktrace);
5204 defsubr (&Sbacktrace_frame);
5205 }
5206
5207 void
5208 reinit_eval (void)
5209 {
5210 specpdl_ptr = specpdl;
5211 specpdl_depth_counter = 0;
5212 catchlist = 0;
5213 Vcondition_handlers = Qnil;
5214 backtrace_list = 0;
5215 Vquit_flag = Qnil;
5216 debug_on_next_call = 0;
5217 lisp_eval_depth = 0;
5218 entering_debugger = 0;
5219 }
5220
5221 void
5222 vars_of_eval (void)
5223 {
5224 DEFVAR_INT ("max-specpdl-size", &max_specpdl_size /*
5225 Limit on number of Lisp variable bindings & unwind-protects before error.
5226 */ );
5227
5228 DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth /*
5229 Limit on depth in `eval', `apply' and `funcall' before error.
5230 This limit is to catch infinite recursions for you before they cause
5231 actual stack overflow in C, which would be fatal for Emacs.
5232 You can safely make it considerably larger than its default value,
5233 if that proves inconveniently small.
5234 */ );
5235
5236 DEFVAR_LISP ("quit-flag", &Vquit_flag /*
5237 Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
5238 Typing C-G sets `quit-flag' non-nil, regardless of `inhibit-quit'.
5239 */ );
5240 Vquit_flag = Qnil;
5241
5242 DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit /*
5243 Non-nil inhibits C-g quitting from happening immediately.
5244 Note that `quit-flag' will still be set by typing C-g,
5245 so a quit will be signalled as soon as `inhibit-quit' is nil.
5246 To prevent this happening, set `quit-flag' to nil
5247 before making `inhibit-quit' nil. The value of `inhibit-quit' is
5248 ignored if a critical quit is requested by typing control-shift-G in
5249 an X frame.
5250 */ );
5251 Vinhibit_quit = Qnil;
5252
5253 DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error /*
5254 *Non-nil means automatically display a backtrace buffer
5255 after any error that is not handled by a `condition-case'.
5256 If the value is a list, an error only means to display a backtrace
5257 if one of its condition symbols appears in the list.
5258 See also variable `stack-trace-on-signal'.
5259 */ );
5260 Vstack_trace_on_error = Qnil;
5261
5262 DEFVAR_LISP ("stack-trace-on-signal", &Vstack_trace_on_signal /*
5263 *Non-nil means automatically display a backtrace buffer
5264 after any error that is signalled, whether or not it is handled by
5265 a `condition-case'.
5266 If the value is a list, an error only means to display a backtrace
5267 if one of its condition symbols appears in the list.
5268 See also variable `stack-trace-on-error'.
5269 */ );
5270 Vstack_trace_on_signal = Qnil;
5271
5272 DEFVAR_LISP ("debug-on-error", &Vdebug_on_error /*
5273 *Non-nil means enter debugger if an unhandled error is signalled.
5274 The debugger will not be entered if the error is handled by
5275 a `condition-case'.
5276 If the value is a list, an error only means to enter the debugger
5277 if one of its condition symbols appears in the list.
5278 See also variables `debug-on-quit' and `debug-on-signal'.
5279 */ );
5280 Vdebug_on_error = Qnil;
5281
5282 DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal /*
5283 *Non-nil means enter debugger if an error is signalled.
5284 The debugger will be entered whether or not the error is handled by
5285 a `condition-case'.
5286 If the value is a list, an error only means to enter the debugger
5287 if one of its condition symbols appears in the list.
5288 See also variable `debug-on-quit'.
5289 */ );
5290 Vdebug_on_signal = Qnil;
5291
5292 DEFVAR_BOOL ("debug-on-quit", &debug_on_quit /*
5293 *Non-nil means enter debugger if quit is signalled (C-G, for example).
5294 Does not apply if quit is handled by a `condition-case'. Entering the
5295 debugger can also be achieved at any time (for X11 console) by typing
5296 control-shift-G to signal a critical quit.
5297 */ );
5298 debug_on_quit = 0;
5299
5300 DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call /*
5301 Non-nil means enter debugger before next `eval', `apply' or `funcall'.
5302 */ );
5303
5304 DEFVAR_LISP ("debugger", &Vdebugger /*
5305 Function to call to invoke debugger.
5306 If due to frame exit, args are `exit' and the value being returned;
5307 this function's value will be returned instead of that.
5308 If due to error, args are `error' and a list of the args to `signal'.
5309 If due to `apply' or `funcall' entry, one arg, `lambda'.
5310 If due to `eval' entry, one arg, t.
5311 */ );
5312 Vdebugger = Qnil;
5313
5314 preparing_for_armageddon = 0;
5315
5316 staticpro (&Vpending_warnings);
5317 Vpending_warnings = Qnil;
5318 Vpending_warnings_tail = Qnil; /* no need to protect this */
5319
5320 in_warnings = 0;
5321
5322 staticpro (&Vautoload_queue);
5323 Vautoload_queue = Qnil;
5324
5325 staticpro (&Vcondition_handlers);
5326
5327 staticpro (&Vcurrent_warning_class);
5328 Vcurrent_warning_class = Qnil;
5329
5330 staticpro (&Vcurrent_error_state);
5331 Vcurrent_error_state = Qnil; /* errors as normal */
5332
5333 Qunbound_suspended_errors_tag = make_opaque_long (0);
5334 staticpro (&Qunbound_suspended_errors_tag);
5335
5336 specpdl_size = 50;
5337 specpdl_depth_counter = 0;
5338 specpdl = (struct specbinding *)
5339 xmalloc (specpdl_size * sizeof (struct specbinding));
5340 /* XEmacs change: increase these values. */
5341 max_specpdl_size = 3000;
5342 max_lisp_eval_depth = 500;
5343 throw_level = 0;
5344
5345 reinit_eval ();
5346 }