Subject: 30.1; False negative in electric-pair-inhibit-if-helps-balance

From: Paul Du Bois <[email-masked]>
Date: Fri, 19 Sep 2025 22:26:44 +0000

Reproduction steps:

$ cat > test.py <<EOF

a = "'"
EOF
$ emacs -Q
C-x C-f test.py
M-x electric-pair-mode

In the following, I'll use | to indicate point.
The current line will be empty
Hit ". The current line will look like "|"
Hit ". The current line will look like ""|
This shows that electric-pair-skip-if-helps-balance is working as expected.

Hit RETURN
Hit C-q ". The current line will look like "|
Hit ". The current line will look like ""|"
Expected: the current line looks like ""|

Diagnosis:

The logic for skip-if-helps and inhibit-if-helps is subtly different.
Both remove the character that the user just typed, and then examine the
parse state.  However:

`skip-if-helps` checks the parse state at `(point)` -- is it inside a
string of the same type as the user's quote char?

`inhibit-if-helps` checks the parse-state at `(point-max)` (modulo any
customizations to electric-pair-string-bound-function).  However, at
`(point-max)` the user's un-terminated quote could have caused all kinds
of havoc.  In the example above, the `a = "'"` makes EOB look like it's in a
`'`-string, rather than a `"`-string.  This causes the false-negative.

Other examples of situations that confuse `inhibit-if-helps`:

```
"|
# a = "'"
```
ppss at EOB still looks like a '-string, because the # is ignored

```
"|
""" " """
```
ppss at EOB looks like it's not in a string

Patch:

A minimal patch is to change inhibit-if-helps to use the same logic as
skip-if-helps:

--- elec-pair.el
+++ elec-pair.el
@@ -467,7 +467,7 @@
                          (t
                           (eq (cdr outermost) pair)))))
                 ((eq syntax ?\")
-                 (electric-pair--inside-string-p char)))))))))
+                 (electric-pair--unbalanced-strings-p char)))))))))
 
 (defun electric-pair-skip-if-helps-balance (char)
   "Return non-nil if skipping CHAR would benefit parentheses' balance.

This would make electric-pair--unbalanced-strings-p entirely unused, so
it could be deleted along with the variable
electric-pair-string-bound-function (which is un-customized by any major
mode in emacs)

Another way is to change the logic of unbalanced-strings-p to check the
parse state at (point) rather than (point-max), by customizing
electric-pair-string-bound-function.

In GNU Emacs 30.1 (build 2, x86_64-w64-mingw32) of 2025-02-23 built on
 AVALON
Windowing system distributor 'Microsoft Corp.', version 10.0.19045
System Description: Microsoft Windows 10 Pro (v10.0.2009.19045.6332)

Configured using:
 'configure --with-modules --without-dbus --with-native-compilation=aot
 --without-compress-install --with-tree-sitter CFLAGS=-O2
 prefix=/g/rel/install/emacs-30.1'

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY W32NOTIFY PDUMPER PNG RSVG SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XPM ZLIB

(NATIVE_COMP present but libgccjit not available)

Important settings:
  value of $LANG: ENU
  locale-coding-system: cp1252

Major mode: Python

Minor modes in effect:
  electric-pair-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils cl-seq python
rx project byte-opt gv bytecomp byte-compile compat pcase treesit comint
subr-x ansi-osc ring cl-loaddefs cl-lib ansi-color cus-start cus-load
elec-pair rmc iso-transl tooltip cconv eldoc paren electric uniquify
ediff-hook vc-hooks lisp-float-type elisp-mode mwheel touch-screen
dos-w32 ls-lisp disp-table term/w32-win w32-win w32-vars term/common-win
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer nadvice seq
simple cl-generic indonesian philippine cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button
loaddefs theme-loaddefs faces cus-face macroexp files window
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget keymap hashtable-print-readable backquote threads
w32notify w32 lcms2 multi-tty move-toolbar make-network-process
native-compile emacs)

Memory information:
((conses 16 86360 11467) (symbols 48 8548 0) (strings 32 23964 1208)
 (string-bytes 1 729777) (vectors 16 13151)
 (vector-slots 8 176946 7166) (floats 8 34 6) (intervals 56 275 0)
 (buffers 992 11))