unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#66706: [PATCH] Automatic elisp dialect insertion
@ 2023-10-23 17:46 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
  0 siblings, 2 replies; 71+ messages in thread
From: Mattias Engdegård @ 2023-10-23 17:46 UTC (permalink / raw)
  To: 66706

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

This patch inserts the lexical cookie in new Elisp files automatically.
It helps users by making it less likely that they forget to add it, and eliminates some drudgery.
Their code will be more future-safe, and more robust and performant here and now.


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

From 1f046584a58da54b32b6be8c17d00c990453a177 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.
---
 etc/NEWS                     | 11 +++++++++++
 lisp/progmodes/elisp-mode.el | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index d0880669752..b87f51a134c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -797,6 +797,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.
 
-- 
2.32.0 (Apple Git-132)


^ permalink raw reply related	[flat|nested] 71+ messages in thread

end of thread, other threads:[~2023-10-29 12:26 UTC | newest]

Thread overview: 71+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).