📄 searchandreplace.java
字号:
} boolean _reverse = reverse && fileset instanceof CurrentBufferSet; if(_reverse && regexp) { GUIUtilities.error(comp,"regexp-reverse",null); return false; } try { view.showWaitCursor(); SearchMatcher matcher = getSearchMatcher(); if(matcher == null) { view.getToolkit().beep(); return false; } record(view,"find(view)",false,true);loop: for(;;) { while(path != null) { Buffer buffer = jEdit.openTemporary( view,null,path,false); /* this is stupid and misleading. * but 'path' is not used anywhere except * the above line, and if this is done * after the 'continue', then we will * either hang, or be forced to duplicate * it inside the buffer == null, or add * a 'finally' clause. you decide which one's * worse. */ path = fileset.getNextFile(view,path); if(buffer == null) continue loop; // Wait for the buffer to load if(!buffer.isLoaded()) VFSManager.waitForRequests(); int start; if(view.getBuffer() == buffer && !repeat) { JEditTextArea textArea = view.getTextArea(); Selection s = textArea.getSelectionAtOffset( textArea.getCaretPosition()); if(s == null) start = textArea.getCaretPosition(); else if(_reverse) start = s.getStart(); else start = s.getEnd(); } else if(_reverse) start = buffer.getLength(); else start = 0; if(find(view,buffer,start,repeat,_reverse)) return true; } if(repeat) { if(!BeanShell.isScriptRunning()) { view.getStatus().setMessageAndClear( jEdit.getProperty("view.status.search-not-found")); view.getToolkit().beep(); } return false; } boolean restart; // if auto wrap is on, always restart search. // if auto wrap is off, and we're called from // a macro, stop search. If we're called // interactively, ask the user what to do. if(wrap) { if(!BeanShell.isScriptRunning()) { view.getStatus().setMessageAndClear( jEdit.getProperty("view.status.auto-wrap")); // beep if beep property set if(jEdit.getBooleanProperty("search.beepOnSearchAutoWrap")) { view.getToolkit().beep(); } } restart = true; } else if(BeanShell.isScriptRunning()) { restart = false; } else { Integer[] args = { new Integer(_reverse ? 1 : 0) }; int result = GUIUtilities.confirm(comp, "keepsearching",args, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); restart = (result == JOptionPane.YES_OPTION); } if(restart) { // start search from beginning path = fileset.getFirstFile(view); repeat = true; } else break loop; } } catch(Exception e) { Log.log(Log.ERROR,SearchAndReplace.class,e); Object[] args = { e.toString() }; GUIUtilities.error(comp,"searcherror",args); } finally { view.hideWaitCursor(); } return false; } //}}} //{{{ find() method /** * 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 */ public static boolean find(View view, Buffer buffer, int start) throws Exception { return find(view,buffer,start,false,false); } //}}} //{{{ find() method /** * 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(CharIndexed, * boolean,boolean,boolean,boolean)}. * @since jEdit 4.1pre7 */ public static boolean find(View view, Buffer buffer, int start, boolean firstTime, boolean reverse) throws Exception { SearchMatcher matcher = getSearchMatcher(); if(matcher == null) { view.getToolkit().beep(); return false; } Segment text = new Segment(); if(reverse) buffer.getText(0,start,text); else buffer.getText(start,buffer.getLength() - start,text); // the start and end flags will be wrong with reverse search enabled, // but they are only used by the regexp matcher, which doesn't // support reverse search yet. // // REMIND: fix flags when adding reverse regexp search. SearchMatcher.Match match = matcher.nextMatch(new CharIndexedSegment(text,reverse), start == 0,true,firstTime,reverse); if(match != null) { jEdit.commitTemporary(buffer); view.setBuffer(buffer); 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; } //}}} //{{{ replace() method /** * 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); } boolean _reverse = !regexp && reverse && fileset instanceof CurrentBufferSet; 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(retVal == 0) { view.getToolkit().beep(); return false; } return true; } catch(Exception e) { Log.log(Log.ERROR,SearchAndReplace.class,e); Object[] args = { e.toString() }; GUIUtilities.error(comp, beanshell ? "searcherror-bsh" : "searcherror",args); } finally { buffer.endCompoundEdit(); } return false; } //}}} //{{{ replace() method /** * Replaces text in the specified range with the replacement string. * @param view The view * @param buffer The buffer * @param start The start offset * @param end The end offset * @return True if the operation was successful, false otherwise */ public static boolean replace(View view, Buffer buffer, int start, int end) { if(!buffer.isEditable()) return false; // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if(comp == null) comp = view; boolean smartCaseReplace = getSmartCaseReplace(); try { buffer.beginCompoundEdit(); SearchMatcher matcher = getSearchMatcher(); if(matcher == null) return false; initReplace(); int retVal = 0; retVal += _replace(view,buffer,matcher,start,end, smartCaseReplace); if(retVal != 0) return true; } catch(Exception e) { Log.log(Log.ERROR,SearchAndReplace.class,e); Object[] args = { e.toString() }; GUIUtilities.error(comp, beanshell ? "searcherror-bsh" : "searcherror",args); } finally { buffer.endCompoundEdit(); } return false; } //}}} //{{{ replaceAll() method /** * Replaces all occurances of the search string with the replacement * string. * @param view The view */ public static boolean replaceAll(View view) { // component that will parent any dialog boxes Component comp = SearchDialog.getSearchDialog(view); if(comp == null) comp = view; int fileCount = 0; int occurCount = 0; if(fileset.getFileCount(view) == 0) { GUIUtilities.error(comp,"empty-fileset",null); return false; } record(view,"replaceAll(view)",true,true); view.showWaitCursor(); boolean smartCaseReplace = (replace != null && TextUtilities.getStringCase(replace) == TextUtilities.LOWER_CASE); try { SearchMatcher matcher = getSearchMatcher(); if(matcher == null) return false; initReplace(); String path = fileset.getFirstFile(view);loop: while(path != null) { Buffer buffer = jEdit.openTemporary( view,null,path,false); /* this is stupid and misleading. * but 'path' is not used anywhere except * the above line, and if this is done * after the 'continue', then we will * either hang, or be forced to duplicate * it inside the buffer == null, or add * a 'finally' clause. you decide which one's * worse. */ path = fileset.getNextFile(view,path); if(buffer == null) continue loop; // Wait for buffer to finish loading if(buffer.isPerformingIO()) VFSManager.waitForRequests(); if(!buffer.isEditable()) continue loop; // Leave buffer in a consistent state if // an error occurs int retVal = 0; try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -