Subject: bug#65347: 29.1; Underscore in query replace prevents case-matching

From: Eli Zaretskii <[email-masked]>
Date: Thu, 17 Aug 2023 10:14:36 +0300

> From: Spencer Baugh <[email-masked]>
> Date: Wed, 16 Aug 2023 17:27:34 -0400
> 
> 
> 1. emacs -Q
> 2. (insert "Foo_bar")
> 3. M-% foo RET quux RET
> 4. Observe the buffer contains Quux_bar
> 5. Undo, so the buffer contains Foo_bar again
> 6. M-% foo_bar RET quux RET
> 7. Observe the buffer contains quux
> 
> Expected behavior:
> 7. Observe the buffer contains Quux
> 
> It seems to me that this would match the docstring better:
> 
>   Replacement transfers the case pattern of the old text to the
>   new text, if both ‘case-fold-search’ and ‘case-replace’ are
>   non-nil and FROM-STRING has no uppercase letters.

This is a documentation issue: the complex heuristics used by
replace-match to decide whether and how to "transfer the case pattern"
depend on whether _all_ the words in the replaced text use the same
case pattern.  In your example, Foo_bar is 2 words (what is a "word"
is determined by the buffer's syntax table), and only one of them is
capitalized.  So Emacs does not capitalize the replacement.  Try doing
the same with Foo_Bar, and you will see Quux in the replacement.

These subtleties come up from time to time, so I think it's high time
we documented that (in the hope this will stop this from popping up),
so I've now done so on the emacs-29 branch.

I think this is all we should do here, as the behavior is correct and
justified, has been like that since time immemoriam, and we should not
change it, therefore.