Search code examples
javamacrosjedit

Creating a jEdit macro, what is the proper way to find out how many instances SearchAndReplace wrote into a buffer


I am attempting to create a macro in jEdit. I am attempting to remove all the white space from the start of every line in a buffer. Here's the jEdit macro (i.e. Java) code I have attempting to do that:

textArea.goToBufferStart(false);
//Set up SearchAndReplace defaults
SearchAndReplace.setBeanShellReplace(false);
SearchAndReplace.setWholeWord(false);
SearchAndReplace.setIgnoreCase(false);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());

public int replace(String from, String to) {
  //must be reg-ex escaped prior to call
  SearchAndReplace.setSearchString(from);
  SearchAndReplace.setReplaceString(to);
  return SearchAndReplace.replaceAll(view);
}

boolean done = false;
while (!done) {
  int count = replaced("\\n ", "\\n");
  found = (count > 0);
}

The documentation that comes with jEdit 5.0 indicates that the call SearchAndReplace.replaceAll(view) returns the number of buffers within which replacements occurred. But when I execute it, I get an error indicating that it does not return an int, but a boolean.

While it would be nice if it did return an int, I would like to be able to know the specific count of replacements that occurred within a specific buffer (the only one against which I am executing the macro).

So, I must be approaching this wrong. Any suggestions on how I can implement my intended effect, remove all whitespace from the start of every line? BTW, the macro currently ignores the first line intentionally. I know it is a defect and will correct that later.

Thank you for any direction you can give me.


Solution

  • If you want to strip whitespace from the beginning of every line, simply

    • Open search and replace with ctrl+f
    • Enter in ^[ \t]+ into the Search for box:
    • check regular expressions
    • click replace all