Skip to main content

Kate searching: On-the-fly case conversion

I just added a feature to search and replace in Kate/KWrite that is so cool I just have to blog about it. :-) The idea is not new, at least EmEditor can do this for quite some time: on-the-fly case conversion when replacing text.

Let me demonstrate what this feature can do. Say our document reads "Music", when we now regex-replace with

 Search: ".*"
Replace: "\U\0_\L\0_\E\0"

we get MUSIC_music_Music as a result. What did we do? We set the case conversion mode to uppercase (\U), lowercase (\L) and then back to don't-modify (\E). With this new tool at hand we can now solve the camel case issue we had last time when generating setters from class members:

 Search: "^(.+) (m_(.)(.+));"
Replace: "void Some::set\U\3\E\4(\1 \3\4) {\n    \2 = \3\4;\n}\n"

Input:

char * m_name;
int m_count;

Output:

void Some::setName(char * name) {
    m_name = name;
}

void Some::setCount(int count) {
    m_count = count;
}

Isn't that nice! :-) The only thing I'm still missing is something like a counter so we can enumerate lines/matches when replacing. It should be quite easy to implement but I'm not sure about the "design" yet: should it offer zero padding width and a start value? What should the escape sequences look like then? Maybe \#w3s2# for 002, 003, ...? Is \# bound to a meaning somewhere else already?

I'll go get some sleep first :-)