Subject: Re: bug#30078: 27.0.50; Use lexical-binding for M-:

From: Stefan Monnier <[email-masked]>
Date: Fri, 12 Jan 2018 11:12:23 -0500

>>> I think we should slowly move towards making lexical-binding the default
>>> and we could start by making M-: always use lexical binding.
>>> WDYT?
>> Why not?
>> But maybe we should keep this consistent with C-x C-e in *scratch* to
>> avoid confusion (a la "why do the evaluation results differ?"),
>> i.e. enable lexical-binding in *scratch* at the same time?
> Good idea.

I figured M-x ielm would qualify as well.
How 'bout this patch, then?

        Stefan

diff --git a/etc/NEWS b/etc/NEWS
index f6f36dfc85..d7e84eca7f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -52,6 +52,10 @@ to reduce differences between developer and production builds.
 
 * Changes in Emacs 27.1
 
+** Lexical binding is now used when evaluating interactive Elisp forms
+More specifically, lexical-binding is used for M-:, M-x ielm, as well
+as in the *scratch* buffer.
+
 ---
 ** The new option 'tooltip-resize-echo-area' avoids truncating tooltip text
 on GUI frames when tooltips are displayed in the echo area.  Instead,
diff --git a/lisp/ielm.el b/lisp/ielm.el
index fb285e80f6..cad1bded4f 100644
--- a/lisp/ielm.el
+++ b/lisp/ielm.el
@@ -408,8 +408,7 @@ ielm-eval-input
                                 (setf standard-output new-standard-output))
                               (kill-buffer (current-buffer))
                               (set-buffer wbuf)
-                              (setq result
-                                    (eval form lexical-binding))
+                              (setq result (eval form t))
                               (setq wbuf (current-buffer))
                               (setq
                                ielm-temp-buffer
diff --git a/lisp/server.el b/lisp/server.el
index ac0d701851..918197ab44 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1294,7 +1294,7 @@ server-execute
 			 ((functionp initial-buffer-choice)
 			  (funcall initial-buffer-choice)))))
 	      (switch-to-buffer
-	       (if (buffer-live-p buf) buf (get-buffer-create "*scratch*"))
+	       (if (buffer-live-p buf) buf (startup--get-buffer-create-scratch))
 	       'norecord)))
 
           ;; Delete the client if necessary.
diff --git a/lisp/simple.el b/lisp/simple.el
index 87e0b23377..e2d3cd1505 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1555,12 +1555,12 @@ eval-expression
          (eval-expression-get-print-arguments current-prefix-arg)))
 
   (if (null eval-expression-debug-on-error)
-      (push (eval exp lexical-binding) values)
+      (push (eval exp t) values)
     (let ((old-value (make-symbol "t")) new-value)
       ;; Bind debug-on-error to something unique so that we can
       ;; detect when evalled code changes it.
       (let ((debug-on-error old-value))
-	(push (eval (macroexpand-all exp) lexical-binding) values)
+	(push (eval (macroexpand-all exp) t) values)
 	(setq new-value debug-on-error))
       ;; If evalled code has changed the value of debug-on-error,
       ;; propagate that change to the global binding.
diff --git a/lisp/startup.el b/lisp/startup.el
index 688ea84b7b..f3c0c7b100 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -2095,7 +2095,7 @@ normal-no-mouse-startup-screen
   (insert "\t\t")
   (insert-button "Open *scratch* buffer"
 		 'action (lambda (_button) (switch-to-buffer
-                                       (get-buffer-create "*scratch*")))
+                                       (startup--get-buffer-create-scratch)))
 		 'follow-link t)
   (insert "\n")
   (insert "\n" (emacs-version) "\n" emacs-copyright "\n")
@@ -2221,6 +2221,12 @@ display-about-screen
 (defalias 'about-emacs 'display-about-screen)
 (defalias 'display-splash-screen 'display-startup-screen)
 
+(defun startup--get-buffer-create-scratch ()
+  (with-current-buffer (get-buffer-create "*scratch*")
+    (set-buffer-major-mode (current-buffer))
+    (setq-local lexical-binding t)
+    (current-buffer)))
+
 (defun command-line-1 (args-left)
   "A subroutine of `command-line'."
   (display-startup-echo-area-message)
@@ -2485,7 +2491,7 @@ command-line-1
     (when (eq initial-buffer-choice t)
       ;; When `initial-buffer-choice' equals t make sure that *scratch*
       ;; exists.
-      (get-buffer-create "*scratch*"))
+      (startup--get-buffer-create-scratch))
 
     ;; If *scratch* exists and is empty, insert initial-scratch-message.
     ;; Do this before switching to *scratch* below to handle bug#9605.
@@ -2504,7 +2510,7 @@ command-line-1
 		   ((functionp initial-buffer-choice)
 		    (funcall initial-buffer-choice))
                    ((eq initial-buffer-choice t)
-                    (get-buffer-create "*scratch*"))
+                    (startup--get-buffer-create-scratch))
                    (t
                     (error "initial-buffer-choice must be a string, a function, or t.")))))
         (unless (buffer-live-p buf)
diff --git a/lisp/window.el b/lisp/window.el
index d7fdceb205..7f89c87af1 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4578,7 +4578,7 @@ last-buffer
   (or (get-next-valid-buffer (nreverse (buffer-list frame))
  			     buffer visible-ok frame)
       (get-buffer "*scratch*")
-      (let ((scratch (get-buffer-create "*scratch*")))
+      (let ((scratch (startup--get-buffer-create-scratch)))
 	(set-buffer-major-mode scratch)
 	scratch)))