GNU bug report logs - #78053
[PATCH] Support strings for electric-pairs.

Previous Next

Package: emacs;

Reported by: Elijah Gabe Pérez <eg642616 <at> gmail.com>

Date: Fri, 25 Apr 2025 00:31:01 UTC

Severity: normal

Tags: patch

To reply to this bug, email your comments to 78053 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to joaotavora <at> gmail.com, bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Fri, 25 Apr 2025 00:31:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Elijah Gabe Pérez <eg642616 <at> gmail.com>:
New bug report received and forwarded. Copy sent to joaotavora <at> gmail.com, bug-gnu-emacs <at> gnu.org. (Fri, 25 Apr 2025 00:31:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Elijah Gabe Pérez <eg642616 <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Support strings for electric-pairs.
Date: Thu, 24 Apr 2025 18:30:10 -0600
[Message part 1 (text/plain, inline)]
Tags: patch

As discussed in bug#77823, this feature bring support for
using strings as pairs in `electric-pair-mode', the advantage is that
this can be used for: Auto-close block comments, close conditional
constructs such as `case esac' from bash and whatever.

This is currently a working prototype (only inserts the pairs).
I would like to hear some comments or suggestions of this implementation.

Thanks.
[0001-Support-strings-for-electric-pairs.-bug.patch (text/patch, attachment)]
[Message part 3 (text/plain, inline)]
-- 
                                          - E.G via GNU Emacs and Org.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sat, 10 May 2025 09:22:02 GMT) Full text and rfc822 format available.

Message #8 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Elijah Gabe Pérez <eg642616 <at> gmail.com>,
 joaotavora <at> gmail.com
Cc: 78053 <at> debbugs.gnu.org
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sat, 10 May 2025 12:21:28 +0300
> Cc: João Távora <joaotavora <at> gmail.com>
> From: Elijah Gabe Pérez <eg642616 <at> gmail.com>
> Date: Thu, 24 Apr 2025 18:30:10 -0600
> 
> As discussed in bug#77823, this feature bring support for
> using strings as pairs in `electric-pair-mode', the advantage is that
> this can be used for: Auto-close block comments, close conditional
> constructs such as `case esac' from bash and whatever.
> 
> This is currently a working prototype (only inserts the pairs).
> I would like to hear some comments or suggestions of this implementation.

João, any comments?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sat, 24 May 2025 08:52:02 GMT) Full text and rfc822 format available.

Message #11 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: joaotavora <at> gmail.com
Cc: eg642616 <at> gmail.com, 78053 <at> debbugs.gnu.org
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sat, 24 May 2025 11:51:04 +0300
Ping!

> Cc: 78053 <at> debbugs.gnu.org
> Date: Sat, 10 May 2025 12:21:28 +0300
> From: Eli Zaretskii <eliz <at> gnu.org>
> 
> > Cc: João Távora <joaotavora <at> gmail.com>
> > From: Elijah Gabe Pérez <eg642616 <at> gmail.com>
> > Date: Thu, 24 Apr 2025 18:30:10 -0600
> > 
> > As discussed in bug#77823, this feature bring support for
> > using strings as pairs in `electric-pair-mode', the advantage is that
> > this can be used for: Auto-close block comments, close conditional
> > constructs such as `case esac' from bash and whatever.
> > 
> > This is currently a working prototype (only inserts the pairs).
> > I would like to hear some comments or suggestions of this implementation.
> 
> João, any comments?
> 
> 
> 
> 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sat, 24 May 2025 17:35:02 GMT) Full text and rfc822 format available.

Message #14 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: João Távora <joaotavora <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Elijah G <eg642616 <at> gmail.com>, 78053 <at> debbugs.gnu.org
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sat, 24 May 2025 18:34:10 +0100
[Message part 1 (text/plain, inline)]
I already expressed my misgivings/opinion/ignorance about this in
bug#77823, no? Or am I figured and this a different matter altogether?

Just to clarify, electric-pair-mode already existed when I touched it and
it did some arguably interesting but not particularly stuff. I just added
the pair-based-on-syntax-while-helping-balance feature. This, as far as
I can tell, is a somewhat distinct area that isn't in my expertise.

My advice is to call Stefan.

João

On Sat, May 24, 2025, 09:51 Eli Zaretskii <eliz <at> gnu.org> wrote:

