Get rid of funvec.
[bpt/emacs.git] / src / eval.c
index 89d353c..869d70e 100644 (file)
@@ -1,7 +1,5 @@
 /* Evaluator for GNU Emacs Lisp interpreter.
-   Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001,
-                2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
-                Free Software Foundation, Inc.
+   Copyright (C) 1985-1987, 1993-1995, 1999-2011  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -58,10 +56,12 @@ int gcpro_level;
 #endif
 
 Lisp_Object Qautoload, Qmacro, Qexit, Qinteractive, Qcommandp, Qdefun;
-Lisp_Object Qinhibit_quit, Vinhibit_quit, Vquit_flag;
+Lisp_Object Qinhibit_quit;
 Lisp_Object Qand_rest, Qand_optional;
 Lisp_Object Qdebug_on_error;
 Lisp_Object Qdeclare;
+Lisp_Object Qinternal_interpreter_environment, Qclosure;
+
 Lisp_Object Qdebug;
 
 /* This holds either the symbol `run-hooks' or nil.
@@ -79,7 +79,7 @@ Lisp_Object Vautoload_queue;
 
 /* Current number of specbindings allocated in specpdl.  */
 
-int specpdl_size;
+EMACS_INT specpdl_size;
 
 /* Pointer to beginning of specpdl.  */
 
@@ -89,55 +89,9 @@ struct specbinding *specpdl;
 
 struct specbinding *specpdl_ptr;
 
-/* Maximum size allowed for specpdl allocation */
-
-EMACS_INT max_specpdl_size;
-
 /* Depth in Lisp evaluations and function calls.  */
 
-int lisp_eval_depth;
-
-/* Maximum allowed depth in Lisp evaluations and function calls.  */
-
-EMACS_INT max_lisp_eval_depth;
-
-/* Nonzero means enter debugger before next function call */
-
-int debug_on_next_call;
-
-/* Non-zero means debugger may continue.  This is zero when the
-   debugger is called during redisplay, where it might not be safe to
-   continue the interrupted redisplay. */
-
-int debugger_may_continue;
-
-/* List of conditions (non-nil atom means all) which cause a backtrace
-   if an error is handled by the command loop's error handler.  */
-
-Lisp_Object Vstack_trace_on_error;
-
-/* List of conditions (non-nil atom means all) which enter the debugger
-   if an error is handled by the command loop's error handler.  */
-
-Lisp_Object Vdebug_on_error;
-
-/* List of conditions and regexps specifying error messages which
-   do not enter the debugger even if Vdebug_on_error says they should.  */
-
-Lisp_Object Vdebug_ignored_errors;
-
-/* Non-nil means call the debugger even if the error will be handled.  */
-
-Lisp_Object Vdebug_on_signal;
-
-/* Hook for edebug to use.  */
-
-Lisp_Object Vsignal_hook_function;
-
-/* Nonzero means enter debugger if a quit signal
-   is handled by the command loop's error handler. */
-
-int debug_on_quit;
+EMACS_INT lisp_eval_depth;
 
 /* The value of num_nonmacro_input_events as of the last time we
    started to enter the debugger.  If we decide to enter the debugger
@@ -148,8 +102,6 @@ int debug_on_quit;
 
 int when_entered_debugger;
 
-Lisp_Object Vdebugger;
-
 /* The function from which the last `signal' was called.  Set in
    Fsignal.  */
 
@@ -161,12 +113,10 @@ Lisp_Object Vsignaling_function;
 
 int handling_signal;
 
-/* Function to process declarations in defmacro forms.  */
-
-Lisp_Object Vmacro_declaration_function;
-
-static Lisp_Object funcall_lambda (Lisp_Object, int, Lisp_Object*);
+static Lisp_Object apply_lambda (Lisp_Object fun, Lisp_Object args);
+static Lisp_Object funcall_lambda (Lisp_Object, int, Lisp_Object *);
 static void unwind_to_catch (struct catchtag *, Lisp_Object) NO_RETURN;
+static int interactive_p (int);
 \f
 void
 init_eval_once (void)
@@ -175,8 +125,8 @@ init_eval_once (void)
   specpdl = (struct specbinding *) xmalloc (specpdl_size * sizeof (struct specbinding));
   specpdl_ptr = specpdl;
   /* Don't forget to update docs (lispref node "Local Variables").  */
-  max_specpdl_size = 1000;
-  max_lisp_eval_depth = 500;
+  max_specpdl_size = 1300; /* 1000 is not enough for CEDET's c-by.el.  */
+  max_lisp_eval_depth = 600;
 
   Vrun_hooks = Qnil;
 }
@@ -216,7 +166,7 @@ call_debugger (Lisp_Object arg)
   int debug_while_redisplaying;
   int count = SPECPDL_INDEX ();
   Lisp_Object val;
-  int old_max = max_specpdl_size;
+  EMACS_INT old_max = max_specpdl_size;
 
   /* Temporarily bump up the stack limits,
      so the debugger won't run out of stack.  */
@@ -292,7 +242,7 @@ usage: (or CONDITIONS...)  */)
 
   while (CONSP (args))
     {
-      val = Feval (XCAR (args));
+      val = eval_sub (XCAR (args));
       if (!NILP (val))
        break;
       args = XCDR (args);
@@ -316,7 +266,7 @@ usage: (and CONDITIONS...)  */)
 
   while (CONSP (args))
     {
-      val = Feval (XCAR (args));
+      val = eval_sub (XCAR (args));
       if (NILP (val))
        break;
       args = XCDR (args);
@@ -338,11 +288,11 @@ usage: (if COND THEN ELSE...)  */)
   struct gcpro gcpro1;
 
   GCPRO1 (args);
-  cond = Feval (Fcar (args));
+  cond = eval_sub (Fcar (args));
   UNGCPRO;
 
   if (!NILP (cond))
-    return Feval (Fcar (Fcdr (args)));
+    return eval_sub (Fcar (Fcdr (args)));
   return Fprogn (Fcdr (Fcdr (args)));
 }
 
@@ -366,7 +316,7 @@ usage: (cond CLAUSES...)  */)
   while (!NILP (args))
     {
       clause = Fcar (args);
-      val = Feval (Fcar (clause));
+      val = eval_sub (Fcar (clause));
       if (!NILP (val))
        {
          if (!EQ (XCDR (clause), Qnil))
@@ -392,7 +342,7 @@ usage: (progn BODY...)  */)
 
   while (CONSP (args))
     {
-      val = Feval (XCAR (args));
+      val = eval_sub (XCAR (args));
       args = XCDR (args);
     }
 
@@ -422,9 +372,9 @@ usage: (prog1 FIRST BODY...)  */)
   do
     {
       if (!(argnum++))
-       val = Feval (Fcar (args_left));
+       val = eval_sub (Fcar (args_left));
       else
-       Feval (Fcar (args_left));
+       eval_sub (Fcar (args_left));
       args_left = Fcdr (args_left);
     }
   while (!NILP(args_left));
@@ -457,9 +407,9 @@ usage: (prog2 FORM1 FORM2 BODY...)  */)
   do
     {
       if (!(argnum++))
-       val = Feval (Fcar (args_left));
+       val = eval_sub (Fcar (args_left));
       else
-       Feval (Fcar (args_left));
+       eval_sub (Fcar (args_left));
       args_left = Fcdr (args_left);
     }
   while (!NILP (args_left));
