def OnReplace(self, evt): """Replace the selected text in the current buffer @param evt: finddlg.EVT_REPLACE """ replacestring = evt.GetReplaceString() if evt.IsRegEx() and self._engine is not None: match = self._engine.GetLastMatch() if match is not None: value = match.expand(replacestring) else: value = replacestring else: value = replacestring sel = self._stc().GetSelection() if sel[0] == sel[1]: return self._stc().ReplaceSelection(value) # Go to the next match # Fake event object for on Find Handler tevt = eclib.FindEvent(eclib.edEVT_FIND_NEXT, ed_glob.ID_FIND_PREVIOUS) self.OnFind(tevt, True)
def DoSearch(self, next=True): """Do the search and move the selection @keyword next: search next or previous """ s_cmd = eclib.edEVT_FIND if not next: self.SetSearchFlag(eclib.AFR_UP) else: if eclib.AFR_UP & self._flags: self.ClearSearchFlag(eclib.AFR_UP) if self.GetValue() == self._last: s_cmd = eclib.edEVT_FIND_NEXT evt = eclib.FindEvent(s_cmd, flags=self._flags) self._last = self.GetValue() evt.SetFindString(self.GetValue()) self.FindService.OnFind(evt) # Give feedback on whether text was found or not if self.FindService.GetLastFound() < 0 and len(self.GetValue()) > 0: if self._txtctrl is None: self.SetForegroundColour(wx.RED) else: self._txtctrl.SetForegroundColour(wx.RED) wx.Bell() else: # ?wxBUG? cant set text back to black after changing color # But setting it to this almost black color works. Most likely its # due to bit masking but I havent looked at the source so I am not # sure if self._txtctrl is None: self.SetForegroundColour(wx.ColourRGB(0 | 1 | 0)) else: self._txtctrl.SetForegroundColour(wx.ColourRGB(0 | 1 | 0)) self.Refresh()
def FindAll(self): """Fire off a FindAll job in the current buffer""" evt = eclib.FindEvent(eclib.edEVT_FIND_ALL, flags=self._flags) evt.SetFindString(self.GetValue()) self.FindService.OnFindAll(evt)
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() self._engine.SetQuery(data.GetFindString()) self._engine.SetFlags(isregex=evt.IsRegEx(), matchcase=evt.IsMatchCase(), wholeword=evt.IsWholeWord(), down=isdown) # Check if expression was valid or not if self._engine.GetQueryObject() is None: fail = ed_txt.DecodeString(self._engine.GetQuery(), 'utf-8') wx.MessageBox(_("Invalid expression \"%s\"") % fail, _("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. self._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 = self._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 = self._engine.Find(0) ed_msg.PostMessage( ed_msg.EDMSG_UI_SB_TXT, (ed_glob.SB_INFO, _("Search wrapped to top"))) else: match = self._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 fail = ed_txt.DecodeString(self._engine.GetQuery(), 'utf-8') ed_msg.PostMessage( ed_msg.EDMSG_UI_SB_TXT, (ed_glob.SB_INFO, _("\"%s\" was not found") % fail))
wx.MessageBox(msg, _("Replace Error"), wx.OK|wx.ICON_ERROR) return else: value = replacestring else: value = replacestring sel = self._stc().GetSelection() if sel[0] == sel[1]: return self._stc().ReplaceSelection(value) # Go to the next match # Fake event object for on Find Handler tevt = eclib.FindEvent(eclib.edEVT_FIND_NEXT, ed_glob.ID_FIND_PREVIOUS) self.OnFind(tevt, True) 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 = EdSearchEngine(evt.GetFindString(), evt.IsRegEx(), True, evt.IsMatchCase(), evt.IsWholeWord()) engine.SetResultFormatter(engine.FormatResult) results = 0 if smode == eclib.LOCATION_CURRENT_DOC: