Subject: Re: bug#77823: 31.0.50; M-j regression since commit 4c6b1712a4d

From: Elijah Gabe Pérez <[email-masked]>
Date: Sun, 20 Apr 2025 14:40:05 -0600

Fine, I'm sending here 2 patches:

For revert the previous commits:
>From 43ffcb0c9f079e0916194c74b13c4b40b5706749 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Fri, 18 Apr 2025 21:27:29 -0600
Subject: [PATCH 1/2] Revert commits 74842b4cb2c and 4c6b1712a4d.

---
 lisp/emacs-lisp/lisp-mode.el         | 2 --
 lisp/newcomment.el                   | 9 ++++-----
 lisp/nxml/nxml-mode.el               | 2 --
 lisp/progmodes/c-ts-common.el        | 4 ----
 lisp/progmodes/cc-cmds.el            | 1 -
 lisp/progmodes/cc-mode.el            | 2 --
 lisp/progmodes/go-ts-mode.el         | 2 --
 lisp/progmodes/js.el                 | 2 --
 lisp/progmodes/json-ts-mode.el       | 2 --
 lisp/progmodes/lua-ts-mode.el        | 2 --
 lisp/progmodes/opascal.el            | 4 +---
 lisp/progmodes/pascal.el             | 2 --
 lisp/progmodes/typescript-ts-mode.el | 2 --
 lisp/textmodes/css-mode.el           | 4 ----
 lisp/textmodes/sgml-mode.el          | 2 --
 15 files changed, 5 insertions(+), 37 deletions(-)

diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el
index f27baae1b36..006b713ae6e 100644
--- a/lisp/emacs-lisp/lisp-mode.el
+++ b/lisp/emacs-lisp/lisp-mode.el
@@ -828,8 +828,6 @@ lisp-mode
 	      "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
   (setq-local comment-end-skip "[ \t]*\\(\\s>\\||#\\)")
   (setq-local font-lock-comment-end-skip "|#")
