/** * Finds the next instance of the search string in the specified buffer. * * @param view The view * @param buffer The buffer * @param start Location where to start the search * @param firstTime See {@link * SearchMatcher#nextMatch(CharSequence,boolean,boolean,boolean,boolean)}. * @since jEdit 4.1pre7 */ public static boolean find( View view, Buffer buffer, int start, boolean firstTime, boolean reverse) throws Exception { EditBus.send(new PositionChanging(view.getEditPane())); SearchMatcher matcher = getSearchMatcher(); if (matcher == null) { view.getToolkit().beep(); return false; } CharSequence text; boolean startOfLine; boolean endOfLine; if (reverse) { text = new ReverseCharSequence(buffer.getSegment(0, start)); startOfLine = true; endOfLine = (buffer.getLineEndOffset(buffer.getLineOfOffset(start)) - 1 == start); } else { text = buffer.getSegment(start, buffer.getLength() - start); startOfLine = (buffer.getLineStartOffset(buffer.getLineOfOffset(start)) == start); endOfLine = true; } String noWordSep = buffer.getStringProperty("noWordSep"); matcher.setNoWordSep(noWordSep); SearchMatcher.Match match = matcher.nextMatch(text, startOfLine, endOfLine, firstTime, reverse); if (match != null) { jEdit.commitTemporary(buffer); view.setBuffer(buffer, true); JEditTextArea textArea = view.getTextArea(); if (reverse) { textArea.setSelection(new Selection.Range(start - match.end, start - match.start)); // make sure end of match is visible textArea.scrollTo(start - match.start, false); textArea.moveCaretPosition(start - match.end); } else { textArea.setSelection(new Selection.Range(start + match.start, start + match.end)); textArea.moveCaretPosition(start + match.end); // make sure start of match is visible textArea.scrollTo(start + match.start, false); } return true; } else return false; } // }}}
/** * Replaces the current selection with the replacement string. * * @param view The view * @return True if the operation was successful, false otherwise */ public static boolean replace(View view) { // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if (comp == null) comp = view; JEditTextArea textArea = view.getTextArea(); Buffer buffer = view.getBuffer(); if (!buffer.isEditable()) return false; boolean smartCaseReplace = getSmartCaseReplace(); Selection[] selection = textArea.getSelection(); if (selection.length == 0) { view.getToolkit().beep(); return false; } record(view, "replace(view)", true, false); // a little hack for reverse replace and find int caret = textArea.getCaretPosition(); Selection s = textArea.getSelectionAtOffset(caret); if (s != null) caret = s.getStart(); try { buffer.beginCompoundEdit(); SearchMatcher matcher = getSearchMatcher(); if (matcher == null) return false; initReplace(); int retVal = 0; for (int i = 0; i < selection.length; i++) { s = selection[i]; retVal += replaceInSelection(view, textArea, buffer, matcher, smartCaseReplace, s); } if (reverse) { // so that Replace and Find continues from // the right location textArea.moveCaretPosition(caret); } else { s = textArea.getSelectionAtOffset(textArea.getCaretPosition()); if (s != null) textArea.moveCaretPosition(s.getEnd()); } if (!BeanShell.isScriptRunning()) { Object[] args = {Integer.valueOf(retVal), Integer.valueOf(1)}; view.getStatus().setMessageAndClear(jEdit.getProperty("view.status.replace-all", args)); } if (retVal == 0) { view.getToolkit().beep(); return false; } return true; } catch (Exception e) { handleError(comp, e); } finally { buffer.endCompoundEdit(); } return false; } // }}}