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

📄 searchandreplace.java

📁 Java写的文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * SearchAndReplace.java - Search and replace * Copyright (C) 1999, 2000, 2001 Slava Pestov * Portions copyright (C) 2001 Tom Locke * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package org.gjt.sp.jedit.search;import javax.swing.text.BadLocationException;import javax.swing.text.Segment;import javax.swing.JOptionPane;import java.awt.Component;import org.gjt.sp.jedit.io.VFSManager;import org.gjt.sp.jedit.msg.SearchSettingsChanged;import org.gjt.sp.jedit.textarea.*;import org.gjt.sp.jedit.*;import org.gjt.sp.util.Log;/** * Class that implements regular expression and literal search within * jEdit buffers. * @author Slava Pestov * @version $Id: SearchAndReplace.java,v 1.1.1.1 2001/09/02 05:37:58 spestov Exp $ */public class SearchAndReplace{	/**	 * Sets the current search string.	 * @param search The new search string	 */	public static void setSearchString(String search)	{		if(search.equals(SearchAndReplace.search))			return;		SearchAndReplace.search = search;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the current search string.	 */	public static String getSearchString()	{		return search;	}	/**	 * Sets the current replacement string.	 * @param search The new replacement string	 */	public static void setReplaceString(String replace)	{		if(replace.equals(SearchAndReplace.replace))			return;		SearchAndReplace.replace = replace;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the current replacement string.	 */	public static String getReplaceString()	{		return replace;	}	/**	 * Sets the ignore case flag.	 * @param ignoreCase True if searches should be case insensitive,	 * false otherwise	 */	public static void setIgnoreCase(boolean ignoreCase)	{		if(ignoreCase == SearchAndReplace.ignoreCase)			return;		SearchAndReplace.ignoreCase = ignoreCase;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the state of the ignore case flag.	 * @return True if searches should be case insensitive,	 * false otherwise	 */	public static boolean getIgnoreCase()	{		return ignoreCase;	}	/**	 * Sets the state of the regular expression flag.	 * @param regexp True if regular expression searches should be	 * performed	 */	public static void setRegexp(boolean regexp)	{		if(regexp == SearchAndReplace.regexp)			return;		SearchAndReplace.regexp = regexp;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the state of the regular expression flag.	 * @return True if regular expression searches should be performed	 */	public static boolean getRegexp()	{		return regexp;	}	/**	 * Sets the reverse search flag. Note that currently, only literal	 * reverse searches are supported.	 * @param reverse True if searches should go backwards,	 * false otherwise	 */	public static void setReverseSearch(boolean reverse)	{		if(reverse == SearchAndReplace.reverse)			return;		SearchAndReplace.reverse = reverse;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the state of the reverse search flag.	 * @return True if searches should go backwards,	 * false otherwise	 */	public static boolean getReverseSearch()	{		return reverse;	}	/**	 * Sets the state of the BeanShell replace flag.	 * @param regexp True if the replace string is a BeanShell expression	 * @since jEdit 3.2pre2	 */	public static void setBeanShellReplace(boolean beanshell)	{		if(beanshell == SearchAndReplace.beanshell)			return;		SearchAndReplace.beanshell = beanshell;		matcher = null;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the state of the BeanShell replace flag.	 * @return True if the replace string is a BeanShell expression	 * @since jEdit 3.2pre2	 */	public static boolean getBeanShellReplace()	{		return beanshell;	}	/**	 * Sets the state of the auto wrap around flag.	 * @param wrap If true, the 'continue search from start' dialog	 * will not be displayed	 * @since jEdit 3.2pre2	 */	public static void setAutoWrapAround(boolean wrap)	{		if(wrap == SearchAndReplace.wrap)			return;		SearchAndReplace.wrap = wrap;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the state of the auto wrap around flag.	 * @param wrap If true, the 'continue search from start' dialog	 * will not be displayed	 * @since jEdit 3.2pre2	 */	public static boolean getAutoWrapAround()	{		return wrap;	}	/**	 * Sets the current search string matcher. Note that calling	 * <code>setSearchString</code>, <code>setReplaceString</code>,	 * <code>setIgnoreCase</code> or <code>setRegExp</code> will	 * reset the matcher to the default.	 */	public static void setSearchMatcher(SearchMatcher matcher)	{		SearchAndReplace.matcher = matcher;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the current search string matcher.	 * @exception IllegalArgumentException if regular expression search	 * is enabled, the search string or replacement string is invalid	 */	public static SearchMatcher getSearchMatcher()		throws Exception	{		return getSearchMatcher(true);	}	/**	 * Returns the current search string matcher.	 * @param reverseOK Replacement commands need a non-reversed matcher,	 * so they set this to false	 * @exception IllegalArgumentException if regular expression search	 * is enabled, the search string or replacement string is invalid	 */	public static SearchMatcher getSearchMatcher(boolean reverseOK)		throws Exception	{		reverseOK &= (fileset instanceof CurrentBufferSet);		if(matcher != null && (reverseOK || !reverse))			return matcher;		if(search == null || "".equals(search))			return null;		// replace must not be null		String replace = (SearchAndReplace.replace == null ? "" : SearchAndReplace.replace);		String replaceMethod;		if(beanshell && replace.length() != 0)		{			replaceMethod = BeanShell.cacheBlock("replace","return ("				+ replace + ");",false);		}		else			replaceMethod = null;		if(regexp)			matcher = new RESearchMatcher(search,replace,ignoreCase,				beanshell,replaceMethod);		else		{			matcher = new BoyerMooreSearchMatcher(search,replace,				ignoreCase,reverse && reverseOK,beanshell,				replaceMethod);		}		return matcher;	}	/**	 * Sets the current search file set.	 * @param fileset The file set to perform searches in	 */	public static void setSearchFileSet(SearchFileSet fileset)	{		SearchAndReplace.fileset = fileset;		EditBus.send(new SearchSettingsChanged(null));	}	/**	 * Returns the current search file set.	 */	public static SearchFileSet getSearchFileSet()	{		return fileset;	}	/**	 * Performs a HyperSearch.	 * @param view The view	 * @since jEdit 2.7pre3	 */	public static boolean hyperSearch(View view)	{		record(view,"hyperSearch(view)",false,true);		view.getDockableWindowManager().addDockableWindow(			HyperSearchResults.NAME);		final HyperSearchResults results = (HyperSearchResults)			view.getDockableWindowManager()			.getDockableWindow(HyperSearchResults.NAME);		results.searchStarted();		try		{			VFSManager.runInWorkThread(new HyperSearchRequest(view,				getSearchMatcher(false),results));			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);			return false;		}	}	/**	 * Finds the next occurance of the search string.	 * @param view The view	 * @return True if the operation was successful, false otherwise	 */	public static boolean find(View view)	{		boolean repeat = false;		Buffer buffer = fileset.getNextBuffer(view,null);		try		{			SearchMatcher matcher = getSearchMatcher(true);			if(matcher == null)			{				view.getToolkit().beep();				return false;			}			record(view,"find(view)",false,true);			view.showWaitCursor();loop:			for(;;)			{				while(buffer != null)				{					// 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))						return true;					buffer = fileset.getNextBuffer(view,buffer);				}				if(repeat)				{					// no point showing this dialog box twice					view.getToolkit().beep();					return false;				}				/* Don't do this when playing a macro */				if(BeanShell.isScriptRunning())					break loop;				boolean restart;				if(wrap)				{					view.getStatus().setMessageAndClear(

⌨️ 快捷键说明

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