aboutsummaryrefslogtreecommitdiffstats
path: root/src/eval.c
diff options
context:
space:
mode:
authorEli Zaretskii2013-09-05 11:01:04 +0300
committerEli Zaretskii2013-09-05 11:01:04 +0300
commit41306318777a942420bc4feadbfacf662ea179dc (patch)
tree669e5cca02f95d6064ce73c0d3fbbf91b8c8b563 /src/eval.c
parent141f1ff7a40cda10f0558e891dd196a943a5082e (diff)
parent257b3b03cb1cff917e0b3b7832ad3eab5b59f257 (diff)
downloademacs-41306318777a942420bc4feadbfacf662ea179dc.tar.gz
emacs-41306318777a942420bc4feadbfacf662ea179dc.zip
Merge from trunk after a lot of time.
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c1611
1 files changed, 942 insertions, 669 deletions
diff --git a/src/eval.c b/src/eval.c
index 1da841a4073..1ce14ae94a6 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -1,5 +1,6 @@
1/* Evaluator for GNU Emacs Lisp interpreter. 1/* Evaluator for GNU Emacs Lisp interpreter.
2 Copyright (C) 1985-1987, 1993-1995, 1999-2012 Free Software Foundation, Inc. 2 Copyright (C) 1985-1987, 1993-1995, 1999-2013 Free Software
3 Foundation, Inc.
3 4
4This file is part of GNU Emacs. 5This file is part of GNU Emacs.
5 6
@@ -19,7 +20,6 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 20
20#include <config.h> 21#include <config.h>
21#include <limits.h> 22#include <limits.h>
22#include <setjmp.h>
23#include <stdio.h> 23#include <stdio.h>
24#include "lisp.h" 24#include "lisp.h"
25#include "blockinput.h" 25#include "blockinput.h"
@@ -32,18 +32,6 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
32#include "xterm.h" 32#include "xterm.h"
33#endif 33#endif
34 34
35struct 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
45static struct backtrace *backtrace_list;
46
47#if !BYTE_MARK_STACK 35#if !BYTE_MARK_STACK
48static 36static
49#endif 37#endif
@@ -69,7 +57,7 @@ Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp;
69Lisp_Object Qinhibit_quit; 57Lisp_Object Qinhibit_quit;
70Lisp_Object Qand_rest; 58Lisp_Object Qand_rest;
71static Lisp_Object Qand_optional; 59static Lisp_Object Qand_optional;
72static Lisp_Object Qdebug_on_error; 60static Lisp_Object Qinhibit_debugger;
73static Lisp_Object Qdeclare; 61static Lisp_Object Qdeclare;
74Lisp_Object Qinternal_interpreter_environment, Qclosure; 62Lisp_Object Qinternal_interpreter_environment, Qclosure;
75 63
@@ -88,17 +76,19 @@ Lisp_Object Vrun_hooks;
88 76
89Lisp_Object Vautoload_queue; 77Lisp_Object Vautoload_queue;
90 78
91/* Current number of specbindings allocated in specpdl. */ 79/* Current number of specbindings allocated in specpdl, not counting
80 the dummy entry specpdl[-1]. */
92 81
93ptrdiff_t specpdl_size; 82ptrdiff_t specpdl_size;
94 83
95/* Pointer to beginning of specpdl. */ 84/* Pointer to beginning of specpdl. A dummy entry specpdl[-1] exists
85 only so that its address can be taken. */
96 86
97struct specbinding *specpdl; 87union specbinding *specpdl;
98 88
99/* Pointer to first unused element in specpdl. */ 89/* Pointer to first unused element in specpdl. */
100 90
101struct specbinding *specpdl_ptr; 91union specbinding *specpdl_ptr;
102 92
103/* Depth in Lisp evaluations and function calls. */ 93/* Depth in Lisp evaluations and function calls. */
104 94
@@ -115,34 +105,143 @@ static EMACS_INT when_entered_debugger;
115 105
116/* The function from which the last `signal' was called. Set in 106/* The function from which the last `signal' was called. Set in
117 Fsignal. */ 107 Fsignal. */
118 108/* FIXME: We should probably get rid of this! */
119Lisp_Object Vsignaling_function; 109Lisp_Object Vsignaling_function;
120 110
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
125int handling_signal;
126
127/* If non-nil, Lisp code must not be run since some part of Emacs is 111/* If non-nil, Lisp code must not be run since some part of Emacs is
128 in an inconsistent state. Currently, x-create-frame uses this to 112 in an inconsistent state. Currently, x-create-frame uses this to
129 avoid triggering window-configuration-change-hook while the new 113 avoid triggering window-configuration-change-hook while the new
130 frame is half-initialized. */ 114 frame is half-initialized. */
131Lisp_Object inhibit_lisp_code; 115Lisp_Object inhibit_lisp_code;
132 116
117/* These would ordinarily be static, but they need to be visible to GDB. */
118bool backtrace_p (union specbinding *) EXTERNALLY_VISIBLE;
119Lisp_Object *backtrace_args (union specbinding *) EXTERNALLY_VISIBLE;
120Lisp_Object backtrace_function (union specbinding *) EXTERNALLY_VISIBLE;
121union specbinding *backtrace_next (union specbinding *) EXTERNALLY_VISIBLE;
122union specbinding *backtrace_top (void) EXTERNALLY_VISIBLE;
123
133static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *); 124static Lisp_Object funcall_lambda (Lisp_Object, ptrdiff_t, Lisp_Object *);
134static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
135static int interactive_p (int);
136static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args); 125static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
137static Lisp_Object Ffetch_bytecode (Lisp_Object); 126
138 127static Lisp_Object
128specpdl_symbol (union specbinding *pdl)
129{
130 eassert (pdl->kind >= SPECPDL_LET);
131 return pdl->let.symbol;
132}
133
134static Lisp_Object
135specpdl_old_value (union specbinding *pdl)
136{
137 eassert (pdl->kind >= SPECPDL_LET);
138 return pdl->let.old_value;
139}
140
141static void
142set_specpdl_old_value (union specbinding *pdl, Lisp_Object val)
143{
144 eassert (pdl->kind >= SPECPDL_LET);
145 pdl->let.old_value = val;
146}
147
148static Lisp_Object
149specpdl_where (union specbinding *pdl)
150{
151 eassert (pdl->kind > SPECPDL_LET);
152 return pdl->let.where;
153}
154
155static Lisp_Object
156specpdl_arg (union specbinding *pdl)
157{
158 eassert (pdl->kind == SPECPDL_UNWIND);
159 return pdl->unwind.arg;
160}
161
162Lisp_Object
163backtrace_function (union specbinding *pdl)
164{
165 eassert (pdl->kind == SPECPDL_BACKTRACE);
166 return pdl->bt.function;
167}
168
169static ptrdiff_t
170backtrace_nargs (union specbinding *pdl)
171{
172 eassert (pdl->kind == SPECPDL_BACKTRACE);
173 return pdl->bt.nargs;
174}
175
176Lisp_Object *
177backtrace_args (union specbinding *pdl)
178{
179 eassert (pdl->kind == SPECPDL_BACKTRACE);
180 return pdl->bt.args;
181}
182
183static bool
184backtrace_debug_on_exit (union specbinding *pdl)
185{
186 eassert (pdl->kind == SPECPDL_BACKTRACE);
187 return pdl->bt.debug_on_exit;
188}
189
190/* Functions to modify slots of backtrace records. */
191
192static void
193set_backtrace_args (union specbinding *pdl, Lisp_Object *args)
194{
195 eassert (pdl->kind == SPECPDL_BACKTRACE);
196 pdl->bt.args = args;
197}
198
199static void
200set_backtrace_nargs (union specbinding *pdl, ptrdiff_t n)
201{
202 eassert (pdl->kind == SPECPDL_BACKTRACE);
203 pdl->bt.nargs = n;
204}
205
206static void
207set_backtrace_debug_on_exit (union specbinding *pdl, bool doe)
208{
209 eassert (pdl->kind == SPECPDL_BACKTRACE);
210 pdl->bt.debug_on_exit = doe;
211}
212
213/* Helper functions to scan the backtrace. */
214
215bool
216backtrace_p (union specbinding *pdl)
217{ return pdl >= specpdl; }
218
219union specbinding *
220backtrace_top (void)
221{
222 union specbinding *pdl = specpdl_ptr - 1;
223 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
224 pdl--;
225 return pdl;
226}
227
228union specbinding *
229backtrace_next (union specbinding *pdl)
230{
231 pdl--;
232 while (backtrace_p (pdl) && pdl->kind != SPECPDL_BACKTRACE)
233 pdl--;
234 return pdl;
235}
236
237
139void 238void
140init_eval_once (void) 239init_eval_once (void)
141{ 240{
142 enum { size = 50 }; 241 enum { size = 50 };
143 specpdl = (struct specbinding *) xmalloc (size * sizeof (struct specbinding)); 242 union specbinding *pdlvec = xmalloc ((size + 1) * sizeof *specpdl);
144 specpdl_size = size; 243 specpdl_size = size;
145 specpdl_ptr = specpdl; 244 specpdl = specpdl_ptr = pdlvec + 1;
146 /* Don't forget to update docs (lispref node "Local Variables"). */ 245 /* Don't forget to update docs (lispref node "Local Variables"). */
147 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */ 246 max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el. */
148 max_lisp_eval_depth = 600; 247 max_lisp_eval_depth = 600;
@@ -156,7 +255,6 @@ init_eval (void)
156 specpdl_ptr = specpdl; 255 specpdl_ptr = specpdl;
157 catchlist = 0; 256 catchlist = 0;
158 handlerlist = 0; 257 handlerlist = 0;
159 backtrace_list = 0;
160 Vquit_flag = Qnil; 258 Vquit_flag = Qnil;
161 debug_on_next_call = 0; 259 debug_on_next_call = 0;
162 lisp_eval_depth = 0; 260 lisp_eval_depth = 0;
@@ -169,20 +267,19 @@ init_eval (void)
169 267
170/* Unwind-protect function used by call_debugger. */ 268/* Unwind-protect function used by call_debugger. */
171 269
172static Lisp_Object 270static void
173restore_stack_limits (Lisp_Object data) 271restore_stack_limits (Lisp_Object data)
174{ 272{
175 max_specpdl_size = XINT (XCAR (data)); 273 max_specpdl_size = XINT (XCAR (data));
176 max_lisp_eval_depth = XINT (XCDR (data)); 274 max_lisp_eval_depth = XINT (XCDR (data));
177 return Qnil;
178} 275}
179 276
180/* Call the Lisp debugger, giving it argument ARG. */ 277/* Call the Lisp debugger, giving it argument ARG. */
181 278
182static Lisp_Object 279Lisp_Object
183call_debugger (Lisp_Object arg) 280call_debugger (Lisp_Object arg)
184{ 281{
185 int debug_while_redisplaying; 282 bool debug_while_redisplaying;
186 ptrdiff_t count = SPECPDL_INDEX (); 283 ptrdiff_t count = SPECPDL_INDEX ();
187 Lisp_Object val; 284 Lisp_Object val;
188 EMACS_INT old_max = max_specpdl_size; 285 EMACS_INT old_max = max_specpdl_size;
@@ -217,7 +314,7 @@ call_debugger (Lisp_Object arg)
217 specbind (intern ("debugger-may-continue"), 314 specbind (intern ("debugger-may-continue"),
218 debug_while_redisplaying ? Qnil : Qt); 315 debug_while_redisplaying ? Qnil : Qt);
219 specbind (Qinhibit_redisplay, Qnil); 316 specbind (Qinhibit_redisplay, Qnil);
220 specbind (Qdebug_on_error, Qnil); 317 specbind (Qinhibit_debugger, Qt);
221 318
222#if 0 /* Binding this prevents execution of Lisp code during 319#if 0 /* Binding this prevents execution of Lisp code during
223 redisplay, which necessarily leads to display problems. */ 320 redisplay, which necessarily leads to display problems. */
@@ -239,8 +336,8 @@ static void
239do_debug_on_call (Lisp_Object code) 336do_debug_on_call (Lisp_Object code)
240{ 337{
241 debug_on_next_call = 0; 338 debug_on_next_call = 0;
242 backtrace_list->debug_on_exit = 1; 339 set_backtrace_debug_on_exit (specpdl_ptr - 1, true);
243 call_debugger (Fcons (code, Qnil)); 340 call_debugger (list1 (code));
244} 341}
245 342
246/* NOTE!!! Every function that can call EVAL must protect its args 343/* NOTE!!! Every function that can call EVAL must protect its args
@@ -303,16 +400,16 @@ If COND yields nil, and there are no ELSE's, the value is nil.
303usage: (if COND THEN ELSE...) */) 400usage: (if COND THEN ELSE...) */)
304 (Lisp_Object args) 401 (Lisp_Object args)
305{ 402{
306 register Lisp_Object cond; 403 Lisp_Object cond;
307 struct gcpro gcpro1; 404 struct gcpro gcpro1;
308 405
309 GCPRO1 (args); 406 GCPRO1 (args);
310 cond = eval_sub (Fcar (args)); 407 cond = eval_sub (XCAR (args));
311 UNGCPRO; 408 UNGCPRO;
312 409
313 if (!NILP (cond)) 410 if (!NILP (cond))
314 return eval_sub (Fcar (Fcdr (args))); 411 return eval_sub (Fcar (XCDR (args)));
315 return Fprogn (Fcdr (Fcdr (args))); 412 return Fprogn (XCDR (XCDR (args)));
316} 413}
317 414
318DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0, 415DEFUN ("cond", Fcond, Scond, 0, UNEVALLED, 0,
@@ -327,18 +424,17 @@ CONDITION's value if non-nil is returned from the cond-form.
327usage: (cond CLAUSES...) */) 424usage: (cond CLAUSES...) */)
328 (Lisp_Object args) 425 (Lisp_Object args)
329{ 426{
330 register Lisp_Object clause, val; 427 Lisp_Object val = args;
331 struct gcpro gcpro1; 428 struct gcpro gcpro1;
332 429
333 val = Qnil;
334 GCPRO1 (args); 430 GCPRO1 (args);
335 while (!NILP (args)) 431 while (CONSP (args))
336 { 432 {
337 clause = Fcar (args); 433 Lisp_Object clause = XCAR (args);
338 val = eval_sub (Fcar (clause)); 434 val = eval_sub (Fcar (clause));
339 if (!NILP (val)) 435 if (!NILP (val))
340 { 436 {
341 if (!EQ (XCDR (clause), Qnil)) 437 if (!NILP (XCDR (clause)))
342 val = Fprogn (XCDR (clause)); 438 val = Fprogn (XCDR (clause));
343 break; 439 break;
344 } 440 }
@@ -352,23 +448,32 @@ usage: (cond CLAUSES...) */)
352DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0, 448DEFUN ("progn", Fprogn, Sprogn, 0, UNEVALLED, 0,
353 doc: /* Eval BODY forms sequentially and return value of last one. 449 doc: /* Eval BODY forms sequentially and return value of last one.
354usage: (progn BODY...) */) 450usage: (progn BODY...) */)
355 (Lisp_Object args) 451 (Lisp_Object body)
356{ 452{
357 register Lisp_Object val = Qnil; 453 Lisp_Object val = Qnil;
358 struct gcpro gcpro1; 454 struct gcpro gcpro1;
359 455
360 GCPRO1 (args); 456 GCPRO1 (body);
361 457
362 while (CONSP (args)) 458 while (CONSP (body))
363 { 459 {
364 val = eval_sub (XCAR (args)); 460 val = eval_sub (XCAR (body));
365 args = XCDR (args); 461 body = XCDR (body);
366 } 462 }
367 463
368 UNGCPRO; 464 UNGCPRO;
369 return val; 465 return val;
370} 466}
371 467
468/* Evaluate BODY sequentially, discarding its value. Suitable for
469 record_unwind_protect. */
470
471void
472unwind_body (Lisp_Object body)
473{
474 Fprogn (body);
475}
476
372DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0, 477DEFUN ("prog1", Fprog1, Sprog1, 1, UNEVALLED, 0,
373 doc: /* Eval FIRST and BODY sequentially; return value from FIRST. 478 doc: /* Eval FIRST and BODY sequentially; return value from FIRST.
374The value of FIRST is saved during the evaluation of the remaining args, 479The value of FIRST is saved during the evaluation of the remaining args,
@@ -377,11 +482,11 @@ usage: (prog1 FIRST BODY...) */)
377 (Lisp_Object args) 482 (Lisp_Object args)
378{ 483{
379 Lisp_Object val; 484 Lisp_Object val;
380 register Lisp_Object args_left; 485 Lisp_Object args_left;
381 struct gcpro gcpro1, gcpro2; 486 struct gcpro gcpro1, gcpro2;
382 487
383 args_left = args; 488 args_left = args;
384 val = Qnil; 489 val = args;
385 GCPRO2 (args, val); 490 GCPRO2 (args, val);
386 491
387 val = eval_sub (XCAR (args_left)); 492 val = eval_sub (XCAR (args_left));
@@ -418,36 +523,37 @@ The return value of the `setq' form is the value of the last VAL.
418usage: (setq [SYM VAL]...) */) 523usage: (setq [SYM VAL]...) */)
419 (Lisp_Object args) 524 (Lisp_Object args)
420{ 525{
421 register Lisp_Object args_left; 526 Lisp_Object val, sym, lex_binding;
422 register Lisp_Object val, sym, lex_binding;
423 struct gcpro gcpro1;
424 527
425 if (NILP (args)) 528 val = args;
426 return Qnil; 529 if (CONSP (args))
427
428 args_left = args;
429 GCPRO1 (args);
430
431 do
432 { 530 {
433 val = eval_sub (Fcar (Fcdr (args_left))); 531 Lisp_Object args_left = args;
434 sym = Fcar (args_left); 532 struct gcpro gcpro1;
533 GCPRO1 (args);
435 534
436 /* Like for eval_sub, we do not check declared_special here since 535 do
437 it's been done when let-binding. */ 536 {
438 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */ 537 val = eval_sub (Fcar (XCDR (args_left)));
439 && SYMBOLP (sym) 538 sym = XCAR (args_left);
440 && !NILP (lex_binding 539
441 = Fassq (sym, Vinternal_interpreter_environment))) 540 /* Like for eval_sub, we do not check declared_special here since
442 XSETCDR (lex_binding, val); /* SYM is lexically bound. */ 541 it's been done when let-binding. */
443 else 542 if (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
444 Fset (sym, val); /* SYM is dynamically bound. */ 543 && SYMBOLP (sym)
544 && !NILP (lex_binding
545 = Fassq (sym, Vinternal_interpreter_environment)))
546 XSETCDR (lex_binding, val); /* SYM is lexically bound. */
547 else
548 Fset (sym, val); /* SYM is dynamically bound. */
445 549
446 args_left = Fcdr (Fcdr (args_left)); 550 args_left = Fcdr (XCDR (args_left));
551 }
552 while (CONSP (args_left));
553
554 UNGCPRO;
447 } 555 }
448 while (!NILP (args_left));
449 556
450 UNGCPRO;
451 return val; 557 return val;
452} 558}
453 559
@@ -464,9 +570,9 @@ of unexpected results when a quoted object is modified.
464usage: (quote ARG) */) 570usage: (quote ARG) */)
465 (Lisp_Object args) 571 (Lisp_Object args)
466{ 572{
467 if (!NILP (Fcdr (args))) 573 if (CONSP (XCDR (args)))
468 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args)); 574 xsignal2 (Qwrong_number_of_arguments, Qquote, Flength (args));
469 return Fcar (args); 575 return XCAR (args);
470} 576}
471 577
472DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0, 578DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0,
@@ -478,7 +584,7 @@ usage: (function ARG) */)
478{ 584{
479 Lisp_Object quoted = XCAR (args); 585 Lisp_Object quoted = XCAR (args);
480 586
481 if (!NILP (Fcdr (args))) 587 if (CONSP (XCDR (args)))
482 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args)); 588 xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
483 589
484 if (!NILP (Vinternal_interpreter_environment) 590 if (!NILP (Vinternal_interpreter_environment)
@@ -494,105 +600,6 @@ usage: (function ARG) */)
494} 600}
495 601
496 602
497DEFUN ("interactive-p", Finteractive_p, Sinteractive_p, 0, 0, 0,
498 doc: /* Return t if the containing function was run directly by user input.
499This means that the function was called with `call-interactively'
500\(which includes being called as the binding of a key)
501and input is currently coming from the keyboard (not a keyboard macro),
502and Emacs is not running in batch mode (`noninteractive' is nil).
503
504The only known proper use of `interactive-p' is in deciding whether to
505display a helpful message, or how to display it. If you're thinking
506of using it for any other purpose, it is quite likely that you're
507making a mistake. Think: what do you want to do when the command is
508called from a keyboard macro?
509
510To test whether your function was called with `call-interactively',
511either (i) add an extra optional argument and give it an `interactive'
512spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
513use `called-interactively-p'. */)
514 (void)
515{
516 return interactive_p (1) ? Qt : Qnil;
517}
518
519
520DEFUN ("called-interactively-p", Fcalled_interactively_p, Scalled_interactively_p, 0, 1, 0,
521 doc: /* Return t if the containing function was called by `call-interactively'.
522If KIND is `interactive', then only return t if the call was made
523interactively by the user, i.e. not in `noninteractive' mode nor
524when `executing-kbd-macro'.
525If KIND is `any', on the other hand, it will return t for any kind of
526interactive call, including being called as the binding of a key, or
527from a keyboard macro, or in `noninteractive' mode.
528
529The only known proper use of `interactive' for KIND is in deciding
530whether to display a helpful message, or how to display it. If you're
531thinking of using it for any other purpose, it is quite likely that
532you're making a mistake. Think: what do you want to do when the
533command is called from a keyboard macro?
534
535This function is meant for implementing advice and other
536function-modifying features. Instead of using this, it is sometimes
537cleaner to give your function an extra optional argument whose
538`interactive' spec specifies non-nil unconditionally (\"p\" is a good
539way to do this), or via (not (or executing-kbd-macro noninteractive)). */)
540 (Lisp_Object kind)
541{
542 return ((INTERACTIVE || !EQ (kind, intern ("interactive")))
543 && interactive_p (1)) ? Qt : Qnil;
544}
545
546
547/* Return 1 if function in which this appears was called using
548 call-interactively.
549
550 EXCLUDE_SUBRS_P non-zero means always return 0 if the function
551 called is a built-in. */
552
553static int
554interactive_p (int exclude_subrs_p)
555{
556 struct backtrace *btp;
557 Lisp_Object fun;
558
559 btp = backtrace_list;
560
561 /* If this isn't a byte-compiled function, there may be a frame at
562 the top for Finteractive_p. If so, skip it. */
563 fun = Findirect_function (*btp->function, Qnil);
564 if (SUBRP (fun) && (XSUBR (fun) == &Sinteractive_p
565 || XSUBR (fun) == &Scalled_interactively_p))
566 btp = btp->next;
567
568 /* If we're running an Emacs 18-style byte-compiled function, there
569 may be a frame for Fbytecode at the top level. In any version of
570 Emacs there can be Fbytecode frames for subexpressions evaluated
571 inside catch and condition-case. Skip past them.
572
573 If this isn't a byte-compiled function, then we may now be
574 looking at several frames for special forms. Skip past them. */
575 while (btp
576 && (EQ (*btp->function, Qbytecode)
577 || btp->nargs == UNEVALLED))
578 btp = btp->next;
579
580 /* `btp' now points at the frame of the innermost function that isn't
581 a special form, ignoring frames for Finteractive_p and/or
582 Fbytecode at the top. If this frame is for a built-in function
583 (such as load or eval-region) return nil. */
584 fun = Findirect_function (*btp->function, Qnil);
585 if (exclude_subrs_p && SUBRP (fun))
586 return 0;
587
588 /* `btp' points to the frame of a Lisp function that called interactive-p.
589 Return t if that function was called interactively. */
590 if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
591 return 1;
592 return 0;
593}
594
595
596DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0, 603DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0,
597 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE. 604 doc: /* Make NEW-ALIAS a variable alias for symbol BASE-VARIABLE.
598Aliased variables always have the same value; setting one sets the other. 605Aliased variables always have the same value; setting one sets the other.
@@ -631,12 +638,11 @@ The return value is BASE-VARIABLE. */)
631 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1); 638 set_internal (base_variable, find_symbol_value (new_alias), Qnil, 1);
632 639
633 { 640 {
634 struct specbinding *p; 641 union specbinding *p;
635 642
636 for (p = specpdl_ptr; p > specpdl; ) 643 for (p = specpdl_ptr; p > specpdl; )
637 if ((--p)->func == NULL 644 if ((--p)->kind >= SPECPDL_LET
638 && (EQ (new_alias, 645 && (EQ (new_alias, specpdl_symbol (p))))
639 CONSP (p->symbol) ? XCAR (p->symbol) : p->symbol)))
640 error ("Don't know how to make a let-bound variable an alias"); 646 error ("Don't know how to make a let-bound variable an alias");
641 } 647 }
642 648
@@ -652,6 +658,51 @@ The return value is BASE-VARIABLE. */)
652 return base_variable; 658 return base_variable;
653} 659}
654 660
661static union specbinding *
662default_toplevel_binding (Lisp_Object symbol)
663{
664 union specbinding *binding = NULL;
665 union specbinding *pdl = specpdl_ptr;
666 while (pdl > specpdl)
667 {
668 switch ((--pdl)->kind)
669 {
670 case SPECPDL_LET_DEFAULT:
671 case SPECPDL_LET:
672 if (EQ (specpdl_symbol (pdl), symbol))
673 binding = pdl;
674 break;
675 }
676 }
677 return binding;
678}
679
680DEFUN ("default-toplevel-value", Fdefault_toplevel_value, Sdefault_toplevel_value, 1, 1, 0,
681 doc: /* Return SYMBOL's toplevel default value.
682"Toplevel" means outside of any let binding. */)
683 (Lisp_Object symbol)
684{
685 union specbinding *binding = default_toplevel_binding (symbol);
686 Lisp_Object value
687 = binding ? specpdl_old_value (binding) : Fdefault_value (symbol);
688 if (!EQ (value, Qunbound))
689 return value;
690 xsignal1 (Qvoid_variable, symbol);
691}
692
693DEFUN ("set-default-toplevel-value", Fset_default_toplevel_value,
694 Sset_default_toplevel_value, 2, 2, 0,
695 doc: /* Set SYMBOL's toplevel default value to VALUE.
696"Toplevel" means outside of any let binding. */)
697 (Lisp_Object symbol, Lisp_Object value)
698{
699 union specbinding *binding = default_toplevel_binding (symbol);
700 if (binding)
701 set_specpdl_old_value (binding, value);
702 else
703 Fset_default (symbol, value);
704 return Qnil;
705}
655 706
656DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0, 707DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0,
657 doc: /* Define SYMBOL as a variable, and return SYMBOL. 708 doc: /* Define SYMBOL as a variable, and return SYMBOL.
@@ -680,49 +731,33 @@ To define a user option, use `defcustom' instead of `defvar'.
680usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) 731usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
681 (Lisp_Object args) 732 (Lisp_Object args)
682{ 733{
683 register Lisp_Object sym, tem, tail; 734 Lisp_Object sym, tem, tail;
684 735
685 sym = Fcar (args); 736 sym = XCAR (args);
686 tail = Fcdr (args); 737 tail = XCDR (args);
687 if (!NILP (Fcdr (Fcdr (tail))))
688 error ("Too many arguments");
689 738
690 tem = Fdefault_boundp (sym); 739 if (CONSP (tail))
691 if (!NILP (tail))
692 { 740 {
741 if (CONSP (XCDR (tail)) && CONSP (XCDR (XCDR (tail))))
742 error ("Too many arguments");
743
744 tem = Fdefault_boundp (sym);
745
693 /* Do it before evaluating the initial value, for self-references. */ 746 /* Do it before evaluating the initial value, for self-references. */
694 XSYMBOL (sym)->declared_special = 1; 747 XSYMBOL (sym)->declared_special = 1;
695 748
696 if (SYMBOL_CONSTANT_P (sym))
697 {
698 /* For upward compatibility, allow (defvar :foo (quote :foo)). */
699 Lisp_Object tem1 = Fcar (tail);
700 if (! (CONSP (tem1)
701 && EQ (XCAR (tem1), Qquote)
702 && CONSP (XCDR (tem1))
703 && EQ (XCAR (XCDR (tem1)), sym)))
704 error ("Constant symbol `%s' specified in defvar",
705 SDATA (SYMBOL_NAME (sym)));
706 }
707
708 if (NILP (tem)) 749 if (NILP (tem))
709 Fset_default (sym, eval_sub (Fcar (tail))); 750 Fset_default (sym, eval_sub (XCAR (tail)));
710 else 751 else
711 { /* Check if there is really a global binding rather than just a let 752 { /* Check if there is really a global binding rather than just a let
712 binding that shadows the global unboundness of the var. */ 753 binding that shadows the global unboundness of the var. */
713 volatile struct specbinding *pdl = specpdl_ptr; 754 union specbinding *binding = default_toplevel_binding (sym);
714 while (pdl > specpdl) 755 if (binding && EQ (specpdl_old_value (binding), Qunbound))
715 { 756 {
716 if (EQ ((--pdl)->symbol, sym) && !pdl->func 757 set_specpdl_old_value (binding, eval_sub (XCAR (tail)));
717 && EQ (pdl->old_value, Qunbound))
718 {
719 message_with_string ("Warning: defvar ignored because %s is let-bound",
720 SYMBOL_NAME (sym), 1);
721 break;
722 }
723 } 758 }
724 } 759 }
725 tail = Fcdr (tail); 760 tail = XCDR (tail);
726 tem = Fcar (tail); 761 tem = Fcar (tail);
727 if (!NILP (tem)) 762 if (!NILP (tem))
728 { 763 {
@@ -737,8 +772,8 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */)
737 /* A simple (defvar foo) with lexical scoping does "nothing" except 772 /* A simple (defvar foo) with lexical scoping does "nothing" except
738 declare that var to be dynamically scoped *locally* (i.e. within 773 declare that var to be dynamically scoped *locally* (i.e. within
739 the current file or let-block). */ 774 the current file or let-block). */
740 Vinternal_interpreter_environment = 775 Vinternal_interpreter_environment
741 Fcons (sym, Vinternal_interpreter_environment); 776 = Fcons (sym, Vinternal_interpreter_environment);
742 else 777 else
743 { 778 {
744 /* Simple (defvar <var>) should not count as a definition at all. 779 /* Simple (defvar <var>) should not count as a definition at all.
@@ -767,18 +802,18 @@ The optional DOCSTRING specifies the variable's documentation string.
767usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */) 802usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
768 (Lisp_Object args) 803 (Lisp_Object args)
769{ 804{
770 register Lisp_Object sym, tem; 805 Lisp_Object sym, tem;
771 806
772 sym = Fcar (args); 807 sym = XCAR (args);
773 if (!NILP (Fcdr (Fcdr (Fcdr (args))))) 808 if (CONSP (Fcdr (XCDR (XCDR (args)))))
774 error ("Too many arguments"); 809 error ("Too many arguments");
775 810
776 tem = eval_sub (Fcar (Fcdr (args))); 811 tem = eval_sub (Fcar (XCDR (args)));
777 if (!NILP (Vpurify_flag)) 812 if (!NILP (Vpurify_flag))
778 tem = Fpurecopy (tem); 813 tem = Fpurecopy (tem);
779 Fset_default (sym, tem); 814 Fset_default (sym, tem);
780 XSYMBOL (sym)->declared_special = 1; 815 XSYMBOL (sym)->declared_special = 1;
781 tem = Fcar (Fcdr (Fcdr (args))); 816 tem = Fcar (XCDR (XCDR (args)));
782 if (!NILP (tem)) 817 if (!NILP (tem))
783 { 818 {
784 if (!NILP (Vpurify_flag)) 819 if (!NILP (Vpurify_flag))
@@ -790,6 +825,17 @@ usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */)
790 return sym; 825 return sym;
791} 826}
792 827
828/* Make SYMBOL lexically scoped. */
829DEFUN ("internal-make-var-non-special", Fmake_var_non_special,
830 Smake_var_non_special, 1, 1, 0,
831 doc: /* Internal function. */)
832 (Lisp_Object symbol)
833{
834 CHECK_SYMBOL (symbol);
835 XSYMBOL (symbol)->declared_special = 0;
836 return Qnil;
837}
838
793 839
794DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0, 840DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
795 doc: /* Bind variables according to VARLIST then eval BODY. 841 doc: /* Bind variables according to VARLIST then eval BODY.
@@ -808,7 +854,7 @@ usage: (let* VARLIST BODY...) */)
808 854
809 lexenv = Vinternal_interpreter_environment; 855 lexenv = Vinternal_interpreter_environment;
810 856
811 varlist = Fcar (args); 857 varlist = XCAR (args);
812 while (CONSP (varlist)) 858 while (CONSP (varlist))
813 { 859 {
814 QUIT; 860 QUIT;
@@ -849,7 +895,7 @@ usage: (let* VARLIST BODY...) */)
849 varlist = XCDR (varlist); 895 varlist = XCDR (varlist);
850 } 896 }
851 UNGCPRO; 897 UNGCPRO;
852 val = Fprogn (Fcdr (args)); 898 val = Fprogn (XCDR (args));
853 return unbind_to (count, val); 899 return unbind_to (count, val);
854} 900}
855 901
@@ -869,7 +915,7 @@ usage: (let VARLIST BODY...) */)
869 struct gcpro gcpro1, gcpro2; 915 struct gcpro gcpro1, gcpro2;
870 USE_SAFE_ALLOCA; 916 USE_SAFE_ALLOCA;
871 917
872 varlist = Fcar (args); 918 varlist = XCAR (args);
873 919
874 /* Make space to hold the values to give the bound variables. */ 920 /* Make space to hold the values to give the bound variables. */
875 elt = Flength (varlist); 921 elt = Flength (varlist);
@@ -896,7 +942,7 @@ usage: (let VARLIST BODY...) */)
896 942
897 lexenv = Vinternal_interpreter_environment; 943 lexenv = Vinternal_interpreter_environment;
898 944
899 varlist = Fcar (args); 945 varlist = XCAR (args);
900 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist)) 946 for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
901 { 947 {
902 Lisp_Object var; 948 Lisp_Object var;
@@ -919,7 +965,7 @@ usage: (let VARLIST BODY...) */)
919 /* Instantiate a new lexical environment. */ 965 /* Instantiate a new lexical environment. */
920 specbind (Qinternal_interpreter_environment, lexenv); 966 specbind (Qinternal_interpreter_environment, lexenv);
921 967
922 elt = Fprogn (Fcdr (args)); 968 elt = Fprogn (XCDR (args));
923 SAFE_FREE (); 969 SAFE_FREE ();
924 return unbind_to (count, elt); 970 return unbind_to (count, elt);
925} 971}
@@ -936,8 +982,8 @@ usage: (while TEST BODY...) */)
936 982
937 GCPRO2 (test, body); 983 GCPRO2 (test, body);
938 984
939 test = Fcar (args); 985 test = XCAR (args);
940 body = Fcdr (args); 986 body = XCDR (args);
941 while (!NILP (eval_sub (test))) 987 while (!NILP (eval_sub (test)))
942 { 988 {
943 QUIT; 989 QUIT;
@@ -980,7 +1026,7 @@ definitions to shadow the loaded ones for use in file byte-compilation. */)
980 if (NILP (tem)) 1026 if (NILP (tem))
981 { 1027 {
982 def = XSYMBOL (sym)->function; 1028 def = XSYMBOL (sym)->function;
983 if (!EQ (def, Qunbound)) 1029 if (!NILP (def))
984 continue; 1030 continue;
985 } 1031 }
986 break; 1032 break;
@@ -991,26 +1037,14 @@ definitions to shadow the loaded ones for use in file byte-compilation. */)
991 { 1037 {
992 /* SYM is not mentioned in ENVIRONMENT. 1038 /* SYM is not mentioned in ENVIRONMENT.
993 Look at its function definition. */ 1039 Look at its function definition. */
994 if (EQ (def, Qunbound) || !CONSP (def)) 1040 struct gcpro gcpro1;
1041 GCPRO1 (form);
1042 def = Fautoload_do_load (def, sym, Qmacro);
1043 UNGCPRO;
1044 if (!CONSP (def))
995 /* Not defined or definition not suitable. */ 1045 /* Not defined or definition not suitable. */
996 break; 1046 break;
997 if (EQ (XCAR (def), Qautoload)) 1047 if (!EQ (XCAR (def), Qmacro))
998 {
999 /* Autoloading function: will it be a macro when loaded? */
1000 tem = Fnth (make_number (4), def);
1001 if (EQ (tem, Qt) || EQ (tem, Qmacro))
1002 /* Yes, load it and try again. */
1003 {
1004 struct gcpro gcpro1;
1005 GCPRO1 (form);
1006 do_autoload (def, sym);
1007 UNGCPRO;
1008 continue;
1009 }
1010 else
1011 break;
1012 }
1013 else if (!EQ (XCAR (def), Qmacro))
1014 break; 1048 break;
1015 else expander = XCDR (def); 1049 else expander = XCDR (def);
1016 } 1050 }
@@ -1020,7 +1054,13 @@ definitions to shadow the loaded ones for use in file byte-compilation. */)
1020 if (NILP (expander)) 1054 if (NILP (expander))
1021 break; 1055 break;
1022 } 1056 }
1023 form = apply1 (expander, XCDR (form)); 1057 {
1058 Lisp_Object newform = apply1 (expander, XCDR (form));
1059 if (EQ (form, newform))
1060 break;
1061 else
1062 form = newform;
1063 }
1024 } 1064 }
1025 return form; 1065 return form;
1026} 1066}
@@ -1040,14 +1080,14 @@ usage: (catch TAG BODY...) */)
1040 struct gcpro gcpro1; 1080 struct gcpro gcpro1;
1041 1081
1042 GCPRO1 (args); 1082 GCPRO1 (args);
1043 tag = eval_sub (Fcar (args)); 1083 tag = eval_sub (XCAR (args));
1044 UNGCPRO; 1084 UNGCPRO;
1045 return internal_catch (tag, Fprogn, Fcdr (args)); 1085 return internal_catch (tag, Fprogn, XCDR (args));
1046} 1086}
1047 1087
1048/* Set up a catch, then call C function FUNC on argument ARG. 1088/* Set up a catch, then call C function FUNC on argument ARG.
1049 FUNC should return a Lisp_Object. 1089 FUNC should return a Lisp_Object.
1050 This is how catches are done from within C code. */ 1090 This is how catches are done from within C code. */
1051 1091
1052Lisp_Object 1092Lisp_Object
1053internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg) 1093internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object arg)
@@ -1059,7 +1099,6 @@ internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object
1059 c.next = catchlist; 1099 c.next = catchlist;
1060 c.tag = tag; 1100 c.tag = tag;
1061 c.val = Qnil; 1101 c.val = Qnil;
1062 c.backlist = backtrace_list;
1063 c.handlerlist = handlerlist; 1102 c.handlerlist = handlerlist;
1064 c.lisp_eval_depth = lisp_eval_depth; 1103 c.lisp_eval_depth = lisp_eval_depth;
1065 c.pdlcount = SPECPDL_INDEX (); 1104 c.pdlcount = SPECPDL_INDEX ();
@@ -1070,7 +1109,7 @@ internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object
1070 catchlist = &c; 1109 catchlist = &c;
1071 1110
1072 /* Call FUNC. */ 1111 /* Call FUNC. */
1073 if (! _setjmp (c.jmp)) 1112 if (! sys_setjmp (c.jmp))
1074 c.val = (*func) (arg); 1113 c.val = (*func) (arg);
1075 1114
1076 /* Throw works by a longjmp that comes right here. */ 1115 /* Throw works by a longjmp that comes right here. */
@@ -1081,7 +1120,7 @@ internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object
1081/* Unwind the specbind, catch, and handler stacks back to CATCH, and 1120/* Unwind the specbind, catch, and handler stacks back to CATCH, and
1082 jump to that CATCH, returning VALUE as the value of that catch. 1121 jump to that CATCH, returning VALUE as the value of that catch.
1083 1122
1084 This is the guts Fthrow and Fsignal; they differ only in the way 1123 This is the guts of Fthrow and Fsignal; they differ only in the way
1085 they choose the catch tag to throw to. A catch tag for a 1124 they choose the catch tag to throw to. A catch tag for a
1086 condition-case form has a TAG of Qnil. 1125 condition-case form has a TAG of Qnil.
1087 1126
@@ -1090,22 +1129,21 @@ internal_catch (Lisp_Object tag, Lisp_Object (*func) (Lisp_Object), Lisp_Object
1090 the handler stack as we go, so that the proper handlers are in 1129 the handler stack as we go, so that the proper handlers are in
1091 effect for each unwind-protect clause we run. At the end, restore 1130 effect for each unwind-protect clause we run. At the end, restore
1092 some static info saved in CATCH, and longjmp to the location 1131 some static info saved in CATCH, and longjmp to the location
1093 specified in the 1132 specified there.
1094 1133
1095 This is used for correct unwinding in Fthrow and Fsignal. */ 1134 This is used for correct unwinding in Fthrow and Fsignal. */
1096 1135
1097static void 1136static _Noreturn void
1098unwind_to_catch (struct catchtag *catch, Lisp_Object value) 1137unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1099{ 1138{
1100 register int last_time; 1139 bool last_time;
1101 1140
1102 /* Save the value in the tag. */ 1141 /* Save the value in the tag. */
1103 catch->val = value; 1142 catch->val = value;
1104 1143
1105 /* Restore certain special C variables. */ 1144 /* Restore certain special C variables. */
1106 set_poll_suppress_count (catch->poll_suppress_count); 1145 set_poll_suppress_count (catch->poll_suppress_count);
1107 UNBLOCK_INPUT_TO (catch->interrupt_input_blocked); 1146 unblock_input_to (catch->interrupt_input_blocked);
1108 handling_signal = 0;
1109 immediate_quit = 0; 1147 immediate_quit = 0;
1110 1148
1111 do 1149 do
@@ -1120,25 +1158,14 @@ unwind_to_catch (struct catchtag *catch, Lisp_Object value)
1120 } 1158 }
1121 while (! last_time); 1159 while (! last_time);
1122 1160
1123#if HAVE_X_WINDOWS
1124 /* If x_catch_errors was done, turn it off now.
1125 (First we give unbind_to a chance to do that.) */
1126#if 0 /* This would disable x_catch_errors after x_connection_closed.
1127 The catch must remain in effect during that delicate
1128 state. --lorentey */
1129 x_fully_uncatch_errors ();
1130#endif
1131#endif
1132
1133 byte_stack_list = catch->byte_stack; 1161 byte_stack_list = catch->byte_stack;
1134 gcprolist = catch->gcpro; 1162 gcprolist = catch->gcpro;
1135#ifdef DEBUG_GCPRO 1163#ifdef DEBUG_GCPRO
1136 gcpro_level = gcprolist ? gcprolist->level + 1 : 0; 1164 gcpro_level = gcprolist ? gcprolist->level + 1 : 0;
1137#endif 1165#endif
1138 backtrace_list = catch->backlist;
1139 lisp_eval_depth = catch->lisp_eval_depth; 1166 lisp_eval_depth = catch->lisp_eval_depth;
1140 1167
1141 _longjmp (catch->jmp, 1); 1168 sys_longjmp (catch->jmp, 1);
1142} 1169}
1143 1170
1144DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0, 1171DEFUN ("throw", Fthrow, Sthrow, 2, 2, 0,
@@ -1169,8 +1196,8 @@ usage: (unwind-protect BODYFORM UNWINDFORMS...) */)
1169 Lisp_Object val; 1196 Lisp_Object val;
1170 ptrdiff_t count = SPECPDL_INDEX (); 1197 ptrdiff_t count = SPECPDL_INDEX ();
1171 1198
1172 record_unwind_protect (Fprogn, Fcdr (args)); 1199 record_unwind_protect (unwind_body, XCDR (args));
1173 val = eval_sub (Fcar (args)); 1200 val = eval_sub (XCAR (args));
1174 return unbind_to (count, val); 1201 return unbind_to (count, val);
1175} 1202}
1176 1203
@@ -1202,12 +1229,9 @@ See also the function `signal' for more info.
1202usage: (condition-case VAR BODYFORM &rest HANDLERS) */) 1229usage: (condition-case VAR BODYFORM &rest HANDLERS) */)
1203 (Lisp_Object args) 1230 (Lisp_Object args)
1204{ 1231{
1205 register Lisp_Object bodyform, handlers; 1232 Lisp_Object var = XCAR (args);
1206 volatile Lisp_Object var; 1233 Lisp_Object bodyform = XCAR (XCDR (args));
1207 1234 Lisp_Object handlers = XCDR (XCDR (args));
1208 var = Fcar (args);
1209 bodyform = Fcar (Fcdr (args));
1210 handlers = Fcdr (Fcdr (args));
1211 1235
1212 return internal_lisp_condition_case (var, bodyform, handlers); 1236 return internal_lisp_condition_case (var, bodyform, handlers);
1213} 1237}
@@ -1239,7 +1263,6 @@ internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1239 1263
1240 c.tag = Qnil; 1264 c.tag = Qnil;
1241 c.val = Qnil; 1265 c.val = Qnil;
1242 c.backlist = backtrace_list;
1243 c.handlerlist = handlerlist; 1266 c.handlerlist = handlerlist;
1244 c.lisp_eval_depth = lisp_eval_depth; 1267 c.lisp_eval_depth = lisp_eval_depth;
1245 c.pdlcount = SPECPDL_INDEX (); 1268 c.pdlcount = SPECPDL_INDEX ();
@@ -1247,7 +1270,7 @@ internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1247 c.interrupt_input_blocked = interrupt_input_blocked; 1270 c.interrupt_input_blocked = interrupt_input_blocked;
1248 c.gcpro = gcprolist; 1271 c.gcpro = gcprolist;
1249 c.byte_stack = byte_stack_list; 1272 c.byte_stack = byte_stack_list;
1250 if (_setjmp (c.jmp)) 1273 if (sys_setjmp (c.jmp))
1251 { 1274 {
1252 if (!NILP (h.var)) 1275 if (!NILP (h.var))
1253 specbind (h.var, c.val); 1276 specbind (h.var, c.val);
@@ -1255,7 +1278,7 @@ internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
1255 1278
1256 /* Note that this just undoes the binding of h.var; whoever 1279 /* Note that this just undoes the binding of h.var; whoever
1257 longjumped to us unwound the stack to c.pdlcount before 1280 longjumped to us unwound the stack to c.pdlcount before
1258 throwing. */ 1281 throwing. */
1259 unbind_to (c.pdlcount, Qnil); 1282 unbind_to (c.pdlcount, Qnil);
1260 return val; 1283 return val;
1261 } 1284 }
@@ -1294,7 +1317,6 @@ internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1294 1317
1295 c.tag = Qnil; 1318 c.tag = Qnil;
1296 c.val = Qnil; 1319 c.val = Qnil;
1297 c.backlist = backtrace_list;
1298 c.handlerlist = handlerlist; 1320 c.handlerlist = handlerlist;
1299 c.lisp_eval_depth = lisp_eval_depth; 1321 c.lisp_eval_depth = lisp_eval_depth;
1300 c.pdlcount = SPECPDL_INDEX (); 1322 c.pdlcount = SPECPDL_INDEX ();
@@ -1302,7 +1324,7 @@ internal_condition_case (Lisp_Object (*bfun) (void), Lisp_Object handlers,
1302 c.interrupt_input_blocked = interrupt_input_blocked; 1324 c.interrupt_input_blocked = interrupt_input_blocked;
1303 c.gcpro = gcprolist; 1325 c.gcpro = gcprolist;
1304 c.byte_stack = byte_stack_list; 1326 c.byte_stack = byte_stack_list;
1305 if (_setjmp (c.jmp)) 1327 if (sys_setjmp (c.jmp))
1306 { 1328 {
1307 return (*hfun) (c.val); 1329 return (*hfun) (c.val);
1308 } 1330 }
@@ -1332,7 +1354,6 @@ internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1332 1354
1333 c.tag = Qnil; 1355 c.tag = Qnil;
1334 c.val = Qnil; 1356 c.val = Qnil;
1335 c.backlist = backtrace_list;
1336 c.handlerlist = handlerlist; 1357 c.handlerlist = handlerlist;
1337 c.lisp_eval_depth = lisp_eval_depth; 1358 c.lisp_eval_depth = lisp_eval_depth;
1338 c.pdlcount = SPECPDL_INDEX (); 1359 c.pdlcount = SPECPDL_INDEX ();
@@ -1340,7 +1361,7 @@ internal_condition_case_1 (Lisp_Object (*bfun) (Lisp_Object), Lisp_Object arg,
1340 c.interrupt_input_blocked = interrupt_input_blocked; 1361 c.interrupt_input_blocked = interrupt_input_blocked;
1341 c.gcpro = gcprolist; 1362 c.gcpro = gcprolist;
1342 c.byte_stack = byte_stack_list; 1363 c.byte_stack = byte_stack_list;
1343 if (_setjmp (c.jmp)) 1364 if (sys_setjmp (c.jmp))
1344 { 1365 {
1345 return (*hfun) (c.val); 1366 return (*hfun) (c.val);
1346 } 1367 }
@@ -1374,7 +1395,6 @@ internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1374 1395
1375 c.tag = Qnil; 1396 c.tag = Qnil;
1376 c.val = Qnil; 1397 c.val = Qnil;
1377 c.backlist = backtrace_list;
1378 c.handlerlist = handlerlist; 1398 c.handlerlist = handlerlist;
1379 c.lisp_eval_depth = lisp_eval_depth; 1399 c.lisp_eval_depth = lisp_eval_depth;
1380 c.pdlcount = SPECPDL_INDEX (); 1400 c.pdlcount = SPECPDL_INDEX ();
@@ -1382,7 +1402,7 @@ internal_condition_case_2 (Lisp_Object (*bfun) (Lisp_Object, Lisp_Object),
1382 c.interrupt_input_blocked = interrupt_input_blocked; 1402 c.interrupt_input_blocked = interrupt_input_blocked;
1383 c.gcpro = gcprolist; 1403 c.gcpro = gcprolist;
1384 c.byte_stack = byte_stack_list; 1404 c.byte_stack = byte_stack_list;
1385 if (_setjmp (c.jmp)) 1405 if (sys_setjmp (c.jmp))
1386 { 1406 {
1387 return (*hfun) (c.val); 1407 return (*hfun) (c.val);
1388 } 1408 }
@@ -1408,7 +1428,9 @@ internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1408 ptrdiff_t nargs, 1428 ptrdiff_t nargs,
1409 Lisp_Object *args, 1429 Lisp_Object *args,
1410 Lisp_Object handlers, 1430 Lisp_Object handlers,
1411 Lisp_Object (*hfun) (Lisp_Object)) 1431 Lisp_Object (*hfun) (Lisp_Object err,
1432 ptrdiff_t nargs,
1433 Lisp_Object *args))
1412{ 1434{
1413 Lisp_Object val; 1435 Lisp_Object val;
1414 struct catchtag c; 1436 struct catchtag c;
@@ -1416,7 +1438,6 @@ internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1416 1438
1417 c.tag = Qnil; 1439 c.tag = Qnil;
1418 c.val = Qnil; 1440 c.val = Qnil;
1419 c.backlist = backtrace_list;
1420 c.handlerlist = handlerlist; 1441 c.handlerlist = handlerlist;
1421 c.lisp_eval_depth = lisp_eval_depth; 1442 c.lisp_eval_depth = lisp_eval_depth;
1422 c.pdlcount = SPECPDL_INDEX (); 1443 c.pdlcount = SPECPDL_INDEX ();
@@ -1424,9 +1445,9 @@ internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1424 c.interrupt_input_blocked = interrupt_input_blocked; 1445 c.interrupt_input_blocked = interrupt_input_blocked;
1425 c.gcpro = gcprolist; 1446 c.gcpro = gcprolist;
1426 c.byte_stack = byte_stack_list; 1447 c.byte_stack = byte_stack_list;
1427 if (_setjmp (c.jmp)) 1448 if (sys_setjmp (c.jmp))
1428 { 1449 {
1429 return (*hfun) (c.val); 1450 return (*hfun) (c.val, nargs, args);
1430 } 1451 }
1431 c.next = catchlist; 1452 c.next = catchlist;
1432 catchlist = &c; 1453 catchlist = &c;
@@ -1444,8 +1465,8 @@ internal_condition_case_n (Lisp_Object (*bfun) (ptrdiff_t, Lisp_Object *),
1444 1465
1445 1466
1446static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object); 1467static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object);
1447static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, 1468static bool maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
1448 Lisp_Object data); 1469 Lisp_Object data);
1449 1470
1450void 1471void
1451process_quit_flag (void) 1472process_quit_flag (void)
@@ -1484,12 +1505,11 @@ See also the function `condition-case'. */)
1484 = (NILP (error_symbol) ? Fcar (data) : error_symbol); 1505 = (NILP (error_symbol) ? Fcar (data) : error_symbol);
1485 register Lisp_Object clause = Qnil; 1506 register Lisp_Object clause = Qnil;
1486 struct handler *h; 1507 struct handler *h;
1487 struct backtrace *bp;
1488 1508
1489 immediate_quit = handling_signal = 0; 1509 immediate_quit = 0;
1490 abort_on_gc = 0; 1510 abort_on_gc = 0;
1491 if (gc_in_progress || waiting_for_input) 1511 if (gc_in_progress || waiting_for_input)
1492 abort (); 1512 emacs_abort ();
1493 1513
1494#if 0 /* rms: I don't know why this was here, 1514#if 0 /* rms: I don't know why this was here,
1495 but it is surely wrong for an error that is handled. */ 1515 but it is surely wrong for an error that is handled. */
@@ -1520,13 +1540,13 @@ See also the function `condition-case'. */)
1520 too. Don't do this when ERROR_SYMBOL is nil, because that 1540 too. Don't do this when ERROR_SYMBOL is nil, because that
1521 is a memory-full error. */ 1541 is a memory-full error. */
1522 Vsignaling_function = Qnil; 1542 Vsignaling_function = Qnil;
1523 if (backtrace_list && !NILP (error_symbol)) 1543 if (!NILP (error_symbol))
1524 { 1544 {
1525 bp = backtrace_list->next; 1545 union specbinding *pdl = backtrace_next (backtrace_top ());
1526 if (bp && bp->function && EQ (*bp->function, Qerror)) 1546 if (backtrace_p (pdl) && EQ (backtrace_function (pdl), Qerror))
1527 bp = bp->next; 1547 pdl = backtrace_next (pdl);
1528 if (bp && bp->function) 1548 if (backtrace_p (pdl))
1529 Vsignaling_function = *bp->function; 1549 Vsignaling_function = backtrace_function (pdl);
1530 } 1550 }
1531 1551
1532 for (h = handlerlist; h; h = h->next) 1552 for (h = handlerlist; h; h = h->next)
@@ -1537,7 +1557,7 @@ See also the function `condition-case'. */)
1537 } 1557 }
1538 1558
1539 if (/* Don't run the debugger for a memory-full error. 1559 if (/* Don't run the debugger for a memory-full error.
1540 (There is no room in memory to do that!) */ 1560 (There is no room in memory to do that!) */
1541 !NILP (error_symbol) 1561 !NILP (error_symbol)
1542 && (!NILP (Vdebug_on_signal) 1562 && (!NILP (Vdebug_on_signal)
1543 /* If no handler is present now, try to run the debugger. */ 1563 /* If no handler is present now, try to run the debugger. */
@@ -1550,7 +1570,7 @@ See also the function `condition-case'. */)
1550 if requested". */ 1570 if requested". */
1551 || EQ (h->handler, Qerror))) 1571 || EQ (h->handler, Qerror)))
1552 { 1572 {
1553 int debugger_called 1573 bool debugger_called
1554 = maybe_call_debugger (conditions, error_symbol, data); 1574 = maybe_call_debugger (conditions, error_symbol, data);
1555 /* We can't return values to code which signaled an error, but we 1575 /* We can't return values to code which signaled an error, but we
1556 can continue code which has signaled a quit. */ 1576 can continue code which has signaled a quit. */
@@ -1586,7 +1606,7 @@ void
1586xsignal (Lisp_Object error_symbol, Lisp_Object data) 1606xsignal (Lisp_Object error_symbol, Lisp_Object data)
1587{ 1607{
1588 Fsignal (error_symbol, data); 1608 Fsignal (error_symbol, data);
1589 abort (); 1609 emacs_abort ();
1590} 1610}
1591 1611
1592/* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */ 1612/* Like xsignal, but takes 0, 1, 2, or 3 args instead of a list. */
@@ -1638,16 +1658,16 @@ signal_error (const char *s, Lisp_Object arg)
1638 } 1658 }
1639 1659
1640 if (!NILP (hare)) 1660 if (!NILP (hare))
1641 arg = Fcons (arg, Qnil); /* Make it a list. */ 1661 arg = list1 (arg);
1642 1662
1643 xsignal (Qerror, Fcons (build_string (s), arg)); 1663 xsignal (Qerror, Fcons (build_string (s), arg));
1644} 1664}
1645 1665
1646 1666
1647/* Return nonzero if LIST is a non-nil atom or 1667/* Return true if LIST is a non-nil atom or
1648 a list containing one of CONDITIONS. */ 1668 a list containing one of CONDITIONS. */
1649 1669
1650static int 1670static bool
1651wants_debugger (Lisp_Object list, Lisp_Object conditions) 1671wants_debugger (Lisp_Object list, Lisp_Object conditions)
1652{ 1672{
1653 if (NILP (list)) 1673 if (NILP (list))
@@ -1667,15 +1687,15 @@ wants_debugger (Lisp_Object list, Lisp_Object conditions)
1667 return 0; 1687 return 0;
1668} 1688}
1669 1689
1670/* Return 1 if an error with condition-symbols CONDITIONS, 1690/* Return true if an error with condition-symbols CONDITIONS,
1671 and described by SIGNAL-DATA, should skip the debugger 1691 and described by SIGNAL-DATA, should skip the debugger
1672 according to debugger-ignored-errors. */ 1692 according to debugger-ignored-errors. */
1673 1693
1674static int 1694static bool
1675skip_debugger (Lisp_Object conditions, Lisp_Object data) 1695skip_debugger (Lisp_Object conditions, Lisp_Object data)
1676{ 1696{
1677 Lisp_Object tail; 1697 Lisp_Object tail;
1678 int first_string = 1; 1698 bool first_string = 1;
1679 Lisp_Object error_message; 1699 Lisp_Object error_message;
1680 1700
1681 error_message = Qnil; 1701 error_message = Qnil;
@@ -1710,7 +1730,7 @@ skip_debugger (Lisp_Object conditions, Lisp_Object data)
1710 = SIG is the error symbol, and DATA is the rest of the data. 1730 = SIG is the error symbol, and DATA is the rest of the data.
1711 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA). 1731 = SIG is nil, and DATA is (SYMBOL . REST-OF-DATA).
1712 This is for memory-full errors only. */ 1732 This is for memory-full errors only. */
1713static int 1733static bool
1714maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data) 1734maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1715{ 1735{
1716 Lisp_Object combined_data; 1736 Lisp_Object combined_data;
@@ -1720,7 +1740,8 @@ maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1720 if ( 1740 if (
1721 /* Don't try to run the debugger with interrupts blocked. 1741 /* Don't try to run the debugger with interrupts blocked.
1722 The editing loop would return anyway. */ 1742 The editing loop would return anyway. */
1723 ! INPUT_BLOCKED_P 1743 ! input_blocked_p ()
1744 && NILP (Vinhibit_debugger)
1724 /* Does user want to enter debugger for this kind of error? */ 1745 /* Does user want to enter debugger for this kind of error? */
1725 && (EQ (sig, Qquit) 1746 && (EQ (sig, Qquit)
1726 ? debug_on_quit 1747 ? debug_on_quit
@@ -1729,7 +1750,7 @@ maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig, Lisp_Object data)
1729 /* RMS: What's this for? */ 1750 /* RMS: What's this for? */
1730 && when_entered_debugger < num_nonmacro_input_events) 1751 && when_entered_debugger < num_nonmacro_input_events)
1731 { 1752 {
1732 call_debugger (Fcons (Qerror, Fcons (combined_data, Qnil))); 1753 call_debugger (list2 (Qerror, combined_data));
1733 return 1; 1754 return 1;
1734 } 1755 }
1735 1756
@@ -1811,7 +1832,6 @@ error (const char *m, ...)
1811 va_list ap; 1832 va_list ap;
1812 va_start (ap, m); 1833 va_start (ap, m);
1813 verror (m, ap); 1834 verror (m, ap);
1814 va_end (ap);
1815} 1835}
1816 1836
1817DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0, 1837DEFUN ("commandp", Fcommandp, Scommandp, 1, 2, 0,
@@ -1837,12 +1857,12 @@ then strings and vectors are not accepted. */)
1837 1857
1838 fun = function; 1858 fun = function;
1839 1859
1840 fun = indirect_function (fun); /* Check cycles. */ 1860 fun = indirect_function (fun); /* Check cycles. */
1841 if (NILP (fun) || EQ (fun, Qunbound)) 1861 if (NILP (fun))
1842 return Qnil; 1862 return Qnil;
1843 1863
1844 /* Check an `interactive-form' property if present, analogous to the 1864 /* Check an `interactive-form' property if present, analogous to the
1845 function-documentation property. */ 1865 function-documentation property. */
1846 fun = function; 1866 fun = function;
1847 while (SYMBOLP (fun)) 1867 while (SYMBOLP (fun))
1848 { 1868 {
@@ -1902,30 +1922,25 @@ this does nothing and returns nil. */)
1902 CHECK_STRING (file); 1922 CHECK_STRING (file);
1903 1923
1904 /* If function is defined and not as an autoload, don't override. */ 1924 /* If function is defined and not as an autoload, don't override. */
1905 if (!EQ (XSYMBOL (function)->function, Qunbound) 1925 if (!NILP (XSYMBOL (function)->function)
1906 && !(CONSP (XSYMBOL (function)->function) 1926 && !AUTOLOADP (XSYMBOL (function)->function))
1907 && EQ (XCAR (XSYMBOL (function)->function), Qautoload)))
1908 return Qnil; 1927 return Qnil;
1909 1928
1910 if (NILP (Vpurify_flag)) 1929 if (!NILP (Vpurify_flag) && EQ (docstring, make_number (0)))
1911 /* Only add entries after dumping, because the ones before are
1912 not useful and else we get loads of them from the loaddefs.el. */
1913 LOADHIST_ATTACH (Fcons (Qautoload, function));
1914 else if (EQ (docstring, make_number (0)))
1915 /* `read1' in lread.c has found the docstring starting with "\ 1930 /* `read1' in lread.c has found the docstring starting with "\
1916 and assumed the docstring will be provided by Snarf-documentation, so it 1931 and assumed the docstring will be provided by Snarf-documentation, so it
1917 passed us 0 instead. But that leads to accidental sharing in purecopy's 1932 passed us 0 instead. But that leads to accidental sharing in purecopy's
1918 hash-consing, so we use a (hopefully) unique integer instead. */ 1933 hash-consing, so we use a (hopefully) unique integer instead. */
1919 docstring = make_number (XUNTAG (function, Lisp_Symbol)); 1934 docstring = make_number (XHASH (function));
1920 return Ffset (function, 1935 return Fdefalias (function,
1921 Fpurecopy (list5 (Qautoload, file, docstring, 1936 list5 (Qautoload, file, docstring, interactive, type),
1922 interactive, type))); 1937 Qnil);
1923} 1938}
1924 1939
1925Lisp_Object 1940void
1926un_autoload (Lisp_Object oldqueue) 1941un_autoload (Lisp_Object oldqueue)
1927{ 1942{
1928 register Lisp_Object queue, first, second; 1943 Lisp_Object queue, first, second;
1929 1944
1930 /* Queue to unwind is current value of Vautoload_queue. 1945 /* Queue to unwind is current value of Vautoload_queue.
1931 oldqueue is the shadowed value to leave in Vautoload_queue. */ 1946 oldqueue is the shadowed value to leave in Vautoload_queue. */
@@ -1942,29 +1957,41 @@ un_autoload (Lisp_Object oldqueue)
1942 Ffset (first, second); 1957 Ffset (first, second);
1943 queue = XCDR (queue); 1958 queue = XCDR (queue);
1944 } 1959 }
1945 return Qnil;
1946} 1960}
1947 1961
1948/* Load an autoloaded function. 1962/* Load an autoloaded function.
1949 FUNNAME is the symbol which is the function's name. 1963 FUNNAME is the symbol which is the function's name.
1950 FUNDEF is the autoload definition (a list). */ 1964 FUNDEF is the autoload definition (a list). */
1951 1965
1952void 1966DEFUN ("autoload-do-load", Fautoload_do_load, Sautoload_do_load, 1, 3, 0,
1953do_autoload (Lisp_Object fundef, Lisp_Object funname) 1967 doc: /* Load FUNDEF which should be an autoload.
1968If non-nil, FUNNAME should be the symbol whose function value is FUNDEF,
1969in which case the function returns the new autoloaded function value.
1970If equal to `macro', MACRO-ONLY specifies that FUNDEF should only be loaded if
1971it is defines a macro. */)
1972 (Lisp_Object fundef, Lisp_Object funname, Lisp_Object macro_only)
1954{ 1973{
1955 ptrdiff_t count = SPECPDL_INDEX (); 1974 ptrdiff_t count = SPECPDL_INDEX ();
1956 Lisp_Object fun;
1957 struct gcpro gcpro1, gcpro2, gcpro3; 1975 struct gcpro gcpro1, gcpro2, gcpro3;
1958 1976
1977 if (!CONSP (fundef) || !EQ (Qautoload, XCAR (fundef)))
1978 return fundef;
1979
1980 if (EQ (macro_only, Qmacro))
1981 {
1982 Lisp_Object kind = Fnth (make_number (4), fundef);
1983 if (! (EQ (kind, Qt) || EQ (kind, Qmacro)))
1984 return fundef;
1985 }
1986
1959 /* This is to make sure that loadup.el gives a clear picture 1987 /* This is to make sure that loadup.el gives a clear picture
1960 of what files are preloaded and when. */ 1988 of what files are preloaded and when. */
1961 if (! NILP (Vpurify_flag)) 1989 if (! NILP (Vpurify_flag))
1962 error ("Attempt to autoload %s while preparing to dump", 1990 error ("Attempt to autoload %s while preparing to dump",
1963 SDATA (SYMBOL_NAME (funname))); 1991 SDATA (SYMBOL_NAME (funname)));
1964 1992
1965 fun = funname;
1966 CHECK_SYMBOL (funname); 1993 CHECK_SYMBOL (funname);
1967 GCPRO3 (fun, funname, fundef); 1994 GCPRO3 (funname, fundef, macro_only);
1968 1995
1969 /* Preserve the match data. */ 1996 /* Preserve the match data. */
1970 record_unwind_save_match_data (); 1997 record_unwind_save_match_data ();
@@ -1979,18 +2006,28 @@ do_autoload (Lisp_Object fundef, Lisp_Object funname)
1979 The value saved here is to be restored into Vautoload_queue. */ 2006 The value saved here is to be restored into Vautoload_queue. */
1980 record_unwind_protect (un_autoload, Vautoload_queue); 2007 record_unwind_protect (un_autoload, Vautoload_queue);
1981 Vautoload_queue = Qt; 2008 Vautoload_queue = Qt;
1982 Fload (Fcar (Fcdr (fundef)), Qnil, Qt, Qnil, Qt); 2009 /* If `macro_only', assume this autoload to be a "best-effort",
2010 so don't signal an error if autoloading fails. */
2011 Fload (Fcar (Fcdr (fundef)), macro_only, Qt, Qnil, Qt);
1983 2012
1984 /* Once loading finishes, don't undo it. */ 2013 /* Once loading finishes, don't undo it. */
1985 Vautoload_queue = Qt; 2014 Vautoload_queue = Qt;
1986 unbind_to (count, Qnil); 2015 unbind_to (count, Qnil);
1987 2016
1988 fun = Findirect_function (fun, Qnil);
1989
1990 if (!NILP (Fequal (fun, fundef)))
1991 error ("Autoloading failed to define function %s",
1992 SDATA (SYMBOL_NAME (funname)));
1993 UNGCPRO; 2017 UNGCPRO;
2018
2019 if (NILP (funname))
2020 return Qnil;
2021 else
2022 {
2023 Lisp_Object fun = Findirect_function (funname, Qnil);
2024
2025 if (!NILP (Fequal (fun, fundef)))
2026 error ("Autoloading failed to define function %s",
2027 SDATA (SYMBOL_NAME (funname)));
2028 else
2029 return fun;
2030 }
1994} 2031}
1995 2032
1996 2033
@@ -2001,10 +2038,58 @@ If LEXICAL is t, evaluate using lexical scoping. */)
2001{ 2038{
2002 ptrdiff_t count = SPECPDL_INDEX (); 2039 ptrdiff_t count = SPECPDL_INDEX ();
2003 specbind (Qinternal_interpreter_environment, 2040 specbind (Qinternal_interpreter_environment,
2004 NILP (lexical) ? Qnil : Fcons (Qt, Qnil)); 2041 CONSP (lexical) || NILP (lexical) ? lexical : list1 (Qt));
2005 return unbind_to (count, eval_sub (form)); 2042 return unbind_to (count, eval_sub (form));
2006} 2043}
2007 2044
2045/* Grow the specpdl stack by one entry.
2046 The caller should have already initialized the entry.
2047 Signal an error on stack overflow.
2048
2049 Make sure that there is always one unused entry past the top of the
2050 stack, so that the just-initialized entry is safely unwound if
2051 memory exhausted and an error is signaled here. Also, allocate a
2052 never-used entry just before the bottom of the stack; sometimes its
2053 address is taken. */
2054
2055static void
2056grow_specpdl (void)
2057{
2058 specpdl_ptr++;
2059
2060 if (specpdl_ptr == specpdl + specpdl_size)
2061 {
2062 ptrdiff_t count = SPECPDL_INDEX ();
2063 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX - 1000);
2064 union specbinding *pdlvec = specpdl - 1;
2065 ptrdiff_t pdlvecsize = specpdl_size + 1;
2066 if (max_size <= specpdl_size)
2067 {
2068 if (max_specpdl_size < 400)
2069 max_size = max_specpdl_size = 400;
2070 if (max_size <= specpdl_size)
2071 signal_error ("Variable binding depth exceeds max-specpdl-size",
2072 Qnil);
2073 }
2074 pdlvec = xpalloc (pdlvec, &pdlvecsize, 1, max_size + 1, sizeof *specpdl);
2075 specpdl = pdlvec + 1;
2076 specpdl_size = pdlvecsize - 1;
2077 specpdl_ptr = specpdl + count;
2078 }
2079}
2080
2081void
2082record_in_backtrace (Lisp_Object function, Lisp_Object *args, ptrdiff_t nargs)
2083{
2084 eassert (nargs >= UNEVALLED);
2085 specpdl_ptr->bt.kind = SPECPDL_BACKTRACE;
2086 specpdl_ptr->bt.debug_on_exit = false;
2087 specpdl_ptr->bt.function = function;
2088 specpdl_ptr->bt.args = args;
2089 specpdl_ptr->bt.nargs = nargs;
2090 grow_specpdl ();
2091}
2092
2008/* Eval a sub-expression of the current expression (i.e. in the same 2093/* Eval a sub-expression of the current expression (i.e. in the same
2009 lexical scope). */ 2094 lexical scope). */
2010Lisp_Object 2095Lisp_Object
@@ -2012,12 +2097,8 @@ eval_sub (Lisp_Object form)
2012{ 2097{
2013 Lisp_Object fun, val, original_fun, original_args; 2098 Lisp_Object fun, val, original_fun, original_args;
2014 Lisp_Object funcar; 2099 Lisp_Object funcar;
2015 struct backtrace backtrace;
2016 struct gcpro gcpro1, gcpro2, gcpro3; 2100 struct gcpro gcpro1, gcpro2, gcpro3;
2017 2101
2018 if (handling_signal)
2019 abort ();
2020
2021 if (SYMBOLP (form)) 2102 if (SYMBOLP (form))
2022 { 2103 {
2023 /* Look up its binding in the lexical environment. 2104 /* Look up its binding in the lexical environment.
@@ -2037,15 +2118,10 @@ eval_sub (Lisp_Object form)
2037 return form; 2118 return form;
2038 2119
2039 QUIT; 2120 QUIT;
2040 if ((consing_since_gc > gc_cons_threshold 2121
2041 && consing_since_gc > gc_relative_threshold) 2122 GCPRO1 (form);
2042 || 2123 maybe_gc ();
2043 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold)) 2124 UNGCPRO;
2044 {
2045 GCPRO1 (form);
2046 Fgarbage_collect ();
2047 UNGCPRO;
2048 }
2049 2125
2050 if (++lisp_eval_depth > max_lisp_eval_depth) 2126 if (++lisp_eval_depth > max_lisp_eval_depth)
2051 { 2127 {
@@ -2055,15 +2131,11 @@ eval_sub (Lisp_Object form)
2055 error ("Lisp nesting exceeds `max-lisp-eval-depth'"); 2131 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2056 } 2132 }
2057 2133
2058 original_fun = Fcar (form); 2134 original_fun = XCAR (form);
2059 original_args = Fcdr (form); 2135 original_args = XCDR (form);
2060 2136
2061 backtrace.next = backtrace_list; 2137 /* This also protects them from gc. */
2062 backtrace_list = &backtrace; 2138 record_in_backtrace (original_fun, &original_args, UNEVALLED);
2063 backtrace.function = &original_fun; /* This also protects them from gc. */
2064 backtrace.args = &original_args;
2065 backtrace.nargs = UNEVALLED;
2066 backtrace.debug_on_exit = 0;
2067 2139
2068 if (debug_on_next_call) 2140 if (debug_on_next_call)
2069 do_debug_on_call (Qt); 2141 do_debug_on_call (Qt);
@@ -2074,9 +2146,11 @@ eval_sub (Lisp_Object form)
2074 2146
2075 /* Optimize for no indirection. */ 2147 /* Optimize for no indirection. */
2076 fun = original_fun; 2148 fun = original_fun;
2077 if (SYMBOLP (fun) && !EQ (fun, Qunbound) 2149 if (SYMBOLP (fun) && !NILP (fun)
2078 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) 2150 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2079 fun = indirect_function (fun); 2151 fun = indirect_function (fun);
2152 else
2153 fun = Ffunction (Fcons (fun, Qnil));
2080 2154
2081 if (SUBRP (fun)) 2155 if (SUBRP (fun))
2082 { 2156 {
@@ -2088,7 +2162,7 @@ eval_sub (Lisp_Object form)
2088 args_left = original_args; 2162 args_left = original_args;
2089 numargs = Flength (args_left); 2163 numargs = Flength (args_left);
2090 2164
2091 CHECK_CONS_LIST (); 2165 check_cons_list ();
2092 2166
2093 if (XINT (numargs) < XSUBR (fun)->min_args 2167 if (XINT (numargs) < XSUBR (fun)->min_args
2094 || (XSUBR (fun)->max_args >= 0 2168 || (XSUBR (fun)->max_args >= 0
@@ -2117,8 +2191,8 @@ eval_sub (Lisp_Object form)
2117 gcpro3.nvars = argnum; 2191 gcpro3.nvars = argnum;
2118 } 2192 }
2119 2193
2120 backtrace.args = vals; 2194 set_backtrace_args (specpdl_ptr - 1, vals);
2121 backtrace.nargs = XINT (numargs); 2195 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2122 2196
2123 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals); 2197 val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
2124 UNGCPRO; 2198 UNGCPRO;
@@ -2139,8 +2213,8 @@ eval_sub (Lisp_Object form)
2139 2213
2140 UNGCPRO; 2214 UNGCPRO;
2141 2215
2142 backtrace.args = argvals; 2216 set_backtrace_args (specpdl_ptr - 1, argvals);
2143 backtrace.nargs = XINT (numargs); 2217 set_backtrace_nargs (specpdl_ptr - 1, XINT (numargs));
2144 2218
2145 switch (i) 2219 switch (i)
2146 { 2220 {
@@ -2188,7 +2262,7 @@ eval_sub (Lisp_Object form)
2188 is supported by this code. We need to either rewrite the 2262 is supported by this code. We need to either rewrite the
2189 subr to use a different argument protocol, or add more 2263 subr to use a different argument protocol, or add more
2190 cases to this switch. */ 2264 cases to this switch. */
2191 abort (); 2265 emacs_abort ();
2192 } 2266 }
2193 } 2267 }
2194 } 2268 }
@@ -2196,7 +2270,7 @@ eval_sub (Lisp_Object form)
2196 val = apply_lambda (fun, original_args); 2270 val = apply_lambda (fun, original_args);
2197 else 2271 else
2198 { 2272 {
2199 if (EQ (fun, Qunbound)) 2273 if (NILP (fun))
2200 xsignal1 (Qvoid_function, original_fun); 2274 xsignal1 (Qvoid_function, original_fun);
2201 if (!CONSP (fun)) 2275 if (!CONSP (fun))
2202 xsignal1 (Qinvalid_function, original_fun); 2276 xsignal1 (Qinvalid_function, original_fun);
@@ -2205,28 +2279,39 @@ eval_sub (Lisp_Object form)
2205 xsignal1 (Qinvalid_function, original_fun); 2279 xsignal1 (Qinvalid_function, original_fun);
2206 if (EQ (funcar, Qautoload)) 2280 if (EQ (funcar, Qautoload))
2207 { 2281 {
2208 do_autoload (fun, original_fun); 2282 Fautoload_do_load (fun, original_fun, Qnil);
2209 goto retry; 2283 goto retry;
2210 } 2284 }
2211 if (EQ (funcar, Qmacro)) 2285 if (EQ (funcar, Qmacro))
2212 val = eval_sub (apply1 (Fcdr (fun), original_args)); 2286 {
2287 ptrdiff_t count = SPECPDL_INDEX ();
2288 Lisp_Object exp;
2289 /* Bind lexical-binding during expansion of the macro, so the
2290 macro can know reliably if the code it outputs will be
2291 interpreted using lexical-binding or not. */
2292 specbind (Qlexical_binding,
2293 NILP (Vinternal_interpreter_environment) ? Qnil : Qt);
2294 exp = apply1 (Fcdr (fun), original_args);
2295 unbind_to (count, Qnil);
2296 val = eval_sub (exp);
2297 }
2213 else if (EQ (funcar, Qlambda) 2298 else if (EQ (funcar, Qlambda)
2214 || EQ (funcar, Qclosure)) 2299 || EQ (funcar, Qclosure))
2215 val = apply_lambda (fun, original_args); 2300 val = apply_lambda (fun, original_args);
2216 else 2301 else
2217 xsignal1 (Qinvalid_function, original_fun); 2302 xsignal1 (Qinvalid_function, original_fun);
2218 } 2303 }
2219 CHECK_CONS_LIST (); 2304 check_cons_list ();
2220 2305
2221 lisp_eval_depth--; 2306 lisp_eval_depth--;
2222 if (backtrace.debug_on_exit) 2307 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2223 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil))); 2308 val = call_debugger (list2 (Qexit, val));
2224 backtrace_list = backtrace.next; 2309 specpdl_ptr--;
2225 2310
2226 return val; 2311 return val;
2227} 2312}
2228 2313
2229DEFUN ("apply", Fapply, Sapply, 2, MANY, 0, 2314DEFUN ("apply", Fapply, Sapply, 1, MANY, 0,
2230 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args. 2315 doc: /* Call FUNCTION with our remaining args, using our last arg as list of args.
2231Then return the value FUNCTION returns. 2316Then return the value FUNCTION returns.
2232Thus, (apply '+ 1 2 '(3 4)) returns 10. 2317Thus, (apply '+ 1 2 '(3 4)) returns 10.
@@ -2259,10 +2344,10 @@ usage: (apply FUNCTION &rest ARGUMENTS) */)
2259 numargs += nargs - 2; 2344 numargs += nargs - 2;
2260 2345
2261 /* Optimize for no indirection. */ 2346 /* Optimize for no indirection. */
2262 if (SYMBOLP (fun) && !EQ (fun, Qunbound) 2347 if (SYMBOLP (fun) && !NILP (fun)
2263 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) 2348 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2264 fun = indirect_function (fun); 2349 fun = indirect_function (fun);
2265 if (EQ (fun, Qunbound)) 2350 if (NILP (fun))
2266 { 2351 {
2267 /* Let funcall get the error. */ 2352 /* Let funcall get the error. */
2268 fun = args[0]; 2353 fun = args[0];
@@ -2295,7 +2380,7 @@ usage: (apply FUNCTION &rest ARGUMENTS) */)
2295 gcpro1.nvars = 1 + numargs; 2380 gcpro1.nvars = 1 + numargs;
2296 } 2381 }
2297 2382
2298 memcpy (funcall_args, args, nargs * sizeof (Lisp_Object)); 2383 memcpy (funcall_args, args, nargs * word_size);
2299 /* Spread the last arg we got. Its first element goes in 2384 /* Spread the last arg we got. Its first element goes in
2300 the slot that it used to occupy, hence this value of I. */ 2385 the slot that it used to occupy, hence this value of I. */
2301 i = nargs - 1; 2386 i = nargs - 1;
@@ -2354,14 +2439,10 @@ usage: (run-hooks &rest HOOKS) */)
2354DEFUN ("run-hook-with-args", Frun_hook_with_args, 2439DEFUN ("run-hook-with-args", Frun_hook_with_args,
2355 Srun_hook_with_args, 1, MANY, 0, 2440 Srun_hook_with_args, 1, MANY, 0,
2356 doc: /* Run HOOK with the specified arguments ARGS. 2441 doc: /* Run HOOK with the specified arguments ARGS.
2357HOOK should be a symbol, a hook variable. If HOOK has a non-nil 2442HOOK should be a symbol, a hook variable. The value of HOOK
2358value, that value may be a function or a list of functions to be 2443may be nil, a function, or a list of functions. Call each
2359called to run the hook. If the value is a function, it is called with 2444function in order with arguments ARGS. The final return value
2360the given arguments and its return value is returned. If it is a list 2445is unspecified.
2361of functions, those functions are called, in order,
2362with the given arguments ARGS.
2363It is best not to depend on the value returned by `run-hook-with-args',
2364as that may change.
2365 2446
2366Do not use `make-local-variable' to make a hook variable buffer-local. 2447Do not use `make-local-variable' to make a hook variable buffer-local.
2367Instead, use `add-hook' and specify t for the LOCAL argument. 2448Instead, use `add-hook' and specify t for the LOCAL argument.
@@ -2371,17 +2452,18 @@ usage: (run-hook-with-args HOOK &rest ARGS) */)
2371 return run_hook_with_args (nargs, args, funcall_nil); 2452 return run_hook_with_args (nargs, args, funcall_nil);
2372} 2453}
2373 2454
2455/* NB this one still documents a specific non-nil return value.
2456 (As did run-hook-with-args and run-hook-with-args-until-failure
2457 until they were changed in 24.1.) */
2374DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, 2458DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success,
2375 Srun_hook_with_args_until_success, 1, MANY, 0, 2459 Srun_hook_with_args_until_success, 1, MANY, 0,
2376 doc: /* Run HOOK with the specified arguments ARGS. 2460 doc: /* Run HOOK with the specified arguments ARGS.
2377HOOK should be a symbol, a hook variable. If HOOK has a non-nil 2461HOOK should be a symbol, a hook variable. The value of HOOK
2378value, that value may be a function or a list of functions to be 2462may be nil, a function, or a list of functions. Call each
2379called to run the hook. If the value is a function, it is called with 2463function in order with arguments ARGS, stopping at the first
2380the given arguments and its return value is returned. 2464one that returns non-nil, and return that value. Otherwise (if
2381If it is a list of functions, those functions are called, in order, 2465all functions return nil, or if there are no functions to call),
2382with the given arguments ARGS, until one of them 2466return nil.
2383returns a non-nil value. Then we return that value.
2384However, if they all return nil, we return nil.
2385 2467
2386Do not use `make-local-variable' to make a hook variable buffer-local. 2468Do not use `make-local-variable' to make a hook variable buffer-local.
2387Instead, use `add-hook' and specify t for the LOCAL argument. 2469Instead, use `add-hook' and specify t for the LOCAL argument.
@@ -2400,13 +2482,12 @@ funcall_not (ptrdiff_t nargs, Lisp_Object *args)
2400DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, 2482DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
2401 Srun_hook_with_args_until_failure, 1, MANY, 0, 2483 Srun_hook_with_args_until_failure, 1, MANY, 0,
2402 doc: /* Run HOOK with the specified arguments ARGS. 2484 doc: /* Run HOOK with the specified arguments ARGS.
2403HOOK should be a symbol, a hook variable. If HOOK has a non-nil 2485HOOK should be a symbol, a hook variable. The value of HOOK
2404value, that value may be a function or a list of functions to be 2486may be nil, a function, or a list of functions. Call each
2405called to run the hook. If the value is a function, it is called with 2487function in order with arguments ARGS, stopping at the first
2406the given arguments and its return value is returned. 2488one that returns nil, and return nil. Otherwise (if all functions
2407If it is a list of functions, those functions are called, in order, 2489return non-nil, or if there are no functions to call), return non-nil
2408with the given arguments ARGS, until one of them returns nil. 2490\(do not rely on the precise return value in this case).
2409Then we return nil. However, if they all return non-nil, we return non-nil.
2410 2491
2411Do not use `make-local-variable' to make a hook variable buffer-local. 2492Do not use `make-local-variable' to make a hook variable buffer-local.
2412Instead, use `add-hook' and specify t for the LOCAL argument. 2493Instead, use `add-hook' and specify t for the LOCAL argument.
@@ -2688,33 +2769,9 @@ DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
2688 doc: /* Non-nil if OBJECT is a function. */) 2769 doc: /* Non-nil if OBJECT is a function. */)
2689 (Lisp_Object object) 2770 (Lisp_Object object)
2690{ 2771{
2691 if (SYMBOLP (object) && !NILP (Ffboundp (object))) 2772 if (FUNCTIONP (object))
2692 {
2693 object = Findirect_function (object, Qt);
2694
2695 if (CONSP (object) && EQ (XCAR (object), Qautoload))
2696 {
2697 /* Autoloaded symbols are functions, except if they load
2698 macros or keymaps. */
2699 int i;
2700 for (i = 0; i < 4 && CONSP (object); i++)
2701 object = XCDR (object);
2702
2703 return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
2704 }
2705 }
2706
2707 if (SUBRP (object))
2708 return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
2709 else if (COMPILEDP (object))
2710 return Qt; 2773 return Qt;
2711 else if (CONSP (object)) 2774 return Qnil;
2712 {
2713 Lisp_Object car = XCAR (object);
2714 return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
2715 }
2716 else
2717 return Qnil;
2718} 2775}
2719 2776
2720DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0, 2777DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
@@ -2729,16 +2786,10 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2729 ptrdiff_t numargs = nargs - 1; 2786 ptrdiff_t numargs = nargs - 1;
2730 Lisp_Object lisp_numargs; 2787 Lisp_Object lisp_numargs;
2731 Lisp_Object val; 2788 Lisp_Object val;
2732 struct backtrace backtrace;
2733 register Lisp_Object *internal_args; 2789 register Lisp_Object *internal_args;
2734 ptrdiff_t i; 2790 ptrdiff_t i;
2735 2791
2736 QUIT; 2792 QUIT;
2737 if ((consing_since_gc > gc_cons_threshold
2738 && consing_since_gc > gc_relative_threshold)
2739 ||
2740 (!NILP (Vmemory_full) && consing_since_gc > memory_full_cons_threshold))
2741 Fgarbage_collect ();
2742 2793
2743 if (++lisp_eval_depth > max_lisp_eval_depth) 2794 if (++lisp_eval_depth > max_lisp_eval_depth)
2744 { 2795 {
@@ -2748,17 +2799,16 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2748 error ("Lisp nesting exceeds `max-lisp-eval-depth'"); 2799 error ("Lisp nesting exceeds `max-lisp-eval-depth'");
2749 } 2800 }
2750 2801
2751 backtrace.next = backtrace_list; 2802 /* This also GCPROs them. */
2752 backtrace_list = &backtrace; 2803 record_in_backtrace (args[0], &args[1], nargs - 1);
2753 backtrace.function = &args[0]; 2804
2754 backtrace.args = &args[1]; 2805 /* Call GC after setting up the backtrace, so the latter GCPROs the args. */
2755 backtrace.nargs = nargs - 1; 2806 maybe_gc ();
2756 backtrace.debug_on_exit = 0;
2757 2807
2758 if (debug_on_next_call) 2808 if (debug_on_next_call)
2759 do_debug_on_call (Qlambda); 2809 do_debug_on_call (Qlambda);
2760 2810
2761 CHECK_CONS_LIST (); 2811 check_cons_list ();
2762 2812
2763 original_fun = args[0]; 2813 original_fun = args[0];
2764 2814
@@ -2766,7 +2816,7 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2766 2816
2767 /* Optimize for no indirection. */ 2817 /* Optimize for no indirection. */
2768 fun = original_fun; 2818 fun = original_fun;
2769 if (SYMBOLP (fun) && !EQ (fun, Qunbound) 2819 if (SYMBOLP (fun) && !NILP (fun)
2770 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun))) 2820 && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
2771 fun = indirect_function (fun); 2821 fun = indirect_function (fun);
2772 2822
@@ -2788,8 +2838,9 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2788 { 2838 {
2789 if (XSUBR (fun)->max_args > numargs) 2839 if (XSUBR (fun)->max_args > numargs)
2790 { 2840 {
2791 internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object)); 2841 internal_args = alloca (XSUBR (fun)->max_args
2792 memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object)); 2842 * sizeof *internal_args);
2843 memcpy (internal_args, args + 1, numargs * word_size);
2793 for (i = numargs; i < XSUBR (fun)->max_args; i++) 2844 for (i = numargs; i < XSUBR (fun)->max_args; i++)
2794 internal_args[i] = Qnil; 2845 internal_args[i] = Qnil;
2795 } 2846 }
@@ -2845,7 +2896,7 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2845 /* If a subr takes more than 8 arguments without using MANY 2896 /* If a subr takes more than 8 arguments without using MANY
2846 or UNEVALLED, we need to extend this function to support it. 2897 or UNEVALLED, we need to extend this function to support it.
2847 Until this is done, there is no way to call the function. */ 2898 Until this is done, there is no way to call the function. */
2848 abort (); 2899 emacs_abort ();
2849 } 2900 }
2850 } 2901 }
2851 } 2902 }
@@ -2853,7 +2904,7 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2853 val = funcall_lambda (fun, numargs, args + 1); 2904 val = funcall_lambda (fun, numargs, args + 1);
2854 else 2905 else
2855 { 2906 {
2856 if (EQ (fun, Qunbound)) 2907 if (NILP (fun))
2857 xsignal1 (Qvoid_function, original_fun); 2908 xsignal1 (Qvoid_function, original_fun);
2858 if (!CONSP (fun)) 2909 if (!CONSP (fun))
2859 xsignal1 (Qinvalid_function, original_fun); 2910 xsignal1 (Qinvalid_function, original_fun);
@@ -2865,18 +2916,18 @@ usage: (funcall FUNCTION &rest ARGUMENTS) */)
2865 val = funcall_lambda (fun, numargs, args + 1); 2916 val = funcall_lambda (fun, numargs, args + 1);
2866 else if (EQ (funcar, Qautoload)) 2917 else if (EQ (funcar, Qautoload))
2867 { 2918 {
2868 do_autoload (fun, original_fun); 2919 Fautoload_do_load (fun, original_fun, Qnil);
2869 CHECK_CONS_LIST (); 2920 check_cons_list ();
2870 goto retry; 2921 goto retry;
2871 } 2922 }
2872 else 2923 else
2873 xsignal1 (Qinvalid_function, original_fun); 2924 xsignal1 (Qinvalid_function, original_fun);
2874 } 2925 }
2875 CHECK_CONS_LIST (); 2926 check_cons_list ();
2876 lisp_eval_depth--; 2927 lisp_eval_depth--;
2877 if (backtrace.debug_on_exit) 2928 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2878 val = call_debugger (Fcons (Qexit, Fcons (val, Qnil))); 2929 val = call_debugger (list2 (Qexit, val));
2879 backtrace_list = backtrace.next; 2930 specpdl_ptr--;
2880 return val; 2931 return val;
2881} 2932}
2882 2933
@@ -2908,15 +2959,17 @@ apply_lambda (Lisp_Object fun, Lisp_Object args)
2908 2959
2909 UNGCPRO; 2960 UNGCPRO;
2910 2961
2911 backtrace_list->args = arg_vector; 2962 set_backtrace_args (specpdl_ptr - 1, arg_vector);
2912 backtrace_list->nargs = i; 2963 set_backtrace_nargs (specpdl_ptr - 1, i);
2913 tem = funcall_lambda (fun, numargs, arg_vector); 2964 tem = funcall_lambda (fun, numargs, arg_vector);
2914 2965
2915 /* Do the debug-on-exit now, while arg_vector still exists. */ 2966 /* Do the debug-on-exit now, while arg_vector still exists. */
2916 if (backtrace_list->debug_on_exit) 2967 if (backtrace_debug_on_exit (specpdl_ptr - 1))
2917 tem = call_debugger (Fcons (Qexit, Fcons (tem, Qnil))); 2968 {
2918 /* Don't do it again when we return to eval. */ 2969 /* Don't do it again when we return to eval. */
2919 backtrace_list->debug_on_exit = 0; 2970 set_backtrace_debug_on_exit (specpdl_ptr - 1, false);
2971 tem = call_debugger (list2 (Qexit, tem));
2972 }
2920 SAFE_FREE (); 2973 SAFE_FREE ();
2921 return tem; 2974 return tem;
2922} 2975}
@@ -2932,7 +2985,7 @@ funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2932 Lisp_Object val, syms_left, next, lexenv; 2985 Lisp_Object val, syms_left, next, lexenv;
2933 ptrdiff_t count = SPECPDL_INDEX (); 2986 ptrdiff_t count = SPECPDL_INDEX ();
2934 ptrdiff_t i; 2987 ptrdiff_t i;
2935 int optional, rest; 2988 bool optional, rest;
2936 2989
2937 if (CONSP (fun)) 2990 if (CONSP (fun))
2938 { 2991 {
@@ -2976,7 +3029,7 @@ funcall_lambda (Lisp_Object fun, ptrdiff_t nargs,
2976 lexenv = Qnil; 3029 lexenv = Qnil;
2977 } 3030 }
2978 else 3031 else
2979 abort (); 3032 emacs_abort ();
2980 3033
2981 i = optional = rest = 0; 3034 i = optional = rest = 0;
2982 for (; CONSP (syms_left); syms_left = XCDR (syms_left)) 3035 for (; CONSP (syms_left); syms_left = XCDR (syms_left))
@@ -3066,48 +3119,59 @@ DEFUN ("fetch-bytecode", Ffetch_bytecode, Sfetch_bytecode,
3066 return object; 3119 return object;
3067} 3120}
3068 3121
3069static void 3122/* Return true if SYMBOL currently has a let-binding
3070grow_specpdl (void) 3123 which was made in the buffer that is now current. */
3124
3125bool
3126let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol)
3071{ 3127{
3072 register ptrdiff_t count = SPECPDL_INDEX (); 3128 union specbinding *p;
3073 ptrdiff_t max_size = min (max_specpdl_size, PTRDIFF_MAX); 3129 Lisp_Object buf = Fcurrent_buffer ();
3074 if (max_size <= specpdl_size) 3130
3075 { 3131 for (p = specpdl_ptr; p > specpdl; )
3076 if (max_specpdl_size < 400) 3132 if ((--p)->kind > SPECPDL_LET)
3077 max_size = max_specpdl_size = 400; 3133 {
3078 if (max_size <= specpdl_size) 3134 struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p));
3079 signal_error ("Variable binding depth exceeds max-specpdl-size", Qnil); 3135 eassert (let_bound_symbol->redirect != SYMBOL_VARALIAS);
3080 } 3136 if (symbol == let_bound_symbol
3081 specpdl = xpalloc (specpdl, &specpdl_size, 1, max_size, sizeof *specpdl); 3137 && EQ (specpdl_where (p), buf))
3082 specpdl_ptr = specpdl + count; 3138 return 1;
3139 }
3140
3141 return 0;
3142}
3143
3144bool
3145let_shadows_global_binding_p (Lisp_Object symbol)
3146{
3147 union specbinding *p;
3148
3149 for (p = specpdl_ptr; p > specpdl; )
3150 if ((--p)->kind >= SPECPDL_LET && EQ (specpdl_symbol (p), symbol))
3151 return 1;
3152
3153 return 0;
3083} 3154}
3084 3155
3085/* `specpdl_ptr->symbol' is a field which describes which variable is 3156/* `specpdl_ptr' describes which variable is
3086 let-bound, so it can be properly undone when we unbind_to. 3157 let-bound, so it can be properly undone when we unbind_to.
3087 It can have the following two shapes: 3158 It can be either a plain SPECPDL_LET or a SPECPDL_LET_LOCAL/DEFAULT.
3088 - SYMBOL : if it's a plain symbol, it means that we have let-bound 3159 - SYMBOL is the variable being bound. Note that it should not be
3089 a symbol that is not buffer-local (at least at the time
3090 the let binding started). Note also that it should not be
3091 aliased (i.e. when let-binding V1 that's aliased to V2, we want 3160 aliased (i.e. when let-binding V1 that's aliased to V2, we want
3092 to record V2 here). 3161 to record V2 here).
3093 - (SYMBOL WHERE . BUFFER) : this means that it is a let-binding for 3162 - WHERE tells us in which buffer the binding took place.
3094 variable SYMBOL which can be buffer-local. WHERE tells us 3163 This is used for SPECPDL_LET_LOCAL bindings (i.e. bindings to a
3095 which buffer is affected (or nil if the let-binding affects the 3164 buffer-local variable) as well as for SPECPDL_LET_DEFAULT bindings,
3096 global value of the variable) and BUFFER tells us which buffer was 3165 i.e. bindings to the default value of a variable which can be
3097 current (i.e. if WHERE is non-nil, then BUFFER==WHERE, otherwise 3166 buffer-local. */
3098 BUFFER did not yet have a buffer-local value). */
3099 3167
3100void 3168void
3101specbind (Lisp_Object symbol, Lisp_Object value) 3169specbind (Lisp_Object symbol, Lisp_Object value)
3102{ 3170{
3103 struct Lisp_Symbol *sym; 3171 struct Lisp_Symbol *sym;
3104 3172
3105 eassert (!handling_signal);
3106
3107 CHECK_SYMBOL (symbol); 3173 CHECK_SYMBOL (symbol);
3108 sym = XSYMBOL (symbol); 3174 sym = XSYMBOL (symbol);
3109 if (specpdl_ptr == specpdl + specpdl_size)
3110 grow_specpdl ();
3111 3175
3112 start: 3176 start:
3113 switch (sym->redirect) 3177 switch (sym->redirect)
@@ -3117,10 +3181,10 @@ specbind (Lisp_Object symbol, Lisp_Object value)
3117 case SYMBOL_PLAINVAL: 3181 case SYMBOL_PLAINVAL:
3118 /* The most common case is that of a non-constant symbol with a 3182 /* The most common case is that of a non-constant symbol with a
3119 trivial value. Make that as fast as we can. */ 3183 trivial value. Make that as fast as we can. */
3120 specpdl_ptr->symbol = symbol; 3184 specpdl_ptr->let.kind = SPECPDL_LET;
3121 specpdl_ptr->old_value = SYMBOL_VAL (sym); 3185 specpdl_ptr->let.symbol = symbol;
3122 specpdl_ptr->func = NULL; 3186 specpdl_ptr->let.old_value = SYMBOL_VAL (sym);
3123 ++specpdl_ptr; 3187 grow_specpdl ();
3124 if (!sym->constant) 3188 if (!sym->constant)
3125 SET_SYMBOL_VAL (sym, value); 3189 SET_SYMBOL_VAL (sym, value);
3126 else 3190 else
@@ -3132,81 +3196,132 @@ specbind (Lisp_Object symbol, Lisp_Object value)
3132 case SYMBOL_FORWARDED: 3196 case SYMBOL_FORWARDED:
3133 { 3197 {
3134 Lisp_Object ovalue = find_symbol_value (symbol); 3198 Lisp_Object ovalue = find_symbol_value (symbol);
3135 specpdl_ptr->func = 0; 3199 specpdl_ptr->let.kind = SPECPDL_LET_LOCAL;
3136 specpdl_ptr->old_value = ovalue; 3200 specpdl_ptr->let.symbol = symbol;
3201 specpdl_ptr->let.old_value = ovalue;
3202 specpdl_ptr->let.where = Fcurrent_buffer ();
3137 3203
3138 eassert (sym->redirect != SYMBOL_LOCALIZED 3204 eassert (sym->redirect != SYMBOL_LOCALIZED
3139 || (EQ (SYMBOL_BLV (sym)->where, 3205 || (EQ (SYMBOL_BLV (sym)->where, Fcurrent_buffer ())));
3140 SYMBOL_BLV (sym)->frame_local ?
3141 Fselected_frame () : Fcurrent_buffer ())));
3142 3206
3143 if (sym->redirect == SYMBOL_LOCALIZED 3207 if (sym->redirect == SYMBOL_LOCALIZED)
3144 || BUFFER_OBJFWDP (SYMBOL_FWD (sym))) 3208 {
3209 if (!blv_found (SYMBOL_BLV (sym)))
3210 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3211 }
3212 else if (BUFFER_OBJFWDP (SYMBOL_FWD (sym)))
3145 { 3213 {
3146 Lisp_Object where, cur_buf = Fcurrent_buffer ();
3147
3148 /* For a local variable, record both the symbol and which
3149 buffer's or frame's value we are saving. */
3150 if (!NILP (Flocal_variable_p (symbol, Qnil)))
3151 {
3152 eassert (sym->redirect != SYMBOL_LOCALIZED
3153 || (BLV_FOUND (SYMBOL_BLV (sym))
3154 && EQ (cur_buf, SYMBOL_BLV (sym)->where)));
3155 where = cur_buf;
3156 }
3157 else if (sym->redirect == SYMBOL_LOCALIZED
3158 && BLV_FOUND (SYMBOL_BLV (sym)))
3159 where = SYMBOL_BLV (sym)->where;
3160 else
3161 where = Qnil;
3162
3163 /* We're not using the `unused' slot in the specbinding
3164 structure because this would mean we have to do more
3165 work for simple variables. */
3166 /* FIXME: The third value `current_buffer' is only used in
3167 let_shadows_buffer_binding_p which is itself only used
3168 in set_internal for local_if_set. */
3169 eassert (NILP (where) || EQ (where, cur_buf));
3170 specpdl_ptr->symbol = Fcons (symbol, Fcons (where, cur_buf));
3171
3172 /* If SYMBOL is a per-buffer variable which doesn't have a 3214 /* If SYMBOL is a per-buffer variable which doesn't have a
3173 buffer-local value here, make the `let' change the global 3215 buffer-local value here, make the `let' change the global
3174 value by changing the value of SYMBOL in all buffers not 3216 value by changing the value of SYMBOL in all buffers not
3175 having their own value. This is consistent with what 3217 having their own value. This is consistent with what
3176 happens with other buffer-local variables. */ 3218 happens with other buffer-local variables. */
3177 if (NILP (where) 3219 if (NILP (Flocal_variable_p (symbol, Qnil)))
3178 && sym->redirect == SYMBOL_FORWARDED)
3179 { 3220 {
3180 eassert (BUFFER_OBJFWDP (SYMBOL_FWD (sym))); 3221 specpdl_ptr->let.kind = SPECPDL_LET_DEFAULT;
3181 ++specpdl_ptr; 3222 grow_specpdl ();
3182 Fset_default (symbol, value); 3223 Fset_default (symbol, value);
3183 return; 3224 return;
3184 } 3225 }
3185 } 3226 }
3186 else 3227 else
3187 specpdl_ptr->symbol = symbol; 3228 specpdl_ptr->let.kind = SPECPDL_LET;
3188 3229
3189 specpdl_ptr++; 3230 grow_specpdl ();
3190 set_internal (symbol, value, Qnil, 1); 3231 set_internal (symbol, value, Qnil, 1);
3191 break; 3232 break;
3192 } 3233 }
3193 default: abort (); 3234 default: emacs_abort ();
3194 } 3235 }
3195} 3236}
3196 3237
3238/* Push unwind-protect entries of various types. */
3239
3197void 3240void
3198record_unwind_protect (Lisp_Object (*function) (Lisp_Object), Lisp_Object arg) 3241record_unwind_protect (void (*function) (Lisp_Object), Lisp_Object arg)
3199{ 3242{
3200 eassert (!handling_signal); 3243 specpdl_ptr->unwind.kind = SPECPDL_UNWIND;
3244 specpdl_ptr->unwind.func = function;
3245 specpdl_ptr->unwind.arg = arg;
3246 grow_specpdl ();
3247}
3201 3248
3202 if (specpdl_ptr == specpdl + specpdl_size) 3249void
3203 grow_specpdl (); 3250record_unwind_protect_ptr (void (*function) (void *), void *arg)
3204 specpdl_ptr->func = function; 3251{
3205 specpdl_ptr->symbol = Qnil; 3252 specpdl_ptr->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3206 specpdl_ptr->old_value = arg; 3253 specpdl_ptr->unwind_ptr.func = function;
3207 specpdl_ptr++; 3254 specpdl_ptr->unwind_ptr.arg = arg;
3255 grow_specpdl ();
3208} 3256}
3209 3257
3258void
3259record_unwind_protect_int (void (*function) (int), int arg)
3260{
3261 specpdl_ptr->unwind_int.kind = SPECPDL_UNWIND_INT;
3262 specpdl_ptr->unwind_int.func = function;
3263 specpdl_ptr->unwind_int.arg = arg;
3264 grow_specpdl ();
3265}
3266
3267void
3268record_unwind_protect_void (void (*function) (void))
3269{
3270 specpdl_ptr->unwind_void.kind = SPECPDL_UNWIND_VOID;
3271 specpdl_ptr->unwind_void.func = function;
3272 grow_specpdl ();
3273}
3274
3275static void
3276do_nothing (void)
3277{}
3278
3279/* Push an unwind-protect entry that does nothing, so that
3280 set_unwind_protect_ptr can overwrite it later. */
3281
3282void
3283record_unwind_protect_nothing (void)
3284{
3285 record_unwind_protect_void (do_nothing);
3286}
3287
3288/* Clear the unwind-protect entry COUNT, so that it does nothing.
3289 It need not be at the top of the stack. */
3290
3291void
3292clear_unwind_protect (ptrdiff_t count)
3293{
3294 union specbinding *p = specpdl + count;
3295 p->unwind_void.kind = SPECPDL_UNWIND_VOID;
3296 p->unwind_void.func = do_nothing;
3297}
3298
3299/* Set the unwind-protect entry COUNT so that it invokes FUNC (ARG).
3300 It need not be at the top of the stack. Discard the entry's
3301 previous value without invoking it. */
3302
3303void
3304set_unwind_protect (ptrdiff_t count, void (*func) (Lisp_Object),
3305 Lisp_Object arg)
3306{
3307 union specbinding *p = specpdl + count;
3308 p->unwind.kind = SPECPDL_UNWIND;
3309 p->unwind.func = func;
3310 p->unwind.arg = arg;
3311}
3312
3313void
3314set_unwind_protect_ptr (ptrdiff_t count, void (*func) (void *), void *arg)
3315{
3316 union specbinding *p = specpdl + count;
3317 p->unwind_ptr.kind = SPECPDL_UNWIND_PTR;
3318 p->unwind_ptr.func = func;
3319 p->unwind_ptr.arg = arg;
3320}
3321
3322/* Pop and execute entries from the unwind-protect stack until the
3323 depth COUNT is reached. Return VALUE. */
3324
3210Lisp_Object 3325Lisp_Object
3211unbind_to (ptrdiff_t count, Lisp_Object value) 3326unbind_to (ptrdiff_t count, Lisp_Object value)
3212{ 3327{
@@ -3218,50 +3333,63 @@ unbind_to (ptrdiff_t count, Lisp_Object value)
3218 3333
3219 while (specpdl_ptr != specpdl + count) 3334 while (specpdl_ptr != specpdl + count)
3220 { 3335 {
3221 /* Copy the binding, and decrement specpdl_ptr, before we do 3336 /* Decrement specpdl_ptr before we do the work to unbind it, so
3222 the work to unbind it. We decrement first 3337 that an error in unbinding won't try to unbind the same entry
3223 so that an error in unbinding won't try to unbind 3338 again. Take care to copy any parts of the binding needed
3224 the same entry again, and we copy the binding first 3339 before invoking any code that can make more bindings. */
3225 in case more bindings are made during some of the code we run. */ 3340
3226 3341 specpdl_ptr--;
3227 struct specbinding this_binding; 3342
3228 this_binding = *--specpdl_ptr; 3343 switch (specpdl_ptr->kind)
3229
3230 if (this_binding.func != 0)
3231 (*this_binding.func) (this_binding.old_value);
3232 /* If the symbol is a list, it is really (SYMBOL WHERE
3233 . CURRENT-BUFFER) where WHERE is either nil, a buffer, or a
3234 frame. If WHERE is a buffer or frame, this indicates we
3235 bound a variable that had a buffer-local or frame-local
3236 binding. WHERE nil means that the variable had the default
3237 value when it was bound. CURRENT-BUFFER is the buffer that
3238 was current when the variable was bound. */
3239 else if (CONSP (this_binding.symbol))
3240 { 3344 {
3241 Lisp_Object symbol, where; 3345 case SPECPDL_UNWIND:
3242 3346 specpdl_ptr->unwind.func (specpdl_ptr->unwind.arg);
3243 symbol = XCAR (this_binding.symbol); 3347 break;
3244 where = XCAR (XCDR (this_binding.symbol)); 3348 case SPECPDL_UNWIND_PTR:
3245 3349 specpdl_ptr->unwind_ptr.func (specpdl_ptr->unwind_ptr.arg);
3246 if (NILP (where)) 3350 break;
3247 Fset_default (symbol, this_binding.old_value); 3351 case SPECPDL_UNWIND_INT:
3248 /* If `where' is non-nil, reset the value in the appropriate 3352 specpdl_ptr->unwind_int.func (specpdl_ptr->unwind_int.arg);
3249 local binding, but only if that binding still exists. */ 3353 break;
3250 else if (BUFFERP (where) 3354 case SPECPDL_UNWIND_VOID:
3251 ? !NILP (Flocal_variable_p (symbol, where)) 3355 specpdl_ptr->unwind_void.func ();
3252 : !NILP (Fassq (symbol, XFRAME (where)->param_alist))) 3356 break;
3253 set_internal (symbol, this_binding.old_value, where, 1); 3357 case SPECPDL_BACKTRACE:
3358 break;
3359 case SPECPDL_LET:
3360 { /* If variable has a trivial value (no forwarding), we can
3361 just set it. No need to check for constant symbols here,
3362 since that was already done by specbind. */
3363 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (specpdl_ptr));
3364 if (sym->redirect == SYMBOL_PLAINVAL)
3365 {
3366 SET_SYMBOL_VAL (sym, specpdl_old_value (specpdl_ptr));
3367 break;
3368 }
3369 else
3370 { /* FALLTHROUGH!!
3371 NOTE: we only ever come here if make_local_foo was used for
3372 the first time on this var within this let. */
3373 }
3374 }
3375 case SPECPDL_LET_DEFAULT:
3376 Fset_default (specpdl_symbol (specpdl_ptr),
3377 specpdl_old_value (specpdl_ptr));
3378 break;
3379 case SPECPDL_LET_LOCAL:
3380 {
3381 Lisp_Object symbol = specpdl_symbol (specpdl_ptr);
3382 Lisp_Object where = specpdl_where (specpdl_ptr);
3383 Lisp_Object old_value = specpdl_old_value (specpdl_ptr);
3384 eassert (BUFFERP (where));
3385
3386 /* If this was a local binding, reset the value in the appropriate
3387 buffer, but only if that buffer's binding still exists. */
3388 if (!NILP (Flocal_variable_p (symbol, where)))
3389 set_internal (symbol, old_value, where, 1);
3390 }
3391 break;
3254 } 3392 }
3255 /* If variable has a trivial value (no forwarding), we can
3256 just set it. No need to check for constant symbols here,
3257 since that was already done by specbind. */
3258 else if (XSYMBOL (this_binding.symbol)->redirect == SYMBOL_PLAINVAL)
3259 SET_SYMBOL_VAL (XSYMBOL (this_binding.symbol),
3260 this_binding.old_value);
3261 else
3262 /* NOTE: we only ever come here if make_local_foo was used for
3263 the first time on this var within this let. */
3264 Fset_default (this_binding.symbol, this_binding.old_value);
3265 } 3393 }
3266 3394
3267 if (NILP (Vquit_flag) && !NILP (quitf)) 3395 if (NILP (Vquit_flag) && !NILP (quitf))
@@ -3287,18 +3415,16 @@ DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
3287The debugger is entered when that frame exits, if the flag is non-nil. */) 3415The debugger is entered when that frame exits, if the flag is non-nil. */)
3288 (Lisp_Object level, Lisp_Object flag) 3416 (Lisp_Object level, Lisp_Object flag)
3289{ 3417{
3290 register struct backtrace *backlist = backtrace_list; 3418 union specbinding *pdl = backtrace_top ();
3291 register EMACS_INT i; 3419 register EMACS_INT i;
3292 3420
3293 CHECK_NUMBER (level); 3421 CHECK_NUMBER (level);
3294 3422
3295 for (i = 0; backlist && i < XINT (level); i++) 3423 for (i = 0; backtrace_p (pdl) && i < XINT (level); i++)
3296 { 3424 pdl = backtrace_next (pdl);
3297 backlist = backlist->next;
3298 }
3299 3425
3300 if (backlist) 3426 if (backtrace_p (pdl))
3301 backlist->debug_on_exit = !NILP (flag); 3427 set_backtrace_debug_on_exit (pdl, !NILP (flag));
3302 3428
3303 return flag; 3429 return flag;
3304} 3430}
@@ -3308,62 +3434,68 @@ DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
3308Output stream used is value of `standard-output'. */) 3434Output stream used is value of `standard-output'. */)
3309 (void) 3435 (void)
3310{ 3436{
3311 register struct backtrace *backlist = backtrace_list; 3437 union specbinding *pdl = backtrace_top ();
3312 Lisp_Object tail;
3313 Lisp_Object tem; 3438 Lisp_Object tem;
3314 struct gcpro gcpro1;
3315 Lisp_Object old_print_level = Vprint_level; 3439 Lisp_Object old_print_level = Vprint_level;
3316 3440
3317 if (NILP (Vprint_level)) 3441 if (NILP (Vprint_level))
3318 XSETFASTINT (Vprint_level, 8); 3442 XSETFASTINT (Vprint_level, 8);
3319 3443
3320 tail = Qnil; 3444 while (backtrace_p (pdl))
3321 GCPRO1 (tail);
3322
3323 while (backlist)
3324 { 3445 {
3325 write_string (backlist->debug_on_exit ? "* " : " ", 2); 3446 write_string (backtrace_debug_on_exit (pdl) ? "* " : " ", 2);
3326 if (backlist->nargs == UNEVALLED) 3447 if (backtrace_nargs (pdl) == UNEVALLED)
3327 { 3448 {
3328 Fprin1 (Fcons (*backlist->function, *backlist->args), Qnil); 3449 Fprin1 (Fcons (backtrace_function (pdl), *backtrace_args (pdl)),
3450 Qnil);
3329 write_string ("\n", -1); 3451 write_string ("\n", -1);
3330 } 3452 }
3331 else 3453 else
3332 { 3454 {
3333 tem = *backlist->function; 3455 tem = backtrace_function (pdl);
3334 Fprin1 (tem, Qnil); /* This can QUIT. */ 3456 Fprin1 (tem, Qnil); /* This can QUIT. */
3335 write_string ("(", -1); 3457 write_string ("(", -1);
3336 if (backlist->nargs == MANY) 3458 {
3337 { /* FIXME: Can this happen? */ 3459 ptrdiff_t i;
3338 int i; 3460 for (i = 0; i < backtrace_nargs (pdl); i++)
3339 for (tail = *backlist->args, i = 0; 3461 {
3340 !NILP (tail); 3462 if (i) write_string (" ", -1);
3341 tail = Fcdr (tail), i = 1) 3463 Fprin1 (backtrace_args (pdl)[i], Qnil);
3342 { 3464 }
3343 if (i) write_string (" ", -1); 3465 }
3344 Fprin1 (Fcar (tail), Qnil);
3345 }
3346 }
3347 else
3348 {
3349 ptrdiff_t i;
3350 for (i = 0; i < backlist->nargs; i++)
3351 {
3352 if (i) write_string (" ", -1);
3353 Fprin1 (backlist->args[i], Qnil);
3354 }
3355 }
3356 write_string (")\n", -1); 3466 write_string (")\n", -1);
3357 } 3467 }
3358 backlist = backlist->next; 3468 pdl = backtrace_next (pdl);
3359 } 3469 }
3360 3470
3361 Vprint_level = old_print_level; 3471 Vprint_level = old_print_level;
3362 UNGCPRO;
3363 return Qnil; 3472 return Qnil;
3364} 3473}
3365 3474
3366DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 1, NULL, 3475static union specbinding *
3476get_backtrace_frame (Lisp_Object nframes, Lisp_Object base)
3477{
3478 union specbinding *pdl = backtrace_top ();
3479 register EMACS_INT i;
3480
3481 CHECK_NATNUM (nframes);
3482
3483 if (!NILP (base))
3484 { /* Skip up to `base'. */
3485 base = Findirect_function (base, Qt);
3486 while (backtrace_p (pdl)
3487 && !EQ (base, Findirect_function (backtrace_function (pdl), Qt)))
3488 pdl = backtrace_next (pdl);
3489 }
3490
3491 /* Find the frame requested. */
3492 for (i = XFASTINT (nframes); i > 0 && backtrace_p (pdl); i--)
3493 pdl = backtrace_next (pdl);
3494
3495 return pdl;
3496}
3497
3498DEFUN ("backtrace-frame", Fbacktrace_frame, Sbacktrace_frame, 1, 2, NULL,
3367 doc: /* Return the function and arguments NFRAMES up from current execution point. 3499 doc: /* Return the function and arguments NFRAMES up from current execution point.
3368If that frame has not evaluated the arguments yet (or is a special form), 3500If that frame has not evaluated the arguments yet (or is a special form),
3369the value is (nil FUNCTION ARG-FORMS...). 3501the value is (nil FUNCTION ARG-FORMS...).
@@ -3372,56 +3504,188 @@ the value is (t FUNCTION ARG-VALUES...).
3372A &rest arg is represented as the tail of the list ARG-VALUES. 3504A &rest arg is represented as the tail of the list ARG-VALUES.
3373FUNCTION is whatever was supplied as car of evaluated list, 3505FUNCTION is whatever was supplied as car of evaluated list,
3374or a lambda expression for macro calls. 3506or a lambda expression for macro calls.
3375If NFRAMES is more than the number of frames, the value is nil. */) 3507If NFRAMES is more than the number of frames, the value is nil.
3376 (Lisp_Object nframes) 3508If BASE is non-nil, it should be a function and NFRAMES counts from its
3509nearest activation frame. */)
3510 (Lisp_Object nframes, Lisp_Object base)
3377{ 3511{
3378 register struct backtrace *backlist = backtrace_list; 3512 union specbinding *pdl = get_backtrace_frame (nframes, base);
3379 register EMACS_INT i;
3380 Lisp_Object tem;
3381
3382 CHECK_NATNUM (nframes);
3383
3384 /* Find the frame requested. */
3385 for (i = 0; backlist && i < XFASTINT (nframes); i++)
3386 backlist = backlist->next;
3387 3513
3388 if (!backlist) 3514 if (!backtrace_p (pdl))
3389 return Qnil; 3515 return Qnil;
3390 if (backlist->nargs == UNEVALLED) 3516 if (backtrace_nargs (pdl) == UNEVALLED)
3391 return Fcons (Qnil, Fcons (*backlist->function, *backlist->args)); 3517 return Fcons (Qnil,
3518 Fcons (backtrace_function (pdl), *backtrace_args (pdl)));
3392 else 3519 else
3393 { 3520 {
3394 if (backlist->nargs == MANY) /* FIXME: Can this happen? */ 3521 Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
3395 tem = *backlist->args; 3522
3396 else 3523 return Fcons (Qt, Fcons (backtrace_function (pdl), tem));
3397 tem = Flist (backlist->nargs, backlist->args); 3524 }
3525}
3526
3527/* For backtrace-eval, we want to temporarily unwind the last few elements of
3528 the specpdl stack, and then rewind them. We store the pre-unwind values
3529 directly in the pre-existing specpdl elements (i.e. we swap the current
3530 value and the old value stored in the specpdl), kind of like the inplace
3531 pointer-reversal trick. As it turns out, the rewind does the same as the
3532 unwind, except it starts from the other end of the specpdl stack, so we use
3533 the same function for both unwind and rewind. */
3534static void
3535backtrace_eval_unrewind (int distance)
3536{
3537 union specbinding *tmp = specpdl_ptr;
3538 int step = -1;
3539 if (distance < 0)
3540 { /* It's a rewind rather than unwind. */
3541 tmp += distance - 1;
3542 step = 1;
3543 distance = -distance;
3544 }
3398 3545
3399 return Fcons (Qt, Fcons (*backlist->function, tem)); 3546 for (; distance > 0; distance--)
3547 {
3548 tmp += step;
3549 /* */
3550 switch (tmp->kind)
3551 {
3552 /* FIXME: Ideally we'd like to "temporarily unwind" (some of) those
3553 unwind_protect, but the problem is that we don't know how to
3554 rewind them afterwards. */
3555 case SPECPDL_UNWIND:
3556 case SPECPDL_UNWIND_PTR:
3557 case SPECPDL_UNWIND_INT:
3558 case SPECPDL_UNWIND_VOID:
3559 case SPECPDL_BACKTRACE:
3560 break;
3561 case SPECPDL_LET:
3562 { /* If variable has a trivial value (no forwarding), we can
3563 just set it. No need to check for constant symbols here,
3564 since that was already done by specbind. */
3565 struct Lisp_Symbol *sym = XSYMBOL (specpdl_symbol (tmp));
3566 if (sym->redirect == SYMBOL_PLAINVAL)
3567 {
3568 Lisp_Object old_value = specpdl_old_value (tmp);
3569 set_specpdl_old_value (tmp, SYMBOL_VAL (sym));
3570 SET_SYMBOL_VAL (sym, old_value);
3571 break;
3572 }
3573 else
3574 { /* FALLTHROUGH!!
3575 NOTE: we only ever come here if make_local_foo was used for
3576 the first time on this var within this let. */
3577 }
3578 }
3579 case SPECPDL_LET_DEFAULT:
3580 {
3581 Lisp_Object sym = specpdl_symbol (tmp);
3582 Lisp_Object old_value = specpdl_old_value (tmp);
3583 set_specpdl_old_value (tmp, Fdefault_value (sym));
3584 Fset_default (sym, old_value);
3585 }
3586 break;
3587 case SPECPDL_LET_LOCAL:
3588 {
3589 Lisp_Object symbol = specpdl_symbol (tmp);
3590 Lisp_Object where = specpdl_where (tmp);
3591 Lisp_Object old_value = specpdl_old_value (tmp);
3592 eassert (BUFFERP (where));
3593
3594 /* If this was a local binding, reset the value in the appropriate
3595 buffer, but only if that buffer's binding still exists. */
3596 if (!NILP (Flocal_variable_p (symbol, where)))
3597 {
3598 set_specpdl_old_value
3599 (tmp, Fbuffer_local_value (symbol, where));
3600 set_internal (symbol, old_value, where, 1);
3601 }
3602 }
3603 break;
3604 }
3400 } 3605 }
3401} 3606}
3402 3607
3608DEFUN ("backtrace-eval", Fbacktrace_eval, Sbacktrace_eval, 2, 3, NULL,
3609 doc: /* Evaluate EXP in the context of some activation frame.
3610NFRAMES and BASE specify the activation frame to use, as in `backtrace-frame'. */)
3611 (Lisp_Object exp, Lisp_Object nframes, Lisp_Object base)
3612{
3613 union specbinding *pdl = get_backtrace_frame (nframes, base);
3614 ptrdiff_t count = SPECPDL_INDEX ();
3615 ptrdiff_t distance = specpdl_ptr - pdl;
3616 eassert (distance >= 0);
3617
3618 if (!backtrace_p (pdl))
3619 error ("Activation frame not found!");
3620
3621 backtrace_eval_unrewind (distance);
3622 record_unwind_protect_int (backtrace_eval_unrewind, -distance);
3623
3624 /* Use eval_sub rather than Feval since the main motivation behind
3625 backtrace-eval is to be able to get/set the value of lexical variables
3626 from the debugger. */
3627 return unbind_to (count, eval_sub (exp));
3628}
3403 3629
3404#if BYTE_MARK_STACK
3405void 3630void
3406mark_backtrace (void) 3631mark_specpdl (void)
3407{ 3632{
3408 register struct backtrace *backlist; 3633 union specbinding *pdl;
3409 ptrdiff_t i; 3634 for (pdl = specpdl; pdl != specpdl_ptr; pdl++)
3410
3411 for (backlist = backtrace_list; backlist; backlist = backlist->next)
3412 { 3635 {
3413 mark_object (*backlist->function); 3636 switch (pdl->kind)
3637 {
3638 case SPECPDL_UNWIND:
3639 mark_object (specpdl_arg (pdl));
3640 break;
3414 3641
3415 if (backlist->nargs == UNEVALLED 3642 case SPECPDL_BACKTRACE:
3416 || backlist->nargs == MANY) /* FIXME: Can this happen? */ 3643 {
3417 i = 1; 3644 ptrdiff_t nargs = backtrace_nargs (pdl);
3645 mark_object (backtrace_function (pdl));
3646 if (nargs == UNEVALLED)
3647 nargs = 1;
3648 while (nargs--)
3649 mark_object (backtrace_args (pdl)[nargs]);
3650 }
3651 break;
3652
3653 case SPECPDL_LET_DEFAULT:
3654 case SPECPDL_LET_LOCAL:
3655 mark_object (specpdl_where (pdl));
3656 /* Fall through. */
3657 case SPECPDL_LET:
3658 mark_object (specpdl_symbol (pdl));
3659 mark_object (specpdl_old_value (pdl));
3660 break;
3661 }
3662 }
3663}
3664
3665void
3666get_backtrace (Lisp_Object array)
3667{
3668 union specbinding *pdl = backtrace_next (backtrace_top ());
3669 ptrdiff_t i = 0, asize = ASIZE (array);
3670
3671 /* Copy the backtrace contents into working memory. */
3672 for (; i < asize; i++)
3673 {
3674 if (backtrace_p (pdl))
3675 {
3676 ASET (array, i, backtrace_function (pdl));
3677 pdl = backtrace_next (pdl);
3678 }
3418 else 3679 else
3419 i = backlist->nargs; 3680 ASET (array, i, Qnil);
3420 while (i--)
3421 mark_object (backlist->args[i]);
3422 } 3681 }
3423} 3682}
3424#endif 3683
3684Lisp_Object backtrace_top_function (void)
3685{
3686 union specbinding *pdl = backtrace_top ();
3687 return (backtrace_p (pdl) ? backtrace_function (pdl) : Qnil);
3688}
3425 3689
3426void 3690void
3427syms_of_eval (void) 3691syms_of_eval (void)
@@ -3462,7 +3726,7 @@ before making `inhibit-quit' nil. */);
3462 3726
3463 DEFSYM (Qinhibit_quit, "inhibit-quit"); 3727 DEFSYM (Qinhibit_quit, "inhibit-quit");
3464 DEFSYM (Qautoload, "autoload"); 3728 DEFSYM (Qautoload, "autoload");
3465 DEFSYM (Qdebug_on_error, "debug-on-error"); 3729 DEFSYM (Qinhibit_debugger, "inhibit-debugger");
3466 DEFSYM (Qmacro, "macro"); 3730 DEFSYM (Qmacro, "macro");
3467 DEFSYM (Qdeclare, "declare"); 3731 DEFSYM (Qdeclare, "declare");
3468 3732
@@ -3477,6 +3741,12 @@ before making `inhibit-quit' nil. */);
3477 DEFSYM (Qclosure, "closure"); 3741 DEFSYM (Qclosure, "closure");
3478 DEFSYM (Qdebug, "debug"); 3742 DEFSYM (Qdebug, "debug");
3479 3743
3744 DEFVAR_LISP ("inhibit-debugger", Vinhibit_debugger,
3745 doc: /* Non-nil means never enter the debugger.
3746Normally set while the debugger is already active, to avoid recursive
3747invocations. */);
3748 Vinhibit_debugger = Qnil;
3749
3480 DEFVAR_LISP ("debug-on-error", Vdebug_on_error, 3750 DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
3481 doc: /* Non-nil means enter debugger if an error is signaled. 3751 doc: /* Non-nil means enter debugger if an error is signaled.
3482Does not apply to errors handled by `condition-case' or those 3752Does not apply to errors handled by `condition-case' or those
@@ -3486,7 +3756,7 @@ if one of its condition symbols appears in the list.
3486When you evaluate an expression interactively, this variable 3756When you evaluate an expression interactively, this variable
3487is temporarily non-nil if `eval-expression-debug-on-error' is non-nil. 3757is temporarily non-nil if `eval-expression-debug-on-error' is non-nil.
3488The command `toggle-debug-on-error' toggles this. 3758The command `toggle-debug-on-error' toggles this.
3489See also the variable `debug-on-quit'. */); 3759See also the variable `debug-on-quit' and `inhibit-debugger'. */);
3490 Vdebug_on_error = Qnil; 3760 Vdebug_on_error = Qnil;
3491 3761
3492 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors, 3762 DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
@@ -3573,9 +3843,12 @@ alist of active lexical bindings. */);
3573 defsubr (&Ssetq); 3843 defsubr (&Ssetq);
3574 defsubr (&Squote); 3844 defsubr (&Squote);
3575 defsubr (&Sfunction); 3845 defsubr (&Sfunction);
3846 defsubr (&Sdefault_toplevel_value);
3847 defsubr (&Sset_default_toplevel_value);
3576 defsubr (&Sdefvar); 3848 defsubr (&Sdefvar);
3577 defsubr (&Sdefvaralias); 3849 defsubr (&Sdefvaralias);
3578 defsubr (&Sdefconst); 3850 defsubr (&Sdefconst);
3851 defsubr (&Smake_var_non_special);
3579 defsubr (&Slet); 3852 defsubr (&Slet);
3580 defsubr (&SletX); 3853 defsubr (&SletX);
3581 defsubr (&Swhile); 3854 defsubr (&Swhile);
@@ -3585,10 +3858,9 @@ alist of active lexical bindings. */);
3585 defsubr (&Sunwind_protect); 3858 defsubr (&Sunwind_protect);
3586 defsubr (&Scondition_case); 3859 defsubr (&Scondition_case);
3587 defsubr (&Ssignal); 3860 defsubr (&Ssignal);
3588 defsubr (&Sinteractive_p);
3589 defsubr (&Scalled_interactively_p);
3590 defsubr (&Scommandp); 3861 defsubr (&Scommandp);
3591 defsubr (&Sautoload); 3862 defsubr (&Sautoload);
3863 defsubr (&Sautoload_do_load);
3592 defsubr (&Seval); 3864 defsubr (&Seval);
3593 defsubr (&Sapply); 3865 defsubr (&Sapply);
3594 defsubr (&Sfuncall); 3866 defsubr (&Sfuncall);
@@ -3601,6 +3873,7 @@ alist of active lexical bindings. */);
3601 defsubr (&Sbacktrace_debug); 3873 defsubr (&Sbacktrace_debug);
3602 defsubr (&Sbacktrace); 3874 defsubr (&Sbacktrace);
3603 defsubr (&Sbacktrace_frame); 3875 defsubr (&Sbacktrace_frame);
3876 defsubr (&Sbacktrace_eval);
3604 defsubr (&Sspecial_variable_p); 3877 defsubr (&Sspecial_variable_p);
3605 defsubr (&Sfunctionp); 3878 defsubr (&Sfunctionp);
3606} 3879}