@@ -480,7 +430,7 @@ usage: (setq [SYM VAL]...)  */)
   (Lisp_Object args)
 {
   register Lisp_Object args_left;
-  register Lisp_Object val, sym;
+  register Lisp_Object val, sym, lex_binding;
   struct gcpro gcpro1;
 
   if (NILP (args))
@@ -491,9 +441,19 @@ usage: (setq [SYM VAL]...)  */)
 
   do
     {
-      val = Feval (Fcar (Fcdr (args_left)));
+      val = eval_sub (Fcar (Fcdr (args_left)));
       sym = Fcar (args_left);
-      Fset (sym, val);
+
+      /* Like for eval_sub, we do not check declared_special here since
+        it's been done when let-binding.  */
+      if (!NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
+         && SYMBOLP (sym)
+         && !NILP (lex_binding
+                   = Fassq (sym, Vinternal_interpreter_environment)))
+       XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
+      else
+       Fset (sym, val);        /* SYM is dynamically bound.  */
+
       args_left = Fcdr (Fcdr (args_left));
     }
   while (!NILP(args_left));
@@ -519,9 +479,20 @@ In byte compilation, `function' causes its argument to be compiled.
 usage: (function ARG)  */)
   (Lisp_Object args)
 {
+  Lisp_Object quoted = XCAR (args);
+
   if (!NILP (Fcdr (args)))
     xsignal2 (Qwrong_number_of_arguments, Qfunction, Flength (args));
-  return Fcar (args);
+
+  if (!NILP (Vinternal_interpreter_environment)
+      && CONSP (quoted)
+      && EQ (XCAR (quoted), Qlambda))
+    /* This is a lambda expression within a lexical environment;
+       return an interpreted closure instead of a simple lambda.  */
+    return Fcons (Qclosure, Fcons (Vinternal_interpreter_environment, quoted));
+  else
+    /* Simply quote the argument.  */
+    return quoted;
 }
 
 
@@ -544,7 +515,7 @@ spec that specifies non-nil unconditionally (such as \"p\"); or (ii)
 use `called-interactively-p'.  */)
   (void)
 {
-  return (INTERACTIVE && interactive_p (1)) ? Qt : Qnil;
+  return interactive_p (1) ? Qt : Qnil;
 }
 
 
@@ -581,7 +552,7 @@ way to do this), or via (not (or executing-kbd-macro noninteractive)).  */)
     EXCLUDE_SUBRS_P non-zero means always return 0 if the function
     called is a built-in.  */
 
-int
+static int
 interactive_p (int exclude_subrs_p)
 {
   struct backtrace *btp;
@@ -637,6 +608,8 @@ usage: (defun NAME ARGLIST [DOCSTRING] BODY...)  */)
   fn_name = Fcar (args);
   CHECK_SYMBOL (fn_name);
   defn = Fcons (Qlambda, Fcdr (args));
+  if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization!  */
+    defn = Ffunction (Fcons (defn, Qnil));
   if (!NILP (Vpurify_flag))
     defn = Fpurecopy (defn);
   if (CONSP (XSYMBOL (fn_name)->function)
@@ -708,7 +681,11 @@ usage: (defmacro NAME ARGLIST [DOCSTRING] [DECL] BODY...)  */)
     tail = Fcons (lambda_list, tail);
   else
     tail = Fcons (lambda_list, Fcons (doc, tail));
-  defn = Fcons (Qmacro, Fcons (Qlambda, tail));
+  
+  defn = Fcons (Qlambda, tail);
+  if (!NILP (Vinternal_interpreter_environment)) /* Mere optimization!  */
+    defn = Ffunction (Fcons (defn, Qnil));
+  defn = Fcons (Qmacro, defn);
 
   if (!NILP (Vpurify_flag))
     defn = Fpurecopy (defn);
@@ -768,6 +745,7 @@ The return value is BASE-VARIABLE.  */)
        error ("Don't know how to make a let-bound variable an alias");
   }
 
+  sym->declared_special = 1;
   sym->redirect = SYMBOL_VARALIAS;
   SET_SYMBOL_ALIAS (sym, XSYMBOL (base_variable));
   sym->constant = SYMBOL_CONSTANT_P (base_variable);
@@ -813,6 +791,10 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
   tem = Fdefault_boundp (sym);
   if (!NILP (tail))
     {
+      if (SYMBOLP (sym))
+       /* Do it before evaluating the initial value, for self-references.  */
+       XSYMBOL (sym)->declared_special = 1;
+
       if (SYMBOL_CONSTANT_P (sym))
        {
          /* For upward compatibility, allow (defvar :foo (quote :foo)).  */
@@ -826,7 +808,7 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
        }
 
       if (NILP (tem))
-       Fset_default (sym, Feval (Fcar (tail)));
+       Fset_default (sym, eval_sub (Fcar (tail)));
       else
        { /* Check if there is really a global binding rather than just a let
             binding that shadows the global unboundness of the var.  */
@@ -852,12 +834,19 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
        }
       LOADHIST_ATTACH (sym);
     }
+  else if (!NILP (Vinternal_interpreter_environment)
+          && !XSYMBOL (sym)->declared_special)
+    /* A simple (defvar foo) with lexical scoping does "nothing" except
+       declare that var to be dynamically scoped *locally* (i.e. within
+       the current file or let-block).  */
+    Vinternal_interpreter_environment =
+      Fcons (sym, Vinternal_interpreter_environment);
   else
     /* Simple (defvar <var>) should not count as a definition at all.
        It could get in the way of other definitions, and unloading this
        package could try to make the variable unbound.  */
     ;
-
+      
   return sym;
 }
 
@@ -881,10 +870,11 @@ usage: (defconst SYMBOL INITVALUE [DOCSTRING])  */)
   if (!NILP (Fcdr (Fcdr (Fcdr (args)))))
     error ("Too many arguments");
 
-  tem = Feval (Fcar (Fcdr (args)));
+  tem = eval_sub (Fcar (Fcdr (args)));
   if (!NILP (Vpurify_flag))
     tem = Fpurecopy (tem);
   Fset_default (sym, tem);
+  XSYMBOL (sym)->declared_special = 1;
   tem = Fcar (Fcdr (Fcdr (args)));
   if (!NILP (tem))
     {
@@ -970,30 +960,59 @@ Each VALUEFORM can refer to the symbols already bound by this VARLIST.
 usage: (let* VARLIST BODY...)  */)
   (Lisp_Object args)
 {
-  Lisp_Object varlist, val, elt;
+  Lisp_Object varlist, var, val, elt, lexenv;
   int count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3;
 
   GCPRO3 (args, elt, varlist);
 
+  lexenv = Vinternal_interpreter_environment;
+
   varlist = Fcar (args);
-  while (!NILP (varlist))
+  while (CONSP (varlist))
     {
       QUIT;
-      elt = Fcar (varlist);
+
+      elt = XCAR (varlist);
       if (SYMBOLP (elt))
-       specbind (elt, Qnil);
+       {
+         var = elt;
+         val = Qnil;
+       }
       else if (! NILP (Fcdr (Fcdr (elt))))
        signal_error ("`let' bindings can have only one value-form", elt);
       else
        {
-         val = Feval (Fcar (Fcdr (elt)));
-         specbind (Fcar (elt), val);
+         var = Fcar (elt);
+         val = eval_sub (Fcar (Fcdr (elt)));
+       }
+
+      if (!NILP (lexenv) && SYMBOLP (var)
+         && !XSYMBOL (var)->declared_special
+         && NILP (Fmemq (var, Vinternal_interpreter_environment)))
+       /* Lexically bind VAR by adding it to the interpreter's binding
+          alist.  */
+       {
+         Lisp_Object newenv
+           = Fcons (Fcons (var, val), Vinternal_interpreter_environment);
+         if (EQ (Vinternal_interpreter_environment, lexenv))
+           /* Save the old lexical environment on the specpdl stack,
+              but only for the first lexical binding, since we'll never
+              need to revert to one of the intermediate ones.  */
+           specbind (Qinternal_interpreter_environment, newenv);
+         else
+           Vinternal_interpreter_environment = newenv;
        }
-      varlist = Fcdr (varlist);
+      else
+       specbind (var, val);
+
+      varlist = XCDR (varlist);
     }
+
   UNGCPRO;
+
   val = Fprogn (Fcdr (args));
+
   return unbind_to (count, val);
 }
 
