Menu

[r175]: / drplugins / Switcheroo.py  Maximize  Restore  History

Download this file

253 lines (200 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Programmer: Daniel Pozmanter
# E-mail: drpython@bluebottle.com
# Note: You must reply to the verification e-mail to get through.
#
# Copyright 2003-2004 Daniel Pozmanter
#
# Distributed under the terms of the GPL (GNU Public License)
#
# DrPython is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#Plugin
#Switcheroo
'''
1.1.0:
Updates for 3.10.6
Dialog fixes/tweaks.
Now works in the prompt as well.
Moved html into a text dialog.
For Version 1.0.4:
Changes:
Updated install/uninstall, toolbar for drpython 3.3.0
For Version 1.0.3:
Changes:
Updated Find History to use new drFindTextControl method.
For Version: 1.0.2:
Changes:
Added History Support.
Added Menu Button.
For Version: 1.0.1:
Changes:
Updates for drFindTextCtrl
Add to Search Menu instead of Edit Menu
'''
import wx
import wx.lib.dialogs
from drFindReplaceDialog import drFindTextCtrl
def OnAbout(DrFrame):
AboutString = '''Version: 1.1.0
By Daniel Pozmanter
Released under the GPL.'''
DrFrame.ShowMessage(AboutString, "About Switcheroo")
def OnHelp(DrFrame):
HelpString = '''Switcheroo:
Switcheroo is like replace, except instead of replacing "Text A" with "Text B",
it replaces every instance of "Text A" with "Text B",
and every instance of "Text B" with "Text A".
So using Switcheroo and selecting:
Switch: Dog
With: Cat
on the following text:
"The Dog chased the Cat, and the Cat bit back."
Will yield:
"The Cat chased the Dog, and the Dog bit back."
'''
DrFrame.ShowMessage(HelpString, "Switcheroo Help")
def UnInstall(DrFrame):
plugindir = DrFrame.pluginsdirectory
if os.path.exists(plugindir + "/bitmaps/16/Switcheroo.png"):
os.remove(plugindir + "/bitmaps/16/Switcheroo.png")
if os.path.exists(plugindir + "/bitmaps/24/Switcheroo.png"):
os.remove(plugindir + "/bitmaps/24/Switcheroo.png")
DrFrame.RemovePluginIcon("Switcheroo")
return True
class drSwitcherooDialog(wx.Dialog):
def __init__(self, parent, id, title, stc):
wx.Dialog.__init__(self, parent, id, title, wx.Point(50, 50), wx.Size(375, 275), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
self.stc = stc
self.ID_SWITCH = 3001
self.ID_CANCEL = 3002
self.ID_CHK_REGEX = 1010
self.ID_CREATERE_A = 1011
self.ID_CREATERE_B = 1012
self.ID_BTNA = 98
self.ID_BTNB = 99
self.parent = parent
self.txtA = drFindTextCtrl(self, id, "", wx.DefaultPosition, wx.Size(175, -1), self.OnbtnSwitch)
self.txtA.SetFocus()
self.txtB = drFindTextCtrl(self, id, "", wx.DefaultPosition, wx.Size(175, -1), self.OnbtnSwitch)
self.txtA.SetHistory(parent.SwitcherooHistory)
self.txtB.SetHistory(parent.SwitcherooHistory)
self.btnPopUpA = wx.Button(self, self.ID_BTNA, " Menu ")
self.btnPopUpB = wx.Button(self, self.ID_BTNB, " Menu ")
self.chkCase = wx.CheckBox(self, id, "&Match Case")
self.chkWholeWord = wx.CheckBox(self, id, "Match &Whole Word")
self.chkInSelection = wx.CheckBox(self, id, "In Selection")
#Prefs
self.chkCase.SetValue(parent.prefs.findreplacematchcase)
self.chkWholeWord.SetValue(parent.prefs.findreplacewholeword)
self.chkInSelection.SetValue(parent.prefs.findreplaceinselection)
self.btnSwitch = wx.Button(self, self.ID_SWITCH, "&Switch All")
self.btnSwitch.SetDefault()
self.btnCancel = wx.Button(self, self.ID_CANCEL, "&Cancel")
self.theSizer = wx.FlexGridSizer(8, 3, 5, 10)
#Size Buffer
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, "Switch: "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(self.txtA, 1, wx.SHAPED)
self.theSizer.Add(self.btnPopUpA, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, "With: "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(self.txtB, 1, wx.SHAPED)
self.theSizer.Add(self.btnPopUpB, 1, wx.SHAPED)
#Size Buffer
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(self.chkCase, 1, wx.SHAPED)
self.theSizer.Add(self.chkWholeWord, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(self.chkInSelection, 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
#Size Buffer
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 1, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, id, " "), 1, wx.SHAPED)
self.theSizer.Add(self.btnSwitch, 1, wx.SHAPED)
self.theSizer.Add(self.btnCancel, 1, wx.SHAPED)
self.SetAutoLayout(True)
self.SetSizer(self.theSizer)
self.Bind(wx.EVT_BUTTON, self.OnbtnSwitch, id=self.ID_SWITCH)
self.Bind(wx.EVT_BUTTON, self.OnbtnCancel, id=self.ID_CANCEL)
self.Bind(wx.EVT_BUTTON, self.OnbtnPopUp, id=self.ID_BTNA)
self.Bind(wx.EVT_BUTTON, self.OnbtnPopUp, id=self.ID_BTNB)
self.textA = ""
self.textB = ""
self.flags = wx.FR_DOWN
def OnbtnPopUp(self, event):
eid = event.GetId()
if eid == self.ID_BTNA:
s = self.txtA.GetPosition()[0]
x = self.btnPopUpA.GetPosition()[0]
self.txtA.PopUp((x-s, 0))
elif eid == self.ID_BTNB:
s = self.txtB.GetPosition()[0]
x = self.btnPopUpB.GetPosition()[0]
self.txtB.PopUp((x-s, 0))
def OnbtnSwitch(self, event):
self.Show(0)
a = self.txtA.GetValue()
b = self.txtB.GetValue()
self.txtA.AppendToHistory(self.parent.SwitcherooHistory)
self.txtB.AppendToHistory(self.parent.SwitcherooHistory)
if self.chkCase.IsChecked():
self.flags = self.flags + wx.FR_MATCHCASE
if self.chkWholeWord.IsChecked():
self.flags = self.flags + wx.FR_WHOLEWORD
temp = "DRPYTHONTEMPSWITCHEROO"
if (len(a) > 0) and (len(b) > 0):
if self.chkInSelection.GetValue():
start = self.stc.GetSelectionStart()
end = self.stc.GetSelectionEnd()
self.stc.Finder.SetTargetRange(start, end)
x = self.stc.Finder.ReplaceAll(a, temp, self.flags)
end = end + ((len(temp) - len(a)) * x)
self.stc.Finder.SetTargetRange(start, end)
x = self.stc.Finder.ReplaceAll(b, a, self.flags)
end = end + ((len(a) - len(b)) * x)
self.stc.Finder.SetTargetRange(start, end)
self.stc.Finder.ReplaceAll(temp, b, self.flags)
else:
self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
self.stc.Finder.ReplaceAll(a, temp, self.flags)
self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
self.stc.Finder.ReplaceAll(b, a, self.flags)
self.stc.Finder.SetTargetRange(0, self.stc.GetTextLength())
self.stc.Finder.ReplaceAll(temp, b, self.flags)
self.Close(1)
def OnbtnCancel(self, event):
self.Close(1)
def Plugin(DrFrame):
def OnSwitcheroo(event):
d = drSwitcherooDialog(DrFrame, -1, "Switcheroo", DrFrame.GetActiveSTC())
d.Show()
ID_SWITCHEROO = DrFrame.GetNewId()
DrFrame.SwitcherooHistory = []
DrFrame.LoadPluginShortcuts('Switcheroo')
DrFrame.searchmenu.AppendSeparator()
DrFrame.searchmenu.Append(ID_SWITCHEROO, "Switcheroo...", " Switch all occurences of A with B, and B with A")
DrFrame.Bind(wx.EVT_MENU, OnSwitcheroo, id=ID_SWITCHEROO)
DrFrame.AddPluginFunction("Switcheroo", "Switcheroo", OnSwitcheroo)