> Ping!
>
> > Cc: 78053 <at> debbugs.gnu.org
> > Date: Sat, 10 May 2025 12:21:28 +0300
> > From: Eli Zaretskii <eliz <at> gnu.org>
> >
> > > Cc: João Távora <joaotavora <at> gmail.com>
> > > From: Elijah Gabe Pérez <eg642616 <at> gmail.com>
> > > Date: Thu, 24 Apr 2025 18:30:10 -0600
> > >
> > > As discussed in bug#77823, this feature bring support for
> > > using strings as pairs in `electric-pair-mode', the advantage is that
> > > this can be used for: Auto-close block comments, close conditional
> > > constructs such as `case esac' from bash and whatever.
> > >
> > > This is currently a working prototype (only inserts the pairs).
> > > I would like to hear some comments or suggestions of this
> implementation.
> >
> > João, any comments?
> >
> >
> >
> >
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sun, 25 May 2025 06:28:02 GMT) Full text and rfc822 format available.

Message #17 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: João Távora <joaotavora <at> gmail.com>,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: eg642616 <at> gmail.com, 78053 <at> debbugs.gnu.org
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sun, 25 May 2025 09:27:12 +0300
> From: João Távora <joaotavora <at> gmail.com>
> Date: Sat, 24 May 2025 18:34:10 +0100
> Cc: Elijah G <eg642616 <at> gmail.com>, 78053 <at> debbugs.gnu.org
> 
> I already expressed my misgivings/opinion/ignorance about this in bug#77823, no? Or am I figured and this
> a different matter altogether? 

I cannot find your comments in that bug.  Could you reiterate your
opinions and comments?

> Just to clarify, electric-pair-mode already existed when I touched it and it did some arguably interesting but
> not particularly stuff. I just added the pair-based-on-syntax-while-helping-balance feature. This, as far as I
> can tell, is a somewhat distinct area that isn't in my expertise.
> 
> My advice is to call Stefan.

Stefan, WDYT about this proposal?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sun, 25 May 2025 13:45:02 GMT) Full text and rfc822 format available.

Message #20 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Elijah Gabe Pérez <eg642616 <at> gmail.com>
Cc: 78053 <at> debbugs.gnu.org,
 João Távora <joaotavora <at> gmail.com>
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sun, 25 May 2025 09:44:45 -0400
> As discussed in bug#77823, this feature bring support for
> using strings as pairs in `electric-pair-mode',

Thank you for looking into it.  I think this would be a nice addition to
`electric-pair-mode`.

> +---
> +** Electric pair mode now supports using strings as pairs.
> +Now 'electric-pair-mode' can use strings as pairs,
> +TODO: I don't know what to put here

The second line just repeats the first, so you can just remove it.
In general, this seems sufficient, tho maybe we should clarify the
"meaning" of using strings there.  IOW, I'd say something like:

    ** Electric pair mode supports multicharacter paired delimiters.
    This is done by supporting strings in 'electric-pair-pairs'.

> @@ -41,7 +41,9 @@ electric-pair-pairs
>  See also the variable `electric-pair-text-pairs'."
>    :version "24.1"
>    :group 'electricity
> -  :type '(repeat (cons character character)))
> +  :type '(repeat
> +          (choice (cons character character)
> +                  (cons string string))))

We want to do the same for `electric-pair-text-pairs`, no?

