Subject: bug#49518: 28.0.50; electric-pair-mode skip-self fails for single-quotes in python-mode

From: Jim Porter <[email-masked]>
Date: Sat, 18 Sep 2021 09:56:17 -0700

(CCing João since he wanted to hear about `electric-pair-mode' issues[1].)

On 9/18/2021 5:55 AM, Daniel Fleischer wrote:
> Jim Porter [2021-07-11 Sun 10:34] wrote:
> 
>> That's true, and I've been working on a patch to make that behavior
>> more robust. The code for that is in
>> `python-electric-pair-string-delimiter', but I think an improved
>> implementation of that would depend on how this bug is fixed. I have a
>> local patch that works for me, but it depends on the other
>> electric-pair settings I use, so it couldn't merge as-is.
> 
> As I'm also bothered by this, is there some resolution?
> 
> BTW, my solution is hooking this code in 'inferior-python-mode':
> 
> (setq-local electric-pair-pairs
>              (append electric-pair-pairs '((?' . ?'))))

This isn't the right fix in general, but maybe it will help you. It 
works for me because I turn off `electric-pair-mode' inside comments and 
strings, but with the default configuration, it will pair single-quotes 
inside double-quoted strings (i.e. you won't be able to type an 
apostrophe). There's probably a more robust solution, but I haven't had 
time to investigate further...

----------------------------------------

   (defun user--python-electric-pair-string-delimiter ()
     (when (and electric-pair-mode
                (memq last-command-event '(?\" ?'))
                (let ((count 0))
                  (while (eq (char-before (- (point) count)) 
last-command-event)
                    (cl-incf count))
                  (= count 3)))
       (save-excursion (insert (make-string 3 last-command-event)))))

   (defun user--electric-pair-python-hook ()
     (setq-local electric-pair-text-syntax-table python-mode-syntax-table))

   (advice-add #'python-electric-pair-string-delimiter
               :override #'user--python-electric-pair-string-delimiter)
   (add-hook 'python-mode-hook #'user--electric-pair-python-hook)

----------------------------------------

[1] https://lists.gnu.org/archive/html/emacs-devel/2021-09/msg01313.html