From 66270adb8cd546f4839fa6a476312063e01b7fa0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Sun, 22 Oct 2023 16:25:28 +0200
Subject: [PATCH] Automatic Elisp dialect declaration insertion

Insert the `;;; -*- lexical-binding:t -*-` cookie and set
`lexical-binding` to `t` when the user visits a new Elisp file, unless
this feature is disabled.  `auto-insert-mode` takes precedence.

* etc/NEWS: Announce.
* lisp/progmodes/elisp-mode.el (emacs-lisp-mode): Add hook.
(elisp-auto-dialect-declaration): New defcustom.
(elisp--insert-auto-dialect-declaration): New function.
* test/lisp/progmodes/elisp-mode-tests.el
(elisp-auto-dialect-declaration): New test.
---
 etc/NEWS                                | 11 ++++++++
 lisp/progmodes/elisp-mode.el            | 35 ++++++++++++++++++++++++-
 test/lisp/progmodes/elisp-mode-tests.el | 25 ++++++++++++++++++
 3 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8becfae7bb9..d09abd42313 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -804,6 +804,17 @@ This argument specifies the prettifying algorithm to use.
 
 ** Emacs Lisp mode
 
+*** Automatic Elisp dialect declaration insertion.
+When visiting a new Elisp file, Emacs will now automatically insert a
+';;; -*- lexical-binding: t -*-' line to declare the modern Elisp
+lexical-binding dialect, and set the 'lexical-binding' variable
+in the buffer to 't'.
+
+This mechanism is controlled by the new 'elisp-auto-dialect-declaration'
+user option.  It will only insert a declaration into an empty buffer:
+if the buffer already had text added by means of 'auto-insert-mode'
+then it will not do anything.
+
 ---
 *** ',@' now has 'prefix' syntax.
 Previously, the '@' character, which normally has 'symbol' syntax,
diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index ff90a744ea3..8ac32ecd077 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -363,7 +363,40 @@ emacs-lisp-mode
   (add-hook 'flymake-diagnostic-functions #'elisp-flymake-checkdoc nil t)
   (add-hook 'flymake-diagnostic-functions
               #'elisp-flymake-byte-compile nil t)
-  (add-hook 'context-menu-functions #'elisp-context-menu 10 t))
+  (add-hook 'context-menu-functions #'elisp-context-menu 10 t)
+  ;; Add this hook sufficient late to give other hooks (like `auto-insert')
+  ;; the opportunity to insert something with higher priority.
+  (add-hook 'find-file-hook #'elisp--insert-auto-dialect-declaration 50 t))
+
+(defcustom elisp-auto-dialect-declaration 'lexical
+  "Dialect declaration automatically inserted in new Elisp buffers.
+The declaration is \";;; -*- lexical-binding: t -*-\".
+It is only inserted when an empty non-existing file is visited.
+Possible values are:
+  `lexical'  declare use of the modern lexical binding dialect.
+  `nil'      do not automatically insert any declaration.
+
+If `auto-insert-mode' is used to put something in the buffer instead,
+then no declaration is inserted."
+  :type '(choice (const :tag "Lexical binding (modern)" lexical)
+                 (const :tag "No automatic declaration" nil))
+  :group 'lisp
+  :version "30.1")
+
+(defun elisp--insert-auto-dialect-declaration ()
+  "Insert the `elisp-auto-dialect-declaration' selection in a new empty buffer.
+Otherwise, do nothing."
+  (when (and (eq elisp-auto-dialect-declaration 'lexical)
+             (not buffer-read-only)
+             (zerop (buffer-size))
+             ;; Don't modify a buffer corresponding to an existing empty file.
+             (not (and buffer-file-name (file-exists-p buffer-file-name))))
+    (let ((was-modified (buffer-modified-p)))
+      (insert ";;; -*- lexical-binding: t -*-\n")
+      (setq-local lexical-binding t)
+      ;; Mark the buffer unmodified (unless it was modified before)
+      ;; so that the user isn't bothered when killing it or quitting Emacs.
+      (set-buffer-modified-p was-modified))))
 
 ;; Font-locking support.
 
diff --git a/test/lisp/progmodes/elisp-mode-tests.el b/test/lisp/progmodes/elisp-mode-tests.el
index 4fa869c773f..2a18b204ef1 100644
--- a/test/lisp/progmodes/elisp-mode-tests.el
+++ b/test/lisp/progmodes/elisp-mode-tests.el
@@ -1131,5 +1131,30 @@ test-indentation
                         (emacs-lisp-mode)
                         (indent-region (point-min) (point-max)))))
 
+(defun elisp-mode-tests--insert-gibberish ()
+  (insert "gibberish\n")
+  (set-buffer-modified-p nil))
+
+(ert-deftest elisp-auto-dialect-declaration ()
+  (let ((file "does-not-exist.el"))
+    (should-not (file-exists-p file))
+    ;; Try with `elisp-auto-dialect-declaration' off and on.
+    (dolist (enabled '(nil lexical))
+      (let ((elisp-auto-dialect-declaration enabled))
+        ;; Try with a competing insertion that uses the `find-file-hook'.
+        (dolist (auto-ins '(nil t))
+          (let ((find-file-hook
+                 (if auto-ins
+                     (cons 'elisp-mode-tests--insert-gibberish find-file-hook)
+                   find-file-hook)))
+            (save-current-buffer
+              (find-file file)
+              (should (equal (buffer-string)
+                             (cond (auto-ins "gibberish\n")
+                                   (enabled ";;; -*- lexical-binding: t -*-\n")
+                                   (t ""))))
+              (should (equal (buffer-modified-p) nil))
+              (kill-buffer))))))))
+
 (provide 'elisp-mode-tests)
 ;;; elisp-mode-tests.el ends here
-- 
2.32.0 (Apple Git-132)

