GNU bug report logs - #66155
'electric-pair-inhibit-if-helps-balance' has side-effects

Previous Next

Package: emacs;

Reported by: Akib Azmain Turja <akib <at> disroot.org>

Date: Fri, 22 Sep 2023 11:53:01 UTC

Severity: normal

To reply to this bug, email your comments to 66155 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 bug-gnu-emacs <at> gnu.org:
bug#66155; Package emacs. (Fri, 22 Sep 2023 11:53:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Akib Azmain Turja <akib <at> disroot.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 22 Sep 2023 11:53:02 GMT) Full text and rfc822 format available.

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

From: Akib Azmain Turja <akib <at> disroot.org>
To: bug-gnu-emacs <bug-gnu-emacs <at> gnu.org>
Cc: Daniel Mendler <mail <at> daniel-mendler.de>
Subject: 'electric-pair-inhibit-if-helps-balance' has side-effects
Date: Fri, 22 Sep 2023 17:51:55 +0600
[Message part 1 (text/plain, inline)]
With the default configuration (which uses
'electric-pair-inhibit-if-helps-balance'), inserting parens at the end
of a Tempel field doesn't extend the field.

I think the modify+undo magic in electric-pair-inhibit-if-helps-balance
moves markers.

The following is the solution that works for me atleast; it uses insert
to insert back the character, and let-bind buffer-undo-list to not
clobber the undo list.

--8<---------------cut here---------------start------------->8---
(defun *electric-pair-inhibit-if-helps-balance (char)
  "Return non-nil if auto-pairing of CHAR would hurt parens' balance.

Works by first removing the character from the buffer, then doing
some list calculations, finally restoring the situation as if nothing
happened."
  (pcase (electric-pair-syntax-info char)
    (`(,syntax ,pair ,_ ,s-or-c)
     (let ((buffer-undo-list t))
       ;; Don't use `delete-char'; that may modify the head of the
       ;; undo list.
       (delete-region (point) (1- (point)))
       (let ((ret
              (cond
               ((eq ?\( syntax)
                (let* ((pair-data
                        (electric-pair--balance-info 1 s-or-c))
                       (outermost (cdr pair-data)))
                  (cond ((car outermost)
                         nil)
                        (t
                         (eq (cdr outermost) pair)))))
               ((eq syntax ?\")
                (electric-pair--unbalanced-strings-p char)))))
         (insert (string char))
         ret)))))
--8<---------------cut here---------------end--------------->8---

-- 
Akib Azmain Turja, GPG key: 70018CE5819F17A3BBA666AFE74F0EFA922AE7F5
Fediverse: akib <at> hostux.social
Codeberg: akib
emailselfdefense.fsf.org | "Nothing can be secure without encryption."
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66155; Package emacs. (Sun, 01 Oct 2023 08:11:01 GMT) Full text and rfc822 format available.

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

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Akib Azmain Turja <akib <at> disroot.org>
Cc: Daniel Mendler <mail <at> daniel-mendler.de>, 66155 <at> debbugs.gnu.org
Subject: Re: bug#66155: 'electric-pair-inhibit-if-helps-balance' has
 side-effects
Date: Sun, 1 Oct 2023 01:09:49 -0700
Akib Azmain Turja <akib <at> disroot.org> writes:

> With the default configuration (which uses
> 'electric-pair-inhibit-if-helps-balance'), inserting parens at the end
> of a Tempel field doesn't extend the field.
>
> I think the modify+undo magic in electric-pair-inhibit-if-helps-balance
> moves markers.
>
> The following is the solution that works for me atleast; it uses insert
> to insert back the character, and let-bind buffer-undo-list to not
> clobber the undo list.
>
> (defun *electric-pair-inhibit-if-helps-balance (char)
>   "Return non-nil if auto-pairing of CHAR would hurt parens' balance.
>
> Works by first removing the character from the buffer, then doing
> some list calculations, finally restoring the situation as if nothing
> happened."
>   (pcase (electric-pair-syntax-info char)
>     (`(,syntax ,pair ,_ ,s-or-c)
>      (let ((buffer-undo-list t))
>        ;; Don't use `delete-char'; that may modify the head of the
>        ;; undo list.
>        (delete-region (point) (1- (point)))
>        (let ((ret
>               (cond
>                ((eq ?\( syntax)
>                 (let* ((pair-data
>                         (electric-pair--balance-info 1 s-or-c))
>                        (outermost (cdr pair-data)))
>                   (cond ((car outermost)
>                          nil)
>                         (t
>                          (eq (cdr outermost) pair)))))
>                ((eq syntax ?\")
>                 (electric-pair--unbalanced-strings-p char)))))
>          (insert (string char))
>          ret)))))

Could you please send the patch as an attachment instead?  We prefer
that patches are created with a command like `git format-patch -1'.

Please also include:

- The bug number of this bug in the commit message, like so: Bug#66155

- A ChangeLog entry, as described in the file CONTRIBUTE.

That would make it easier for us to review and hopefully install
this patch.  Thanks in advance.




This bug report was last modified 1 year and 313 days ago.

Previous Next


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