How to use SearchAndReplace in a loop?
Submitted by Wednesday, 21 November, 2007 - 16:59
on
Hello,
I have been using jEdit for a few months now, and it is an awesome editor. I have written a few macros, but I do not know Beanshell very well and now I am stumped.
I want to write a macro to correct some words using SearchAndReplace command. I would like to be able to define the words to be searched and replaced, and then to run a loop, where the replacements happen. I am vaguely aware of things such as arrays and maps, and I think in my case I am going to have to use one of those, but I don't know where to begin.
This is an example of how I want it to work (just a concept, not a working code):
//Define search and replace pairs
searchReplacePairs[] = ("thats","that's",
"lets","let's"
"...","...");
//Grab each pair and perform replace
Loop searchReplacePairs
{
SearchAndReplace.setSearchString(textToSearch);
SearchAndReplace.setReplaceString(textToReplace);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
}
As you can see, I do not know how to define the array, then how to parse it to get the textToSearch/textToReplace pair and process it in the loop. Any help would be welcome.
Thanks, tvojeho
I have been using jEdit for a few months now, and it is an awesome editor. I have written a few macros, but I do not know Beanshell very well and now I am stumped.
I want to write a macro to correct some words using SearchAndReplace command. I would like to be able to define the words to be searched and replaced, and then to run a loop, where the replacements happen. I am vaguely aware of things such as arrays and maps, and I think in my case I am going to have to use one of those, but I don't know where to begin.
This is an example of how I want it to work (just a concept, not a working code):
//Define search and replace pairs
searchReplacePairs[] = ("thats","that's",
"lets","let's"
"...","...");
//Grab each pair and perform replace
Loop searchReplacePairs
{
SearchAndReplace.setSearchString(textToSearch);
SearchAndReplace.setReplaceString(textToReplace);
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());
SearchAndReplace.replaceAll(view);
}
As you can see, I do not know how to define the array, then how to parse it to get the textToSearch/textToReplace pair and process it in the loop. Any help would be welcome.
Thanks, tvojeho