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

From: Elijah Gabe Pérez <[email-masked]>
Date: Mon, 17 Mar 2025 13:40:24 -0600

Hello,

This minor automatically lets you close block comment when typing
a block comment start.

Thus...
#+begin_src
  typing /* in c-mode
  automatically inserts */.

  electric-block-comment-mode: on

  /*|
    ^ cursor

  /* | */
     ^ cursor

  Or in lua --[[ inserts ]]--.

  electric-block-comment-mode: on

  --[[|
      ^ cursor

  --[[ | ]]--
       ^ cursor
#+end_src

and so on.

*However, there is a problem,*
This minor mode uses =block-comment-start= and =block-comment-end=
variables, which most of the major modes do not use or define.

The reason I decided to use these variables is because are more explicit
with the type of comment that is used/supported.

I would like to hear your feedback.

>From 45414d4b0d47ddd9300fcc24723e0aa5e4efc8cb 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 automatically lets you close block comment when typing
a block comment start.  Thus, typing "/*" in c-mode automatically
inserts "*/".

* 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         |  4 ++++
 lisp/electric.el | 28 ++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index b33f7cec73f..0924e86f691 100644
--- 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 "*/".
+
 ** 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..c561d168883 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -731,6 +731,34 @@ 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 when a block comment start is typed."
+  (when (and block-comment-start block-comment-end
+             ;; Check if we are exactly behind the block comment start
+             (save-excursion
+               (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 " */"))))
+
+
+(define-minor-mode electric-block-comment-mode
+  "Toggle automatic close comment blocks (Electric Comment mode).
+
+When enabled, typing `block-comment-start' closes it inserting their
+corresponding `block-comment-end'."
+  :group 'electricity
+  (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.48.1

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