Subject: Re: bug#77081: [PATCH] New minor mode: `electric-block-comment-mode'

From: Elijah Gabe Pérez <[email-masked]>
Date: Sat, 22 Mar 2025 13:51:42 -0600

Eli Zaretskii <[email-masked]> writes:
>> --- a/etc/NEWS
>> +++ b/etc/NEWS
>> @@ -111,6 +111,10 @@ If you have been using these variables in Lisp code (for example, in
>>  font-lock rules), simply quote the symbol, to use the face directly
>>  instead of its now-obsolete variable.

>> +** New minor mode 'electric-block-comment-mode'
>> +This mode automatically close block comment typed, thus typing
>> +"/*" in c-mode automatically inserts "*/".
>> +

> This should mention block-comment-start and block-comment-end.  Also,
> it would be good to name a few modes which support block comments,
> just so users of those modes will become interested.

I agree, done.

Sending fixed patch.

>From 8c921b83485f3bc301eec6ec5dbb93a6e09ed86d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Mon, 17 Mar 2025 12:56:52 -0600
Subject: [PATCH] New minor mode: `electric-block-comment-mode'

This minor lets you automatically closing block comments after typing
`block-comment-start'.  Thus, typing "/*" in c-mode and derived
automatically inserts "*/". (Bug#77081)

* etc/NEWS: Add minor-mode item.
* lisp/electric.el (electric-block-comment-post-self-insert-function):
New function.
(electric-block-comment-mode): New minor mode definition.
---
 etc/NEWS         | 11 +++++++++++
 lisp/electric.el | 30 ++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index cc63d03eafe..3db1d55cb7e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -111,6 +111,17 @@ If you have been using these variables in Lisp code (for example, in
 font-lock rules), simply quote the symbol, to use the face directly
 instead of its now-obsolete variable.
 
+** New minor mode 'electric-block-comment-mode'
+This mode automatically close block comment, typing `block-comment-start'
+closes it inserting their corresponding `block-comment-end'.
+Thus, allows closing block comments for major modes ​​that support it,
+such as:
+- c-mode, c++-mode, java-mode, js-mode, css-mode, and derived
+- html-mode, mhtml-mode, xml-mode and nxml-mode
+- pascal-mode
+- lua-ts-mode
+- lisp-mode and common-lisp-mode
+
 ** Network Security Manager (NSM) is now more strict.
 
 *** NSM warns about TLS 1.1 by default.
diff --git a/lisp/electric.el b/lisp/electric.el
index 39e13e1ca0c..86c01438e9a 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -731,6 +731,36 @@ electric-quote-local-mode
     (setq-default electric-quote-mode nil) ; But keep it globally disabled.
     )))
 
+;;; Electric comment block
+
+(defun electric-block-comment-post-self-insert-function ()
+  "Function that `electric-block-comment' adds to `post-self-insert-hook'.
+This closes block comment with `block-comment-end' when `block-comment-start'
+is typed."
+  (when (and block-comment-start block-comment-end
+             ;; Check if we are exactly behind a `block-comment-start'
+             (save-excursion
+               (save-match-data
+                 (re-search-backward (regexp-quote block-comment-start)
+                                     (- (point) (length block-comment-start))
+                                     t)))
+             ;; And if there is not anything front us
+             (looking-at-p (concat "[^[:space:]]")))
+    (insert " ")
+    (save-excursion
+      (insert (concat " " block-comment-end)))))
+
+(define-minor-mode electric-block-comment-mode
+  "Toggle automatic closing of block comments (Electric Block Comment mode).
+
+When enabled, typing `block-comment-start' closes it inserting their
+corresponding `block-comment-end'."
+  :group 'electricity
+  :version "31.1"
+  (if electric-block-comment-mode
+      (add-hook 'post-self-insert-hook #'electric-block-comment-post-self-insert-function 10 t)
+    (remove-hook 'post-self-insert-hook #'electric-block-comment-post-self-insert-function t)))
+
 (provide 'electric)
 
 ;;; electric.el ends here
-- 
2.49.0

-- 
                                          - E.G via GNU Emacs and Org.