Subject: bug#77973: 30.1; `which-key' breaks `string-rectangle' after popup shown

From: Alcor <[email-masked]>
Date: Tue, 22 Apr 2025 21:46:57 +0200

Eli Zaretskii <[email-masked]> writes:

> I've now returned the Subject to its original.

Thanks Eli, I'm still not very familiar with the bug tracking system -
will keep that in mind.

That aside, I would like to note that the commit:

  3a8222e700304e4dff84fcdfa8ff2a4e48646c82
  Faster region-beginning and region-end for rectangle selections

Solves the issue. The actual underlying problem was the fact that the

  ; rectangle--region-beginning
  ...
  (apply #'min (mapcar #'car (region-bounds)))

  ; rectangle--region-end
  ...
  (apply #'max (mapcar #'cdr (region-bounds)))

forms in the 30.x implementations of rectangle--region-{beginning,end}
may result in a mutual recursion, as `region-bounds' calls
`region-extract-function', which calls `region-{beginning,end}' (both
advised by `rectangle--region-{beginning,end}' during a rectangular
selection).

I'm not sure why `which-key-mode' triggers this, but it seems it's
related to the `rectangle-mark-mode' state, as replacing the above forms
in `region-{beginning,end}' with:

  ; rectangle--region-beginning
  ...
  (let (rectangle-mark-mode) (apply #'min (mapcar #'car (region-bounds))))

  ; rectangle--region-end
  ...
  (let (rectangle-mark-mode) (apply #'max (mapcar #'cdr (region-bounds))))

As done in many other places in rect.el fixes the issue on 30.x -
however, I've also tried backporting
3a8222e700304e4dff84fcdfa8ff2a4e48646c82 onto 30.1 and it worked without
any issues, on top of it being faster as well.

Would it be possible to cherry-pick 3a8222e onto the emacs-30 branch so
a fix is available on 30.2? IMO, this bug is annoyingly easy to trigger
on 30.1 when using `which-key-mode' (see video in previous message), and
it's not trivial to unbreak rectangle-mark-mode after running into it,
so having it in the next bugfix release would improve things
significantly.

Cheers,
-A.