unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: Stefan Kangas <stefankangas@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>,
	66706@debbugs.gnu.org, Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#66706: [PATCH] Automatic elisp dialect insertion
Date: Tue, 24 Oct 2023 19:31:33 +0200	[thread overview]
Message-ID: <98CD592C-0E82-4795-8168-2B5E597FF7A7@gmail.com> (raw)
In-Reply-To: <CADwFkmnb9riGSVeHKYOPuR63-KOaj666TJyia8hKbWvj4qZ4Rg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 168 bytes --]

Here is an updated patch, now with a test.

Not sure if this belongs in the manual, but if so it's probably in the Elisp manual ('Selecting Lisp Dialect'), right?


[-- Attachment #2: 0001-Automatic-Elisp-dialect-declaration-insertion.patch --]
[-- Type: application/octet-stream, Size: 5661 bytes --]

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)


  parent reply	other threads:[~2023-10-24 17:31 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 17:46 bug#66706: [PATCH] Automatic elisp dialect insertion Mattias Engdegård
2023-10-23 18:21 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-23 18:44 ` Eli Zaretskii
2023-10-23 19:21   ` Stefan Kangas
2023-10-23 20:20     ` Mattias Engdegård
2023-10-24 17:31     ` Mattias Engdegård [this message]
2023-10-24 18:25       ` Eli Zaretskii
2023-10-24 19:19         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-24 20:22           ` Stefan Kangas
2023-10-25  2:31             ` Eli Zaretskii
2023-10-25 11:56               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:17               ` Stefan Kangas
2023-10-25 12:54                 ` Dmitry Gutov
2023-10-26  0:31                   ` Michael Heerdegen
2023-10-26  6:35                     ` Eli Zaretskii
2023-10-27  3:14                       ` Michael Heerdegen
2023-10-27  6:26                         ` Eli Zaretskii
2023-10-27  7:24                           ` Michael Heerdegen
2023-10-27  7:32                             ` Eli Zaretskii
2023-10-27 14:41                           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-29 12:26                             ` Eli Zaretskii
2023-10-25  0:59           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  1:20             ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  2:01             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25  3:01               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 11:48                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:46                   ` Dmitry Gutov
2023-10-25 12:48                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 14:56                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 16:04                       ` Eli Zaretskii
2023-10-26  0:01                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 12:03               ` Eli Zaretskii
2023-10-25 13:06                 ` Dmitry Gutov
2023-10-25 13:20                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 13:40                     ` Dmitry Gutov
2023-10-26  0:07                     ` Jim Porter
2023-10-26  0:40                       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:51                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  1:19                         ` Jim Porter
2023-10-26  1:41                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  3:48                           ` Jim Porter
2023-10-26  5:56                             ` Jim Porter
2023-10-26  7:09                             ` Eli Zaretskii
2023-10-26  2:37                         ` Drew Adams
2023-10-26  2:28                       ` Drew Adams
2023-10-26  5:21                       ` Eli Zaretskii
2023-10-25 13:57                   ` Eli Zaretskii
2023-10-25 15:11                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 16:08                   ` Eli Zaretskii
2023-10-25 16:10                     ` Dmitry Gutov
2023-10-25 16:20                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:02                         ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 18:19                     ` Mattias Engdegård
2023-10-25 18:40                       ` Eli Zaretskii
2023-10-25 19:09                         ` Mattias Engdegård
2023-10-25 23:43                           ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  0:07                           ` Jim Porter
2023-10-26  2:34                             ` Drew Adams
2023-10-26  3:56                               ` Jim Porter
2023-10-26  5:22                             ` Eli Zaretskii
2023-10-26  6:31                               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 13:54                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 14:02                                   ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26 15:35                                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-26  8:32                             ` Mattias Engdegård
2023-10-26 11:39                               ` Nikolay Kudryavtsev
2023-10-26 15:36                                 ` Drew Adams
2023-10-25 12:36               ` Nikolay Kudryavtsev
2023-10-25 12:48                 ` Dmitry Gutov
2023-10-26 11:06                   ` Nikolay Kudryavtsev
2023-10-25  2:27           ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98CD592C-0E82-4795-8168-2B5E597FF7A7@gmail.com \
    --to=mattias.engdegard@gmail.com \
    --cc=66706@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).