コード例 #1
0
ファイル: ed_search.py プロジェクト: wangdyna/wxPython
    def OnCount(self, evt):
        """Count the number of matches"""
        stc = self._stc()

        # Create the search engine
        query = evt.GetFindString()
        mode = evt.GetSearchType()
        engine = ebmlib.SearchEngine(query, evt.IsRegEx(), True,
                                     evt.IsMatchCase(), evt.IsWholeWord())

        if mode == eclib.LOCATION_CURRENT_DOC:
            engine.SetSearchPool(stc.GetTextRaw())
        elif mode == eclib.LOCATION_IN_SELECTION:
            engine.SetSearchPool(stc.GetSelectedTextRaw())
        else:
            # TODO: report that this is not supported yet
            #       this case should not happen as the count button is currently
            #       disabled for any conditions that fall into this case.
            return

        matches = engine.FindAll()
        if matches:
            count = len(matches)
        else:
            count = 0

        rmap = dict(term=query, count=count)
        wx.MessageBox(
            _("The search term \'%(term)s\' was found %(count)d times.") %
            rmap, _("Find Count"), wx.ICON_INFORMATION | wx.OK)
コード例 #2
0
    def testNormalizedFind(self):
        """Test searching for normalized Unicode data"""
        pool = unicodedata.normalize('NFC', u"école")
        query = unicodedata.normalize('NFD', u"école")
        self.assertTrue(pool != query)
        search = ebmlib.SearchEngine(query)
        search.SetSearchPool(pool)
        val = search.Find()
        self.assertTrue(val is not None)

        pool = unichr(0x00E9)
        query = u'e' + u'\u0301'
        self.assertTrue(pool != query)
        search = ebmlib.SearchEngine(query)
        search.SetSearchPool(u"foobar " + pool)
        val = search.Find()
        self.assertTrue(val is not None)
コード例 #3
0
ファイル: ed_search.py プロジェクト: wangdyna/wxPython
    def OnReplaceAll(self, evt):
        """Replace all instance of the search string with the given
        replace string for the given search context.

        """
        smode = evt.GetSearchType()
        rstring = evt.GetReplaceString()
        engine = ebmlib.SearchEngine(evt.GetFindString(), evt.IsRegEx(), True,
                                     evt.IsMatchCase(), evt.IsWholeWord())
        engine.SetResultFormatter(FormatResult)

        results = 0

        if smode == eclib.LOCATION_CURRENT_DOC:
            stc = self._stc()
            engine.SetSearchPool(stc.GetTextRaw())
            matches = engine.FindAll()
            if matches is not None:
                self.ReplaceInStc(stc, matches, rstring, evt.IsRegEx())
                results = len(matches)
        elif smode == eclib.LOCATION_OPEN_DOCS:
            for ctrl in self._parent.GetTextControls():
                engine.SetSearchPool(ctrl.GetTextRaw())
                matches = engine.FindAll()
                if matches is not None:
                    self.ReplaceInStc(ctrl, matches, rstring, evt.IsRegEx())
                    results += len(matches)
        elif smode == eclib.LOCATION_IN_FILES:
            dlg = wx.MessageDialog(
                self._parent,
                _("Sorry will be ready for next version"),
                _("Not implemented"),
                #                                   _("Warning this cannot be undone!"),
                #                                   _("Do Replace All?"),
                style=wx.ICON_WARNING | wx.OK | wx.CANCEL | wx.CENTER)
            result = dlg.ShowModal()
            dlg.Destroy()

            if result == wx.ID_OK:
                pass
#                path = evt.GetDirectory()
#                ed_msg.PostMessage(ed_msg.EDMSG_START_SEARCH,
#                                   (engine.SearchInDirectory,
#                                    [path,], dict(recursive=evt.IsRecursive())))
            else:
                return

        # Post number of matches that were replaced to the status bar
        if results > 0:
            ed_msg.PostMessage(
                ed_msg.EDMSG_UI_SB_TXT,
                (ed_glob.SB_INFO, _("%d matches were replaced.") % results))
コード例 #4
0
ファイル: ed_search.py プロジェクト: wangdyna/wxPython
    def OnFindAll(self, evt):
        """Find all results for the given query and display results in a
        L{SearchResultScreen} in the Shelf.

        """
        smode = evt.GetSearchType()
        query = evt.GetFindString()
        if not query:
            return

        # Create a new search engine object
        engine = ebmlib.SearchEngine(query, evt.IsRegEx(), True,
                                     evt.IsMatchCase(), evt.IsWholeWord())
        engine.SetResultFormatter(FormatResult)

        # Send the search function over to any interested parties that wish
        # to process the results.
        if smode == eclib.LOCATION_CURRENT_DOC:
            stc = self._stc()
            fname = stc.GetFileName()
            if len(fname):
                ed_msg.PostMessage(ed_msg.EDMSG_START_SEARCH,
                                   (engine.SearchInFile, [
                                       fname,
                                   ], dict()))
            else:
                engine.SetSearchPool(stc.GetTextRaw())
                ed_msg.PostMessage(ed_msg.EDMSG_START_SEARCH,
                                   (engine.FindAllLines, ))
        elif smode == eclib.LOCATION_OPEN_DOCS:
            files = [
                fname.GetFileName()
                for fname in self._parent.GetTextControls()
            ]
            ed_msg.PostMessage(ed_msg.EDMSG_START_SEARCH,
                               (engine.SearchInFiles, [
                                   files,
                               ], dict()))
        elif smode == eclib.LOCATION_IN_FILES:
            path = evt.GetDirectory()
            engine.SetFileFilters(evt.GetFileFilters())
            ed_msg.PostMessage(ed_msg.EDMSG_START_SEARCH,
                               (engine.SearchInDirectory, [
                                   path,
                               ], dict(recursive=evt.IsRecursive())))
