From ffe0166e4e199d7386c7318d06e7ee1613159795 Mon Sep 17 00:00:00 2001 From: Kazuya O'moto Date: Sat, 5 Jul 2025 21:51:06 +0900 Subject: [PATCH] Enable whole-word search Add whole-word search functionality to DoFindNext * Use stc's SearchInTarget instead of str.find/rfind. Modify ShowPosition (override) * Reverted the comment line. Folded lines need to be expanded to ensure the target line is visible. --- wx/py/editwindow.py | 36 ++++++++++++++++-------------------- wx/py/frame.py | 7 +++---- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/wx/py/editwindow.py b/wx/py/editwindow.py index 8cb77751..f4058722 100644 --- a/wx/py/editwindow.py +++ b/wx/py/editwindow.py @@ -251,34 +251,30 @@ class EditWindow(stc.StyledTextCtrl): def ShowPosition(self, pos): line = self.LineFromPosition(pos) - #self.EnsureVisible(line) + self.EnsureVisible(line) # Expand the line if folded. self.GotoLine(line) def DoFindNext(self, findData, findDlg=None): backward = not (findData.GetFlags() & wx.FR_DOWN) - matchcase = (findData.GetFlags() & wx.FR_MATCHCASE) != 0 - end = self.GetLastPosition() - # Changed to reflect the fact that StyledTextControl is in UTF-8 encoding - textstring = self.GetRange(0, end).encode('utf-8') - findstring = findData.GetFindString().encode('utf-8') - if not matchcase: - textstring = textstring.lower() - findstring = findstring.lower() + matchcase = findData.GetFlags() & wx.FR_MATCHCASE + wholeword = findData.GetFlags() & wx.FR_WHOLEWORD + findstring = findData.GetFindString().encode() + self.SetSearchFlags(0 + | (wx.stc.STC_FIND_MATCHCASE if matchcase else 0) + | (wx.stc.STC_FIND_WHOLEWORD if wholeword else 0) + ) if backward: - start = self.GetSelection()[0] - loc = textstring.rfind(findstring, 0, start) + self.TargetStart = self.GetAnchor() # backward anchor + self.TargetEnd = 0 else: - start = self.GetSelection()[1] - loc = textstring.find(findstring, start) + self.TargetStart = self.GetCurrentPos() # forward anchor + self.TargetEnd = self.TextLength + loc = self.SearchInTarget(findstring) # if it wasn't found then restart at beginning - if loc == -1 and start != 0: - if backward: - start = end - loc = textstring.rfind(findstring, 0, start) - else: - start = 0 - loc = textstring.find(findstring, start) + if loc == -1: + self.TargetStart = self.TextLength if backward else 0 + loc = self.SearchInTarget(findstring) # was it still not found? if loc == -1: diff --git a/wx/py/frame.py b/wx/py/frame.py index efb02ffe..9c334fa6 100644 --- a/wx/py/frame.py +++ b/wx/py/frame.py @@ -536,11 +536,10 @@ class Frame(wx.Frame): return win = wx.Window.FindFocus() if self.shellName == 'PyCrust': - self.findDlg = wx.FindReplaceDialog(win, self.findData, - "Find",wx.FR_NOWHOLEWORD) + self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find") else: - self.findDlg = wx.FindReplaceDialog(win, self.findData, - "Find & Replace", wx.FR_NOWHOLEWORD|wx.FR_REPLACEDIALOG) + self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find & Replace", + wx.FR_REPLACEDIALOG) self.findDlg.Show() def OnFindNext(self, event, backward=False):