@@ -1006,7 +1025,7 @@ All the VALUEFORMs are evalled before any symbols are bound.
 usage: (let VARLIST BODY...)  */)
   (Lisp_Object args)
 {
-  Lisp_Object *temps, tem;
+  Lisp_Object *temps, tem, lexenv;
   register Lisp_Object elt, varlist;
   int count = SPECPDL_INDEX ();
   register int argnum;
@@ -1033,22 +1052,36 @@ usage: (let VARLIST BODY...)  */)
       else if (! NILP (Fcdr (Fcdr (elt))))
        signal_error ("`let' bindings can have only one value-form", elt);
       else
-       temps [argnum++] = Feval (Fcar (Fcdr (elt)));
+       temps [argnum++] = eval_sub (Fcar (Fcdr (elt)));
       gcpro2.nvars = argnum;
     }
   UNGCPRO;
 
+  lexenv = Vinternal_interpreter_environment;
+
   varlist = Fcar (args);
   for (argnum = 0; CONSP (varlist); varlist = XCDR (varlist))
     {
+      Lisp_Object var;
+
       elt = XCAR (varlist);
+      var = SYMBOLP (elt) ? elt : Fcar (elt);
       tem = temps[argnum++];
-      if (SYMBOLP (elt))
-       specbind (elt, tem);
+
+      if (!NILP (lexenv) && SYMBOLP (var)
+         && !XSYMBOL (var)->declared_special
+         && NILP (Fmemq (var, Vinternal_interpreter_environment)))
+       /* Lexically bind VAR by adding it to the lexenv alist.  */
+       lexenv = Fcons (Fcons (var, tem), lexenv);
       else
-       specbind (Fcar (elt), tem);
+       /* Dynamically bind VAR.  */
+       specbind (var, tem);
     }
 
+  if (!EQ (lexenv, Vinternal_interpreter_environment))
+    /* Instantiate a new lexical environment.  */
+    specbind (Qinternal_interpreter_environment, lexenv);
+
   elt = Fprogn (Fcdr (args));
   SAFE_FREE ();
   return unbind_to (count, elt);
@@ -1068,7 +1101,7 @@ usage: (while TEST BODY...)  */)
 
   test = Fcar (args);
   body = Fcdr (args);
-  while (!NILP (Feval (test)))
+  while (!NILP (eval_sub (test)))
     {
       QUIT;
       Fprogn (body);
@@ -1170,7 +1203,7 @@ usage: (catch TAG BODY...)  */)
   struct gcpro gcpro1;
 
   GCPRO1 (args);
-  tag = Feval (Fcar (args));
+  tag = eval_sub (Fcar (args));
   UNGCPRO;
   return internal_catch (tag, Fprogn, Fcdr (args));
 }
@@ -1303,7 +1336,7 @@ usage: (unwind-protect BODYFORM UNWINDFORMS...)  */)
   int count = SPECPDL_INDEX ();
 
   record_unwind_protect (Fprogn, Fcdr (args));
-  val = Feval (Fcar (args));
+  val = eval_sub (Fcar (args));
   return unbind_to (count, val);
 }
 \f
