Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: ReplaceDialog.py
from Tkinter import *
[0] Fix | Delete
[1] Fix | Delete
from idlelib import SearchEngine
[2] Fix | Delete
from idlelib.SearchDialogBase import SearchDialogBase
[3] Fix | Delete
import re
[4] Fix | Delete
[5] Fix | Delete
[6] Fix | Delete
def replace(text):
[7] Fix | Delete
root = text._root()
[8] Fix | Delete
engine = SearchEngine.get(root)
[9] Fix | Delete
if not hasattr(engine, "_replacedialog"):
[10] Fix | Delete
engine._replacedialog = ReplaceDialog(root, engine)
[11] Fix | Delete
dialog = engine._replacedialog
[12] Fix | Delete
dialog.open(text)
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
class ReplaceDialog(SearchDialogBase):
[16] Fix | Delete
[17] Fix | Delete
title = "Replace Dialog"
[18] Fix | Delete
icon = "Replace"
[19] Fix | Delete
[20] Fix | Delete
def __init__(self, root, engine):
[21] Fix | Delete
SearchDialogBase.__init__(self, root, engine)
[22] Fix | Delete
self.replvar = StringVar(root)
[23] Fix | Delete
[24] Fix | Delete
def open(self, text):
[25] Fix | Delete
SearchDialogBase.open(self, text)
[26] Fix | Delete
try:
[27] Fix | Delete
first = text.index("sel.first")
[28] Fix | Delete
except TclError:
[29] Fix | Delete
first = None
[30] Fix | Delete
try:
[31] Fix | Delete
last = text.index("sel.last")
[32] Fix | Delete
except TclError:
[33] Fix | Delete
last = None
[34] Fix | Delete
first = first or text.index("insert")
[35] Fix | Delete
last = last or first
[36] Fix | Delete
self.show_hit(first, last)
[37] Fix | Delete
self.ok = 1
[38] Fix | Delete
[39] Fix | Delete
def create_entries(self):
[40] Fix | Delete
SearchDialogBase.create_entries(self)
[41] Fix | Delete
self.replent = self.make_entry("Replace with:", self.replvar)[0]
[42] Fix | Delete
[43] Fix | Delete
def create_command_buttons(self):
[44] Fix | Delete
SearchDialogBase.create_command_buttons(self)
[45] Fix | Delete
self.make_button("Find", self.find_it)
[46] Fix | Delete
self.make_button("Replace", self.replace_it)
[47] Fix | Delete
self.make_button("Replace+Find", self.default_command, 1)
[48] Fix | Delete
self.make_button("Replace All", self.replace_all)
[49] Fix | Delete
[50] Fix | Delete
def find_it(self, event=None):
[51] Fix | Delete
self.do_find(0)
[52] Fix | Delete
[53] Fix | Delete
def replace_it(self, event=None):
[54] Fix | Delete
if self.do_find(self.ok):
[55] Fix | Delete
self.do_replace()
[56] Fix | Delete
[57] Fix | Delete
def default_command(self, event=None):
[58] Fix | Delete
if self.do_find(self.ok):
[59] Fix | Delete
if self.do_replace(): # Only find next match if replace succeeded.
[60] Fix | Delete
# A bad re can cause it to fail.
[61] Fix | Delete
self.do_find(0)
[62] Fix | Delete
[63] Fix | Delete
def _replace_expand(self, m, repl):
[64] Fix | Delete
""" Helper function for expanding a regular expression
[65] Fix | Delete
in the replace field, if needed. """
[66] Fix | Delete
if self.engine.isre():
[67] Fix | Delete
try:
[68] Fix | Delete
new = m.expand(repl)
[69] Fix | Delete
except re.error:
[70] Fix | Delete
self.engine.report_error(repl, 'Invalid Replace Expression')
[71] Fix | Delete
new = None
[72] Fix | Delete
else:
[73] Fix | Delete
new = repl
[74] Fix | Delete
return new
[75] Fix | Delete
[76] Fix | Delete
def replace_all(self, event=None):
[77] Fix | Delete
prog = self.engine.getprog()
[78] Fix | Delete
if not prog:
[79] Fix | Delete
return
[80] Fix | Delete
repl = self.replvar.get()
[81] Fix | Delete
text = self.text
[82] Fix | Delete
res = self.engine.search_text(text, prog)
[83] Fix | Delete
if not res:
[84] Fix | Delete
text.bell()
[85] Fix | Delete
return
[86] Fix | Delete
text.tag_remove("sel", "1.0", "end")
[87] Fix | Delete
text.tag_remove("hit", "1.0", "end")
[88] Fix | Delete
line = res[0]
[89] Fix | Delete
col = res[1].start()
[90] Fix | Delete
if self.engine.iswrap():
[91] Fix | Delete
line = 1
[92] Fix | Delete
col = 0
[93] Fix | Delete
ok = 1
[94] Fix | Delete
first = last = None
[95] Fix | Delete
# XXX ought to replace circular instead of top-to-bottom when wrapping
[96] Fix | Delete
text.undo_block_start()
[97] Fix | Delete
while 1:
[98] Fix | Delete
res = self.engine.search_forward(text, prog, line, col, 0, ok)
[99] Fix | Delete
if not res:
[100] Fix | Delete
break
[101] Fix | Delete
line, m = res
[102] Fix | Delete
chars = text.get("%d.0" % line, "%d.0" % (line+1))
[103] Fix | Delete
orig = m.group()
[104] Fix | Delete
new = self._replace_expand(m, repl)
[105] Fix | Delete
if new is None:
[106] Fix | Delete
break
[107] Fix | Delete
i, j = m.span()
[108] Fix | Delete
first = "%d.%d" % (line, i)
[109] Fix | Delete
last = "%d.%d" % (line, j)
[110] Fix | Delete
if new == orig:
[111] Fix | Delete
text.mark_set("insert", last)
[112] Fix | Delete
else:
[113] Fix | Delete
text.mark_set("insert", first)
[114] Fix | Delete
if first != last:
[115] Fix | Delete
text.delete(first, last)
[116] Fix | Delete
if new:
[117] Fix | Delete
text.insert(first, new)
[118] Fix | Delete
col = i + len(new)
[119] Fix | Delete
ok = 0
[120] Fix | Delete
text.undo_block_stop()
[121] Fix | Delete
if first and last:
[122] Fix | Delete
self.show_hit(first, last)
[123] Fix | Delete
self.close()
[124] Fix | Delete
[125] Fix | Delete
def do_find(self, ok=0):
[126] Fix | Delete
if not self.engine.getprog():
[127] Fix | Delete
return False
[128] Fix | Delete
text = self.text
[129] Fix | Delete
res = self.engine.search_text(text, None, ok)
[130] Fix | Delete
if not res:
[131] Fix | Delete
text.bell()
[132] Fix | Delete
return False
[133] Fix | Delete
line, m = res
[134] Fix | Delete
i, j = m.span()
[135] Fix | Delete
first = "%d.%d" % (line, i)
[136] Fix | Delete
last = "%d.%d" % (line, j)
[137] Fix | Delete
self.show_hit(first, last)
[138] Fix | Delete
self.ok = 1
[139] Fix | Delete
return True
[140] Fix | Delete
[141] Fix | Delete
def do_replace(self):
[142] Fix | Delete
prog = self.engine.getprog()
[143] Fix | Delete
if not prog:
[144] Fix | Delete
return False
[145] Fix | Delete
text = self.text
[146] Fix | Delete
try:
[147] Fix | Delete
first = pos = text.index("sel.first")
[148] Fix | Delete
last = text.index("sel.last")
[149] Fix | Delete
except TclError:
[150] Fix | Delete
pos = None
[151] Fix | Delete
if not pos:
[152] Fix | Delete
first = last = pos = text.index("insert")
[153] Fix | Delete
line, col = SearchEngine.get_line_col(pos)
[154] Fix | Delete
chars = text.get("%d.0" % line, "%d.0" % (line+1))
[155] Fix | Delete
m = prog.match(chars, col)
[156] Fix | Delete
if not prog:
[157] Fix | Delete
return False
[158] Fix | Delete
new = self._replace_expand(m, self.replvar.get())
[159] Fix | Delete
if new is None:
[160] Fix | Delete
return False
[161] Fix | Delete
text.mark_set("insert", first)
[162] Fix | Delete
text.undo_block_start()
[163] Fix | Delete
if m.group():
[164] Fix | Delete
text.delete(first, last)
[165] Fix | Delete
if new:
[166] Fix | Delete
text.insert(first, new)
[167] Fix | Delete
text.undo_block_stop()
[168] Fix | Delete
self.show_hit(first, text.index("insert"))
[169] Fix | Delete
self.ok = 0
[170] Fix | Delete
return True
[171] Fix | Delete
[172] Fix | Delete
def show_hit(self, first, last):
[173] Fix | Delete
text = self.text
[174] Fix | Delete
text.mark_set("insert", first)
[175] Fix | Delete
text.tag_remove("sel", "1.0", "end")
[176] Fix | Delete
text.tag_add("sel", first, last)
[177] Fix | Delete
text.tag_remove("hit", "1.0", "end")
[178] Fix | Delete
if first == last:
[179] Fix | Delete
text.tag_add("hit", first)
[180] Fix | Delete
else:
[181] Fix | Delete
text.tag_add("hit", first, last)
[182] Fix | Delete
text.see("insert")
[183] Fix | Delete
text.update_idletasks()
[184] Fix | Delete
[185] Fix | Delete
def close(self, event=None):
[186] Fix | Delete
SearchDialogBase.close(self, event)
[187] Fix | Delete
self.text.tag_remove("hit", "1.0", "end")
[188] Fix | Delete
[189] Fix | Delete
def _replace_dialog(parent):
[190] Fix | Delete
root = Tk()
[191] Fix | Delete
root.title("Test ReplaceDialog")
[192] Fix | Delete
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
[193] Fix | Delete
root.geometry("+%d+%d"%(x, y + 150))
[194] Fix | Delete
[195] Fix | Delete
# mock undo delegator methods
[196] Fix | Delete
def undo_block_start():
[197] Fix | Delete
pass
[198] Fix | Delete
[199] Fix | Delete
def undo_block_stop():
[200] Fix | Delete
pass
[201] Fix | Delete
[202] Fix | Delete
text = Text(root)
[203] Fix | Delete
text.undo_block_start = undo_block_start
[204] Fix | Delete
text.undo_block_stop = undo_block_stop
[205] Fix | Delete
text.pack()
[206] Fix | Delete
text.insert("insert","This is a sample string.\n"*10)
[207] Fix | Delete
[208] Fix | Delete
def show_replace():
[209] Fix | Delete
text.tag_add(SEL, "1.0", END)
[210] Fix | Delete
replace(text)
[211] Fix | Delete
text.tag_remove(SEL, "1.0", END)
[212] Fix | Delete
[213] Fix | Delete
button = Button(root, text="Replace", command=show_replace)
[214] Fix | Delete
button.pack()
[215] Fix | Delete
[216] Fix | Delete
if __name__ == '__main__':
[217] Fix | Delete
from idlelib.idle_test.htest import run
[218] Fix | Delete
run(_replace_dialog)
[219] Fix | Delete
[220] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function