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