@@ -1404,7 +1437,7 @@ internal_lisp_condition_case (volatile Lisp_Object var, Lisp_Object bodyform,
   h.tag = &c;
   handlerlist = &h;
 
-  val = Feval (bodyform);
+  val = eval_sub (bodyform);
   catchlist = c.next;
   handlerlist = h.next;
   return val;
@@ -1609,6 +1642,8 @@ internal_condition_case_n (Lisp_Object (*bfun) (int, Lisp_Object*),
 \f
 static Lisp_Object find_handler_clause (Lisp_Object, Lisp_Object,
                                        Lisp_Object, Lisp_Object);
+static int maybe_call_debugger (Lisp_Object conditions, Lisp_Object sig,
+                               Lisp_Object data);
 
 DEFUN ("signal", Fsignal, Ssignal, 2, 2, 0,
        doc: /* Signal an error.  Args are ERROR-SYMBOL and associated DATA.
@@ -1629,10 +1664,12 @@ See also the function `condition-case'.  */)
   /* When memory is full, ERROR-SYMBOL is nil,
      and DATA is (REAL-ERROR-SYMBOL . REAL-DATA).
      That is a special case--don't do this in other situations.  */
-  register struct handler *allhandlers = handlerlist;
   Lisp_Object conditions;
   Lisp_Object string;
-  Lisp_Object real_error_symbol;
+  Lisp_Object real_error_symbol
+    = (NILP (error_symbol) ? Fcar (data) : error_symbol);
+  register Lisp_Object clause = Qnil;
+  struct handler *h;
   struct backtrace *bp;
 
   immediate_quit = handling_signal = 0;
@@ -1640,11 +1677,6 @@ See also the function `condition-case'.  */)
   if (gc_in_progress || waiting_for_input)
     abort ();
 
-  if (NILP (error_symbol))
-    real_error_symbol = Fcar (data);
-  else
-    real_error_symbol = error_symbol;
-
 #if 0 /* rms: I don't know why this was here,
         but it is surely wrong for an error that is handled.  */
 #ifdef HAVE_WINDOW_SYSTEM
@@ -1683,49 +1715,49 @@ See also the function `condition-case'.  */)
        Vsignaling_function = *bp->function;
     }
 
-  for (; handlerlist; handlerlist = handlerlist->next)
+  for (h = handlerlist; h; h = h->next)
     {
-      register Lisp_Object clause;
-
-      clause = find_handler_clause (handlerlist->handler, conditions,
+      clause = find_handler_clause (h->handler, conditions,
                                    error_symbol, data);
-
-      if (EQ (clause, Qlambda))
-       {
-         /* We can't return values to code which signaled an error, but we
-            can continue code which has signaled a quit.  */
-         if (EQ (real_error_symbol, Qquit))
-           return Qnil;
-         else
-           error ("Cannot return from the debugger in an error");
-       }
-
       if (!NILP (clause))
-       {
-         Lisp_Object unwind_data;
-         struct handler *h = handlerlist;
-
-         handlerlist = allhandlers;
-
-         if (NILP (error_symbol))
-           unwind_data = data;
-         else
-           unwind_data = Fcons (error_symbol, data);
-         h->chosen_clause = clause;
-         unwind_to_catch (h->tag, unwind_data);
-       }
+       break;
     }
+         
+  if (/* Don't run the debugger for a memory-full error.
+        (There is no room in memory to do that!) */
+      !NILP (error_symbol)
+      && (!NILP (Vdebug_on_signal)
+         /* If no handler is present now, try to run the debugger.  */
+         || NILP (clause)
+         /* Special handler that means "print a message and run debugger
+            if requested".  */
+         || EQ (h->handler, Qerror)))
+    {
+      int debugger_called
+       = maybe_call_debugger (conditions, error_symbol, data);
+      /* We can't return values to code which signaled an error, but we
+        can continue code which has signaled a quit.  */
+      if (debugger_called && EQ (real_error_symbol, Qquit))
+       return Qnil;
+    }      
 
-  handlerlist = allhandlers;
-  /* If no handler is present now, try to run the debugger,
-     and if that fails, throw to top level.  */
-  find_handler_clause (Qerror, conditions, error_symbol, data);
-  if (catchlist != 0)
-    Fthrow (Qtop_level, Qt);
+  if (!NILP (clause))
+    {
+      Lisp_Object unwind_data
+       = (NILP (error_symbol) ? data : Fcons (error_symbol, data));
+      
+      h->chosen_clause = clause;
+      unwind_to_catch (h->tag, unwind_data);
+    }
+  else
+    {
+      if (catchlist != 0)
+       Fthrow (Qtop_level, Qt);
+    }
 
   if (! NILP (error_symbol))
     data = Fcons (error_symbol, data);
-
+      
   string = Ferror_message_string (data);
   fatal ("%s", SDATA (string), 0);
 }
@@ -1900,63 +1932,24 @@ find_handler_clause (Lisp_Object handlers, Lisp_Object conditions,
                     Lisp_Object sig, Lisp_Object data)
 {
   register Lisp_Object h;
-  register Lisp_Object tem;
-  int debugger_called = 0;
-  int debugger_considered = 0;
 
   /* t is used by handlers for all conditions, set up by C code.  */
   if (EQ (handlers, Qt))
     return Qt;
 
-  /* Don't run the debugger for a memory-full error.
-     (There is no room in memory to do that!)  */
-  if (NILP (sig))
-    debugger_considered = 1;
-
   /* error is used similarly, but means print an error message
      and run the debugger if that is enabled.  */
-  if (EQ (handlers, Qerror)
-      || !NILP (Vdebug_on_signal)) /* This says call debugger even if
-                                     there is a handler.  */
-    {
-      if (!NILP (sig) && wants_debugger (Vstack_trace_on_error, conditions))
-       {
-         max_lisp_eval_depth += 15;
-         max_specpdl_size++;
-         if (noninteractive)
-           Fbacktrace ();
-         else
-           internal_with_output_to_temp_buffer
-             ("*Backtrace*",
-              (Lisp_Object (*) (Lisp_Object)) Fbacktrace,
-              Qnil);
-         max_specpdl_size--;
-         max_lisp_eval_depth -= 15;
-       }
-
-      if (!debugger_considered)
-       {
-         debugger_considered = 1;
-         debugger_called = maybe_call_debugger (conditions, sig, data);
-       }
-
-      /* If there is no handler, return saying whether we ran the debugger.  */
-      if (EQ (handlers, Qerror))
-       {
-         if (debugger_called)
-           return Qlambda;
-         return Qt;
-       }
-    }
+  if (EQ (handlers, Qerror))
+    return Qt;
 
-  for (h = handlers; CONSP (h); h = Fcdr (h))
+  for (h = handlers; CONSP (h); h = XCDR (h))
     {
-      Lisp_Object handler, condit;
+      Lisp_Object handler = XCAR (h);
+      Lisp_Object condit, tem;
 
-      handler = Fcar (h);
       if (!CONSP (handler))
        continue;
-      condit = Fcar (handler);
+      condit = XCAR (handler);
       /* Handle a single condition name in handler HANDLER.  */
       if (SYMBOLP (condit))
        {
@@ -1970,15 +1963,9 @@ find_handler_clause (Lisp_Object handlers, Lisp_Object conditions,
          Lisp_Object tail;
          for (tail = condit; CONSP (tail); tail = XCDR (tail))
            {
-             tem = Fmemq (Fcar (tail), conditions);
+             tem = Fmemq (XCAR (tail), conditions);
              if (!NILP (tem))
-               {
-                 /* This handler is going to apply.
-                    Does it allow the debugger to run first?  */
-                 if (! debugger_considered && !NILP (Fmemq (Qdebug, condit)))
-                   maybe_call_debugger (conditions, sig, data);
-                 return handler;
-               }
+               return handler;
            }
        }
     }
@@ -1992,10 +1979,9 @@ void
 verror (const char *m, va_list ap)
 {
   char buf[200];
-  int size = 200;
+  EMACS_INT size = 200;
   int mlen;
   char *buffer = buf;
-  char *args[3];
   int allocated = 0;
   Lisp_Object string;
 
@@ -2003,7 +1989,7 @@ verror (const char *m, va_list ap)
 
   while (1)
     {
-      int used;
+      EMACS_INT used;
       used = doprnt (buffer, size, m, m + mlen, ap);
       if (used < size)
        break;
@@ -2095,9 +2081,11 @@ then strings and vectors are not accepted.  */)
   if (!CONSP (fun))
     return Qnil;
   funcar = XCAR (fun);
+  if (EQ (funcar, Qclosure))
+    fun = Fcdr (XCDR (fun)), funcar = Fcar (fun);
   if (EQ (funcar, Qlambda))
     return !NILP (Fassq (Qinteractive, Fcdr (XCDR (fun)))) ? Qt : if_prop;
-  if (EQ (funcar, Qautoload))
+  else if (EQ (funcar, Qautoload))
     return !NILP (Fcar (Fcdr (Fcdr (XCDR (fun))))) ? Qt : if_prop;
   else
     return Qnil;
@@ -2215,9 +2203,21 @@ do_autoload (Lisp_Object fundef, Lisp_Object funname)
 }
 
 \f
-DEFUN ("eval", Feval, Seval, 1, 1, 0,
-       doc: /* Evaluate FORM and return its value.  */)
-  (Lisp_Object form)
+DEFUN ("eval", Feval, Seval, 1, 2, 0,
+       doc: /* Evaluate FORM and return its value.
+If LEXICAL is t, evaluate using lexical scoping.  */)
+  (Lisp_Object form, Lisp_Object lexical)
+{
+  int count = SPECPDL_INDEX ();
+  specbind (Qinternal_interpreter_environment,
+           NILP (lexical) ? Qnil : Fcons (Qt, Qnil));
+  return unbind_to (count, eval_sub (form));
+}
+
+/* Eval a sub-expression of the current expression (i.e. in the same
+   lexical scope).  */
+Lisp_Object
+eval_sub (Lisp_Object form)
 {
   Lisp_Object fun, val, original_fun, original_args;
   Lisp_Object funcar;
@@ -2228,7 +2228,20 @@ DEFUN ("eval", Feval, Seval, 1, 1, 0,
     abort ();
 
   if (SYMBOLP (form))
-    return Fsymbol_value (form);
+    {
+      /* Look up its binding in the lexical environment.
+        We do not pay attention to the declared_special flag here, since we
+        already did that when let-binding the variable.  */
+      Lisp_Object lex_binding
+       = !NILP (Vinternal_interpreter_environment) /* Mere optimization!  */
+       ? Fassq (form, Vinternal_interpreter_environment)
+       : Qnil;
+      if (CONSP (lex_binding))
+       return XCDR (lex_binding);
+      else
+       return Fsymbol_value (form);
+    }
+
   if (!CONSP (form))
     return form;
 
@@ -2291,14 +2304,12 @@ DEFUN ("eval", Feval, Seval, 1, 1, 0,
          (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < XINT (numargs)))
        xsignal2 (Qwrong_number_of_arguments, original_fun, numargs);
 
-      if (XSUBR (fun)->max_args == UNEVALLED)
+      else if (XSUBR (fun)->max_args == UNEVALLED)
        {
          backtrace.evalargs = 0;
          val = (XSUBR (fun)->function.aUNEVALLED) (args_left);
-         goto done;
        }
-
-      if (XSUBR (fun)->max_args == MANY)
+      else if (XSUBR (fun)->max_args == MANY)
        {
          /* Pass a vector of evaluated arguments */
          Lisp_Object *vals;
@@ -2313,7 +2324,7 @@ DEFUN ("eval", Feval, Seval, 1, 1, 0,
 
          while (!NILP (args_left))
            {
-             vals[argnum++] = Feval (Fcar (args_left));
+             vals[argnum++] = eval_sub (Fcar (args_left));
              args_left = Fcdr (args_left);
              gcpro3.nvars = argnum;
            }
@@ -2324,74 +2335,77 @@ DEFUN ("eval", Feval, Seval, 1, 1, 0,
          val = (XSUBR (fun)->function.aMANY) (XINT (numargs), vals);
          UNGCPRO;
          SAFE_FREE ();
-         goto done;
        }
-
-      GCPRO3 (args_left, fun, fun);
-      gcpro3.var = argvals;
-      gcpro3.nvars = 0;
-
-      maxargs = XSUBR (fun)->max_args;
-      for (i = 0; i < maxargs; args_left = Fcdr (args_left))
+      else
        {
-         argvals[i] = Feval (Fcar (args_left));
-         gcpro3.nvars = ++i;
-       }
+         GCPRO3 (args_left, fun, fun);
+         gcpro3.var = argvals;
+         gcpro3.nvars = 0;
 
-      UNGCPRO;
+         maxargs = XSUBR (fun)->max_args;
+         for (i = 0; i < maxargs; args_left = Fcdr (args_left))
+           {
+             argvals[i] = eval_sub (Fcar (args_left));
+             gcpro3.nvars = ++i;
+           }
 
-      backtrace.args = argvals;
-      backtrace.nargs = XINT (numargs);
+         UNGCPRO;
 
-      switch (i)
-       {
-       case 0:
-         val = (XSUBR (fun)->function.a0) ();
-         goto done;
-       case 1:
-         val = (XSUBR (fun)->function.a1) (argvals[0]);
-         goto done;
-       case 2:
-         val = (XSUBR (fun)->function.a2) (argvals[0], argvals[1]);
-         goto done;
-       case 3:
-         val = (XSUBR (fun)->function.a3) (argvals[0], argvals[1],
-                                           argvals[2]);
-         goto done;
-       case 4:
-         val = (XSUBR (fun)->function.a4) (argvals[0], argvals[1],
-                                           argvals[2], argvals[3]);
-         goto done;
-       case 5:
-         val = (XSUBR (fun)->function.a5) (argvals[0], argvals[1], argvals[2],
-                                           argvals[3], argvals[4]);
-         goto done;
-       case 6:
-         val = (XSUBR (fun)->function.a6) (argvals[0], argvals[1], argvals[2],
-                                           argvals[3], argvals[4], argvals[5]);
-         goto done;
-       case 7:
-         val = (XSUBR (fun)->function.a7) (argvals[0], argvals[1], argvals[2],
-                                           argvals[3], argvals[4], argvals[5],
-                                           argvals[6]);
-         goto done;
-
-       case 8:
-         val = (XSUBR (fun)->function.a8) (argvals[0], argvals[1], argvals[2],
-                                           argvals[3], argvals[4], argvals[5],
-                                           argvals[6], argvals[7]);
-         goto done;
-
-       default:
-         /* Someone has created a subr that takes more arguments than
-            is supported by this code.  We need to either rewrite the
-            subr to use a different argument protocol, or add more
-            cases to this switch.  */
-         abort ();
+         backtrace.args = argvals;
+         backtrace.nargs = XINT (numargs);
+
+         switch (i)
+           {
+           case 0:
+             val = (XSUBR (fun)->function.a0 ());
+             break;
+           case 1:
+             val = (XSUBR (fun)->function.a1 (argvals[0]));
+             break;
+           case 2:
+             val = (XSUBR (fun)->function.a2 (argvals[0], argvals[1]));
+             break;
+           case 3:
+             val = (XSUBR (fun)->function.a3
+                    (argvals[0], argvals[1], argvals[2]));
+             break;
+           case 4:
+             val = (XSUBR (fun)->function.a4
+                    (argvals[0], argvals[1], argvals[2], argvals[3]));
+             break;
+           case 5:
+             val = (XSUBR (fun)->function.a5
+                    (argvals[0], argvals[1], argvals[2], argvals[3],
+                     argvals[4]));
+             break;
+           case 6:
+             val = (XSUBR (fun)->function.a6
+                    (argvals[0], argvals[1], argvals[2], argvals[3],
+                     argvals[4], argvals[5]));
+             break;
+           case 7:
+             val = (XSUBR (fun)->function.a7
+                    (argvals[0], argvals[1], argvals[2], argvals[3],
+                     argvals[4], argvals[5], argvals[6]));
+             break;
+
+           case 8:
+             val = (XSUBR (fun)->function.a8
+                    (argvals[0], argvals[1], argvals[2], argvals[3],
+                     argvals[4], argvals[5], argvals[6], argvals[7]));
+             break;
+
+           default:
+             /* Someone has created a subr that takes more arguments than
+                is supported by this code.  We need to either rewrite the
+                subr to use a different argument protocol, or add more
+                cases to this switch.  */
+             abort ();
+           }
        }
     }
-  if (COMPILEDP (fun))
-    val = apply_lambda (fun, original_args, 1);
+  else if (COMPILEDP (fun))
+    val = apply_lambda (fun, original_args);
   else
     {
       if (EQ (fun, Qunbound))
@@ -2407,13 +2421,13 @@ DEFUN ("eval", Feval, Seval, 1, 1, 0,
          goto retry;
        }
       if (EQ (funcar, Qmacro))
-       val = Feval (apply1 (Fcdr (fun), original_args));
-      else if (EQ (funcar, Qlambda))
-       val = apply_lambda (fun, original_args, 1);
+       val = eval_sub (apply1 (Fcdr (fun), original_args));
+      else if (EQ (funcar, Qlambda)
+              || EQ (funcar, Qclosure))
+       val = apply_lambda (fun, original_args);
       else
        xsignal1 (Qinvalid_function, original_fun);
     }
- done:
   CHECK_CONS_LIST ();
 
   lisp_eval_depth--;
@@ -2685,53 +2699,6 @@ run_hook_with_args (int nargs, Lisp_Object *args, enum run_hooks_condition cond)
     }
 }
 
-/* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
-   present value of that symbol.
-   Call each element of FUNLIST,
-   passing each of them the rest of ARGS.
-   The caller (or its caller, etc) must gcpro all of ARGS,
-   except that it isn't necessary to gcpro ARGS[0].  */
-
-Lisp_Object
-run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
-{
-  Lisp_Object sym;
-  Lisp_Object val;
-  Lisp_Object globals;
-  struct gcpro gcpro1, gcpro2, gcpro3;
-
-  sym = args[0];
-  globals = Qnil;
-  GCPRO3 (sym, val, globals);
-
-  for (val = funlist; CONSP (val); val = XCDR (val))
-    {
-      if (EQ (XCAR (val), Qt))
-       {
-         /* t indicates this hook has a local binding;
-            it means to run the global binding too.  */
-
-         for (globals = Fdefault_value (sym);
-              CONSP (globals);
-              globals = XCDR (globals))
-           {
-             args[0] = XCAR (globals);
-             /* In a global value, t should not occur.  If it does, we
-                must ignore it to avoid an endless loop.  */
-             if (!EQ (args[0], Qt))
-               Ffuncall (nargs, args);
-           }
-       }
-      else
-       {
-         args[0] = XCAR (val);
-         Ffuncall (nargs, args);
-       }
-    }
-  UNGCPRO;
-  return Qnil;
-}
-
 /* Run the hook HOOK, giving each function the two args ARG1 and ARG2.  */
 
 void
@@ -2900,6 +2867,39 @@ call7 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2, Lisp_Object arg3,
 
 /* The caller should GCPRO all the elements of ARGS.  */
 
+DEFUN ("functionp", Ffunctionp, Sfunctionp, 1, 1, 0,
+       doc: /* Return non-nil if OBJECT is a type of object that can be called as a function.  */)
+     (Lisp_Object object)
+{
+  if (SYMBOLP (object) && !NILP (Ffboundp (object)))
+    {
+      object = Findirect_function (object, Qnil);
+
+      if (CONSP (object) && EQ (XCAR (object), Qautoload))
+       {
+         /* Autoloaded symbols are functions, except if they load
+            macros or keymaps.  */
+         int i;
+         for (i = 0; i < 4 && CONSP (object); i++)
+           object = XCDR (object);
+
+         return (CONSP (object) && !NILP (XCAR (object))) ? Qnil : Qt;
+       }
+    }
+
+  if (SUBRP (object))
+    return (XSUBR (object)->max_args != UNEVALLED) ? Qt : Qnil;
+  else if (COMPILEDP (object))
+    return Qt;
+  else if (CONSP (object))
+    {
+      Lisp_Object car = XCAR (object);
+      return (EQ (car, Qlambda) || EQ (car, Qclosure)) ? Qt : Qnil;
+    }
+  else
+    return Qnil;
+}
+
 DEFUN ("funcall", Ffuncall, Sfuncall, 1, MANY, 0,
        doc: /* Call first argument as a function, passing remaining arguments to it.
 Return the value that function returns.
@@ -2956,83 +2956,84 @@ usage: (funcall FUNCTION &rest ARGUMENTS)  */)
 
   if (SUBRP (fun))
     {
-       if (numargs < XSUBR (fun)->min_args
+      if (numargs < XSUBR (fun)->min_args
          || (XSUBR (fun)->max_args >= 0 && XSUBR (fun)->max_args < numargs))
        {
          XSETFASTINT (lisp_numargs, numargs);
          xsignal2 (Qwrong_number_of_arguments, original_fun, lisp_numargs);
        }
 
-      if (XSUBR (fun)->max_args == UNEVALLED)
+      else if (XSUBR (fun)->max_args == UNEVALLED)
        xsignal1 (Qinvalid_function, original_fun);
 
-      if (XSUBR (fun)->max_args == MANY)
-       {
-         val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
-         goto done;
-       }
-
-      if (XSUBR (fun)->max_args > numargs)
-       {
-         internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
-         memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
-         for (i = numargs; i < XSUBR (fun)->max_args; i++)
-           internal_args[i] = Qnil;
-       }
+      else if (XSUBR (fun)->max_args == MANY)
+       val = (XSUBR (fun)->function.aMANY) (numargs, args + 1);
       else
-       internal_args = args + 1;
-      switch (XSUBR (fun)->max_args)
        {
-       case 0:
-         val = (XSUBR (fun)->function.a0) ();
-         goto done;
-       case 1:
-         val = (XSUBR (fun)->function.a1) (internal_args[0]);
-         goto done;
-       case 2:
-         val = (XSUBR (fun)->function.a2) (internal_args[0], internal_args[1]);
-         goto done;
-       case 3:
-         val = (XSUBR (fun)->function.a3) (internal_args[0], internal_args[1],
-                                           internal_args[2]);
-         goto done;
-       case 4:
-         val = (XSUBR (fun)->function.a4) (internal_args[0], internal_args[1],
-                                           internal_args[2], internal_args[3]);
-         goto done;
-       case 5:
-         val = (XSUBR (fun)->function.a5) (internal_args[0], internal_args[1],
-                                           internal_args[2], internal_args[3],
-                                           internal_args[4]);
-         goto done;
-       case 6:
-         val = (XSUBR (fun)->function.a6) (internal_args[0], internal_args[1],
-                                           internal_args[2], internal_args[3],
-                                           internal_args[4], internal_args[5]);
-         goto done;
-       case 7:
-         val = (XSUBR (fun)->function.a7) (internal_args[0], internal_args[1],
-                                           internal_args[2], internal_args[3],
-                                           internal_args[4], internal_args[5],
-                                           internal_args[6]);
-         goto done;
-
-       case 8:
-         val = (XSUBR (fun)->function.a8) (internal_args[0], internal_args[1],
-                                           internal_args[2], internal_args[3],
-                                           internal_args[4], internal_args[5],
-                                           internal_args[6], internal_args[7]);
-         goto done;
-
-       default:
-
-         /* If a subr takes more than 8 arguments without using MANY
-            or UNEVALLED, we need to extend this function to support it.
-            Until this is done, there is no way to call the function.  */
-         abort ();
+         if (XSUBR (fun)->max_args > numargs)
+           {
+             internal_args = (Lisp_Object *) alloca (XSUBR (fun)->max_args * sizeof (Lisp_Object));
+             memcpy (internal_args, args + 1, numargs * sizeof (Lisp_Object));
+             for (i = numargs; i < XSUBR (fun)->max_args; i++)
+               internal_args[i] = Qnil;
+           }
+         else
+           internal_args = args + 1;
+         switch (XSUBR (fun)->max_args)
+           {
+           case 0:
+             val = (XSUBR (fun)->function.a0 ());
+             break;
+           case 1:
+             val = (XSUBR (fun)->function.a1 (internal_args[0]));
+             break;
+           case 2:
+             val = (XSUBR (fun)->function.a2
+                    (internal_args[0], internal_args[1]));
+             break;
+           case 3:
+             val = (XSUBR (fun)->function.a3
+                    (internal_args[0], internal_args[1], internal_args[2]));
+             break;
+           case 4:
+             val = (XSUBR (fun)->function.a4
+                    (internal_args[0], internal_args[1], internal_args[2],
+                    internal_args[3]));
+             break;
+           case 5:
+             val = (XSUBR (fun)->function.a5
+                    (internal_args[0], internal_args[1], internal_args[2],
+                     internal_args[3], internal_args[4]));
+             break;
+           case 6:
+             val = (XSUBR (fun)->function.a6
+                    (internal_args[0], internal_args[1], internal_args[2],
+                     internal_args[3], internal_args[4], internal_args[5]));
+             break;
+           case 7:
+             val = (XSUBR (fun)->function.a7
+                    (internal_args[0], internal_args[1], internal_args[2],
+                     internal_args[3], internal_args[4], internal_args[5],
+                     internal_args[6]));
+             break;
+
+           case 8:
+             val = (XSUBR (fun)->function.a8
+                    (internal_args[0], internal_args[1], internal_args[2],
+                     internal_args[3], internal_args[4], internal_args[5],
+                     internal_args[6], internal_args[7]));
+             break;
+
+           default:
+
+             /* If a subr takes more than 8 arguments without using MANY
+                or UNEVALLED, we need to extend this function to support it.
+                Until this is done, there is no way to call the function.  */
+             abort ();
+           }
        }
     }
-  if (COMPILEDP (fun))
+  else if (COMPILEDP (fun))
     val = funcall_lambda (fun, numargs, args + 1);
   else
     {
@@ -3043,7 +3044,8 @@ usage: (funcall FUNCTION &rest ARGUMENTS)  */)
       funcar = XCAR (fun);
       if (!SYMBOLP (funcar))
        xsignal1 (Qinvalid_function, original_fun);
-      if (EQ (funcar, Qlambda))
+      if (EQ (funcar, Qlambda)
+         || EQ (funcar, Qclosure))
        val = funcall_lambda (fun, numargs, args + 1);
       else if (EQ (funcar, Qautoload))
        {
@@ -3054,7 +3056,6 @@ usage: (funcall FUNCTION &rest ARGUMENTS)  */)
       else
        xsignal1 (Qinvalid_function, original_fun);
     }
- done:
   CHECK_CONS_LIST ();
   lisp_eval_depth--;
   if (backtrace.debug_on_exit)
@@ -3063,8 +3064,8 @@ usage: (funcall FUNCTION &rest ARGUMENTS)  */)
   return val;
 }
 \f
-Lisp_Object
-apply_lambda (Lisp_Object fun, Lisp_Object args, int eval_flag)
+static Lisp_Object
+apply_lambda (Lisp_Object fun, Lisp_Object args)
 {
   Lisp_Object args_left;
   Lisp_Object numargs;
@@ -3084,18 +3085,15 @@ apply_lambda (Lisp_Object fun, Lisp_Object args, int eval_flag)
   for (i = 0; i < XINT (numargs);)
     {
       tem = Fcar (args_left), args_left = Fcdr (args_left);
-      if (eval_flag) tem = Feval (tem);
+      tem = eval_sub (tem);
       arg_vector[i++] = tem;
       gcpro1.nvars = i;
     }
 
   UNGCPRO;
 
-  if (eval_flag)
-    {
-      backtrace_list->args = arg_vector;
-      backtrace_list->nargs = i;
-    }
+  backtrace_list->args = arg_vector;
+  backtrace_list->nargs = i;
   backtrace_list->evalargs = 0;
   tem = funcall_lambda (fun, XINT (numargs), arg_vector);
 
@@ -3113,14 +3111,23 @@ apply_lambda (Lisp_Object fun, Lisp_Object args, int eval_flag)
    FUN must be either a lambda-expression or a compiled-code object.  */
 
 static Lisp_Object
-funcall_lambda (Lisp_Object fun, int nargs, register Lisp_Object *arg_vector)
+funcall_lambda (Lisp_Object fun, int nargs,
+               register Lisp_Object *arg_vector)
 {
-  Lisp_Object val, syms_left, next;
+  Lisp_Object val, syms_left, next, lexenv;
   int count = SPECPDL_INDEX ();
   int i, optional, rest;
 
   if (CONSP (fun))
     {
+      if (EQ (XCAR (fun), Qclosure))
+       {
+         fun = XCDR (fun);     /* Drop `closure'.  */
+         lexenv = XCAR (fun);
+         fun = XCDR (fun);     /* Drop the lexical environment.  */
+       }
+      else
+       lexenv = Qnil;
       syms_left = XCDR (fun);
       if (CONSP (syms_left))
        syms_left = XCAR (syms_left);
@@ -3128,7 +3135,31 @@ funcall_lambda (Lisp_Object fun, int nargs, register Lisp_Object *arg_vector)
        xsignal1 (Qinvalid_function, fun);
     }
   else if (COMPILEDP (fun))
-    syms_left = AREF (fun, COMPILED_ARGLIST);
+    {
+      if ((ASIZE (fun) & PSEUDOVECTOR_SIZE_MASK) > COMPILED_PUSH_ARGS
+         && ! NILP (XVECTOR (fun)->contents[COMPILED_PUSH_ARGS]))
+       /* A byte-code object with a non-nil `push args' slot means we
+          shouldn't bind any arguments, instead just call the byte-code
+          interpreter directly; it will push arguments as necessary.
+
+          Byte-code objects with either a non-existant, or a nil value for
+          the `push args' slot (the default), have dynamically-bound
+          arguments, and use the argument-binding code below instead (as do
+          all interpreted functions, even lexically bound ones).  */
+       {
+         /* If we have not actually read the bytecode string
+            and constants vector yet, fetch them from the file.  */
+         if (CONSP (AREF (fun, COMPILED_BYTECODE)))
+           Ffetch_bytecode (fun);
+         return exec_byte_code (AREF (fun, COMPILED_BYTECODE),
+                                AREF (fun, COMPILED_CONSTANTS),
+                                AREF (fun, COMPILED_STACK_DEPTH),
+                                AREF (fun, COMPILED_ARGLIST),
+                                nargs, arg_vector);
+       }
+      syms_left = AREF (fun, COMPILED_ARGLIST);
+      lexenv = Qnil;
+    }
   else
     abort ();
 
@@ -3145,17 +3176,29 @@ funcall_lambda (Lisp_Object fun, int nargs, register Lisp_Object *arg_vector)
        rest = 1;
       else if (EQ (next, Qand_optional))
        optional = 1;
-      else if (rest)
+      else
        {
-         specbind (next, Flist (nargs - i, &arg_vector[i]));
-         i = nargs;
+         Lisp_Object val;
+         if (rest)
+           {
+             val = Flist (nargs - i, &arg_vector[i]);
+             i = nargs;
+           }
+         else if (i < nargs)
+           val = arg_vector[i++];
+         else if (!optional)
+           xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
+         else
+           val = Qnil;
+           
+         /* Bind the argument.  */
+         if (!NILP (lexenv) && SYMBOLP (next))
+           /* Lexically bind NEXT by adding it to the lexenv alist.  */
+           lexenv = Fcons (Fcons (next, val), lexenv);
+         else
+           /* Dynamically bind NEXT.  */
+           specbind (next, val);
        }
-      else if (i < nargs)
-       specbind (next, arg_vector[i++]);
-      else if (!optional)
-       xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
-      else
-       specbind (next, Qnil);
     }
 
   if (!NILP (syms_left))
@@ -3163,6 +3206,10 @@ funcall_lambda (Lisp_Object fun, int nargs, register Lisp_Object *arg_vector)
   else if (i < nargs)
     xsignal2 (Qwrong_number_of_arguments, fun, make_number (nargs));
 
+  if (!EQ (lexenv, Vinternal_interpreter_environment))
+    /* Instantiate a new lexical environment.  */
+    specbind (Qinternal_interpreter_environment, lexenv);
+
   if (CONSP (fun))
     val = Fprogn (XCDR (XCDR (fun)));
   else
@@ -3171,9 +3218,10 @@ funcall_lambda (Lisp_Object fun, int nargs, register Lisp_Object *arg_vector)
         and constants vector yet, fetch them from the file.  */
       if (CONSP (AREF (fun, COMPILED_BYTECODE)))
        Ffetch_bytecode (fun);
-      val = Fbyte_code (AREF (fun, COMPILED_BYTECODE),
-                       AREF (fun, COMPILED_CONSTANTS),
-                       AREF (fun, COMPILED_STACK_DEPTH));
+      val = exec_byte_code (AREF (fun, COMPILED_BYTECODE),
+                           AREF (fun, COMPILED_CONSTANTS),
+                           AREF (fun, COMPILED_STACK_DEPTH),
+                           Qnil, 0, 0);
     }
 
   return unbind_to (count, val);
@@ -3409,7 +3457,21 @@ unbind_to (int count, Lisp_Object value)
   UNGCPRO;
   return value;
 }
+
+\f
+
+DEFUN ("special-variable-p", Fspecial_variable_p, Sspecial_variable_p, 1, 1, 0,
+       doc: /* Return non-nil if SYMBOL's global binding has been declared special.
+A special variable is one that will be bound dynamically, even in a
+context where binding is lexical by default.  */)
+  (Lisp_Object symbol)
+{
+   CHECK_SYMBOL (symbol);
+   return XSYMBOL (symbol)->declared_special ? Qt : Qnil;
+}
+
 \f
+
 DEFUN ("backtrace-debug", Fbacktrace_debug, Sbacktrace_debug, 2, 2, 0,
        doc: /* Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
 The debugger is entered when that frame exits, if the flag is non-nil.  */)
@@ -3441,8 +3503,10 @@ Output stream used is value of `standard-output'.  */)
   Lisp_Object tail;
   Lisp_Object tem;
   struct gcpro gcpro1;
+  Lisp_Object old_print_level = Vprint_level;
 
-  XSETFASTINT (Vprint_level, 3);
+  if (NILP (Vprint_level))
+    XSETFASTINT (Vprint_level, 8);
 
   tail = Qnil;
   GCPRO1 (tail);
@@ -3483,7 +3547,7 @@ Output stream used is value of `standard-output'.  */)
       backlist = backlist->next;
     }
 
