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