> @@ -258,7 +260,18 @@ electric-pair-syntax-info
>       (direct (if (eq (car direct) (cdr direct))
>                   (list ?\" command-event t string-or-comment)
>                 (list ?\( (cdr direct) t string-or-comment)))
> -     (reverse (list ?\) (car reverse) t string-or-comment)))))
> +     (reverse (list ?\) (car reverse) t string-or-comment))
> +     ;; The if-let here is for avoid unnecessary looking-back computation
> +     ((if-let* ((str-pair
> +		 (cl-loop for pairs in fallback
> +			  do
> +			  (if (and
> +			       (stringp (car pairs))
> +			       (looking-back
> +				(regexp-quote (car pairs))
> +				(- (point) (length (car pairs)))))
> +			      (cl-return (cdr pairs))))))
> +	  (list 'str str-pair nil nil))))))

BTW, here you could use `compare-buffer-substrings`.

And you can get rid of the `if-let*` altogether by doing:

     (t (cl-loop
         for pairs in fallback
         do (when {...}
              (cl-return (list 'str ...))))))))

>  (defun electric-pair--insert (char times)
>    (let ((last-command-event char)
> @@ -591,6 +604,11 @@ electric-pair-post-self-insert-function
>                                pos))
>           (forward-char num))
>          ;; Insert matching pair.
> +        ;; String pairs
> +        ((and (eq syntax 'str) (not overwrite-mode))
> +         (insert " ")
> +         (save-excursion (insert " " pair)))
> +        ;; Char pairs
>          ((and (memq syntax '(?\( ?\" ?\$))
>                (not overwrite-mode)
>                (or unconditional

I can imagine some multi-char delimiters which don't call for spaces
inside of them (e.g. LaTeX's ``...''), so maybe the entries in
`electric-pair-pairs` should have some extra info about it.
I suggest we replace the (STRING . STRING) format with
(STRING STRING ...) so we can put extra info in the `...`.


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Sun, 25 May 2025 17:21:02 GMT) Full text and rfc822 format available.

Message #23 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Rudolf Schlatte <rudi <at> constantly.at>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sun, 25 May 2025 19:20:08 +0200
Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
text editors" <bug-gnu-emacs <at> gnu.org> writes:

>>  (defun electric-pair--insert (char times)
>>    (let ((last-command-event char)
>> @@ -591,6 +604,11 @@ electric-pair-post-self-insert-function
>>                                pos))
>>           (forward-char num))
>>          ;; Insert matching pair.
>> +        ;; String pairs
>> +        ((and (eq syntax 'str) (not overwrite-mode))
>> +         (insert " ")
>> +         (save-excursion (insert " " pair)))
>> +        ;; Char pairs
>>          ((and (memq syntax '(?\( ?\" ?\$))
>>                (not overwrite-mode)
>>                (or unconditional
>
> I can imagine some multi-char delimiters which don't call for spaces
> inside of them (e.g. LaTeX's ``...''), so maybe the entries in
> `electric-pair-pairs` should have some extra info about it.
> I suggest we replace the (STRING . STRING) format with
> (STRING STRING ...) so we can put extra info in the `...`.

See for example Java's text blocks:

    The opening delimiter is a sequence of three double quote characters
    (""") followed by zero or more white spaces followed by a line
    terminator. The content begins at the first character after the line
    terminator of the opening delimiter.

    The closing delimiter is a sequence of three double quote
    characters. The content ends at the last character before the first
    double quote of the closing delimiter.

(https://openjdk.org/jeps/378) -- it would be nifty if the mandatory
linebreak was inserted automatically.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Mon, 26 May 2025 01:18:02 GMT) Full text and rfc822 format available.

Message #26 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Elijah Gabe Pérez <eg642616 <at> gmail.com>
To: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
 text editors" <bug-gnu-emacs <at> gnu.org>
Cc: 78053 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>,
 João Távora <joaotavora <at> gmail.com>
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Sun, 25 May 2025 19:16:56 -0600
[Message part 1 (text/plain, inline)]
Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of
text editors" <bug-gnu-emacs <at> gnu.org> writes:

>> @@ -41,7 +41,9 @@ electric-pair-pairs
>>  See also the variable `electric-pair-text-pairs'."
>>    :version "24.1"
>>    :group 'electricity
>> -  :type '(repeat (cons character character)))
>> +  :type '(repeat
>> +          (choice (cons character character)
>> +                  (cons string string))))
>
> We want to do the same for `electric-pair-text-pairs`, no?
>

Yes, I forgot it.

>> @@ -258,7 +260,18 @@ electric-pair-syntax-info
>>       (direct (if (eq (car direct) (cdr direct))
>>                   (list ?\" command-event t string-or-comment)
>>                 (list ?\( (cdr direct) t string-or-comment)))
>> -     (reverse (list ?\) (car reverse) t string-or-comment)))))
>> +     (reverse (list ?\) (car reverse) t string-or-comment))
>> +     ;; The if-let here is for avoid unnecessary looking-back computation
>> +     ((if-let* ((str-pair
>> +		 (cl-loop for pairs in fallback
>> +			  do
>> +			  (if (and
>> +			       (stringp (car pairs))
>> +			       (looking-back
>> +				(regexp-quote (car pairs))
>> +				(- (point) (length (car pairs)))))
>> +			      (cl-return (cdr pairs))))))
>> +	  (list 'str str-pair nil nil))))))
>
> BTW, here you could use `compare-buffer-substrings`.

Thanks, Can you explain how to use it in this case?  AFAIK
`compare-buffer-substrings' needs 2 strings in the buffer for the
comparison.

>>  (defun electric-pair--insert (char times)
>>    (let ((last-command-event char)
>> @@ -591,6 +604,11 @@ electric-pair-post-self-insert-function
>>                                pos))
>>           (forward-char num))
>>          ;; Insert matching pair.
>> +        ;; String pairs
>> +        ((and (eq syntax 'str) (not overwrite-mode))
>> +         (insert " ")
>> +         (save-excursion (insert " " pair)))
>> +        ;; Char pairs
>>          ((and (memq syntax '(?\( ?\" ?\$))
>>                (not overwrite-mode)
>>                (or unconditional
>
> I can imagine some multi-char delimiters which don't call for spaces
> inside of them (e.g. LaTeX's ``...''), so maybe the entries in
> `electric-pair-pairs` should have some extra info about it.
> I suggest we replace the (STRING . STRING) format with
> (STRING STRING ...) so we can put extra info in the `...`.

Are you sure it's good idea to replace the cons cell form with a list?
I've added the list form, but it coexists with the cons cell to avoid
any future bug.

[0001-Support-strings-for-electric-pairs.-bug-78053.patch (text/x-patch, attachment)]
[Message part 3 (text/plain, inline)]
-- 
                                          - E.G via GNU Emacs and Org.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Mon, 26 May 2025 01:18:03 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78053; Package emacs. (Mon, 26 May 2025 16:04:01 GMT) Full text and rfc822 format available.

Message #32 received at 78053 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Elijah Gabe Pérez <eg642616 <at> gmail.com>
Cc: 78053 <at> debbugs.gnu.org,
 João Távora <joaotavora <at> gmail.com>
Subject: Re: bug#78053: [PATCH] Support strings for electric-pairs.
Date: Mon, 26 May 2025 12:03:00 -0400
>>> +     ((if-let* ((str-pair
>>> +		 (cl-loop for pairs in fallback
>>> +			  do
>>> +			  (if (and
>>> +			       (stringp (car pairs))
>>> +			       (looking-back
>>> +				(regexp-quote (car pairs))
>>> +				(- (point) (length (car pairs)))))
>>> +			      (cl-return (cdr pairs))))))
>>> +	  (list 'str str-pair nil nil))))))
>>
>> BTW, here you could use `compare-buffer-substrings`.
>
> Thanks, Can you explain how to use it in this case?  AFAIK
> `compare-buffer-substrings' needs 2 strings in the buffer for the
> comparison.

Duh, you're right, it can't be used here.
We should probably extend `compare-strings` to accept buffer arguments
and then obsolete `compare-buffer-substrings'.

>>>  (defun electric-pair--insert (char times)
>>>    (let ((last-command-event char)
>>> @@ -591,6 +604,11 @@ electric-pair-post-self-insert-function
>>>                                pos))
>>>           (forward-char num))
>>>          ;; Insert matching pair.
>>> +        ;; String pairs
>>> +        ((and (eq syntax 'str) (not overwrite-mode))
>>> +         (insert " ")
>>> +         (save-excursion (insert " " pair)))
>>> +        ;; Char pairs
>>>          ((and (memq syntax '(?\( ?\" ?\$))
>>>                (not overwrite-mode)
>>>                (or unconditional
>>
>> I can imagine some multi-char delimiters which don't call for spaces
>> inside of them (e.g. LaTeX's ``...''), so maybe the entries in
>> `electric-pair-pairs` should have some extra info about it.
>> I suggest we replace the (STRING . STRING) format with
>> (STRING STRING ...) so we can put extra info in the `...`.
>
> Are you sure it's good idea to replace the cons cell form with a list?

We need to keep supporting the (CHAR . CHAR) form, of course, but yes,
I think using (STRING STRING . PROPERTIES) is a better option than
(STRING . STRING) and there is no backward compatibility need to
support (STRING . STRING).


        Stefan





This bug report was last modified 10 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.