Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.

From: Elijah Gabe Pérez <[email-masked]>
Date: Sun, 08 Jun 2025 12:24:57 -0600

Eli Zaretskii <[email-masked]> writes:

> Is the last patch okay to install?  If not, Elijah, would you please
> submit an updated patch?

Sure, I'm sending here a better version.

I made a few minor changes since I found several bugs.
If there are no objections, the patch is ready to be installed.

>From c4454a65b48ec33a1a22186ab4c3767d1496e618 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Thu, 24 Apr 2025 17:34:58 -0600
Subject: [PATCH] Support strings for electric-pairs.  (Bug#78053)

This add support for string pairs in electric-pairs-mode.  For
example, typing '/*' in c-mode and derived will auto insert
' */', or in sh-mode 'case ' inserts 'esac'.

* lisp/elec-pair.el (electric-pair-pairs): Add new defcustom
types.
(electric-pair-syntax-info)
(electric-pair-post-self-insert-function): Add support for
strings.
* etc/NEWS: Announce changes.
---
 etc/NEWS          |  4 ++++
 lisp/elec-pair.el | 42 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index ade5abc43fc..bd514b110d8 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -527,6 +527,10 @@ You can now insert or wrap text with multiple sets of parentheses and
 other matching delimiters at once with Electric Pair mode, by providing
 a prefix argument when inserting one of the delimiters.
 
+---
+** Electric pair mode now supports multi-character paired delimiters.
+This is done by supporting strings in electric-pair-pairs.
+
 +++
 ** You can now use 'M-~' during 'C-x s' ('save-some-buffers').
 Typing 'M-~' while saving some buffers means not to save the buffer and
diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index aa2577300fd..0481d0d467b 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -38,10 +38,16 @@ electric-pair-pairs
 no syntax relevant to `electric-pair-mode' in the mode's syntax
 table.
 
+NOTE: In string pairs, the first pair is a regular expression.
+
 See also the variable `electric-pair-text-pairs'."
   :version "24.1"
   :group 'electricity
-  :type '(repeat (cons character character)))
+  :type '(repeat
+          (choice (cons :tag "Characters" character character)
+                  (cons :tag "Strings" string string)
+                  (list :tag "Strings and insert space in pairs"
+                        string string boolean))))
 
 (defcustom electric-pair-text-pairs
   `((?\" . ?\")
@@ -51,10 +57,16 @@ electric-pair-text-pairs
 
 Pairs of delimiters in this list are a fallback in case they have
 no syntax relevant to `electric-pair-mode' in the syntax table
-defined in `electric-pair-text-syntax-table'."
+defined in `electric-pair-text-syntax-table'.
+
+NOTE: In string pairs, the first pair is a regular expression."
   :version "24.4"
   :group 'electricity
-  :type '(repeat (cons character character)))
+  :type '(repeat
+          (choice (cons :tag "Characters" character character)
+                  (cons :tag "Strings" string string)
+                  (list :tag "Strings and insert space in pairs"
+                        string string boolean))))
 
 (defcustom electric-pair-skip-self #'electric-pair-default-skip-self
   "If non-nil, skip char instead of inserting a second closing paren.
@@ -252,6 +264,22 @@ electric-pair-syntax-info
          (direct (assq command-event fallback))
          (reverse (rassq command-event fallback)))
     (cond
+     ((cl-loop
+       for pairs in fallback
+       if (and
+	   (stringp (car pairs))
+	   (looking-back (car pairs) (pos-bol)))
+         return (list
+                 'str
+                 ;; Get pair ender
+                 (if (proper-list-p pairs)
+                     (nth 1 pairs)
+                   (cdr pairs))
+                 nil
+                 ;; Check if pairs have to insert a space after
+                 ;; first pair was inserted.
+                 (if (proper-list-p pairs)
+                     (nth 2 pairs)))))
      ((memq (car table-syntax-and-pair)
             '(?\" ?\( ?\) ?\$))
       (append table-syntax-and-pair (list nil string-or-comment)))
@@ -533,7 +561,7 @@ electric-pair-post-self-insert-function
          (beg (when num (- pos num)))
          (skip-whitespace-info))
     (pcase (electric-pair-syntax-info last-command-event)
-      (`(,syntax ,pair ,unconditional ,_)
+      (`(,syntax ,pair ,unconditional ,space)
        (cond
         ((null pos) nil)
         ((zerop num) nil)
@@ -591,6 +619,12 @@ electric-pair-post-self-insert-function
                               pos))
          (forward-char num))
         ;; Insert matching pair.
+        ;; String pairs
+        ((and (eq syntax 'str) (not overwrite-mode))
+         (if space (insert " "))
+         (save-excursion
+           (insert pair)))
+        ;; Char pairs
         ((and (memq syntax '(?\( ?\" ?\$))
               (not overwrite-mode)
               (or unconditional
-- 
2.49.0

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