Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341410

Summary: [find/replace] Make search and replace work more like other editors
Product: [Eclipse Project] Platform Reporter: Arun <arun936>
Component: TextAssignee: Platform-Text-Inbox <platform-text-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: P3 CC: daniel_megert, eclipse.sprigogin, Olivier_Thomann, remy.suen
Version: 3.7Keywords: needinfo
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Arun CLA 2011-03-30 14:20:45 EDT
In Emacs, when you do a search and replace, the replacement text matches the case of the text being replaced. Supporting this in Eclipse would be immensely useful. Right now, I often drop down to Emacs when I need to do something like this.
Comment 1 Remy Suen CLA 2011-03-30 14:26:50 EDT
Are you talking about the 'Find/Replace' dialog (Ctrl+F) or the 'Search' dialog (Ctrl+H)? Please attach screenshots describing your problem.
Comment 2 Sergey Prigogin CLA 2011-03-30 14:29:57 EDT
Please provide details of the behavior that you would like to see. It should
still be possible to do replacement that does not preserve case, shouldn't it?
There is currently \C control character for preserving case. If you would like
to see a different mechanism, please describe it in detail.
Comment 3 Olivier Thomann CLA 2011-03-30 14:36:06 EDT
Move to JDT/Text
Comment 4 Arun CLA 2011-03-30 15:52:45 EDT
(In reply to comment #2)
> Please provide details of the behavior that you would like to see. It should
> still be possible to do replacement that does not preserve case, shouldn't it?
> There is currently \C control character for preserving case. If you would like
> to see a different mechanism, please describe it in detail.

Yes, I see that the \C control character appears to do whatever Emacs does. I did not know it existed. Even when someone brought it to my attention, I did not know how to turn it on until I asked.

I would argue that the default behavior for:

Find: foobarbaz
Replace: abcdefxyz

should be:

Replace: \Cabcdefxyz

For example, Emacs does the following:

Original text:

foobarbaz
fooBarbaz
FooBarBaz
FOObarBAZ
FoOBArbAz
FOOBARBAZ

Replace with: abcDefXYZ

New text:

abcDefXYZ
abcDefXYZ
AbcDefXYZ
AbcDefXYZ
AbcDefXYZ
ABCDEFXYZ

Hope this helps,
arun
Comment 5 Arun CLA 2011-03-30 15:53:43 EDT
I should add that I am not sure that \C<replacetext> will do what I showed in my example.

(In reply to comment #4)
> (In reply to comment #2)
> > Please provide details of the behavior that you would like to see. It should
> > still be possible to do replacement that does not preserve case, shouldn't it?
> > There is currently \C control character for preserving case. If you would like
> > to see a different mechanism, please describe it in detail.
> 
> Yes, I see that the \C control character appears to do whatever Emacs does. I
> did not know it existed. Even when someone brought it to my attention, I did
> not know how to turn it on until I asked.
> 
> I would argue that the default behavior for:
> 
> Find: foobarbaz
> Replace: abcdefxyz
> 
> should be:
> 
> Replace: \Cabcdefxyz
> 
> For example, Emacs does the following:
> 
> Original text:
> 
> foobarbaz
> fooBarbaz
> FooBarBaz
> FOObarBAZ
> FoOBArbAz
> FOOBARBAZ
> 
> Replace with: abcDefXYZ
> 
> New text:
> 
> abcDefXYZ
> abcDefXYZ
> AbcDefXYZ
> AbcDefXYZ
> AbcDefXYZ
> ABCDEFXYZ
> 
> Hope this helps,
> arun
Comment 6 Sergey Prigogin CLA 2011-03-30 16:18:26 EDT
(In reply to comment #4)

What should happen when the input and output strings have different length?

It should be possible to replace FOOBARBAZ get abcDefXYZ and get abcDefXYZ as the result. Does emacs support that? How do you propose this behavior should be controlled?
Comment 7 Arun CLA 2011-03-30 16:29:23 EDT
(In reply to comment #6)
> (In reply to comment #4)
> 
> What should happen when the input and output strings have different length?
> 
> It should be possible to replace FOOBARBAZ get abcDefXYZ and get abcDefXYZ as
> the result. Does emacs support that? How do you propose this behavior should be
> controlled?

But that's the whole point. The case of the replacement text influences the final replacement if it deviates from lower case.

As I mentioned before, it would help if we made the default behavior be
  \C<replace-text>
Comment 8 Sergey Prigogin CLA 2011-03-30 17:28:38 EDT
(In reply to comment #7)
The question 

> What should happen when the input and output strings have different length?

was not answered in comment #7.
Comment 9 Arun CLA 2011-03-30 20:07:02 EDT
(In reply to comment #8)
> (In reply to comment #7)
> The question 
> 
> > What should happen when the input and output strings have different length?
> 
> was not answered in comment #7.

I missed that parts. Here are some examples where the original text is:

foobarbaz
fooBarbaz
FooBarBaz
FOObarBAZ
FoOBArbAz
FOOBARBAZ

Replacement is longer: foobarba -> abcDefXYZ
Result is:

abcDefXYZz
abcDefXYZz
AbcDefXYZz
AbcDefXYZZ
AbcDefXYZz
ABCDEFXYZZ

Replacement is shorter: foobarbaz -> abcDefXY
Result is:

abcDefXY
abcDefXY
AbcDefXY
AbcDefXY
AbcDefXY
ABCDEFXY



Replacement is longer:
Comment 10 Sergey Prigogin CLA 2011-03-30 20:17:14 EDT
(In reply to comment #9)

Do I understand it correctly that using this algorithm it not possible to replace mySomething to yourSomething and MySomething to YourSomething in a single operation?
Comment 11 Arun CLA 2011-03-31 13:43:24 EDT
(In reply to comment #10)
> (In reply to comment #9)
> 
> Do I understand it correctly that using this algorithm it not possible to
> replace mySomething to yourSomething and MySomething to YourSomething in a
> single operation?

Everything is possible in Emacs. :-)

What I was showing so far is what was possible using the 'M-x query-replace' command (for those who know Emacs). There is another command 'M-x query-replace-regexp' that will do any thing that you can think of and express using regular expressions. I have used Emacs for more years than I would like to share and have never felt the need to use regular expressions for search and replace. Hence, I have never needed to use 'M-x query-replace-regexp'.

A simple change to the current default semantics in Eclipse Find and Replace would make it much more useful (of course, IMHO). That is all I am looking for. I am not asking for support for the much more powerful 'query-replace-regexp' command.
Comment 12 Sergey Prigogin CLA 2011-03-31 14:16:38 EDT
(In reply to comment #11)
> > Do I understand it correctly that using this algorithm it not possible to
> > replace mySomething to yourSomething and MySomething to YourSomething in a
> > single operation?

I don't think you answered the question above. You are proposing a feature to be implemented in Eclipse. I'm trying to understand the area of applicability of the proposed feature.
Comment 13 Arun CLA 2011-03-31 14:30:07 EDT
(In reply to comment #12)
> (In reply to comment #11)
> > > Do I understand it correctly that using this algorithm it not possible to
> > > replace mySomething to yourSomething and MySomething to YourSomething in a
> > > single operation?
> 
> I don't think you answered the question above. You are proposing a feature to
> be implemented in Eclipse. I'm trying to understand the area of applicability
> of the proposed feature.

Yes, it is possible that I don't understand your question. You mentioned an algorithm and I naturally assumed that you were referring to the default semantics that I am suggesting for Eclipse Find and Replace since that has been the discussion so far. If you are referring to some other algorithm, please clarify.

If you are asking for the area of applicability, it is text Find and Replace as I understand Eclipse. Again, if you are asking for something else, please clarify.

If it is too difficult to implement or would cause too much disruption, please let me know and I'll understand.

Thanks for listening,
arun
Comment 14 Sergey Prigogin CLA 2011-03-31 14:39:05 EDT
(In reply to comment #13)
> Yes, it is possible that I don't understand your question.

Let me rephrase it.

Is it true that with the Replace algorithm proposed in this feature request it would not be possible to replace mySomething with yourSomething and MySomething with YourSomething in a single Replace operation without using regular expressions?
Comment 15 Arun CLA 2011-03-31 17:11:55 EDT
(In reply to comment #14)
> (In reply to comment #13)
> > Yes, it is possible that I don't understand your question.
> 
> Let me rephrase it.
> 
> Is it true that with the Replace algorithm proposed in this feature request it
> would not be possible to replace mySomething with yourSomething and MySomething
> with YourSomething in a single Replace operation without using regular
> expressions?

In a single operation with our regular expressions, no.

The proposed default semantics do not obviate the need for regular expressions as I said in my previous note.

In current Eclipse Find and Replace semantics, one would still need to use regular expressions by setting
Find -> mySomething
Replace -> \Cyour\CSomething

Is that what you are looking for?
Comment 16 Sergey Prigogin CLA 2011-03-31 17:15:29 EDT
(In reply to comment #15)

Could you please provide real-life use cases where the proposed functionality would useful.
Comment 17 Arun CLA 2011-03-31 19:21:40 EDT
(In reply to comment #16)
> (In reply to comment #15)
> 
> Could you please provide real-life use cases where the proposed functionality
> would useful.

I have many times needed to do the following:

replace 'my' with 'your' respecting the case of the replaced text in

mySomething
MySomething
MYSOMETHING

In Emacs that is easy in the query-replace command.

I cannot do that in Eclipse without the following:

Find: my
Replace: \Cyour\C

with Regular Expression selected.

That is exactly the default behavior I am asking for.

Hope this helps.
arun
Comment 18 Dani Megert CLA 2011-04-01 03:34:09 EDT
The subject says: "Make search and replace work more like other editors". Most editors I know behave like Eclipse. Can you list the editors that support your feature by default (besides Emacs)?

We won't change the default as this would be too disruptive for long-time Eclipse users and RCPs. The only way to address this would be to add another check box but that would make the dialog bigger, which is something we tried to avoid in the past.
Comment 19 Arun CLA 2011-04-01 13:48:21 EDT
(In reply to comment #18)
> The subject says: "Make search and replace work more like other editors". Most
> editors I know behave like Eclipse. Can you list the editors that support your
> feature by default (besides Emacs)?
> 
> We won't change the default as this would be too disruptive for long-time
> Eclipse users and RCPs. The only way to address this would be to add another
> check box but that would make the dialog bigger, which is something we tried to
> avoid in the past.

No problem, I understand your point about being disruptive. There is a workaround which is to check the Regular Expression box and to surround the replacement text with \C.

Also, someone at work has created a plugin that allows us to switch easily between the Eclipse buffer for a file and an Emacs buffer thereby getting the best of both worlds.

Thanks for listening,
arun