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

From: Spencer Baugh <[email-masked]>
Date: Fri, 18 Aug 2023 14:31:46 -0400

Eli Zaretskii <[email-masked]> writes:
>> From: Spencer Baugh <[email-masked]>
>> Cc: Stefan Monnier <[email-masked]>,  [email-masked]
>> Date: Fri, 18 Aug 2023 12:30:48 -0400
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> >> There's `superword-mode`, but I suspect that query-replace does not
>> >> honor it in this case.  Could be considered as a bug in
>> >> `superword-mode`.
>> >
>> > Actually, I think it's a bug in replace-match: it doesn't honor
>> > find-word-boundary-function-table.  I think it should.  Patches
>> > welcome.
>> 
>> I suppose this is a bit of an XY problem - the real thing I (speaking
>> for the user who reported this to me) want is to have case-matching for
>> symbols instead of words.
>
> We look at this from different angles.  As an Emacs maintainer, I
> would like to see every place that deals with words to honor
> find-word-boundary-function-table, instead of inventing its own ideas
> about where a word begins and where it ends.

Understandable, I want this too of course.  Better support for case
matching in words would be good.

>> e.g. if I have Foo-do-action and I do a replace of foo-do-action with
>> bar-do-action, I want the replacement to be Bar-do-action.
>
> Here, you look at the issue from a very narrow perspective: of words
> separated by '-' or '_', because that just happens to be the case that
> you bumped into.  But that is just one very particular use case; words
> can be separated by a lot more characters.  I would actually begin
> considering the simpler case of "Foo do action".

But no, this use case very specifically is about replacing symbols
(sexps) while matching case.  There's a body of code which frequently
uses the same symbol with different capitalization (both as a lowercase
variable name and an uppercase type name) and I want to do replacements
which match case.

I don't much care about the word boundary or superword-mode, because I'm
not dealing with words - I'm very deliberately dealing with symbols.

>> Alternatively, much more simply, maybe the case-matching could just
>> detect if the first word is capitalized, and then also capitalize the
>> first word in the replacement?  That would work for this specific usage.
>
> Given enough user options, we could support that as optional behavior.
> But one thing is clear: we cannot simultaneously support all of the
> possible interpretations of "capitalization pattern".  And another
> thing is clear: this heuristic is confusing and rarely kept in mind as
> it is, so imagine what will happen if we add half a dozen options to
> support its variants...

Sure.  This idea is very hacky anyway.

What about the other thing I proposed?  That's what I'm more interested
in, because this is a problem of replacing symbols.  And it's much more
elegant.  Repeated here:

>>Alternatively, much more interestingly, the case-matching could *always*
>>detect case patterns both for symbols and for words.  And if there's no
>>case pattern for words, but there is a case pattern for symbols, apply
>>that case pattern to symbols in the replacement.  We could even turn
>>that on by default.  Does that sound like a good change?  I can make
>>that change if it sounds desirable.