// Find the next location of the text we're searching for.
RegExReplaceInfo info = getRegExReplaceInfo(findIn, context);
// If a match was found, do the replace and return!
DocumentRange range = null;
if (info!=null) {
// Without this, if JTextArea isn't in focus, selection won't
// appear selected.
c.setSelectionVisible(true);
int matchStart = info.getStartIndex();
int matchEnd = info.getEndIndex();
if (forward) {
matchStart += start;
matchEnd += start;
}
textArea.setSelectionStart(matchStart);
textArea.setSelectionEnd(matchEnd);
String replacement = info.getReplacement();
textArea.replaceSelection(replacement);
// If there is another match, find and select it.
int dot = matchStart + replacement.length();
findIn = getFindInCharSequence(textArea, dot, forward);
info = getRegExReplaceInfo(findIn, context);
if (info!=null) {
matchStart = info.getStartIndex();
matchEnd = info.getEndIndex();
if (forward) {
matchStart += dot;
matchEnd += dot;
}
range = new DocumentRange(matchStart, matchEnd);
}
else {
range = new DocumentRange(dot, dot);
}
RSyntaxUtilities.selectAndPossiblyCenter(textArea, range, true);
}