Bug 134101

Summary: \2 placeholder in regular expression find-and-replace fails
Product: [Applications] kate Reporter: Manoj Rajagopalan <rmanoj>
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed In:

Description Manoj Rajagopalan 2006-09-15 06:27:32 UTC
Version:           3.3.2 (using KDE KDE 3.5.2)
Installed from:    Fedora RPMs
Compiler:          gcc 4.1.0 Fedora Core 5 / P4 1.7 GHz platform
OS:                Linux

When performing regular-expression find-and-replace, the \2 placeholder to indicate substitution by the second match is not substituted and simply appears as \2.

Example:

   I want to change variable names xmin_tex_plane, xmax_tmx_plane, ymax_tmy_plane, zmin_tmz_plane so that each begins with m_ (eg: m_xmin_tex_plane). To do this, I perform the following sequence of steps:

1. Ctrl-R to activate regular expression find-and-replace
2. declare the regular expression to be :  ([xyz])(min|max)(_t[em][eyz])
3. I enable the check-box labelled "Regular expression"
4. I declare the replace string to be : m_\1\2\3
5. I enable the "Use placeholders" check box

The result is that the above variables are modified to m_x\2_tex_plane, m_x\2_tmx_plane, m_y\2_tmy_plane, m_z\2_tmz_plane.
Comment 1 Manoj Rajagopalan 2006-09-15 06:31:29 UTC
The above bug doesn't show up if there is a piece of text separating the \1 and \2 match strings. For example, if I change the regular expression to be

   ([xyz])m(in|ax)(_t[em][xyz])

then using the same replace string m_\1\2\3 yields perfect results.
Comment 2 Jens Dagerbo 2006-09-15 21:22:30 UTC
Must be Kate's. Reassigning.
Comment 3 Maciej Pilichowski 2007-01-30 11:11:54 UTC
Even simpler case -- without regexps at all.

Let's say we have text
vector a;
vector b;

Now, replace 
vector
with
std::\0
with placeholders enabled. The result?

std::\0 a;
std::\0 b;
Comment 4 Dominik Haumann 2007-01-31 18:13:45 UTC
Comment #3 is confusing. To me the behaviour is correct. Placeholders without reg exps are not useful afaik.
Comment 5 Anders Lund 2007-01-31 18:18:57 UTC
I'll try and look into that
Comment 6 Maciej Pilichowski 2007-01-31 19:38:30 UTC
Dominik, it is totally useful. Full match works only and this make sense.

Try to replace
oh, really long phrase I don't know how long it is 
with
"oh, really long phrase I don't know how long it is"

And repeat it ten times with various phrases. How it would be much easier with
oh, really long phrase I don't know how long it is 
replace
"\0"


Comment 7 Dominik Haumann 2007-01-31 19:38:53 UTC
SVN commit 628855 by dhaumann:

Patch by Anders: fix bug in search&replace; placeholder did not work correctly
CCBUG: 134101


 M  +1 -1      katesearch.cpp  


--- branches/KDE/3.5/kdelibs/kate/part/katesearch.cpp #628854:628855
@@ -393,7 +393,7 @@
           kdDebug()<<"KateSearch::replaceOne(): you don't have "<<ccap<<" backreferences in regexp '"<<m_re.pattern()<<"'"<<endl;
         }
       }
-      pos = br.search( replaceWith, pos+kMax(br.matchedLength(), (int)sc.length()) );
+      pos = br.search( replaceWith, pos + (int)sc.length() );
     }
   }
 
Comment 8 Dominik Haumann 2007-01-31 19:54:56 UTC
Maciej: In the meantime I understood your issue ;) It's simply that \0 is the "whole match", which is if regexp is disabled of course the text in the line-edit itself.
Can you open another bug report about it, please? I think it's a valid bug (especially because the user interface does not disable the placeholder button when regexp is disabled)
Comment 9 Anders Lund 2007-01-31 20:15:14 UTC
SVN commit 628866 by alund:

get the right position when inserting backreferences

BUG: 134101


 M  +1 -1      katesearch.cpp  


--- trunk/KDE/kdelibs/kate/part/katesearch.cpp #628865:628866
@@ -384,7 +384,7 @@
           kDebug()<<"KateSearch::replaceOne(): you don't have "<<ccap<<" backreferences in regexp '"<<m_re.pattern()<<"'"<<endl;
         }
       }
-      pos = br.indexIn( replaceWith, pos + qMax(br.matchedLength(), (int)sc.length()) );
+      pos = br.indexIn( replaceWith, pos + (int)sc.length());
     }
   }
 
Comment 10 Anders Lund 2007-01-31 20:19:54 UTC
Wrt the 'use placeholders' button being enabled during non-regex replace, that is a bug in the KDE find/replace dialog, which is not owned by Kate.

For Kate in KDE4 this will not be an issue -- we will not be using that dialog anymore, as we are switching to embed the tool dialogs in the view. I hope that this includes the replace dialog.
Comment 11 Maciej Pilichowski 2007-01-31 20:35:43 UTC
Dominik: done,
https://bugs.kde.org/show_bug.cgi?id=140970

Anders,
> Wrt the 'use placeholders' button being enabled during non-regex replace,
> that is a bug

Why it is a bug? It says "use the entire text in replace editbox".
Comment 12 Tim Hutt 2007-01-31 21:03:47 UTC
Also, (another bug, but I am currently too lazy to submit it):

If you edit the regex with KRegexp (or whatever it is called) and add brackets ('(' and ')'), when you click OK they are changed to square brackets ('[' and ']'). Weird.
Comment 13 Anders Lund 2007-01-31 21:14:39 UTC
On Wednesday 31 January 2007, Tim Hutt wrote:
> Also, (another bug, but I am currently too lazy to submit it):
>
> If you edit the regex with KRegexp (or whatever it is called) and add
> brackets ('(' and ')'), when you click OK they are changed to square
> brackets ('[' and ']'). Weird.


Well, this is not a bug in kate, rather in the kde find dialog or in kreplace, 
which i'd think returned a string. So it would be a nice touch to report it 
separately if you please ;)
Comment 14 Anders Lund 2007-02-01 08:33:08 UTC
On Wednesday 31 January 2007, Maciej Pilichowski wrote:
> Why it is a bug? It says "use the entire text in replace editbox".


Because it's not meant to be acitvated during non-regex replace. That said, I 
can see the use for it.
Comment 15 Anders Lund 2007-02-01 08:35:00 UTC
On Wednesday 31 January 2007, Maciej Pilichowski wrote:
> Try to replace
> oh, really long phrase I don't know how long it is
> with
> "oh, really long phrase I don't know how long it is"
>
> And repeat it ten times with various phrases. How it would be much easier
> with oh, really long phrase I don't know how long it is
> replace
> "\0"


This is an example of why regex replace rocks, you can do all those operations 
at once with regex replace ;)
Comment 16 Maciej Pilichowski 2007-02-01 08:57:13 UTC
> This is an example of why regex replace rocks, 

Better is the enemy of good.

How can you replace (easily) a bit longer phrase? Like this one. A *lot* of work! Using regexp of course.