コード例 #5
0
 def setUp(self):
     """Setup the test items"""
     self._def_eng = ebmlib.SearchEngine(u"", regex=False, down=True,
                                            matchcase=False, wholeword=False)
     self._ww_eng = ebmlib.SearchEngine(u"", regex=False, down=True,
                                           matchcase=False, wholeword=True)
     self._mc_eng = ebmlib.SearchEngine(u"", regex=False, down=True,
                                           matchcase=True, wholeword=False)
     self._regex_eng = ebmlib.SearchEngine(u"", regex=True, down=True,
                                              matchcase=False, wholeword=False)
     self._regexmc_eng = ebmlib.SearchEngine(u"", regex=True, down=True,
                                               matchcase=True, wholeword=False)
     self._regww_eng = ebmlib.SearchEngine(u"", regex=True, down=True,
                                              matchcase=False, wholeword=True)
     self._all_eng = ebmlib.SearchEngine(u"", regex=True, down=True,
                                            matchcase=True, wholeword=True)
コード例 #6
0
ファイル: ed_search.py プロジェクト: wangdyna/wxPython
    def OnCount(self, evt):
        """Count the number of matches"""
        stc = self._stc()

        # Create the search engine
        query = evt.GetFindString()
        engine = ebmlib.SearchEngine(query, evt.IsRegEx(), True,
                                     evt.IsMatchCase(), evt.IsWholeWord())
        engine.SetSearchPool(stc.GetTextRaw())

        matches = engine.FindAll()
        if matches:
            count = len(matches)
        else:
            count = 0

        rmap = dict(term=query, count=count)
        wx.MessageBox(
            _("The search term \'%(term)s\' was found %(count)d times.") %
            rmap, _("Find Count"), wx.ICON_INFORMATION | wx.OK)
コード例 #7
0
    def OnFind(self, evt, findnext=False):
        """Do an incremental search in the currently buffer
        @param evt: EVT_FIND, EVT_FIND_NEXT
        @keyword findnext: force a find next action

        """
        data = self.GetData()

        # Find next from menu event or called internally by replace
        if findnext or evt.GetEventType() == wx.wxEVT_COMMAND_MENU_SELECTED:

            # Adjust flags
            flags = data.GetFlags()
            if not findnext and evt.GetId() == ed_glob.ID_FIND_PREVIOUS:
                flags |= eclib.AFR_UP

            evt = eclib.FindEvent(eclib.edEVT_FIND_NEXT,
                                    flags=flags)
            evt.SetFindString(data.GetFindString())

        stc = self._stc()
        data.SetFindString(evt.GetFindString())

        # Create the search engine
        isdown = not evt.IsUp()
        engine = ebmlib.SearchEngine(data.GetFindString(), evt.IsRegEx(),
                                     isdown, evt.IsMatchCase(),
                                     evt.IsWholeWord())
        engine.SetResultFormatter(FormatResult)
        self._engine = engine

        # Check if expression was valid or not
        if engine.GetQueryObject() is None:
            wx.MessageBox(_("Invalid expression \"%s\"") % engine.GetQuery(),
                          _("Regex Compile Error"),
                          style=wx.OK|wx.CENTER|wx.ICON_ERROR)
            return

        # XXX: may be inefficent to copy whole buffer each time for files
        #      that are large.
        engine.SetSearchPool(stc.GetTextRaw())

        # Check if the direction changed
        ldir = self._posinfo['ldir']
        if isdown:
            self._posinfo['ldir'] = 'down'
        else:
            self._posinfo['ldir'] = 'up'

        # Get the search start position
        if evt.GetEventType() == eclib.edEVT_FIND:
            spos = self._posinfo['found']
        else:
            spos = stc.GetCurrentPos()
            if ldir != self._posinfo['ldir']:
                start, end = stc.GetSelection()
                if ldir == 'down':
                    spos = start
                else:
                    spos = end

        # Do the find
        match = engine.Find(spos)
        if match is not None:
            start, end = match

            if isdown:
                start = start + spos
                end = end + spos
                stc.SetSelection(start, end)
            else:
                stc.SetSelection(end, start)

            # Ensure caret and the line its in is exposed
            stc.EnsureCaretVisible()
            line = stc.LineFromPosition(start)
            stc.EnsureVisible(line)

            self._posinfo['found'] = start

            ed_msg.PostMessage(ed_msg.EDMSG_UI_SB_TXT,
                               (ed_glob.SB_INFO, u""))
        else:
            # try search from top again
            if isdown:
                match = engine.Find(0)
                ed_msg.PostMessage(ed_msg.EDMSG_UI_SB_TXT,
                                  (ed_glob.SB_INFO, _("Search wrapped to top")))
            else:
                match = engine.Find(-1)
                ed_msg.PostMessage(ed_msg.EDMSG_UI_SB_TXT,
                                  (ed_glob.SB_INFO,
                                  _("Search wrapped to bottom")))

            if match is not None:
                start, end = match

                self._posinfo['found'] = start

                match = list(match)
                if not isdown:
                    match.reverse()
                stc.SetSelection(match[0], match[1])
                
                # Ensure caret and the line its in is exposed
                stc.EnsureCaretVisible()
                line = stc.LineFromPosition(match[0])
                stc.EnsureVisible(line)
            else:
                self._posinfo['found'] = -1
                ed_msg.PostMessage(ed_msg.EDMSG_UI_SB_TXT,
                                  (ed_glob.SB_INFO,
                                  _("\"%s\" was not found") % engine.GetQuery()))