Subject: Re: bug#62505: 29.0.60; Switching between query-replace and query-replace-regexp should be easy, like isearch

From: Juri Linkov <[email-masked]>
Date: Sat, 13 Sep 2025 21:37:56 +0300

>> There is such logic in 'query-replace-read-from':
>>
>>               (if regexp-flag
>>                   (read-regexp ...)
>>                 (read-from-minibuffer ...))
>>
>> Changing the value of 'regexp-flag' while the minibuffer is active will require
>> canceling the current minibuffer and restarting 'query-replace' from scratch.
>
> Ah.  Right, troubling.
>
> Hmm...
>
> I guess we could give read-regexp a "non-regexp" or "plain text" mode,
> which can be toggled on and off by the user, and just always call
> read-regexp.  Then read-regexp could return "this string isn't a regex"
> using a text property on the string, just as it returns the text
> property case-fold right now.
>
> Then after we return from read-regexp we could read that text property
> and set regexp-flag based on it.
>
> What do you think?  Too hacky?

Admittedly, it's too hacky: the single purpose of 'read-regexp' is to
read a regexp, so switching to the string mode would defeat its purpose.

>> However, how to implement toggling the regexp mode while using query-replace
>> is clear and is implemented in this patch:
>
> Nice!  I tried it and it works.
>
> Though, one issue is that if you start a query-replace that was meant to
> be a query-replace-regexp, it will probably not have any matches, so it
> will immediately fail.  Though I guess in that case one can just
> immediately do C-M-%.

Yes, I noticed this too.  And indeed it's easy to just do 'C-M-% RET'
that reuses the last search→replace pair in regexp mode.

Also it helps to notice the mistake earlier by looking at the number of
matches at the beginning of the minibuffer while typing a string to replace
that shows "0" for no matches.