Ticket #39: userDictsWithCaseSensitiveAndRegexpOptions.patch

File userDictsWithCaseSensitiveAndRegexpOptions.patch, 9.1 KB (added by mdcurran, 4 years ago)

Patch that implements the features.

  • userdicts/default.dic

     
    1 #General NVDA dictionary 
    2 #syntax: record fields divided by tab character 
    3  
    4 #Limit groups of the same character to 5 or less. 
    5 ([\W])\1{5,}    \1\1\1\1\1 
     1#Limit groups of the same character to 5 or less. 
     2([\W])\1{5,}    \1\1\1\1\1      1       1 
    63#break up words that use a capital letter to denote another word 
    7 ([a-z])([A-Z])  \1 \2 
     4([a-z])([A-Z])  \1 \2   1       1 
    85#Break away a word starting with a capital from a fully uppercase word 
    9 ([A-Z])([A-Z][a-z])     \1 \2 
     6([A-Z])([A-Z][a-z])     \1 \2   1       1 
    107#telephone numbers 
    11 \b(\d{3})\-(\d{2})(\d{2})\W     \1 \2 \3 
     8\b(\d{3})\-(\d{2})(\d{2})\W     \1 \2 \3        1       1 
    129#numbers beginning on the zero 
    13 \b(0+)(\d+)     \1 \2 
     10\b(0+)(\d+)     \1 \2   1       1 
    1411#Break words that have numbers at the end 
    15 ((?:(?=\D)\w)+)(\d+)    \1 \2 
     12((?:(?=\D)\w)+)(\d+)    \1 \2   1       1 
  • gui/settingsDialogs.py

     
    588588                config.conf["documentFormatting"]["reportAlignment"]=self.alignmentCheckBox.IsChecked() 
    589589                super(DocumentFormattingDialog, self).onOk(evt) 
    590590 
     591class DictionaryEntryDialog(wx.Dialog): 
     592 
     593        def __init__(self,parent): 
     594                super(DictionaryEntryDialog,self).__init__(parent,title=_("Edit dictionary entry")) 
     595                mainSizer=wx.BoxSizer(wx.VERTICAL) 
     596                settingsSizer=wx.BoxSizer(wx.VERTICAL) 
     597                settingsSizer.Add(wx.StaticText(self,-1,label=_("&Pattern"))) 
     598                self.patternTextCtrl=wx.TextCtrl(self,wx.NewId()) 
     599                settingsSizer.Add(self.patternTextCtrl) 
     600                settingsSizer.Add(wx.StaticText(self,-1,label=_("&Replacement"))) 
     601                self.replacementTextCtrl=wx.TextCtrl(self,wx.NewId()) 
     602                settingsSizer.Add(self.replacementTextCtrl) 
     603                settingsSizer.Add(wx.StaticText(self,-1,label=_("&Comment"))) 
     604                self.commentTextCtrl=wx.TextCtrl(self,wx.NewId()) 
     605                settingsSizer.Add(self.commentTextCtrl) 
     606                self.caseSensitiveCheckBox=wx.CheckBox(self,wx.NewId(),label=_("Case &sensitive")) 
     607                settingsSizer.Add(self.caseSensitiveCheckBox) 
     608                self.regexpCheckBox=wx.CheckBox(self,wx.NewId(),label=_("Regular &expression")) 
     609                settingsSizer.Add(self.regexpCheckBox) 
     610                mainSizer.Add(settingsSizer,border=20,flag=wx.LEFT|wx.RIGHT|wx.TOP) 
     611                buttonSizer=self.CreateButtonSizer(wx.OK|wx.CANCEL) 
     612                mainSizer.Add(buttonSizer,border=20,flag=wx.LEFT|wx.RIGHT|wx.BOTTOM) 
     613                mainSizer.Fit(self) 
     614                self.SetSizer(mainSizer) 
     615                self.patternTextCtrl.SetFocus() 
     616 
    591617class DictionaryDialog(SettingsDialog): 
    592618 
    593619        def __init__(self,parent,title,userDict): 
     
    607633                self.dictList.InsertColumn(0,_("Comment")) 
    608634                self.dictList.InsertColumn(1,_("Pattern")) 
    609635                self.dictList.InsertColumn(2,_("Replacement")) 
     636                self.dictList.InsertColumn(3,_("case sensitive")) 
     637                self.dictList.InsertColumn(4,_("Regular expression")) 
    610638                for entry in self.tempUserDict: 
    611                         self.dictList.Append((entry.comment,entry.pattern,entry.replacement)) 
     639                        self.dictList.Append((entry.comment,entry.pattern,entry.replacement,str(entry.caseSensitive),str(entry.regexp))) 
    612640                self.editingIndex=-1 
    613641                entriesSizer.Add(self.dictList) 
    614642                settingsSizer.Add(entriesSizer) 
     
    641669                super(DictionaryDialog, self).onOk(evt) 
    642670 
    643671        def OnAddClick(self,evt): 
    644                 if self.editingIndex==-1: 
    645                         addEntryDialog=scriptUI.TextEntriesDialog((_("&Pattern"),_("&Replacement"),_("&Comment")),title=_("Add an entry"),callback=self.onDialog) 
    646                         addEntryDialog.run() 
     672                entryDialog=DictionaryEntryDialog(self) 
     673                if entryDialog.ShowModal()==wx.ID_OK: 
     674                        self.tempUserDict.append(userDictHandler.UserDictEntry(entryDialog.patternTextCtrl.GetValue(),entryDialog.replacementTextCtrl.GetValue(),entryDialog.commentTextCtrl.GetValue(),entryDialog.caseSensitiveCheckBox.GetValue(),entryDialog.regexpCheckBox.GetValue())) 
     675                        self.dictList.Append((entryDialog.commentTextCtrl.GetValue(),entryDialog.patternTextCtrl.GetValue(),entryDialog.replacementTextCtrl.GetValue(),str(bool(entryDialog.caseSensitiveCheckBox.GetValue())),str(bool(entryDialog.regexpCheckBox.GetValue())))) 
     676                        index=self.dictList.GetFirstSelected() 
     677                        while index>=0: 
     678                                self.dictList.Select(index,on=0) 
     679                                index=self.dictList.GetNextSelected(index) 
     680                        addedIndex=self.dictList.GetItemCount()-1 
     681                        self.dictList.Select(addedIndex) 
     682                        self.dictList.Focus(addedIndex) 
     683                        self.dictList.SetFocus() 
     684                entryDialog.Destroy() 
    647685 
    648686        def OnEditClick(self,evt): 
    649                 if (self.dictList.GetSelectedItemCount()==1) and (self.editingIndex==-1): 
    650                         self.editingIndex=self.dictList.GetNextItem(-1,wx.LIST_NEXT_ALL,wx.LIST_STATE_SELECTED) 
    651                         editEntryDialog=scriptUI.TextEntriesDialog((_("&Pattern"),_("&Replacement"),_("&Comment")),title=_("Edit an entry"),defaults=(self.tempUserDict[self.editingIndex].pattern,self.tempUserDict[self.editingIndex].replacement,self.tempUserDict[self.editingIndex].comment), callback=self.onDialog) 
    652                         editEntryDialog.run() 
     687                if self.dictList.GetSelectedItemCount()!=1: 
     688                        return 
     689                editIndex=self.dictList.GetFirstSelected() 
     690                if editIndex<0: 
     691                        return 
     692                entryDialog=DictionaryEntryDialog(self) 
     693                entryDialog.patternTextCtrl.SetValue(self.tempUserDict[editIndex].pattern) 
     694                entryDialog.replacementTextCtrl.SetValue(self.tempUserDict[editIndex].replacement) 
     695                entryDialog.commentTextCtrl.SetValue(self.tempUserDict[editIndex].comment) 
     696                entryDialog.caseSensitiveCheckBox.SetValue(self.tempUserDict[editIndex].caseSensitive) 
     697                entryDialog.regexpCheckBox.SetValue(self.tempUserDict[editIndex].regexp) 
     698                if entryDialog.ShowModal()==wx.ID_OK: 
     699                        self.tempUserDict[editIndex].comment=entryDialog.commentTextCtrl.GetValue() 
     700                        self.dictList.SetStringItem(editIndex,0,entryDialog.commentTextCtrl.GetValue()) 
     701                        self.tempUserDict[editIndex].pattern=entryDialog.patternTextCtrl.GetValue() 
     702                        self.dictList.SetStringItem(editIndex,1,entryDialog.patternTextCtrl.GetValue()) 
     703                        self.tempUserDict[editIndex].replacement=entryDialog.replacementTextCtrl.GetValue() 
     704                        self.dictList.SetStringItem(editIndex,2,entryDialog.replacementTextCtrl.GetValue()) 
     705                        self.tempUserDict[editIndex].caseSensitive=bool(entryDialog.caseSensitiveCheckBox.GetValue()) 
     706                        self.dictList.SetStringItem(editIndex,3,str(bool(entryDialog.caseSensitiveCheckBox.GetValue()))) 
     707                        self.tempUserDict[editIndex].regexp=bool(entryDialog.regexpCheckBox.GetValue()) 
     708                        self.dictList.SetStringItem(editIndex,4,str(bool(entryDialog.regexpCheckBox.GetValue()))) 
     709                        self.dictList.SetFocus() 
     710                entryDialog.Destroy() 
    653711 
    654712        def OnRemoveClick(self,evt): 
    655                 index=self.dictList.GetNextItem(-1,wx.LIST_NEXT_ALL,wx.LIST_STATE_SELECTED) 
    656                 if (index!=self.editingIndex)and(self.dictList.GetSelectedItemCount()==1): 
     713                index=self.dictList.GetFirstSelected() 
     714                while index>=0: 
    657715                        self.dictList.DeleteItem(index) 
    658716                        del self.tempUserDict[index] 
     717                        index=self.dictList.GetNextSelected(index) 
    659718                self.dictList.SetFocus() 
    660  
    661         def onDialog(self,texts): 
    662                 if texts is not None: 
    663                         if self.editingIndex>=0: 
    664                                 self.tempUserDict[self.editingIndex]=userDictHandler.UserDictEntry(texts[0],texts[1],texts[2]) 
    665                                 self.dictList.SetStringItem(self.editingIndex,1,texts[0]) 
    666                                 self.dictList.SetStringItem(self.editingIndex,2,texts[1]) 
    667                                 self.dictList.SetStringItem(self.editingIndex,0,texts[2]) 
    668                         else: 
    669                                 self.tempUserDict.append(userDictHandler.UserDictEntry(texts[0],texts[1],texts[2])) 
    670                                 self.dictList.Append((texts[2],texts[0],texts[1])) 
    671                 self.editingIndex=-1 
    672                 self.dictList.SetFocus() 
  • userDictHandler.py

     
    1616 
    1717class UserDictEntry: 
    1818 
    19         def __init__(self, pattern, replacement,comment): 
     19        def __init__(self, pattern, replacement,comment,caseSensitive=True,regexp=False): 
    2020                self.pattern = pattern 
    21                 self.compiled = re.compile(pattern) 
     21                flags=re.IGNORECASE if not caseSensitive else 0 
     22                tempPattern=pattern if regexp else re.escape(pattern) 
     23                self.compiled = re.compile(tempPattern,flags) 
    2224                self.replacement = replacement 
    2325                self.comment=comment 
     26                self.caseSensitive=caseSensitive 
     27                self.regexp=regexp 
    2428 
    2529        def sub(self, text): 
    26                 return self.compiled.sub(self.replacement, text) 
     30                replacement=self.replacement if self.regexp else re.escape(self.replacement) 
     31                return self.compiled.sub(replacement, text) 
    2732 
    2833class UserDict(list): 
    2934 
     
    4752                                comment+=line[1:] 
    4853                        else: 
    4954                                temp=line.split("\t") 
    50                                 if len(temp) ==2: 
    51                                         self.append(UserDictEntry(temp[0],temp[1],comment)) 
     55                                if len(temp) ==4: 
     56                                        self.append(UserDictEntry(temp[0],temp[1],comment,bool(int(temp[2])),bool(int(temp[3])))) 
    5257                                        comment="" 
    5358                                else: 
    5459                                        globalVars.log.warning("can't parse line '%s'" % line) 
     
    6469                        for entry in self: 
    6570                                if entry.comment: 
    6671                                        file.write("#%s\r\n"%entry.comment) 
    67                                 file.write("%s\t%s\r\n"%(entry.pattern,entry.replacement)) 
     72                                file.write("%s\t%s\t%s\t%s\r\n"%(entry.pattern,entry.replacement,int(entry.caseSensitive),int(entry.regexp))) 
    6873                        file.close() 
    6974 
    7075        def sub(self, text):