⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchandreplace.java

📁 Java写的文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						jEdit.getProperty("view.status.auto-wrap"));					restart = true;				}				else				{					Integer[] args = { new Integer(reverse ? 1 : 0) };					int result = GUIUtilities.confirm(view,						"keepsearching",args,						JOptionPane.YES_NO_OPTION,						JOptionPane.QUESTION_MESSAGE);					restart = (result == JOptionPane.YES_OPTION);				}				if(restart)				{					// start search from beginning					buffer = fileset.getFirstBuffer(view);					repeat = true;				}				else					break loop;			}		}		catch(Exception e)		{			Log.log(Log.ERROR,SearchAndReplace.class,e);			Object[] args = { e.getMessage() };			if(args[0] == null)				args[0] = e.toString();			GUIUtilities.error(view,"searcherror",args);		}		finally		{			view.hideWaitCursor();		}		return false;	}	/**	 * 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(final View view, final Buffer buffer, final int start)		throws Exception	{		SearchMatcher matcher = getSearchMatcher(true);		Segment text = new Segment();		if(reverse)			buffer.getText(0,start,text);		else			buffer.getText(start,buffer.getLength() - start,text);		int[] match = matcher.nextMatch(text);		if(match != null)		{			fileset.matchFound(buffer);			view.setBuffer(buffer);			JEditTextArea textArea = view.getTextArea();			int matchStart = (reverse ? 0 : start);			textArea.setSelection(new Selection.Range(				matchStart + match[0],				matchStart + match[1]));			textArea.moveCaretPosition(matchStart + match[1]);			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)	{		JEditTextArea textArea = view.getTextArea();		Selection[] selection = textArea.getSelection();		if(selection.length == 0)		{			view.getToolkit().beep();			return false;		}		record(view,"replace(view)",true,false);		Buffer buffer = view.getBuffer();		try		{			buffer.beginCompoundEdit();			int retVal = 0;			for(int i = 0; i < selection.length; i++)			{				Selection s = selection[i];				/* if an occurence occurs at the				beginning of the selection, the				selection start will get moved.				this sucks, so we hack to avoid it. */				int start = s.getStart();				retVal += _replace(view,buffer,					s.getStart(),s.getEnd());				// this has no effect if the selection				// no longer exists				textArea.removeFromSelection(s);				if(s instanceof Selection.Range)				{					textArea.addToSelection(new Selection.Range(						start,s.getEnd()));				}				else if(s instanceof Selection.Rect)				{					textArea.addToSelection(new Selection.Rect(						start,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.getMessage() };			if(args[0] == null)				args[0] = e.toString();			GUIUtilities.error(view,"searcherror",args);		}		finally		{			buffer.endCompoundEdit();		}		return false;	}	/**	 * 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)	{		JEditTextArea textArea = view.getTextArea();		try		{			int retVal = 0;			buffer.beginCompoundEdit();			retVal += _replace(view,buffer,start,end);			if(retVal != 0)				return true;		}		catch(Exception e)		{			Log.log(Log.ERROR,SearchAndReplace.class,e);			Object[] args = { e.getMessage() };			if(args[0] == null)				args[0] = e.toString();			GUIUtilities.error(view,"searcherror",args);		}		finally		{			buffer.endCompoundEdit();		}		return false;	}	/**	 * Replaces all occurances of the search string with the replacement	 * string.	 * @param view The view	 */	public static boolean replaceAll(View view)	{		int fileCount = 0;		int occurCount = 0;		record(view,"replaceAll(view)",true,true);		view.showWaitCursor();		try		{			Buffer buffer = fileset.getFirstBuffer(view);			do			{				// Wait for buffer to finish loading				if(buffer.isPerformingIO())					VFSManager.waitForRequests();				// Leave buffer in a consistent state if				// an error occurs				try				{					buffer.beginCompoundEdit();					int retVal = _replace(view,buffer,						0,buffer.getLength());					if(retVal != 0)					{						fileCount++;						occurCount += retVal;						fileset.matchFound(buffer);					}				}				finally				{					buffer.endCompoundEdit();				}			}			while((buffer = fileset.getNextBuffer(view,buffer)) != null);		}		catch(Exception e)		{			Log.log(Log.ERROR,SearchAndReplace.class,e);			Object[] args = { e.getMessage() };			if(args[0] == null)				args[0] = e.toString();			GUIUtilities.error(view,"searcherror",args);		}		finally		{			view.hideWaitCursor();		}		/* Don't do this when playing a macro, cos it's annoying */		if(!BeanShell.isScriptRunning())		{			Object[] args = { new Integer(occurCount),				new Integer(fileCount) };			view.getStatus().setMessageAndClear(jEdit.getProperty(				"view.status.replace-all",args));		}		return (fileCount != 0);	}	/**	 * Loads search and replace state from the properties.	 */	public static void load()	{		search = jEdit.getProperty("search.find.value");		replace = jEdit.getProperty("search.replace.value");		ignoreCase = jEdit.getBooleanProperty("search.ignoreCase.toggle");		regexp = jEdit.getBooleanProperty("search.regexp.toggle");		reverse = jEdit.getBooleanProperty("search.reverse.toggle");		beanshell = jEdit.getBooleanProperty("search.beanshell.toggle");		wrap = jEdit.getBooleanProperty("search.wrap.toggle");		String filesetCode = jEdit.getProperty("search.fileset.value");		if(filesetCode != null)		{			fileset = (SearchFileSet)BeanShell.eval(null,filesetCode,true);		}		if(fileset == null)			fileset = new CurrentBufferSet();	}	/**	 * Saves search and replace state to the properties.	 */	public static void save()	{		jEdit.setProperty("search.find.value",search);		jEdit.setProperty("search.replace.value",replace);		jEdit.setBooleanProperty("search.ignoreCase.toggle",ignoreCase);		jEdit.setBooleanProperty("search.regexp.toggle",regexp);		jEdit.setBooleanProperty("search.reverse.toggle",reverse);		jEdit.setBooleanProperty("search.beanshell.toggle",beanshell);		jEdit.setBooleanProperty("search.wrap.toggle",wrap);		String code = fileset.getCode();		if(code != null)			jEdit.setProperty("search.fileset.value",code);		else			jEdit.unsetProperty("search.fileset.value");	}	// private members	private static String search;	private static String replace;	private static boolean regexp;	private static boolean ignoreCase;	private static boolean reverse;	private static boolean beanshell;	private static boolean wrap;	private static SearchMatcher matcher;	private static SearchFileSet fileset;	private static void record(View view, String action,		boolean replaceAction, boolean recordFileSet)	{		Macros.Recorder recorder = view.getMacroRecorder();		if(recorder != null)		{			recorder.record("SearchAndReplace.setSearchString(\""				+ MiscUtilities.charsToEscapes(search) + "\");");			if(replaceAction)			{				recorder.record("SearchAndReplace.setReplaceString(\""					+ MiscUtilities.charsToEscapes(replace) + "\");");				recorder.record("SearchAndReplace.setBeanShellReplace("					+ beanshell + ");");			}			else			{				// only record this if doing a find next				recorder.record("SearchAndReplace.setAutoWrapAround("					+ wrap + ");");				recorder.record("SearchAndReplace.setReverseSearch("					+ reverse + ");");			}			recorder.record("SearchAndReplace.setIgnoreCase("				+ ignoreCase + ");");			recorder.record("SearchAndReplace.setRegexp("				+ regexp + ");");			if(recordFileSet)			{				recorder.record("SearchAndReplace.setSearchFileSet("					+ fileset.getCode() + ");");			}			recorder.record("SearchAndReplace." + action + ";");		}	}	/**	 * Replaces all occurances of the search string 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 replace operation was successful, false	 * if no matches were found	 */	private static int _replace(View view, Buffer buffer,		int start, int end) throws Exception	{		if(!buffer.isEditable())			return 0;		SearchMatcher matcher = getSearchMatcher(false);		if(matcher == null)			return 0;		int occurCount = 0;		Segment text = new Segment();		int offset = start;loop:		for(;;)		{			buffer.getText(offset,end - offset,text);			int[] occur = matcher.nextMatch(text);			if(occur == null)				break loop;			int _start = occur[0];			int _length = occur[1] - occur[0];			String found = new String(text.array,text.offset + _start,_length);			String subst = matcher.substitute(found);			if(subst != null)			{				buffer.remove(offset + _start,_length);				buffer.insertString(offset + _start,subst,null);				occurCount++;				offset += _start + subst.length();				end += (subst.length() - found.length());			}			else				offset += _start + _length;		}		return occurCount;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -