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