Subject: bug#74091: 31.0.50; string-pixel-width in mode line disables region

From: Eshel Yaron <[email-masked]>
Date: Thu, 31 Oct 2024 12:09:20 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Eshel Yaron <[email-masked]>
>> Cc: [email-masked]
>> Date: Wed, 30 Oct 2024 16:26:38 +0100
>> 
>> >> At this point the region is expected to be active since we activated it
>> >> in step 4.  But in step 5 the mode line is updated, which calls
>> >> string-pixel-width, which in turn unexpectedly disables the region.
>> >
>> > Thanks, should be fixed now.
>> 
>> Great!  That seems to work.
>
> Thanks for testing, I will close this bug.

Since we don't fully understand the issue, it may manifest in more ways.
So I think closing the bug is premature.

After looking a bit more into it, it seems like the problem has to do
with the call to kill-all-local-variables in work-buffer--release, the
following patch circumvents the unexpected behavior, although I still
don't understand why:

diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 5b47deb880e..b5cbe28afad 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -361,7 +361,7 @@ work-buffer--release
           (erase-buffer))
         (delete-all-overlays)
         (let (change-major-mode-hook)
-          (kill-all-local-variables t))
+          (kill-all-local-variables))
         ;; Make the buffer available again.
         (push buffer work-buffer--list)))
   ;; If the maximum number of reusable work buffers is exceeded, kill

Here's another interesting data point:

--8<---------------cut here---------------start------------->8---
(defun foo ()
  (interactive)
  (with-current-buffer (get-buffer-create "some-buffer")
    (kill-all-local-variables t)
    (insert "foo")))
--8<---------------cut here---------------end--------------->8---

Invoking this command twice in a row in subr.el deactivates the region,
while the same without the argument to kill-all-local-variables keeps
the region active.

So the problem seems to be in a lower level than string-pixel-width...

>> >> I'm not really sure why this happens...
>> >
>> > It happens because string-pixel-width modifies a buffer, and that sets
>> > deactivate-mark, which then causes the region to be deactivated when
>> > a command finishes.
>> 
>> Hmm but string-pixel-width used to modify a buffer also in the old
>> implementation, and that never caused this issue...
>
> The new implementation also didn't cause this issue in some buffers.
> For example, in *scratch*.  Trying to understand the logic of a bug is
> never a good investment of time.

:)

As I'm sure you know, applying a crude fix without fully understanding
the problem is likely to hide other subtle bugs that may then be harder
to investigate.

>> And in both the old
>> implementation and in the new one, the modification is in a different
>> buffer, is that expected to disable the mark in the original buffer?
>
> The variable deactivate-mark only becomes buffer-local if set;
> otherwise the global value will be changed.

Could you perhaps elaborate?  I see that running a command that modifies
a different buffer does not deactivate the region in the current buffer,
which is basically what I would expect.

Best,

Eshel