Class SearchAndReplace


  • public class SearchAndReplace
    extends java.lang.Object
    Class that implements regular expression and literal search within jEdit buffers.

    There are two main groups of methods in this class:

    • Property accessors - for changing search and replace settings.
    • Actions - for performing search and replace.
    The "HyperSearch" and "Keep dialog" features, as reflected in checkbox options in the search dialog, are not handled from within this class. If you wish to have these options set before the search dialog appears, make a prior call to either or both of the following:
     jEdit.setBooleanProperty("search.hypersearch.toggle",true);
     jEdit.setBooleanProperty("search.keepDialog.toggle",true);
    If you are not using the dialog to undertake a search or replace, you may call any of the search and replace methods (including hyperSearch(View)) without concern for the value of these properties.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String escapeRegexp​(java.lang.String str, boolean multiline)
      Escapes characters with special meaning in a regexp.
      static boolean find​(View view)
      Finds the next occurrence of the search string.
      static boolean find​(View view, Buffer buffer, int start)
      Finds the next instance of the search string in the specified buffer.
      static boolean find​(View view, Buffer buffer, int start, boolean firstTime, boolean reverse)
      Finds the next instance of the search string in the specified buffer.
      static boolean getAutoWrapAround()
      Returns the state of the auto wrap around flag.
      static boolean getBeanShellReplace()
      Returns the state of the BeanShell replace flag.
      static boolean getIgnoreCase()
      Returns the state of the ignore case flag.
      static boolean getRegexp()
      Returns the state of the regular expression flag.
      static java.lang.String getReplaceString()
      Returns the current replacement string.
      static boolean getReverseSearch()
      Returns the state of the reverse search flag.
      static SearchFileSet getSearchFileSet()
      Returns the current search file set.
      static SearchMatcher getSearchMatcher()
      Returns the current search string matcher.
      static java.lang.String getSearchString()
      Returns the current search string.
      static boolean getSmartCaseReplace()
      Returns if the replacement string will assume the same case as each specific occurrence of the search string.
      static boolean getWholeWord()
      Returns the state of the whole word flag.
      static boolean hyperSearch​(View view)
      Performs a HyperSearch.
      static boolean hyperSearch​(View view, boolean selection)
      Performs a HyperSearch.
      static void load()
      Loads search and replace state from the properties.
      static boolean replace​(View view)
      Replaces the current selection with the replacement string.
      static boolean replace​(View view, Buffer buffer, int start, int end)
      Replaces text in the specified range with the replacement string.
      static boolean replaceAll​(View view)
      Replaces all occurrences of the search string with the replacement string.
      static boolean replaceAll​(View view, boolean dontOpenChangedFiles)
      Replaces all occurrences of the search string with the replacement string.
      static void save()
      Saves search and replace state to the properties.
      static void setAutoWrapAround​(boolean wrap)
      Sets the state of the auto wrap around flag.
      static void setBeanShellReplace​(boolean beanshell)
      Sets the state of the BeanShell replace flag.
      static void setIgnoreCase​(boolean ignoreCase)
      Sets the ignore case flag.
      static void setRegexp​(boolean regexp)
      Sets the state of the regular expression flag.
      static void setReplaceString​(java.lang.String replace)
      Sets the current replacement string.
      static void setReverseSearch​(boolean reverse)
      Determines whether a reverse search will conducted from the current position to the beginning of a buffer.
      static void setSearchFileSet​(SearchFileSet fileset)
      Sets the current search file set.
      static void setSearchMatcher​(SearchMatcher matcher)
      Sets a custom search string matcher.
      static void setSearchString​(java.lang.String search)
      Sets the current search string.
      static void setWholeWord​(boolean wholeWord)
      Sets the whole word flag.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SearchAndReplace

        public SearchAndReplace()
    • Method Detail

      • setSearchString

        public static void setSearchString​(java.lang.String search)
        Sets the current search string.
        Parameters:
        search - The new search string
      • getSearchString

        public static java.lang.String getSearchString()
        Returns the current search string.
      • setReplaceString

        public static void setReplaceString​(java.lang.String replace)
        Sets the current replacement string.
        Parameters:
        replace - The new replacement string
      • getReplaceString

        public static java.lang.String getReplaceString()
        Returns the current replacement string.
      • setWholeWord

        public static void setWholeWord​(boolean wholeWord)
        Sets the whole word flag.
        Parameters:
        wholeWord - True if only whole words should be searched, false otherwise
        Since:
        4.5pre1
      • setIgnoreCase

        public static void setIgnoreCase​(boolean ignoreCase)
        Sets the ignore case flag.
        Parameters:
        ignoreCase - True if searches should be case insensitive, false otherwise
      • getWholeWord

        public static boolean getWholeWord()
        Returns the state of the whole word flag.
        Returns:
        True if only whole words should be searched, false otherwise
        Since:
        4.5pre1
      • getIgnoreCase

        public static boolean getIgnoreCase()
        Returns the state of the ignore case flag.
        Returns:
        True if searches should be case insensitive, false otherwise
      • setRegexp

        public static void setRegexp​(boolean regexp)
        Sets the state of the regular expression flag.
        Parameters:
        regexp - True if regular expression searches should be performed
      • getRegexp

        public static boolean getRegexp()
        Returns the state of the regular expression flag.
        Returns:
        True if regular expression searches should be performed
      • setReverseSearch

        public static void setReverseSearch​(boolean reverse)
        Determines whether a reverse search will conducted from the current position to the beginning of a buffer. Note that reverse search and regular expression search is mutually exclusive; enabling one will disable the other.
        Parameters:
        reverse - True if searches should go backwards, false otherwise
      • getReverseSearch

        public static boolean getReverseSearch()
        Returns the state of the reverse search flag.
        Returns:
        True if searches should go backwards, false otherwise
      • setBeanShellReplace

        public static void setBeanShellReplace​(boolean beanshell)
        Sets the state of the BeanShell replace flag.
        Parameters:
        beanshell - True if the replace string is a BeanShell expression
        Since:
        jEdit 3.2pre2
      • getBeanShellReplace

        public static boolean getBeanShellReplace()
        Returns the state of the BeanShell replace flag.
        Returns:
        True if the replace string is a BeanShell expression
        Since:
        jEdit 3.2pre2
      • setAutoWrapAround

        public static void setAutoWrapAround​(boolean wrap)
        Sets the state of the auto wrap around flag.
        Parameters:
        wrap - If true, the 'continue search from start' dialog will not be displayed
        Since:
        jEdit 3.2pre2
      • getAutoWrapAround

        public static boolean getAutoWrapAround()
        Returns the state of the auto wrap around flag.
        Since:
        jEdit 3.2pre2
      • getSearchMatcher

        public static SearchMatcher getSearchMatcher()
                                              throws java.lang.Exception
        Returns the current search string matcher.
        Returns:
        a SearchMatcher or null if there is no search or if the matcher can match empty String
        Throws:
        java.lang.IllegalArgumentException - if regular expression search is enabled, the search string or replacement string is invalid
        java.lang.Exception
        Since:
        jEdit 4.1pre7
      • getSearchFileSet

        public static SearchFileSet getSearchFileSet()
        Returns the current search file set.
      • getSmartCaseReplace

        public static boolean getSmartCaseReplace()
        Returns if the replacement string will assume the same case as each specific occurrence of the search string.
        Since:
        jEdit 4.2pre10
      • hyperSearch

        public static boolean hyperSearch​(View view)
        Performs a HyperSearch.
        Parameters:
        view - The view
        Since:
        jEdit 2.7pre3
      • hyperSearch

        public static boolean hyperSearch​(View view,
                                          boolean selection)
        Performs a HyperSearch.
        Parameters:
        view - The view
        selection - If true, will only search in the current selection. Note that the file set must be the current buffer file set for this to work.
        Since:
        jEdit 4.0pre1
      • find

        public static boolean find​(View view)
        Finds the next occurrence of the search string.
        Parameters:
        view - The view
        Returns:
        True if the operation was successful, false otherwise
      • find

        public static boolean find​(View view,
                                   Buffer buffer,
                                   int start)
                            throws java.lang.Exception
        Finds the next instance of the search string in the specified buffer.
        Parameters:
        view - The view
        buffer - The buffer
        start - Location where to start the search
        Throws:
        java.lang.Exception
      • find

        public static boolean find​(View view,
                                   Buffer buffer,
                                   int start,
                                   boolean firstTime,
                                   boolean reverse)
                            throws java.lang.Exception
        Finds the next instance of the search string in the specified buffer.
        Parameters:
        view - The view
        buffer - The buffer
        start - Location where to start the search
        firstTime - See SearchMatcher.nextMatch(CharSequence,boolean,boolean,boolean,boolean).
        Throws:
        java.lang.Exception
        Since:
        jEdit 4.1pre7
      • replace

        public static boolean replace​(View view)
        Replaces the current selection with the replacement string.
        Parameters:
        view - The view
        Returns:
        True if the operation was successful, false otherwise
      • replace

        public static boolean replace​(View view,
                                      Buffer buffer,
                                      int start,
                                      int end)
        Replaces text in the specified range with the replacement string.
        Parameters:
        view - The view
        buffer - The buffer
        start - The start offset
        end - The end offset
        Returns:
        True if the operation was successful, false otherwise
      • replaceAll

        public static boolean replaceAll​(View view)
        Replaces all occurrences of the search string with the replacement string.
        Parameters:
        view - The view
        Returns:
        the number of modified files
      • replaceAll

        public static boolean replaceAll​(View view,
                                         boolean dontOpenChangedFiles)
        Replaces all occurrences of the search string with the replacement string.
        Parameters:
        view - The view
        dontOpenChangedFiles - Whether to open changed files or to autosave them quietly
        Returns:
        the number of modified files
      • escapeRegexp

        public static java.lang.String escapeRegexp​(java.lang.String str,
                                                    boolean multiline)
        Escapes characters with special meaning in a regexp.
        Parameters:
        str - the string to escape
        multiline - Should \n be escaped?
        Returns:
        the string with escaped characters
        Since:
        jEdit 4.3pre1
      • load

        public static void load()
        Loads search and replace state from the properties.
      • save

        public static void save()
        Saves search and replace state to the properties.