unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#66155: 'electric-pair-inhibit-if-helps-balance' has side-effects
@ 2023-09-22 11:51 Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-10-01  8:09 ` Stefan Kangas
  0 siblings, 1 reply; 2+ messages in thread
From: Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-09-22 11:51 UTC (permalink / raw)
  To: 66155; +Cc: Daniel Mendler

[-- Attachment #1: Type: text/plain, Size: 1809 bytes --]


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@hostux.social
Codeberg: akib
emailselfdefense.fsf.org | "Nothing can be secure without encryption."

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug#66155: 'electric-pair-inhibit-if-helps-balance' has side-effects
  2023-09-22 11:51 bug#66155: 'electric-pair-inhibit-if-helps-balance' has side-effects Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-10-01  8:09 ` Stefan Kangas
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Kangas @ 2023-10-01  8:09 UTC (permalink / raw)
  To: Akib Azmain Turja; +Cc: Daniel Mendler, 66155

Akib Azmain Turja <akib@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.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-01  8:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-22 11:51 bug#66155: 'electric-pair-inhibit-if-helps-balance' has side-effects Akib Azmain Turja via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-01  8:09 ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).