-  (setq-local block-comment-start "#|")
-  (setq-local block-comment-end "|#")
   (setq imenu-case-fold-search t))
 
 (defun lisp-find-tag-default ()
diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index eb36f91104d..945187e863f 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -56,6 +56,7 @@
 ;; - spill auto-fill of comments onto the end of the next line.
 ;; - uncomment-region with a consp (for blocks) or somehow make the
 ;;   deletion of continuation markers less dangerous.
+;; - drop block-comment-<foo> unless it's really used.
 ;; - uncomment-region on a subpart of a comment.
 ;; - support gnu-style "multi-line with space in continue".
 ;; - somehow allow comment-dwim to use the region even if transient-mark-mode
@@ -182,11 +183,9 @@ uncomment-region-function
 Applicable at least in modes for languages like fixed-format Fortran where
 comments always start in column zero.")
 
-(defvar block-comment-start nil
-  "String to insert to start a new block comment, or nil if no supported.")
-
-(defvar block-comment-end nil
-  "String to insert to end a new block comment, or nil if no supported.")
+;; ?? never set
+(defvar block-comment-start nil)
+(defvar block-comment-end nil)
 
 (defvar comment-quote-nested t
   "Non-nil if nested comments should be quoted.
diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el
index 994b2779d1a..7acc19b9058 100644
--- a/lisp/nxml/nxml-mode.el
+++ b/lisp/nxml/nxml-mode.el
@@ -529,8 +529,6 @@ nxml-mode
   (setq-local comment-start-skip "<!--[ \t\r\n]*")
   (setq-local comment-end "-->")
   (setq-local comment-end-skip "[ \t\r\n]*-->")
-  (setq-local block-comment-start "<!--")
-  (setq-local block-comment-end "-->")
   (setq-local comment-line-break-function #'nxml-newline-and-indent)
   (setq-local comment-quote-nested-function #'nxml-comment-quote-nested)
   (setq-local comment-continue "") ; avoid double-hyphens as a padding
diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el
index 56c8ae49000..5f9d485538a 100644
--- a/lisp/progmodes/c-ts-common.el
+++ b/lisp/progmodes/c-ts-common.el
@@ -282,8 +282,6 @@ c-ts-common-comment-setup
  - `comment-end'
  - `comment-start-skip'
  - `comment-end-skip'
- - `block-comment-start'
- - `block-comment-end'
  - `adaptive-fill-mode'
  - `adaptive-fill-first-line-regexp'
  - `paragraph-start'
@@ -300,8 +298,6 @@ c-ts-common-comment-setup
               (rx (* (syntax whitespace))
                   (group (or (syntax comment-end)
                              (seq (+ "*") "/")))))
-  (setq-local block-comment-start "/*")
-  (setq-local block-comment-end "*/")
   (setq-local adaptive-fill-mode t)
   (setq-local adaptive-fill-function #'c-ts-common--adaptive-fill-prefix)
   ;; Always accept * or | as prefix, even if there's only one line in
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index 06397d23321..9230faa56da 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -5015,7 +5015,6 @@ c-indent-new-comment-line
 		      (setq comment-start "/* " comment-end " */"))
 		  (unless (string-match "[ \t]*//" comment-start)
 		    (setq comment-start "// " comment-end "")))
-		(setq block-comment-start "/*" block-comment-end "*/")
 		(setq col (save-excursion
 			    (back-to-indentation)
 			    (current-column)))
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index fa3788ba383..efeb6c1005a 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -610,8 +610,6 @@ c-basic-common-init
   (make-local-variable 'comment-start)
   (make-local-variable 'comment-end)
   (make-local-variable 'comment-start-skip)
-  (make-local-variable 'block-comment-start)
-  (make-local-variable 'block-comment-end)
 
   (make-local-variable 'paragraph-start)
   (make-local-variable 'paragraph-separate)
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index e1736fe9208..8cabaa9f34d 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -692,8 +692,6 @@ go-work-ts-mode
     (setq-local comment-start "// ")
     (setq-local comment-end "")
     (setq-local comment-start-skip (rx "//" (* (syntax whitespace))))
-    (setq-local block-comment-start "/*")
-    (setq-local block-comment-end "*/")
 
     ;; Indent.
     (setq-local indent-tabs-mode t
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 243329de7ae..498ce955c44 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3781,8 +3781,6 @@ js--mode-setup
   (setq-local comment-start "// ")
   (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end "")
-  (setq-local block-comment-start "/*")
-  (setq-local block-comment-end "*/")
   (setq-local fill-paragraph-function #'js-fill-paragraph)
   (setq-local normal-auto-fill-function #'js-do-auto-fill)
 
diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el
index 9d374119810..11ec5d5c079 100644
--- a/lisp/progmodes/json-ts-mode.el
+++ b/lisp/progmodes/json-ts-mode.el
@@ -142,8 +142,6 @@ json-ts-mode
   (setq-local comment-start "// ")
   (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end "")
-  (setq-local block-comment-start "/*")
-  (setq-local block-comment-end "*/")
 
   ;; Electric
   (setq-local electric-indent-chars
diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el
index dc129277778..8c2ff2ae7e0 100644
--- a/lisp/progmodes/lua-ts-mode.el
+++ b/lisp/progmodes/lua-ts-mode.el
@@ -673,8 +673,6 @@ lua-ts-mode
     (setq-local comment-start "--")
     (setq-local comment-start-skip "--\\s-*")
     (setq-local comment-end "")
-    (setq-local block-comment-start "--[[" )
-    (setq-local block-comment-end "]]")
 
     ;; Font-lock.
     (setq-local treesit-font-lock-settings lua-ts--font-lock-settings)
diff --git a/lisp/progmodes/opascal.el b/lisp/progmodes/opascal.el
index 9dcaff9645e..bd6bc3b28ac 100644
--- a/lisp/progmodes/opascal.el
+++ b/lisp/progmodes/opascal.el
@@ -1767,9 +1767,7 @@ opascal-mode
 
   (setq-local comment-start "// ")
   (setq-local comment-start-skip "\\(?://\\|(\\*\\|{\\)[ \t]*")
-  (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*)\\|}\\)")
-  (setq-local block-comment-start "(*")
-  (setq-local block-comment-end "*)"))
+  (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*)\\|}\\)"))
 
 (provide 'opascal)
 ;;; opascal.el ends here
diff --git a/lisp/progmodes/pascal.el b/lisp/progmodes/pascal.el
index 113cf68c8d6..b6316d4dfe1 100644
--- a/lisp/progmodes/pascal.el
+++ b/lisp/progmodes/pascal.el
@@ -348,8 +348,6 @@ pascal-mode
   (setq-local comment-start "{")
   (setq-local comment-start-skip "(\\*+ *\\|{ *")
   (setq-local comment-end "}")
-  (setq-local block-comment-start "(*")
-  (setq-local block-comment-end "*)")
   (add-hook 'completion-at-point-functions 'pascal-completions-at-point nil t)
   ;; Font lock support
   (setq-local font-lock-defaults '(pascal-font-lock-keywords nil t))
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index a2049ba2d64..e914dabfd16 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -665,8 +665,6 @@ tsx-ts-mode
     ;; Comments.
     (setq-local comment-start "// ")
     (setq-local comment-end "")
-    (setq-local block-comment-start "/*")
-    (setq-local block-comment-end "*/")
     (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
                                            (seq "/" (+ "*")))
                                        (* (syntax whitespace))))
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index e3f295c3412..b48c98efa02 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -1858,8 +1858,6 @@ css-base-mode
   (setq-local comment-start-skip "/\\*+[ \t]*")
   (setq-local comment-end "*/")
   (setq-local comment-end-skip "[ \t]*\\*+/")
-  (setq-local block-comment-start "/*")
-  (setq-local block-comment-end "*/")
   (setq-local electric-indent-chars
               (append css-electric-keys electric-indent-chars))
   ;; The default "." creates ambiguity with class selectors.
@@ -2083,8 +2081,6 @@ scss-mode
   "Major mode to edit \"Sassy CSS\" files."
   (setq-local comment-start "// ")
   (setq-local comment-end "")
-  (setq-local block-comment-start "/*")
-  (setq-local block-comment-end "*/")
   (setq-local comment-continue " *")
   (setq-local comment-start-skip "/[*/]+[ \t]*")
   (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)")
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 4dbc6839c68..2accd31bc36 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -622,8 +622,6 @@ sgml-mode
   (setq-local indent-line-function #'sgml-indent-line)
   (setq-local comment-start "<!-- ")
   (setq-local comment-end " -->")
-  (setq-local block-comment-start "<!--")
-  (setq-local block-comment-end "-->")
   (setq-local comment-indent-function #'sgml-comment-indent)
   (setq-local comment-line-break-function #'sgml-comment-indent-new-line)
   (setq-local skeleton-further-elements '((completion-ignore-case t)))
-- 
2.49.0

And the new feature implementation:
>From 5305a92c89ce37d44ac01dd273933418a43dd92f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Sat, 19 Apr 2025 23:17:45 -0600
Subject: [PATCH] Change electric-block-comment-mode to
 electric-multichar-pair-mode.

* etc/NEWS: Change `electric-block-comment-mode' entry to
`electric-multichar-pair-mode'.
* lisp/electric.el
(electric-block-comment-post-self-insert-function)
(electric-block-comment-mode): Move and rename code...
* lisp/elec-pair.el
(electric-multichar-pair-post-self-insert-function)
(electric-multichar-pair-mode): ... To this.
(electric-multichar-pairs): New buffer-local variable.
---
 etc/NEWS          | 11 +++--------
 lisp/elec-pair.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
 lisp/electric.el  | 32 --------------------------------
 3 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 98f8e703013..53c68cb9045 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1959,14 +1959,9 @@ A major mode based on the tree-sitter library for editing "go.work"
 files.  If tree-sitter is properly set-up by the user, it can be
 enabled for files named "go.work".
 
-** New minor mode 'electric-block-comment-mode'.
-This mode automatically closes block comments.  Typing the value of
-'block-comment-start' closes it inserting the 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'.
+** New minor mode 'electric-multichar-pair-mode'.
+This minor mode provides automatic pairing for multi-character pairs.
+The pairs are defined via the variable `electric-multichar-pairs'.
 
 
 * Incompatible Lisp Changes in Emacs 31.1
diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index aa2577300fd..db25a9ddb23 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -700,6 +700,50 @@ electric-pair-local-mode
     (setq-default electric-pair-mode nil) ; But keep it globally disabled.
     )))
 
+;;; Electric multichar pairing.
+
+;; `electric-multichar-pair-post-self-insert-function'
+(defvar-local electric-multichar-pairs nil
+  "Alist which contains multichar pairs to insert `electric-multichar-pair-mode'.
+Each element must be in the form:
+ (START . END)
+Where START and END are respectively pair start and end.")
+
+(defun electric-multichar-pair-post-self-insert-function ()
+  "Function that `electric-multichar-pair' adds to `post-self-insert-hook'.
+This closes pairs according to `electric-multichar-pairs'."
+  (when-let* ((pair-ender ; Check if we are in any pair starter from `electric-multichar-pairs' ...
+               (cl-loop for pairs in electric-multichar-pairs
+                        do
+                        (if (save-excursion
+                              (save-match-data
+                                (re-search-backward
+                                 (regexp-quote (car pairs))
+                                 (- (point) (length (car pairs)))
+                                 t)))
+                            (cl-return (cdr pairs)))))
+              ;; ... and if there is not anything front us.
+              ((null (looking-at-p (concat "[[:space:]]\\|"
+                                           (regexp-quote pair-ender))))))
+    (let (electric-pair-mode ; Avoid conflicts with other electric modes
+          electric-quote-mode)
+      (insert " ")
+      (save-excursion
+        (insert (concat " " pair-ender))))))
+
+(define-minor-mode electric-multichar-pair-mode
+  "Toggle automatic pairing of multicharacter pairs (Electric Multichar mode).
+
+When enabled, typing the beginning of any pair in `electric-multichar-pairs'
+closes it inserting their corresponding ender."
+  :group 'electricity
+  :version "31.1"
+  (if (and electric-multichar-pair-mode (consp electric-multichar-pairs))
+      (add-hook 'post-self-insert-hook
+                #'electric-multichar-pair-post-self-insert-function 10 t)
+    (remove-hook 'post-self-insert-hook
+                 #'electric-multichar-pair-post-self-insert-function t)))
+
 (provide 'elec-pair)
 
 ;;; elec-pair.el ends here
diff --git a/lisp/electric.el b/lisp/electric.el
index da5fa973757..39e13e1ca0c 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -731,38 +731,6 @@ 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.