-  Vprint_level = Qnil;
+  Vprint_level = old_print_level;
   UNGCPRO;
   return Qnil;
 }
@@ -3545,10 +3609,12 @@ mark_backtrace (void)
     }
 }
 
+EXFUN (Funintern, 2);
+
 void
 syms_of_eval (void)
 {
-  DEFVAR_INT ("max-specpdl-size", &max_specpdl_size,
+  DEFVAR_INT ("max-specpdl-size", max_specpdl_size,
              doc: /* *Limit on number of Lisp variable bindings and `unwind-protect's.
 If Lisp code tries to increase the total number past this amount,
 an error is signaled.
@@ -3556,7 +3622,7 @@ You can safely use a value considerably larger than the default value,
 if that proves inconveniently small.  However, if you increase it too far,
 Emacs could run out of memory trying to make the stack bigger.  */);
 
-  DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth,
+  DEFVAR_INT ("max-lisp-eval-depth", max_lisp_eval_depth,
              doc: /* *Limit on depth in `eval', `apply' and `funcall' before error.
 
 This limit serves to catch infinite recursions for you before they cause
@@ -3565,7 +3631,7 @@ You can safely make it considerably larger than its default value,
 if that proves inconveniently small.  However, if you increase it too far,
 Emacs could overflow the real C stack, and crash.  */);
 
-  DEFVAR_LISP ("quit-flag", &Vquit_flag,
+  DEFVAR_LISP ("quit-flag", Vquit_flag,
               doc: /* Non-nil causes `eval' to abort, unless `inhibit-quit' is non-nil.
 If the value is t, that means do an ordinary quit.
 If the value equals `throw-on-input', that means quit by throwing
@@ -3574,7 +3640,7 @@ Typing C-g sets `quit-flag' to t, regardless of `inhibit-quit',
 but `inhibit-quit' non-nil prevents anything from taking notice of that.  */);
   Vquit_flag = Qnil;
 
-  DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit,
+  DEFVAR_LISP ("inhibit-quit", Vinhibit_quit,
               doc: /* Non-nil inhibits C-g quitting from happening immediately.
 Note that `quit-flag' will still be set by typing C-g,
 so a quit will be signaled as soon as `inhibit-quit' is nil.
@@ -3617,18 +3683,13 @@ before making `inhibit-quit' nil.  */);
   Qand_optional = intern_c_string ("&optional");
   staticpro (&Qand_optional);
 
+  Qclosure = intern_c_string ("closure");
+  staticpro (&Qclosure);
+
   Qdebug = intern_c_string ("debug");
   staticpro (&Qdebug);
 
-  DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error,
-              doc: /* *Non-nil means errors display a backtrace buffer.
-More precisely, this happens for any error that is handled
-by the editor command loop.
-If the value is a list, an error only means to display a backtrace
-if one of its condition symbols appears in the list.  */);
-  Vstack_trace_on_error = Qnil;
-
-  DEFVAR_LISP ("debug-on-error", &Vdebug_on_error,
+  DEFVAR_LISP ("debug-on-error", Vdebug_on_error,
               doc: /* *Non-nil means enter debugger if an error is signaled.
 Does not apply to errors handled by `condition-case' or those
 matched by `debug-ignored-errors'.
@@ -3640,7 +3701,7 @@ The command `toggle-debug-on-error' toggles this.
 See also the variable `debug-on-quit'.  */);
   Vdebug_on_error = Qnil;
 
-  DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors,
+  DEFVAR_LISP ("debug-ignored-errors", Vdebug_ignored_errors,
     doc: /* *List of errors for which the debugger should not be called.
 Each element may be a condition-name or a regexp that matches error messages.
 If any element applies to a given error, that error skips the debugger
@@ -3649,21 +3710,21 @@ This overrides the variable `debug-on-error'.
 It does not apply to errors handled by `condition-case'.  */);
   Vdebug_ignored_errors = Qnil;
 
-  DEFVAR_BOOL ("debug-on-quit", &debug_on_quit,
+  DEFVAR_BOOL ("debug-on-quit", debug_on_quit,
     doc: /* *Non-nil means enter debugger if quit is signaled (C-g, for example).
 Does not apply if quit is handled by a `condition-case'.  */);
   debug_on_quit = 0;
 
-  DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call,
+  DEFVAR_BOOL ("debug-on-next-call", debug_on_next_call,
               doc: /* Non-nil means enter debugger before next `eval', `apply' or `funcall'.  */);
 
-  DEFVAR_BOOL ("debugger-may-continue", &debugger_may_continue,
+  DEFVAR_BOOL ("debugger-may-continue", debugger_may_continue,
               doc: /* Non-nil means debugger may continue execution.
 This is nil when the debugger is called under circumstances where it
 might not be safe to continue.  */);
   debugger_may_continue = 1;
 
-  DEFVAR_LISP ("debugger", &Vdebugger,
+  DEFVAR_LISP ("debugger", Vdebugger,
               doc: /* Function to call to invoke debugger.
 If due to frame exit, args are `exit' and the value being returned;
  this function's value will be returned instead of that.
@@ -3672,19 +3733,19 @@ If due to `apply' or `funcall' entry, one arg, `lambda'.
 If due to `eval' entry, one arg, t.  */);
   Vdebugger = Qnil;
 
-  DEFVAR_LISP ("signal-hook-function", &Vsignal_hook_function,
+  DEFVAR_LISP ("signal-hook-function", Vsignal_hook_function,
               doc: /* If non-nil, this is a function for `signal' to call.
 It receives the same arguments that `signal' was given.
 The Edebug package uses this to regain control.  */);
   Vsignal_hook_function = Qnil;
 
-  DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal,
+  DEFVAR_LISP ("debug-on-signal", Vdebug_on_signal,
               doc: /* *Non-nil means call the debugger regardless of condition handlers.
 Note that `debug-on-error', `debug-on-quit' and friends
 still determine whether to handle the particular condition.  */);
   Vdebug_on_signal = Qnil;
 
-  DEFVAR_LISP ("macro-declaration-function", &Vmacro_declaration_function,
+  DEFVAR_LISP ("macro-declaration-function", Vmacro_declaration_function,
               doc: /* Function to process declarations in a macro definition.
 The function will be called with two args MACRO and DECL.
 MACRO is the name of the macro being defined.
@@ -3692,6 +3753,28 @@ DECL is a list `(declare ...)' containing the declarations.
 The value the function returns is not used.  */);
   Vmacro_declaration_function = Qnil;
 
+  /* When lexical binding is being used,
+   vinternal_interpreter_environment is non-nil, and contains an alist
+   of lexically-bound variable, or (t), indicating an empty
+   environment.  The lisp name of this variable would be
+   `internal-interpreter-environment' if it weren't hidden.
+   Every element of this list can be either a cons (VAR . VAL)
+   specifying a lexical binding, or a single symbol VAR indicating
+   that this variable should use dynamic scoping.  */
+  Qinternal_interpreter_environment
+    = intern_c_string ("internal-interpreter-environment");
+  staticpro (&Qinternal_interpreter_environment);
+  DEFVAR_LISP ("internal-interpreter-environment",
+               Vinternal_interpreter_environment,
+              doc: /* If non-nil, the current lexical environment of the lisp interpreter.
+When lexical binding is not being used, this variable is nil.
+A value of `(t)' indicates an empty environment, otherwise it is an
+alist of active lexical bindings.  */);
+  Vinternal_interpreter_environment = Qnil;
+  /* Don't export this variable to Elisp, so noone can mess with it
+     (Just imagine if someone makes it buffer-local).  */
+  Funintern (Qinternal_interpreter_environment, Qnil);
+
   Vrun_hooks = intern_c_string ("run-hooks");
   staticpro (&Vrun_hooks);
 
@@ -3740,7 +3823,7 @@ The value the function returns is not used.  */);
   defsubr (&Sbacktrace_debug);
   defsubr (&Sbacktrace);
   defsubr (&Sbacktrace_frame);
+  defsubr (&Sspecial_variable_p);
+  defsubr (&Sfunctionp);
 }
 
-/* arch-tag: 014a07aa-33ab-4a8f-a3d2-ee8a4a9ff7fb
-   (do not change this comment) */